ifnex/04_Laravel/app/Imports/OldShipmentsImport.php
2026-08-02 04:50:17 +03:30

105 lines
4.4 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\Imports;
use App\Models\Shipment;
use Maatwebsite\Excel\Concerns\OnEachRow;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithStartRow;
class OldShipmentsImport implements OnEachRow, WithHeadingRow, WithStartRow
{
/**
* چون ممکنه در فایل اکسل چندین سطر اول هدر هستند، می‌اییم ۳ سطر اول رو رد کنیم.
* هدر اصلی معمولاً ردیف ۱۰ یا ۱۱ هست.
*/
protected int $startRow = 3;
/**
* متد الزامی برای WithStartRow
*/
public function startRow(): int
{
return $this->startRow;
}
/**
* تبدیل تاریخ شمسی/میلادی به میلادی برای ذخیره در دیتابیس
*/
private function convertDateToGregorian($dateStr)
{
if (preg_match('/\d{4}\/\d{2}\/\d{2,4}/', $dateStr)) {
$parts = explode('/', $dateStr);
if (strlen($parts[2]) === 4) {
list($y, $m, $d) = $parts;
return "$y-$m-$d";
}
}
return $dateStr;
}
public function onRow(\Maatwebsite\Excel\Row $row)
{
$rowIndex = $row->getIndex();
$cells = $row->toArray();
// ۱. ردیف‌های خالی یا هدرها را رد کنیم
if (empty($cells[1]) || in_array($cells[1], ['HAWB No.', 'Date', 'Forwarder', 'Shipper']) || in_array($cells[0], ['HEAD', 'List'])) {
return;
}
// ۲. استخراج داده‌ها (بر اساس شیت لیست شما)
$data = [
'awb_no' => $cells[0],
'direction' => 'Outbound', // طبق اکسل شما بیشتر صادرات است
'type' => $cells[8] === 'DOC' ? 'DOC' : 'NON DOC',
'status' => $cells[18] ?? 'Processed', // فیلد Last State
'reason_for_export' => $cells[17] ?? '',
'weight' => is_numeric($cells[11]) ? $cells[11] : 0,
'volumetric_weight' => is_numeric($cells[12]) ? $cells[12] : 0,
'chargeable_weight' => is_numeric($cells[13]) ? $cells[13] : 0,
'dimensions' => trim(($cells[14] ?? '') . ' * ' . ($cells[15] ?? '') . ' * ' . ($cells[16] ?? '')),
// اصلاح تداخل ایندکس: ایندکس 18 برای status رزرو شد، قیمت را از ایندکس بعدی شروع کردیم
'shipping_price' => is_numeric($cells[19] ?? null) ? $cells[19] : 0,
'extra_service' => is_numeric($cells[20] ?? null) ? $cells[20] : 0,
'packing_cost' => is_numeric($cells[21] ?? null) ? $cells[21] : 0,
'discount' => is_numeric($cells[22] ?? null) ? $cells[22] : 0,
'total_fee' => is_numeric($cells[23] ?? null) ? $cells[23] : 0,
'net_dirham' => is_numeric($cells[25] ?? null) ? $cells[25] : 0,
'net_rial' => is_numeric($cells[26] ?? null) ? $cells[26] : 0,
'sender_name' => $cells[2],
'sender_company' => '',
'sender_phone' => $cells[3],
'sender_email' => $cells[4],
'sender_address' => $cells[5],
'sender_city' => $cells[6],
'sender_zip' => $cells[7],
'sender_id_number' => '',
'receiver_name' => $cells[9],
'receiver_company' => '',
'receiver_phone' => $cells[10],
// اصلاح تداخل ایندکس: ایندکس 11 مربوط به weight است، ایمیل گیرنده را به ایندکس بعدی منتقل کردیم
'receiver_email' => $cells[12] ?? '',
// رفع خطای متغیر ناموجود: خواندن مستقیم از cells به جای data
'receiver_address' => ($cells[5] ?? '') . ' ' . ($cells[6] ?? ''),
'receiver_city' => $cells[6] ?? '',
'receiver_zip' => $cells[13],
'receiver_id_number' => $cells[14],
'user_id' => 1, // به صورت پیش‌فرض برای سفارشات قدیمی که کاربر خاصی ندارند
];
// ۳. تبدیل تاریخ (Date)
if (!empty($cells[1])) {
$data['created_at'] = $this->convertDateToGregorian($cells[1]);
}
// ۴. ذخیره یا آپدیت در دیتابیس
Shipment::updateOrCreate(
['awb_no' => $data['awb_no']],
$data
);
}
}