Advertisement
Valleri

PharmacistaPesho

Sep 1st, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.07 KB | None | 0 0
  1. <?php
  2. date_default_timezone_set("Europe/Sofia");
  3.  
  4. $today = "07/08/2014";
  5. $invoices = array("11/05/2013 | Sopharma | Paracetamol | 20.54 lv",
  6.     "11/05/2013 | Sopharma | Analgin | 57.45 lv",
  7.     "02/12/2011 | Actavis | Aulin | 120.54 lv",
  8.     "13/05/2009 | Sopharma | Tamiflu | 221.54 ",
  9.     "23/01/2014 | Actavis | Paracetamol | 7.54 ",
  10.     "11/05/2013 | Actavis | Paracetamol | 17.54 ");
  11.  
  12. /*$today = $_GET['today'];
  13. $invoices = $_GET['invoices'];*/
  14.  
  15. $invoices = array_filter($invoices, function($val) {
  16.     $tempInspectTime = $GLOBALS['today'];
  17.     $inspectorVisit = new DateTime(str_replace('/', '-', $tempInspectTime));
  18.     $regex = "/(\s*\d{2}\/\d{2}\/\d{4})\s*/";
  19.     preg_match($regex, $val, $getDate);
  20.     $invoiceDate = new DateTime(str_replace('/', '-', $getDate[1]));
  21.  
  22.     $interval = $inspectorVisit->diff($invoiceDate);
  23.     if($interval->days > 1825) {
  24.         return false;
  25.     }
  26.     $helpCompareInspectorDate = date("Y-m-d", strtotime($GLOBALS['today']));
  27.     $helpCompareInvoiceDate = date("Y-m-d", strtotime($getDate[1]));
  28.  
  29.     if($helpCompareInspectorDate < $helpCompareInvoiceDate) {
  30.         return false;
  31.     }
  32.     return true;
  33. });
  34.  
  35.  
  36. function byDate($a, $b) {
  37.     $regex = "/(\s*\d{2}\/\d{2}\/\d{4})\s*/";
  38.     preg_match($regex, $a, $first);
  39.     preg_match($regex, $b, $second);
  40.     $temp1 = date("d-m-Y", strtotime(str_replace('/', '-',$first[1])));
  41.     $temp2 = date("d-m-Y", strtotime(str_replace('/', '-',$second[1])));
  42.     $date1 = date("Y-d-m", strtotime($temp1));
  43.     $date2 = date("Y-d-m", strtotime($temp2));
  44.     return strcmp($date1, $date2);
  45. }
  46.  
  47. usort($invoices, 'byDate');
  48.  
  49. $map = array();
  50.  
  51. for($i = 0; $i < count($invoices) ;$i++) {
  52.     $reg = "/^(\s*\d{2}\/\d{2}\/\d{4})\s*\|\s*(.*?)\s*\|\s*(.*?)\s*\|\s*([\d\.]+)\s*([\w\.]+)?$/";
  53.     preg_match($reg, $invoices[$i], $info);
  54.  
  55.     $date = $info[1];
  56.     $company = $info[2];
  57.     $drug = $info[3];
  58.     $cost = $info[4];
  59.     $currency = "";
  60.     if(!empty($info[5])) {
  61.         $currency = $info[5];
  62.     } else {
  63.         $currency = "";
  64.     }
  65.  
  66.  
  67.     if(!array_key_exists($date, $map)) {
  68.         $map[$date] = array();
  69.     }
  70.     if(!array_key_exists($company, $map[$date])) {
  71.         $map[$date][$company] = array();
  72.         $map[$date][$company]["drugs"] = array();
  73.         $map[$date][$company]["total"] = 0;
  74.         $map[$date][$company]["curr"] = "";
  75.     }
  76.     $map[$date][$company]["drugs"][] = $drug;
  77.     $map[$date][$company]["total"] += $cost;
  78.     $map[$date][$company]["curr"] = $currency;
  79. }
  80.  
  81. foreach ($map as $key => &$value) {
  82.     ksort($map[$key]);
  83.     foreach ($value as &$company) {
  84.         asort($company["drugs"]);
  85.     }
  86.  
  87. }
  88.  
  89.  
  90. $table = "<ul>";
  91.  
  92. foreach ($map as $key => &$value) {
  93.     $table .= "<li><p>" . htmlspecialchars($key) . "</p><ul>";
  94.     foreach ($value as $key1 => $value1) {
  95.         $table .= "<li><p>" . htmlspecialchars($key1) . "</p><ul>";
  96.         $table .= "<li><p>" . implode(',', $value1["drugs"]) . "-" . $value1["total"] . $value1["curr"] .
  97.         "</p></li>";
  98.     }
  99. $table .= "</ul></li></ul>";
  100. }
  101. $table .= "</ul>";
  102. echo $table;
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement