PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/fannie/reports/DDD/SmartDDDReport.php

https://github.com/CORE-POS/IS4C
PHP | 254 lines | 209 code | 20 blank | 25 comment | 24 complexity | be3b84a6b9c8c6e745ed6d1a628d77c5 MD5 | raw file
  1. <?php
  2. /*******************************************************************************
  3. Copyright 2014 Whole Foods Co-op
  4. This file is part of CORE-POS.
  5. CORE-POS is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. CORE-POS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. in the file license.txt along with IT CORE; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. *********************************************************************************/
  17. include(dirname(__FILE__) . '/../../config.php');
  18. if (!class_exists('FannieAPI')) {
  19. include(__DIR__ . '/../../classlib2.0/FannieAPI.php');
  20. }
  21. class SmartDDDReport extends FannieReportPage
  22. {
  23. protected $report_cache = 'none';
  24. protected $title = "Fannie : Extended DDD Report";
  25. protected $header = "Extended Report";
  26. protected $required_fields = array('date1', 'date2');
  27. public $themed = true;
  28. private $mode = 'PLU';
  29. public function preprocess()
  30. {
  31. $this->mode = FormLib::get('sort', 'PLU');
  32. return parent::preprocess();
  33. }
  34. public function report_description_content()
  35. {
  36. if ($this->report_format != 'html') {
  37. return array();
  38. }
  39. $url = $this->config->get('URL');
  40. $this->addScript($url . 'src/javascript/jquery.js');
  41. $this->addScript($url . 'src/javascript/jquery-ui.js');
  42. $this->addCssFile($url . 'src/javascript/jquery-ui.css');
  43. $dates_form = '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
  44. foreach ($_GET as $key => $value) {
  45. if ($key != 'date1' && $key != 'date2' && $key != 'store') {
  46. if (is_array($value)) {
  47. foreach ($value as $v) {
  48. $dates_form .= sprintf('<input type="hidden" name="%s[]" value="%s" />', $key, $v);
  49. }
  50. } else {
  51. $dates_form .= sprintf('<input type="hidden" name="%s" value="%s" />', $key, $value);
  52. }
  53. }
  54. }
  55. foreach ($_POST as $key => $value) {
  56. if ($key != 'date1' && $key != 'date2' && $key != 'store') {
  57. if (is_array($value)) {
  58. foreach ($value as $v) {
  59. $dates_form .= sprintf('<input type="hidden" name="%s[]" value="%s" />', $key, $v);
  60. }
  61. } else {
  62. $dates_form .= sprintf('<input type="hidden" name="%s" value="%s" />', $key, $value);
  63. }
  64. }
  65. }
  66. $stores = FormLib::storePicker();
  67. $dates_form .= '
  68. <label>Start Date</label>
  69. <input class="date-field" type="text" name="date1" value="' . FormLib::get('date1') . '" />
  70. <label>End Date</label>
  71. <input class="date-field" type="text" name="date2" value="' . FormLib::get('date2') . '" />
  72. <input type="hidden" name="excel" value="" id="excel" />
  73. ' . $stores['html'] . '
  74. <button type="submit" onclick="$(\'#excel\').val(\'\');return true;">Change Dates</button>
  75. <button type="submit" onclick="$(\'#excel\').val(\'csv\');return true;">Download</button>
  76. </form>
  77. <style type="text/css">
  78. .ui-datepicker {
  79. z-index: 999 !important;
  80. }
  81. </style>';
  82. $this->add_onload_command("\$('.date-field').datepicker({dateFormat:'yy-mm-dd'});");
  83. return array($dates_form);
  84. }
  85. public function fetch_report_data()
  86. {
  87. $dbc = $this->connection;
  88. $dbc->selectDB($this->config->get('OP_DB'));
  89. $query = '';
  90. $from_where = FormLib::standardItemFromWhere();
  91. $dlog = DTransactionsModel::selectDlog($this->form->date1, $this->form->date2);
  92. $dtrans = DTransactionsModel::selectDTrans($this->form->date1, $this->form->date2);
  93. $from_where['query'] = str_replace($dlog, $dtrans, $from_where['query']);
  94. $from_where['query'] = str_replace('tdate', 'datetime', $from_where['query']);
  95. $query = "
  96. SELECT t.upc,
  97. COALESCE(p.brand, '') AS brand,
  98. CASE WHEN p.description IS NULL THEN t.description ELSE p.description END as description, "
  99. . DTrans::sumQuantity('t')." as qty,
  100. SUM(t.cost) AS total,
  101. t.department,
  102. d.dept_name,
  103. m.super_name,
  104. v.vendorName AS distributor,
  105. i.sku
  106. " . $from_where['query'] . "
  107. AND trans_status='Z'
  108. AND emp_no <> 9999
  109. AND register_no <> 99
  110. AND t.upc <> '0'
  111. GROUP BY t.upc,
  112. COALESCE(p.brand, ''),
  113. CASE WHEN p.description IS NULL THEN t.description ELSE p.description END,
  114. t.department,
  115. d.dept_name,
  116. m.super_name,
  117. v.vendorName
  118. ORDER BY SUM(t.total) DESC";
  119. $prep = $dbc->prepare($query);
  120. $saleQ = str_replace("='Z'", "<>'Z'", $query);
  121. $saleQ = str_replace("SUM(t.cost)", "SUM(t.total)", $saleQ);
  122. $saleP = $dbc->prepare($saleQ);
  123. try {
  124. $result = $dbc->execute($prep, $from_where['args']);
  125. $saleR = $dbc->execute($saleP, $from_where['args']);
  126. } catch (Exception $ex) {
  127. // MySQL 5.6 GROUP BY problem
  128. return array();
  129. }
  130. $sales = array();
  131. while ($row = $dbc->fetchRow($saleR)) {
  132. $upc = $row['upc'];
  133. if ($upc == '0068031488891') echo $row['total'];
  134. $sales[$upc] = array(
  135. 'qty' => $row['qty'],
  136. 'ttl' => $row['total'],
  137. );
  138. }
  139. $data = array();
  140. while ($row = $dbc->fetch_row($result)) {
  141. $data[] = array(
  142. $row['upc'],
  143. $row['brand'],
  144. $row['description'],
  145. sprintf('%.2f', $row['qty']),
  146. sprintf('%.2f', $row['total']),
  147. //sprintf('%.2f', isset($sales[$row['upc']]['qty']) ? $sales[$row['upc']]['qty'] : 0),
  148. //sprintf('%.2f', isset($sales[$row['upc']]['ttl']) ? $sales[$row['upc']]['ttl'] : 0),
  149. $row['department'],
  150. $row['dept_name'],
  151. $row['super_name'],
  152. $row['distributor'],
  153. $row['sku'] == $row['upc'] ? '' : $row['sku'],
  154. );
  155. }
  156. return $data;
  157. }
  158. public function calculate_footers($data)
  159. {
  160. $this->report_headers = array('UPC','Brand','Description','Shrink Qty','Shrink $',
  161. //'Sales Qty', 'Sales $',
  162. 'Dept#','Department','Super','Vendor', 'SKU');
  163. $this->sort_column = 4;
  164. $this->sort_direction = 1;
  165. $sumQty = 0.0;
  166. $sumSales = 0.0;
  167. foreach($data as $row) {
  168. $sumQty += $row[3];
  169. $sumSales += $row[4];
  170. }
  171. return array('Total',null,null,$sumQty,$sumSales,'',null,null,null);
  172. }
  173. public function form_content()
  174. {
  175. ob_start();
  176. ?>
  177. <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  178. <div class="row">
  179. <?php echo FormLib::standardItemFields(); ?>
  180. <?php echo FormLib::standardDateFields(); ?>
  181. </div>
  182. <div class="row form-horizontal">
  183. <div class="form-group">
  184. <label class="col-sm-1 control-label">Store</label>
  185. <div class="col-sm-2">
  186. <?php $s = FormLib::storePicker(); echo $s['html']; ?>
  187. </div>
  188. <label class="col-sm-2 control-label">
  189. <input type="checkbox" name="excel" value="csv" />
  190. Excel
  191. </label>
  192. </div>
  193. </div>
  194. <p>
  195. <button type="submit" class="btn btn-default btn-core">Get Report</button>
  196. <button type="reset" class="btn btn-default btn-reset">Reset Form</button>
  197. </p>
  198. </form>
  199. <?php
  200. return ob_get_clean();
  201. }
  202. public function helpContent()
  203. {
  204. return '<p>
  205. This tool performs a variety of different movement reports.
  206. Select the set of items by department, brand, vendor, or
  207. like code using the appropriate tabs.
  208. </p>
  209. <p>
  210. Identifying items by department provides a super department
  211. choice and/or a range of departments and/or a range of
  212. sub departments. Identifying by brand allows either a text
  213. brand name or a numeric UPC prefix. Vendor simply allows
  214. a choice of vendor. Like code provides a range of like
  215. codes.
  216. </p>
  217. <p>
  218. After choosing a method for identifying a set of items
  219. and defining a date range, there is also an option to choose
  220. how the report is summed. PLU will show one row for each
  221. item sold. Date will show one row for each day in the period.
  222. Department will show one row for each department that
  223. has sales. Weekday is similar to date but combines each Monday,
  224. Tuesday, etc into a single row.
  225. </p>';
  226. }
  227. }
  228. FannieDispatch::conditionalExec();