ifnex/04_Laravel/app/Models/RateEntry.php
Kazem Alghasi 58493feca4 refactor(currency): IRR as single base currency for all shipping rates
Make Iranian Rial (IRR) the sole base currency for shipping rates:
- RateImportService now converts all imported rates to IRR at import
  time using the current daily exchange rate (source currency AED/USD/
  EUR/IRR). Rates are stored in rate_entries with currency='IRR'.
- PriceCalculatorService looks up IRR rates directly — no conversion
  at calc time. Also fixes a pre-existing bug where extras (in IRR)
  were incorrectly added to the AED base before conversion.
- RateEntry::lookup() default currency changed from 'AED' to 'IRR'.
- Migration changes the column default to 'IRR'.
- Filament import form: relabel 'Rate currency' to 'Source currency
  (in Excel file)' with helper text explaining the conversion.
- WordPress JS: update price summary labels from 'درهم' to 'ریال'
  and drop the deprecated net_dirham field.

Architectural benefit: rates are 'frozen' in IRR at import time —
daily exchange rate changes only affect NEW imports, not already-
stored rates. This is more stable and predictable for customers.
2026-10-29 17:01:30 +03:30

83 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
/**
* RateEntry
* --------
* Stores one rate cell from the rate matrix:
* direction + service_type + weight + zone + currency + effective_at → rate
*
* The latest effective rate for a given (direction, service_type, weight, zone)
* is used by PriceCalculatorService. Old rates are kept for history.
*/
class RateEntry extends Model
{
protected $table = 'rate_entries';
protected $fillable = [
'direction', 'service_type', 'weight', 'zone',
'rate', 'currency', 'effective_at',
];
protected $casts = [
'weight' => 'float',
'rate' => 'float',
'zone' => 'integer',
'effective_at' => 'datetime',
];
/**
* Scope: get the latest rates effective on or before a given date.
*/
public function scopeLatestEffective(Builder $q, ?\DateTimeInterface $at = null): Builder
{
$at ??= now();
return $q->where('effective_at', '<=', $at)
->orderByDesc('effective_at');
}
/**
* Look up the applicable rate for a given weight/zone.
* Rounds UP to the next available weight tier if exact match not found.
*
* @return static|null
*/
public static function lookup(
string $direction,
string $serviceType,
float $weight,
int $zone,
string $currency = 'IRR',
?\DateTimeInterface $at = null
): ?self {
$at ??= now();
// Try exact weight first
$entry = self::where('direction', $direction)
->where('service_type', $serviceType)
->where('weight', $weight)
->where('zone', $zone)
->where('currency', $currency)
->where('effective_at', '<=', $at)
->orderByDesc('effective_at')
->first();
if ($entry) return $entry;
// Otherwise: round UP to the next available weight tier
// (this is correct for shipping — a 2.3 kg parcel pays the 2.5 kg rate)
return self::where('direction', $direction)
->where('service_type', $serviceType)
->where('weight', '>=', $weight)
->where('zone', $zone)
->where('currency', $currency)
->where('effective_at', '<=', $at)
->orderBy('weight')
->orderByDesc('effective_at')
->first();
}
}