2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Reports;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Income\Invoice;
|
|
|
|
use App\Models\Income\InvoicePayment;
|
|
|
|
use App\Models\Income\Revenue;
|
|
|
|
use App\Models\Setting\Category;
|
2017-12-13 12:04:08 +03:00
|
|
|
use Charts;
|
2017-09-14 22:21:00 +03:00
|
|
|
use Date;
|
|
|
|
|
|
|
|
class IncomeSummary extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$dates = $totals = $incomes = $incomes_graph = $categories = [];
|
|
|
|
|
|
|
|
$status = request('status');
|
|
|
|
|
2018-04-23 22:17:20 +03:00
|
|
|
$categories = Category::enabled()->type('income')->pluck('name', 'id')->toArray();
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
// Get year
|
|
|
|
$year = request('year');
|
|
|
|
if (empty($year)) {
|
|
|
|
$year = Date::now()->year;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dates
|
|
|
|
for ($j = 1; $j <= 12; $j++) {
|
|
|
|
$dates[$j] = Date::parse($year . '-' . $j)->format('F');
|
|
|
|
|
|
|
|
$incomes_graph[Date::parse($year . '-' . $j)->format('F-Y')] = 0;
|
|
|
|
|
|
|
|
// Totals
|
|
|
|
$totals[$dates[$j]] = array(
|
|
|
|
'amount' => 0,
|
|
|
|
'currency_code' => setting('general.default_currency'),
|
|
|
|
'currency_rate' => 1
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($categories as $category_id => $category_name) {
|
|
|
|
$incomes[$category_id][$dates[$j]] = array(
|
|
|
|
'category_id' => $category_id,
|
|
|
|
'name' => $category_name,
|
|
|
|
'amount' => 0,
|
|
|
|
'currency_code' => setting('general.default_currency'),
|
|
|
|
'currency_rate' => 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoices
|
|
|
|
switch ($status) {
|
2017-11-07 05:11:03 +03:00
|
|
|
case 'paid':
|
|
|
|
$invoices = InvoicePayment::monthsOfYear('paid_at')->get();
|
2018-10-16 15:04:46 +03:00
|
|
|
$this->setRecurring($invoices, 'paid_at');
|
2017-11-07 05:11:03 +03:00
|
|
|
$this->setAmount($incomes_graph, $totals, $incomes, $invoices, 'invoice', 'paid_at');
|
2017-09-14 22:21:00 +03:00
|
|
|
break;
|
|
|
|
case 'upcoming':
|
2017-11-07 05:11:03 +03:00
|
|
|
$invoices = Invoice::accrued()->monthsOfYear('due_at')->get();
|
2017-09-14 22:21:00 +03:00
|
|
|
$this->setAmount($incomes_graph, $totals, $incomes, $invoices, 'invoice', 'due_at');
|
2018-10-16 15:04:46 +03:00
|
|
|
$this->setRecurring($invoices, 'due_at');
|
2017-09-14 22:21:00 +03:00
|
|
|
break;
|
|
|
|
default:
|
2017-11-07 05:11:03 +03:00
|
|
|
$invoices = Invoice::accrued()->monthsOfYear('invoiced_at')->get();
|
2018-10-16 15:04:46 +03:00
|
|
|
$this->setRecurring($invoices, 'invoiced_at');
|
2017-11-07 05:11:03 +03:00
|
|
|
$this->setAmount($incomes_graph, $totals, $incomes, $invoices, 'invoice', 'invoiced_at');
|
2017-09-14 22:21:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revenues
|
|
|
|
if ($status != 'upcoming') {
|
2018-04-11 17:52:41 +03:00
|
|
|
$revenues = Revenue::monthsOfYear('paid_at')->isNotTransfer()->get();
|
2017-09-14 22:21:00 +03:00
|
|
|
$this->setAmount($incomes_graph, $totals, $incomes, $revenues, 'revenue', 'paid_at');
|
|
|
|
}
|
|
|
|
|
2018-04-07 12:00:39 +03:00
|
|
|
// Check if it's a print or normal request
|
|
|
|
if (request('print')) {
|
|
|
|
$chart_template = 'vendor.consoletvs.charts.chartjs.multi.line_print';
|
|
|
|
$view_template = 'reports.income_summary.print';
|
|
|
|
} else {
|
|
|
|
$chart_template = 'vendor.consoletvs.charts.chartjs.multi.line';
|
|
|
|
$view_template = 'reports.income_summary.index';
|
|
|
|
}
|
|
|
|
|
2017-12-13 12:04:08 +03:00
|
|
|
// Incomes chart
|
|
|
|
$chart = Charts::multi('line', 'chartjs')
|
|
|
|
->dimensions(0, 300)
|
|
|
|
->colors(['#00c0ef'])
|
|
|
|
->dataset(trans_choice('general.incomes', 1), $incomes_graph)
|
|
|
|
->labels($dates)
|
|
|
|
->credits(false)
|
2018-04-07 12:00:39 +03:00
|
|
|
->view($chart_template);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2018-04-07 12:00:39 +03:00
|
|
|
return view($view_template, compact('chart', 'dates', 'categories', 'incomes', 'totals'));
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:04:46 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
private function setAmount(&$graph, &$totals, &$incomes, $items, $type, $date_field)
|
|
|
|
{
|
|
|
|
foreach ($items as $item) {
|
2018-07-19 14:54:23 +03:00
|
|
|
if ($item['table'] == 'invoice_payments') {
|
|
|
|
$invoice = $item->invoice;
|
|
|
|
|
|
|
|
$item->category_id = $invoice->category_id;
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
$date = Date::parse($item->$date_field)->format('F');
|
|
|
|
|
2018-04-23 22:17:20 +03:00
|
|
|
if (!isset($incomes[$item->category_id])) {
|
2017-09-14 22:21:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$amount = $item->getConvertedAmount();
|
|
|
|
|
|
|
|
// Forecasting
|
|
|
|
if (($type == 'invoice') && ($date_field == 'due_at')) {
|
|
|
|
foreach ($item->payments as $payment) {
|
|
|
|
$amount -= $payment->getConvertedAmount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 22:17:20 +03:00
|
|
|
$incomes[$item->category_id][$date]['amount'] += $amount;
|
|
|
|
$incomes[$item->category_id][$date]['currency_code'] = $item->currency_code;
|
|
|
|
$incomes[$item->category_id][$date]['currency_rate'] = $item->currency_rate;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
$graph[Date::parse($item->$date_field)->format('F-Y')] += $amount;
|
|
|
|
|
|
|
|
$totals[$date]['amount'] += $amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|