argument('file'); if (!file_exists($filePath)) { $this->error("Không tìm thấy file: {$filePath}"); return 1; } $this->info("Đang đọc file Excel..."); $spreadsheet = IOFactory::load($filePath); $worksheet = $spreadsheet->getActiveSheet(); $rows = $worksheet->toArray(); $count = 0; foreach ($rows as $index => $row) { if ($index === 0 || empty($row[1])) continue; // Bỏ qua header hoặc CMND trống // 1. Xử lý số điện thoại (Tách nếu có nhiều số) $phoneRaw = $row[5] ?? ''; $phones = preg_split('/[,\/ \n]+/', $phoneRaw, -1, PREG_SPLIT_NO_EMPTY); $mainPhone = $phones[0] ?? null; $secondaryPhones = array_slice($phones, 1); // 2. Xử lý ngày tháng (Excel thường lưu ngày là số serial) $dob = $this->parseExcelDate($row[4]); $issueDate = $this->parseExcelDate($row[7]); Customer::updateOrCreate( ['cmnd_cccd' => (string)$row[1]], [ 'title' => $row[2], 'full_name' => $row[3], 'dob' => $dob, 'phone' => $mainPhone, 'secondary_phones' => $secondaryPhones, 'email' => $row[6], 'id_issue_date' => $issueDate, 'id_issue_place' => $row[8], 'permanent_address' => $row[9], 'contact_address' => $row[10], 'type' => 'INDIVIDUAL', ] ); $count++; if ($count % 10 === 0) $this->line("Đã import: {$count} khách hàng..."); } $this->info("--- TẠO DỮ LIỆU MẪU CÔNG TY ---"); $this->createSampleCompany(); $this->info("Thành công! Đã import {$count} khách hàng và tạo 1 cặp Công ty + Người đại diện mẫu."); return 0; } private function parseExcelDate($value) { if (empty($value)) return null; try { if (is_numeric($value)) { return Carbon::instance(ExcelDate::excelToDateTimeObject($value))->format('Y-m-d'); } return Carbon::parse(str_replace('/', '-', $value))->format('Y-m-d'); } catch (\Exception $e) { return null; } } private function createSampleCompany() { // 1. Tạo người đại diện (Cá nhân) $rep = Customer::updateOrCreate( ['cmnd_cccd' => '079083000123'], [ 'title' => 'Ông', 'full_name' => 'NGUYỄN VĂN ĐẠI DIỆN', 'phone' => '0909123456', 'permanent_address' => '123 Đường ABC, Phường 1, Quận 1, TP.HCM', 'contact_address' => '123 Đường ABC, Phường 1, Quận 1, TP.HCM', 'type' => 'INDIVIDUAL', ] ); // 2. Tạo công ty liên kết với người đại diện trên Customer::updateOrCreate( ['tax_code' => '0102030405'], [ 'type' => 'COMPANY', 'full_name' => 'CÔNG TY TNHH BẤT ĐỘNG SẢN THỊNH VƯỢNG', 'cmnd_cccd' => '0102030405', // GPKD 'representative_id' => $rep->id, 'permanent_address' => '456 Đường XYZ, Phường 2, Quận Tân Bình, TP.HCM', // Trụ sở chính 'contact_address' => '456 Đường XYZ, Phường 2, Quận Tân Bình, TP.HCM', 'phone' => '02838111222', ] ); $this->info("Đã tạo: Công ty Thịnh Vượng (Đại diện bởi: {$rep->full_name})"); } }