v2 first commit

This commit is contained in:
denisdulici
2019-11-16 10:21:14 +03:00
parent 5b23e9c2c4
commit 6d50fa8442
3075 changed files with 3451681 additions and 65594 deletions

View File

@ -0,0 +1,33 @@
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Banking\Account;
class AccountBalance extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-4'
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
//
$accounts = Account::enabled()->take(5)->get();
return view('widgets.account_balance', [
'config' => (object) $this->config,
'accounts' => $accounts,
]);
}
}

248
app/Widgets/CashFlow.php Normal file
View File

@ -0,0 +1,248 @@
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Banking\Transaction;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Utilities\Chartjs;
use Date;
class CashFlow extends AbstractWidget
{
use Currencies, DateTime;
// get any custom financial year beginning
public $financial_start;
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-12'
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
$this->financial_start = $this->getFinancialStart()->format('Y-m-d');
$cashflow = $this->getCashFlow();
return view('widgets.cash_flow', [
'config' => (object) $this->config,
'cashflow' => $cashflow,
]);
}
private function getCashFlow()
{
// check and assign year start
if (($year_start = Date::today()->startOfYear()->format('Y-m-d')) !== $this->financial_start) {
$year_start = $this->financial_start;
}
$start = Date::parse(request('start', $year_start));
$end = Date::parse(request('end', Date::parse($year_start)->addYear(1)->subDays(1)->format('Y-m-d')));
$period = request('period', 'month');
$range = request('range', 'custom');
$start_month = $start->month;
$end_month = $end->month;
// Monthly
$labels = array();
$s = clone $start;
if ($range == 'last_12_months') {
$end_month = 12;
$start_month = 0;
} elseif ($range == 'custom') {
$end_month = $end->diffInMonths($start);
$start_month = 0;
}
for ($j = $end_month; $j >= $start_month; $j--) {
$labels[$end_month - $j] = $s->format('M Y');
if ($period == 'month') {
$s->addMonth();
} else {
$s->addMonths(3);
$j -= 2;
}
}
$income = $this->calculateCashFlowTotals('income', $start, $end, $period);
$expense = $this->calculateCashFlowTotals('expense', $start, $end, $period);
$profit = $this->calculateCashFlowProfit($income, $expense);
$options = [
'tooltips' => [
'backgroundColor' => '#f5f5f5',
'titleFontColor' => '#333',
'bodyFontColor' => '#666',
'bodySpacing' => 4,
'YrPadding' => 12,
'mode' => 'nearest',
'intersect' => 0,
'position' => 'nearest'
],
'responsive' => true,
'scales' => [
'yAxes' => [
[
'barPercentage' => 1.6,
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(29,140,248,0.1)',
'zeroLineColor' => 'transparent',
'borderDash' => [2],
'borderDashOffset' => [2],
],
'ticks' => [
'padding' => 10,
'fontColor' => '#9e9e9e'
]
]
],
'xAxes' => [
[
'barPercentage' => 1.6,
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(29,140,248,0.0)',
'zeroLineColor' => 'transparent'
],
'ticks' => [
'suggestedMin' => 60,
'suggestedMax' => 125,
'padding' => 20,
'fontColor' => '#9e9e9e'
]
]
]
]
];
$chart = new Chartjs();
$chart->type('line')
->width(0)
->height(300)
->options($options)
->labels(array_values($labels));
$chart->dataset(trans_choice('general.profits', 1), 'line', array_values($profit))
->backgroundColor('#6da252')
->color('#6da252')
->options([
'borderWidth' => 4,
'pointStyle' => 'line',
])
->fill(false);
$chart->dataset(trans_choice('general.incomes', 1), 'line', array_values($income))
->backgroundColor('#328aef')
->color('#328aef')
->options([
'borderWidth' => 4,
'pointStyle' => 'line',
])
->fill(false);
$chart->dataset(trans_choice('general.expenses', 1), 'line', array_values($expense))
->backgroundColor('#ef3232')
->color('#ef3232')
->options([
'borderWidth' => 4,
'pointStyle' => 'line',
])
->fill(false);
return $chart;
}
private function calculateCashFlowTotals($type, $start, $end, $period)
{
$totals = array();
$date_format = 'Y-m';
if ($period == 'month') {
$n = 1;
$start_date = $start->format($date_format);
$end_date = $end->format($date_format);
$next_date = $start_date;
} else {
$n = 3;
$start_date = $start->quarter;
$end_date = $end->quarter;
$next_date = $start_date;
}
$s = clone $start;
//$totals[$start_date] = 0;
while ($next_date <= $end_date) {
$totals[$next_date] = 0;
if ($period == 'month') {
$next_date = $s->addMonths($n)->format($date_format);
} else {
if (isset($totals[4])) {
break;
}
$next_date = $s->addMonths($n)->quarter;
}
}
$items = Transaction::type($type)->whereBetween('paid_at', [$start, $end])->isNotTransfer()->get();
$this->setCashFlowTotals($totals, $items, $date_format, $period);
return $totals;
}
private function setCashFlowTotals(&$totals, $items, $date_format, $period)
{
foreach ($items as $item) {
if ($period == 'month') {
$i = Date::parse($item->paid_at)->format($date_format);
} else {
$i = Date::parse($item->paid_at)->quarter;
}
if (!isset($totals[$i])) {
continue;
}
$totals[$i] += $item->getAmountConvertedToDefault();
}
}
private function calculateCashFlowProfit($incomes, $expenses)
{
$profit = [];
foreach ($incomes as $key => $income) {
if ($income > 0 && $income > $expenses[$key]) {
$profit[$key] = $income - $expenses[$key];
} else {
$profit[$key] = 0;
}
}
return $profit;
}
}

View File

@ -0,0 +1,233 @@
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Setting\Category;
use App\Utilities\Chartjs;
use Date;
class ExpensesByCategory extends AbstractWidget
{
public $expense_donut = ['colors' => [], 'labels' => [], 'values' => []];
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-6'
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
$expenses_amount = $open_bill = $overdue_bill = 0;
// Get categories
$categories = Category::with(['bills', 'payments'])->type(['expense'])->enabled()->get();
foreach ($categories as $category) {
$amount = 0;
// Payments
foreach ($category->payments as $payment) {
$amount += $payment->getAmountConvertedToDefault();
}
$expenses_amount += $amount;
// Bills
$bills = $category->bills()->accrued()->get();
foreach ($bills as $bill) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($bill, 'bill');
$open_bill += $open;
$overdue_bill += $overdue;
}
$this->addToExpenseDonut($category->color, $amount, $category->name);
}
// Show donut prorated if there is no expense
if (array_sum($this->expense_donut['values']) == 0) {
foreach ($this->expense_donut['values'] as $key => $value) {
$this->expense_donut['values'][$key] = 1;
}
}
// Get 6 categories by amount
$colors = $labels = [];
$values = collect($this->expense_donut['values'])->sort()->reverse()->take(6)->all();
foreach ($values as $id => $val) {
$colors[$id] = $this->expense_donut['colors'][$id];
$labels[$id] = $this->expense_donut['labels'][$id];
}
$options = [
'color' => $colors,
'cutoutPercentage' => 80,
'legend' => [
'position' => 'right'
],
'tooltips' => [
'backgroundColor' => '#f5f5f5',
'titleFontColor' => '#333',
'bodyFontColor' => '#666',
'bodySpacing' => 4,
'xPadding' => 12,
'mode' => 'nearest',
'intersect' => 0,
'position' => 'nearest'
],
'scales' => [
'yAxes' => [
[
'display' => 0,
'ticks' => [
'display' => false
],
'gridLines' => [
'drawBorder' => false,
'zeroLineColor' => 'transparent',
'color' => 'rgba(255,255,255,0.05)'
]
]
],
'xAxes' => [
[
'display' => 0,
'barPercentage' => 1.6,
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(255,255,255,0.1)',
'zeroLineColor' => 'transparent'
],
'ticks' => [
'display' => false
]
]
]
]
];
$donut_expenses = new Chartjs();
$donut_expenses->type('doughnut')
->width(0)
->height(160)
->options($options)
->labels(array_values($labels));
$donut_expenses->dataset(trans_choice('general.expenses', 2), 'doughnut', array_values($values))
->backgroundColor(array_values($colors));
return view('widgets.expenses_by_category', [
'config' => (object) $this->config,
'donut_expenses' => $donut_expenses,
]);
}
public function getData()
{
//
$expenses_amount = $open_bill = $overdue_bill = 0;
// Get categories
$categories = Category::with(['bills', 'payments'])->type(['expense'])->enabled()->get();
foreach ($categories as $category) {
$amount = 0;
// Payments
foreach ($category->payments as $payment) {
$amount += $payment->getAmountConvertedToDefault();
}
$expenses_amount += $amount;
// Bills
$bills = $category->bills()->accrued()->get();
foreach ($bills as $bill) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($bill, 'bill');
$open_bill += $open;
$overdue_bill += $overdue;
}
$this->addToExpenseDonut($category->color, $amount, $category->name);
}
// Show donut prorated if there is no expense
if (array_sum($this->expense_donut['values']) == 0) {
foreach ($this->expense_donut['values'] as $key => $value) {
$this->expense_donut['values'][$key] = 1;
}
}
// Get 6 categories by amount
$colors = $labels = [];
$values = collect($this->expense_donut['values'])->sort()->reverse()->take(6)->all();
foreach ($values as $id => $val) {
$colors[$id] = $this->expense_donut['colors'][$id];
$labels[$id] = $this->expense_donut['labels'][$id];
}
return [
'labels' => $labels,
'colors' => $colors,
'values' => $values,
];
}
private function calculateInvoiceBillTotals($item, $type)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
$code_field = $type . '_status_code';
if ($item->$code_field != 'paid') {
$payments = 0;
if ($item->$code_field == 'partial') {
foreach ($item->transactions as $transaction) {
$payments += $transaction->getAmountConvertedToDefault();
}
}
// Check if it's open or overdue invoice
if ($item->due_at > $today) {
$open += $item->getAmountConvertedToDefault() - $payments;
} else {
$overdue += $item->getAmountConvertedToDefault() - $payments;
}
}
return [$open, $overdue];
}
private function addToExpenseDonut($color, $amount, $text)
{
$this->expense_donut['colors'][] = $color;
$this->expense_donut['labels'][] = money($amount, setting('default.currency'), true)->format() . ' - ' . $text;
$this->expense_donut['values'][] = (int) $amount;
}
}

View File

@ -0,0 +1,235 @@
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Setting\Category;
use App\Utilities\Chartjs;
use Date;
class IncomesByCategory extends AbstractWidget
{
public $income_donut = ['colors' => [], 'labels' => [], 'values' => []];
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-6'
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
//
$incomes_amount = $open_invoice = $overdue_invoice = 0;
// Get categories
$categories = Category::with(['invoices', 'revenues'])->type(['income'])->enabled()->get();
foreach ($categories as $category) {
$amount = 0;
// Revenues
foreach ($category->revenues as $revenue) {
$amount += $revenue->getAmountConvertedToDefault();
}
$incomes_amount += $amount;
// Invoices
$invoices = $category->invoices()->accrued()->get();
foreach ($invoices as $invoice) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($invoice, 'invoice');
$open_invoice += $open;
$overdue_invoice += $overdue;
}
$this->addToIncomeDonut($category->color, $amount, $category->name);
}
// Show donut prorated if there is no income
if (array_sum($this->income_donut['values']) == 0) {
foreach ($this->income_donut['values'] as $key => $value) {
$this->income_donut['values'][$key] = 1;
}
}
// Get 6 categories by amount
$colors = $labels = [];
$values = collect($this->income_donut['values'])->sort()->reverse()->take(6)->all();
foreach ($values as $id => $val) {
$colors[$id] = $this->income_donut['colors'][$id];
$labels[$id] = $this->income_donut['labels'][$id];
}
$options = [
'backgroudColor' => array_values($colors),
'cutoutPercentage' => 80,
'legend' => [
'position' => 'right'
],
'tooltips' => [
'backgroundColor' => '#f5f5f5',
'titleFontColor' => '#333',
'bodyFontColor' => '#666',
'bodySpacing' => 4,
'xPadding' => 12,
'mode' => 'nearest',
'intersect' => 0,
'position' => 'nearest'
],
'scales' => [
'yAxes' => [
[
'display' => 0,
'ticks' => [
'display' => false
],
'gridLines' => [
'drawBorder' => false,
'zeroLineColor' => 'transparent',
'color' => 'rgba(255,255,255,0.05)'
]
]
],
'xAxes' => [
[
'display' => 0,
'barPercentage' => 1.6,
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(255,255,255,0.1)',
'zeroLineColor' => 'transparent'
],
'ticks' => [
'display' => false
]
]
]
]
];
$donut_incomes = new Chartjs();
$donut_incomes->type('doughnut')
->width(0)
->height(160)
->options($options)
->labels(array_values($labels));
$donut_incomes->dataset(trans_choice('general.incomes', 2), 'doughnut', array_values($values))
->backgroundColor(array_values($colors));
return view('widgets.incomes_by_category', [
'config' => (object) $this->config,
'donut_incomes' => $donut_incomes,
]);
}
public function getData()
{
//
$incomes_amount = $open_invoice = $overdue_invoice = 0;
// Get categories
$categories = Category::with(['invoices', 'revenues'])->type(['income'])->enabled()->get();
foreach ($categories as $category) {
$amount = 0;
// Revenues
foreach ($category->revenues as $revenue) {
$amount += $revenue->getAmountConvertedToDefault();
}
$incomes_amount += $amount;
// Invoices
$invoices = $category->invoices()->accrued()->get();
foreach ($invoices as $invoice) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($invoice, 'invoice');
$open_invoice += $open;
$overdue_invoice += $overdue;
}
$this->addToIncomeDonut($category->color, $amount, $category->name);
}
// Show donut prorated if there is no income
if (array_sum($this->income_donut['values']) == 0) {
foreach ($this->income_donut['values'] as $key => $value) {
$this->income_donut['values'][$key] = 1;
}
}
// Get 6 categories by amount
$colors = $labels = [];
$values = collect($this->income_donut['values'])->sort()->reverse()->take(6)->all();
foreach ($values as $id => $val) {
$colors[$id] = $this->income_donut['colors'][$id];
$labels[$id] = $this->income_donut['labels'][$id];
}
return [
'labels' => $labels,
'colors' => $colors,
'values' => $values,
];
}
private function calculateInvoiceBillTotals($item, $type)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
$code_field = $type . '_status_code';
if ($item->$code_field != 'paid') {
$payments = 0;
if ($item->$code_field == 'partial') {
foreach ($item->transactions as $transaction) {
$payments += $transaction->getAmountConvertedToDefault();
}
}
// Check if it's open or overdue invoice
if ($item->due_at > $today) {
$open += $item->getAmountConvertedToDefault() - $payments;
} else {
$overdue += $item->getAmountConvertedToDefault() - $payments;
}
}
return [$open, $overdue];
}
private function addToIncomeDonut($color, $amount, $text)
{
$this->income_donut['colors'][] = $color;
$this->income_donut['labels'][] = money($amount, setting('default.currency'), true)->format() . ' - ' . $text;
$this->income_donut['values'][] = (int) $amount;
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Banking\Transaction;
class LatestExpenses extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-4'
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
//
$latest_expenses = Transaction::type('expense')->orderBy('paid_at', 'desc')->isNotTransfer()->take(5)->get();
return view('widgets.latest_expenses', [
'config' => (object) $this->config,
'latest_expenses' => $latest_expenses,
]);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Banking\Transaction;
class LatestIncomes extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-4'
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
//
$latest_incomes = Transaction::type('income')->orderBy('paid_at', 'desc')->isNotTransfer()->take(5)->get();
return view('widgets.latest_incomes', [
'config' => (object) $this->config,
'latest_incomes' => $latest_incomes,
]);
}
}

View File

@ -0,0 +1,98 @@
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Setting\Category;
use Date;
class TotalExpenses extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-4'
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
//
$expenses_amount = $open_bill = $overdue_bill = 0;
// Get categories
$categories = Category::with(['bills', 'payments'])->type(['expense'])->enabled()->get();
foreach ($categories as $category) {
$amount = 0;
// Payments
foreach ($category->payments as $payment) {
$amount += $payment->getAmountConvertedToDefault();
}
$expenses_amount += $amount;
// Bills
$bills = $category->bills()->accrued()->get();
foreach ($bills as $bill) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($bill, 'bill');
$open_bill += $open;
$overdue_bill += $overdue;
}
}
$expenses_progress = 100;
if (!empty($open_bill) && !empty($overdue_bill)) {
$expenses_progress = (int) ($open_bill * 100) / ($open_bill + $overdue_bill);
}
$total_expenses = array(
'total' => $expenses_amount,
'open_bill' => money($open_bill, setting('default.currency'), true),
'overdue_bill' => money($overdue_bill, setting('default.currency'), true),
'progress' => $expenses_progress
);
return view('widgets.total_expenses', [
'config' => (object) $this->config,
'total_expenses' => $total_expenses,
]);
}
private function calculateInvoiceBillTotals($item, $type)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
$code_field = $type . '_status_code';
if ($item->$code_field != 'paid') {
$payments = 0;
if ($item->$code_field == 'partial') {
foreach ($item->transactions as $transaction) {
$payments += $transaction->getAmountConvertedToDefault();
}
}
// Check if it's open or overdue invoice
if ($item->due_at > $today) {
$open += $item->getAmountConvertedToDefault() - $payments;
} else {
$overdue += $item->getAmountConvertedToDefault() - $payments;
}
}
return [$open, $overdue];
}
}

View File

@ -0,0 +1,99 @@
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Setting\Category;
use Date;
class TotalIncomes extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-4'
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
//
$incomes_amount = $open_invoice = $overdue_invoice = 0;
// Get categories
$categories = Category::with(['invoices', 'revenues'])->type(['income'])->enabled()->get();
foreach ($categories as $category) {
$amount = 0;
// Revenues
foreach ($category->revenues as $revenue) {
$amount += $revenue->getAmountConvertedToDefault();
}
$incomes_amount += $amount;
// Invoices
$invoices = $category->invoices()->accrued()->get();
foreach ($invoices as $invoice) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($invoice, 'invoice');
$open_invoice += $open;
$overdue_invoice += $overdue;
}
}
$incomes_progress = 100;
if (!empty($open_invoice) && !empty($overdue_invoice)) {
$incomes_progress = (int) ($open_invoice * 100) / ($open_invoice + $overdue_invoice);
}
// Totals
$total_incomes = array(
'total' => $incomes_amount,
'open_invoice' => money($open_invoice, setting('default.currency'), true),
'overdue_invoice' => money($overdue_invoice, setting('default.currency'), true),
'progress' => $incomes_progress
);
return view('widgets.total_incomes', [
'config' => (object) $this->config,
'total_incomes' => $total_incomes,
]);
}
private function calculateInvoiceBillTotals($item, $type)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
$code_field = $type . '_status_code';
if ($item->$code_field != 'paid') {
$payments = 0;
if ($item->$code_field == 'partial') {
foreach ($item->transactions as $transaction) {
$payments += $transaction->getAmountConvertedToDefault();
}
}
// Check if it's open or overdue invoice
if ($item->due_at > $today) {
$open += $item->getAmountConvertedToDefault() - $payments;
} else {
$overdue += $item->getAmountConvertedToDefault() - $payments;
}
}
return [$open, $overdue];
}
}

162
app/Widgets/TotalProfit.php Normal file
View File

@ -0,0 +1,162 @@
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Setting\Category;
use Date;
class TotalProfit extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-4'
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
//
list($incomes_amount, $open_invoice, $overdue_invoice, $expenses_amount, $open_bill, $overdue_bill) = $this->calculateAmounts();
$incomes_progress = 100;
if (!empty($open_invoice) && !empty($overdue_invoice)) {
$incomes_progress = (int) ($open_invoice * 100) / ($open_invoice + $overdue_invoice);
}
// Totals
$total_incomes = array(
'total' => $incomes_amount,
'open_invoice' => money($open_invoice, setting('default.currency'), true),
'overdue_invoice' => money($overdue_invoice, setting('default.currency'), true),
'progress' => $incomes_progress
);
$expenses_progress = 100;
if (!empty($open_bill) && !empty($overdue_bill)) {
$expenses_progress = (int) ($open_bill * 100) / ($open_bill + $overdue_bill);
}
$total_expenses = array(
'total' => $expenses_amount,
'open_bill' => money($open_bill, setting('default.currency'), true),
'overdue_bill' => money($overdue_bill, setting('default.currency'), true),
'progress' => $expenses_progress
);
$amount_profit = $incomes_amount - $expenses_amount;
$open_profit = $open_invoice - $open_bill;
$overdue_profit = $overdue_invoice - $overdue_bill;
$total_progress = 100;
if (!empty($open_profit) && !empty($overdue_profit)) {
$total_progress = (int) ($open_profit * 100) / ($open_profit + $overdue_profit);
}
$total_profit = array(
'total' => $amount_profit,
'open' => money($open_profit, setting('default.currency'), true),
'overdue' => money($overdue_profit, setting('default.currency'), true),
'progress' => $total_progress
);
return view('widgets.total_profit', [
'config' => (object) $this->config,
'total_profit' => $total_profit,
]);
}
private function calculateAmounts()
{
$incomes_amount = $open_invoice = $overdue_invoice = 0;
$expenses_amount = $open_bill = $overdue_bill = 0;
// Get categories
$categories = Category::with(['bills', 'invoices', 'payments', 'revenues'])->type(['income', 'expense'])->enabled()->get();
foreach ($categories as $category) {
switch ($category->type) {
case 'income':
$amount = 0;
// Revenues
foreach ($category->revenues as $revenue) {
$amount += $revenue->getAmountConvertedToDefault();
}
$incomes_amount += $amount;
// Invoices
$invoices = $category->invoices()->accrued()->get();
foreach ($invoices as $invoice) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($invoice, 'invoice');
$open_invoice += $open;
$overdue_invoice += $overdue;
}
break;
case 'expense':
$amount = 0;
// Payments
foreach ($category->payments as $payment) {
$amount += $payment->getAmountConvertedToDefault();
}
$expenses_amount += $amount;
// Bills
$bills = $category->bills()->accrued()->get();
foreach ($bills as $bill) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($bill, 'bill');
$open_bill += $open;
$overdue_bill += $overdue;
}
break;
}
}
return array($incomes_amount, $open_invoice, $overdue_invoice, $expenses_amount, $open_bill, $overdue_bill);
}
private function calculateInvoiceBillTotals($item, $type)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
$code_field = $type . '_status_code';
if ($item->$code_field != 'paid') {
$payments = 0;
if ($item->$code_field == 'partial') {
foreach ($item->transactions as $transaction) {
$payments += $transaction->getAmountConvertedToDefault();
}
}
// Check if it's open or overdue invoice
if ($item->due_at > $today) {
$open += $item->getAmountConvertedToDefault() - $payments;
} else {
$overdue += $item->getAmountConvertedToDefault() - $payments;
}
}
return [$open, $overdue];
}
}