FIX Lines are not inserted correctly if VAT have code

This commit is contained in:
Ferran Marcet 2018-12-03 12:28:17 +01:00
parent 135a94dfed
commit 7247b14bd9
7 changed files with 342 additions and 326 deletions

View File

@ -416,48 +416,40 @@ class Propal extends CommonObject
global $mysoc, $conf, $langs; global $mysoc, $conf, $langs;
dol_syslog(get_class($this)."::addline propalid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_except=$remise_percent, price_base_type=$price_base_type, pu_ttc=$pu_ttc, info_bits=$info_bits, type=$type, fk_remise_except=".$fk_remise_except); dol_syslog(get_class($this)."::addline propalid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_except=$remise_percent, price_base_type=$price_base_type, pu_ttc=$pu_ttc, info_bits=$info_bits, type=$type, fk_remise_except=".$fk_remise_except);
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
// Clean parameters
if (empty($remise_percent)) $remise_percent=0;
if (empty($qty)) $qty=0;
if (empty($info_bits)) $info_bits=0;
if (empty($rang)) $rang=0;
if (empty($fk_parent_line) || $fk_parent_line < 0) $fk_parent_line=0;
$localtaxes_type=getLocalTaxesFromRate($txtva,0,$this->thirdparty,$mysoc);
// Clean vat code
$vat_src_code='';
if (preg_match('/\((.*)\)/', $txtva, $reg))
{
$vat_src_code = $reg[1];
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
}
$remise_percent=price2num($remise_percent);
$qty=price2num($qty);
$pu_ht=price2num($pu_ht);
$pu_ht_devise=price2num($pu_ht_devise);
$pu_ttc=price2num($pu_ttc);
$txtva=price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
$txlocaltax1=price2num($txlocaltax1);
$txlocaltax2=price2num($txlocaltax2);
$pa_ht=price2num($pa_ht);
if ($price_base_type=='HT')
{
$pu=$pu_ht;
}
else
{
$pu=$pu_ttc;
}
// Check parameters
if ($type < 0) return -1;
if ($this->statut == self::STATUS_DRAFT) if ($this->statut == self::STATUS_DRAFT)
{ {
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
// Clean parameters
if (empty($remise_percent)) $remise_percent=0;
if (empty($qty)) $qty=0;
if (empty($info_bits)) $info_bits=0;
if (empty($rang)) $rang=0;
if (empty($fk_parent_line) || $fk_parent_line < 0) $fk_parent_line=0;
$remise_percent=price2num($remise_percent);
$qty=price2num($qty);
$pu_ht=price2num($pu_ht);
$pu_ht_devise=price2num($pu_ht_devise);
$pu_ttc=price2num($pu_ttc);
if (!preg_match('/\((.*)\)/', $txtva)) {
$txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
}
$txlocaltax1=price2num($txlocaltax1);
$txlocaltax2=price2num($txlocaltax2);
$pa_ht=price2num($pa_ht);
if ($price_base_type=='HT')
{
$pu=$pu_ht;
}
else
{
$pu=$pu_ttc;
}
// Check parameters
if ($type < 0) return -1;
$this->db->begin(); $this->db->begin();
$product_type=$type; $product_type=$type;
@ -480,6 +472,16 @@ class Propal extends CommonObject
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker // 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. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
$localtaxes_type=getLocalTaxesFromRate($txtva,0,$this->thirdparty,$mysoc);
// Clean vat code
$vat_src_code='';
if (preg_match('/\((.*)\)/', $txtva, $reg))
{
$vat_src_code = $reg[1];
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
}
$tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise); $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
$total_ht = $tabprice[0]; $total_ht = $tabprice[0];

View File

@ -1270,54 +1270,46 @@ class Commande extends CommonOrder
dol_syslog(get_class($this)."::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent, info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc, date_start=$date_start, date_end=$date_end, type=$type special_code=$special_code, fk_unit=$fk_unit, origin=$origin, origin_id=$origin_id, pu_ht_devise=$pu_ht_devise", LOG_DEBUG); dol_syslog(get_class($this)."::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent, info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc, date_start=$date_start, date_end=$date_end, type=$type special_code=$special_code, fk_unit=$fk_unit, origin=$origin, origin_id=$origin_id, pu_ht_devise=$pu_ht_devise", LOG_DEBUG);
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; if ($this->statut == self::STATUS_DRAFT)
// Clean parameters
if (empty($remise_percent)) $remise_percent=0;
if (empty($qty)) $qty=0;
if (empty($info_bits)) $info_bits=0;
if (empty($rang)) $rang=0;
if (empty($txtva)) $txtva=0;
if (empty($txlocaltax1)) $txlocaltax1=0;
if (empty($txlocaltax2)) $txlocaltax2=0;
if (empty($fk_parent_line) || $fk_parent_line < 0) $fk_parent_line=0;
if (empty($this->fk_multicurrency)) $this->fk_multicurrency=0;
$localtaxes_type=getLocalTaxesFromRate($txtva,0,$this->thirdparty,$mysoc);
// Clean vat code
$vat_src_code='';
if (preg_match('/\((.*)\)/', $txtva, $reg))
{ {
$vat_src_code = $reg[1]; include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
}
$remise_percent=price2num($remise_percent); // Clean parameters
$qty=price2num($qty); if (empty($remise_percent)) $remise_percent=0;
$pu_ht=price2num($pu_ht); if (empty($qty)) $qty=0;
$pu_ht_devise=price2num($pu_ht_devise); if (empty($info_bits)) $info_bits=0;
$pu_ttc=price2num($pu_ttc); if (empty($rang)) $rang=0;
$pa_ht=price2num($pa_ht); if (empty($txtva)) $txtva=0;
$txtva = price2num($txtva); if (empty($txlocaltax1)) $txlocaltax1=0;
$txlocaltax1 = price2num($txlocaltax1); if (empty($txlocaltax2)) $txlocaltax2=0;
$txlocaltax2 = price2num($txlocaltax2); if (empty($fk_parent_line) || $fk_parent_line < 0) $fk_parent_line=0;
if ($price_base_type=='HT') if (empty($this->fk_multicurrency)) $this->fk_multicurrency=0;
{
$pu=$pu_ht;
}
else
{
$pu=$pu_ttc;
}
$label=trim($label);
$desc=trim($desc);
// Check parameters $remise_percent=price2num($remise_percent);
if ($type < 0) return -1; $qty=price2num($qty);
$pu_ht=price2num($pu_ht);
$pu_ht_devise=price2num($pu_ht_devise);
$pu_ttc=price2num($pu_ttc);
$pa_ht=price2num($pa_ht);
if (!preg_match('/\((.*)\)/', $txtva)) {
$txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
}
$txlocaltax1 = price2num($txlocaltax1);
$txlocaltax2 = price2num($txlocaltax2);
if ($price_base_type=='HT')
{
$pu=$pu_ht;
}
else
{
$pu=$pu_ttc;
}
$label=trim($label);
$desc=trim($desc);
// Check parameters
if ($type < 0) return -1;
if ($this->statut == self::STATUS_DRAFT)
{
$this->db->begin(); $this->db->begin();
$product_type=$type; $product_type=$type;
@ -1336,11 +1328,20 @@ class Commande extends CommonOrder
return self::STOCK_NOT_ENOUGH_FOR_ORDER; return self::STOCK_NOT_ENOUGH_FOR_ORDER;
} }
} }
// Calcul du total TTC et de la TVA pour la ligne a partir de // Calcul du total TTC et de la TVA pour la ligne a partir de
// qty, pu, remise_percent et txtva // qty, pu, remise_percent et txtva
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker // 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. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
$localtaxes_type=getLocalTaxesFromRate($txtva,0,$this->thirdparty,$mysoc);
// Clean vat code
$vat_src_code='';
if (preg_match('/\((.*)\)/', $txtva, $reg))
{
$vat_src_code = $reg[1];
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
}
$tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise); $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);

View File

@ -2580,55 +2580,47 @@ class Facture extends CommonInvoice
global $mysoc, $conf, $langs; global $mysoc, $conf, $langs;
dol_syslog(get_class($this)."::addline id=$this->id,desc=$desc,pu_ht=$pu_ht,qty=$qty,txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, fk_product=$fk_product,remise_percent=$remise_percent,date_start=$date_start,date_end=$date_end,ventil=$ventil,info_bits=$info_bits,fk_remise_except=$fk_remise_except,price_base_type=$price_base_type,pu_ttc=$pu_ttc,type=$type, fk_unit=$fk_unit", LOG_DEBUG); dol_syslog(get_class($this)."::addline id=$this->id,desc=$desc,pu_ht=$pu_ht,qty=$qty,txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, fk_product=$fk_product,remise_percent=$remise_percent,date_start=$date_start,date_end=$date_end,ventil=$ventil,info_bits=$info_bits,fk_remise_except=$fk_remise_except,price_base_type=$price_base_type,pu_ttc=$pu_ttc,type=$type, fk_unit=$fk_unit", LOG_DEBUG);
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
// Clean parameters
if (empty($remise_percent)) $remise_percent=0;
if (empty($qty)) $qty=0;
if (empty($info_bits)) $info_bits=0;
if (empty($rang)) $rang=0;
if (empty($ventil)) $ventil=0;
if (empty($txtva)) $txtva=0;
if (empty($txlocaltax1)) $txlocaltax1=0;
if (empty($txlocaltax2)) $txlocaltax2=0;
if (empty($fk_parent_line) || $fk_parent_line < 0) $fk_parent_line=0;
if (empty($fk_prev_id)) $fk_prev_id = 'null';
if (! isset($situation_percent) || $situation_percent > 100 || (string) $situation_percent == '') $situation_percent = 100;
$localtaxes_type=getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
// Clean vat code
$vat_src_code='';
if (preg_match('/\((.*)\)/', $txtva, $reg))
{
$vat_src_code = $reg[1];
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
}
$remise_percent=price2num($remise_percent);
$qty=price2num($qty);
$pu_ht=price2num($pu_ht);
$pu_ht_devise=price2num($pu_ht_devise);
$pu_ttc=price2num($pu_ttc);
$pa_ht=price2num($pa_ht);
$txtva=price2num($txtva);
$txlocaltax1=price2num($txlocaltax1);
$txlocaltax2=price2num($txlocaltax2);
if ($price_base_type=='HT')
{
$pu=$pu_ht;
}
else
{
$pu=$pu_ttc;
}
// Check parameters
if ($type < 0) return -1;
if (! empty($this->brouillon)) if (! empty($this->brouillon))
{ {
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
// Clean parameters
if (empty($remise_percent)) $remise_percent=0;
if (empty($qty)) $qty=0;
if (empty($info_bits)) $info_bits=0;
if (empty($rang)) $rang=0;
if (empty($ventil)) $ventil=0;
if (empty($txtva)) $txtva=0;
if (empty($txlocaltax1)) $txlocaltax1=0;
if (empty($txlocaltax2)) $txlocaltax2=0;
if (empty($fk_parent_line) || $fk_parent_line < 0) $fk_parent_line=0;
if (empty($fk_prev_id)) $fk_prev_id = 'null';
if (! isset($situation_percent) || $situation_percent > 100 || (string) $situation_percent == '') $situation_percent = 100;
$remise_percent=price2num($remise_percent);
$qty=price2num($qty);
$pu_ht=price2num($pu_ht);
$pu_ht_devise=price2num($pu_ht_devise);
$pu_ttc=price2num($pu_ttc);
$pa_ht=price2num($pa_ht);
if (!preg_match('/\((.*)\)/', $txtva)) {
$txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
}
$txlocaltax1=price2num($txlocaltax1);
$txlocaltax2=price2num($txlocaltax2);
if ($price_base_type=='HT')
{
$pu=$pu_ht;
}
else
{
$pu=$pu_ttc;
}
// Check parameters
if ($type < 0) return -1;
$this->db->begin(); $this->db->begin();
$product_type=$type; $product_type=$type;
@ -2651,6 +2643,16 @@ class Facture extends CommonInvoice
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker // 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. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
$localtaxes_type=getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
// Clean vat code
$vat_src_code='';
if (preg_match('/\((.*)\)/', $txtva, $reg))
{
$vat_src_code = $reg[1];
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
}
$tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, $situation_percent, $this->multicurrency_tx, $pu_ht_devise); $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, $situation_percent, $this->multicurrency_tx, $pu_ht_devise);
$total_ht = $tabprice[0]; $total_ht = $tabprice[0];

View File

@ -1358,21 +1358,13 @@ class Contrat extends CommonObject
{ {
$this->db->begin(); $this->db->begin();
$localtaxes_type=getLocalTaxesFromRate($txtva, 0, $this->societe, $mysoc);
// Clean vat code
$vat_src_code='';
if (preg_match('/\((.*)\)/', $txtva, $reg))
{
$vat_src_code = $reg[1];
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
}
// Clean parameters // Clean parameters
$pu_ht=price2num($pu_ht); $pu_ht=price2num($pu_ht);
$pu_ttc=price2num($pu_ttc); $pu_ttc=price2num($pu_ttc);
$pa_ht=price2num($pa_ht); $pa_ht=price2num($pa_ht);
$txtva=price2num($txtva); if (!preg_match('/\((.*)\)/', $txtva)) {
$txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
}
$txlocaltax1=price2num($txlocaltax1); $txlocaltax1=price2num($txlocaltax1);
$txlocaltax2=price2num($txlocaltax2); $txlocaltax2=price2num($txlocaltax2);
$remise_percent=price2num($remise_percent); $remise_percent=price2num($remise_percent);
@ -1402,6 +1394,16 @@ class Contrat extends CommonObject
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker // 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. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
$localtaxes_type=getLocalTaxesFromRate($txtva, 0, $this->societe, $mysoc);
// Clean vat code
$vat_src_code='';
if (preg_match('/\((.*)\)/', $txtva, $reg))
{
$vat_src_code = $reg[1];
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
}
$tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1,$mysoc, $localtaxes_type); $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1,$mysoc, $localtaxes_type);
$total_ht = $tabprice[0]; $total_ht = $tabprice[0];
$total_tva = $tabprice[1]; $total_tva = $tabprice[1];

View File

@ -1678,29 +1678,30 @@ class ExpenseReport extends CommonObject
dol_syslog(get_class($this)."::addline qty=$qty, up=$up, fk_c_type_fees=$fk_c_type_fees, vatrate=$vatrate, date=$date, fk_project=$fk_project, type=$type, comments=$comments", LOG_DEBUG); dol_syslog(get_class($this)."::addline qty=$qty, up=$up, fk_c_type_fees=$fk_c_type_fees, vatrate=$vatrate, date=$date, fk_project=$fk_project, type=$type, comments=$comments", LOG_DEBUG);
if (empty($qty)) $qty = 0;
if (empty($fk_c_type_fees) || $fk_c_type_fees < 0) $fk_c_type_fees = 0;
if (empty($fk_c_exp_tax_cat) || $fk_c_exp_tax_cat < 0) $fk_c_exp_tax_cat = 0;
if (empty($vatrate) || $vatrate < 0) $vatrate = 0;
if (empty($date)) $date = '';
if (empty($fk_project)) $fk_project = 0;
if (preg_match('/\((.*)\)/', $vatrate, $reg))
{
$vat_src_code = $reg[1];
$vatrate = preg_replace('/\s*\(.*\)/', '', $vatrate); // Remove code into vatrate.
}
$qty = price2num($qty);
$vatrate = price2num($vatrate);
$up = price2num($up);
if ($this->fk_statut == self::STATUS_DRAFT) if ($this->fk_statut == self::STATUS_DRAFT)
{ {
if (empty($qty)) $qty = 0;
if (empty($fk_c_type_fees) || $fk_c_type_fees < 0) $fk_c_type_fees = 0;
if (empty($fk_c_exp_tax_cat) || $fk_c_exp_tax_cat < 0) $fk_c_exp_tax_cat = 0;
if (empty($vatrate) || $vatrate < 0) $vatrate = 0;
if (empty($date)) $date = '';
if (empty($fk_project)) $fk_project = 0;
$qty = price2num($qty);
if (!preg_match('/\((.*)\)/', $vatrate)) {
$vatrate = price2num($vatrate); // $txtva can have format '5.0(XXX)' or '5'
}
$up = price2num($up);
$this->db->begin(); $this->db->begin();
$this->line = new ExpenseReportLine($this->db); $this->line = new ExpenseReportLine($this->db);
if (preg_match('/\((.*)\)/', $vatrate, $reg))
{
$vat_src_code = $reg[1];
$vatrate = preg_replace('/\s*\(.*\)/', '', $vatrate); // Remove code into vatrate.
}
$vatrate = preg_replace('/\*/','',$vatrate); $vatrate = preg_replace('/\*/','',$vatrate);
$seller = ''; // seller is unknown $seller = ''; // seller is unknown

View File

@ -1425,54 +1425,48 @@ class CommandeFournisseur extends CommonOrder
$error = 0; $error = 0;
dol_syslog(get_class($this)."::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $fk_prod_fourn_price, $ref_supplier, $remise_percent, $price_base_type, $pu_ttc, $type, $fk_unit, $pu_ht_devise, $origin, $origin_id"); dol_syslog(get_class($this)."::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $fk_prod_fourn_price, $ref_supplier, $remise_percent, $price_base_type, $pu_ttc, $type, $fk_unit, $pu_ht_devise, $origin, $origin_id");
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
// Clean parameters if ($this->statut == self::STATUS_DRAFT)
if (! $qty) $qty=1;
if (! $info_bits) $info_bits=0;
if (empty($txtva)) $txtva=0;
if (empty($txlocaltax1)) $txlocaltax1=0;
if (empty($txlocaltax2)) $txlocaltax2=0;
if (empty($remise_percent)) $remise_percent=0;
$localtaxes_type=getLocalTaxesFromRate($txtva,0,$mysoc,$this->thirdparty);
// Clean vat code
$vat_src_code='';
if (preg_match('/\((.*)\)/', $txtva, $reg))
{ {
$vat_src_code = $reg[1]; include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
}
$remise_percent=price2num($remise_percent); // Clean parameters
$qty=price2num($qty); if (! $qty) $qty=1;
$pu_ht=price2num($pu_ht); if (! $info_bits) $info_bits=0;
$pu_ht_devise=price2num($pu_ht_devise); if (empty($txtva)) $txtva=0;
$pu_ttc=price2num($pu_ttc); if (empty($txlocaltax1)) $txlocaltax1=0;
$txtva = price2num($txtva); if (empty($txlocaltax2)) $txlocaltax2=0;
$txlocaltax1 = price2num($txlocaltax1); if (empty($remise_percent)) $remise_percent=0;
$txlocaltax2 = price2num($txlocaltax2);
if ($price_base_type=='HT') $remise_percent=price2num($remise_percent);
{ $qty=price2num($qty);
$pu=$pu_ht; $pu_ht=price2num($pu_ht);
} $pu_ht_devise=price2num($pu_ht_devise);
else $pu_ttc=price2num($pu_ttc);
{ if (!preg_match('/\((.*)\)/', $txtva)) {
$pu=$pu_ttc; $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
} }
$desc=trim($desc); $txlocaltax1 = price2num($txlocaltax1);
$txlocaltax2 = price2num($txlocaltax2);
if ($price_base_type=='HT')
{
$pu=$pu_ht;
}
else
{
$pu=$pu_ttc;
}
$desc=trim($desc);
// Check parameters
if ($qty < 1 && ! $fk_product)
{
$this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Product"));
return -1;
}
if ($type < 0) return -1;
// Check parameters
if ($qty < 1 && ! $fk_product)
{
$this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Product"));
return -1;
}
if ($type < 0) return -1;
if ($this->statut == 0)
{
$this->db->begin(); $this->db->begin();
if ($fk_product > 0) if ($fk_product > 0)
@ -1537,10 +1531,20 @@ class CommandeFournisseur extends CommonOrder
$product_type = $type; $product_type = $type;
} }
// Calcul du total TTC et de la TVA pour la ligne a partir de // Calcul du total TTC et de la TVA pour la ligne a partir de
// qty, pu, remise_percent et txtva // qty, pu, remise_percent et txtva
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker // 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. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
$localtaxes_type=getLocalTaxesFromRate($txtva,0,$mysoc,$this->thirdparty);
// Clean vat code
$vat_src_code='';
if (preg_match('/\((.*)\)/', $txtva, $reg))
{
$vat_src_code = $reg[1];
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
}
$tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx,$pu_ht_devise); $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx,$pu_ht_devise);
$total_ht = $tabprice[0]; $total_ht = $tabprice[0];

View File

@ -1392,136 +1392,140 @@ class FactureFournisseur extends CommonInvoice
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
global $mysoc; global $mysoc;
// Clean parameters if ($this->statut == self::STATUS_DRAFT){
if (empty($remise_percent)) $remise_percent=0; // Clean parameters
if (empty($qty)) $qty=0; if (empty($remise_percent)) $remise_percent=0;
if (empty($info_bits)) $info_bits=0; if (empty($qty)) $qty=0;
if (empty($rang)) $rang=0; if (empty($info_bits)) $info_bits=0;
if (empty($ventil)) $ventil=0; if (empty($rang)) $rang=0;
if (empty($txtva)) $txtva=0; if (empty($ventil)) $ventil=0;
if (empty($txlocaltax1)) $txlocaltax1=0; if (empty($txtva)) $txtva=0;
if (empty($txlocaltax2)) $txlocaltax2=0; if (empty($txlocaltax1)) $txlocaltax1=0;
if (empty($txlocaltax2)) $txlocaltax2=0;
if ($rang < 0) { if ($rang < 0) {
$rangmax = $this->line_max(); $rangmax = $this->line_max();
$rang = $rangmax + 1; $rang = $rangmax + 1;
} }
$localtaxes_type=getLocalTaxesFromRate($txtva, 0, $mysoc, $this->thirdparty);
// Clean vat code $remise_percent=price2num($remise_percent);
$vat_src_code=''; $qty=price2num($qty);
if (preg_match('/\((.*)\)/', $txtva, $reg)) $pu=price2num($pu);
{ if (!preg_match('/\((.*)\)/', $txtva)) {
$vat_src_code = $reg[1]; $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate. }
} $txlocaltax1=price2num($txlocaltax1);
$txlocaltax2=price2num($txlocaltax2);
$remise_percent=price2num($remise_percent); $localtaxes_type=getLocalTaxesFromRate($txtva, 0, $mysoc, $this->thirdparty);
$qty=price2num($qty);
$pu=price2num($pu);
$txtva=price2num($txtva);
$txlocaltax1=price2num($txlocaltax1);
$txlocaltax2=price2num($txlocaltax2);
$tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise); // Clean vat code
$total_ht = $tabprice[0]; $vat_src_code='';
$total_tva = $tabprice[1]; if (preg_match('/\((.*)\)/', $txtva, $reg))
$total_ttc = $tabprice[2]; {
$total_localtax1 = $tabprice[9]; $vat_src_code = $reg[1];
$total_localtax2 = $tabprice[10]; $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
$pu_ht = $tabprice[3]; }
// MultiCurrency $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
$multicurrency_total_ht = $tabprice[16]; $total_ht = $tabprice[0];
$multicurrency_total_tva = $tabprice[17]; $total_tva = $tabprice[1];
$multicurrency_total_ttc = $tabprice[18]; $total_ttc = $tabprice[2];
$pu_ht_devise = $tabprice[19]; $total_localtax1 = $tabprice[9];
$total_localtax2 = $tabprice[10];
$pu_ht = $tabprice[3];
// Check parameters // MultiCurrency
if ($type < 0) return -1; $multicurrency_total_ht = $tabprice[16];
$multicurrency_total_tva = $tabprice[17];
$multicurrency_total_ttc = $tabprice[18];
$pu_ht_devise = $tabprice[19];
if ($rang < 0) // Check parameters
{ if ($type < 0) return -1;
$rangmax = $this->line_max();
$rang = $rangmax + 1;
}
// Insert line if ($rang < 0)
$this->line=new SupplierInvoiceLine($this->db); {
$rangmax = $this->line_max();
$rang = $rangmax + 1;
}
$this->line->context = $this->context; // Insert line
$this->line=new SupplierInvoiceLine($this->db);
$this->line->fk_facture_fourn=$this->id; $this->line->context = $this->context;
//$this->line->label=$label; // deprecated
$this->line->desc=$desc;
$this->line->qty= ($this->type==self::TYPE_CREDIT_NOTE?abs($qty):$qty); // For credit note, quantity is always positive and unit price negative
$this->line->ref_supplier=$ref_supplier;
$this->line->vat_src_code=$vat_src_code; $this->line->fk_facture_fourn=$this->id;
$this->line->tva_tx=$txtva; //$this->line->label=$label; // deprecated
$this->line->localtax1_tx=($total_localtax1?$localtaxes_type[1]:0); $this->line->desc=$desc;
$this->line->localtax2_tx=($total_localtax2?$localtaxes_type[3]:0); $this->line->qty= ($this->type==self::TYPE_CREDIT_NOTE?abs($qty):$qty); // For credit note, quantity is always positive and unit price negative
$this->line->localtax1_type = $localtaxes_type[0]; $this->line->ref_supplier=$ref_supplier;
$this->line->localtax2_type = $localtaxes_type[2];
$this->line->fk_product=$fk_product;
$this->line->product_type=$type;
$this->line->remise_percent=$remise_percent;
$this->line->subprice= ($this->type==self::TYPE_CREDIT_NOTE?-abs($pu_ht):$pu_ht); // For credit note, unit price always negative, always positive otherwise
$this->line->date_start=$date_start;
$this->line->date_end=$date_end;
$this->line->ventil=$ventil;
$this->line->rang=$rang;
$this->line->info_bits=$info_bits;
$this->line->total_ht= (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_ht):$total_ht); // For credit note and if qty is negative, total is negative
$this->line->total_tva= $total_tva;
$this->line->total_localtax1=$total_localtax1;
$this->line->total_localtax2=$total_localtax2;
$this->line->total_ttc= (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_ttc):$total_ttc);
$this->line->special_code=$this->special_code;
$this->line->fk_parent_line=$this->fk_parent_line;
$this->line->origin=$this->origin;
$this->line->origin_id=$origin_id;
$this->line->fk_unit=$fk_unit;
// Multicurrency $this->line->vat_src_code=$vat_src_code;
$this->line->fk_multicurrency = $this->fk_multicurrency; $this->line->tva_tx=$txtva;
$this->line->multicurrency_code = $this->multicurrency_code; $this->line->localtax1_tx=($total_localtax1?$localtaxes_type[1]:0);
$this->line->multicurrency_subprice = $pu_ht_devise; $this->line->localtax2_tx=($total_localtax2?$localtaxes_type[3]:0);
$this->line->multicurrency_total_ht = $multicurrency_total_ht; $this->line->localtax1_type = $localtaxes_type[0];
$this->line->multicurrency_total_tva = $multicurrency_total_tva; $this->line->localtax2_type = $localtaxes_type[2];
$this->line->multicurrency_total_ttc = $multicurrency_total_ttc; $this->line->fk_product=$fk_product;
$this->line->product_type=$type;
$this->line->remise_percent=$remise_percent;
$this->line->subprice= ($this->type==self::TYPE_CREDIT_NOTE?-abs($pu_ht):$pu_ht); // For credit note, unit price always negative, always positive otherwise
$this->line->date_start=$date_start;
$this->line->date_end=$date_end;
$this->line->ventil=$ventil;
$this->line->rang=$rang;
$this->line->info_bits=$info_bits;
$this->line->total_ht= (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_ht):$total_ht); // For credit note and if qty is negative, total is negative
$this->line->total_tva= $total_tva;
$this->line->total_localtax1=$total_localtax1;
$this->line->total_localtax2=$total_localtax2;
$this->line->total_ttc= (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_ttc):$total_ttc);
$this->line->special_code=$this->special_code;
$this->line->fk_parent_line=$this->fk_parent_line;
$this->line->origin=$this->origin;
$this->line->origin_id=$origin_id;
$this->line->fk_unit=$fk_unit;
if (is_array($array_options) && count($array_options)>0) { // Multicurrency
$this->line->array_options=$array_options; $this->line->fk_multicurrency = $this->fk_multicurrency;
} $this->line->multicurrency_code = $this->multicurrency_code;
$this->line->multicurrency_subprice = $pu_ht_devise;
$this->line->multicurrency_total_ht = $multicurrency_total_ht;
$this->line->multicurrency_total_tva = $multicurrency_total_tva;
$this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
$result=$this->line->insert($notrigger); if (is_array($array_options) && count($array_options)>0) {
if ($result > 0) $this->line->array_options=$array_options;
{ }
// Reorder if child line
if (! empty($fk_parent_line)) $this->line_order(true,'DESC');
// Mise a jour informations denormalisees au niveau de la facture meme $result=$this->line->insert($notrigger);
$result=$this->update_price(1,'auto',0,$this->thirdparty); // The addline method is designed to add line from user input so total calculation with update_price must be done using 'auto' mode. if ($result > 0)
if ($result > 0) {
{ // Reorder if child line
$this->db->commit(); if (! empty($fk_parent_line)) $this->line_order(true,'DESC');
return $this->line->id;
} // Mise a jour informations denormalisees au niveau de la facture meme
else $result=$this->update_price(1,'auto',0,$this->thirdparty); // The addline method is designed to add line from user input so total calculation with update_price must be done using 'auto' mode.
{ if ($result > 0)
$this->error=$this->db->error(); {
$this->db->rollback(); $this->db->commit();
return -1; return $this->line->id;
} }
} else
else {
{ $this->error=$this->db->error();
$this->error=$this->line->error; $this->db->rollback();
$this->errors=$this->line->errors; return -1;
$this->db->rollback(); }
return -2; }
else
{
$this->error=$this->line->error;
$this->errors=$this->line->errors;
$this->db->rollback();
return -2;
}
} }
} }