Advertisement
Guest User

Untitled

a guest
Jun 7th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 107.55 KB | None | 0 0
  1. <?php
  2.  //Error handler for handling exceptions.
  3. error_reporting(0);
  4. function __construct()
  5.     {
  6.    
  7.     }
  8.     $stocktake_id = $Navigation->getParam('stocktake_id');
  9.     $filename = 'Reports'.$stocktake_id.'\\' ;
  10. function download_file_to_client($file, $mime = 'application/zip')
  11. {  
  12.     $url = "".$file;
  13.  
  14.    // header('Content-Type: '.$mime.';charset=utf-8');
  15.        
  16.      header($http[$num]);
  17.     header ('Location: '.$url.'');
  18.  flush();
  19.    
  20.  
  21.    
  22. }
  23. if (file_exists($filename)) {
  24.  
  25. } else {
  26.    mkdir($filename, 0777, true);
  27. }
  28. $x = 0;
  29. if ($x==0){
  30. //Department Report
  31.     class Report1{
  32.      function departmentReportContent($stocktake_id, $sections){
  33.        
  34.         //get all the departments for the given stocktake
  35.         $dep_qry = "SELECT TRIM(UPPER(IFNULL(department_code,''))) AS department_code, TRIM(UPPER(department_description)) AS department_description, SUM(total_cost) AS total_cost, SUM(total_retail) AS total_retail, SUM(qty) AS qty FROM (
  36.                    SELECT
  37.                         IFNULL(sp.department_code,'') AS department_code, sp.department_description AS department_description, SUM(sp.unit_cost_price * ss.quantity) AS total_cost, SUM(sp.unit_retail_price * ss.quantity) AS total_retail, sum(ss.quantity) AS qty
  38.                    FROM stocktake_scans ss
  39.                    INNER JOIN stocktake_products sp ON ss.stocktake_product_id = sp.stocktake_product_id ";
  40.        
  41.         if ($sections)
  42.         {
  43.             $dep_qry.= " INNER JOIN stocktake_areas sa ON ss.stocktake_area_id = sa.stocktake_area_id ";
  44.         }
  45.        
  46.         $dep_qry .= " WHERE ss.stocktake_id = '{$stocktake_id}' ";
  47.        
  48.         if ($sections)
  49.         {
  50.             $dep_qry.= " AND sa.section IN ({$sections}) ";
  51.         }
  52.        
  53.         $dep_qry .= " GROUP BY TRIM(UPPER(IFNULL(sp.department_code,''))), TRIM(UPPER(sp.department_description))
  54.        
  55.                    UNION ALL
  56.        
  57.                    SELECT '' AS department_code, TRIM(UPPER(department_description)) AS department_description, SUM(unit_cost_price * quantity) AS total_cost, SUM(unit_retail_price * quantity) AS total_retail, sum(quantity) AS qty
  58.                    FROM stocktake_items
  59.                    WHERE stocktake_id = '{$stocktake_id}' ";
  60.        
  61.         if ($sections)
  62.         {
  63.             $dep_qry.= " AND section IN ({$sections}) ";
  64.         }
  65.        
  66.         $dep_qry .= " GROUP BY TRIM(UPPER(department_description))) a
  67.                GROUP BY TRIM(UPPER(IFNULL(department_code,0))), TRIM(UPPER(department_description))";
  68.        
  69.         //echo $dep_qry;
  70.        
  71.         $dep_res = db::c()->query($dep_qry);
  72.        
  73.         $cur_symbol = Stocktake::getCurrencySymbol($stocktake_id);
  74.        
  75.         //if (strcmp($cur_res, 'EUR') == 0) setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
  76.        
  77.         //$rep_table = self::reportHeader("Department Stock Take Report");
  78.         //$rep_table .= self::stocktakeDetails($stocktake_id);
  79.        
  80.         $rep_table .= "
  81.                     <table id='department_report' cellspacing='0' style='margin-top:10px;'>
  82.                         <tr>
  83.                             <td colspan='2' class='top'><div class='nosplit'></div></td>
  84.                             <td colspan='2' class='top'><div class='nosplit'>Stock Value At</div></td>
  85.                         </tr>
  86.                         <tr>
  87.                             <td class='top' style='width:55%;'><div class='nosplit'>Departments</div></td>
  88.                             <td class='top' style='width:15%;'><div class='nosplit'>Total Count</div></td>
  89.                             <td class='top' style='width:15%;'><div class='nosplit'>Cost</div></td>
  90.                             <td class='top' style='width:15%;'><div class='nosplit'>Retail</div></td>
  91.                         </tr>";
  92.         $sum_total_cost = 0;
  93.         $sum_total_retail = 0;
  94.         $sum_total_qty = 0;
  95.        
  96.         while ($row = $dep_res->fetch(PDO::FETCH_ASSOC))
  97.         {        
  98.             $total_cost = $row['total_cost'];//$row['unit_cost_price']*$row['qty'];
  99.             $total_retail = $row['total_retail'];//*$row['qty'];
  100.            
  101.             $sum_total_cost += $total_cost;
  102.             $sum_total_retail += $total_retail;
  103.             $sum_total_qty += $row['qty'];
  104.            
  105.             $tc = number_format($total_cost, 2);
  106.             $tr = number_format($total_retail, 2);
  107.            
  108.             $rep_table .= "
  109.                        <tr>
  110.                             <td><div class='nosplit'>". $row['department_description'];
  111.            
  112.             if($row['department_code']) $rep_table.= ' (' .$row['department_code']. ')';
  113.            
  114.             $rounded_qty = (floor($row['qty']) != $row['qty']) ? $row['qty'] : round($row['qty']);
  115.            
  116.             $rep_table .= "</div></td>
  117.                           <td style='text-align:right;'><div class='nosplit'>".$rounded_qty."</div></td>
  118.                           <td style='text-align:right;'><div class='nosplit'>".$cur_symbol.$tc."</div></td>
  119.                           <td style='text-align:right;'><div class='nosplit'>".$cur_symbol.$tr."</div></td>
  120.                        </tr>";                
  121.         }
  122.        
  123.        
  124.         $stc = number_format($sum_total_cost, 2);
  125.         $str = number_format($sum_total_retail, 2);
  126.        
  127.         $rep_table .= "<tr>
  128.                        <td class='bottom'><div class='nosplit'>Total:</div></td>
  129.                        <td class='bottom'><div class='nosplit'>".$sum_total_qty."</div></td>
  130.                        <td class='bottom'><div class='nosplit'>".$cur_symbol.$stc."</div></td>
  131.                        <td class='bottom'><div class='nosplit'>".$cur_symbol.$str."</div></td>
  132.                    </tr>
  133.                </table>";
  134.         $name = "Department Report";
  135.        
  136.         return $rep_table;
  137.        
  138.        
  139.        
  140.     }
  141.    
  142.       function reportHeader($title){
  143.        
  144.         $header = "<table class='report_header' cellspacing='0' cellpadding='0'>
  145.                        <tr >
  146.                            <td style='width:50%; text-align:left;'>
  147.                                <strong>Stocktaking.ie</strong><br/>
  148.                                        Lakeview Point<br/>
  149.                                        Claregalway Corporate Park<br/>
  150.                                        Claregalway, Galway
  151.                            </td>
  152.                            <td style='text-align:right;'>
  153.                                <img src='/images/stocktaking_s.gif' alt='Stocktaking' height='69' width='211'/>
  154.                            </td>
  155.                        </tr>
  156.                        <tr>
  157.                            <td colspan='2'><h2 style='color:black;font-size:20px;font-weight:bold;margin-top:10px;'>".$title."</h2></td>
  158.                        </tr>
  159.                    </table>";
  160.         return $header;
  161.     }
  162.       function stocktakeDetails($stocktake_id){
  163.        
  164.         $stocktake_qry = "
  165.                            SELECT
  166.                                s.stocktake_date, s.customer_name, s.store_name, s.store_address, s.store_post_code, s.store_town, s.store_county_name,
  167.                                c.country_name, s.date_created
  168.                            FROM stocktakes s LEFT JOIN country c
  169.                            ON c.country_code_alpha2 = s.customer_country_code
  170.                            WHERE stocktake_id = '{$stocktake_id}';";
  171.         $stocktake_res = db::c()->query($stocktake_qry);
  172.         $stocktake_row = $stocktake_res->fetch(PDO::FETCH_ASSOC);
  173.         $date_created = $stocktake_row['date_created'];
  174.         $stocktake_date = l::date($stocktake_row['stocktake_date']);
  175.  
  176.         $stocktake_details = "<table class='st_det_rep'>
  177.                                <tr>
  178.                                    <td style='width:40%; text-align:left;'>
  179.                                        <table style='font-size:12px;float:left;'>
  180.                                            <tr>
  181.                                                <td style='text-align:left;'>Customer:</td>
  182.                                                <td style='text-align:left;'><strong>".$stocktake_row['customer_name']."</strong></td>
  183.                                            </tr>        
  184.                                            <tr>
  185.                                                    <td style='width:120px; text-align:left;'>Stocktake Number:</td>
  186.                                                <td style='text-align:left;'>".$stocktake_id."</td>
  187.                                            </tr>
  188.                                            <tr>
  189.                                                <td style='text-align:left;'>Stocktake Date:</td>
  190.                                                <td style='text-align:left;'>".$stocktake_date."</td>
  191.                                            </tr>
  192.                                        </table>
  193.                                    </td>
  194.                                <td style='width:60%; text-align:left;'>
  195.                                        <table style='font-size:12px;float:right;'>
  196.                                            <tr>
  197.                                                <td style='width:120px;text-align:right;font-weight:bold;padding-right:20px;'>Store:</td>
  198.                                                <td style='text-align:right;font-weight:bold;'>".$stocktake_row['store_name']."</td>
  199.                                            </tr>
  200.                                            <tr>
  201.                                                <td style='text-align:left;'></td>
  202.                                                <td style='text-align:right;'>".$stocktake_row['store_address']."<br/>
  203.                                                ".$stocktake_row['store_town'].", ".$stocktake_row['store_post_code']."<br/>
  204.                                                ".$stocktake_row['store_county_name'].", ".$stocktake_row['country_name']."
  205.                                                </td>
  206.                                            </tr>
  207.                                        </table>
  208.                                    </td>
  209.                                </tr>
  210.                            </table>";
  211.        
  212.         return $stocktake_details;        
  213.        
  214.     }
  215.     }
  216. $report = $Navigation->getParam('report');
  217. $stocktake_id = $Navigation->getParam('stocktake_id');
  218. $section[] = $_POST['section'];
  219.  
  220. $part = $Navigation->getParam('part');
  221.  
  222. $html_begin = '<html><head>
  223.                 <link rel="stylesheet" type="text/css" href="http://sttest.aptvision.com/styles/main_style.css" />
  224.                 <script>
  225.                     function subst() {
  226.                       var vars={};
  227.                       var x=document.location.search.substring(1).split("&");
  228.                       for(var i in x) {var z=x[i].split("=",2);vars[z[0]] = unescape(z[1]);}
  229.                       var x=["topage","page"];
  230.                       for(var i in x) {
  231.                         var y = document.getElementsByClassName(x[i]);
  232.                         for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
  233.                       }
  234.                     }
  235.                 </script>
  236.                 </head><body onload="subst()">';
  237.  
  238. $html_end = '</body></html>';
  239.    
  240. if (!$part)
  241. {
  242.     //update stocktake
  243.    
  244.     $res = db::c()->query("SELECT stocktake_product_id FROM stocktake_scans WHERE stocktake_id = 304");
  245.  
  246.     while ($row = $res->fetch())
  247.     {
  248.         $qty = db::c()->query("SELECT SUM(quantity) FROM stocktake_scans WHERE stocktake_product_id = '{$row['stocktake_product_id']}';")->fetchColumn(0);
  249.         if (!$qty)
  250.             $qty = '0';
  251.         db::c()->query("UPDATE stocktake_products SET quantity_scanned = '{$qty}' WHERE stocktake_product_id = '{$row['stocktake_product_id']}';");
  252.     }
  253.    
  254.     $params = array();
  255.     $params['security_off'] = true;
  256.     $params['stocktake_id'] = $stocktake_id;
  257.     $params['report'] = $report;
  258.    
  259.     $params['section'] = $Page->getPOSTVar('section');
  260.     $params['sdep'] = $Page->getPOSTVar('sel_rep_department');
  261.     $params['start'] = $Page->getPOSTVar('start_code1');
  262.     $params['finish'] = $Page->getPOSTVar('finish_code1');
  263.  
  264.     if ($Page->getGETVar('a_ids'))
  265.     {
  266.         $params['a_ids'] = $Page->getGETVar('a_ids');
  267.     }
  268.      unset($content_url);
  269.     $params['part'] = 'content1';
  270.     $content_url = Navigation::gUrl('/users/admin/reportspack.php', $params);
  271.    
  272.     $params['part'] = 'header1';
  273.     $header_url = Navigation::gUrl('/users/admin/reportspack.php', $params);
  274.    
  275.     $params['part'] = 'footer1';
  276.     $footer_url = Navigation::gUrl('/users/admin/reportspack.php', $params);
  277.    
  278.     $wkpdf = new WKPDF($content_url);
  279.     $wkpdf->footerUrl = $footer_url;
  280.     $wkpdf->headerUrl = $header_url;
  281.  
  282.     $wkpdf->marginTop = '35';
  283.     $wkpdf->marginBottom = '20';
  284.     $wkpdf->output('S', 'Reports'.$stocktake_id .'\DepartmentSummaryReport_' . $stocktake_id .'.pdf');
  285.     //echo "<a href='{$content_url}' target='_blank'>Content</a><br>";
  286.     //echo "<a href='{$header_url}' target='_blank'>Header</a><br>";
  287.     //echo "<a href='{$footer_url}' target='_blank'>Footer</a><br>";   
  288.  
  289. }
  290.  
  291. else if ($part == 'content1'){
  292.  
  293.     $all_sections = $Page->getPOSTVar('all_sections');
  294.     if ($all_sections) {
  295.         $section = null;
  296.     }
  297.     else {
  298.         $secarr = $Navigation->getParam('section');
  299.         $section = '';
  300.         if ($secarr && is_array($secarr) && count($secarr) > 0)
  301.         {
  302.             foreach($secarr as $s)
  303.             {
  304.                 $section.= db::c()->quote($s) . ',';
  305.             }
  306.             $section = substr($section, 0, -1);
  307.         }
  308.         else
  309.         {
  310.             $section = null;
  311.         }
  312.    
  313.     $sdep = $Navigation->getParam('sdep');
  314.     $start = $Navigation->getParam('start');
  315.     $finish = $Navigation->getParam('finish');
  316.    
  317.     if (!$sdep || $sdep == '00all')
  318.     {
  319.         $sdep = null;
  320.     }
  321.     echo $html_begin;
  322.     echo Report1::stocktakeDetails($stocktake_id);
  323.     echo Report1::departmentReportContent($stocktake_id, $sections);
  324.     echo $html_end;
  325. }} 
  326.    
  327. else if ($part == 'header1')
  328. {
  329.     echo $html_begin;
  330.     echo Report1::reportHeader('Department Report');
  331.     echo $html_end;
  332. }
  333. else if ($part == 'footer1')
  334. {
  335. echo $html_begin;
  336. echo "<div class='page_numbers'> &ndash; Page <span class='page'></span> of <span class='topage'></span> &ndash; </div>";
  337. echo $html_end;
  338.  
  339. }
  340.  
  341. //Sub Department Report
  342.  class Report2{
  343.    function subDepartmentReportContent($stocktake_id, $sections)
  344.      {
  345.    
  346.          //get currency code for the stocktake
  347.          $cur_symbol = Stocktake::getCurrencySymbol($stocktake_id);    
  348.        
  349.          // $report = Report2::reportHeader("Sub-Department Stock Take Report");
  350.          // $report .= Report2::stocktakeDetails($stocktake_id);
  351.    
  352.          $report = "     <table id='sub_department_report' cellspacing='0'>
  353.                              <tr>
  354.                                  <td colspan='3' class='top'><div class='nosplit'></div></td>
  355.                                  <td colspan='2' class='top'><div class='nosplit'>Stock Value At</div></td>
  356.                              </tr>
  357.                              <tr>
  358.                                  <td class='top' style='width:30%;'><div class='nosplit'>Departments</div></td>
  359.                                  <td class='top' style='width:30%;'><div class='nosplit'>Sub Departments</div></td>
  360.                                  <td class='top' style='width:10%;'><div class='nosplit'>Total Count</div></td>
  361.                                  <td class='top' style='width:15%;'><div class='nosplit'>Cost</div></td>
  362.                                  <td class='top' style='width:15%;'><div class='nosplit'>Retail</div></td>
  363.                              </tr>";
  364.    
  365.          $dep_qry = "
  366.                             SELECT
  367.                                
  368.                                  (CASE WHEN TRIM(IFNULL(sp.department_description, '')) = '' THEN 'N/A' ELSE sp.department_description END) AS department_description,
  369.                                
  370.                                  (CASE WHEN TRIM(IFNULL(sp.sub_department_description, '')) = '' THEN 'N/A' ELSE sp.sub_department_description END) AS sub_department_description,
  371.                                  SUM(sp.unit_cost_price * ss.quantity) AS total_cost, SUM(sp.unit_retail_price * ss.quantity) AS total_retail, sum(ss.quantity) AS qty
  372.                             FROM stocktake_scans ss
  373.                             INNER JOIN stocktake_products sp ON ss.stocktake_product_id = sp.stocktake_product_id ";
  374.          if ($sections)
  375.          {
  376.              $dep_qry.= " INNER JOIN stocktake_areas sa ON ss.stocktake_area_id = sa.stocktake_area_id ";
  377.          }
  378.        
  379.          $dep_qry.= "         WHERE ss.stocktake_id = '{$stocktake_id}' ";
  380.        
  381.          if ($sections)
  382.          {
  383.              $dep_qry.= " AND sa.section IN ({$sections}) ";
  384.          }
  385.        
  386.          $dep_qry.= "         GROUP BY department_description, sub_department_description
  387.        
  388.                             UNION ALL
  389.        
  390.                             SELECT
  391.                                
  392.                                 (CASE WHEN TRIM(IFNULL(department_description, '')) = '' THEN 'N/A' ELSE department_description END) AS department_description,
  393.                                
  394.                                 'N/A',
  395.                                 SUM(unit_cost_price * quantity) AS total_cost, SUM(unit_retail_price * quantity) AS total_retail, sum(quantity) AS qty
  396.                             FROM stocktake_items
  397.                             WHERE stocktake_id = '{$stocktake_id}' ";
  398.        
  399.          if ($sections)
  400.          {
  401.              $dep_qry.= " AND section IN ({$sections}) ";
  402.          }
  403.        
  404.          $dep_qry.= "     GROUP BY department_description;";        
  405.          $dep_res = db::c()->query($dep_qry);
  406.        
  407.          $prev_dep_code = null;
  408.          $prev_dep_desc = null;
  409.        
  410.          while ($row = $dep_res->fetch(PDO::FETCH_ASSOC))
  411.          {
  412.            
  413.              if ($prev_dep_desc !== $row['department_description'])
  414.              {
  415.                  if ($prev_dep_desc !== null)
  416.                  {
  417.                      $stc = number_format($sum_total_cost, 2);
  418.                      $str = number_format($sum_total_retail, 2);
  419.                      $rounded_tq = (floor($sum_total_qty) != $sum_total_qty) ? $sum_total_qty : round($sum_total_qty);
  420.                        
  421.                      $report .= "<tr>
  422.                                     <td class='bottom' colspan='2' style='text-transform:uppercase'><div class='nosplit'>(".$dep_title.")</div></td>
  423.                                     <td class='bottom'><div class='nosplit'>".$rounded_tq."</div></td>
  424.                                     <td class='bottom'><div class='nosplit'>".$cur_symbol.$stc."</div></td>
  425.                                     <td class='bottom'><div class='nosplit'>".$cur_symbol.$str."</div></td>
  426.                                 </tr>";        
  427.                  }
  428.                
  429.                 // get all subdepartments for the given department
  430.                  $dep_des = $row['department_description'];
  431.                  $dep_code = $row['department_code'];
  432.                    
  433.                  $dep_title = $dep_des;
  434.                
  435.                  $report .= "<tr>
  436.                                 <td colspan='5' class='title'><div class='nosplit'> &nbsp;".$dep_title."</div></td>
  437.                             </tr>";
  438.                
  439.                  $sum_total_cost = 0;
  440.                  $sum_total_retail = 0;
  441.                  $sum_total_qty = 0;
  442.                
  443.              }
  444.  
  445.            
  446.              $total_cost = $row['total_cost'];$row['unit_cost_price']*$row['qty'];
  447.              $total_retail = $row['total_retail'];$row['unit_retail_price']*$row['qty'];
  448.                
  449.              $sum_total_cost += $total_cost;
  450.              $sum_total_retail += $total_retail;
  451.              $sum_total_qty += $row['qty'];
  452.                
  453.              $tc = number_format($total_cost, 2);
  454.              $tr = number_format($total_retail, 2);
  455.            
  456.              $rounded_qty = (floor($row['qty']) != $row['qty']) ? $row['qty'] : round($row['qty']);
  457.                
  458.              $report .= "
  459.                             <tr>
  460.                                 <td style='border-right:0px;'><div class='nosplit'>&nbsp;</div></td>
  461.                                  <td><div class='nosplit'>". $row['sub_department_description'];
  462.                
  463.              if($row['sub_department_code']) $report.= ' (' .$row['sub_department_code']. ')';
  464.                
  465.              $report .= "        </div></td>
  466.                                <td style='text-align:right;'><div class='nosplit'>".$rounded_qty."</div></td>
  467.                                <td style='text-align:right;'><div class='nosplit'>".$cur_symbol.$tc."</div></td>
  468.                                <td style='text-align:right;'><div class='nosplit'>".$cur_symbol.$tr."</div></td>
  469.                         </tr>";                
  470.            
  471.            
  472.              $prev_dep_code = $row['department_code'];
  473.              $prev_dep_desc = $row['department_description'];
  474.            
  475.          }
  476.        
  477.          $stc = number_format($sum_total_cost, 2);
  478.          $str = number_format($sum_total_retail, 2);
  479.          $rounded_tq = (floor($sum_total_qty) != $sum_total_qty) ? $sum_total_qty : round($sum_total_qty);
  480.        
  481.          $report .= "<tr>
  482.                         <td class='bottom' colspan='2' style='text-transform:uppercase'><div class='nosplit'>(".$dep_title.")</div></td>
  483.                         <td class='bottom'><div class='nosplit'>".$rounded_tq."</div></td>
  484.                         <td class='bottom'><div class='nosplit'>".$cur_symbol.$stc."</div></td>
  485.                         <td class='bottom'><div class='nosplit'>".$cur_symbol.$str."</div></td>
  486.                     </tr>";        
  487.        
  488.          $report .= "</table>";
  489.          return $report;
  490.      }
  491.   function reportHeader($title){
  492.        
  493.         $header = "<table class='report_header' cellspacing='0' cellpadding='0'>
  494.                        <tr >
  495.                            <td style='width:50%; text-align:left;'>
  496.                                <strong>Stocktaking.ie</strong><br/>
  497.                                        Lakeview Point<br/>
  498.                                        Claregalway Corporate Park<br/>
  499.                                        Claregalway, Galway
  500.                            </td>
  501.                            <td style='text-align:right;'>
  502.                                <img src='/images/stocktaking_s.gif' alt='Stocktaking' height='69' width='211'/>
  503.                            </td>
  504.                        </tr>
  505.                        <tr>
  506.                            <td colspan='2'><h2 style='color:black;font-size:20px;font-weight:bold;margin-top:10px;'>".$title."</h2></td>
  507.                        </tr>
  508.                    </table>";
  509.         return $header;
  510.     }
  511.      function stocktakeDetails($stocktake_id){
  512.        
  513.         $stocktake_qry = "
  514.                            SELECT
  515.                                s.stocktake_date, s.customer_name, s.store_name, s.store_address, s.store_post_code, s.store_town, s.store_county_name,
  516.                                c.country_name, s.date_created
  517.                            FROM stocktakes s LEFT JOIN country c
  518.                            ON c.country_code_alpha2 = s.customer_country_code
  519.                            WHERE stocktake_id = '{$stocktake_id}';";
  520.         $stocktake_res = db::c()->query($stocktake_qry);
  521.         $stocktake_row = $stocktake_res->fetch(PDO::FETCH_ASSOC);
  522.         $date_created = $stocktake_row['date_created'];
  523.         $stocktake_date = l::date($stocktake_row['stocktake_date']);
  524.  
  525.         $stocktake_details = "<table class='st_det_rep'>
  526.                                <tr>
  527.                                    <td style='width:40%; text-align:left;'>
  528.                                        <table style='font-size:12px;float:left;'>
  529.                                            <tr>
  530.                                                <td style='text-align:left;'>Customer:</td>
  531.                                                <td style='text-align:left;'><strong>".$stocktake_row['customer_name']."</strong></td>
  532.                                            </tr>        
  533.                                            <tr>
  534.                                                    <td style='width:120px; text-align:left;'>Stocktake Number:</td>
  535.                                                <td style='text-align:left;'>".$stocktake_id."</td>
  536.                                            </tr>
  537.                                            <tr>
  538.                                                <td style='text-align:left;'>Stocktake Date:</td>
  539.                                                <td style='text-align:left;'>".$stocktake_date."</td>
  540.                                            </tr>
  541.                                        </table>
  542.                                    </td>
  543.                                <td style='width:60%; text-align:left;'>
  544.                                        <table style='font-size:12px;float:right;'>
  545.                                            <tr>
  546.                                                <td style='width:120px;text-align:right;font-weight:bold;padding-right:20px;'>Store:</td>
  547.                                                <td style='text-align:right;font-weight:bold;'>".$stocktake_row['store_name']."</td>
  548.                                            </tr>
  549.                                            <tr>
  550.                                                <td style='text-align:left;'></td>
  551.                                                <td style='text-align:right;'>".$stocktake_row['store_address']."<br/>
  552.                                                ".$stocktake_row['store_town'].", ".$stocktake_row['store_post_code']."<br/>
  553.                                                ".$stocktake_row['store_county_name'].", ".$stocktake_row['country_name']."
  554.                                                </td>
  555.                                            </tr>
  556.                                        </table>
  557.                                    </td>
  558.                                </tr>
  559.                            </table>";
  560.        
  561.         return $stocktake_details;        
  562.        
  563.     }
  564.     }
  565. $report = $Navigation->getParam('report');
  566. $stocktake_id = $Navigation->getParam('stocktake_id');
  567. $section[] = $_POST['section'];
  568.  
  569. $part = $Navigation->getParam('part');
  570.  
  571. $html_begin = '<html><head>
  572.                 <link rel="stylesheet" type="text/css" href="http://sttest.aptvision.com/styles/main_style.css" />
  573.                 <script>
  574.                     function subst() {
  575.                       var vars={};
  576.                       var x=document.location.search.substring(1).split("&");
  577.                       for(var i in x) {var z=x[i].split("=",2);vars[z[0]] = unescape(z[1]);}
  578.                       var x=["topage","page"];
  579.                       for(var i in x) {
  580.                         var y = document.getElementsByClassName(x[i]);
  581.                         for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
  582.                       }
  583.                     }
  584.                 </script>
  585.                 </head><body onload="subst()">';
  586.  
  587. $html_end = '</body></html>';
  588.   if (!$part)
  589.   {
  590.      $content_url1 = '';
  591.       //update stocktake
  592.    
  593.       $res = db::c()->query("SELECT stocktake_product_id FROM stocktake_scans WHERE stocktake_id = 304");
  594.  
  595.       while ($row = $res->fetch())
  596.       {
  597.           $qty = db::c()->query("SELECT SUM(quantity) FROM stocktake_scans WHERE stocktake_product_id = '{$row['stocktake_product_id']}';")->fetchColumn(0);
  598.           if (!$qty)
  599.               $qty = '0';
  600.           db::c()->query("UPDATE stocktake_products SET quantity_scanned = '{$qty}' WHERE stocktake_product_id = '{$row['stocktake_product_id']}';");
  601.       }
  602.    
  603.       $params = array();
  604.       $params['security_off'] = true;
  605.       $params['stocktake_id'] = $stocktake_id;
  606.       $params['report'] = $report;
  607.    
  608.       $params['section'] = $Page->getPOSTVar('section');
  609.       $params['sdep'] = $Page->getPOSTVar('sel_rep_department');
  610.       $params['start'] = $Page->getPOSTVar('start_code1');
  611.       $params['finish'] = $Page->getPOSTVar('finish_code1');
  612.      
  613.       if ($Page->getGETVar('a_ids'))
  614.       {
  615.           $params['a_ids'] = $Page->getGETVar('a_ids');
  616.       }
  617.          unset($content_url);
  618.       $params['part'] = 'content';
  619.       $content_url1 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  620.    
  621.       $params['part'] = 'header';
  622.       $header_url1 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  623.    
  624.       $params['part'] = 'footer';
  625.       $footer_url1 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  626.    
  627.       $wkpdf = new WKPDF($content_url1);
  628.       $wkpdf->footerUrl = $footer_url1;
  629.       $wkpdf->headerUrl = $header_url1;
  630.  
  631.       $wkpdf->marginTop = '35';
  632.       $wkpdf->marginBottom = '20';
  633.       $wkpdf->output('S', 'Reports'.$stocktake_id .'\SubDepartmentSummaryReport__' . $stocktake_id .'.pdf');
  634.    
  635.      
  636.   }
  637.   else if ($part == 'content')
  638.   {
  639.       $all_sections = $Page->getPOSTVar('all_sections');
  640.       if ($all_sections) {
  641.           $section = null;
  642.       }
  643.       else {
  644.           $secarr = $Navigation->getParam('section');
  645.           $section = '';
  646.           if ($secarr && is_array($secarr) && count($secarr) > 0)
  647.           {
  648.               foreach($secarr as $s)
  649.               {
  650.                   $section.= db::c()->quote($s) . ',';
  651.               }
  652.               $section = substr($section, 0, -1);
  653.           }
  654.           else
  655.           {
  656.               $section = null;
  657.           }
  658.       }
  659.       $sdep = $Navigation->getParam('sdep');
  660.       $start = $Navigation->getParam('start');
  661.       $finish = $Navigation->getParam('finish');
  662.    
  663.       if (!$sdep || $sdep == '00all')
  664.       {
  665.           $sdep = null;
  666.       }
  667.       echo $html_begin;
  668.       echo Report2::stocktakeDetails($stocktake_id);
  669.       echo Report2::subDepartmentReportContent($stocktake_id, $sections);
  670.       echo $html_end;
  671.    
  672.       }
  673.   else if ($part == 'header')
  674.   {
  675.       echo $html_begin;
  676.       echo Report2::reportHeader('Sub Department Report');
  677.       echo $html_end;
  678.   }
  679.   else if ($part == 'footer')
  680.   {
  681.   echo $html_begin;
  682.   echo "<div class='page_numbers'> &ndash; Page <span class='page'></span> of <span class='topage'></span> &ndash; </div>";
  683.   echo $html_end;
  684.   }
  685.  
  686.  //Stock Detail Report
  687.   class Report3{
  688.    function stockDetailReportContent($stocktake_id, $sections, $sdep){
  689.    
  690.         if ($sdep) $sdep = db::c()->quote($sdep);
  691.        
  692.         //get all the departments for the given stocktake
  693.         $dep_qry = "SELECT a.department_description, a.product_name, a.product_description, a.barcode,
  694.                        SUM(a.quantity) AS quantity, SUM(a.cost) AS cost, SUM(a.retail) AS retail, a.unit_cost_price_estimated, a.unit_retail_price_estimated
  695.                    FROM
  696.                        (
  697.                        
  698.                            SELECT
  699.                                 sp.department_description AS department_description, sp.product_name, sp.product_description AS product_description,
  700.                                 ss.barcode AS barcode, sum(ss.quantity) AS quantity, sp.unit_cost_price*sum(ss.quantity) AS cost, sp.unit_retail_price*sum(ss.quantity) AS retail,
  701.                                 sp.unit_cost_price_estimated AS unit_cost_price_estimated, sp.unit_retail_price_estimated AS unit_retail_price_estimated
  702.                            FROM stocktake_scans ss
  703.                            INNER JOIN stocktake_products sp ON ss.stocktake_product_id = sp.stocktake_product_id ";
  704.         if ($sections)
  705.         {
  706.             $dep_qry.= " INNER JOIN stocktake_areas sa ON ss.stocktake_area_id = sa.stocktake_area_id ";
  707.         }
  708.        
  709.         $dep_qry.= "        WHERE ss.stocktake_id = '{$stocktake_id}' ";
  710.        
  711.         if ($sdep)
  712.         {
  713.             $dep_qry.= " AND STRCMP(sp.department_description, {$sdep}) = 0 ";
  714.         }
  715.        
  716.         if ($sections)
  717.         {
  718.             $dep_qry.= " AND sa.section IN ({$sections}) ";
  719.         }
  720.        
  721.         $dep_qry.= "        GROUP BY sp.department_description, sp.product_description, sp.product_name, ss.barcode
  722.        
  723.                            UNION ALL
  724.                        
  725.                            SELECT
  726.                                 si.department_description AS department_description, si.product_name AS product_name, si.product_name  AS product_description,
  727.                                 'N/A' AS barcode, si.quantity AS quantity, si.unit_cost_price*si.quantity AS cost, si.unit_retail_price*si.quantity AS retail,
  728.                                 'N/A' AS unit_cost_price_estimated, 'N/A' AS unit_retail_price_estimated
  729.                            FROM stocktake_items si
  730.                            WHERE si.stocktake_id = '{$stocktake_id}' ";
  731.        
  732.         if ($sdep)
  733.         {
  734.             $dep_qry.= " AND STRCMP(si.department_description, {$sdep}) = 0 ";
  735.         }
  736.  
  737.         if ($sections)
  738.         {
  739.             $dep_qry.= " AND section IN ({$sections}) ";
  740.         }
  741.        
  742.         $dep_qry.= "    
  743.                            GROUP BY si.department_description, si.product_name
  744.                        ) a
  745.                    GROUP BY a.department_description, a.product_name, a.product_description, a.barcode
  746.                    ORDER BY a.department_description ASC, a.product_name ASC, a.product_description ASC;";
  747.  
  748.         $dep_res = db::c()->query($dep_qry);
  749.            
  750.         //get currency code for the stocktake
  751.         $cur_symbol = Stocktake::getCurrencySymbol($stocktake_id);
  752.        
  753.         //if (strcmp($cur_res, 'EUR') == 0) setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
  754.    
  755.         //$report = self::reportHeader("Stock Detail Report");
  756.         //$report .= self::stocktakeDetails($stocktake_id);
  757.    
  758.         $report .= "
  759.                         <table id='stock_detail_report' cellspacing='0'>
  760.                             <tr>
  761.                                 <td class='top' style='text-align:left;width:50%;'><div class='nosplit'>Description</div></td>
  762.                                 <td class='top' style='text-align:left;width:20%;'><div class='nosplit'>Barcode</div></td>
  763.                                 <td class='top' style='text-align:center;width:10%;'><div class='nosplit'>Quantity</div></td>
  764.                                 <td class='top' style='text-align:right;width:10%;'><div class='nosplit'>Cost</div></td>
  765.                                 <td class='top' style='text-align:right;width:10%;'><div class='nosplit'>Retail</div></td>
  766.                             </tr>";
  767.    
  768.         $curdep = null;
  769.        
  770.         $gtq = 0;
  771.         $gtc = 0;
  772.         $gtr = 0;
  773.        
  774.         while ($row = $dep_res->fetch(PDO::FETCH_ASSOC))
  775.         {
  776.             $dep = $row['department_description'];
  777.            
  778.             if (!$dep) $dep = 'N/A';
  779.             else $fix = '';
  780.            
  781.             if ($curdep === null)
  782.             {
  783.                 $report .= "<tr>
  784.                <td colspan='5' class='title'> &nbsp;".$fix.$dep."</td>
  785.                </tr>";
  786.             }
  787.            
  788.            
  789.             if ($curdep !== null && $curdep != $dep)
  790.             {
  791.                 $stc = number_format($sum_total_cost, 2);
  792.                 $str = number_format($sum_total_retail, 2);
  793.                
  794.                 $rounded_tq = (floor($sum_total_qty) != $sum_total_qty) ? $sum_total_qty : round($sum_total_qty);
  795.                    
  796.                 $report .= "
  797.                    <tr>
  798.                        <td class='bottom' colspan='2' style='text-align:right;'><div class='nosplit'>Total:</div></td>
  799.                        <td class='bottom' style='text-align:center;'><div class='nosplit'>".$rounded_tq."</div></td>
  800.                        <td class='bottom' style='text-align:right;'><div class='nosplit'>".$cur_symbol.$stc."</div></td>
  801.                        <td class='bottom' style='text-align:right;'><div class='nosplit'>".$cur_symbol.$str."</div></td>
  802.                    </tr>";
  803.  
  804.                 $gtq += $sum_total_qty;
  805.                 $gtc += $sum_total_cost;
  806.                 $gtr += $sum_total_retail;
  807.                
  808.                 $sum_total_cost = 0;
  809.                 $sum_total_retail = 0;
  810.                 $sum_total_qty = 0;
  811.                
  812.                 $report .= "
  813.                    <tr>
  814.                        <td colspan='5' class='title'> &nbsp;".$fix.$dep."</td>
  815.                    </tr>";
  816.  
  817.             }
  818.            
  819.            
  820.            
  821.             $total_cost = $row['cost'];
  822.             $total_retail = $row['retail'];
  823.                
  824.             $sum_total_cost += $total_cost;
  825.             $sum_total_retail += $total_retail;
  826.             $sum_total_qty += $row['quantity'];
  827.                
  828.             $tc = number_format($total_cost, 2);
  829.             $tr = number_format($total_retail, 2);
  830.            
  831.             $cost_estimated = $row['unit_cost_price_estimated'] ? ' (e)' : '';
  832.             $retail_estimated = $row['unit_retail_price_estimated'] ? ' (e)' : '';
  833.            
  834.             $rounded_qty = (floor($row['quantity']) != $row['quantity']) ? $row['quantity'] : round($row['quantity']);
  835.                
  836.             $report .= "
  837.                            <tr>
  838.                                 <td><div class='nosplit'>". ($row['product_name'] ? $row['product_name'] : $row['product_description'])."</div></td>
  839.                                 <td><div class='nosplit'>". $row['barcode']."</div></td>
  840.                                 <td style='text-align:center;'><div class='nosplit'>".$rounded_qty."</div></td>
  841.                               <td style='text-align:right;'><div class='nosplit'>".$cur_symbol.$tc.$cost_estimated."</div></td>
  842.                               <td style='text-align:right;'><div class='nosplit'>".$cur_symbol.$tr.$retail_estimated."</div></td>
  843.                            </tr>";        
  844.            
  845.            
  846.             $curdep = $dep;
  847.         }
  848.    
  849.         $stc = number_format($sum_total_cost, 2);
  850.         $str = number_format($sum_total_retail, 2);
  851.        
  852.         $rounded_tq = (floor($sum_total_qty) != $sum_total_qty) ? $sum_total_qty : round($sum_total_qty);
  853.            
  854.         $report .= "
  855.                    <tr>
  856.                        <td class='bottom' colspan='2'><div class='nosplit'>Total:</div></td>
  857.                        <td class='bottom' style='text-align:center;'><div class='nosplit'>".$rounded_tq."</div></td>
  858.                        <td class='bottom'><div class='nosplit'>".$cur_symbol.$stc."</div></td>
  859.                        <td class='bottom'><div class='nosplit'>".$cur_symbol.$str."</div></td>
  860.                    </tr>";
  861.        
  862.         $report .= "
  863.                    <tr>
  864.                        <td class='bottom' colspan='5'><div class='nosplit'></div></td>
  865.                    </tr>";
  866.         $gtq += $sum_total_qty;
  867.                 $gtc += $sum_total_cost;
  868.                 $gtr += $sum_total_retail;
  869.                
  870.         $gtc = number_format($gtc, 2);
  871.         $gtr = number_format($gtr, 2);
  872.        
  873.         $report .= "
  874.                    <tr>
  875.                        <td class='bottom' colspan='2'><div class='nosplit'>Grand Total:</div></td>
  876.                        <td class='bottom' style='text-align:center;'><div class='nosplit'>".$gtq."</div></td>
  877.                        <td class='bottom'><div class='nosplit'>".$cur_symbol.$gtc."</div></td>
  878.                        <td class='bottom'><div class='nosplit'>".$cur_symbol.$gtr."</div></td>
  879.                    </tr>";
  880.        
  881.         $report .= "</table>";
  882.         return $report;
  883.     }      
  884.   function reportHeader($title){
  885.        
  886.         $header = "<table class='report_header' cellspacing='0' cellpadding='0'>
  887.                        <tr >
  888.                            <td style='width:50%; text-align:left;'>
  889.                                <strong>Stocktaking.ie</strong><br/>
  890.                                        Lakeview Point<br/>
  891.                                        Claregalway Corporate Park<br/>
  892.                                        Claregalway, Galway
  893.                            </td>
  894.                            <td style='text-align:right;'>
  895.                                <img src='/images/stocktaking_s.gif' alt='Stocktaking' height='69' width='211'/>
  896.                            </td>
  897.                        </tr>
  898.                        <tr>
  899.                            <td colspan='2'><h2 style='color:black;font-size:20px;font-weight:bold;margin-top:10px;'>".$title."</h2></td>
  900.                        </tr>
  901.                    </table>";
  902.         return $header;
  903.     }
  904.      function stocktakeDetails($stocktake_id){
  905.        
  906.         $stocktake_qry = "
  907.                            SELECT
  908.                                s.stocktake_date, s.customer_name, s.store_name, s.store_address, s.store_post_code, s.store_town, s.store_county_name,
  909.                                c.country_name, s.date_created
  910.                            FROM stocktakes s LEFT JOIN country c
  911.                            ON c.country_code_alpha2 = s.customer_country_code
  912.                            WHERE stocktake_id = '{$stocktake_id}';";
  913.         $stocktake_res = db::c()->query($stocktake_qry);
  914.         $stocktake_row = $stocktake_res->fetch(PDO::FETCH_ASSOC);
  915.         $date_created = $stocktake_row['date_created'];
  916.         $stocktake_date = l::date($stocktake_row['stocktake_date']);
  917.  
  918.         $stocktake_details = "<table class='st_det_rep'>
  919.                                <tr>
  920.                                    <td style='width:40%; text-align:left;'>
  921.                                        <table style='font-size:12px;float:left;'>
  922.                                            <tr>
  923.                                                <td style='text-align:left;'>Customer:</td>
  924.                                                <td style='text-align:left;'><strong>".$stocktake_row['customer_name']."</strong></td>
  925.                                            </tr>        
  926.                                            <tr>
  927.                                                    <td style='width:120px; text-align:left;'>Stocktake Number:</td>
  928.                                                <td style='text-align:left;'>".$stocktake_id."</td>
  929.                                            </tr>
  930.                                            <tr>
  931.                                                <td style='text-align:left;'>Stocktake Date:</td>
  932.                                                <td style='text-align:left;'>".$stocktake_date."</td>
  933.                                            </tr>
  934.                                        </table>
  935.                                    </td>
  936.                                <td style='width:60%; text-align:left;'>
  937.                                        <table style='font-size:12px;float:right;'>
  938.                                            <tr>
  939.                                                <td style='width:120px;text-align:right;font-weight:bold;padding-right:20px;'>Store:</td>
  940.                                                <td style='text-align:right;font-weight:bold;'>".$stocktake_row['store_name']."</td>
  941.                                            </tr>
  942.                                            <tr>
  943.                                                <td style='text-align:left;'></td>
  944.                                                <td style='text-align:right;'>".$stocktake_row['store_address']."<br/>
  945.                                                ".$stocktake_row['store_town'].", ".$stocktake_row['store_post_code']."<br/>
  946.                                                ".$stocktake_row['store_county_name'].", ".$stocktake_row['country_name']."
  947.                                                </td>
  948.                                            </tr>
  949.                                        </table>
  950.                                    </td>
  951.                                </tr>
  952.                            </table>";
  953.        
  954.         return $stocktake_details;        
  955.        
  956.     }
  957.     }
  958. $report = $Navigation->getParam('report');
  959. $stocktake_id = $Navigation->getParam('stocktake_id');
  960. $section[] = $_POST['section'];
  961.  
  962. $part = $Navigation->getParam('part');
  963.  
  964. $html_begin = '<html><head>
  965.                 <link rel="stylesheet" type="text/css" href="http://sttest.aptvision.com/styles/main_style.css" />
  966.                 <script>
  967.                     function subst() {
  968.                       var vars={};
  969.                       var x=document.location.search.substring(1).split("&");
  970.                       for(var i in x) {var z=x[i].split("=",2);vars[z[0]] = unescape(z[1]);}
  971.                       var x=["topage","page"];
  972.                       for(var i in x) {
  973.                         var y = document.getElementsByClassName(x[i]);
  974.                         for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
  975.                       }
  976.                     }
  977.                 </script>
  978.                 </head><body onload="subst()">';
  979.  
  980. $html_end = '</body></html>';
  981.   if (!$part)
  982.   {
  983.      $content_url1 = '';
  984.       //update stocktake
  985.    
  986.       $res = db::c()->query("SELECT stocktake_product_id FROM stocktake_scans WHERE stocktake_id = 304");
  987.  
  988.       while ($row = $res->fetch())
  989.       {
  990.           $qty = db::c()->query("SELECT SUM(quantity) FROM stocktake_scans WHERE stocktake_product_id = '{$row['stocktake_product_id']}';")->fetchColumn(0);
  991.           if (!$qty)
  992.               $qty = '0';
  993.           db::c()->query("UPDATE stocktake_products SET quantity_scanned = '{$qty}' WHERE stocktake_product_id = '{$row['stocktake_product_id']}';");
  994.       }
  995.    
  996.       $params = array();
  997.       $params['security_off'] = true;
  998.       $params['stocktake_id'] = $stocktake_id;
  999.       $params['report'] = $report;
  1000.    
  1001.       $params['section'] = $Page->getPOSTVar('section');
  1002.       $params['sdep'] = $Page->getPOSTVar('sel_rep_department');
  1003.       $params['start'] = $Page->getPOSTVar('start_code1');
  1004.       $params['finish'] = $Page->getPOSTVar('finish_code1');
  1005.      
  1006.       if ($Page->getGETVar('a_ids'))
  1007.       {
  1008.           $params['a_ids'] = $Page->getGETVar('a_ids');
  1009.       }
  1010.          unset($content_url);
  1011.       $params['part'] = 'content2';
  1012.       $content_url2 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  1013.    
  1014.       $params['part'] = 'header2';
  1015.       $header_url2 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  1016.    
  1017.       $params['part'] = 'footer2';
  1018.       $footer_url2 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  1019.    
  1020.       $wkpdf = new WKPDF($content_url2);
  1021.       $wkpdf->footerUrl = $footer_url2;
  1022.       $wkpdf->headerUrl = $header_url2;
  1023.  
  1024.       $wkpdf->marginTop = '35';
  1025.       $wkpdf->marginBottom = '20';
  1026.       $wkpdf->output('S', 'Reports'.$stocktake_id .'\StockDetailReport__' . $stocktake_id .'.pdf');
  1027.    
  1028.      
  1029.   }
  1030.   else if ($part == 'content2')
  1031.   {
  1032.       $all_sections = $Page->getPOSTVar('all_sections');
  1033.       if ($all_sections) {
  1034.           $section = null;
  1035.       }
  1036.       else {
  1037.           $secarr = $Navigation->getParam('section');
  1038.           $section = '';
  1039.           if ($secarr && is_array($secarr) && count($secarr) > 0)
  1040.           {
  1041.               foreach($secarr as $s)
  1042.               {
  1043.                   $section.= db::c()->quote($s) . ',';
  1044.               }
  1045.               $section = substr($section, 0, -1);
  1046.           }
  1047.           else
  1048.           {
  1049.               $section = null;
  1050.           }
  1051.       }
  1052.       $sdep = $Navigation->getParam('sdep');
  1053.       $start = $Navigation->getParam('start');
  1054.       $finish = $Navigation->getParam('finish');
  1055.    
  1056.       if (!$sdep || $sdep == '00all')
  1057.       {
  1058.           $sdep = null;
  1059.       }
  1060.       echo $html_begin;
  1061.       echo Report3::stocktakeDetails($stocktake_id);
  1062.       echo Report3::stockDetailReportContent($stocktake_id, $sections, $sdep);
  1063.       echo $html_end;
  1064.    
  1065.       }
  1066.   else if ($part == 'header2')
  1067.   {
  1068.       echo $html_begin;
  1069.       echo Report3::reportHeader('Stock Detail Report');
  1070.       echo $html_end;
  1071.   }
  1072.   else if ($part == 'footer2')
  1073.   {
  1074.   echo $html_begin;
  1075.   echo "<div class='page_numbers'> &ndash; Page <span class='page'></span> of <span class='topage'></span> &ndash; </div>";
  1076.   echo $html_end;
  1077.   }
  1078.  
  1079.   //Audit trail report
  1080.   class Report4{
  1081.   function areaAuditTrailReport2Content($stocktake_id, $area_ids = null, $sections){
  1082.    
  1083.         //get currency code for the stocktake
  1084.         $cur_symbol = Stocktake::getCurrencySymbol($stocktake_id);
  1085.        
  1086.         //get all areas for the given stocktake for which there are valid scans
  1087.         $area_qry = "
  1088.                    SELECT * FROM
  1089.                    (( SELECT
  1090.                        sa.stocktake_area_id, sa.area_name
  1091.                        FROM stocktake_areas sa
  1092.                        INNER JOIN stocktake_scans ss
  1093.                            ON sa.stocktake_area_id = ss.stocktake_area_id
  1094.                        WHERE sa.stocktake_id = '{$stocktake_id}' AND ss.stocktake_id = '{$stocktake_id}' AND ss.quantity > 0 ";
  1095.    
  1096.         if ($area_ids) {
  1097.             $area_qry.= " AND sa.stocktake_area_id IN ($area_ids) ";
  1098.         }
  1099.    
  1100.         $area_qry.= "GROUP BY stocktake_area_id, area_name )
  1101.  
  1102.                     UNION ALL
  1103.        
  1104.                    ( SELECT
  1105.                        sa.stocktake_area_id, sa.area_name
  1106.                        FROM stocktake_areas sa
  1107.                        INNER JOIN stocktake_items si
  1108.                            ON sa.stocktake_area_id = si.stocktake_area_id
  1109.                        WHERE sa.stocktake_id = '{$stocktake_id}' AND si.stocktake_id = '{$stocktake_id}' AND si.quantity > 0 ";
  1110.         if ($area_ids) {
  1111.             $area_qry.= " AND sa.stocktake_area_id IN ($area_ids) ";
  1112.         }
  1113.        
  1114.         $area_qry.= " GROUP BY stocktake_area_id, area_name )
  1115.                    ) a ";
  1116.        
  1117.         $area_qry.= "
  1118.                    ORDER BY area_name ASC " ;                        
  1119.                 //echo $area_qry;
  1120.         //die();
  1121.        
  1122.         try{
  1123.             $area_res = db::c()->query($area_qry);
  1124.         }
  1125.         catch (Exception $e){
  1126.             echo $e;
  1127.         }
  1128.    
  1129.         $report = '';
  1130.        
  1131.         $grand_total_count = 0;
  1132.         $grand_total_cost = 0;
  1133.         $grand_total_ret = 0;
  1134.        
  1135.         //select all products or items which have no area_id but belong to this stocktake
  1136.         $noarea_qry = "
  1137.                    SELECT * FROM
  1138.                    ( (SELECT
  1139.                        ss.barcode AS barcode, sp.product_name AS product_name, sp.product_description AS product_description,
  1140.                        sp.unit_cost_price AS unit_cost_price, sp.unit_retail_price AS unit_retail_price, sp.product_source AS product_source,
  1141.                        sp.unit_cost_price_estimated, sp.unit_retail_price_estimated,
  1142.                        ss.quantity AS quantity, ss.scan_date AS scan_date, u.stocktake_staff_name AS stocktake_staff_name,
  1143.                    UNIX_TIMESTAMP(ss.scan_date) AS scan_date_ts
  1144.                    FROM stocktake_scans ss
  1145.                    LEFT JOIN stocktake_products sp ON sp.stocktake_product_id = ss.stocktake_product_id
  1146.                    LEFT JOIN stocktake_staff u ON u.stocktake_staff_id = ss.stocktake_staff_id
  1147.                    LEFT JOIN stocktake_errors se ON se.barcode = ss.barcode AND se.stocktake_id = '{$stocktake_id}'
  1148.                    WHERE
  1149.                        ss.stocktake_id = '{$stocktake_id}'
  1150.                        AND IFNULL(ss.stocktake_area_id, 0) = 0
  1151.                        AND (
  1152.                            se.resolution_status IS NULL OR (se.resolution_status <> 'WRONG BARCODE' AND se.resolution_status <> 'IGNORED')
  1153.                        ) )
  1154.                        
  1155.                    UNION ALL
  1156.                    
  1157.                    ( SELECT
  1158.                    'N/A' AS barcode, si.product_name AS product_name, 'N/A' AS product_description,
  1159.                    si.unit_cost_price AS unit_cost_price, si.unit_retail_price AS unit_retail_price, 'N/A' AS product_source,
  1160.                    'N/A' AS unit_cost_price_estimated, 'N/A' AS unit_retail_price_estimated,
  1161.                    si.quantity AS quantity, si.date_created AS scan_date,
  1162.                    'N/A' AS stocktake_staff_name, UNIX_TIMESTAMP(si.date_created) AS scan_date_ts
  1163.                    FROM
  1164.                        stocktake_items si
  1165.                    WHERE si.stocktake_id = '{$stocktake_id}'
  1166.                    AND IFNULL(si.stocktake_area_id, 0) = 0 ) ) a
  1167.                    
  1168.                    ORDER BY scan_date_ts ASC; ";
  1169.        
  1170.         //echo $scan_qry;
  1171.         //die();
  1172.            
  1173.         $noarea_res = db::c()->query($noarea_qry);
  1174.        
  1175.         if ($noarea_res->rowCount())
  1176.         {
  1177.             $report .= "<div>
  1178.                            <table cellspacing='0' class='at_report'>
  1179.                                <thead>
  1180.                                    <tr><th colspan='8'>Area Not Specified</th></tr>
  1181.                                </thead>
  1182.                            <tr>
  1183.                            <td class='simple_header' style='width:7%;'><div class='nosplit'>No.</div></td>
  1184.                            <td class='simple_header' style='width:8%;'><div class='nosplit'>Barcode</div></td>
  1185.                            <td class='simple_header' style='width:40%;'><div class='nosplit'>Product</div></td>
  1186.                            <td class='simple_header' style='width:5%;'><div class='nosplit'>Qty</div></td>
  1187.                            <td class='simple_header' style='width:7%;'><div class='nosplit'>Cost Value</div></td>
  1188.                            <td class='simple_header' style='width:7%;'><div class='nosplit'>Retail Value</div></td>
  1189.                            <td class='simple_header' style='width:16%;'><div class='nosplit'>Created By</div></td>
  1190.                            <td class='simple_header' style='width:10%;'><div class='nosplit'>Time</div></td>
  1191.                            </tr>";
  1192.            
  1193.             $total_count = 0;
  1194.             $total_cost = 0;
  1195.             $total_ret = 0;
  1196.             $cost_e = false;
  1197.             $retail_e = false;
  1198.            
  1199.             $n = 1;
  1200.            
  1201.             $row_counter = 1;
  1202.            
  1203.             while ($nrow = $noarea_res->fetch(PDO::FETCH_ASSOC))
  1204.             {
  1205.            
  1206.                 //print_r($nrow);
  1207.                 //die();
  1208.                
  1209.                 $cost = $nrow['quantity']*$nrow['unit_cost_price'];
  1210.                 $ret = $nrow['quantity']*$nrow['unit_retail_price'];
  1211.                 $cn = number_format($cost, 2);
  1212.                 $rn = number_format($ret, 2);
  1213.            
  1214.                 $total_count += $nrow['quantity'];
  1215.                 $total_cost += $cost;
  1216.                 $total_ret += $ret;
  1217.                
  1218.                 $cost_estimated = $nrow['unit_cost_price_estimated'] ? ' (e)' : '';
  1219.                 $retail_estimated = $nrow['unit_retail_price_estimated'] ? ' (e)' : '';
  1220.                
  1221.                 if($nrow['unit_cost_price_estimated']) $cost_e = true;
  1222.                 if($nrow['unit_retail_price_estimated']) $retail_e = true;
  1223.                    
  1224.                 $dt = new DateTime($nrow['scan_date']);
  1225.                 $row_style ='';
  1226.                 if ($nrow['product_source'] == 'M') $row_style = 'style="background:#ffdddd;"';
  1227.                 $rounded_qty = (floor($nrow['quantity']) != $nrow['quantity']) ? $nrow['quantity'] : round($nrow['quantity']);
  1228.            
  1229.                 //$time_string = date("H:i:s", $nrow['scan_date_ts']);
  1230.                 //date_format($nrow['scan_date'], 'g:i A');
  1231.                
  1232.                 $product_nd = $nrow['product_name'] ? $nrow['product_name'] : $nrow['product_description'];
  1233.                 if (strlen($product_nd)> 40)
  1234.                     $product_nd_short = substr($product_nd, 0, 40)."...";
  1235.                 else
  1236.                     $product_nd_short = $product_nd;
  1237.            
  1238.                 if (strlen($product_nd)> 40)
  1239.                     $product_nd_short = substr($product_nd, 0, 40)."...";
  1240.                 else
  1241.                     $product_nd_short = $product_nd;
  1242.                    
  1243.                
  1244.                 $report .= "<tr ".$row_style.">
  1245.                <td><div class='nosplit'>".$n."</div></td>
  1246.                <td><div class='nosplit'>".$nrow['barcode']."</div></td>
  1247.                <td><div class='nosplit'>".$product_nd_short."</div></td>
  1248.                <td style='text-align:center;'><div class='nosplit'>".$rounded_qty."</div></td>
  1249.                <td style='text-align:center;'><div class='nosplit'>".$cur_symbol.$cn.$cost_estimated."</div></td>
  1250.                <td style='text-align:center;'><div class='nosplit'>".$cur_symbol.$rn.$retail_estimated."</div></td>
  1251.                <td style='text-align:center;'><div class='nosplit'>".((strlen($nrow['stocktake_staff_name'])>16)? substr($nrow['stocktake_staff_name'], 0, 15)."..." : $nrow['stocktake_staff_name'])."</div></td>
  1252.                <td style='text-align:center;'><div class='nosplit'>".$dt->format('H:i:s')."</div></td>
  1253.                </tr>";
  1254.                
  1255.                 if ($row_counter%44 == 0) $report .= "</table> </div>
  1256.                    <div class='at_report_div'>
  1257.                            <table cellspacing='0' class='at_report'>
  1258.                                <thead>
  1259.                                    <tr><th colspan='8'>Area Not Specified</th></tr>
  1260.                                </thead>
  1261.                            <tr>
  1262.                            <td class='simple_header' style='width:7%;'><div class='nosplit'>No.</div></td>
  1263.                            <td class='simple_header' style='width:8%;'><div class='nosplit'>Barcode</div></td>
  1264.                            <td class='simple_header' style='width:40%;'><div class='nosplit'>Product</div></td>
  1265.                            <td class='simple_header' style='width:5%;'><div class='nosplit'>Qty</div></td>
  1266.                            <td class='simple_header' style='width:7%;'><div class='nosplit'>Cost Value</div></td>
  1267.                            <td class='simple_header' style='width:7%;'><div class='nosplit'>Retail Value</div></td>
  1268.                            <td class='simple_header' style='width:16%;'><div class='nosplit'>Created By</div></td>
  1269.                            <td class='simple_header' style='width:10%;'><div class='nosplit'>Time</div></td>
  1270.                            </tr>";
  1271.                 $n++;
  1272.                
  1273.                 $row_counter++;
  1274.             }
  1275.  
  1276.  
  1277.             $tc = number_format($total_cost, 2);
  1278.             $tr = number_format($total_ret, 2);
  1279.             $rounded_tc = (floor($total_count) != $total_count) ? $total_count : round($total_count);
  1280.            
  1281.             $report .= "<tr>
  1282.                <td class='simple_footer' colspan='3' style='text-align:right;'><div class='nosplit'>Total:</div></td>
  1283.                <td class='simple_footer' style='text-align:center;'><div class='nosplit'>".$rounded_tc."</div></td>
  1284.                <td class='simple_footer' style='text-align:right;'><div class='nosplit'>".$cur_symbol.$tc."</div></td>
  1285.                <td class='simple_footer' style='text-align:right;'><div class='nosplit'>".$cur_symbol.$tr."</div></td>
  1286.                <td class='simple_footer'><div class='nosplit'>&nbsp;</div></td>
  1287.                <td class='simple_footer'><div class='nosplit'>&nbsp;</div></td>
  1288.            </tr>";
  1289.             $report .= "</table><br/><br/>
  1290.            </div>";
  1291.            
  1292.             $grand_total_count += $total_count;
  1293.             $grand_total_cost += $total_cost;
  1294.             $grand_total_ret += $total_ret;
  1295.        
  1296.         }
  1297.  
  1298.         while ($row = $area_res->fetch(PDO::FETCH_ASSOC)){
  1299.            
  1300.             $total_count = 0;
  1301.             $total_cost = 0;
  1302.             $total_ret = 0;
  1303.            
  1304.             $row_counter = 1;
  1305.            
  1306.             $report .= "<div ";
  1307.            
  1308.             if ($n != 1) $report .= "class='at_report_div' ";
  1309.             else $report .= "style='page-break-inside: avoid;'";
  1310.            
  1311.             $report .= ">
  1312.                        <table cellspacing='0' class='at_report'>
  1313.                            <thead>
  1314.                                <tr><th colspan='8'>Area ".$row['area_name']."</th></tr>
  1315.                            </thead>
  1316.                            <tr>
  1317.                                <td class='simple_header' style='width:7%;'><div class='nosplit'>No.</div></td>
  1318.                                <td class='simple_header' style='width:8%;'><div class='nosplit'>Barcode</div></td>
  1319.                                <td class='simple_header' style='width:40%;'><div class='nosplit'>Product</div></td>
  1320.                                <td class='simple_header' style='width:5%;'><div class='nosplit'>Qty</div></td>
  1321.                                <td class='simple_header' style='width:7%;'><div class='nosplit'>Cost Value</div></td>
  1322.                                <td class='simple_header' style='width:7%;'><div class='nosplit'>Retail Value</div></td>
  1323.                                <td class='simple_header' style='width:16%;'><div class='nosplit'>Created By</div></td>
  1324.                                <td class='simple_header' style='width:10%;'><div class='nosplit'>Time</div></td>
  1325.                            </tr>";
  1326.        
  1327.             $area_id = $row['stocktake_area_id'];
  1328.             //select all the scans for the current area
  1329.             $scan_qry = "
  1330.                        SELECT * FROM
  1331.                        ( (SELECT
  1332.                        ss.barcode AS barcode, sp.product_name AS product_name, sp.product_description AS product_description,
  1333.                        sp.unit_cost_price AS unit_cost_price, sp.unit_retail_price AS unit_retail_price, sp.product_source AS product_source,
  1334.                        sp.unit_cost_price_estimated AS unit_cost_price_estimated, sp.unit_retail_price_estimated AS unit_retail_price_estimated,
  1335.                        ss.quantity AS quantity, ss.scan_date AS scan_date, u.stocktake_staff_name AS stocktake_staff_name,
  1336.                        UNIX_TIMESTAMP(ss.scan_date) AS scan_date_ts
  1337.                        FROM stocktake_scans ss
  1338.                        LEFT JOIN stocktake_products sp ON sp.stocktake_product_id = ss.stocktake_product_id
  1339.                        LEFT JOIN stocktake_staff u ON u.stocktake_staff_id = ss.stocktake_staff_id
  1340.                        LEFT JOIN stocktake_errors se ON se.barcode = ss.barcode AND se.stocktake_id = '{$stocktake_id}'";
  1341.             if ($sections)
  1342.             {
  1343.                 $scan_qry .= " LEFT JOIN stocktake_areas sa ON ss.stocktake_area_id = sa.stocktake_area_id ";
  1344.             }
  1345.        
  1346.             $scan_qry .= "
  1347.                        WHERE
  1348.                            ss.stocktake_id = '{$stocktake_id}'
  1349.                        AND ss.stocktake_area_id = '{$area_id}'
  1350.                        AND (
  1351.                        se.resolution_status IS NULL OR (se.resolution_status <> 'WRONG BARCODE' AND se.resolution_status <> 'IGNORED')
  1352.                        ) ";
  1353.            
  1354.             if ($sections)
  1355.             {
  1356.                 $scan_qry .= " AND sa.section IN ({$sections}) ";
  1357.             }
  1358.            
  1359.             $scan_qry .= " )
  1360.                        UNION ALL
  1361.                        
  1362.                        ( SELECT
  1363.                            'N/A' AS barcode, si.product_name AS product_name, 'N/A' AS product_description, 'N/A' AS product_source,  
  1364.                            si.unit_cost_price AS unit_cost_price, si.unit_retail_price AS unit_retail_price,
  1365.                            'N/A' AS unit_cost_price_estimated, 'N/A' AS unit_retail_price_estimated,
  1366.                            si.quantity AS quantity, si.date_created AS scan_date,
  1367.                            'N/A' AS stocktake_staff_name, UNIX_TIMESTAMP(si.date_created) AS scan_date_ts
  1368.                        FROM
  1369.                            stocktake_items si ";
  1370.             if ($sections)
  1371.             {
  1372.                 $scan_qry .= " LEFT JOIN stocktake_areas sa ON si.stocktake_area_id = sa.stocktake_area_id ";
  1373.             }
  1374.                
  1375.             $scan_qry .= " WHERE si.stocktake_id = '{$stocktake_id}'
  1376.                          AND si.stocktake_area_id = '{$area_id}' ";
  1377.            
  1378.             if ($sections)
  1379.             {
  1380.                 $scan_qry .= " AND sa.section IN ({$sections}) ";
  1381.             }
  1382.                                        
  1383.             $scan_qry .= "    ) ) a
  1384.                        ORDER BY scan_date_ts ASC; ";
  1385.  
  1386.             //echo $scan_qry;
  1387.             //die();  
  1388.            
  1389.             $scan_res = db::c()->query($scan_qry);
  1390.                
  1391.             $i = 1;
  1392.                
  1393.             while ($srow = $scan_res->fetch(PDO::FETCH_ASSOC)){
  1394.                
  1395.                     //print_r($srow);
  1396.                     //die();
  1397.             if ($srow['quantity'] > 0)
  1398.             {
  1399.                     $cost = $srow['quantity']*$srow['unit_cost_price'];
  1400.                     $ret = $srow['quantity']*$srow['unit_retail_price'];
  1401.                     $cn = number_format($cost, 2);
  1402.                     $rn = number_format($ret, 2);
  1403.                
  1404.                     $total_count += $srow['quantity'];
  1405.                     $total_cost += $cost;
  1406.                     $total_ret += $ret;
  1407.                    
  1408.                     $mcost_estimated = $srow['unit_cost_price_estimated'] ? ' (e)' : '';
  1409.                     $mretail_estimated = $srow['unit_retail_price_estimated'] ? ' (e)' : '';
  1410.                    
  1411.                     $dt = new DateTime($srow['scan_date']);
  1412.                     $row_style ='';
  1413.                     if ($srow['product_source'] == 'M') $row_style = 'style="background:#ffdddd;"';
  1414.                     $rounded_qty = (floor($srow['quantity']) != $srow['quantity']) ? $srow['quantity'] : round($srow['quantity']);
  1415.                    
  1416.                     $product_nd = $srow['product_name'] ? $srow['product_name'] : $srow['product_description'];
  1417.                     if (strlen($product_nd) > 40)
  1418.                         $product_nd_short = substr($product_nd, 0, 40)."...";
  1419.                     else
  1420.                         $product_nd_short = $product_nd;
  1421.                    
  1422.                        
  1423.                
  1424.                     //$time_string = date("H:i:s", $srow['scan_date_ts']);
  1425.                     //date_format($srow['scan_date'], 'g:i A');
  1426.                
  1427.                     $report .= "<tr ".$row_style.">
  1428.                                    <td><div class='nosplit'>".$n."</div></td>
  1429.                                    <td><div class='nosplit'>".$srow['barcode']."</div></td>
  1430.                                    <td><div class='nosplit'>".$product_nd_short."</div></td>
  1431.                                    <td style='text-align:center;'><div class='nosplit'>".$rounded_qty."</div></td>
  1432.                                    <td style='text-align:center;'><div class='nosplit'>".$cur_symbol.$cn.$mcost_estimated."</div></td>
  1433.                                    <td style='text-align:center;'><div class='nosplit'>".$cur_symbol.$rn.$mretail_estimated."</div></td>
  1434.                                    <td style='text-align:center;'><div class='nosplit'>".((strlen($srow['stocktake_staff_name'])>16)? substr($srow['stocktake_staff_name'], 0, 16)."..." : $srow['stocktake_staff_name'])."</div></td>
  1435.                                    <td style='text-align:center;'><div class='nosplit'>".$dt->format('H:i:s')."</div></td>
  1436.                                </tr>";
  1437.                    
  1438.                     if ($row_counter%44 == 0) $report .= "</table><br/><br/></div>
  1439.                                <div class='at_report_div'>
  1440.                                <table cellspacing='0' class='at_report'>
  1441.                                <thead>
  1442.                                <tr><th colspan='8'>Area ".$row['area_name']."</th></tr>
  1443.                                </thead>
  1444.                                <tr>
  1445.                                <td class='simple_header' style='width:7%;'><div class='nosplit'>No.</div></td>
  1446.                                <td class='simple_header' style='width:8%;'><div class='nosplit'>Barcode</div></td>
  1447.                                <td class='simple_header' style='width:40%;'><div class='nosplit'>Product</div></td>
  1448.                                <td class='simple_header' style='width:5%;'><div class='nosplit'>Qty</div></td>
  1449.                                <td class='simple_header' style='width:7%;'><div class='nosplit'>Cost Value</div></td>
  1450.                                <td class='simple_header' style='width:7%;'><div class='nosplit'>Retail Value</div></td>
  1451.                                <td class='simple_header' style='width:16%;'><div class='nosplit'>Created By</div></td>
  1452.                                <td class='simple_header' style='width:10%;'><div class='nosplit'>Time</div></td>
  1453.                                </tr>";
  1454.                     $row_counter++;    
  1455.                     $n++;
  1456.                     }
  1457.                 }
  1458.                 $i++;
  1459.                 $tc = number_format($total_cost, 2);                
  1460.                 $tr = number_format($total_ret, 2);
  1461.                 $rounded_tc = (floor($total_count) != $total_count) ? $total_count : round($total_count);
  1462.                
  1463.                 $report .= "<tr>                
  1464.                                <td class='simple_footer' colspan='3' style='text-align:right;'><div class='nosplit'>Total:</div></td>
  1465.                                <td class='simple_footer' style='text-align:center;'><div class='nosplit'>".$rounded_tc."</div></td>
  1466.                                <td class='simple_footer' style='text-align:right;'><div class='nosplit'>".$cur_symbol.$tc."</div></td>
  1467.                                <td class='simple_footer' style='text-align:right;'><div class='nosplit'>".$cur_symbol.$tr."</div></td>
  1468.                                <td class='simple_footer'><div class='nosplit'>&nbsp;</div></td>
  1469.                                <td class='simple_footer'><div class='nosplit'>&nbsp;</div></td>
  1470.                            </tr>";
  1471.                 $report .= "</table><br/><br/>
  1472.                            </div>";
  1473.                
  1474.                 $grand_total_count += $total_count;
  1475.                 $grand_total_cost += $total_cost;
  1476.                 $grand_total_ret += $total_ret;
  1477.         }
  1478.                
  1479.         $gtc = number_format($grand_total_cost, 2);
  1480.         $gtr = number_format($grand_total_ret, 2);
  1481.         $grand_rounded_tc = (floor($grand_total_count) != $grand_total_count) ? $grand_total_count : round($grand_total_count);
  1482.        
  1483.         $report .= "<table cellspacing='0' class='at_report'>
  1484.                    <tr>        
  1485.                        <td class='simple_footer' style='text-align:right;width:55%;'><div class='nosplit'>Grand Total:</div></td>
  1486.                        <td class='simple_footer' style='text-align:center;width:5%;'><div class='nosplit'>".$grand_rounded_tc."</div></td>
  1487.                        <td class='simple_footer' style='text-align:right;width:7%;'><div class='nosplit'>".$cur_symbol.$gtc."</div></td>
  1488.                        <td class='simple_footer' style='text-align:right;width:7%;'><div class='nosplit'>".$cur_symbol.$gtr."</div></td>
  1489.                        <td class='simple_footer' style='width:16%;'><div class='nosplit'>&nbsp;</div></td>
  1490.                        <td class='simple_footer' style='width:10%;'><div class='nosplit'>&nbsp;</div></td>
  1491.                    </tr>
  1492.                    </table>
  1493.            </div><br/><br/>";
  1494.        
  1495.         return $report;
  1496.     }  
  1497.   function reportHeader($title){
  1498.        
  1499.         $header = "<table class='report_header' cellspacing='0' cellpadding='0'>
  1500.                        <tr >
  1501.                            <td style='width:50%; text-align:left;'>
  1502.                                <strong>Stocktaking.ie</strong><br/>
  1503.                                        Lakeview Point<br/>
  1504.                                        Claregalway Corporate Park<br/>
  1505.                                        Claregalway, Galway
  1506.                            </td>
  1507.                            <td style='text-align:right;'>
  1508.                                <img src='/images/stocktaking_s.gif' alt='Stocktaking' height='69' width='211'/>
  1509.                            </td>
  1510.                        </tr>
  1511.                        <tr>
  1512.                            <td colspan='2'><h2 style='color:black;font-size:20px;font-weight:bold;margin-top:10px;'>".$title."</h2></td>
  1513.                        </tr>
  1514.                    </table>";
  1515.         return $header;
  1516.     }
  1517.      function stocktakeDetails($stocktake_id){
  1518.        
  1519.         $stocktake_qry = "
  1520.                            SELECT
  1521.                                s.stocktake_date, s.customer_name, s.store_name, s.store_address, s.store_post_code, s.store_town, s.store_county_name,
  1522.                                c.country_name, s.date_created
  1523.                            FROM stocktakes s LEFT JOIN country c
  1524.                            ON c.country_code_alpha2 = s.customer_country_code
  1525.                            WHERE stocktake_id = '{$stocktake_id}';";
  1526.         $stocktake_res = db::c()->query($stocktake_qry);
  1527.         $stocktake_row = $stocktake_res->fetch(PDO::FETCH_ASSOC);
  1528.         $date_created = $stocktake_row['date_created'];
  1529.         $stocktake_date = l::date($stocktake_row['stocktake_date']);
  1530.  
  1531.         $stocktake_details = "<table class='st_det_rep'>
  1532.                                <tr>
  1533.                                    <td style='width:40%; text-align:left;'>
  1534.                                        <table style='font-size:12px;float:left;'>
  1535.                                            <tr>
  1536.                                                <td style='text-align:left;'>Customer:</td>
  1537.                                                <td style='text-align:left;'><strong>".$stocktake_row['customer_name']."</strong></td>
  1538.                                            </tr>        
  1539.                                            <tr>
  1540.                                                    <td style='width:120px; text-align:left;'>Stocktake Number:</td>
  1541.                                                <td style='text-align:left;'>".$stocktake_id."</td>
  1542.                                            </tr>
  1543.                                            <tr>
  1544.                                                <td style='text-align:left;'>Stocktake Date:</td>
  1545.                                                <td style='text-align:left;'>".$stocktake_date."</td>
  1546.                                            </tr>
  1547.                                        </table>
  1548.                                    </td>
  1549.                                <td style='width:60%; text-align:left;'>
  1550.                                        <table style='font-size:12px;float:right;'>
  1551.                                            <tr>
  1552.                                                <td style='width:120px;text-align:right;font-weight:bold;padding-right:20px;'>Store:</td>
  1553.                                                <td style='text-align:right;font-weight:bold;'>".$stocktake_row['store_name']."</td>
  1554.                                            </tr>
  1555.                                            <tr>
  1556.                                                <td style='text-align:left;'></td>
  1557.                                                <td style='text-align:right;'>".$stocktake_row['store_address']."<br/>
  1558.                                                ".$stocktake_row['store_town'].", ".$stocktake_row['store_post_code']."<br/>
  1559.                                                ".$stocktake_row['store_county_name'].", ".$stocktake_row['country_name']."
  1560.                                                </td>
  1561.                                            </tr>
  1562.                                        </table>
  1563.                                    </td>
  1564.                                </tr>
  1565.                            </table>";
  1566.        
  1567.         return $stocktake_details;        
  1568.        
  1569.     }
  1570.     }
  1571. $report = $Navigation->getParam('report');
  1572. $stocktake_id = $Navigation->getParam('stocktake_id');
  1573. $section[] = $_POST['section'];
  1574.  
  1575. $part = $Navigation->getParam('part');
  1576.  
  1577. $html_begin = '<html><head>
  1578.                 <link rel="stylesheet" type="text/css" href="http://sttest.aptvision.com/styles/main_style.css" />
  1579.                 <script>
  1580.                     function subst() {
  1581.                       var vars={};
  1582.                       var x=document.location.search.substring(1).split("&");
  1583.                       for(var i in x) {var z=x[i].split("=",2);vars[z[0]] = unescape(z[1]);}
  1584.                       var x=["topage","page"];
  1585.                       for(var i in x) {
  1586.                         var y = document.getElementsByClassName(x[i]);
  1587.                         for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
  1588.                       }
  1589.                     }
  1590.                 </script>
  1591.                 </head><body onload="subst()">';
  1592.  
  1593. $html_end = '</body></html>';
  1594.   if (!$part)
  1595.   {
  1596.    
  1597.       //update stocktake
  1598.    
  1599.       $res = db::c()->query("SELECT stocktake_product_id FROM stocktake_scans WHERE stocktake_id = 304");
  1600.  
  1601.       while ($row = $res->fetch())
  1602.       {
  1603.           $qty = db::c()->query("SELECT SUM(quantity) FROM stocktake_scans WHERE stocktake_product_id = '{$row['stocktake_product_id']}';")->fetchColumn(0);
  1604.           if (!$qty)
  1605.               $qty = '0';
  1606.           db::c()->query("UPDATE stocktake_products SET quantity_scanned = '{$qty}' WHERE stocktake_product_id = '{$row['stocktake_product_id']}';");
  1607.       }
  1608.    
  1609.       $params = array();
  1610.       $params['security_off'] = true;
  1611.       $params['stocktake_id'] = $stocktake_id;
  1612.       $params['report'] = $report;
  1613.    
  1614.       $params['section'] = $Page->getPOSTVar('section');
  1615.       $params['sdep'] = $Page->getPOSTVar('sel_rep_department');
  1616.       $params['start'] = $Page->getPOSTVar('start_code1');
  1617.       $params['finish'] = $Page->getPOSTVar('finish_code1');
  1618.      
  1619.       if ($Page->getGETVar('a_ids'))
  1620.       {
  1621.           $params['a_ids'] = $Page->getGETVar('a_ids');
  1622.       }
  1623.          unset($content_url);
  1624.       $params['part'] = 'content3';
  1625.       $content_url3 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  1626.    
  1627.       $params['part'] = 'header3';
  1628.       $header_url3 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  1629.    
  1630.       $params['part'] = 'footer3';
  1631.       $footer_url3 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  1632.    
  1633.       $wkpdf = new WKPDF($content_url3);
  1634.       $wkpdf->footerUrl = $footer_url3;
  1635.       $wkpdf->headerUrl = $header_url3;
  1636.  
  1637.       $wkpdf->marginTop = '35';
  1638.       $wkpdf->marginBottom = '20';
  1639.       $wkpdf->output('S', 'Reports'.$stocktake_id .'\AuditTrailReport__' . $stocktake_id .'.pdf');
  1640.    
  1641.      
  1642.   }
  1643.   else if ($part == 'content3')
  1644.   {
  1645.       $all_sections = $Page->getPOSTVar('all_sections');
  1646.       if ($all_sections) {
  1647.           $section = null;
  1648.       }
  1649.       else {
  1650.           $secarr = $Navigation->getParam('section');
  1651.           $section = '';
  1652.           if ($secarr && is_array($secarr) && count($secarr) > 0)
  1653.           {
  1654.               foreach($secarr as $s)
  1655.               {
  1656.                   $section.= db::c()->quote($s) . ',';
  1657.               }
  1658.               $section = substr($section, 0, -1);
  1659.           }
  1660.           else
  1661.           {
  1662.               $section = null;
  1663.           }
  1664.       }
  1665.       $sdep = $Navigation->getParam('sdep');
  1666.       $start = $Navigation->getParam('start');
  1667.       $finish = $Navigation->getParam('finish');
  1668.    
  1669.       if (!$sdep || $sdep == '00all')
  1670.       {
  1671.           $sdep = null;
  1672.       }
  1673.       echo $html_begin;
  1674.       echo Report4::stocktakeDetails($stocktake_id);
  1675.       echo Report4::areaAuditTrailReport2Content($stocktake_id, $area_ids = null, $sections);
  1676.       echo $html_end;
  1677.    
  1678.       }
  1679.   else if ($part == 'header3')
  1680.   {
  1681.       echo $html_begin;
  1682.       echo Report4::reportHeader('Audit Trail Report');
  1683.       echo $html_end;
  1684.   }
  1685.   else if ($part == 'footer3')
  1686.   {
  1687.   echo $html_begin;
  1688.   echo "<div class='page_numbers'> &ndash; Page <span class='page'></span> of <span class='topage'></span> &ndash; </div>";
  1689.   echo $html_end;
  1690.   }
  1691.  
  1692.  //Valuation Certificate
  1693.  
  1694.   class Report5{
  1695.   function valuationCertificateContent($stocktake_id, $sections){
  1696.    
  1697.         //get currency code for the stocktake
  1698.         $cur_symbol = Stocktake::getCurrencySymbol($stocktake_id);
  1699.    
  1700.         // $report = "<table class='report_header' style='border:0px;'>
  1701.                         // <tr>
  1702.                             // <td style='width:50%; text-align:left;'>
  1703.                                 // <strong>Stocktaking.ie</strong><br/>
  1704.                                         // Lakeview Point<br/>
  1705.                                         // Claregalway Corporate Park<br/>
  1706.                                         // Claregalway<br/>
  1707.                                         // Galway<br/><br/><br/><br/><br/>
  1708.                             // </td>
  1709.                             // <td style='text-align:right;'>
  1710.                                 // <img src='/images/stocktaking.gif' alt='Stocktaking' height='92' width='211'/>
  1711.                             // </td>
  1712.                         // </tr>
  1713.                         // <tr>
  1714.                             // <td colspan='2' style='border-top:1px solid #cccccc;border-bottom:1px solid #cccccc;padding:5px;'>
  1715.                                 // <div style='padding:6px;color:black;font-size:20px;font-weight:bold;background:#eeeeee;'>
  1716.                                 // Valuation Certificate
  1717.                                 // </div>
  1718.                             // </td>
  1719.                         // </tr>
  1720.                   // </table>";
  1721.  
  1722.         $stocktake_qry = "
  1723.                            SELECT
  1724.                                s.store_name, s.store_address, s.store_post_code, s.store_town, s.store_county_name,
  1725.                                c.country_name, s.stocktake_date AS date_created
  1726.                            FROM stocktakes s LEFT JOIN country c
  1727.                            ON c.country_code_alpha2 = s.store_country_code
  1728.                            WHERE stocktake_id = '{$stocktake_id}';";
  1729.         $stocktake_res = db::c()->query($stocktake_qry);
  1730.         $stocktake_row = $stocktake_res->fetch(PDO::FETCH_ASSOC);
  1731.         $date_created = $stocktake_row['date_created'];
  1732.         $stocktake_date = l::date($date_created);
  1733.  
  1734.         $report = "<table class='st_det_rep'>
  1735.                                <tr>
  1736.                                    <td style='width:60%; text-align:left;'>
  1737.                                        <strong>".$stocktake_row['store_name']."</strong><br/>
  1738.                                                ".$stocktake_row['store_address']."<br/>
  1739.                                                ".$stocktake_row['store_post_code']."<br/>
  1740.                                                ".$stocktake_row['store_town'].", ".$stocktake_row['store_county_name']."<br/>
  1741.                                                ".$stocktake_row['country_name']."
  1742.                                    </td>
  1743.                                    <td>
  1744.                                        <table style='width:100%;'>
  1745.                                            <tr>
  1746.                                                <td style='text-align:right;font-weight:bold;'>Valuation Date:</td>
  1747.                                            </tr>
  1748.                                            <tr>
  1749.                                                <td style='text-align:right;'>".$stocktake_date."</td>
  1750.                                            </tr>
  1751.                                        </table>
  1752.                                    </td>
  1753.                                </tr>
  1754.                            </table>";
  1755.    
  1756.         //do a query to calculate total values
  1757.         $total_qry = "
  1758.                SELECT SUM( sp.unit_cost_price * ss.quantity) AS cost, SUM( sp.unit_retail_price * ss.quantity ) AS ret
  1759.                FROM stocktake_scans ss
  1760.                LEFT JOIN stocktake_products sp ON sp.stocktake_product_id = ss.stocktake_product_id ";
  1761.         if ($sections)
  1762.         {
  1763.             $total_qry .= " INNER JOIN stocktake_areas sa ON ss.stocktake_area_id = sa.stocktake_area_id ";
  1764.         }
  1765.        
  1766.         $total_qry .= " WHERE ss.stocktake_id = '{$stocktake_id}' ";
  1767.        
  1768.         if ($sections)
  1769.         {
  1770.             $total_qry .= " AND sa.section IN ({$sections}) ";
  1771.         }
  1772.        
  1773.         $total_qry .= " ;";
  1774.         $total_res = db::c()->query($total_qry)->fetch(PDO::FETCH_ASSOC);        
  1775.        
  1776.         $nonbc_total_qry =     "
  1777.                SELECT SUM( si.unit_cost_price * si.quantity ) AS cost, SUM( si.unit_retail_price * si.quantity ) AS ret
  1778.                FROM stocktake_items si
  1779.                WHERE
  1780.                    si.stocktake_id = '{$stocktake_id}'";
  1781.                    
  1782.         if ($sections)
  1783.         {
  1784.             $nonbc_total_qry .= " AND si.section IN ({$sections}) ";
  1785.         }
  1786.        
  1787.         $nonbc_total_res = db::c()->query($nonbc_total_qry)->fetch(PDO::FETCH_ASSOC);
  1788.        
  1789.         $total_cost = $total_res['cost'] + $nonbc_total_res['cost'];
  1790.         $total_retail = $total_res['ret'] + $nonbc_total_res['ret'];
  1791.         $tc = number_format($total_cost, 2);
  1792.         $tr = number_format($total_retail, 2);
  1793.        
  1794.         $report .= "<div style='width:100%;background:white;padding:30px 10px;'>
  1795.                        <table cellspacing='0' id='val_report'>
  1796.                             <tr>
  1797.                                 <td class='simple_header' colspan='2'>".$stocktake_row['store_name']."</td>
  1798.                            </tr>
  1799.                            <tr>
  1800.                                <td class='title'>Value at Cost</td>
  1801.                                <td class='title'>Value at Retail</td>
  1802.                            </tr>
  1803.                            <tr>
  1804.                                <td>".$cur_symbol.$tc."</td>
  1805.                                <td>".$cur_symbol.$tr."</td>
  1806.                            </tr>
  1807.                        </table>
  1808.                    </div>";        
  1809.  
  1810.         $report .= "<div style='width:100%;background:white;padding:10px;text-align:left;vertical-align:top;font-size:12px;'>
  1811.                     <div class='simple_title'>Valuation Carried Out By:</div>";        
  1812.  
  1813.         //get all the staff that worked on the stocktake
  1814.         $staff_qry = "
  1815.                    SELECT
  1816.                        stocktake_staff_name
  1817.                    FROM stocktake_staff
  1818.                    WHERE stocktake_id = ". $stocktake_id ."
  1819.                    ORDER BY stocktake_staff_id ASC;";
  1820.        
  1821.         $staff_res = db::c()->query($staff_qry);
  1822.        
  1823.         while ($staff_row = $staff_res->fetch(PDO::FETCH_ASSOC))
  1824.         {
  1825.             $report .= $staff_row['stocktake_staff_name'];
  1826.             $report .= "<br/>";
  1827.         }
  1828.         $report .= "<br/><br/></div>";
  1829.         return $report;
  1830.     }
  1831.   function reportHeader($title){
  1832.        
  1833.         $header = "<table class='report_header' cellspacing='0' cellpadding='0'>
  1834.                        <tr >
  1835.                            <td style='width:50%; text-align:left;'>
  1836.                                <strong>Stocktaking.ie</strong><br/>
  1837.                                        Lakeview Point<br/>
  1838.                                        Claregalway Corporate Park<br/>
  1839.                                        Claregalway, Galway
  1840.                            </td>
  1841.                            <td style='text-align:right;'>
  1842.                                <img src='/images/stocktaking_s.gif' alt='Stocktaking' height='69' width='211'/>
  1843.                            </td>
  1844.                        </tr>
  1845.                        <tr>
  1846.                            <td colspan='2'><h2 style='color:black;font-size:20px;font-weight:bold;margin-top:10px;'>".$title."</h2></td>
  1847.                        </tr>
  1848.                    </table>";
  1849.         return $header;
  1850.     }
  1851.      function stocktakeDetails($stocktake_id){
  1852.        
  1853.         $stocktake_qry = "
  1854.                            SELECT
  1855.                                s.stocktake_date, s.customer_name, s.store_name, s.store_address, s.store_post_code, s.store_town, s.store_county_name,
  1856.                                c.country_name, s.date_created
  1857.                            FROM stocktakes s LEFT JOIN country c
  1858.                            ON c.country_code_alpha2 = s.customer_country_code
  1859.                            WHERE stocktake_id = '{$stocktake_id}';";
  1860.         $stocktake_res = db::c()->query($stocktake_qry);
  1861.         $stocktake_row = $stocktake_res->fetch(PDO::FETCH_ASSOC);
  1862.         $date_created = $stocktake_row['date_created'];
  1863.         $stocktake_date = l::date($stocktake_row['stocktake_date']);
  1864.  
  1865.         $stocktake_details = "<table class='st_det_rep'>
  1866.                                <tr>
  1867.                                    <td style='width:40%; text-align:left;'>
  1868.                                        <table style='font-size:12px;float:left;'>
  1869.                                            <tr>
  1870.                                                <td style='text-align:left;'>Customer:</td>
  1871.                                                <td style='text-align:left;'><strong>".$stocktake_row['customer_name']."</strong></td>
  1872.                                            </tr>        
  1873.                                            <tr>
  1874.                                                    <td style='width:120px; text-align:left;'>Stocktake Number:</td>
  1875.                                                <td style='text-align:left;'>".$stocktake_id."</td>
  1876.                                            </tr>
  1877.                                            <tr>
  1878.                                                <td style='text-align:left;'>Stocktake Date:</td>
  1879.                                                <td style='text-align:left;'>".$stocktake_date."</td>
  1880.                                            </tr>
  1881.                                        </table>
  1882.                                    </td>
  1883.                                <td style='width:60%; text-align:left;'>
  1884.                                        <table style='font-size:12px;float:right;'>
  1885.                                            <tr>
  1886.                                                <td style='width:120px;text-align:right;font-weight:bold;padding-right:20px;'>Store:</td>
  1887.                                                <td style='text-align:right;font-weight:bold;'>".$stocktake_row['store_name']."</td>
  1888.                                            </tr>
  1889.                                            <tr>
  1890.                                                <td style='text-align:left;'></td>
  1891.                                                <td style='text-align:right;'>".$stocktake_row['store_address']."<br/>
  1892.                                                ".$stocktake_row['store_town'].", ".$stocktake_row['store_post_code']."<br/>
  1893.                                                ".$stocktake_row['store_county_name'].", ".$stocktake_row['country_name']."
  1894.                                                </td>
  1895.                                            </tr>
  1896.                                        </table>
  1897.                                    </td>
  1898.                                </tr>
  1899.                            </table>";
  1900.        
  1901.         return $stocktake_details;        
  1902.        
  1903.     }
  1904.     }
  1905. $report = $Navigation->getParam('report');
  1906. $stocktake_id = $Navigation->getParam('stocktake_id');
  1907. $section[] = $_POST['section'];
  1908.  
  1909. $part = $Navigation->getParam('part');
  1910.  
  1911. $html_begin = '<html><head>
  1912.                 <link rel="stylesheet" type="text/css" href="http://sttest.aptvision.com/styles/main_style.css" />
  1913.                 <script>
  1914.                     function subst() {
  1915.                       var vars={};
  1916.                       var x=document.location.search.substring(1).split("&");
  1917.                       for(var i in x) {var z=x[i].split("=",2);vars[z[0]] = unescape(z[1]);}
  1918.                       var x=["topage","page"];
  1919.                       for(var i in x) {
  1920.                         var y = document.getElementsByClassName(x[i]);
  1921.                         for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
  1922.                       }
  1923.                     }
  1924.                 </script>
  1925.                 </head><body onload="subst()">';
  1926.  
  1927. $html_end = '</body></html>';
  1928.   if (!$part)
  1929.   {
  1930.    
  1931.       //update stocktake
  1932.    
  1933.       $res = db::c()->query("SELECT stocktake_product_id FROM stocktake_scans WHERE stocktake_id = 304");
  1934.  
  1935.       while ($row = $res->fetch())
  1936.       {
  1937.           $qty = db::c()->query("SELECT SUM(quantity) FROM stocktake_scans WHERE stocktake_product_id = '{$row['stocktake_product_id']}';")->fetchColumn(0);
  1938.           if (!$qty)
  1939.               $qty = '0';
  1940.           db::c()->query("UPDATE stocktake_products SET quantity_scanned = '{$qty}' WHERE stocktake_product_id = '{$row['stocktake_product_id']}';");
  1941.       }
  1942.    
  1943.       $params = array();
  1944.       $params['security_off'] = true;
  1945.       $params['stocktake_id'] = $stocktake_id;
  1946.       $params['report'] = $report;
  1947.    
  1948.       $params['section'] = $Page->getPOSTVar('section');
  1949.       $params['sdep'] = $Page->getPOSTVar('sel_rep_department');
  1950.       $params['start'] = $Page->getPOSTVar('start_code1');
  1951.       $params['finish'] = $Page->getPOSTVar('finish_code1');
  1952.      
  1953.       if ($Page->getGETVar('a_ids'))
  1954.       {
  1955.           $params['a_ids'] = $Page->getGETVar('a_ids');
  1956.       }
  1957.          unset($content_url);
  1958.       $params['part'] = 'content4';
  1959.       $content_url4 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  1960.    
  1961.       $params['part'] = 'header4';
  1962.       $header_url4 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  1963.    
  1964.       $params['part'] = 'footer4';
  1965.       $footer_url4 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  1966.    
  1967.       $wkpdf = new WKPDF($content_url4);
  1968.       $wkpdf->footerUrl = $footer_url4;
  1969.       $wkpdf->headerUrl = $header_url4;
  1970.  
  1971.       $wkpdf->marginTop = '35';
  1972.       $wkpdf->marginBottom = '20';
  1973.       $wkpdf->output('S', 'Reports'.$stocktake_id .'\ValuationCertificate__' . $stocktake_id .'.pdf');
  1974.    
  1975.      
  1976.   }
  1977.   else if ($part == 'content4')
  1978.   {
  1979.       $all_sections = $Page->getPOSTVar('all_sections');
  1980.       if ($all_sections) {
  1981.           $section = null;
  1982.       }
  1983.       else {
  1984.           $secarr = $Navigation->getParam('section');
  1985.           $section = '';
  1986.           if ($secarr && is_array($secarr) && count($secarr) > 0)
  1987.           {
  1988.               foreach($secarr as $s)
  1989.               {
  1990.                   $section.= db::c()->quote($s) . ',';
  1991.               }
  1992.               $section = substr($section, 0, -1);
  1993.           }
  1994.           else
  1995.           {
  1996.               $section = null;
  1997.           }
  1998.       }
  1999.       $sdep = $Navigation->getParam('sdep');
  2000.       $start = $Navigation->getParam('start');
  2001.       $finish = $Navigation->getParam('finish');
  2002.    
  2003.       if (!$sdep || $sdep == '00all')
  2004.       {
  2005.           $sdep = null;
  2006.       }
  2007.       echo $html_begin;
  2008.       echo Report5::stocktakeDetails($stocktake_id);
  2009.       echo Report5::valuationCertificateContent($stocktake_id, $sections);
  2010.       echo $html_end;
  2011.    
  2012.       }
  2013.   else if ($part == 'header4')
  2014.   {
  2015.       echo $html_begin;
  2016.       echo Report5::reportHeader('Valuation Certificate');
  2017.       echo $html_end;
  2018.   }
  2019.   else if ($part == 'footer4')
  2020.   {
  2021.   echo $html_begin;
  2022.   echo "<div class='page_numbers'> &ndash; Page <span class='page'></span> of <span class='topage'></span> &ndash; </div>";
  2023.   echo $html_end;
  2024.   }
  2025.  
  2026.   //Non scan list report
  2027.     class Report6{
  2028.   function nonScanListReportContent($stocktake_id, $sections){
  2029.    
  2030.         //get currency code for the stocktake
  2031.         $cur_symbol = Stocktake::getCurrencySymbol($stocktake_id);
  2032.    
  2033.         //$report = self::reportHeader("Non Scan Locations Report");
  2034.         //$report .= self::stocktakeDetails($stocktake_id);
  2035.    
  2036.         //get all the errors from the database
  2037.         $error_qry = "    SELECT
  2038.                            s.stocktake_scan_id, s.barcode, s.stocktake_area_id, a.area_name, p.unit_retail_price,p.unit_cost_price, p.product_name,
  2039.                            p.product_description, p.department_description,
  2040.                            st.stocktake_staff_name, s.scan_date, SUM(s.quantity) AS quantity
  2041.                        FROM stocktake_scans s
  2042.                        LEFT JOIN stocktake_areas a ON s.stocktake_area_id = a.stocktake_area_id
  2043.                        LEFT JOIN stocktake_staff st ON s.stocktake_staff_id = st.stocktake_staff_id
  2044.                        LEFT JOIN stocktake_products p ON p.stocktake_product_id = s.stocktake_product_id
  2045.                        WHERE s.stocktake_id = '{$stocktake_id}' AND p.product_source = 'M'
  2046.                         GROUP BY s.barcode";
  2047.    
  2048.         if ($sections)
  2049.         {
  2050.             $error_qry .= " AND a.section IN ({$sections}) ";
  2051.         }
  2052.    
  2053.         $error_qry .= " ORDER BY s.scan_date DESC, s.stocktake_scan_id DESC;";
  2054.  
  2055.         try{
  2056.             $error_res = db::c()->query($error_qry);
  2057.         }
  2058.         catch (Exception $e){
  2059.         echo "MySQL exception: $e ";
  2060.         }
  2061.    
  2062.         $report .= "
  2063.                    <table id='nsloc_report' cellspacing='0'>
  2064.                    <tr>
  2065.                            <th style='width:10%;'><div class='nosplit'>Barcode</div></th>
  2066.                             <th style='width:10%;'><div class='nosplit'>Quantity</div></th>
  2067.                             <th style='width:8%;'><div class='nosplit'>Cost Price</div></th>
  2068.                            <th style='width:8%;'><div class='nosplit'>Retail Price</div></th>
  2069.                            <th style='width:26%;'><div class='nosplit'>Product</div></th>
  2070.                            <th style='width:16%;'><div class='nosplit'>Department</div></th>
  2071.                            <th style='width:10%;'><div class='nosplit'>Area</div></th>
  2072.                            <th style='width:12%;'><div class='nosplit'>Staff member</div></th>
  2073.                            <th style='width:8%;'><div class='nosplit'>Time</div></th>
  2074.                    </tr>";
  2075.    
  2076.         while ($error_row = $error_res->fetch(PDO::FETCH_ASSOC))
  2077.         {
  2078.             //$date = date_create($error_row['date_time']);
  2079.                 $area_id = $error_row['stocktake_area_id'];
  2080.                
  2081.             $date = new DateTime($error_row['scan_date']);
  2082.             $pname = empty($error_row['product_name']) ? $error_row['product_description'] : $error_row['product_name'];
  2083.             $report .= "<tr>
  2084.                             <td class='topborder'><div class='nosplit'>".$error_row['barcode']."</div></td>
  2085.                            <td class='topborder'><div class='nosplit'>".$error_row['quantity']."</div></td>
  2086.                            <td class='topborder'><div class='nosplit'>".$error_row['unit_cost_price']."</div></td>
  2087.                             <td class='topborder'><div class='nosplit'>".$error_row['unit_retail_price']."</div></td>
  2088.                            <td class='topborder'><div class='nosplit'>".$pname."</div></td>
  2089.                            <td class='topborder'><div class='nosplit'>".$error_row['department_description']."</div></td>
  2090.                            <td class='topborder'><div class='nosplit'>".$error_row['area_name']."</div></td>
  2091.                                <td class='topborder'><div class='nosplit'>".$error_row['stocktake_staff_name']."</div></td>
  2092.                            <td class='topborder'><div class='nosplit'>".$date->format('H:i:s')."</div></td>
  2093.                                </tr>";
  2094.         }
  2095.         $report .= "</table>";
  2096.         return $report;
  2097.     }
  2098.   function reportHeader($title){
  2099.        
  2100.         $header = "<table class='report_header' cellspacing='0' cellpadding='0'>
  2101.                        <tr >
  2102.                            <td style='width:50%; text-align:left;'>
  2103.                                <strong>Stocktaking.ie</strong><br/>
  2104.                                        Lakeview Point<br/>
  2105.                                        Claregalway Corporate Park<br/>
  2106.                                        Claregalway, Galway
  2107.                            </td>
  2108.                            <td style='text-align:right;'>
  2109.                                <img src='/images/stocktaking_s.gif' alt='Stocktaking' height='69' width='211'/>
  2110.                            </td>
  2111.                        </tr>
  2112.                        <tr>
  2113.                            <td colspan='2'><h2 style='color:black;font-size:20px;font-weight:bold;margin-top:10px;'>".$title."</h2></td>
  2114.                        </tr>
  2115.                    </table>";
  2116.         return $header;
  2117.     }
  2118.      function stocktakeDetails($stocktake_id){
  2119.        
  2120.         $stocktake_qry = "
  2121.                            SELECT
  2122.                                s.stocktake_date, s.customer_name, s.store_name, s.store_address, s.store_post_code, s.store_town, s.store_county_name,
  2123.                                c.country_name, s.date_created
  2124.                            FROM stocktakes s LEFT JOIN country c
  2125.                            ON c.country_code_alpha2 = s.customer_country_code
  2126.                            WHERE stocktake_id = '{$stocktake_id}';";
  2127.         $stocktake_res = db::c()->query($stocktake_qry);
  2128.         $stocktake_row = $stocktake_res->fetch(PDO::FETCH_ASSOC);
  2129.         $date_created = $stocktake_row['date_created'];
  2130.         $stocktake_date = l::date($stocktake_row['stocktake_date']);
  2131.  
  2132.         $stocktake_details = "<table class='st_det_rep'>
  2133.                                <tr>
  2134.                                    <td style='width:40%; text-align:left;'>
  2135.                                        <table style='font-size:12px;float:left;'>
  2136.                                            <tr>
  2137.                                                <td style='text-align:left;'>Customer:</td>
  2138.                                                <td style='text-align:left;'><strong>".$stocktake_row['customer_name']."</strong></td>
  2139.                                            </tr>        
  2140.                                            <tr>
  2141.                                                    <td style='width:120px; text-align:left;'>Stocktake Number:</td>
  2142.                                                <td style='text-align:left;'>".$stocktake_id."</td>
  2143.                                            </tr>
  2144.                                            <tr>
  2145.                                                <td style='text-align:left;'>Stocktake Date:</td>
  2146.                                                <td style='text-align:left;'>".$stocktake_date."</td>
  2147.                                            </tr>
  2148.                                        </table>
  2149.                                    </td>
  2150.                                <td style='width:60%; text-align:left;'>
  2151.                                        <table style='font-size:12px;float:right;'>
  2152.                                            <tr>
  2153.                                                <td style='width:120px;text-align:right;font-weight:bold;padding-right:20px;'>Store:</td>
  2154.                                                <td style='text-align:right;font-weight:bold;'>".$stocktake_row['store_name']."</td>
  2155.                                            </tr>
  2156.                                            <tr>
  2157.                                                <td style='text-align:left;'></td>
  2158.                                                <td style='text-align:right;'>".$stocktake_row['store_address']."<br/>
  2159.                                                ".$stocktake_row['store_town'].", ".$stocktake_row['store_post_code']."<br/>
  2160.                                                ".$stocktake_row['store_county_name'].", ".$stocktake_row['country_name']."
  2161.                                                </td>
  2162.                                            </tr>
  2163.                                        </table>
  2164.                                    </td>
  2165.                                </tr>
  2166.                            </table>";
  2167.        
  2168.         return $stocktake_details;        
  2169.        
  2170.     }
  2171.     }
  2172. $report = $Navigation->getParam('report');
  2173. $stocktake_id = $Navigation->getParam('stocktake_id');
  2174. $section[] = $_POST['section'];
  2175.  
  2176. $part = $Navigation->getParam('part');
  2177.  
  2178. $html_begin = '<html><head>
  2179.                 <link rel="stylesheet" type="text/css" href="http://sttest.aptvision.com/styles/main_style.css" />
  2180.                 <script>
  2181.                     function subst() {
  2182.                       var vars={};
  2183.                       var x=document.location.search.substring(1).split("&");
  2184.                       for(var i in x) {var z=x[i].split("=",2);vars[z[0]] = unescape(z[1]);}
  2185.                       var x=["topage","page"];
  2186.                       for(var i in x) {
  2187.                         var y = document.getElementsByClassName(x[i]);
  2188.                         for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
  2189.                       }
  2190.                     }
  2191.                 </script>
  2192.                 </head><body onload="subst()">';
  2193.  
  2194. $html_end = '</body></html>';
  2195.   if (!$part)
  2196.   {
  2197.    
  2198.       //update stocktake
  2199.    
  2200.       $res = db::c()->query("SELECT stocktake_product_id FROM stocktake_scans WHERE stocktake_id = 304");
  2201.  
  2202.       while ($row = $res->fetch())
  2203.       {
  2204.           $qty = db::c()->query("SELECT SUM(quantity) FROM stocktake_scans WHERE stocktake_product_id = '{$row['stocktake_product_id']}';")->fetchColumn(0);
  2205.           if (!$qty)
  2206.               $qty = '0';
  2207.           db::c()->query("UPDATE stocktake_products SET quantity_scanned = '{$qty}' WHERE stocktake_product_id = '{$row['stocktake_product_id']}';");
  2208.       }
  2209.    
  2210.       $params = array();
  2211.       $params['security_off'] = true;
  2212.       $params['stocktake_id'] = $stocktake_id;
  2213.       $params['report'] = $report;
  2214.    
  2215.       $params['section'] = $Page->getPOSTVar('section');
  2216.       $params['sdep'] = $Page->getPOSTVar('sel_rep_department');
  2217.       $params['start'] = $Page->getPOSTVar('start_code1');
  2218.       $params['finish'] = $Page->getPOSTVar('finish_code1');
  2219.    
  2220.       if ($Page->getGETVar('a_ids'))
  2221.       {
  2222.           $params['a_ids'] = $Page->getGETVar('a_ids');
  2223.       }
  2224.          unset($content_url);
  2225.       $params['part'] = 'content5';
  2226.       $content_url5 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  2227.    
  2228.       $params['part'] = 'header5';
  2229.       $header_url5 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  2230.    
  2231.       $params['part'] = 'footer5';
  2232.       $footer_url5 = Navigation::gUrl('/users/admin/reportspack.php', $params);
  2233.    
  2234.       $wkpdf = new WKPDF($content_url5);
  2235.       $wkpdf->footerUrl = $footer_url5;
  2236.       $wkpdf->headerUrl = $header_url5;
  2237.  
  2238.       $wkpdf->marginTop = '35';
  2239.       $wkpdf->marginBottom = '20';
  2240.       $wkpdf->output('S', 'Reports'.$stocktake_id .'\NonScanListReport__' . $stocktake_id .'.pdf');
  2241.    
  2242.      $content_url5= '';
  2243.   }
  2244.   else if ($part == 'content5')
  2245.   {
  2246.       $all_sections = $Page->getPOSTVar('all_sections');
  2247.       if ($all_sections) {
  2248.           $section = null;
  2249.       }
  2250.       else {
  2251.           $secarr = $Navigation->getParam('section');
  2252.           $section = '';
  2253.           if ($secarr && is_array($secarr) && count($secarr) > 0)
  2254.           {
  2255.               foreach($secarr as $s)
  2256.               {
  2257.                   $section.= db::c()->quote($s) . ',';
  2258.               }
  2259.               $section = substr($section, 0, -1);
  2260.           }
  2261.           else
  2262.           {
  2263.               $section = null;
  2264.           }
  2265.       }
  2266.       $sdep = $Navigation->getParam('sdep');
  2267.       $start = $Navigation->getParam('start');
  2268.       $finish = $Navigation->getParam('finish');
  2269.    
  2270.       if (!$sdep || $sdep == '00all')
  2271.       {
  2272.           $sdep = null;
  2273.       }
  2274.       echo $html_begin;
  2275.       echo Report6::stocktakeDetails($stocktake_id);
  2276.       echo Report6::nonScanListReportContent($stocktake_id, $sections);
  2277.       echo $html_end;
  2278.    
  2279.       }
  2280.   else if ($part == 'header5')
  2281.   {
  2282.       echo $html_begin;
  2283.       echo Report6::reportHeader('Non Scan List Report');
  2284.       echo $html_end;
  2285.   }
  2286.   else if ($part == 'footer5')
  2287.   {
  2288.   echo $html_begin;
  2289.   echo "<div class='page_numbers'> &ndash; Page <span class='page'></span> of <span class='topage'></span> &ndash; </div>";
  2290.   echo $html_end;
  2291.   }
  2292.   $x++;
  2293.  
  2294.   }
  2295.  
  2296. if ($x==1) {
  2297.  
  2298. $dir = 'Reports'.$stocktake_id;
  2299.  
  2300. $archive  =  'C:\Temp\Reports'.$stocktake_id.'.zip';
  2301.  
  2302. $zip = new ZipArchive;
  2303. $zip->open($archive, ZipArchive::CREATE);
  2304.  
  2305. $files = scandir($dir);
  2306. unset($files[0], $files[1]);
  2307. foreach ($files as $file) {
  2308. $zip->addFile($dir.'/'.$file);
  2309. }
  2310. $zip->close();
  2311. // $saved_file = $archive;
  2312. // header("Refresh: 2;url=Reports".$stocktake_id.".zip");
  2313. // flush();
  2314.  
  2315. }
  2316. require_once "/swiftmailer/swiftmailer/lib/swift_required.php";
  2317.         //require_once "mime.php";
  2318.         $query = "SELECT s.store_name AS store_name, s.supervisor_name AS supervisor_name ,DATE_FORMAT(s.stocktake_date, '%d/%l/%Y') AS stocktake_date, u.username AS username, u.password AS password
  2319.                         FROM stocktakes s
  2320.                         INNER JOIN users u ON u.company_id = s.sms_customer_id
  2321.                         WHERE stocktake_id = '{$this->stocktake_id}'
  2322.                           ";
  2323.         //echo $query;
  2324.         //die();
  2325.         $result = $this->dbClient->query($query);;
  2326.         $row = $result->fetch(PDO::FETCH_ASSOC);
  2327.         $file = 'C:\Temp\Reports'.$this->stocktake_id.'.zip';
  2328.         $transport = Swift_SmtpTransport::newInstance('lh233.dnsireland.com', 465, 'ssl')
  2329.             ->setUsername('arturl@stocktaking.ie')
  2330.             ->setPassword('leonowicz100%');
  2331.    
  2332.         $from = 'arturl@stocktaking.ie';
  2333.         $to = $emailAddress;
  2334.         //echo $to;
  2335.         //die();
  2336.         //$subject ='Test stocktake summary reports' ;
  2337.         $body = "Hello,\n\nPlease find attached reports for your most recent stocktake carried out at " . $row['store_name'] ."\n\n These reports have also been uploaded to the customer portal and can be accessed by logging in to http://reports.stocktaking.ie/ using the following credentials:\n\nusername:".$row['username']." \n\npassword:".$row['password']."\n\nShould you have any queries, please do not hesitate to contact me.\n\nKind Regards,\n\n ". $row['supervisor_name'] ."\n\n <<HOLDER FOR STOCKTAKING.IE SIGNATURE>>";
  2338.        
  2339.         $mailer = Swift_Mailer::newInstance($transport);
  2340.         $message = Swift_Message::newInstance("".$row['store_name'].",Stocktake Summary Reports, ".$row['stocktake_date']."")
  2341.         ->setFrom(array($from => $row['supervisor_name']))
  2342.   ->setTo(array($to, $to => $row['store_name']))
  2343.   ->setBody($body)
  2344.   ;
  2345.         $message->attach(Swift_Attachment::fromPath($file));
  2346.         //echo $message;
  2347.         $result = $mailer->send($message);
  2348.         echo $result;
  2349.         if ($result != 1) {
  2350.              echo('<p>Error when sending the e-mail.</p>');
  2351.          } else {
  2352.              echo('<h2>Message successfully sent!</h2>');
  2353.          }
  2354.  
  2355. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement