include "../lib/smarty.php";
include "../lib/A2B_invoice.php";
session_start();
getpost_ifset(array('billcalls', 'billcharges', 'entercustomer', 'enterprovider', 'entertrunk', 'posted', 'period', 'frommonth', 'fromstatsmonth', 'tomonth', 'tostatsmonth', 'fromday', 'fromstatsmonth_sday', 'fromstatsday_sday', 'fromstatsmonth_shour', 'fromstatsmonth_smin', 'today', 'tostatsmonth_sday', 'tostatsday_sday', 'tostatsmonth_shour', 'tostatsmonth_smin', 'dsttype', 'dst', 'srctype', 'src', 'choose_currency', 'exporttype', 'templatefile'));
$verbose_level = 0;
$nowdate = date('Y-m-d H:i:s');
$DBHandle = DbConnect();
// because we cannot display debug & PDF on the same page
if ($exporttype != 'pdf') {
    $smarty->display('main.tpl');
} else {
    $verbose_level = 0;
}
if ($posted == 1) {
    // Create Invoice object
    $invoice = new A2B_Invoice($DBHandle, $verbose_level);
    // Configure Invoice
    if ($enterprovider != "") {
        $invoice->filter_provider = $enterprovider;
    }
    if ($entertrunk != "") {
        $invoice->filter_trunk = $entertrunk;
    }
    // Filter on Customer
    if (isset($entercustomer) && $entercustomer != "") {
        $invoice->ReadCardInfo('', $entercustomer);
        $invoice->FindCoverDates($billcalls == 'on', $billcharges == 'on', $nowdate);
    }
    // Filter on source & destination
    if ($src != "") {
        $invoice->filter_source = $src;
    $tocustomer = $forcustomer = $customer;
}
$QUERY = "SELECT count(*) FROM cc_card WHERE ID >= '{$forcustomer}' AND ID <= '{$tocustomer}'";
$result = $instance_table->SQLExec($HD_Form->DBHandle, $QUERY);
$nb_card = $result[0][0];
$nbpagemax = intval($nb_card / $groupcard);
if ($verbose_level >= 1) {
    echo "===> NB_CARD : {$nb_card} - NBPAGEMAX:{$nbpagemax}\n";
}
if ($nb_card < 1) {
    if ($verbose_level >= 1) {
        echo "[No card to create the Invoice]\n";
    }
} else {
    // Create Invoice object
    $invoice = new A2B_Invoice($HD_Form->DBHandle, $verbose_level);
    // For each customer
    for ($page = 0; $page <= $nbpagemax; $page++) {
        if ($verbose_level >= 1) {
            echo "{$page} <= {$nbpagemax} \n";
        }
        $Query_Customers = "SELECT id FROM cc_card WHERE ID >= '{$forcustomer}' AND ID <= '{$tocustomer}'";
        if ($A2B->config["database"]['dbtype'] == "postgres") {
            $Query_Customers .= " LIMIT {$groupcard} OFFSET " . $page * $groupcard;
        } else {
            $Query_Customers .= " LIMIT " . $page * $groupcard . ", {$groupcard}";
        }
        $resmax = $instance_table->SQLExec($HD_Form->DBHandle, $Query_Customers);
        if (is_array($resmax)) {
            $numrow = count($resmax);
            if ($verbose_level >= 2) {
$verbose_level = 0;
$payment_status_list = array();
$payment_status_list["0"] = array(gettext("UNPAID"), "0");
$payment_status_list["1"] = array(gettext("SENT-UNPAID"), "1");
$payment_status_list["2"] = array(gettext("SENT-PAID"), "2");
$payment_status_list["3"] = array(gettext("PAID"), "3");
getpost_ifset(array('invoice_type', 'cardid', 'billcalls', 'billcharges', 'id', 'exporttype', 'payment_status', 'templatefile'));
// Because, in order to display pdf, there shall be no html displayed
if ($exporttype == 'pdf') {
    $verbose_level = 0;
}
// Connect to Database
$DBHandle = DbConnect();
try {
    // Create Invoice object
    $invoice = new A2B_Invoice($DBHandle, $verbose_level);
    // Collect Data
    switch ($invoice_type) {
        case "":
        case 1:
            $invoice->RetrieveInformation($cardid, $billcalls == 'on', $billcharges == 'on', date('Y-m-d H:i:s'));
            $invoice->CreateInvoice();
            $invoice_type_name = 'outstanding';
            break;
        case 2:
            $invoice->LoadInvoice($id);
            // Change Payment Status if posted so
            if ($payment_status != "") {
                $QUERY = "UPDATE cc_invoice SET payment_status ='{$payment_status}' WHERE id='{$id}'";
                $DBHandle->Execute($QUERY);
                if ($verbose_level >= 1) {