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 @@ + + +
No invoice due
+ @endif +{{ $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) }} |