Reflect next recurrings to reports
This commit is contained in:
		| @@ -59,14 +59,17 @@ class ExpenseSummary extends Controller | ||||
|         switch ($status) { | ||||
|             case 'paid': | ||||
|                 $bills = BillPayment::monthsOfYear('paid_at')->get(); | ||||
|                 $this->setRecurring($bills, 'paid_at'); | ||||
|                 $this->setAmount($expenses_graph, $totals, $expenses, $bills, 'bill', 'paid_at'); | ||||
|                 break; | ||||
|             case 'upcoming': | ||||
|                 $bills = Bill::accrued()->monthsOfYear('due_at')->get(); | ||||
|                 $this->setRecurring($bills, 'due_at'); | ||||
|                 $this->setAmount($expenses_graph, $totals, $expenses, $bills, 'bill', 'due_at'); | ||||
|                 break; | ||||
|             default: | ||||
|                 $bills = Bill::accrued()->monthsOfYear('billed_at')->get(); | ||||
|                 $this->setRecurring($bills, 'billed_at'); | ||||
|                 $this->setAmount($expenses_graph, $totals, $expenses, $bills, 'bill', 'billed_at'); | ||||
|                 break; | ||||
|         } | ||||
| @@ -98,6 +101,45 @@ class ExpenseSummary extends Controller | ||||
|         return view($view_template, compact('chart', 'dates', 'categories', 'expenses', 'totals')); | ||||
|     } | ||||
|  | ||||
|     private function setRecurring(&$bills, $date) | ||||
|     { | ||||
|         foreach ($bills as $bill) { | ||||
|             if ($bill['table'] == 'bill_payments') { | ||||
|                 $item  = $bill->bill; | ||||
|                 $item->category_id = $bill->category_id; | ||||
|  | ||||
|                 $bill = $item; | ||||
|             } | ||||
|  | ||||
|             if (!empty($bill->parent_id)) { | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             if ($bill->recurring) { | ||||
|                 $recurred_bills = Bill::where('parent_id', $bill->id)->accrued()->monthsOfYear($date)->get(); | ||||
|  | ||||
|                 foreach ($bill->recurring->schedule() as $recurr) { | ||||
|                     if ($recurred_bills->count() > $recurr->getIndex()) { | ||||
|                         continue; | ||||
|                     } | ||||
|  | ||||
|                     if ($recurr->getStart()->format('Y') != Date::now()->format('Y')) { | ||||
|                         continue; | ||||
|                     } | ||||
|  | ||||
|                     $recurr_bill = clone $bill; | ||||
|  | ||||
|                     $recurr_bill->parent_id = $bill->id; | ||||
|                     $recurr_bill->created_at = $recurr->getStart()->format('Y-m-d'); | ||||
|                     $recurr_bill->invoiced_at = $recurr->getStart()->format('Y-m-d'); | ||||
|                     $recurr_bill->due_at = $recurr->getEnd()->format('Y-m-d'); | ||||
|  | ||||
|                     $bills->push($recurr_bill); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private function setAmount(&$graph, &$totals, &$expenses, $items, $type, $date_field) | ||||
|     { | ||||
|         foreach ($items as $item) { | ||||
|   | ||||
| @@ -74,14 +74,17 @@ class IncomeExpenseSummary extends Controller | ||||
|         switch ($status) { | ||||
|             case 'paid': | ||||
|                 $invoices = InvoicePayment::monthsOfYear('paid_at')->get(); | ||||
|                 $this->setRecurring($invoices, 'paid_at', 'invoice'); | ||||
|                 $this->setAmount($profit_graph, $totals, $compares, $invoices, 'invoice', 'paid_at'); | ||||
|                 break; | ||||
|             case 'upcoming': | ||||
|                 $invoices = Invoice::accrued()->monthsOfYear('due_at')->get(); | ||||
|                 $this->setRecurring($invoices, 'due_at', 'invoice'); | ||||
|                 $this->setAmount($profit_graph, $totals, $compares, $invoices, 'invoice', 'due_at'); | ||||
|                 break; | ||||
|             default: | ||||
|                 $invoices = Invoice::accrued()->monthsOfYear('invoiced_at')->get(); | ||||
|                 $this->setRecurring($invoices, 'invoiced_at', 'invoice'); | ||||
|                 $this->setAmount($profit_graph, $totals, $compares, $invoices, 'invoice', 'invoiced_at'); | ||||
|                 break; | ||||
|         } | ||||
| @@ -96,14 +99,17 @@ class IncomeExpenseSummary extends Controller | ||||
|         switch ($status) { | ||||
|             case 'paid': | ||||
|                 $bills = BillPayment::monthsOfYear('paid_at')->get(); | ||||
|                 $this->setRecurring($bills, 'paid_at', 'bill'); | ||||
|                 $this->setAmount($profit_graph, $totals, $compares, $bills, 'bill', 'paid_at'); | ||||
|                 break; | ||||
|             case 'upcoming': | ||||
|                 $bills = Bill::accrued()->monthsOfYear('due_at')->get(); | ||||
|                 $this->setRecurring($bills, 'due_at', 'bill'); | ||||
|                 $this->setAmount($profit_graph, $totals, $compares, $bills, 'bill', 'due_at'); | ||||
|                 break; | ||||
|             default: | ||||
|                 $bills = Bill::accrued()->monthsOfYear('billed_at')->get(); | ||||
|                 $this->setRecurring($bills, 'billed_at', 'bill'); | ||||
|                 $this->setAmount($profit_graph, $totals, $compares, $bills, 'bill', 'billed_at'); | ||||
|                 break; | ||||
|         } | ||||
| @@ -135,6 +141,50 @@ class IncomeExpenseSummary extends Controller | ||||
|         return view($view_template, compact('chart', 'dates', 'income_categories', 'expense_categories', 'compares', 'totals')); | ||||
|     } | ||||
|  | ||||
|     private function setRecurring(&$items, $date, $type) | ||||
|     { | ||||
|         foreach ($items as $item) { | ||||
|             if ($item['table'] == 'bill_payments' || $item['table'] == 'invoice_payments') { | ||||
|                 $type_item = $item->$type; | ||||
|  | ||||
|                 $item->category_id = $type_item->category_id; | ||||
|  | ||||
|                 $item = $type_item; | ||||
|             } | ||||
|  | ||||
|             if (!empty($item->parent_id)) { | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             if ($item->recurring) { | ||||
|                 if ($type == 'invoice') { | ||||
|                     $recurred_items = Invoice::where('parent_id', $item->id)->accrued()->monthsOfYear($date)->get(); | ||||
|                 } else { | ||||
|                     $recurred_items = Bill::where('parent_id', $item->id)->accrued()->monthsOfYear($date)->get(); | ||||
|                 } | ||||
|  | ||||
|                 foreach ($item->recurring->schedule() as $recurr) { | ||||
|                     if ($recurred_items->count() > $recurr->getIndex()) { | ||||
|                         continue; | ||||
|                     } | ||||
|  | ||||
|                     if ($recurr->getStart()->format('Y') != Date::now()->format('Y')) { | ||||
|                         continue; | ||||
|                     } | ||||
|  | ||||
|                     $recurr_item = clone $item; | ||||
|  | ||||
|                     $recurr_item->parent_id = $item->id; | ||||
|                     $recurr_item->created_at = $recurr->getStart()->format('Y-m-d'); | ||||
|                     $recurr_item->invoiced_at = $recurr->getStart()->format('Y-m-d'); | ||||
|                     $recurr_item->due_at = $recurr->getEnd()->format('Y-m-d'); | ||||
|  | ||||
|                     $items->push($recurr_item); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private function setAmount(&$graph, &$totals, &$compares, $items, $type, $date_field) | ||||
|     { | ||||
|         foreach ($items as $item) { | ||||
|   | ||||
| @@ -59,14 +59,17 @@ class IncomeSummary extends Controller | ||||
|         switch ($status) { | ||||
|             case 'paid': | ||||
|                 $invoices = InvoicePayment::monthsOfYear('paid_at')->get(); | ||||
|                 $this->setRecurring($invoices, 'paid_at'); | ||||
|                 $this->setAmount($incomes_graph, $totals, $incomes, $invoices, 'invoice', 'paid_at'); | ||||
|                 break; | ||||
|             case 'upcoming': | ||||
|                 $invoices = Invoice::accrued()->monthsOfYear('due_at')->get(); | ||||
|                 $this->setAmount($incomes_graph, $totals, $incomes, $invoices, 'invoice', 'due_at'); | ||||
|                 $this->setRecurring($invoices, 'due_at'); | ||||
|                 break; | ||||
|             default: | ||||
|                 $invoices = Invoice::accrued()->monthsOfYear('invoiced_at')->get(); | ||||
|                 $this->setRecurring($invoices, 'invoiced_at'); | ||||
|                 $this->setAmount($incomes_graph, $totals, $incomes, $invoices, 'invoice', 'invoiced_at'); | ||||
|                 break; | ||||
|         } | ||||
| @@ -98,6 +101,45 @@ class IncomeSummary extends Controller | ||||
|         return view($view_template, compact('chart', 'dates', 'categories', 'incomes', 'totals')); | ||||
|     } | ||||
|  | ||||
|     private function setRecurring(&$invoices, $date) | ||||
|     { | ||||
|         foreach ($invoices as $invoice) { | ||||
|             if ($invoice['table'] == 'invoice_payments') { | ||||
|                 $item  = $invoice->invoice; | ||||
|                 $item->category_id = $invoice->category_id; | ||||
|  | ||||
|                 $invoice = $item; | ||||
|             } | ||||
|  | ||||
|             if (!empty($invoice->parent_id)) { | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             if ($invoice->recurring) { | ||||
|                 $recurred_invoices = Invoice::where('parent_id', $invoice->id)->accrued()->monthsOfYear($date)->get(); | ||||
|  | ||||
|                 foreach ($invoice->recurring->schedule() as $recurr) { | ||||
|                     if ($recurred_invoices->count() > $recurr->getIndex()) { | ||||
|                         continue; | ||||
|                     } | ||||
|  | ||||
|                     if ($recurr->getStart()->format('Y') != Date::now()->format('Y')) { | ||||
|                         continue; | ||||
|                     } | ||||
|  | ||||
|                     $recurr_invoice = clone $invoice; | ||||
|  | ||||
|                     $recurr_invoice->parent_id = $invoice->id; | ||||
|                     $recurr_invoice->created_at = $recurr->getStart()->format('Y-m-d'); | ||||
|                     $recurr_invoice->invoiced_at = $recurr->getStart()->format('Y-m-d'); | ||||
|                     $recurr_invoice->due_at = $recurr->getEnd()->format('Y-m-d'); | ||||
|  | ||||
|                     $invoices->push($recurr_invoice); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private function setAmount(&$graph, &$totals, &$incomes, $items, $type, $date_field) | ||||
|     { | ||||
|         foreach ($items as $item) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user