/**
  * This method generates a full report for the given period on all
  * the users asset and cash accounts.
  *
  * @param Carbon $date
  * @param Carbon $end
  * @param        $shared
  *
  * @return AccountCollection
  */
 public function getAccountReport(Carbon $date, Carbon $end, $shared)
 {
     $accounts = $this->query->getAllAccounts($date, $end, $shared);
     $start = '0';
     $end = '0';
     $diff = '0';
     bcscale(2);
     // remove cash account, if any:
     $accounts = $accounts->filter(function (Account $account) {
         if ($account->accountType->type != 'Cash account') {
             return $account;
         }
         return null;
     });
     // summarize:
     foreach ($accounts as $account) {
         $start = bcadd($start, $account->startBalance);
         $end = bcadd($end, $account->endBalance);
         $diff = bcadd($diff, bcsub($account->endBalance, $account->startBalance));
     }
     $object = new AccountCollection();
     $object->setStart($start);
     $object->setEnd($end);
     $object->setDifference($diff);
     $object->setAccounts($accounts);
     return $object;
 }
 /**
  * This method generates a full report for the given period on all
  * given accounts
  *
  * @param Carbon     $start
  * @param Carbon     $end
  * @param Collection $accounts
  *
  * @return AccountCollection
  */
 public function getAccountReport(Carbon $start, Carbon $end, Collection $accounts)
 {
     $startAmount = '0';
     $endAmount = '0';
     $diff = '0';
     $ids = $accounts->pluck('id')->toArray();
     $yesterday = clone $start;
     $yesterday->subDay();
     bcscale(2);
     // get balances for start.
     $startSet = Account::leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->whereIn('accounts.id', $ids)->whereNull('transaction_journals.deleted_at')->whereNull('transactions.deleted_at')->where('transaction_journals.date', '<=', $yesterday->format('Y-m-d'))->groupBy('accounts.id')->get(['accounts.id', DB::Raw('SUM(`transactions`.`amount`) as `balance`')]);
     // and end:
     $endSet = Account::leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->whereIn('accounts.id', $ids)->whereNull('transaction_journals.deleted_at')->whereNull('transactions.deleted_at')->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->groupBy('accounts.id')->get(['accounts.id', DB::Raw('SUM(`transactions`.`amount`) as `balance`')]);
     $accounts->each(function (Account $account) use($startSet, $endSet) {
         /**
          * The balance for today always incorporates transactions
          * made on today. So to get todays "start" balance, we sub one
          * day.
          */
         //
         $currentStart = $startSet->filter(function (Account $entry) use($account) {
             return $account->id == $entry->id;
         });
         if ($currentStart->first()) {
             $account->startBalance = $currentStart->first()->balance;
         }
         $currentEnd = $endSet->filter(function (Account $entry) use($account) {
             return $account->id == $entry->id;
         });
         if ($currentEnd->first()) {
             $account->endBalance = $currentEnd->first()->balance;
         }
     });
     // summarize:
     foreach ($accounts as $account) {
         $startAmount = bcadd($startAmount, $account->startBalance);
         $endAmount = bcadd($endAmount, $account->endBalance);
         $diff = bcadd($diff, bcsub($account->endBalance, $account->startBalance));
     }
     $object = new AccountCollection();
     $object->setStart($startAmount);
     $object->setEnd($endAmount);
     $object->setDifference($diff);
     $object->setAccounts($accounts);
     return $object;
 }
 /**
  * This method generates a full report for the given period on all
  * given accounts.
  *
  * a special consideration for accounts that did exist on this exact day.
  * we also grab the balance from today just in case, to see if that changes things.
  * it's a fall back for users who (rightly so) start keeping score at the first of
  * the month and find the first report lacking / broken.
  *
  * @param Carbon     $start
  * @param Carbon     $end
  * @param Collection $accounts
  *
  * @return AccountCollection
  */
 public function getAccountReport(Carbon $start, Carbon $end, Collection $accounts) : AccountCollection
 {
     $startAmount = '0';
     $endAmount = '0';
     $diff = '0';
     $ids = $accounts->pluck('id')->toArray();
     $yesterday = clone $start;
     $yesterday->subDay();
     $startSet = $this->getSet($ids, $yesterday);
     // get balances for start.
     $backupSet = $this->getSet($ids, $start);
     $endSet = $this->getSet($ids, $end);
     $accounts->each(function (Account $account) use($startSet, $endSet, $backupSet) {
         return self::reportFilter($account, $startSet, $endSet, $backupSet);
     });
     // summarize:
     foreach ($accounts as $account) {
         $startAmount = bcadd($startAmount, $account->startBalance);
         $endAmount = bcadd($endAmount, $account->endBalance);
         $diff = bcadd($diff, bcsub($account->endBalance, $account->startBalance));
     }
     $object = new AccountCollection();
     $object->setStart($startAmount);
     $object->setEnd($endAmount);
     $object->setDifference($diff);
     $object->setAccounts($accounts);
     return $object;
 }
Example #4
0
 /**
  * This method generates a full report for the given period on all
  * given accounts
  *
  * @param Carbon     $start
  * @param Carbon     $end
  * @param Collection $accounts
  *
  * @return AccountCollection
  */
 public function getAccountReport(Carbon $start, Carbon $end, Collection $accounts)
 {
     $startAmount = '0';
     $endAmount = '0';
     $diff = '0';
     bcscale(2);
     $accounts->each(function (Account $account) use($start, $end) {
         /**
          * The balance for today always incorporates transactions
          * made on today. So to get todays "start" balance, we sub one
          * day.
          */
         $yesterday = clone $start;
         $yesterday->subDay();
         /** @noinspection PhpParamsInspection */
         $account->startBalance = Steam::balance($account, $yesterday);
         $account->endBalance = Steam::balance($account, $end);
     });
     // summarize:
     foreach ($accounts as $account) {
         $startAmount = bcadd($startAmount, $account->startBalance);
         $endAmount = bcadd($endAmount, $account->endBalance);
         $diff = bcadd($diff, bcsub($account->endBalance, $account->startBalance));
     }
     $object = new AccountCollection();
     $object->setStart($startAmount);
     $object->setEnd($endAmount);
     $object->setDifference($diff);
     $object->setAccounts($accounts);
     return $object;
 }