Refactor the shipping rate import process to support bidirectional data handling (import/export) and implement currency conversion using system settings. Update the price calculator to ensure valid zone data is retrieved. - Update `ShippingRatesImport` to handle both import and export directions via `SimpleRateSheetImport`. - Implement AED to IRR conversion during rate importation using `SystemSetting`. - Add validation for zone numbers and rate values during import. - Enhance `PriceCalculatorService` to filter for non-zero zone values. - Add various debug and testing scripts for pricing and data structure verification.
129 lines
3.3 KiB
PHP
129 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use App\Enums\ShipmentType;
|
|
use App\Models\ShippingRate;
|
|
use App\Models\SystemSetting;
|
|
use Maatwebsite\Excel\Concerns\OnEachRow;
|
|
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
|
use Maatwebsite\Excel\Concerns\WithStartRow;
|
|
use Maatwebsite\Excel\Row;
|
|
|
|
class ShippingRatesImport implements WithMultipleSheets
|
|
{
|
|
public function sheets(): array
|
|
{
|
|
return [
|
|
'Export Rate' => new RateSheetImport('export'),
|
|
'Import Rate' => new RateSheetImport('import'),
|
|
'DocEco' => new SimpleRateSheetImport(ShipmentType::DocEconomy, 'export'),
|
|
];
|
|
}
|
|
}
|
|
|
|
class RateSheetImport implements OnEachRow, WithStartRow
|
|
{
|
|
public function __construct(protected string $direction) {}
|
|
|
|
public function startRow(): int
|
|
{
|
|
return 5;
|
|
}
|
|
|
|
public function onRow(Row $row)
|
|
{
|
|
$rowIndex = $row->getIndex();
|
|
$cells = $row->toArray();
|
|
|
|
if (empty($cells[0]) || !is_numeric($cells[0])) {
|
|
return;
|
|
}
|
|
|
|
$type = $this->resolveType($rowIndex, $cells);
|
|
|
|
$zones = [];
|
|
for ($i = 1; $i <= 10; $i++) {
|
|
$val = str_replace([',', ' '], '', (string) ($cells[$i] ?? 0));
|
|
$zones['zone_' . $i] = is_numeric($val) ? (float) $val : 0;
|
|
}
|
|
|
|
ShippingRate::updateOrCreate(
|
|
[
|
|
'direction' => $this->direction,
|
|
'type' => $type->value,
|
|
'weight' => (float) $cells[0],
|
|
],
|
|
$zones
|
|
);
|
|
}
|
|
|
|
private function resolveType(int $rowIndex, array $cells): ShipmentType
|
|
{
|
|
$header = strtoupper((string) ($cells[3] ?? ''));
|
|
|
|
if (str_contains($header, 'DOCUMENT') || str_contains($header, 'DOC')) {
|
|
return ShipmentType::DocNormal;
|
|
}
|
|
|
|
if (str_contains($header, 'NON') || str_contains($header, 'PARCEL')) {
|
|
return ShipmentType::Parcel;
|
|
}
|
|
|
|
return $rowIndex < 10 ? ShipmentType::DocNormal : ShipmentType::Parcel;
|
|
}
|
|
}
|
|
|
|
class SimpleRateSheetImport implements OnEachRow, WithStartRow
|
|
{
|
|
public function __construct(
|
|
protected ShipmentType $type,
|
|
protected string $direction = 'export'
|
|
) {}
|
|
|
|
public function startRow(): int
|
|
{
|
|
return 2;
|
|
}
|
|
|
|
public function onRow(Row $row)
|
|
{
|
|
$cells = $row->toArray();
|
|
|
|
if (empty($cells[0]) || !is_numeric($cells[0])) {
|
|
return;
|
|
}
|
|
|
|
$weight = (float) $cells[0];
|
|
$zoneNumber = (int) $cells[1];
|
|
$rawValue = str_replace([',', ' '], '', (string) ($cells[2] ?? 0));
|
|
$rawValue = is_numeric($rawValue) ? (float) $rawValue : 0;
|
|
|
|
if ($zoneNumber < 1 || $zoneNumber > 10 || $rawValue <= 0) {
|
|
return;
|
|
}
|
|
|
|
$aedToIrr = (float) SystemSetting::get('aed_to_irr', 455000);
|
|
$value = $aedToIrr > 0 ? $rawValue / $aedToIrr : $rawValue;
|
|
|
|
$zoneColumn = 'zone_' . $zoneNumber;
|
|
|
|
$defaults = [];
|
|
for ($i = 1; $i <= 10; $i++) {
|
|
$defaults['zone_' . $i] = 0;
|
|
}
|
|
|
|
$record = ShippingRate::firstOrCreate(
|
|
[
|
|
'direction' => $this->direction,
|
|
'type' => $this->type->value,
|
|
'weight' => $weight,
|
|
],
|
|
$defaults
|
|
);
|
|
|
|
$record->{$zoneColumn} = $value;
|
|
$record->save();
|
|
}
|
|
}
|