From 2b4d773f66a8d1ce184c2c1d0e5276cd9cded4cd Mon Sep 17 00:00:00 2001 From: Rodolphe Quiedeville Date: Fri, 1 Dec 2006 21:35:44 +0000 Subject: [PATCH] Mets a jour les infos de stock des produits --- htdocs/commande/commande.class.php | 2468 ++++++++++++++-------------- 1 file changed, 1253 insertions(+), 1215 deletions(-) diff --git a/htdocs/commande/commande.class.php b/htdocs/commande/commande.class.php index de4db2de968..39000e74103 100644 --- a/htdocs/commande/commande.class.php +++ b/htdocs/commande/commande.class.php @@ -1,5 +1,5 @@ +/* Copyright (C) 2003-2006 Rodolphe Quiedeville * Copyright (C) 2004-2006 Laurent Destailleur * Copyright (C) 2005-2006 Regis Houssin * Copyright (C) 2006 Andre Cianfarani @@ -23,144 +23,144 @@ */ /** - \file htdocs/commande/commande.class.php - \ingroup commande - \brief Fichier des classes de commandes - \version $Revision$ + \file htdocs/commande/commande.class.php + \ingroup commande + \brief Fichier des classes de commandes + \version $Revision$ */ -require_once(DOL_DOCUMENT_ROOT ."/commonobject.class.php"); +require_once(DOL_DOCUMENT_ROOT."/commonobject.class.php"); require_once(DOL_DOCUMENT_ROOT."/product.class.php"); -require_once(DOL_DOCUMENT_ROOT ."/contact.class.php"); +require_once(DOL_DOCUMENT_ROOT."/contact.class.php"); /** - \class Commande - \brief Classe de gestion de commande + \class Commande + \brief Classe de gestion de commande */ class Commande extends CommonObject { - var $db ; - var $element='commande'; - - var $id ; - - var $socid; // Id client - var $client; // Objet societe client (à charger par fetch_client) - - var $ref; - var $ref_client; - var $contactid; - var $projet_id; - var $statut; - var $facturee; - var $brouillon; - var $cond_reglement_id; - var $cond_reglement_code; - var $mode_reglement_id; - var $mode_reglement_code; - var $adresse_livraison_id; - var $adresse; - var $date; // Date commande - var $date_livraison; // Date livraison souhaitée - var $remise_percent; - var $remise_absolue; - var $modelpdf; - - var $lines = array(); - - // Pour board - var $nbtodo; - var $nbtodolate; - - /** - * \brief Constructeur - * \param DB Handler d'accès base - */ - function Commande($DB, $socid="", $commandeid=0) - { - global $langs; - $langs->load('orders'); - $this->db = $DB; - $this->socid = $socid; - $this->id = $commandeid; - - $this->sources[0] = $langs->trans('OrderSource0'); - $this->sources[1] = $langs->trans('OrderSource1'); - $this->sources[2] = $langs->trans('OrderSource2'); - $this->sources[3] = $langs->trans('OrderSource3'); - $this->sources[4] = $langs->trans('OrderSource4'); - $this->sources[5] = $langs->trans('OrderSource5'); - - $this->remise = 0; - $this->remise_percent = 0; - - $this->products = array(); - } - - /** \brief Créé la commande depuis une propale existante - \param user Utilisateur qui crée - \param propale_id id de la propale qui sert de modèle - */ - function create_from_propale($user, $propale_id) - { - dolibarr_syslog("Commande.class.php::create_from_propale propale_id=$propale_id"); - - $propal = new Propal($this->db); - $propal->fetch($propale_id); - - $this->lines = array(); - $this->date_commande = time(); - $this->source = 0; - for ($i = 0 ; $i < sizeof($propal->lignes) ; $i++) - { - $CommLigne = new CommandeLigne($this->db); - $CommLigne->libelle = $propal->lignes[$i]->libelle; - $CommLigne->desc = $propal->lignes[$i]->desc; - $CommLigne->price = $propal->lignes[$i]->price; - $CommLigne->subprice = $propal->lignes[$i]->subprice; - $CommLigne->tva_tx = $propal->lignes[$i]->tva_tx; - $CommLigne->qty = $propal->lignes[$i]->qty; - $CommLigne->remise_percent = $propal->lignes[$i]->remise_percent; - $CommLigne->fk_product = $propal->lignes[$i]->fk_product; - $this->lines[$i] = $CommLigne; - } - - $this->socid = $propal->socid; - $this->projetid = $propal->projetidp; - $this->cond_reglement_id = $propal->cond_reglement_id; - $this->mode_reglement_id = $propal->mode_reglement_id; - $this->date_livraison = $propal->date_livraison; - $this->adresse_livraison_id = $propal->adresse_livraison_id; - $this->contact_id = $propal->contactid; - $this->ref_client = $propal->ref_client; + var $db ; + var $element='commande'; + + var $id ; + + var $socid; // Id client + var $client; // Objet societe client (à charger par fetch_client) + + var $ref; + var $ref_client; + var $contactid; + var $projet_id; + var $statut; + var $facturee; + var $brouillon; + var $cond_reglement_id; + var $cond_reglement_code; + var $mode_reglement_id; + var $mode_reglement_code; + var $adresse_livraison_id; + var $adresse; + var $date; // Date commande + var $date_livraison; // Date livraison souhaitée + var $remise_percent; + var $remise_absolue; + var $modelpdf; + + var $lines = array(); + + // Pour board + var $nbtodo; + var $nbtodolate; + + /** + * \brief Constructeur + * \param DB Handler d'accès base + */ + function Commande($DB, $socid="", $commandeid=0) + { + global $langs; + $langs->load('orders'); + $this->db = $DB; + $this->socid = $socid; + $this->id = $commandeid; - /* Définit la société comme un client */ - $soc = new Societe($this->db); - $soc->id = $this->socid; - $soc->set_as_client(); - $this->propale_id = $propal->id; + $this->sources[0] = $langs->trans('OrderSource0'); + $this->sources[1] = $langs->trans('OrderSource1'); + $this->sources[2] = $langs->trans('OrderSource2'); + $this->sources[3] = $langs->trans('OrderSource3'); + $this->sources[4] = $langs->trans('OrderSource4'); + $this->sources[5] = $langs->trans('OrderSource5'); + + $this->remise = 0; + $this->remise_percent = 0; + + $this->products = array(); + } + + /** \brief Créé la commande depuis une propale existante + \param user Utilisateur qui crée + \param propale_id id de la propale qui sert de modèle + */ + function create_from_propale($user, $propale_id) + { + dolibarr_syslog("Commande.class.php::create_from_propale propale_id=$propale_id"); + + $propal = new Propal($this->db); + $propal->fetch($propale_id); + + $this->lines = array(); + $this->date_commande = time(); + $this->source = 0; + for ($i = 0 ; $i < sizeof($propal->lignes) ; $i++) + { + $CommLigne = new CommandeLigne($this->db); + $CommLigne->libelle = $propal->lignes[$i]->libelle; + $CommLigne->desc = $propal->lignes[$i]->desc; + $CommLigne->price = $propal->lignes[$i]->price; + $CommLigne->subprice = $propal->lignes[$i]->subprice; + $CommLigne->tva_tx = $propal->lignes[$i]->tva_tx; + $CommLigne->qty = $propal->lignes[$i]->qty; + $CommLigne->remise_percent = $propal->lignes[$i]->remise_percent; + $CommLigne->fk_product = $propal->lignes[$i]->fk_product; + $this->lines[$i] = $CommLigne; + } - return $this->create($user); - } + $this->socid = $propal->socid; + $this->projetid = $propal->projetidp; + $this->cond_reglement_id = $propal->cond_reglement_id; + $this->mode_reglement_id = $propal->mode_reglement_id; + $this->date_livraison = $propal->date_livraison; + $this->adresse_livraison_id = $propal->adresse_livraison_id; + $this->contact_id = $propal->contactid; + $this->ref_client = $propal->ref_client; + + /* Définit la société comme un client */ + $soc = new Societe($this->db); + $soc->id = $this->socid; + $soc->set_as_client(); + $this->propale_id = $propal->id; + + return $this->create($user); + } - /** - * \brief Renvoie la référence de commande suivante non utilisée en fonction du module - * de numérotation actif défini dans COMMANDE_ADDON - * \param soc objet societe - * \return string reference libre pour la commande - */ - function getNextNumRef($soc) - { - global $db, $langs, $conf; - $langs->load("order"); + /** + * \brief Renvoie la référence de commande suivante non utilisée en fonction du module + * de numérotation actif défini dans COMMANDE_ADDON + * \param soc objet societe + * \return string reference libre pour la commande + */ + function getNextNumRef($soc) + { + global $db, $langs, $conf; + $langs->load("order"); - $dir = DOL_DOCUMENT_ROOT . "/includes/modules/commande"; + $dir = DOL_DOCUMENT_ROOT . "/includes/modules/commande"; - if (defined("COMMANDE_ADDON") && COMMANDE_ADDON) - { - $file = COMMANDE_ADDON.".php"; + if (defined("COMMANDE_ADDON") && COMMANDE_ADDON) + { + $file = COMMANDE_ADDON.".php"; // Chargement de la classe de numérotation $classname = $conf->global->COMMANDE_ADDON; @@ -172,12 +172,12 @@ class Commande extends CommonObject if ( $numref != "") { - return $numref; + return $numref; } else { - dolibarr_print_error($db,"Commande::getNextNumRef ".$obj->error); - return ""; + dolibarr_print_error($db,"Commande::getNextNumRef ".$obj->error); + return ""; } } else @@ -367,60 +367,60 @@ class Commande extends CommonObject } } - /** - * \brief Créé la commande - * \param user Objet utilisateur qui crée - */ - function create($user) - { - global $conf,$langs,$mysoc; - - // Nettoyage parametres - $this->brouillon = 1; // On positionne en mode brouillon la commande - - dolibarr_syslog("Commande.class.php::create"); - - // Vérification paramètres - if ($this->source < 0) - { - $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Source")); - dolibarr_syslog("Commande.class.php::create ".$this->error, LOG_ERR); - return -1; - } - if (! $remise) $remise=0; - if (! $this->projetid) $this->projetid = 0; - - $soc = new Societe($this->db); - $result=$soc->fetch($this->socid); - if ($result < 0) - { - $this->error="Failed to fetch company"; - dolibarr_syslog("Commande.class.php::create ".$this->error, LOG_ERR); - return -2; - } - - $this->db->begin(); - - $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commande ('; - $sql.= 'fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_public, ref_client,'; - $sql.= ' model_pdf, fk_cond_reglement, fk_mode_reglement, date_livraison, fk_adresse_livraison,'; - $sql.= ' remise_absolue, remise_percent)'; - $sql.= ' VALUES ('.$this->socid.', now(), '.$user->id.', '.$this->projetid.','; - $sql.= ' '.$this->db->idate($this->date_commande).','; - $sql.= ' '.$this->source.', '; - $sql.= " '".addslashes($this->note)."', "; - $sql.= " '".addslashes($this->ref_client)."', '".$this->modelpdf."', '".$this->cond_reglement_id."', '".$this->mode_reglement_id."',"; - $sql.= " '".($this->date_livraison?$this->db->idate($this->date_livraison):'null')."',"; - $sql.= " '".$this->adresse_livraison_id."',"; - $sql.= " '".$this->remise_absolue."',"; - $sql.= " '".$this->remise_percent."')"; - - dolibarr_syslog("Commande.class.php::create sql=".$sql); - - $resql=$this->db->query($sql); - if ($resql) - { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'commande'); + /** + * \brief Créé la commande + * \param user Objet utilisateur qui crée + */ + function create($user) + { + global $conf,$langs,$mysoc; + + // Nettoyage parametres + $this->brouillon = 1; // On positionne en mode brouillon la commande + + dolibarr_syslog("Commande.class.php::create"); + + // Vérification paramètres + if ($this->source < 0) + { + $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Source")); + dolibarr_syslog("Commande.class.php::create ".$this->error, LOG_ERR); + return -1; + } + if (! $remise) $remise=0; + if (! $this->projetid) $this->projetid = 0; + + $soc = new Societe($this->db); + $result=$soc->fetch($this->socid); + if ($result < 0) + { + $this->error="Failed to fetch company"; + dolibarr_syslog("Commande.class.php::create ".$this->error, LOG_ERR); + return -2; + } + + $this->db->begin(); + + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commande ('; + $sql.= 'fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_public, ref_client,'; + $sql.= ' model_pdf, fk_cond_reglement, fk_mode_reglement, date_livraison, fk_adresse_livraison,'; + $sql.= ' remise_absolue, remise_percent)'; + $sql.= ' VALUES ('.$this->socid.', now(), '.$user->id.', '.$this->projetid.','; + $sql.= ' '.$this->db->idate($this->date_commande).','; + $sql.= ' '.$this->source.', '; + $sql.= " '".addslashes($this->note)."', "; + $sql.= " '".addslashes($this->ref_client)."', '".$this->modelpdf."', '".$this->cond_reglement_id."', '".$this->mode_reglement_id."',"; + $sql.= " '".($this->date_livraison?$this->db->idate($this->date_livraison):'null')."',"; + $sql.= " '".$this->adresse_livraison_id."',"; + $sql.= " '".$this->remise_absolue."',"; + $sql.= " '".$this->remise_percent."')"; + + dolibarr_syslog("Commande.class.php::create sql=".$sql); + + $resql=$this->db->query($sql); + if ($resql) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'commande'); if ($this->id) { @@ -503,558 +503,558 @@ class Commande extends CommonObject } - /** - * \brief Ajout d'un produit dans la commande, en base - * \param commandeid id de la commande - * \param desc description de la ligne - * \param pu prix unitaire - * \param qty quantité - * \param txtva taux de tva forcé, sinon -1 - * \param fk_product id du produit/service predéfini - * \param remise_percent pourcentage de remise de la ligne - * \return int >0 si ok, <0 si ko - * \see add_product - * \remarks Les parametres sont deja censé etre juste et avec valeurs finales a l'appel - * de cette methode. Aussi, pour le taux tva, il doit deja avoir ete défini - * par l'appelant par la methode get_default_tva(societe_vendeuse,societe_acheteuse,taux_produit) - * et le desc doit deja avoir la bonne valeur (a l'appelant de gerer le multilangue) - */ - function addline($commandeid, $desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0) - { - dolibarr_syslog("Commande.class.php::addline this->id=$this->id, $commandeid, $desc, $pu, $qty, $txtva, $fk_product, $remise_percent"); - include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); - - if ($this->statut == 0) - { - $this->db->begin(); - - // Nettoyage paramètres - $remise_percent=price2num($remise_percent); - $qty=price2num($qty); - if (! $qty) $qty=1; - $pu = price2num($pu); - $txtva = price2num($txtva); - - // 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); - $total_ht = $tabprice[0]; - $total_tva = $tabprice[1]; - $total_ttc = $tabprice[2]; - - // Anciens indicateurs: $price, $remise (a ne plus utiliser) - $price = $pu; - if ($remise_percent > 0) - { - $remise = round(($pu * $remise_percent / 100), 2); - $price = $pu - $remise; - } - - // Insertion ligne - $ligne=new CommandeLigne($this->db); - - $ligne->fk_commande=$commandeid; - $ligne->desc=$desc; - $ligne->qty=$qty; - $ligne->tva_tx=$txtva; - $ligne->fk_product=$fk_product; - $ligne->remise_percent=$remise_percent; - $ligne->subprice=$pu; - $ligne->rang=-1; - $ligne->info_bits=$info_bits; - $ligne->total_ht=$total_ht; - $ligne->total_tva=$total_tva; - $ligne->total_ttc=$total_ttc; - - // Ne plus utiliser - $ligne->price=$price; - $ligne->remise=$remise; - - $result=$ligne->insert(); - if ($result > 0) - { - // Mise a jour informations denormalisees au niveau de la facture meme - $result=$this->update_price($this->id); - - if ($result > 0) - { - $this->db->commit(); - return 1; - } - else - { - $this->error=$this->db->error(); - dolibarr_syslog("Error sql=$sql, error=".$this->error); - $this->db->rollback(); - return -1; - } - } - else - { - $this->error=$ligne->error; - $this->db->rollback(); - return -2; - } - } - } - - - /** - * \brief Ajoute une ligne dans tableau lines - * \param idproduct Id du produit à ajouter - * \param qty Quantité - * \remise_percent remise_percent Remise relative effectuée sur le produit - * \return void - * \remarks $this->client doit etre chargé - * \TODO Remplacer les appels a cette fonction par generation objet Ligne - * inséré dans tableau $this->products - */ - function add_product($idproduct, $qty, $remise_percent=0) - { - global $conf, $mysoc; - - if (! $qty) $qty = 1; - - if ($idproduct > 0) - { - $prod=new Product($this->db); - $prod->fetch($idproduct); - - $tva_tx = get_default_tva($mysoc,$this->client,$prod->tva_tx); - // multiprix - if($conf->global->PRODUIT_MULTIPRICES == 1) - $price = $prod->multiprices[$this->client->price_level]; - else - $price = $prod->price; - - $line=new CommandeLigne($this->db); - $line->fk_product=$idproduct; - $line->desc=$prod->description; - $line->qty=$qty; - $line->subprice=$price; - $line->remise_percent=$remise_percent; - $line->tva_tx=$tva_tx; - $line->ref=$prod->ref; - $line->libelle=$prod->libelle; - $line->product_desc=$prod->description; - - $this->lines[] = $line; - - /** POUR AJOUTER AUTOMATIQUEMENT LES SOUSPRODUITS À LA COMMANDE - if($conf->global->PRODUIT_SOUSPRODUITS == 1) - { - $prod = new Product($this->db, $idproduct); - $prod -> get_sousproduits_arbo (); - $prods_arbo = $prod->get_each_prod(); - if(sizeof($prods_arbo) > 0) - { - foreach($prods_arbo as $key => $value) - { - // print "id : ".$value[1].' :qty: '.$value[0].'
'; - if(! in_array($value[1],$this->products)) - $this->add_product($value[1], $value[0]); - - } - } - - } - **/ - } - } + /** + * \brief Ajout d'un produit dans la commande, en base + * \param commandeid id de la commande + * \param desc description de la ligne + * \param pu prix unitaire + * \param qty quantité + * \param txtva taux de tva forcé, sinon -1 + * \param fk_product id du produit/service predéfini + * \param remise_percent pourcentage de remise de la ligne + * \return int >0 si ok, <0 si ko + * \see add_product + * \remarks Les parametres sont deja censé etre juste et avec valeurs finales a l'appel + * de cette methode. Aussi, pour le taux tva, il doit deja avoir ete défini + * par l'appelant par la methode get_default_tva(societe_vendeuse,societe_acheteuse,taux_produit) + * et le desc doit deja avoir la bonne valeur (a l'appelant de gerer le multilangue) + */ + function addline($commandeid, $desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0) + { + dolibarr_syslog("Commande.class.php::addline this->id=$this->id, $commandeid, $desc, $pu, $qty, $txtva, $fk_product, $remise_percent"); + include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); + + if ($this->statut == 0) + { + $this->db->begin(); - /** - * \brief Stocke un numéro de rang pour toutes les lignes de - * detail d'une commande qui n'en ont pas. - */ - function line_order() - { - $sql = 'SELECT count(rowid) FROM '.MAIN_DB_PREFIX.'commandedet'; - $sql .= ' WHERE fk_commande='.$this->id; - $sql .= ' AND rang = 0'; - $resql = $this->db->query($sql); - if ($resql) - { - $row = $this->db->fetch_row($resql); - $nl = $row[0]; - } - if ($nl > 0) - { - $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'commandedet'; - $sql .= ' WHERE fk_commande='.$this->id; - $sql .= ' ORDER BY rang ASC, rowid ASC'; - $resql = $this->db->query($sql); - if ($resql) - { - $num = $this->db->num_rows($resql); - $i = 0; - while ($i < $num) - { - $row = $this->db->fetch_row($resql); - $li[$i] = $row[0]; - $i++; - } - } - for ($i = 0 ; $i < sizeof($li) ; $i++) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet SET rang = '.($i+1); - $sql .= ' WHERE rowid = '.$li[$i]; - if (!$this->db->query($sql) ) - { - dolibarr_syslog($this->db->error()); - } - } - } - } - - function line_up($rowid) - { - $this->line_order(); - - /* Lecture du rang de la ligne */ - $sql = 'SELECT rang FROM '.MAIN_DB_PREFIX.'commandedet'; - $sql .= ' WHERE rowid ='.$rowid; - $resql = $this->db->query($sql); - if ($resql) - { - $row = $this->db->fetch_row($resql); - $rang = $row[0]; - } - - if ($rang > 1 ) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet SET rang = '.$rang ; - $sql .= ' WHERE fk_commande = '.$this->id; - $sql .= ' AND rang = '.($rang - 1); - if ($this->db->query($sql) ) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet SET rang = '.($rang - 1); - $sql .= ' WHERE rowid = '.$rowid; - if (! $this->db->query($sql) ) - { - dolibarr_print_error($this->db); - } - } - else - { - dolibarr_print_error($this->db); - } - } - } - - function line_down($rowid) - { - $this->line_order(); - - /* Lecture du rang de la ligne */ - $sql = 'SELECT rang FROM '.MAIN_DB_PREFIX.'commandedet'; - $sql .= ' WHERE rowid ='.$rowid; - $resql = $this->db->query($sql); - if ($resql) - { - $row = $this->db->fetch_row($resql); - $rang = $row[0]; - } - - /* Lecture du rang max de la facture */ - $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'commandedet'; - $sql .= ' WHERE fk_commande ='.$this->id; - $resql = $this->db->query($sql); - if ($resql) - { - $row = $this->db->fetch_row($resql); - $max = $row[0]; - } - - if ($rang < $max ) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet SET rang = '.$rang; - $sql .= ' WHERE fk_commande = '.$this->id; - $sql .= ' AND rang = '.($rang+1); - if ($this->db->query($sql) ) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet SET rang = '.($rang+1); - $sql .= ' WHERE rowid = '.$rowid; - if (! $this->db->query($sql) ) - { - dolibarr_print_error($this->db); - } - } - else - { - dolibarr_print_error($this->db); - } - } - } - - /** - * \brief Recupère de la base les caractéristiques d'une commande - * \param rowid id de la commande à récupérer - */ - function fetch($id) - { - $sql = 'SELECT c.rowid, c.date_creation, c.ref, c.fk_soc, c.fk_user_author, c.fk_statut, c.amount_ht, c.total_ht, c.total_ttc, c.tva, c.fk_cond_reglement, c.fk_mode_reglement,'; - $sql.= ' '.$this->db->pdate('c.date_commande').' as date_commande, '.$this->db->pdate('c.date_livraison').' as date_livraison,'; - $sql.= ' c.fk_projet, c.remise_percent, c.remise, c.remise_absolue, c.source, c.facture as facturee, c.note, c.note_public, c.ref_client, c.model_pdf, c.fk_adresse_livraison'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'commande as c'; - $sql.= ' WHERE c.rowid = '.$id; + // Nettoyage paramètres + $remise_percent=price2num($remise_percent); + $qty=price2num($qty); + if (! $qty) $qty=1; + $pu = price2num($pu); + $txtva = price2num($txtva); - dolibarr_syslog("Commande::fetch sql=$sql"); + // 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); + $total_ht = $tabprice[0]; + $total_tva = $tabprice[1]; + $total_ttc = $tabprice[2]; - $result = $this->db->query($sql) ; - if ($result) - { - $obj = $this->db->fetch_object($result); - if ($obj) - { - $this->id = $obj->rowid; - $this->ref = $obj->ref; - $this->ref_client = $obj->ref_client; - $this->socid = $obj->fk_soc; - $this->statut = $obj->fk_statut; - $this->user_author_id = $obj->fk_user_author; - $this->total_ht = $obj->total_ht; - $this->total_tva = $obj->tva; - $this->total_ttc = $obj->total_ttc; - $this->date = $obj->date_commande; - $this->remise = $obj->remise; - $this->remise_percent = $obj->remise_percent; - $this->remise_absolue = $obj->remise_absolue; - $this->source = $obj->source; - $this->facturee = $obj->facturee; - $this->note = $obj->note; - $this->note_public = $obj->note_public; - $this->projet_id = $obj->fk_projet; - $this->modelpdf = $obj->model_pdf; - $this->cond_reglement_id = $obj->fk_cond_reglement; - $this->mode_reglement_id = $obj->fk_mode_reglement; - $this->date_livraison = $obj->date_livraison; - $this->adresse_livraison_id = $obj->fk_adresse_livraison; - if ($this->statut == 0) $this->brouillon = 1; + // Anciens indicateurs: $price, $remise (a ne plus utiliser) + $price = $pu; + if ($remise_percent > 0) + { + $remise = round(($pu * $remise_percent / 100), 2); + $price = $pu - $remise; + } + + // Insertion ligne + $ligne=new CommandeLigne($this->db); + + $ligne->fk_commande=$commandeid; + $ligne->desc=$desc; + $ligne->qty=$qty; + $ligne->tva_tx=$txtva; + $ligne->fk_product=$fk_product; + $ligne->remise_percent=$remise_percent; + $ligne->subprice=$pu; + $ligne->rang=-1; + $ligne->info_bits=$info_bits; + $ligne->total_ht=$total_ht; + $ligne->total_tva=$total_tva; + $ligne->total_ttc=$total_ttc; + + // Ne plus utiliser + $ligne->price=$price; + $ligne->remise=$remise; + + $result=$ligne->insert(); + if ($result > 0) + { + // Mise a jour informations denormalisees au niveau de la facture meme + $result=$this->update_price($this->id); + + if ($result > 0) + { + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog("Error sql=$sql, error=".$this->error); + $this->db->rollback(); + return -1; + } + } + else + { + $this->error=$ligne->error; + $this->db->rollback(); + return -2; + } + } + } + + + /** + * \brief Ajoute une ligne dans tableau lines + * \param idproduct Id du produit à ajouter + * \param qty Quantité + * \remise_percent remise_percent Remise relative effectuée sur le produit + * \return void + * \remarks $this->client doit etre chargé + * \TODO Remplacer les appels a cette fonction par generation objet Ligne + * inséré dans tableau $this->products + */ + function add_product($idproduct, $qty, $remise_percent=0) + { + global $conf, $mysoc; + + if (! $qty) $qty = 1; + + if ($idproduct > 0) + { + $prod=new Product($this->db); + $prod->fetch($idproduct); + + $tva_tx = get_default_tva($mysoc,$this->client,$prod->tva_tx); + // multiprix + if($conf->global->PRODUIT_MULTIPRICES == 1) + $price = $prod->multiprices[$this->client->price_level]; + else + $price = $prod->price; + + $line=new CommandeLigne($this->db); + $line->fk_product=$idproduct; + $line->desc=$prod->description; + $line->qty=$qty; + $line->subprice=$price; + $line->remise_percent=$remise_percent; + $line->tva_tx=$tva_tx; + $line->ref=$prod->ref; + $line->libelle=$prod->libelle; + $line->product_desc=$prod->description; + + $this->lines[] = $line; + + /** POUR AJOUTER AUTOMATIQUEMENT LES SOUSPRODUITS À LA COMMANDE + if($conf->global->PRODUIT_SOUSPRODUITS == 1) + { + $prod = new Product($this->db, $idproduct); + $prod -> get_sousproduits_arbo (); + $prods_arbo = $prod->get_each_prod(); + if(sizeof($prods_arbo) > 0) + { + foreach($prods_arbo as $key => $value) + { + // print "id : ".$value[1].' :qty: '.$value[0].'
'; + if(! in_array($value[1],$this->products)) + $this->add_product($value[1], $value[0]); + + } + } + + } + **/ + } + } + + /** + * \brief Stocke un numéro de rang pour toutes les lignes de + * detail d'une commande qui n'en ont pas. + */ + function line_order() + { + $sql = 'SELECT count(rowid) FROM '.MAIN_DB_PREFIX.'commandedet'; + $sql .= ' WHERE fk_commande='.$this->id; + $sql .= ' AND rang = 0'; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + $nl = $row[0]; + } + if ($nl > 0) + { + $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'commandedet'; + $sql .= ' WHERE fk_commande='.$this->id; + $sql .= ' ORDER BY rang ASC, rowid ASC'; + $resql = $this->db->query($sql); + if ($resql) + { + $num = $this->db->num_rows($resql); + $i = 0; + while ($i < $num) + { + $row = $this->db->fetch_row($resql); + $li[$i] = $row[0]; + $i++; + } + } + for ($i = 0 ; $i < sizeof($li) ; $i++) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet SET rang = '.($i+1); + $sql .= ' WHERE rowid = '.$li[$i]; + if (!$this->db->query($sql) ) + { + dolibarr_syslog($this->db->error()); + } + } + } + } + + function line_up($rowid) + { + $this->line_order(); + + /* Lecture du rang de la ligne */ + $sql = 'SELECT rang FROM '.MAIN_DB_PREFIX.'commandedet'; + $sql .= ' WHERE rowid ='.$rowid; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + $rang = $row[0]; + } + + if ($rang > 1 ) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet SET rang = '.$rang ; + $sql .= ' WHERE fk_commande = '.$this->id; + $sql .= ' AND rang = '.($rang - 1); + if ($this->db->query($sql) ) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet SET rang = '.($rang - 1); + $sql .= ' WHERE rowid = '.$rowid; + if (! $this->db->query($sql) ) + { + dolibarr_print_error($this->db); + } + } + else + { + dolibarr_print_error($this->db); + } + } + } + + function line_down($rowid) + { + $this->line_order(); + + /* Lecture du rang de la ligne */ + $sql = 'SELECT rang FROM '.MAIN_DB_PREFIX.'commandedet'; + $sql .= ' WHERE rowid ='.$rowid; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + $rang = $row[0]; + } + + /* Lecture du rang max de la facture */ + $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'commandedet'; + $sql .= ' WHERE fk_commande ='.$this->id; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + $max = $row[0]; + } + + if ($rang < $max ) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet SET rang = '.$rang; + $sql .= ' WHERE fk_commande = '.$this->id; + $sql .= ' AND rang = '.($rang+1); + if ($this->db->query($sql) ) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet SET rang = '.($rang+1); + $sql .= ' WHERE rowid = '.$rowid; + if (! $this->db->query($sql) ) + { + dolibarr_print_error($this->db); + } + } + else + { + dolibarr_print_error($this->db); + } + } + } + + /** + * \brief Recupère de la base les caractéristiques d'une commande + * \param rowid id de la commande à récupérer + */ + function fetch($id) + { + $sql = 'SELECT c.rowid, c.date_creation, c.ref, c.fk_soc, c.fk_user_author, c.fk_statut, c.amount_ht, c.total_ht, c.total_ttc, c.tva, c.fk_cond_reglement, c.fk_mode_reglement,'; + $sql.= ' '.$this->db->pdate('c.date_commande').' as date_commande, '.$this->db->pdate('c.date_livraison').' as date_livraison,'; + $sql.= ' c.fk_projet, c.remise_percent, c.remise, c.remise_absolue, c.source, c.facture as facturee, c.note, c.note_public, c.ref_client, c.model_pdf, c.fk_adresse_livraison'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'commande as c'; + $sql.= ' WHERE c.rowid = '.$id; + + dolibarr_syslog("Commande::fetch sql=$sql"); + + $result = $this->db->query($sql) ; + if ($result) + { + $obj = $this->db->fetch_object($result); + if ($obj) + { + $this->id = $obj->rowid; + $this->ref = $obj->ref; + $this->ref_client = $obj->ref_client; + $this->socid = $obj->fk_soc; + $this->statut = $obj->fk_statut; + $this->user_author_id = $obj->fk_user_author; + $this->total_ht = $obj->total_ht; + $this->total_tva = $obj->tva; + $this->total_ttc = $obj->total_ttc; + $this->date = $obj->date_commande; + $this->remise = $obj->remise; + $this->remise_percent = $obj->remise_percent; + $this->remise_absolue = $obj->remise_absolue; + $this->source = $obj->source; + $this->facturee = $obj->facturee; + $this->note = $obj->note; + $this->note_public = $obj->note_public; + $this->projet_id = $obj->fk_projet; + $this->modelpdf = $obj->model_pdf; + $this->cond_reglement_id = $obj->fk_cond_reglement; + $this->mode_reglement_id = $obj->fk_mode_reglement; + $this->date_livraison = $obj->date_livraison; + $this->adresse_livraison_id = $obj->fk_adresse_livraison; + if ($this->statut == 0) $this->brouillon = 1; - $this->db->free(); + $this->db->free(); - if ($this->cond_reglement_id) - { - $sql = "SELECT rowid, libelle, code"; - $sql.= " FROM ".MAIN_DB_PREFIX."cond_reglement"; - $sql.= " WHERE rowid = ".$this->cond_reglement_id; + if ($this->cond_reglement_id) + { + $sql = "SELECT rowid, libelle, code"; + $sql.= " FROM ".MAIN_DB_PREFIX."cond_reglement"; + $sql.= " WHERE rowid = ".$this->cond_reglement_id; - $resqlcond = $this->db->query($sql); + $resqlcond = $this->db->query($sql); - if ($resqlcond) - { - $objc = $this->db->fetch_object($resqlcond); - $this->cond_reglement = $objc->libelle; - $this->cond_reglement_code = $objc->code; - } - } + if ($resqlcond) + { + $objc = $this->db->fetch_object($resqlcond); + $this->cond_reglement = $objc->libelle; + $this->cond_reglement_code = $objc->code; + } + } - $this->lignes = $this->fetch_lignes(); + $this->lignes = $this->fetch_lignes(); - /* - * Propale associée - */ - $sql = 'SELECT cp.fk_propale'; - $sql .= ' FROM '.MAIN_DB_PREFIX.'co_pr as cp'; - $sql .= ' WHERE cp.fk_commande = '.$this->id; - if ($this->db->query($sql)) - { - if ($this->db->num_rows()) - { - $obj = $this->db->fetch_object(); - $this->propale_id = $obj->fk_propale; - } - return 1; - } - else - { - dolibarr_print_error($this->db); - return -1; - } - } - else - { - $this->error="Order not found"; - return -2; - } - } - else - { - dolibarr_print_error($this->db); - return -3; - } - } + /* + * Propale associée + */ + $sql = 'SELECT cp.fk_propale'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'co_pr as cp'; + $sql .= ' WHERE cp.fk_commande = '.$this->id; + if ($this->db->query($sql)) + { + if ($this->db->num_rows()) + { + $obj = $this->db->fetch_object(); + $this->propale_id = $obj->fk_propale; + } + return 1; + } + else + { + dolibarr_print_error($this->db); + return -1; + } + } + else + { + $this->error="Order not found"; + return -2; + } + } + else + { + dolibarr_print_error($this->db); + return -3; + } + } - /** - * \brief Ajout d'une ligne remise fixe dans la commande, en base - * \param idremise Id de la remise fixe - * \return int >0 si ok, <0 si ko - */ - function insert_discount($idremise) - { - global $langs; + /** + * \brief Ajout d'une ligne remise fixe dans la commande, en base + * \param idremise Id de la remise fixe + * \return int >0 si ok, <0 si ko + */ + function insert_discount($idremise) + { + global $langs; - include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); - include_once(DOL_DOCUMENT_ROOT.'/discount.class.php'); + include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); + include_once(DOL_DOCUMENT_ROOT.'/discount.class.php'); - $this->db->begin(); + $this->db->begin(); - $remise=new DiscountAbsolute($this->db); - $result=$remise->fetch($idremise); + $remise=new DiscountAbsolute($this->db); + $result=$remise->fetch($idremise); - if ($result > 0) - { - $comligne=new CommandeLigne($this->db); - $comligne->fk_commande=$this->id; - $comligne->fk_remise_except=$remise->id; - $comligne->desc=$remise->description; // Description ligne - $comligne->tva_tx=$remise->tva_tx; - $comligne->subprice=-$remise->amount_ht; - $comligne->price=-$remise->amount_ht; - $comligne->fk_product=0; // Id produit prédéfini - $comligne->qty=1; - $comligne->remise=0; - $comligne->remise_percent=0; - $comligne->rang=-1; - $comligne->info_bits=2; + if ($result > 0) + { + $comligne=new CommandeLigne($this->db); + $comligne->fk_commande=$this->id; + $comligne->fk_remise_except=$remise->id; + $comligne->desc=$remise->description; // Description ligne + $comligne->tva_tx=$remise->tva_tx; + $comligne->subprice=-$remise->amount_ht; + $comligne->price=-$remise->amount_ht; + $comligne->fk_product=0; // Id produit prédéfini + $comligne->qty=1; + $comligne->remise=0; + $comligne->remise_percent=0; + $comligne->rang=-1; + $comligne->info_bits=2; - $tabprice=calcul_price_total($comligne->qty, $comligne->subprice, 0,$comligne->tva_tx); - $comligne->total_ht = $tabprice[0]; - $comligne->total_tva = $tabprice[1]; - $comligne->total_ttc = $tabprice[2]; + $tabprice=calcul_price_total($comligne->qty, $comligne->subprice, 0,$comligne->tva_tx); + $comligne->total_ht = $tabprice[0]; + $comligne->total_tva = $tabprice[1]; + $comligne->total_ttc = $tabprice[2]; - $result=$comligne->insert(); - if ($result > 0) - { - $result=$this->update_price($this->id); - if ($result > 0) - { - $this->db->commit(); - return 1; - } - else - { - $this->db->rollback(); - return -1; - } - } - else - { - $this->error=$comligne->error; - $this->db->rollback(); - return -2; - } - } - else - { - $this->db->rollback(); - return -2; - } - } + $result=$comligne->insert(); + if ($result > 0) + { + $result=$this->update_price($this->id); + if ($result > 0) + { + $this->db->commit(); + return 1; + } + else + { + $this->db->rollback(); + return -1; + } + } + else + { + $this->error=$comligne->error; + $this->db->rollback(); + return -2; + } + } + else + { + $this->db->rollback(); + return -2; + } + } - /** - * \brief Positionne modele derniere generation - * \param user Objet use qui modifie - * \param modelpdf Nom du modele - */ - function set_pdf_model($user, $modelpdf) - { - if ($user->rights->commande->creer) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."commande SET model_pdf = '$modelpdf'"; - $sql.= " WHERE rowid = ".$this->id; + /** + * \brief Positionne modele derniere generation + * \param user Objet use qui modifie + * \param modelpdf Nom du modele + */ + function set_pdf_model($user, $modelpdf) + { + if ($user->rights->commande->creer) + { + $sql = "UPDATE ".MAIN_DB_PREFIX."commande SET model_pdf = '$modelpdf'"; + $sql.= " WHERE rowid = ".$this->id; - if ($this->db->query($sql) ) - { - $this->modelpdf=$modelpdf; - return 1; - } - else - { - dolibarr_print_error($this->db); - return 0; - } - } - } + if ($this->db->query($sql) ) + { + $this->modelpdf=$modelpdf; + return 1; + } + else + { + dolibarr_print_error($this->db); + return 0; + } + } + } - /** - * \brief Reinitialise le tableau lignes - * \param only_product Ne renvoie que ligne liées à des produits physiques prédéfinis - * \return array Tableau de CommandeLigne - */ - function fetch_lignes($only_product=0) - { - $this->lignes = array(); - $sql = 'SELECT l.rowid, l.fk_product, l.fk_commande, l.description, l.price, l.qty, l.tva_tx,'; - $sql.= ' l.remise_percent, l.subprice, l.coef, l.rang, l.info_bits, '; - $sql.= ' p.ref as product_ref, p.description as product_desc, p.fk_product_type, p.label'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as l'; - $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON (p.rowid = l.fk_product)'; - $sql.= ' WHERE l.fk_commande = '.$this->id; - if ($only_product==1) $sql .= ' AND p.fk_product_type = 0'; - $sql .= ' ORDER BY l.rang'; + /** + * \brief Reinitialise le tableau lignes + * \param only_product Ne renvoie que ligne liées à des produits physiques prédéfinis + * \return array Tableau de CommandeLigne + */ + function fetch_lignes($only_product=0) + { + $this->lignes = array(); + $sql = 'SELECT l.rowid, l.fk_product, l.fk_commande, l.description, l.price, l.qty, l.tva_tx,'; + $sql.= ' l.remise_percent, l.subprice, l.coef, l.rang, l.info_bits, '; + $sql.= ' p.ref as product_ref, p.description as product_desc, p.fk_product_type, p.label'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as l'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON (p.rowid = l.fk_product)'; + $sql.= ' WHERE l.fk_commande = '.$this->id; + if ($only_product==1) $sql .= ' AND p.fk_product_type = 0'; + $sql .= ' ORDER BY l.rang'; - $result = $this->db->query($sql); - if ($result) - { - $num = $this->db->num_rows(); - $i = 0; - while ($i < $num) - { - $objp = $this->db->fetch_object($result); + $result = $this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows(); + $i = 0; + while ($i < $num) + { + $objp = $this->db->fetch_object($result); - $ligne = new CommandeLigne($this->db); - $ligne->rowid = $objp->rowid; - $ligne->id = $objp->rowid; // \deprecated - $ligne->fk_commande = $objp->fk_commande; - $ligne->commande_id = $objp->fk_commande; // \deprecated - $ligne->desc = $objp->description; // Description ligne - $ligne->qty = $objp->qty; - $ligne->tva_tx = $objp->tva_tx; - $ligne->subprice = $objp->subprice; - $ligne->remise_percent = $objp->remise_percent; - $ligne->price = $objp->price; - $ligne->fk_product = $objp->fk_product; - $ligne->coef = $objp->coef; - $ligne->rang = $objp->rang; - $ligne->info_bits = $objp->info_bits; + $ligne = new CommandeLigne($this->db); + $ligne->rowid = $objp->rowid; + $ligne->id = $objp->rowid; // \deprecated + $ligne->fk_commande = $objp->fk_commande; + $ligne->commande_id = $objp->fk_commande; // \deprecated + $ligne->desc = $objp->description; // Description ligne + $ligne->qty = $objp->qty; + $ligne->tva_tx = $objp->tva_tx; + $ligne->subprice = $objp->subprice; + $ligne->remise_percent = $objp->remise_percent; + $ligne->price = $objp->price; + $ligne->fk_product = $objp->fk_product; + $ligne->coef = $objp->coef; + $ligne->rang = $objp->rang; + $ligne->info_bits = $objp->info_bits; - $ligne->ref = $objp->product_ref; - $ligne->libelle = $objp->label; - $ligne->product_desc = $objp->product_desc; // Description produit - $ligne->fk_product_type= $objp->fk_product_type; // Produit ou service + $ligne->ref = $objp->product_ref; + $ligne->libelle = $objp->label; + $ligne->product_desc = $objp->product_desc; // Description produit + $ligne->fk_product_type= $objp->fk_product_type; // Produit ou service - $this->lignes[$i] = $ligne; - $i++; - } - $this->db->free(); - } - return $this->lignes; - } + $this->lignes[$i] = $ligne; + $i++; + } + $this->db->free(); + } + return $this->lignes; + } - /** - * \brief Renvoie nombre de lignes de type produits. Doit etre appelé après fetch_lignes - * \return int <0 si ko, Nbre de lignes produits sinon - */ - function getNbOfProductsLines() - { - $nb=0; - foreach($this->lignes as $ligne) - { - if ($ligne->fk_product_type == 0) $nb++; - } - return $nb; - } + /** + * \brief Renvoie nombre de lignes de type produits. Doit etre appelé après fetch_lignes + * \return int <0 si ko, Nbre de lignes produits sinon + */ + function getNbOfProductsLines() + { + $nb=0; + foreach($this->lignes as $ligne) + { + if ($ligne->fk_product_type == 0) $nb++; + } + return $nb; + } - /** - * - * - */ + /** + * + * + */ function fetch_adresse_livraison($id) { $idadresse = $id; @@ -1179,142 +1179,158 @@ class Commande extends CommonObject } return 0; } + + /** + * \brief Supprime une ligne de la commande + * \param idligne Id de la ligne à supprimer + * \return int >0 si ok, <0 si ko + */ + function delete_line($idligne) + { + if ($this->statut == 0) + { + $sql = "SELECT fk_product, qty"; + $sql.= " FROM ".MAIN_DB_PREFIX."commandedet"; + $sql.= " WHERE rowid = '$idligne';"; - /** - * \brief Supprime une ligne de la commande - * \param idligne Id de la ligne à supprimer - * \return int >0 si ok, <0 si ko - */ - function delete_line($idligne) - { - if ($this->statut == 0) - { - $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commandedet WHERE rowid = $idligne"; - if ($this->db->query($sql) ) - { - $this->update_price(); - return 1; - } - else - { - return -1; - } - } - } + $result = $this->db->query($sql); + if ($result) + { + while ($obj = $this->db->fetch_object($result)) + { + $product = new Product($this->db); + $product->id = $obj->fk_product; + $product->ajust_stock_commande($obj->qty, 1); + } + $this->db->free($result); + } - /** - * \brief Applique une remise relative - * \param user User qui positionne la remise - * \param remise - * \return int <0 si ko, >0 si ok - */ - function set_remise($user, $remise) - { - $remise=trim($remise)?trim($remise):0; + $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commandedet WHERE rowid = '$idligne';"; + if ($this->db->query($sql) ) + { + $this->update_price(); + return 1; + } + else + { + return -1; + } + } + } + + /** + * \brief Applique une remise relative + * \param user User qui positionne la remise + * \param remise + * \return int <0 si ko, >0 si ok + */ + function set_remise($user, $remise) + { + $remise=trim($remise)?trim($remise):0; + + if ($user->rights->commande->creer) + { + $remise=price2num($remise); + + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande'; + $sql.= ' SET remise_percent = '.$remise; + $sql.= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;'; + + if ($this->db->query($sql)) + { + $this->remise_percent = $remise; + $this->update_price($this->id); + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } + } + } + + + /** + * \brief Applique une remise absolue + * \param user User qui positionne la remise + * \param remise + * \return int <0 si ko, >0 si ok + */ + function set_remise_absolue($user, $remise) + { + $remise=trim($remise)?trim($remise):0; + + if ($user->rights->commande->creer) + { + $remise=price2num($remise); + + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande'; + $sql.= ' SET remise_absolue = '.$remise; + $sql.= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;'; + + dolibarr_syslog("Commande::set_remise_absolue sql=$sql"); + + if ($this->db->query($sql)) + { + $this->remise_absolue = $remise; + $this->update_price($this->id); + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } + } + } - if ($user->rights->commande->creer) - { - $remise=price2num($remise); - - $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande'; - $sql.= ' SET remise_percent = '.$remise; - $sql.= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;'; - - if ($this->db->query($sql)) - { - $this->remise_percent = $remise; - $this->update_price($this->id); - return 1; - } - else - { - $this->error=$this->db->error(); - return -1; - } - } - } - - - /** - * \brief Applique une remise absolue - * \param user User qui positionne la remise - * \param remise - * \return int <0 si ko, >0 si ok - */ - function set_remise_absolue($user, $remise) - { - $remise=trim($remise)?trim($remise):0; - - if ($user->rights->commande->creer) - { - $remise=price2num($remise); - - $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande'; - $sql.= ' SET remise_absolue = '.$remise; - $sql.= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;'; - - dolibarr_syslog("Commande::set_remise_absolue sql=$sql"); - - if ($this->db->query($sql)) - { - $this->remise_absolue = $remise; - $this->update_price($this->id); - return 1; - } - else - { - $this->error=$this->db->error(); - return -1; - } - } - } - - /** - * \brief Mets à jour le prix total de la commnde - * \return int <0 si ko, >0 si ok - */ - function update_price() - { - include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); - - // Liste des lignes factures a sommer - $sql = "SELECT price, qty, tva_tx, total_ht, total_tva, total_ttc"; - $sql.= " FROM ".MAIN_DB_PREFIX."commandedet"; - $sql.= " WHERE fk_commande = ".$this->id; - - dolibarr_syslog("Commande.class.php::update_price this->id=".$this->id); - - $result = $this->db->query($sql); - if ($result) - { - $this->total_ht = 0; - $this->total_tva = 0; - $this->total_ttc = 0; - - $num = $this->db->num_rows($result); - $i = 0; - while ($i < $num) - { - $obj = $this->db->fetch_object($result); - - $this->total_ht += $obj->total_ht; - $this->total_tva += ($obj->total_ttc - $obj->total_ht); - $this->total_ttc += $obj->total_ttc; - - // Anciens indicateurs - $this->amount_ht += $obj->price * $obj->qty; - $this->total_remise += 0; // Plus de remise globale (toute remise est sur une ligne) -/* \deprecated car simplifie par les 3 indicateurs total_ht, total_tva et total_ttc sur lignes - $products[$i][0] = $obj->price; - $products[$i][1] = $obj->qty; - $products[$i][2] = $obj->tva_tx; -*/ - $i++; - } - - $this->db->free($result); - } -/* \deprecated car simplifie par les 3 indicateurs total_ht, total_tva et total_ttc sur lignes + /** + * \brief Mets à jour le prix total de la commnde + * \return int <0 si ko, >0 si ok + */ + function update_price() + { + include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); + + // Liste des lignes factures a sommer + $sql = "SELECT price, qty, tva_tx, total_ht, total_tva, total_ttc"; + $sql.= " FROM ".MAIN_DB_PREFIX."commandedet"; + $sql.= " WHERE fk_commande = ".$this->id; + + dolibarr_syslog("Commande.class.php::update_price this->id=".$this->id); + + $result = $this->db->query($sql); + if ($result) + { + $this->total_ht = 0; + $this->total_tva = 0; + $this->total_ttc = 0; + + $num = $this->db->num_rows($result); + $i = 0; + while ($i < $num) + { + $obj = $this->db->fetch_object($result); + + $this->total_ht += $obj->total_ht; + $this->total_tva += ($obj->total_ttc - $obj->total_ht); + $this->total_ttc += $obj->total_ttc; + + // Anciens indicateurs + $this->amount_ht += $obj->price * $obj->qty; + $this->total_remise += 0; // Plus de remise globale (toute remise est sur une ligne) + /* \deprecated car simplifie par les 3 indicateurs total_ht, total_tva et total_ttc sur lignes + $products[$i][0] = $obj->price; + $products[$i][1] = $obj->qty; + $products[$i][2] = $obj->tva_tx; + */ + $i++; + } + + $this->db->free($result); + } + /* \deprecated car simplifie par les 3 indicateurs total_ht, total_tva et total_ttc sur lignes $calculs = calcul_price($products, $this->remise_percent, $this->remise_absolue); $this->total_remise = $calculs[3]; $this->amount_ht = $calculs[4]; @@ -1337,9 +1353,9 @@ class Commande extends CommonObject } else { - $this->error=$this->db->error(); - dolibarr_syslog("Commande::update_price error=".$this->error); - return -1; + $this->error=$this->db->error(); + dolibarr_syslog("Commande::update_price error=".$this->error); + return -1; } } @@ -1397,31 +1413,31 @@ class Commande extends CommonObject */ function set_date_livraison($user, $date_livraison) { - if ($user->rights->commande->creer) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."commande"; - $sql.= " SET date_livraison = ".($date_livraison ? $this->db->idate($date_livraison) : 'null'); - $sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0"; - - $resql=$this->db->query($sql); - if ($resql) + if ($user->rights->commande->creer) + { + $sql = "UPDATE ".MAIN_DB_PREFIX."commande"; + $sql.= " SET date_livraison = ".($date_livraison ? $this->db->idate($date_livraison) : 'null'); + $sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0"; + + $resql=$this->db->query($sql); + if ($resql) { - $this->date_livraison = $date_livraison; - return 1; + $this->date_livraison = $date_livraison; + return 1; } - else + else { - $this->error=$this->db->error(); - dolibarr_syslog("Commande::set_date_livraison Erreur SQL sql=$sql"); - return -1; + $this->error=$this->db->error(); + dolibarr_syslog("Commande::set_date_livraison Erreur SQL sql=$sql"); + return -1; } } - else + else { - return -2; + return -2; } } - + /** * \brief Définit une adresse de livraison * \param user Objet utilisateur qui modifie @@ -1630,178 +1646,193 @@ class Commande extends CommonObject } - /** - * \brief Mets à jour une ligne de commande - * \param rowid Id de la ligne de facture - * \param desc Description de la ligne - * \param pu Prix unitaire - * \param qty Quantité - * \param remise_percent Pourcentage de remise de la ligne - * \param tva_tx Taux TVA - * \return int < 0 si erreur, > 0 si ok - */ - function updateline($rowid, $desc, $pu, $qty, $remise_percent=0, $txtva) - { - dolibarr_syslog("Commande::UpdateLine $rowid, $desc, $pu, $qty, $remise_percent, $txtva"); - include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); + /** + * \brief Mets à jour une ligne de commande + * \param rowid Id de la ligne de facture + * \param desc Description de la ligne + * \param pu Prix unitaire + * \param qty Quantité + * \param remise_percent Pourcentage de remise de la ligne + * \param tva_tx Taux TVA + * \return int < 0 si erreur, > 0 si ok + */ + function updateline($rowid, $desc, $pu, $qty, $remise_percent=0, $txtva) + { + dolibarr_syslog("Commande::UpdateLine $rowid, $desc, $pu, $qty, $remise_percent, $txtva"); + include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); + + if ($this->brouillon) + { + $this->db->begin(); - if ($this->brouillon) - { - $this->db->begin(); - - // Nettoyage paramètres - $remise_percent=price2num($remise_percent); - $qty=price2num($qty); - if (! $qty) $qty=1; - $pu = price2num($pu); - $txtva=price2num($txtva); + // Nettoyage paramètres + $remise_percent=price2num($remise_percent); + $qty=price2num($qty); + if (! $qty) $qty=1; + $pu = price2num($pu); + $txtva=price2num($txtva); + + // 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); + $total_ht = $tabprice[0]; + $total_tva = $tabprice[1]; + $total_ttc = $tabprice[2]; + + // Anciens indicateurs: $price, $subprice, $remise (a ne plus utiliser) + $price = $pu; + $subprice = $pu; + $remise = 0; + if ($remise_percent > 0) + { + $remise = round(($pu * $remise_percent / 100),2); + $price = ($pu - $remise); + } + $price = price2num($price); + $subprice = price2num($subprice); - // 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); - $total_ht = $tabprice[0]; - $total_tva = $tabprice[1]; - $total_ttc = $tabprice[2]; - // Anciens indicateurs: $price, $subprice, $remise (a ne plus utiliser) - $price = $pu; - $subprice = $pu; - $remise = 0; - if ($remise_percent > 0) - { - $remise = round(($pu * $remise_percent / 100),2); - $price = ($pu - $remise); - } - $price = price2num($price); - $subprice = price2num($subprice); + $LigneOld = new CommandeLigne($this->db); + $LigneOld->fetch($rowid); + + // Mise a jour ligne en base + $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET"; + $sql.= " description='".addslashes($desc)."'"; + $sql.= ",price='".price2num($price)."'"; + $sql.= ",subprice='".price2num($subprice)."'"; + $sql.= ",remise='".price2num($remise)."'"; + $sql.= ",remise_percent='".price2num($remise_percent)."'"; + $sql.= ",tva_tx='".price2num($txtva)."'"; + $sql.= ",qty='".price2num($qty)."'"; + //if ($date_end) { $sql.= ",date_start='$date_end'"; } + //else { $sql.=',date_start=null'; } + //if ($date_end) { $sql.= ",date_end='$date_end'"; } + //else { $sql.=',date_end=null'; } + //$sql.= " info_bits=".$info_bits.","; + $sql.= ",total_ht='".price2num($total_ht)."'"; + $sql.= ",total_tva='".price2num($total_tva)."'"; + $sql.= ",total_ttc='".price2num($total_ttc)."'"; + $sql.= " WHERE rowid = ".$rowid; + + $result = $this->db->query( $sql); + if ($result > 0) + { + // Mise a jour info denormalisees au niveau facture + $this->update_price($this->id); - // Mise a jour ligne en base - $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET"; - $sql.= " description='".addslashes($desc)."'"; - $sql.= ",price='".price2num($price)."'"; - $sql.= ",subprice='".price2num($subprice)."'"; - $sql.= ",remise='".price2num($remise)."'"; - $sql.= ",remise_percent='".price2num($remise_percent)."'"; - $sql.= ",tva_tx='".price2num($txtva)."'"; - $sql.= ",qty='".price2num($qty)."'"; - //if ($date_end) { $sql.= ",date_start='$date_end'"; } - //else { $sql.=',date_start=null'; } - //if ($date_end) { $sql.= ",date_end='$date_end'"; } - //else { $sql.=',date_end=null'; } - //$sql.= " info_bits=".$info_bits.","; - $sql.= ",total_ht='".price2num($total_ht)."'"; - $sql.= ",total_tva='".price2num($total_tva)."'"; - $sql.= ",total_ttc='".price2num($total_ttc)."'"; - $sql.= " WHERE rowid = ".$rowid; + if ($LigneOld->qty <> $qty && $LigneOld->produit_id) + { + $delta = $qty - $LigneOld->qty; + $op = ($delta > 0) ? 0 : 1; - $result = $this->db->query( $sql); - if ($result > 0) - { - // Mise a jour info denormalisees au niveau facture - $this->update_price($this->id); - $this->db->commit(); - return $result; - } - else - { - $this->error=$this->db->error(); - $this->db->rollback(); - return -1; - } - } - else - { - $this->error="Commande::updateline Order status makes operation forbidden"; - return -2; - } - } - + $product = new Product($this->db); + $product->id = $LigneOld->produit_id; + $product->ajust_stock_commande(abs($delta), $op); + } - /** - * \brief Supprime la commande - */ - function delete() - { - global $conf, $lang; + $this->db->commit(); + return $result; + } + else + { + $this->error=$this->db->error(); + $this->db->rollback(); + return -1; + } + } + else + { + $this->error="Commande::updateline Order status makes operation forbidden"; + return -2; + } + } + + + /** + * \brief Supprime la commande + */ + function delete() + { + global $conf, $lang; + + $err = 0; + $this->db->begin(); + + $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commandedet WHERE fk_commande = $this->id ;"; + if (! $this->db->query($sql) ) + { + $err++; + } + + $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commande WHERE rowid = $this->id;"; + if (! $this->db->query($sql) ) + { + $err++; + } + + $sql = 'DELETE FROM '.MAIN_DB_PREFIX."co_pr WHERE fk_commande = $this->id;"; + if (! $this->db->query($sql) ) + { + $err++; + } + + // On efface le répertoire de pdf provisoire + $comref = sanitize_string($this->ref); + if ($conf->commande->dir_output) + { + $dir = $conf->commande->dir_output . "/" . $comref ; + $file = $conf->commande->dir_output . "/" . $comref . "/" . $comref . ".pdf"; + if (file_exists($file)) + { + commande_delete_preview($this->db, $this->id, $this->ref); + + if (!dol_delete_file($file)) + { + $this->error=$langs->trans("ErrorCanNotDeleteFile",$file); + $this->db->rollback(); + return 0; + } + } + if (file_exists($dir)) + { + if (!dol_delete_dir($dir)) + { + $this->error=$langs->trans("ErrorCanNotDeleteDir",$dir); + $this->db->rollback(); + return 0; + } + } + } + + if ($err == 0) + { + // Appel des triggers + include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); + $interface=new Interfaces($this->db); + $result=$interface->run_triggers('ORDER_DELETE',$this,$user,$langs,$conf); + // Fin appel triggers - $err = 0; - $this->db->begin(); - - $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commandedet WHERE fk_commande = $this->id ;"; - if (! $this->db->query($sql) ) - { - $err++; - } - - $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commande WHERE rowid = $this->id;"; - if (! $this->db->query($sql) ) - { - $err++; - } - - $sql = 'DELETE FROM '.MAIN_DB_PREFIX."co_pr WHERE fk_commande = $this->id;"; - if (! $this->db->query($sql) ) - { - $err++; - } - - // On efface le répertoire de pdf provisoire - $comref = sanitize_string($this->ref); - if ($conf->commande->dir_output) - { - $dir = $conf->commande->dir_output . "/" . $comref ; - $file = $conf->commande->dir_output . "/" . $comref . "/" . $comref . ".pdf"; - if (file_exists($file)) - { - commande_delete_preview($this->db, $this->id, $this->ref); - - if (!dol_delete_file($file)) - { - $this->error=$langs->trans("ErrorCanNotDeleteFile",$file); - $this->db->rollback(); - return 0; - } - } - if (file_exists($dir)) - { - if (!dol_delete_dir($dir)) - { - $this->error=$langs->trans("ErrorCanNotDeleteDir",$dir); - $this->db->rollback(); - return 0; - } - } - } - - if ($err == 0) - { - // Appel des triggers - include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); - $interface=new Interfaces($this->db); - $result=$interface->run_triggers('ORDER_DELETE',$this,$user,$langs,$conf); - // Fin appel triggers - - $this->db->commit(); - return 1; - } - else - { - $this->db->rollback(); - return -1; - } - } - - /** - * \brief Classer la commande dans un projet - * \param cat_id Id du projet - */ - function classin($cat_id) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET fk_projet = $cat_id"; - $sql .= " WHERE rowid = $this->id;"; - + $this->db->commit(); + return 1; + } + else + { + $this->db->rollback(); + return -1; + } + } + + /** + * \brief Classer la commande dans un projet + * \param cat_id Id du projet + */ + function classin($cat_id) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET fk_projet = $cat_id"; + $sql .= " WHERE rowid = $this->id;"; + if ($this->db->query($sql) ) { return 1; @@ -2061,205 +2092,212 @@ class Commande extends CommonObject /** - \class CommandeLigne - \brief Classe de gestion des lignes de commande + \class CommandeLigne + \brief Classe de gestion des lignes de commande */ class CommandeLigne { - var $db; - var $error; + var $db; + var $error; + + // From llx_commandedet + var $rowid; + var $fk_facture; + var $desc; // Description ligne + var $fk_product; // Id produit prédéfini + + var $qty; + var $tva_tx; + var $subprice; + var $remise_percent; + var $rang = 0; + var $coef; + var $info_bits = 0; // Bit 0: 0 si TVA normal - 1 si TVA NPR + // Bit 1: 0 ligne normale - 1 si ligne de remise fixe + var $total_ht; // Total HT de la ligne toute quantité et incluant la remise ligne + var $total_tva; // Total TVA de la ligne toute quantité et incluant la remise ligne + var $total_ttc; // Total TTC de la ligne toute quantité et incluant la remise ligne + + // Ne plus utiliser + var $remise; + var $price; + + // From llx_product + var $ref; // Reference produit + var $product_libelle; // Label produit + var $product_desc; // Description produit + + + /** + * \brief Constructeur d'objets ligne de commande + * \param DB handler d'accès base de donnée + */ + function CommandeLigne($DB) + { + $this->db= $DB ; + } + + /** + * \brief Recupére l'objet ligne de commande + * \param rowid id de la ligne de commande + */ + function fetch($rowid) + { + $sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.description, cd.price, cd.qty, cd.tva_tx,'; + $sql.= ' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice,'; + $sql.= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc, cd.coef, cd.rang,'; + $sql.= ' p.ref as product_ref, p.label as product_libelle, p.description as product_desc'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as cd'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON cd.fk_product = p.rowid'; + $sql.= ' WHERE cd.rowid = '.$rowid; + $result = $this->db->query($sql); + if ($result) + { + $objp = $this->db->fetch_object($result); + $this->rowid = $objp->rowid; + $this->fk_propal = $objp->fk_propal; + $this->desc = $objp->description; + $this->qty = $objp->qty; + $this->price = $objp->price; + $this->subprice = $objp->subprice; + $this->tva_tx = $objp->tva_tx; + $this->remise = $objp->remise; + $this->remise_percent = $objp->remise_percent; + $this->fk_remise_except = $objp->fk_remise_except; + $this->produit_id = $objp->fk_product; + $this->info_bits = $objp->info_bits; + $this->total_ht = $objp->total_ht; + $this->total_tva = $objp->total_tva; + $this->total_ttc = $objp->total_ttc; + $this->coef = $objp->coef; + $this->rang = $objp->rang; - // From llx_commandedet - var $rowid; - var $fk_facture; - var $desc; // Description ligne - var $fk_product; // Id produit prédéfini - - var $qty; - var $tva_tx; - var $subprice; - var $remise_percent; - var $rang = 0; - var $coef; - var $info_bits = 0; // Bit 0: 0 si TVA normal - 1 si TVA NPR - // Bit 1: 0 ligne normale - 1 si ligne de remise fixe - var $total_ht; // Total HT de la ligne toute quantité et incluant la remise ligne - var $total_tva; // Total TVA de la ligne toute quantité et incluant la remise ligne - var $total_ttc; // Total TTC de la ligne toute quantité et incluant la remise ligne - - // Ne plus utiliser - var $remise; - var $price; + $this->ref = $objp->product_ref; + $this->product_libelle= $objp->product_libelle; + $this->product_desc = $objp->product_desc; - // From llx_product - var $ref; // Reference produit - var $product_libelle; // Label produit - var $product_desc; // Description produit + $this->db->free($result); + } + else + { + dolibarr_print_error($this->db); + } + } + + /** + * \brief Insère l'objet ligne de commande en base + * \return int <0 si ko, >0 si ok + */ + function insert() + { + dolibarr_syslog("CommandeLigne.class::insert rang=".$this->rang); + $this->db->begin(); + + $rangtouse=$this->rang; + if ($rangtouse == -1) + { + // Récupère rang max de la commande dans $rangmax + $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'commandedet'; + $sql.= ' WHERE fk_commande ='.$this->fk_commande; + $resql = $this->db->query($sql); + if ($resql) + { + $obj = $this->db->fetch_object($resql); + $rangtouse = $obj->max + 1; + } + else + { + dolibarr_print_error($this->db); + $this->db->rollback(); + return -1; + } + } + + // Insertion dans base de la ligne + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet'; + $sql.= ' (fk_commande, description, price, qty, tva_tx,'; + $sql.= ' fk_product, remise_percent, subprice, remise, fk_remise_except, '; + $sql.= ' rang, coef,'; + $sql.= ' info_bits, total_ht, total_tva, total_ttc)'; + $sql.= " VALUES (".$this->fk_commande.","; + $sql.= " '".addslashes($this->desc)."',"; + $sql.= " '".price2num($this->price)."',"; + $sql.= " '".price2num($this->qty)."',"; + $sql.= " '".price2num($this->tva_tx)."',"; + if ($this->fk_product) { $sql.= "'".$this->fk_product."',"; } + else { $sql.='null,'; } + $sql.= " '".price2num($this->remise_percent)."',"; + $sql.= " '".price2num($this->subprice)."',"; + $sql.= " '".price2num($this->remise)."',"; + if ($this->fk_remise_except) $sql.= $this->fk_remise_except.","; + else $sql.= 'null,'; + $sql.= ' '.$rangtouse.','; + if (isset($this->coef)) $sql.= ' '.$this->coef.','; + else $sql.= ' null,'; + $sql.= " '".$this->info_bits."',"; + $sql.= " '".price2num($this->total_ht)."',"; + $sql.= " '".price2num($this->total_tva)."',"; + $sql.= " '".price2num($this->total_ttc)."'"; + $sql.= ')'; + + dolibarr_syslog("CommandeLigne.class.php::insert sql=$sql"); + + if ($this->fk_product) + { + $product = new Product($this->db); + $product->id = $this->fk_product; + $product->ajust_stock_commande($this->qty, 0); + } - - /** - * \brief Constructeur d'objets ligne de commande - * \param DB handler d'accès base de donnée - */ - function CommandeLigne($DB) - { - $this->db= $DB ; - } - - /** - * \brief Recupére l'objet ligne de commande - * \param rowid id de la ligne de commande - */ - function fetch($rowid) - { - $sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.description, cd.price, cd.qty, cd.tva_tx,'; - $sql.= ' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice,'; - $sql.= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc, cd.coef, cd.rang,'; - $sql.= ' p.ref as product_ref, p.label as product_libelle, p.description as product_desc'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as cd'; - $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON cd.fk_product = p.rowid'; - $sql.= ' WHERE cd.rowid = '.$rowid; - $result = $this->db->query($sql); - if ($result) - { - $objp = $this->db->fetch_object($result); - $this->rowid = $objp->rowid; - $this->fk_propal = $objp->fk_propal; - $this->desc = $objp->description; - $this->qty = $objp->qty; - $this->price = $objp->price; - $this->subprice = $objp->subprice; - $this->tva_tx = $objp->tva_tx; - $this->remise = $objp->remise; - $this->remise_percent = $objp->remise_percent; - $this->fk_remise_except = $objp->fk_remise_except; - $this->produit_id = $objp->fk_product; - $this->info_bits = $objp->info_bits; - $this->total_ht = $objp->total_ht; - $this->total_tva = $objp->total_tva; - $this->total_ttc = $objp->total_ttc; - $this->coef = $objp->coef; - $this->rang = $objp->rang; - - $this->ref = $objp->product_ref; - $this->product_libelle= $objp->product_libelle; - $this->product_desc = $objp->product_desc; - - $this->db->free($result); - } - else - { - dolibarr_print_error($this->db); - } - } - - /** - * \brief Insère l'objet ligne de commande en base - * \return int <0 si ko, >0 si ok - */ - function insert() - { - dolibarr_syslog("CommandeLigne.class::insert rang=".$this->rang); - $this->db->begin(); - - $rangtouse=$this->rang; - if ($rangtouse == -1) - { - // Récupère rang max de la commande dans $rangmax - $sql = 'SELECT max(rang) as max FROM '.MAIN_DB_PREFIX.'commandedet'; - $sql.= ' WHERE fk_commande ='.$this->fk_commande; - $resql = $this->db->query($sql); - if ($resql) - { - $obj = $this->db->fetch_object($resql); - $rangtouse = $obj->max + 1; - } - else - { - dolibarr_print_error($this->db); - $this->db->rollback(); - return -1; - } - } - - // Insertion dans base de la ligne - $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet'; - $sql.= ' (fk_commande, description, price, qty, tva_tx,'; - $sql.= ' fk_product, remise_percent, subprice, remise, fk_remise_except, '; - $sql.= ' rang, coef,'; - $sql.= ' info_bits, total_ht, total_tva, total_ttc)'; - $sql.= " VALUES (".$this->fk_commande.","; - $sql.= " '".addslashes($this->desc)."',"; - $sql.= " '".price2num($this->price)."',"; - $sql.= " '".price2num($this->qty)."',"; - $sql.= " '".price2num($this->tva_tx)."',"; - if ($this->fk_product) { $sql.= "'".$this->fk_product."',"; } - else { $sql.='null,'; } - $sql.= " '".price2num($this->remise_percent)."',"; - $sql.= " '".price2num($this->subprice)."',"; - $sql.= " '".price2num($this->remise)."',"; - if ($this->fk_remise_except) $sql.= $this->fk_remise_except.","; - else $sql.= 'null,'; - $sql.= ' '.$rangtouse.','; - if (isset($this->coef)) $sql.= ' '.$this->coef.','; - else $sql.= ' null,'; - $sql.= " '".$this->info_bits."',"; - $sql.= " '".price2num($this->total_ht)."',"; - $sql.= " '".price2num($this->total_tva)."',"; - $sql.= " '".price2num($this->total_ttc)."'"; - $sql.= ')'; - - dolibarr_syslog("CommandeLigne.class.php::insert sql=$sql"); - - $resql=$this->db->query($sql); - if ($resql) - { - $this->db->commit(); - return 1; - } - else - { - $this->error=$this->db->error(); - dolibarr_syslog("CommandeLigne.class.php::insert Error ".$this->error); - $this->db->rollback(); - return -2; - } - } - - - /** - * \brief Mise a jour de l'objet ligne de commande en base - * \return int <0 si ko, >0 si ok - */ - function update_total() - { - $this->db->begin(); - - // Mise a jour ligne en base - $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET"; - $sql.= " total_ht='".price2num($this->total_ht)."'"; - $sql.= ",total_tva='".price2num($this->total_tva)."'"; - $sql.= ",total_ttc='".price2num($this->total_ttc)."'"; - $sql.= " WHERE rowid = ".$this->rowid; - - dolibarr_syslog("CommandeLigne.class.php::update_total sql=$sql"); - - $resql=$this->db->query($sql); - if ($resql) - { - $this->db->commit(); - return 1; - } - else - { - $this->error=$this->db->error(); - dolibarr_syslog("CommandeLigne.class.php::update_total Error ".$this->error); - $this->db->rollback(); - return -2; - } - } - + + $resql=$this->db->query($sql); + if ($resql) + { + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog("CommandeLigne.class.php::insert Error ".$this->error); + $this->db->rollback(); + return -2; + } + } + + + /** + * \brief Mise a jour de l'objet ligne de commande en base + * \return int <0 si ko, >0 si ok + */ + function update_total() + { + $this->db->begin(); + + // Mise a jour ligne en base + $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET"; + $sql.= " total_ht='".price2num($this->total_ht)."'"; + $sql.= ",total_tva='".price2num($this->total_tva)."'"; + $sql.= ",total_ttc='".price2num($this->total_ttc)."'"; + $sql.= " WHERE rowid = ".$this->rowid; + + dolibarr_syslog("CommandeLigne.class.php::update_total sql=$sql"); + + $resql=$this->db->query($sql); + if ($resql) + { + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog("CommandeLigne.class.php::update_total Error ".$this->error); + $this->db->rollback(); + return -2; + } + } } ?>