Skip to content

Commit

Permalink
New: add vat rate in supplier price
Browse files Browse the repository at this point in the history
New: add events system in jquery combobox
  • Loading branch information
hregis committed Mar 15, 2012
1 parent d296f17 commit e8ec5f5
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 80 deletions.
55 changes: 55 additions & 0 deletions htdocs/core/ajax/vatrates.php
@@ -0,0 +1,55 @@
<?php
/* Copyright (C) 2012 Regis Houssin <regis@dolibarr.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/

/**
* \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 '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\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);
}

?>
12 changes: 7 additions & 5 deletions htdocs/core/class/html.form.class.php
Expand Up @@ -640,9 +640,10 @@ function select_type_fees($selected='',$htmlname='type',$showempty=0)
* @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;

Expand All @@ -665,7 +666,7 @@ function select_company($selected='',$htmlname='socid',$filter='',$showempty=0,
{
//$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.= '<select id="'.$htmlname.'" class="flat" name="'.$htmlname.'">';
Expand Down Expand Up @@ -2890,9 +2891,10 @@ function load_cache_vatrates($country_code)
* Si (vendeur et acheteur dans Communaute europeenne) et bien vendu = moyen de transports neuf (auto, bateau, avion), TVA par defaut=0 (La TVA doit etre paye par l'acheteur au centre d'impots de son pays et non au vendeur). Fin de regle.
* Si (vendeur et acheteur dans Communaute europeenne) et bien vendu autre que transport neuf alors la TVA par defaut=TVA du produit vendu. Fin de regle.
* Sinon la TVA proposee par defaut=0. Fin de regle.
* @param bool $options_only Return options only (for ajax treatment)
* @return void
*/
function load_tva($htmlname='tauxtva', $selectedrate='', $societe_vendeuse='', $societe_acheteuse='', $idprod=0, $info_bits=0, $type='')
function load_tva($htmlname='tauxtva', $selectedrate='', $societe_vendeuse='', $societe_acheteuse='', $idprod=0, $info_bits=0, $type='', $options_only=false)
{
global $langs,$conf,$mysoc;

Expand Down Expand Up @@ -2981,7 +2983,7 @@ function load_tva($htmlname='tauxtva', $selectedrate='', $societe_vendeuse='', $
$defaulttx = $this->cache_vatrates[$num-1]['txtva'];
}

$return.= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
if (! $options_only) $return.= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';

foreach ($this->cache_vatrates as $rate)
{
Expand All @@ -3001,7 +3003,7 @@ function load_tva($htmlname='tauxtva', $selectedrate='', $societe_vendeuse='', $
$this->tva_taux_npr[] = $rate['nprtva'];
}

$return.= '</select>';
if (! $options_only) $return.= '</select>';
}
else
{
Expand Down
36 changes: 30 additions & 6 deletions htdocs/core/lib/ajax.lib.php
Expand Up @@ -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.= '<script type="text/javascript">
$(function() {
$("#'.$htmlname.'" ).combobox();
});
</script>';

$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.= "</script>\n";

return $msg;
}
Expand Down
72 changes: 39 additions & 33 deletions htdocs/fourn/class/fournisseur.product.class.php
Expand Up @@ -139,9 +139,10 @@ function remove_product_fournisseur_price($rowid)
* @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;

Expand All @@ -166,16 +167,17 @@ function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $avai
{
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');

$now=dol_now();

// 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.",";
Expand All @@ -184,6 +186,7 @@ function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $avai
$sql.= " ".price2num($buyprice).",";
$sql.= " ".$qty.",";
$sql.= " ".$unitBuyPrice.",";
$sql.= " ".$tva_tx.",";
$sql.= " ".$availability.",";
$sql.= $conf->entity;
$sql.=")";
Expand Down Expand Up @@ -279,7 +282,7 @@ function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $avai
*/
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;
Expand All @@ -291,14 +294,15 @@ function fetch_product_fournisseur_price($rowid)
$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
Expand Down Expand Up @@ -327,7 +331,7 @@ function list_product_fournisseur_price($prodid)

$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).")";
Expand All @@ -347,16 +351,17 @@ function list_product_fournisseur_price($prodid)
//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))
{
Expand Down Expand Up @@ -406,7 +411,7 @@ function find_min_price_product_fournisseur($prodid)

$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;
Expand All @@ -420,14 +425,15 @@ function find_min_price_product_fournisseur($prodid)
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;
}
Expand Down
10 changes: 5 additions & 5 deletions htdocs/install/mysql/migration/3.1.0-3.2.0.sql
Expand Up @@ -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
(
Expand Down Expand Up @@ -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;

@@ -1,6 +1,7 @@
-- ============================================================================
-- Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
-- Copyright (C) 2005 Laurent Destailleur <eldy@users.sourceforge.net>
-- Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
-- Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
-- Copyright (C) 2009-2012 Regis Houssin <regis@dolibarr.fr>
--
-- 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
Expand All @@ -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

Expand Down
28 changes: 14 additions & 14 deletions htdocs/install/mysql/tables/llx_product_fournisseur_price.sql
@@ -1,7 +1,7 @@
-- ============================================================================
-- Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
-- Copyright (C) 2009-2011 Laurent Destailleur <eldy@users.sourceforge.net>
-- Copyright (C) 2009-2011 Regis Houssin <regis@dolibarr.fr>
-- Copyright (C) 2009-2012 Regis Houssin <regis@dolibarr.fr>
--
-- 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
Expand All @@ -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;

0 comments on commit e8ec5f5

Please sign in to comment.