reconstruction of the project
This commit is contained in:
@@ -3,11 +3,12 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class PurchaseOrderItem extends BaseModel
|
||||
{
|
||||
protected $table = 'purchase_order_items';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
protected $fillable = [
|
||||
'purchase_order_id',
|
||||
'supply_id',
|
||||
@@ -16,12 +17,71 @@ class PurchaseOrderItem extends BaseModel
|
||||
'received_qty',
|
||||
'unit_cost',
|
||||
];
|
||||
|
||||
public $timestamps = true;
|
||||
|
||||
protected $validationRules = [
|
||||
'purchase_order_id' => 'required|is_natural_no_zero',
|
||||
'supply_id' => 'required|is_natural_no_zero',
|
||||
'quantity' => 'required|is_natural_no_zero',
|
||||
'unit_cost' => 'required|decimal',
|
||||
protected $casts = [
|
||||
'purchase_order_id' => 'integer',
|
||||
'supply_id' => 'integer',
|
||||
'quantity' => 'integer',
|
||||
'received_qty' => 'integer',
|
||||
'unit_cost' => 'decimal:2', // adjust scale to match DB (e.g., decimal:4)
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Relationships (optional but recommended)
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
public function purchaseOrder(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(PurchaseOrder::class, 'purchase_order_id');
|
||||
}
|
||||
|
||||
public function supply(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Supply::class, 'supply_id');
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Accessors (handy for totals / UI)
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
// total ordered cost
|
||||
public function getLineTotalAttribute(): float
|
||||
{
|
||||
$qty = (int) ($this->quantity ?? 0);
|
||||
$cost = (float) ($this->unit_cost ?? 0);
|
||||
return $qty * $cost;
|
||||
}
|
||||
|
||||
// total received cost
|
||||
public function getReceivedLineTotalAttribute(): float
|
||||
{
|
||||
$qty = (int) ($this->received_qty ?? 0);
|
||||
$cost = (float) ($this->unit_cost ?? 0);
|
||||
return $qty * $cost;
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Validation rules (use in FormRequest or controller)
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
public static function rules(bool $updating = false): array
|
||||
{
|
||||
// if updating, you might allow partial updates (sometimes)
|
||||
// keep strict by default to match CI behavior
|
||||
return [
|
||||
'purchase_order_id' => [$updating ? 'sometimes' : 'required', 'integer', 'min:1', 'exists:purchase_orders,id'],
|
||||
'supply_id' => [$updating ? 'sometimes' : 'required', 'integer', 'min:1', 'exists:supplies,id'],
|
||||
'description' => ['nullable', 'string', 'max:1000'],
|
||||
'quantity' => [$updating ? 'sometimes' : 'required', 'integer', 'min:1'],
|
||||
'received_qty' => ['nullable', 'integer', 'min:0', 'lte:quantity'],
|
||||
'unit_cost' => [$updating ? 'sometimes' : 'required', 'numeric', 'min:0'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user