Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[fa-stable.git] / reporting / rep114.php
index 96f743e76834c5920f3bfcac8b6b83513070d847..d1cc7a36d0dcd8e1a572734263c139e4fea123bc 100644 (file)
@@ -32,58 +32,71 @@ function getTaxTransactions($from, $to, $tax_id)
 {
        $fromdate = date2sql($from);
        $todate = date2sql($to);
-\r
-       $sql = "SELECT d.debtor_no, d.name AS cust_name, d.tax_id, 
-               CASE WHEN net_amount IS NULL THEN 
-                       SUM(CASE WHEN dt.type=".ST_CUSTCREDIT." THEN (ov_amount+ov_freight+ov_discount)*-1 
-                       ELSE (ov_amount+ov_freight+ov_discount) END *dt.rate) ELSE 
-                       SUM(CASE WHEN dt.type=".ST_CUSTCREDIT." THEN -net_amount ELSE net_amount END *ex_rate) END AS total,
-               SUM(CASE WHEN dt.type=".ST_CUSTCREDIT." THEN -amount ELSE amount END *ex_rate) AS tax\r
-               FROM ".TB_PREF."debtor_trans dt\r
-                       LEFT JOIN ".TB_PREF."debtors_master d ON d.debtor_no=dt.debtor_no\r
-                       LEFT JOIN ".TB_PREF."trans_tax_details t ON (t.trans_type=dt.type AND t.trans_no=dt.trans_no)\r
-               WHERE (dt.type=".ST_SALESINVOICE." OR dt.type=".ST_CUSTCREDIT.") ";\r
-       if ($tax_id)\r
-               $sql .= "AND tax_id<>'' ";\r
-       $sql .= "AND dt.tran_date >=".db_escape($fromdate)." AND dt.tran_date<=".db_escape($todate)."\r
-               GROUP BY d.debtor_no, d.name, d.tax_id ORDER BY d.name"; \r
+
+       $sql = "SELECT d.debtor_no, d.name AS cust_name, d.tax_id, dt.type, dt.trans_no,  
+                       CASE WHEN dt.type=".ST_CUSTCREDIT." THEN (ov_amount+ov_freight+ov_discount)*-1 
+                       ELSE (ov_amount+ov_freight+ov_discount) END *dt.rate AS total
+               FROM ".TB_PREF."debtor_trans dt
+                       LEFT JOIN ".TB_PREF."debtors_master d ON d.debtor_no=dt.debtor_no
+               WHERE (dt.type=".ST_SALESINVOICE." OR dt.type=".ST_CUSTCREDIT.") ";
+       if ($tax_id)
+               $sql .= "AND tax_id<>'' ";
+       $sql .= "AND dt.tran_date >=".db_escape($fromdate)." AND dt.tran_date<=".db_escape($todate)."
+               ORDER BY d.debtor_no"; 
     return db_query($sql,"No transactions were returned");
 }
 
+function getTaxes($type, $trans_no)
+{
+       $sql = "SELECT included_in_price, SUM(CASE WHEN trans_type=".ST_CUSTCREDIT." THEN -amount ELSE amount END * ex_rate) AS tax
+               FROM ".TB_PREF."trans_tax_details WHERE trans_type=$type AND trans_no=$trans_no GROUP BY included_in_price";
+
+    $result = db_query($sql,"No transactions were returned");
+    if ($result !== false)
+       return db_fetch($result);
+    else
+       return null;
+}      
+
 //----------------------------------------------------------------------------------------------------
 
 function print_sales_summary_report()
 {
-       global $path_to_root;\r
+       global $path_to_root;
        
        $from = $_POST['PARAM_0'];
        $to = $_POST['PARAM_1'];
-       $tax_id = $_POST['PARAM_2'];\r
+       $tax_id = $_POST['PARAM_2'];
        $comments = $_POST['PARAM_3'];
-       $destination = $_POST['PARAM_4'];
-       if ($tax_id == 0)\r
-               $tid = _('No');\r
-       else\r
-               $tid = _('Yes');\r
-\r
+       $orientation = $_POST['PARAM_4'];
+       $destination = $_POST['PARAM_5'];
+       if ($tax_id == 0)
+               $tid = _('No');
+       else
+               $tid = _('Yes');
+
 
        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');
 
        $dec = user_price_dec();
 
-       $rep = new FrontReport(_('Sales Summary Report'), "SalesSummaryReport", user_pagesize());
+       $rep = new FrontReport(_('Sales Summary Report'), "SalesSummaryReport", user_pagesize(), 9, $orientation);
 
        $params =   array(      0 => $comments,
                                                1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
                                                2 => array(  'text' => _('Tax Id Only'),'from' => $tid,'to' => ''));
 
-       $cols = array(0, 130, 180, 270, 350, 500);\r
+       $cols = array(0, 130, 180, 270, 350, 500);
+
+       $headers = array(_('Customer'), _('Tax Id'), _('Total ex. Tax'), _('Tax'));
+       $aligns = array('left', 'left', 'right', 'right');
+    if ($orientation == 'L')
+       recalculate_cols($cols);
 
-       $headers = array(_('Customer'), _('Tax Id'), _('Total ex. Tax'), _('Tax'));\r
-       $aligns = array('left', 'left', 'right', 'right');\r
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->NewPage();
@@ -91,38 +104,66 @@ function print_sales_summary_report()
        $totalnet = 0.0;
        $totaltax = 0.0;
        $transactions = getTaxTransactions($from, $to, $tax_id);
-\r
-       $rep->TextCol(0, 4, _("Values in domestic currency"));\r
-       $rep->NewLine(2);\r
+
+       $rep->TextCol(0, 4, _('Balances in Home Currency'));
+       $rep->NewLine(2);
        
+       $custno = 0;
+       $tax = $total = 0;
+       $custname = $tax_id = "";
        while ($trans=db_fetch($transactions))
        {
-               $rep->TextCol(0, 1, $trans['cust_name']);
-               $rep->TextCol(1, 2,     $trans['tax_id']);
-               $rep->AmountCol(2, 3,   $trans['total'], $dec);
-               $rep->AmountCol(3, 4,   $trans['tax'], $dec);
-               $totalnet += $trans['total'];\r
-               $totaltax += $trans['tax'];\r
-\r
-               $rep->NewLine();
+               if ($custno != $trans['debtor_no'])
+               {
+                       if ($custno != 0)
+                       {
+                               $rep->TextCol(0, 1, $custname);
+                               $rep->TextCol(1, 2,     $tax_id);
+                               $rep->AmountCol(2, 3, $total, $dec);
+                               $rep->AmountCol(3, 4, $tax, $dec);
+                               $totalnet += $total;
+                               $totaltax += $tax;
+                               $total = $tax = 0;
+                               $rep->NewLine();
 
-               if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
+                               if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
+                               {
+                                       $rep->Line($rep->row - 2);
+                                       $rep->NewPage();
+                               }
+                       }
+                       $custno = $trans['debtor_no'];
+                       $custname = $trans['cust_name'];
+                       $tax_id = $trans['tax_id'];
+               }       
+               $taxes = getTaxes($trans['type'], $trans['trans_no']);
+               if ($taxes != null)
                {
-                       $rep->Line($rep->row - 2);
-                       $rep->NewPage();
-               }
+                       if ($taxes['included_in_price'])
+                               $trans['total'] -= $taxes['tax'];
+                       $tax += $taxes['tax'];
+               }       
+               $total += $trans['total']; 
        }
-\r
-       $rep->Font('bold');\r
-       $rep->NewLine();\r
-       $rep->Line($rep->row + $rep->lineHeight);\r
-       $rep->TextCol(0, 2,     _("Total"));\r
-       $rep->AmountCol(2, 3, $totalnet, $dec);\r
-       $rep->AmountCol(3, 4, $totaltax, $dec);\r
-       $rep->Line($rep->row - 5);\r
-       $rep->Font();\r
-\r
+       if ($custno != 0)
+       {
+               $rep->TextCol(0, 1, $custname);
+               $rep->TextCol(1, 2,     $tax_id);
+               $rep->AmountCol(2, 3, $total, $dec);
+               $rep->AmountCol(3, 4, $tax, $dec);
+               $totalnet += $total;
+               $totaltax += $tax;
+               $rep->NewLine();
+       }
+       $rep->Font('bold');
+       $rep->NewLine();
+       $rep->Line($rep->row + $rep->lineHeight);
+       $rep->TextCol(0, 2,     _("Total"));
+       $rep->AmountCol(2, 3, $totalnet, $dec);
+       $rep->AmountCol(3, 4, $totaltax, $dec);
+       $rep->Line($rep->row - 5);
+       $rep->Font();
+
        $rep->End();
 }
 
-?>
\ No newline at end of file