From cdbc7897ca0f9d4642aeb1af54b65ea9490f4e9b Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Tue, 13 Nov 2018 17:42:05 +0800 Subject: [PATCH] Add legacy battery to current battery translation #168 --- src/Command/ImportLegacyJobOrderCommand.php | 401 ++++++++++++++++++++ src/Ramcar/LegacyBattery.php | 71 ++++ 2 files changed, 472 insertions(+) create mode 100644 src/Command/ImportLegacyJobOrderCommand.php create mode 100644 src/Ramcar/LegacyBattery.php diff --git a/src/Command/ImportLegacyJobOrderCommand.php b/src/Command/ImportLegacyJobOrderCommand.php new file mode 100644 index 00000000..8838986a --- /dev/null +++ b/src/Command/ImportLegacyJobOrderCommand.php @@ -0,0 +1,401 @@ +em = $om; + + $this->loadBatteryModels(); + $this->loadBatterySizes(); + + parent::__construct(); + } + + protected function configure() + { + $this->setName('legacy:import_jo') + ->setDescription('Import a CSV file with legacy job orders.') + ->setHelp('Updates job order, plate number and warranty database from imported CSV entries.') + ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $csv_file = $input->getArgument('file'); + $output->writeln("parsing $csv_file...\n"); + + /* + CSV column order: + 0 - Internal ID + 1 - Type + 2 - Transaction Number + 3 - Primary Information : Date of Transaction + 4 - Primary Information : Job Order Number + 5 - Primary Information : MEH : Transaction Type + 6 - Primary Information : Reference JO Number + 7 - Primary Information : Type of Service + 8 - Primary Information : Warranty Status + 9 - Primary Information : Transaction Origin + 10 - Primary Information : Existing Battery + 11 - Primary Information : Existing Battery Type + 12 - Primary Information : Recommended Battery + 13 - Primary Infromation : Alternative Battery + 14 - Primary Information : Vehicle Plate Number + 15 - Primary Information : Brand + 16 - Primary Information : Make + 17 - Primary Information : Model + 18 - Primary Information : Vehicle Color + 19 - Primary Information : Caller Name + 20 - First Name + 21 - Middle Name + 22 - Last Name + 23 - Primary Information : Caller Contact + 24 - Primary Information : Caller Mobile No. + 25 - Primary Information : Caller Landline No. + 26 - Primary Information : Invoice / Company Name + 27 - Primary Information : New Name? + 28 - Primary Information : New Invoice / Company Name + 29 - Primary Information : Delivery Instructions + 30 - Primary Information : Agent Notes - Tier 1 + 31 - Primary Information : Delivery Date + 32 - Primary Information : Delivery Time + 33 - Primary Information : Advance Order + 34 - Primary Information : Stages + 35 - Primary Information : Reason for Cancellation + 36 - Primary Information : Specify the Reason for Cancellation + 37 - Primary Information : Payment Method + 38 - Primary Information : Change for: + 39 - Primary Information : Prepared by + 40 - Current Battery Details : Serial No.: + 41 - Current Battery Details : DR No.: + 42 - Current Battery Details : Date of Replacement + 43 - Current Battery Details : Battery Brand + 44 - Current Battery Details : Type + 45 - Current Battery Details : Issued By: + 46 - Current Battery Details : Issued By Direct + 47 - Original Battery Details : Serial No. + 48 - Original Battery Details : OR No.: + 49 - Original Battery Details : Date of Purchase + 50 - Original Battery Details : Battery Brand: + 51 - Original Battery Details : Battery Type: + 52 - Original Battery Details : Purchased From: + 53 - Original Battery Details : Purchase from Direct + 54 - Original Battery Details : Current Warranty Expiration Date + 55 - Original Battery Details : Trade-In Battery + 56 - Dispatching : Agent Notes : Tier 2 + 57 - Dispatching : Notes (Customer Classification) + 58 - Dispatching : Pending + 59 - Dispatching : Pending - Awaiting Stocks + 60 - Dispatching : Date started from Awaiting Stock + 61 - Dispatching : 4TH Due + 62 - Dispatching : 8TH Due + 63 - Dispatching : 12TH Due + 64 - Dispatching : 14TH Due + 65 - Dispatching : Currently Assigned Enrollee + 66 - Dispatching : Non-HUB? + 67 - Dispatching : HUB Catered? + 68 - Dispatching : Thru Annex? + 69 - Dispatching : HUB List + 70 - Dispatching : Annex List + 71 - Dispatching : Exempted to 6% + 72 - Dispatching : Rider Name List + 73 - Dispatching : Rider Name + 74 - Dispatching : Rider Contact Information + 75 - DIspatching : Dispatch Time + 76 - Dispatching : Dispatch Date + 77 - Dispatching : Dispatched By + 78 - Dispatching : Time of Arrival + 79 - Dispatching : Time Job Completed + 80 - Dispatching : For Line Up? + 81 - DIspatching : Order + 82 - Vehicle Location : Address + 83 - Vehicle Location : Landmark + 84 - Customer Service Record : Purchase Date + 85 - Customer Service Record : Battery / Item + 86 - Customer Service Record : Battery / Item Warranty in Months + 87 - Customer Service Record : Warranty Inquiry (Expiration Date) + 88 - JO : Amount + 89 - JO : TAX + 90 - JO : Discount + 91 - JO : Final Amount + 92 - Line Details : Item + 93 - Line Details : Quantity + 94 - Line Details : Item Rate + 95 - Line Details : Price Level + 96 - Line Details : Item Base Price + 97 - Line Details : Price Level Amount Deductions + 98 - Line Details : Final Amount + 99 - Line Details : No Trade-In + 100 - Line Details : Reason for No Trade-In + 101 - Line Details : Battery Class + 102 - Line Details : Discount Items + 103 - Line Details : Discount Code + 104 - Line Details : Percentage + 105 - Line Details : Converted Rate + 106 - Line Details : Discount Amount + 107 - Line Details : Warranty Classification + 108 - Line Details : Warranty in Months + 109 - Line Details : Warranty Expiration Date + 110 - Line Details : Serial Number + 111 - Line Details : Invoice/DR Number + 112 - Line Details : Repalcement + 113 - Line Details : Employee / Card No. / Referred by + 114 - Line Details : Amount + 115 - Line Details : Gross Amount + 116 - Line Details : Gross Amount + 117 - Time Stamp : Date / Time (JO Open) + 118 - Time Stamp : Date / Time JO Saved + 119 - Time Stamp : Ticket Handle TIme (in mins) + 120 - Time Stamp : Idle Time + 121 - Time Stamp : Date and Time (Dispatched By) + 122 - Time Stamp : In-Transit Date and Time + 123 - TIme Stamp : Completion Date and TIme + 124 - TIme Stamp : Dispatch By to In-Transit Time (Time difference in Mins) + 125 - Time Stamp : In-Transit to Completed (Time difference in Mins) + 126 - Time Stamp : Raw Ticket Handle TIme (Decimal) + 127 - Time Stamp : Raw Idle Time (Decimal) + 128 - Account + 129 - Status + 130 - Tracking Numbers + 131 - Memo + 132 - Posting + 133 - Notes + 134 - Originally Assigned Enrollee + 135 - Remarks + 136 - Follow Up Transactions + 137 - CARD TYPE + 138 - CARD NO. + 139 - Ongoing Editing + 140 - Current User + 141 - Cancellation Date + 142 - Cancellation Time + 143 - Ticket Completion Date + 144 - Ticket Completion Time + 145 - Date Created + 146 - Time Created + 147 - PREVIOUS STAGE + 148 - DISPATCHED BY EDITED? + 149 - Previous Dispatched By + 150 - POST SERVICE + 151 - Pending (2) + 152 - COMPLETED MANUALLY + 153 - THRU HUB NOTE + 154 - CURRENTLY ASSIGNED ENROLLEE (free-form) + 155 - IN-TRANSIT ALLOWANCE + 156 - COPY FROM JO # + 157 - NO. OF POSSIBLE DUPLICATE + 158 - Call Status + 159 - DISTRIBUTOR OUTLETS + 160 - CURRENTLY ASSIGNED ENROLLEE (free-form) + 161 - HUB LIST + 162 - HUB LIST 2 + 163 - Enrollee + 164 - Currently Assigned Enrollee + 165 - Payment: + */ + + // attempt to open file + try + { + $fh = fopen($csv_file, "r"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be read.'); + } + + // get entity manager + $em = $this->em; + + $trans_types = []; + $service_types = []; + $no_sizes = []; + + // loop through rows + $row_num = 0; + while (($fields = fgetcsv($fh)) !== false) + { + $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; + + // check column count + if (count($fields) != 166) + { + echo "*** wrong field count on row $row_num\n"; + print_r($fields); + exit; + } + + // split battery into model and size + // echo "trying match - " . $fields[92] . "\n"; + $res = preg_match("/^(.+)(GOLD|EXCEL|ENDURO|\(Trade-In\))/", $fields[92], $matches); + if (!$res) + { + // echo "no match - " . $fields[92] . "\n"; + continue; + } + + + // check if we have the size + $found_size = $this->simplifyName($matches[1]); + if (!isset($this->bsize_hash[$found_size])) + { + // try legacy battery lookup + $legacy_size = LegacyBattery::translate($found_size); + if ($legacy_size == null) + { + // echo "no size - $found_size\n"; + if (isset($no_sizes[$found_size])) + $no_sizes[$found_size]++; + else + $no_sizes[$found_size] = 1; + continue; + } + + $found_size = $legacy_size; + } + + + // let's track purchases first + if ($fields[5] != 'PURCHASE') + continue; + + // consolidate transaction and service types + $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; + } + + + /* + // plate + echo '14 - ' . $fields[14] . "\n"; + + // transaction + echo '5 - ' . $fields[5] . "\n"; + echo '7 - ' . $fields[7] . "\n"; + + // name + echo '22 - ' . $fields[22] . "\n"; + echo '20 - ' . $fields[20] . "\n"; + echo '21 - ' . $fields[21] . "\n"; + + // number + echo '23 - ' . $fields[23] . "\n"; + echo '24 - ' . $fields[24] . "\n"; + echo '25 - ' . $fields[25] . "\n"; + + // vehicle + echo '15 - ' . $fields[15] . "\n"; + echo '16 - ' . $fields[16] . "\n"; + echo '17 - ' . $fields[17] . "\n"; + echo '18 - ' . $fields[18] . "\n"; + + + // battery + echo '10 - ' . $fields[10] . "\n"; + echo '11 - ' . $fields[11] . "\n"; + echo '50 - ' . $fields[50] . "\n"; + echo '51 - ' . $fields[51] . "\n"; + echo '40 - ' . $fields[40] . "\n"; + echo '43 - ' . $fields[43] . "\n"; + echo '44 - ' . $fields[44] . "\n"; + echo '47 - ' . $fields[47] . "\n"; + echo '54 - ' . $fields[54] . "\n"; + + // customer service record + echo '84 - ' . $fields[84] . "\n"; + echo '85 - ' . $fields[85] . "\n"; + echo '86 - ' . $fields[86] . "\n"; + echo '87 - ' . $fields[87] . "\n"; + */ + // echo '92 - ' . $fields[92] . "\n"; + + /* + echo '110 - ' . $fields[110] . "\n"; + */ + } + + // print_r($trans_types); + // print_r($service_types); + print_r($no_sizes); + // print_r($b_models); + // print_r($batteries); + + // save to db + // print_r($batteries); + } + + protected function loadBatteryModels() + { + $this->bmodel_hash = []; + + $models = $this->em->getRepository(BatteryModel::class)->findAll(); + foreach ($models as $model) + { + // clean name + // $name = strtoupper(trim(str_replace(' ', '', $model->getName()))); + $name = $this->simplifyName($model->getName()); + + $this->bmodel_hash[$name] = $model->getID(); + } + } + + protected function loadBatterySizes() + { + $this->bsize_hash = []; + + $sizes = $this->em->getRepository(BatterySize::class)->findAll(); + foreach ($sizes as $size) + { + // clean name + // $name = strtoupper(trim(str_replace(' ', '', $size->getName()))); + $name = $this->simplifyName($size->getName()); + + $this->bsize_hash[$name] = $size->getID(); + } + } + + protected function simplifyName($text) + { + return strtoupper(trim(str_replace(' ', '', $text))); + } +} diff --git a/src/Ramcar/LegacyBattery.php b/src/Ramcar/LegacyBattery.php new file mode 100644 index 00000000..01705bac --- /dev/null +++ b/src/Ramcar/LegacyBattery.php @@ -0,0 +1,71 @@ +