diff --git a/src/Command/ImportCMBBatteryDataCommand.php b/src/Command/ImportCMBBatteryDataCommand.php index 79e84571..30a6f4a4 100644 --- a/src/Command/ImportCMBBatteryDataCommand.php +++ b/src/Command/ImportCMBBatteryDataCommand.php @@ -148,19 +148,19 @@ class ImportCMBBatteryDataCommand extends Command // save battery manufacturer if not yet in system if (!isset($this->bmanu_hash[$normalized_manu])) { - $this->addBatteryManufacturer($battery_manufacturer); + $this->addBatteryManufacturer(strtoupper($battery_manufacturer)); } // save battery model if not yet in system if (!isset($this->bmodel_hash[$normalized_model])) { - $this->addBatteryModel($battery_model); + $this->addBatteryModel(strtoupper($battery_model)); } // save battery size if not yet in system if (!isset($this->bsize_hash[$normalized_size])) { - $this->addBatterySize($clean_size); + $this->addBatterySize(strtoupper($clean_size)); } // save battery if not yet in system diff --git a/src/Command/ImportCMBCarFixDataCommand.php b/src/Command/ImportCMBCarFixDataCommand.php index f1fa23f3..ff7f3b23 100644 --- a/src/Command/ImportCMBCarFixDataCommand.php +++ b/src/Command/ImportCMBCarFixDataCommand.php @@ -21,12 +21,15 @@ use App\Entity\CustomerVehicle; use App\Ramcar\CMBServiceType; use App\Ramcar\JOStatus; +use App\Ramcar\FuelType; +use App\Ramcar\VehicleStatusCondition; use DateTime; class ImportCMBCarFixDataCommand extends Command { // field index in csv file + const F_INDEX = 0; const F_CREATED_DATE = 1; const F_CASE_NO = 2; const F_INSURER = 3; @@ -61,7 +64,7 @@ class ImportCMBCarFixDataCommand extends Command protected $vmanu_hash; protected $vmake_hash; - protected $cust_hash; + protected $cv_hash; public function __construct(EntityManagerInterface $em) { @@ -106,6 +109,7 @@ class ImportCMBCarFixDataCommand extends Command $em = $this->em; $row_num = 0; + $invalid_entries = []; error_log('Processing CarFix data csv file...'); while (($fields = fgetcsv($fh)) !== false) { @@ -116,6 +120,7 @@ class ImportCMBCarFixDataCommand extends Command } // get the information + $entry_num = trim($fields[self::F_INDEX]); $date_create = trim($fields[self::F_CREATED_DATE]); $case_number = trim($fields[self::F_CASE_NO]); $insurer = trim($fields[self::F_INSURER]); @@ -143,16 +148,32 @@ class ImportCMBCarFixDataCommand extends Command //error_log($date_create . ' ' . $case_number . ' ' . $driver . ' ' . $customer_name . ' ' . $remark . ' ' . $satisfaction); // get customer vehicle - // parse car_model - // vehicle manufacturer is the first entry - // vehicle model is the 2nd entry - // check if manufacturer is in hash - // if not, add to csv of unprocessed entries - // if so, check if model is in hash - // if not, add to csv of unprocessed entries + $v_status = $this->processVehicleInfo($car_model); + if ($v_status != null) + { + error_log($v_status . ' ' . $car_model); + $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, + $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, + $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, + $trade_in, $replaced_by, $remark, $satisfaction, $v_status); + + // move to next entry + continue; + } // check batteries - // if battery info not in hash, add to csv of unprocessed entries + $batt_status = $this->processBatteryInfo($batt_model, $batt_size); + if ($batt_status != null) + { + error_log($batt_status); + $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, + $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, + $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, + $trade_in, $replaced_by, $remark, $satisfaction, $batt_status); + + // move to next entry + continue; + } $new_jo = new JobOrder(); @@ -204,20 +225,176 @@ class ImportCMBCarFixDataCommand extends Command $new_jo->setStatus(JOStatus::FULFILLED); $new_jo->addMeta('status', $status); - // plate number == vehicle_number. Use as key to cust_hash + // plate number == vehicle_number. Use as key to cv_hash $clean_plate = $this->cleanPlateNumber($vehicle_number); //error_log($clean_plate . ' ' . $new_jo->getServiceType()); // check if plate number has been added - if (!isset($this->cust_hash[$clean_plate])) - $this->addCustomer($clean_plate, $customer_name, $customer_mobile); - + if (!isset($this->cv_hash[$clean_plate])) + { + $cust = $this->addCustomer($customer_name, $customer_mobile); + $cv = $this->addCustomerVehicle($clean_plate, $car_model, $cust); + } + else + { + // get customer vehicle from hash + $cv = $this->cv_hash[$clean_plate]; + $cust = $cv->getCustomer(); + } + + $new_jo->setCustomer($cust) + ->setCustomerVehicle($cv); + $row_num++; } + $this->em->flush(); + $this->em->clear(); + + // check for invalid entries. if any, write to csv + return 0; } + protected function processVehicleInfo($car_model) + { + // vehicle manufacturer is the first entry + // vehicle model is the 2nd entry + whatever follows + $v_array = explode(' ', $car_model); + + // manufacturer + $v_manufacturer = trim($v_array[0]); + + // get model + $model_info = ''; + $v_model = ''; + for ($i = 1; $i < count($v_array); $i++) + { + $model_info = $model_info . ' ' . trim($v_array[$i]); + } + + $v_model = trim($model_info); + //error_log($v_manufacturer . ' ' . $v_model); + + // check if manufacturer is in hash + if (!isset($this->vmanu_hash[$v_manufacturer])) + { + //error_log($v_manufacturer . ' invalid.'); + return 'Vehicle manufacturer not in system.'; + } + + // check if manufacturer and make is in hash + if (!isset($this->vmake_hash[$v_manufacturer][$v_model])) + { + //error_log($v_model . ' invalid.'); + return 'Vehicle model not in system.'; + } + // car model info valid + return null; + } + + protected function processBatteryInfo($batt_model, $batt_size) + { + // check if battery model is in hash + if (!isset($this->bmodel_hash[$batt_model])) + return 'Battery model not in system.'; + + // check if battery size is in hash + if (!isset($this->bsize_hash[$batt_size])) + return 'Battery size not in system.'; + + // battery info valid + return null; + } + + protected function addCustomer($name, $mobile) + { + $new_cust = new Customer(); + + $new_cust->setFirstName($name) + ->setLastName('') + ->setPhoneMobile($mobile); + + $this->em->persist($new_cust); + + return $new_cust; + } + + protected function addCustomerVehicle($plate_num, $car_model, $customer) + { + $new_cv = new CustomerVehicle(); + + // get vehicle from hash + $v_array = explode(' ', $car_model); + + // manufacturer + $v_manufacturer = trim($v_array[0]); + + // get model + $model_info = ''; + $v_model = ''; + for ($i = 1; $i < count($v_array); $i++) + { + $model_info = $model_info . ' ' . trim($v_array[$i]); + } + + $v_model = trim($model_info); + + $vehicle = $this->vmake_hash[$v_manufacturer][$v_model]; + + $new_cv->setCustomer($customer) + ->setPlateNumber($plate_num) + ->setStatusCondition(VehicleStatusCondition::BRAND_NEW) + ->setModelYear('') + ->setColor('') + ->setFuelType(FuelType::GAS) + ->setHasMotoliteBattery(true) + ->setVehicle($vehicle); + + $this->em->persist($new_cv); + + // add customer vehicle to cv_hash + $this->cv_hash[$plate_num] = $new_cv; + + return $new_cv; + } + + protected function addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $vehicle_number, $car_model, + $nature_of_call, $service_needed, $location, $state, $driver, $truck, $workshop_arrival_time, + $status, $customer_name, $customer_mobile, $reference, $odometer, $batt_model, $batt_size, + $trade_in, $replaced_by, $remark, $satisfaction, $v_status) + { + $inv_entry = [ + 'number' => $entry_num, + 'created_date' => $date_create, + 'case_number' => $case_number, + 'insurer' => $insurer, + 'vehicle_number' => $vehicle_number, + 'car_model' => $car_model, + 'nature_of_call' => $nature_of_call, + 'service_needed' => $service_needed, + 'location' => $location, + 'state' => $state, + 'driver' => $driver, + 'truck' => $truck, + 'workshop_arrival_time' => $workshop_arrival_time, + 'status' => $status, + 'customer_name' => $customer_name, + 'customer_phone_number' => $customer_mobile, + 'reference' => $reference, + 'odometer' => $odometer, + 'batt_model' => $batt_model, + 'batt_size' => $batt_size, + 'batt_trade_in' => $trade_in, + 'replaced_by' => $replaced_by, + 'remark' => $remark, + 'satisfaction' => $satisfaction, + 'reason' => $v_status, + ]; + + return $inv_entry; + } + protected function loadBatteryManufacturers() { $this->bmanu_hash = []; @@ -295,10 +472,6 @@ class ImportCMBCarFixDataCommand extends Command } } - public function addCustomer($plate_num, $name, $mobile) - { - } - protected function normalizeName($name) { $normalized_key = trim(strtolower($name)); diff --git a/src/Command/ImportCMBVehicleCompatibilityCommand.php b/src/Command/ImportCMBVehicleCompatibilityCommand.php index 4034aec8..47393c01 100644 --- a/src/Command/ImportCMBVehicleCompatibilityCommand.php +++ b/src/Command/ImportCMBVehicleCompatibilityCommand.php @@ -222,8 +222,8 @@ class ImportCMBVehicleCompatibilityCommand extends Command //} // vehicle info - $manufacturer = trim($fields[self::F_VEHICLE_MANUFACTURER]); - $make = trim($fields[self::F_VEHICLE_MAKE]); + $manufacturer = trim(strtolower($fields[self::F_VEHICLE_MANUFACTURER])); + $make = trim(strtolower($fields[self::F_VEHICLE_MAKE])); $year = trim($fields[self::F_VEHICLE_YEAR]); // vehicle data @@ -298,7 +298,7 @@ class ImportCMBVehicleCompatibilityCommand extends Command // save to db $vehicle_manufacturer = new VehicleManufacturer(); - $vehicle_manufacturer->setName($name); + $vehicle_manufacturer->setName(strtoupper($name)); $this->em->persist($vehicle_manufacturer); $this->em->flush(); @@ -332,7 +332,7 @@ class ImportCMBVehicleCompatibilityCommand extends Command } $vehicle->setManufacturer($vmanu) - ->setMake($make) + ->setMake(strtoupper($make)) ->setModelYearFrom($year_from) ->setModelYearTo($year_to);