<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[FrontAccounting forum — Customer Trial Balance Report]]></title>
	<link rel="self" href="https://frontaccounting.com/punbb/extern.php?action=feed&amp;tid=7897&amp;type=atom" />
	<updated>2019-05-18T13:42:24Z</updated>
	<generator>PunBB</generator>
	<id>https://frontaccounting.com/punbb/viewtopic.php?id=7897</id>
		<entry>
			<title type="html"><![CDATA[Re: Customer Trial Balance Report]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=34963#p34963" />
			<content type="html"><![CDATA[<p>This is fixed and some margin problems in Customer Trial Balance Report fixed too.</p><p>Thanks @boxygen for seeing this.</p><p>Fixed files can be downloaded <a href="https://sourceforge.net/p/frontaccounting/git/ci/master/tree/reporting/rep115.php">here</a> and <a href="https://sourceforge.net/p/frontaccounting/git/ci/master/tree/reporting/rep206.php">here</a>.</p><p>/Joe</p>]]></content>
			<author>
				<name><![CDATA[joe]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=3</uri>
			</author>
			<updated>2019-05-18T13:42:24Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=34963#p34963</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Customer Trial Balance Report]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=34961#p34961" />
			<content type="html"><![CDATA[<p>in rep115.php Line # 234 shall be modified as below</p><div class="codebox"><pre><code>         if (db_num_rows($res) == 0 &amp;&amp; !$no_zeros)</code></pre></div>]]></content>
			<author>
				<name><![CDATA[boxygen]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=20175</uri>
			</author>
			<updated>2019-05-18T10:47:17Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=34961#p34961</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Customer Trial Balance Report]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=34960#p34960" />
			<content type="html"><![CDATA[<p>In rep206.php Line # 168 shall be modified as below</p><div class="codebox"><pre><code> if (db_num_rows($res) == 0 &amp;&amp; !$no_zeros) </code></pre></div><p>and Line # 181 shall be commented, Because ignoring the loop here is not adding up the values of $curr_cr, $curr_db and $item variables.</p>]]></content>
			<author>
				<name><![CDATA[boxygen]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=20175</uri>
			</author>
			<updated>2019-05-18T10:34:05Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=34960#p34960</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Customer Trial Balance Report]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=33743#p33743" />
			<content type="html"><![CDATA[<p>@joe This is an honour for me and all contributors.</p>]]></content>
			<author>
				<name><![CDATA[boxygen]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=20175</uri>
			</author>
			<updated>2018-12-24T05:43:12Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=33743#p33743</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Customer Trial Balance Report]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=33737#p33737" />
			<content type="html"><![CDATA[<p>Updated the <a href="https://frontaccounting.com/fawiki/index.php?n=Help.ReportsAndAnalysis">Wiki</a> with the screenshots.</p><p>Whilst the <strong>Suppliers <span class="bbu">T</span>rial Balance Report</strong> has a hot key (T), the Customer Trial balance Report does not have any. Also naming styles (plurals) can be standardised.</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2018-12-23T09:04:37Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=33737#p33737</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Customer Trial Balance Report]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=33724#p33724" />
			<content type="html"><![CDATA[<p>A couple of minor issues are fixed in the rep115.php and rep206. The <strong>no_zeroes</strong> parameter didn&#039;t work and some parameters were missing in the rep115.php report. The files are committed to repo and you can re-download them a bit up.</p><p>We are not glad for the Copyright notice in the beginning of the 2 files. We are going to change our copyright to be FrontAccounting Team from next major 2.5. In the readme.md files we are going to present the active developers in the FrontAccounting Team.<br />For now I have changed the Copyright to FrontAccounting Team for the 2 files. <br />A bit down there are a field called &#039;Creator&#039;. We will use that field for the Team Member that have created the file.<br />I hope you are ok with this, @boxygen.</p><p>/Joe</p>]]></content>
			<author>
				<name><![CDATA[joe]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=3</uri>
			</author>
			<updated>2018-12-22T10:12:40Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=33724#p33724</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Customer Trial Balance Report]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=33722#p33722" />
			<content type="html"><![CDATA[<p>This is nice one. @joe add it to the stable download . So new downloads will get the feature</p>]]></content>
			<author>
				<name><![CDATA[kvvaradha]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=19348</uri>
			</author>
			<updated>2018-12-22T03:14:59Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=33722#p33722</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Customer Trial Balance Report]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=33721#p33721" />
			<content type="html"><![CDATA[<p>The 2 reports has been added to core and are committed.</p><p>You can download the 3 files<br /><a href="https://sourceforge.net/p/frontaccounting/git/ci/master/tree/reporting/rep115.php">/reporting/rep115.php</a><br /><a href="https://sourceforge.net/p/frontaccounting/git/ci/master/tree/reporting/rep206.php">/reporting/rep206.php</a><br /><a href="https://sourceforge.net/p/frontaccounting/git/ci/master/tree/reporting/reports_main.php">/reporting/reports_main.php</a></p><p>/Joe</p>]]></content>
			<author>
				<name><![CDATA[joe]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=3</uri>
			</author>
			<updated>2018-12-21T16:11:40Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=33721#p33721</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Customer Trial Balance Report]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=33720#p33720" />
			<content type="html"><![CDATA[<p>wow, you are my man, @boxygen.</p><p>Will incorporate this as well. will be committed later this afternoon.</p><p>/Joe</p>]]></content>
			<author>
				<name><![CDATA[joe]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=3</uri>
			</author>
			<updated>2018-12-21T14:17:08Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=33720#p33720</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Customer Trial Balance Report]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=33717#p33717" />
			<content type="html"><![CDATA[<p>Here is Supplier Trial Balance report</p><div class="codebox"><pre><code>&lt;?php
/**********************************************************************
    Copyright (C) Boxygen, 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 = &#039;SA_SUPPLIERANALYTIC&#039;;
// ----------------------------------------------------------------
// $ Revision:    2.0 $
// Creator:    Joe Hunt
// date_:    2005-05-19
// Title:    Supplier Balances
// ----------------------------------------------------------------
$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;/gl/includes/gl_db.inc&quot;);

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

print_supplier_balances();

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

        $sql = &quot;SELECT SUM(IF(t.type = &quot;.ST_SUPPINVOICE.&quot; OR (t.type IN (&quot;.ST_JOURNAL.&quot; , &quot;.ST_BANKDEPOSIT.&quot;) AND t.ov_amount&gt;0),
                        -abs(t.ov_amount + t.ov_gst + t.ov_discount), 0)) AS charges,&quot;;

 $sql .= &quot;SUM(IF(t.type != &quot;.ST_SUPPINVOICE.&quot; AND NOT(t.type IN (&quot;.ST_JOURNAL.&quot; , &quot;.ST_BANKDEPOSIT.&quot;) AND t.ov_amount&gt;0),
                        abs(t.ov_amount + t.ov_gst + t.ov_discount) * -1, 0)) AS credits,&quot;;

     $sql .= &quot;SUM(IF(t.type != &quot;.ST_SUPPINVOICE.&quot; AND NOT(t.type IN (&quot;.ST_JOURNAL.&quot; , &quot;.ST_BANKDEPOSIT.&quot;)), t.alloc * -1, t.alloc)) AS Allocated,&quot;;

     $sql .=    &quot;SUM(IF(t.type = &quot;.ST_SUPPINVOICE.&quot;, 1, -1) *
             (abs(t.ov_amount + t.ov_gst + t.ov_discount) - abs(t.alloc))) AS OutStanding
        FROM &quot;.TB_PREF.&quot;supp_trans t
        WHERE t.supplier_id = &quot;.db_escape($supplier_id);
    if ($to)
        $sql .= &quot; AND t.tran_date &lt; &#039;$to&#039;&quot;;
    $sql .= &quot; GROUP BY supplier_id&quot;;

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


// function get_open_balance($supplier_id, $to)
// {
//     $to = date2sql($to);
//
//     $sql = &quot;SELECT SUM(IF(&quot;.TB_PREF.&quot;supp_trans.type = &quot;.ST_SUPPINVOICE.&quot; OR &quot;.TB_PREF.&quot;supp_trans.type = &quot;.ST_BANKDEPOSIT.&quot;,
//         (&quot;.TB_PREF.&quot;supp_trans.ov_amount + &quot;.TB_PREF.&quot;supp_trans.ov_gst + &quot;.TB_PREF.&quot;supp_trans.ov_discount), 0)) AS charges,
//         SUM(IF(&quot;.TB_PREF.&quot;supp_trans.type &lt;&gt; &quot;.ST_SUPPINVOICE.&quot; AND &quot;.TB_PREF.&quot;supp_trans.type &lt;&gt; &quot;.ST_BANKDEPOSIT.&quot;,
//         (&quot;.TB_PREF.&quot;supp_trans.ov_amount + &quot;.TB_PREF.&quot;supp_trans.ov_gst + &quot;.TB_PREF.&quot;supp_trans.ov_discount), 0)) AS credits,
//         SUM(&quot;.TB_PREF.&quot;supp_trans.alloc) AS Allocated,
//         SUM(IF(&quot;.TB_PREF.&quot;supp_trans.type = &quot;.ST_SUPPINVOICE.&quot; OR &quot;.TB_PREF.&quot;supp_trans.type = &quot;.ST_BANKDEPOSIT.&quot;,
//         (&quot;.TB_PREF.&quot;supp_trans.ov_amount + &quot;.TB_PREF.&quot;supp_trans.ov_gst + &quot;.TB_PREF.&quot;supp_trans.ov_discount - &quot;.TB_PREF.&quot;supp_trans.alloc),
//         (&quot;.TB_PREF.&quot;supp_trans.ov_amount + &quot;.TB_PREF.&quot;supp_trans.ov_gst + &quot;.TB_PREF.&quot;supp_trans.ov_discount + &quot;.TB_PREF.&quot;supp_trans.alloc))) AS OutStanding
//         FROM &quot;.TB_PREF.&quot;supp_trans
//         WHERE &quot;.TB_PREF.&quot;supp_trans.tran_date &lt; &#039;$to&#039;
//         AND &quot;.TB_PREF.&quot;supp_trans.supplier_id = &#039;$supplier_id&#039; GROUP BY supplier_id&quot;;
//
//     $result = db_query($sql,&quot;No transactions were returned&quot;);
//     return db_fetch($result);
// }

function getTransactions($supplier_id, $from, $to)
{
    $from = date2sql($from);
    $to = date2sql($to);
//memo added by faisal
    $sql = &quot;SELECT &quot;.TB_PREF.&quot;supp_trans.*, comments.memo_,
                (&quot;.TB_PREF.&quot;supp_trans.ov_amount + &quot;.TB_PREF.&quot;supp_trans.ov_gst + &quot;.TB_PREF.&quot;supp_trans.ov_discount)
                AS TotalAmount, &quot;.TB_PREF.&quot;supp_trans.alloc AS Allocated,
                ((&quot;.TB_PREF.&quot;supp_trans.type = &quot;.ST_SUPPINVOICE.&quot;)
                    AND &quot;.TB_PREF.&quot;supp_trans.due_date &lt; &#039;$to&#039;) AS OverDue
                FROM &quot;.TB_PREF.&quot;supp_trans
                    LEFT JOIN &quot;.TB_PREF.&quot;comments comments ON &quot;.TB_PREF.&quot;supp_trans.type=comments.type AND &quot;.TB_PREF.&quot;supp_trans.trans_no=comments.id
                     WHERE &quot;.TB_PREF.&quot;supp_trans.tran_date &gt;= &#039;$from&#039; AND &quot;.TB_PREF.&quot;supp_trans.tran_date &lt;= &#039;$to&#039;
                AND &quot;.TB_PREF.&quot;supp_trans.supplier_id = &#039;$supplier_id&#039; AND &quot;.TB_PREF.&quot;supp_trans.ov_amount!=0
                    ORDER BY &quot;.TB_PREF.&quot;supp_trans.tran_date&quot;;

    $TransResult = db_query($sql,&quot;No transactions were returned&quot;);

    return $TransResult;
}

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

function print_supplier_balances()
{
        global $path_to_root, $systypes_array;

        $from = $_POST[&#039;PARAM_0&#039;];
        $to = $_POST[&#039;PARAM_1&#039;];
        $fromsupp = $_POST[&#039;PARAM_2&#039;];
        $currency = $_POST[&#039;PARAM_3&#039;];
        $no_zeros = $_POST[&#039;PARAM_4&#039;];
            $comments = $_POST[&#039;PARAM_5&#039;];
    $orientation = $_POST[&#039;PARAM_6&#039;];
    $destination = $_POST[&#039;PARAM_7&#039;];
    if ($destination)
        include_once($path_to_root . &quot;/reporting/includes/excel_report.inc&quot;);
    else
        include_once($path_to_root . &quot;/reporting/includes/pdf_report.inc&quot;);

    $orientation = ($orientation ? &#039;L&#039; : &#039;P&#039;);
    if ($fromsupp == ALL_TEXT)
        $supp = _(&#039;All&#039;);
    else
        $supp = get_supplier_name($fromsupp);
        $dec = user_price_dec();

    if ($currency == ALL_TEXT)
    {
        $convert = true;
        $currency = _(&#039;Balances in Home currency&#039;);
    }
    else
        $convert = false;

    if ($no_zeros) $nozeros = _(&#039;Yes&#039;);
    else $nozeros = _(&#039;No&#039;);

    $cols = array(0, 100, 130, 190,    250, 320, 385, 450,    515);

    $headers = array(_(&#039;Name&#039;), _(&#039;&#039;), _(&#039;&#039;), _(&#039;Opening Bal&#039;), _(&#039;Debit&#039;),
        _(&#039;Credit&#039;), _(&#039;&#039;), _(&#039;Balance&#039;));

    $aligns = array(&#039;left&#039;,    &#039;left&#039;,    &#039;left&#039;,    &#039;right&#039;,    &#039;right&#039;, &#039;right&#039;, &#039;right&#039;, &#039;right&#039;);

    $params =   array(     0 =&gt; $comments,
                1 =&gt; array(&#039;text&#039; =&gt; _(&#039;Period&#039;), &#039;from&#039; =&gt; $from, &#039;to&#039; =&gt; $to),
                2 =&gt; array(&#039;text&#039; =&gt; _(&#039;Supplier&#039;), &#039;from&#039; =&gt; $supp, &#039;to&#039; =&gt; &#039;&#039;),
                3 =&gt; array(  &#039;text&#039; =&gt; _(&#039;Currency&#039;),&#039;from&#039; =&gt; $currency, &#039;to&#039; =&gt; &#039;&#039;),
            4 =&gt; array(&#039;text&#039; =&gt; _(&#039;Suppress Zeros&#039;), &#039;from&#039; =&gt; $nozeros, &#039;to&#039; =&gt; &#039;&#039;));

    $rep = new FrontReport(_(&#039;Supplier TB&#039;), &quot;SupplierTB&quot;, user_pagesize(), 9, $orientation);
    if ($orientation == &#039;L&#039;)
        recalculate_cols($cols);

    $rep-&gt;Font();
    $rep-&gt;Info($params, $cols, $headers, $aligns);
    $rep-&gt;NewPage();

    $total = array();
    $grandtotal = array(0,0,0,0);

    $sql = &quot;SELECT supplier_id, supp_name AS name, curr_code FROM &quot;.TB_PREF.&quot;suppliers&quot;;
    if ($fromsupp != ALL_TEXT)
        $sql .= &quot; WHERE supplier_id=&quot;.db_escape($fromsupp);
    $sql .= &quot; ORDER BY supp_name&quot;;
    $result = db_query($sql, &quot;The customers could not be retrieved&quot;);

$tot_cur_cr = $tot_cur_db = 0;
    while ($myrow=db_fetch($result))
    {
        if (!$convert &amp;&amp; $currency != $myrow[&#039;curr_code&#039;])
            continue;
        $accumulate = 0;
        $rate = $convert ? get_exchange_rate_from_home_currency($myrow[&#039;curr_code&#039;], Today()) : 1;
        $bal = get_open_balance($myrow[&#039;supplier_id&#039;], $from);
        $init[0] = $init[1] = 0.0;
        $init[0] = round2(abs($bal[&#039;charges&#039;]*$rate), $dec);
        $init[1] = round2(Abs($bal[&#039;credits&#039;]*$rate), $dec);
        $init[2] = round2($bal[&#039;Allocated&#039;]*$rate, $dec);

        $init[3] = $init[1] - $init[0];
        $accumulate += $init[3];

        $res = getTransactions($myrow[&#039;supplier_id&#039;], $from, $to);

        $total = array(0,0,0,0);
        for ($i = 0; $i &lt; 4; $i++)
        {
            $total[$i] += $init[$i];
            $grandtotal[$i] += $init[$i];
        }

        if ($no_zeros &amp;&amp; $init[3] == 0) continue;

        if (db_num_rows($res)==0) {
            $rep-&gt;TextCol(0, 2, $myrow[&#039;name&#039;]);
            $rep-&gt;AmountCol(3, 4, $init[3], $dec);
            $rep-&gt;AmountCol(7, 8, $init[3], $dec);
            $rep-&gt;Line($rep-&gt;row  - 2);
        $rep-&gt;NewLine();

            continue;
        }
        $curr_db = $curr_cr =0;
        while ($trans=db_fetch($res))
        {
            if ($no_zeros &amp;&amp; floatcmp(abs($trans[&#039;TotalAmount&#039;]), $trans[&#039;Allocated&#039;]) == 0) continue;
            $item[0] = $item[1] = 0.0;
            if ($trans[&#039;TotalAmount&#039;] &gt; 0.0)
            {
                $item[0] = round2(abs($trans[&#039;TotalAmount&#039;]) * $rate, $dec);
                $curr_cr += $item[0];
                $tot_cur_cr += $item[0];

                $accumulate -= $item[0];
            }
            else
            {
                $item[1] = round2(abs($trans[&#039;TotalAmount&#039;]) * $rate, $dec);
                $curr_db += $item[1];
                $tot_cur_db += $item[1];
                $accumulate += $item[1];
            }
            $item[2] = round2($trans[&#039;Allocated&#039;] * $rate, $dec);
            if ($trans[&#039;TotalAmount&#039;] &gt; 0.0)
                $item[3] = $item[2] - $item[0];
            else
                $item[3] = ($item[2] - $item[1]) * -1;

            for ($i = 0; $i &lt; 4; $i++)
            {
                $total[$i] += $item[$i];
                $grandtotal[$i] += $item[$i];
            }
                $total[3] = $total[1] - $total[0];

        }

                $rep-&gt;AmountCol(5, 6, $curr_cr, $dec);
                $rep-&gt;AmountCol(4, 5, $curr_db, $dec);


        $rep-&gt;TextCol(0, 2, $myrow[&#039;name&#039;]);
        $rep-&gt;AmountCol(3, 4, $total[3] + $curr_cr - $curr_db, $dec);
        $rep-&gt;AmountCol(7, 8, $total[3], $dec);


        for ($i = 2; $i &lt; 4; $i++)
        {
            $total[$i] = 0.0;
        }
        $rep-&gt;Line($rep-&gt;row  - 2);
        $rep-&gt;NewLine();
    }
    $rep-&gt;NewLine();
    $rep-&gt;fontSize += 2;
    $rep-&gt;TextCol(0, 3,    _(&#039;Grand Total&#039;));
    $rep-&gt;fontSize -= 2;

    $grandtotal[3] = $grandtotal[1] - $grandtotal[0];

$rep-&gt;AmountCol(3, 4,$grandtotal[3] - $tot_cur_db + $tot_cur_cr, $dec);

$rep-&gt;AmountCol(4, 5,$tot_cur_db, $dec);
$rep-&gt;AmountCol(5, 6,$tot_cur_cr, $dec);

    $rep-&gt;AmountCol(7, 8,$grandtotal[3], $dec);
    $rep-&gt;Line($rep-&gt;row  - 2);
    $rep-&gt;NewLine();
    $rep-&gt;End();
}

?&gt;</code></pre></div><p>Here is the code for reports_main.php</p><div class="codebox"><pre><code>    $reports-&gt;addReport(RC_SUPPLIER, &quot;_supplier_tb&quot;, _(&#039;Supplier &amp;TB&#039;),
                array(    _(&#039;Start Date&#039;) =&gt; &#039;DATEBEGIN&#039;,
                        _(&#039;End Date&#039;) =&gt; &#039;DATEENDM&#039;,
                        _(&#039;Supplier&#039;) =&gt; &#039;SUPPLIERS_NO_FILTER&#039;,
                        _(&#039;Currency Filter&#039;) =&gt; &#039;CURRENCY&#039;,
                        _(&#039;Suppress Zeros&#039;) =&gt; &#039;YES_NO&#039;,
                        _(&#039;Comments&#039;) =&gt; &#039;TEXTBOX&#039;,
                        _(&#039;Orientation&#039;) =&gt; &#039;ORIENTATION&#039;,
                        _(&#039;Destination&#039;) =&gt; &#039;DESTINATION&#039;));</code></pre></div>]]></content>
			<author>
				<name><![CDATA[boxygen]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=20175</uri>
			</author>
			<updated>2018-12-21T09:37:20Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=33717#p33717</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Customer Trial Balance Report]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=33714#p33714" />
			<content type="html"><![CDATA[<p>Hi @boxygen,</p><p>This is a nice and productive report. Let me incorporate this in the core.<br />Maybe a good idea to also make a Supplier Trial Balance, right?</p><p>/Joe</p>]]></content>
			<author>
				<name><![CDATA[joe]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=3</uri>
			</author>
			<updated>2018-12-21T09:24:17Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=33714#p33714</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Customer Trial Balance Report]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=33713#p33713" />
			<content type="html"><![CDATA[<p>I have designed this report for one of my client. May be this is helpful for our Community</p><p>Below is the report to be added in reporting folder as <strong>rep_customer_tb.php</strong><br /></p><div class="codebox"><pre><code>&lt;?php
/**********************************************************************
    Copyright (C) Boxygen, 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 = &#039;SA_CUSTPAYMREP&#039;;

// ----------------------------------------------------------------
// $ Revision:    2.0 $
// Creator:    Joe Hunt
// date_:    2005-05-19
// Title:    Customer Balances
// ----------------------------------------------------------------
$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;/gl/includes/gl_db.inc&quot;);
include_once($path_to_root . &quot;/sales/includes/db/customers_db.inc&quot;);

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

print_customer_balances();

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 IN (&quot;.ST_JOURNAL.&quot; , &quot;.ST_BANKPAYMENT.&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 IN (&quot;.ST_JOURNAL.&quot; , &quot;.ST_BANKPAYMENT.&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 IN (&quot;.ST_JOURNAL.&quot; , &quot;.ST_BANKPAYMENT.&quot;)), 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);
}

function get_transactions($debtorno, $from, $to, $only_rec)
{
    $from = date2sql($from);
    $to = date2sql($to);

     $allocated_from =
             &quot;(SELECT trans_type_from as trans_type, trans_no_from as trans_no, date_alloc, sum(amt) amount
             FROM &quot;.TB_PREF.&quot;cust_allocations alloc
                 WHERE person_id=&quot;.db_escape($debtorno).&quot;
                     AND date_alloc &lt;= &#039;$to&#039;
                 GROUP BY trans_type_from, trans_no_from) alloc_from&quot;;
     $allocated_to =
             &quot;(SELECT trans_type_to as trans_type, trans_no_to as trans_no, date_alloc, sum(amt) amount
             FROM &quot;.TB_PREF.&quot;cust_allocations alloc
                 WHERE person_id=&quot;.db_escape($debtorno).&quot;
                     AND date_alloc &lt;= &#039;$to&#039;
                 GROUP BY trans_type_to, trans_no_to) alloc_to&quot;;

     $sql = &quot;SELECT trans.*, comments.memo_,
         (trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount) AS TotalAmount,
         IFNULL(alloc_from.amount, alloc_to.amount) AS Allocated,
         ((trans.type = &quot;.ST_SALESINVOICE.&quot;)    AND trans.due_date &lt; &#039;$to&#039;) AS OverDue
         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
            LEFT JOIN &quot;.TB_PREF.&quot;comments comments ON trans.type=comments.type AND trans.trans_no=comments.id
             LEFT JOIN $allocated_from ON alloc_from.trans_type = trans.type AND alloc_from.trans_no = trans.trans_no
             LEFT JOIN $allocated_to ON alloc_to.trans_type = trans.type AND alloc_to.trans_no = trans.trans_no

         WHERE trans.tran_date &gt;= &#039;$from&#039;
             AND trans.tran_date &lt;= &#039;$to&#039;
             AND trans.debtor_no = &quot;.db_escape($debtorno);

            $sql .= &quot; AND trans.type &lt;&gt; &quot;.ST_CUSTDELIVERY;

             $sql .= &quot; AND ISNULL(voided.id)
         ORDER BY trans.tran_date &quot;;
    return db_query($sql,&quot;No transactions were returned&quot;);
}

function get_customer_reference ($order_number)
{

    $sql = &quot;SELECT customer_ref FROM &quot;.TB_PREF.&quot;sales_orders WHERE order_no =&#039;$order_number&#039; AND trans_type=&quot;.ST_SALESORDER.&quot;&quot;;

    $result = db_query($sql,&quot;No Transcation were returned&quot;);

    $val = db_fetch($result);

    return $val[&#039;customer_ref&#039;];


}
//----------------------------------------------------------------------------------------------------

function print_customer_balances()
{
        global $path_to_root, $systypes_array;

            $from = $_POST[&#039;PARAM_0&#039;];
        $to = $_POST[&#039;PARAM_1&#039;];
        $fromcust = $_POST[&#039;PARAM_2&#039;];
        $area = $_POST[&#039;PARAM_3&#039;]; //added by Faisal to filter by area
        $folk = $_POST[&#039;PARAM_4&#039;];  // added by Faisal to filter by sales person
        $currency = $_POST[&#039;PARAM_5&#039;];
        $no_zeros = $_POST[&#039;PARAM_6&#039;];
        $hide_trans = 1;
        $comments = $_POST[&#039;PARAM_7&#039;];
    $orientation = $_POST[&#039;PARAM_8&#039;];
    $destination = $_POST[&#039;PARAM_9&#039;];
    if ($destination)
        include_once($path_to_root . &quot;/reporting/includes/excel_report.inc&quot;);
    else
        include_once($path_to_root . &quot;/reporting/includes/pdf_report.inc&quot;);

    $orientation = ($orientation ? &#039;L&#039; : &#039;P&#039;);
    if ($fromcust == ALL_TEXT)
        $cust = _(&#039;All&#039;);
    else
        $cust = get_customer_name($fromcust);
        $dec = user_price_dec();

    if ($area == ALL_NUMERIC)
             $area = 0;

     if ($area == 0)
             $sarea = _(&#039;All Areas&#039;);
     else
             $sarea = get_area_name($area);

     if ($folk == ALL_NUMERIC)
             $folk = 0;
     if ($folk == 0)
             $salesfolk = _(&#039;All Sales Man&#039;);
     else
             $salesfolk = get_salesman_name($folk);

    if ($currency == ALL_TEXT)
    {
        $convert = true;
        $currency = _(&#039;Balances in Home Currency&#039;);
    }
    else
        $convert = false;

    if ($no_zeros) $nozeros = _(&#039;Yes&#039;);
    else $nozeros = _(&#039;No&#039;);

    $cols = array(0, 70, 140, 180, 230,    270, 350, 445, 495,    555);

    $headers = array(_(&#039;Name&#039;), _(&#039;&#039;), _(&#039;&#039;), _(&#039;Opening Bal&#039;), _(&#039;&#039;), _(&#039;Debits&#039;), _(&#039;Credits&#039;),
        _(&#039;&#039;),     _(&#039;Balance&#039;));

    $aligns = array(&#039;left&#039;,&#039;left&#039;,    &#039;left&#039;,    &#039;right&#039;,    &#039;left&#039;,    &#039;right&#039;, &#039;right&#039;, &#039;right&#039;, &#039;right&#039;);

    $params =   array(     0 =&gt; $comments,
                        1 =&gt; array(&#039;text&#039; =&gt; _(&#039;Period&#039;), &#039;from&#039; =&gt; $from,         &#039;to&#039; =&gt; $to),
                        2 =&gt; array(&#039;text&#039; =&gt; _(&#039;Customer&#039;), &#039;from&#039; =&gt; $cust,       &#039;to&#039; =&gt; &#039;&#039;),
                        3 =&gt; array(&#039;text&#039; =&gt; _(&#039;Currency&#039;), &#039;from&#039; =&gt; $currency, &#039;to&#039; =&gt; &#039;&#039;),
                        4 =&gt; array(&#039;text&#039; =&gt; _(&#039;Suppress Zeros&#039;), &#039;from&#039; =&gt; $nozeros, &#039;to&#039; =&gt; &#039;&#039;));

    $rep = new FrontReport(_(&#039;Customer TB&#039;), &quot;Customer TB&quot;, user_pagesize(), 9, $orientation);
    if ($orientation == &#039;L&#039;)
        recalculate_cols($cols);
    $rep-&gt;Font();
    $rep-&gt;Info($params, $cols, $headers, $aligns);
    $rep-&gt;NewPage();

    $grandtotal = array(0,0,0,0);


        $sql = &quot;SELECT &quot;.TB_PREF.&quot;debtors_master.debtor_no, name, curr_code FROM &quot;.TB_PREF.&quot;debtors_master
        INNER JOIN &quot;.TB_PREF.&quot;cust_branch
        ON &quot;.TB_PREF.&quot;debtors_master.debtor_no=&quot;.TB_PREF.&quot;cust_branch.debtor_no
    INNER JOIN &quot;.TB_PREF.&quot;areas
        ON &quot;.TB_PREF.&quot;cust_branch.area = &quot;.TB_PREF.&quot;areas.area_code
    INNER JOIN &quot;.TB_PREF.&quot;salesman
        ON &quot;.TB_PREF.&quot;cust_branch.salesman=&quot;.TB_PREF.&quot;salesman.salesman_code&quot;;
    if ($fromcust != ALL_TEXT )
        {
           // if ($area != 0 || $folk != 0) continue;
            $sql .= &quot; WHERE &quot;.TB_PREF.&quot;debtors_master.debtor_no=&quot;.db_escape($fromcust);
        }

    elseif ($area != 0)
        {
            if ($folk != 0)
                $sql .= &quot; WHERE &quot;.TB_PREF.&quot;salesman.salesman_code=&quot;.db_escape($folk).&quot;
                    AND &quot;.TB_PREF.&quot;areas.area_code=&quot;.db_escape($area);
            else
                $sql .= &quot; WHERE &quot;.TB_PREF.&quot;areas.area_code=&quot;.db_escape($area);
        }
    elseif ($folk != 0 )
        {
            $sql .= &quot; WHERE &quot;.TB_PREF.&quot;salesman.salesman_code=&quot;.db_escape($folk);
        }

$sql .= &quot; GROUP BY &quot;.TB_PREF.&quot;debtors_master.debtor_no ORDER BY name&quot;;


    $result = db_query($sql, &quot;The customers could not be retrieved&quot;);

$tot_cur_cr = $tot_cur_db = 0;
    while ($myrow = db_fetch($result))
    {
        if (!$convert &amp;&amp; $currency != $myrow[&#039;curr_code&#039;]) continue;

        $accumulate = 0;
        $rate = $convert ? get_exchange_rate_from_home_currency($myrow[&#039;curr_code&#039;], Today()) : 1;
        $bal = get_open_balance($myrow[&#039;debtor_no&#039;], $from, $convert);
        $init[0] = $init[1] = 0.0;
        $init[0] = round2(abs($bal[&#039;charges&#039;]*$rate), $dec);
        $init[1] = round2(Abs($bal[&#039;credits&#039;]*$rate), $dec);
        $init[2] = round2($bal[&#039;Allocated&#039;]*$rate, $dec);
        $init[3] = $init[0] - $init[1];
        $accumulate += $init[3];

        $res = get_transactions($myrow[&#039;debtor_no&#039;], $from, $to, false);

        $total = array(0,0,0,0);
            for ($i = 0; $i &lt; 4; $i++)
            {
                    $total[$i] += $init[$i];
                    $grandtotal[$i] += $init[$i];

            }

            if ($no_zeros &amp;&amp; $init[3]==0) continue;

        if (db_num_rows($res)==0) {

                $rep-&gt;TextCol(0, 2, $myrow[&#039;name&#039;]);
                $rep-&gt;AmountCol(3, 4, $init[3], $dec);
                $rep-&gt;AmountCol(8, 9, $init[3], $dec);
                $rep-&gt;Line($rep-&gt;row  - 2);
               $rep-&gt;NewLine(1);
                continue;
        }
        $curr_cr = $curr_db = 0;
        while ($trans = db_fetch($res)) //Detail starts here
        {

          $item[0] = $item[1] = 0.0;
            //modified below by faisal
            if ($trans[&#039;type&#039;] == ST_CUSTCREDIT || $trans[&#039;type&#039;] == ST_CUSTPAYMENT || $trans[&#039;type&#039;] == ST_BANKDEPOSIT || $trans[&#039;type&#039;] == ST_BULKDEPOSIT || $trans[&#039;type&#039;] == ST_CASHDEPOSIT)
                $trans[&#039;TotalAmount&#039;] *= -1;
            if ($trans[&#039;TotalAmount&#039;] &gt; 0.0)
            {
                $item[0] = round2(abs($trans[&#039;TotalAmount&#039;]) * $rate, $dec);
                $accumulate += $item[0];
                $curr_db += $item[0];
                $tot_cur_db += $item[0];
                $item[2] = round2($trans[&#039;Allocated&#039;] * $rate, $dec);
            }
            else
            {
                $item[1] = round2(Abs($trans[&#039;TotalAmount&#039;]) * $rate, $dec);
                $accumulate -= $item[1];
                $curr_cr += $item[1];
                $tot_cur_cr +=$item[1];
                $item[2] = round2($trans[&#039;Allocated&#039;] * $rate, $dec) * -1;
            }

            if ($trans[&#039;type&#039;] == ST_JOURNAL || $trans[&#039;type&#039;] == ST_SALESINVOICE || $trans[&#039;type&#039;] == ST_BANKPAYMENT)
                $item[3] = $item[0] - $item[2];
            else
                $item[3] = -$item[1] - $item[2];


            for ($i = 0; $i &lt; 4; $i++)
            {
                $total[$i] += $item[$i];
                $grandtotal[$i] += $item[$i];
            }
            $total[3] = $total[0] - $total[1];
        }

        $rep-&gt;TextCol(0, 2, $myrow[&#039;name&#039;]);
        $rep-&gt;AmountCol(8, 9, $total[3], $dec);

        $rep-&gt;AmountCol(3, 4, $total[3] + $curr_cr - $curr_db, $dec);
        $rep-&gt;AmountCol(5, 6, $curr_db, $dec);

        $rep-&gt;AmountCol(6, 7, $curr_cr, $dec);



           $rep-&gt;Line($rep-&gt;row  - 2);
           $rep-&gt;NewLine(1);
    }
    $rep-&gt;NewLine();
    $rep-&gt;fontSize += 2;
    $rep-&gt;TextCol(0, 3, _(&#039;Grand Total&#039;));
    $rep-&gt;fontSize -= 2;
    $grandtotal[3] = $grandtotal[0] - $grandtotal[1];

    $rep-&gt;AmountCol(3, 4, $grandtotal[3] - $tot_cur_db + $tot_cur_cr, $dec);
    $rep-&gt;AmountCol(5, 6, $tot_cur_db, $dec);
    $rep-&gt;AmountCol(6, 7, $tot_cur_cr, $dec);
        $rep-&gt;AmountCol(8, 9, $grandtotal[3], $dec);
    $rep-&gt;Line($rep-&gt;row  - 2);
    $rep-&gt;NewLine();
        $rep-&gt;End();
}</code></pre></div><p>Below is the code to be added in reports_main.php</p><div class="codebox"><pre><code>$reports-&gt;addReport(RC_CUSTOMER, &quot;_customer_tb&quot;, _(&#039;Customer TB&#039;),
                array(    _(&#039;Start Date&#039;) =&gt; &#039;DATEBEGIN&#039;,
                        _(&#039;End Date&#039;) =&gt; &#039;DATEENDM&#039;,
                        _(&#039;Customer&#039;) =&gt; &#039;CUSTOMERS_NO_FILTER&#039;,
                        _(&#039;Sales Areas&#039;) =&gt; &#039;AREAS&#039;,
                        _(&#039;Sales Folk&#039;) =&gt; &#039;SALESMEN&#039;,
                        _(&#039;Currency Filter&#039;) =&gt; &#039;CURRENCY&#039;,
                        _(&#039;Suppress Zeros&#039;) =&gt; &#039;YES_NO&#039;,
                        _(&#039;Comments&#039;) =&gt; &#039;TEXTBOX&#039;,
                        _(&#039;Orientation&#039;) =&gt; &#039;ORIENTATION&#039;,
                        _(&#039;Destination&#039;) =&gt; &#039;DESTINATION&#039;));</code></pre></div><p>Here is the <a href="https://www.dropbox.com/s/prut0acv9zjh1xo/CustomerTB.png?dl=0">screen shot</a></p>]]></content>
			<author>
				<name><![CDATA[boxygen]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=20175</uri>
			</author>
			<updated>2018-12-21T06:11:21Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=33713#p33713</id>
		</entry>
</feed>
