function print_statements()
{
    global $path_to_root, $systypes_array;
    include_once $path_to_root . "/reporting/includes/pdf_report2.inc";
    $from = date2sql($_POST['PARAM_0']);
    $to = date2sql($_POST['PARAM_1']);
    $customer = $_POST['PARAM_2'];
    $currency = $_POST['PARAM_3'];
    $email = $_POST['PARAM_4'];
    $comments = $_POST['PARAM_5'];
    $orientation = $_POST['PARAM_6'];
    $orientation = $orientation ? 'L' : 'P';
    $dec = 2;
    $cols = array(4, 64, 180, 250, 300, 350, 400, 480);
    //$headers in doctext.inc
    $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
    $params = array('comments' => $comments);
    $cur = get_company_pref('curr_default');
    $PastDueDays1 = get_company_pref('past_due_days');
    $PastDueDays2 = 2 * $PastDueDays1;
    if ($email == 0) {
        $rep = new FrontReport(_('CUSTOMER ACCOUNT STATEMENT'), "StatementBulk", user_pagesize(), 9, $orientation);
    }
    if ($orientation == 'L') {
        recalculate_cols($cols);
    }
    $sql = "SELECT b.debtor_no, b.name AS DebtorName, b.address, b.tax_id, b.curr_code, cust.salesman, \n\tcurdate() AS tran_date, CONCAT (d.name, d.name2) AS contactPerson, d.phone, d.phone2  \n\tFROM " . TB_PREF . "debtors_master b INNER JOIN\n" . TB_PREF . "crm_contacts c on b.debtor_no=c.entity_id INNER JOIN " . TB_PREF . "crm_persons d on c.person_id=d.id\nINNER JOIN " . TB_PREF . "cust_branch cust on b.debtor_no=cust.debtor_no";
    if ($customer != ALL_TEXT) {
        $sql .= " WHERE c.type='customer' and cust.salesman = " . db_escape($customer);
    } else {
        $sql .= " where c.type='customer' and cust.salesman = " . db_escape($customer) . " ORDER by b.name";
    }
    $result = db_query($sql, "The customers could not be retrieved");
    while ($debtor_row = db_fetch($result)) {
        $date = date('Y-m-d');
        if ($from != $to) {
            // find the latest point where the balance was null
            $start = findLatestNullDate($debtor_row['debtor_no'], $from);
            // but not earlier than the $to date.
            if (date1_greater_date2(sql2date($start), sql2date($to))) {
                $start = $to;
            }
            if (date1_greater_date2(sql2date($from), sql2date($start))) {
                $start = $from;
            }
        } else {
            $start = $from;
        }
        $debtor_row['order_'] = "";
        $TransResult = getTransactions($debtor_row['debtor_no'], $start, $date);
        $baccount = get_default_bank_account($debtor_row['curr_code']);
        $params['bankaccount'] = $baccount['id'];
        if (db_num_rows($TransResult) == 0) {
            continue;
        }
        if ($email == 1) {
            $rep = new FrontReport("CUSTOMER ACCOUNT STATEMENT", "", user_pagesize(), 9, $orientation);
            $rep->title = _('STATEMENT OF ACCOUNT');
            $rep->filename = "Statement" . $debtor_row['debtor_no'] . ".pdf";
            $rep->Info($params, $cols, null, $aligns);
        }
        $rep->filename = "ST-" . strtr($debtor_row['DebtorName'], " '", "__") . "--" . strtr(Today(), "/", "-") . ".pdf";
        $contacts = get_customer_contacts($debtor_row['debtor_no'], 'invoice');
        $rep->SetHeaderType('customheader');
        $rep->currency = $cur;
        $rep->Font();
        $rep->Info(null, $cols, null, $aligns);
        $rep->SetCommonData($debtor_row, null, null, $baccount, ST_STATEMENT, $contacts);
        $rep->NewPage();
        $doctype = ST_STATEMENT;
        //$rep->TextCol(0,4,"yeah");
        $current = false;
        $balance = getInitialBalance($debtor_row['debtor_no'], $start);
        if (true || Abs($balance) > 1.0E-6) {
            if (Abs($balance) < 1.0E-6) {
                $rep->SetTextColor(190, 190, 190);
            } else {
                if ($balance > 0) {
                    $rep->SetTextColor(190, 0, 0);
                }
            }
            $rep->SetTextColor(0, 0, 0);
        }
        $overdue = 0;
        $prev = '';
        $gross_amount = 0;
        $gross_amount2 = 0;
        $payment_tot = 0;
        $tots = 0;
        $discount_amount = 0;
        $percent = 0;
        while ($transaction_row = db_fetch($TransResult)) {
            if ($myrow['IsVoid'] == '') {
                $company_data = get_company_prefs();
                $branch = get_branch($transaction_row["branch_code"]);
                $branch_data = get_branch_accounts($transaction_row['branch_code']);
                $dt = get_discount($branch_data['sales_discount_account'], $transaction_row['type'], $transaction_row['trans_no']);
                $DisplayTotal = number_format2(Abs($transaction_row["TotalAmount"] + $dt), $dec);
                $DisplayAlloc = number_format2($transaction_row["Allocated"], $dec);
                $DisplayNet = number_format2($transaction_row["TotalAmount"] - $transaction_row["Allocated"], $dec);
                /*if ($dt != 0 && $transaction_row['type'] == ST_SALESINVOICE || $transaction_row['type'] == ST_CUSTCREDIT)
                		{
                			$discount_amount += $dt;
                				$ctr = $transaction_row['bulk_discount'];
                		}*/
                $amount = $transaction_row["TotalAmount"] + $dt;
                $balance += $transaction_row["TotalAmount"];
                $invoice_no = get_custom_no($transaction_row['trans_no'], $transaction_row['type']);
                $enter1 = 0;
                if ($systypes_array[$transaction_row['type']] == "Customer Payment") {
                    $open_pay = get_payment_invoice_details($transaction_row["trans_no"], ST_CUSTPAYMENT);
                    if ($open_pay) {
                        $stat2 = false;
                    } else {
                        $stat2 = true;
                        $payment_tot += $amount;
                        $text = "pr#";
                    }
                }
                if ($systypes_array[$transaction_row['type']] == "Sales Invoice") {
                    if ($transaction_row['ov_amount'] > $transaction_row['alloc'] || $transaction_row['alloc'] == 0) {
                        $discount_amount += $dt;
                        $ctr = $transaction_row['bulk_discount'];
                        $gross_amount += $amount;
                        $text = '';
                        $stat3 = true;
                    } else {
                        $stat3 = false;
                    }
                }
                if ($systypes_array[$transaction_row['type']] == "Customer Credit Note") {
                    $open = get_sales_invoice_details($transaction_row['order_'], ST_SALESINVOICE);
                    if ($open) {
                        $stat = false;
                    } else {
                        $discount_amount += $dt;
                        $ctr = $transaction_row['bulk_discount'];
                        $gross_amount2 += $amount;
                        $stat = true;
                    }
                    $text = "cm#";
                }
                $current = $text;
                $tot = $gross_amount + $gross_amount2 - $discount_amount;
                if ($current != '') {
                    if ($prev == $current) {
                    } else {
                        if ($prev == "" && $text == "cm#" && $stat) {
                            $rep->NewLine();
                            $rep->TextCol(1, 2, "Less Returns");
                            $rep->NewLine();
                        }
                        if ($text == 'pr#' && $prev == "cm#" && $stat2) {
                            $rep->AmountCol(6, 7, $tot, 2);
                            $rep->NewLine(2);
                            $rep->TextCol(1, 2, "Less Payments:");
                            $rep->NewLine();
                        }
                        if ($prev == '' && $text == 'pr#' && $stat2) {
                            $rep->TextCol(1, 2, "Less Payments:");
                            $rep->NewLine();
                        }
                    }
                    $prev = $current;
                }
                if ($transaction_row['type'] == ST_SALESINVOICE && $stat3) {
                    $rep->TextCol(1, 2, $text . $invoice_no, -2);
                    $rep->TextCol(0, 3, sql2date($transaction_row['tran_date']), -2);
                    $rep->TextCol(3, 4, $DisplayTotal, -2);
                    $rep->NewLine();
                }
                if ($transaction_row['type'] == ST_CUSTCREDIT && $stat) {
                    $rep->TextCol(1, 2, $text . $invoice_no, -2);
                    $rep->TextCol(0, 3, sql2date($transaction_row['tran_date']), -2);
                    $rep->TextCol(3, 4, $DisplayTotal, -2);
                    $rep->NewLine();
                }
                if ($transaction_row['type'] == ST_CUSTPAYMENT && $stat2) {
                    if ($invoice_no == "") {
                        $rep->TextCol(1, 2, $text . $transaction_row['reference'], -2);
                    } else {
                        $rep->TextCol(1, 2, $text . $invoice_no, -2);
                    }
                    $rep->TextCol(0, 3, sql2date($transaction_row['tran_date']), -2);
                    $rep->TextCol(5, 6, $DisplayTotal, -2);
                    $rep->NewLine();
                }
                $rep->SetTextColor(0, 0, 0);
                //$rep->NewLine();
                if ($rep->row < $rep->bottomMargin + 10 * $rep->lineHeight) {
                    $rep->NewPage();
                }
            }
        }
        if (!$current) {
            $overdue = $balance;
            $balance = 0;
        }
        $rep->NewLine();
        $net = $gross_amount - abs($gross_amount2);
        $percent = $ctr / $net * 100;
        $per = number_format2($percent, 2);
        if ($per != 0) {
            $rep->TextCol(1, 2, "Less  " . $ctr . "% discount");
            $rep->AmountCol(5, 6, $discount_amount, 2);
        }
        $rep->SetTextColor(0, 0, 0);
        $rep->fontSize += 2;
        $rep->NewLine(5);
        $rep->TextCol(1, 2, 'Amount Due');
        //$rep->TextCol(6,7, "     	____________", -2);
        if ($payment_tot != 0) {
            $rep->TextCol(6, 7, number_format2($tot - abs($payment_tot), $dec));
        } else {
            $rep->AmountCol(6, 7, $tot, 2);
        }
        $rep->NewLine(5);
        $rep->TextCol(2, 4, "Verified & Checked by:");
        $rep->TextCol(4, 6, "___________________");
        $rep->NewLine();
        $rep->TextCol(4, 6, "Credit & Collection");
        $rep->fontSize -= 2;
    }
    //$rep->NewPage();
    if ($email == 0) {
        $rep->End();
    }
}
function print_statements()
{
    global $path_to_root, $systypes_array;
    include_once $path_to_root . "/reporting/includes/pdf_report.inc";
    $from = date2sql($_POST['PARAM_0']);
    $to = date2sql($_POST['PARAM_1']);
    $customer = $_POST['PARAM_2'];
    $currency = $_POST['PARAM_3'];
    $email = $_POST['PARAM_4'];
    $comments = $_POST['PARAM_5'];
    $orientation = $_POST['PARAM_6'];
    $orientation = $orientation ? 'L' : 'P';
    $dec = user_price_dec();
    $cols = array(4, 64, 180, 250, 320, 385, 450, 515);
    //$headers in doctext.inc
    $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
    $params = array('comments' => $comments);
    $cur = get_company_pref('curr_default');
    $PastDueDays1 = get_company_pref('past_due_days');
    $PastDueDays2 = 2 * $PastDueDays1;
    if ($email == 0) {
        $rep = new FrontReport(_('CUSTOMER ACCOUNT STATEMENT'), "StatementBulk", user_pagesize(), 9, $orientation);
    }
    if ($orientation == 'L') {
        recalculate_cols($cols);
    }
    $sql = "SELECT b.debtor_no, b.name AS DebtorName, b.address, b.tax_id, b.curr_code, curdate() AS tran_date, CONCAT (d.name, d.name2) AS contactPerson, d.phone, d.phone2  FROM " . TB_PREF . "debtors_master b INNER JOIN\n" . TB_PREF . "crm_contacts c on b.debtor_no=c.entity_id INNER JOIN " . TB_PREF . "crm_persons d on c.person_id=d.id";
    if ($customer != ALL_TEXT) {
        $sql .= " WHERE b.debtor_no = " . db_escape($customer);
    } else {
        $sql .= " ORDER by b.name";
    }
    $result = db_query($sql, "The customers could not be retrieved");
    while ($debtor_row = db_fetch($result)) {
        $date = date('Y-m-d');
        if ($from != $to) {
            // find the latest point where the balance was null
            $start = findLatestNullDate($debtor_row['debtor_no'], $from);
            // but not earlier than the $to date.
            if (date1_greater_date2(sql2date($start), sql2date($to))) {
                $start = $to;
            }
            if (date1_greater_date2(sql2date($from), sql2date($start))) {
                $start = $from;
            }
        } else {
            $start = $from;
        }
        $debtor_row['order_'] = "";
        $TransResult = getTransactions($debtor_row['debtor_no'], $start, $date);
        $baccount = get_default_bank_account($debtor_row['curr_code']);
        $params['bankaccount'] = $baccount['id'];
        if (db_num_rows($TransResult) == 0) {
            continue;
        }
        if ($email == 1) {
            $rep = new FrontReport("CUSTOMER ACCOUNT STATEMENT", "", user_pagesize(), 9, $orientation);
            $rep->title = _('CUSTOMER ACCOUNT STATEMENT');
            $rep->filename = "Statement" . $debtor_row['debtor_no'] . ".pdf";
            $rep->Info($params, $cols, null, $aligns);
        }
        $rep->filename = "ST-" . strtr($debtor_row['DebtorName'], " '", "__") . "--" . strtr(Today(), "/", "-") . ".pdf";
        $contacts = get_customer_contacts($debtor_row['debtor_no'], 'invoice');
        $rep->SetHeaderType(0);
        $rep->currency = $cur;
        $rep->Font();
        $rep->Info(null, $cols, null, $aligns);
        //= get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no']);
        $rep->SetCommonData($debtor_row, null, null, $baccount, ST_STATEMENT, $contacts);
        $rep->NewPage();
        $doctype = ST_STATEMENT;
        /*
        		$rep->NewLine();
        		$rep->fontSize += 2;
        		$rep->TextCol(0, 7, _("Overdue"));
        		$rep->fontSize -= 2;
        		$rep->NewLine(2);
        */
        $rep->NewLine(10);
        $rep->TextCol(0, 5, $debtor_row['DebtorName']);
        $rep->NewLine();
        $rep->TextCol(0, 5, $debtor_row['contactPerson']);
        $rep->NewLine();
        $rep->TextCol(0, 5, $debtor_row['address']);
        $rep->NewLine();
        $rep->TextCol(0, 5, $debtor_row['phone'] . " " . $debtor_row['phone2']);
        $rep->NewLine(5);
        $current = false;
        $balance = getInitialBalance($debtor_row['debtor_no'], $start);
        if (true || Abs($balance) > 1.0E-6) {
            // Display initial balance
            //$rep->TextCol(1, 4, 'Balance Brought Forward');
            if (Abs($balance) < 1.0E-6) {
                $rep->SetTextColor(190, 190, 190);
            } else {
                if ($balance > 0) {
                    $rep->SetTextColor(190, 0, 0);
                }
            }
            //$rep->TextCol(6, 7,	number_format2(-$balance, $dec), -2);
            $rep->SetTextColor(0, 0, 0);
            //$rep->NewLine();
        }
        $overdue = 0;
        while ($transaction_row = db_fetch($TransResult)) {
            if (!$current && !$transaction_row['OverDue'] == true) {
                $rep->fontSize += 2;
                //$rep->NewLine(2);
                //$rep->TextCol(0, 7, _("Due Soon"));
                $rep->fontSize -= 2;
                $current = true;
                $overdue = $balance;
                /* Reset the balance. so we have a separate balance for overdue
                 * and current. However if the customer is in credit
                 * don't reset the balance.
                 * Example : A Customer has made a payment before the invoice
                 * is overdue. The total balance after the invoice should be 0.
                 */
                if ($balance > 0) {
                    $balance = 0;
                } else {
                    $overdue = 0;
                }
                $rep->NewLine(2);
            }
            if ($current) {
                $rep->SetTextColor(0, 0, 190);
            }
            $DisplayTotal = number_format2(Abs($transaction_row["TotalAmount"]), $dec);
            $DisplayAlloc = number_format2($transaction_row["Allocated"], $dec);
            $DisplayNet = number_format2($transaction_row["TotalAmount"] - $transaction_row["Allocated"], $dec);
            $balance += $transaction_row["TotalAmount"];
            if ($systypes_array[$transaction_row['type']] == "Customer Payment") {
                $invoice_no = get_custom_no($transaction_row['trans_no'], 53);
            } else {
                $invoice_no = get_custom_no($transaction_row['trans_no'], $transaction_row['type']);
            }
            if ($systypes_array[$transaction_row['type']] == "Sales Invoice") {
                $typename = "Charge Invoice";
            } else {
                $typename = $systypes_array[$transaction_row['type']];
            }
            $rep->TextCol(1, 1, $typename, -2);
            $rep->TextCol(2, 2, $invoice_no, -2);
            $rep->TextCol(0, 3, sql2date($transaction_row['EffectiveDate']), -2);
            if ($transaction_row['type'] == ST_SALESINVOICE) {
                $rep->TextCol(3, 4, sql2date($transaction_row['tran_date']), -2);
            }
            if ($transaction_row['type'] == ST_SALESINVOICE || $transaction_row['type'] == ST_BANKPAYMENT) {
                $rep->TextCol(4, 5, $DisplayTotal, -2);
            } else {
                $rep->TextCol(5, 6, $DisplayTotal, -2);
            }
            if (!$current) {
                if (Abs($balance) < 1.0E-6) {
                    $rep->SetTextColor(190, 190, 190);
                } else {
                    if ($balance > 0) {
                        $rep->SetTextColor(190, 0, 0);
                    }
                }
            }
            $rep->TextCol(6, 7, number_format2(-$balance, $dec), -2);
            $rep->SetTextColor(0, 0, 0);
            $rep->NewLine();
            if ($rep->row < $rep->bottomMargin + 10 * $rep->lineHeight) {
                $rep->NewPage();
            }
        }
        if (!$current) {
            $overdue = $balance;
            $balance = 0;
        }
        // Total
        $rep->NewLine();
        $rep->SetTextColor(0, 0, 0);
        $rep->fontSize += 2;
        $rep->NewLine(18);
        $rep->TextCol(1, 2, 'Total Balance');
        $rep->TextCol(2, 3, number_format2(-($balance + $overdue), $dec));
        if ($overdue > 1.0E-6) {
            // $rep->fontSize += 2;
            $rep->NewLine(2);
            $rep->SetTextColor(190, 0, 0);
            $rep->TextCol(5, 6, 'Overdue');
            $rep->TextCol(6, 7, number_format2($overdue, $dec));
            $rep->TextCol(2, 5, 'PLEASE PAY NOW');
            //	$rep->fontSize -= 2;
            $rep->SetTextColor(0, 0, 0);
            $rep->NewLine();
        }
        $rep->fontSize -= 2;
        /*
        		$nowdue = "1-" . $PastDueDays1 . " " . _("Days");
        		$pastdue1 = $PastDueDays1 + 1 . "-" . $PastDueDays2 . " " . _("Days");
        		$pastdue2 = _("Over") . " " . $PastDueDays2 . " " . _("Days");
        		$CustomerRecord = get_customer_details($debtor_row['debtor_no'], null, $show_also_allocated);
        		$str = array(_("Current"), $nowdue, $pastdue1, $pastdue2, _("Total Balance"));
        		$str2 = array(number_format2(($CustomerRecord["Balance"] - $CustomerRecord["Due"]),$dec),
        			number_format2(($CustomerRecord["Due"]-$CustomerRecord["Overdue1"]),$dec),
        			number_format2(($CustomerRecord["Overdue1"]-$CustomerRecord["Overdue2"]) ,$dec),
        			number_format2($CustomerRecord["Overdue2"],$dec),
        			number_format2($CustomerRecord["Balance"],$dec));
        		$col = array($rep->cols[0], $rep->cols[0] + 110, $rep->cols[0] + 210, $rep->cols[0] + 310,
        			$rep->cols[0] + 410, $rep->cols[0] + 510);
        		$rep->row = $rep->bottomMargin + (10 * $rep->lineHeight - 6);
        		for ($i = 0; $i < 5; $i++)
        			$rep->TextWrap($col[$i], $rep->row, $col[$i + 1] - $col[$i], $str[$i], 'right');
        		$rep->NewLine();
        		for ($i = 0; $i < 5; $i++)
        			$rep->TextWrap($col[$i], $rep->row, $col[$i + 1] - $col[$i], $str2[$i], 'right');
        */
        if ($email == 1) {
            $rep->End($email, _("Statement") . " " . _("as of") . " " . sql2date($date));
        }
    }
    if ($email == 0) {
        $rep->End();
    }
}