From 92a2be3ef35115bed311cfebf1ea34bbe95b91a3 Mon Sep 17 00:00:00 2001 From: Juanjo Menent Date: Thu, 17 Jun 2010 17:12:34 +0000 Subject: [PATCH] =?UTF-8?q?Local=20Taxes:=20Works=20in=20Propals.=20=C2=A1?= =?UTF-8?q?NOT=20FINISHED!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htdocs/comm/propal.php | 8 +++++ htdocs/comm/propal/class/propal.class.php | 40 +++++++++++++++++++---- htdocs/core/class/commonobject.class.php | 19 +++++++++-- htdocs/lib/functions.lib.php | 15 ++++++--- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/htdocs/comm/propal.php b/htdocs/comm/propal.php index 0d137b70ea8..a48ecadb518 100644 --- a/htdocs/comm/propal.php +++ b/htdocs/comm/propal.php @@ -712,6 +712,8 @@ if ($_POST['action'] == "addline" && $user->rights->propale->creer) $tva_npr=preg_match('/\*/',$_POST['np_tva_tx'])?1:0; $desc=$_POST['dp_desc']; $type=$_POST["type"]; + $localtax1_tx=get_localtax($tva_tx,1,$propal->client); + $localtax2_tx=get_localtax($tva_tx,2,$propal->client); } $info_bits=0; @@ -730,6 +732,8 @@ if ($_POST['action'] == "addline" && $user->rights->propale->creer) $pu_ht, $_POST['qty'], $tva_tx, + $localtax1_tx, + $localtax2_tx, $_POST['idprod'], $_POST['remise_percent'], $price_base_type, @@ -786,6 +790,8 @@ if ($_POST['action'] == 'updateligne' && $user->rights->propale->creer && $_POST // Define vat_rate $vat_rate=$_POST['tva_tx']; $vat_rate=str_replace('*','',$vat_rate); + $localtax1_rate=get_localtax($vat_rate,1,$propal->client); + $localtax2_rate=get_localtax($vat_rate,2,$propal->client); // On verifie que le prix minimum est respecte $productid = $_POST['productid'] ; @@ -805,6 +811,8 @@ if ($_POST['action'] == 'updateligne' && $user->rights->propale->creer && $_POST $_POST['qty'], $_POST['remise_percent'], $vat_rate, + $localtax1_rate, + $localtax2_rate, $_POST['desc'], 'HT', $info_bits); diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index 84ebbb45e3c..587a4dbd094 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -71,6 +71,8 @@ class Propal extends CommonObject var $total_ht; // Total net of tax var $total_tva; // Total VAT + var $total_localtax1; // Total Local Taxes 1 + var $total_localtax2; // Total Local Taxes 2 var $total_ttc; // Total with tax var $price; // deprecated (for compatibility) var $tva; // deprecated (for compatibility) @@ -161,6 +163,10 @@ class Propal extends CommonObject $productdesc = $prod->description; $tva_tx = get_default_tva($mysoc,$this->client,$prod->tva_tx); + // local taxes + $localtax1_tx = get_default_localtax($mysoc,$this->client,1,$prod->tva_tx); + $localtax2_tx = get_default_localtax($mysoc,$this->client,2,$prod->tva_tx); + // multiprix if($conf->global->PRODUIT_MULTIPRICES && $this->client->price_level) { @@ -278,7 +284,7 @@ class Propal extends CommonObject * par l'appelant par la methode get_default_tva(societe_vendeuse,societe_acheteuse,taux_produit) * et le desc doit deja avoir la bonne valeur (a l'appelant de gerer le multilangue) */ - function addline($propalid, $desc, $pu_ht, $qty, $txtva, $fk_product=0, $remise_percent=0, $price_base_type='HT', $pu_ttc=0, $info_bits=0, $type=0) + function addline($propalid, $desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $price_base_type='HT', $pu_ttc=0, $info_bits=0, $type=0) { global $conf; @@ -296,7 +302,8 @@ class Propal extends CommonObject $pu_ht=price2num($pu_ht); $pu_ttc=price2num($pu_ttc); $txtva=price2num($txtva); - + $txlocaltax1=price2num($txlocaltax1); + $txlocaltax2=price2num($txlocaltax2); if ($price_base_type=='HT') { $pu=$pu_ht; @@ -310,10 +317,12 @@ class Propal extends CommonObject // qty, pu, remise_percent et txtva // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva. - $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, 0, 0, 0, $price_base_type, $info_bits); + $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits); $total_ht = $tabprice[0]; $total_tva = $tabprice[1]; $total_ttc = $tabprice[2]; + $total_localtax1 = $tabprice[9]; + $total_localtax2 = $tabprice[10]; // TODO A virer // Anciens indicateurs: $price, $remise (a ne plus utiliser) @@ -332,6 +341,8 @@ class Propal extends CommonObject $ligne->desc=$desc; $ligne->qty=$qty; $ligne->tva_tx=$txtva; + $ligne->localtax1_tx=$txlocaltax1; + $ligne->localtax2_tx=$txlocaltax2; $ligne->fk_product=$fk_product; $ligne->remise_percent=$remise_percent; $ligne->subprice=$pu_ht; @@ -340,6 +351,8 @@ class Propal extends CommonObject $ligne->fk_remise_except=$fk_remise_except; $ligne->total_ht=$total_ht; $ligne->total_tva=$total_tva; + $ligne->total_localtax1=$total_localtax1; + $ligne->total_localtax2=$total_localtax2; $ligne->total_ttc=$total_ttc; $ligne->product_type=$type; @@ -391,7 +404,7 @@ class Propal extends CommonObject * \param info_bits Miscellanous informations * \return int 0 en cas de succes */ - function updateline($rowid, $pu, $qty, $remise_percent=0, $txtva, $desc='', $price_base_type='HT', $info_bits=0) + function updateline($rowid, $pu, $qty, $remise_percent=0, $txtva, $txlocaltax1=0, $txlocaltax2=0, $desc='', $price_base_type='HT', $info_bits=0) { global $conf,$user,$langs; @@ -418,15 +431,20 @@ class Propal extends CommonObject */ $pu = price2num($pu); $txtva = price2num($txtva); + $txlocaltax1=price2num($txlocaltax1); + $txlocaltax2=price2num($txlocaltax2); // Calcul du total TTC et de la TVA pour la ligne a partir de // qty, pu, remise_percent et txtva // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva. - $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, 0, 0, 0, $price_base_type, $info_bits); + $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits); $total_ht = $tabprice[0]; $total_tva = $tabprice[1]; $total_ttc = $tabprice[2]; + $total_localtax1 = $tabprice[9]; + $total_localtax2 = $tabprice[10]; + // Anciens indicateurs: $price, $remise (a ne plus utiliser) $price = $pu; @@ -442,9 +460,13 @@ class Propal extends CommonObject $sql.= " , remise_percent='".$remise_percent."'"; // \TODO A virer $sql.= " , subprice=".price2num($pu); $sql.= " , tva_tx=".price2num($txtva); + $sql.= " , localtax1_tx=".price2num($txlocaltax1); + $sql.= " , localtax2_tx=".price2num($txlocaltax2); $sql.= " , description='".addslashes($desc)."'"; $sql.= " , total_ht=".price2num($total_ht); $sql.= " , total_tva=".price2num($total_tva); + $sql.= " , total_localtax1=".price2num($total_localtax1); + $sql.= " , total_localtax2=".price2num($total_localtax2); $sql.= " , total_ttc=".price2num($total_ttc); $sql.= " , info_bits=".$info_bits; //if ($conf->global->PROPALE_USE_OPTION_LINE && !$qty) @@ -2269,10 +2291,10 @@ class PropaleLigne // Insertion dans base de la ligne $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'propaldet'; - $sql.= ' (fk_propal, description, fk_product, product_type, fk_remise_except, qty, tva_tx,'; + $sql.= ' (fk_propal, description, fk_product, product_type, fk_remise_except, qty, tva_tx, localtax1_tx, localtax2_tx,'; $sql.= ' subprice, remise_percent, '; $sql.= ' info_bits, '; - $sql.= ' total_ht, total_tva, total_ttc, marge_tx, marque_tx, special_code, rang)'; + $sql.= ' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, marge_tx, marque_tx, special_code, rang)'; $sql.= " VALUES (".$this->fk_propal.","; $sql.= " '".addslashes($this->desc)."',"; $sql.= " ".($this->fk_product?"'".$this->fk_product."'":"null").","; @@ -2280,11 +2302,15 @@ class PropaleLigne $sql.= " ".($this->fk_remise_except?"'".$this->fk_remise_except."'":"null").","; $sql.= " ".price2num($this->qty).","; $sql.= " ".price2num($this->tva_tx).","; + $sql.= " ".price2num($this->localtax1_tx).","; + $sql.= " ".price2num($this->localtax2_tx).","; $sql.= " ".($this->subprice?price2num($this->subprice):'null').","; $sql.= " ".price2num($this->remise_percent).","; $sql.= " '".$this->info_bits."',"; $sql.= " ".price2num($this->total_ht).","; $sql.= " ".price2num($this->total_tva).","; + $sql.= " ".price2num($this->total_localtax1).","; + $sql.= " ".price2num($this->total_localtax2).","; $sql.= " ".price2num($this->total_ttc).","; if (isset($this->marge_tx)) $sql.= ' '.$this->marge_tx.','; else $sql.= ' null,'; diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 9aaa62d57bd..c3b865fee82 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -1,6 +1,7 @@ * Copyright (C) 2005-2010 Regis Houssin + * Copyright (C) 2010 Juanjo Menent * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -895,7 +896,7 @@ class CommonObject $fieldtva='total_tva'; if ($this->element == 'facture_fourn') $fieldtva='tva'; - $sql = 'SELECT qty, total_ht, '.$fieldtva.' as total_tva, total_ttc'; + $sql = 'SELECT qty, total_ht, '.$fieldtva.' as total_tva, total_localtax1, total_localtax2, total_ttc'; $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line; $sql.= ' WHERE '.$this->fk_element.' = '.$this->id; @@ -905,6 +906,8 @@ class CommonObject { $this->total_ht = 0; $this->total_tva = 0; + $this->total_localtax1 = 0; + $this->total_localtax2 = 0; $this->total_ttc = 0; $num = $this->db->num_rows($resql); @@ -915,6 +918,8 @@ class CommonObject $this->total_ht += $obj->total_ht; $this->total_tva += $obj->total_tva; + $this->total_localtax1 += $obj->total_localtax1; + $this->total_localtax2 += $obj->total_localtax2; $this->total_ttc += $obj->total_ttc; $i++; @@ -928,12 +933,22 @@ class CommonObject if ($this->element == 'facturerec') $fieldht='total'; $fieldtva='tva'; if ($this->element == 'facture_fourn') $fieldtva='total_tva'; + $fieldlocaltax1='total_localtax1'; + $fieldlocaltax2='total_localtax2'; $fieldttc='total_ttc'; - if ($this->element == 'propal') $fieldttc='total'; + + if ($this->element == 'propal') + { + $fieldlocaltax1='localtax1'; + $fieldlocaltax2='localtax2'; + $fieldttc='total'; + } $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET'; $sql .= " ".$fieldht."='".price2num($this->total_ht)."',"; $sql .= " ".$fieldtva."='".price2num($this->total_tva)."',"; + $sql .= " ".$fieldlocaltax1."='".price2num($this->total_localtax1)."',"; + $sql .= " ".$fieldlocaltax2."='".price2num($this->total_localtax2)."',"; $sql .= " ".$fieldttc."='".price2num($this->total_ttc)."'"; $sql .= ' WHERE rowid = '.$this->id; diff --git a/htdocs/lib/functions.lib.php b/htdocs/lib/functions.lib.php index 727af15ae03..d32b8871a01 100644 --- a/htdocs/lib/functions.lib.php +++ b/htdocs/lib/functions.lib.php @@ -2484,19 +2484,24 @@ function price2num($amount,$rounding='',$alreadysqlnb=0) * \param local Local taxe to search and return * \return int <0 if KO, localtax if found */ -function get_localtax($tva, $local) +function get_localtax($tva, $local, $societe_acheteuse="") { global $db, $conf, $mysoc; - + if (is_object($societe_acheteuse)) + { + if ($local==1 && !$societe_acheteuse->localtax1_assuj) return 0; + elseif ($local==2 && !$societe_acheteuse->localtax2_assuj) return 0; + } + $code_pays=$mysoc->pays_code; - + // Search local taxes $sql = "SELECT t.localtax1, t.localtax2"; $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_pays as p"; $sql .= " WHERE t.fk_pays = p.rowid AND p.code = '".$code_pays."'"; $sql .= " AND t.taux =".$tva." AND t.active = 1"; $sql .= " ORDER BY t.localtax1 ASC, t.localtax2 ASC"; - + $resql=$db->query($sql); if ($resql) { @@ -2504,7 +2509,7 @@ function get_localtax($tva, $local) if ($local==1) return $obj->localtax1; elseif ($local==2) return $obj->localtax2; } - + return -1; }