Changed tax grup retrieval functions to return all tax types (with filtered tax rates...
authorJanusz Dobrowolski <janusz@frontaccouting.eu>
Sat, 9 Apr 2011 09:59:40 +0000 (11:59 +0200)
committerJanusz Dobrowolski <janusz@frontaccouting.eu>
Sat, 9 Apr 2011 09:59:40 +0000 (11:59 +0200)
gl/includes/db/gl_db_trans.inc
includes/ui/ui_view.inc
taxes/db/tax_groups_db.inc
taxes/tax_groups.php

index 863d1b44b84af03d38a18926a8240f5919e72aab..80663e0bcbf42afdc47ec22add83f73579613638 100644 (file)
@@ -390,7 +390,9 @@ function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included
 function get_trans_tax_details($trans_type, $trans_no)
 {
        $sql = "SELECT ".TB_PREF."trans_tax_details.*, "
-               .TB_PREF."tax_types.name AS tax_type_name
+               .TB_PREF."tax_types.name AS tax_type_name, "
+               .TB_PREF."trans_tax_details.rate AS effective_rate, "
+               .TB_PREF."tax_types.rate AS rate
                FROM ".TB_PREF."trans_tax_details,".TB_PREF."tax_types
                WHERE trans_type = ".db_escape($trans_type)."
                AND trans_no = ".db_escape($trans_no)."
index aaf634dd6e9de0e2b8a45c1dec2434afd44d4035..a469854c1dbb2c9e6644e456a25a947a0daecff3 100644 (file)
@@ -441,12 +441,12 @@ function display_edit_tax_items($taxes, $columns, $tax_included, $leftspan=0)
     {
        if ($tax_included)
        {
-               label_row(_("Included") . " " . $taxitem['tax_type_name'] . " (" . $taxitem['rate'] . "%) "
+               label_row(_("Included") . " " . $taxitem['tax_type_name']
                        . " " . number_format2($taxitem['Value'],user_price_dec()), "", "colspan=$columns align=right", "align=right",$leftspan);
        }
        else
        {
-               label_row($taxitem['tax_type_name'] . " (" . $taxitem['rate'] . "%)",
+               label_row($taxitem['tax_type_name'],
                        number_format2($taxitem['Value'],user_price_dec()), "colspan=$columns align=right", "align=right",$leftspan);
                $total +=  round2($taxitem['Value'], user_price_dec());
        }
@@ -633,7 +633,7 @@ function display_quick_entries(&$cart, $id, $base, $type, $descr='')
                                        {
                                                $taxgroup = $cart->tax_group_id;
                                                $rates = 0;
-                                               $res = get_tax_group_items($cart->tax_group_id);
+                                               $res = get_tax_group_rates($cart->tax_group_id);
                                                while ($row = db_fetch($res))
                                                        $rates += $row['rate'];
                                                if ($rates == 0)
index 5c217aa275bb0457c67a8285b57bf0ff4f509fca..826ad8a05865b3b6dc482b86d017b764c3fbb678 100644 (file)
@@ -94,67 +94,58 @@ function delete_tax_group_items($id)
        db_query($sql, "could not delete item tax group items");                                        
 }
 
-function get_tax_group_items($id)
+//
+//     Return all tax types with rate value updated according to tax group selected
+//     Call the function without arg to find shipment group taxes.
+//
+function get_tax_group_rates($group_id=null)
 {
-       $sql = "SELECT ".TB_PREF."tax_group_items.*, ".TB_PREF."tax_types.name AS tax_type_name, ".TB_PREF."tax_types.rate, 
-               ".TB_PREF."tax_types.sales_gl_code, ".TB_PREF."tax_types.purchasing_gl_code  
-               FROM ".TB_PREF."tax_group_items, ".TB_PREF."tax_types 
-               WHERE tax_group_id=".db_escape($id)."
-                       AND ".TB_PREF."tax_types.id=tax_type_id";
-       
-       return db_query($sql, "could not get item tax type group items");
+       global $suppress_tax_rates;
+
+       $sql = 
+       "SELECT t.id as tax_type_id,"
+               .(isset($suppress_tax_rates) && $suppress_tax_rates == 1
+                       ? "t.name as tax_type_name,"
+                       : "CONCAT(t.name, ' (', t.rate, '%)') as tax_type_name,")
+               ."t.sales_gl_code,
+                 t.purchasing_gl_code,
+                 IF(g.tax_type_id, t.rate, NULL) as rate
+               FROM ".TB_PREF."tax_types t 
+                 LEFT JOIN ".TB_PREF."tax_group_items g ON t.id=g.tax_type_id
+                       AND g.tax_group_id=". ($group_id ? db_escape($group_id) : "(SELECT id FROM ".TB_PREF."tax_groups WHERE tax_shipping=1)")
+               ." WHERE !t.inactive";
+
+       return db_query($sql, "cannot get tax types as array");
 }
 
 function get_tax_group_items_as_array($id)
 {
-       $ret_tax_array = array();
-       
-       $tax_group_items = get_tax_group_items($id);
-       
-       while ($tax_group_item = db_fetch($tax_group_items)) 
+       $active_taxes = $ret_tax_array = array();
+
+       $tax_group_items = get_tax_group_rates($id);
+
+       while ($tax_group_item = db_fetch_assoc($tax_group_items)) 
        {
-               $index = $tax_group_item['tax_type_id'];
-               $ret_tax_array[$index]['tax_type_id'] = $tax_group_item['tax_type_id'];
-               $ret_tax_array[$index]['tax_type_name'] = $tax_group_item['tax_type_name'];
-               $ret_tax_array[$index]['sales_gl_code'] = $tax_group_item['sales_gl_code'];
-               $ret_tax_array[$index]['purchasing_gl_code'] = $tax_group_item['purchasing_gl_code'];
-               $ret_tax_array[$index]['rate'] = $tax_group_item['rate'];
-               $ret_tax_array[$index]['Value'] = 0;
+               $tax_group_item['Value'] = 0;
+               $ret_tax_array[$tax_group_item['tax_type_id']] = $tax_group_item;
        }
-       
+
        return $ret_tax_array;
 }
 
-function get_shipping_tax_group_items()
-{
-
-       $sql = "SELECT ".TB_PREF."tax_group_items.*, ".TB_PREF."tax_types.name AS tax_type_name, ".TB_PREF."tax_types.rate,
-               ".TB_PREF."tax_types.sales_gl_code, ".TB_PREF."tax_types.purchasing_gl_code  
-               FROM " .TB_PREF."tax_group_items, ".TB_PREF."tax_types, ".TB_PREF."tax_groups
-               WHERE " .TB_PREF."tax_groups.tax_shipping=1
-               AND " .TB_PREF."tax_groups.id=tax_group_id
-               AND ".TB_PREF."tax_types.id=tax_type_id";
-       return db_query($sql, "could not get shipping tax group items");
-}
 
 function get_shipping_tax_as_array()
 {
-       $ret_tax_array = array();
+       $active_taxes = $ret_tax_array = array();
 
-       
-       $tax_group_items = get_shipping_tax_group_items();
+       $tax_group_items = get_tax_group_rates();
 
-       while ($tax_group_item = db_fetch($tax_group_items)) 
+       while ($tax_group_item = db_fetch_assoc($tax_group_items)) 
        {
-               $index = $tax_group_item['tax_type_id'];
-               $ret_tax_array[$index]['tax_type_id'] = $tax_group_item['tax_type_id'];
-               $ret_tax_array[$index]['tax_type_name'] = $tax_group_item['tax_type_name'];
-               $ret_tax_array[$index]['sales_gl_code'] = $tax_group_item['sales_gl_code'];
-               $ret_tax_array[$index]['purchasing_gl_code'] = $tax_group_item['purchasing_gl_code'];
-               $ret_tax_array[$index]['rate'] = $tax_group_item['rate'];
-               $ret_tax_array[$index]['Value'] = 0;
+               $tax_group_item['Value'] = 0;
+               $ret_tax_array[$tax_group_item['tax_type_id']] = $tax_group_item;
        }
-       
+
        return $ret_tax_array;
 }
-?>
\ No newline at end of file
+
index bf888159043376a45cfdcb14c72bec0f5d09707f..cac3520b090dc3dff03c2f87afcb0c019597faa7 100644 (file)
@@ -40,24 +40,7 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
                display_error(_("The tax group name cannot be empty."));
                set_focus('name');
        } 
-       /* Editable rate has been removed 090920 Joe Hunt
-       else 
-       {
-               // make sure any entered rates are valid
-       for ($i = 0; $i < 5; $i++) 
-       {
-               if (isset($_POST['tax_type_id' . $i]) && 
-                       $_POST['tax_type_id' . $i] != ALL_NUMERIC       && 
-                       !check_num('rate' . $i, 0))
-               {
-                       display_error( _("An entered tax rate is invalid or less than zero."));
-                       $input_error = 1;
-                       set_focus('rate');
-                       break;
-               }
-       }
-       }
-       */
+
        if ($input_error != 1) 
        {
 
@@ -65,18 +48,12 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
        $taxes = array();
        $rates = array();
 
-       for ($i = 0; $i < 5; $i++) 
-       {
-               if (isset($_POST['tax_type_id' . $i]) &&
-                               $_POST['tax_type_id' . $i] != ANY_NUMERIC) 
-                       {
-                       $taxes[] = $_POST['tax_type_id' . $i];
-                               $rates[] = get_tax_type_default_rate($_POST['tax_type_id' . $i]);
-                               //Editable rate has been removed 090920 Joe Hunt
-                       //$rates[] = input_num('rate' . $i);
-               }
-       }
-
+               while (($id = find_submit('tax_type_id'))!=-1)
+               {
+                       $taxes[] = $id;
+                       $rates[] = get_tax_type_default_rate($id);
+                       unset($_POST['tax_type_id' . $id]);
+               }
        if ($selected_id != -1) 
        {
                        update_tax_group($selected_id, $_POST['name'], $_POST['tax_shipping'], $taxes, 
@@ -134,7 +111,8 @@ if ($Mode == 'RESET')
        $selected_id = -1;
        $sav = get_post('show_inactive');
        unset($_POST);
-       $_POST['show_inactive'] = $sav;
+       if($sav)
+               $_POST['show_inactive'] = $sav;
 }
 //-----------------------------------------------------------------------------------
 
@@ -160,10 +138,6 @@ while ($myrow = db_fetch($result))
        else
                label_cell(_("No"));
 
-       /*for ($i=0; $i< 5; $i++)
-               if ($myrow["type" . $i] != ALL_NUMERIC)
-                       echo "<td>" . $myrow["type" . $i] . "</td>";*/
-
        inactive_control_cell($myrow["id"], $myrow["inactive"], 'tax_groups', 'id');
        edit_button_cell("Edit".$myrow["id"], _("Edit"));
        delete_button_cell("Delete".$myrow["id"], _("Delete"));
@@ -187,18 +161,7 @@ if ($selected_id != -1)
        $_POST['name']  = $group["name"];
        $_POST['tax_shipping'] = $group["tax_shipping"];
 
-       $items = get_tax_group_items($selected_id);
-
-       $i = 0;
-       while ($tax_item = db_fetch($items)) 
-       {
-               $_POST['tax_type_id' . $i]  = $tax_item["tax_type_id"];
-               $_POST['rate' . $i]  = percent_format($tax_item["rate"]);
-               $i ++;
-       }
-       while($i<5) unset($_POST['tax_type_id'.$i++]);
        }
-
        hidden('selected_id', $selected_id);
 }
 text_row_ex(_("Description:"), 'name', 40);
@@ -208,30 +171,18 @@ end_table();
 
 display_note(_("Select the taxes that are included in this group."), 1, 1);
 
+// null means transport tax group, but for new we do not use real rates
+$items = get_tax_group_rates($selected_id!=-1 ? $selected_id : null);
+
+$th = array(_("Tax"), "");
+
 start_table(TABLESTYLE2);
-//$th = array(_("Tax"), _("Default Rate (%)"), _("Rate (%)"));
-//Editable rate has been removed 090920 Joe Hunt
-$th = array(_("Tax"), _("Rate (%)"));
 table_header($th);
-for ($i = 0; $i < 5; $i++) 
-{
-       start_row();
-       if (!isset($_POST['tax_type_id' . $i]))
-               $_POST['tax_type_id' . $i] = 0;
-       tax_types_list_cells(null, 'tax_type_id' . $i, $_POST['tax_type_id' . $i], _("None"), true);
 
-       if ($_POST['tax_type_id' . $i] != 0 && $_POST['tax_type_id' . $i] != ALL_NUMERIC) 
-       {
-               $default_rate = get_tax_type_default_rate($_POST['tax_type_id' . $i]);
-               label_cell(percent_format($default_rate), "nowrap align=right");
-
-               //Editable rate has been removed 090920 Joe Hunt
-               //if (!isset($_POST['rate' . $i]) || $_POST['rate' . $i] == "")
-               //      $_POST['rate' . $i] = percent_format($default_rate);
-               //small_amount_cells(null, 'rate' . $i, $_POST['rate' . $i], null, null, 
-               //  user_percent_dec()); 
-       }
-       end_row();
+while($item = db_fetch_assoc($items)) 
+{
+       check_row($item['tax_type_name'], 'tax_type_id' . $item['tax_type_id'], 
+               $selected_id!=-1 && isset($item['rate']), "align='center'");
 }
 
 end_table(1);