| Server IP : 139.59.63.204 / Your IP : 216.73.217.62 Web Server : Apache/2.4.58 (Ubuntu) System : Linux ubuntu-s-1vcpu-1gb-blr1-01 6.8.0-110-generic #110-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 19 15:09:20 UTC 2026 x86_64 User : root ( 0) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/crmapp/Modules/Inventory/Imports/ |
Upload File : |
<?php
namespace Modules\Inventory\Imports;
use Modules\Inventory\Entities\Product;
use Modules\Inventory\Entities\Serialnumber;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class ProductsImport implements ToModel, WithHeadingRow
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
//use Importable;
public function model(array $row)
{
// Trim keys to avoid hidden spaces
$row = array_map('trim', $row);
// Skip if SKU is missing
if (empty($row['sku'])) {
return null;
}
return DB::transaction(function () use ($row) {
// Create or update product
$product = Product::firstOrCreate(
['sku' => $row['sku']],
[
'name' => $row['name'] ?? 'Unnamed Product',
'unit' => $row['unit'] ?? 0,
'cost_price' => $row['cost_price'] ?? 0,
'sale_price' => $row['sale_price'] ?? 0,
'warranty_period' => $row['warranty_period'] ?? 0,
'reorder_level' => $row['reorder_level'] ?? 0,
'model_number' => $row['model_number'] ?? null,
]
);
// Create serial number if provided
if (!empty($row['serial_number'])) {
SerialNumber::firstOrCreate(
[
'product_id' => $product->id,
'serial_number' => $row['serial_number'],
],
[
'status' => $row['status'] ?? 'in_stock',
'warehouse_id' => $row['warehouse_id'] ?? 1,
'bin_id' => $row['bin_id'] ?? 1,
]
);
}
return $product;
});
}
public function onError(\Throwable $e)
{
// optionally log or handle errors
\Log::error('Excel import error: '.$e->getMessage());
}
}