not reflecting revenues & payments #557
This commit is contained in:
parent
88f139d20d
commit
af6e6eb1b6
@ -7,6 +7,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;
|
||||
|
||||
@ -55,29 +56,37 @@ class ExpenseSummary extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
// Bills
|
||||
$payments = Payment::monthsOfYear('paid_at')->isNotTransfer()->get();
|
||||
|
||||
switch ($status) {
|
||||
case 'paid':
|
||||
// Bills
|
||||
$bills = BillPayment::monthsOfYear('paid_at')->get();
|
||||
$this->setRecurring($bills, 'paid_at');
|
||||
$this->setAmount($expenses_graph, $totals, $expenses, $bills, 'bill', 'paid_at');
|
||||
|
||||
// Payments
|
||||
$this->setAmount($expenses_graph, $totals, $expenses, $payments, 'payment', 'paid_at');
|
||||
break;
|
||||
case 'upcoming':
|
||||
// Bills
|
||||
$bills = Bill::accrued()->monthsOfYear('due_at')->get();
|
||||
$this->setRecurring($bills, 'due_at');
|
||||
Recurring::reflect($bills, 'bill', 'billed_at', $status);
|
||||
$this->setAmount($expenses_graph, $totals, $expenses, $bills, 'bill', 'due_at');
|
||||
|
||||
// Payments
|
||||
Recurring::reflect($payments, 'payment', 'paid_at', $status);
|
||||
$this->setAmount($expenses_graph, $totals, $expenses, $payments, 'payment', 'paid_at');
|
||||
break;
|
||||
default:
|
||||
// Bills
|
||||
$bills = Bill::accrued()->monthsOfYear('billed_at')->get();
|
||||
$this->setRecurring($bills, 'billed_at');
|
||||
Recurring::reflect($bills, 'bill', 'billed_at', $status);
|
||||
$this->setAmount($expenses_graph, $totals, $expenses, $bills, 'bill', 'billed_at');
|
||||
break;
|
||||
}
|
||||
|
||||
// Payments
|
||||
if ($status != 'upcoming') {
|
||||
$payments = Payment::monthsOfYear('paid_at')->isNotTransfer()->get();
|
||||
$this->setAmount($expenses_graph, $totals, $expenses, $payments, 'payment', 'paid_at');
|
||||
// Payments
|
||||
Recurring::reflect($payments, 'payment', 'paid_at', $status);
|
||||
$this->setAmount($expenses_graph, $totals, $expenses, $payments, 'payment', 'paid_at');
|
||||
break;
|
||||
}
|
||||
|
||||
// Check if it's a print or normal request
|
||||
@ -101,49 +110,10 @@ 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) {
|
||||
if ($item['table'] == 'bill_payments') {
|
||||
if ($item->getTable() == 'bill_payments') {
|
||||
$bill = $item->bill;
|
||||
|
||||
$item->category_id = $bill->category_id;
|
||||
|
@ -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;
|
||||
|
@ -7,6 +7,7 @@ use App\Models\Income\Invoice;
|
||||
use App\Models\Income\InvoicePayment;
|
||||
use App\Models\Income\Revenue;
|
||||
use App\Models\Setting\Category;
|
||||
use App\Utilities\Recurring;
|
||||
use Charts;
|
||||
use Date;
|
||||
|
||||
@ -55,29 +56,37 @@ class IncomeSummary extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
// Invoices
|
||||
$revenues = Revenue::monthsOfYear('paid_at')->isNotTransfer()->get();
|
||||
|
||||
switch ($status) {
|
||||
case 'paid':
|
||||
// Invoices
|
||||
$invoices = InvoicePayment::monthsOfYear('paid_at')->get();
|
||||
$this->setRecurring($invoices, 'paid_at');
|
||||
$this->setAmount($incomes_graph, $totals, $incomes, $invoices, 'invoice', 'paid_at');
|
||||
|
||||
// Revenues
|
||||
$this->setAmount($incomes_graph, $totals, $incomes, $revenues, 'revenue', 'paid_at');
|
||||
break;
|
||||
case 'upcoming':
|
||||
// Invoices
|
||||
$invoices = Invoice::accrued()->monthsOfYear('due_at')->get();
|
||||
Recurring::reflect($invoices, 'invoice', 'invoiced_at', $status);
|
||||
$this->setAmount($incomes_graph, $totals, $incomes, $invoices, 'invoice', 'due_at');
|
||||
$this->setRecurring($invoices, 'due_at');
|
||||
|
||||
// Revenues
|
||||
Recurring::reflect($revenues, 'revenue', 'paid_at', $status);
|
||||
$this->setAmount($incomes_graph, $totals, $incomes, $revenues, 'revenue', 'paid_at');
|
||||
break;
|
||||
default:
|
||||
// Invoices
|
||||
$invoices = Invoice::accrued()->monthsOfYear('invoiced_at')->get();
|
||||
$this->setRecurring($invoices, 'invoiced_at');
|
||||
Recurring::reflect($invoices, 'invoice', 'invoiced_at', $status);
|
||||
$this->setAmount($incomes_graph, $totals, $incomes, $invoices, 'invoice', 'invoiced_at');
|
||||
break;
|
||||
}
|
||||
|
||||
// Revenues
|
||||
if ($status != 'upcoming') {
|
||||
$revenues = Revenue::monthsOfYear('paid_at')->isNotTransfer()->get();
|
||||
$this->setAmount($incomes_graph, $totals, $incomes, $revenues, 'revenue', 'paid_at');
|
||||
// Revenues
|
||||
Recurring::reflect($revenues, 'revenue', 'paid_at', $status);
|
||||
$this->setAmount($incomes_graph, $totals, $incomes, $revenues, 'revenue', 'paid_at');
|
||||
break;
|
||||
}
|
||||
|
||||
// Check if it's a print or normal request
|
||||
@ -101,49 +110,10 @@ 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) {
|
||||
if ($item['table'] == 'invoice_payments') {
|
||||
if ($item->getTable() == 'invoice_payments') {
|
||||
$invoice = $item->invoice;
|
||||
|
||||
$item->category_id = $invoice->category_id;
|
||||
|
59
app/Utilities/Recurring.php
Normal file
59
app/Utilities/Recurring.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utilities;
|
||||
|
||||
use Date;
|
||||
|
||||
class Recurring
|
||||
{
|
||||
|
||||
public static function reflect(&$items, $type, $issued_date_field, $status)
|
||||
{
|
||||
foreach ($items as $key => $item) {
|
||||
if (($item->getTable() == 'bill_payments') || ($item->getTable() == 'invoice_payments')) {
|
||||
$i = $item->$type;
|
||||
$i->category_id = $item->category_id;
|
||||
|
||||
$item = $i;
|
||||
}
|
||||
|
||||
if (($status == 'upcoming') && (($type == 'revenue') || ($type == 'payment'))) {
|
||||
$items->forget($key);
|
||||
}
|
||||
|
||||
if (!$item->recurring || !empty($item->parent_id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($item->recurring->schedule() as $recurr) {
|
||||
$issued = Date::parse($item->$issued_date_field);
|
||||
$start = $recurr->getStart();
|
||||
|
||||
if ($issued->format('Y') != $start->format('Y')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (($issued->format('Y-m') == $start->format('Y-m')) && ($issued->format('d') >= $start->format('d'))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$clone = clone $item;
|
||||
|
||||
$start_date = Date::parse($start->format('Y-m-d'));
|
||||
|
||||
if (($type == 'invoice') || ($type == 'bill')) {
|
||||
// Days between invoiced/billed and due date
|
||||
$diff_days = Date::parse($clone->due_at)->diffInDays(Date::parse($clone->invoiced_at));
|
||||
|
||||
$clone->due_at = $start_date->addDays($diff_days)->format('Y-m-d');
|
||||
}
|
||||
|
||||
$clone->parent_id = $item->id;
|
||||
$clone->created_at = $start_date->format('Y-m-d');
|
||||
$clone->$issued_date_field = $start_date->format('Y-m-d');
|
||||
|
||||
$items->push($clone);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user