profit & loss

This commit is contained in:
denisdulici 2018-04-11 19:40:21 +03:00
parent eeb715f169
commit a97772c3a0
10 changed files with 400 additions and 2 deletions

View File

@ -80,7 +80,7 @@ class IncomeExpenseSummary extends Controller
$compares['expense'][0][$dates[$j]] = array(
'category_id' => 0,
'name' => trans_choice('general.invoices', 1),
'name' => trans_choice('general.bills', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1

View File

@ -0,0 +1,232 @@
<?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\Expense\Bill;
use App\Models\Expense\BillPayment;
use App\Models\Expense\Payment;
use App\Models\Setting\Category;
use Charts;
use Date;
class ProfitLoss extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$dates = $totals = $compares = $categories = [];
$status = request('status');
$income_categories = Category::enabled()->type('income')->pluck('name', 'id')->toArray();
// Add invoice to income categories
$income_categories[0] = trans_choice('general.invoices', 2);
$expense_categories = Category::enabled()->type('expense')->pluck('name', 'id')->toArray();
// Add bill to expense categories
$expense_categories[0] = trans_choice('general.bills', 2);
// 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)->quarter;
// Totals
$totals[$dates[$j]] = array(
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
);
// Compares
$compares['income'][0][$dates[$j]] = [
'category_id' => 0,
'name' => trans_choice('general.invoices', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
foreach ($income_categories as $category_id => $category_name) {
$compares['income'][$category_id][$dates[$j]] = [
'category_id' => $category_id,
'name' => $category_name,
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
}
$compares['expense'][0][$dates[$j]] = [
'category_id' => 0,
'name' => trans_choice('general.bills', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
foreach ($expense_categories as $category_id => $category_name) {
$compares['expense'][$category_id][$dates[$j]] = [
'category_id' => $category_id,
'name' => $category_name,
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
}
$j += 2;
}
$totals['total'] = [
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
$compares['income'][0]['total'] = [
'category_id' => 0,
'name' => trans_choice('general.totals', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
foreach ($income_categories as $category_id => $category_name) {
$compares['income'][$category_id]['total'] = [
'category_id' => $category_id,
'name' => trans_choice('general.totals', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
}
$compares['expense'][0]['total'] = [
'category_id' => 0,
'name' => trans_choice('general.totals', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
foreach ($expense_categories as $category_id => $category_name) {
$compares['expense'][$category_id]['total'] = [
'category_id' => $category_id,
'name' => trans_choice('general.totals', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
}
// Invoices
switch ($status) {
case 'paid':
$invoices = InvoicePayment::monthsOfYear('paid_at')->get();
$this->setAmount($totals, $compares, $invoices, 'invoice', 'paid_at');
break;
case 'upcoming':
$invoices = Invoice::accrued()->monthsOfYear('due_at')->get();
$this->setAmount($totals, $compares, $invoices, 'invoice', 'due_at');
break;
default:
$invoices = Invoice::accrued()->monthsOfYear('invoiced_at')->get();
$this->setAmount($totals, $compares, $invoices, 'invoice', 'invoiced_at');
break;
}
// Revenues
if ($status != 'upcoming') {
$revenues = Revenue::monthsOfYear('paid_at')->isNotTransfer()->get();
$this->setAmount($totals, $compares, $revenues, 'revenue', 'paid_at');
}
// Bills
switch ($status) {
case 'paid':
$bills = BillPayment::monthsOfYear('paid_at')->get();
$this->setAmount($totals, $compares, $bills, 'bill', 'paid_at');
break;
case 'upcoming':
$bills = Bill::accrued()->monthsOfYear('due_at')->get();
$this->setAmount($totals, $compares, $bills, 'bill', 'due_at');
break;
default:
$bills = Bill::accrued()->monthsOfYear('billed_at')->get();
$this->setAmount($totals, $compares, $bills, 'bill', 'billed_at');
break;
}
// Payments
if ($status != 'upcoming') {
$payments = Payment::monthsOfYear('paid_at')->isNotTransfer()->get();
$this->setAmount($totals, $compares, $payments, 'payment', 'paid_at');
}
// Check if it's a print or normal request
if (request('print')) {
$view_template = 'reports.profit_loss.print';
} else {
$view_template = 'reports.profit_loss.index';
}
return view($view_template, compact('dates', 'income_categories', 'expense_categories', 'compares', 'totals'));
}
private function setAmount(&$totals, &$compares, $items, $type, $date_field)
{
foreach ($items as $item) {
$date = Date::parse($item->$date_field)->quarter;
if (($type == 'invoice') || ($type == 'bill')) {
$category_id = 0;
} else {
$category_id = $item->category_id;
}
$group = (($type == 'invoice') || ($type == 'revenue')) ? 'income' : 'expense';
if (!isset($compares[$group][$category_id])) {
continue;
}
$amount = $item->getConvertedAmount();
// Forecasting
if ((($type == 'invoice') || ($type == 'bill')) && ($date_field == 'due_at')) {
foreach ($item->payments as $payment) {
$amount -= $payment->getConvertedAmount();
}
}
$compares[$group][$category_id][$date]['amount'] += $amount;
$compares[$group][$category_id][$date]['currency_code'] = $item->currency_code;
$compares[$group][$category_id][$date]['currency_rate'] = $item->currency_rate;
$compares[$group][$category_id]['total']['amount'] += $amount;
if ($group == 'income') {
$totals[$date]['amount'] += $amount;
$totals['total']['amount'] += $amount;
} else {
$totals[$date]['amount'] -= $amount;
$totals['total']['amount'] -= $amount;
}
}
}
}

View File

@ -111,7 +111,7 @@ class AdminMenu
}
// Reports
if ($user->can(['read-reports-income-summary', 'read-reports-expense-summary', 'read-reports-income-expense-summary'])) {
if ($user->can(['read-reports-income-summary', 'read-reports-expense-summary', 'read-reports-income-expense-summary', 'read-reports-profit-loss'])) {
$menu->dropdown(trans_choice('general.reports', 2), function ($sub) use($user, $attr) {
if ($user->can('read-reports-income-summary')) {
$sub->url('reports/income-summary', trans('reports.summary.income'), 1, $attr);
@ -124,6 +124,10 @@ class AdminMenu
if ($user->can('read-reports-income-expense-summary')) {
$sub->url('reports/income-expense-summary', trans('reports.summary.income_expense'), 3, $attr);
}
if ($user->can('read-reports-profit-loss')) {
$sub->url('reports/profit-loss', trans('reports.profit_loss'), 4, $attr);
}
}, 6, [
'title' => trans_choice('general.reports', 2),
'icon' => 'fa fa-bar-chart',

View File

@ -0,0 +1,48 @@
<?php
namespace App\Listeners\Updates;
use App\Events\UpdateFinished;
use App\Models\Auth\Role;
use App\Models\Auth\Permission;
class Version120 extends Listener
{
const ALIAS = 'core';
const VERSION = '1.2.0';
/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle(UpdateFinished $event)
{
// Check if should listen
if (!$this->check($event)) {
return;
}
// Create permission
$permission = Permission::firstOrCreate([
'name' => 'read-reports-profit-loss',
'display_name' => 'Read Reports Profit Loss',
'description' => 'Read Reports Profit Loss',
]);
// Attach permission to roles
$roles = Role::all();
foreach ($roles as $role) {
$allowed = ['admin', 'manager'];
if (!in_array($role->name, $allowed)) {
continue;
}
$role->attachPermission($permission);
}
}
}

View File

@ -22,6 +22,7 @@ class EventServiceProvider extends ServiceProvider
'App\Listeners\Updates\Version112',
'App\Listeners\Updates\Version113',
'App\Listeners\Updates\Version119',
'App\Listeners\Updates\Version120',
],
'Illuminate\Auth\Events\Login' => [
'App\Listeners\Auth\Login',

View File

@ -7,6 +7,9 @@ return [
'this_quarter' => 'This Quarter',
'previous_quarter' => 'Previous Quarter',
'last_12_months' => 'Last 12 Months',
'profit_loss' => 'Profit & Loss',
'gross_profit' => 'Gross Profit',
'net_profit' => 'Net Profit',
'summary' => [
'income' => 'Income Summary',
@ -14,4 +17,11 @@ return [
'income_expense' => 'Income vs Expense',
],
'quarter' => [
'1' => 'Jan-Mar',
'2' => 'Apr-Jun',
'3' => 'Jul-Sep',
'4' => 'Oct-Dec',
],
];

View File

@ -0,0 +1,67 @@
<div class="box-body">
<div class="table table-responsive">
<table class="table" id="tbl-payments">
<thead>
<tr>
<th class="col-md-2">&nbsp;</th>
@foreach($dates as $date)
<th class="col-md-2 text-right">{{ trans('reports.quarter.' . $date) }}</th>
@endforeach
<th class="col-md-2 text-right">{{ trans_choice('general.totals', 1) }}</th>
</tr>
</thead>
<tbody>
@if ($compares)
<table class="table">
<thead>
<th class="col-md-2" colspan="6">{{ trans_choice('general.incomes', 2) }}</th>
</thead>
<tbody>
@foreach($compares['income'] as $category_id => $category)
<tr>
<td>{{ $income_categories[$category_id] }}</td>
@foreach($category as $item)
<td class="col-md-2 text-right">@money($item['amount'], $item['currency_code'], true)</td>
@endforeach
</tr>
@endforeach
</tbody>
</table>
<table class="table">
<thead>
<th class="col-md-2" colspan="6">{{ trans_choice('general.expenses', 2) }}</th>
</thead>
<tbody>
@foreach($compares['expense'] as $category_id => $category)
<tr>
<td>{{ $expense_categories[$category_id] }}</td>
@foreach($category as $item)
<td class="col-md-2 text-right">@money($item['amount'], $item['currency_code'], true)</td>
@endforeach
</tr>
@endforeach
</tbody>
</table>
@else
<tr>
<td colspan="13">
<h5 class="text-center">{{ trans('general.no_records') }}</h5>
</td>
</tr>
@endif
</tbody>
<table class="table">
<tbody>
<tr>
<th class="col-md-2" colspan="6">{{ trans('reports.net_profit') }}</th>
@foreach($totals as $total)
<th class="col-md-2 text-right"><span>@money($total['amount'], $total['currency_code'], true)</span></th>
@endforeach
</tr>
</tbody>
</table>
</table>
</div>
</div>

View File

@ -0,0 +1,28 @@
@extends('layouts.admin')
@section('title', trans('reports.profit_loss'))
@section('new_button')
<span class="new-button"><a href="{{ url('reports/profit-loss') }}?print=1&status={{ request('status') }}&year={{ request('year', $this_year) }}" target="_blank" class="btn btn-success btn-sm"><span class="fa fa-print"></span> &nbsp;{{ trans('general.print') }}</a></span>
@endsection
@section('content')
<!-- Default box -->
<div class="box box-success">
<div class="box-header">
<div class="pull-left" style="margin-left: 23px">
<a href="{{ url('reports/profit-loss') }}?year={{ request('year', $this_year) }}"><span class="badge @if (request('status') == '') bg-green @else bg-default @endif">{{ trans('general.all') }}</span></a>
<a href="{{ url('reports/profit-loss') }}?status=paid&year={{ request('year', $this_year) }}"><span class="badge @if (request('status') == 'paid') bg-green @else bg-default @endif">{{ trans('invoices.paid') }}</span></a>
<a href="{{ url('reports/profit-loss') }}?status=upcoming&year={{ request('year', $this_year) }}"><span class="badge @if (request('status') == 'upcoming') bg-green @else bg-default @endif">{{ trans('general.upcoming') }}</span></a>
</div>
{!! Form::open(['url' => 'reports/profit-loss', 'role' => 'form', 'method' => 'GET']) !!}
<div class="pull-right">
{!! Form::select('year', $years, request('year', $this_year), ['class' => 'form-control input-filter input-sm', 'onchange' => 'this.form.submit()']) !!}
</div>
{!! Form::close() !!}
</div>
@include('reports.profit_loss.body')
</div>
<!-- /.box -->
@endsection

View File

@ -0,0 +1,7 @@
@extends('layouts.print')
@section('title', trans('reports.profit_loss'))
@section('content')
@include('reports.profit_loss.body')
@endsection

View File

@ -96,6 +96,7 @@ Route::group(['middleware' => 'language'], function () {
Route::resource('income-summary', 'Reports\IncomeSummary');
Route::resource('expense-summary', 'Reports\ExpenseSummary');
Route::resource('income-expense-summary', 'Reports\IncomeExpenseSummary');
Route::resource('profit-loss', 'Reports\ProfitLoss');
});
Route::group(['prefix' => 'settings'], function () {