119 lines
2.8 KiB
PHP
119 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class InvoiceItem extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'invoice_id',
|
|
'container_id', // Container mapping
|
|
'container_row_index', // Container row index
|
|
|
|
'description',
|
|
'ctn',
|
|
'qty',
|
|
'ttl_qty',
|
|
'unit',
|
|
'price',
|
|
'ttl_amount',
|
|
|
|
'cbm',
|
|
'ttl_cbm',
|
|
|
|
'kg',
|
|
'ttl_kg',
|
|
|
|
'shop_no',
|
|
'mark_no',
|
|
];
|
|
|
|
/****************************
|
|
* Relationships
|
|
****************************/
|
|
|
|
public function invoice()
|
|
{
|
|
return $this->belongsTo(Invoice::class);
|
|
}
|
|
|
|
public function chargeGroupItems()
|
|
{
|
|
return $this->hasMany(InvoiceChargeGroupItem::class, 'invoice_item_id');
|
|
}
|
|
|
|
// हे helper: पहिला group fetch करून त्यावरून rate/total काढणे
|
|
public function getChargeRateAttribute()
|
|
{
|
|
$pivot = $this->chargeGroupItems->first();
|
|
if (!$pivot || !$pivot->group) {
|
|
return 0;
|
|
}
|
|
|
|
$group = $pivot->group;
|
|
|
|
// basis नुसार या item चा basis value
|
|
$basis = 0;
|
|
switch ($group->basis_type) {
|
|
case 'ttl_qty':
|
|
$basis = $this->ttl_qty;
|
|
break;
|
|
case 'amount':
|
|
$basis = $this->ttl_amount;
|
|
break;
|
|
case 'ttl_cbm':
|
|
$basis = $this->ttl_cbm;
|
|
break;
|
|
case 'ttl_kg':
|
|
$basis = $this->ttl_kg;
|
|
break;
|
|
}
|
|
|
|
if ($basis <= 0 || ($group->basis_value ?? 0) <= 0) {
|
|
return 0;
|
|
}
|
|
|
|
// group चा rate field आधीच आहे, ते direct वापरू
|
|
return (float) $group->rate;
|
|
}
|
|
|
|
public function getChargeTotalAttribute()
|
|
{
|
|
$pivot = $this->chargeGroupItems->first();
|
|
if (!$pivot || !$pivot->group) {
|
|
return 0;
|
|
}
|
|
|
|
$group = $pivot->group;
|
|
|
|
$basis = 0;
|
|
switch ($group->basis_type) {
|
|
case 'ttl_qty':
|
|
$basis = $this->ttl_qty;
|
|
break;
|
|
case 'amount':
|
|
$basis = $this->ttl_amount;
|
|
break;
|
|
case 'ttl_cbm':
|
|
$basis = $this->ttl_cbm;
|
|
break;
|
|
case 'ttl_kg':
|
|
$basis = $this->ttl_kg;
|
|
break;
|
|
}
|
|
|
|
if ($basis <= 0 || ($group->basis_value ?? 0) <= 0) {
|
|
return 0;
|
|
}
|
|
|
|
// per unit rate
|
|
$rate = (float) $group->rate;
|
|
// item total = basis * rate
|
|
return $basis * $rate;
|
|
}
|
|
}
|