· 5 years ago · Mar 08, 2021, 03:46 PM
1<?php
2
3require_once("/var/www/vhosts/dentzone.com/cronjobs/classes/dzProduct/dBOperations.class.php");
4
5class ImportUtil
6{
7 /**
8 * @param $data
9 * @param $conn
10 * @return int
11 *
12 * ========================================
13 * Check is this Contract is editable
14 * ========================================
15 */
16 public static function isContractEditable($data, $conn)
17 {
18 $editable = 1;
19 $non_editable_fields = array('termmo', 'base_prdcd', 'actcd', 'date_sale');
20 $query = "SELECT termmo,base_prdcd,actcd,date_sale,bundle FROM contracts WHERE vin='{$data['vin']}' AND affgrp='{$data['affgrp']}' AND status='INFORCE' LIMIT 1";
21 $res = $conn->Execute($query);
22 $row = $res->FetchRow();
23 foreach ($non_editable_fields as $f) {
24 if ($row[$f] != $data[$f]) {
25 $editable = 0;
26 break;
27 }
28 }
29 return $editable;
30 }
31
32 /**
33 * @param $data
34 * @param null $conn
35 * @return mixed
36 *
37 * ==============================
38 * Insert Contract vie Curl API
39 * ==============================
40 */
41 public static function insertContract($data, $conn = NULL)
42 {
43 $data['billing_period'] = date("Y-m-d");
44 list($y, $m, $d) = explode('-', $data['date_sale']);
45 $data['expy_date'] = date("Y-m-d", mktime(0, 0, 0, $m + $data['termmo'], $d, $y));
46 $data_json = json_encode($data);
47
48 $url = "http://www.dentzone.com/admin/contractEntry/contract_entry_dev.php";
49 $ch = curl_init($url);
50 curl_setopt($ch, CURLOPT_POST, true);
51 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
52 curl_setopt($ch, CURLOPT_HEADER, 0);
53 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
54 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
55 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
56 $result = curl_exec($ch);
57 curl_close($ch);
58 $result_arr = json_decode($result, TRUE);
59 return $result_arr;
60 }
61
62 /**
63 * @param $data
64 * @param $result_arr
65 * @param $raw_data
66 * @param $conn
67 * @param $import_notes
68 * @return mixed
69 *
70 * ===============================
71 * Log Error History
72 * ===============================
73 */
74 public static function handleError($data, $result_arr, $raw_data, $conn, $import_notes)
75 {
76 if (isset($result_arr['success']) and ((intval($result_arr['success']) < 1) and (!count($result_arr['errors'])))) {
77 $result_arr['errors'][] = 'Contract failed to load';
78 }
79 if (is_array($result_arr['errors']) and count($result_arr['errors'])) {
80 $raw_data['import_status'] = 'error';
81 }
82
83 /**
84 * ====================
85 * START CODE BLOCK
86 * ====================
87 * Charles addon validation
88 */
89 /*$ic_query = "SELECT * FROM imported_contracts WHERE uid='{$data['uid']}' AND vin='{$data['vin']}' AND affgrp='{$data['affgrp']}'";
90 $res = $conn->Execute($ic_query);
91 $imported_data = $res->FetchRow();
92 if (empty($imported_data)) {
93 $imported_contract_id = dzProduct_DBOperations::insert('imported_contracts', $raw_data, $conn);
94 }*/
95 /**
96 * ====================
97 * END CODE BLOCK
98 * ====================
99 */
100
101 $imported_contract_id = dzProduct_DBOperations::insert('imported_contracts', $raw_data, $conn);
102 $err_date = date("Y-m-d H:i:s");
103 if (is_array($result_arr['errors']) and count($result_arr['errors'])) {
104 foreach ($result_arr['errors'] as $err) {
105 $err_data = array(
106 'imported_contract_id' => ($imported_contract_id != 0) ? $imported_contract_id : $raw_data['id'],
107 'error_message' => $err,
108 'date_created' => $err_date,
109 'status' => 'in_queue'
110 );
111 dzProduct_DBOperations::insert('import_errors', $err_data, $conn);
112 }
113 }
114
115 if (is_array($import_notes) and count($import_notes)) {
116 foreach ($import_notes as $note) {
117 $err_data = array(
118 'imported_contract_id' => ($imported_contract_id != 0) ? $imported_contract_id : $raw_data['id'],
119 'note' => $note,
120 'date_created' => $err_date,
121 'status' => $raw_data['import_status']
122 );
123 dzProduct_DBOperations::insert('import_error_notes', $err_data, $conn);
124 }
125 }
126
127 return $data;
128 }
129
130 /**
131 * @param $data
132 * @param $doctype
133 * @param $conn
134 * @return mixed
135 *
136 * ================================
137 * Check & Fetch Transaction Count
138 * ================================
139 */
140 public static function checkTransactions($data, $doctype, $conn)
141 {
142 $query = "SELECT count(id) as transCount FROM transactions WHERE contract_num='{$data['uid']}' AND affgrp='{$data['affgrp']}' AND doctype='$doctype' LIMIT 1";
143 $res = $conn->Execute($query);
144 $row = $res->FetchRow();
145 return $row['transCount'];
146 }
147
148 /**
149 * @param $data
150 * @return mixed
151 *
152 * ==============================================
153 * Process and Decorate Data for Nobilinx
154 * ==============================================
155 */
156 public static function prepareDataForNobilinxAPI($data)
157 {
158 $nob_data_arr = array();
159 $field_mapping_arr = array(
160 'vehicle_year' => 'myear',
161 'vehicle_make' => 'make',
162 'vehicle_model' => 'model',
163 'vin' => 'vin',
164
165 'imported_vehicle_class' => 'imported_vehicle_class',
166 'imported_dealer_cost' => 'imported_dealer_cost',
167 'lien_name' => 'lien_name',
168 'lien_addr' => 'lien_addr',
169 'lien_city' => 'lien_city',
170 'lien_state' => 'lien_state',
171 'lien_zip' => 'lien_zip',
172 'linxmanager_dealer_id' => 'dealer_id',
173
174 'customer_first_name' => 'fname',
175 'customer_last_name' => 'lname',
176 'customer_address1' => 'addr1',
177 'customer_address2' => 'addr2',
178 'customer_city' => 'city',
179 'customer_state' => 'state',
180 'customer_zip' => 'zip',
181 'customer_country' => 'country',
182 'customer_email' => 'email',
183 'customer_home_phone' => 'phone',
184 'store_number' => 'actcd',
185 'contract_number' => 'uid',
186 'contract_product_code' => 'base_prdcd',
187 'contract_sale_date' => 'date_sale',
188 'contract_price' => 'contract_price',
189 'contract_term' => 'termmo',
190 'contract_term_mile' => 'termmi',
191 'current_odometer' => 'mileage',
192 'contract_bundle' => 'bundle',
193 'surcharges' => 'surcharges',
194 'deductible_option' => 'deductible_option',
195 'deductible_surcharge' => 'deductible_surcharge',
196 'is_disappearing_deductible' => 'deductible_disappearing',
197 'vehicle_purchase_price' => 'vehicle_purchase_price',
198 'is_mechanical' => 'is_mechanical',
199 'encoded_data' => 'encoded_data',
200 );
201 foreach ($field_mapping_arr as $nob => $lm) {
202 $nob_data_arr[$nob] = isset($data[$lm]) ? $data[$lm] : '';
203 }
204 $arr['contract_data'] = $nob_data_arr;
205 return $arr;
206 }
207
208 /**
209 * @param $job_id
210 * @param $conn
211 * @return array
212 *
213 * ====================================
214 * Match DB columns with Data fields
215 * To decorate data to import
216 * ====================================
217 */
218 public static function getFieldMapping($job_id, $conn)
219 {
220 $query = "SELECT * FROM import_maps WHERE job_id='{$job_id}' ORDER BY `index` ASC";
221 $res = $conn->Execute($query);
222 $result = $res->getAll();
223 $map = [];
224 if (!empty($result)) {
225 foreach ($result as $key => $row) {
226 $map[$row['index']] = $row['field'];
227 }
228 }
229 return $map;
230 }
231
232 /**
233 * @param $job_id
234 * @param $date_recvd
235 * @param $file_name
236 * @param $data
237 * @param $conn
238 * @return false|int
239 *
240 * ================================================
241 * Insert contracts in Imported Contract Table
242 * ================================================
243 */
244 public static function insertInImportedContracts($job_id, $date_recvd, $file_name, $data, $conn)
245 {
246 if (!empty($data)) {
247 $data['job_id'] = $job_id;
248 $data['date_recvd'] = $date_recvd;
249 $data['file_name'] = $file_name;
250 $data['import_status'] = 'new';
251 $imported_contract_id = dzProduct_DBOperations::insert('imported_contracts', $data, $conn);
252 return $imported_contract_id;
253 } else {
254 return false;
255 }
256 }
257
258 /**
259 * @param $data
260 * @param $conn
261 * @return array
262 *
263 * ===============================
264 * Check For Duplicate Contracts
265 * ===============================
266 */
267 public static function checkDuplicateContract($data, $conn)
268 {
269 $q = "SELECT * FROM contracts WHERE affgrp='{$data['affgrp']}' AND vin='{$data['vin']}' AND base_prdcd='{$data['base_prdcd']}' AND uid='{$data['uid']}' AND lname='{$data['lname']}'";
270 $r = $conn->Execute($q);
271 $rw = $r->FetchRow();
272 $exists = $r->RecordCount();
273 return ['rw' => $rw, 'exists' => $exists];
274 }
275
276 /**
277 * @param $data
278 * @param $makeObj
279 * @return mixed
280 *
281 * ======================================
282 * Process and decorate contract data
283 * ======================================
284 */
285 public static function processDataType($data, $makeObj)
286 {
287 $data['termmo'] = intval($data['termmo']);
288 $data['plncd'] = $data['plncd'] ? $data['plncd'] : "";
289 $data['affgrp'] = 'coachnet';
290 $data['zip'] = substr(trim($data['zip']), 0, 5);
291 $date = date("Y-m-d");
292 $data['billing_period'] = date("Y-m-d");
293 $data['date_sale'] = substr(trim($data['date_sale']), 0, 10);
294 $data['expydate'] = date("Y-m-d", strtotime($data['date_sale']) + 60 * 60 * 24 * 365.25 * ($data['termmo'] / 12));
295 $data['nucd'] = 'N';
296 $data['date_created'] = date("Y-m-d H:i:s");
297 $data['vin'] = strtoupper(trim($data['vin']));
298 $data['business_type'] = 'D';
299 $data['source'] = 'import';
300 $data['effective_date'] = $data['date_sale'];
301 if (stristr($data['phone1'], '-')) {
302 $data['ph1'] = substr($data['phone1'], 0, 3);
303 $data['ph2'] = substr($data['phone1'], 4, 3);
304 $data['ph3'] = substr($data['phone1'], 8, 4);
305 } else {
306 $data['ph1'] = substr($data['phone1'], 0, 3);
307 $data['ph2'] = substr($data['phone1'], 3, 3);
308 $data['ph3'] = substr($data['phone1'], 6, 4);
309 }
310
311 if ($data['make'] and $data['year'] and $data['model']) {
312 $data['make'] = $makeObj->remap($data['make']);
313 }
314 return $data;
315 }
316
317 /**
318 * @param $data
319 * @param $prodObj
320 * @return mixed
321 *
322 * ============================
323 * Get Dealer with Actcd
324 * ============================
325 */
326 public static function getDealerbyActcd($data, $prodObj)
327 {
328 $query = " SELECT id, affgrp FROM dealers WHERE code = '{$data['actcd']}' AND affgrp='dg' AND subgroup='mbpi'";
329 $dealerRow = $prodObj->getDealerData($query);
330 $data['dealer_id'] = $dealerRow['id'];
331 return $data;
332 }
333
334}