function income_display_report($vars)
{
    global $t, $db, $config;
    // get income
    $beg_tm = $vars['beg_date'] . ' 00:00:00';
    $end_tm = $vars['end_date'] . ' 00:00:00';
    $res = array();
    $where_p = " AND product_id IN (0," . implode(',', $vars['products']) . ") ";
    switch ($vars['discretion']) {
        case 'week':
            $what = $group = 'YEARWEEK(tm_added)';
            break;
        case 'month':
            $what = $group = "DATE_FORMAT(tm_added, '%Y%m')";
            break;
        case 'day':
            $what = 'FROM_DAYS(TO_DAYS(tm_added))';
            $group = 'TO_DAYS(tm_added)';
    }
    $q = $db->query($s = "SELECT {$what} as date,\n        count(payment_id) as added_count, sum(amount) as added_amount\n        FROM {$db->config[prefix]}payments p\n        WHERE tm_added BETWEEN '{$beg_tm}' AND '{$end_tm}' {$where_p} \n        GROUP BY {$group}\n        ");
    while ($x = mysql_fetch_assoc($q)) {
        switch ($vars['discretion']) {
            case 'week':
                $year = substr($x['date'], 0, 4);
                $weeknum = substr($x['date'], 4, 2);
                if ($weeknum == 53) {
                    $weeknum = 0;
                    $year++;
                }
                $w = date('w', strtotime("{$year}-01-01"));
                if ($w == 0) {
                    $w = 7;
                }
                $weekstartday = $weeknum * 7 - $w;
                $d = date('Y-m-d', strtotime($year . '-01-01') + $weekstartday * 3600 * 24);
                break;
            case 'month':
                $d1 = substr($x['date'], 0, 4);
                $d2 = substr($x['date'], 4, 2);
                $d = "{$d1}-{$d2}-01";
                break;
            case 'day':
                $d = $x['date'];
        }
        $res[$d] = $x;
    }
    $what = str_replace('tm_added', 'tm_completed', $what);
    $group = str_replace('tm_added', 'tm_completed', $group);
    $q = $db->query($s = "SELECT {$what} as date,\n        count(payment_id) as completed_count, sum(amount) as completed_amount\n        FROM {$db->config[prefix]}payments p\n        WHERE tm_completed BETWEEN '{$beg_tm}' AND '{$end_tm}' {$where_p} \n        AND completed>0\n        GROUP BY {$group}\n        ");
    $max_total = 0;
    while ($x = mysql_fetch_assoc($q)) {
        switch ($vars['discretion']) {
            case 'week':
                $year = substr($x['date'], 0, 4);
                $weeknum = substr($x['date'], 4, 2);
                if ($weeknum == 53) {
                    $weeknum = 0;
                    $year++;
                }
                $w = date('w', strtotime("{$year}-01-01"));
                if ($w == 0) {
                    $w = 7;
                }
                $weekstartday = $weeknum * 7 - $w;
                $d = date('Y-m-d', strtotime($year . '-01-01') + $weekstartday * 3600 * 24);
                break;
            case 'month':
                $d1 = substr($x['date'], 0, 4);
                $d2 = substr($x['date'], 4, 2);
                $d = "{$d1}-{$d2}-01";
                break;
            case 'day':
                $d = $x['date'];
        }
        $res[$d] = array_merge($x, (array) $res[$d]);
        $total_completed += $x['completed_amount'];
        if ($x['completed_amount'] > $max_total) {
            $max_total = $x['completed_amount'];
        }
    }
    //////
    /**/
    $members = array();
    $q = $db->query($s = "SELECT payment_id, member_id, {$what} as date\n        FROM {$db->config[prefix]}payments p\n        WHERE tm_completed BETWEEN '{$beg_tm}' AND '{$end_tm}' {$where_p} \n        AND completed > 0\n\tAND amount > 0\n        ");
    $xx = array();
    while ($xxx = mysql_fetch_assoc($q)) {
        $member_id = $xxx['member_id'];
        $x_date = $xxx['date'];
        if (!$members[$member_id]) {
            $members[$member_id]++;
            $xx[$x_date]++;
        }
    }
    $xy = array();
    foreach ($xx as $date => $paid_count) {
        $xy[] = array('date' => $date, 'paid_count' => $paid_count);
    }
    //    print_r ($xy);
    //    while ($x = mysql_fetch_assoc($q)){
    while (list(, $x) = each($xy)) {
        //    print_r ($x);
        switch ($vars['discretion']) {
            case 'week':
                $year = substr($x['date'], 0, 4);
                $weeknum = substr($x['date'], 4, 2);
                if ($weeknum == 53) {
                    $weeknum = 0;
                    $year++;
                }
                $w = date('w', strtotime("{$year}-01-01"));
                if ($w == 0) {
                    $w = 7;
                }
                $weekstartday = $weeknum * 7 - $w;
                $d = date('Y-m-d', strtotime($year . '-01-01') + $weekstartday * 3600 * 24);
                break;
            case 'month':
                $d1 = substr($x['date'], 0, 4);
                $d2 = substr($x['date'], 4, 2);
                $d = "{$d1}-{$d2}-01";
                break;
            case 'day':
                $d = $x['date'];
        }
        $res[$d] = array_merge($x, (array) $res[$d]);
    }
    /**/
    //////
    $res1 = array();
    $keys = array_keys($res);
    $min = @min($keys);
    $max = @max($keys);
    list($min, $e) = round_period($min, $min, $vars['discretion']);
    list($m, $max) = round_period($max, $max, $vars['discretion']);
    for ($k = $min; $k <= $max; list($k, $e) = next_period($k, $vars['discretion'])) {
        switch ($vars['discretion']) {
            case 'week':
                $dp = strftime($config['date_format'], strtotime($k)) . ' - ' . strftime($config['date_format'], strtotime($e));
                break;
            case 'month':
                $dp = date("m/Y", strtotime($k));
                break;
            case 'day':
                $dp = strftime("%a&nbsp;" . $config['date_format'], strtotime($k));
                break;
        }
        $d = $k;
        //        $res1[$d]['date'] = $d;
        $totals[0] = 'TOTAL';
        $totals[1] += $res[$d]['paid_count'];
        $totals[2] += $res[$d]['added_count'];
        $totals[3] += $res[$d]['completed_count'];
        $totals[4] += $res[$d]['added_amount'];
        $totals[5] += $res[$d]['completed_amount'];
        $totals[6] = '';
        $res1[$d][0] = $dp;
        $res1[$d][1] = intval($res[$d]['paid_count']);
        $res1[$d][2] = intval($res[$d]['added_count']);
        $res1[$d][3] = intval($res[$d]['completed_count']);
        $res1[$d][4] = number_format($res[$d]['added_amount'], 2, '.', ',');
        $res1[$d][5] = number_format($res[$d]['completed_amount'], 2, '.', ',');
        if ($max_total) {
            $x = round(100 * $res[$d]['completed_amount'] / $max_total);
            $p = round(100 * $res[$d]['completed_amount'] / $total_completed);
            if ($x) {
                $res1[$d][6] = "\n            <table align=left width={$x} cellpadding=0 cellspacing=0 style='font-size: 5pt;' height=6><tr><td bgcolor=red style='background-color: red;'></td></tr></table>\n            &nbsp;({$p}%)\n            ";
            } else {
                $res1[$d][6] = '';
            }
        }
    }
    ksort($res1);
    $totalacv = $totals[5];
    $totals[4] = number_format($totals[4], 2, '.', ',');
    $totals[5] = number_format($totals[5], 2, '.', ',');
    ///// DISPLAY RESULT
    $t->assign('header', array(0 => 'Date', 1 => 'Paid Count', 2 => 'Added Count', 3 => 'Completed Count', 4 => 'Added Amount', 5 => 'Completed Amount', 6 => 'Percent Graph'));
    $acv = $totals[1] > 0 ? $totalacv / $totals[1] : 0;
    $acv = round($acv, 3);
    $t->assign('acv', $acv);
    $t->assign('title', 'Income Report');
    $t->assign('report', $res1);
    $t->assign('totals', $totals);
    $t->display('admin/header.inc.html');
    $otd = $t->template_dir;
    $t->template_dir = dirname(__FILE__);
    $t->display('income_result.inc.html');
    $t->template_dir = $otd;
    $t->display('admin/footer.inc.html');
    print "<br /><br /><font size=1>Creation date: " . strftime($config['time_format']) . "</font>";
}
function calendar_widget()
{
    global $filters, $is_archive;
    $start_offset = gmdate('w', gmmktime(12, 0, 0, $filters['mo'], 1, $filters['yr']));
    $days_in_month = days_in_month($filters['mo'], $filters['yr']);
    $table = array();
    for ($d = 1; $d <= $days_in_month; $d++) {
        $this_w = intval(floor(($d + $start_offset - 1) / 7));
        $target_w = $this_w;
        if (!isset($table[$target_w])) {
            $table[$target_w] = array();
            for ($x = 0; $x < 7; $x++) {
                $table[$target_w][$x] = 0;
            }
        }
        $table[$target_w][$d + $start_offset - 1 - $this_w * 7] = $d;
    }
    $prev = prev_period($filters, true);
    $prev_link = '<a href="./' . filter_url($prev) . '" title="' . date_label($prev, false) . '">&larr;</a>';
    if ($filters['yr'] < date('Y') || $filters['mo'] < date('n')) {
        $next = next_period($filters, true);
        $next_link = '<a href="./' . filter_url($next) . '" title="' . date_label($next, false) . '">&rarr;</a>';
    } else {
        $next_link = '';
    }
    echo '<table class="calendar center"><thead>';
    echo '<tr>';
    echo "<th>{$prev_link}";
    echo '<th colspan="5"><a href="./' . filter_url(next_period($prev)) . '" title="' . date_label($filters, false) . '">' . date_label($filters, false) . '</a>';
    echo "<th>{$next_link}";
    if ($is_archive) {
        echo '</table>';
        return;
    }
    echo '<tbody>';
    echo '<tr>';
    foreach (array(__('Sunday'), __('Monday'), __('Tuesday'), __('Wednesday'), __('Thursday'), __('Friday'), __('Saturday')) as $day) {
        $day = htmlspecialchars($day);
        $d = substr($day, 0, 1);
        echo "<th title='{$day}'>{$d}";
    }
    $actual_dy = intval(date('d'));
    $actual_mo = intval(date('m'));
    $actual_yr = intval(date('Y'));
    $dy_filters = $filters;
    for ($w = 0; $w < sizeof($table); $w++) {
        echo '<tr>';
        for ($d = 0; $d < 7; $d++) {
            $class = isset($filters['dy']) && $filters['dy'] == $table[$w][$d] ? ' class="selected"' : '';
            echo "<td{$class}>";
            if ($table[$w][$d] > 0) {
                if ($filters['yr'] == $actual_yr && $filters['mo'] == $actual_mo && $table[$w][$d] > $actual_dy) {
                    echo '<a class="future">' . $table[$w][$d] . '</a>';
                } else {
                    $dy_filters['dy'] = $table[$w][$d];
                    echo '<a href="./' . filter_url($dy_filters) . '" title="';
                    echo date_label($filters, $table[$w][$d]) . '">' . $table[$w][$d] . '</a>';
                }
            }
        }
    }
    echo '</table>';
}
function tax_display_report($vars)
{
    global $t, $db, $config;
    // get income
    $beg_tm = $vars['beg_date'] . ' 00:00:00';
    $end_tm = $vars['end_date'] . ' 23:59:59';
    $res = array();
    switch ($vars['discretion']) {
        case 'week':
            $what = $group = 'YEARWEEK(tm_completed)';
            break;
        case 'month':
            $what = $group = "DATE_FORMAT(tm_completed, '%Y%m')";
            break;
        case 'day':
            $what = 'FROM_DAYS(TO_DAYS(tm_completed))';
            $group = 'TO_DAYS(tm_completed)';
    }
    $q = $db->query($s = "SELECT {$what} as date,\n        count(payment_id) as completed_count, sum(tax_amount) as completed_amount, sum(amount) as completed_total\n        FROM {$db->config[prefix]}payments p\n        WHERE tm_completed BETWEEN '{$beg_tm}' AND '{$end_tm}'\n        AND completed>0 and tax_amount > 0\n        GROUP BY {$group}\n        ");
    $max_total = 0;
    while ($x = mysql_fetch_assoc($q)) {
        switch ($vars['discretion']) {
            case 'week':
                $year = substr($x['date'], 0, 4);
                $weeknum = substr($x['date'], 4, 2);
                if ($weeknum == 53) {
                    $weeknum = 0;
                    $year++;
                }
                $w = date('w', strtotime("{$year}-01-01"));
                $weekstartday = $weeknum * 7 - $w;
                $d = date('Y-m-d', strtotime($year . '-01-01') + $weekstartday * 3600 * 24);
                break;
            case 'month':
                $d1 = substr($x['date'], 0, 4);
                $d2 = substr($x['date'], 4, 2);
                $d = "{$d1}-{$d2}-01";
                break;
            case 'day':
                $d = $x['date'];
        }
        $res[$d] = array_merge($x, (array) $res[$d]);
        $total_completed += $x['completed_amount'];
        if ($x['completed_amount'] > $max_total) {
            $max_total = $x['completed_amount'];
        }
    }
    $res1 = array();
    $keys = array_keys($res);
    if (count($keys) > 0) {
        $min = @min($keys);
        $max = @max($keys);
    } else {
        $min = 0;
        $max = 0;
    }
    list($min, $e) = round_period($min, $min, $vars['discretion']);
    list($m, $max) = round_period($max, $max, $vars['discretion']);
    for ($k = $min; $k <= $max; list($k, $e) = next_period($k, $vars['discretion'])) {
        switch ($vars['discretion']) {
            case 'week':
                $dp = strftime($config['date_format'], strtotime($k)) . ' - ' . strftime($config['date_format'], strtotime($e));
                break;
            case 'month':
                $dp = date("m/Y", strtotime($k));
                break;
            case 'day':
                $dp = strftime("%a&nbsp;" . $config['date_format'], strtotime($k));
                break;
        }
        $d = $k;
        //        $res1[$d]['date'] = $d;
        $totals[0] = 'TOTAL';
        $totals[1] += $res[$d]['completed_total'];
        $totals[2] += $res[$d]['completed_amount'];
        $res1[$d][0] = $dp;
        $res1[$d][1] = number_format($res[$d]['completed_total'], 2, '.', ',');
        $res1[$d][2] = number_format($res[$d]['completed_amount'], 2, '.', ',');
    }
    ksort($res1);
    $totals[1] = number_format($totals[1], 2, '.', ',');
    $totals[2] = number_format($totals[2], 2, '.', ',');
    ///// DISPLAY RESULT
    $t->assign('header', array(0 => 'Date', 1 => 'Total Amount', 2 => 'Tax Amount'));
    $t->assign('title', 'Tax Report');
    $t->assign('report', $res1);
    $t->assign('totals', $totals);
    $t->display('admin/header.inc.html');
    $otd = $t->template_dir;
    $t->template_dir = dirname(__FILE__);
    $t->display('income_result.inc.html');
    $t->template_dir = $otd;
    $t->display('admin/footer.inc.html');
    print "<br><br><font size=1>Creation date: " . strftime($config['time_format']) . "</font>";
}