Incorrect Journal Balance (sales invoice) when many decimals in tax and price.
[fa-stable.git] / reporting / rep106.php
index ca1c84725b1b7a8bc07dabf96f6cf5331c1e92c4..1274a8df9cb94b4018c63e7f1940b2cafbb9ef3b 100644 (file)
@@ -35,20 +35,27 @@ function GetSalesmanTrans($from, $to)
        $fromdate = date2sql($from);
        $todate = date2sql($to);
 
-       $sql = "SELECT DISTINCT ".TB_PREF."debtor_trans.*,
-               ov_amount+ov_discount AS InvoiceTotal,
-               ".TB_PREF."debtors_master.name AS DebtorName, ".TB_PREF."debtors_master.curr_code, ".TB_PREF."cust_branch.br_name,
-               ".TB_PREF."cust_branch.contact_name, ".TB_PREF."salesman.*
-               FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master, ".TB_PREF."sales_orders, ".TB_PREF."cust_branch,
-                       ".TB_PREF."salesman
-               WHERE ".TB_PREF."sales_orders.order_no=".TB_PREF."debtor_trans.order_
-                   AND ".TB_PREF."sales_orders.branch_code=".TB_PREF."cust_branch.branch_code
-                   AND ".TB_PREF."cust_branch.salesman=".TB_PREF."salesman.salesman_code
-                   AND ".TB_PREF."debtor_trans.debtor_no=".TB_PREF."debtors_master.debtor_no
-                   AND (".TB_PREF."debtor_trans.type=".ST_SALESINVOICE." OR ".TB_PREF."debtor_trans.type=".ST_CUSTCREDIT.")
-                   AND ".TB_PREF."debtor_trans.tran_date>='$fromdate'
-                   AND ".TB_PREF."debtor_trans.tran_date<='$todate'
-               ORDER BY ".TB_PREF."salesman.salesman_code, ".TB_PREF."debtor_trans.tran_date";
+       $sql = "SELECT DISTINCT trans.*,
+                       ov_amount+ov_discount AS InvoiceTotal,
+                       cust.name AS DebtorName,
+                       cust.curr_code,
+                       branch.br_name,
+                       sorder.customer_ref,
+                       salesman.*
+               FROM ".TB_PREF."debtor_trans trans,
+                        ".TB_PREF."debtors_master cust,
+                        ".TB_PREF."sales_orders sorder,
+                        ".TB_PREF."cust_branch branch,
+                       ".TB_PREF."salesman salesman
+               WHERE sorder.order_no=trans.order_
+                   AND sorder.branch_code=branch.branch_code
+                   AND sorder.trans_type = ".ST_SALESORDER."
+                   AND branch.salesman=salesman.salesman_code
+                   AND trans.debtor_no=cust.debtor_no
+                   AND (trans.type=".ST_SALESINVOICE." OR trans.type=".ST_CUSTCREDIT.")
+                   AND trans.tran_date>='$fromdate'
+                   AND trans.tran_date<='$todate'
+               ORDER BY salesman.salesman_code, trans.tran_date";
 
        return db_query($sql, "Error getting order details");
 }
@@ -63,11 +70,13 @@ function print_salesman_list()
        $to = $_POST['PARAM_1'];
        $summary = $_POST['PARAM_2'];
        $comments = $_POST['PARAM_3'];
-       $destination = $_POST['PARAM_4'];
+       $orientation = $_POST['PARAM_4'];
+       $destination = $_POST['PARAM_5'];
        if ($destination)
                include_once($path_to_root . "/reporting/includes/excel_report.inc");
        else
                include_once($path_to_root . "/reporting/includes/pdf_report.inc");
+       $orientation = ($orientation ? 'L' : 'P');
 
        if ($summary == 0)
                $sum = _("No");
@@ -90,10 +99,12 @@ function print_salesman_list()
                                        1 => array(  'text' => _('Period'), 'from' => $from, 'to' => $to),
                                        2 => array(  'text' => _('Summary Only'),'from' => $sum,'to' => ''));
 
-       $cols2 = $cols;
        $aligns2 = $aligns;
 
-       $rep = new FrontReport(_('Salesman Listing'), "SalesmanListing", user_pagesize());
+       $rep = new FrontReport(_('Salesman Listing'), "SalesmanListing", user_pagesize(), 9, $orientation);
+    if ($orientation == 'L')
+       recalculate_cols($cols);
+       $cols2 = $cols;
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
 
@@ -133,16 +144,20 @@ function print_salesman_list()
                }
                $rate = $myrow['rate'];
                $amt = $myrow['InvoiceTotal'] * $rate;
-               if ($subprov > $myrow['break_pt'] && $myrow['provision2'] != 0)
-                       $prov = $myrow['provision2'] * $amt / 100;
-               else
+               if ($myrow['provision2'] == 0)
                        $prov = $myrow['provision'] * $amt / 100;
+               else {
+                       $amt1 = min($amt, max(0, $myrow['break_pt']-$subtotal));
+                       $amt2 = $amt - $amt1;
+
+                       $prov = $amt1*$myrow['provision']/100 + $amt2*$myrow['provision2']/100;
+               }
                if (!$summary)
                {
                        $rep->TextCol(0, 1,     $myrow['trans_no']);
                        $rep->TextCol(1, 2,     $myrow['DebtorName']);
                        $rep->TextCol(2, 3,     $myrow['br_name']);
-                       $rep->TextCol(3, 4,     $myrow['contact_name']);
+                       $rep->TextCol(3, 4,     $myrow['customer_ref']);
                        $rep->DateCol(4, 5,     $myrow['tran_date'], true);
                        $rep->AmountCol(5, 6, $amt, $dec);
                        $rep->AmountCol(6, 7, $prov, $dec);
@@ -173,4 +188,3 @@ function print_salesman_list()
        $rep->End();
 }
 
-?>
\ No newline at end of file