| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Helpers\Report; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  | use FireflyIII\Helpers\Collection\Account as AccountCollection; | 
					
						
							| 
									
										
										
										
											2015-05-16 14:51:23 +02:00
										 |  |  | use FireflyIII\Helpers\Collection\Balance; | 
					
						
							|  |  |  | use FireflyIII\Helpers\Collection\BalanceEntry; | 
					
						
							|  |  |  | use FireflyIII\Helpers\Collection\BalanceHeader; | 
					
						
							|  |  |  | use FireflyIII\Helpers\Collection\BalanceLine; | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  | use FireflyIII\Helpers\Collection\Bill as BillCollection; | 
					
						
							|  |  |  | use FireflyIII\Helpers\Collection\BillLine; | 
					
						
							| 
									
										
										
										
											2015-05-16 13:53:08 +02:00
										 |  |  | use FireflyIII\Helpers\Collection\Budget as BudgetCollection; | 
					
						
							| 
									
										
										
										
											2015-05-16 14:14:22 +02:00
										 |  |  | use FireflyIII\Helpers\Collection\BudgetLine; | 
					
						
							| 
									
										
										
										
											2015-05-16 13:53:08 +02:00
										 |  |  | use FireflyIII\Helpers\Collection\Category as CategoryCollection; | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  | use FireflyIII\Helpers\Collection\Expense; | 
					
						
							|  |  |  | use FireflyIII\Helpers\Collection\Income; | 
					
						
							| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  | use FireflyIII\Models\Bill; | 
					
						
							| 
									
										
										
										
											2015-05-16 14:51:23 +02:00
										 |  |  | use FireflyIII\Models\Budget as BudgetModel; | 
					
						
							| 
									
										
										
										
											2015-05-16 14:14:22 +02:00
										 |  |  | use FireflyIII\Models\LimitRepetition; | 
					
						
							| 
									
										
										
										
											2015-12-06 13:11:43 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2015-12-06 13:17:00 +01:00
										 |  |  | use Steam; | 
					
						
							| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class ReportHelper | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Helpers\Report | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class ReportHelper implements ReportHelperInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |     /** @var ReportQueryInterface */ | 
					
						
							|  |  |  |     protected $query; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-05-22 10:17:20 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2015-05-23 08:51:24 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-05-17 09:18:44 +02:00
										 |  |  |      * @param ReportQueryInterface $query | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(ReportQueryInterface $query) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->query = $query; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |      * This method generates a full report for the given period on all | 
					
						
							|  |  |  |      * the users asset and cash accounts. | 
					
						
							| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon $date | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |      * @param Carbon $end | 
					
						
							|  |  |  |      * @param        $shared | 
					
						
							| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-05-26 12:08:46 +02:00
										 |  |  |      * @return AccountCollection | 
					
						
							| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |     public function getAccountReport(Carbon $date, Carbon $end, $shared) | 
					
						
							| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $accounts = $this->query->getAllAccounts($date, $end, $shared); | 
					
						
							| 
									
										
										
										
											2015-05-20 20:04:27 +02:00
										 |  |  |         // remove cash account, if any:
 | 
					
						
							| 
									
										
										
										
											2015-05-23 08:51:24 +02:00
										 |  |  |         $accounts = $accounts->filter( | 
					
						
							| 
									
										
										
										
											2015-06-06 23:09:12 +02:00
										 |  |  |             function (Account $account) { | 
					
						
							| 
									
										
										
										
											2015-05-23 08:51:24 +02:00
										 |  |  |                 if ($account->accountType->type != 'Cash account') { | 
					
						
							|  |  |  |                     return $account; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2015-06-03 21:25:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 08:17:58 +02:00
										 |  |  |                 return null; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-05-23 08:51:24 +02:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-12-06 13:17:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 13:11:43 +01:00
										 |  |  |         return $this->getAccountReportForList($date, $end, $accounts); | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 14:51:23 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * The balance report contains a Balance object which in turn contains: | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * A BalanceHeader object which contains all relevant user asset accounts for the report. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * A number of BalanceLine objects, which hold: | 
					
						
							|  |  |  |      * - A budget | 
					
						
							|  |  |  |      * - A number of BalanceEntry objects. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * The BalanceEntry object holds: | 
					
						
							|  |  |  |      *   - The same budget (again) | 
					
						
							|  |  |  |      *   - A user asset account as mentioned in the BalanceHeader | 
					
						
							|  |  |  |      *   - The amount of money spent on the budget by the user asset account | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon  $start | 
					
						
							|  |  |  |      * @param Carbon  $end | 
					
						
							|  |  |  |      * @param boolean $shared | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Balance | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getBalanceReport(Carbon $start, Carbon $end, $shared) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-07 19:09:45 +02:00
										 |  |  |         $repository    = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); | 
					
						
							|  |  |  |         $tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface'); | 
					
						
							| 
									
										
										
										
											2015-05-17 12:49:09 +02:00
										 |  |  |         $balance       = new Balance; | 
					
						
							| 
									
										
										
										
											2015-05-16 14:51:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // build a balance header:
 | 
					
						
							|  |  |  |         $header = new BalanceHeader; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $accounts = $this->query->getAllAccounts($start, $end, $shared); | 
					
						
							|  |  |  |         $budgets  = $repository->getBudgets(); | 
					
						
							|  |  |  |         foreach ($accounts as $account) { | 
					
						
							|  |  |  |             $header->addAccount($account); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 15:43:58 +02:00
										 |  |  |         /** @var BudgetModel $budget */ | 
					
						
							| 
									
										
										
										
											2015-05-16 14:51:23 +02:00
										 |  |  |         foreach ($budgets as $budget) { | 
					
						
							|  |  |  |             $line = new BalanceLine; | 
					
						
							|  |  |  |             $line->setBudget($budget); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 15:43:58 +02:00
										 |  |  |             // get budget amount for current period:
 | 
					
						
							| 
									
										
										
										
											2015-09-28 20:41:53 +02:00
										 |  |  |             $rep = $repository->getCurrentRepetition($budget, $start, $end); | 
					
						
							| 
									
										
										
										
											2015-12-02 08:44:23 +01:00
										 |  |  |             // could be null?
 | 
					
						
							| 
									
										
										
										
											2015-05-17 12:49:09 +02:00
										 |  |  |             $line->setRepetition($rep); | 
					
						
							| 
									
										
										
										
											2015-05-16 15:43:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 14:51:23 +02:00
										 |  |  |             // loop accounts:
 | 
					
						
							|  |  |  |             foreach ($accounts as $account) { | 
					
						
							|  |  |  |                 $balanceEntry = new BalanceEntry; | 
					
						
							|  |  |  |                 $balanceEntry->setAccount($account); | 
					
						
							| 
									
										
										
										
											2015-05-16 15:43:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 // get spent:
 | 
					
						
							| 
									
										
										
										
											2015-05-20 07:07:46 +02:00
										 |  |  |                 $spent = $this->query->spentInBudgetCorrected($account, $budget, $start, $end); // I think shared is irrelevant.
 | 
					
						
							| 
									
										
										
										
											2015-05-16 15:43:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $balanceEntry->setSpent($spent); | 
					
						
							| 
									
										
										
										
											2015-05-16 14:51:23 +02:00
										 |  |  |                 $line->addBalanceEntry($balanceEntry); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // add line to balance:
 | 
					
						
							|  |  |  |             $balance->addBalanceLine($line); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 15:43:58 +02:00
										 |  |  |         // then a new line for without budget.
 | 
					
						
							| 
									
										
										
										
											2015-05-17 12:49:09 +02:00
										 |  |  |         // and one for the tags:
 | 
					
						
							| 
									
										
										
										
											2015-12-02 08:46:03 +01:00
										 |  |  |         // and one for "left unbalanced".
 | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  |         $empty    = new BalanceLine; | 
					
						
							|  |  |  |         $tags     = new BalanceLine; | 
					
						
							| 
									
										
										
										
											2015-05-17 12:49:09 +02:00
										 |  |  |         $diffLine = new BalanceLine; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $tags->setRole(BalanceLine::ROLE_TAGROLE); | 
					
						
							|  |  |  |         $diffLine->setRole(BalanceLine::ROLE_DIFFROLE); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 16:47:52 +02:00
										 |  |  |         foreach ($accounts as $account) { | 
					
						
							| 
									
										
										
										
											2015-12-02 08:46:03 +01:00
										 |  |  |             $spent = $this->query->spentNoBudget($account, $start, $end) * -1; | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  |             $left  = $tagRepository->coveredByBalancingActs($account, $start, $end); | 
					
						
							| 
									
										
										
										
											2015-06-13 10:02:36 +02:00
										 |  |  |             bcscale(2); | 
					
						
							| 
									
										
										
										
											2015-06-16 18:34:19 +02:00
										 |  |  |             $diff = bcsub($spent, $left); | 
					
						
							| 
									
										
										
										
											2015-05-17 12:49:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // budget
 | 
					
						
							|  |  |  |             $budgetEntry = new BalanceEntry; | 
					
						
							|  |  |  |             $budgetEntry->setAccount($account); | 
					
						
							|  |  |  |             $budgetEntry->setSpent($spent); | 
					
						
							|  |  |  |             $empty->addBalanceEntry($budgetEntry); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // balanced by tags
 | 
					
						
							|  |  |  |             $tagEntry = new BalanceEntry; | 
					
						
							|  |  |  |             $tagEntry->setAccount($account); | 
					
						
							|  |  |  |             $tagEntry->setLeft($left); | 
					
						
							|  |  |  |             $tags->addBalanceEntry($tagEntry); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // difference:
 | 
					
						
							|  |  |  |             $diffEntry = new BalanceEntry; | 
					
						
							|  |  |  |             $diffEntry->setAccount($account); | 
					
						
							|  |  |  |             $diffEntry->setSpent($diff); | 
					
						
							|  |  |  |             $diffLine->addBalanceEntry($diffEntry); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 16:47:52 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-05-17 12:49:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 16:47:52 +02:00
										 |  |  |         $balance->addBalanceLine($empty); | 
					
						
							| 
									
										
										
										
											2015-05-17 12:49:09 +02:00
										 |  |  |         $balance->addBalanceLine($tags); | 
					
						
							|  |  |  |         $balance->addBalanceLine($diffLine); | 
					
						
							| 
									
										
										
										
											2015-05-16 15:43:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 14:51:23 +02:00
										 |  |  |         $balance->setBalanceHeader($header); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $balance; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * This method generates a full report for the given period on all | 
					
						
							|  |  |  |      * the users bills and their payments. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-07-07 19:09:45 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return BillCollection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-06 22:23:34 +02:00
										 |  |  |     public function getBillReport(Carbon $start, Carbon $end) | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var \FireflyIII\Repositories\Bill\BillRepositoryInterface $repository */ | 
					
						
							| 
									
										
										
										
											2015-07-07 19:09:45 +02:00
										 |  |  |         $repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface'); | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  |         $bills      = $repository->getBills(); | 
					
						
							|  |  |  |         $collection = new BillCollection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Bill $bill */ | 
					
						
							|  |  |  |         foreach ($bills as $bill) { | 
					
						
							|  |  |  |             $billLine = new BillLine; | 
					
						
							|  |  |  |             $billLine->setBill($bill); | 
					
						
							|  |  |  |             $billLine->setActive(intval($bill->active) == 1); | 
					
						
							| 
									
										
										
										
											2015-06-14 08:22:02 +02:00
										 |  |  |             $billLine->setMin($bill->amount_min); | 
					
						
							|  |  |  |             $billLine->setMax($bill->amount_max); | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // is hit in period?
 | 
					
						
							| 
									
										
										
										
											2015-06-14 08:22:02 +02:00
										 |  |  |             bcscale(2); | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  |             $set = $repository->getJournalsInRange($bill, $start, $end); | 
					
						
							|  |  |  |             if ($set->count() == 0) { | 
					
						
							|  |  |  |                 $billLine->setHit(false); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $billLine->setHit(true); | 
					
						
							| 
									
										
										
										
											2015-06-14 08:22:02 +02:00
										 |  |  |                 $amount = '0'; | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  |                 foreach ($set as $entry) { | 
					
						
							| 
									
										
										
										
											2015-06-14 08:22:02 +02:00
										 |  |  |                     $amount = bcadd($amount, $entry->amount); | 
					
						
							| 
									
										
										
										
											2015-05-17 17:54:13 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 $billLine->setAmount($amount); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $collection->addBill($billLine); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 13:53:08 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon  $start | 
					
						
							|  |  |  |      * @param Carbon  $end | 
					
						
							|  |  |  |      * @param boolean $shared | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return BudgetCollection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getBudgetReport(Carbon $start, Carbon $end, $shared) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-16 14:14:22 +02:00
										 |  |  |         $object = new BudgetCollection; | 
					
						
							|  |  |  |         /** @var \FireflyIII\Repositories\Budget\BudgetRepositoryInterface $repository */ | 
					
						
							| 
									
										
										
										
											2015-07-07 19:09:45 +02:00
										 |  |  |         $repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); | 
					
						
							| 
									
										
										
										
											2015-05-16 14:14:22 +02:00
										 |  |  |         $set        = $repository->getBudgets(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 19:42:28 +02:00
										 |  |  |         bcscale(2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 14:14:22 +02:00
										 |  |  |         foreach ($set as $budget) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $repetitions = $repository->getBudgetLimitRepetitions($budget, $start, $end); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // no repetition(s) for this budget:
 | 
					
						
							|  |  |  |             if ($repetitions->count() == 0) { | 
					
						
							| 
									
										
										
										
											2015-08-02 07:04:43 +02:00
										 |  |  |                 $spent      = $repository->balanceInPeriod($budget, $start, $end, $shared); | 
					
						
							| 
									
										
										
										
											2015-05-16 14:14:22 +02:00
										 |  |  |                 $budgetLine = new BudgetLine; | 
					
						
							|  |  |  |                 $budgetLine->setBudget($budget); | 
					
						
							|  |  |  |                 $budgetLine->setOverspent($spent); | 
					
						
							|  |  |  |                 $object->addOverspent($spent); | 
					
						
							|  |  |  |                 $object->addBudgetLine($budgetLine); | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // one or more repetitions for budget:
 | 
					
						
							|  |  |  |             /** @var LimitRepetition $repetition */ | 
					
						
							|  |  |  |             foreach ($repetitions as $repetition) { | 
					
						
							|  |  |  |                 $budgetLine = new BudgetLine; | 
					
						
							|  |  |  |                 $budgetLine->setBudget($budget); | 
					
						
							|  |  |  |                 $budgetLine->setRepetition($repetition); | 
					
						
							| 
									
										
										
										
											2015-08-02 07:04:43 +02:00
										 |  |  |                 $expenses  = $repository->balanceInPeriod($budget, $repetition->startdate, $repetition->enddate, $shared); | 
					
						
							| 
									
										
										
										
											2015-12-04 06:55:54 +01:00
										 |  |  |                 $expenses  = $expenses * -1; | 
					
						
							| 
									
										
										
										
											2015-07-26 19:42:28 +02:00
										 |  |  |                 $left      = $expenses < $repetition->amount ? bcsub($repetition->amount, $expenses) : 0; | 
					
						
							| 
									
										
										
										
											2015-07-26 19:07:02 +02:00
										 |  |  |                 $spent     = $expenses > $repetition->amount ? 0 : $expenses; | 
					
						
							| 
									
										
										
										
											2015-07-26 19:42:28 +02:00
										 |  |  |                 $overspent = $expenses > $repetition->amount ? bcsub($expenses, $repetition->amount) : 0; | 
					
						
							| 
									
										
										
										
											2015-05-16 14:14:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $budgetLine->setLeft($left); | 
					
						
							|  |  |  |                 $budgetLine->setSpent($spent); | 
					
						
							|  |  |  |                 $budgetLine->setOverspent($overspent); | 
					
						
							|  |  |  |                 $budgetLine->setBudgeted($repetition->amount); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $object->addBudgeted($repetition->amount); | 
					
						
							|  |  |  |                 $object->addSpent($spent); | 
					
						
							|  |  |  |                 $object->addLeft($left); | 
					
						
							|  |  |  |                 $object->addOverspent($overspent); | 
					
						
							|  |  |  |                 $object->addBudgetLine($budgetLine); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // stuff outside of budgets:
 | 
					
						
							|  |  |  |         $noBudget   = $repository->getWithoutBudgetSum($start, $end); | 
					
						
							|  |  |  |         $budgetLine = new BudgetLine; | 
					
						
							|  |  |  |         $budgetLine->setOverspent($noBudget); | 
					
						
							| 
									
										
										
										
											2015-12-02 08:35:15 +01:00
										 |  |  |         $budgetLine->setSpent($noBudget); | 
					
						
							| 
									
										
										
										
											2015-05-16 14:14:22 +02:00
										 |  |  |         $object->addOverspent($noBudget); | 
					
						
							|  |  |  |         $object->addBudgetLine($budgetLine); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $object; | 
					
						
							| 
									
										
										
										
											2015-05-16 13:53:08 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon  $start | 
					
						
							|  |  |  |      * @param Carbon  $end | 
					
						
							|  |  |  |      * @param boolean $shared | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return CategoryCollection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getCategoryReport(Carbon $start, Carbon $end, $shared) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-16 14:51:23 +02:00
										 |  |  |         $object = new CategoryCollection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** | 
					
						
							|  |  |  |          * GET CATEGORIES: | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         /** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */ | 
					
						
							| 
									
										
										
										
											2015-07-07 19:09:45 +02:00
										 |  |  |         $repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); | 
					
						
							| 
									
										
										
										
											2015-05-16 14:51:23 +02:00
										 |  |  |         $set        = $repository->getCategories(); | 
					
						
							|  |  |  |         foreach ($set as $category) { | 
					
						
							| 
									
										
										
										
											2015-12-04 06:55:54 +01:00
										 |  |  |             $spent           = $repository->balanceInPeriod($category, $start, $end, $shared); | 
					
						
							| 
									
										
										
										
											2015-05-16 14:51:23 +02:00
										 |  |  |             $category->spent = $spent; | 
					
						
							|  |  |  |             $object->addCategory($category); | 
					
						
							|  |  |  |             $object->addTotal($spent); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $object; | 
					
						
							| 
									
										
										
										
											2015-05-16 13:53:08 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get a full report on the users expenses during the period. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon  $start | 
					
						
							|  |  |  |      * @param Carbon  $end | 
					
						
							|  |  |  |      * @param boolean $shared | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Expense | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getExpenseReport($start, $end, $shared) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $object = new Expense; | 
					
						
							| 
									
										
										
										
											2015-05-20 07:20:02 +02:00
										 |  |  |         $set    = $this->query->expenseInPeriodCorrected($start, $end, $shared); | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |         foreach ($set as $entry) { | 
					
						
							| 
									
										
										
										
											2015-09-25 20:40:24 +02:00
										 |  |  |             $object->addToTotal($entry->amount_positive); | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |             $object->addOrCreateExpense($entry); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $object; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a full report on the users incomes during the period. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon  $start | 
					
						
							|  |  |  |      * @param Carbon  $end | 
					
						
							|  |  |  |      * @param boolean $shared | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Income | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getIncomeReport($start, $end, $shared) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $object = new Income; | 
					
						
							| 
									
										
										
										
											2015-05-20 07:20:02 +02:00
										 |  |  |         $set    = $this->query->incomeInPeriodCorrected($start, $end, $shared); | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |         foreach ($set as $entry) { | 
					
						
							| 
									
										
										
										
											2015-09-25 20:40:24 +02:00
										 |  |  |             $object->addToTotal($entry->amount_positive); | 
					
						
							| 
									
										
										
										
											2015-05-16 13:06:38 +02:00
										 |  |  |             $object->addOrCreateIncome($entry); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $object; | 
					
						
							| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon $date | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function listOfMonths(Carbon $date) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-14 13:41:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  |         $start  = clone $date; | 
					
						
							|  |  |  |         $end    = Carbon::now(); | 
					
						
							|  |  |  |         $months = []; | 
					
						
							|  |  |  |         while ($start <= $end) { | 
					
						
							| 
									
										
										
										
											2015-12-04 06:55:54 +01:00
										 |  |  |             $year = $start->year; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!isset($months[$year])) { | 
					
						
							|  |  |  |                 $months[$year] = [ | 
					
						
							|  |  |  |                     'start'  => Carbon::createFromDate($year, 1, 1)->format('Y-m-d'), | 
					
						
							|  |  |  |                     'end'    => Carbon::createFromDate($year, 12, 31)->format('Y-m-d'), | 
					
						
							|  |  |  |                     'months' => [], | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $currentEnd = clone $start; | 
					
						
							|  |  |  |             $currentEnd->endOfMonth(); | 
					
						
							|  |  |  |             $months[$year]['months'][] = [ | 
					
						
							| 
									
										
										
										
											2015-05-14 13:41:21 +02:00
										 |  |  |                 'formatted' => $start->formatLocalized('%B %Y'), | 
					
						
							| 
									
										
										
										
											2015-12-04 06:55:54 +01:00
										 |  |  |                 'start'     => $start->format('Y-m-d'), | 
					
						
							|  |  |  |                 'end'       => $currentEnd->format('Y-m-d'), | 
					
						
							| 
									
										
										
										
											2015-05-05 10:30:39 +02:00
										 |  |  |                 'month'     => $start->month, | 
					
						
							|  |  |  |                 'year'      => $year, | 
					
						
							| 
									
										
										
										
											2015-02-23 20:25:48 +01:00
										 |  |  |             ]; | 
					
						
							|  |  |  |             $start->addMonth(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $months; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-06 13:11:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * This method generates a full report for the given period on all | 
					
						
							|  |  |  |      * given accounts | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-12-06 13:17:00 +01:00
										 |  |  |      * @param Carbon     $start | 
					
						
							| 
									
										
										
										
											2015-12-06 13:11:43 +01:00
										 |  |  |      * @param Carbon     $end | 
					
						
							|  |  |  |      * @param Collection $accounts | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return AccountCollection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-12-06 13:17:00 +01:00
										 |  |  |     public function getAccountReportForList(Carbon $start, Carbon $end, Collection $accounts) | 
					
						
							| 
									
										
										
										
											2015-12-06 13:11:43 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-06 13:17:00 +01:00
										 |  |  |         $startAmount = '0'; | 
					
						
							|  |  |  |         $endAmount   = '0'; | 
					
						
							|  |  |  |         $diff        = '0'; | 
					
						
							| 
									
										
										
										
											2015-12-06 13:11:43 +01:00
										 |  |  |         bcscale(2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 13:17:00 +01:00
										 |  |  |         $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); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 13:11:43 +01:00
										 |  |  |         // summarize:
 | 
					
						
							|  |  |  |         foreach ($accounts as $account) { | 
					
						
							| 
									
										
										
										
											2015-12-06 13:17:00 +01:00
										 |  |  |             $startAmount = bcadd($startAmount, $account->startBalance); | 
					
						
							|  |  |  |             $endAmount   = bcadd($endAmount, $account->endBalance); | 
					
						
							|  |  |  |             $diff        = bcadd($diff, bcsub($account->endBalance, $account->startBalance)); | 
					
						
							| 
									
										
										
										
											2015-12-06 13:11:43 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $object = new AccountCollection; | 
					
						
							| 
									
										
										
										
											2015-12-06 13:17:00 +01:00
										 |  |  |         $object->setStart($startAmount); | 
					
						
							|  |  |  |         $object->setEnd($endAmount); | 
					
						
							| 
									
										
										
										
											2015-12-06 13:11:43 +01:00
										 |  |  |         $object->setDifference($diff); | 
					
						
							|  |  |  |         $object->setAccounts($accounts); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $object; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |