text changes to registration mail content
[namibia] / module / Report / src / Report / Report / ValuationStatistical.php
1 <?php
2 namespace Report\Report;
3
4
5
6 /**
7  * Valuation Statistics Report.
8  * @author andre.fourie
9  */
10 class ValuationStatistical extends \Utility\Service\Report
11 {
12
13
14
15     /**
16      * @var string
17      */
18     protected $_title  = 'Valuation Statistical Report';
19     /**
20      * @var string
21      */
22     protected $_subject = 'Valuation';
23     /**
24      * @var string
25      */
26     protected $_description = 'Statistical report for vehicles valuations.';
27     /**
28      * @var array
29      */
30     protected $_notes = array(
31         'Confidential information, generated using Bid 4 Cars, for more information visit bid4cars.com.na'
32     );
33     /**
34      * @var array
35      */
36     protected $_headers = array(
37         'Dealer',
38         'Total Valuations',
39         'No of Valuations sent to sales',
40         '% To Trade',
41         //'Total Trade Value',
42         //'Total Recon',
43         //'Total Managers Offer',
44         'Deals Done',
45         '% Deals Done',
46         'Deals Not Done',
47         '%Deals Not Done',
48         'Send to Trade Centre',
49         '%Send to Trade Centre'
50
51     );
52     /**
53      * @var array
54      */
55     protected $_fields = array(
56         'company_name',
57         'total_valuations',
58         'num_valuations',
59         'percentage_toTrade',
60         //'total_trade',
61         //'total_recon',
62         //'total_amountOffered',
63         'total_dealsDone',
64         'percentage_dealsDone',
65         'total_dealsNotDone',
66         'percentage_dealsNotDone',
67         'total_sendToTradeCenter',
68         'percentage_sendToTradeCenter',
69     );
70     /**
71      * @var array
72      */
73     protected $_totals = array(
74         /* 'total_valuations',
75         'num_valuations',
76         'total_trade',
77         'total_recon',
78         'total_managerOffer',
79         'total_dealsDone' */
80     );
81     /**
82      * @var array
83      */
84     protected $_currencyFields = array(
85         'total_trade',
86         'total_recon',
87         'total_amountOffered'
88     );
89
90
91
92     /**
93      * Build the dataset.
94      */
95     public function build()
96     {
97         ini_set('memory_limit','512M');
98         $totalRow = array(
99             'company_name'                                      => '',
100             'total_valuations'                          => '',
101             'num_valuations'                            => '',
102             'percentage_toTrade'                        => '% Deals Done:',
103             'total_trade'                                       => '',
104             //'total_recon'                             => '',
105             //'total_amountOffered'             => '',
106             'total_dealsDone'                           => '',
107             'percentage_dealsDone'                      => '',
108             'total_dealsNotDone'                        => '',
109             'percentage_dealsNotDone'           => '',
110             'total_sendToTradeCenter'           => '',
111             'percentage_sendToTradeCentere' => '',
112             'total_tradeP'                                      => 0.0,
113             'total_amountOfferedP'                      => 0.0
114         );
115         $baseRow = array(
116             'company_name'                              => '',
117             'total_valuations'                  => '',
118             'num_valuations'                    => 0,
119             'percentage_toTrade'                => 0,
120             'total_trade'                               => 0,
121             //'total_recon'                     => 0,
122             //'total_amountOffered'     => 0,
123             'total_dealsDone'                   => 0,
124             'percentage_dealsDone'              => 0.0,
125             'total_dealsNotDone'                => 0,
126             'percentage_dealsNotDone'   => 0.0,
127             'total_sendToTradeCenter'   => 0,
128             'percentage_sendToTradeCenter'      => 0.0,
129             'total_tradeP'                              => 0.0,
130             'total_amountOfferedP'              => 0.0
131         );
132         $titles =  array(
133             'company_name'                              => 'Dealer',
134             'total_valuations'                  => 'Total Valuations',
135             'num_valuations'                    => 'No of Valuations Send to Sales',
136             'percentage_toTrade'                => '% To Trade',
137             'total_trade'                               => 'Total Trade Value',
138             //'total_recon'                     => 'Total Recon',
139             //'total_amountOffered'     => 'Total Managers Offer',
140             'total_dealsDone'                   => 'Deals Done',
141             'percentage_dealsDone'              => '% Deals Done',
142             'total_dealsNotDone'                => 'Deals Not Done',
143             'percentage_dealsNotDone'   => '% Deals Not Done',
144             'total_sendToTradeCenter'           => 'Send to Trade Centre',
145             'percentage_sendToTradeCenter'      => '% Send to Trade Centre',
146             'id'                                                => 'h',
147             'excel.rowStyle'                    => 'Titles'
148         );
149
150         #-> Establish list of dealerships to work with.
151         $selection = array(
152             'company', 'companyGroup', 'groupDivision'
153         );
154         $query = 'SELECT [SELECTION] '
155             . 'FROM \Company\Entity\Company company'
156             . ' LEFT JOIN company.group companyGroup '
157             . ' LEFT JOIN company.groupDivision groupDivision '
158             . '[WHERE] '
159             . 'ORDER BY companyGroup.name ASC, groupDivision.name ASC, company.name ASC, company.id ASC';
160         if (isset($this->_input['dateFrom']) && !empty($this->_input['dateFrom']))
161         {
162             $this->_queries['Date Range From'] = $this->_input['dateFrom'];
163         }
164         if (isset($this->_input['dateTo']) && !empty($this->_input['dateTo']))
165         {
166             $this->_queries['Date Range Until'] = $this->_input['dateTo'];
167         }
168         if (isset($this->_input['group'])
169             && !empty($this->_input['group'])
170             && 'null' != $this->_input['group'])
171         {
172             $this->_queries['Group'] = $this->em
173                 ->find('\Company\Entity\Group', $this->_input['group'])
174                 ->name;
175             $where[] = 'companyGroup.id = :group';
176             $params['group'] = $this->_input['group'];
177         }
178         if (isset($this->_input['groupDivision'])
179             && !empty($this->_input['groupDivision'])
180             && 'null' != $this->_input['groupDivision'])
181         {
182             error_log($this->_input['groupDivision']);
183             $this->_queries['Division'] = $this->em
184                 ->find('\Company\Entity\GroupDivision', $this->_input['groupDivision'])
185                 ->name;
186             $where[] = 'groupDivision.id = :division';
187             $params['division'] = $this->_input['groupDivision'];
188         }
189         if (isset($this->_input['company'])
190             && !empty($this->_input['company'])
191             && 'null' != $this->_input['company'])
192         {
193             $this->_queries['Dealership'] = $this->em
194                 ->find('\Company\Entity\Company', $this->_input['company'])
195                 ->name;
196             $where[] = 'company.id = :company';
197             $params['company'] = $this->_input['company'];
198         }
199         #-> Finalize query.
200         $query = str_replace(array(
201             '[SELECTION]', '[WHERE]'
202         ), array(
203             implode(', ', $selection) . ' ',
204             !empty($where)
205                 ? 'WHERE ' . implode(' AND ', $where) . ' '
206                 : ' '
207         ), $query);
208         #-> Collect data.
209         $query = $this->em->createQuery($query);
210         !empty($params)
211         && $query->setParameters($params);
212
213         $companies = $query->getScalarResult();
214         $this->em->clear();
215         $grp = false;
216         $div = false;
217         foreach ($companies as $company)
218         {
219             if ('' == $company['companyGroup_name'])
220             {
221                 $company['companyGroup_name'] = 'None';
222             }
223             if ('' == $company['groupDivision_name'])
224             {
225                 $company['groupDivision_name'] = 'None';
226             }
227             if ($div != $company['groupDivision_name'])
228             {
229                 if (false != $div)
230                 {
231                     $totals = $baseRow;
232                     $totals['company_name'] = 'DIVISION TOTALS';
233                     $totals['id'] = 'dt';
234                     $totals['excel.rowStyle'] = 'Totals';
235                     $this->_data[] = $totals;
236                     $totals = $totalRow;
237                     $totals['company_name'] = $grp . ': ' . $div;
238                     $totals['id'] = 'dn';
239                     $totals['excel.rowStyle'] = 'Totals';
240                     $this->_data[] = $totals;
241                     $this->_data[] = array();
242                     if ($grp == $company['companyGroup_name'])
243                     {
244                         if (false != $grp)
245                         {
246                             $this->_data[] = $titles;
247                         }
248                     }
249                 }
250                 $div = $company['groupDivision_name'];
251             }
252             if ($grp != $company['companyGroup_name'])
253             {
254                 if (false != $grp)
255                 {
256                     #-> Not the first group, so we have grand totals to add
257                     $totals = $baseRow;
258                     $totals['company_name'] = 'GROUP TOTALS';
259                     $totals['id'] = 'gt';
260                     $totals['excel.rowStyle'] = 'Totals';
261                     $this->_data[] = $totals;
262                     $totals = $totalRow;
263                     $totals['company_name'] = $grp;
264                     $totals['id'] = 'gn';
265                     $totals['excel.rowStyle'] = 'Totals';
266                     $this->_data[] = $totals;
267                     $this->_data[] = array();
268                     $this->_data[] = array();
269                     $this->_data[] = $titles;
270                 }
271                 $grp = $company['companyGroup_name'];
272             }
273             $entry = $baseRow;
274             $entry['id'] = $company['company_id'];
275             $entry['company_name'] = $company['company_name'];
276             $this->_data[] = $entry;
277         }
278         $totals = $baseRow;
279         $totals['company_name'] = 'DIVISION TOTALS';
280         $totals['id'] = 'dt';
281         $totals['excel.rowStyle'] = 'Totals';
282         $this->_data[] = $totals;
283         $totals = $totalRow;
284         $totals['company_name'] = $grp . ': ' . $div;
285         $totals['id'] = 'dn';
286         $totals['excel.rowStyle'] = 'Totals';
287         $this->_data[] = $totals;
288         $this->_data[] = array();
289         $totals = $baseRow;
290         $totals['company_name'] = 'GROUP TOTALS';
291         $totals['id'] = 'gt';
292         $totals['excel.rowStyle'] = 'Totals';
293         $this->_data[] = $totals;
294         $totals = $totalRow;
295         $totals['company_name'] = $grp;
296         $totals['id'] = 'gn';
297         $totals['excel.rowStyle'] = 'Totals';
298         $this->_data[] = $totals;
299
300
301         #-> Collect stats.
302         $selection = array(
303             'companyGroup.name', 'groupDivision.name', 'company.name', 'company.id',
304             'COUNT(valuation.id) AS total_valuations',
305             'SUM(CASE valuation.amountOffered WHEN 0.0 THEN 0 ELSE 1 END) AS num_valuations',
306             'SUM(stock.tradePrice) AS total_trade',
307             'SUM(stock.damageTotal) AS total_recon',
308             'SUM(valuation.amountOffered) AS total_amountOffered',
309             'SUM(CASE WHEN stock.tradePrice > 0.0 AND valuation.amountOffered > 0.0 THEN stock.tradePrice ELSE 0.0 END) AS total_tradeP',
310             'SUM(CASE WHEN stock.tradePrice > 0.0 AND valuation.amountOffered > 0.0 THEN valuation.amountOffered ELSE 0.0 END) AS total_amountOfferedP'
311         );
312         $join      = array();
313         $leftJion  = array();
314         $where     = array();
315         $params    = array();
316         $query = 'SELECT [SELECTION] '
317             . 'FROM \Valuation\Entity\Valuation valuation '
318             . ' JOIN valuation.stock stock '
319             . ' JOIN stock.company company '
320             . ' LEFT JOIN company.group companyGroup '
321             . ' LEFT JOIN company.groupDivision groupDivision '
322             . '[WHERE] '
323             . 'GROUP BY companyGroup.name, groupDivision.name, company.name, company.id '
324             . 'ORDER BY companyGroup.name ASC, groupDivision.name ASC, company.name ASC, company.id ASC';
325
326         #-> Construct details.
327 //        $where[] = 'stock.tradePrice > 0.0';
328         $authData = \Utility\Registry::getAuthData();
329         if (isset($this->_input['dateFrom']) && !empty($this->_input['dateFrom']))
330         {
331             $where[] = 'valuation.created >= :dateFrom';
332             $params['dateFrom'] = new \DateTime($this->_input['dateFrom']);
333         }
334         if (isset($this->_input['dateTo']) && !empty($this->_input['dateTo']))
335         {
336             $where[] = 'valuation.created <= :dateTo';
337             $params['dateTo'] = new \DateTime($this->_input['dateTo'] . ' 23:59:59');
338         }
339         if (isset($this->_input['group'])
340             && !empty($this->_input['group'])
341             && 'null' != $this->_input['group'])
342         {
343             $where[] = 'companyGroup.id = :group';
344             $params['group'] = $this->_input['group'];
345         }
346         if (isset($this->_input['groupDivision'])
347             && !empty($this->_input['groupDivision'])
348             && 'null' != $this->_input['groupDivision'])
349         {
350             $where[] = 'groupDivision.id = :division';
351             $params['division'] = $this->_input['groupDivision'];
352         }
353         if (isset($this->_input['company'])
354             && !empty($this->_input['company'])
355             && 'null' != $this->_input['company'])
356         {
357             $where[] = 'company.id = :company';
358             $params['company'] = $this->_input['company'];
359         }
360
361         #-> Finalize query.
362         $query = str_replace(array(
363             '[SELECTION]', '[WHERE]'
364         ), array(
365             implode(', ', $selection) . ' ',
366             !empty($where)
367                 ? 'WHERE ' . implode(' AND ', $where) . ' '
368                 : ' '
369         ), $query);
370
371         #-> Collect data.
372         $query = $this->em->createQuery($query);
373         !empty($params)
374         && $query->setParameters($params);
375         $stats = $query->getScalarResult();
376         $this->em->clear();
377         $grp = '';
378         $div = '';
379         $divTotals = $baseRow;
380         $grpTotal = $baseRow;
381
382         $stat = array_shift($stats);
383         foreach ($this->_data as $i => $entry)
384         {
385             if (empty($entry))
386             {
387                 continue;
388             }
389             switch ($entry['id'])
390             {
391                 case 'h':
392                     break;
393                 case 'dt': // Division Totals
394                     $this->_data[$i]['total_valuations']                        = $divTotals['total_valuations'];
395                     $this->_data[$i]['num_valuations']                          = $divTotals['num_valuations'];
396                     $this->_data[$i]['total_dealsDone']                         = 0;
397                     $this->_data[$i]['total_dealsNotDone']                      = 0;
398                     $this->_data[$i]['total_sendToTradeCenter']         = 0;
399                     $this->_data[$i]['percentage_toTrade']                      = '';
400                     $this->_data[$i]['percentage_dealsDone']            = '';
401                     $this->_data[$i]['percentage_dealsNotDone']         = '';
402                     $this->_data[$i]['percentage_sendToTradeCenter'] = '';
403
404
405                     break;
406                 case 'dn':// Division Ave
407                     $this->_data[$i]['percentage_toTrade'] = $divTotals['total_tradeP'] > 0
408                         ? round((100 / $divTotals['total_tradeP']) * $divTotals['total_amountOfferedP'], 1)
409                         : 0;
410                     $this->_data[$i]['percentage_dealsDone']            = 0;
411                     $this->_data[$i]['percentage_dealsNotDone']         = 0;
412                     $this->_data[$i]['percentage_sendToTradeCenter'] = 0;
413
414                     $divTotals = $baseRow;
415                     break;
416                 case 'gt'://Group Totals
417                     $this->_data[$i]['total_valuations']                        = $grpTotal['total_valuations'];
418                     $this->_data[$i]['num_valuations']                          = $grpTotal['num_valuations'];
419                     $this->_data[$i]['total_dealsDone']                         = 0;
420                     $this->_data[$i]['total_dealsNotDone']                      = 0;
421                     $this->_data[$i]['total_sendToTradeCenter']         = 0;
422                     $this->_data[$i]['percentage_toTrade']                      = '';
423                     $this->_data[$i]['percentage_dealsDone']            = '';
424                     $this->_data[$i]['percentage_dealsNotDone']         = '';
425                     $this->_data[$i]['percentage_sendToTradeCenter'] = '';
426
427                     break;
428                 case 'gn':// Group Ave
429                     $this->_data[$i]['percentage_toTrade'] = $grpTotal['total_tradeP'] > 0
430                         ? round((100 / $grpTotal['total_tradeP']) * $grpTotal['total_amountOfferedP'], 1)
431                         : 0;
432                     $this->_data[$i]['percentage_dealsDone']            = 0;
433                     $this->_data[$i]['percentage_dealsNotDone']         = 0;
434                     $this->_data[$i]['percentage_sendToTradeCenter'] = 0;
435
436                     $grpTotal = $baseRow;
437                     break;
438                 default:
439                     if ($stat['id'] == $entry['id'])
440                     {
441                         #-> Update the entry.
442                         //rows
443                         $this->_data[$i]['total_valuations'] = $stat['total_valuations'];
444                         $this->_data[$i]['num_valuations'] = $stat['num_valuations'];
445                         $this->_data[$i]['total_trade'] = $stat['total_trade'];
446                         $this->_data[$i]['total_trade'] = $stat['total_trade'];
447                         $this->_data[$i]['total_recon'] = $stat['total_recon'];
448                         $this->_data[$i]['total_amountOffered'] = $stat['total_amountOffered'];
449                         $this->_data[$i]['percentage_toTrade'] = $stat['total_tradeP'] > 0
450                         && $stat['total_amountOfferedP'] > 0
451                             ? round((100 / $stat['total_tradeP']) * $stat['total_amountOfferedP'], 1)
452                             : 0;
453
454                         $this->_data[$i]['total_dealsDone'] = 0;
455                         $this->_data[$i]['percentage_dealsDone'] = 0;
456                         $this->_data[$i]['total_dealsNotDone'] = 0;
457                         $this->_data[$i]['percentage_dealsNotDone'] = 0;
458                         $this->_data[$i]['total_sendToTradeCenter'] = 0;
459                         $this->_data[$i]['percentage_sendToTradeCenter'] = 0;
460
461                         #-> Update the totals.
462                         $divTotals['total_tradeP'] += $stat['total_tradeP'];
463                         $divTotals['total_amountOfferedP'] += $stat['total_amountOfferedP'];
464                         $divTotals['total_valuations'] += $stat['total_valuations'];
465                         $divTotals['num_valuations'] += $stat['num_valuations'];
466                         $divTotals['total_trade'] += $stat['total_trade'];
467                         $divTotals['total_dealsDone'] += 0;
468                         $divTotals['total_dealsNotDone'] += 0;
469                         $divTotals['total_sendToTradeCenter'] += 0;
470
471                         $grpTotal['total_amountOfferedP'] += $stat['total_amountOfferedP'];
472                         $grpTotal['total_tradeP'] += $stat['total_tradeP'];
473                         $grpTotal['num_valuations'] += $stat['num_valuations'];
474                         $grpTotal['total_trade'] += $stat['total_trade'];
475                         $grpTotal['total_dealsDone'] += 0;
476                         $grpTotal['total_dealsNotDone'] += 0;
477                         $grpTotal['total_sendToTradeCenter'] += 0;
478
479                         #-> Company handled, move on to next one.
480                         $stat = array_shift($stats);
481                     }
482                     break;
483             }
484         }
485
486         #-> Collect more stats.
487         $selection = array(
488             'companyGroup.name', 'groupDivision.name', 'company.name', 'company.id', 'valuation.jobState as job_state','stock.tradePrice as trade_price',
489             'SUM(stat.dealdone) AS total_dealsDone',
490             'SUM(stat.dealnotdone) AS total_dealsNotDone',
491             'SUM(stat.tradecentre) AS total_sendToTradeCenter'
492         );
493         $join      = array();
494         $leftJion  = array();
495         $where     = array();
496         $params    = array();
497         $query = 'SELECT [SELECTION] '
498             . 'FROM \Statistical\Entity\Statistical stat '
499             . ' JOIN stat.stock stock '
500             . ' LEFT JOIN stock.valuation valuation '
501             . ' LEFT JOIN stock.company company '
502             . ' LEFT JOIN company.group companyGroup '
503             . ' LEFT JOIN company.groupDivision groupDivision '
504             . '[WHERE] '
505             . 'GROUP BY companyGroup.name, groupDivision.name, company.name, company.id '
506             . 'ORDER BY companyGroup.name ASC, groupDivision.name ASC, company.name ASC, company.id ASC';
507
508         #-> Construct details.
509 //        $where[] = 'stock.tradePrice > 0.0';
510         $authData = \Utility\Registry::getAuthData();
511         if (isset($this->_input['dateFrom']) && !empty($this->_input['dateFrom']))
512         {
513             $where[] = 'stat.created >= :dateFrom';
514             $params['dateFrom'] = new \DateTime($this->_input['dateFrom']);
515         }
516         if (isset($this->_input['dateTo']) && !empty($this->_input['dateTo']))
517         {
518             $where[] = 'stat.created <= :dateTo';
519             $params['dateTo'] = new \DateTime($this->_input['dateTo'] . ' 23:59:59');
520         }
521         if (isset($this->_input['group'])
522             && !empty($this->_input['group'])
523             && 'null' != $this->_input['group'])
524         {
525             $where[] = 'companyGroup.id = :group';
526             $params['group'] = $this->_input['group'];
527         }
528         if (isset($this->_input['groupDivision'])
529             && !empty($this->_input['groupDivision'])
530             && 'null' != $this->_input['groupDivision'])
531         {
532             $where[] = 'groupDivision.id = :division';
533             $params['division'] = $this->_input['groupDivision'];
534         }
535         if (isset($this->_input['company'])
536             && !empty($this->_input['company'])
537             && 'null' != $this->_input['company'])
538         {
539             $where[] = 'company.id = :company';
540             $params['company'] = $this->_input['company'];
541         }
542
543         #-> Finalize query.
544         $query = str_replace(array(
545             '[SELECTION]', '[WHERE]'
546         ), array(
547             implode(', ', $selection) . ' ',
548             !empty($where)
549                 ? 'WHERE ' . implode(' AND ', $where) . ' '
550                 : ' '
551         ), $query);
552
553         #-> Collect data.
554         $query = $this->em->createQuery($query);
555         !empty($params)
556         && $query->setParameters($params);
557
558 //        \Utility\Debug::errorLog('$query->getSQL()', $query->getSQL());
559 //        \Utility\Debug::errorLog('$query->getParameters()', $query->getParameters());
560
561         $stats = $query->getScalarResult();
562         $this->em->clear();
563         $grp = '';
564         $div = '';
565         $divTotals = $baseRow;
566         $grpTotal = $baseRow;
567         $stat = $stats;
568         $countStat = 0;
569         foreach ($this->_data as $i => $entry)
570         {
571             if (empty($entry))
572             {
573                 continue;
574             }
575             switch ($entry['id'])
576             {
577                 case 'h':
578                     break;
579                 case 'dt': // Division Totals
580                     $this->_data[$i]['total_dealsDone']                         = $divTotals['total_dealsDone'];
581                     $this->_data[$i]['total_dealsNotDone']                      = $divTotals['total_dealsNotDone'];
582                     $this->_data[$i]['total_sendToTradeCenter']         = $divTotals['total_sendToTradeCenter'];
583                     $this->_data[$i]['percentage_dealsDone']            = '';
584                     $this->_data[$i]['percentage_dealsNotDone']         = '';
585                     $this->_data[$i]['percentage_sendToTradeCenter'] = '';
586
587
588                     break;
589                 case 'dn': // Division Ave
590                     $this->_data[$i]['percentage_dealsDone'] = $divTotals['total_dealsDone'] > 0
591                     && $grpTotal['num_valuations'] > 0
592                         ? round(($divTotals['total_dealsDone'] / $divTotals['num_valuations']) * 100, 2)
593                         : 0;
594                     $this->_data[$i]['percentage_dealsNotDone'] = $divTotals['total_dealsNotDone'] > 0
595                     && $grpTotal['num_valuations'] > 0
596                         ? round(($divTotals['total_dealsNotDone'] / $divTotals['num_valuations']) * 100, 2)
597                         : 0;
598                     $this->_data[$i]['percentage_sendToTradeCenter'] = $divTotals['total_sendToTradeCenter'] > 0
599                     && $grpTotal['num_valuations'] > 0
600                         ? round(($divTotals['total_sendToTradeCenter'] / $divTotals['num_valuations']) * 100, 2)
601                         : 0;
602                     $divTotals = $baseRow;
603                     break;
604                 case 'gt': //Group Totals
605                     $this->_data[$i]['total_dealsDone']                         = $grpTotal['total_dealsDone'];
606                     $this->_data[$i]['total_dealsNotDone']                      = $grpTotal['total_dealsNotDone'];
607                     $this->_data[$i]['total_sendToTradeCenter']         = $grpTotal['total_sendToTradeCenter'];
608                     $this->_data[$i]['percentage_dealsDone']            = '';
609                     $this->_data[$i]['percentage_dealsNotDone']         = '';
610                     $this->_data[$i]['percentage_sendToTradeCenter'] = '';
611
612                     break;
613                 case 'gn': // Group Ave
614                     $this->_data[$i]['percentage_dealsDone'] = $grpTotal['total_dealsDone'] > 0
615                     && $grpTotal['num_valuations'] > 0
616                         ? round(($grpTotal['total_dealsDone'] / $grpTotal['num_valuations']) * 100, 2)
617                         : 0;
618                     $this->_data[$i]['percentage_dealsNotDone'] = $grpTotal['total_dealsNotDone'] > 0
619                     && $grpTotal['num_valuations'] > 0
620                         ? round(($grpTotal['total_dealsNotDone'] / $grpTotal['num_valuations']) * 100, 2)
621                         : 0;
622                     $this->_data[$i]['percentage_sendToTradeCenter'] = $grpTotal['total_sendToTradeCenter'] > 0
623                     && $grpTotal['num_valuations'] > 0
624                         ? round(($grpTotal['total_sendToTradeCenter'] / $grpTotal['num_valuations']) * 100, 2)
625                         : 0;
626                     $grpTotal = $baseRow;
627                     break;
628                 default:
629
630                     $statArrayId = $this->searcharray($entry['id'], 'id', $stat);
631
632                     if ($stat[$statArrayId]['id'] == $entry['id'])
633                     {
634                         if('Complete Valuation' != $stat[$statArrayId]['job_state'] && 0 < $stat[$statArrayId]['trade_price']) {
635
636                             #-> Update the entry.
637                             $this->_data[$i]['total_dealsDone'] = $stat[$statArrayId]['total_dealsDone'];
638                             $this->_data[$i]['percentage_dealsDone'] = $stat[$statArrayId]['total_dealsDone'] > 0
639                             && $this->_data[$i]['num_valuations'] > 0
640                                 ? round(($stat[$statArrayId]['total_dealsDone'] / $this->_data[$i]['num_valuations']) * 100, 1)
641                                 : 0;
642                             $this->_data[$i]['total_dealsNotDone'] = $stat[$statArrayId]['total_dealsNotDone'];
643                             $this->_data[$i]['percentage_dealsNotDone'] = $stat[$statArrayId]['total_dealsNotDone'] > 0
644                             && $this->_data[$i]['num_valuations'] > 0
645                                 ? round(($stat[$statArrayId]['total_dealsNotDone'] / $this->_data[$i]['num_valuations']) * 100, 1)
646                                 : 0;
647                             $this->_data[$i]['total_sendToTradeCenter'] = $stat[$statArrayId]['total_sendToTradeCenter'];
648                             $this->_data[$i]['percentage_sendToTradeCenter'] = $stat[$statArrayId]['total_sendToTradeCenter'] > 0
649                             && $this->_data[$i]['num_valuations'] > 0
650                                 ? round(($stat[$statArrayId]['total_sendToTradeCenter'] / $this->_data[$i]['num_valuations']) * 100, 1)
651                                 : 0;
652
653                             #-> Update the totals.
654                             $divTotals['total_dealsDone'] += $stat[$statArrayId]['total_dealsDone'];
655                             $divTotals['total_dealsNotDone'] += $stat[$statArrayId]['total_dealsNotDone'];
656                             $divTotals['total_sendToTradeCenter'] += $stat[$statArrayId]['total_sendToTradeCenter'];
657
658                             $grpTotal['total_dealsDone'] += $stat[$statArrayId]['total_dealsDone'];
659                             $grpTotal['total_dealsNotDone'] += $stat[$statArrayId]['total_dealsNotDone'];
660                             $grpTotal['total_sendToTradeCenter'] += $stat[$statArrayId]['total_sendToTradeCenter'];
661
662                             #-> Company handled, move on to next one.
663 //                            $stat = array_shift($stats);
664                         }
665                     }
666
667                     #-> Update the totals.
668                     $divTotals['num_valuations'] += $this->_data[$i]['num_valuations'];
669                     $grpTotal['num_valuations'] += $this->_data[$i]['num_valuations'];
670                     break;
671             }
672 //            $countStat++;
673         }
674
675
676     }
677
678
679     public function searcharray($value, $key, $array) {
680         foreach ($array as $k => $val) {
681             if ($val[$key] == $value) {
682                 return $k;
683             }
684         }
685         return null;
686     }
687
688 }