refactored report listener

This commit is contained in:
denisdulici
2020-01-27 22:41:08 +03:00
parent 877d299533
commit 9c6e3c2751
31 changed files with 484 additions and 200 deletions

View File

@ -30,11 +30,12 @@ class ExpenseSummary extends Report
public function getTotals()
{
$payments = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at'])->get();
$transactions = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at']);
switch ($this->model->settings->basis) {
case 'cash':
// Payments
$payments = $transactions->get();
$this->setTotals($payments, 'paid_at');
break;
@ -45,6 +46,7 @@ class ExpenseSummary extends Report
$this->setTotals($bills, 'billed_at');
// Payments
$payments = $transactions->isNotDocument()->get();
Recurring::reflect($payments, 'paid_at');
$this->setTotals($payments, 'paid_at');

View File

@ -16,16 +16,18 @@ class IncomeExpenseSummary extends Report
public function getTotals()
{
$income_transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at'])->get();
$expense_transactions = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at'])->get();
$income_transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at']);
$expense_transactions = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at']);
switch ($this->model->settings->basis) {
case 'cash':
// Income Transactions
$this->setTotals($income_transactions, 'paid_at', true);
// Revenues
$revenues = $income_transactions->get();
$this->setTotals($revenues, 'paid_at', true);
// Expense Transactions
$this->setTotals($expense_transactions, 'paid_at', true);
// Payments
$payments = $expense_transactions->get();
$this->setTotals($payments, 'paid_at', true);
break;
default:
@ -34,18 +36,20 @@ class IncomeExpenseSummary extends Report
Recurring::reflect($invoices, 'invoiced_at');
$this->setTotals($invoices, 'invoiced_at', true);
// Income Transactions
Recurring::reflect($income_transactions, 'paid_at');
$this->setTotals($income_transactions, 'paid_at', true);
// Revenues
$revenues = $income_transactions->isNotDocument()->get();
Recurring::reflect($revenues, 'paid_at');
$this->setTotals($revenues, 'paid_at', true);
// Bills
$bills = $this->applyFilters(Bill::accrued(), ['date_field' => 'billed_at'])->get();
Recurring::reflect($bills, 'bill', 'billed_at');
$this->setTotals($bills, 'billed_at', true);
// Expense Transactions
Recurring::reflect($expense_transactions, 'paid_at');
$this->setTotals($expense_transactions, 'paid_at', true);
// Payments
$payments = $expense_transactions->isNotDocument()->get();
Recurring::reflect($payments, 'paid_at');
$this->setTotals($payments, 'paid_at', true);
break;
}

View File

@ -30,12 +30,13 @@ class IncomeSummary extends Report
public function getTotals()
{
$transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at'])->get();
$transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at']);
switch ($this->model->settings->basis) {
case 'cash':
// Transactions
$this->setTotals($transactions, 'paid_at');
// Revenues
$revenues = $transactions->get();
$this->setTotals($revenues, 'paid_at');
break;
default:
@ -44,9 +45,10 @@ class IncomeSummary extends Report
Recurring::reflect($invoices, 'invoiced_at');
$this->setTotals($invoices, 'invoiced_at');
// Transactions
Recurring::reflect($transactions, 'paid_at');
$this->setTotals($transactions, 'paid_at');
// Revenues
$revenues = $transactions->isNotDocument()->get();
Recurring::reflect($revenues, 'paid_at');
$this->setTotals($revenues, 'paid_at');
break;
}

View File

@ -6,7 +6,6 @@ use App\Abstracts\Report;
use App\Models\Banking\Transaction;
use App\Models\Purchase\Bill;
use App\Models\Sale\Invoice;
use App\Models\Setting\Category;
use App\Utilities\Recurring;
class ProfitLoss extends Report
@ -36,44 +35,20 @@ class ProfitLoss extends Report
];
}
public function getTableRowList()
{
$this->cat_list = Category::type(['income', 'expense'])->enabled()->orderBy('name')->get();
return collect($this->cat_list)->pluck('name', 'id')->toArray();
}
public function setRows()
{
$list = $this->getTableRowList();
foreach ($this->dates as $date) {
foreach ($this->tables as $t_id => $t_name) {
foreach ($list as $id => $name) {
$cat = $this->cat_list->where('id', $id)->first();
if ($cat->type != $t_id) {
continue;
}
$this->rows[$t_name][$id][$date] = 0;
}
}
}
}
public function getTotals()
{
$income_transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at'])->get();
$expense_transactions = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at'])->get();
$income_transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at']);
$expense_transactions = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at']);
switch ($this->model->settings->basis) {
case 'cash':
// Income Transactions
$this->setTotals($income_transactions, 'paid_at', true, $this->tables['income']);
// Revenues
$revenues = $income_transactions->get();
$this->setTotals($revenues, 'paid_at', true, $this->tables['income']);
// Expense Transactions
$this->setTotals($expense_transactions, 'paid_at', true, $this->tables['expense']);
// Payments
$payments = $expense_transactions->get();
$this->setTotals($payments, 'paid_at', true, $this->tables['expense']);
break;
default:
@ -82,24 +57,26 @@ class ProfitLoss extends Report
Recurring::reflect($invoices, 'invoiced_at');
$this->setTotals($invoices, 'invoiced_at', true, $this->tables['income']);
// Income Transactions
Recurring::reflect($income_transactions, 'paid_at');
$this->setTotals($income_transactions, 'paid_at', true, $this->tables['income']);
// Revenues
$revenues = $income_transactions->isNotDocument()->get();
Recurring::reflect($revenues, 'paid_at');
$this->setTotals($revenues, 'paid_at', true, $this->tables['income']);
// Bills
$bills = $this->applyFilters(Bill::accrued(), ['date_field' => 'billed_at'])->get();
Recurring::reflect($bills, 'bill', 'billed_at');
$this->setTotals($bills, 'billed_at', true, $this->tables['expense']);
// Expense Transactions
Recurring::reflect($expense_transactions, 'paid_at');
$this->setTotals($expense_transactions, 'paid_at', true, $this->tables['expense']);
// Payments
$payments = $expense_transactions->isNotDocument()->get();
Recurring::reflect($payments, 'paid_at');
$this->setTotals($payments, 'paid_at', true, $this->tables['expense']);
break;
}
// TODO: move to views
foreach ($this->totals as $table => $dates) {
foreach ($this->footer_totals as $table => $dates) {
foreach ($dates as $date => $total) {
if (!isset($this->net_profit[$date])) {
$this->net_profit[$date] = 0;

View File

@ -38,24 +38,16 @@ class TaxSummary extends Report
$this->tables = array_combine($taxes, $taxes);
}
public function getTableRowList()
{
return [
'income' => trans_choice('general.incomes', 2),
'expense' => trans_choice('general.expenses', 2),
];
}
public function getTotals()
{
switch ($this->model->settings->basis) {
case 'cash':
// Invoice Payments
$invoices = $this->applyFilters(Transaction::type('income')->isDocument()->with(['invoice', 'invoice.totals'])->isNotTransfer(), ['date_field' => 'paid_at'])->get();
$invoices = $this->applyFilters(Transaction::with(['invoice', 'invoice.totals'])->type('income')->isDocument()->isNotTransfer(), ['date_field' => 'paid_at'])->get();
$this->setTotals($invoices, 'paid_at');
// Bill Payments
$bills = $this->applyFilters(Transaction::type('expense')->isDocument()->with(['bill', 'bill.totals'])->isNotTransfer(), ['date_field' => 'paid_at'])->get();
$bills = $this->applyFilters(Transaction::with(['bill', 'bill.totals'])->type('expense')->isDocument()->isNotTransfer(), ['date_field' => 'paid_at'])->get();
$this->setTotals($bills, 'paid_at');
break;
@ -97,8 +89,9 @@ class TaxSummary extends Report
continue;
}
if (!isset($this->rows[$item_total->name][$type][$date]) ||
!isset($this->totals[$item_total->name][$date]))
if (
!isset($this->row_values[$item_total->name][$type][$date])
|| !isset($this->footer_totals[$item_total->name][$date]))
{
continue;
}
@ -113,13 +106,13 @@ class TaxSummary extends Report
$amount = $this->convertToDefault($item_amount, $item->currency_code, $item->currency_rate);
if ($type == 'income') {
$this->rows[$item_total->name][$type][$date] += $amount;
$this->row_values[$item_total->name][$type][$date] += $amount;
$this->totals[$item_total->name][$date] += $amount;
$this->footer_totals[$item_total->name][$date] += $amount;
} else {
$this->rows[$item_total->name][$type][$date] -= $amount;
$this->row_values[$item_total->name][$type][$date] -= $amount;
$this->totals[$item_total->name][$date] -= $amount;
$this->footer_totals[$item_total->name][$date] -= $amount;
}
}
}
@ -128,7 +121,6 @@ class TaxSummary extends Report
public function getFields()
{
return [
$this->getGroupField(),
$this->getPeriodField(),
$this->getBasisField(),
];