hasMany(InvoiceItem::class)->orderBy('id', 'ASC'); } public function container() { return $this->belongsTo(Container::class); } public function customer() { return $this->belongsTo(User::class, 'customer_id'); } public function installments() { return $this->hasMany(InvoiceInstallment::class); } // ✅ SINGLE, correct relation public function chargeGroups() { return $this->hasMany(\App\Models\InvoiceChargeGroup::class, 'invoice_id'); } /**************************** * Helper Functions ****************************/ public function calculateTotals() { $gst = ($this->final_amount * $this->gst_percent) / 100; $this->gst_amount = $gst; $this->final_amount_with_gst = $this->final_amount + $gst; } public function isOverdue() { return $this->status === 'pending' && now()->gt($this->due_date); } public function getShipment() { return null; } // ✅ Charge groups total accessor public function getChargeGroupsTotalAttribute() { // relation already loaded असेल तर collection वरून sum होईल return (float) $this->chargeGroups->sum('total_charge'); } // ✅ Grand total accessor (items + GST + charge groups) public function getGrandTotalWithChargesAttribute() { return (float) ($this->final_amount_with_gst ?? 0) + $this->charge_groups_total; } public function totalPaid() { return $this->installments->sum('amount'); } public function remainingAmount() { return $this->grand_total_with_charges - $this->totalPaid(); } }