From c3adbb11fe640140263c606f125924772efec4b3 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 25 Jan 2009 17:05:40 +0000 Subject: [PATCH] Fix: Error in price when adding a product in contract with base type that is with tax. --- htdocs/contrat/contrat.class.php | 51 ++++++++-------- htdocs/contrat/fiche.php | 102 +++++++++++++++---------------- htdocs/lib/price.lib.php | 8 +-- 3 files changed, 82 insertions(+), 79 deletions(-) diff --git a/htdocs/contrat/contrat.class.php b/htdocs/contrat/contrat.class.php index ec32bbde9cc..012486cc85b 100644 --- a/htdocs/contrat/contrat.class.php +++ b/htdocs/contrat/contrat.class.php @@ -68,7 +68,7 @@ class Contrat extends CommonObject var $fk_projet; var $lignes=array(); - + /** * \brief Constructeur de la classe @@ -178,7 +178,7 @@ class Contrat extends CommonObject function cloture($user,$langs='',$conf='') { $this->db->begin(); - + // Load lines $this->fetch_lignes(); @@ -199,7 +199,7 @@ class Contrat extends CommonObject } } } - + if ($ok) { $this->db->commit(); @@ -368,14 +368,14 @@ class Contrat extends CommonObject $ligne->fk_user_author = $objp->fk_user_author; $ligne->fk_user_ouverture= $objp->fk_user_ouverture; $ligne->fk_user_cloture = $objp->fk_user_cloture; - + $ligne->ref = $objp->ref; $ligne->libelle = $objp->label; // Label produit $ligne->label = $objp->label; // For backward compatibility $ligne->product_desc = $objp->product_desc; // Description produit $ligne->description = $objp->description; - + $ligne->date_ouverture_prevue = $this->db->jdate($objp->date_ouverture_prevue); $ligne->date_ouverture = $this->db->jdate($objp->date_ouverture); $ligne->date_fin_validite = $this->db->jdate($objp->date_fin_validite); @@ -385,7 +385,7 @@ class Contrat extends CommonObject $ligne->date_debut_reel = $this->db->jdate($objp->date_ouverture); $ligne->date_fin_prevue = $this->db->jdate($objp->date_fin_validite); $ligne->date_fin_reel = $this->db->jdate($objp->date_cloture); - + $this->lignes[] = $ligne; //dolibarr_syslog("1 ".$ligne->desc); //dolibarr_syslog("2 ".$ligne->product_desc); @@ -428,7 +428,7 @@ class Contrat extends CommonObject while ($i < $num) { $objp = $this->db->fetch_object($result); - + $ligne = new ContratLigne($this->db); $ligne->id = $objp->rowid; $ligne->fk_contrat = $objp->fk_contrat; @@ -447,7 +447,7 @@ class Contrat extends CommonObject $ligne->total_ttc = $objp->total_ttc; $ligne->fk_product = 0; $ligne->info_bits = $objp->info_bits; - + $ligne->fk_user_author = $objp->fk_user_author; $ligne->fk_user_ouverture= $objp->fk_user_ouverture; $ligne->fk_user_cloture = $objp->fk_user_cloture; @@ -605,13 +605,13 @@ class Contrat extends CommonObject $sql.= " AND tc.element='".$this->element."'"; $sql.= " AND ec.element_id=".$this->id; */ - + $sql = "SELECT ec.rowid as ecrowid"; $sql.= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc"; $sql.= " WHERE ec.fk_c_type_contact = tc.rowid"; $sql.= " AND tc.element='".$this->element."'"; $sql.= " AND ec.element_id=".$this->id; - + dolibarr_syslog("Contrat::delete element_contact sql=".$sql,LOG_DEBUG); $resql=$this->db->query($sql); if (! $resql) @@ -654,7 +654,7 @@ class Contrat extends CommonObject $sql = "SELECT cdl.rowid as cdlrowid "; $sql.= " FROM ".MAIN_DB_PREFIX."contratdet_log as cdl, ".MAIN_DB_PREFIX."contratdet as cd"; $sql.= " WHERE cdl.fk_contratdet=cd.rowid AND cd.fk_contrat=".$this->id; - + dolibarr_syslog("Contrat::delete contratdet_log sql=".$sql, LOG_DEBUG); $resql=$this->db->query($sql); if (! $resql) @@ -675,7 +675,7 @@ class Contrat extends CommonObject $sql= "DELETE FROM ".MAIN_DB_PREFIX."contratdet_log "; $sql.= " WHERE ".MAIN_DB_PREFIX."contratdet_log.rowid IN (".implode(",",$tab_resql).")"; - + dolibarr_syslog("Contrat::delete contratdet_log sql=".$sql, LOG_DEBUG); $resql=$this->db->query($sql); if (! $resql) @@ -706,7 +706,7 @@ class Contrat extends CommonObject // Delete contrat $sql = "DELETE FROM ".MAIN_DB_PREFIX."contrat"; $sql.= " WHERE rowid=".$this->id; - + dolibarr_syslog("Contrat::delete contrat sql=".$sql); $resql=$this->db->query($sql); if (! $resql) @@ -762,7 +762,7 @@ class Contrat extends CommonObject if ($this->statut == 0 || ($this->statut == 1 && $conf->global->CONTRAT_EDITWHENVALIDATED)) { $this->db->begin(); - + // Clean parameters $remise_percent=price2num($remise_percent); $qty=price2num($qty); @@ -781,7 +781,7 @@ class Contrat extends CommonObject { $pu=$pu_ttc; } - + // 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 @@ -794,11 +794,11 @@ class Contrat extends CommonObject // \TODO A virer // Anciens indicateurs: $price, $remise (a ne plus utiliser) $remise = 0; - $price = price2num(round($pu, 2)); + $price = price2num(round($pu_ht, 2)); if (strlen($remise_percent) > 0) { - $remise = round(($pu * $remise_percent / 100), 2); - $price = $pu - $remise; + $remise = round(($pu_ht * $remise_percent / 100), 2); + $price = $pu_ht - $remise; } // Insertion dans la base @@ -814,7 +814,7 @@ class Contrat extends CommonObject $sql.= ($fk_product>0 ? $fk_product : "null").","; $sql.= " '".$qty."',"; $sql.= " '".$txtva."',"; - $sql.= " ".price2num($remise_percent).",".price2num($pu).","; + $sql.= " ".price2num($remise_percent).",".price2num($pu_ht).","; $sql.= " ".price2num($total_ht).",".price2num($total_tva).",".price2num($total_ttc).","; $sql.= " '".$info_bits."',"; $sql.= " ".price2num($price).",".price2num( $remise); // \TODO A virer @@ -1155,7 +1155,7 @@ class Contrat extends CommonObject $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as cd"; $sql.= " WHERE fk_contrat =".$this->id; if ($statut >= 0) $sql.= " AND statut = '$statut'"; - + dolibarr_syslog("Contrat::array_detail() sql=".$sql,LOG_DEBUG); $resql=$this->db->query($sql); if ($resql) @@ -1226,7 +1226,7 @@ class Contrat extends CommonObject global $conf, $user; $now=gmmktime(); - + $this->nbtodo=$this->nbtodolate=0; if ($mode == 'inactives') { @@ -1360,11 +1360,14 @@ class ContratLigne var $remise_percent; var $remise; var $fk_remise_except; - var $subprice; + + var $subprice; // Unit price HT var $price_ht; + var $total_ht; var $total_tva; var $total_ttc; + var $info_bits; var $fk_user_author; var $fk_user_ouverture; @@ -1567,7 +1570,7 @@ class ContratLigne function update($user, $notrigger=0) { global $conf, $langs; - + // Clean parameters $this->fk_contrat=trim($this->fk_contrat); $this->fk_product=trim($this->fk_product); @@ -1589,7 +1592,7 @@ class ContratLigne $this->fk_user_ouverture=trim($this->fk_user_ouverture); $this->fk_user_cloture=trim($this->fk_user_cloture); $this->commentaire=trim($this->commentaire); - + // Check parameters // Put here code to add control on parameters values diff --git a/htdocs/contrat/fiche.php b/htdocs/contrat/fiche.php index 8a9913b337b..bd276da79b1 100644 --- a/htdocs/contrat/fiche.php +++ b/htdocs/contrat/fiche.php @@ -1,6 +1,6 @@ - * Copyright (C) 2004-2008 Laurent Destailleur + * Copyright (C) 2004-2009 Laurent Destailleur * Copyright (C) 2006 Andre Cianfarani * 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 @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - + /** * \file htdocs/contrat/fiche.php * \ingroup contrat @@ -44,6 +44,7 @@ $result=restrictedArea($user,'contrat',$contratid,'contrat'); $usehm=$conf->global->MAIN_USE_HOURMIN_IN_DATE_RANGE; + /* * Actions */ @@ -61,7 +62,7 @@ if ($_POST["action"] == 'confirm_active' && $_POST["confirm"] == 'yes' && $user- exit; } else { - $mesg=$contrat->error; + $mesg=$contrat->error; } } @@ -77,7 +78,7 @@ if ($_POST["action"] == 'confirm_closeline' && $_POST["confirm"] == 'yes' && $us exit; } else { - $mesg=$contrat->error; + $mesg=$contrat->error; } } @@ -183,7 +184,7 @@ if ($_POST["action"] == 'addligne' && $user->rights->contrat->creer) exit; } $ret=$contrat->fetch_client(); - + $date_start=''; $date_end=''; // Si ajout champ produit libre @@ -211,8 +212,6 @@ if ($_POST["action"] == 'addligne' && $user->rights->contrat->creer) } } - $price_base_type = 'HT'; - // Ecrase $pu par celui du produit // Ecrase $desc par celui du produit // Ecrase $txtva par celui du produit @@ -252,7 +251,7 @@ if ($_POST["action"] == 'addligne' && $user->rights->contrat->creer) $pu_ttc = price2num($pu_ht * (1 + ($tva_tx/100)), 'MU'); } } - + $desc = $prod->description; $desc.= $prod->description && $_POST['desc'] ? "\n" : ""; $desc.= $_POST['desc']; @@ -260,6 +259,7 @@ if ($_POST["action"] == 'addligne' && $user->rights->contrat->creer) else { $pu_ht=$_POST['pu']; + $price_base_type = 'HT'; $tva_tx=eregi_replace('\*','',$_POST['tva_tx']); $tva_npr=eregi('\*',$_POST['tva_tx'])?1:0; $desc=$_POST['desc']; @@ -267,7 +267,7 @@ if ($_POST["action"] == 'addligne' && $user->rights->contrat->creer) $info_bits=0; if ($tva_npr) $info_bits |= 0x01; - + // Insert line $result = $contrat->addline( $desc, @@ -282,7 +282,7 @@ if ($_POST["action"] == 'addligne' && $user->rights->contrat->creer) $pu_ttc, $info_bits ); - + if ($result > 0) { /* @@ -308,10 +308,10 @@ if ($_POST["action"] == 'updateligne' && $user->rights->contrat->creer && ! $_PO if ($contratline->fetch($_POST["elrowid"])) { $db->begin(); - + if ($date_start_real_update == '') $date_start_real_update=$contratline->date_ouverture; if ($date_end_real_update == '') $date_end_real_update=$contratline->date_cloture; - + $contratline->description=$_POST["eldesc"]; $contratline->price_ht=$_POST["elprice"]; $contratline->subprice=$_POST["elprice"]; @@ -324,7 +324,7 @@ if ($_POST["action"] == 'updateligne' && $user->rights->contrat->creer && ! $_PO $contratline->date_fin_validite=$date_end_update; $contratline->date_cloture=$date_end_real_update; $contratline->fk_user_cloture=$user->id; - + $result=$contratline->update($user); if ($result > 0) { @@ -334,7 +334,7 @@ if ($_POST["action"] == 'updateligne' && $user->rights->contrat->creer && ! $_PO { dolibarr_print_error($db,'Failed to update contrat_det'); $db->rollback(); - } + } } else { @@ -471,7 +471,7 @@ if ($_GET["action"] == 'create') // Ref print ''.$langs->trans("Ref").''; print ''; - + // Customer print ''.$langs->trans("Customer").''.$soc->getNomUrl(1).''; @@ -485,7 +485,7 @@ if ($_GET["action"] == 'create') else print $langs->trans("CompanyHasNoAbsoluteDiscount"); print '.'; print ''; - + // Commercial suivi print ''.$langs->trans("TypeContact_contrat_internal_SALESREPFOLL").''; print $form->select_users('','commercial_suivi_id',1,''); @@ -507,7 +507,7 @@ if ($_GET["action"] == 'create') $form->select_array("projetid",$proj->liste_array($soc->id),0,1); print ""; } - + print ''.$langs->trans("NotePublic").''; print ''; @@ -516,7 +516,7 @@ if ($_GET["action"] == 'create') print ''.$langs->trans("NotePrivate").''; print ''; } - + print ''; print "\n"; @@ -595,7 +595,7 @@ if ($_GET["action"] == 'create') { dolibarr_print_error($db); } - + print ''; } else @@ -606,7 +606,7 @@ else /* *************************************************************************** */ { $now=gmmktime(); - + $id = $_GET["id"]; if ($id > 0) { @@ -620,13 +620,13 @@ else } if ($mesg) print $mesg; - + $nbofservices=sizeof($contrat->lignes); $author = new User($db); $author->id = $contrat->user_author_id; $author->fetch(); - + $commercial_signature = new User($db); $commercial_signature->id = $contrat->commercial_signature_id; $commercial_signature->fetch(); @@ -741,12 +741,12 @@ else } echo '
'; - + $servicepos=(isset($_REQUEST["servicepos"])?$_REQUEST["servicepos"]:1); $colorb='333333'; $arrayothercontracts=$contrat->getListOfContracts('others'); - + /* * Lignes de contrats */ @@ -791,7 +791,7 @@ else $var=true; $objp = $db->fetch_object($result); - + $var=!$var; if ($_GET["action"] != 'editline' || $_GET["rowid"] != $objp->rowid) @@ -855,7 +855,7 @@ else print ''; } print ''; - + print "\n"; // Dates de en service prevues et effectives @@ -863,7 +863,7 @@ else { print ''; print ''; - + // Date planned print $langs->trans("DateStartPlanned").': '; if ($objp->date_debut) { @@ -879,10 +879,10 @@ else if ($objp->statut == 4 && $objp->date_fin < ($now - $conf->contrat->services->inactifs->warning_delay)) { print " ".img_warning($langs->trans("Late")); } } else print $langs->trans("Unknown"); - + print ''; print ''; - } + } } // Ligne en mode update else @@ -926,21 +926,21 @@ else print "\n"; } - + $db->free($result); } else { dolibarr_print_error($db); } - + if ($contrat->statut > 0) { print ''; print '
'; print "\n"; } - + print ""; @@ -960,11 +960,11 @@ else $formquestion=array( 'text' => $langs->trans("ConfirmMoveToAnotherContractQuestion"), array('type' => 'select', 'name' => 'newcid', 'values' => $arraycontractid)); - + $html->form_confirm($_SERVER["PHP_SELF"]."?id=".$contrat->id."&lineid=".$_GET["rowid"],$langs->trans("MoveToAnotherContract"),$langs->trans("ConfirmMoveToAnotherContract"),"confirm_move",$formquestion); print '
'; } - + /* * Confirmation de la validation activation */ @@ -987,8 +987,8 @@ else $dateactend = dolibarr_mktime(12, 0 , 0, $_POST["endmonth"], $_POST["endday"], $_POST["endyear"]); $html->form_confirm($_SERVER["PHP_SELF"]."?id=".$contrat->id."&ligne=".$_GET["ligne"]."&date=".$dateactstart."&dateend=".$dateactend,$langs->trans("CloseService"),$langs->trans("ConfirmCloseService",dolibarr_print_date($dateactend,"%A %d %B %Y")),"confirm_closeline"); print '
'; - } - + } + // Area with activation info if ($contrat->statut > 0) { @@ -1012,7 +1012,7 @@ else print "\n"; print ''; - + print ''; // Si pas encore active if (! $objp->date_debut_reelle) { @@ -1077,7 +1077,7 @@ else print ''.$langs->trans("DateEndPlanned").''; print $html->select_date($dateactend,"end",$usehm,$usehm,'',"active"); print ''; - + print ''; print '
'; print ''; @@ -1139,16 +1139,16 @@ else print ''; print ''; - } - + } + print ''; // End td if line is 1 - + print ''; print ' '; $cursorline++; } print ''; - + /* * Ajouter une ligne produit/service */ @@ -1157,7 +1157,7 @@ else { print '
'; print ''; // Array with (n*2)+1 lines - + print ""; print ''; print ''; @@ -1191,7 +1191,7 @@ else print ''; print ''; print ''."\n"; - + print ""; print ''; print ''; - + print ''; $var=!$var; - + // Service libre print ''; print ''; @@ -1232,18 +1232,18 @@ else $form->select_date('',"date_end_sl",$usehm,$usehm,1,"addligne_sl"); print ''; print ''; - + print ''; print '
'.$langs->trans("Service").''.$langs->trans("VAT").'%
'; print $langs->trans("DateStartPlanned").' '; @@ -1200,11 +1200,11 @@ else $form->select_date('',"date_end",$usehm,$usehm,1,"addligne"); print '
'; } - - + + //print ''; //print ''; - + print ''; @@ -1279,7 +1279,7 @@ else // print ''.$langs->trans("Close").''; //} } - + // On peut supprimer entite si // - Droit de creer + mode brouillon (erreur creation) // - Droit de supprimer diff --git a/htdocs/lib/price.lib.php b/htdocs/lib/price.lib.php index 70069cf092c..2e7dca9deed 100644 --- a/htdocs/lib/price.lib.php +++ b/htdocs/lib/price.lib.php @@ -28,7 +28,7 @@ /** \brief Permet de calculer les parts total HT, TVA et TTC d'une ligne de facture, propale, commande ou autre depuis: - quantity, unit price, remise_percent_ligne, txtva, remise_percent_global + quantity, unit price, remise_percent_ligne, txtva, remise_percent_global, price_base_type, info_bits \param qty Quantity \param pu Prix unitaire (HT ou TTC selon price_base_type) \param remise_percent_ligne Remise ligne @@ -41,7 +41,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $remise_percent_global=0, $price_base_type='HT', $info_bits=0) { global $conf; - + $result=array(); //dolibarr_syslog("price.lib::calcul_price_total $qty, $pu, $remise_percent_ligne, $txtva, $price_base_type $info_bits"); @@ -53,11 +53,11 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $remise_pe $tot_avec_remise = $tot_avec_remise_ligne * ( 1 - ($remise_percent_global / 100)); $result[0] = price2num($tot_avec_remise, 'MT'); $result[2] = price2num($tot_avec_remise * ( 1 + ( (($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non - $result2bis= price2num($tot_avec_remise * ( 1 + ( $txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) + $result2bis= price2num($tot_avec_remise * ( 1 + ( $txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR) $result[1] = $result2bis - $result[0]; $result[3] = price2num($pu, 'MU'); $result[5] = price2num($pu * ( 1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non - $result5bis= price2num($pu * ( 1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR) + $result5bis= price2num($pu * ( 1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR) $result[4] = $result5bis - $result[3]; } else