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 @@ -
Invoice | -Total | -Due | -Date Date | -
---|---|---|---|
Count {{ $o->invoices_due->count() }} | - {{-- @todo Number format should configured by currency --}} -{{ number_format($o->invoices_due->sum('total'),2) }} | -{{ number_format($o->invoices_due->sum('due'),2) }} | -- |
No invoices due
- @endif -# | +Issued | +Due | +Total | +Payments | +Outstanding | +
---|---|---|---|---|---|
{{ $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) }} | +