Advertisement
tockata

Pesho-the-Pharmacist

Dec 10th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1.     <?php
  2.         date_default_timezone_set('Europe/Sofia');
  3.        
  4.         $today = new DateTime($_GET['today']);
  5.         $invoices = [];
  6.         foreach ($_GET['invoices'] as $key => $row) {
  7.             $invoices[] = preg_split('/\s*\|\s*/', $row, -1, PREG_SPLIT_NO_EMPTY);
  8.             $invoices[$key][0] = DateTime::createFromFormat('d/m/Y', $invoices[$key][0]);
  9.             preg_match('/\d+\.*\d*/', $invoices[$key][3], $invoices[$key][3]);
  10.             $invoices[$key][3] = $invoices[$key][3][0];
  11.         }
  12.        
  13.         //clean unnecessary dates:
  14.         foreach ($invoices as $key => $value) {
  15.             $diffBetweenDates = $today->diff($value[0]);
  16.             if ($diffBetweenDates->days >= 1826) {
  17.                 unset($invoices[$key]);
  18.             }
  19.         }
  20.        
  21.         // Obtain a list of keys for sorting:
  22.         foreach ($invoices as $key => $row) {
  23.             $sortDate[$key] = $row[0];
  24.             $sortCompany[$key] = $row[1];
  25.         }
  26.         // Sort the data:
  27.         array_multisort($sortDate, SORT_ASC, $sortCompany, SORT_ASC, $invoices );
  28.        
  29.         $invoiceResultArr = fillResultArray($invoices);
  30.        
  31.         //print result:
  32.         echo '<ul>';
  33.         foreach ($invoiceResultArr as $date => $value) {
  34.             echo '<li><p>' . $date . '</p>';
  35.             echo '<ul>';
  36.             foreach ($invoiceResultArr[$date] as $company => $order) {
  37.                 echo '<li><p>' . $company . '</p>';
  38.                 echo '<ul>';
  39.                 echo '<li><p>' . implode(',', $order['products']) . '-' . $order['total'] . 'lv</p></li>';
  40.                 echo '</ul></li>';
  41.             }
  42.             echo '</ul>';
  43.             echo '</li>';
  44.         }
  45.         echo '</ul>';
  46.        
  47.         function fillResultArray($array) {
  48.             $resultArray = [];
  49.             foreach ($array as $key => $value) {
  50.                 $date = $value[0]->format('d/m/Y');
  51.                 if (!key_exists($date, $resultArray)) {
  52.                     $resultArray[$date] = [];
  53.                 }
  54.                 if (!key_exists($value[1], $resultArray[$date])) {
  55.                     $resultArray[$date][$value[1]] = array('products' => [], 'total' => 0);
  56.                 }
  57.                 $resultArray[$date][$value[1]]['products'][] = $value[2];
  58.                 $resultArray[$date][$value[1]]['total'] += $value[3];
  59.             }
  60.             return $resultArray;
  61.         }
  62.         ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement