Rerun of new tax group and shipping files
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Wed, 13 Jul 2011 08:52:33 +0000 (10:52 +0200)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Wed, 13 Jul 2011 08:52:33 +0000 (10:52 +0200)
includes/ui/ui_view.inc
taxes/db/tax_groups_db.inc
taxes/tax_calc.inc
taxes/tax_groups.php

index 8f684453c9a3d832381d89ec1f14d341fa7029cc..eb7e7713c272cd641b2cd5da081e0ad26d76a63b 100644 (file)
@@ -445,16 +445,18 @@ function display_edit_tax_items($taxes, $columns, $tax_included, $leftspan=0)
 
     foreach ($taxes as $taxitem)
     {
-       if ($tax_included)
-       {
-               label_row(_("Included") . " " . $taxitem['tax_type_name'] . " (" . $taxitem['rate'] . "%) "
-                       . " " . number_format2($taxitem['Value'],user_price_dec()), "", "colspan=$columns align=right", "align=right",$leftspan);
-       }
-       else
-       {
-               label_row($taxitem['tax_type_name'] . " (" . $taxitem['rate'] . "%)",
-                       number_format2($taxitem['Value'],user_price_dec()), "colspan=$columns align=right", "align=right",$leftspan);
-               $total +=  round2($taxitem['Value'], user_price_dec());
+       if ($taxitem['Value'] != 0) {
+               if ($tax_included)
+               {
+                       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'],
+                               number_format2($taxitem['Value'],user_price_dec()), "colspan=$columns align=right", "align=right",$leftspan);
+                       $total +=  round2($taxitem['Value'], user_price_dec());
+               }
        }
     }
 
index 05e36f9235de5b367a16f8763d63e50f00790c91..cb411d54a7d93e12733328d2a40cd6ecc1372922 100644 (file)
@@ -83,65 +83,81 @@ 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, $tax_shipping=false)
 {
-       $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 ";
+       if (!$group_id)
+               $sql .= "DISTINCT ";
+       $sql .= "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,
+                 t.rate,
+                 g.tax_shipping,
+                 g.tax_group_id
+               FROM ".TB_PREF."tax_types t, ".TB_PREF."tax_group_items g 
+                 WHERE t.id=g.tax_type_id";
+       if ($group_id)
+               $sql .= " AND g.tax_group_id=".db_escape($group_id);
+                       
+       $sql .= " AND t.inactive=0";
+       if ($tax_shipping)
+               $sql .= " AND g.tax_shipping=1";
+*/
+       $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,
+                 g.tax_shipping
+               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 MIN(id) FROM ".TB_PREF."tax_groups)")                  
+       . " WHERE !t.inactive";
+       if ($tax_shipping)
+               $sql .= " AND g.tax_shipping=1";
+
+       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);
+       $tax_group_items = get_tax_group_rates($id);
        
        while ($tax_group_item = db_fetch($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($tax_group)
-{
-
-       $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 " .TB_PREF."tax_group_items.tax_shipping=1
-               AND tax_group_id=$tax_group
-               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($tax_group)
+function get_shipping_tax_as_array($id)
 {
        $ret_tax_array = array();
 
-       
-       $tax_group_items = get_shipping_tax_group_items($tax_group);
+       $tax_group_items = get_tax_group_rates($id, true);
 
        while ($tax_group_item = db_fetch($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;
index f9906e0fca11f24b623d50ca25a75ab3785e9aa2..dbb7999e46f0548cf576ac9380755533abf13f37 100644 (file)
@@ -41,16 +41,16 @@ function get_tax_free_price_for_item($stock_id, $price, $tax_group, $tax_include
        if ($tax_array == null)
                return $price;
        
-       $tax_multiplier = 0;    
+       $tax_multiplier = 0;
 
-       // loop for all items
+       // loop for all taxes
 
        foreach ($tax_array as $taxitem) 
        {
-                       $tax_multiplier += $taxitem["rate"];
+               $tax_multiplier += $taxitem["rate"];
        }
-       
-       return round($price / (1 + ($tax_multiplier / 100)),  2*user_price_dec());
+
+       return round($price / (1 + ($tax_multiplier / 100)),  user_price_dec());
 }
 //
 //     Full price (incl. VAT) for item $stock_id with line price $price,
@@ -86,7 +86,7 @@ function get_full_price_for_item($stock_id, $price, $tax_group, $tax_included, $
                        $tax_multiplier += $taxitem["rate"];
        }
        
-       return round($price * (1 + ($tax_multiplier / 100)),  2*user_price_dec());
+       return round($price * (1 + ($tax_multiplier / 100)),  user_price_dec());
 }
 
 //---------------------------------------------------------------------------------
@@ -150,10 +150,10 @@ function get_tax_for_items($items, $prices, $shipping_cost, $tax_group, $tax_inc
        else
          $ret_tax_array = get_tax_group_items_as_array($tax_group);
 
-       $dec = user_price_dec();
        foreach($ret_tax_array as $k=>$t)
                $ret_tax_array[$k]['Net'] = 0;
 
+       $dec = user_price_dec();
        // loop for all items
        for ($i = 0; $i < count($items); $i++)
        {
@@ -163,14 +163,16 @@ function get_tax_for_items($items, $prices, $shipping_cost, $tax_group, $tax_inc
                {
                        foreach ($item_taxes as $item_tax) 
                        {
-                               $index = $item_tax['tax_type_id'];
-                               if($tax_included==1) {// 2008-11-26 Joe Hunt Taxes are stored without roundings
-                                 $nprice = get_tax_free_price_for_item($items[$i], $prices[$i], $tax_group, $tax_included);
-                                 $ret_tax_array[$index]['Value'] += round2($nprice * $item_tax['rate'] / 100, $dec);
-                                 $ret_tax_array[$index]['Net'] += $nprice;
-                               } else {
-                                 $ret_tax_array[$index]['Value'] += round2($prices[$i] * $item_tax['rate'] / 100, $dec);
-                                 $ret_tax_array[$index]['Net'] += $prices[$i];
+                               if ($item_tax['rate'] !== null) {
+                                       $index = $item_tax['tax_type_id'];
+                                       if($tax_included==1) {// 2008-11-26 Joe Hunt Taxes are stored without roundings
+                                         $nprice = get_tax_free_price_for_item($items[$i], $prices[$i], $tax_group, $tax_included, null, $item_tax['tax_type_id']);
+                                         $ret_tax_array[$index]['Value'] += $nprice*$item_tax['rate']/100;
+                                         $ret_tax_array[$index]['Net'] += $nprice;
+                                       } else {
+                                         $ret_tax_array[$index]['Value'] += ($prices[$i] * $item_tax['rate'] / 100);
+                                         $ret_tax_array[$index]['Net'] += $prices[$i];
+                                       }
                                }
                        }
                }
@@ -197,14 +199,14 @@ function get_tax_for_items($items, $prices, $shipping_cost, $tax_group, $tax_inc
                        foreach ($item_taxes as $item_tax) 
                        {
                                $index = $item_tax['tax_type_id'];
-                               if(isset($ret_tax_array[$index])) {
-                                 if($tax_included==1) {// 2008-11-26 Joe Hunt Taxes are stored without roundings
-                                       $ret_tax_array[$index]['Value'] += round2($shipping_net * $item_tax['rate'] / 100, $dec);
-                                   $ret_tax_array[$index]['Net'] += $shipping_net;
-                                 } else {
-                                       $ret_tax_array[$index]['Value'] += round2($shipping_cost * $item_tax['rate'] / 100, $dec);
-                                   $ret_tax_array[$index]['Net'] += $shipping_cost;
-                                 }
+                               if ($item_tax['rate'] !== null && $ret_tax_array[$index]['rate'] !== null) {
+                                         if($tax_included==1) {// 2008-11-26 Joe Hunt Taxes are stored without roundings
+                                               $ret_tax_array[$index]['Value'] += ($shipping_net * $item_tax['rate'] / 100);
+                                           $ret_tax_array[$index]['Net'] += $shipping_net;
+                                         } else {
+                                               $ret_tax_array[$index]['Value'] += ($shipping_cost * $item_tax['rate'] / 100);
+                                           $ret_tax_array[$index]['Net'] += $shipping_cost;
+                                         }
                                }
                        }
                }
index 2f300e7ee71b2377a199391f8c57be2634ee4a03..317b433ad654fcb0f193e654d6d5730e3ba29bee 100644 (file)
@@ -40,24 +40,6 @@ 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) 
        {
 
@@ -66,19 +48,14 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
        $rates = array();
        $tax_shippings = 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]);
-                       $tax_shippings[] = check_value('tax_shipping' . $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);
+                       $tax_shippings[] = check_value('tax_shipping'.$id);
+                       unset($_POST['tax_type_id' . $id]);
+                       unset($_POST['tax_shipping' . $id]);
+               }
        if ($selected_id != -1) 
        {
                        update_tax_group($selected_id, $_POST['name'], $taxes, $rates, $tax_shippings);
@@ -135,7 +112,8 @@ if ($Mode == 'RESET')
        $selected_id = -1;
        $sav = get_post('show_inactive');
        unset($_POST);
-       $_POST['show_inactive'] = $sav;
+       if ($sav)
+               $_POST['show_inactive'] = $sav;
 }
 //-----------------------------------------------------------------------------------
 
@@ -157,10 +135,6 @@ while ($myrow = db_fetch($result))
 
        label_cell($myrow["name"]);
 
-       /*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"));
@@ -183,20 +157,9 @@ if ($selected_id != -1)
 
        $_POST['name']  = $group["name"];
 
-       $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"]);
-               $_POST['tax_shipping' . $i]  = $tax_item["tax_shipping"];
-               $i ++;
-       }
-       while($i<5) unset($_POST['tax_type_id'.$i++]);
        }
-
        hidden('selected_id', $selected_id);
+
 }
 text_row_ex(_("Description:"), 'name', 40);
 
@@ -204,33 +167,32 @@ end_table();
 
 display_note(_("Select the taxes that are included in this group."), 1, 1);
 
+$items = get_tax_group_rates($selected_id!=-1 ? $selected_id : null);
+
 start_table(TABLESTYLE2);
-//$th = array(_("Tax"), _("Default Rate (%)"), _("Rate (%)"));
-//Editable rate has been removed 090920 Joe Hunt
-$th = array(_("Tax"), _("Rate (%)"), _("Shipping Tax"));
+$th = array(_("Tax"), "", _("Shipping Tax"));
 table_header($th);
-for ($i = 0; $i < 5; $i++) 
+
+while($item = db_fetch($items)) 
 {
        start_row();
-       if (!isset($_POST['tax_type_id' . $i]))
-               $_POST['tax_type_id' . $i] = 0;
-       if (!isset($_POST['tax_shipping' . $i]))
-               $_POST['tax_shipping' . $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) 
+       check_cells($item['tax_type_name'], 'tax_type_id' . $item['tax_type_id'], 
+               $selected_id!=-1 && isset($item['rate']), true, false, "align='center'");
+       /*      
+       $upd = false;   
+       if (get_post('_tax_type_id' . $item['tax_type_id'].'_update'))  
        {
-               $default_rate = get_tax_type_default_rate($_POST['tax_type_id' . $i]);
-               label_cell(percent_format($default_rate), "nowrap align=right");
-               
-               check_cells(null, 'tax_shipping' . $i);
-               //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()); 
+               $_POST['_tax_type_id' . $item['tax_type_id'].'_update'] = 0;
+               $Ajax->activate('_page_body');
+               $upd = true;
        }
-       end_row();
+       
+       if (($selected_id!=-1 && isset($item['rate']) || $upd))
+       */
+       if ($selected_id!=-1 && isset($item['rate']))
+               check_cells(null, 'tax_shipping' . $item['tax_type_id'], $item['tax_shipping']);
+       end_row();      
+       
 }
 
 end_table(1);