not reflecting revenues & payments #557

This commit is contained in:
denisdulici
2018-10-30 13:01:33 +03:00
parent 88f139d20d
commit af6e6eb1b6
4 changed files with 141 additions and 176 deletions

View File

@ -10,6 +10,7 @@ use App\Models\Expense\Bill;
use App\Models\Expense\BillPayment;
use App\Models\Expense\Payment;
use App\Models\Setting\Category;
use App\Utilities\Recurring;
use Charts;
use Date;
@ -70,55 +71,64 @@ class IncomeExpenseSummary extends Controller
}
}
// Invoices
$revenues = Revenue::monthsOfYear('paid_at')->isNotTransfer()->get();
$payments = Payment::monthsOfYear('paid_at')->isNotTransfer()->get();
switch ($status) {
case 'paid':
// Invoices
$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;
}
// Revenues
if ($status != 'upcoming') {
$revenues = Revenue::monthsOfYear('paid_at')->isNotTransfer()->get();
$this->setAmount($profit_graph, $totals, $compares, $revenues, 'revenue', 'paid_at');
}
// Revenues
$this->setAmount($profit_graph, $totals, $compares, $revenues, 'revenue', 'paid_at');
// Bills
switch ($status) {
case 'paid':
// Bills
$bills = BillPayment::monthsOfYear('paid_at')->get();
$this->setRecurring($bills, 'paid_at', 'bill');
$this->setAmount($profit_graph, $totals, $compares, $bills, 'bill', 'paid_at');
// Payments
$this->setAmount($profit_graph, $totals, $compares, $payments, 'payment', 'paid_at');
break;
case 'upcoming':
// Invoices
$invoices = Invoice::accrued()->monthsOfYear('due_at')->get();
Recurring::reflect($invoices, 'invoice', 'due_at', $status);
$this->setAmount($profit_graph, $totals, $compares, $invoices, 'invoice', 'due_at');
// Revenues
Recurring::reflect($revenues, 'revenue', 'paid_at', $status);
$this->setAmount($profit_graph, $totals, $compares, $revenues, 'revenue', 'paid_at');
// Bills
$bills = Bill::accrued()->monthsOfYear('due_at')->get();
$this->setRecurring($bills, 'due_at', 'bill');
Recurring::reflect($bills, 'bill', 'billed_at', $status);
$this->setAmount($profit_graph, $totals, $compares, $bills, 'bill', 'due_at');
// Payments
Recurring::reflect($payments, 'payment', 'paid_at', $status);
$this->setAmount($profit_graph, $totals, $compares, $payments, 'payment', 'paid_at');
break;
default:
// Invoices
$invoices = Invoice::accrued()->monthsOfYear('invoiced_at')->get();
Recurring::reflect($invoices, 'invoice', 'invoiced_at', $status);
$this->setAmount($profit_graph, $totals, $compares, $invoices, 'invoice', 'invoiced_at');
// Revenues
Recurring::reflect($revenues, 'revenue', 'paid_at', $status);
$this->setAmount($profit_graph, $totals, $compares, $revenues, 'revenue', 'paid_at');
// Bills
$bills = Bill::accrued()->monthsOfYear('billed_at')->get();
$this->setRecurring($bills, 'billed_at', 'bill');
Recurring::reflect($bills, 'bill', 'billed_at', $status);
$this->setAmount($profit_graph, $totals, $compares, $bills, 'bill', 'billed_at');
// Payments
Recurring::reflect($payments, 'payment', 'paid_at', $status);
$this->setAmount($profit_graph, $totals, $compares, $payments, 'payment', 'paid_at');
break;
}
// Payments
if ($status != 'upcoming') {
$payments = Payment::monthsOfYear('paid_at')->isNotTransfer()->get();
$this->setAmount($profit_graph, $totals, $compares, $payments, 'payment', 'paid_at');
}
// Check if it's a print or normal request
if (request('print')) {
@ -141,54 +151,10 @@ 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) {
if ($item['table'] == 'bill_payments' || $item['table'] == 'invoice_payments') {
if ($item->getTable() == 'bill_payments' || $item->getTable() == 'invoice_payments') {
$type_item = $item->$type;
$item->category_id = $type_item->category_id;