From e8ec5f5ad73a35fe55361736910ba94e98a4c443 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Thu, 15 Mar 2012 16:52:18 +0100 Subject: [PATCH] New: add vat rate in supplier price New: add events system in jquery combobox --- htdocs/core/ajax/vatrates.php | 55 ++++++++++++++ htdocs/core/class/html.form.class.php | 12 ++-- htdocs/core/lib/ajax.lib.php | 36 ++++++++-- .../fourn/class/fournisseur.product.class.php | 72 ++++++++++--------- .../install/mysql/migration/3.1.0-3.2.0.sql | 10 +-- .../llx_product_fournisseur_price.key.sql | 7 +- .../tables/llx_product_fournisseur_price.sql | 28 ++++---- htdocs/product/fournisseurs.php | 38 ++++++---- htdocs/societe/soc.php | 2 +- 9 files changed, 180 insertions(+), 80 deletions(-) create mode 100644 htdocs/core/ajax/vatrates.php diff --git a/htdocs/core/ajax/vatrates.php b/htdocs/core/ajax/vatrates.php new file mode 100644 index 00000000000..d987b074bc6 --- /dev/null +++ b/htdocs/core/ajax/vatrates.php @@ -0,0 +1,55 @@ + + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file htdocs/core/ajax/vatrates.php + * \brief File to load vat rates combobox + */ + +if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal +if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); +//if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); +if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); +//if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); +//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); + +require('../../main.inc.php'); + +$id = GETPOST('id','int'); +$action = GETPOST('action','alpha'); +$htmlname = GETPOST('htmlname','alpha'); + +/* + * View + */ + +top_httphead(); + +//print ''."\n"; + +// Load original field value +if (! empty($id) && ! empty($action) && ! empty($htmlname)) +{ + $form = new Form($db); + $soc = new Societe($db); + + $soc->fetch($id); + + echo $form->load_tva('tva_tx','',$soc,$mysoc,0,0,'',true); +} + +?> diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index c0eeea480b6..5b9d2db15d8 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -640,9 +640,10 @@ class Form * @param int $showempty Add an empty field * @param int $showtype Show third party type in combolist (customer, prospect or supplier) * @param int $forcecombo Force to use combo box + @param array $event Event options * @return string HTML string with */ - function select_company($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0) + function select_company($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0, $event=array()) { global $conf,$user,$langs; @@ -665,7 +666,7 @@ class Form { //$minLength = (is_numeric($conf->global->COMPANY_USE_SEARCH_TO_SELECT)?$conf->global->COMPANY_USE_SEARCH_TO_SELECT:2); - $out.= ajax_combobox($htmlname); + $out.= ajax_combobox($htmlname, $event); } $out.= ''; + if (! $options_only) $return.= ''; + if (! $options_only) $return.= ''; } else { diff --git a/htdocs/core/lib/ajax.lib.php b/htdocs/core/lib/ajax.lib.php index 937c187d1e5..2d7426f279e 100644 --- a/htdocs/core/lib/ajax.lib.php +++ b/htdocs/core/lib/ajax.lib.php @@ -202,17 +202,41 @@ function ajax_dialog($title,$message,$w=350,$h=150) * Convert a select html field into an ajax combobox * * @param string $htmlname Name of html field + * @param array $event Event options * @return string Return html string to convert a select field into a combo */ -function ajax_combobox($htmlname) +function ajax_combobox($htmlname, $event=array()) { $msg.= ''; - - $msg.= "\n"; + $("#'.$htmlname.'").combobox({ + selected : function(event,ui) { + var obj = '.json_encode($event).'; + $.each(obj, function(key,values) { + if (values.method.length) { + getMethod(values); + } + }); + } + }); + + function getMethod(obj) { + var id = $("#'.$htmlname.'").val(); + var method = obj.method; + var url = obj.url; + var htmlname = obj.htmlname; + $.get(url, + { + action: method, + id: id, + htmlname: htmlname + }, + function(response) { + $("select#" + htmlname).html(response); + }); + } + });'; + $msg.= "\n"; return $msg; } diff --git a/htdocs/fourn/class/fournisseur.product.class.php b/htdocs/fourn/class/fournisseur.product.class.php index 3acdd83fb1e..69436f3b4ae 100644 --- a/htdocs/fourn/class/fournisseur.product.class.php +++ b/htdocs/fourn/class/fournisseur.product.class.php @@ -139,9 +139,10 @@ class ProductFournisseur extends Product * @param Societe $fourn Supplier * @param int $availability Product availability * @param string $ref_fourn Supplier ref + * @param float $tva_tx VAT rate * @return int >0 if KO, >0 if OK */ - function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn) + function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx) { global $conf,$mysoc; @@ -166,8 +167,9 @@ class ProductFournisseur extends Product { if ($price_base_type == 'TTC') { - $ttx = get_default_tva($fourn,$mysoc,$this->id); - $buyprice = $buyprice/(1+($ttx/100)); + //$ttx = get_default_tva($fourn,$mysoc,$this->id); + //$buyprice = $buyprice/(1+($ttx/100)); + $buyprice = $buyprice/(1+($tva_tx/100)); } $unitBuyPrice = price2num($buyprice/$qty,'MU'); @@ -175,7 +177,7 @@ class ProductFournisseur extends Product // Ajoute prix courant du fournisseur pour cette quantite $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price("; - $sql.= "datec, fk_product, fk_soc, ref_fourn, fk_user, price, quantity, unitprice, fk_availability, entity)"; + $sql.= "datec, fk_product, fk_soc, ref_fourn, fk_user, price, quantity, unitprice, tva_tx, fk_availability, entity)"; $sql.= " values('".$this->db->idate($now)."',"; $sql.= " ".$this->id.","; $sql.= " ".$fourn->id.","; @@ -184,6 +186,7 @@ class ProductFournisseur extends Product $sql.= " ".price2num($buyprice).","; $sql.= " ".$qty.","; $sql.= " ".$unitBuyPrice.","; + $sql.= " ".$tva_tx.","; $sql.= " ".$availability.","; $sql.= $conf->entity; $sql.=")"; @@ -279,7 +282,7 @@ class ProductFournisseur extends Product */ function fetch_product_fournisseur_price($rowid) { - $sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.fk_availability,"; + $sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.fk_availability,"; $sql.= " pfp.fk_soc, pfp.ref_fourn, pfp.fk_product"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; $sql.= " WHERE pfp.rowid = ".$rowid; @@ -291,14 +294,15 @@ class ProductFournisseur extends Product $obj = $this->db->fetch_object($resql); if ($obj) { - $this->product_fourn_price_id = $rowid; - $this->fourn_ref = $obj->ref_fourn; - $this->fourn_price = $obj->price; - $this->fourn_qty = $obj->quantity; - $this->fourn_unitprice = $obj->unitprice; - $this->product_id = $obj->fk_product; // deprecated - $this->fk_product = $obj->fk_product; - $this->fk_availability = $obj->fk_availability; + $this->product_fourn_price_id = $rowid; + $this->fourn_ref = $obj->ref_fourn; + $this->fourn_price = $obj->price; + $this->fourn_qty = $obj->quantity; + $this->fourn_unitprice = $obj->unitprice; + $this->tva_tx = $obj->tva_tx; + $this->product_id = $obj->fk_product; // deprecated + $this->fk_product = $obj->fk_product; + $this->fk_availability = $obj->fk_availability; return 1; } else @@ -327,7 +331,7 @@ class ProductFournisseur extends Product $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,"; $sql.= " pfp.rowid as product_fourn_pri_id, pfp.ref_fourn,"; - $sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.fk_availability"; + $sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.fk_availability"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; $sql.= ", ".MAIN_DB_PREFIX."societe as s"; $sql.= " WHERE pfp.entity IN (".getEntity('product', 1).")"; @@ -347,16 +351,17 @@ class ProductFournisseur extends Product //define base attribute $prodfourn = new ProductFournisseur($this->db); - $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"]; - $prodfourn->product_fourn_id = $record["product_fourn_id"]; - $prodfourn->fourn_ref = $record["ref_fourn"]; - $prodfourn->fourn_price = $record["price"]; - $prodfourn->fourn_qty = $record["quantity"]; - $prodfourn->fourn_unitprice = $record["unitprice"]; - $prodfourn->fourn_id = $record["fourn_id"]; - $prodfourn->fourn_name = $record["supplier_name"]; - $prodfourn->fk_availability = $record["fk_availability"]; - $prodfourn->id = $prodid; + $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"]; + $prodfourn->product_fourn_id = $record["product_fourn_id"]; + $prodfourn->fourn_ref = $record["ref_fourn"]; + $prodfourn->fourn_price = $record["price"]; + $prodfourn->fourn_qty = $record["quantity"]; + $prodfourn->fourn_unitprice = $record["unitprice"]; + $prodfourn->fourn_tva_tx = $record["tva_tx"]; + $prodfourn->fourn_id = $record["fourn_id"]; + $prodfourn->fourn_name = $record["supplier_name"]; + $prodfourn->fk_availability = $record["fk_availability"]; + $prodfourn->id = $prodid; if (!isset($prodfourn->fourn_unitprice)) { @@ -406,7 +411,7 @@ class ProductFournisseur extends Product $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,"; $sql.= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,"; - $sql.= " pfp.price, pfp.quantity, pfp.unitprice"; + $sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx"; $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; $sql.= " WHERE s.entity IN (".getEntity('societe', 1).")"; $sql.= " AND pfp.fk_product = ".$prodid; @@ -420,14 +425,15 @@ class ProductFournisseur extends Product if ($resql) { $record = $this->db->fetch_array($resql); - $this->product_fourn_price_id = $record["product_fourn_price_id"]; - $this->fourn_ref = $record["ref_fourn"]; - $this->fourn_price = $record["price"]; - $this->fourn_qty = $record["quantity"]; - $this->fourn_unitprice = $record["unitprice"]; - $this->fourn_id = $record["fourn_id"]; - $this->fourn_name = $record["supplier_name"]; - $this->id = $prodid; + $this->product_fourn_price_id = $record["product_fourn_price_id"]; + $this->fourn_ref = $record["ref_fourn"]; + $this->fourn_price = $record["price"]; + $this->fourn_qty = $record["quantity"]; + $this->fourn_unitprice = $record["unitprice"]; + $this->fourn_tva_tx = $record["tva_tx"]; + $this->fourn_id = $record["fourn_id"]; + $this->fourn_name = $record["supplier_name"]; + $this->id = $prodid; $this->db->free($resql); return 1; } diff --git a/htdocs/install/mysql/migration/3.1.0-3.2.0.sql b/htdocs/install/mysql/migration/3.1.0-3.2.0.sql index 80606c4f6ac..08a3b7ef659 100755 --- a/htdocs/install/mysql/migration/3.1.0-3.2.0.sql +++ b/htdocs/install/mysql/migration/3.1.0-3.2.0.sql @@ -271,9 +271,6 @@ ALTER TABLE llx_adherent_extrafields ADD COLUMN import_key varchar(14); ALTER TABLE llx_product_extrafields ADD COLUMN import_key varchar(14); ALTER TABLE llx_societe_extrafields ADD COLUMN import_key varchar(14); --- Disable foreign key checks for external modules constraints ---SET FOREIGN_KEY_CHECKS=0; - DROP TABLE llx_c_currencies; create table llx_c_currencies ( @@ -425,11 +422,14 @@ INSERT INTO llx_c_currencies ( code_iso, unicode, active, label ) VALUES ( 'XEU' INSERT INTO llx_c_currencies ( code_iso, unicode, active, label ) VALUES ( 'ARP', NULL, 0, 'Pesos argentins'); INSERT INTO llx_c_currencies ( code_iso, unicode, active, label ) VALUES ( 'MXP', NULL, 0, 'Pesos Mexicans'); ---SET FOREIGN_KEY_CHECKS=1; - ALTER TABLE llx_propal ADD CONSTRAINT fk_propal_fk_currency FOREIGN KEY (fk_currency) REFERENCES llx_c_currencies (code_iso); ALTER TABLE llx_commande ADD CONSTRAINT fk_commande_fk_currency FOREIGN KEY (fk_currency) REFERENCES llx_c_currencies (code_iso); ALTER TABLE llx_facture ADD CONSTRAINT fk_facture_fk_currency FOREIGN KEY (fk_currency) REFERENCES llx_c_currencies (code_iso); ALTER TABLE llx_expedition DROP COLUMN billed; + +ALTER TABLE llx_product_fournisseur_price DROP FOREIGN KEY fk_product_fournisseur_price_fk_product_fournisseur; +ALTER TABLE llx_product_fournisseur_price DROP INDEX idx_product_fournisseur_price_fk_product_fournisseur; +ALTER TABLE llx_product_fournisseur_price DROP COLUMN fk_product_fournisseur; +ALTER TABLE llx_product_fournisseur_price ADD COLUMN tva_tx double(6,3) NOT NULL AFTER unitprice; \ No newline at end of file diff --git a/htdocs/install/mysql/tables/llx_product_fournisseur_price.key.sql b/htdocs/install/mysql/tables/llx_product_fournisseur_price.key.sql index a625efcfbbc..26f82f6c397 100644 --- a/htdocs/install/mysql/tables/llx_product_fournisseur_price.key.sql +++ b/htdocs/install/mysql/tables/llx_product_fournisseur_price.key.sql @@ -1,6 +1,7 @@ -- ============================================================================ --- Copyright (C) 2003 Rodolphe Quiedeville --- Copyright (C) 2005 Laurent Destailleur +-- Copyright (C) 2003 Rodolphe Quiedeville +-- Copyright (C) 2005-2012 Laurent Destailleur +-- Copyright (C) 2009-2012 Regis Houssin -- -- 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 @@ -19,10 +20,8 @@ ALTER TABLE llx_product_fournisseur_price ADD INDEX idx_product_fournisseur_price_fk_user (fk_user); ---ALTER TABLE llx_product_fournisseur_price ADD INDEX idx_product_fournisseur_price_fk_product_fournisseur (fk_product_fournisseur); ALTER TABLE llx_product_fournisseur_price ADD CONSTRAINT fk_product_fournisseur_price_fk_user FOREIGN KEY (fk_user) REFERENCES llx_user (rowid); ---ALTER TABLE llx_product_fournisseur_price ADD CONSTRAINT fk_product_fournisseur_price_fk_product_fournisseur FOREIGN KEY (fk_product_fournisseur) REFERENCES llx_product_fournisseur (rowid); -- Added to remove table llx_product_fournisseur diff --git a/htdocs/install/mysql/tables/llx_product_fournisseur_price.sql b/htdocs/install/mysql/tables/llx_product_fournisseur_price.sql index 3adb886fa31..9d6c25fe1e8 100644 --- a/htdocs/install/mysql/tables/llx_product_fournisseur_price.sql +++ b/htdocs/install/mysql/tables/llx_product_fournisseur_price.sql @@ -1,7 +1,7 @@ -- ============================================================================ -- Copyright (C) 2003 Rodolphe Quiedeville -- Copyright (C) 2009-2011 Laurent Destailleur --- Copyright (C) 2009-2011 Regis Houssin +-- Copyright (C) 2009-2012 Regis Houssin -- -- 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 @@ -20,17 +20,17 @@ create table llx_product_fournisseur_price ( - rowid integer AUTO_INCREMENT PRIMARY KEY, - entity integer DEFAULT 1 NOT NULL, -- multi company id - datec datetime, - tms timestamp, - fk_product_fournisseur integer NOT NULL, -- deprecated - fk_product integer, - fk_soc integer, - ref_fourn varchar(30), - fk_availability integer, - price double(24,8) DEFAULT 0, - quantity double, - unitprice double(24,8) DEFAULT 0, - fk_user integer + rowid integer AUTO_INCREMENT PRIMARY KEY, + entity integer DEFAULT 1 NOT NULL, -- multi company id + datec datetime, + tms timestamp, + fk_product integer, + fk_soc integer, + ref_fourn varchar(30), + fk_availability integer, + price double(24,8) DEFAULT 0, + quantity double, + unitprice double(24,8) DEFAULT 0, + tva_tx double(6,3) NOT NULL, + fk_user integer )ENGINE=innodb; diff --git a/htdocs/product/fournisseurs.php b/htdocs/product/fournisseurs.php index e21333a50a0..e7670f77974 100644 --- a/htdocs/product/fournisseurs.php +++ b/htdocs/product/fournisseurs.php @@ -67,7 +67,6 @@ if (! $sortfield) $sortfield="s.nom"; if (! $sortorder) $sortorder="ASC"; - /* * Actions */ @@ -93,13 +92,14 @@ if ($action == 'updateprice' && $_POST["cancel"] <> $langs->trans("Cancel")) $ref_fourn=GETPOST("ref_fourn"); if (empty($ref_fourn)) $ref_fourn=GETPOST("search_ref_fourn"); $quantity=GETPOST("qty"); + $tva_tx=GETPOST('tva_tx','alpha'); if (empty($quantity)) { $error++; $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Qty")).'
'; } - if (empty($ref_fourn)) // TODO Why not making this optionnal ? + if (empty($ref_fourn)) { $error++; $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("RefSupplier")).'
'; @@ -151,7 +151,7 @@ if ($action == 'updateprice' && $_POST["cancel"] <> $langs->trans("Cancel")) $supplier=new Fournisseur($db); $result=$supplier->fetch($id_fourn); - $ret=$product->update_buyprice($quantity, $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn); + $ret=$product->update_buyprice($quantity, $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn, $tva_tx); if ($ret < 0) { $error++; @@ -279,7 +279,9 @@ if ($id || $ref) } else { - print $form->select_company(GETPOST("id_fourn"),'id_fourn','fournisseur=1',1); + $events=array(); + $events[]=array('method' => 'getVatRates', 'url' => dol_buildpath('/core/ajax/vatrates.php',1), 'htmlname' => 'tva_tx', 'params' => array()); + print $form->select_company(GETPOST("id_fourn"),'id_fourn','fournisseur=1',1,0,0,$events); if (is_object($hookmanager)) { @@ -301,9 +303,13 @@ if ($id || $ref) } print ''; print ''; + + // Vat rate + print ''.$langs->trans("VATRate").''; + print ''.$form->load_tva('tva_tx',$product->tva_tx,$supplier,$mysoc).''; // Availability - if(!empty($conf->global->FOURN_PRODUCT_AVAILABILITY)) + if (! empty($conf->global->FOURN_PRODUCT_AVAILABILITY)) { $langs->load("propal"); print ''.$langs->trans("Availability").''; @@ -313,8 +319,8 @@ if ($id || $ref) // Qty min print ''; - print ''.$langs->trans("QtyMin").''; - print ''; + print ''.$langs->trans("QtyMin").''; + print ''; $quantity = $_REQUEST["qty"] ? $_REQUEST["qty"] : "1"; if ($_GET["rowid"]) { @@ -326,8 +332,10 @@ if ($id || $ref) print ''; } print ''; - print ''.$langs->trans("PriceQtyMin").''; - print 'fourn_price)?price($product->fourn_price):'')).'">'; + + // Price qty min + print ''.$langs->trans("PriceQtyMin").''; + print 'fourn_price)?price($product->fourn_price):'')).'">'; print ' '; print $form->select_PriceBaseType(($_POST["price_base_type"]?$_POST["price_base_type"]:$product->price_base_type), "price_base_type"); print ''; @@ -335,7 +343,7 @@ if ($id || $ref) print ''; - print '
'; + print '
'; print '   '; print '
'; @@ -376,6 +384,7 @@ if ($id || $ref) print ''.$langs->trans("SupplierRef").''; if (!empty($conf->global->FOURN_PRODUCT_AVAILABILITY)) print_liste_field_titre($langs->trans("Availability"),$_SERVER["PHP_SELF"],"pfp.fk_availability","",$param,"",$sortfield,$sortorder); print_liste_field_titre($langs->trans("QtyMin"),$_SERVER["PHP_SELF"],"pfp.quantity","",$param,'align="right"',$sortfield,$sortorder); + print ''.$langs->trans("VATRate").''; print ''.$langs->trans("PriceQtyMinHT").''; print_liste_field_titre($langs->trans("UnitPriceHT"),$_SERVER["PHP_SELF"],"pfp.unitprice","",$param,'align="right"',$sortfield,$sortorder); print ''; @@ -411,6 +420,11 @@ if ($id || $ref) print ''; print $productfourn->fourn_qty; print ''; + + // VAT rate + print ''; + print vatrate($productfourn->fourn_tva_tx,true); + print ''; // Price quantity print ''; @@ -448,7 +462,7 @@ else } -$db->close(); - +// End of page llxFooter(); +$db->close(); ?> \ No newline at end of file diff --git a/htdocs/societe/soc.php b/htdocs/societe/soc.php index 0190a4c8a49..fe588f9d7af 100644 --- a/htdocs/societe/soc.php +++ b/htdocs/societe/soc.php @@ -614,7 +614,7 @@ else if ($conf->use_javascript_ajax) { - print "\n".'