From 444f2cf52d63274d2ea682d9a12a1e44737dc98b Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 21 Jul 2020 16:39:23 +1000 Subject: [PATCH] Invoice summary improvements --- app/User.php | 114 +++++++++++++++++- .../common/invoice/widget/due.blade.php | 65 ---------- .../theme/backend/adminlte/r/home.blade.php | 39 +++++- .../theme/backend/adminlte/u/home.blade.php | 38 +++++- .../adminlte/widgets/invoice.blade.php | 52 ++++++++ 5 files changed, 236 insertions(+), 72 deletions(-) delete mode 100644 resources/views/theme/backend/adminlte/common/invoice/widget/due.blade.php create mode 100644 resources/views/theme/backend/adminlte/widgets/invoice.blade.php diff --git a/app/User.php b/app/User.php index fe32bd5..caa80bf 100644 --- a/app/User.php +++ b/app/User.php @@ -6,15 +6,16 @@ use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Support\Collection; use Illuminate\Database\Eloquent\Collection as DatabaseCollection; +use Illuminate\Support\Facades\DB; use Laravel\Passport\HasApiTokens; use Leenooks\Carbon; use Leenooks\Traits\UserSwitch; -use App\Notifications\ResetPassword as ResetPasswordNotification; -use App\Models\Site; -use App\Models\Service; use Spinen\QuickBooks\HasQuickBooksToken; +use App\Notifications\ResetPassword as ResetPasswordNotification; +use App\Models\{Invoice,Payment,Site,Service}; + class User extends Authenticatable { use HasApiTokens,Notifiable,UserSwitch,HasQuickBooksToken; @@ -506,6 +507,113 @@ class User extends Authenticatable 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() { // If I have agents and no parent, I am the wholesaler diff --git a/resources/views/theme/backend/adminlte/common/invoice/widget/due.blade.php b/resources/views/theme/backend/adminlte/common/invoice/widget/due.blade.php deleted file mode 100644 index cbfa480..0000000 --- a/resources/views/theme/backend/adminlte/common/invoice/widget/due.blade.php +++ /dev/null @@ -1,65 +0,0 @@ -
-
-

Invoices Due

-
- -
- @if ($o->invoices_due->count()) - - - - - - - - - - - - - {{-- @todo Number format should configured by currency --}} - - - - - -
InvoiceTotalDueDate Date
Count {{ $o->invoices_due->count() }}{{ number_format($o->invoices_due->sum('total'),2) }}{{ number_format($o->invoices_due->sum('due'),2) }} 
- @else -

No invoices due

- @endif -
-
- -@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') - - -@append \ No newline at end of file diff --git a/resources/views/theme/backend/adminlte/r/home.blade.php b/resources/views/theme/backend/adminlte/r/home.blade.php index 818dc4e..7df1785 100644 --- a/resources/views/theme/backend/adminlte/r/home.blade.php +++ b/resources/views/theme/backend/adminlte/r/home.blade.php @@ -38,7 +38,24 @@
- @include('common.invoice.widget.due') + +
+
+

Outstanding Invoices

+ +
+ +
+
+ +
+ @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' + ]) +
+
+ @include('common.payment.widget.history')
@@ -52,9 +69,27 @@
@include('r.service.widget.movement') -
+ +
+
+

Outstanding Invoices

+ +
+ +
+
+ +
+ @include('widgets.invoice',[ + 'o'=>$o->query_invoice_summary()->having('balance','>',0), + 'widget_invoice_name'=>'widget-invoice-outstanding-client' + ]) +
+
+ {{-- +
@include('r.agents')
diff --git a/resources/views/theme/backend/adminlte/u/home.blade.php b/resources/views/theme/backend/adminlte/u/home.blade.php index 619e982..c0bdd8b 100644 --- a/resources/views/theme/backend/adminlte/u/home.blade.php +++ b/resources/views/theme/backend/adminlte/u/home.blade.php @@ -39,8 +39,42 @@
- @include('common.invoice.widget.due') - @include('common.invoice.widget.history') + +
+
+

Outstanding Invoices

+ +
+ +
+
+ +
+ @include('widgets.invoice',[ + 'o'=>$o->query_invoice_summary()->whereIN('account_id',$o->accounts()->pluck('id')->toArray())->having('balance','>',0), + 'widget_invoice_name'=>'widget-invoice' + ]) +
+
+ + +
+
+

Invoices - Last 12 Months

+ +
+ +
+
+ +
+ @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' + ]) +
+
+ @include('common.payment.widget.history')
diff --git a/resources/views/theme/backend/adminlte/widgets/invoice.blade.php b/resources/views/theme/backend/adminlte/widgets/invoice.blade.php new file mode 100644 index 0000000..26523c2 --- /dev/null +++ b/resources/views/theme/backend/adminlte/widgets/invoice.blade.php @@ -0,0 +1,52 @@ +@if(($x=$o->get())->count()) + + + + + + + + + + + + + + @foreach ($x as $oo) + + + + + + + + + @endforeach + +
#IssuedDueTotalPaymentsOutstanding
{{ $oo->id }}{{ $oo->date_orig->format('Y-m-d') }}{{ $oo->due_date->format('Y-m-d') }}${{ number_format($oo->invoice_total,2) }}${{ number_format($oo->payments,2) }}${{ number_format($oo->balance,2) }}
+ +@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') + + +@append + +@else + No data to display +@endif \ No newline at end of file