change to dino mail address for auction cron
[namibia] / module / Auction / src / Auction / Service / Auction.php
1 <?php
2 namespace Auction\Service;
3
4
5 /**
6  * Manage Auction data.
7  * @author andre.fourie
8  */
9 class Auction extends \Auction\DataBin\Auction
10 {
11
12
13         /**
14          * New Auction Item from existing Stock.
15          *
16          * @param \Stock\Entity\Stock $stock
17          * @param string              $previousState
18          * @param array               $routingData
19          */
20         public function initNewItemFromStock(
21                 \Stock\Entity\Stock $stock, $previousState, array $routingData = array()
22         )
23         {
24                 #-> Establish initial increment value.
25                 $increments = $this->em
26                         ->getRepository('\Auction\Entity\Increment')
27                         ->findBy(array(), array('to' => 'ASC'));
28                 foreach ($increments as $incr)
29                 {
30                         if ($incr->to >= $routingData['reservePrice']
31                             && $incr->from <= $routingData['reservePrice']
32                         )
33                         {
34                                 break;
35                         }
36                 }
37                 #-> Create auction entry.
38                 if (isset($routingData['profileId']))
39                 {
40                         $profile    = $this->em->find('User\\Entity\\Profile', $routingData['profileId']);
41                         $email      = $profile->email;
42                         $firstName  = $profile->firstName;
43                         $familyName = $profile->familyName;
44                         $company    = !is_null($profile->company)
45                                 ? $profile->company->id
46                                 : null;
47                         $profile    = $profile->id;
48                 }
49                 else
50                 {
51                         $authData   = \Utility\Registry::getAuthData();
52                         $email      = $authData['email'];
53                         $firstName  = $authData['firstName'];
54                         $familyName = $authData['familyName'];
55                         $profile    = \Utility\Registry::resolveProfileContext()->id;
56                         $company    = \Utility\Registry::resolveCompanyContext()->id;
57                 }
58                 $routingData['endDate'] = str_replace(' 00:00:00', '', $routingData['endDate']);
59                 $record                 = $this->create(array(
60                                                                 'createdBy'       => $profile,
61                                                                 'company'         => $company,
62                                                                 'stock'           => $stock->id,
63                                                                 'reservePrice'    => $routingData['reservePrice'],
64                                                                 'currentBidPrice' => $routingData['reservePrice'],
65                                                                 'bidIncrement'    => $incr->amount,
66                                                                 'startDate'       => new \DateTime("now"),
67                                                                 'endDate'         => new \DateTime($routingData['endDate'] . ' 14:30:00')
68                                                         ));
69
70                 #-> Update stock with count of times sent to auction.
71                 $stats              = $this->em->createQuery(
72                         'SELECT COUNT(auction.id) AS numAuctions '
73                         . 'FROM \Auction\Entity\Auction auction '
74                         . 'WHERE IDENTITY(auction.stock) = :stockId'
75                 )
76                         ->setParameter('stockId', $stock->id)
77                         ->getSingleResult();
78                 $stock->auction     = $record;
79                 $stock->highestBid  = 0.00;
80                 $stock->timesListed = $stats['numAuctions'];
81                 $this->em->flush($stock);
82
83                 #-> Send loaded notification.
84                 #-> Attachments.
85                 $pdf = new \Auction\Pdf\Complete();
86                 $pdf->process(array(
87                                       'jobRecord' => $record
88                               ), array());
89                 $docsDir     = __DIR__ . '/../../../../../public/documents/';
90                 $writer      = new \Utility\Export\PdfTemplate($pdf);
91                 $attachments = array(
92                         'auction' . $record->id . '.pdf' => $writer->output(''),
93                         'REGULATION_32_NOTICE_38960.pdf' => file_get_contents($docsDir . 'REGULATION_32_NOTICE_38960.pdf')
94                 );
95                 #-> Send email.
96                 $oNotify = new \Utility\Comms\Notification();
97                 $oNotify->sendFromTemplate(
98                         null, null,
99                         $company, $profile,
100                         $email, null,
101                         'Vehicle live on Bid4Cars Auction - ' . $record->id,
102                         'auction-loaded',
103                         array(
104                                 'first_name'  => $firstName,
105                                 'family_name' => $familyName,
106                                 'make'        => $stock->type->model->make->name,
107                                 'model'       => $stock->type->model->name,
108                                 'type'        => $stock->type->name,
109                                 'image'       => !is_null($stock->mainImage)
110                                         ? $stock->mainImage->filename
111                                         : 'main_img_car.png'
112                         ),
113                         $attachments,
114                         array(),
115                         false,
116                         true
117                 );
118         }
119
120         /**
121          * Contract to get selectlist.
122          * @param object|null $jobRecord
123          * @param array       $input
124          * @return \Workspace\Contract\UseOnce
125          */
126         public function contractHistoricMakeSelectList($jobRecord, array $input = array())
127         {
128                 $options     = new \Workspace\UseCase\Options();
129                 $requirement = new \Workspace\UseCase\Requirement();
130                 $requirement->addOptionalInput(array('filter' => array('Filters' => 'Array')));
131                 return new \Workspace\Contract\UseOnce($options, $requirement);
132         }
133
134         /**
135          * Get Select List.
136          * @param object|null                           $jobRecord
137          * @param \Workspace\Utility\ServiceInputParams $contract
138          * @return array
139          */
140
141         public function executeHistoricMakeSelectList($jobRecord, \Workspace\Utility\ServiceInputParams $contract)
142         {
143                 $em        = \Utility\Registry::getEntityManager();
144                 $authData  = \Utility\Registry::getAuthData();
145                 $companyId = $authData['company']['id'];
146
147                 $queryFilter = array();
148                 if (isset($contract->data->filter) && is_array($contract->data->filter['Filters']))
149                 {
150                         foreach ($contract->data->filter['Filters'] as $field => $filter)
151                         {
152                                 $queryFilter[] = $field . ' = ' . $filter;
153                         }
154                 }
155
156                 $queryFilter = $queryFilter ? 'AND ' . implode(' AND ', $queryFilter) : '';
157
158                 $memberslistdrop = $em->createQuery('
159                                 SELECT distinct make.id, make.name FROM \Auction\Entity\Auction auction
160                                                           JOIN auction.company company
161                                                           JOIN company.city city
162                                                           JOIN company.contact contact
163                                                           JOIN city.region region
164                                                           JOIN auction.stock stock
165                 JOIN stock.company companyStock
166                                                           JOIN stock.vehicleYear vehicleYear
167                                                           JOIN stock.type type
168                                                           JOIN type.model model
169                                                           JOIN model.make make
170                                                           LEFT JOIN stock.exteriorColour exteriorColour
171                 LEFT JOIN auction.currentBid currentBid
172                 WHERE auction.jobState = :sold AND auction.archived = 0 AND auction.endDate > :endDate ' . $queryFilter .
173                                                     'ORDER BY make.name');
174
175                 $memberslistdrop->setParameter('endDate', new \DateTime('-180 Days'));
176                 $memberslistdrop->setParameter('sold', 'Sold');
177                 $memberslistu = $memberslistdrop->getArrayResult();
178                 $datax        = array();
179                 foreach ($memberslistu as $data)
180                 {
181
182
183                         $datax[] = array(
184                                 'value' => $data['id'],
185                                 'label' => $data['name']
186                         );
187                 }
188                 return $contract->success('List Retrieved.', $datax);
189         }
190
191         /**
192          * Contract to get selectlist.
193          * @param object|null $jobRecord
194          * @param array       $input
195          * @return \Workspace\Contract\UseOnce
196          */
197         public function contractHistoricModelSelectList($jobRecord, array $input = array())
198         {
199                 $options     = new \Workspace\UseCase\Options();
200                 $requirement = new \Workspace\UseCase\Requirement();
201                 $requirement->addOptionalInput(array('filter' => array('Filters' => 'Array')));
202                 return new \Workspace\Contract\UseOnce($options, $requirement);
203         }
204
205         /**
206          * Get Select List.
207          * @param object|null                           $jobRecord
208          * @param \Workspace\Utility\ServiceInputParams $contract
209          * @return array
210          */
211
212         public function executeHistoricModelSelectList($jobRecord, \Workspace\Utility\ServiceInputParams $contract)
213         {
214                 $em        = \Utility\Registry::getEntityManager();
215                 $authData  = \Utility\Registry::getAuthData();
216                 $companyId = $authData['company']['id'];
217
218                 $queryFilter = array();
219                 if (isset($contract->data->filter) && is_array($contract->data->filter['Filters']))
220                 {
221                         foreach ($contract->data->filter['Filters'] as $field => $filter)
222                         {
223                                 $queryFilter[] = $field . ' = ' . $filter;
224                         }
225                 }
226
227                 $queryFilter = $queryFilter ? 'AND ' . implode(' AND ', $queryFilter) : '';
228
229                 $memberslistdrop = $em->createQuery('
230                                 SELECT distinct model.id, model.name FROM \Auction\Entity\Auction auction
231                                                           JOIN auction.company company
232                                                           JOIN company.city city
233                                                           JOIN company.contact contact
234                                                           JOIN city.region region
235                                                           JOIN auction.stock stock
236                 JOIN stock.company companyStock
237                                                           JOIN stock.vehicleYear vehicleYear
238                                                           JOIN stock.type type
239                                                           JOIN type.model model
240                                                           JOIN model.make make
241                                                           LEFT JOIN stock.exteriorColour exteriorColour
242                 LEFT JOIN auction.currentBid currentBid
243                 WHERE auction.jobState = :sold AND auction.archived = 0 AND auction.endDate > :endDate ' . $queryFilter .
244                                                     'ORDER BY model.name');
245
246                 $memberslistdrop->setParameter('endDate', new \DateTime('-180 Days'));
247                 $memberslistdrop->setParameter('sold', 'Sold');
248                 $memberslistu = $memberslistdrop->getArrayResult();
249                 $datax        = array();
250                 foreach ($memberslistu as $data)
251                 {
252
253
254                         $datax[] = array(
255                                 'value' => $data['id'],
256                                 'label' => $data['name']
257                         );
258                 }
259                 return $contract->success('List Retrieved.', $datax);
260         }
261
262         /**
263          * Contract to get selectlist.
264          * @param object|null $jobRecord
265          * @param array       $input
266          * @return \Workspace\Contract\UseOnce
267          */
268         public function contractHistoricTypeSelectList($jobRecord, array $input = array())
269         {
270                 $options     = new \Workspace\UseCase\Options();
271                 $requirement = new \Workspace\UseCase\Requirement();
272                 $requirement->addOptionalInput(array('filter' => array('Filters' => 'Array')));
273                 return new \Workspace\Contract\UseOnce($options, $requirement);
274         }
275
276         /**
277          * Get Select List.
278          * @param object|null                           $jobRecord
279          * @param \Workspace\Utility\ServiceInputParams $contract
280          * @return array
281          */
282
283         public function executeHistoricTypeSelectList($jobRecord, \Workspace\Utility\ServiceInputParams $contract)
284         {
285                 $em        = \Utility\Registry::getEntityManager();
286                 $authData  = \Utility\Registry::getAuthData();
287                 $companyId = $authData['company']['id'];
288
289                 $queryFilter = array();
290                 if (isset($contract->data->filter) && is_array($contract->data->filter['Filters']))
291                 {
292                         foreach ($contract->data->filter['Filters'] as $field => $filter)
293                         {
294                                 $queryFilter[] = $field . ' = ' . $filter;
295                         }
296                 }
297
298                 $queryFilter = $queryFilter ? 'AND ' . implode(' AND ', $queryFilter) : '';
299
300                 $memberslistdrop = $em->createQuery('
301                                 SELECT distinct type.id, type.name FROM \Auction\Entity\Auction auction
302                                                           JOIN auction.company company
303                                                           JOIN company.city city
304                                                           JOIN company.contact contact
305                                                           JOIN city.region region
306                                                           JOIN auction.stock stock
307                 JOIN stock.company companyStock
308                                                           JOIN stock.vehicleYear vehicleYear
309                                                           JOIN stock.type type
310                                                           JOIN type.model model
311                                                           JOIN model.make make
312                                                           LEFT JOIN stock.exteriorColour exteriorColour
313                 LEFT JOIN auction.currentBid currentBid
314                 WHERE auction.jobState = :sold AND auction.archived = 0 AND auction.endDate > :endDate ' . $queryFilter .
315                                                     'ORDER BY type.name');
316
317                 $memberslistdrop->setParameter('endDate', new \DateTime('-180 Days'));
318                 $memberslistdrop->setParameter('sold', 'Sold');
319                 $memberslistu = $memberslistdrop->getArrayResult();
320                 $datax        = array();
321                 foreach ($memberslistu as $data)
322                 {
323
324
325                         $datax[] = array(
326                                 'value' => $data['id'],
327                                 'label' => $data['name']
328                         );
329                 }
330                 return $contract->success('List Retrieved.', $datax);
331         }
332
333         /**
334          * Contract to get selectlist.
335          * @param object|null $jobRecord
336          * @param array       $input
337          * @return \Workspace\Contract\UseOnce
338          */
339         public function contractHistoricYearSelectList($jobRecord, array $input = array())
340         {
341                 $options     = new \Workspace\UseCase\Options();
342                 $requirement = new \Workspace\UseCase\Requirement();
343                 $requirement->addOptionalInput(array('filter' => array('Filters' => 'Array')));
344                 return new \Workspace\Contract\UseOnce($options, $requirement);
345         }
346
347         /**
348          * Get Select List.
349          * @param object|null                           $jobRecord
350          * @param \Workspace\Utility\ServiceInputParams $contract
351          * @return array
352          */
353
354         public function executeHistoricYearSelectList($jobRecord, \Workspace\Utility\ServiceInputParams $contract)
355         {
356                 $em        = \Utility\Registry::getEntityManager();
357                 $authData  = \Utility\Registry::getAuthData();
358                 $companyId = $authData['company']['id'];
359
360                 $queryFilter = array();
361                 if (isset($contract->data->filter) && is_array($contract->data->filter['Filters']))
362                 {
363                         foreach ($contract->data->filter['Filters'] as $field => $filter)
364                         {
365                                 $queryFilter[] = $field . ' = ' . $filter;
366                         }
367                 }
368
369                 $queryFilter = $queryFilter ? 'AND ' . implode(' AND ', $queryFilter) : '';
370
371                 $memberslistdrop = $em->createQuery('
372                                 SELECT distinct vehicleYear.id, vehicleYear.name FROM \Auction\Entity\Auction auction
373                                                           JOIN auction.company company
374                                                           JOIN company.city city
375                                                           JOIN company.contact contact
376                                                           JOIN city.region region
377                                                           JOIN auction.stock stock
378                 JOIN stock.company companyStock
379                                                           JOIN stock.vehicleYear vehicleYear
380                                                           JOIN stock.type type
381                                                           JOIN type.model model
382                                                           JOIN model.make make
383                                                           LEFT JOIN stock.exteriorColour exteriorColour
384                 LEFT JOIN auction.currentBid currentBid
385                 WHERE auction.jobState = :sold AND auction.archived = 0 AND auction.endDate > :endDate ' . $queryFilter .
386                                                     'ORDER BY vehicleYear.name');
387
388                 $memberslistdrop->setParameter('endDate', new \DateTime('-180 Days'));
389                 $memberslistdrop->setParameter('sold', 'Sold');
390                 $memberslistu = $memberslistdrop->getArrayResult();
391                 $datax        = array();
392                 foreach ($memberslistu as $data)
393                 {
394
395
396                         $datax[] = array(
397                                 'value' => $data['id'],
398                                 'label' => $data['name']
399                         );
400                 }
401                 return $contract->success('List Retrieved.', $datax);
402         }
403
404         /**
405          * Contract to remove item from auction.
406          * @param object|null $jobRecord
407          * @param array       $input
408          * @return \Workspace\Contract\UseOnce
409          */
410         public function contractRemove($jobRecord, array $input = array())
411         {
412                 $options     = new \Workspace\UseCase\Options();
413                 $requirement = new \Workspace\UseCase\Requirement();
414                 return new \Workspace\Contract\UseOnce($options, $requirement);
415         }
416
417         /**
418          * Remove item from auction.
419          * @param object|null                           $jobRecord
420          * @param \Workspace\Utility\ServiceInputParams $contract
421          * @return array
422          */
423
424         public function executeRemove($jobRecord, \Workspace\Utility\ServiceInputParams $contract)
425         {
426                 try
427                 {
428                         $workflow = \Utility\Registry::getServiceManager()
429                                 ->get('Stock');
430                         $workflow->handover('Auction', $jobRecord->stock->id);
431                 }
432                 catch (\Exception $e)
433                 {
434                         \Utility\Debug::errorLog(
435                                 'Error on Auction removal process for item ' . $jobRecord->id,
436                                 $e->getMessage()
437                         );
438                 }
439                 $this->workflowNode->changeState('This.Archived');
440                 return $contract->success('Vehicle removed from auction.', array());
441         }
442
443         /**
444          * Contract to undo auction.
445          * @param object|null $jobRecord
446          * @param array       $input
447          * @return \Workspace\Contract\UseOnce
448          */
449         public function contractUndo($jobRecord, array $input = array())
450         {
451                 $options     = new \Workspace\UseCase\Options();
452                 $requirement = new \Workspace\UseCase\Requirement();
453                 return new \Workspace\Contract\UseOnce($options, $requirement);
454         }
455
456         /**
457          * Undo auction.
458          * @param object|null                           $jobRecord
459          * @param \Workspace\Utility\ServiceInputParams $contract
460          * @return array
461          */
462
463         public function executeUndo($jobRecord, \Workspace\Utility\ServiceInputParams $contract)
464         {
465                 $this->workflowNode->changeState('This.Undone');
466                 $jobRecord->currentBid->status = 'Undone';
467
468                 try
469                 {
470                         $workflow = \Utility\Registry::getServiceManager()
471                                 ->get('Stock');
472                         $workflow->handover('Auction', $jobRecord->stock->id);
473                 }
474                 catch (\Exception $e)
475                 {
476                         \Utility\Debug::errorLog(
477                                 'Error on Auction Undo process for item ' . $jobRecord->id,
478                                 $e->getMessage()
479                         );
480                 }
481
482                 return $contract->success('Vehicle removed from auction.', array());
483         }
484
485         /**
486          * CRON functionality: Send newsletter of cars coming off from auction today.
487          */
488         public function newsletterProcess()
489         {
490                 set_time_limit(0);
491                 ini_set('memory_limit', '512M');
492                 #-> Collect data.
493                 $date = date('Y-m-d 23:59:59');
494                 $data = $this->em->createQuery(
495                         'SELECT DISTINCT auction.id, auction.reservePrice AS auction_reservePrice, stock.km AS stock_km,  '
496                         . 'type.name AS type_name, type.mmCode AS type_mmCode, model.name AS model_name, '
497                         . 'make.name AS make_name, vehicleYear.name AS vehicleYear_name, region.name AS region_name '
498                         . 'FROM \Auction\Entity\Auction auction '
499                         . 'JOIN auction.stock stock '
500                         . 'JOIN stock.type type '
501                         . 'JOIN type.model model '
502                         . 'JOIN model.make make '
503                         . 'JOIN stock.vehicleYear vehicleYear '
504                         . 'LEFT JOIN stock.company company '
505                         . 'LEFT JOIN company.city city '
506                         . 'LEFT JOIN company.contact contact'
507                         . 'LEFT JOIN city.region region '
508                         . 'WHERE auction.jobState = \'Active\''
509                         . ' AND auction.endDate >= :startDate '
510                         . ' AND auction.endDate <= :endDate '
511                         . 'ORDER BY make.name, model.name, type.name ASC'
512                 )
513                         ->setParameter('startDate', date('Y-m-d 01:00:00'))
514                         ->setParameter('endDate', date('Y-m-d 23:59:59'))
515                         ->getScalarResult();
516                 if (empty($data))
517                 {
518                         return;
519                 }
520                 $i = count($data);
521                 $this->em->clear();
522                 $oNotify = new \Utility\Comms\Notification();
523                 $oNotify->setRepeaterData($data);
524                 $oNotify->setSendAsNewsletter();
525                 $oNotify->sendFromTemplate(
526                         null,
527                         null,
528                         null,
529                         null,
530                         null,
531                         null,
532                         null,
533                         'auction-list',
534                         array('auctioncount' => $i)
535                 );
536         }
537
538         /**
539          * CRON functionality: Move Auction items as needed.
540          */
541         public function cronProcess()
542         {
543                 ini_set('memory_limit', '512M');
544                 error_log('------------------------------------');
545                 error_log('Auction cron started @ ' . date('Y-m-d H:i:s'));
546                 #-> Phase 1.
547                 $date     = date('Y-m-d H:i:s');
548                 $result   = $this->em->createQuery(
549                         'SELECT auction FROM \Auction\Entity\Auction auction '
550                         . 'WHERE auction.jobState = \'Active\''
551             . ' AND auction.jobState != \'Archived\''
552                         . ' AND auction.endDate <= \'' . $date . '\''
553                 )
554                         ->getResult();
555                 $workflow = \Utility\Registry::getServiceManager()
556                         ->get('Stock');
557                 error_log("Item count: " . count($result));
558                 foreach ($result as $item)
559                 {
560                         $this->workflowNode->setJob($item);
561                         try
562                         {
563                                 \Utility\Debug::errorLog('currentbid',$item->currentBid->amount);
564                                 \Utility\Debug::errorLog('reserve',$item->reservePrice);
565
566                                 if (!is_null($item->currentBid) && $item->currentBid->amount > $item->reservePrice)
567                                 {
568                                         #-> We have a winner.
569                                         \Utility\Debug::errorLog('Winner',$item->id);
570
571
572                                         $item->soldToCompany = $item->currentBid->company;
573                                         $item->soldToProfile = $item->currentBid->profile;
574                                         $this->workflowNode->changeState('This.Sold');
575                                 }
576                                 else
577                                 {
578                                         #-> Each and every one a loser.
579                                         \Utility\Debug::errorLog('no winner',$item->id);
580                                         $this->workflowNode->changeState('This.Relist');
581                                 }
582                         }
583                         catch (\Exception $e)
584                         {
585                                 \Utility\Debug::errorLog('Error on Auction cron process (state change) for item ' . $item->id, $e->getMessage());
586                         }
587                         try
588                         {
589                                 $workflow->handover('Auction', $item->stock->id);
590                         }
591                         catch (\Exception $e)
592                         {
593                                 \Utility\Debug::errorLog('Error on Auction cron process (handover) for item ' . $item->id, $e->getMessage());
594                         }
595                 }
596                 $this->em->flush();
597                 foreach ($result as $item)
598                 {
599                         try
600                         {
601                                 \Utility\Debug::errorLog('setting data for mail',$item->id);
602
603                                 #-> General data prep.
604                                 $vehicle    = $item->stock->type->model->make->name
605                                               . ', ' . $item->stock->type->model->name
606                                               . ', ' . $item->stock->type->name;
607                                 $currPrefix = \Utility\Definitions\Locale::getCurrencyPrefix() . ' ';
608                                 $oNotify    = new \Utility\Comms\Notification();
609
610                                 \Utility\Debug::errorLog('jobstate',$item->jobState);
611                                 \Utility\Debug::errorLog('currentbid',$item->currentBid->amount);
612                                 \Utility\Debug::errorLog('reserve',$item->reservePrice);
613
614                                 if (!is_null($item->currentBid) && $item->currentBid->amount > $item->reservePrice && 'Sold' == $item->jobState)
615                                 {
616                                         \Utility\Debug::errorLog('sending mails',$item->jobState);
617
618
619                                         #-> Update record to reflect winner.
620                                         $item->soldToCompany = $item->currentBid->company;
621                                         $item->soldToProfile = $item->currentBid->profile;
622
623                                         #-> Prep work.
624
625                                         #-> Auction PDF.
626                                         $pdf = new \Auction\Pdf\CompleteNew();
627                                         $pdf->process(array(
628                                                               'jobRecord' => $item
629                                                       ), array());
630                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
631
632                                         #-> Declaration PDF.
633                                         $pdf_Declaration = new \Auction\Pdf\Declaration();
634                                         $pdf_Declaration->process(array(
635                                                                           'jobRecord' => $item
636                                                                   ), array());
637                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
638
639                                         #-> Deliveryreceipt PDF.
640                                         $pdf_Deliveryreceipt = new \Auction\Pdf\Deliveryreceipt();
641                                         $pdf_Deliveryreceipt->process(array(
642                                                                               'jobRecord' => $item
643                                                                       ), array());
644                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
645
646                                         $writer                 = new \Utility\Export\PdfTemplate($pdf);
647                                         $writer_Declaration     = new \Utility\Export\PdfTemplate($pdf_Declaration);
648                                         $writer_Deliveryreceipt = new \Utility\Export\PdfTemplate($pdf_Deliveryreceipt);
649
650                                         $attachments = array(
651                                                 'auction' . $item->id . '.pdf'         => $writer->output(''),
652                                                 'declaration' . $item->id . '.pdf'     => $writer_Declaration->output(''),
653                                                 'deliveryreceipt' . $item->id . '.pdf' => $writer_Deliveryreceipt->output('')
654                                         );
655
656
657                                         #-> Admin.
658                                         $oNotify->sendFromTemplate(
659                                                 $item->company->id, $item->createdBy->id,
660                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
661                                                 'info@bid4cars.com.na', null,
662                                                 'Auction Successful - ' . $item->id,
663                                                 'auction-sale-buyer',
664                                                 array(
665                                                         'first_name'             => 'Admin',
666                                                         'family_name'            => '',
667                                                         'vehicle'                => $vehicle,
668                                                         'price'                  => $currPrefix . $item->currentBid->amount,
669                                                         'seller'                 => $item->company->name,
670                                                         'seller_name'            => $item->createdBy->firstName . ' '
671                                                                                     . $item->createdBy->familyName,
672                                                         'vehicle_address'        => $item->company->city->region->name
673                                                                                     . ', ' . $item->company->city->name
674                                                                                     . ', ' . $item->company->street
675                                                                                     . ', ' . $item->company->postalCode,
676                                                         'seller_billing_address' => $item->company->billingCity
677                                                                 ? $item->company->billingCity->region->name
678                                                                   . ', ' . $item->company->billingCity->name
679                                                                   . ', ' . $item->company->billingStreet
680                                                                   . ', ' . $item->company->billingPostalCode
681                                                                 : '',
682                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
683                                                                 ? $item->company->vatNumber
684                                                                 : '',
685                                                         'seller_email'           => $item->createdBy->email,
686                                                         'stock_number'           => $item->stock->stockNumber,
687                                                         'buyer'                  => $item->currentBid->company->name,
688                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
689                                                                                     . $item->currentBid->profile->familyName,
690                                                         'buyer_address'          => $item->currentBid->company->city->region->name
691                                                                                     . ', ' . $item->currentBid->company->city->name
692                                                                                     . ', ' . $item->currentBid->company->street
693                                                                                     . ', ' . $item->currentBid->company->postalCode,
694                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
695                                                                 ? $item->currentBid->company->vatNumber
696                                                                 : '',
697                                                         'buyer_email'            => $item->currentBid->profile->email,
698                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
699                                                 ),
700                                                 $attachments,
701                                                 array(),
702                                                 false,
703                                                 true
704                                         );
705
706                                         #-> Combined freight.
707                                         $oNotify->sendFromTemplate(
708                                                 $item->company->id, $item->createdBy->id,
709                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
710                                                 'louise@combinefreight.co.za', null,
711                                                 'Auction Successful - ' . $item->id,
712                                                 'auction-sale-buyer',
713                                                 array(
714                                                         'first_name'             => 'Admin',
715                                                         'family_name'            => '',
716                                                         'vehicle'                => $vehicle,
717                                                         'price'                  => $currPrefix . $item->currentBid->amount,
718                                                         'seller'                 => $item->company->name,
719                                                         'seller_name'            => $item->createdBy->firstName . ' '
720                                                                                     . $item->createdBy->familyName,
721                                                         'vehicle_address'        => $item->company->city->region->name
722                                                                                     . ', ' . $item->company->city->name
723                                                                                     . ', ' . $item->company->street
724                                                                                     . ', ' . $item->company->postalCode,
725                                                         'seller_billing_address' => $item->company->billingCity
726                                                                 ? $item->company->billingCity->region->name
727                                                                   . ', ' . $item->company->billingCity->name
728                                                                   . ', ' . $item->company->billingStreet
729                                                                   . ', ' . $item->company->billingPostalCode
730                                                                 : '',
731                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
732                                                                 ? $item->company->vatNumber
733                                                                 : '',
734                                                         'seller_email'           => $item->createdBy->email,
735                                                         'stock_number'           => $item->stock->stockNumber,
736                                                         'buyer'                  => $item->currentBid->company->name,
737                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
738                                                                                     . $item->currentBid->profile->familyName,
739                                                         'buyer_address'          => $item->currentBid->company->city->region->name
740                                                                                     . ', ' . $item->currentBid->company->city->name
741                                                                                     . ', ' . $item->currentBid->company->street
742                                                                                     . ', ' . $item->currentBid->company->postalCode,
743                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
744                                                                 ? $item->currentBid->company->vatNumber
745                                                                 : '',
746                                                         'buyer_email'            => $item->currentBid->profile->email,
747                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
748                                                 ),
749                                                 $attachments,
750                                                 array(),
751                                                 false,
752                                                 true
753                                         );
754
755
756                                         #-> Buyer.
757                                         $oNotify->sendFromTemplate(
758                                                 $item->company->id, $item->createdBy->id,
759                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
760                                                 $item->currentBid->profile->email, null,
761                                                 'Bid Successful - ' . $item->id,
762                                                 'auction-sale-buyer',
763                                                 array(
764                                                         'first_name'             => $item->currentBid->profile->firstName,
765                                                         'family_name'            => $item->currentBid->profile->familyName,
766                                                         'vehicle'                => $vehicle,
767                                                         'price'                  => $currPrefix . $item->currentBid->amount,
768                                                         'seller'                 => $item->company->name,
769                                                         'seller_name'            => $item->createdBy->firstName . ' '
770                                                                                     . $item->createdBy->familyName,
771                                                         'vehicle_address'        => $item->company->city->region->name
772                                                                                     . ', ' . $item->company->city->name
773                                                                                     . ', ' . $item->company->street
774                                                                                     . ', ' . $item->company->postalCode,
775                                                         'seller_billing_address' => $item->company->billingCity
776                                                                 ? $item->company->billingCity->region->name
777                                                                   . ', ' . $item->company->billingCity->name
778                                                                   . ', ' . $item->company->billingStreet
779                                                                   . ', ' . $item->company->billingPostalCode
780                                                                 : '',
781                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
782                                                                 ? $item->company->vatNumber
783                                                                 : '',
784                                                         'seller_email'           => $item->createdBy->email,
785                                                         'stock_number'           => $item->stock->stockNumber,
786                                                         'buyer'                  => $item->currentBid->company->name,
787                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
788                                                                                     . $item->currentBid->profile->familyName,
789                                                         'buyer_address'          => $item->currentBid->company->city->region->name
790                                                                                     . ', ' . $item->currentBid->company->city->name
791                                                                                     . ', ' . $item->currentBid->company->street
792                                                                                     . ', ' . $item->currentBid->company->postalCode,
793                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
794                                                                 ? $item->currentBid->company->vatNumber
795                                                                 : '',
796                                                         'buyer_email'            => $item->currentBid->profile->email,
797                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
798                                                 ),
799                                                 $attachments,
800                                                 array(),
801                                                 false,
802                                                 true
803                                         );
804
805                                         if ($item->stock->company->id != $item->company->id && '' != $item->stock->company->contact->email)
806                                         {
807                                                 //send `auction-sale-seller` email to $item->stock->company->contact->email
808                                                 $oNotify->sendFromTemplate(
809                                                         $item->company->id, $item->createdBy->id,
810                                                         $item->company->id, $item->createdBy->id,
811                                                         $item->stock->company->contact->email, null,
812                                                         'Auction Successful - ' . $item->id,
813                                                         'auction-sale-seller',
814                                                         array(
815                                                                 'first_name'             => $item->stock->company->contact->firstName,
816                                                                 'family_name'            => $item->stock->company->contact->familyName,
817                                                                 'vehicle'                => $vehicle,
818                                                                 'price'                  => $currPrefix . $item->currentBid->amount,
819                                                                 'buyer'                  => $item->currentBid->company->name,
820                                                                 'buyer_name'             => $item->currentBid->profile->firstName . ' '
821                                                                                             . $item->currentBid->profile->familyName,
822                                                                 'buyer_address'          => $item->currentBid->company->city->region->name
823                                                                                             . ', ' . $item->currentBid->company->city->name
824                                                                                             . ', ' . $item->currentBid->company->street
825                                                                                             . ', ' . $item->currentBid->company->postalCode,
826                                                                 'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
827                                                                         ? $item->currentBid->company->vatNumber
828                                                                         : '',
829                                                                 'buyer_email'            => $item->currentBid->profile->email,
830                                                                 'buyer_mobile'           => $item->currentBid->profile->mobile,
831                                                                 'seller'                 => $item->company->name,
832                                                                 'seller_name'            => $item->createdBy->firstName . ' '
833                                                                                             . $item->createdBy->familyName,
834                                                                 'vehicle_address'        => $item->company->city->region->name
835                                                                                             . ', ' . $item->company->city->name
836                                                                                             . ', ' . $item->company->street
837                                                                                             . ', ' . $item->company->postalCode,
838                                                                 'seller_billing_address' => $item->company->billingCity
839                                                                         ? $item->company->billingCity->region->name
840                                                                           . ', ' . $item->company->billingCity->name
841                                                                           . ', ' . $item->company->billingStreet
842                                                                           . ', ' . $item->company->billingPostalCode
843                                                                         : '',
844                                                                 'seller_vat_number'      => !is_null($item->company->vatNumber)
845                                                                         ? $item->company->vatNumber
846                                                                         : '',
847                                                                 'seller_email'           => $item->createdBy->email,
848                                                                 'stock_number'           => $item->stock->stockNumber
849                                                         ),
850                                                         $attachments,
851                                                         array(),
852                                                         false,
853                                                         true
854                                                 );
855
856                                         }
857
858                                         #-> Seller.
859                                         $oNotify->sendFromTemplate(
860                                                 $item->company->id, $item->createdBy->id,
861                                                 $item->company->id, $item->createdBy->id,
862                                                 $item->createdBy->email, null,
863                                                 'Auction Successful - ' . $item->id,
864                                                 'auction-sale-seller',
865                                                 array(
866                                                         'first_name'             => $item->createdBy->firstName,
867                                                         'family_name'            => $item->createdBy->familyName,
868                                                         'vehicle'                => $vehicle,
869                                                         'price'                  => $currPrefix . $item->currentBid->amount,
870                                                         'buyer'                  => $item->currentBid->company->name,
871                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
872                                                                                     . $item->currentBid->profile->familyName,
873                                                         'buyer_address'          => $item->currentBid->company->city->region->name
874                                                                                     . ', ' . $item->currentBid->company->city->name
875                                                                                     . ', ' . $item->currentBid->company->street
876                                                                                     . ', ' . $item->currentBid->company->postalCode,
877                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
878                                                                 ? $item->currentBid->company->vatNumber
879                                                                 : '',
880                                                         'buyer_email'            => $item->currentBid->profile->email,
881                                                         'buyer_mobile'           => $item->currentBid->profile->mobile,
882                                                         'seller'                 => $item->company->name,
883                                                         'seller_name'            => $item->createdBy->firstName . ' '
884                                                                                     . $item->createdBy->familyName,
885                                                         'vehicle_address'        => $item->company->city->region->name
886                                                                                     . ', ' . $item->company->city->name
887                                                                                     . ', ' . $item->company->street
888                                                                                     . ', ' . $item->company->postalCode,
889                                                         'seller_billing_address' => $item->company->billingCity
890                                                                 ? $item->company->billingCity->region->name
891                                                                   . ', ' . $item->company->billingCity->name
892                                                                   . ', ' . $item->company->billingStreet
893                                                                   . ', ' . $item->company->billingPostalCode
894                                                                 : '',
895                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
896                                                                 ? $item->company->vatNumber
897                                                                 : '',
898                                                         'seller_email'           => $item->createdBy->email,
899                                                         'stock_number'           => $item->stock->stockNumber
900                                                 ),
901                                                 $attachments,
902                                                 array(),
903                                                 false,
904                                                 true
905                                         );
906                                         $attachments = array();
907                                 }
908                                 else
909                                 {
910                                         #-> No winner, send notification to seller & admin & feeder.
911                                         $oNotify->sendFromTemplate(
912                                                 $item->company->id, $item->createdBy->id,
913                                                 $item->company->id, $item->createdBy->id,
914                                                 $item->createdBy->email, null,
915                                                 'No winner on Bid4Cars Auction - ' . $item->id,
916                                                 'auction-no-sale',
917                                                 array(
918                                                         'first_name'         => $item->createdBy->firstName,
919                                                         'family_name'        => $item->createdBy->familyName,
920                                                         'vehicle'            => $vehicle,
921                                                         'reservePrice'       => $item->reservePrice,
922                                                         'registrationNumber' => $item->stock->registrationNumber,
923                                                         'stockNumber'        => $item->stock->stockNumber
924                                                 ),
925                                                 array(),
926                                                 array(),
927                                                 false,
928                                                 true
929                                         );
930
931                                         $oNotify->sendFromTemplate(
932                                                 $item->company->id, $item->createdBy->id,
933                                                 $item->company->id, $item->createdBy->id,
934                                                 'info@bid4cars.com.na', null,
935                                                 'No winner on Bid4Cars Auction - ' . $item->id,
936                                                 'auction-no-sale',
937                                                 array(
938                                                         'first_name'         => $item->createdBy->firstName,
939                                                         'family_name'        => $item->createdBy->familyName,
940                                                         'vehicle'            => $vehicle,
941                                                         'reservePrice'       => $item->reservePrice,
942                                                         'registrationNumber' => $item->stock->registrationNumber,
943                                                         'stockNumber'        => $item->stock->stockNumber
944                                                 ),
945                                                 array(),
946                                                 array(),
947                                                 false,
948                                                 true
949                                         );
950
951                                         if ($item->stock->company->id != $item->company->id && '' != $item->stock->company->contact->email)
952                                         {
953                                                 $oNotify->sendFromTemplate(
954                                                         $item->company->id, $item->createdBy->id,
955                                                         $item->company->id, $item->createdBy->id,
956                                                         $item->stock->company->contact->email, null,
957                                                         'No winner on Bid4Cars Auction - ' . $item->id,
958                                                         'auction-no-sale',
959                                                         array(
960                                                                 'first_name'         => $item->stock->company->contact->firstName,
961                                                                 'family_name'        => $item->stock->company->contact->familyName,
962                                                                 'vehicle'            => $vehicle,
963                                                                 'reservePrice'       => $item->reservePrice,
964                                                                 'registrationNumber' => $item->stock->registrationNumber,
965                                                                 'stockNumber'        => $item->stock->stockNumber
966                                                         ),
967                                                         array(),
968                                                         array(),
969                                                         false,
970                                                         true
971                                                 );
972
973                                         }
974
975                                 }
976                                 $item->emailSent = true;
977                                 $this->em->flush($item);
978                         }
979                         catch (\Exception $e)
980                         {
981                                 \Utility\Debug::errorLog('Error on Auction cron process for item ' . $item->id, $e->getMessage());
982                         }
983                 }
984                 error_log('Auction cron ended @ ' . date('Y-m-d H:i:s'));
985                 error_log('------------------------------------');
986         }
987
988         /**
989          * Contract get total vehicels on auction.
990          * @param object|null $jobRecord
991          * @param array       $input
992          * @return \Workspace\Contract\UseOnce
993          */
994         public function contractCount($jobRecord)
995         {
996                 $options     = new \Workspace\UseCase\Options();
997                 $requirement = new \Workspace\UseCase\Requirement();
998                 return new \Workspace\Contract\Recurring($options, $requirement);
999         }
1000
1001         /**
1002          * get total vehicels on auction.
1003          * @param object|null                           $jobRecord
1004          * @param \Workspace\Utility\ServiceInputParams $contract
1005          * @return array
1006          */
1007         public function executeCount($jobRecord, \Workspace\Utility\ServiceInputParams $contract)
1008         {
1009                 $result = $this->em->createQuery(
1010                         'SELECT COUNT(auction) AS total '
1011                         . 'FROM \Auction\Entity\Auction auction '
1012                         . 'WHERE auction.jobState = \'Active\' '
1013                         . ' AND auction.archived = \'0\''
1014                 )
1015                         ->getSingleResult();
1016                 return $contract->success('Totals collected.', array('Items' => $result['total']));
1017         }
1018
1019
1020
1021
1022
1023         //
1024
1025         /**
1026          * CRON functionality: Move Auction items as needed.
1027          */
1028         public function cronProcessCustom()
1029         {
1030                 ini_set('memory_limit', '512M');
1031                 error_log('------------------------------------');
1032                 error_log('Auction cron started @ ' . date('Y-m-d H:i:s'));
1033                 #-> Phase 1.
1034                 $date   = date('Y-m-d H:i:s');
1035                 $result = $this->em->createQuery(
1036                         'SELECT auction FROM \Auction\Entity\Auction auction '
1037                         . 'WHERE auction.id IN (:ids)'
1038                 )
1039                         ->setParameter('ids', array(30871, 30881, 30868, 30874, 30880, 30882, 30883, 30905))
1040                         ->getResult();
1041                 error_log("Item count: " . count($result));
1042                 foreach ($result as $item)
1043                 {
1044                         try
1045                         {
1046                                 #-> General data prep.
1047                                 $vehicle    = $item->stock->type->model->make->name
1048                                               . ', ' . $item->stock->type->model->name
1049                                               . ', ' . $item->stock->type->name;
1050                                 $currPrefix = \Utility\Definitions\Locale::getCurrencyPrefix() . ' ';
1051                                 $oNotify    = new \Utility\Comms\Notification();
1052
1053                                 if (!is_null($item->currentBid) && $item->currentBid->amount > $item->reservePrice && 'Sold' == $item->jobState)
1054                                 {
1055                                         #-> Update record to reflect winner.
1056                                         if (is_null($item->soldToCompany))
1057                                         {
1058                                                 $item->soldToCompany = $item->currentBid->company;
1059                                         }
1060                                         if (is_null($item->soldToProfile))
1061                                         {
1062                                                 $item->soldToProfile = $item->currentBid->profile;
1063                                         }
1064
1065                                         #-> Prep work.
1066                                         #-> Auction PDF.
1067                                         $pdf = new \Auction\Pdf\CompleteNew();
1068                                         $pdf->process(array(
1069                                                               'jobRecord' => $item
1070                                                       ), array());
1071                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
1072
1073                                         #-> Declaration PDF.
1074                                         $pdf_Declaration = new \Auction\Pdf\Declaration();
1075                                         $pdf_Declaration->process(array(
1076                                                                           'jobRecord' => $item
1077                                                                   ), array());
1078                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
1079
1080                                         #-> Deliveryreceipt PDF.
1081                                         $pdf_Deliveryreceipt = new \Auction\Pdf\Deliveryreceipt();
1082                                         $pdf_Deliveryreceipt->process(array(
1083                                                                               'jobRecord' => $item
1084                                                                       ), array());
1085                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
1086
1087                                         $writer                 = new \Utility\Export\PdfTemplate($pdf);
1088                                         $writer_Declaration     = new \Utility\Export\PdfTemplate($pdf_Declaration);
1089                                         $writer_Deliveryreceipt = new \Utility\Export\PdfTemplate($pdf_Deliveryreceipt);
1090
1091                                         $attachments = array(
1092                                                 'auction' . $item->id . '.pdf'         => $writer->output(''),
1093                                                 'declaration' . $item->id . '.pdf'     => $writer_Declaration->output(''),
1094                                                 'deliveryreceipt' . $item->id . '.pdf' => $writer_Deliveryreceipt->output('')
1095                                         );
1096
1097
1098                                         #-> Admin.
1099                                         $oNotify->sendFromTemplate(
1100                                                 $item->company->id, $item->createdBy->id,
1101                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
1102                                                 'info@bid4cars.com.na', null,
1103                                                 'Auction Successful - ' . $item->id,
1104                                                 'auction-sale-buyer',
1105                                                 array(
1106                                                         'first_name'             => 'Admin',
1107                                                         'family_name'            => '',
1108                                                         'vehicle'                => $vehicle,
1109                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1110                                                         'seller'                 => $item->company->name,
1111                                                         'seller_name'            => $item->createdBy->firstName . ' '
1112                                                                                     . $item->createdBy->familyName,
1113                                                         'vehicle_address'        => $item->company->city->region->name
1114                                                                                     . ', ' . $item->company->city->name
1115                                                                                     . ', ' . $item->company->street
1116                                                                                     . ', ' . $item->company->postalCode,
1117                                                         'seller_billing_address' => $item->company->billingCity
1118                                                                 ? $item->company->billingCity->region->name
1119                                                                   . ', ' . $item->company->billingCity->name
1120                                                                   . ', ' . $item->company->billingStreet
1121                                                                   . ', ' . $item->company->billingPostalCode
1122                                                                 : '',
1123                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1124                                                                 ? $item->company->vatNumber
1125                                                                 : '',
1126                                                         'seller_email'           => $item->createdBy->email,
1127                                                         'stock_number'           => $item->stock->stockNumber,
1128                                                         'buyer'                  => $item->currentBid->company->name,
1129                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1130                                                                                     . $item->currentBid->profile->familyName,
1131                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1132                                                                                     . ', ' . $item->currentBid->company->city->name
1133                                                                                     . ', ' . $item->currentBid->company->street
1134                                                                                     . ', ' . $item->currentBid->company->postalCode,
1135                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1136                                                                 ? $item->currentBid->company->vatNumber
1137                                                                 : '',
1138                                                         'buyer_email'            => $item->currentBid->profile->email,
1139                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
1140                                                 ),
1141                                                 $attachments,
1142                                                 array(),
1143                                                 false,
1144                                                 true
1145                                         );
1146
1147
1148                                         #-> Admin.
1149                                         $oNotify->sendFromTemplate(
1150                                                 $item->company->id, $item->createdBy->id,
1151                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
1152                                                 'louise@combinefreight.co.za', null,
1153                                                 'Auction Successful - ' . $item->id,
1154                                                 'auction-sale-buyer',
1155                                                 array(
1156                                                         'first_name'             => 'Admin',
1157                                                         'family_name'            => '',
1158                                                         'vehicle'                => $vehicle,
1159                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1160                                                         'seller'                 => $item->company->name,
1161                                                         'seller_name'            => $item->createdBy->firstName . ' '
1162                                                                                     . $item->createdBy->familyName,
1163                                                         'vehicle_address'        => $item->company->city->region->name
1164                                                                                     . ', ' . $item->company->city->name
1165                                                                                     . ', ' . $item->company->street
1166                                                                                     . ', ' . $item->company->postalCode,
1167                                                         'seller_billing_address' => $item->company->billingCity
1168                                                                 ? $item->company->billingCity->region->name
1169                                                                   . ', ' . $item->company->billingCity->name
1170                                                                   . ', ' . $item->company->billingStreet
1171                                                                   . ', ' . $item->company->billingPostalCode
1172                                                                 : '',
1173                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1174                                                                 ? $item->company->vatNumber
1175                                                                 : '',
1176                                                         'seller_email'           => $item->createdBy->email,
1177                                                         'stock_number'           => $item->stock->stockNumber,
1178                                                         'buyer'                  => $item->currentBid->company->name,
1179                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1180                                                                                     . $item->currentBid->profile->familyName,
1181                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1182                                                                                     . ', ' . $item->currentBid->company->city->name
1183                                                                                     . ', ' . $item->currentBid->company->street
1184                                                                                     . ', ' . $item->currentBid->company->postalCode,
1185                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1186                                                                 ? $item->currentBid->company->vatNumber
1187                                                                 : '',
1188                                                         'buyer_email'            => $item->currentBid->profile->email,
1189                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
1190                                                 ),
1191                                                 $attachments,
1192                                                 array(),
1193                                                 false,
1194                                                 true
1195                                         );
1196
1197                                         #-> Buyer.
1198                                         $oNotify->sendFromTemplate(
1199                                                 $item->company->id, $item->createdBy->id,
1200                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
1201                                                 $item->currentBid->profile->email, null,
1202                                                 'Bid Successful - ' . $item->id,
1203                                                 'auction-sale-buyer',
1204                                                 array(
1205                                                         'first_name'             => $item->currentBid->profile->firstName,
1206                                                         'family_name'            => $item->currentBid->profile->familyName,
1207                                                         'vehicle'                => $vehicle,
1208                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1209                                                         'seller'                 => $item->company->name,
1210                                                         'seller_name'            => $item->createdBy->firstName . ' '
1211                                                                                     . $item->createdBy->familyName,
1212                                                         'vehicle_address'        => $item->company->city->region->name
1213                                                                                     . ', ' . $item->company->city->name
1214                                                                                     . ', ' . $item->company->street
1215                                                                                     . ', ' . $item->company->postalCode,
1216                                                         'seller_billing_address' => $item->company->billingCity
1217                                                                 ? $item->company->billingCity->region->name
1218                                                                   . ', ' . $item->company->billingCity->name
1219                                                                   . ', ' . $item->company->billingStreet
1220                                                                   . ', ' . $item->company->billingPostalCode
1221                                                                 : '',
1222                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1223                                                                 ? $item->company->vatNumber
1224                                                                 : '',
1225                                                         'seller_email'           => $item->createdBy->email,
1226                                                         'stock_number'           => $item->stock->stockNumber,
1227                                                         'buyer'                  => $item->currentBid->company->name,
1228                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1229                                                                                     . $item->currentBid->profile->familyName,
1230                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1231                                                                                     . ', ' . $item->currentBid->company->city->name
1232                                                                                     . ', ' . $item->currentBid->company->street
1233                                                                                     . ', ' . $item->currentBid->company->postalCode,
1234                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1235                                                                 ? $item->currentBid->company->vatNumber
1236                                                                 : '',
1237                                                         'buyer_email'            => $item->currentBid->profile->email,
1238                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
1239                                                 ),
1240                                                 $attachments,
1241                                                 array(),
1242                                                 false,
1243                                                 true
1244                                         );
1245
1246                                         #-> Feeder.
1247
1248                                         if ($item->stock->company->id != $item->company->id && '' != $item->stock->company->contact->email)
1249                                         {
1250                                                 //send `auction-sale-seller` email to $item->stock->company->contact->email
1251                                                 $oNotify->sendFromTemplate(
1252                                                         $item->company->id, $item->createdBy->id,
1253                                                         $item->company->id, $item->createdBy->id,
1254                                                         $item->stock->company->contact->email, null,
1255                                                         'Auction Successful - ' . $item->id,
1256                                                         'auction-sale-seller',
1257                                                         array(
1258                                                                 'first_name'             => $item->stock->company->contact->firstName,
1259                                                                 'family_name'            => $item->stock->company->contact->familyName,
1260                                                                 'vehicle'                => $vehicle,
1261                                                                 'price'                  => $currPrefix . $item->currentBid->amount,
1262                                                                 'buyer'                  => $item->currentBid->company->name,
1263                                                                 'buyer_name'             => $item->currentBid->profile->firstName . ' '
1264                                                                                             . $item->currentBid->profile->familyName,
1265                                                                 'buyer_address'          => $item->currentBid->company->city->region->name
1266                                                                                             . ', ' . $item->currentBid->company->city->name
1267                                                                                             . ', ' . $item->currentBid->company->street
1268                                                                                             . ', ' . $item->currentBid->company->postalCode,
1269                                                                 'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1270                                                                         ? $item->currentBid->company->vatNumber
1271                                                                         : '',
1272                                                                 'buyer_email'            => $item->currentBid->profile->email,
1273                                                                 'buyer_mobile'           => $item->currentBid->profile->mobile,
1274                                                                 'seller'                 => $item->company->name,
1275                                                                 'seller_name'            => $item->createdBy->firstName . ' '
1276                                                                                             . $item->createdBy->familyName,
1277                                                                 'vehicle_address'        => $item->company->city->region->name
1278                                                                                             . ', ' . $item->company->city->name
1279                                                                                             . ', ' . $item->company->street
1280                                                                                             . ', ' . $item->company->postalCode,
1281                                                                 'seller_billing_address' => $item->company->billingCity
1282                                                                         ? $item->company->billingCity->region->name
1283                                                                           . ', ' . $item->company->billingCity->name
1284                                                                           . ', ' . $item->company->billingStreet
1285                                                                           . ', ' . $item->company->billingPostalCode
1286                                                                         : '',
1287                                                                 'seller_vat_number'      => !is_null($item->company->vatNumber)
1288                                                                         ? $item->company->vatNumber
1289                                                                         : '',
1290                                                                 'seller_email'           => $item->createdBy->email,
1291                                                                 'stock_number'           => $item->stock->stockNumber
1292                                                         ),
1293                                                         $attachments,
1294                                                         array(),
1295                                                         false,
1296                                                         true
1297                                                 );
1298                                         }
1299
1300                                         #-> Seller.
1301                                         $oNotify->sendFromTemplate(
1302                                                 $item->company->id, $item->createdBy->id,
1303                                                 $item->company->id, $item->createdBy->id,
1304                                                 $item->createdBy->email, null,
1305                                                 'Auction Successful - ' . $item->id,
1306                                                 'auction-sale-seller',
1307                                                 array(
1308                                                         'first_name'             => $item->createdBy->firstName,
1309                                                         'family_name'            => $item->createdBy->familyName,
1310                                                         'vehicle'                => $vehicle,
1311                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1312                                                         'buyer'                  => $item->currentBid->company->name,
1313                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1314                                                                                     . $item->currentBid->profile->familyName,
1315                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1316                                                                                     . ', ' . $item->currentBid->company->city->name
1317                                                                                     . ', ' . $item->currentBid->company->street
1318                                                                                     . ', ' . $item->currentBid->company->postalCode,
1319                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1320                                                                 ? $item->currentBid->company->vatNumber
1321                                                                 : '',
1322                                                         'buyer_email'            => $item->currentBid->profile->email,
1323                                                         'buyer_mobile'           => $item->currentBid->profile->mobile,
1324                                                         'seller'                 => $item->company->name,
1325                                                         'seller_name'            => $item->createdBy->firstName . ' '
1326                                                                                     . $item->createdBy->familyName,
1327                                                         'vehicle_address'        => $item->company->city->region->name
1328                                                                                     . ', ' . $item->company->city->name
1329                                                                                     . ', ' . $item->company->street
1330                                                                                     . ', ' . $item->company->postalCode,
1331                                                         'seller_billing_address' => $item->company->billingCity
1332                                                                 ? $item->company->billingCity->region->name
1333                                                                   . ', ' . $item->company->billingCity->name
1334                                                                   . ', ' . $item->company->billingStreet
1335                                                                   . ', ' . $item->company->billingPostalCode
1336                                                                 : '',
1337                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1338                                                                 ? $item->company->vatNumber
1339                                                                 : '',
1340                                                         'seller_email'           => $item->createdBy->email,
1341                                                         'stock_number'           => $item->stock->stockNumber
1342                                                 ),
1343                                                 $attachments,
1344                                                 array(),
1345                                                 false,
1346                                                 true
1347                                         );
1348                                         $attachments = array();
1349                                 }
1350                                 else
1351                                 {
1352                                         #-> No winner, send notification to seller & admin & feeder dealer.
1353                                         $oNotify->sendFromTemplate(
1354                                                 $item->company->id, $item->createdBy->id,
1355                                                 $item->company->id, $item->createdBy->id,
1356                                                 $item->createdBy->email, null,
1357                                                 'No winner on Bid4Cars Auction - ' . $item->id,
1358                                                 'auction-no-sale',
1359                                                 array(
1360                                                         'first_name'         => $item->createdBy->firstName,
1361                                                         'family_name'        => $item->createdBy->familyName,
1362                                                         'vehicle'            => $vehicle,
1363                                                         'reservePrice'       => $item->reservePrice,
1364                                                         'registrationNumber' => $item->stock->registrationNumber,
1365                                                         'stockNumber'        => $item->stock->stockNumber
1366                                                 ),
1367                                                 array(),
1368                                                 array(),
1369                                                 false,
1370                                                 true
1371                                         );
1372
1373                                         $oNotify->sendFromTemplate(
1374                                                 $item->company->id, $item->createdBy->id,
1375                                                 $item->company->id, $item->createdBy->id,
1376                                                 'info@bid4cars.com.na', null,
1377                                                 'No winner on Bid4Cars Auction - ' . $item->id,
1378                                                 'auction-no-sale',
1379                                                 array(
1380                                                         'first_name'         => $item->createdBy->firstName,
1381                                                         'family_name'        => $item->createdBy->familyName,
1382                                                         'vehicle'            => $vehicle,
1383                                                         'reservePrice'       => $item->reservePrice,
1384                                                         'registrationNumber' => $item->stock->registrationNumber,
1385                                                         'stockNumber'        => $item->stock->stockNumber
1386                                                 ),
1387                                                 array(),
1388                                                 array(),
1389                                                 false,
1390                                                 true
1391                                         );
1392
1393                                         #No Winner Feeder
1394                                         if ($item->stock->company->id != $item->company->id && '' != $item->stock->company->contact->email)
1395                                         {
1396                                                 $oNotify->sendFromTemplate(
1397                                                         $item->company->id, $item->createdBy->id,
1398                                                         $item->company->id, $item->createdBy->id,
1399                                                         $item->stock->company->contact->email, null,
1400                                                         'No winner on Bid4Cars Auction - ' . $item->id,
1401                                                         'auction-no-sale',
1402                                                         array(
1403                                                                 'first_name'         => $item->stock->company->contact->firstName,
1404                                                                 'family_name'        => $item->stock->company->contact->familyName,
1405                                                                 'vehicle'            => $vehicle,
1406                                                                 'reservePrice'       => $item->reservePrice,
1407                                                                 'registrationNumber' => $item->stock->registrationNumber,
1408                                                                 'stockNumber'        => $item->stock->stockNumber
1409                                                         ),
1410                                                         array(),
1411                                                         array(),
1412                                                         false,
1413                                                         true
1414                                                 );
1415
1416                                         }
1417
1418                                 }
1419                                 $item->emailSent = true;
1420                                 $this->em->flush($item);
1421                         }
1422                         catch (\Exception $e)
1423                         {
1424                                 \Utility\Debug::errorLog('Error on Auction cron process for item ' . $item->id, $e->getMessage());
1425                         }
1426                 }
1427                 error_log('Auction cron ended @ ' . date('Y-m-d H:i:s'));
1428                 error_log('------------------------------------');
1429         }
1430
1431         /**
1432          * CRON functionality: Move Auction items as needed.
1433          */
1434         public function cronProcessCleanup()
1435         {
1436                 $mailTo = 'dino@donetechnologies.co.za';
1437                 ini_set('memory_limit', '512M');
1438                 error_log('------------------------------------');
1439                 error_log('Auction cleanup cron started @ ' . date('Y-m-d H:i:s'));
1440
1441
1442                 #-> Opening mail.
1443                 $numRecsRes   = $this->em->createQuery(
1444                         'SELECT COUNT(auction.id) AS total FROM Auction\\Entity\\Auction auction '
1445                         . 'WHERE auction.endDate <= \'' . date('Y-m-d H:i:s') . '\''
1446                         . ' AND auction.endDate > \'' . date('Y-m-d') . ' 00:00:00\''
1447             . ' AND auction.jobState != :jobStateArchived'
1448                 )
1449             ->setParameter('jobStateArchived', 'Archived')
1450                         ->getSingleResult();
1451                 $totalItems   = (int)$numRecsRes['total'];
1452                 $numRecsRes   = $this->em->createQuery(
1453                         'SELECT COUNT(auction.id) AS total FROM Auction\\Entity\\Auction auction '
1454                         . 'WHERE auction.jobState = :jobState '
1455                         . ' AND auction.endDate <= :startDate'
1456                         . ' AND auction.endDate > :endDate'
1457                 )
1458                         ->setParameter('jobState', 'Sold')
1459                         ->setParameter('startDate', date('Y-m-d H:i:s'))
1460                         ->setParameter('endDate', date('Y-m-d') . ' 00:00:00')
1461                         ->getSingleResult();
1462                 $successItems = (int)$numRecsRes['total'];
1463 //              $result       = $this->em->createQuery(
1464 //                      'SELECT auction FROM \Auction\Entity\Auction auction '
1465 //                      . 'WHERE auction.jobState != :jobState AND auction.jobState != :jobStateTwo'
1466 //                      . ' AND auction.emailSent = :emailSent'
1467 //              )
1468 //                      ->setParameter('jobState', 'Active')
1469 //                      ->setParameter('jobStateTwo', 'Relist')
1470 //                      ->setParameter('emailSent', false)
1471 //                      ->getResult();
1472
1473         $result       = $this->em->createQuery(
1474             'SELECT auction FROM \Auction\Entity\Auction auction '
1475             . 'WHERE auction.jobState != :jobState'
1476             . ' AND auction.jobState != :jobStateArchived'
1477             . ' AND auction.emailSent = :emailSent'
1478         )
1479             ->setParameter('jobState', 'Active')
1480             ->setParameter('jobStateArchived', 'Archived')
1481             ->setParameter('emailSent', false)
1482             ->getResult();
1483
1484                 $mailer       = new \Utility\Comms\Email();
1485                 $mailer->send(array(
1486                                       'From'    => \Utility\Registry::getConfigParam('sourceEmailAddress'),
1487                                       'To'      => $mailTo,
1488                                       'Subject' => 'Auction cleanup cron Start - ' . date('Y-m-d H:i:s'),
1489                                       'Html'    => '<b>Total auction items:<//b> ' . $totalItems . '<br//>'
1490                                                    . '<b>Total items sold:<//b> ' . $successItems . '<br//>'
1491                                                    . '<b>Total items no winner:<//b> ' . ($totalItems - $successItems) . '<br//>'
1492                                                    . '<b>Mails not yet sent:<//b> ' . count($result) . '<br//>'
1493                               ));
1494                 $sent = 0;
1495
1496                 #-> Phase 1.
1497                 error_log("Item count: " . count($result));
1498                 foreach ($result as $item)
1499                 {
1500                         try
1501                         {
1502                                 #-> General data prep.
1503                                 $vehicle    = $item->stock->type->model->make->name
1504                                               . ', ' . $item->stock->type->model->name
1505                                               . ', ' . $item->stock->type->name;
1506                                 $currPrefix = \Utility\Definitions\Locale::getCurrencyPrefix() . ' ';
1507                                 $oNotify    = new \Utility\Comms\Notification();
1508
1509                                 if (!is_null($item->currentBid) && $item->currentBid->amount > $item->reservePrice && 'Sold' == $item->jobState)
1510                                 {
1511                                         #-> Update record to reflect winner.
1512                                         if (is_null($item->soldToCompany))
1513                                         {
1514                                                 $item->soldToCompany = $item->currentBid->company;
1515                                         }
1516                                         if (is_null($item->soldToProfile))
1517                                         {
1518                                                 $item->soldToProfile = $item->currentBid->profile;
1519                                         }
1520
1521                                         #-> Prep work.
1522                                         #-> Auction PDF.
1523                                         $pdf = new \Auction\Pdf\CompleteNew();
1524                                         $pdf->process(array(
1525                                                               'jobRecord' => $item
1526                                                       ), array());
1527                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
1528
1529                                         #-> Declaration PDF.
1530                                         $pdf_Declaration = new \Auction\Pdf\Declaration();
1531                                         $pdf_Declaration->process(array(
1532                                                                           'jobRecord' => $item
1533                                                                   ), array());
1534                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
1535
1536                                         #-> Deliveryreceipt PDF.
1537                                         $pdf_Deliveryreceipt = new \Auction\Pdf\Deliveryreceipt();
1538                                         $pdf_Deliveryreceipt->process(array(
1539                                                                               'jobRecord' => $item
1540                                                                       ), array());
1541                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
1542
1543                                         $writer                 = new \Utility\Export\PdfTemplate($pdf);
1544                                         $writer_Declaration     = new \Utility\Export\PdfTemplate($pdf_Declaration);
1545                                         $writer_Deliveryreceipt = new \Utility\Export\PdfTemplate($pdf_Deliveryreceipt);
1546
1547                                         $attachments = array(
1548                                                 'auction' . $item->id . '.pdf'         => $writer->output(''),
1549                                                 'declaration' . $item->id . '.pdf'     => $writer_Declaration->output(''),
1550                                                 'deliveryreceipt' . $item->id . '.pdf' => $writer_Deliveryreceipt->output('')
1551                                         );
1552
1553
1554                                         #-> Admin.
1555                                         $oNotify->sendFromTemplate(
1556                                                 $item->company->id, $item->createdBy->id,
1557                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
1558                                                 'info@bid4cars.com.na', null,
1559                                                 'Auction Successful - ' . $item->id,
1560                                                 'auction-sale-buyer',
1561                                                 array(
1562                                                         'first_name'             => 'Admin',
1563                                                         'family_name'            => '',
1564                                                         'vehicle'                => $vehicle,
1565                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1566                                                         'seller'                 => $item->company->name,
1567                                                         'seller_name'            => $item->createdBy->firstName . ' '
1568                                                                                     . $item->createdBy->familyName,
1569                                                         'vehicle_address'        => $item->company->city->region->name
1570                                                                                     . ', ' . $item->company->city->name
1571                                                                                     . ', ' . $item->company->street
1572                                                                                     . ', ' . $item->company->postalCode,
1573                                                         'seller_billing_address' => $item->company->billingCity
1574                                                                 ? $item->company->billingCity->region->name
1575                                                                   . ', ' . $item->company->billingCity->name
1576                                                                   . ', ' . $item->company->billingStreet
1577                                                                   . ', ' . $item->company->billingPostalCode
1578                                                                 : '',
1579                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1580                                                                 ? $item->company->vatNumber
1581                                                                 : '',
1582                                                         'seller_email'           => $item->createdBy->email,
1583                                                         'stock_number'           => $item->stock->stockNumber,
1584                                                         'buyer'                  => $item->currentBid->company->name,
1585                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1586                                                                                     . $item->currentBid->profile->familyName,
1587                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1588                                                                                     . ', ' . $item->currentBid->company->city->name
1589                                                                                     . ', ' . $item->currentBid->company->street
1590                                                                                     . ', ' . $item->currentBid->company->postalCode,
1591                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1592                                                                 ? $item->currentBid->company->vatNumber
1593                                                                 : '',
1594                                                         'buyer_email'            => $item->currentBid->profile->email,
1595                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
1596                                                 ),
1597                                                 $attachments,
1598                                                 array(),
1599                                                 false,
1600                                                 true
1601                                         );
1602
1603                                         #-> Admin.
1604                                         $oNotify->sendFromTemplate(
1605                                                 $item->company->id, $item->createdBy->id,
1606                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
1607                                                 'louise@combinefreight.co.za', null,
1608                                                 'Auction Successful - ' . $item->id,
1609                                                 'auction-sale-buyer',
1610                                                 array(
1611                                                         'first_name'             => 'Admin',
1612                                                         'family_name'            => '',
1613                                                         'vehicle'                => $vehicle,
1614                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1615                                                         'seller'                 => $item->company->name,
1616                                                         'seller_name'            => $item->createdBy->firstName . ' '
1617                                                                                     . $item->createdBy->familyName,
1618                                                         'vehicle_address'        => $item->company->city->region->name
1619                                                                                     . ', ' . $item->company->city->name
1620                                                                                     . ', ' . $item->company->street
1621                                                                                     . ', ' . $item->company->postalCode,
1622                                                         'seller_billing_address' => $item->company->billingCity
1623                                                                 ? $item->company->billingCity->region->name
1624                                                                   . ', ' . $item->company->billingCity->name
1625                                                                   . ', ' . $item->company->billingStreet
1626                                                                   . ', ' . $item->company->billingPostalCode
1627                                                                 : '',
1628                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1629                                                                 ? $item->company->vatNumber
1630                                                                 : '',
1631                                                         'seller_email'           => $item->createdBy->email,
1632                                                         'stock_number'           => $item->stock->stockNumber,
1633                                                         'buyer'                  => $item->currentBid->company->name,
1634                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1635                                                                                     . $item->currentBid->profile->familyName,
1636                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1637                                                                                     . ', ' . $item->currentBid->company->city->name
1638                                                                                     . ', ' . $item->currentBid->company->street
1639                                                                                     . ', ' . $item->currentBid->company->postalCode,
1640                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1641                                                                 ? $item->currentBid->company->vatNumber
1642                                                                 : '',
1643                                                         'buyer_email'            => $item->currentBid->profile->email,
1644                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
1645                                                 ),
1646                                                 $attachments,
1647                                                 array(),
1648                                                 false,
1649                                                 true
1650                                         );
1651
1652                                         #-> Buyer.
1653                                         $oNotify->sendFromTemplate(
1654                                                 $item->company->id, $item->createdBy->id,
1655                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
1656                                                 $item->currentBid->profile->email, null,
1657                                                 'Bid Successful - ' . $item->id,
1658                                                 'auction-sale-buyer',
1659                                                 array(
1660                                                         'first_name'             => $item->currentBid->profile->firstName,
1661                                                         'family_name'            => $item->currentBid->profile->familyName,
1662                                                         'vehicle'                => $vehicle,
1663                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1664                                                         'seller'                 => $item->company->name,
1665                                                         'seller_name'            => $item->createdBy->firstName . ' '
1666                                                                                     . $item->createdBy->familyName,
1667                                                         'vehicle_address'        => $item->company->city->region->name
1668                                                                                     . ', ' . $item->company->city->name
1669                                                                                     . ', ' . $item->company->street
1670                                                                                     . ', ' . $item->company->postalCode,
1671                                                         'seller_billing_address' => $item->company->billingCity
1672                                                                 ? $item->company->billingCity->region->name
1673                                                                   . ', ' . $item->company->billingCity->name
1674                                                                   . ', ' . $item->company->billingStreet
1675                                                                   . ', ' . $item->company->billingPostalCode
1676                                                                 : '',
1677                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1678                                                                 ? $item->company->vatNumber
1679                                                                 : '',
1680                                                         'seller_email'           => $item->createdBy->email,
1681                                                         'stock_number'           => $item->stock->stockNumber,
1682                                                         'buyer'                  => $item->currentBid->company->name,
1683                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1684                                                                                     . $item->currentBid->profile->familyName,
1685                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1686                                                                                     . ', ' . $item->currentBid->company->city->name
1687                                                                                     . ', ' . $item->currentBid->company->street
1688                                                                                     . ', ' . $item->currentBid->company->postalCode,
1689                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1690                                                                 ? $item->currentBid->company->vatNumber
1691                                                                 : '',
1692                                                         'buyer_email'            => $item->currentBid->profile->email,
1693                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
1694                                                 ),
1695                                                 $attachments,
1696                                                 array(),
1697                                                 false,
1698                                                 true
1699                                         );
1700
1701                                         #-> Feeder.
1702                                         if ($item->stock->company->id != $item->company->id && '' != $item->stock->company->contact->email)
1703                                         {
1704                                                 //send `auction-sale-seller` email to $item->stock->company->contact->email
1705                                                 $oNotify->sendFromTemplate(
1706                                                         $item->company->id, $item->createdBy->id,
1707                                                         $item->company->id, $item->createdBy->id,
1708                                                         $item->stock->company->contact->email, null,
1709                                                         'Auction Successful - ' . $item->id,
1710                                                         'auction-sale-seller',
1711                                                         array(
1712                                                                 'first_name'             => $item->stock->company->contact->firstName,
1713                                                                 'family_name'            => $item->stock->company->contact->familyName,
1714                                                                 'vehicle'                => $vehicle,
1715                                                                 'price'                  => $currPrefix . $item->currentBid->amount,
1716                                                                 'buyer'                  => $item->currentBid->company->name,
1717                                                                 'buyer_name'             => $item->currentBid->profile->firstName . ' '
1718                                                                                             . $item->currentBid->profile->familyName,
1719                                                                 'buyer_address'          => $item->currentBid->company->city->region->name
1720                                                                                             . ', ' . $item->currentBid->company->city->name
1721                                                                                             . ', ' . $item->currentBid->company->street
1722                                                                                             . ', ' . $item->currentBid->company->postalCode,
1723                                                                 'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1724                                                                         ? $item->currentBid->company->vatNumber
1725                                                                         : '',
1726                                                                 'buyer_email'            => $item->currentBid->profile->email,
1727                                                                 'buyer_mobile'           => $item->currentBid->profile->mobile,
1728                                                                 'seller'                 => $item->company->name,
1729                                                                 'seller_name'            => $item->createdBy->firstName . ' '
1730                                                                                             . $item->createdBy->familyName,
1731                                                                 'vehicle_address'        => $item->company->city->region->name
1732                                                                                             . ', ' . $item->company->city->name
1733                                                                                             . ', ' . $item->company->street
1734                                                                                             . ', ' . $item->company->postalCode,
1735                                                                 'seller_billing_address' => $item->company->billingCity
1736                                                                         ? $item->company->billingCity->region->name
1737                                                                           . ', ' . $item->company->billingCity->name
1738                                                                           . ', ' . $item->company->billingStreet
1739                                                                           . ', ' . $item->company->billingPostalCode
1740                                                                         : '',
1741                                                                 'seller_vat_number'      => !is_null($item->company->vatNumber)
1742                                                                         ? $item->company->vatNumber
1743                                                                         : '',
1744                                                                 'seller_email'           => $item->createdBy->email,
1745                                                                 'stock_number'           => $item->stock->stockNumber
1746                                                         ),
1747                                                         $attachments,
1748                                                         array(),
1749                                                         false,
1750                                                         true
1751                                                 );
1752
1753                                         }
1754
1755                                         #-> Seller.
1756                                         $oNotify->sendFromTemplate(
1757                                                 $item->company->id, $item->createdBy->id,
1758                                                 $item->company->id, $item->createdBy->id,
1759                                                 $item->createdBy->email, null,
1760                                                 'Auction Successful - ' . $item->id,
1761                                                 'auction-sale-seller',
1762                                                 array(
1763                                                         'first_name'             => $item->createdBy->firstName,
1764                                                         'family_name'            => $item->createdBy->familyName,
1765                                                         'vehicle'                => $vehicle,
1766                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1767                                                         'buyer'                  => $item->currentBid->company->name,
1768                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1769                                                                                     . $item->currentBid->profile->familyName,
1770                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1771                                                                                     . ', ' . $item->currentBid->company->city->name
1772                                                                                     . ', ' . $item->currentBid->company->street
1773                                                                                     . ', ' . $item->currentBid->company->postalCode,
1774                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1775                                                                 ? $item->currentBid->company->vatNumber
1776                                                                 : '',
1777                                                         'buyer_email'            => $item->currentBid->profile->email,
1778                                                         'buyer_mobile'           => $item->currentBid->profile->mobile,
1779                                                         'seller'                 => $item->company->name,
1780                                                         'seller_name'            => $item->createdBy->firstName . ' '
1781                                                                                     . $item->createdBy->familyName,
1782                                                         'vehicle_address'        => $item->company->city->region->name
1783                                                                                     . ', ' . $item->company->city->name
1784                                                                                     . ', ' . $item->company->street
1785                                                                                     . ', ' . $item->company->postalCode,
1786                                                         'seller_billing_address' => $item->company->billingCity
1787                                                                 ? $item->company->billingCity->region->name
1788                                                                   . ', ' . $item->company->billingCity->name
1789                                                                   . ', ' . $item->company->billingStreet
1790                                                                   . ', ' . $item->company->billingPostalCode
1791                                                                 : '',
1792                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1793                                                                 ? $item->company->vatNumber
1794                                                                 : '',
1795                                                         'seller_email'           => $item->createdBy->email,
1796                                                         'stock_number'           => $item->stock->stockNumber
1797                                                 ),
1798                                                 $attachments,
1799                                                 array(),
1800                                                 false,
1801                                                 true
1802                                         );
1803                                         $attachments = array();
1804                                 }
1805                                 else
1806                                 {
1807                                         #-> No winner, send notification to seller & admin & feeder.
1808                                         $oNotify->sendFromTemplate(
1809                                                 $item->company->id, $item->createdBy->id,
1810                                                 $item->company->id, $item->createdBy->id,
1811                                                 $item->createdBy->email, null,
1812                                                 'No winner on Bid4Cars Auction - ' . $item->id,
1813                                                 'auction-no-sale',
1814                                                 array(
1815                                                         'first_name'         => $item->createdBy->firstName,
1816                                                         'family_name'        => $item->createdBy->familyName,
1817                                                         'vehicle'            => $vehicle,
1818                                                         'reservePrice'       => $item->reservePrice,
1819                                                         'registrationNumber' => $item->stock->registrationNumber,
1820                                                         'stockNumber'        => $item->stock->stockNumber
1821                                                 ),
1822                                                 array(),
1823                                                 array(),
1824                                                 false,
1825                                                 true
1826                                         );
1827                                         $oNotify->sendFromTemplate(
1828                                                 $item->company->id, $item->createdBy->id,
1829                                                 $item->company->id, $item->createdBy->id,
1830                                                 'info@bid4cars.com.na', null,
1831                                                 'No winner on Bid4Cars Auction - ' . $item->id,
1832                                                 'auction-no-sale',
1833                                                 array(
1834                                                         'first_name'         => $item->createdBy->firstName,
1835                                                         'family_name'        => $item->createdBy->familyName,
1836                                                         'vehicle'            => $vehicle,
1837                                                         'reservePrice'       => $item->reservePrice,
1838                                                         'registrationNumber' => $item->stock->registrationNumber,
1839                                                         'stockNumber'        => $item->stock->stockNumber
1840                                                 ),
1841                                                 array(),
1842                                                 array(),
1843                                                 false,
1844                                                 true
1845                                         );
1846                                         #No Winner Feeder
1847                                         if ($item->stock->company->id != $item->company->id && '' != $item->stock->company->contact->email)
1848                                         {
1849                                                 $oNotify->sendFromTemplate(
1850                                                         $item->company->id, $item->createdBy->id,
1851                                                         $item->company->id, $item->createdBy->id,
1852                                                         $item->stock->company->contact->email, null,
1853                                                         'No winner on Bid4Cars Auction - ' . $item->id,
1854                                                         'auction-no-sale',
1855                                                         array(
1856                                                                 'first_name'         => $item->stock->company->contact->firstName,
1857                                                                 'family_name'        => $item->stock->company->contact->familyName,
1858                                                                 'vehicle'            => $vehicle,
1859                                                                 'reservePrice'       => $item->reservePrice,
1860                                                                 'registrationNumber' => $item->stock->registrationNumber,
1861                                                                 'stockNumber'        => $item->stock->stockNumber
1862                                                         ),
1863                                                         array(),
1864                                                         array(),
1865                                                         false,
1866                                                         true
1867                                                 );
1868
1869                                         }
1870
1871                                 }
1872                                 $item->emailSent = true;
1873                                 $this->em->flush($item);
1874                                 $sent++;
1875                         }
1876                         catch (\Exception $e)
1877                         {
1878                                 \Utility\Debug::errorLog('Error on Auction cron process for item ' . $item->id, $e->getMessage());
1879                         }
1880                 }
1881                 error_log('Auction cleanup cron ended @ ' . date('Y-m-d H:i:s'));
1882                 error_log('------------------------------------');
1883
1884
1885         $mailer = new \Utility\Comms\Email();
1886         $mailer->send(array(
1887             'From'    => \Utility\Registry::getConfigParam('sourceEmailAddress'),
1888             'To'      => $mailTo,
1889             'Subject' => 'Auction cleanup cron End - ' . date('Y-m-d H:i:s'),
1890             'Html'    => '<b>Total auction items:<//b> ' . $totalItems . '<br//>'
1891                 . '<b>Emails sent:<//b> ' . $sent . ' of ' . count($result) . '<br//>'
1892         ));
1893     }
1894
1895
1896 }