<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[FrontAccounting forum — Print Customer Balance On Invoice]]></title>
	<link rel="self" href="https://frontaccounting.com/punbb/extern.php?action=feed&amp;tid=5803&amp;type=atom" />
	<updated>2018-01-18T21:59:08Z</updated>
	<generator>PunBB</generator>
	<id>https://frontaccounting.com/punbb/viewtopic.php?id=5803</id>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=30548#p30548" />
			<content type="html"><![CDATA[<p>seems like there is a bug with this code, when you print an invoice for a customer who have a credit note the balance w&#039;ll be incorrect.</p>]]></content>
			<author>
				<name><![CDATA[Alaa]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=20274</uri>
			</author>
			<updated>2018-01-18T21:59:08Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=30548#p30548</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=30543#p30543" />
			<content type="html"><![CDATA[<p>@trafficpest: If the Balance is 0 (zero) then it is best suppressed.<br />@joe: Can it be an option in the existing one?.</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2018-01-18T05:27:01Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=30543#p30543</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=30540#p30540" />
			<content type="html"><![CDATA[<p>No, I guess this should be an extension.</p><p>/Joe</p>]]></content>
			<author>
				<name><![CDATA[joe]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=3</uri>
			</author>
			<updated>2018-01-17T10:57:52Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=30540#p30540</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=30539#p30539" />
			<content type="html"><![CDATA[<p>You will need to incorporate <a href="https://github.com/FrontAccountingERP/FA/commit/8333e3e6b2b09fb6d72d3605275a8212bd71de84">this fix</a> as well.</p><p>Attachment has it done.</p><p>@joe: Want to include it in the core?</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2018-01-17T07:23:46Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=30539#p30539</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=30537#p30537" />
			<content type="html"><![CDATA[<p>Ok here is a stock file with only the print invoice balance modifacations.</p><div class="codebox"><pre><code>&lt;?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
    Released under the terms of the GNU General Public License, GPL, 
    as published by the Free Software Foundation, either version 3 
    of the License, or (at your option) any later version.
    This program 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 License here &lt;http://www.gnu.org/licenses/gpl-3.0.html&gt;.
***********************************************************************/
$page_security = $_POST[&#039;PARAM_0&#039;] == $_POST[&#039;PARAM_1&#039;] ?
    &#039;SA_SALESTRANSVIEW&#039; : &#039;SA_SALESBULKREP&#039;;
// ----------------------------------------------------------------
// $ Revision:    2.0 $
// Creator:    Joe Hunt
// date_:    2005-05-19
// Title:    Print Invoices
// ----------------------------------------------------------------
$path_to_root=&quot;..&quot;;

include_once($path_to_root . &quot;/includes/session.inc&quot;);
include_once($path_to_root . &quot;/includes/date_functions.inc&quot;);
include_once($path_to_root . &quot;/includes/data_checks.inc&quot;);
include_once($path_to_root . &quot;/sales/includes/sales_db.inc&quot;);

//----------------------------------------------------------------------------------------------------
function get_invoice_range($from, $to)
{
    global $SysPrefs;

    $ref = ($SysPrefs-&gt;print_invoice_no() == 1 ? &quot;trans_no&quot; : &quot;reference&quot;);

    $sql = &quot;SELECT trans.trans_no, trans.reference
        FROM &quot;.TB_PREF.&quot;debtor_trans trans 
            LEFT JOIN &quot;.TB_PREF.&quot;voided voided ON trans.type=voided.type AND trans.trans_no=voided.id
        WHERE trans.type=&quot;.ST_SALESINVOICE
            .&quot; AND ISNULL(voided.id)&quot;
            .&quot; AND trans.reference&gt;=&quot;.db_escape(get_reference(ST_SALESINVOICE, $from))
            .&quot; AND trans.reference&lt;=&quot;.db_escape(get_reference(ST_SALESINVOICE, $to))
        .&quot; ORDER BY trans.tran_date, trans.$ref&quot;;

    return db_query($sql, &quot;Cant retrieve invoice range&quot;);
}

print_invoices();

//---------------------------------------------------------------------------------------------------- added from rep101 to get current balance


function get_open_balance($debtorno, $to)
{
    if($to)
        $to = date2sql($to);

     $sql = &quot;SELECT SUM(IF(t.type = &quot;.ST_SALESINVOICE.&quot; OR (t.type = &quot;.ST_JOURNAL.&quot; AND t.ov_amount&gt;0),
         -abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount), 0)) AS charges,&quot;;
     $sql .= &quot;SUM(IF(t.type != &quot;.ST_SALESINVOICE.&quot; AND NOT(t.type = &quot;.ST_JOURNAL.&quot; AND t.ov_amount&gt;0),
         abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) * -1, 0)) AS credits,&quot;;
     $sql .= &quot;SUM(IF(t.type != &quot;.ST_SALESINVOICE.&quot; AND NOT(t.type = &quot;.ST_JOURNAL.&quot; AND t.ov_amount&gt;0), t.alloc * -1, t.alloc)) AS Allocated,&quot;;

     $sql .=    &quot;SUM(IF(t.type = &quot;.ST_SALESINVOICE.&quot;, 1, -1) *
             (abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) - abs(t.alloc))) AS OutStanding
        FROM &quot;.TB_PREF.&quot;debtor_trans t
        WHERE t.debtor_no = &quot;.db_escape($debtorno)
        .&quot; AND t.type &lt;&gt; &quot;.ST_CUSTDELIVERY;
    if ($to)
        $sql .= &quot; AND t.tran_date &lt; &#039;$to+1&#039;&quot;; // Has +1 to include any invoices created today in the balance
    $sql .= &quot; GROUP BY debtor_no&quot;;

    $result = db_query($sql,&quot;No transactions were returned&quot;);
    return db_fetch($result);
}


//----------------------------------------------------------------------------------------------------

function print_invoices()
{
    global $path_to_root, $SysPrefs;
    
    $show_this_payment = true; // include payments invoiced here in summary

    include_once($path_to_root . &quot;/reporting/includes/pdf_report.inc&quot;);

    $from = $_POST[&#039;PARAM_0&#039;];
    $to = $_POST[&#039;PARAM_1&#039;];
    $currency = $_POST[&#039;PARAM_2&#039;];
    $email = $_POST[&#039;PARAM_3&#039;];
    $pay_service = $_POST[&#039;PARAM_4&#039;];
    $comments = $_POST[&#039;PARAM_5&#039;];
    $customer = $_POST[&#039;PARAM_6&#039;];
    $orientation = $_POST[&#039;PARAM_7&#039;];

    if (!$from || !$to) return;

    $orientation = ($orientation ? &#039;L&#039; : &#039;P&#039;);
    $dec = user_price_dec();

     $fno = explode(&quot;-&quot;, $from);
    $tno = explode(&quot;-&quot;, $to);
    $from = min($fno[0], $tno[0]);
    $to = max($fno[0], $tno[0]);

    //-------------code-Descr-Qty--uom--tax--prc--Disc-Tot--//
    $cols = array(4, 60, 225, 300, 325, 385, 450, 515);

    // $headers in doctext.inc
    $aligns = array(&#039;left&#039;,    &#039;left&#039;,    &#039;right&#039;, &#039;center&#039;, &#039;right&#039;, &#039;right&#039;, &#039;right&#039;);

    $params = array(&#039;comments&#039; =&gt; $comments);

    $cur = get_company_Pref(&#039;curr_default&#039;);

    if ($email == 0)
        $rep = new FrontReport(_(&#039;INVOICE&#039;), &quot;InvoiceBulk&quot;, user_pagesize(), 9, $orientation);
    if ($orientation == &#039;L&#039;)
        recalculate_cols($cols);

    $range = get_invoice_range($from, $to);
    while($row = db_fetch($range))
    {
            if (!exists_customer_trans(ST_SALESINVOICE, $row[&#039;trans_no&#039;]))
                continue;
            $sign = 1;
            $myrow = get_customer_trans($row[&#039;trans_no&#039;], ST_SALESINVOICE);

            if ($customer &amp;&amp; $myrow[&#039;debtor_no&#039;] != $customer) {
                continue;
            }
            if ($currency != ALL_TEXT &amp;&amp; $myrow[&#039;curr_code&#039;] != $currency) {
                continue;
            }
            $baccount = get_default_bank_account($myrow[&#039;curr_code&#039;]);
            $params[&#039;bankaccount&#039;] = $baccount[&#039;id&#039;];

            $branch = get_branch($myrow[&quot;branch_code&quot;]);
            $sales_order = get_sales_order_header($myrow[&quot;order_&quot;], ST_SALESORDER);
            if ($email == 1)
            {
                $rep = new FrontReport(&quot;&quot;, &quot;&quot;, user_pagesize(), 9, $orientation);
                $rep-&gt;title = _(&#039;INVOICE&#039;);
                $rep-&gt;filename = &quot;Invoice&quot; . $myrow[&#039;reference&#039;] . &quot;.pdf&quot;;
            }    
            $rep-&gt;currency = $cur;
            $rep-&gt;Font();
            $rep-&gt;Info($params, $cols, null, $aligns);

            $contacts = get_branch_contacts($branch[&#039;branch_code&#039;], &#039;invoice&#039;, $branch[&#039;debtor_no&#039;], true);
            $baccount[&#039;payment_service&#039;] = $pay_service;
            $rep-&gt;SetCommonData($myrow, $branch, $sales_order, $baccount, ST_SALESINVOICE, $contacts);
            $rep-&gt;SetHeaderType(&#039;Header2&#039;);
            $rep-&gt;NewPage();
            // calculate summary start row for later use
            $summary_start_row = $rep-&gt;bottomMargin + (15 * $rep-&gt;lineHeight);

            if ($rep-&gt;formData[&#039;prepaid&#039;])
            {
                $result = get_sales_order_invoices($myrow[&#039;order_&#039;]);
                $prepayments = array();
                while($inv = db_fetch($result))
                {
                    $prepayments[] = $inv;
                    if ($inv[&#039;trans_no&#039;] == $row[&#039;trans_no&#039;])
                    break;
                }

                if (count($prepayments) &gt; ($show_this_payment ? 0 : 1))
                    $summary_start_row += (count($prepayments)) * $rep-&gt;lineHeight;
                else
                    unset($prepayments);
            }

               $result = get_customer_trans_details(ST_SALESINVOICE, $row[&#039;trans_no&#039;]);
            $SubTotal = 0;
            while ($myrow2=db_fetch($result))
            {
                if ($myrow2[&quot;quantity&quot;] == 0)
                    continue;

                $Net = round2($sign * ((1 - $myrow2[&quot;discount_percent&quot;]) * $myrow2[&quot;unit_price&quot;] * $myrow2[&quot;quantity&quot;]),
                   user_price_dec());
                $SubTotal += $Net;
                $DisplayPrice = number_format2($myrow2[&quot;unit_price&quot;],$dec);
                $DisplayQty = number_format2($sign*$myrow2[&quot;quantity&quot;],get_qty_dec($myrow2[&#039;stock_id&#039;]));
                $DisplayNet = number_format2($Net,$dec);
                if ($myrow2[&quot;discount_percent&quot;]==0)
                      $DisplayDiscount =&quot;&quot;;
                else
                      $DisplayDiscount = number_format2($myrow2[&quot;discount_percent&quot;]*100,user_percent_dec()) . &quot;%&quot;;
                $c=0;
                $rep-&gt;TextCol($c++, $c,    $myrow2[&#039;stock_id&#039;], -2);
                $oldrow = $rep-&gt;row;
                $rep-&gt;TextColLines($c++, $c, $myrow2[&#039;StockDescription&#039;], -2);
                $newrow = $rep-&gt;row;
                $rep-&gt;row = $oldrow;
                if ($Net != 0.0 || !is_service($myrow2[&#039;mb_flag&#039;]) || !$SysPrefs-&gt;no_zero_lines_amount())
                {
                    $rep-&gt;TextCol($c++, $c,    $DisplayQty, -2);
                    $rep-&gt;TextCol($c++, $c,    $myrow2[&#039;units&#039;], -2);
                    $rep-&gt;TextCol($c++, $c,    $DisplayPrice, -2);
                    $rep-&gt;TextCol($c++, $c,    $DisplayDiscount, -2);
                    $rep-&gt;TextCol($c++, $c,    $DisplayNet, -2);
                }
                $rep-&gt;row = $newrow;
                //$rep-&gt;NewLine(1);
                if ($rep-&gt;row &lt; $summary_start_row)
                    $rep-&gt;NewPage();
            }

            $memo = get_comments_string(ST_SALESINVOICE, $row[&#039;trans_no&#039;]);
            if ($memo != &quot;&quot;)
            {
                $rep-&gt;NewLine();
                $rep-&gt;TextColLines(1, 3, $memo, -2);
            }

               $DisplaySubTot = number_format2($SubTotal,$dec);

            // set to start of summary line:
            $rep-&gt;row = $summary_start_row;
            if (isset($prepayments))
            {
                // Partial invoices table
                $rep-&gt;TextCol(0, 3,_(&quot;Prepayments invoiced to this order up to day:&quot;));
                $rep-&gt;TextCol(0, 3,    str_pad(&#039;&#039;, 150, &#039;_&#039;));
                $rep-&gt;cols[2] -= 20;
                $rep-&gt;aligns[2] = &#039;right&#039;;
                $rep-&gt;NewLine(); $c = 0; $tot_pym=0;
                $rep-&gt;TextCol(0, 3,    str_pad(&#039;&#039;, 150, &#039;_&#039;));
                $rep-&gt;TextCol($c++, $c, _(&quot;Date&quot;));
                $rep-&gt;TextCol($c++, $c,    _(&quot;Invoice reference&quot;));
                $rep-&gt;TextCol($c++, $c,    _(&quot;Amount&quot;));

                foreach ($prepayments as $invoice)
                {
                    if ($show_this_payment || ($invoice[&#039;reference&#039;] != $myrow[&#039;reference&#039;]))
                    {
                        $rep-&gt;NewLine();
                        $c = 0; $tot_pym += $invoice[&#039;prep_amount&#039;];
                        $rep-&gt;TextCol($c++, $c,    sql2date($invoice[&#039;tran_date&#039;]));
                        $rep-&gt;TextCol($c++, $c,    $invoice[&#039;reference&#039;]);
                        $rep-&gt;TextCol($c++, $c, number_format2($invoice[&#039;prep_amount&#039;], $dec));
                    }
                    if ($invoice[&#039;reference&#039;]==$myrow[&#039;reference&#039;]) break;
                }
                $rep-&gt;TextCol(0, 3,    str_pad(&#039;&#039;, 150, &#039;_&#039;));
                $rep-&gt;NewLine();
                $rep-&gt;TextCol(1, 2,    _(&quot;Total payments:&quot;));
                $rep-&gt;TextCol(2, 3,    number_format2($tot_pym, $dec));
            }


            $doctype = ST_SALESINVOICE;
            $rep-&gt;row = $summary_start_row;
            $rep-&gt;cols[2] += 20;
            $rep-&gt;cols[3] += 20;
            $rep-&gt;aligns[3] = &#039;left&#039;;

            $rep-&gt;TextCol(3, 6, _(&quot;Sub-total&quot;), -2);
            $rep-&gt;TextCol(6, 7,    $DisplaySubTot, -2);
            $rep-&gt;NewLine();
            if ($myrow[&#039;ov_freight&#039;] != 0.0)
            {
                   $DisplayFreight = number_format2($sign*$myrow[&quot;ov_freight&quot;],$dec);
                $rep-&gt;TextCol(3, 6, _(&quot;Shipping&quot;), -2);
                $rep-&gt;TextCol(6, 7,    $DisplayFreight, -2);
                $rep-&gt;NewLine();
            }    
            $tax_items = get_trans_tax_details(ST_SALESINVOICE, $row[&#039;trans_no&#039;]);
            $first = true;
            while ($tax_item = db_fetch($tax_items))
            {
                if ($tax_item[&#039;amount&#039;] == 0)
                    continue;
                $DisplayTax = number_format2($sign*$tax_item[&#039;amount&#039;], $dec);

                if ($SysPrefs-&gt;suppress_tax_rates() == 1)
                    $tax_type_name = $tax_item[&#039;tax_type_name&#039;];
                else
                    $tax_type_name = $tax_item[&#039;tax_type_name&#039;].&quot; (&quot;.$tax_item[&#039;rate&#039;].&quot;%) &quot;;

                if ($myrow[&#039;tax_included&#039;])
                {
                    if ($SysPrefs-&gt;alternative_tax_include_on_docs() == 1)
                    {
                        if ($first)
                        {
                            $rep-&gt;TextCol(3, 6, _(&quot;Total Tax Excluded&quot;), -2);
                            $rep-&gt;TextCol(6, 7,    number_format2($sign*$tax_item[&#039;net_amount&#039;], $dec), -2);
                            $rep-&gt;NewLine();
                        }
                        $rep-&gt;TextCol(3, 6, $tax_type_name, -2);
                        $rep-&gt;TextCol(6, 7,    $DisplayTax, -2);
                        $first = false;
                    }
                    else
                        $rep-&gt;TextCol(3, 6, _(&quot;Included&quot;) . &quot; &quot; . $tax_type_name . _(&quot;Amount&quot;) . &quot;: &quot; . $DisplayTax, -2);
                }
                else
                {
                    $rep-&gt;TextCol(3, 6, $tax_type_name, -2);
                    $rep-&gt;TextCol(6, 7,    $DisplayTax, -2);
                }
                $rep-&gt;NewLine();
            }

            $rep-&gt;NewLine();

//-----------------------------------------------------------added to fetch and calculate total balance
                $accumulate = 0;                    
                $bal = get_open_balance($myrow[&#039;debtor_no&#039;],today()); 
                $init[0] = $init[1] = 0.0;
            $init[0] = round2(abs($bal[&#039;charges&#039;]), $dec);
            $init[1] = round2(Abs($bal[&#039;credits&#039;]), $dec);
            $init[2] = round2($bal[&#039;Allocated&#039;], $dec);
            $init[3] = $init[0] - $init[1];
            $accumulate += $init[3];
//-----------------------------------------------------------End added to fetch and calculate total balance

            $DisplayTotal = number_format2($sign*($myrow[&quot;ov_freight&quot;] + $myrow[&quot;ov_gst&quot;] +
                $myrow[&quot;ov_amount&quot;]+$myrow[&quot;ov_freight_tax&quot;]),$dec);
            $rep-&gt;Font(&#039;bold&#039;);
            if (!$myrow[&#039;prepaid&#039;]) $rep-&gt;Font(&#039;bold&#039;);
                $rep-&gt;TextCol(3, 6, $rep-&gt;formData[&#039;prepaid&#039;] ? _(&quot;TOTAL ORDER VAT INCL.&quot;) : _(&quot;TOTAL INVOICE&quot;), - 2);
            $rep-&gt;TextCol(6, 7, $DisplayTotal, -2);

//-------------------------------------------- added to display total balance            
            $rep-&gt;NewLine(); 
            $rep-&gt;TextCol(3, 6, $rep-&gt;formData[&#039;prepaid&#039;] ? _(&quot;TOTAL ORDER VAT INCL.&quot;) : _(&quot;TOTAL BALANCE&quot;), - 2); 
            $DisplayGrand = number_format2($init[3],$dec);
            $rep-&gt;TextCol(6, 7, $DisplayGrand, -2); 
//-----------------------------------------------------------End added to display total balance    

            if ($rep-&gt;formData[&#039;prepaid&#039;])
            {
                $rep-&gt;NewLine();
                $rep-&gt;Font(&#039;bold&#039;);
                $rep-&gt;TextCol(3, 6, $rep-&gt;formData[&#039;prepaid&#039;]==&#039;final&#039; ? _(&quot;THIS INVOICE&quot;) : _(&quot;TOTAL INVOICE&quot;), - 2);
                $rep-&gt;TextCol(6, 7, number_format2($myrow[&#039;prep_amount&#039;], $dec), -2);
            }
            $words = price_in_words($rep-&gt;formData[&#039;prepaid&#039;] ? $myrow[&#039;prep_amount&#039;] : $myrow[&#039;Total&#039;]
                , array( &#039;type&#039; =&gt; ST_SALESINVOICE, &#039;currency&#039; =&gt; $myrow[&#039;curr_code&#039;]));
            if ($words != &quot;&quot;)
            {
                $rep-&gt;NewLine(1);
                $rep-&gt;TextCol(1, 7, $myrow[&#039;curr_code&#039;] . &quot;: &quot; . $words, - 2);
            }
            $rep-&gt;Font();
            if ($email == 1)
            {
                $rep-&gt;End($email);
            }
    }
    if ($email == 0)
        $rep-&gt;End();
}</code></pre></div><p>I was pretty busy with work.</p>]]></content>
			<author>
				<name><![CDATA[trafficpest]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=42061</uri>
			</author>
			<updated>2018-01-17T07:13:40Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=30537#p30537</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=30510#p30510" />
			<content type="html"><![CDATA[<p>The error in getting sales invoice sequences is fixed and <a href="https://github.com/FrontAccountingERP/FA/commit/8333e3e6b2b09fb6d72d3605275a8212bd71de84">committed</a>.</p><p>/Joe</p>]]></content>
			<author>
				<name><![CDATA[joe]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=3</uri>
			</author>
			<updated>2018-01-13T13:18:47Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=30510#p30510</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=30506#p30506" />
			<content type="html"><![CDATA[<p>The fix for the invoice range is not to rely on any pattern but to rely on the transaction number and type directly. Hence, replace lines 39-40 of <strong>reporting/rep107.php</strong>:<br /></p><div class="codebox"><pre><code>            .&quot; AND trans.reference&gt;=&quot;.db_escape(get_reference(ST_SALESINVOICE, $from))
            .&quot; AND trans.reference&lt;=&quot;.db_escape(get_reference(ST_SALESINVOICE, $to))</code></pre></div><p>with:<br /></p><div class="codebox"><pre><code>            .&quot; AND trans.trans_no BETWEEN &quot;.db_escape($from).&quot; AND &quot;.db_escape($to)</code></pre></div><p>This will result in the <strong>$sql</strong> in the get range function to be like:<br /></p><div class="codebox"><pre><code>SELECT trans.trans_no, trans.reference
FROM &amp;TB_PREF&amp;debtor_trans trans 
    LEFT JOIN &amp;TB_PREF&amp;voided voided ON trans.type=voided.type AND trans.trans_no=voided.id
WHERE trans.type=10 AND ISNULL(voided.id) 
    AND trans.trans_no BETWEEN &#039;1&#039; AND &#039;5&#039; 
ORDER BY trans.tran_date, trans.reference</code></pre></div><br /><p>@joe: see if fit to commit.</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2018-01-13T07:50:07Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=30506#p30506</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=30502#p30502" />
			<content type="html"><![CDATA[<p>@trafficpest: The &quot;add this above ... below&quot;, etc are not discernible. Kindly post the whole modified rep107.php file so that a diff file can be prepared. Thanks for the &quot;money&quot; offer - don&#039;t bother.</p><p>@joe: there is an error in the <strong>get_invoice_range()</strong> if it straddles fiscal years with the default &quot;pattern&quot; of references. The year comes last in the default pattern (<strong>{001}/{YEAR}</strong>)and the actual invoice number comes in first so if we choose 001/2016 to 001/2018, we miss out on 002/2016, 002/2017, etc.</p><p>The <strong>$sql</strong> in <strong>get_invoice_range()</strong> we get will be:<br /></p><div class="codebox"><pre><code>SELECT trans.trans_no, trans.reference
        FROM &amp;TB_PREF&amp;debtor_trans trans 
            LEFT JOIN &amp;TB_PREF&amp;voided voided ON trans.type=voided.type AND trans.trans_no=voided.id
        WHERE trans.type=10 AND ISNULL(voided.id) 
        AND trans.reference&gt;=&#039;001/2016&#039; 
        AND trans.reference&lt;=&#039;001/2018&#039; 
        ORDER BY trans.tran_date, trans.reference</code></pre></div><p>We inspect the above by temporarily putting in:<br /><strong>file_put_contents(&#039;D:/WebServers/XAMPP/htdocs/frontac24/tmp/log_file.txt&#039;, $sql, FILE_APPEND);</strong><br />in line 42 of <strong>reporting/rep107.php</strong></p><p>To alleviate this, we can change the default pattern to be <strong>{YEAR}/{001}</strong> but will that lead to other problems?</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2018-01-13T06:56:39Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=30502#p30502</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=30498#p30498" />
			<content type="html"><![CDATA[<p>@Apmuthu </p><p>Ok I got it working Thank You for the help. I&#039;ll send you some money on Pay Pal for your help on Monday (Waiting to verify the new Bank Account) I&#039;ll send it to the Email on you whois for the domain.</p><p>So, Here is the code for anyone that wants it. I am no coder so someone might want to clean it up, but it works.<br />It will print the clients current balance for all invoices up to the day it s printed. For example if today is 1/12/18 it will includeinvoices with dates from 1/5/18 and 1/12/18 but not an invoice made for the future 1/16/18.</p><p>add this funcion from rep101.php to the top of your /reporting/rep107.php where all the funcions are</p><div class="codebox"><pre><code>function get_open_balance($debtorno, $to)
{
    if($to)
        $to = date2sql($to);

     $sql = &quot;SELECT SUM(IF(t.type = &quot;.ST_SALESINVOICE.&quot; OR (t.type = &quot;.ST_JOURNAL.&quot; AND t.ov_amount&gt;0),
         -abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount), 0)) AS charges,&quot;;
     $sql .= &quot;SUM(IF(t.type != &quot;.ST_SALESINVOICE.&quot; AND NOT(t.type = &quot;.ST_JOURNAL.&quot; AND t.ov_amount&gt;0),
         abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) * -1, 0)) AS credits,&quot;;
     $sql .= &quot;SUM(IF(t.type != &quot;.ST_SALESINVOICE.&quot; AND NOT(t.type = &quot;.ST_JOURNAL.&quot; AND t.ov_amount&gt;0), t.alloc * -1, t.alloc)) AS Allocated,&quot;;

     $sql .=    &quot;SUM(IF(t.type = &quot;.ST_SALESINVOICE.&quot;, 1, -1) *
             (abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) - abs(t.alloc))) AS OutStanding
        FROM &quot;.TB_PREF.&quot;debtor_trans t
        WHERE t.debtor_no = &quot;.db_escape($debtorno)
        .&quot; AND t.type &lt;&gt; &quot;.ST_CUSTDELIVERY;
    if ($to)
        $sql .= &quot; AND t.tran_date &lt; &#039;$to+1&#039;&quot;;
    $sql .= &quot; GROUP BY debtor_no&quot;;

    $result = db_query($sql,&quot;No transactions were returned&quot;);
    return db_fetch($result);
}</code></pre></div><p>Then add this above</p><div class="codebox"><pre><code>                        $accumulate = 0;                    
                $bal = get_open_balance($myrow[&#039;debtor_no&#039;],today()); 
                $init[0] = $init[1] = 0.0;
            $init[0] = round2(abs($bal[&#039;charges&#039;]), $dec);
            $init[1] = round2(Abs($bal[&#039;credits&#039;]), $dec);
            $init[2] = round2($bal[&#039;Allocated&#039;], $dec);
            $init[3] = $init[0] - $init[1];
            $accumulate += $init[3];</code></pre></div><p>and this below</p><div class="codebox"><pre><code>$rep-&gt;TextCol(6, 7, $DisplayTotal, -2);
            $rep-&gt;NewLine(); // added for total balance
            $rep-&gt;TextCol(3, 6, $rep-&gt;formData[&#039;prepaid&#039;] ? _(&quot;TOTAL ORDER VAT INCL.&quot;) : _(&quot;TOTAL BALANCE&quot;), - 2); 
            $DisplayGrand = number_format2($init[3],$dec);
            $rep-&gt;TextCol(6, 7, $DisplayGrand, -2); // added for total balance</code></pre></div><p>This line of code</p><div class="codebox"><pre><code>            $DisplayTotal = number_format2($sign*($myrow[&quot;ov_freight&quot;] + $myrow[&quot;ov_gst&quot;] +
                $myrow[&quot;ov_amount&quot;]+$myrow[&quot;ov_freight_tax&quot;]),$dec);
            $rep-&gt;Font(&#039;bold&#039;);
            if (!$myrow[&#039;prepaid&#039;]) $rep-&gt;Font(&#039;bold&#039;);
                $rep-&gt;TextCol(3, 6, $rep-&gt;formData[&#039;prepaid&#039;] ? _(&quot;TOTAL ORDER VAT INCL.&quot;) : _(&quot;THIS INVOICE&quot;), - 2);</code></pre></div>]]></content>
			<author>
				<name><![CDATA[trafficpest]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=42061</uri>
			</author>
			<updated>2018-01-12T17:52:28Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=30498#p30498</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=30469#p30469" />
			<content type="html"><![CDATA[<p>The function needs 2 arguments and the second one <strong>$to</strong> is missing. It should be computed to be the current date/time within the function if the argument is not given.</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2018-01-10T23:26:32Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=30469#p30469</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=30462#p30462" />
			<content type="html"><![CDATA[<p>I have a service industry and am trying to create a &quot;service ticket/invoice&quot; with the total customer balance at the time of printing. I have imported the function get_open_balance from rep101 and am trying to use it but it returns an array. Maybe the array needs to be added up before displaying? </p><p>Here is the funcion I imported to the begining of my version of rep107<br />The parts with variable $to were disabled since it is not passed in an invoice</p><div class="codebox"><pre><code>function get_open_balance($debtorno)
{
    #if($to) 
    #    $to = date2sql($to);

     $sql = &quot;SELECT SUM(IF(t.type = &quot;.ST_SALESINVOICE.&quot; OR (t.type = &quot;.ST_JOURNAL.&quot; AND t.ov_amount&gt;0),
         -abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount), 0)) AS charges,&quot;;
     $sql .= &quot;SUM(IF(t.type != &quot;.ST_SALESINVOICE.&quot; AND NOT(t.type = &quot;.ST_JOURNAL.&quot; AND t.ov_amount&gt;0),
         abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) * -1, 0)) AS credits,&quot;;
     $sql .= &quot;SUM(IF(t.type != &quot;.ST_SALESINVOICE.&quot; AND NOT(t.type = &quot;.ST_JOURNAL.&quot; AND t.ov_amount&gt;0), t.alloc * -1, t.alloc)) AS Allocated,&quot;;

     $sql .=    &quot;SUM(IF(t.type = &quot;.ST_SALESINVOICE.&quot;, 1, -1) *
             (abs(t.ov_amount + t.ov_gst + t.ov_freight + t.ov_freight_tax + t.ov_discount) - abs(t.alloc))) AS OutStanding
        FROM &quot;.TB_PREF.&quot;debtor_trans t
        WHERE t.debtor_no = &quot;.db_escape($debtorno)
        .&quot; AND t.type &lt;&gt; &quot;.ST_CUSTDELIVERY;
    # if ($to)
    #    $sql .= &quot; AND t.tran_date &lt; &#039;$to&#039;&quot;;
    #$sql .= &quot; GROUP BY debtor_no&quot;; 

    $result = db_query($sql,&quot;No transactions were returned&quot;);
    return db_fetch($result);
}</code></pre></div><p>On the end of the invoice printing side I have this:<br />The lines are commented becuse it is broken.</p><div class="codebox"><pre><code>$rep-&gt;NewLine();
                #$bal = get_open_balance($myrow[&#039;debtor_no&#039;]); // added for total balance not working function returns an array.
                #$DisplayCustTotal = number_format2($bal,$dec); // added for total balance
            $DisplayTotal = number_format2($sign*($myrow[&quot;ov_freight&quot;] + $myrow[&quot;ov_gst&quot;] +
                $myrow[&quot;ov_amount&quot;]+$myrow[&quot;ov_freight_tax&quot;]),$dec);
            $rep-&gt;Font(&#039;bold&#039;);
            if (!$myrow[&#039;prepaid&#039;]) $rep-&gt;Font(&#039;bold&#039;);
                $rep-&gt;TextCol(3, 6, $rep-&gt;formData[&#039;prepaid&#039;] ? _(&quot;TOTAL ORDER VAT INCL.&quot;) : _(&quot;THIS INVOICE&quot;), - 2);
            $rep-&gt;TextCol(6, 7, $DisplayTotal, -2);
            $rep-&gt;NewLine(); // added for total balance
            $rep-&gt;TextCol(3, 6, $rep-&gt;formData[&#039;prepaid&#039;] ? _(&quot;TOTAL ORDER VAT INCL.&quot;) : _(&quot;TOTAL BALANCE&quot;), - 2); // added for total balance
            #$rep-&gt;TextCol(6, 7, $DisplayCustTotal, -2); // added for total balance
            if ($rep-&gt;formData[&#039;prepaid&#039;])
            {
                $rep-&gt;NewLine();
                $rep-&gt;Font(&#039;bold&#039;);
                $rep-&gt;TextCol(3, 6, $rep-&gt;formData[&#039;prepaid&#039;]==&#039;final&#039; ? _(&quot;THIS INVOICE&quot;) : _(&quot;TOTAL INVOICE&quot;), - 2);
                $rep-&gt;TextCol(6, 7, number_format2($myrow[&#039;prep_amount&#039;], $dec), -2);
            }
            $words = price_in_words($rep-&gt;formData[&#039;prepaid&#039;] ? $myrow[&#039;prep_amount&#039;] : $myrow[&#039;Total&#039;]
                , array( &#039;type&#039; =&gt; ST_SALESINVOICE, &#039;currency&#039; =&gt; $myrow[&#039;curr_code&#039;]));
            if ($words != &quot;&quot;)
            {
                $rep-&gt;NewLine(1);
                $rep-&gt;TextCol(1, 7, $myrow[&#039;curr_code&#039;] . &quot;: &quot; . $words, - 2);
            }
            $rep-&gt;Font();
            if ($email == 1)
            {
                $rep-&gt;End($email);
            }</code></pre></div><p>I am not a very good programmer and I have no experience in PHP. Thank you in advance you guys are lifesavers.</p>]]></content>
			<author>
				<name><![CDATA[trafficpest]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=42061</uri>
			</author>
			<updated>2018-01-10T08:41:50Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=30462#p30462</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=23745#p23745" />
			<content type="html"><![CDATA[<p>Sales Invoice is not for that !!<br />Can&#039;t you attach Customer Balance report to the invocie instead ???</p><p>If not, you will have to customize sales invoice report for your needs.</p><p>Janusz</p>]]></content>
			<author>
				<name><![CDATA[itronics]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=89</uri>
			</author>
			<updated>2015-06-25T21:58:52Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=23745#p23745</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=23730#p23730" />
			<content type="html"><![CDATA[<p>This could be tricky - any copy that is printed out will possibly have the balance value (dynamically computed at print time) as on the print out date and not on the invoice creation date.</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2015-06-25T02:48:00Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=23730#p23730</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Print Customer Balance On Invoice]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=23726#p23726" />
			<content type="html"><![CDATA[<p>Hello All!!</p><p>Can anyone help me to add on invoice printout the customer balance???</p><p>thanks</p>]]></content>
			<author>
				<name><![CDATA[tcpip]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=20513</uri>
			</author>
			<updated>2015-06-24T19:26:22Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=23726#p23726</id>
		</entry>
</feed>
