diff --git a/app/Models/Account.php b/app/Models/Account.php index 7dff235..07ccf5c 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; @@ -24,6 +25,15 @@ class Account extends Model implements IDs { use HasFactory,ScopeActive; + /* STATIC */ + + public static function InvoicesDue(Collection $invoices=NULL): Collection + { + return (new self) + ->invoiceSummaryDue($invoices,TRUE) + ->get(); + } + /* INTERFACES */ public function getLIDAttribute(): string @@ -121,7 +131,7 @@ class Account extends Model implements IDs public function services() { return $this->hasMany(Service::class) - ->with(['product.translate','invoice_items']); + ->with(['product.translate','product.type.supplied']); } /** @@ -236,11 +246,12 @@ class Account extends Model implements IDs * @param Collection|NULL $invoices * @return Collection */ - public function invoiceSummary(Collection $invoices=NULL): Collection + public function invoiceSummary(Collection $invoices=NULL,bool $all=FALSE): Builder { return (new Invoice) ->select([ - 'invoice_id as id', + 'invoices.account_id', + 'invoices.id as id', DB::raw('SUM(item) AS _item'), DB::raw('SUM(tax) AS _tax'), DB::raw('SUM(payments) AS _payment'), @@ -249,7 +260,8 @@ class Account extends Model implements IDs DB::raw('SUM(payment_fees) AS _payment_fee'), DB::raw('ROUND(CAST(SUM(item_total)-SUM(COALESCE(discount,0))+COALESCE(invoices.discount_amt,0) AS NUMERIC),2) AS _total'), DB::raw('ROUND(CAST(SUM(item_total)-SUM(COALESCE(discount,0))+COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) AS _balance'), - 'due_at', + 'invoices.due_at', + 'invoices.created_at', ]) ->from( (new Payment) @@ -284,10 +296,25 @@ class Account extends Model implements IDs ->groupBy(['invoice_items.invoice_id']), ),'p') ->join('invoices',['invoices.id'=>'invoice_id']) - ->where('account_id',$this->id) - ->groupBy(['p.invoice_id']) - ->groupBy(['due_at','discount_amt']) - ->get(); + ->when(($all === FALSE),fn($query)=>$query->where('invoices.account_id',$this->id)) + ->orderBy('due_at') + ->groupBy(['invoices.account_id','invoices.id','invoices.created_at','invoices.due_at','invoices.discount_amt']) + ->with(['account']); + } + + public function invoiceSummaryDue(Collection $invoices=NULL,bool $all=FALSE): Builder + { + return $this->invoiceSummary($invoices,$all) + ->havingRaw('ROUND(CAST(SUM(item_total)-SUM(COALESCE(discount,0))+COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) > 0'); + } + + public function invoiceSummaryPast(Collection $invoices=NULL,bool $all=FALSE): Builder + { + return $this->invoiceSummary($invoices,$all) + ->join('payment_items',['payment_items.invoice_id'=>'invoices.id']) + ->join('payments',['payments.id'=>'payment_items.payment_id']) + ->addSelect(DB::raw('max(paid_at) as _paid_at')) + ->havingRaw('ROUND(CAST(SUM(item_total)-SUM(COALESCE(discount,0))+COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) <= 0'); } /** diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 0a7a45a..75677ee 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -36,8 +36,10 @@ class Invoice extends Model implements IDs use PushNew,ScopeActive; protected $casts = [ + 'created_at' => 'datetime:Y-m-d', + 'due_at' => 'datetime:Y-m-d', 'reminders'=>'json', - 'due_at'=>'datetime:y-m-d', + '_paid_at' => 'datetime:Y-m-d', ]; public const BILL_WEEKLY = 0; diff --git a/app/Models/Service.php b/app/Models/Service.php index af047a1..96ba74c 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -1180,7 +1180,8 @@ class Service extends Model implements IDs public function invoices() { return $this->account - ->invoiceSummary($this->invoiced_service_items_active->pluck('invoice_id')); + ->invoiceSummary($this->invoiced_service_items_active->pluck('invoice_id')) + ->get(); } /** diff --git a/resources/views/theme/backend/adminlte/account/widget/invoice_due.blade.php b/resources/views/theme/backend/adminlte/account/widget/invoice_due.blade.php new file mode 100644 index 0000000..12e7de6 --- /dev/null +++ b/resources/views/theme/backend/adminlte/account/widget/invoice_due.blade.php @@ -0,0 +1,16 @@ + + +
+
+

Invoices Due

+
+ +
+ @if(($list=$o->invoiceSummaryDue()->get())->count()) + @include('theme.backend.adminlte.invoice.widget.due') + + @else +

No invoice due

+ @endif +
+
\ No newline at end of file diff --git a/resources/views/theme/backend/adminlte/invoice/widget/list.blade.php b/resources/views/theme/backend/adminlte/account/widget/invoice_past.blade.php similarity index 72% rename from resources/views/theme/backend/adminlte/invoice/widget/list.blade.php rename to resources/views/theme/backend/adminlte/account/widget/invoice_past.blade.php index dfdccfb..348d7bf 100644 --- a/resources/views/theme/backend/adminlte/invoice/widget/list.blade.php +++ b/resources/views/theme/backend/adminlte/account/widget/invoice_past.blade.php @@ -6,7 +6,7 @@
- @if(($x=$o->invoices->where('created_at','>',\Carbon\Carbon::now()->subMonths(12))->where('due','<=',0))->count()) + @if(($list=$o->invoiceSummaryPast()->where('invoices.created_at','>=',\Carbon\Carbon::now()->subYears(2)->startOfYear())->get())->count()) @@ -19,13 +19,13 @@ - @foreach ($x as $oo) + @foreach ($list as $oo) - + - - + + @endforeach @@ -37,12 +37,15 @@ -@section('page-scripts') +@section('page-styles') @css(datatables,bootstrap4|rowgroup) +@append + +@section('page-scripts') @js(datatables,bootstrap4|rowgroup) @append \ No newline at end of file diff --git a/resources/views/theme/backend/adminlte/common/account/widget/summary.blade.php b/resources/views/theme/backend/adminlte/account/widget/summary_boxes.blade.php similarity index 94% rename from resources/views/theme/backend/adminlte/common/account/widget/summary.blade.php rename to resources/views/theme/backend/adminlte/account/widget/summary_boxes.blade.php index 6e10b6d..955e32c 100644 --- a/resources/views/theme/backend/adminlte/common/account/widget/summary.blade.php +++ b/resources/views/theme/backend/adminlte/account/widget/summary_boxes.blade.php @@ -45,7 +45,7 @@
Account Balance - $ {{ number_format(($x=$o->invoices()->with(['items.taxes','paymentitems.payment','account'])->get()->where('due','>',0))->sum('due'),2) }} + $ {{ number_format(($x=$o->accounts->map(fn($item)=>$item->invoiceSummaryDue()->get()->pluck('_balance'))->flatten())->sum(),2) }}
diff --git a/resources/views/theme/backend/adminlte/home.blade.php b/resources/views/theme/backend/adminlte/home.blade.php index ac84e81..20e12af 100644 --- a/resources/views/theme/backend/adminlte/home.blade.php +++ b/resources/views/theme/backend/adminlte/home.blade.php @@ -17,7 +17,7 @@ @section('main-content')
- @include('theme.backend.adminlte.common.account.widget.summary') + @include('theme.backend.adminlte.account.widget.summary_boxes')
@@ -49,10 +49,6 @@
{{ $oo->account->name }}{{ $oo->sid }}{{ $oo->lid }} {{ $oo->created_at->format('Y-m-d') }}{{ $oo->paid_date ? $oo->paid_date->format('Y-m-d') : '' }}${{ number_format($oo->total,2) }}{{ $oo->_paid_at?->format('Y-m-d') }}${{ number_format($oo->_total,2) }}
+ + + + + + + + + -
- @if(($x=$o->invoices->where('due','>',0))->count()) -
Account#DueTotalOutstanding
- - - - - - - - - + + @foreach ($list as $oo) + due_at->isPast()) class="table-danger" @endif> + + + + + + + @endforeach + +
Account#DueTotalOutstanding
{{ $oo->account->name }}{{ $oo->lid }}{{ $oo->due_at->format('Y-m-d') }}${{ number_format($oo->_total,2) }}${{ number_format($oo->_balance,2) }}
- - @foreach ($x as $oo) - due_at->isPast()) class="table-danger" @endif> - {{ $oo->account->name }} - {{ $oo->lid }} - {{ $oo->due_at->format('Y-m-d') }} - ${{ number_format($oo->total,2) }} - ${{ number_format($oo->due,2) }} - - @endforeach - - - - @else -

No invoice due

- @endif -
- +@section('page-styles') + @css(datatables,bootstrap4|rowgroup) +@append @section('page-scripts') - @css(datatables,bootstrap4|rowgroup) @js(datatables,bootstrap4|rowgroup) @append \ No newline at end of file diff --git a/resources/views/theme/backend/adminlte/invoice/widget/next.blade.php b/resources/views/theme/backend/adminlte/invoice/widget/next.blade.php index 9757fa5..3622afa 100644 --- a/resources/views/theme/backend/adminlte/invoice/widget/next.blade.php +++ b/resources/views/theme/backend/adminlte/invoice/widget/next.blade.php @@ -1,11 +1,11 @@ -@if ($o->next_invoice_items($future)->count()) +@if (($x=$o->next_invoice_items($future))->count())
- @foreach (($x=$o->next_invoice_items($future))->groupBy('product_id') as $id => $oo) + @foreach ($x->groupBy('product_id') as $id => $oo) diff --git a/resources/views/theme/backend/adminlte/payment/widget/list.blade.php b/resources/views/theme/backend/adminlte/payment/widget/list.blade.php deleted file mode 100644 index 0225798..0000000 --- a/resources/views/theme/backend/adminlte/payment/widget/list.blade.php +++ /dev/null @@ -1,71 +0,0 @@ - - -
-
-

Past Payments

-
- -
- @if(($x=$o->payments->where('created_at','>',\Carbon\Carbon::now()->subMonths(12)))->count()) -
{{ $oo->first()->product->name }} ${{ number_format($oo->sum('total'),2) }}
- - - - - - - {{----}} - - - - - - @foreach ($x as $oo) - - - - - - {{----}} - - - @endforeach - -
Account#ReceivedTotalBalanceInvoice(s)
{{ $oo->account->name }}{{ $oo->lid }}{{ $oo->paid_at->format('Y-m-d') }}${{ number_format($oo->total,2) }}${{ number_format($oo->balance,2) }} - {!! join(', ',$oo->items - ->filter(function($item) { return $item->invoice_id; }) - ->transform(function($item) { return sprintf('%s',url('u/invoice',$item->invoice_id),$item->invoice_id); }) - ->toArray()) !!} -
- - @else -

No payments to list

- @endif -
-
- -@section('page-scripts') - @css(datatables,bootstrap4|rowgroup) - @js(datatables,bootstrap4|rowgroup) - - -@append \ No newline at end of file diff --git a/resources/views/theme/backend/adminlte/r/invoice/widgets/due.blade.php b/resources/views/theme/backend/adminlte/r/invoice/widgets/due.blade.php index d479a11..e773737 100644 --- a/resources/views/theme/backend/adminlte/r/invoice/widgets/due.blade.php +++ b/resources/views/theme/backend/adminlte/r/invoice/widgets/due.blade.php @@ -6,66 +6,11 @@
- @if($o->query_invoice_summary()->havingRaw('invoice_total-payments > 0')->count()) - - - - - - - - - - - - - - - @foreach ($o->query_invoice_summary()->havingRaw('invoice_total-payments > 0')->get() as $oo) - due_at->isPast()) class="table-danger" @endif> - - - - - - - - - @endforeach - -
Account#IssuedDueTotalPaymentsOutstanding
{{ $oo->account->name }}{{ $oo->sid }}{{ $oo->created_at->format('Y-m-d') }}{{ $oo->due_at->format('Y-m-d') }}${{ number_format($oo->total,2) }}${{ number_format($oo->paid,2) }}${{ number_format($oo->due,2) }}
+ @if(($list=\App\Models\Account::InvoicesDue())->count()) + @include('theme.backend.adminlte.invoice.widget.due') @else

No invoice due

@endif
- - -@section('page-styles') - @css(datatables,bootstrap4|rowgroup) -@append - -@section('page-scripts') - @js(datatables,bootstrap4|rowgroup) - - -@append \ No newline at end of file + \ No newline at end of file