Additional fixes for journal handling. Added reg_type in trans_tax_details.
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Thu, 19 Mar 2015 18:05:45 +0000 (19:05 +0100)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Wed, 25 Mar 2015 11:49:47 +0000 (12:49 +0100)
gl/includes/db/gl_db_trans.inc
gl/inquiry/journal_inquiry.php
includes/ui/items_cart.inc
purchasing/includes/db/invoice_db.inc
reporting/rep709.php
sales/includes/db/sales_credit_db.inc
sales/includes/db/sales_delivery_db.inc
sales/includes/db/sales_invoice_db.inc
sql/alter2.4.sql
sql/en_US-demo.sql
sql/en_US-new.sql

index 18c6ba666b8def991f78a69caabba6dc968f0efd..d5adab4fa87d27ffc3bbe3faf4298efbcb5511cc 100644 (file)
@@ -407,7 +407,7 @@ function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate,
                }
        }
        add_trans_tax_details($trans_type, $trans_no, $tax['id'], $tax['rate'], $included, 
-               $amount, $net_amount, $ex_rate, $date, $memo);
+               $amount, $net_amount, $ex_rate, $date, $memo, null);
 
 }
 
@@ -417,17 +417,21 @@ function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate,
 //     actual tax type rate.
 //
 function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
-       $amount, $net_amount, $ex_rate, $tran_date, $memo)
+       $amount, $net_amount, $ex_rate, $tran_date, $memo, $reg_type=null)
 {
+       // guess tax register if not set
+       if (!isset($reg_type))
+               $reg_type = in_array($trans_type, array(ST_SUPPINVOICE, ST_SUPPCREDIT)) ? TR_OUTPUT
+                               : in_array($trans_type, array(ST_SALESINVOICE, ST_CUSTCREDIT)) ? TR_INPUT : null;
 
        $sql = "INSERT INTO ".TB_PREF."trans_tax_details 
                (trans_type, trans_no, tran_date, tax_type_id, rate, ex_rate,
-                       included_in_price, net_amount, amount, memo)
+                       included_in_price, net_amount, amount, memo, reg_type)
                VALUES (".db_escape($trans_type)."," . db_escape($trans_no).",'"
                                .date2sql($tran_date)."',".db_escape($tax_id).","
                                .db_escape($rate).",".db_escape($ex_rate).",".($included ? 1:0).","
                                .db_escape($net_amount).","
-                               .db_escape($amount).",".db_escape($memo).")";
+                               .db_escape($amount).",".db_escape($memo).",".db_escape($reg_type, true).")";
 
        db_query($sql, "Cannot save trans tax details");
 
@@ -471,47 +475,46 @@ function clear_trans_tax_details($type, $type_no)
        db_query($sql, "The transaction tax details could not be cleared");
 }
 
-function get_tax_summary($from, $to)
+function get_tax_summary($from, $to, $also_zero_purchases=false)
 {
        $fromdate = date2sql($from);
        $todate = date2sql($to);
 
        $sql = "SELECT 
-                               SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE
-                                       ." || trans_type=".ST_JOURNAL.",-1,1)*
-                               IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE 
-                                       ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
-                                       ." || trans_type=".ST_CUSTCREDIT.", net_amount*ex_rate,0)) net_output,
-
-                               SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE
-                                       ." || trans_type=".ST_JOURNAL.",-1,1)*
-                               IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
-                                       ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
-                                       ." || trans_type=".ST_CUSTCREDIT.", amount*ex_rate,0)) payable,
-
-                               SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE." || trans_type=".ST_JOURNAL.",-1,1)*
-                               IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
-                                       ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
-                                       ." || trans_type=".ST_CUSTCREDIT.", 0, net_amount*ex_rate)) net_input,
-
-                               SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE." || trans_type=".ST_JOURNAL.",-1,1)*
-                               IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
-                                       ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
-                                       ." || trans_type=".ST_CUSTCREDIT.", 0, amount*ex_rate)) collectible,
+                               SUM(IF(trans_type=".ST_SUPPCREDIT.",-1,1)*IF(taxrec.amount, taxrec.effective_amount/taxrec.amount, 1)*
+                               IF((reg_type=".TR_OUTPUT.")"
+                                       ." || ((trans_type IN(".ST_SUPPINVOICE.",".ST_SUPPCREDIT.") OR (trans_type=".ST_JOURNAL." AND reg_type=".TR_INPUT."))
+                                       ), net_amount*ex_rate,0)
+                               ) net_output,
+
+                               SUM(IF(trans_type=".ST_SUPPCREDIT.",-1,1)*
+                               IF((reg_type=".TR_OUTPUT.")"
+                                       ." || ((trans_type IN(".ST_SUPPINVOICE.",".ST_SUPPCREDIT.") OR (trans_type=".ST_JOURNAL." AND reg_type=".TR_INPUT."))
+                                       ), amount*ex_rate,0)) payable,
+
+                               SUM(IF(trans_type IN(".ST_SUPPCREDIT."),-1,1)*
+                               IF(reg_type=".TR_INPUT
+                                       . ($also_zero_purchases ? '': " AND tax_type_id AND taxrec.rate")
+                                       .", net_amount*ex_rate, 0)) net_input,
+
+                               SUM(IF(trans_type IN(".ST_SUPPCREDIT."),-1,1)*
+                               IF(reg_type=".TR_INPUT
+                                       . ($also_zero_purchases ? '': " AND tax_type_id AND taxrec.rate ") 
+                                       .", amount*ex_rate, 0)) collectible,
                                taxrec.rate,
                                ttype.id,
                                ttype.name
-               FROM ".TB_PREF."tax_types ttype,
-                        ".TB_PREF."trans_tax_details taxrec
-               WHERE taxrec.tax_type_id=ttype.id
-                       AND taxrec.trans_type != ".ST_CUSTDELIVERY."
+               FROM ".TB_PREF."trans_tax_details taxrec LEFT JOIN ".TB_PREF."tax_types ttype ON taxrec.tax_type_id=ttype.id
+                       LEFT JOIN ".TB_PREF."tax_groups tgroup ON taxrec.tax_group_id=tgroup.id
+               WHERE taxrec.trans_type IN (".implode(',',
+                       array(ST_SALESINVOICE, ST_CUSTCREDIT, ST_SUPPINVOICE, ST_SUPPCREDIT, ST_JOURNAL)).")
                        AND taxrec.tran_date >= '$fromdate'
                        AND taxrec.tran_date <= '$todate'
                GROUP BY ttype.id";
-//display_error($sql);
+
+               // display_error($sql);
     return db_query($sql,"Cannot retrieve tax summary");
 }
-
 //--------------------------------------------------------------------------------------------------
 
 function exists_gl_trans($type, $trans_id)
@@ -563,7 +566,7 @@ function get_sql_for_journal_inquiry($filter, $from, $to, $ref='', $memo='', $al
                gl.tran_date,
                gl.type as trans_type,
                gl.type_no as trans_no,
-               IFNULL(max(supp.supp_name), max(cust.name)) as name,
+               IFNULL(MAX(supp.supp_name), MAX(cust.name)) as name,
                IF(ISNULL(st.supp_reference), '', st.supp_reference) AS supp_reference,
                refs.reference,"
                .($dimension ? " -SUM(IF(dim.dimension in(gl.dimension_id,gl.dimension2_id), gl.amount, 0)) as amount,":" SUM(IF(gl.amount>0, gl.amount,0)) as amount,")
index ad77e8bf919e9aa7d17bda257d72f023a9c849f8..999ae232cd23145de5073d23a85e266e862f1d80 100644 (file)
@@ -92,7 +92,12 @@ function edit_link($row)
                if ($myrow['alloc'] != 0 || get_voided_entry(ST_SALESINVOICE, $row["trans_no"]) !== false)
                        $ok = false;
        }
-       return $ok ? trans_editor_link($row["trans_type"], $row["trans_type"]) : '';
+       return $ok ? trans_editor_link( $row["trans_type"], $row["trans_no"]) : '';
+}
+
+function invoice_supp_reference($row)
+{
+       return $row['supp_reference'];
 }
 
 $sql = get_sql_for_journal_inquiry(get_post('filterType', -1), get_post('FromDate'),
@@ -104,6 +109,7 @@ $cols = array(
        _("Type") => array('fun'=>'systype_name'), 
        _("Trans #") => array('fun'=>'view_link'), 
        _("Counterparty") => array('ord' => ''),
+       _("Supplier's Reference") => 'skip',
        _("Reference"), 
        _("Amount") => array('type'=>'amount'),
        _("Memo"),
@@ -116,6 +122,11 @@ if (!check_value('AlsoClosed')) {
        $cols[_("#")] = 'skip';
 }
 
+if($_POST['filterType'] == ST_SUPPINVOICE) //add the payment column if shown supplier invoices only
+{
+       $cols[_("Supplier's Reference")] = array('fun'=>'invoice_supp_reference', 'align'=>'center');
+}
+
 $table =& new_db_pager('journal_tbl', $sql, $cols);
 
 $table->width = "80%";
index e149865f651fd9ded3b6bd6f01a308a354e27032..e2a2ca917b9a2fac75adc4559faa100053dc85d0 100644 (file)
@@ -428,10 +428,7 @@ class items_cart
                                add_trans_tax_details($this->trans_type, $this->order_id,
                                        $tax_id, $this->tax_info['rate'][$tax_id], 0, $tax_nominal, $net, $this->rate,
                                        $this->tran_date,
-                                       $this->source_ref,
-                                       $this->tax_info['tax_group'],
-                                       $this->tax_info['tax_date'],
-                                       $tax, $this->tax_info['tax_category'], 0, $reg);
+                                       $this->source_ref, $reg);
                        }
                }
        }
index b14c7199a384a6289e28392a30130551dd698868..1cfd451892b2d4d39294de1674ca13c54e72051d 100644 (file)
@@ -200,7 +200,7 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
                        }
                        add_trans_tax_details($trans_type, $invoice_id, 
                                $taxitem['tax_type_id'], $taxitem['rate'], $supp_trans->tax_included, $taxitem['Value'],
-                               $taxitem['Net'], $ex_rate, $date_, $supp_trans->supp_reference);
+                               $taxitem['Net'], $ex_rate, $date_, $supp_trans->supp_reference, TR_INPUT);
 
                        if (isset($taxitem['purchasing_gl_code']))
                        {
index b9cd1171272cbf905579c3589375c230b4fef819..9ee5ef2c3050f289c715a5dd21a46c2b32c49468 100644 (file)
@@ -53,7 +53,7 @@ function getTaxTransactions($from, $to)
                LEFT JOIN ".TB_PREF."debtors_master as debt ON dtrans.debtor_no=debt.debtor_no
                LEFT JOIN ".TB_PREF."cust_branch as branch ON dtrans.branch_code=branch.branch_code
                WHERE (taxrec.amount <> 0 OR taxrec.net_amount <> 0)
-                       AND taxrec.trans_type <> ".ST_CUSTDELIVERY."
+                       AND !ISNULL(taxrec.reg_type)
                        AND taxrec.tran_date >= '$fromdate'
                        AND taxrec.tran_date <= '$todate'
                ORDER BY taxrec.trans_type, taxrec.tran_date, taxrec.trans_no, taxrec.ex_rate";
@@ -161,18 +161,18 @@ function print_tax_report()
                                $rep->NewPage();
                        }
                }
-               if ($trans['trans_type']==ST_JOURNAL && $trans['amount']<0) {
+               if ($trans['trans_type']==ST_JOURNAL && $trans['reg_type']==TR_INPUT) {
                        $taxes[$trans['tax_type_id']]['taxin'] += $trans['amount'];
                        $taxes[$trans['tax_type_id']]['in'] += $trans['net_amount'];
                }
-               elseif ($trans['trans_type']==ST_JOURNAL && $trans['amount']>=0) {
+               elseif ($trans['trans_type']==ST_JOURNAL && $trans['reg_type']==TR_OUTPUT) {
                        $taxes[$trans['tax_type_id']]['taxout'] += $trans['amount'];
                        $taxes[$trans['tax_type_id']]['out'] += $trans['net_amount'];
                }
                elseif (in_array($trans['trans_type'], array(ST_BANKDEPOSIT,ST_SALESINVOICE,ST_CUSTCREDIT))) {
                        $taxes[$trans['tax_type_id']]['taxout'] += $trans['amount'];
                        $taxes[$trans['tax_type_id']]['out'] += $trans['net_amount'];
-               } else {
+               } elseif ($trans['reg_type'] !== NULL) {
                        $taxes[$trans['tax_type_id']]['taxin'] += $trans['amount'];
                        $taxes[$trans['tax_type_id']]['in'] += $trans['net_amount'];
                }
index cdf86abfdccaa4ebaed845476777f990c618cb0f..9e8e9ae732a6be9421212439d1d9064075116044 100644 (file)
@@ -153,7 +153,7 @@ function write_credit_note(&$credit_note, $write_off_acc)
                        add_trans_tax_details(ST_CUSTCREDIT, $credit_no, $taxitem['tax_type_id'],
                                $taxitem['rate'], $credit_note->tax_included, $taxitem['Value'],
                                $taxitem['Net'], $ex_rate,
-                               $credit_note->document_date, $credit_note->reference);
+                               $credit_note->document_date, $credit_note->reference, TR_OUTPUT);
 
                        $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $credit_date, $taxitem['sales_gl_code'], 0, 0,
                                $taxitem['Value'], $credit_note->customer_id,
index 290a3f2dffda99856f2627f5899207b20ed1c12b..eec60c8df166ecbc7fb46cdc3dbd9018263d8116 100644 (file)
@@ -157,7 +157,7 @@ function write_sales_delivery(&$delivery,$bo_policy)
                                $ex_rate = get_exchange_rate_from_home_currency(get_customer_currency($delivery->customer_id), $delivery->document_date);
                    add_trans_tax_details(ST_CUSTDELIVERY, $delivery_no, $taxitem['tax_type_id'],
                                $taxitem['rate'], $delivery->tax_included, $taxitem['Value'],
-                               $taxitem['Net'], $ex_rate, $delivery->document_date, $delivery->reference );
+                               $taxitem['Net'], $ex_rate, $delivery->document_date, $delivery->reference, null);
                }
        }
 
index d2b97cf2fbc29750ed9dc31088ad020095ce739b..c944c6bf656ed47976380ea1bc199d7f47cd0534 100644 (file)
@@ -169,8 +169,8 @@ function write_sales_invoice(&$invoice)
                if ($taxitem['Net'] != 0) {
                        $ex_rate = get_exchange_rate_from_home_currency(get_customer_currency($invoice->customer_id), $date_);
                        add_trans_tax_details(ST_SALESINVOICE, $invoice_no, $taxitem['tax_type_id'],
-                               $taxitem['rate'], $invoice->tax_included, $taxitem['Value'],
-                                $taxitem['Net'], $ex_rate, $date_, $invoice->reference, $date_, $prepaid_factor*$taxitem['Value']);
+                               $taxitem['rate'], $invoice->tax_included, $prepaid_factor*$taxitem['Value'],
+                                $taxitem['Net'], $ex_rate, $date_, $invoice->reference, TR_OUTPUT);
                        if (isset($taxitem['sales_gl_code']))
                                $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $taxitem['sales_gl_code'], 0, 0,
                                        (-$taxitem['Value'])*$prepaid_factor, $invoice->customer_id,
index eedb056622033b40b2fbe8e8cb0c5ed20183ddc3..3ae9e0ec53311aff3f57f89d45a257cc6ff29710 100644 (file)
@@ -200,3 +200,13 @@ ALTER TABLE `0_debtor_trans` ADD  PRIMARY KEY (`type`,`trans_no`, `debtor_no`);
 ALTER TABLE `0_supp_trans` DROP PRIMARY KEY;
 ALTER TABLE `0_supp_trans` ADD  PRIMARY KEY (`type`,`trans_no`, `supplier_id`);
 
+ALTER TABLE  `0_trans_tax_details` ADD COLUMN `reg_type` tinyint(1) DEFAULT NULL AFTER `memo`;
+
+UPDATE `0_trans_tax_details` reg
+       SET reg.reg_type=1
+       WHERE reg.trans_type IN(20, 21);
+
+UPDATE `0_trans_tax_details` reg
+       SET reg.reg_type=0
+       WHERE reg.trans_type IN(10, 11);
+
index f286b143623b884bc8c60ee801dd5a021816a3d4..781d50379db3e00c5c8f0c01f1e742bd1a281e19 100644 (file)
@@ -2284,6 +2284,7 @@ CREATE TABLE IF NOT EXISTS `0_trans_tax_details` (
   `net_amount` double NOT NULL default '0',
   `amount` double NOT NULL default '0',
   `memo` tinytext,
+  `reg_type` tinyint(1) default NULL,
   PRIMARY KEY  (`id`),
   KEY `Type_and_Number` (`trans_type`,`trans_no`),
   KEY `tran_date` (`tran_date`)
@@ -2293,17 +2294,17 @@ CREATE TABLE IF NOT EXISTS `0_trans_tax_details` (
 -- Dumping data for table `0_trans_tax_details`
 --
 
-INSERT INTO `0_trans_tax_details` VALUES(1, 20, 7, '2014-06-21', 1, 5, 1, 0, 3300, 165, '5t');
-INSERT INTO `0_trans_tax_details` VALUES(2, 13, 3, '2014-06-21', 1, 5, 1, 0, 50, 2.5, 'auto');
-INSERT INTO `0_trans_tax_details` VALUES(3, 10, 17, '2014-06-21', 1, 5, 1, 0, 50, 2.5, '1');
-INSERT INTO `0_trans_tax_details` VALUES(4, 13, 4, '2014-06-21', 1, 5, 1.3932, 0, 35.89, 1.7945, 'auto');
-INSERT INTO `0_trans_tax_details` VALUES(5, 10, 18, '2014-06-21', 1, 5, 1.3932, 0, 35.89, 1.7945, '2');
-INSERT INTO `0_trans_tax_details` VALUES(6, 2, 5, '2014-06-21', 1, 5, 1, 0, 95.2, 4.76, '');
-INSERT INTO `0_trans_tax_details` VALUES(7, 1, 8, '2014-06-21', 1, 5, 1, 0, -47.6, -2.38, '');
-INSERT INTO `0_trans_tax_details` VALUES(8, 20, 8, '2014-06-21', 1, 5, 1, 0, -19, -0.95, 'cc');
-INSERT INTO `0_trans_tax_details` VALUES(9, 13, 5, '2014-06-21', 1, 5, 1, 1, 47.619047619048, 2.3809523809524, 'auto');
-INSERT INTO `0_trans_tax_details` VALUES(10, 10, 19, '2014-06-21', 1, 5, 1, 1, 47.619047619048, 2.3809523809524, '3');
-INSERT INTO `0_trans_tax_details` VALUES(11, 11, 3, '2014-06-21', 1, 5, 1.3932, 0, 35.89, 1.7945, '1');
+INSERT INTO `0_trans_tax_details` VALUES(1, 20, 7, '2014-06-21', 1, 5, 1, 0, 3300, 165, '5t', 1);
+INSERT INTO `0_trans_tax_details` VALUES(2, 13, 3, '2014-06-21', 1, 5, 1, 0, 50, 2.5, 'auto', NULL);
+INSERT INTO `0_trans_tax_details` VALUES(3, 10, 17, '2014-06-21', 1, 5, 1, 0, 50, 2.5, '1', 0);
+INSERT INTO `0_trans_tax_details` VALUES(4, 13, 4, '2014-06-21', 1, 5, 1.3932, 0, 35.89, 1.7945, 'auto', NULL);
+INSERT INTO `0_trans_tax_details` VALUES(5, 10, 18, '2014-06-21', 1, 5, 1.3932, 0, 35.89, 1.7945, '2', 0);
+INSERT INTO `0_trans_tax_details` VALUES(6, 2, 5, '2014-06-21', 1, 5, 1, 0, 95.2, 4.76, '', NULL);
+INSERT INTO `0_trans_tax_details` VALUES(7, 1, 8, '2014-06-21', 1, 5, 1, 0, -47.6, -2.38, '', NULL);
+INSERT INTO `0_trans_tax_details` VALUES(8, 20, 8, '2014-06-21', 1, 5, 1, 0, -19, -0.95, 'cc', 1);
+INSERT INTO `0_trans_tax_details` VALUES(9, 13, 5, '2014-06-21', 1, 5, 1, 1, 47.619047619048, 2.3809523809524, 'auto', NULL);
+INSERT INTO `0_trans_tax_details` VALUES(10, 10, 19, '2014-06-21', 1, 5, 1, 1, 47.619047619048, 2.3809523809524, '3', 0);
+INSERT INTO `0_trans_tax_details` VALUES(11, 11, 3, '2014-06-21', 1, 5, 1.3932, 0, 35.89, 1.7945, '1', 0);
 
 -- --------------------------------------------------------
 
index b4191c003b1b14eebe096035f30304471889d508..69cb4c3d28af5bdf57553b2933b74645362e6171 100644 (file)
@@ -2044,6 +2044,7 @@ CREATE TABLE IF NOT EXISTS `0_trans_tax_details` (
   `net_amount` double NOT NULL default '0',
   `amount` double NOT NULL default '0',
   `memo` tinytext,
+  `reg_type` tinyint(1) default NULL,
   PRIMARY KEY  (`id`),
   KEY `Type_and_Number` (`trans_type`,`trans_no`),
   KEY `tran_date` (`tran_date`)