X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=taxes%2Fdb%2Ftax_groups_db.inc;h=f39eccef51bbc1655a4d6532edebddff126330a1;hb=020d309dc37e9400d27ef3cad381e6f1b04f67fd;hp=05e36f9235de5b367a16f8763d63e50f00790c91;hpb=7732e889ec468099f6b66173b8dd6363e0a30ed8;p=fa-stable.git diff --git a/taxes/db/tax_groups_db.inc b/taxes/db/tax_groups_db.inc index 05e36f92..f39eccef 100644 --- a/taxes/db/tax_groups_db.inc +++ b/taxes/db/tax_groups_db.inc @@ -9,7 +9,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License here . ***********************************************************************/ -function add_tax_group($name, $taxes, $rates, $tax_shippings) +function add_tax_group($name, $taxes, $tax_shippings) { begin_transaction(); @@ -18,12 +18,12 @@ function add_tax_group($name, $taxes, $rates, $tax_shippings) $id = db_insert_id(); - add_tax_group_items($id, $taxes, $rates, $tax_shippings); + add_tax_group_items($id, $taxes, $tax_shippings); commit_transaction(); } -function update_tax_group($id, $name, $taxes, $rates, $tax_shippings) +function update_tax_group($id, $name, $taxes, $tax_shippings) { begin_transaction(); @@ -31,7 +31,7 @@ function update_tax_group($id, $name, $taxes, $rates, $tax_shippings) db_query($sql, "could not update tax group"); delete_tax_group_items($id); - add_tax_group_items($id, $taxes, $rates, $tax_shippings); + add_tax_group_items($id, $taxes, $tax_shippings); commit_transaction(); } @@ -66,12 +66,12 @@ function delete_tax_group($id) commit_transaction(); } -function add_tax_group_items($id, $items, $rates, $tax_shippings) +function add_tax_group_items($id, $items, $tax_shippings) { for ($i=0; $i < count($items); $i++) { - $sql = "INSERT INTO ".TB_PREF."tax_group_items (tax_group_id, tax_type_id, rate, tax_shipping) - VALUES (".db_escape($id).", ".db_escape($items[$i]).", " . $rates[$i] .", " . $tax_shippings[$i] .")"; + $sql = "INSERT INTO ".TB_PREF."tax_group_items (tax_group_id, tax_type_id, tax_shipping) + VALUES (".db_escape($id).", ".db_escape($items[$i]).", " . $tax_shippings[$i] .")"; db_query($sql, "could not add item tax group item"); } } @@ -83,67 +83,59 @@ 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 $SysPrefs; + + $sql = + "SELECT t.id as tax_type_id," + .($SysPrefs->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; } -?> \ No newline at end of file