From bfa39f4640ce01ac6874f7f44cc5f638f241111b Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Wed, 9 Jan 2019 17:01:08 +0800 Subject: [PATCH] Add legacy job order and details migration commands #168 --- src/Command/ImportLegacyJobOrderCommand.php | 315 ++++++++++++++++++-- src/Entity/LegacyJobOrder.php | 186 +++++++++--- src/Entity/LegacyJobOrderRow.php | 72 +++++ src/Ramcar/LegacyOrigin.php | 41 +++ src/Ramcar/LegacyTransactionType.php | 37 +++ 5 files changed, 583 insertions(+), 68 deletions(-) create mode 100644 src/Entity/LegacyJobOrderRow.php create mode 100644 src/Ramcar/LegacyOrigin.php create mode 100644 src/Ramcar/LegacyTransactionType.php diff --git a/src/Command/ImportLegacyJobOrderCommand.php b/src/Command/ImportLegacyJobOrderCommand.php index 07a300cb..8161fbae 100644 --- a/src/Command/ImportLegacyJobOrderCommand.php +++ b/src/Command/ImportLegacyJobOrderCommand.php @@ -18,7 +18,10 @@ use App\Entity\PlateNumber; use App\Ramcar\LegacyBattery; use App\Ramcar\LegacyVehicleManufacturer; +use App\Ramcar\LegacyTransactionType; use App\Ramcar\LegacyVehicle; +use App\Ramcar\LegacyOrigin; + use App\Ramcar\WarrantyClass; use App\Ramcar\WarrantyStatus; @@ -32,6 +35,12 @@ class ImportLegacyJobOrderCommand extends Command protected $bsize_hash; protected $vmfg_hash; protected $vehicle_hash; + protected $jo_hash; + + protected $row_fields; + protected $data_fields; + protected $row_max_field_size; + protected $data_max_field_size; public function __construct(ObjectManager $om) { @@ -42,6 +51,9 @@ class ImportLegacyJobOrderCommand extends Command $this->loadVehicleManufacturers(); $this->loadVehicles(); + $this->jo_hash = []; + + parent::__construct(); } @@ -245,8 +257,13 @@ class ImportLegacyJobOrderCommand extends Command $service_types = []; $no_sizes = []; + // files $plate_outfile = fopen('/tmp/plate_numbers.csv', 'a'); $warr_outfile = fopen('/tmp/warranty.csv', 'a'); + $jo_outfile = fopen('/tmp/legacy_jo.csv', 'a'); + $jorow_outfile = fopen('/tmp/legacy_jo_row.csv', 'a'); + + $this->initializeMaxFieldCounters(); // loop through rows $save_plates = []; @@ -256,13 +273,6 @@ class ImportLegacyJobOrderCommand extends Command $row_num++; // $output->writeln("Parsing row " . $row_num . "..."); - // skip taxes - if ($fields[14] == 'Bureau of Internal Revenue') - { - // $output->writeln('Skipping BIR line'); - continue; - } - // ignore first 5 rows if ($row_num <= 5) continue; @@ -275,6 +285,28 @@ class ImportLegacyJobOrderCommand extends Command exit; } + $this->processLegacyTransaction($fields); + + // skip taxes + if ($fields[14] == 'Bureau of Internal Revenue') + { + // $output->writeln('Skipping BIR line'); + continue; + } + + // clean plate numbers + $id = $fields[0]; + $plate_num = Warranty::cleanPlateNumber($fields[14]); + if (!$plate_num) + { + // $output->writeln('Invalid plate number - ' . $fields[14]); + $this->jo_hash[$id]['data']['plate_number'] = null; + continue; + } + else + $this->jo_hash[$id]['data']['plate_number'] = $plate_num; + + // let's track purchases first if ($fields[5] != 'PURCHASE') continue; @@ -283,7 +315,7 @@ class ImportLegacyJobOrderCommand extends Command $found_battery = $this->findBattery($fields[92], $batt_model, $batt_size); if (!$found_battery) { - $output->writeln('battery not found - ' . $fields[92]); + // $output->writeln('battery not found - ' . $fields[92]); continue; } @@ -291,7 +323,7 @@ class ImportLegacyJobOrderCommand extends Command $found_vehicle = $this->findVehicle($fields[15], $fields[16], $fields[17], $vehicle); if (!$found_vehicle) { - $output->writeln('vehicle not found - ' . $fields[15] . ' - ' . $fields[16]); + // $output->writeln('vehicle not found - ' . $fields[15] . ' - ' . $fields[16]); continue; } @@ -299,19 +331,11 @@ class ImportLegacyJobOrderCommand extends Command $trans_types[$fields[5]] = 1; $service_types[$fields[7]] = $fields[5]; - // clean plate numbers - $plate_num = Warranty::cleanPlateNumber($fields[14]); - if (!$plate_num) - { - $output->writeln('Invalid plate number - ' . $fields[14]); - continue; - } - fwrite($plate_outfile, $plate_num . ',' . $vehicle['id'] . "\n"); if (isset($fields[107])) { - echo 'warranty class - ' . $fields[107] . "\n"; + // echo 'warranty class - ' . $fields[107] . "\n"; $warr_class = WarrantyClass::convertFromLegacy($fields[107]); @@ -323,9 +347,9 @@ class ImportLegacyJobOrderCommand extends Command // model and size $line = $batt_model_id . ',' . $batt_size_id . ','; - echo 'wclass - ' . $warr_class . "\n"; - echo 'serial - ' . $fields[110] . "\n"; - echo 'expiry - ' . $fields[109] . "\n"; + // echo 'wclass - ' . $warr_class . "\n"; + // echo 'serial - ' . $fields[110] . "\n"; + // echo 'expiry - ' . $fields[109] . "\n"; // serial if (isset($fields[110]) && strlen(trim($fields[110])) > 0) @@ -381,6 +405,7 @@ class ImportLegacyJobOrderCommand extends Command } } + /* // check if we marked it already if (isset($save_plates[$plate_num])) @@ -453,6 +478,8 @@ class ImportLegacyJobOrderCommand extends Command */ } + // print_r($this->jo_hash); + // $em->flush(); // print_r($trans_types); @@ -466,8 +493,250 @@ class ImportLegacyJobOrderCommand extends Command // print_r($this->vehicle_hash); // print_r($no_makes); + /* + print_r($this->data_max_field_size); + print_r($this->row_max_field_size); + */ + + // save job order + foreach ($this->jo_hash as $jo) + { + $id = $jo['data']['id']; + + $line = $this->exportJOData($jo['data']); + fwrite($jo_outfile, $line . "\n"); + + foreach ($jo['rows'] as $jo_row) + fputcsv($jorow_outfile, $jo_row); + } + + fclose($plate_outfile); fclose($warr_outfile); + fclose($jo_outfile); + fclose($jorow_outfile); + } + + protected function initializeMaxFieldCounters() + { + $this->data_fields = [ + 'trans_type', + 'origin', + + 'car_brand', + 'car_make', + 'car_model', + 'car_color', + + 'cust_name', + 'cust_first_name', + 'cust_middle_name', + 'cust_last_name', + 'cust_contact', + 'cust_mobile', + 'cust_landline', + + 'delivery_instructions', + 'agent_notes_1', + 'delivery_date', + 'delivery_time', + 'advance_order', + 'stage', + 'cancel_reason', + 'cancel_reason_specify', + 'payment_method', + + 'prepared_by', + + 'dispatch_time', + 'dispatch_date', + 'dispatched_by', + + 'address', + 'landmark', + 'date_purchase', + ]; + + $this->row_fields = [ + 'item', + 'qty', + 'price', + 'price_level', + 'status', + 'account', + 'enrollee', + ]; + + // initialize max field size counter + $this->data_max_field_size = [ + 'trans_type' => 0, + 'origin' => 0, + + 'car_brand' => 0, + 'car_make' => 0, + 'car_model' => 0, + 'car_color' => 0, + + 'cust_name' => 0, + 'cust_first_name' => 0, + 'cust_middle_name' => 0, + 'cust_last_name' => 0, + 'cust_contact' => 0, + 'cust_mobile' => 0, + 'cust_landline' => 0, + + 'delivery_instructions' => 0, + 'agent_notes_1' => 0, + 'delivery_date' => 0, + 'delivery_time' => 0, + 'advance_order' => 0, + 'stage' => 0, + 'cancel_reason' => 0, + 'cancel_reason_specify' => 0, + 'payment_method' => 0, + + 'prepared_by' => 0, + + 'dispatch_time' => 0, + 'dispatch_date' => 0, + 'dispatched_by' => 0, + + 'address' => 0, + 'landmark' => 0, + 'date_purchase' => 0, + ]; + + $this->row_max_field_size = [ + 'item' => 0, + 'qty' => 0, + 'price' => 0, + 'price_level' => 0, + 'status' => 0, + 'account' => 0, + 'enrollee' => 0, + ]; + + } + + protected function processLegacyTransaction($fields) + { + // echo "trans type - " . $fields[5] . "\n"; + // echo "origin - " . $fields[9] . "\n"; + // echo "stage - " . $fields[34] . "\n"; + // echo "payment method - " . $fields[37] . "\n"; + // echo "price level - " . $fields[95] . "\n"; + + $id = $fields[0]; + + $trans_type = LegacyTransactionType::translate($fields[5]); + $origin = LegacyOrigin::translate($fields[9]); + + // consolidate based on jo id + if (!isset($this->jo_hash[$id])) + { + $this->jo_hash[$id] = [ + // 'raw' => [], + 'data' => [], + 'rows' => [], + ]; + + $date_trans = DateTime::createFromFormat('m/d/Y', $fields[3]); + + // labelled data + $this->jo_hash[$id]['data'] = [ + 'id' => $id, + 'date_trans' => $date_trans->format('Ymd'), + 'trans_type' => $trans_type, + 'origin' => $fields[9], + + 'car_brand' => $fields[15], + 'car_make' => $fields[16], + 'car_model' => $fields[17], + 'car_color' => $fields[18], + + 'cust_name' => $fields[19], + 'cust_first_name' => $fields[20], + 'cust_middle_name' => $fields[21], + 'cust_last_name' => $fields[22], + 'cust_contact' => $fields[23], + 'cust_mobile' => $fields[24], + 'cust_landline' => $fields[25], + + 'delivery_instructions' => $fields[29], + 'agent_notes_1' => $fields[30], + 'delivery_date' => $fields[31], + 'delivery_time' => $fields[32], + 'advance_order' => $fields[33], + 'stage' => $fields[34], + 'cancel_reason' => $fields[35], + 'cancel_reason_specify' => $fields[36], + 'payment_method' => $fields[37], + + 'prepared_by' => $fields[39], + + 'dispatch_time' => $fields[75], + 'dispatch_date' => $fields[76], + 'dispatched_by' => $fields[77], + + 'address' => $fields[82], + 'landmark' => $fields[83], + 'date_purchase' => $fields[84], + ]; + + + // max field size + foreach ($this->data_fields as $data_field) + { + $len = strlen($this->jo_hash[$id]['data'][$data_field]); + // echo "$data_field - $len vs " . $this->data_max_field_size[$data_field] . "\n"; + + if ($len > $this->data_max_field_size[$data_field]) + $this->data_max_field_size[$data_field] = $len; + } + /* + // raw + for ($x = 0; $x < 88; $x++) + $this->jo_hash[$id]['raw'][$x] = $fields[$x]; + */ + } + + $row = []; + /* + for ($x = 88; $x <= 165; $x++) + $row[$x - 88] = $fields[$x]; + */ + $row = [ + 'id' => $id, + 'item' => $fields[92], + 'qty' => $fields[93], + 'price' => $fields[114], + 'price_level' => $fields[95], + 'status' => $fields[129], + 'account' => $fields[128], + 'enrollee' => $fields[134], + ]; + $this->jo_hash[$id]['rows'][] = $row; + + // max row field size + foreach ($this->row_fields as $row_field) + { + $len = strlen($row[$row_field]); + if ($len > $this->row_max_field_size[$row_field]) + $this->row_max_field_size[$row_field] = $len; + } + } + + protected function exportJOData($data) + { + $line_data = []; + + foreach ($data as $field => $value) + { + $enc_value = str_replace('\\', '\\\\', $value); + $line_data[] = str_replace('|', '\\|', $enc_value); + } + + return implode('|', $line_data); } protected function loadBatteryModels() @@ -603,7 +872,7 @@ class ImportLegacyJobOrderCommand extends Command $legacy_vmfg = LegacyVehicleManufacturer::translate($found_vmfg); if ($legacy_vmfg == null) { - echo "vmfg not found - $vmfg_field\n"; + // echo "vmfg not found - $vmfg_field\n"; return false; } @@ -619,7 +888,7 @@ class ImportLegacyJobOrderCommand extends Command if ($legacy_make == null) { $no_makes[$found_make] = $found_vmfg . ' - ' . $found_make; - echo "vmake not found - $vmfg_field - $vmake_field\n"; + // echo "vmake not found - $vmfg_field - $vmake_field\n"; return false; } diff --git a/src/Entity/LegacyJobOrder.php b/src/Entity/LegacyJobOrder.php index d9143352..345fc724 100644 --- a/src/Entity/LegacyJobOrder.php +++ b/src/Entity/LegacyJobOrder.php @@ -188,65 +188,161 @@ class LegacyJobOrder */ protected $id; - protected $data; + /** + * @ORM\Column(type="date") + */ + protected $trans_date; -/* - protected $type; - protected $trans_num; - protected $trans_date; - protected $jo_num; - protected $meh_trans_type; - protected $ref_jo_num; - protected $service_type; - protected $warr_status; - protected $trans_origin; - protected $exist_batt; - protected $exist_batt_type; - protected $reco_batt; - protected $alt_batt; - protected $car_plate_num; + /** + * @ORM\Column(type="string", length=10) + */ + protected $trans_type; + + /** + * @ORM\Column(type="string", length=30) + */ + protected $origin; + + /** + * @ORM\Column(type="string", length=20) + */ protected $car_brand; + + /** + * @ORM\Column(type="string", length=50) + */ protected $car_make; + + /** + * @ORM\Column(type="string", length=20) + */ protected $car_model; + + /** + * @ORM\Column(type="string", length=40) + */ protected $car_color; + + /** + * @ORM\Column(type="string", length=90) + */ protected $cust_name; + + /** + * @ORM\Column(type="string", length=35) + */ protected $cust_first_name; + + /** + * @ORM\Column(type="string", length=15) + */ protected $cust_middle_name; + + /** + * @ORM\Column(type="string", length=35) + */ protected $cust_last_name; - protected $cust_contact_num; - protected $cust_mobile_num; - protected $cust_landline_num; - protected $invoice_name; - protected $new_name; - protected $new_invoice_name; + + /** + * @ORM\Column(type="string", length=25) + */ + protected $cust_contact; + + /** + * @ORM\Column(type="string", length=10) + */ + protected $cust_mobile; + + /** + * @ORM\Column(type="string", length=25) + */ + protected $cust_landline; + + /** + * @ORM\Column(type="string", length=2000) + */ protected $delivery_instructions; - protected $agent_notes; - protected $del_date; - protected $del_time; + + /** + * @ORM\Column(type="string", length=4000) + */ + protected $agent_notes_1; + + /** + * @ORM\Column(type="string", length=10) + */ + protected $delivery_date; + + /** + * @ORM\Column(type="string", length=10) + */ + protected $delivery_time; + + /** + * @ORM\Column(type="string", length=3) + */ protected $advance_order; + + /** + * @ORM\Column(type="string", length=30) + */ protected $stage; + + /** + * @ORM\Column(type="string", length=40) + */ protected $cancel_reason; - protected $specify_reason; + + /** + * @ORM\Column(type="string", length=2000) + */ + protected $cancel_reason_specify; + + /** + * @ORM\Column(type="string", length=30) + */ protected $payment_method; - protected $change_for; - protected $perpared_by; - protected $curr_batt_serial; - protected $curr_batt_dr_num; - protected $curr_batt_date_replace; - protected $curr_batt_brand; - protected $curr_batt_type; - protected $curr_batt_issued_by; - protected $curr_batt_issued_direct; - protected $orig_batt_serial; - protected $orig_batt_or_num; - protected $orig_batt_date_purchase; - protected $orig_batt_brand; - protected $orig_batt_type; - protected $orig_batt_pur_from; - protected $orig_batt_pur_from_direct; - protected $orig_batt_warr_expire_date; - protected $orig_trade_in_batt; -*/ + + /** + * @ORM\Column(type="string", length=30) + */ + protected $prepared_by; + + /** + * @ORM\Column(type="string", length=10) + */ + protected $dispatch_time; + + /** + * @ORM\Column(type="string", length=10) + */ + protected $dispatch_date; + + /** + * @ORM\Column(type="string", length=30) + */ + protected $dispatched_by; + + /** + * @ORM\Column(type="string", length=200) + */ + protected $address; + + /** + * @ORM\Column(type="string", length=320) + */ + protected $landmark; + + /** + * @ORM\Column(type="string", length=10) + */ + protected $date_purchase; + + /** + * @ORM\Column(type="string", length=20) + */ + protected $plate_number; + public function setID($id) diff --git a/src/Entity/LegacyJobOrderRow.php b/src/Entity/LegacyJobOrderRow.php new file mode 100644 index 00000000..6927afb2 --- /dev/null +++ b/src/Entity/LegacyJobOrderRow.php @@ -0,0 +1,72 @@ +id = $id; + return $this; + } + + public function getID() + { + return $this->id; + } +} diff --git a/src/Ramcar/LegacyOrigin.php b/src/Ramcar/LegacyOrigin.php new file mode 100644 index 00000000..6fb9b90c --- /dev/null +++ b/src/Ramcar/LegacyOrigin.php @@ -0,0 +1,41 @@ + 'Direct', + 'facebook' => 'Facebook', + 'hotline' => 'Hotline', + 'online' => 'Online', + 'website' => 'Website', + ]; + + public static function translate($origin) + { + $clean_origin = strtolower(trim($origin)); + + switch ($clean_origin) + { + case 'direct': + return self::DIRECT; + case 'facebook': + return self::FACEBOOK; + case 'hotline': + return self::HOTLINE; + case 'online (24 hour window time)': + return self::ONLINE; + case 'website': + return self::WEBSITE; + } + + return false; + } +} diff --git a/src/Ramcar/LegacyTransactionType.php b/src/Ramcar/LegacyTransactionType.php new file mode 100644 index 00000000..1fc45ef2 --- /dev/null +++ b/src/Ramcar/LegacyTransactionType.php @@ -0,0 +1,37 @@ + 'Purchase', + 'post' => 'Post Service', + 'resq' => 'Res-q Service', + 'service' => 'Service', + ]; + + public static function translate($type) + { + $clean_type = strtolower(trim($type)); + + switch ($clean_type) + { + case 'post service': + return self::POST_SERVICE; + case 'purchase': + return self::PURCHASE; + case 'res-q services': + return self::RESQ_SERVICE; + case 'service': + return self::SERVICE; + } + + return false; + } +}