PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/SERVER/reports/BAK_2011-05-19/reportDate_merged.php

https://github.com/joelbrock/is4c_nofc
PHP | 348 lines | 223 code | 48 blank | 77 comment | 20 complexity | ed32b1b713ef1d1e48c58d588b728704 MD5 | raw file
  1. <?php
  2. //
  3. //
  4. // Copyright (C) 2007
  5. // authors: Christof Van Rabenau - Whole Foods Cooperative,
  6. // Joel Brock - People's Food Cooperative
  7. //
  8. // This program is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU General Public License
  10. // as published by the Free Software Foundation; either version 2
  11. // of the License, or (at your option) any later version.
  12. //
  13. // This program 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. // along with this program; if not, write to the Free Software
  20. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. //
  22. //
  23. include('../src/functions.php');
  24. include('reportFunctions.php');
  25. // include('../src/datediff.php');
  26. require_once('../define.conf');
  27. ?>
  28. <HTML>
  29. <BODY BGCOLOR = 'FFCC99' > <font SIZE=2>
  30. <?php
  31. $bgcolor = 'FFCC99';
  32. if (isset($_POST['date'])) {
  33. $date = $_POST['date'];
  34. }
  35. else {
  36. if ($_GET['date']) { $date = $_GET['date']; }
  37. else { $date = date('Y-m-d'); }
  38. }
  39. echo "<font size=+1><b>Sales Report for ".$date."</b></font>";
  40. echo "<br>";
  41. $dateArray = explode("-",$date);
  42. $db_date = date('Y-m-d', mktime(0, 0, 0, $dateArray[1], $dateArray[2], $dateArray[0]));
  43. $year = idate('Y',strtotime($db_date));
  44. if ($db_date == date('Y-m-d')) { $table = 'dtransactions'; }
  45. else {
  46. $result = mysql_query("TRUNCATE " . DB_LOGNAME . ".dlog_tmp");
  47. if (!$result) {
  48. $message = 'Invalid query: ' . mysql_error() . "\n";
  49. die($message);
  50. }
  51. $dlog_table = 'dlog_' . $year;
  52. $query = "INSERT INTO " . DB_LOGNAME . ".dlog_tmp SELECT * FROM " . DB_LOGNAME . ".$dlog_table WHERE DATE(datetime) = '$db_date'";
  53. $result = mysql_query($query);
  54. if (!$result) {
  55. $message = 'Invalid query: ' . mysql_error() . "\n";
  56. die($message);
  57. }
  58. $table = 'dlog_tmp';
  59. }
  60. //////////////////////////////////
  61. //
  62. //
  63. // Let's crunch some numbers...
  64. //
  65. //
  66. //////////////////////////////////
  67. /**
  68. * total sales
  69. * Gross = total of all inventory depts. 1-15 (at ACG)
  70. * Net = Gross + Hash - All discounts - Coupons(IC & MC) - Gift Cert. Tender - Store Charge
  71. */
  72. $grossQ = "SELECT ROUND(sum(total),2) as GROSS_sales
  73. FROM " . DB_LOGNAME . ".$table
  74. WHERE date(datetime) = '$db_date'
  75. AND department <= 35
  76. AND department <> 0
  77. AND trans_subtype NOT IN('IC','MC')
  78. AND trans_status <> 'X'
  79. AND emp_no <> 9999";
  80. // echo $grossQ;
  81. $results = mysql_query($grossQ);
  82. $row = mysql_fetch_row($results);
  83. $gross = $row[0];
  84. /**
  85. * sales of inventory departments
  86. */
  87. if ($gross == 0 || !$gross) $gross = 1; //to prevent division by 0 or division by null in the query below
  88. $inventoryDeptQ = "SELECT t.dept_no ,t.dept_name,ROUND(sum(d.total),2) AS total,ROUND((SUM(d.total)/$gross)*100,2) as pct
  89. FROM " . DB_LOGNAME . ".$table AS d, " . DB_NAME . ".departments AS t
  90. WHERE d.department = t.dept_no
  91. AND date(d.datetime) = '$db_date'
  92. AND d.department <= 20
  93. AND d.department <> 0
  94. AND trans_subtype NOT IN('IC','MC')
  95. AND d.trans_status <> 'X'
  96. AND d.emp_no <> 9999
  97. GROUP BY t.dept_no
  98. ORDER BY t.dept_no";
  99. // $gross = 0;
  100. /**
  101. * Sales for non-inventory departments
  102. */
  103. $noninventoryDeptQ = "SELECT d.department,t.dept_name,ROUND(sum(total),2) as total
  104. FROM " . DB_LOGNAME . ".$table as d, " . DB_NAME . ".departments as t
  105. WHERE d.department = t.dept_no
  106. AND date(d.datetime) = '$db_date'
  107. AND d.department >= 34 AND d.department <= 44
  108. AND d.trans_status <> 'X'
  109. AND d.emp_no <> 9999
  110. GROUP BY t.dept_no
  111. ORDER BY t.dept_no";
  112. /*
  113. * pull tender report.
  114. */
  115. $tendersQ = "SELECT t.TenderName as tender_type,ROUND(-sum(d.total),2) as total,COUNT(*) as count
  116. FROM " . DB_LOGNAME . ".$table as d," . DB_NAME . ".tenders as t
  117. WHERE d.trans_subtype = t.TenderCode
  118. AND date(d.datetime) = '$db_date'
  119. AND d.trans_status <> 'X'
  120. AND d.emp_no <> 9999
  121. GROUP BY t.TenderName";
  122. $instoreQ = "SELECT d.description, COUNT(*) AS ct,SUM(d.total) AS total
  123. FROM " . DB_LOGNAME . ".$table AS d
  124. WHERE DATE(d.datetime) = '$db_date'
  125. AND d.trans_subtype = 'IC'
  126. AND d.trans_status <> 'X'
  127. AND d.emp_no <> 9999
  128. GROUP BY d.description";
  129. $custSvcQ = "SELECT (CASE WHEN d.upc LIKE '%DP%' THEN d.description ELSE s.subdept_name END) AS descrip,COUNT(*) AS ct,SUM(d.total) AS total
  130. FROM " . DB_LOGNAME . ".$table AS d, " . DB_NAME . ".subdeptindex s
  131. WHERE d.upc = s.upc
  132. AND date(d.datetime) = '$db_date'
  133. AND d.department = 40
  134. AND d.trans_status <> 'X'
  135. AND d.emp_no <> 9999
  136. GROUP BY descrip";
  137. $storeChargeQ = "SELECT d.emp_no AS cashier, d.total AS storechg_total
  138. FROM " . DB_LOGNAME . ".$table AS d
  139. WHERE date(d.datetime) = '$db_date'
  140. AND d.trans_subtype = 'MI'
  141. AND d.card_no = 9999
  142. AND d.trans_status <> 'X'
  143. AND d.emp_no <> 9999";
  144. $houseChargeQ = "SELECT COUNT(total) AS housechg_count, ROUND(-SUM(d.total),2) AS housechg_total
  145. FROM " . DB_LOGNAME . ".$table AS d
  146. WHERE d.trans_subtype = 'MI'
  147. AND card_no != 9999
  148. AND d.trans_status <> 'X'
  149. AND date(d.datetime) = '$db_date'
  150. AND d.emp_no <> 9999";
  151. $transCountQ = "SELECT COUNT(d.total) as transactionCount
  152. FROM " . DB_LOGNAME . ".$table AS d
  153. WHERE date(d.datetime) = '$db_date'
  154. AND d.trans_status <> 'X'
  155. AND d.emp_no <> 9999
  156. AND d.upc = 'DISCOUNT'";
  157. $transCountR = mysql_query($transCountQ);
  158. $row = mysql_fetch_row($transCountR);
  159. $count = $row[0];
  160. $basketSizeQ = "SELECT ROUND(($gross/$count),2) AS basket_size";
  161. /**
  162. * Sales of equity
  163. */
  164. $sharePaymentsQ = "SELECT d.emp_no, d.card_no, 'MEMBER SHARE PMT',ROUND(d.total,2) as total
  165. FROM " . DB_LOGNAME . ".$table as d
  166. WHERE date(d.datetime) = '$db_date'
  167. AND d.department = 36
  168. AND d.trans_status <> 'X'
  169. AND d.emp_no <> 9999
  170. GROUP BY d.card_no";
  171. /*
  172. $shareCountQ = "SELECT COUNT(total) AS peopleshare_count
  173. FROM " . DB_LOGNAME . ".$table
  174. WHERE date(datetime) = '$db_date'
  175. AND description = 'MEMBERSHIP EQUITY'
  176. AND trans_status <> 'X'
  177. AND emp_no <> 9999";
  178. $shareCountR = mysql_query($shareCountQ);
  179. $row = mysql_fetch_row($shareCountR);
  180. $shareCount = $row[0];
  181. */
  182. /**
  183. * Discounts by member type;
  184. */
  185. $percentsQ = "SELECT c.discount AS volunteer_discount,(ROUND(SUM(d.unitPrice),2)) AS totals
  186. FROM " . DB_LOGNAME . ".$table AS d LEFT JOIN " . DB_NAME . ".custdata AS c
  187. ON d.card_no = c.CardNo
  188. WHERE date(d.datetime) = '$db_date'
  189. AND c.staff IN(3,4,6)
  190. AND d.voided = '5'
  191. AND d.trans_status <> 'X'
  192. AND d.emp_no <> 9999
  193. GROUP BY c.discount
  194. WITH ROLLUP";
  195. $memstatus = "SELECT m.memDesc as memStatus,ROUND(SUM(d.total),2) AS Sales,ROUND((SUM(d.total)/$gross*100),2) AS pct
  196. FROM " . DB_LOGNAME . ".$table d, " . DB_NAME . ".memtype m
  197. WHERE d.memType = m.memtype
  198. AND date(d.datetime) = '$db_date'
  199. AND d.trans_type IN('I','D')
  200. AND d.trans_status <>'X'
  201. AND d.department <= 35 AND d.department <> 0
  202. AND d.upc <> 'DISCOUNT'
  203. AND d.emp_no <> 9999
  204. GROUP BY m.memtype";
  205. $memtype = "SELECT s.staff_desc as memType,ROUND(SUM(d.total),2) AS Sales,ROUND((SUM(d.total)/$gross*100),2) AS pct
  206. FROM " . DB_LOGNAME . ".$table d, " . DB_NAME . ".staff s
  207. WHERE d.staff = s.staff_no
  208. AND date(d.datetime) = '$db_date'
  209. AND d.trans_type IN('I','D')
  210. AND d.trans_status <>'X'
  211. AND d.department <= 35 AND d.department <> 0
  212. AND d.upc <> 'DISCOUNT'
  213. AND d.emp_no <> 9999
  214. GROUP BY s.staff_no";
  215. $patronage = "SELECT emp_no, card_no, description, total
  216. FROM " . DB_LOGNAME . ".$table
  217. WHERE date(datetime) = '$db_date'
  218. AND trans_subtype = 'PT'
  219. AND emp_no <> 9999 AND trans_status <> 'X'
  220. ORDER BY card_no";
  221. ////////////////////////////
  222. //
  223. //
  224. // NOW....SPIT IT ALL OUT....
  225. //
  226. //
  227. ////////////////////////////
  228. // echo $db_date . '<br>';
  229. echo '<font size = 2>';
  230. echo '<h2>Sales - Gross & NET</h2>';
  231. include('net.php');
  232. echo "<table border=0><tr><td><b>sales (gross) total</b></td><td align=right><b>".money_format('%n',$gross)."</b></td></tr>";
  233. echo "<tr><td>non-inv total</td><td align=right>".money_format('%n',$hash)."</td></tr>";
  234. echo "<tr><td>totalDisc</td><td align=right>".money_format('%n',$totalDisc)."</td></tr>";
  235. echo "<tr><td>coupon & gift cert. tenders</td><td align=right>".money_format('%n',$coupons)."</td></tr>";
  236. echo "<tr><td>store charges</td><td align=right>".money_format('%n',$strchg)."</td></tr>";
  237. echo "<tr><td>rcvd/accts</td><td align=right>".money_format('%n',$RA)."</td></tr>";
  238. //echo "<tr><td>mkt EBT & chg pmts</td><td align=right>".money_format('%n',$other)."</td></tr>";
  239. echo "<tr><td>&nbsp;</td><td align=right>+___________</td></tr>";
  240. echo "<tr><b><td>net total</td><td align=right>".money_format('%n',$net)."</td></b></tr>";
  241. echo "<tr><b><td>cashier net total</td><td align=right>".money_format('%n',$cnet)."</td></tr>";
  242. echo "<tr><td>D2 (for kris)</td><td align=right>". money_format('%n',$d2) ."</td></tr></table>";
  243. echo '------------------------------<br>';
  244. echo '<h2>Sales by Inventory Dept.</h2>';
  245. select_to_table($inventoryDeptQ,0,$bgcolor);
  246. deptTotals('Grocery',$gross,$table,$db_date,$db_date,'2,3,6,7,8,9',$bgcolor);
  247. deptTotals('Produce',$gross,$table,$db_date,$db_date,'1,14,15',$bgcolor);
  248. deptTotals('Nonfoods',$gross,$table,$db_date,$db_date,'4,5,10',$bgcolor);
  249. echo '<br />';
  250. echo '<h2>Sales by Non-Inventory Dept.</h2>';
  251. select_to_table($noninventoryDeptQ,0,$bgcolor);
  252. echo '------------------------------<br>';
  253. echo '<h2>Tender Report</h2>';
  254. select_to_table($tendersQ,0,$bgcolor); // sales by tender type
  255. echo "<h2>Instore Coupon Breakdown</h2>";
  256. select_to_table($instoreQ,0,$bgcolor); // instore coupon breakdown
  257. echo "<h2>Customer Services Breakdown</h2>";
  258. select_to_table($custSvcQ,0,$bgcolor); // customer svc breakdown
  259. echo "<h2>Store Charge Breakdown</h2>";
  260. select_to_table($storeChargeQ,0,$bgcolor); // store charge breakdown
  261. // select_to_table($houseChargeQ,0,$bgcolor); // house charges
  262. select_to_table($transCountQ,0,$bgcolor); // transaction count
  263. select_to_table($basketSizeQ,0,$bgcolor); // basket size
  264. echo '------------------------------<br>';
  265. echo '<h2>Membership & Discount Totals</h2><br>';
  266. echo "<table border=0><font size=2>";
  267. echo "<tr><td>staff total</td><td align=right>".money_format('%n',$staff_total)."</td></tr>";
  268. echo "<tr><td>hoo total</td><td align=right>".money_format('%n',$hoo_total)."</td></tr>";
  269. echo "<tr><td>benefits total</td><td align=right>".money_format('%n',$bene_total)."</td></tr>";
  270. echo "<tr><td>bod total</td><td align=right>".money_format('%n',$bod_total)."</td></tr>";
  271. echo "<tr><td>MAD coupon ($MAD_num)</td><td align=right>".money_format('%n',$MADcoupon)."</td></tr>";
  272. echo "<tr><td>Non-Member Discount ($NMD_num)</td><td align=right>".money_format('%n',$NMD_total)."</td></tr>";
  273. echo "<tr><td>foodforall total ($ffa_num)</td><td align=right>".money_format('%n',$foodforall)."</td></tr>";
  274. if ($tenDisc != 0) {
  275. echo "<tr><td>10% on the 10th Discount</td><td align=right>".money_format('%n',$tenDisc)."</td></tr>";
  276. }
  277. if ($SSDD_num != 0) {
  278. echo "<tr><td><i>SPECIAL</i> discount ($SSDD_num)</td><td align=right>".money_format('%n',$SSDdiscount2)."</td></tr>";
  279. }
  280. if (strtotime($db_date) <= strtotime($dbChangeDate)) {
  281. echo "<tr><td>Uncaught Discount/FFA</td><td align=right>".money_format('%n',$miscDisc)."</td></tr>";
  282. } else {
  283. echo "<tr><td>Manual Member Discounts</td><td align=right>".money_format('%n',$miscDisc)."</td></tr>";
  284. }
  285. echo "<tr><td>&nbsp;</td><td align=right>+___________</td></tr>";
  286. echo "<tr><td><b>total discount</td><td align=right>".money_format('%n',$totalDisc)."</b></td></tr></font></table>";
  287. //select_to_table($percentsQ,0,$bgcolor); // discounts awarded by percent
  288. //select_to_table($memstatus,0,$bgcolor);
  289. //select_to_table($memtype,0,$bgcolor);
  290. echo '<h2>Share Payments</h2><br>';
  291. select_to_table($sharePaymentsQ,0,$bgcolor); // peopleshare payments
  292. //echo '<b>Share count = '.$shareCount.'</b>'; // peopleshare count
  293. echo '<h2>Patronage Redemption</h2><br>';
  294. select_to_table($patronage,0,$bgcolor); // patronage redemption
  295. echo '</font>';
  296. ?>
  297. </font>
  298. </body>
  299. </html>