2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Reports;
|
|
|
|
|
2019-11-23 21:47:20 +03:00
|
|
|
use App\Abstracts\Report;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Models\Banking\Transaction;
|
2019-12-31 15:49:09 +03:00
|
|
|
use App\Models\Purchase\Bill;
|
|
|
|
use App\Models\Sale\Invoice;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Utilities\Recurring;
|
|
|
|
|
|
|
|
class ProfitLoss extends Report
|
|
|
|
{
|
2020-01-16 15:39:37 +03:00
|
|
|
public $default_name = 'reports.profit_loss';
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-01-16 15:39:37 +03:00
|
|
|
public $category = 'general.accounting';
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-01-16 15:39:37 +03:00
|
|
|
public $icon = 'fa fa-heart';
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-01-28 15:51:36 +03:00
|
|
|
public $indents = [
|
|
|
|
'table_header' => '0px',
|
|
|
|
'table_rows' => '48px',
|
|
|
|
];
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
public function setViews()
|
|
|
|
{
|
|
|
|
parent::setViews();
|
|
|
|
$this->views['content.header'] = 'reports.profit_loss.content.header';
|
|
|
|
$this->views['content.footer'] = 'reports.profit_loss.content.footer';
|
|
|
|
$this->views['table.header'] = 'reports.profit_loss.table.header';
|
|
|
|
$this->views['table.footer'] = 'reports.profit_loss.table.footer';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setTables()
|
|
|
|
{
|
|
|
|
$this->tables = [
|
|
|
|
'income' => trans_choice('general.incomes', 1),
|
|
|
|
'expense' => trans_choice('general.expenses', 2),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTotals()
|
|
|
|
{
|
2020-01-27 22:41:08 +03:00
|
|
|
$income_transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at']);
|
|
|
|
$expense_transactions = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at']);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-01-16 15:39:37 +03:00
|
|
|
switch ($this->model->settings->basis) {
|
2019-11-16 10:21:14 +03:00
|
|
|
case 'cash':
|
2020-01-27 22:41:08 +03:00
|
|
|
// Revenues
|
|
|
|
$revenues = $income_transactions->get();
|
|
|
|
$this->setTotals($revenues, 'paid_at', true, $this->tables['income']);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-01-27 22:41:08 +03:00
|
|
|
// Payments
|
|
|
|
$payments = $expense_transactions->get();
|
|
|
|
$this->setTotals($payments, 'paid_at', true, $this->tables['expense']);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Invoices
|
|
|
|
$invoices = $this->applyFilters(Invoice::accrued(), ['date_field' => 'invoiced_at'])->get();
|
2020-01-18 13:11:19 +03:00
|
|
|
Recurring::reflect($invoices, 'invoiced_at');
|
2019-11-16 10:21:14 +03:00
|
|
|
$this->setTotals($invoices, 'invoiced_at', true, $this->tables['income']);
|
|
|
|
|
2020-01-27 22:41:08 +03:00
|
|
|
// Revenues
|
|
|
|
$revenues = $income_transactions->isNotDocument()->get();
|
|
|
|
Recurring::reflect($revenues, 'paid_at');
|
|
|
|
$this->setTotals($revenues, 'paid_at', true, $this->tables['income']);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
// 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']);
|
|
|
|
|
2020-01-27 22:41:08 +03:00
|
|
|
// Payments
|
|
|
|
$payments = $expense_transactions->isNotDocument()->get();
|
|
|
|
Recurring::reflect($payments, 'paid_at');
|
|
|
|
$this->setTotals($payments, 'paid_at', true, $this->tables['expense']);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move to views
|
2020-01-27 22:41:08 +03:00
|
|
|
foreach ($this->footer_totals as $table => $dates) {
|
2019-11-16 10:21:14 +03:00
|
|
|
foreach ($dates as $date => $total) {
|
|
|
|
if (!isset($this->net_profit[$date])) {
|
|
|
|
$this->net_profit[$date] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->net_profit[$date] += $total;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-23 15:03:25 +03:00
|
|
|
|
|
|
|
public function getFields()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
$this->getGroupField(),
|
|
|
|
$this->getPeriodField(),
|
|
|
|
$this->getBasisField(),
|
|
|
|
];
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|