3 namespace Adherence\Service;
6 * Manage Adherence data.
9 class Adherence extends \Workspace\Service\DataBin
13 * CRON functionality: Fetch and import AutoMate and AutoLine data.
15 public function cronProcess()
19 $automateFiles = array(
20 './b4c_CJD.csv' => '/tmp/b4c_CJD.csv',
21 './b4c_LSaker.csv' => '/tmp/b4c_LSaker.csv',
22 './b4c_MultiFran.csv' => '/tmp/b4c_MultiFran.csv',
23 './b4c_Toyota.csv' => '/tmp/b4c_Toyota.csv'
26 $autolineFiles = array(
27 './b4c_Ford.csv' => '/tmp/b4c_Ford.csv',
28 './b4c_GM.csv' => '/tmp/b4c_GM.csv',
29 './b4c_Honda.csv' => '/tmp/b4c_Honda.csv',
30 './b4c_Nissan.csv' => '/tmp/b4c_Nissan.csv',
31 './b4c_Cargo.csv' => '/tmp/b4c_Cargo.csv'
33 $amhgroupFiles = array(
34 './Vehusesold.11' => '/tmp/Vehusesold.11',
35 './Vehusesolh.11' => '/tmp/Vehusesolh.11',
36 './Vehusesold.20' => '/tmp/Vehusesold.20'
38 $amhgroupFiles2 = array();
39 // headers for the fields
41 // Vehicle stock number
53 //'./Vehusestk.11' => '/tmp/Vehusestk.11',
54 //'./Vehusestkh.11' => '/tmp/Vehusestkh.11',
55 //'./Vehusestk.20' => '/tmp/Vehusestk.20'
58 $amhgroupFiles3 = array();
59 //headers for the fields
60 // Vehicle Stock number
68 // './vhsleb4car.11' => '/tmp/vhsleb4car.11'
74 '/tmp/b4c_Ford.csv' => 'Ford',
75 '/tmp/b4c_GM.csv' => 'GM',
76 '/tmp/b4c_Honda.csv' => 'Honda',
77 '/tmp/b4c_Nissan.csv' => 'Nissan',
78 '/tmp/b4c_Cargo.csv' => 'Cargo',
79 '/tmp/b4c_CJD.csv' => 'CJD',
80 '/tmp/b4c_LSaker.csv' => 'LSaker',
81 '/tmp/b4c_MultiFran.csv' => 'MultiFran',
82 '/tmp/b4c_Toyota.csv' => 'Toyota',
83 '/tmp/Vehusestk.11' => 'Multifranchise',
84 '/tmp/Vehusestkh.11' => 'Hyundai',
85 '/tmp/Vehusestk.20' => 'Pearldb',
86 '/tmp/Vehusesold.11' => 'Multifranchise',
87 '/tmp/Vehusesolh.11' => 'Hyundai',
88 '/tmp/Vehusesold.20' => 'Pearldb',
89 '/tmp/vhsleb4car.11' => 'Hyundai'
103 'Multifranchise' => 3,
106 'Multifranchise' => 3,
112 $ftpWrapper = new \Utility\Remote\Ftp(
113 '197.242.75.234', //'127.0.0.1'
114 'automate', 'aut0mat3@', 180, true
116 $ftpWrapper2 = new \Utility\Remote\Ftp(
117 'ftps.amhgroup.net', 'bid4cars', 'rZMd0GJx', 180, true
135 #-> Cleanup existing inported data that does not have company linked.
136 /* $results = $this->em->createQuery(
138 . 'FROM \Adherence\Entity\Adherence adherence '
139 . 'WHERE adherence.company IS NULL'
142 foreach ($results as $adherence)
144 $search = 'Automate' == $adherence->type
146 'automateFile' => $adherence->file,
147 'automateDealerCode' => $adherence->dealerCode,
148 'group' => $groupMap[$adherence->file]
151 'autolineFile' => $adherence->file,
152 'autolineDealerCode' => $adherence->dealerCode,
153 'group' => $groupMap[$adherence->file]
157 ->getRepository('\Company\Entity\Company')
158 ->findOneBy($search);
159 if (!is_null($company))
161 $adherence->company = $company;
162 $this->em->flush($adherence);
167 #-> Import AutoMate csv
168 $companies = array();
169 foreach ($automateFiles as $remoteFile => $localFile)
171 echo "\n\nProcessing $localFile\n";
172 $downloadOk = $ftpWrapper->downloadFile($localFile, $remoteFile, FTP_ASCII);
173 if (!$downloadOk || !file_exists($localFile))
177 $companies[$localFile] = array();
178 $csvImporter = new \Utility\Import\Csv($localFile);
179 $csvImporter->setDelimiter(',');
181 while (($record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_LAZY)) !== false)
184 if ('T' != trim($record[6]) && 't' != trim($record[6]))
189 $hash = md5(serialize($record));
190 $record[7] = str_replace(' ', '', $record[7]);
191 if (empty($record[1]) || !is_numeric(trim($record[7])))
198 #-> Change date format from d-m-Y to Y-m-d.
199 $parts = explode('/', $record[1]);
200 $record[1] = new \DateTime($parts[2] . '-' . $parts[1] . '-' . $parts[0]);
201 $dateFrom = new \DateTime(date('Y-m-d', mktime(1, 1, 1, $parts[1] - 4, $parts[0], $parts[2])));
203 #-> Check if this will cause duplicate.
204 $existing = $this->em
205 ->getRepository('\Adherence\Entity\Adherence')
208 'dateSold' => $record[1]
210 if (!is_null($existing))
221 #-> Log to import table.
222 if (!isset($companies[$localFile][$record[0]]))
224 $companies[$localFile][$record[0]] = $this->em
225 ->getRepository('\Company\Entity\Company')
227 'automateFile' => $fileMap[$localFile],
228 'automateDealerCode' => $record[0],
229 'group' => $groupMap[$fileMap[$localFile]]
232 $companyId = $companies[$localFile][$record[0]];
235 $entry = new \Adherence\Entity\Adherence();
236 $entry->fromArray(array(
238 'source' => 'Imperial',
239 'type' => 'Automate',
240 'file' => $fileMap[$localFile],
241 'dealerCode' => $record[0],
242 'company' => $companyId,
243 'dateSold' => $record[1],
244 'stockNumber' => trim($record[2]),
245 'vinNumber' => trim($record[3]),
246 'soldTo' => trim($record[4]),
247 'amount' => trim($record[7]),
248 'registrationNumber' => trim($record[8]),
249 'engineNumber' => trim($record[9]),
250 'make' => trim($record[10])
252 $this->em->persist($entry);
253 $this->em->flush($entry);
257 #-> Match on VIN, registration or engine number.
258 $dql = 'SELECT priceGuide.id AS pgId, stock.id AS stockId '
259 . 'FROM \PriceGuide\Entity\PriceGuide priceGuide '
260 . 'JOIN priceGuide.stock stock '
261 . 'WHERE priceGuide.created BETWEEN :dateFrom AND :dateTo ';
264 'dateFrom' => $dateFrom,
265 'dateTo' => $record[1]
267 if ('' != $entry->vinNumber && !in_array(strtolower($entry->vinNumber), $filter))
269 $where[] = 'stock.vinNumber = :vinNumber';
270 $params['vinNumber'] = $entry->vinNumber;
272 if ('' != $entry->registrationNumber && !in_array(strtolower($entry->registrationNumber), $filter))
274 $where[] = 'stock.registrationNumber = :registrationNumber';
275 $params['registrationNumber'] = $entry->registrationNumber;
277 if ('' != $entry->engineNumber && !in_array(strtolower($entry->engineNumber), $filter))
279 $where[] = 'stock.engineNumber = :engineNumber';
280 $params['engineNumber'] = $entry->engineNumber;
282 $dql .= 'AND (' . implode(' OR ', $where) . ')';
283 $results = $this->em->createQuery($dql)
284 ->setParameters($params)
286 $stockFound = array();
287 foreach ($results as $result)
289 if (isset($stockFound[$result['stockId']]))
293 $stockFound[$result['stockId']] = true;
294 $link = new \Adherence\Entity\AdherenceStock();
295 $link->adherence = $entry;
296 $link->stock = $this->em->getReference('\Stock\Entity\Stock', $result['stockId']);
297 $this->em->persist($link);
298 $this->em->flush($link);
305 } catch (\Exception $e)
307 echo "-----------------------\n";
308 echo $e->getMessage() . "\n";
317 #-> Import AutoLine csv
318 $companies = array();
319 foreach ($autolineFiles as $remoteFile => $localFile)
321 echo "\n\nProcessing $localFile\n";
322 $downloadOk = $ftpWrapper->downloadFile($localFile, $remoteFile, FTP_ASCII);
323 if (!$downloadOk || !file_exists($localFile))
327 $companies[$localFile] = array();
328 $csvImporter = new \Utility\Import\Csv($localFile);
329 $csvImporter->setDelimiter(',');
331 $yearPrepend = '/tmp/b4c_Cargo.csv' == $localFile ? '' : '20';
332 while (($record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_LAZY)) !== false)
335 if ('/tmp/b4c_Cargo.csv' == $localFile && 'T' != trim($record[6]) && 't' != trim($record[6]))
339 $hash = md5(serialize($record));
340 $record[7] = str_replace(' ', '', $record[7]);
341 if (empty($record[1]) || !is_numeric($record[7]))
347 #-> Change date format from d-m-Y to Y-m-d.
348 $parts = explode('/', $record[1]);
349 $record[1] = new \DateTime($yearPrepend . $parts[2] . '-' . $parts[1] . '-' . $parts[0]);
350 $dateFrom = new \DateTime(date('Y-m-d', mktime(1, 1, 1, $parts[1] - 4, $parts[0], $parts[2])));
352 #-> Check if this will cause duplicate.
353 $existing = $this->em
354 ->getRepository('\Adherence\Entity\Adherence')
357 'file' => $fileMap[$localFile],
358 'dateSold' => $record[1]
360 if (!is_null($existing))
371 #-> Log to import table.
372 if (!isset($companies[$localFile][$record[0]]))
374 $companies[$localFile][$record[0]] = $this->em
375 ->getRepository('\Company\Entity\Company')
377 'autolineFile' => $fileMap[$localFile],
378 'autolineDealerCode' => $record[0],
379 'group' => $groupMap[$fileMap[$localFile]]
382 $companyId = $companies[$localFile][$record[0]];
385 $entry = new \Adherence\Entity\Adherence();
386 $entry->fromArray(array(
388 'source' => 'Imperial',
389 'type' => 'Autoline',
390 'file' => $fileMap[$localFile],
391 'dealerCode' => $record[0],
392 'company' => $companyId,
393 'dateSold' => $record[1],
394 'stockNumber' => trim($record[2]),
395 'vinNumber' => trim($record[3]),
396 'soldTo' => trim($record[4]),
397 'amount' => trim($record[7]),
398 'registrationNumber' => trim($record[8]),
399 'engineNumber' => trim($record[9]),
400 'make' => trim($record[10])
402 $this->em->persist($entry);
403 $this->em->flush($entry);
407 #-> Match on VIN, registration or engine number.
408 $dql = 'SELECT priceGuide.id AS pgId, stock.id AS stockId '
409 . 'FROM \PriceGuide\Entity\PriceGuide priceGuide '
410 . 'JOIN priceGuide.stock stock '
411 . 'WHERE priceGuide.created BETWEEN :dateFrom AND :dateTo ';
414 'dateFrom' => $dateFrom,
415 'dateTo' => $record[1]
417 if ('' != $entry->vinNumber && !in_array(strtolower($entry->vinNumber), $filter))
419 $where[] = 'stock.vinNumber = :vinNumber';
420 $params['vinNumber'] = $entry->vinNumber;
422 if ('' != $entry->registrationNumber && !in_array(strtolower($entry->registrationNumber), $filter))
424 $where[] = 'stock.registrationNumber = :registrationNumber';
425 $params['registrationNumber'] = $entry->registrationNumber;
427 if ('' != $entry->engineNumber && !in_array(strtolower($entry->engineNumber), $filter))
429 $where[] = 'stock.engineNumber = :engineNumber';
430 $params['engineNumber'] = $entry->engineNumber;
432 $dql .= 'AND (' . implode(' OR ', $where) . ')';
433 $results = $this->em->createQuery($dql)
434 ->setParameters($params)
436 $stockFound = array();
437 foreach ($results as $result)
439 if (isset($stockFound[$result['stockId']]))
443 $stockFound[$result['stockId']] = true;
444 $link = new \Adherence\Entity\AdherenceStock();
445 $link->adherence = $entry;
446 $link->stock = $this->em->getReference('\Stock\Entity\Stock', $result['stockId']);
447 $this->em->persist($link);
448 $this->em->flush($link);
455 } catch (\Exception $e)
457 echo "-----------------------\n";
458 echo $e->getMessage() . "\n";
467 #-> Import AMH Group csv
468 $companies = array();
469 foreach ($amhgroupFiles as $remoteFile => $localFile)
471 echo "\n\nProcessing $localFile\n";
472 $downloadOk = $ftpWrapper2->downloadFile($localFile, $remoteFile, FTP_ASCII);
473 if (!$downloadOk || !file_exists($localFile))
477 $companies[$localFile] = array();
478 $csvImporter = new \Utility\Import\Csv($localFile);
479 $csvImporter->setDelimiter(' ');
482 while (($record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_LAZY)) !== false)
485 $hash = md5(serialize($record));
486 if (empty($record[2]) || !is_numeric(trim($record[8])) || !is_numeric(trim($record[9])))
492 #-> Change date format from d-m-Y to Y-m-d.
493 $parts = explode('/', $record[2]);
494 $record[2] = new \DateTime($parts[2] . '-' . $parts[1] . '-' . $parts[0]);
495 $dateFrom = new \DateTime(date('Y-m-d', mktime(1, 1, 1, $parts[1] - 4, $parts[0], $parts[2])));
497 #-> Check if this will cause duplicate.
498 $existing = $this->em
499 ->getRepository('\Adherence\Entity\Adherence')
502 'dateSold' => $record[2]
504 if (!is_null($existing))
515 #-> Log to import table.
516 if (!isset($companies[$localFile][$record[0]]))
518 $companies[$localFile][$record[0]] = $this->em
519 ->getRepository('\Company\Entity\Company')
521 'amhgroupFile' => $fileMap[$localFile],
522 'amhgroupDealerCode' => $record[0],
523 'group' => $groupMap[$fileMap[$localFile]]
526 $companyId = $companies[$localFile][$record[0]];
531 $entry = new \Adherence\Entity\Adherence();
532 $entry->fromArray(array(
534 'source' => 'AMHgroup',
535 'type' => 'AMHgroup',
536 'file' => $fileMap[$localFile],
537 'dealerCode' => $record[0],
538 'company' => $companyId,
539 'dateSold' => $record[2],
540 'stockNumber' => trim($record[1]),
541 'vinNumber' => trim($record[3]),
542 'soldTo' => trim($record[7]),
543 'amount' => trim($record[8]),
544 'registrationNumber' => trim($record[4])
547 $this->em->persist($entry);
548 $this->em->flush($entry);
552 #-> Match on VIN, registration or engine number.
553 $dql = 'SELECT priceGuide.id AS pgId, stock.id AS stockId '
554 . 'FROM \PriceGuide\Entity\PriceGuide priceGuide '
555 . 'JOIN priceGuide.stock stock '
556 . 'WHERE priceGuide.created BETWEEN :dateFrom AND :dateTo ';
559 'dateFrom' => $dateFrom,
560 'dateTo' => $record[2]
562 if ('' != $entry->vinNumber && !in_array(strtolower($entry->vinNumber), $filter))
564 $where[] = 'stock.vinNumber = :vinNumber';
565 $params['vinNumber'] = $entry->vinNumber;
567 if ('' != $entry->registrationNumber && !in_array(strtolower($entry->registrationNumber), $filter))
569 $where[] = 'stock.registrationNumber = :registrationNumber';
570 $params['registrationNumber'] = $entry->registrationNumber;
573 $dql .= 'AND (' . implode(' OR ', $where) . ')';
574 $results = $this->em->createQuery($dql)
575 ->setParameters($params)
577 $stockFound = array();
579 //var_dump($results);
581 foreach ($results as $result)
583 if (isset($stockFound[$result['stockId']]))
587 $stockFound[$result['stockId']] = true;
588 $link = new \Adherence\Entity\AdherenceStock();
589 $link->adherence = $entry;
590 $link->stock = $this->em->getReference('\Stock\Entity\Stock', $result['stockId']);
591 $this->em->persist($link);
592 $this->em->flush($link);
599 } catch (\Exception $e)
601 echo "-----------------------\n";
602 echo $e->getMessage() . "\n";
611 #-> Import AMH group Files2 csv
612 $companies = array();
613 foreach ($amhgroupFiles2 as $remoteFile => $localFile)
615 echo "Processing $localFile\n";
616 $downloadOk = $ftpWrapper2->downloadFile($localFile, $remoteFile, FTP_ASCII);
617 if (!$downloadOk && !file_exists($localFile))
622 $companies[$localFile] = array();
623 $csvImporter = new \Utility\Import\Csv($localFile);
624 $csvImporter->setDelimiter(' ');
625 $record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_LAZY);
626 if (false == $record)
631 //var_dump($csvImporter->getRecord(\Utility\Import\Csv::FETCH_LAZY));
634 while (($record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_LAZY)) !== false)
637 $hash = md5(serialize($record));
638 if (!is_numeric(trim($record[11])) || !is_numeric(trim($record[12])))
644 // The file has no date field
645 // #-> Change date format from d-m-Y to Y-m-d.
646 // $parts = explode('/', $record[1]);
647 // $record[1] = new \DateTime('20' . $parts[2] . '-' . $parts[1] . '-' . $parts[0]);
648 // $dateFrom = new \DateTime(date('Y-m-d', mktime(1, 1, 1, $parts[1] - 4, $parts[0], $parts[2])));
649 // #-> Check if this will cause duplicate.
650 // $existing = $this->em
651 // ->getRepository('\Adherence\Entity\Adherence')
652 // ->findOneBy(array(
654 // 'dateSold' => $record[1]
656 // if (!is_null($existing))
665 #-> Log to import table.
666 if (!isset($companies[$localFile][$record[0]]))
668 $companies[$localFile][$record[0]] = $this->em
669 ->getRepository('\Company\Entity\Company')
671 'amhgroupFile' => $fileMap[$localFile],
672 'amhgroupDealerCode' => $record[0],
673 'group' => $groupMap[$fileMap[$localFile]]
676 $companyId = $companies[$localFile][$record[0]];
679 $entry = new \Adherence\Entity\Adherence();
680 $entry->fromArray(array(
682 'source' => 'AMHgroup',
683 'type' => 'AMHgroup',
684 'file' => $fileMap[$localFile],
685 'dealerCode' => $record[0],
686 'company' => $companyId,
688 'stockNumber' => trim($record[1]),
689 'vinNumber' => trim($record[2]),
691 'amount' => trim($record[11]),
692 'registrationNumber' => trim($record[5]),
693 'engineNumber' => '',
694 'make' => trim($record[3])
698 $this->em->persist($entry);
699 $this->em->flush($entry);
703 #-> Match on VIN, registration or engine number.
704 $dql = 'SELECT priceGuide.id AS pgId, stock.id AS stockId '
705 . 'FROM \PriceGuide\Entity\PriceGuide priceGuide '
706 . 'JOIN priceGuide.stock stock ';
707 //. 'WHERE priceGuide.created BETWEEN :dateFrom AND :dateTo ';
710 // 'dateFrom' => $dateFrom,
711 // 'dateTo' => $record[1]
713 if ('' != $entry->vinNumber && !in_array(strtolower($entry->vinNumber), $filter))
715 $where[] = 'stock.vinNumber = :vinNumber';
716 $params['vinNumber'] = $entry->vinNumber;
718 if ('' != $entry->registrationNumber && !in_array(strtolower($entry->registrationNumber), $filter))
720 $where[] = 'stock.registrationNumber = :registrationNumber';
721 $params['registrationNumber'] = $entry->registrationNumber;
724 $dql .= 'AND (' . implode(' OR ', $where) . ')';
726 $results = $this->em->createQuery($dql)
727 ->setParameters($params)
729 $stockFound = array();
731 foreach ($results as $result)
733 if (isset($stockFound[$result['stockId']]))
737 $stockFound[$result['stockId']] = true;
738 $link = new \Adherence\Entity\AdherenceStock();
739 $link->adherence = $entry;
740 $link->stock = $this->em->getReference('\Stock\Entity\Stock', $result['stockId']);
741 $this->em->persist($link);
742 $this->em->flush($link);
749 } catch (\Exception $e)
751 echo "-----------------------\n";
752 echo $e->getMessage() . "\n";
761 #-> Import AMH group Files3 csv
762 $companies = array(); // needs the company number field
763 foreach ($amhgroupFiles3 as $remoteFile => $localFile)
765 echo "Processing $localFile\n";
766 $downloadOk = $ftpWrapper2->downloadFile($localFile, $remoteFile, FTP_ASCII);
767 if (!$downloadOk && !file_exists($localFile))
772 $companies[$localFile] = array();
773 $csvImporter = new \Utility\Import\Csv($localFile);
774 $csvImporter->setDelimiter(' ');
775 $record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_LAZY);
776 if (false == $record)
781 var_dump($csvImporter->getRecord(\Utility\Import\Csv::FETCH_LAZY));
785 while (($record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_LAZY)) !== false)
788 $hash = md5(serialize($record));
790 if (empty($record[7]))
795 #-> Change date format from d-m-Y to Y-m-d.
796 $parts = explode('/', $record[7]);
797 $record[7] = new \DateTime('20' . $parts[2] . '-' . $parts[1] . '-' . $parts[0]);
798 $dateFrom = new \DateTime(date('Y-m-d', mktime(1, 1, 1, $parts[1] - 4, $parts[0], $parts[2])));
801 #-> Check if this will cause duplicate.
802 $existing = $this->em
803 ->getRepository('\Adherence\Entity\Adherence')
806 'dateSold' => $record[7]
808 if (!is_null($existing))
817 $test = $companies[$localFile][$record[0]];
820 #-> Log to import table.
821 if (!isset($companies[$localFile][$record[0]]))
823 $companies[$localFile][$record[0]] = $this->em
824 ->getRepository('\Company\Entity\Company')
826 'amhgroupFile' => $fileMap[$localFile],
827 'amhgroupDealerCode' => $record[0],
828 'group' => $groupMap[$fileMap[$localFile]]
831 $companyId = $companies[$localFile][$record[0]];
833 var_dump($companyId);
838 $entry = new \Adherence\Entity\Adherence();
839 $entry->fromArray(array(
841 'source' => 'AMHgroup',
842 'type' => 'AMHgroup',
843 'file' => $fileMap[$localFile],
844 'dealerCode' => $record[0],
845 'company' => $companyId,
846 'dateSold' => $record[7],
848 'vinNumber' => $record[4],
851 'registrationNumber' => $record[5],
852 'engineNumber' => '',
859 $this->em->persist($entry);
860 $this->em->flush($entry);
864 #-> Match on VIN, registration or engine number.
865 $dql = 'SELECT priceGuide.id AS pgId, stock.id AS stockId '
866 . 'FROM \PriceGuide\Entity\PriceGuide priceGuide '
867 . 'JOIN priceGuide.stock stock '
868 . 'WHERE priceGuide.created BETWEEN :dateFrom AND :dateTo ';
871 'dateFrom' => $dateFrom,
872 'dateTo' => $record[7]
874 if ('' != $entry->vinNumber && !in_array(strtolower($entry->vinNumber), $filter))
876 $where[] = 'stock.vinNumber = :vinNumber';
877 $params['vinNumber'] = $entry->vinNumber;
879 if ('' != $entry->registrationNumber && !in_array(strtolower($entry->registrationNumber), $filter))
881 $where[] = 'stock.registrationNumber = :registrationNumber';
882 $params['registrationNumber'] = $entry->registrationNumber;
885 $dql .= 'AND (' . implode(' OR ', $where) . ')';
887 $results = $this->em->createQuery($dql)
888 ->setParameters($params)
890 $stockFound = array();
895 foreach ($results as $result)
897 if (isset($stockFound[$result['stockId']]))
901 $stockFound[$result['stockId']] = true;
902 $link = new \Adherence\Entity\AdherenceStock();
903 $link->adherence = $entry;
904 $link->stock = $this->em->getReference('\Stock\Entity\Stock', $result['stockId']);
905 $this->em->persist($link);
906 $this->em->flush($link);
913 } catch (\Exception $e)
915 echo "-----------------------\n";
916 echo $e->getMessage() . "\n";
926 public function cronTradeSales()
928 $ftpWrapper = new \Utility\Remote\Ftp(
929 'ftp.nirph.com', //'127.0.0.1'
930 'b4c@nirph.com', 'B!d4C@r$', 180, true
933 $localFile = '/tmp/trade_sales' . date('Y-m') . '.xlsx';
934 $remoteFile = '/Monthly AMH Trade Sales Report/TradeSales' . date('Y-m') . '.xlsx';
936 $downloadOk = $ftpWrapper->downloadFile($localFile, $remoteFile, FTP_ASCII);
937 if ($downloadOk && file_exists($localFile))
939 /** Identify the type of $inputFileName * */
940 $inputFileType = \PHPExcel_IOFactory::identify($localFile);
941 /** Create a new Reader of the type that has been identified * */
942 $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
943 /** Advise the Reader that we only want to load cell data * */
944 $objReader->setReadDataOnly(true);
945 /** Load $inputFileName to a PHPExcel Object * */
946 $objPHPExcel = $objReader->load($localFile);
948 $worksheetData = $objReader->listWorksheetInfo($localFile);
950 foreach ($worksheetData as $worksheet)
952 $objPHPExcel->setActiveSheetIndexByName($worksheet['worksheetName']);
953 $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, false, false);
955 if (trim($sheetData[0][0]) == 'Dealer Name')
957 foreach ($sheetData as $row)
959 if (trim($row[0]) != 'Dealer Name' && $row[0] != '')
963 $entry = new \Adherence\Entity\TradeSales();
964 $entry->fromArray(array(
965 'dealerName' => $row[0],
966 'stockNumber' => $row[2],
967 'invoiceNumber' => $row[3],
968 'regNumber' => $row[4],
969 'vinNumber' => $row[5],
970 'vehicleMake' => $row[6],
971 'regDate' => new \DateTime(gmdate("d-m-Y H:i:s", ($row[7] - 25569) * 86400)),
972 'saleDate' => new \DateTime(gmdate("d-m-Y H:i:s", ($row[8] - 25569) * 86400)),
973 'saleType' => $row[9],
974 'totalCost' => $row[10],
975 'salesTotal' => $row[11],
980 $dql = 'SELECT stock.id AS stockId '
981 . 'FROM \Stock\Entity\Stock stock WHERE ';
982 $where = $params = array();
985 $where[] = 'stock.vinNumber = :vinNumber';
986 $params['vinNumber'] = str_replace(' ', '', $row[5]);
989 $dql .= implode(' AND ', $where);
991 $results = $this->em->createQuery($dql)
992 ->setParameters($params)
997 $entry->stock = $this->em->getReference('\Stock\Entity\Stock', $results[0]['stockId']);
998 $entry->vinMatch = true;
1001 #-> Match on registration number
1002 $dql = 'SELECT stock.id AS stockId '
1003 . 'FROM \Stock\Entity\Stock stock WHERE ';
1004 $where = $params = array();
1007 $where[] = 'stock.registrationNumber = :registrationNumber';
1008 $params['registrationNumber'] = str_replace(' ', '', $row[4]);
1011 $dql .= implode(' AND ', $where);
1013 $results = $this->em->createQuery($dql)
1014 ->setParameters($params)
1017 if (count($results))
1019 $entry->stock = $this->em->getReference('\Stock\Entity\Stock', $results[0]['stockId']);
1020 $entry->regMatch = true;
1024 $this->em->persist($entry);
1025 $this->em->flush($entry);
1028 } catch (Exception $e)
1030 print_r($e->getMessage());