ifnex/04_Laravel/app/Models/ShipmentPackage.php
Kazem Alghasi 1c8fb2c9d9 - Add shipment_packages migration and ShipmentPackage model
- Add packages() relation to Shipment model
- Update CustomerOrderController to accept packages[] array
- Auto-calculate volumetric weight from dimensions (L*W*H/5000)
- Redesign order form Step 1 with Multi-Package UI
- Add package repeater (add/remove packages)
- Real-time summary of total weights
- Both real weight and dimensions are required
- Dimensions normalized to * format (5*6*9)

Phase 3.5.1 — Multi-Package complete"
2026-08-29 07:09:25 +03:30

64 lines
1.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ShipmentPackage extends Model
{
protected $fillable = [
'shipment_id',
'package_number',
'weight',
'volumetric_weight',
'chargeable_weight',
'dimensions',
'description',
];
protected $casts = [
'weight' => 'decimal:3',
'volumetric_weight' => 'decimal:3',
'chargeable_weight' => 'decimal:3',
];
/**
* رابطه با Shipment
*/
public function shipment(): BelongsTo
{
return $this->belongsTo(Shipment::class);
}
/**
* محاسبه وزن قابل محاسبه (ماکزیمم وزن واقعی و حجمی)
*/
public function calculateChargeableWeight(): float
{
return max((float) $this->weight, (float) $this->volumetric_weight);
}
/**
* محاسبه وزن حجمی از ابعاد
* فرمول: (طول × عرض × ارتفاع) / 5000
*/
public static function calculateVolumetricWeight(?string $dimensions): float
{
if (!$dimensions) return 0;
// پارس کردن ابعاد - فرمت‌های ممکن: "25*15*3" یا "25x15x3" یا "25,15,3"
$parts = preg_split('/[\*xX,]/', trim($dimensions));
if (count($parts) !== 3) return 0;
$length = (float) trim($parts[0]);
$width = (float) trim($parts[1]);
$height = (float) trim($parts[2]);
if ($length <= 0 || $width <= 0 || $height <= 0) return 0;
// فرمول استاندارد IATA: (L × W × H) / 5000
return round(($length * $width * $height) / 5000, 3);
}
}