PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/SERVER/dev/reports/subdeptReport.php

https://github.com/joelbrock/is4c_nofc
PHP | 224 lines | 136 code | 35 blank | 53 comment | 12 complexity | 02021bbe93a64d2505cd20dbded31b49 MD5 | raw file
  1. <?php
  2. // /*******************************************************************************
  3. //
  4. // Copyright 2007 People's Food Co-op, Portland, Oregon.
  5. //
  6. // This file is part of Fannie.
  7. //
  8. // IS4C is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation; either version 2 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // IS4C is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // in the file license.txt along with IS4C; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. //
  22. // *********************************************************************************/
  23. //
  24. include('../src/functions.php');
  25. // mysql_select_db('DB_NAME',$dbc);
  26. require_once('../define.conf');
  27. if (isset($_POST['submit'])) {
  28. echo "<html><head>
  29. <script type=\"text/javascript\" src=\"../src/tablesort.js\"></script>
  30. <link rel='stylesheet' href='../src/tablesort.css' type='text/css' />
  31. <link rel='stylesheet' href='../src/style.css' type='text/css' /></head>";
  32. if (isset($_GET['sort'])) {
  33. foreach ($_GET AS $key => $value) {
  34. $$key = $value;
  35. //echo $key ." : " . $value."<br>";
  36. }
  37. } else {
  38. foreach ($_POST AS $key => $value) {
  39. $$key = $value;
  40. }
  41. }
  42. echo "<body>";
  43. $today = date("F d, Y");
  44. if (isset($allDepts)) {
  45. $deptArray = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,40";
  46. $arrayName = "ALL DEPARTMENTS";
  47. } else {
  48. if (isset($_POST['dept'])) {$deptArray = implode(",",$_POST['dept']);}
  49. elseif (isset($_GET['dept'])) {$deptArray = $_GET['dept'];}
  50. $arrayName = $deptArray;
  51. }
  52. // Check year in query, match to a dlog table
  53. $year1 = idate('Y',strtotime($date1));
  54. $year2 = idate('Y',strtotime($date2));
  55. if ($year1 != $year2) {
  56. echo "<div id='alert'><h4>Reporting Error</h4>
  57. <p>Fannie cannot run reports across multiple years.<br>
  58. Please retry your query.</p></div>";
  59. exit();
  60. }
  61. //elseif ($year1 == date('Y')) { $table = 'dtransactions'; }
  62. else { $table = 'dlog_' . $year1; }
  63. // echo "<center><h1>Subdepartment Report</h1>
  64. // <h4>Sales by category for $date1 thru $date2</h4></center>";
  65. // echo "<p><font size=-1>Report sorted by " . $order_name . " on " . $today . "</br>Department range: " . $arrayName;
  66. // echo "</font>";
  67. $grossQ = "SELECT ROUND(sum(total),2) as GROSS_sales
  68. FROM " . DB_LOGNAME . ".$table
  69. WHERE date(datetime) >= '$date1' AND date(datetime) <= '$date2'
  70. AND department IN($deptArray)
  71. AND trans_status <> 'X'
  72. AND emp_no <> 9999";
  73. // echo "<br>".$grossQ."<br>";
  74. $grossR = mysql_query($grossQ);
  75. $row = mysql_fetch_row($grossR);
  76. $gross = $row[0];
  77. function getmicrotime(){
  78. list($usec, $sec) = explode(" ",microtime());
  79. return ((float)$usec + (float)$sec);
  80. }
  81. $time_start = getmicrotime();
  82. // $subdeptQ = "SELECT s.subdept_name AS subdept,
  83. // s.dept_name AS dept,
  84. // ROUND(SUM(t.quantity),2) as qty,
  85. // ROUND(SUM(t.total),2) as total
  86. // FROM " . DB_LOGNAME . ".$table t, DB_NAME.subdeptindex s
  87. // WHERE t.upc = s.upc
  88. // AND date(t.datetime) >= '$date1' AND date(t.datetime) <= '$date2'
  89. // AND t.trans_type <> 'D'
  90. // AND t.department IN($deptArray)
  91. // GROUP BY s.subdept_name";
  92. $subdeptQ = "SELECT s.subdept_name AS subdept,
  93. d.dept_name AS dept,
  94. ROUND(SUM(t.quantity),2) as qty,
  95. ROUND(SUM(t.total),2) as total
  96. FROM " . DB_LOGNAME . ".$table t, " . DB_NAME . "." . PRODUCTS_TBL . " p, " . DB_NAME . ".departments d, " . DB_NAME . ".subdepts s
  97. WHERE t.upc = p.upc AND p.department = d.dept_no AND p.subdept = s.subdept_no
  98. AND date(t.datetime) >= '$date1' AND date(t.datetime) <= '$date2'
  99. AND t.trans_type <> 'D'
  100. AND t.department IN($deptArray)
  101. GROUP BY s.subdept_name";
  102. $result = mysql_query($subdeptQ);
  103. $num = mysql_num_rows($result);
  104. // echo $subdeptQ;
  105. if (!$result) {
  106. $message = 'Invalid query: ' . mysql_error() . "\n";
  107. $message .= 'Whole query: ' . $subdeptQ;
  108. die($message);
  109. }
  110. $time_end = getmicrotime();
  111. $time_sec = ($time_end - $time_start);
  112. $time_min = ($time_end - $time_start) / 60;
  113. echo "<center><h1>Subdepartment Sales Report</h1>
  114. <h3>" . strftime('%D', strtotime($date1)) . " thru " . strftime('%D', strtotime($date2)) . "</h3></center>";
  115. echo "<table id=\"output\" cellpadding=0 cellspacing=0 border=0 class=\"sortable-onload-3 rowstyle-alt colstyle-alt\">\n
  116. <caption>Dept.: ".$arrayName.". Yielded (".$num.") results. Run on " . date('n/j/y \a\t h:i A') . "</caption>\n
  117. <thead>\n
  118. <tr>\n
  119. <th class=\"sortable-text\">Subdepartment</th>\n
  120. <th class=\"sortable-text\">Department</th>\n
  121. <th class=\"sortable-numeric favour-reverse\">Qty.</th>\n
  122. <th class=\"sortable-currency favour-reverse\">Total</th>\n
  123. </tr>\n
  124. </thead>\n
  125. <tbody>\n";
  126. while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
  127. echo "<td align=left>" . $row["subdept"] . "</td>
  128. <td align=left>" . $row["dept"] . "</td>
  129. <td align=right>" . $row["qty"] . "</td>
  130. <td align=right>" . money_format('%n',$row["total"]) . "</td>";
  131. echo "</tr>";
  132. }
  133. echo '</table>';
  134. echo "<center>Query executed in <b>" . number_format($time_min,2) . "</b> minutes (<b>" . number_format($time_sec,2) . "</b> seconds)</center>";
  135. //
  136. // PHP INPUT DEBUG SCRIPT -- very helpful!
  137. //
  138. // function debug_p($var, $title)
  139. // {
  140. // print "<p>$title</p><pre>";
  141. // print_r($var);
  142. // print "</pre>";
  143. // }
  144. //
  145. // debug_p($_REQUEST, "all the data coming in");
  146. } else {
  147. $page_title = 'Fannie - Reporting';
  148. $header = 'Subdepartment Report';
  149. include('../src/header.php');
  150. echo '<script src="../src/putfocus.js" language="javascript"></script>
  151. <form method="post" action="subdeptReport.php" target="_blank">
  152. <table border="0" cellspacing="5" cellpadding="5">
  153. <tr>
  154. <td align="right">
  155. <p><b>Date Start:</b></p>
  156. <p><b>End:</b></p>
  157. </td>
  158. <td>
  159. <div class="date"><p><input type="text" name="date1" class="datepicker" />&nbsp;&nbsp;*</p></div>
  160. <div class="date"><p><input type="text" name="date2" class="datepicker" />&nbsp;&nbsp;*</p></div>
  161. </td>
  162. <td>&nbsp;</td>
  163. </tr>
  164. </table>
  165. <table border="0" cellspacing="5" cellpadding="5">
  166. <tr valign=top>';
  167. include('../src/departments.php');
  168. echo '</tr>
  169. <tr>
  170. <td>&nbsp;</td>
  171. <td>
  172. <input type=submit name=submit value="Submit">
  173. </td>
  174. <td>
  175. <input type=reset name=reset value="Start Over">
  176. </td>
  177. </tr>
  178. </table>
  179. </form>';
  180. include('../src/footer.php');
  181. }
  182. ?>
  183. <script>
  184. $(function() {
  185. $( ".datepicker" ).datepicker({
  186. dateFormat: 'yy-mm-dd'
  187. });
  188. });
  189. </script>