![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/ledger.corals.io/Corals/modules/Ledger/Classes/ |
<?php namespace Corals\Modules\Ledger\Classes; use Corals\Modules\Ledger\Classes\Scopes\CreatedAtBetweenScope; use Corals\Modules\Ledger\Models\Account; use Corals\Modules\Ledger\Models\Item; use Corals\Modules\Ledger\Models\Record; use Illuminate\Support\Arr; use Illuminate\Support\Collection; class Ledger { protected $start_date; protected $end_date; protected $currencyCode; public function __construct() { $this->start_date = request()->get('start_date', now()->startOfMonth()); $this->end_date = request()->get('end_date', now()->endOfMonth()); $this->currencyCode = request()->get('currency', current(array_keys(config('ledger.supported_currencies')))); } public function getAccounts(): Collection { return Account::query()->pluck('name', 'id'); } public function getItems(): Collection { return Item::query()->pluck('name', 'id'); } public function getTotalAmountOfRecords($account_id, $isOverall = false) { $available_currencies = config('ledger.supported_currencies'); if ($this->currencyCode) { $available_currencies = Arr::only($available_currencies, $this->currencyCode); } $total = []; foreach ($available_currencies as $currencyCode => $currency) { $creditRecords = Record::query() ->confirmed() ->where('account_id', '=', $account_id) ->where('type', '=', 'credit') ->where('currency', '=', $currencyCode); $debitRecords = Record::query() ->confirmed() ->where('account_id', '=', $account_id) ->where('type', '=', 'debit') ->where('currency', '=', $currencyCode); if (!$isOverall) { (new CreatedAtBetweenScope('ledger_records', 'record_date')) ->apply($creditRecords, $this->start_date, $this->end_date); } $creditRecordsTotal = $creditRecords->sum('amount'); if (!$isOverall) { (new CreatedAtBetweenScope('ledger_records', 'record_date')) ->apply($debitRecords, $this->start_date, $this->end_date); } $debitRecordsTotal = $debitRecords->sum('amount'); // if ($creditRecordsTotal - $debitRecordsTotal == 0) { // continue; // } $total[$currencyCode] = sprintf('Credit: %s <br/> Debit: -%s = %s %s', round($creditRecordsTotal, 2), round($debitRecordsTotal, 2), round($creditRecordsTotal - $debitRecordsTotal, 2), $currencyCode); } return $total; } public function getTotalForSpecificDate($account_id, $currencyCode = null, $date = null) { if (!$date) { $date = $this->start_date; } if (!$currencyCode) { $currencyCode = $this->currencyCode; } $creditRecordsTotal = Record::query() ->confirmed() ->where('account_id', '=', $account_id) ->where('type', '=', 'credit') ->where('currency', '=', $currencyCode) ->where('record_date', '<', $date)->sum('amount'); $debitRecordsTotal = Record::query() ->confirmed() ->where('account_id', '=', $account_id) ->where('type', '=', 'debit') ->where('currency', '=', $currencyCode) ->where('record_date', '<', $date)->sum('amount'); return round($creditRecordsTotal - $debitRecordsTotal, 2); } public function getRecordsBetweenTwoDates($account_id) { $records = Record::query()->confirmed()->where('account_id', '=', $account_id); (new CreatedAtBetweenScope('ledger_records', 'record_date')) ->apply($records, $this->start_date, $this->end_date); $records->where('currency', $this->currencyCode); $balanceBeforeSelectedPeriod = $this->getTotalForSpecificDate($account_id); return $records->orderBy('record_date')->get() ->map(function (Record $record) use (&$balanceBeforeSelectedPeriod) { $balanceBeforeSelectedPeriod += $record->type == 'credit' ? $record->amount : (-1 * $record->amount); $record->accumulative_amount = sprintf("%s (%s)", $balanceBeforeSelectedPeriod, $record->currency); return $record; }); } }