<?php
/*******************************************************************************

    Copyright 2013 Whole Foods Co-op

    This file is part of IT CORE.

    IT CORE is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    IT CORE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    in the file license.txt along with IT CORE; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*********************************************************************************/

include(dirname(__FILE__).'/../../../config.php');
if (!class_exists('FannieAPI')) {
    include_once(__DIR__ . '/../../../classlib2.0/FannieAPI.php');
}

class OverShortSafecountPage extends FanniePage {

    protected $window_dressing = False;
    protected $auth_classes = array('overshorts');

    public $page_set = 'Plugin :: Over/Shorts';
    public $description = '[Safe Count] stores information about cash on hand and change buys.';

    function preprocess(){
        $action = FormLib::get_form_value('action',False);
        if ($action !== False){
            $this->ajaxRequest($action);
            return False;
        }
        return True;
    }

    function ajaxRequest($action){
        global $FANNIE_OP_DB, $FANNIE_PLUGIN_SETTINGS;
        $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['OverShortDatabase']);
        $d1 = FormLib::get_form_value('date1');
        $d2 = FormLib::get_form_value('date2');
        $store = FormLib::get('store');
    
        $dateStr = $d1." ".$d2;
        if ($d1 == $d2) $dateStr = $d1;
        switch($action){
        case 'loader':
            echo $this->displayUI($dateStr, $store);
            break;
        case 'save':
            echo $this->save($dateStr, $store,
                FormLib::get_form_value('changeOrder'),
                FormLib::get_form_value('openSafeCount'),
                FormLib::get_form_value('closeSafeCount'),
                FormLib::get_form_value('buyAmount'),
                FormLib::get_form_value('dropAmount'),
                FormLib::get_form_value('depositAmount'),
                FormLib::get_form_value('atmAmount')
                );
            break;  
        }
    }

    function save($dateStr,$store,$changeOrder,$openSafeCount,$closeSafeCount,$buyAmount,$dropAmount,$depositAmount,$atmAmount){
        $this->saveInputs($dateStr,$store,'changeOrder',$changeOrder);
        $this->saveInputs($dateStr,$store,'openSafeCount',$openSafeCount);
        $this->saveInputs($dateStr,$store,'closeSafeCount',$closeSafeCount);
        $this->saveInputs($dateStr,$store,'buyAmount',$buyAmount);
        $this->saveInputs($dateStr,$store,'dropAmount',$dropAmount);
        $this->saveInputs($dateStr,$store,'depositAmount',$depositAmount);
        $this->saveInputs($dateStr,$store,'atm',$atmAmount);
    
        return 'Saved';
    }

    function saveInputs($dateStr,$store,$row,$data){
        global $FANNIE_PLUGIN_SETTINGS;
        $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['OverShortDatabase']);

        $model = new DailyDepositModel($dbc);
        $model->dateStr($dateStr);
        $model->storeID($store);
        $model->rowName($row);
        $model->countFormat(1);

        $temp = explode('|',$data);
        foreach($temp as $t){
            $temp2 = explode(':',$t);
            if (count($temp2) < 2) continue;
            $denom = $temp2[0];
            $amt = $temp2[1];
        
            if ($amt == '') continue;

            $model->denomination($denom);
            $model->amt($amt);
            $model->save();
        }
    }

    function displayUI($dateStr, $store){
        global $FANNIE_PLUGIN_SETTINGS;
        $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['OverShortDatabase']);

        $startDate = $dateStr;
        $endDate = $dateStr;
        if (strstr($dateStr," ")){
            $temp = explode(" ",$dateStr);
            $startDate = $temp[0];
            $endDate = $temp[1];
        }
        if (trim($startDate) === '' || trim($endDate) === '') {
            return "Invalid Dates";
        }

        $holding = array('changeOrder'=>array(),
                'openSafeCount'=>array(),
                'closeSafeCount'=>array(),
                'dropAmount'=>array(),
                'atm'=>array('fill'=>0,'reject'=>0,'count'=>0)
                );

        $denoms = array('0.01','0.05','0.10','0.25','Junk','1.00','5.00','10.00','20.00','50.00','100.00','Checks');
        foreach($denoms as $d){
            foreach($holding as $k=>$v){
                $holding[$k]["$d"] = 0;
            }
        }

        $model = new DailyDepositModel($dbc);
        $model->dateStr($dateStr);
        $model->storeID($store);
        foreach($model->find() as $obj){
            if ($obj->rowName() == 'buyAmount')
                continue;
        
            $holding[$obj->rowName()][$obj->denomination()] = $obj->amt();
        }

        $actualTotal = 0;
        $accountableTotal = 0;
        $buyAmountTotal = 0;
    
        $ret = "<h3>$dateStr</h3>";
        $ret .= "<table cellspacing=0 border=1 cellpadding=4><tr><td>&nbsp;</td>";
        foreach ($denoms as $d) $ret .= "<th>$d</th>";
        $ret .= "<th>Total</th></tr>";

        $ret .= "<tr class=color><th title=\"Currency ordered from the bank. This should match Buy Amount from the previous count.\">Change Order</th>";
        $sum = 0;
        foreach($denoms as $d){ 
            if ($d == 'Checks' || $d == "100.00" || $d == "50.00" || $d == "20.00" || $d == "Junk") 
                $ret .= "<td>&nbsp;</td>";
            else{
                $ret .= "<td><input size=4 type=text id=\"changeOrder$d\" value=".$holding['changeOrder'][$d];
                $ret .= " onchange=\"updateChangeOrder('$d');\" /></td>";
                $sum += $holding['changeOrder'][$d];
            }
        }
        $ret .= "<td id=changeOrderTotal>$sum</td></tr>";

        $ret .= "<tr><th title=\"Money in the safe at the start of this cont.\">Open Safe Count</th>";
        $sum = 0;
        foreach($denoms as $d){
            if ($d == 'Checks') 
                $ret .= "<td>&nbsp;</td>";
            else{
                $ret .= "<td><input size=4 type=text id=\"safeCount1$d\" value=".$holding['openSafeCount'][$d];
                $ret .= " onchange=\"updateOpenSafeCount('$d');\" /></td>";
                $sum += $holding['openSafeCount'][$d];
            }
        }
        $ret .= "<td id=safeCount1Total>$sum</td></tr>";

        $dateClause = ' date = ?';
        $dateArgs = array($dateStr);
        if (strstr($dateStr," ")){
            $dates = explode(" ",$dateStr);
            $dateClause = ' date BETWEEN ? AND ? AND storeID = ? ';
            $dateArgs = array($dates[0],$dates[1], $store);
        }
        $countQ = "SELECT tender_type,sum(amt) from dailyCounts where tender_type in ('CA','CK','SCA') and $dateClause GROUP BY tender_type";
        $countP = $dbc->prepare($countQ);
        $countR = $dbc->execute($countP, $dateArgs);
        $osCounts = array('CA'=>0,'CK'=>0,'SCA'=>0);
        while($countW = $dbc->fetch_row($countR))
            $osCounts[$countW[0]] = $countW[1];

        $bags = round($osCounts['SCA'] / 168.00);
        //$osCounts['CA'] -= 168*$bags;

        $ret .= "<tr class=color><th title=\"This is the Open Safe Count plus the Change Order.\">Total Change Fund</th>";
        $sum = 0;
        foreach($denoms as $d){
            if ($d == "Checks"){
                $ret .= "<td>&nbsp;</td>";
            }
            else {
                $val = $holding['changeOrder'][$d] + $holding['openSafeCount'][$d];
                $ret .= "<td id=cashInTills$d>$val</td>";
                $sum += $val;
            }
        }
        $ret .= "<td id=cashInTillsTotal>$sum</td></tr>";
        $accountableTotal += $sum;

        $ret .= "<tr><th title=\"Money dropped by cashiers since the last count. The total comes from adding the daily over/short counts for cash and checks. The 1.00 value is then auto-adjusted so the line sum matches the total.\">Drop Amount</th>";
        foreach($denoms as $d){
            if ($d == "1.00"){
                $ret .= "<td id=dropAmount1.00>".$holding['dropAmount'][$d]."</td>";
            }
            else if ($d == "Checks"){
                $ret .= "<td id=dropAmountChecks>".$osCounts['CK']."</td>";
            }
            else {
                $ret .= "<td><input size=4 type=text id=dropAmount$d value=".$holding['dropAmount'][$d];
                $ret .= " onchange=\"updateDropAmount('$d');\" /></td>";
            }
        }
        $val = ($osCounts['CA'] + $osCounts['CK']);
        $ret .= "<td id=dropAmountTotal>".round($val,2)."</td></tr>";
        $buyAmountTotal -= $val;
        $accountableTotal += $val;

        $ret .= "<tr class=\"color\"><th title=\"Fill is cash retained to refill the ATM. Reject is worn or damaged bills that the ATM cannot process and will be deposited instead.\">ATM</th>";
        $ret .= "<td colspan=\"5\">&nbsp;</td>";
        $ret .= "<td>Count:</td>";
        $ret .= "<td><input size=4 type=text id=atmCount value=\"".$holding['atm']['count']."\"
                onchange=\"updateAtmAmounts();\" /></td>";
        $ret .= "<td>Fill:</td>";
        $ret .= "<td><input size=4 type=text id=atmFill value=\"".$holding['atm']['fill']."\"
                onchange=\"updateAtmAmounts();\" /></td>";
        $ret .= "<td>&nbsp;</td>";
        $ret .= "<td>Reject:</td>";
        $ret .= "<td><input size=4 type=text id=atmReject value=\"".$holding['atm']['reject']."\"
                onchange=\"updateAtmAmounts();\" /></td>";
        $ret .= "<td>&nbsp;</td>";
        $ret .= "</tr>";

        $accountableTotal += ($holding['atm']['reject'] - $holding['atm']['fill']);
        
        $ret .= "<tr><th>Fill Amount</th>";
        $ret .= "<td id=fill0.01>".(1*$bags)."</td>";
        $ret .= "<td id=fill0.05>".(2*$bags)."</td>";
        $ret .= "<td id=fill0.10>".(5*$bags)."</td>";
        $ret .= "<td id=fill0.25>".(10*$bags)."</td>";
        $ret .= "<td>&nbsp;</td>";
        $ret .= "<td id=fill1.00>".(50*$bags)."</td>";
        $ret .= "<td id=fill5.00>".(50*$bags)."</td>";
        $ret .= "<td id=fill10.00>".(50*$bags)."</td>";
        $ret .= "<td colspan=4>&nbsp;</td>";
        $ret .= "<td id=fillTotal>".(168*$bags)."</td></tr>";

        $accountableTotal -= (168*$bags);

        $fills = array('0.01'=>1,'0.05'=>2,'0.10'=>5,'0.25'=>10,'1.00'=>50,'5.00'=>50,'10.00'=>50);
        if ($store == 1) {
            $pars = array("0.01"=>60,"0.05"=>120,"0.10"=>320,"0.25"=>1200,"1.00"=>2600,"5.00"=>1000,"10.00"=>1300);
        } else {
            $pars = array("0.01"=>35,"0.05"=>80,"0.10"=>250,"0.25"=>800,"1.00"=>1755,"5.00"=>750,"10.00"=>830);
        }
        if (file_exists(__DIR__ . '/pars.json')) {
            $json = json_decode(file_get_contents(__DIR__ . '/pars.json'), true);
            if (isset($json[$store])) {
                $pars = $json[$store];
            }
        }

        $ret .= "<tr class=\"color\"><th title=\"The amount we're sending to the bank is calculated by subtracting the Total Change Fund and Drop Amount from the Par Amount. With coins loose amounts that won't fit in a roll are also deposited. With 20.00s the deposit is modifed by ATM fills and rejects.\">Deposit Amount</th>";
        $sum = 0;
        $depositAmount = array();
        foreach($denoms as $d){
            if ($d == 'Checks'){
                $ret .= "<td id=depositAmount$d>".$osCounts['CK']."</td>";
                $sum += $osCounts['CK'];
                $depositAmount['Checks'] = $osCounts['CK'];
            }
            else if ($d == '100.00' || $d == '50.00' || $d == 'Junk'){
                $ret .= "<td id=depositAmount$d>".($holding['openSafeCount'][$d] + $holding['dropAmount'][$d])."</td>";
                $sum += ($holding['openSafeCount'][$d] + $holding['dropAmount'][$d]);
                $depositAmount[$d] = $holding['openSafeCount'][$d]+$holding['dropAmount'][$d];
            }
            else if ($d == '20.00'){
                $atmTtl = $holding['openSafeCount'][$d] + $holding['dropAmount'][$d] 
                    - $holding['atm']['fill'] + $holding['atm']['reject'];
                $ret .= "<td id=depositAmount$d>".$atmTtl."</td>";
                $sum += $atmTtl;
                $depositAmount[$d] = $atmTtl;
            }
            else if ($d == '10.00'){
                $val = $holding['changeOrder'][$d] + $holding['openSafeCount'][$d] + $holding['dropAmount'][$d] - $pars['10.00'] - (50*$bags);
                $val = round($val,2);
                if ($val < 0) $val = 0;
                $ret .= "<td id=depositAmount$d>".$val."</td>";
                $sum += $val;
                $depositAmount[$d] = $val;
            }
            else if ($d == '5.00'){
                $val = $holding['changeOrder'][$d] + $holding['openSafeCount'][$d] + $holding['dropAmount'][$d] - $pars['5.00'] - (50*$bags);
                $val = round($val,2);
                if ($val < 0) $val = 0;
                $ret .= "<td id=depositAmount$d>".$val."</td>";
                $sum += $val;
                $depositAmount[$d] = $val;
            }
            else if ($d == '1.00'){
                $ret .= "<td id=depositAmount$d>0</td>";
                $val = round($val,2);
                $depositAmount[$d] = 0;
            }
            else if ($d == '0.25'){
                $val = $holding['dropAmount'][$d] - ( ((int)($holding['dropAmount'][$d]/10)) * 10 );
                $val = round($val,2);
                $ret .= "<td id=depositAmount$d>".$val."</td>";
                $sum += $val;
                $depositAmount[$d] = $val;
            }
            else if ($d == '0.10'){
                $val = $holding['dropAmount'][$d] - ( ((int)($holding['dropAmount'][$d]/5)) * 5 );
                $val = round($val,2);
                $ret .= "<td id=depositAmount$d>".$val."</td>";
                $sum += $val;
                $depositAmount[$d] = $val;
            }
            else if ($d == '0.05'){
                $val = $holding['dropAmount'][$d] - ( ((int)($holding['dropAmount'][$d]/2)) * 2 );
                $val = round($val,2);
                $ret .= "<td id=depositAmount$d>".$val."</td>";
                $sum += $val;
                $depositAmount[$d] = $val;
            }
            else if ($d == '0.01'){
                $val = $holding['dropAmount'][$d] - ( ((int)($holding['dropAmount'][$d]/0.50)) * 0.50 );
                $val = round($val,2);
                $ret .= "<td id=depositAmount$d>".$val."</td>";
                $sum += $val;
                $depositAmount[$d] = $val;
            }
        }
        $ret .= "<td id=depositAmountTotal>$sum</td></tr>";
        $buyAmountTotal += $sum;
        $accountableTotal -= $sum;
        
        $ret .= "<tr><th title=\"What remains in the safe after the count is Total Change Fund plus Drop Amount minus Deposit Amount.\">Close Safe Count</th>";
        $sum = 0;
        foreach($denoms as $d){
            if ($d == 'Checks' || $d == "Junk") 
                $ret .= "<td>&nbsp;</td>";
            else{
                $ret .= "<td><input size=4 type=text id=safeCount2$d value=".$holding['closeSafeCount'][$d];
                $ret .= " onchange=\"updateCloseSafeCount('$d');\" /></td>";
                $sum += $holding['closeSafeCount'][$d];
            }
        }
        $ret .= "<td id=safeCount2Total>$sum</td></tr>";
        $actualTotal += $sum;

        $parTTL = 0; foreach($pars as $k=>$v) $parTTL += $v;
        $ret .= "<tr class=\"color\"><th title=\"Pars are the amounts we want to keep on hand of each denomination. Click here to adjust the current pars for the store.\"><a href=\"OverShortParsPage.php\">Par Amounts</a></th>";
        $ret .= "<td id=par0.01>".$pars['0.01']."</td>";
        $ret .= "<td id=par0.05>".$pars['0.05']."</td>";
        $ret .= "<td id=par0.10>".$pars['0.10']."</td>";
        $ret .= "<td id=par0.25>".$pars['0.25']."</td>";
        $ret .= "<td>&nbsp;</td>";
        $ret .= "<td id=par1.00>".$pars['1.00']."</td>";
        $ret .= "<td id=par5.00>".$pars['5.00']."</td>";
        $ret .= "<td id=par10.00>".$pars['10.00']."</td>";
        $ret .= "<td id=par20.00>".$pars['20.00']."</td>";
        $ret .= "<td colspan=3>&nbsp;</td>";
        $ret .= sprintf("<td>%.2f</td></tr>",$parTTL);

        $buyAmounts = array("0.01"=>0,"0.05"=>0,"0.10"=>0,"0.25"=>0,"1.00"=>0,"5.00"=>0,"10.00"=>0,"20.00"=>0);
        foreach ($buyAmounts as $k=>$v){
            if ($k == '20.00') {
                continue; // buy 20s but don't fill w/ them
            }
            $val = $pars[$k];
            $val -= $holding['changeOrder'][$k];
            $val -= $holding['openSafeCount'][$k];
            $val -= $holding['dropAmount'][$k];
            $val += $depositAmount[$k];
            $val += ($fills[$k]*$bags);
            if ($val < 0) $val = 0;
            $buyAmounts[$k] = $val;
        }
        $overage = 0;
        while($buyAmounts['1.00'] % 50 != 0){
            $buyAmounts['1.00'] -= 1;
            $overage += 1;
        }
        while($buyAmounts['5.00'] % 5 == 0 && $buyAmounts['5.00'] % 50 != 0){ 
            $buyAmounts['5.00'] -= 5;
            $overage += 5;
        }
        while($buyAmounts['10.00'] % 10 == 0 && $buyAmounts['10.00'] % 50 != 0){ 
            $buyAmounts['10.00'] -= 10;
            $overage += 10;
        }
        $buyAmounts['20.00'] = $pars['20.00'] - $holding['atm']['count'];
        if ($buyAmounts['20.00'] < 0) {
            $buyAmounts['20.00'] = 0;
        }

        $overs = $this->denom_overage($overage);
        $buyAmounts['0.25'] += $overs['0.25'];
        $buyAmounts['0.10'] += $overs['0.10'];
        $buyAmounts['0.05'] += $overs['0.05'];
        $buyAmounts['0.01'] += $overs['0.01'];

        $ret .= "<tr><th title=\"This is what we need to order from the bank to reach pars. It's not exactly Par Amount minus Close Safe Count because we can only purchase change in specific-sized rolls.\">Buy Amount</th>";
        foreach ($denoms as $d){
            if (isset($buyAmounts[$d]))
                $ret .= "<td id=buyAmount$d>".$buyAmounts[$d]."</td>";
            else
                $ret .= "<td>&nbsp;</td>";
        }
        $ret .= "<td id=buyAmountTotal>".array_sum($buyAmounts)."</td></tr>";

        $dlog = DTransactionsModel::selectDlog($startDate,$endDate);
        $dlogClause = str_replace(' date ', ' d.tdate ', $dateClause);
        $dlogClause = str_replace(' storeID ', ' d.store_id ', $dlogClause);
        $posTotalQ = "SELECT -1*sum(d.total) FROM $dlog as d WHERE ". $dlogClause . " AND d.trans_subtype IN ('CA','CK')";
        $posTotalP = $dbc->prepare($posTotalQ);   
        $dateArgs[1] .= ' 23:59:59';
        $posTotalR = $dbc->execute($posTotalP, $dateArgs);
        $posTotalW = $dbc->fetch_row($posTotalR);
        $posTotal = sprintf('%.2f', $posTotalW[0]);

        $ret .= "<tr class=\"color\"><th>Over/Shorts</th>";
        $ret .= "<td><i>Count total</i></td><td>".round(($osCounts['CA']+$osCounts['CK'] - $osCounts['SCA'] ),2)."</td>";
        $ret .= "<td><i>POS total</i></td><td>".$posTotal."</td>";
        $ret .= "<td><i>Variance</i></td><td>".round(($osCounts['CA']+$osCounts['CK']) - $osCounts['SCA'] -$posTotal,2)."</td>";
        $ret .= "<td><i>Actual</i></td><td id=actualTotal>$actualTotal</td>";
        $ret .= "<td><i>Accountable</i></td><td id=accountableTotal>".round($accountableTotal,2)."</td>";
        $ret .= "<td><i>Variance</i></td><td id=aaVariance>".round($actualTotal - $accountableTotal,2)."</td>";
        $ret .= "<td>&nbsp;</td></tr>"; 


        $dailies = array();
        $countQ = "SELECT date,
                SUM(CASE WHEN tender_type IN ('CA','CK') THEN amt
                    WHEN tender_type = 'SCA' THEN -amt
                ELSE 0 end) AS total
                FROM dailyCounts WHERE date BETWEEN ? AND ?
                    AND storeID=?
                GROUP BY date";
        $countP = $dbc->prepare($countQ);
        $countR = $dbc->execute($countP, array($startDate,$endDate,$store));
        while($row = $dbc->fetch_row($countR)){
            $d = $row['date'];
            if (!isset($dailies[$d])) $dailies[$d] = array(0,0);
            $dailies[$d][0] = $row['total'];
        }
        $posQ = "SELECT YEAR(tdate),MONTH(tdate),DAY(tdate),
                SUM(case when trans_subtype in ('CA','CK') then -total ELSE 0 END) as total
                FROM $dlog AS d WHERE tdate BETWEEN ? AND ?
                    AND d.store_id=?
                GROUP BY YEAR(tdate),MONTH(tdate),DAY(tdate)";
        $posP = $dbc->prepare($posQ);
        $posR = $dbc->execute($posP, array($startDate.' 00:00:00',$endDate.' 23:59:59',$store));
        while($row = $dbc->fetch_row($posR)){
            $d = $row[0]."-".str_pad($row[1],2,'0',STR_PAD_LEFT)."-".str_pad($row[2],2,'0',STR_PAD_LEFT);
            if (!isset($dailies[$d])) $dailies[$d] = array(0,0);
            $dailies[$d][1] = $row[3];
        }
        $num = 0;
        foreach($dailies as $k=>$v){
            if ($num % 2 == 0){
                if ($num != 0) $ret .= "</tr>";
                if ($num % 4 == 0) $ret .= "<tr>";
                else $ret .= "<tr class=\"color\">";
            }
            $ret .= sprintf("<th>%s</th><td><i>Count</i></td><td>%.2f</td>
                    <td><i>POS</i></td><td>%.2f</td><td><i>Variance
                    </i></td><td>%.2f</td>",$k,$v[0],$v[1],($v[0]-$v[1]));
            $num++;
        }
        if ($num % 2 != 0)
            $ret .= "<td colspan=7>&nbsp;</td>";
        $ret .= "</tr>";

        $ret .= "</table>";
        $ret .= "<input type=hidden id=savedDate1 value=\"$startDate\" />";
        $ret .= "<input type=hidden id=savedDate2 value=\"$endDate\" />";
        $ret .= "<input type=hidden id=savedStore value=\"$store\" />";
        foreach($denoms as $d){
            $ret .= "<input type=\"hidden\" class=\"denom\" value=\"$d\" />";
        }
        $ret .= "<input type=submit value=Save onclick=\"save();\" />";
    
        return $ret;
    }

    function denom_overage($overage){
        $ret = array("0.25"=>0,"0.10"=>0,"0.05"=>0,"0.01"=>0);

        $ret["0.25"] = floor($overage / 10.0)*10;
        $overage = $overage % 10;
        $ret["0.10"] = floor($overage / 5.0)*5;
        $overage = $overage % 5;
        $ret["0.05"] = floor($overage / 2.0)*2;
        $overage = $overage % 2;
        $ret["0.01"] = floor($overage / 0.50)*0.50;
        
        return $ret;
    }

    function css_content(){
        return '
            tr.color {
                background: #ffffcc;
            }
            body, table, td, th {
              color: #000;
            }
        ';
    }

    function body_content(){
        global $FANNIE_URL, $FANNIE_PLUGIN_SETTINGS;
        $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['OverShortDatabase']);
        $this->addScript('js/count.js?date=20200622');
        $this->addScript($FANNIE_URL.'src/javascript/jquery.js');
        $this->addScript($FANNIE_URL.'src/javascript/jquery-ui.js');
        $this->addCssFile($FANNIE_URL.'src/style.css');
        $this->addCssFile($FANNIE_URL.'src/javascript/jquery-ui.css');
        $this->addOnloadCommand("\$('#startDate').datepicker({dateFormat:'yy-mm-dd'});");
        $this->addOnloadCommand("\$('#endDate').datepicker({dateFormat:'yy-mm-dd'});");
        ob_start();
        ?>
        <html>
        <head>
            <title>Count</title>
        </head>
        <body>

        <div id=input>
        <table>
        <tr>
            <th colspan="5">June 2020 &amp; older version (<a href="OverShortSafecountV2.php">Switch</a>)</th>
        </tr>
        <tr>
            <th>Start Date</th><td><input type=text id=startDate autocomplete=off /></td>
            <td>
            <input type=submit Value=Load onclick="loader();" />
            </td>
            <td >
            Recent Counts: <select onchange="existingDates(this.value);">
            <option value=''>Select one...</option>
            <?php
            $res = $dbc->query('SELECT dateStr FROM dailyDeposit WHERE countFormat=1 GROUP BY dateStr ORDER BY dateStr DESC');
            $count = 0;
            while($row = $dbc->fetch_row($res)) {
                if ($count++ > 100) {
                    break;
                }
                echo '<option>'.$row['dateStr'].'</option>';
            }
            ?>
            </select>
            </td>
        </tr>
        <tr>
            <th>End Date</th><td><input type=text id=endDate autocomplete=off /></td>
            <td>Store</td>
            <td>
            <?php
            $stores = FormLib::storePicker('store', false);
            echo $stores['html'];
            ?>
            </td>
        </tr>
        </table>
        </div>

        <hr />

        <div id=display></div>
        <?php
        return ob_get_clean();
    }

}

FannieDispatch::conditionalExec();

