Invoice summary improvements

This commit is contained in:
Deon George 2020-07-21 16:39:23 +10:00
parent 337beee7bf
commit 444f2cf52d
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254
5 changed files with 236 additions and 72 deletions

View File

@ -6,15 +6,16 @@ use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection; use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Support\Facades\DB;
use Laravel\Passport\HasApiTokens; use Laravel\Passport\HasApiTokens;
use Leenooks\Carbon; use Leenooks\Carbon;
use Leenooks\Traits\UserSwitch; use Leenooks\Traits\UserSwitch;
use App\Notifications\ResetPassword as ResetPasswordNotification;
use App\Models\Site;
use App\Models\Service;
use Spinen\QuickBooks\HasQuickBooksToken; use Spinen\QuickBooks\HasQuickBooksToken;
use App\Notifications\ResetPassword as ResetPasswordNotification;
use App\Models\{Invoice,Payment,Site,Service};
class User extends Authenticatable class User extends Authenticatable
{ {
use HasApiTokens,Notifiable,UserSwitch,HasQuickBooksToken; use HasApiTokens,Notifiable,UserSwitch,HasQuickBooksToken;
@ -506,6 +507,113 @@ class User extends Authenticatable
return $result; return $result;
} }
/**
* Return an SQL query that will return a list of invoices
*
* @return \Illuminate\Database\Query\Builder
*/
private function query_invoice_items()
{
return DB::table('ab_invoice_item')
->select([
'invoice_id',
DB::raw('ab_invoice_item.id AS invoice_item_id'),
DB::raw('IFNULL(ab_invoice_item.discount_amt,0) AS discount'),
DB::raw('ROUND(CAST(quantity*price_base AS decimal(8,2)),2) AS base'),
DB::raw('ROUND(ab_invoice_item_tax.amount,2) AS tax'),
])
->join('ab_invoice_item_tax',['ab_invoice_item_tax.invoice_item_id'=>'ab_invoice_item.id'])
->where('active',TRUE);
}
/**
* Return an SQL query that will return payment summaries by invoices.
*
* @return \Illuminate\Database\Query\Builder
*/
private function query_payment_items()
{
return DB::table('ab_payment_item')
->select([
'payment_id',
'invoice_id',
DB::raw('SUM(alloc_amt) AS allocate'),
])
->where('alloc_amt','>',0)
->groupBy(['invoice_id','payment_id']);
}
/**
* Return an SQL query that will summarise invoices with payments
*
* @return \Illuminate\Database\Query\Builder
*/
public function query_invoice_summary()
{
$invoices = (new Invoice)
->select([
'invoice_id',
DB::raw('SUM(discount) AS discount'),
DB::raw('SUM(base) AS base'),
DB::raw('SUM(tax) AS tax'),
DB::raw('ROUND(SUM(base)+SUM(tax)-SUM(discount),2) AS total'),
DB::raw('false AS payments'),
DB::raw('false AS payment_fees'),
])
->from($this->query_invoice_items(),'II')
->join('ab_invoice',['ab_invoice.id'=>'II.invoice_id'])
->whereIN('account_id',$this->all_accounts()->pluck('id')->unique()->toArray())
->where('ab_invoice.active',TRUE)
->groupBy(['invoice_id']);
$payments = (new Payment)
->select([
'invoice_id',
DB::raw('false AS discount'),
DB::raw('false AS base'),
DB::raw('false AS tax'),
DB::raw('false AS total'),
DB::raw('SUM(allocate) AS payments'),
DB::raw('SUM(fees_amt) AS payment_fees'),
])
->from($this->query_payment_items(),'PI')
->join('ab_payment',['ab_payment.id'=>'PI.payment_id'])
->whereIN('account_id',$this->all_accounts()->pluck('id')->unique()->toArray())
//->where('ab_payment.active',TRUE) // @todo To implement
->groupBy(['invoice_id']);
$summary = (new Invoice)
->select([
'invoice_id',
DB::raw('SUM(discount) AS discount'),
DB::raw('SUM(base) AS invoice_base'),
DB::raw('SUM(tax) AS invoice_tax'),
DB::raw('SUM(total) AS invoice_total'),
DB::raw('SUM(payments) AS payments'),
DB::raw('SUM(payment_fees) AS payment_fees'),
])
->from($invoices->unionAll($payments),'invoices')
->groupBy(['invoice_id']);
return (new Invoice)
->select([
'account_id',
'id',
'due_date',
'date_orig',
'discount',
'invoice_base',
'invoice_tax',
'invoice_total',
'payments',
'payment_fees',
DB::raw('ROUND(invoice_total-payments,2) AS balance'),
])
->join('ab_invoice',['ab_invoice.id'=>'invoice_id'])
->from($summary,'summary');
}
public function role() public function role()
{ {
// If I have agents and no parent, I am the wholesaler // If I have agents and no parent, I am the wholesaler

View File

@ -1,65 +0,0 @@
<div class="card card-dark card-outline">
<div class="card-header">
<h4 class="card-title">Invoices Due</h4>
</div>
<div class="card-body">
@if ($o->invoices_due->count())
<table class="table table-striped table-hover" id="invoices" style="width: 100%;">
<thead>
<tr>
<th>Invoice</th>
<th>Total</th>
<th>Due</th>
<th>Date Date</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Count {{ $o->invoices_due->count() }}</th>
{{-- @todo Number format should configured by currency --}}
<th class="right">{{ number_format($o->invoices_due->sum('total'),2) }}</th>
<th class="right">{{ number_format($o->invoices_due->sum('due'),2) }}</th>
<th>&nbsp;</th>
</tr>
</tfoot>
</table>
@else
<p>No invoices due</p>
@endif
</div>
</div>
@section('page-scripts')
@css('//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css','jq-dt-css','jquery')
@js('//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js','jq-dt-js','jquery')
@css('//cdn.datatables.net/responsive/2.2.1/css/responsive.dataTables.min.css','dt-responsive-css','jq-dt-css')
@js('//cdn.datatables.net/responsive/2.2.1/js/dataTables.responsive.min.js','dt-responsive-js','jq-dt-js')
@css('/plugin/dataTables/dataTables.bootstrap4.css','dt-bootstrap4-css','jq-dt-css')
@js('/plugin/dataTables/dataTables.bootstrap4.js','dt-bootstrap4-js','jq-dt-js')
<script type="text/javascript">
$(document).ready(function() {
$('#invoices').DataTable( {
responsive: true,
ajax: {
url: "/api/u/invoices/{{ $o->id }}"
},
columns: [
{ data: "invoice_id_url" },
{ data: "total" },
{ data: "due" },
{ data: "date_due" }
],
language: {
emptyTable: "No Invoices Due"
},
order: [3, 'asc']
});
$('#invoices tbody').on('click','tr', function () {
$(this).toggleClass('selected');
});
});
</script>
@append

View File

@ -38,7 +38,24 @@
</div> </div>
<div class="col-5"> <div class="col-5">
@include('common.invoice.widget.due') <!-- Show outstanding invoices -->
<div class="card card-warning card-outline">
<div class="card-header">
<h4 class="card-title">Outstanding Invoices</h4>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div>
<div class="card-body">
@include('widgets.invoice',[
'o'=>$o->query_invoice_summary()->whereIN('account_id',$o->accounts()->pluck('id')->toArray())->having('balance','>',0),
'widget_invoice_name'=>'widget-invoice-outstanding'
])
</div>
</div>
@include('common.payment.widget.history') @include('common.payment.widget.history')
</div> </div>
</div> </div>
@ -52,9 +69,27 @@
<div class="col-8"> <div class="col-8">
@include('r.service.widget.movement') @include('r.service.widget.movement')
<!-- Show outstanding invoices -->
<div class="card card-warning card-outline">
<div class="card-header">
<h4 class="card-title">Outstanding Invoices</h4>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div> </div>
<div class="card-body">
@include('widgets.invoice',[
'o'=>$o->query_invoice_summary()->having('balance','>',0),
'widget_invoice_name'=>'widget-invoice-outstanding-client'
])
</div>
</div>
</div>
{{-- {{--
<div class="col-xs-6"> <div class="col-xs-6">
@include('r.agents') @include('r.agents')
</div> </div>

View File

@ -39,8 +39,42 @@
</div> </div>
<div class="col-5"> <div class="col-5">
@include('common.invoice.widget.due') <!-- Show outstanding invoices -->
@include('common.invoice.widget.history') <div class="card card-warning card-outline">
<div class="card-header">
<h4 class="card-title">Outstanding Invoices</h4>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div>
<div class="card-body">
@include('widgets.invoice',[
'o'=>$o->query_invoice_summary()->whereIN('account_id',$o->accounts()->pluck('id')->toArray())->having('balance','>',0),
'widget_invoice_name'=>'widget-invoice'
])
</div>
</div>
<!-- Show last 10 invoices -->
<div class="card card-warning card-outline">
<div class="card-header">
<h4 class="card-title">Invoices - Last 12 Months</h4>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div>
<div class="card-body">
@include('widgets.invoice',[
'o'=>$o->query_invoice_summary()->whereIN('account_id',$o->accounts()->pluck('id')->toArray())->where('due_date','>',now()->subYear()->timestamp),
'widget_invoice_name'=>'widget-invoice-last'
])
</div>
</div>
@include('common.payment.widget.history') @include('common.payment.widget.history')
</div> </div>
</div> </div>

View File

@ -0,0 +1,52 @@
@if(($x=$o->get())->count())
<table class="table table-bordered w-100" id="{{ $widget_invoice_name ?? 'widget-invoice'}}">
<thead>
<tr>
<th class="text-right">#</th>
<th class="text-right">Issued</th>
<th class="text-right">Due</th>
<th class="text-right">Total</th>
<th class="text-right">Payments</th>
<th class="text-right">Outstanding</th>
</tr>
</thead>
<tbody>
@foreach ($x as $oo)
<tr>
<td class="text-right"><a href="{{ url('u/invoice',$oo->id) }}">{{ $oo->id }}</a></td>
<td class="text-right">{{ $oo->date_orig->format('Y-m-d') }}</td>
<td class="text-right">{{ $oo->due_date->format('Y-m-d') }}</td>
<td class="text-right">${{ number_format($oo->invoice_total,2) }}</td>
<td class="text-right">${{ number_format($oo->payments,2) }}</td>
<td class="text-right">${{ number_format($oo->balance,2) }}</td>
</tr>
@endforeach
</tbody>
</table>
@section('page-scripts')
@css('//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css','jq-dt-css','jquery')
@js('//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js','jq-dt-js','jquery')
@css('//cdn.datatables.net/responsive/2.2.1/css/responsive.dataTables.min.css','jq-dt-r-css','jq-dt-css')
@js('//cdn.datatables.net/responsive/2.2.1/js/dataTables.responsive.min.js','jq-dt-r-js','jq-dt-js')
@css('/plugin/dataTables/dataTables.bootstrap4.css','dt-bootstrap4-css','jq-dt-css')
@js('/plugin/dataTables/dataTables.bootstrap4.js','dt-bootstrap4-js','jq-dt-js')
<script type="text/javascript">
$(document).ready(function() {
$('#{{ $widget_invoice_name ?? 'widget-invoice'}}').DataTable({
responsive: true,
order: [1, 'desc']
});
$('#{{ $widget_invoice_name ?? 'widget-invoice'}} tbody').on('click','tr', function () {
$(this).toggleClass('selected');
});
});
</script>
@append
@else
No data to display
@endif