first(); // if buyer not found then first create it then import the data if (!$buyer) { $buyer = Buyer::create([ 'division_id' => 2, 'company_id' => 3, 'company_name' => 'FAL - Factory', 'division_name' => 'Factory', 'name' => strtoupper($row['buyer']) ]); } //automatically create the job_no $job_no = Job::max('id') + 1; $job_no = 'FAL-' . date('y') . '-' . str_pad($job_no, 6, '0', STR_PAD_LEFT); //calculate production_plan, total_value, total_cm, fabric_qnty, print_wash from the data // $production_plan = $row['order_quantity'] * $row['target_smv'] / 60; // $total_value = $row['order_quantity'] * $row['unit_price']; // $total_cm = $row['order_quantity'] * $row['cm_pc']; // $fabric_qnty = $row['consumption_dzn'] * $row['order_quantity'] / 12; // dd($row); return Job::updateOrCreate( [ 'job_no' => $job_no, 'color' => $row['color'], 'size' => $row['size'], ], [ 'company_id' => 3, 'division_id' => 2, 'company_name' => 'FAL - Factory', 'division_name' => 'Factory', 'buyer_id' => $buyer->id, 'buyer' => $buyer->name, 'style' => $row['style'], 'po' => $row['po'], 'department' => $row['department'], 'item' => $row['item'], 'color_quantity' => $row['color_quantity'], 'destination' => $row['destination'], 'order_quantity' => $row['order_quantity'], 'delivery_date' => $row['delivery_date'], 'target_smv' => $row['target_smv'], 'unit_price' => $row['unit_price'], 'cm_pc' => $row['cm_pc'], 'consumption_dzn' => $row['consumption_dzn'], 'fabrication' => $row['fabrication'], 'order_received_date' => $row['order_received_date'], 'aop' => $row['aop'], 'print' => $row['print'], 'embroidery' => $row['embroidery'], 'wash' => $row['wash'], 'print_wash' => $row['print_wash'], 'remarks' => $row['remarks'], ] ); } public function rules(): array { return [ 'job_no' => 'required|string', 'buyer' => 'required|exists:buyers,name', 'style' => 'required|string', 'color' => 'required|string', 'size' => 'required|string', 'color_quantity' => 'required|numeric', 'order_quantity' => 'required|numeric', ]; } public function onError(\Throwable $e) { // Handle errors here, if buyer not found then first create it then import the data Log::error('Error importing job: ' . $e->getMessage()); } } private function parseOrderReference(string $reference): array { preg_match_all('/PO-?\d+/', $reference, $poMatches); $po = $poMatches[0][0] ?? null; $stylePart = trim(preg_replace('/PO-?\d+/', '', $reference)); $stylePart = preg_replace('/\s{2,}/', ' ', $stylePart); return [ 'style' => $stylePart, 'po' => $po, ]; } private function mapJobAttributes(array $row, int $buyerId, string $jobNo, array $parsedRef): array { return [ 'company_id' => 3, 'division_id' => 2, 'buyer_id' => $buyerId, 'style' => $parsedRef['style'], 'po' => $parsedRef['po'], 'department' => $row['dept'] ?? null, 'item' => $row['item'] ?? null, 'destination' => $row['dest'] ?? null, 'order_quantity' => $row['order_quantity'] ?? 0, 'delivery_date' => $this->parseDate($row['delivery_date']), 'target_smv' => $row['target_smv'] ?? 0, 'production_minutes' => $row['production_minutes'] ?? 0, 'unit_price' => $row['price'] ?? 0, 'total_value' => $row['total_amount'] ?? 0, 'cm_pc' => $row['cm_pcs'] ?? 0, 'total_cm' => $row['total_cm'] ?? 0, 'consumption_dzn' => $row['consumption'] ?? 0, 'fabric_qnty' => $row['fabric_qnty'] ?? 0, // In mapJobAttributes() 'fabrication' => $row['fabrication'] ?? '', // Corrected from 'item_description' 'order_received_date' => $this->parseDate($row['order_received_date'] ?? null), 'remarks' => $row['remarks'] ?? null ]; } private function parseDate($value) { if (empty($value) || $value === '0000-00-00 00:00:00') { return null; } try { return \Carbon\Carbon::parse($value); } catch (\Exception $e) { Log::error("Failed to parse date: {$value}"); return null; } } public function rules(): array { return [ '*.buyer' => 'required|string|max:255', '*.or_no_ref_style_po' => 'required|string', '*.order_quantity' => 'required|numeric|min:0', // Make other fields nullable '*.delivery_date' => 'nullable|date', '*.target_smv' => 'nullable|numeric|min:0', '*.price' => 'nullable|numeric|min:0', '*.total_amount' => 'nullable|numeric|min:0', '*.cm_pcs' => 'nullable|numeric|min:0', '*.production_minutes' => 'nullable|numeric|min:0' ]; }