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\Models\Setting\Category;
|
|
|
|
use App\Utilities\Recurring;
|
|
|
|
|
|
|
|
class ProfitLoss extends Report
|
|
|
|
{
|
|
|
|
public $icon = 'fa fa-heart';
|
|
|
|
|
|
|
|
public $chart = false;
|
|
|
|
|
2020-01-04 13:42:58 +03:00
|
|
|
public function getDefaultName()
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
return trans('reports.profit_loss');
|
|
|
|
}
|
|
|
|
|
2020-01-04 13:42:58 +03:00
|
|
|
public function getCategory()
|
|
|
|
{
|
|
|
|
return trans('general.accounting');
|
|
|
|
}
|
|
|
|
|
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 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()
|
|
|
|
{
|
2019-11-27 12:08:36 +03:00
|
|
|
$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();
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
switch ($this->report->basis) {
|
|
|
|
case 'cash':
|
2019-11-27 12:08:36 +03:00
|
|
|
// Income Transactions
|
|
|
|
$this->setTotals($income_transactions, 'paid_at', true, $this->tables['income']);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
// Expense Transactions
|
|
|
|
$this->setTotals($expense_transactions, '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();
|
|
|
|
Recurring::reflect($invoices, 'invoice', 'invoiced_at');
|
|
|
|
$this->setTotals($invoices, 'invoiced_at', true, $this->tables['income']);
|
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
// Income Transactions
|
|
|
|
Recurring::reflect($income_transactions, 'transaction', 'paid_at');
|
|
|
|
$this->setTotals($income_transactions, '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']);
|
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
// Expense Transactions
|
|
|
|
Recurring::reflect($expense_transactions, 'transaction', 'paid_at');
|
|
|
|
$this->setTotals($expense_transactions, 'paid_at', true, $this->tables['expense']);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move to views
|
|
|
|
foreach ($this->totals as $table => $dates) {
|
|
|
|
foreach ($dates as $date => $total) {
|
|
|
|
if (!isset($this->net_profit[$date])) {
|
|
|
|
$this->net_profit[$date] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->net_profit[$date] += $total;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|