diff --git a/src/Command/ImportCMBLegacyJobOrderCommand.php b/src/Command/ImportCMBLegacyJobOrderCommand.php index be25c4b3..6ee7022a 100644 --- a/src/Command/ImportCMBLegacyJobOrderCommand.php +++ b/src/Command/ImportCMBLegacyJobOrderCommand.php @@ -102,8 +102,8 @@ class ImportCMBLegacyJobOrderCommand extends Command $customer_mobile = trim($fields[self::F_CUSTOMER_PHONE_NO]); $reference = trim($fields[self::F_OUR_REFERENCE]); $odometer = trim($fields[self::F_ODOMETER]); - $batt_model = trim(strtolower($fields[self::F_BATT_MODEL])); - $batt_size = trim(strtolower($fields[self::F_BATT_SIZE])); + $batt_model = trim($fields[self::F_BATT_MODEL]); + $batt_size = trim($fields[self::F_BATT_SIZE]); $trade_in = trim($fields[self::F_BATT_TRADE_IN]); $replaced_by = trim($fields[self::F_REPLACED_BY]); $remark = trim($fields[self::F_REMARK]); diff --git a/src/Command/ImportCMBVehicleCompatibilityCommand.php b/src/Command/ImportCMBVehicleCompatibilityCommand.php index 47393c01..8add2968 100644 --- a/src/Command/ImportCMBVehicleCompatibilityCommand.php +++ b/src/Command/ImportCMBVehicleCompatibilityCommand.php @@ -412,7 +412,7 @@ class ImportCMBVehicleCompatibilityCommand extends Command $vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll(); foreach ($vmanus as $vmanu) { - $name = $vmanu->getName(); + $name = $this->normalizeName($vmanu->getName()); $this->vmanu_hash[$name] = $vmanu; } } @@ -425,7 +425,7 @@ class ImportCMBVehicleCompatibilityCommand extends Command foreach ($vmakes as $vmake) { $manufacturer = $vmake->getManufacturer()->getName(); - $make = $vmake->getMake(); + $make = $this->normalizeName($vmake->getMake()); $this->vmake_hash[$manufacturer][$make] = $vmake; } diff --git a/src/Command/MigrateCMBLegacyJobOrderCommand.php b/src/Command/MigrateCMBLegacyJobOrderCommand.php index 4be27570..72a114c4 100644 --- a/src/Command/MigrateCMBLegacyJobOrderCommand.php +++ b/src/Command/MigrateCMBLegacyJobOrderCommand.php @@ -22,6 +22,13 @@ use App\Entity\CustomerVehicle; use App\Entity\Invoice; use App\Entity\InvoiceItem; +use App\Ramcar\CMBServiceType; +use App\Ramcar\JOStatus; +use App\Ramcar\FuelType; +use App\Ramcar\VehicleStatusCondition; + +use DateTime; + class MigrateCMBLegacyJobOrderCommand extends Command { protected $em; @@ -50,6 +57,9 @@ class MigrateCMBLegacyJobOrderCommand extends Command $this->loadVehicleManufacturers(); $this->loadVehicleMakes(); + // load existing customer vehicle data + $this->loadCustomerVehicles(); + parent::__construct(); } @@ -74,11 +84,11 @@ class MigrateCMBLegacyJobOrderCommand extends Command // get the entry information $entry_num = $jo_entry['entry_num']; - $date_created = $jo_entry['created_date']; + $date_create = $jo_entry['created_date']; $case_number = $jo_entry['case_number']; $insurer = $jo_entry['insurer']; $plate_number = $this->cleanPlateNumber($jo_entry['vehicle_number']); - $car_model = $jo_entry['car_model']; + $car_model = $this->normalizeName($jo_entry['car_model']); $nature_of_call = $jo_entry['nature_of_call']; $service_needed = $jo_entry['service_needed']; $location = $jo_entry['location']; @@ -91,8 +101,8 @@ class MigrateCMBLegacyJobOrderCommand extends Command $customer_mobile = $jo_entry['customer_phone_number']; $reference = $jo_entry['reference']; $odometer = $jo_entry['odometer']; - $batt_model = $jo_entry['batt_model']; - $batt_size = $jo_entry['batt_size']; + $batt_model = $this->normalizeName($jo_entry['batt_model']); + $batt_size = $this->normalizeName($jo_entry['batt_size']); $batt_trade_in = $jo_entry['batt_trade_in']; $replaced_by = $jo_entry['replaced_by']; $remark = $jo_entry['remark']; @@ -103,10 +113,10 @@ class MigrateCMBLegacyJobOrderCommand extends Command 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, + $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $plate_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); + $batt_trade_in, $replaced_by, $remark, $satisfaction, $v_status); // move to next entry continue; @@ -116,11 +126,11 @@ class MigrateCMBLegacyJobOrderCommand extends Command $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, + error_log($batt_status . ' ' . $batt_model . ' ' . $batt_size); + $invalid_entries[] = $this->addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $plate_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); + $batt_trade_in, $replaced_by, $remark, $satisfaction, $batt_status); // move to next entry continue; @@ -178,19 +188,16 @@ class MigrateCMBLegacyJobOrderCommand extends Command $new_jo->addMeta('status', $status); // 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->cv_hash[$clean_plate])) + if (!isset($this->cv_hash[$plate_number])) { $cust = $this->addCustomer($customer_name, $customer_mobile); - $cv = $this->addCustomerVehicle($clean_plate, $car_model, $cust); + $cv = $this->addCustomerVehicle($plate_number, $car_model, $cust); } else { // get customer vehicle from hash - $cv = $this->cv_hash[$clean_plate]; + $cv = $this->cv_hash[$plate_number]; $cust = $cv->getCustomer(); } @@ -198,10 +205,37 @@ class MigrateCMBLegacyJobOrderCommand extends Command ->setCustomerVehicle($cv); // create the invoice + $invoice_item = new InvoiceItem(); + $invoice = new Invoice(); - $this->em->persist($new_jo); + // get the battery + // cannot get the battery from the hash since we have no manufacturer data + // have to find the battery using model and size + $b_model = $this->bmodel_hash[$batt_model]; + $b_size = $this->bsize_hash[$batt_size]; - $this->em->detach($row[0]); + $battery = $this->findBattery($b_model, $b_size); + if ($battery != null) + { + // assume quantity of 1 + $invoice_item->setBattery($battery) + ->setPrice($battery->getSellingPrice()) + ->setQuantity(1) + ->setInvoice($invoice); + } + + // TODO: invoice need to check if entry was cancelled or fulfilled + // if fulfilled, set date_paid else set date_cancel + // check if trade_in + // set InvoiceStatus to fulfilled if fulfilled, cancelled if cancelled + // compute totals + + $invoice->setJobOrder($new_jo); + + + //$this->em->persist($new_jo); + + $this->em->detach($row[0]); } $this->em->flush(); @@ -247,6 +281,132 @@ class MigrateCMBLegacyJobOrderCommand extends Command 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 addInvalidEntry($entry_num, $date_create, $case_number, $insurer, $plate_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, + $batt_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' => $plate_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' => $batt_trade_in, + 'replaced_by' => $replaced_by, + 'remark' => $remark, + 'satisfaction' => $satisfaction, + 'reason' => $v_status, + ]; + + return $inv_entry; + } + + 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 findBattery($batt_model, $batt_size) + { + $battery = null; + $b_query = $this->em->createQuery('SELECT battery from App\Entity\Battery battery + INNER JOIN battery.model bm + INNER JOIN battery.size bs + WHERE battery.model = :bmodel + AND battery.size = :bsize'); + $b_query->setParameter('bmodel', $batt_model) + ->setParameter('bsize', $batt_size); + + $b_results = $b_query->iterate(); + + foreach ($b_results as $b_result) + { + $battery = $b_result[0]; + //error_log($battery->getID() . ' ' . $battery->getProductCode()); + + $this->em->detach($b_result[0]); + } + + return $battery; + } + protected function loadBatteryManufacturers() { $this->bmanu_hash = []; @@ -305,7 +465,7 @@ class MigrateCMBLegacyJobOrderCommand extends Command $vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll(); foreach ($vmanus as $vmanu) { - $name = $vmanu->getName(); + $name = $this->normalizeName($vmanu->getName()); $this->vmanu_hash[$name] = $vmanu; } } @@ -317,13 +477,26 @@ class MigrateCMBLegacyJobOrderCommand extends Command $vmakes = $this->em->getRepository(Vehicle::class)->findAll(); foreach ($vmakes as $vmake) { - $manufacturer = $vmake->getManufacturer()->getName(); - $make = $vmake->getMake(); + $manufacturer = $this->normalizeName($vmake->getManufacturer()->getName()); + $make = $this->normalizeName($vmake->getMake()); $this->vmake_hash[$manufacturer][$make] = $vmake; } } + protected function loadCustomerVehicles() + { + $this->cv_hash = []; + + $cvs = $this->em->getRepository(CustomerVehicle::class)->findAll(); + foreach ($cvs as $cv) + { + $plate_number = $this->cleanPlateNumber($cv->getPlateNumber()); + + $this->cv_hash[$plate_number] = $cv; + } + } + protected function normalizeName($name) { $normalized_key = trim(strtolower($name));