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