latest update
[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                                 if (!is_null($item->currentBid) && $item->currentBid->amount > $item->reservePrice)
564                                 {
565                                         #-> We have a winner.
566                                         $item->soldToCompany = $item->currentBid->company;
567                                         $item->soldToProfile = $item->currentBid->profile;
568                                         $this->workflowNode->changeState('This.Sold');
569                                 }
570                                 else
571                                 {
572                                         #-> Each and every one a loser.
573                                         $this->workflowNode->changeState('This.Relist');
574                                 }
575                         }
576                         catch (\Exception $e)
577                         {
578                                 \Utility\Debug::errorLog('Error on Auction cron process (state change) for item ' . $item->id, $e->getMessage());
579                         }
580                         try
581                         {
582                                 $workflow->handover('Auction', $item->stock->id);
583                         }
584                         catch (\Exception $e)
585                         {
586                                 \Utility\Debug::errorLog('Error on Auction cron process (handover) for item ' . $item->id, $e->getMessage());
587                         }
588                 }
589                 $this->em->flush();
590                 foreach ($result as $item)
591                 {
592                         try
593                         {
594
595                                 #-> General data prep.
596                                 $vehicle    = $item->stock->type->model->make->name
597                                               . ', ' . $item->stock->type->model->name
598                                               . ', ' . $item->stock->type->name;
599                                 $currPrefix = \Utility\Definitions\Locale::getCurrencyPrefix() . ' ';
600                                 $oNotify    = new \Utility\Comms\Notification();
601
602                                 if (!is_null($item->currentBid) && $item->currentBid->amount > $item->reservePrice && 'Sold' == $item->jobState)
603                                 {
604                                         #-> Update record to reflect winner.
605                                         $item->soldToCompany = $item->currentBid->company;
606                                         $item->soldToProfile = $item->currentBid->profile;
607
608                                         #-> Prep work.
609
610                                         #-> Auction PDF.
611                                         $pdf = new \Auction\Pdf\CompleteNew();
612                                         $pdf->process(array(
613                                                               'jobRecord' => $item
614                                                       ), array());
615                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
616
617                                         #-> Declaration PDF.
618                                         $pdf_Declaration = new \Auction\Pdf\Declaration();
619                                         $pdf_Declaration->process(array(
620                                                                           'jobRecord' => $item
621                                                                   ), array());
622                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
623
624                                         #-> Deliveryreceipt PDF.
625                                         $pdf_Deliveryreceipt = new \Auction\Pdf\Deliveryreceipt();
626                                         $pdf_Deliveryreceipt->process(array(
627                                                                               'jobRecord' => $item
628                                                                       ), array());
629                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
630
631                                         $writer                 = new \Utility\Export\PdfTemplate($pdf);
632                                         $writer_Declaration     = new \Utility\Export\PdfTemplate($pdf_Declaration);
633                                         $writer_Deliveryreceipt = new \Utility\Export\PdfTemplate($pdf_Deliveryreceipt);
634
635                                         $attachments = array(
636                                                 'auction' . $item->id . '.pdf'         => $writer->output(''),
637                                                 'declaration' . $item->id . '.pdf'     => $writer_Declaration->output(''),
638                                                 'deliveryreceipt' . $item->id . '.pdf' => $writer_Deliveryreceipt->output('')
639                                         );
640
641
642                                         #-> Admin.
643                                         $oNotify->sendFromTemplate(
644                                                 $item->company->id, $item->createdBy->id,
645                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
646                                                 'info@bid4cars.com.na', null,
647                                                 'Auction Successful - ' . $item->id,
648                                                 'auction-sale-buyer',
649                                                 array(
650                                                         'first_name'             => 'Admin',
651                                                         'family_name'            => '',
652                                                         'vehicle'                => $vehicle,
653                                                         'price'                  => $currPrefix . $item->currentBid->amount,
654                                                         'seller'                 => $item->company->name,
655                                                         'seller_name'            => $item->createdBy->firstName . ' '
656                                                                                     . $item->createdBy->familyName,
657                                                         'vehicle_address'        => $item->company->city->region->name
658                                                                                     . ', ' . $item->company->city->name
659                                                                                     . ', ' . $item->company->street
660                                                                                     . ', ' . $item->company->postalCode,
661                                                         'seller_billing_address' => $item->company->billingCity
662                                                                 ? $item->company->billingCity->region->name
663                                                                   . ', ' . $item->company->billingCity->name
664                                                                   . ', ' . $item->company->billingStreet
665                                                                   . ', ' . $item->company->billingPostalCode
666                                                                 : '',
667                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
668                                                                 ? $item->company->vatNumber
669                                                                 : '',
670                                                         'seller_email'           => $item->createdBy->email,
671                                                         'stock_number'           => $item->stock->stockNumber,
672                                                         'buyer'                  => $item->currentBid->company->name,
673                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
674                                                                                     . $item->currentBid->profile->familyName,
675                                                         'buyer_address'          => $item->currentBid->company->city->region->name
676                                                                                     . ', ' . $item->currentBid->company->city->name
677                                                                                     . ', ' . $item->currentBid->company->street
678                                                                                     . ', ' . $item->currentBid->company->postalCode,
679                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
680                                                                 ? $item->currentBid->company->vatNumber
681                                                                 : '',
682                                                         'buyer_email'            => $item->currentBid->profile->email,
683                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
684                                                 ),
685                                                 $attachments,
686                                                 array(),
687                                                 false,
688                                                 true
689                                         );
690
691                                         #-> Combined freight.
692                                         $oNotify->sendFromTemplate(
693                                                 $item->company->id, $item->createdBy->id,
694                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
695                                                 'louise@combinefreight.co.za', null,
696                                                 'Auction Successful - ' . $item->id,
697                                                 'auction-sale-buyer',
698                                                 array(
699                                                         'first_name'             => 'Admin',
700                                                         'family_name'            => '',
701                                                         'vehicle'                => $vehicle,
702                                                         'price'                  => $currPrefix . $item->currentBid->amount,
703                                                         'seller'                 => $item->company->name,
704                                                         'seller_name'            => $item->createdBy->firstName . ' '
705                                                                                     . $item->createdBy->familyName,
706                                                         'vehicle_address'        => $item->company->city->region->name
707                                                                                     . ', ' . $item->company->city->name
708                                                                                     . ', ' . $item->company->street
709                                                                                     . ', ' . $item->company->postalCode,
710                                                         'seller_billing_address' => $item->company->billingCity
711                                                                 ? $item->company->billingCity->region->name
712                                                                   . ', ' . $item->company->billingCity->name
713                                                                   . ', ' . $item->company->billingStreet
714                                                                   . ', ' . $item->company->billingPostalCode
715                                                                 : '',
716                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
717                                                                 ? $item->company->vatNumber
718                                                                 : '',
719                                                         'seller_email'           => $item->createdBy->email,
720                                                         'stock_number'           => $item->stock->stockNumber,
721                                                         'buyer'                  => $item->currentBid->company->name,
722                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
723                                                                                     . $item->currentBid->profile->familyName,
724                                                         'buyer_address'          => $item->currentBid->company->city->region->name
725                                                                                     . ', ' . $item->currentBid->company->city->name
726                                                                                     . ', ' . $item->currentBid->company->street
727                                                                                     . ', ' . $item->currentBid->company->postalCode,
728                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
729                                                                 ? $item->currentBid->company->vatNumber
730                                                                 : '',
731                                                         'buyer_email'            => $item->currentBid->profile->email,
732                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
733                                                 ),
734                                                 $attachments,
735                                                 array(),
736                                                 false,
737                                                 true
738                                         );
739
740
741                                         #-> Buyer.
742                                         $oNotify->sendFromTemplate(
743                                                 $item->company->id, $item->createdBy->id,
744                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
745                                                 $item->currentBid->profile->email, null,
746                                                 'Bid Successful - ' . $item->id,
747                                                 'auction-sale-buyer',
748                                                 array(
749                                                         'first_name'             => $item->currentBid->profile->firstName,
750                                                         'family_name'            => $item->currentBid->profile->familyName,
751                                                         'vehicle'                => $vehicle,
752                                                         'price'                  => $currPrefix . $item->currentBid->amount,
753                                                         'seller'                 => $item->company->name,
754                                                         'seller_name'            => $item->createdBy->firstName . ' '
755                                                                                     . $item->createdBy->familyName,
756                                                         'vehicle_address'        => $item->company->city->region->name
757                                                                                     . ', ' . $item->company->city->name
758                                                                                     . ', ' . $item->company->street
759                                                                                     . ', ' . $item->company->postalCode,
760                                                         'seller_billing_address' => $item->company->billingCity
761                                                                 ? $item->company->billingCity->region->name
762                                                                   . ', ' . $item->company->billingCity->name
763                                                                   . ', ' . $item->company->billingStreet
764                                                                   . ', ' . $item->company->billingPostalCode
765                                                                 : '',
766                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
767                                                                 ? $item->company->vatNumber
768                                                                 : '',
769                                                         'seller_email'           => $item->createdBy->email,
770                                                         'stock_number'           => $item->stock->stockNumber,
771                                                         'buyer'                  => $item->currentBid->company->name,
772                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
773                                                                                     . $item->currentBid->profile->familyName,
774                                                         'buyer_address'          => $item->currentBid->company->city->region->name
775                                                                                     . ', ' . $item->currentBid->company->city->name
776                                                                                     . ', ' . $item->currentBid->company->street
777                                                                                     . ', ' . $item->currentBid->company->postalCode,
778                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
779                                                                 ? $item->currentBid->company->vatNumber
780                                                                 : '',
781                                                         'buyer_email'            => $item->currentBid->profile->email,
782                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
783                                                 ),
784                                                 $attachments,
785                                                 array(),
786                                                 false,
787                                                 true
788                                         );
789
790                                         if ($item->stock->company->id != $item->company->id && '' != $item->stock->company->contact->email)
791                                         {
792                                                 //send `auction-sale-seller` email to $item->stock->company->contact->email
793                                                 $oNotify->sendFromTemplate(
794                                                         $item->company->id, $item->createdBy->id,
795                                                         $item->company->id, $item->createdBy->id,
796                                                         $item->stock->company->contact->email, null,
797                                                         'Auction Successful - ' . $item->id,
798                                                         'auction-sale-seller',
799                                                         array(
800                                                                 'first_name'             => $item->stock->company->contact->firstName,
801                                                                 'family_name'            => $item->stock->company->contact->familyName,
802                                                                 'vehicle'                => $vehicle,
803                                                                 'price'                  => $currPrefix . $item->currentBid->amount,
804                                                                 'buyer'                  => $item->currentBid->company->name,
805                                                                 'buyer_name'             => $item->currentBid->profile->firstName . ' '
806                                                                                             . $item->currentBid->profile->familyName,
807                                                                 'buyer_address'          => $item->currentBid->company->city->region->name
808                                                                                             . ', ' . $item->currentBid->company->city->name
809                                                                                             . ', ' . $item->currentBid->company->street
810                                                                                             . ', ' . $item->currentBid->company->postalCode,
811                                                                 'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
812                                                                         ? $item->currentBid->company->vatNumber
813                                                                         : '',
814                                                                 'buyer_email'            => $item->currentBid->profile->email,
815                                                                 'buyer_mobile'           => $item->currentBid->profile->mobile,
816                                                                 'seller'                 => $item->company->name,
817                                                                 'seller_name'            => $item->createdBy->firstName . ' '
818                                                                                             . $item->createdBy->familyName,
819                                                                 'vehicle_address'        => $item->company->city->region->name
820                                                                                             . ', ' . $item->company->city->name
821                                                                                             . ', ' . $item->company->street
822                                                                                             . ', ' . $item->company->postalCode,
823                                                                 'seller_billing_address' => $item->company->billingCity
824                                                                         ? $item->company->billingCity->region->name
825                                                                           . ', ' . $item->company->billingCity->name
826                                                                           . ', ' . $item->company->billingStreet
827                                                                           . ', ' . $item->company->billingPostalCode
828                                                                         : '',
829                                                                 'seller_vat_number'      => !is_null($item->company->vatNumber)
830                                                                         ? $item->company->vatNumber
831                                                                         : '',
832                                                                 'seller_email'           => $item->createdBy->email,
833                                                                 'stock_number'           => $item->stock->stockNumber
834                                                         ),
835                                                         $attachments,
836                                                         array(),
837                                                         false,
838                                                         true
839                                                 );
840
841                                         }
842
843                                         #-> Seller.
844                                         $oNotify->sendFromTemplate(
845                                                 $item->company->id, $item->createdBy->id,
846                                                 $item->company->id, $item->createdBy->id,
847                                                 $item->createdBy->email, null,
848                                                 'Auction Successful - ' . $item->id,
849                                                 'auction-sale-seller',
850                                                 array(
851                                                         'first_name'             => $item->createdBy->firstName,
852                                                         'family_name'            => $item->createdBy->familyName,
853                                                         'vehicle'                => $vehicle,
854                                                         'price'                  => $currPrefix . $item->currentBid->amount,
855                                                         'buyer'                  => $item->currentBid->company->name,
856                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
857                                                                                     . $item->currentBid->profile->familyName,
858                                                         'buyer_address'          => $item->currentBid->company->city->region->name
859                                                                                     . ', ' . $item->currentBid->company->city->name
860                                                                                     . ', ' . $item->currentBid->company->street
861                                                                                     . ', ' . $item->currentBid->company->postalCode,
862                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
863                                                                 ? $item->currentBid->company->vatNumber
864                                                                 : '',
865                                                         'buyer_email'            => $item->currentBid->profile->email,
866                                                         'buyer_mobile'           => $item->currentBid->profile->mobile,
867                                                         'seller'                 => $item->company->name,
868                                                         'seller_name'            => $item->createdBy->firstName . ' '
869                                                                                     . $item->createdBy->familyName,
870                                                         'vehicle_address'        => $item->company->city->region->name
871                                                                                     . ', ' . $item->company->city->name
872                                                                                     . ', ' . $item->company->street
873                                                                                     . ', ' . $item->company->postalCode,
874                                                         'seller_billing_address' => $item->company->billingCity
875                                                                 ? $item->company->billingCity->region->name
876                                                                   . ', ' . $item->company->billingCity->name
877                                                                   . ', ' . $item->company->billingStreet
878                                                                   . ', ' . $item->company->billingPostalCode
879                                                                 : '',
880                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
881                                                                 ? $item->company->vatNumber
882                                                                 : '',
883                                                         'seller_email'           => $item->createdBy->email,
884                                                         'stock_number'           => $item->stock->stockNumber
885                                                 ),
886                                                 $attachments,
887                                                 array(),
888                                                 false,
889                                                 true
890                                         );
891                                         $attachments = array();
892                                 }
893                                 else
894                                 {
895                                         #-> No winner, send notification to seller & admin & feeder.
896                                         $oNotify->sendFromTemplate(
897                                                 $item->company->id, $item->createdBy->id,
898                                                 $item->company->id, $item->createdBy->id,
899                                                 $item->createdBy->email, null,
900                                                 'No winner on Bid4Cars Auction - ' . $item->id,
901                                                 'auction-no-sale',
902                                                 array(
903                                                         'first_name'         => $item->createdBy->firstName,
904                                                         'family_name'        => $item->createdBy->familyName,
905                                                         'vehicle'            => $vehicle,
906                                                         'reservePrice'       => $item->reservePrice,
907                                                         'registrationNumber' => $item->stock->registrationNumber,
908                                                         'stockNumber'        => $item->stock->stockNumber
909                                                 ),
910                                                 array(),
911                                                 array(),
912                                                 false,
913                                                 true
914                                         );
915
916                                         $oNotify->sendFromTemplate(
917                                                 $item->company->id, $item->createdBy->id,
918                                                 $item->company->id, $item->createdBy->id,
919                                                 'info@bid4cars.com.na', null,
920                                                 'No winner on Bid4Cars Auction - ' . $item->id,
921                                                 'auction-no-sale',
922                                                 array(
923                                                         'first_name'         => $item->createdBy->firstName,
924                                                         'family_name'        => $item->createdBy->familyName,
925                                                         'vehicle'            => $vehicle,
926                                                         'reservePrice'       => $item->reservePrice,
927                                                         'registrationNumber' => $item->stock->registrationNumber,
928                                                         'stockNumber'        => $item->stock->stockNumber
929                                                 ),
930                                                 array(),
931                                                 array(),
932                                                 false,
933                                                 true
934                                         );
935
936                                         if ($item->stock->company->id != $item->company->id && '' != $item->stock->company->contact->email)
937                                         {
938                                                 $oNotify->sendFromTemplate(
939                                                         $item->company->id, $item->createdBy->id,
940                                                         $item->company->id, $item->createdBy->id,
941                                                         $item->stock->company->contact->email, null,
942                                                         'No winner on Bid4Cars Auction - ' . $item->id,
943                                                         'auction-no-sale',
944                                                         array(
945                                                                 'first_name'         => $item->stock->company->contact->firstName,
946                                                                 'family_name'        => $item->stock->company->contact->familyName,
947                                                                 'vehicle'            => $vehicle,
948                                                                 'reservePrice'       => $item->reservePrice,
949                                                                 'registrationNumber' => $item->stock->registrationNumber,
950                                                                 'stockNumber'        => $item->stock->stockNumber
951                                                         ),
952                                                         array(),
953                                                         array(),
954                                                         false,
955                                                         true
956                                                 );
957
958                                         }
959
960                                 }
961                                 $item->emailSent = true;
962                                 $this->em->flush($item);
963                         }
964                         catch (\Exception $e)
965                         {
966                                 \Utility\Debug::errorLog('Error on Auction cron process for item ' . $item->id, $e->getMessage());
967                         }
968                 }
969                 error_log('Auction cron ended @ ' . date('Y-m-d H:i:s'));
970                 error_log('------------------------------------');
971         }
972
973         /**
974          * Contract get total vehicels on auction.
975          * @param object|null $jobRecord
976          * @param array       $input
977          * @return \Workspace\Contract\UseOnce
978          */
979         public function contractCount($jobRecord)
980         {
981                 $options     = new \Workspace\UseCase\Options();
982                 $requirement = new \Workspace\UseCase\Requirement();
983                 return new \Workspace\Contract\Recurring($options, $requirement);
984         }
985
986         /**
987          * get total vehicels on auction.
988          * @param object|null                           $jobRecord
989          * @param \Workspace\Utility\ServiceInputParams $contract
990          * @return array
991          */
992         public function executeCount($jobRecord, \Workspace\Utility\ServiceInputParams $contract)
993         {
994                 $result = $this->em->createQuery(
995                         'SELECT COUNT(auction) AS total '
996                         . 'FROM \Auction\Entity\Auction auction '
997                         . 'WHERE auction.jobState = \'Active\' '
998                         . ' AND auction.archived = \'0\''
999                 )
1000                         ->getSingleResult();
1001                 return $contract->success('Totals collected.', array('Items' => $result['total']));
1002         }
1003
1004
1005
1006
1007
1008         //
1009
1010         /**
1011          * CRON functionality: Move Auction items as needed.
1012          */
1013         public function cronProcessCustom()
1014         {
1015                 ini_set('memory_limit', '512M');
1016                 error_log('------------------------------------');
1017                 error_log('Auction cron started @ ' . date('Y-m-d H:i:s'));
1018                 #-> Phase 1.
1019                 $date   = date('Y-m-d H:i:s');
1020                 $result = $this->em->createQuery(
1021                         'SELECT auction FROM \Auction\Entity\Auction auction '
1022                         . 'WHERE auction.id IN (:ids)'
1023                 )
1024                         ->setParameter('ids', array(30871, 30881, 30868, 30874, 30880, 30882, 30883, 30905))
1025                         ->getResult();
1026                 error_log("Item count: " . count($result));
1027                 foreach ($result as $item)
1028                 {
1029                         try
1030                         {
1031                                 #-> General data prep.
1032                                 $vehicle    = $item->stock->type->model->make->name
1033                                               . ', ' . $item->stock->type->model->name
1034                                               . ', ' . $item->stock->type->name;
1035                                 $currPrefix = \Utility\Definitions\Locale::getCurrencyPrefix() . ' ';
1036                                 $oNotify    = new \Utility\Comms\Notification();
1037
1038                                 if (!is_null($item->currentBid) && $item->currentBid->amount > $item->reservePrice && 'Sold' == $item->jobState)
1039                                 {
1040                                         #-> Update record to reflect winner.
1041                                         if (is_null($item->soldToCompany))
1042                                         {
1043                                                 $item->soldToCompany = $item->currentBid->company;
1044                                         }
1045                                         if (is_null($item->soldToProfile))
1046                                         {
1047                                                 $item->soldToProfile = $item->currentBid->profile;
1048                                         }
1049
1050                                         #-> Prep work.
1051                                         #-> Auction PDF.
1052                                         $pdf = new \Auction\Pdf\CompleteNew();
1053                                         $pdf->process(array(
1054                                                               'jobRecord' => $item
1055                                                       ), array());
1056                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
1057
1058                                         #-> Declaration PDF.
1059                                         $pdf_Declaration = new \Auction\Pdf\Declaration();
1060                                         $pdf_Declaration->process(array(
1061                                                                           'jobRecord' => $item
1062                                                                   ), array());
1063                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
1064
1065                                         #-> Deliveryreceipt PDF.
1066                                         $pdf_Deliveryreceipt = new \Auction\Pdf\Deliveryreceipt();
1067                                         $pdf_Deliveryreceipt->process(array(
1068                                                                               'jobRecord' => $item
1069                                                                       ), array());
1070                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
1071
1072                                         $writer                 = new \Utility\Export\PdfTemplate($pdf);
1073                                         $writer_Declaration     = new \Utility\Export\PdfTemplate($pdf_Declaration);
1074                                         $writer_Deliveryreceipt = new \Utility\Export\PdfTemplate($pdf_Deliveryreceipt);
1075
1076                                         $attachments = array(
1077                                                 'auction' . $item->id . '.pdf'         => $writer->output(''),
1078                                                 'declaration' . $item->id . '.pdf'     => $writer_Declaration->output(''),
1079                                                 'deliveryreceipt' . $item->id . '.pdf' => $writer_Deliveryreceipt->output('')
1080                                         );
1081
1082
1083                                         #-> Admin.
1084                                         $oNotify->sendFromTemplate(
1085                                                 $item->company->id, $item->createdBy->id,
1086                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
1087                                                 'info@bid4cars.com.na', null,
1088                                                 'Auction Successful - ' . $item->id,
1089                                                 'auction-sale-buyer',
1090                                                 array(
1091                                                         'first_name'             => 'Admin',
1092                                                         'family_name'            => '',
1093                                                         'vehicle'                => $vehicle,
1094                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1095                                                         'seller'                 => $item->company->name,
1096                                                         'seller_name'            => $item->createdBy->firstName . ' '
1097                                                                                     . $item->createdBy->familyName,
1098                                                         'vehicle_address'        => $item->company->city->region->name
1099                                                                                     . ', ' . $item->company->city->name
1100                                                                                     . ', ' . $item->company->street
1101                                                                                     . ', ' . $item->company->postalCode,
1102                                                         'seller_billing_address' => $item->company->billingCity
1103                                                                 ? $item->company->billingCity->region->name
1104                                                                   . ', ' . $item->company->billingCity->name
1105                                                                   . ', ' . $item->company->billingStreet
1106                                                                   . ', ' . $item->company->billingPostalCode
1107                                                                 : '',
1108                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1109                                                                 ? $item->company->vatNumber
1110                                                                 : '',
1111                                                         'seller_email'           => $item->createdBy->email,
1112                                                         'stock_number'           => $item->stock->stockNumber,
1113                                                         'buyer'                  => $item->currentBid->company->name,
1114                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1115                                                                                     . $item->currentBid->profile->familyName,
1116                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1117                                                                                     . ', ' . $item->currentBid->company->city->name
1118                                                                                     . ', ' . $item->currentBid->company->street
1119                                                                                     . ', ' . $item->currentBid->company->postalCode,
1120                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1121                                                                 ? $item->currentBid->company->vatNumber
1122                                                                 : '',
1123                                                         'buyer_email'            => $item->currentBid->profile->email,
1124                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
1125                                                 ),
1126                                                 $attachments,
1127                                                 array(),
1128                                                 false,
1129                                                 true
1130                                         );
1131
1132
1133                                         #-> Admin.
1134                                         $oNotify->sendFromTemplate(
1135                                                 $item->company->id, $item->createdBy->id,
1136                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
1137                                                 'louise@combinefreight.co.za', null,
1138                                                 'Auction Successful - ' . $item->id,
1139                                                 'auction-sale-buyer',
1140                                                 array(
1141                                                         'first_name'             => 'Admin',
1142                                                         'family_name'            => '',
1143                                                         'vehicle'                => $vehicle,
1144                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1145                                                         'seller'                 => $item->company->name,
1146                                                         'seller_name'            => $item->createdBy->firstName . ' '
1147                                                                                     . $item->createdBy->familyName,
1148                                                         'vehicle_address'        => $item->company->city->region->name
1149                                                                                     . ', ' . $item->company->city->name
1150                                                                                     . ', ' . $item->company->street
1151                                                                                     . ', ' . $item->company->postalCode,
1152                                                         'seller_billing_address' => $item->company->billingCity
1153                                                                 ? $item->company->billingCity->region->name
1154                                                                   . ', ' . $item->company->billingCity->name
1155                                                                   . ', ' . $item->company->billingStreet
1156                                                                   . ', ' . $item->company->billingPostalCode
1157                                                                 : '',
1158                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1159                                                                 ? $item->company->vatNumber
1160                                                                 : '',
1161                                                         'seller_email'           => $item->createdBy->email,
1162                                                         'stock_number'           => $item->stock->stockNumber,
1163                                                         'buyer'                  => $item->currentBid->company->name,
1164                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1165                                                                                     . $item->currentBid->profile->familyName,
1166                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1167                                                                                     . ', ' . $item->currentBid->company->city->name
1168                                                                                     . ', ' . $item->currentBid->company->street
1169                                                                                     . ', ' . $item->currentBid->company->postalCode,
1170                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1171                                                                 ? $item->currentBid->company->vatNumber
1172                                                                 : '',
1173                                                         'buyer_email'            => $item->currentBid->profile->email,
1174                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
1175                                                 ),
1176                                                 $attachments,
1177                                                 array(),
1178                                                 false,
1179                                                 true
1180                                         );
1181
1182                                         #-> Buyer.
1183                                         $oNotify->sendFromTemplate(
1184                                                 $item->company->id, $item->createdBy->id,
1185                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
1186                                                 $item->currentBid->profile->email, null,
1187                                                 'Bid Successful - ' . $item->id,
1188                                                 'auction-sale-buyer',
1189                                                 array(
1190                                                         'first_name'             => $item->currentBid->profile->firstName,
1191                                                         'family_name'            => $item->currentBid->profile->familyName,
1192                                                         'vehicle'                => $vehicle,
1193                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1194                                                         'seller'                 => $item->company->name,
1195                                                         'seller_name'            => $item->createdBy->firstName . ' '
1196                                                                                     . $item->createdBy->familyName,
1197                                                         'vehicle_address'        => $item->company->city->region->name
1198                                                                                     . ', ' . $item->company->city->name
1199                                                                                     . ', ' . $item->company->street
1200                                                                                     . ', ' . $item->company->postalCode,
1201                                                         'seller_billing_address' => $item->company->billingCity
1202                                                                 ? $item->company->billingCity->region->name
1203                                                                   . ', ' . $item->company->billingCity->name
1204                                                                   . ', ' . $item->company->billingStreet
1205                                                                   . ', ' . $item->company->billingPostalCode
1206                                                                 : '',
1207                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1208                                                                 ? $item->company->vatNumber
1209                                                                 : '',
1210                                                         'seller_email'           => $item->createdBy->email,
1211                                                         'stock_number'           => $item->stock->stockNumber,
1212                                                         'buyer'                  => $item->currentBid->company->name,
1213                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1214                                                                                     . $item->currentBid->profile->familyName,
1215                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1216                                                                                     . ', ' . $item->currentBid->company->city->name
1217                                                                                     . ', ' . $item->currentBid->company->street
1218                                                                                     . ', ' . $item->currentBid->company->postalCode,
1219                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1220                                                                 ? $item->currentBid->company->vatNumber
1221                                                                 : '',
1222                                                         'buyer_email'            => $item->currentBid->profile->email,
1223                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
1224                                                 ),
1225                                                 $attachments,
1226                                                 array(),
1227                                                 false,
1228                                                 true
1229                                         );
1230
1231                                         #-> Feeder.
1232
1233                                         if ($item->stock->company->id != $item->company->id && '' != $item->stock->company->contact->email)
1234                                         {
1235                                                 //send `auction-sale-seller` email to $item->stock->company->contact->email
1236                                                 $oNotify->sendFromTemplate(
1237                                                         $item->company->id, $item->createdBy->id,
1238                                                         $item->company->id, $item->createdBy->id,
1239                                                         $item->stock->company->contact->email, null,
1240                                                         'Auction Successful - ' . $item->id,
1241                                                         'auction-sale-seller',
1242                                                         array(
1243                                                                 'first_name'             => $item->stock->company->contact->firstName,
1244                                                                 'family_name'            => $item->stock->company->contact->familyName,
1245                                                                 'vehicle'                => $vehicle,
1246                                                                 'price'                  => $currPrefix . $item->currentBid->amount,
1247                                                                 'buyer'                  => $item->currentBid->company->name,
1248                                                                 'buyer_name'             => $item->currentBid->profile->firstName . ' '
1249                                                                                             . $item->currentBid->profile->familyName,
1250                                                                 'buyer_address'          => $item->currentBid->company->city->region->name
1251                                                                                             . ', ' . $item->currentBid->company->city->name
1252                                                                                             . ', ' . $item->currentBid->company->street
1253                                                                                             . ', ' . $item->currentBid->company->postalCode,
1254                                                                 'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1255                                                                         ? $item->currentBid->company->vatNumber
1256                                                                         : '',
1257                                                                 'buyer_email'            => $item->currentBid->profile->email,
1258                                                                 'buyer_mobile'           => $item->currentBid->profile->mobile,
1259                                                                 'seller'                 => $item->company->name,
1260                                                                 'seller_name'            => $item->createdBy->firstName . ' '
1261                                                                                             . $item->createdBy->familyName,
1262                                                                 'vehicle_address'        => $item->company->city->region->name
1263                                                                                             . ', ' . $item->company->city->name
1264                                                                                             . ', ' . $item->company->street
1265                                                                                             . ', ' . $item->company->postalCode,
1266                                                                 'seller_billing_address' => $item->company->billingCity
1267                                                                         ? $item->company->billingCity->region->name
1268                                                                           . ', ' . $item->company->billingCity->name
1269                                                                           . ', ' . $item->company->billingStreet
1270                                                                           . ', ' . $item->company->billingPostalCode
1271                                                                         : '',
1272                                                                 'seller_vat_number'      => !is_null($item->company->vatNumber)
1273                                                                         ? $item->company->vatNumber
1274                                                                         : '',
1275                                                                 'seller_email'           => $item->createdBy->email,
1276                                                                 'stock_number'           => $item->stock->stockNumber
1277                                                         ),
1278                                                         $attachments,
1279                                                         array(),
1280                                                         false,
1281                                                         true
1282                                                 );
1283                                         }
1284
1285                                         #-> Seller.
1286                                         $oNotify->sendFromTemplate(
1287                                                 $item->company->id, $item->createdBy->id,
1288                                                 $item->company->id, $item->createdBy->id,
1289                                                 $item->createdBy->email, null,
1290                                                 'Auction Successful - ' . $item->id,
1291                                                 'auction-sale-seller',
1292                                                 array(
1293                                                         'first_name'             => $item->createdBy->firstName,
1294                                                         'family_name'            => $item->createdBy->familyName,
1295                                                         'vehicle'                => $vehicle,
1296                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1297                                                         'buyer'                  => $item->currentBid->company->name,
1298                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1299                                                                                     . $item->currentBid->profile->familyName,
1300                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1301                                                                                     . ', ' . $item->currentBid->company->city->name
1302                                                                                     . ', ' . $item->currentBid->company->street
1303                                                                                     . ', ' . $item->currentBid->company->postalCode,
1304                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1305                                                                 ? $item->currentBid->company->vatNumber
1306                                                                 : '',
1307                                                         'buyer_email'            => $item->currentBid->profile->email,
1308                                                         'buyer_mobile'           => $item->currentBid->profile->mobile,
1309                                                         'seller'                 => $item->company->name,
1310                                                         'seller_name'            => $item->createdBy->firstName . ' '
1311                                                                                     . $item->createdBy->familyName,
1312                                                         'vehicle_address'        => $item->company->city->region->name
1313                                                                                     . ', ' . $item->company->city->name
1314                                                                                     . ', ' . $item->company->street
1315                                                                                     . ', ' . $item->company->postalCode,
1316                                                         'seller_billing_address' => $item->company->billingCity
1317                                                                 ? $item->company->billingCity->region->name
1318                                                                   . ', ' . $item->company->billingCity->name
1319                                                                   . ', ' . $item->company->billingStreet
1320                                                                   . ', ' . $item->company->billingPostalCode
1321                                                                 : '',
1322                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1323                                                                 ? $item->company->vatNumber
1324                                                                 : '',
1325                                                         'seller_email'           => $item->createdBy->email,
1326                                                         'stock_number'           => $item->stock->stockNumber
1327                                                 ),
1328                                                 $attachments,
1329                                                 array(),
1330                                                 false,
1331                                                 true
1332                                         );
1333                                         $attachments = array();
1334                                 }
1335                                 else
1336                                 {
1337                                         #-> No winner, send notification to seller & admin & feeder dealer.
1338                                         $oNotify->sendFromTemplate(
1339                                                 $item->company->id, $item->createdBy->id,
1340                                                 $item->company->id, $item->createdBy->id,
1341                                                 $item->createdBy->email, null,
1342                                                 'No winner on Bid4Cars Auction - ' . $item->id,
1343                                                 'auction-no-sale',
1344                                                 array(
1345                                                         'first_name'         => $item->createdBy->firstName,
1346                                                         'family_name'        => $item->createdBy->familyName,
1347                                                         'vehicle'            => $vehicle,
1348                                                         'reservePrice'       => $item->reservePrice,
1349                                                         'registrationNumber' => $item->stock->registrationNumber,
1350                                                         'stockNumber'        => $item->stock->stockNumber
1351                                                 ),
1352                                                 array(),
1353                                                 array(),
1354                                                 false,
1355                                                 true
1356                                         );
1357
1358                                         $oNotify->sendFromTemplate(
1359                                                 $item->company->id, $item->createdBy->id,
1360                                                 $item->company->id, $item->createdBy->id,
1361                                                 'info@bid4cars.com.na', null,
1362                                                 'No winner on Bid4Cars Auction - ' . $item->id,
1363                                                 'auction-no-sale',
1364                                                 array(
1365                                                         'first_name'         => $item->createdBy->firstName,
1366                                                         'family_name'        => $item->createdBy->familyName,
1367                                                         'vehicle'            => $vehicle,
1368                                                         'reservePrice'       => $item->reservePrice,
1369                                                         'registrationNumber' => $item->stock->registrationNumber,
1370                                                         'stockNumber'        => $item->stock->stockNumber
1371                                                 ),
1372                                                 array(),
1373                                                 array(),
1374                                                 false,
1375                                                 true
1376                                         );
1377
1378                                         #No Winner Feeder
1379                                         if ($item->stock->company->id != $item->company->id && '' != $item->stock->company->contact->email)
1380                                         {
1381                                                 $oNotify->sendFromTemplate(
1382                                                         $item->company->id, $item->createdBy->id,
1383                                                         $item->company->id, $item->createdBy->id,
1384                                                         $item->stock->company->contact->email, null,
1385                                                         'No winner on Bid4Cars Auction - ' . $item->id,
1386                                                         'auction-no-sale',
1387                                                         array(
1388                                                                 'first_name'         => $item->stock->company->contact->firstName,
1389                                                                 'family_name'        => $item->stock->company->contact->familyName,
1390                                                                 'vehicle'            => $vehicle,
1391                                                                 'reservePrice'       => $item->reservePrice,
1392                                                                 'registrationNumber' => $item->stock->registrationNumber,
1393                                                                 'stockNumber'        => $item->stock->stockNumber
1394                                                         ),
1395                                                         array(),
1396                                                         array(),
1397                                                         false,
1398                                                         true
1399                                                 );
1400
1401                                         }
1402
1403                                 }
1404                                 $item->emailSent = true;
1405                                 $this->em->flush($item);
1406                         }
1407                         catch (\Exception $e)
1408                         {
1409                                 \Utility\Debug::errorLog('Error on Auction cron process for item ' . $item->id, $e->getMessage());
1410                         }
1411                 }
1412                 error_log('Auction cron ended @ ' . date('Y-m-d H:i:s'));
1413                 error_log('------------------------------------');
1414         }
1415
1416         /**
1417          * CRON functionality: Move Auction items as needed.
1418          */
1419         public function cronProcessCleanup()
1420         {
1421                 $mailTo = 'dino@nirph.com';
1422                 ini_set('memory_limit', '512M');
1423                 error_log('------------------------------------');
1424                 error_log('Auction cleanup cron started @ ' . date('Y-m-d H:i:s'));
1425
1426
1427                 #-> Opening mail.
1428                 $numRecsRes   = $this->em->createQuery(
1429                         'SELECT COUNT(auction.id) AS total FROM Auction\\Entity\\Auction auction '
1430                         . 'WHERE auction.endDate <= \'' . date('Y-m-d H:i:s') . '\''
1431                         . ' AND auction.endDate > \'' . date('Y-m-d') . ' 00:00:00\''
1432             . ' AND auction.jobState != :jobStateArchived'
1433                 )
1434             ->setParameter('jobStateArchived', 'Archived')
1435                         ->getSingleResult();
1436                 $totalItems   = (int)$numRecsRes['total'];
1437                 $numRecsRes   = $this->em->createQuery(
1438                         'SELECT COUNT(auction.id) AS total FROM Auction\\Entity\\Auction auction '
1439                         . 'WHERE auction.jobState = :jobState '
1440                         . ' AND auction.endDate <= :startDate'
1441                         . ' AND auction.endDate > :endDate'
1442                 )
1443                         ->setParameter('jobState', 'Sold')
1444                         ->setParameter('startDate', date('Y-m-d H:i:s'))
1445                         ->setParameter('endDate', date('Y-m-d') . ' 00:00:00')
1446                         ->getSingleResult();
1447                 $successItems = (int)$numRecsRes['total'];
1448 //              $result       = $this->em->createQuery(
1449 //                      'SELECT auction FROM \Auction\Entity\Auction auction '
1450 //                      . 'WHERE auction.jobState != :jobState AND auction.jobState != :jobStateTwo'
1451 //                      . ' AND auction.emailSent = :emailSent'
1452 //              )
1453 //                      ->setParameter('jobState', 'Active')
1454 //                      ->setParameter('jobStateTwo', 'Relist')
1455 //                      ->setParameter('emailSent', false)
1456 //                      ->getResult();
1457
1458         $result       = $this->em->createQuery(
1459             'SELECT auction FROM \Auction\Entity\Auction auction '
1460             . 'WHERE auction.jobState != :jobState'
1461             . ' AND auction.jobState != :jobStateArchived'
1462             . ' AND auction.emailSent = :emailSent'
1463         )
1464             ->setParameter('jobState', 'Active')
1465             ->setParameter('jobStateArchived', 'Archived')
1466             ->setParameter('emailSent', false)
1467             ->getResult();
1468
1469                 $mailer       = new \Utility\Comms\Email();
1470                 $mailer->send(array(
1471                                       'From'    => \Utility\Registry::getConfigParam('sourceEmailAddress'),
1472                                       'To'      => $mailTo,
1473                                       'Subject' => 'Auction cleanup cron Start - ' . date('Y-m-d H:i:s'),
1474                                       'Html'    => '<b>Total auction items:<//b> ' . $totalItems . '<br//>'
1475                                                    . '<b>Total items sold:<//b> ' . $successItems . '<br//>'
1476                                                    . '<b>Total items no winner:<//b> ' . ($totalItems - $successItems) . '<br//>'
1477                                                    . '<b>Mails not yet sent:<//b> ' . count($result) . '<br//>'
1478                               ));
1479                 $sent = 0;
1480
1481                 #-> Phase 1.
1482                 error_log("Item count: " . count($result));
1483                 foreach ($result as $item)
1484                 {
1485                         try
1486                         {
1487                                 #-> General data prep.
1488                                 $vehicle    = $item->stock->type->model->make->name
1489                                               . ', ' . $item->stock->type->model->name
1490                                               . ', ' . $item->stock->type->name;
1491                                 $currPrefix = \Utility\Definitions\Locale::getCurrencyPrefix() . ' ';
1492                                 $oNotify    = new \Utility\Comms\Notification();
1493
1494                                 if (!is_null($item->currentBid) && $item->currentBid->amount > $item->reservePrice && 'Sold' == $item->jobState)
1495                                 {
1496                                         #-> Update record to reflect winner.
1497                                         if (is_null($item->soldToCompany))
1498                                         {
1499                                                 $item->soldToCompany = $item->currentBid->company;
1500                                         }
1501                                         if (is_null($item->soldToProfile))
1502                                         {
1503                                                 $item->soldToProfile = $item->currentBid->profile;
1504                                         }
1505
1506                                         #-> Prep work.
1507                                         #-> Auction PDF.
1508                                         $pdf = new \Auction\Pdf\CompleteNew();
1509                                         $pdf->process(array(
1510                                                               'jobRecord' => $item
1511                                                       ), array());
1512                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
1513
1514                                         #-> Declaration PDF.
1515                                         $pdf_Declaration = new \Auction\Pdf\Declaration();
1516                                         $pdf_Declaration->process(array(
1517                                                                           'jobRecord' => $item
1518                                                                   ), array());
1519                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
1520
1521                                         #-> Deliveryreceipt PDF.
1522                                         $pdf_Deliveryreceipt = new \Auction\Pdf\Deliveryreceipt();
1523                                         $pdf_Deliveryreceipt->process(array(
1524                                                                               'jobRecord' => $item
1525                                                                       ), array());
1526                                         $docsDir = __DIR__ . '/../../../../../public/documents/';
1527
1528                                         $writer                 = new \Utility\Export\PdfTemplate($pdf);
1529                                         $writer_Declaration     = new \Utility\Export\PdfTemplate($pdf_Declaration);
1530                                         $writer_Deliveryreceipt = new \Utility\Export\PdfTemplate($pdf_Deliveryreceipt);
1531
1532                                         $attachments = array(
1533                                                 'auction' . $item->id . '.pdf'         => $writer->output(''),
1534                                                 'declaration' . $item->id . '.pdf'     => $writer_Declaration->output(''),
1535                                                 'deliveryreceipt' . $item->id . '.pdf' => $writer_Deliveryreceipt->output('')
1536                                         );
1537
1538
1539                                         #-> Admin.
1540                                         $oNotify->sendFromTemplate(
1541                                                 $item->company->id, $item->createdBy->id,
1542                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
1543                                                 'info@bid4cars.com.na', null,
1544                                                 'Auction Successful - ' . $item->id,
1545                                                 'auction-sale-buyer',
1546                                                 array(
1547                                                         'first_name'             => 'Admin',
1548                                                         'family_name'            => '',
1549                                                         'vehicle'                => $vehicle,
1550                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1551                                                         'seller'                 => $item->company->name,
1552                                                         'seller_name'            => $item->createdBy->firstName . ' '
1553                                                                                     . $item->createdBy->familyName,
1554                                                         'vehicle_address'        => $item->company->city->region->name
1555                                                                                     . ', ' . $item->company->city->name
1556                                                                                     . ', ' . $item->company->street
1557                                                                                     . ', ' . $item->company->postalCode,
1558                                                         'seller_billing_address' => $item->company->billingCity
1559                                                                 ? $item->company->billingCity->region->name
1560                                                                   . ', ' . $item->company->billingCity->name
1561                                                                   . ', ' . $item->company->billingStreet
1562                                                                   . ', ' . $item->company->billingPostalCode
1563                                                                 : '',
1564                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1565                                                                 ? $item->company->vatNumber
1566                                                                 : '',
1567                                                         'seller_email'           => $item->createdBy->email,
1568                                                         'stock_number'           => $item->stock->stockNumber,
1569                                                         'buyer'                  => $item->currentBid->company->name,
1570                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1571                                                                                     . $item->currentBid->profile->familyName,
1572                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1573                                                                                     . ', ' . $item->currentBid->company->city->name
1574                                                                                     . ', ' . $item->currentBid->company->street
1575                                                                                     . ', ' . $item->currentBid->company->postalCode,
1576                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1577                                                                 ? $item->currentBid->company->vatNumber
1578                                                                 : '',
1579                                                         'buyer_email'            => $item->currentBid->profile->email,
1580                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
1581                                                 ),
1582                                                 $attachments,
1583                                                 array(),
1584                                                 false,
1585                                                 true
1586                                         );
1587
1588                                         #-> Admin.
1589                                         $oNotify->sendFromTemplate(
1590                                                 $item->company->id, $item->createdBy->id,
1591                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
1592                                                 'louise@combinefreight.co.za', null,
1593                                                 'Auction Successful - ' . $item->id,
1594                                                 'auction-sale-buyer',
1595                                                 array(
1596                                                         'first_name'             => 'Admin',
1597                                                         'family_name'            => '',
1598                                                         'vehicle'                => $vehicle,
1599                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1600                                                         'seller'                 => $item->company->name,
1601                                                         'seller_name'            => $item->createdBy->firstName . ' '
1602                                                                                     . $item->createdBy->familyName,
1603                                                         'vehicle_address'        => $item->company->city->region->name
1604                                                                                     . ', ' . $item->company->city->name
1605                                                                                     . ', ' . $item->company->street
1606                                                                                     . ', ' . $item->company->postalCode,
1607                                                         'seller_billing_address' => $item->company->billingCity
1608                                                                 ? $item->company->billingCity->region->name
1609                                                                   . ', ' . $item->company->billingCity->name
1610                                                                   . ', ' . $item->company->billingStreet
1611                                                                   . ', ' . $item->company->billingPostalCode
1612                                                                 : '',
1613                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1614                                                                 ? $item->company->vatNumber
1615                                                                 : '',
1616                                                         'seller_email'           => $item->createdBy->email,
1617                                                         'stock_number'           => $item->stock->stockNumber,
1618                                                         'buyer'                  => $item->currentBid->company->name,
1619                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1620                                                                                     . $item->currentBid->profile->familyName,
1621                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1622                                                                                     . ', ' . $item->currentBid->company->city->name
1623                                                                                     . ', ' . $item->currentBid->company->street
1624                                                                                     . ', ' . $item->currentBid->company->postalCode,
1625                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1626                                                                 ? $item->currentBid->company->vatNumber
1627                                                                 : '',
1628                                                         'buyer_email'            => $item->currentBid->profile->email,
1629                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
1630                                                 ),
1631                                                 $attachments,
1632                                                 array(),
1633                                                 false,
1634                                                 true
1635                                         );
1636
1637                                         #-> Buyer.
1638                                         $oNotify->sendFromTemplate(
1639                                                 $item->company->id, $item->createdBy->id,
1640                                                 $item->currentBid->company->id, $item->currentBid->profile->id,
1641                                                 $item->currentBid->profile->email, null,
1642                                                 'Bid Successful - ' . $item->id,
1643                                                 'auction-sale-buyer',
1644                                                 array(
1645                                                         'first_name'             => $item->currentBid->profile->firstName,
1646                                                         'family_name'            => $item->currentBid->profile->familyName,
1647                                                         'vehicle'                => $vehicle,
1648                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1649                                                         'seller'                 => $item->company->name,
1650                                                         'seller_name'            => $item->createdBy->firstName . ' '
1651                                                                                     . $item->createdBy->familyName,
1652                                                         'vehicle_address'        => $item->company->city->region->name
1653                                                                                     . ', ' . $item->company->city->name
1654                                                                                     . ', ' . $item->company->street
1655                                                                                     . ', ' . $item->company->postalCode,
1656                                                         'seller_billing_address' => $item->company->billingCity
1657                                                                 ? $item->company->billingCity->region->name
1658                                                                   . ', ' . $item->company->billingCity->name
1659                                                                   . ', ' . $item->company->billingStreet
1660                                                                   . ', ' . $item->company->billingPostalCode
1661                                                                 : '',
1662                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1663                                                                 ? $item->company->vatNumber
1664                                                                 : '',
1665                                                         'seller_email'           => $item->createdBy->email,
1666                                                         'stock_number'           => $item->stock->stockNumber,
1667                                                         'buyer'                  => $item->currentBid->company->name,
1668                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1669                                                                                     . $item->currentBid->profile->familyName,
1670                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1671                                                                                     . ', ' . $item->currentBid->company->city->name
1672                                                                                     . ', ' . $item->currentBid->company->street
1673                                                                                     . ', ' . $item->currentBid->company->postalCode,
1674                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1675                                                                 ? $item->currentBid->company->vatNumber
1676                                                                 : '',
1677                                                         'buyer_email'            => $item->currentBid->profile->email,
1678                                                         'buyer_mobile'           => $item->currentBid->profile->mobile
1679                                                 ),
1680                                                 $attachments,
1681                                                 array(),
1682                                                 false,
1683                                                 true
1684                                         );
1685
1686                                         #-> Feeder.
1687                                         if ($item->stock->company->id != $item->company->id && '' != $item->stock->company->contact->email)
1688                                         {
1689                                                 //send `auction-sale-seller` email to $item->stock->company->contact->email
1690                                                 $oNotify->sendFromTemplate(
1691                                                         $item->company->id, $item->createdBy->id,
1692                                                         $item->company->id, $item->createdBy->id,
1693                                                         $item->stock->company->contact->email, null,
1694                                                         'Auction Successful - ' . $item->id,
1695                                                         'auction-sale-seller',
1696                                                         array(
1697                                                                 'first_name'             => $item->stock->company->contact->firstName,
1698                                                                 'family_name'            => $item->stock->company->contact->familyName,
1699                                                                 'vehicle'                => $vehicle,
1700                                                                 'price'                  => $currPrefix . $item->currentBid->amount,
1701                                                                 'buyer'                  => $item->currentBid->company->name,
1702                                                                 'buyer_name'             => $item->currentBid->profile->firstName . ' '
1703                                                                                             . $item->currentBid->profile->familyName,
1704                                                                 'buyer_address'          => $item->currentBid->company->city->region->name
1705                                                                                             . ', ' . $item->currentBid->company->city->name
1706                                                                                             . ', ' . $item->currentBid->company->street
1707                                                                                             . ', ' . $item->currentBid->company->postalCode,
1708                                                                 'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1709                                                                         ? $item->currentBid->company->vatNumber
1710                                                                         : '',
1711                                                                 'buyer_email'            => $item->currentBid->profile->email,
1712                                                                 'buyer_mobile'           => $item->currentBid->profile->mobile,
1713                                                                 'seller'                 => $item->company->name,
1714                                                                 'seller_name'            => $item->createdBy->firstName . ' '
1715                                                                                             . $item->createdBy->familyName,
1716                                                                 'vehicle_address'        => $item->company->city->region->name
1717                                                                                             . ', ' . $item->company->city->name
1718                                                                                             . ', ' . $item->company->street
1719                                                                                             . ', ' . $item->company->postalCode,
1720                                                                 'seller_billing_address' => $item->company->billingCity
1721                                                                         ? $item->company->billingCity->region->name
1722                                                                           . ', ' . $item->company->billingCity->name
1723                                                                           . ', ' . $item->company->billingStreet
1724                                                                           . ', ' . $item->company->billingPostalCode
1725                                                                         : '',
1726                                                                 'seller_vat_number'      => !is_null($item->company->vatNumber)
1727                                                                         ? $item->company->vatNumber
1728                                                                         : '',
1729                                                                 'seller_email'           => $item->createdBy->email,
1730                                                                 'stock_number'           => $item->stock->stockNumber
1731                                                         ),
1732                                                         $attachments,
1733                                                         array(),
1734                                                         false,
1735                                                         true
1736                                                 );
1737
1738                                         }
1739
1740                                         #-> Seller.
1741                                         $oNotify->sendFromTemplate(
1742                                                 $item->company->id, $item->createdBy->id,
1743                                                 $item->company->id, $item->createdBy->id,
1744                                                 $item->createdBy->email, null,
1745                                                 'Auction Successful - ' . $item->id,
1746                                                 'auction-sale-seller',
1747                                                 array(
1748                                                         'first_name'             => $item->createdBy->firstName,
1749                                                         'family_name'            => $item->createdBy->familyName,
1750                                                         'vehicle'                => $vehicle,
1751                                                         'price'                  => $currPrefix . $item->currentBid->amount,
1752                                                         'buyer'                  => $item->currentBid->company->name,
1753                                                         'buyer_name'             => $item->currentBid->profile->firstName . ' '
1754                                                                                     . $item->currentBid->profile->familyName,
1755                                                         'buyer_address'          => $item->currentBid->company->city->region->name
1756                                                                                     . ', ' . $item->currentBid->company->city->name
1757                                                                                     . ', ' . $item->currentBid->company->street
1758                                                                                     . ', ' . $item->currentBid->company->postalCode,
1759                                                         'buyer_vat_number'       => !is_null($item->currentBid->company->vatNumber)
1760                                                                 ? $item->currentBid->company->vatNumber
1761                                                                 : '',
1762                                                         'buyer_email'            => $item->currentBid->profile->email,
1763                                                         'buyer_mobile'           => $item->currentBid->profile->mobile,
1764                                                         'seller'                 => $item->company->name,
1765                                                         'seller_name'            => $item->createdBy->firstName . ' '
1766                                                                                     . $item->createdBy->familyName,
1767                                                         'vehicle_address'        => $item->company->city->region->name
1768                                                                                     . ', ' . $item->company->city->name
1769                                                                                     . ', ' . $item->company->street
1770                                                                                     . ', ' . $item->company->postalCode,
1771                                                         'seller_billing_address' => $item->company->billingCity
1772                                                                 ? $item->company->billingCity->region->name
1773                                                                   . ', ' . $item->company->billingCity->name
1774                                                                   . ', ' . $item->company->billingStreet
1775                                                                   . ', ' . $item->company->billingPostalCode
1776                                                                 : '',
1777                                                         'seller_vat_number'      => !is_null($item->company->vatNumber)
1778                                                                 ? $item->company->vatNumber
1779                                                                 : '',
1780                                                         'seller_email'           => $item->createdBy->email,
1781                                                         'stock_number'           => $item->stock->stockNumber
1782                                                 ),
1783                                                 $attachments,
1784                                                 array(),
1785                                                 false,
1786                                                 true
1787                                         );
1788                                         $attachments = array();
1789                                 }
1790                                 else
1791                                 {
1792                                         #-> No winner, send notification to seller & admin & feeder.
1793                                         $oNotify->sendFromTemplate(
1794                                                 $item->company->id, $item->createdBy->id,
1795                                                 $item->company->id, $item->createdBy->id,
1796                                                 $item->createdBy->email, null,
1797                                                 'No winner on Bid4Cars Auction - ' . $item->id,
1798                                                 'auction-no-sale',
1799                                                 array(
1800                                                         'first_name'         => $item->createdBy->firstName,
1801                                                         'family_name'        => $item->createdBy->familyName,
1802                                                         'vehicle'            => $vehicle,
1803                                                         'reservePrice'       => $item->reservePrice,
1804                                                         'registrationNumber' => $item->stock->registrationNumber,
1805                                                         'stockNumber'        => $item->stock->stockNumber
1806                                                 ),
1807                                                 array(),
1808                                                 array(),
1809                                                 false,
1810                                                 true
1811                                         );
1812                                         $oNotify->sendFromTemplate(
1813                                                 $item->company->id, $item->createdBy->id,
1814                                                 $item->company->id, $item->createdBy->id,
1815                                                 'info@bid4cars.com.na', null,
1816                                                 'No winner on Bid4Cars Auction - ' . $item->id,
1817                                                 'auction-no-sale',
1818                                                 array(
1819                                                         'first_name'         => $item->createdBy->firstName,
1820                                                         'family_name'        => $item->createdBy->familyName,
1821                                                         'vehicle'            => $vehicle,
1822                                                         'reservePrice'       => $item->reservePrice,
1823                                                         'registrationNumber' => $item->stock->registrationNumber,
1824                                                         'stockNumber'        => $item->stock->stockNumber
1825                                                 ),
1826                                                 array(),
1827                                                 array(),
1828                                                 false,
1829                                                 true
1830                                         );
1831                                         #No Winner Feeder
1832                                         if ($item->stock->company->id != $item->company->id && '' != $item->stock->company->contact->email)
1833                                         {
1834                                                 $oNotify->sendFromTemplate(
1835                                                         $item->company->id, $item->createdBy->id,
1836                                                         $item->company->id, $item->createdBy->id,
1837                                                         $item->stock->company->contact->email, null,
1838                                                         'No winner on Bid4Cars Auction - ' . $item->id,
1839                                                         'auction-no-sale',
1840                                                         array(
1841                                                                 'first_name'         => $item->stock->company->contact->firstName,
1842                                                                 'family_name'        => $item->stock->company->contact->familyName,
1843                                                                 'vehicle'            => $vehicle,
1844                                                                 'reservePrice'       => $item->reservePrice,
1845                                                                 'registrationNumber' => $item->stock->registrationNumber,
1846                                                                 'stockNumber'        => $item->stock->stockNumber
1847                                                         ),
1848                                                         array(),
1849                                                         array(),
1850                                                         false,
1851                                                         true
1852                                                 );
1853
1854                                         }
1855
1856                                 }
1857                                 $item->emailSent = true;
1858                                 $this->em->flush($item);
1859                                 $sent++;
1860                         }
1861                         catch (\Exception $e)
1862                         {
1863                                 \Utility\Debug::errorLog('Error on Auction cron process for item ' . $item->id, $e->getMessage());
1864                         }
1865                 }
1866                 error_log('Auction cleanup cron ended @ ' . date('Y-m-d H:i:s'));
1867                 error_log('------------------------------------');
1868
1869
1870         $mailer = new \Utility\Comms\Email();
1871         $mailer->send(array(
1872             'From'    => \Utility\Registry::getConfigParam('sourceEmailAddress'),
1873             'To'      => $mailTo,
1874             'Subject' => 'Auction cleanup cron End - ' . date('Y-m-d H:i:s'),
1875             'Html'    => '<b>Total auction items:<//b> ' . $totalItems . '<br//>'
1876                 . '<b>Emails sent:<//b> ' . $sent . ' of ' . count($result) . '<br//>'
1877         ));
1878     }
1879
1880
1881 }