PageRenderTime 38ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/SERVER/reports/reportDate.php

https://github.com/joelbrock/is4c_nofc
PHP | 391 lines | 244 code | 52 blank | 95 comment | 24 complexity | aba23bc7f43f766936276be04bb40e5e 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. $strdate = strtotime($date);
  40. $longdate = strftime('%A %B %e, %Y',$strdate);
  41. echo "<h1>Sales Report for ".$longdate."</h1>";
  42. // echo "<br>";
  43. $dateArray = explode("-",$date);
  44. $db_date = date('Y-m-d', mktime(0, 0, 0, $dateArray[1], $dateArray[2], $dateArray[0]));
  45. $year = idate('Y',strtotime($db_date));
  46. if ($db_date == date('Y-m-d')) { $table = 'dtransactions'; }
  47. else {
  48. $result = mysql_query("TRUNCATE " . DB_LOGNAME . ".dlog_tmp");
  49. if (!$result) {
  50. $message = 'Invalid query: ' . mysql_error() . "\n";
  51. die($message);
  52. }
  53. $dlog_table = 'dlog_' . $year;
  54. $query = "INSERT INTO " . DB_LOGNAME . ".dlog_tmp SELECT * FROM " . DB_LOGNAME . ".$dlog_table WHERE DATE(datetime) = '$db_date'";
  55. $result = mysql_query($query);
  56. if (!$result) {
  57. $message = 'Invalid query: ' . mysql_error() . "\n";
  58. die($message);
  59. }
  60. $table = 'dlog_tmp';
  61. }
  62. //////////////////////////////////
  63. //
  64. //
  65. // Let's crunch some numbers...
  66. //
  67. //
  68. //////////////////////////////////
  69. $gross = gross($table, $db_date, $db_date);
  70. $hash = hash_total($table, $db_date, $db_date);
  71. $coupons = coupon_total($table, $db_date, $db_date);
  72. $strchg = charge_total($table, $db_date, $db_date);
  73. $RA = RA_total($table, $db_date, $db_date);
  74. $staff_total = staff_total($table, $db_date, $db_date);
  75. $hoo_total = hoo_total($table, $db_date, $db_date);
  76. $bene_total = bene_total($table, $db_date, $db_date);
  77. $bod_total = bod_total($table, $db_date, $db_date);
  78. $misc_total = miscDisc($table, $db_date, $db_date);
  79. $tenDisc = tenDisc($table, $db_date, $db_date);
  80. extract(MADcoupon($table, $db_date, $db_date));
  81. extract(foodforall($table, $db_date, $db_date));
  82. extract(SSDdiscount($table, $db_date, $db_date));
  83. // extract(staff_total($table, $db_date, $db_date));
  84. extract(NMDdiscount($table, $db_date, $db_date));
  85. $pt_total = patronage_total($table, $db_date, $db_date);
  86. $totalDisc = discount_total($table, $db_date, $db_date);
  87. $net = net_total($table, $db_date, $db_date);
  88. /**
  89. * total sales
  90. * Gross = total of all inventory depts. 1-15 (at ACG)
  91. * Net = Gross + Hash - All discounts - Coupons(IC & MC) - Gift Cert. Tender - Store Charge
  92. */
  93. //
  94. // $grossQ = "SELECT ROUND(sum(total),2) as GROSS_sales
  95. // FROM " . DB_LOGNAME . ".$table
  96. // WHERE date(datetime) = '$db_date'
  97. // AND department <= 35
  98. // AND department <> 0
  99. // AND trans_subtype NOT IN('IC','MC')
  100. // AND trans_status <> 'X'
  101. // AND emp_no <> 9999";
  102. //
  103. // // echo $grossQ;
  104. //
  105. // $results = mysql_query($grossQ);
  106. // $row = mysql_fetch_row($results);
  107. // $gross = $row[0];
  108. /**
  109. * sales of inventory departments
  110. */
  111. if ($gross == 0 || !$gross) $gross = 1; //to prevent division by 0 or division by null in the query below
  112. $inventoryDeptQ = "SELECT t.dept_no ,t.dept_name,ROUND(sum(d.total),2) AS total,ROUND((SUM(d.total)/$gross)*100,2) as pct
  113. FROM " . DB_LOGNAME . ".$table AS d, " . DB_NAME . ".departments AS t
  114. WHERE d.department = t.dept_no
  115. AND date(d.datetime) = '$db_date'
  116. AND d.department <= 20
  117. AND d.department <> 0
  118. AND trans_subtype NOT IN('IC','MC')
  119. AND d.trans_status <> 'X'
  120. AND d.emp_no <> 9999
  121. GROUP BY t.dept_no
  122. ORDER BY t.dept_no";
  123. // $gross = 0;
  124. /**
  125. * Sales for non-inventory departments
  126. */
  127. $noninventoryDeptQ = "SELECT d.department,t.dept_name,ROUND(sum(total),2) as total
  128. FROM " . DB_LOGNAME . ".$table as d, " . DB_NAME . ".departments as t
  129. WHERE d.department = t.dept_no
  130. AND date(d.datetime) = '$db_date'
  131. AND d.department >= 33
  132. AND d.trans_status <> 'X'
  133. AND d.emp_no <> 9999
  134. GROUP BY t.dept_no
  135. ORDER BY t.dept_no";
  136. /*
  137. * pull tender report.
  138. */
  139. $tendersQ = "SELECT t.TenderName as tender_type,ROUND(-sum(d.total),2) as total,COUNT(*) as count
  140. FROM " . DB_LOGNAME . ".$table as d," . DB_NAME . ".tenders as t
  141. WHERE d.trans_subtype = t.TenderCode
  142. AND date(d.datetime) = '$db_date'
  143. AND d.trans_status <> 'X'
  144. AND d.emp_no <> 9999
  145. GROUP BY t.TenderName";
  146. $instoreQ = "SELECT d.description, COUNT(*) AS ct,SUM(d.total) AS total
  147. FROM " . DB_LOGNAME . ".$table AS d
  148. WHERE DATE(d.datetime) = '$db_date'
  149. AND d.trans_subtype = 'IC'
  150. AND d.trans_status <> 'X'
  151. AND d.emp_no <> 9999
  152. GROUP BY d.description";
  153. $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
  154. FROM " . DB_LOGNAME . ".$table AS d, " . DB_NAME . ".subdeptindex s
  155. WHERE d.upc = s.upc
  156. AND date(d.datetime) = '$db_date'
  157. AND d.department = 40
  158. AND d.trans_status <> 'X'
  159. AND d.emp_no <> 9999
  160. GROUP BY descrip";
  161. $storeChargeQ = "SELECT d.emp_no AS cashier, d.total AS storechg_total
  162. FROM " . DB_LOGNAME . ".$table AS d
  163. WHERE date(d.datetime) = '$db_date'
  164. AND d.trans_subtype = 'MI'
  165. AND d.card_no = 9999
  166. AND d.trans_status <> 'X'
  167. AND d.emp_no <> 9999";
  168. $houseChargeQ = "SELECT COUNT(total) AS housechg_count, ROUND(-SUM(d.total),2) AS housechg_total
  169. FROM " . DB_LOGNAME . ".$table AS d
  170. WHERE d.trans_subtype = 'MI'
  171. AND card_no != 9999
  172. AND d.trans_status <> 'X'
  173. AND date(d.datetime) = '$db_date'
  174. AND d.emp_no <> 9999";
  175. $transCountQ = "SELECT COUNT(d.total) as transactionCount
  176. FROM " . DB_LOGNAME . ".$table AS d
  177. WHERE date(d.datetime) = '$db_date'
  178. AND d.trans_status <> 'X'
  179. AND d.emp_no <> 9999
  180. AND d.upc = 'DISCOUNT'";
  181. $transCountR = mysql_query($transCountQ);
  182. $row = mysql_fetch_row($transCountR);
  183. $count = $row[0];
  184. $basketSizeQ = "SELECT ROUND(($gross/$count),2) AS basket_size";
  185. /**
  186. * Sales of equity
  187. */
  188. $sharePaymentsQ = "SELECT d.emp_no, d.card_no, 'MEMBER SHARE PMT',ROUND(d.total,2) as total
  189. FROM " . DB_LOGNAME . ".$table as d
  190. WHERE date(d.datetime) = '$db_date'
  191. AND d.department = 36
  192. AND d.trans_status <> 'X'
  193. AND d.emp_no <> 9999
  194. GROUP BY d.card_no";
  195. /*
  196. $shareCountQ = "SELECT COUNT(total) AS peopleshare_count
  197. FROM " . DB_LOGNAME . ".$table
  198. WHERE date(datetime) = '$db_date'
  199. AND description = 'MEMBERSHIP EQUITY'
  200. AND trans_status <> 'X'
  201. AND emp_no <> 9999";
  202. $shareCountR = mysql_query($shareCountQ);
  203. $row = mysql_fetch_row($shareCountR);
  204. $shareCount = $row[0];
  205. */
  206. /**
  207. * Discounts by member type;
  208. */
  209. $percentsQ = "SELECT c.discount AS volunteer_discount,(ROUND(SUM(d.unitPrice),2)) AS totals
  210. FROM " . DB_LOGNAME . ".$table AS d LEFT JOIN " . DB_NAME . ".custdata AS c
  211. ON d.card_no = c.CardNo
  212. WHERE date(d.datetime) = '$db_date'
  213. AND c.staff IN(3,4,6)
  214. AND d.voided = '5'
  215. AND d.trans_status <> 'X'
  216. AND d.emp_no <> 9999
  217. GROUP BY c.discount
  218. WITH ROLLUP";
  219. $memstatus = "SELECT m.memDesc as memStatus,ROUND(SUM(d.total),2) AS Sales,ROUND((SUM(d.total)/$gross*100),2) AS pct
  220. FROM " . DB_LOGNAME . ".$table d, " . DB_NAME . ".memtype m
  221. WHERE d.memType = m.memtype
  222. AND date(d.datetime) = '$db_date'
  223. AND d.trans_type IN('I','D')
  224. AND d.trans_status <>'X'
  225. AND d.department <= 35 AND d.department <> 0
  226. AND d.upc <> 'DISCOUNT'
  227. AND d.emp_no <> 9999
  228. GROUP BY m.memtype";
  229. $memtype = "SELECT s.staff_desc as memType,ROUND(SUM(d.total),2) AS Sales,ROUND((SUM(d.total)/$gross*100),2) AS pct
  230. FROM " . DB_LOGNAME . ".$table d, " . DB_NAME . ".staff s
  231. WHERE d.staff = s.staff_no
  232. AND date(d.datetime) = '$db_date'
  233. AND d.trans_type IN('I','D')
  234. AND d.trans_status <>'X'
  235. AND d.department <= 35 AND d.department <> 0
  236. AND d.upc <> 'DISCOUNT'
  237. AND d.emp_no <> 9999
  238. GROUP BY s.staff_no";
  239. $patronage = "SELECT emp_no, card_no, description, total
  240. FROM " . DB_LOGNAME . ".$table
  241. WHERE date(datetime) = '$db_date'
  242. AND trans_subtype = 'PT'
  243. AND emp_no <> 9999 AND trans_status <> 'X'
  244. ORDER BY card_no";
  245. $cashier_netQ = "SELECT -SUM(total) AS net
  246. FROM " . DB_LOGNAME . ".$table
  247. WHERE DATE(datetime) = '$db_date'
  248. AND trans_subtype IN ('CA','CK','DC','CC','FS','EC')
  249. AND emp_no <> 9999 AND trans_status <> 'X'";
  250. // echo $cashier_netQ;
  251. $cnR = mysql_query($cashier_netQ);
  252. $row = mysql_fetch_row($cnR);
  253. $cnet = $row[0];
  254. $d2 = $net - $cnet;
  255. // include('net.php');
  256. ////////////////////////////
  257. //
  258. //
  259. // NOW....SPIT IT ALL OUT....
  260. //
  261. //
  262. ////////////////////////////
  263. // echo $db_date . '<br>';
  264. echo '<font size = 2>';
  265. echo '<h2>Sales - Gross & NET</h2>';
  266. echo "<table border=0><tr><td><b>sales (gross) total</b></td><td align=right><b>".money_format('%n',$gross)."</b></td></tr>";
  267. echo "<tr><td>non-inv total</td><td align=right>".money_format('%n',$hash)."</td></tr>";
  268. echo "<tr><td>totalDisc</td><td align=right>".money_format('%n',$totalDisc)."</td></tr>";
  269. echo "<tr><td>coupon & gift cert. tenders</td><td align=right>".money_format('%n',$coupons)."</td></tr>";
  270. echo "<tr><td>store charges</td><td align=right>".money_format('%n',$strchg)."</td></tr>";
  271. if ($pt_total != 0) {
  272. echo "<tr><td>patronage dividends</td><td align=right>".money_format('%n',$pt_total)."</td></tr>";
  273. }
  274. echo "<tr><td>rcvd/accts</td><td align=right>".money_format('%n',$RA)."</td></tr>";
  275. //echo "<tr><td>mkt EBT & chg pmts</td><td align=right>".money_format('%n',$other)."</td></tr>";
  276. echo "<tr><td>&nbsp;</td><td align=right>+___________</td></tr>";
  277. echo "<tr><b><td>net total</td><td align=right>".money_format('%n',$net)."</td></b></tr>";
  278. echo "<tr><b><td>cashier net total</td><td align=right>".money_format('%n',$cnet)."</td></tr>";
  279. echo "<tr><td>D2 (for kris)</td><td align=right>". money_format('%n',$d2) ."</td></tr></table>";
  280. echo '------------------------------<br>';
  281. echo '<h2>Sales by Inventory Dept.</h2>';
  282. select_to_table($inventoryDeptQ,0,$bgcolor);
  283. deptTotals('Grocery',$gross,$table,$db_date,$db_date,'2,3,6,7,8,9',$bgcolor);
  284. deptTotals('Produce',$gross,$table,$db_date,$db_date,'1,14,15',$bgcolor);
  285. deptTotals('Nonfoods',$gross,$table,$db_date,$db_date,'4,5,10',$bgcolor);
  286. echo '<br />';
  287. echo '<h2>Sales by Non-Inventory Dept.</h2>';
  288. select_to_table($noninventoryDeptQ,0,$bgcolor);
  289. echo '------------------------------<br>';
  290. echo '<h2>Tender Report</h2>';
  291. select_to_table($tendersQ,0,$bgcolor); // sales by tender type
  292. echo "<h2>Instore Coupon Breakdown</h2>";
  293. select_to_table($instoreQ,0,$bgcolor); // instore coupon breakdown
  294. echo "<h2>Customer Services Breakdown</h2>";
  295. select_to_table($custSvcQ,0,$bgcolor); // customer svc breakdown
  296. echo "<h2>Store Charge Breakdown</h2>";
  297. select_to_table($storeChargeQ,0,$bgcolor); // store charge breakdown
  298. // select_to_table($houseChargeQ,0,$bgcolor); // house charges
  299. select_to_table($transCountQ,0,$bgcolor); // transaction count
  300. select_to_table($basketSizeQ,0,$bgcolor); // basket size
  301. echo '------------------------------<br>';
  302. echo '<h2>Membership & Discount Totals</h2><br>';
  303. echo "<table border=0><font size=2>";
  304. echo "<tr><td>staff total</td><td align=right>".money_format('%n',$staff_total)."</td></tr>";
  305. echo "<tr><td>hoo total</td><td align=right>".money_format('%n',$hoo_total)."</td></tr>";
  306. echo "<tr><td>benefits total</td><td align=right>".money_format('%n',$bene_total)."</td></tr>";
  307. echo "<tr><td>bod total</td><td align=right>".money_format('%n',$bod_total)."</td></tr>";
  308. echo "<tr><td>MAD coupon ($MAD_num)</td><td align=right>".money_format('%n',$MADcoupon)."</td></tr>";
  309. if ($NMD_num != 0) {
  310. echo "<tr><td>Non-Member Discount ($NMD_num)</td><td align=right>".money_format('%n',$NMD_total)."</td></tr>";
  311. }
  312. echo "<tr><td>foodforall total ($ffa_num)</td><td align=right>".money_format('%n',$foodforall)."</td></tr>";
  313. if ($tenDisc != 0) {
  314. echo "<tr><td>10% on the 10th Discount</td><td align=right>".money_format('%n',$tenDisc)."</td></tr>";
  315. }
  316. if ($SSDD_num != 0) {
  317. echo "<tr><td><i>SPECIAL</i> discount ($SSDD_num)</td><td align=right>".money_format('%n',$SSDdiscount2)."</td></tr>";
  318. }
  319. if (strtotime($db_date) <= strtotime($dbChangeDate)) {
  320. echo "<tr><td>Uncaught Discount/FFA</td><td align=right>".money_format('%n',$misc_total)."</td></tr>";
  321. } else {
  322. echo "<tr><td>Manual Member Discounts</td><td align=right>".money_format('%n',$misc_total)."</td></tr>";
  323. }
  324. echo "<tr><td>&nbsp;</td><td align=right>+___________</td></tr>";
  325. echo "<tr><td><b>total discount</td><td align=right>".money_format('%n',$totalDisc)."</b></td></tr></font></table>";
  326. //select_to_table($percentsQ,0,$bgcolor); // discounts awarded by percent
  327. //select_to_table($memstatus,0,$bgcolor);
  328. //select_to_table($memtype,0,$bgcolor);
  329. echo '<h2>Share Payments</h2><br>';
  330. select_to_table($sharePaymentsQ,0,$bgcolor); // peopleshare payments
  331. //echo '<b>Share count = '.$shareCount.'</b>'; // peopleshare count
  332. echo '<h2>Patronage Redemption</h2><br>';
  333. select_to_table($patronage,0,$bgcolor); // patronage redemption
  334. echo '</font>';
  335. ?>
  336. </font>
  337. </body>
  338. </html>