diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 1285d6da920..d89237c07c5 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -1,7 +1,7 @@ * Copyright (C) 2004 Eric Seigne - * Copyright (C) 2004-2011 Laurent Destailleur + * Copyright (C) 2004-2012 Laurent Destailleur * Copyright (C) 2005 Marc Barilley / Ocebo * Copyright (C) 2005-2012 Regis Houssin * Copyright (C) 2006 Andre Cianfarani @@ -1924,7 +1924,7 @@ if ($action == 'create') // Calcul contrat->price (HT), contrat->total (TTC), contrat->tva $objectsrc->remise_absolue=$remise_absolue; $objectsrc->remise_percent=$remise_percent; - $objectsrc->update_price(1); + $objectsrc->update_price(1,-1,1); } print "\n"; diff --git a/htdocs/contrat/class/contrat.class.php b/htdocs/contrat/class/contrat.class.php index de54bcdd128..2a16039a351 100644 --- a/htdocs/contrat/class/contrat.class.php +++ b/htdocs/contrat/class/contrat.class.php @@ -821,7 +821,7 @@ class Contrat extends CommonObject * @param float $remise_percent Pourcentage de remise de la ligne * @param timestamp $date_start Date de debut prevue * @param timestamp $date_end Date de fin prevue - * @param float $price_base_type HT ou TTC + * @param float $price_base_type HT or TTC * @param float $pu_ttc Prix unitaire TTC * @param int $info_bits Bits de type de lignes * @return int <0 si erreur, >0 si ok @@ -897,8 +897,8 @@ class Contrat extends CommonObject $sql.= " ".price2num($total_ht).",".price2num($total_tva).",".price2num($total_localtax1).",".price2num($total_localtax2).",".price2num($total_ttc).","; $sql.= " '".$info_bits."',"; $sql.= " ".price2num($price).",".price2num($remise); // TODO A virer - if ($date_start > 0) { $sql.= ",".$this->db->idate($date_start); } - if ($date_end > 0) { $sql.= ",".$this->db->idate($date_end); } + if ($date_start > 0) { $sql.= ",'".$this->db->idate($date_start)."'"; } + if ($date_end > 0) { $sql.= ",'".$this->db->idate($date_end)."'"; } $sql.= ")"; dol_syslog(get_class($this)."::addline sql=".$sql); @@ -949,9 +949,11 @@ class Contrat extends CommonObject * @param float $localtax2tx Local tax 2 rate * @param timestamp $date_debut_reel Date de debut reelle * @param timestamp $date_fin_reel Date de fin reelle + * @param float $price_base_type HT or TTC + * @param int $info_bits Bits de type de lignes * @return int < 0 si erreur, > 0 si ok */ - function updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $tvatx, $localtax1tx=0, $localtax2tx=0, $date_debut_reel='', $date_fin_reel='') + function updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $tvatx, $localtax1tx=0, $localtax2tx=0, $date_debut_reel='', $date_fin_reel='', $price_base_type='HT', $info_bits=0) { global $user, $conf, $langs; @@ -975,30 +977,55 @@ class Contrat extends CommonObject $remise_percent=0; } - dol_syslog(get_class($this)."::UpdateLine $rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $date_debut_reel, $date_fin_reel, $tvatx, $localtax1tx, $localtax2tx"); + dol_syslog(get_class($this)."::updateline $rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $date_debut_reel, $date_fin_reel, $tvatx, $localtax1tx, $localtax2tx, $price_base_type, $info_bits"); $this->db->begin(); + // 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, $localtaxtx1, $txlocaltaxtx2, 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) + $remise = 0; + $price = price2num(round($pu_ht, 2)); + if (dol_strlen($remise_percent) > 0) + { + $remise = round(($pu_ht * $remise_percent / 100), 2); + $price = $pu_ht - $remise; + } + $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet set description='".$this->db->escape($desc)."'"; - $sql .= ",price_ht='" . price2num($price)."'"; - $sql .= ",subprice='" . price2num($subprice)."'"; - $sql .= ",remise='" . price2num($remise)."'"; - $sql .= ",remise_percent='".price2num($remise_percent)."'"; - $sql .= ",qty='$qty'"; - $sql .= ",tva_tx='". price2num($tvatx)."'"; - $sql .= ",localtax1_tx='". price2num($localtax1tx)."'"; - $sql .= ",localtax2_tx='". price2num($localtax2tx)."'"; - if ($date_start > 0) { $sql.= ",date_ouverture_prevue=".$this->db->idate($date_start); } + $sql.= ",price_ht='" . price2num($price)."'"; + $sql.= ",subprice='" . price2num($subprice)."'"; + $sql.= ",remise='" . price2num($remise)."'"; + $sql.= ",remise_percent='".price2num($remise_percent)."'"; + $sql.= ",qty='".$qty."'"; + $sql.= ",tva_tx='". price2num($tvatx)."'"; + $sql.= ",localtax1_tx='". price2num($localtax1tx)."'"; + $sql.= ",localtax2_tx='". price2num($localtax2tx)."'"; + $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)."'"; + if ($date_start > 0) { $sql.= ",date_ouverture_prevue='".$this->db->idate($date_start)."'"; } else { $sql.=",date_ouverture_prevue=null"; } - if ($date_end > 0) { $sql.= ",date_fin_validite=".$this->db->idate($date_end); } + if ($date_end > 0) { $sql.= ",date_fin_validite='".$this->db->idate($date_end)."'"; } else { $sql.=",date_fin_validite=null"; } - if ($date_debut_reel > 0) { $sql.= ",date_ouverture=".$this->db->idate($date_debut_reel); } + if ($date_debut_reel > 0) { $sql.= ",date_ouverture='".$this->db->idate($date_debut_reel)."'"; } else { $sql.=",date_ouverture=null"; } - if ($date_fin_reel > 0) { $sql.= ",date_cloture=".$this->db->idate($date_fin_reel); } + if ($date_fin_reel > 0) { $sql.= ",date_cloture='".$this->db->idate($date_fin_reel)."'"; } else { $sql.=",date_cloture=null"; } $sql .= " WHERE rowid = ".$rowid; - dol_syslog(get_class($this)."::UpdateLine sql=".$sql); + dol_syslog(get_class($this)."::updateline sql=".$sql); $result = $this->db->query($sql); if ($result) { @@ -1011,7 +1038,7 @@ class Contrat extends CommonObject else { $this->db->rollback(); - dol_syslog(get_class($this)."::UpdateLigne Erreur -2"); + dol_syslog(get_class($this)."::updateligne Erreur -2"); return -2; } } @@ -1019,7 +1046,7 @@ class Contrat extends CommonObject { $this->db->rollback(); $this->error=$this->db->error(); - dol_syslog(get_class($this)."::UpdateLigne Erreur -1"); + dol_syslog(get_class($this)."::updateligne Erreur -1"); return -1; } } @@ -1818,6 +1845,17 @@ class ContratLigne // Check parameters // Put here code to add control on parameters values + // 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($this->qty, $this->price_ht, $this->remise_percent, $this->tva_tx, $this->localtax1_tx, $this->localtax2_tx, 0, 'HT', 0); + $this->total_ht = $tabprice[0]; + $this->total_tva = $tabprice[1]; + $this->total_ttc = $tabprice[2]; + $this->total_localtax1= $tabprice[9]; + $this->total_localtax2= $tabprice[10]; + // Update request $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET"; $sql.= " fk_contrat='".$this->fk_contrat."',"; @@ -1825,11 +1863,11 @@ class ContratLigne $sql.= " statut='".$this->statut."',"; $sql.= " label='".$this->db->escape($this->label)."',"; $sql.= " description='".$this->db->escape($this->description)."',"; - $sql.= " date_commande=".($this->date_commande!=''?$this->db->idate($this->date_commande):"null").","; - $sql.= " date_ouverture_prevue=".($this->date_ouverture_prevue!=''?$this->db->idate($this->date_ouverture_prevue):"null").","; - $sql.= " date_ouverture=".($this->date_ouverture!=''?$this->db->idate($this->date_ouverture):"null").","; - $sql.= " date_fin_validite=".($this->date_fin_validite!=''?$this->db->idate($this->date_fin_validite):"null").","; - $sql.= " date_cloture=".($this->date_cloture!=''?$this->db->idate($this->date_cloture):"null").","; + $sql.= " date_commande=".($this->date_commande!=''?"'".$this->db->idate($this->date_commande)."'":"null").","; + $sql.= " date_ouverture_prevue=".($this->date_ouverture_prevue!=''?"'".$this->db->idate($this->date_ouverture_prevue)."'":"null").","; + $sql.= " date_ouverture=".($this->date_ouverture!=''?"'".$this->db->idate($this->date_ouverture)."'":"null").","; + $sql.= " date_fin_validite=".($this->date_fin_validite!=''?"'".$this->db->idate($this->date_fin_validite)."'":"null").","; + $sql.= " date_cloture=".($this->date_cloture!=''?"'".$this->db->idate($this->date_cloture)."'":"null").","; $sql.= " tva_tx='".$this->tva_tx."',"; $sql.= " localtax1_tx='".$this->localtax1_tx."',"; $sql.= " localtax2_tx='".$this->localtax2_tx."',"; @@ -1851,7 +1889,7 @@ class ContratLigne $sql.= " commentaire='".$this->db->escape($this->commentaire)."'"; $sql.= " WHERE rowid=".$this->id; - dol_syslog("ContratLigne::update sql=".$sql, LOG_DEBUG); + dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { @@ -1862,7 +1900,7 @@ class ContratLigne else { $this->error="Error ".$this->db->lasterror(); - dol_syslog("ContratLigne::update ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR); return -1; } @@ -1899,7 +1937,7 @@ class ContratLigne $sql.= ",total_ttc=".price2num($this->total_ttc,'MT').""; $sql.= " WHERE rowid = ".$this->rowid; - dol_syslog("ContratLigne::update_total sql=".$sql); + dol_syslog(get_class($this)."::update_total sql=".$sql); $resql=$this->db->query($sql); if ($resql) @@ -1910,7 +1948,7 @@ class ContratLigne else { $this->error=$this->db->error(); - dol_syslog("ContratLigne::update_total Error ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::update_total Error ".$this->error, LOG_ERR); $this->db->rollback(); return -2; } diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index ac01cfd5bba..922bd3d9f1d 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -1439,18 +1439,19 @@ abstract class CommonObject /** * Update total_ht, total_ttc and total_vat for an object (sum of lines) * - * @param int $exclspec Exclude special product (product_type=9) - * @param int $roundingadjust -1=Use default method (MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND or 0), 0=Use total of rounding, 1=Use rounding of total - * @return int <0 if KO, >0 if OK + * @param int $exclspec Exclude special product (product_type=9) + * @param int $roundingadjust -1=Use default method (MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND or 0), 0=Use total of rounding, 1=Use rounding of total + * @param int $nodatabaseupdate 1=Do not update database. Update only properties of object. + * @return int <0 if KO, >0 if OK */ - function update_price($exclspec=0,$roundingadjust=-1) + function update_price($exclspec=0,$roundingadjust=-1,$nodatabaseupdate=0) { include_once(DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'); if ($roundingadjust < 0 && isset($conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND)) $roundingadjust=$conf->global->MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND; if ($roundingadjust < 0) $roundingadjust=0; - $err=0; + $error=0; // Define constants to find lines to sum $fieldtva='total_tva'; @@ -1462,7 +1463,12 @@ abstract class CommonObject $sql.= ' tva_tx as vatrate'; $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line; $sql.= ' WHERE '.$this->fk_element.' = '.$this->id; - if ($exclspec) $sql.= ' AND product_type <> 9'; + if ($exclspec) + { + $product_field='product_type'; + if ($this->table_element_line == 'contratdet') $product_field=''; // contratdet table has no product_type field + if ($product_field) $sql.= ' AND '.$product_field.' <> 9'; + } dol_syslog(get_class($this)."::update_price sql=".$sql); $resql = $this->db->query($sql); @@ -1537,32 +1543,39 @@ abstract class CommonObject if ($this->element == 'facture_fourn' || $this->element == 'invoice_supplier') $fieldtva='total_tva'; if ($this->element == 'propal') $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; + if (empty($nodatabaseupdate)) + { + $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; - //print "xx".$sql; - dol_syslog(get_class($this)."::update_price sql=".$sql); - $resql=$this->db->query($sql); - if ($resql) + //print "xx".$sql; + dol_syslog(get_class($this)."::update_price sql=".$sql); + $resql=$this->db->query($sql); + if (! $resql) + { + $error++; + $this->error=$this->db->error(); + dol_syslog(get_class($this)."::update_price error=".$this->error,LOG_ERR); + } + } + + if (! $error) { return 1; } else { - $this->error=$this->db->error(); - dol_syslog(get_class($this)."::update_price error=".$this->error,LOG_ERR); return -1; } } else { - $this->error=$this->db->error(); - dol_syslog(get_class($this)."::update_price error=".$this->error,LOG_ERR); + dol_print_error($this->db,'Bad request in update_price'); return -1; } } @@ -2140,7 +2153,7 @@ abstract class CommonObject function setExtraParameters() { $this->db->begin(); - + $extraparams = (! empty($this->extraparams) ? json_encode($this->extraparams) : null); $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element; @@ -2364,7 +2377,7 @@ abstract class CommonObject function printObjectLines($action,$seller,$buyer,$selected=0,$dateSelector=0,$hookmanager=false) { global $conf,$langs; - + print ''; if (! empty($conf->global->MAIN_VIEW_LINE_NUMBER)) { @@ -2380,15 +2393,15 @@ abstract class CommonObject print ' '; print ' '; // No width to allow autodim print "\n"; - + $num = count($this->lines); $var = true; $i = 0; - + foreach ($this->lines as $line) { $var=!$var; - + if (is_object($hookmanager) && (($line->product_type == 9 && ! empty($line->special_code)) || ! empty($line->fk_parent_line))) { if (empty($line->fk_parent_line)) @@ -2401,11 +2414,11 @@ abstract class CommonObject { $this->printObjectLine($action,$line,$var,$num,$i,$dateSelector,$seller,$buyer,$selected,$hookmanager); } - + $i++; } } - + /** * Return HTML content of a detail line * TODO Move this into an output class file (htmlline.class.php) @@ -2428,16 +2441,16 @@ abstract class CommonObject { global $conf,$langs,$user; global $form,$bc,$bcdd; - + $element=$this->element; - + // Show product and description $type=$line->product_type?$line->product_type:$line->fk_product_type; // Try to enhance type detection using date_start and date_end for free lines where type // was not saved. if (! empty($line->date_start)) $type=1; if (! empty($line->date_end)) $type=1; - + // Ligne en mode visu if ($action != 'editline' || $selected != $line->id) { @@ -2445,13 +2458,13 @@ abstract class CommonObject if ($line->fk_product > 0) { $product_static = new Product($this->db); - + // Define output language if (! empty($conf->global->MAIN_MULTILANGS) && ! empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) { $this->fetch_thirdparty(); $prod = new Product($this->db, $line->fk_product); - + $outputlangs = $langs; $newlang=''; if (empty($newlang) && GETPOST('lang_id')) $newlang=GETPOST('lang_id'); @@ -2461,14 +2474,14 @@ abstract class CommonObject $outputlangs = new Translate("",$conf); $outputlangs->setDefaultLang($newlang); } - + $label = (! empty($prod->multilangs[$outputlangs->defaultlang]["libelle"])) ? $prod->multilangs[$outputlangs->defaultlang]["libelle"] : $line->product_label; } else { $label = $line->product_label; } - + $product_static->type=$line->fk_product_type; $product_static->id=$line->fk_product; $product_static->ref=$line->ref; @@ -2476,7 +2489,7 @@ abstract class CommonObject $text=$product_static->getNomUrl(1); $text.= ' - '.$label; $description=($conf->global->PRODUIT_DESC_IN_FORM?'':dol_htmlentitiesbr($line->description)); - + // Use global variables + $seller and $buyer include(DOL_DOCUMENT_ROOT.'/core/tpl/predefinedproductline_view.tpl.php'); } @@ -2486,7 +2499,7 @@ abstract class CommonObject include(DOL_DOCUMENT_ROOT.'/core/tpl/freeproductline_view.tpl.php'); } } - + // Ligne en mode update if ($this->statut == 0 && $action == 'editline' && $selected == $line->id) { diff --git a/htdocs/install/upgrade2.php b/htdocs/install/upgrade2.php index 42c55e82ff3..7efaca24306 100644 --- a/htdocs/install/upgrade2.php +++ b/htdocs/install/upgrade2.php @@ -308,6 +308,8 @@ if (! GETPOST("action") || preg_match('/upgrade/i',GETPOST('action'))) $beforeversionarray=explode('.','3.2.9'); if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0) { + migrate_price_contrat($db,$langs,$conf); + migrate_mode_reglement($db,$langs,$conf); // Reload modules @@ -1460,7 +1462,6 @@ function migrate_price_contrat($db,$langs,$conf) $pu = $obj->subprice; $txtva = $obj->tva_taux; $remise_percent = $obj->remise_percent; - $remise_percent_global = $obj->remise_percent_global; $info_bits = $obj->info_bits; // On met a jour les 3 nouveaux champs @@ -1468,7 +1469,7 @@ function migrate_price_contrat($db,$langs,$conf) //$contratligne->fetch($rowid); Non requis car le update_total ne met a jour que chp redefinis $contratligne->rowid=$rowid; - $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,$remise_percent_global,'HT',$info_bits); + $result=calcul_price_total($qty,$pu,$remise_percent,$txtva,0,0,0,'HT',$info_bits); $total_ht = $result[0]; $total_tva = $result[1]; $total_ttc = $result[2]; @@ -1477,30 +1478,10 @@ function migrate_price_contrat($db,$langs,$conf) $contratligne->total_tva = $total_tva; $contratligne->total_ttc = $total_ttc; - dolibarr_install_syslog("upgrade2: Line $rowid: contratdetid=$obj->rowid pu=$pu qty=$qty tva_taux=$txtva remise_percent=$remise_percent remise_global=$remise_percent_global -> $total_ht, $total_tva, $total_ttc"); + dolibarr_install_syslog("upgrade2: Line $rowid: contratdetid=$obj->rowid pu=$pu qty=$qty tva_taux=$txtva remise_percent=$remise_percent -> $total_ht, $total_tva, $total_ttc"); print ". "; $contratligne->update_total($rowid); - - /* On touche pas a contrat mere - $propal = new Propal($db); - $propal->id=$obj->rowid; - if ( $propal->fetch($propal->id) >= 0 ) - { - if ( $propal->update_price() > 0 ) - { - print ". "; - } - else - { - print "Error id=".$propal->id; - } - } - else - { - print "Error #3"; - } - */ $i++; } }