require_once '../functions-2.php';
require_once DIR_WS_FUNCTIONS . 'html_output.php';
tep_db_connect();
//TO AVOID ISSUES WITH DAILY CALCULATION (WHICH IS VERY IMPORTANT)
//PLEASE RUN ANY SPECIAL RECALCULATION IN A DIFFERENT FILE, THANKS!
use_class('logger');
$logger = new logger('cron', 'daily-counter-scorecard');
use_class('scorecard');
$date = isset($_GET['date']) ? tep_db_prepare_input($_GET['date']) : date('Y-m-d', strtotime("-1 day"));
$logger->write("Generating Data for Date {$date}");
//SALES (SP AND STARS)
scorecard::calculateScorecardSP($date);
$logger->write('Scorecard data with SP based is calculated and saved');
scorecard::calculateScorecardStars($date);
$logger->write('Scorecard data with Stars based is calculated and saved');
//WAREHOUSES
//only possible to save today's date
scorecard::generateWarehouseLog($date);
$logger->write('Daily warehouse data is logged');
//PRODUCTS MANAGEMENT
scorecard::generatePMLog($date);
$logger->write('Product Management Data is logged');
//DEPOT ORDERS
scorecard::generateDepotOrdersLog();
$logger->write('Depot Orders Data is logged');
//SAVE STARS LOG OF THE DAY
//only possible to save today's date
scorecard::generateStarsLog();
$logger->write('Daily stars data is logged');
$logger->close();
tep_db_close();
                }
                if (isset($table_header[$table_key])) {
                    $result .= ' - ' . $table_header[$table_key];
                }
                $result .= '</h3>';
                //echo '<pre>';var_dump($tc_value); echo '</pre><br /><br />';
                //Prepare the total values
                foreach ($table_total[$table_key][$tc_key] as $tt_temp_key => $tt_temp_val) {
                    //Label column need no values
                    if ($tt_temp_key != $classname_label) {
                        if (is_array($tt_temp_val)) {
                            $total = array_sum($tt_temp_val) / count($tt_temp_val);
                        } else {
                            $total = $tt_temp_val;
                        }
                        $table_total[$table_key][$tc_key][$tt_temp_key] = scorecard::finalDisplayFormat($tc_key, $total);
                    }
                }
                $tc_value[$total_key] = $table_total[$table_key][$tc_key];
                $result .= tep_draw_table('scorecard', $tc_value, false, true);
            }
        }
        //That's it, now give back the table
        echo utf8_encode($result);
        exit;
    }
}
$period_options = array();
$period_options['all'] = 'Show All Months';
$period_options['actual'] = 'Show Actual Months';
$view_option = array();
        $result .= tep_draw_table('scorecard', $table_products_margin);
        //DRAW table average margin by categories
        $table_products_margin = array();
        $tpn_row = array();
        $tpn_row[$classname_label] = 'Brand';
        foreach ($periods as $p) {
            $tpn_row[$p] = $periods_name[$p];
        }
        $table_products_margin[] = $tpn_row;
        $table_total = array();
        $table_total[$classname_label] = 'Total';
        foreach ($products_margin_categories as $categories_id => $margins) {
            $tpn_row = array();
            $tpn_row[$classname_label] = $product_categories_name[$categories_id];
            foreach ($margins as $p => $v) {
                $tpn_row[$p] = $v == '-' ? '-' : scorecard::finalDisplayFormat('average_margin', $v / 100);
            }
            $table_products_margin[] = $tpn_row;
        }
        $result .= '<h3 style="margin:20px 0 0 0;">Average Margin by Categories</h3>';
        $result .= tep_draw_table('scorecard', $table_products_margin);
        echo utf8_encode($result);
        exit;
    }
}
$period_options = array();
$period_options['all'] = 'Show All Months';
$period_options['actual'] = 'Show Actual Months';
$view_option = array();
$view_option['overview'] = 'Show Overview';
$view_option['details'] = 'Show Details';