Inserted Copyright Notice and fixed graphic items
[fa-stable.git] / reporting / rep709.php
index d8aed7219994ed532c3c4d5b6fe8aa47fe4b4943..471343dc1939ae3a6482776b84ac805f397a17ae 100644 (file)
@@ -1,5 +1,14 @@
 <?php
-
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU Affero General Public License,
+       AGPL, 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 <http://www.gnu.org/licenses/agpl-3.0.html>.
+***********************************************************************/
 $page_security = 2;
 // ----------------------------------------------------------------
 // $ Revision: 2.0 $
@@ -20,10 +29,11 @@ print_tax_report();
 
 function getTax($tno, $tpe)
 {
-       $sql = "SELECT amount, included_in_price FROM ".TB_PREF."debtor_trans_tax_details
-               WHERE ".TB_PREF."debtor_trans_tax_details.debtor_trans_no=$tno
-                       AND ".TB_PREF."debtor_trans_tax_details.debtor_trans_type=$tpe
-                       AND ".TB_PREF."debtor_trans_tax_details.amount <> 0";
+       //      GROUP BY debtor_trans_type, debtor_trans_no";
+       $sql = "SELECT SUM(amount) AS Amount, MAX(included_in_price) AS Included FROM ".TB_PREF."debtor_trans_tax_details
+               WHERE debtor_trans_no=$tno
+                       AND debtor_trans_type=$tpe
+                       AND amount <> 0";
 
     $result = db_query($sql,"No transactions were returned");
     return db_fetch($result);
@@ -35,8 +45,6 @@ function getCustTransactions($from, $to)
        $todate = date2sql($to);
 
        $netamount = "IF(".TB_PREF."debtor_trans.type=11,-(ov_amount+ov_freight+ov_discount),ov_amount+ov_freight+ov_discount)*".TB_PREF."debtor_trans.rate";
-       //      IF(".TB_PREF."debtor_trans.type=11,-(ov_amount+ov_freight+ov_discount),ov_amount+ov_freight+ov_discount)*rate AS NetAmount,
-       //      IF(".TB_PREF."debtor_trans.type=11,-(ov_gst+ov_freight_tax),ov_gst+ov_freight_tax)*rate AS Tax
 
        $sql = "SELECT ".TB_PREF."debtor_trans.reference,
                        ".TB_PREF."debtor_trans.trans_no,
@@ -90,7 +98,7 @@ function getSuppTransactions($from, $to)
 
 function getTaxTypes()
 {
-       $sql = "SELECT id FROM ".TB_PREF."tax_types ORDER BY id";
+       $sql = "SELECT * FROM ".TB_PREF."tax_types ORDER BY id";
     return db_query($sql,"No transactions were returned");
 }
 
@@ -107,13 +115,15 @@ function getCustInvTax($taxtype, $from, $to)
        $todate = date2sql($to);
        $amount = "IF(".TB_PREF."debtor_trans_tax_details.debtor_trans_type=11,-amount,amount)*".TB_PREF."debtor_trans.rate";
        $mamount = "IF(".TB_PREF."debtor_trans_tax_details.included_in_price=0, 0, $amount)";
-       $netamount = "IF(".TB_PREF."debtor_trans.type=11,-(ov_amount+ov_freight+ov_discount),ov_amount+ov_freight+ov_discount)*".TB_PREF."debtor_trans.rate-$mamount";
+       $netamount = "IF(".TB_PREF."debtor_trans.type=11,-quantity,quantity)*unit_price*".TB_PREF."debtor_trans.rate-$mamount";
 
        $sql = "SELECT SUM($netamount),
                SUM($amount)
-               FROM ".TB_PREF."debtor_trans_tax_details, ".TB_PREF."debtor_trans
+               FROM ".TB_PREF."debtor_trans_details, ".TB_PREF."debtor_trans_tax_details, ".TB_PREF."debtor_trans
                                WHERE ".TB_PREF."debtor_trans.type>=10
                                        AND ".TB_PREF."debtor_trans.type<=11
+                                       AND ".TB_PREF."debtor_trans_details.debtor_trans_no=".TB_PREF."debtor_trans.trans_no
+                                       AND ".TB_PREF."debtor_trans_details.debtor_trans_type=".TB_PREF."debtor_trans.type
                                        AND ".TB_PREF."debtor_trans.trans_no=".TB_PREF."debtor_trans_tax_details.debtor_trans_no
                                        AND ".TB_PREF."debtor_trans.type=".TB_PREF."debtor_trans_tax_details.debtor_trans_type
                                        AND ".TB_PREF."debtor_trans_tax_details.amount <> 0
@@ -129,12 +139,14 @@ function getSuppInvTax($taxtype, $from, $to)
 {
        $fromdate = date2sql($from);
        $todate = date2sql($to);
-       $sql = "SELECT SUM((ov_amount+ov_discount) * ".TB_PREF."supp_trans.rate), SUM(amount*".TB_PREF."supp_trans.rate)
-               FROM ".TB_PREF."supp_invoice_tax_items, ".TB_PREF."supp_trans
+       $sql = "SELECT SUM(unit_price * quantity * ".TB_PREF."supp_trans.rate), SUM(amount*".TB_PREF."supp_trans.rate)
+               FROM ".TB_PREF."supp_invoice_items, ".TB_PREF."supp_invoice_tax_items, ".TB_PREF."supp_trans
                                WHERE ".TB_PREF."supp_trans.type>=20
                                        AND ".TB_PREF."supp_trans.type<=21
                                        AND ".TB_PREF."supp_trans.trans_no=".TB_PREF."supp_invoice_tax_items.supp_trans_no
                                        AND ".TB_PREF."supp_trans.type=".TB_PREF."supp_invoice_tax_items.supp_trans_type
+                                       AND ".TB_PREF."supp_invoice_items.supp_trans_no=".TB_PREF."supp_trans.trans_no
+                                       AND ".TB_PREF."supp_invoice_items.supp_trans_type=".TB_PREF."supp_trans.type
                                        AND ".TB_PREF."supp_invoice_tax_items.tax_type_id=$taxtype
                                        AND ".TB_PREF."supp_invoice_tax_items.amount <> 0
                                        AND ".TB_PREF."supp_trans.tran_date >= '$fromdate'
@@ -184,9 +196,9 @@ function print_tax_report()
                $cols = array(0, 80, 130, 190, 290, 370, 435, 500, 565);
 
                $headers = array(_('Trans Type'), _('#'), _('Date'), _('Name'), _('Branch Name'),
-                       _('Net'), _('Tax'));
+                       _('Net'), _('Tax'), '');
 
-               $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right');
+               $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right');
 
                $params =   array(      0 => $comments,
                                                        1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
@@ -208,12 +220,12 @@ function print_tax_report()
                        $tax_amt = 0;
                else
                {
-                       $tx['amount'] *= $trans['rate'];
+                       $tx['Amount'] *= $trans['rate'];
                        if ($trans['type'] == 11)
-                               $tx['amount'] *= -1;
-                       if ($tx['included_in_price'])
-                               $trans['NetAmount'] -= $tx['amount'];
-                       $tax_amt = $tx['amount'];
+                               $tx['Amount'] *= -1;
+                       if ($tx['Included'] > 0)
+                               $trans['NetAmount'] -= $tx['Amount'];
+                       $tax_amt = $tx['Amount'];
                }
                if (!$summaryOnly)
                {
@@ -385,6 +397,71 @@ function print_tax_report()
        }
        $rep->Line($rep->row - 4);
 
+       $rep->row -= 16;
+       $rep->Font('italic');
+       $rep->TextCol(0, 1, _("General Ledger"));
+       $rep->aligns[1] = 'left';
+       $rep->TextCol(1, 3, _("Description"));
+       $rep->TextCol(3, 4, _("Amount"));
+       $rep->Font();
+       $rep->Line($rep->row - 6);
+
+       $rep->row -= 22;
+       
+       $taxes = getTaxTypes();
+       $total = 0;
+       $bdate = date2sql($from);
+       $edate = date2sql($to);
+
+       while ($tx = db_fetch($taxes))
+       {
+               if ($tx['sales_gl_code'] == $tx['purchasing_gl_code'])
+               {
+                       $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) AS payable, SUM(IF(amount < 0, -amount, 0)) AS collectible
+                               FROM ".TB_PREF."gl_trans WHERE account = '".$tx['sales_gl_code']."' AND tran_date >= '$bdate' AND tran_date <= '$edate'";
+                       $result = db_query($sql, "Error retrieving tax inquiry");
+                       $row = db_fetch($result);
+                       $payable = -$row['payable'];
+                       $collectible.= -$row['collectible'];
+               }
+               else
+               {
+                       $sql = "SELECT SUM(amount) AS collectible
+                               FROM ".TB_PREF."gl_trans WHERE account = '".$tx['sales_gl_code']."' AND tran_date >= '$bdate' AND tran_date <= '$edate'";
+                       $result = db_query($sql, "Error retrieving tax inquiry");
+                       $row = db_fetch($result);
+                       $collectible = -$row['collectible'];
+                       $sql = "SELECT SUM(amount) AS payable
+                               FROM ".TB_PREF."gl_trans WHERE account = '".$tx['purchasing_gl_code']."' AND tran_date >= '$bdate' AND tran_date <= '$edate'";
+                       $result = db_query($sql, "Error retrieving tax inquiry");
+                       $row = db_fetch($result);
+                       $payable = -$row['payable'];
+               }
+               $net = $collectible + $payable;
+               $total += $net;
+               $rep->TextCol(0, 1, $tx['name'] . " " . $tx['rate'] . "%");
+               $rep->TextCol(1, 3, _("Charged on sales") . " (" . _("Output Tax")."):");
+               $rep->TextCol(3, 4, number_format2($collectible, $dec));
+               $rep->NewLine();
+               $rep->TextCol(0, 1, $tx['name'] . " " . $tx['rate'] . "%");
+               $rep->TextCol(1, 3, _("Paid on purchases") . " (" . _("Input Tax")."):");
+               $rep->TextCol(3, 4, number_format2($payable, $dec));
+               $rep->NewLine();
+               $rep->Font('bold');
+               $rep->TextCol(0, 1, $tx['name'] . " " . $tx['rate'] . "%");
+               $rep->TextCol(1, 3, _("Net payable or collectible"));
+               $rep->TextCol(3, 4, number_format2($net, $dec));
+               $rep->Font();
+               $rep->NewLine();
+       }
+       $rep->Font('bold');
+       $rep->TextCol(1, 3, _("Total payable or refund"));
+       $rep->TextCol(3, 4, number_format2($total, $dec));
+       $rep->Font();
+       $rep->NewLine();
+       
+       $rep->Line($rep->row - 4);
+
        $locale = $path_to_root . "lang/" . $_SESSION['language']->code . "/locale.inc";
        if (file_exists($locale))
        {