'integer', 'unit_price' => 'decimal:2', 'discount_percentage' => 'decimal:2', 'discount_amount' => 'decimal:2', 'tax_percentage' => 'decimal:2', 'tax_amount' => 'decimal:2', 'total_amount' => 'decimal:2' ]; public function invoice(): BelongsTo { return $this->belongsTo(SalesInvoice::class, 'invoice_id'); } public function product(): BelongsTo { return $this->belongsTo(\Workdo\ProductService\Models\ProductServiceItem::class, 'product_id'); } public function taxes(): HasMany { return $this->hasMany(SalesInvoiceItemTax::class, 'item_id'); } public function calculateAmounts() { $lineTotal = $this->quantity * $this->unit_price; $this->discount_amount = ($lineTotal * $this->discount_percentage) / 100; $afterDiscount = $lineTotal - $this->discount_amount; $this->tax_amount = ($afterDiscount * $this->tax_percentage) / 100; $this->total_amount = $afterDiscount + $this->tax_amount; } protected static function boot() { parent::boot(); static::saving(function ($item) { $item->calculateAmounts(); }); } }