Ejemplo n.º 1
0
if (strtotime($repDate) <= strtotime('2015-04-01')) {
    $countyTax = 0;
}
$taxQ = $dbc->prepare_statement("\nSELECT \n    (CASE WHEN d.tax = 1 THEN 'Non Deli Sales' ELSE 'Deli Sales' END) as type, \n    sum(total) as taxable_sales,\n    {$cityTax}*(sum(total)) as city_tax,\n    {$deliTax}*(sum(CASE WHEN d.tax = 2 THEN total ELSE 0 END)) as deli_tax,\n    {$stateTax}*(sum(total)) as state_tax,\n    {$countyTax}*(SUM(total)) AS county_tax\nFROM {$dlog} as d \nWHERE d.tdate BETWEEN ? AND ?\nAND d.tax <> 0 \nAND " . DTrans::isStoreID($store, 'd') . "\nGROUP BY d.tax ORDER BY d.tax DESC");
$taxR = $dbc->exec_statement($taxQ, $store_dates);
$taxes = array();
while ($row = $dbc->fetch_row($taxR)) {
    $taxes["{$row['0']}"] = array(-1 * $row['taxable_sales'], -1 * $row['city_tax'], -1 * $row['deli_tax'], -1 * $row['county_tax'], -1 * $row['state_tax'], -1 * ($row['city_tax'] + $row['county_tax'] + $row['state_tax'] + $row['deli_tax']));
}
echo "<br /><b>Sales Tax</b>";
echo WfcLib::tablify($taxes, array(0, 1, 2, 3, 4, 5, 6), array("&nbsp;", "Taxable Sales", sprintf("City Tax (%.2f%%)", $cityTax * 100), sprintf("Deli Tax (%.2f%%)", $deliTax * 100), sprintf("County Tax (%.2f%%)", $countyTax * 100), sprintf("State Tax (%.3f%%)", $stateTax * 100), "Total Tax"), array(WfcLib::ALIGN_LEFT, WfcLib::ALIGN_RIGHT | WfcLib::TYPE_MONEY, WfcLib::ALIGN_RIGHT | WfcLib::TYPE_MONEY, WfcLib::ALIGN_RIGHT | WfcLib::TYPE_MONEY, WfcLib::ALIGN_RIGHT | WfcLib::TYPE_MONEY, WfcLib::ALIGN_RIGHT | WfcLib::TYPE_MONEY, WfcLib::ALIGN_RIGHT | WfcLib::TYPE_MONEY));
$taxSumQ = $dbc->prepare_statement("SELECT  -1*sum(total) as tax_collected\nFROM {$dlog} as d \nWHERE d.tdate BETWEEN ? AND ?\nAND (d.upc = 'tax')\nAND " . DTrans::isStoreID($store, 'd') . "\nGROUP BY d.upc");
$taxSumR = $dbc->exec_statement($taxSumQ, $store_dates);
echo "<br /><b><u>Actual Tax Collected</u></b><br />";
echo sprintf("%.2f<br />", array_pop($dbc->fetch_row($taxSumR)));
$transQ = $dbc->prepare_statement("select q.trans_num,sum(q.quantity) as items,transaction_type, sum(q.total) from\n    (\n    select trans_num,card_no,quantity,total,\n        m.memdesc as transaction_type\n    from {$dlog} as d\n    left join custdata as c on d.card_no = c.cardno\n    left join memTypeID as m on c.memtype = m.memTypeID\n    WHERE d.tdate BETWEEN ? AND ?\n    AND trans_type in ('I','D')\n    and upc <> 'RRR'\n    and c.personNum=1\n    AND " . DTrans::isStoreID($store, 'd') . "\n    ) as q \n    group by q.trans_num,q.transaction_type");
$transR = $dbc->exec_statement($transQ, $store_dates);
$transinfo = array("Member" => array(0, 0.0, 0.0, 0.0, 0.0), "Non Member" => array(0, 0.0, 0.0, 0.0, 0.0), "Staff Member" => array(0, 0.0, 0.0, 0.0, 0.0), "Staff NonMem" => array(0, 0.0, 0.0, 0.0, 0.0));
while ($row = $dbc->fetch_array($transR)) {
    if (!isset($transinfo[$row[2]])) {
        continue;
    }
    $transinfo[$row[2]][0] += 1;
    $transinfo[$row[2]][1] += $row[1];
    $transinfo[$row[2]][3] += $row[3];
}
$tSum = 0;
$tItems = 0;
$tDollars = 0;
foreach (array_keys($transinfo) as $k) {
    $transinfo[$k][2] = round($transinfo[$k][1] / $transinfo[$k][0], 2);
Ejemplo n.º 2
0
 /**
   Lots of options on this report.
 */
 function fetch_report_data()
 {
     $dbc = $this->connection;
     $dbc->selectDB($this->config->get('OP_DB'));
     $date1 = $this->form->date1;
     $date2 = $this->form->date2;
     $deptStart = FormLib::get_form_value('deptStart', '');
     $deptEnd = FormLib::get_form_value('deptEnd', '');
     $deptMulti = FormLib::get('departments', array());
     $buyer = FormLib::get_form_value('buyer', '');
     $groupby = FormLib::get_form_value('sort', 'PLU');
     $store = FormLib::get('store', 0);
     $superP = $dbc->prepare('SELECT dept_ID FROM superdepts WHERE superID=?');
     /**
       Build a WHERE condition for later.
       Superdepartment (buyer) takes precedence over
       department and negative values have special
       meaning
     
       Extra lookup to write condition in terms of
       transaction.department seems to result in
       better index utilization and faster queries
     */
     $filter_condition = 't.department BETWEEN ? AND ?';
     $args = array($deptStart, $deptEnd);
     if (count($deptMulti) > 0) {
         $filter_condition = 't.department IN (';
         $args = array();
         foreach ($deptMulti as $d) {
             $filter_condition .= '?,';
             $args[] = $d;
         }
         $filter_condition = substr($filter_condition, 0, strlen($filter_condition) - 1) . ')';
     }
     if ($buyer !== "" && $buyer > 0) {
         $filter_condition .= ' AND s.superID=? ';
         $args[] = $buyer;
         /*
         $superR = $dbc->execute($superP, array($buyer));
         $filter_condition = 't.department IN (';
         $args = array();
         while ($superW = $dbc->fetch_row($superR)) {
             $filter_condition .= '?,';
             $args[] = $superW['dept_ID'];
         }
         $filter_condition = substr($filter_condition, 0, strlen($filter_condition)-1) . ')';
         $filter_condition .= ' AND s.superID=?';
         $args[] = $buyer;
         */
     } else {
         if ($buyer !== "" && $buyer == -1) {
             $filter_condition = "1=1";
             $args = array();
         } else {
             if ($buyer !== "" && $buyer == -2) {
                 $superR = $dbc->execute($superP, array(0));
                 $filter_condition = 't.department NOT IN (0,';
                 $args = array();
                 while ($superW = $dbc->fetch_row($superR)) {
                     $filter_condition .= '?,';
                     $args[] = $superW['dept_ID'];
                 }
                 $filter_condition = substr($filter_condition, 0, strlen($filter_condition) - 1) . ')';
                 $filter_condition .= ' AND s.superID <> 0';
             }
         }
     }
     /**
      * Provide more WHERE conditions to filter irrelevant
      * transaction records, as a stop-gap until this is
      * handled more uniformly across the application.
      */
     $filter_transactions = "t.trans_status NOT IN ('D','X','Z')\n            AND t.emp_no <> 9999\n            AND t.register_no <> 99";
     $filter_transactions = DTrans::isValid() . ' AND ' . DTrans::isNotTesting();
     /**
       Select a summary table. For UPC results, per-unique-ring
       summary is needed. For date/dept/weekday results the
       per-department summary is fine (and a smaller table)
     */
     $dlog = DTransactionsModel::selectDlog($date1, $date2);
     /**
       Build an appropriate query depending on the grouping option
     */
     $query = "";
     $superTable = $buyer !== "" && $buyer > 0 ? 'superdepts' : 'MasterSuperDepts';
     $args[] = $date1 . ' 00:00:00';
     $args[] = $date2 . ' 23:59:59';
     $args[] = $store;
     switch ($groupby) {
         case 'PLU':
             $query = "SELECT t.upc,\n                      CASE WHEN p.description IS NULL THEN t.description ELSE p.description END as description, \n                      SUM(CASE WHEN trans_status IN('','0','R') THEN 1 WHEN trans_status='V' THEN -1 ELSE 0 END) as rings," . DTrans::sumQuantity('t') . " as qty,\n                      SUM(t.total) AS total,\n                      d.dept_no,d.dept_name,s.superID,\n                      COALESCE(v.vendorName,x.distributor) AS distributor\n                      FROM {$dlog} as t " . DTrans::joinProducts() . DTrans::joinDepartments() . "LEFT JOIN {$superTable} AS s ON t.department = s.dept_ID\n                      LEFT JOIN prodExtra as x on t.upc = x.upc\n                      LEFT JOIN vendors AS v ON p.default_vendor_id=v.vendorID\n                      WHERE {$filter_condition}\n                      AND t.trans_type IN ('I', 'D')\n                      AND tdate BETWEEN ? AND ?\n                      AND {$filter_transactions}\n                      AND " . DTrans::isStoreID($store, 't') . "\n                      GROUP BY t.upc,\n                          CASE WHEN p.description IS NULL THEN t.description ELSE p.description END,\n                          CASE WHEN t.trans_status = 'R' THEN 'Refund' ELSE 'Sale' END,\n                      d.dept_no,d.dept_name,s.superID,distributor ORDER BY SUM(t.total) DESC";
             break;
         case 'Department':
             $query = "SELECT t.department,d.dept_name," . DTrans::sumQuantity('t') . " as qty,\n                    SUM(total) as Sales \n                    FROM {$dlog} as t " . DTrans::joinDepartments() . "LEFT JOIN {$superTable} AS s ON s.dept_ID = t.department \n                    WHERE {$filter_condition}\n                    AND tdate BETWEEN ? AND ?\n                    AND t.trans_type IN ('I', 'D')\n                    AND {$filter_transactions}\n                    AND " . DTrans::isStoreID($store, 't') . "\n                    GROUP BY t.department,d.dept_name ORDER BY SUM(total) DESC";
             break;
         case 'Date':
             $query = "SELECT year(tdate),month(tdate),day(tdate)," . DTrans::sumQuantity('t') . " as qty,\n                    SUM(total) as Sales ,\n                    MAX(" . $dbc->dayofweek('tdate') . ") AS dow\n                    FROM {$dlog} as t " . DTrans::joinDepartments() . "LEFT JOIN {$superTable} AS s ON s.dept_ID = t.department\n                    WHERE {$filter_condition}\n                    AND tdate BETWEEN ? AND ?\n                    AND t.trans_type IN ('I', 'D')\n                    AND {$filter_transactions}\n                    AND " . DTrans::isStoreID($store, 't') . "\n                    GROUP BY year(tdate),month(tdate),day(tdate) \n                    ORDER BY year(tdate),month(tdate),day(tdate)";
             break;
         case 'Weekday':
             $cols = $dbc->dayofweek("tdate") . ",CASE \n                    WHEN " . $dbc->dayofweek("tdate") . "=1 THEN 'Sun'\n                    WHEN " . $dbc->dayofweek("tdate") . "=2 THEN 'Mon'\n                    WHEN " . $dbc->dayofweek("tdate") . "=3 THEN 'Tue'\n                    WHEN " . $dbc->dayofweek("tdate") . "=4 THEN 'Wed'\n                    WHEN " . $dbc->dayofweek("tdate") . "=5 THEN 'Thu'\n                    WHEN " . $dbc->dayofweek("tdate") . "=6 THEN 'Fri'\n                    WHEN " . $dbc->dayofweek("tdate") . "=7 THEN 'Sat'\n                    ELSE 'Err' END";
             $query = "SELECT {$cols}," . DTrans::sumQuantity('t') . " as qty,\n                    SUM(total) as Sales \n                    FROM {$dlog} as t " . DTrans::joinDepartments() . "LEFT JOIN {$superTable} AS s ON s.dept_ID = t.department \n                    WHERE {$filter_condition}\n                    AND tdate BETWEEN ? AND ?\n                    AND t.trans_type IN ('I', 'D')\n                    AND {$filter_transactions}\n                    AND " . DTrans::isStoreID($store, 't') . "\n                    GROUP BY {$cols}\n                    ORDER BY " . $dbc->dayofweek('tdate');
             break;
     }
     /**
       Copy the results into an array. Date requires a
       special case to combine year, month, and day into
       a single field
     */
     $prep = $dbc->prepare_statement($query);
     $result = $dbc->exec_statement($prep, $args);
     $ret = array();
     while ($row = $dbc->fetch_array($result)) {
         $record = array();
         if ($groupby == "Date") {
             $record[] = $row[1] . "/" . $row[2] . "/" . $row[0];
             $record[] = date('l', strtotime($record[0]));
             $record[] = sprintf('%.2f', $row[3]);
             $record[] = sprintf('%.2f', $row[4]);
         } else {
             for ($i = 0; $i < $dbc->num_fields($result); $i++) {
                 if (preg_match('/^\\d+\\.\\d+$/', $row[$i])) {
                     $row[$i] = sprintf('%.2f', $row[$i]);
                 }
                 $record[] .= $row[$i];
             }
         }
         $ret[] = $record;
     }
     return $ret;
 }