From e7ca38a3ea88f16ac9181a0a470574b5c3e807b8 Mon Sep 17 00:00:00 2001 From: marc_ocebo Date: Thu, 15 Sep 2005 00:37:03 +0000 Subject: [PATCH] =?UTF-8?q?Gestion=20des=20r=E9f=E9rences=20client=20dans?= =?UTF-8?q?=20une=20commande=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htdocs/commande/commande.class.php | 1894 ++++++++------- htdocs/commande/fiche.php | 1629 ++++++------- htdocs/commande/index.php | 14 +- htdocs/commande/liste.php | 223 +- htdocs/compta/facture.php | 3469 ++++++++++++++-------------- htdocs/facture.class.php | 2933 ++++++++++++----------- mysql/migration/1.1.0-2.0.0.sql | 2 + 7 files changed, 5046 insertions(+), 5118 deletions(-) diff --git a/htdocs/commande/commande.class.php b/htdocs/commande/commande.class.php index 52eecc52330..734d5c8beae 100644 --- a/htdocs/commande/commande.class.php +++ b/htdocs/commande/commande.class.php @@ -1,1002 +1,950 @@ - - * Copyright (C) 2004-2005 Laurent Destailleur - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * $Id$ - * $Source$ - */ - -/** - \file htdocs/commande/commande.class.php - \ingroup commande - \brief Fichier des classes de commandes - \version $Revision$ -*/ - + + * Copyright (C) 2004-2005 Laurent Destailleur + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ + * $Source$ + */ +/** + \file htdocs/commande/commande.class.php + \ingroup commande + \brief Fichier des classes de commandes + \version $Revision$ +*/ require_once(DOL_DOCUMENT_ROOT."/product.class.php"); - - -/** - \class Commande - \brief Classe de gestion de commande -*/ - -class Commande -{ - var $db ; - var $id ; - var $brouillon; - - // Pour board - var $nbtodo; - var $nbtodolate; - - /** - * \brief Constructeur - * \param DB Handler d'accès base - */ - function Commande($DB) - { - global $langs; - $langs->load("orders"); - - $this->db = $DB; - - $this->statuts[-1] = $langs->trans("StatusOrderCanceled"); - $this->statuts[0] = $langs->trans("StatusOrderDraft"); - $this->statuts[1] = $langs->trans("StatusOrderValidated"); - $this->statuts[2] = $langs->trans("StatusOrderOnProcess"); - $this->statuts[3] = $langs->trans("StatusOrderProcessed"); - - $this->status_label_short[-1] = $langs->trans("StatusOrderCanceled"); - $this->status_label_short[0] = $langs->trans("StatusOrderDraft"); - $this->status_label_short[1] = $langs->trans("StatusOrderValidated"); - $this->status_label_short[2] = $langs->trans("StatusOrderOnProcessShort"); - $this->status_label_short[3] = $langs->trans("StatusOrderProcessed"); - - $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->products = array(); - } - - /** \brief Créé la facture 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) - { - $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(); - - $CommLigne->libelle = $propal->lignes[$i]->libelle; - $CommLigne->price = $propal->lignes[$i]->subprice; - $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->product_id = $propal->lignes[$i]->product_id; - - $this->lines[$i] = $CommLigne; - } - - $this->soc_id = $propal->soc_id; - - /* Définit la société comme un client */ - $soc = new Societe($this->db); - $soc->id = $this->soc_id; - $soc->set_as_client(); - - $this->propale_id = $propal->id; - return $this->create($user); - } - - /** \brief Valide la commande - \param user Utilisateur qui valide - */ - function valid($user) - { - $result = 0; - if ($user->rights->commande->valider) - { - if (defined("COMMANDE_ADDON")) - { - if (is_readable(DOL_DOCUMENT_ROOT ."/includes/modules/commande/".COMMANDE_ADDON.".php")) - { - require_once DOL_DOCUMENT_ROOT ."/includes/modules/commande/".COMMANDE_ADDON.".php"; - - // Definition du nom de module de numerotation de commande - - // \todo Normer le nom des classes des modules de numérotation de ref de commande avec un nom du type NumRefCommandesXxxx - // - //$list=split("_",COMMANDE_ADDON); - //$numrefname=$list[2]; - //$modName = "NumRefCommandes".ucfirst($numrefname); - $modName=COMMANDE_ADDON; - - // Recuperation de la nouvelle reference - $objMod = new $modName($this->db); - $soc = new Societe($this->db); - $soc->fetch($this->soc_id); - $num = $objMod->commande_get_num($soc); - - $sql = "UPDATE ".MAIN_DB_PREFIX."commande SET ref='$num', fk_statut = 1, date_valid=now(), fk_user_valid=$user->id"; - $sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; - - if ($this->db->query($sql) ) - { - $result = 1; - } - else - { - $result = -1; - dolibarr_print_error($this->db); - } - - } - else - { - print "Impossible de lire le module de numérotation"; - } - } - else - { - print "Le module de numérotation n'est pas définit" ; - } - } - return $result ; - } - - /** - * \brief Cloture la commande - * \param user Objet utilisateur qui cloture - * \return int <0 si ko, >0 si ok - */ - function cloture($user) - { - if ($user->rights->commande->valider) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."commande"; - $sql.= " SET fk_statut = 3,"; - $sql.= " fk_user_cloture = ".$user->id.","; - $sql.= " date_cloture = now()"; - $sql.= " WHERE rowid = $this->id AND fk_statut > 0 ;"; - - if ($this->db->query($sql) ) - { - return 1; - } - else - { - dolibarr_print_error($this->db); - return -1; - } - } - } - - /** - * Annule la commande - * - */ - function cancel($user) - { - if ($user->rights->commande->valider) - { - - $sql = "UPDATE ".MAIN_DB_PREFIX."commande SET fk_statut = -1"; - $sql .= " WHERE rowid = $this->id AND fk_statut = 1 ;"; - - if ($this->db->query($sql) ) - { - return 1; - } - else - { - dolibarr_print_error($this->db); - } - } - } - - /** - * Créé la commande - * - */ - function create($user) - { - /* On positionne en mode brouillon la commande */ - $this->brouillon = 1; - - if (! $remise) - { - $remise = 0 ; - } - - if (! $this->projetid) - { - $this->projetid = 0; - } - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande (fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source) "; - $sql .= " VALUES ($this->soc_id, now(), $user->id, $this->projetid, ".$this->db->idate($this->date_commande).", $this->source)"; - - if ( $this->db->query($sql) ) - { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."commande"); - - /* - * Insertion des produits dans la base - */ - for ($i = 0 ; $i < sizeof($this->products) ; $i++) - { - $prod = new Product($this->db, $this->products[$i]); - if ($prod->fetch($this->products[$i])) - { - $this->insert_product_generic($prod->libelle, - $prod->price, - $this->products_qty[$i], - $prod->tva_tx, - $this->products[$i], - $this->products_remise_percent[$i]); - } - } - /* - * - * - */ - - $sql = "UPDATE ".MAIN_DB_PREFIX."commande SET ref='(PROV".$this->id.")' WHERE rowid=".$this->id; - if ($this->db->query($sql)) - { - - if ($this->id && $this->propale_id) - { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."co_pr (fk_commande, fk_propale) VALUES (".$this->id.",".$this->propale_id.")"; - $this->db->query($sql); - } - /* - * Produits - * - */ - for ($i = 0 ; $i < sizeof($this->lines) ; $i++) - { - $result_insert = $this->insert_product_generic( - $this->lines[$i]->libelle, - $this->lines[$i]->price, - $this->lines[$i]->qty, - $this->lines[$i]->tva_tx, - $this->lines[$i]->product_id, - $this->lines[$i]->remise_percent); - - if ( $result_insert < 0) - { - dolibarr_print_error($this->db); - } - } - - /* - * - * - */ - return $this->id; - } - else - { - return -1; - } - } - else - { - dolibarr_print_error($this->db); - return 0; - } - } - /** - * Ajoute un produit - * - */ - function insert_product_generic($p_desc, $p_price, $p_qty, $p_tva_tx=19.6, $p_product_id=0, $remise_percent=0) - { - if ($this->statut == 0) - { - if (strlen(trim($p_qty)) == 0) - { - $p_qty = 1; - } - - $p_price = ereg_replace(",",".",$p_price); - - $price = $p_price; - $subprice = $p_price; - if ($remise_percent > 0) - { - $remise = round(($p_price * $remise_percent / 100), 2); - $price = $p_price - $remise; - } - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."commandedet (fk_commande, fk_product, qty, price, tva_tx, description, remise_percent, subprice) VALUES "; - $sql .= " ('".$this->id."', '$p_product_id','". $p_qty."','". $price."','".$p_tva_tx."','".addslashes($p_desc)."','$remise_percent', '$subprice') ; "; - - if ($this->db->query($sql) ) - { - - if ($this->update_price() > 0) - { - return 1; - } - else - { - return -1; - } - } - else - { - dolibarr_print_error($this->db); - return -2; - } - } - } - - /** - * Ajoute une ligne de commande - * - */ - function addline($desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0) - { - // Nettoyage parametres - $qty = ereg_replace(",",".",$qty); - $pu = ereg_replace(",",".",$pu); - $desc=trim($desc); - if (strlen(trim($qty))==0) - { - $qty=1; - } - - // Verifs - if (! $this->brouillon) return -1; - - $this->db->begin(); - - if ($fk_product > 0) - { - $prod = new Product($this->db, $fk_product); - if ($prod->fetch($fk_product) > 0) - { - $desc = $desc?$desc:$prod->libelle; - $pu = $prod->price; - $txtva = $prod->tva_tx; - } - } - - $remise = 0; - $price = round(ereg_replace(",",".",$pu), 2); - $subprice = $price; - if (trim(strlen($remise_percent)) > 0) - { - $remise = round(($pu * $remise_percent / 100), 2); - $price = $pu - $remise; - } - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."commandedet (fk_commande,label,description,fk_product, price,qty,tva_tx, remise_percent, subprice, remise)"; - $sql .= " VALUES ($this->id, '" . addslashes($desc) . "','" . addslashes($desc) . "',$fk_product,".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."') ;"; - - if ( $this->db->query( $sql) ) - { - $this->update_price(); - $this->db->commit(); - return 1; - } - else - { - $this->error=$this->db->error(); - $this->db->rollback(); - return -1; - } - } - - /** - * Ajoute un produit dans la commande - * - */ - function add_product($idproduct, $qty, $remise_percent=0) - { - if ($idproduct > 0) - { - $i = sizeof($this->products); - $this->products[$i] = $idproduct; - if (!$qty) - { - $qty = 1 ; - } - $this->products_qty[$i] = $qty; - $this->products_remise_percent[$i] = $remise_percent; - } - } - - /** - * Lit une commande - * - */ - 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"; - $sql .= ", ".$this->db->pdate("c.date_commande")." as date_commande, c.fk_projet, c.remise_percent, c.source, c.facture, c.note"; - $sql .= " FROM ".MAIN_DB_PREFIX."commande as c"; - $sql .= " WHERE c.rowid = ".$id; - - $result = $this->db->query($sql) ; - - if ( $result ) - { - $obj = $this->db->fetch_object(); - - $this->id = $obj->rowid; - $this->ref = $obj->ref; - $this->soc_id = $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_percent = $obj->remise_percent; - - $this->source = $obj->source; - $this->facturee = $obj->facture; - $this->note = $obj->note; - $this->projet_id = $obj->fk_projet; - - $this->db->free(); - - if ($this->statut == 0) - $this->brouillon = 1; - - /* - * Propale associée - */ - $sql = "SELECT fk_propale FROM ".MAIN_DB_PREFIX."co_pr WHERE 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 - { - dolibarr_print_error($this->db); - return -1; - } - } - /** - * - * - */ - function fetch_lignes($only_product=0) - { - $this->lignes = array(); - - $sql = "SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_tx, l.remise_percent, l.subprice"; - - if ($only_product==1) - { - $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as l LEFT JOIN ".MAIN_DB_PREFIX."product as p ON (p.rowid = l.fk_product) WHERE l.fk_commande = ".$this->id." AND p.fk_product_type <> 1 ORDER BY l.rowid"; - } - else - { - $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as l WHERE l.fk_commande = $this->id ORDER BY l.rowid"; - } - - $result = $this->db->query($sql); - if ($result) - { - $num = $this->db->num_rows(); - $i = 0; - while ($i < $num) - { - $ligne = new CommandeLigne(); - - $objp = $this->db->fetch_object($result); - - $ligne->id = $objp->rowid; - - $ligne->qty = $objp->qty; - $ligne->price = $objp->price; - $ligne->tva_tx = $objp->tva_tx; - $ligne->subprice = $objp->subprice; - $ligne->remise_percent = $objp->remise_percent; - $ligne->product_id = $objp->fk_product; - $ligne->description = stripslashes($objp->description); - - $this->lignes[$i] = $ligne; - $i++; - } - $this->db->free(); - } - - return $this->lignes; - } - /** - * Renvoie un tableau avec les livraison par ligne - * - * - */ - function livraison_array() - { - $this->livraisons = array(); - - $sql = "SELECT fk_product, sum(ed.qty)"; - $sql .= " FROM ".MAIN_DB_PREFIX."expeditiondet as ed, ".MAIN_DB_PREFIX."commande as c, ".MAIN_DB_PREFIX."commandedet as cd"; - $sql .=" WHERE ed.fk_commande_ligne = cd .rowid AND cd.fk_commande = c.rowid"; - $sql .= " AND cd.fk_commande =" .$this->id; - $sql .= " GROUP BY fk_product "; - $result = $this->db->query($sql); - if ($result) - { - $num = $this->db->num_rows(); - $i = 0; - while ($i < $num) - { - $row = $this->db->fetch_row( $i); - - $this->livraisons[$row[0]] = $row[1]; - - $i++; - } - $this->db->free(); - } - } - /** - * Renvoie un tableau avec les livraison par ligne - * - */ - function nb_expedition() - { - $sql = "SELECT count(*) FROM ".MAIN_DB_PREFIX."expedition as e"; - $sql .=" WHERE e.fk_commande = $this->id"; - - $result = $this->db->query($sql); - if ($result) - { - $row = $this->db->fetch_row(0); - - return $row[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 = "DELETE FROM ".MAIN_DB_PREFIX."commandedet WHERE rowid = $idligne"; - - if ($this->db->query($sql) ) - { - $this->update_price(); - - return 1; - } - else - { - return -1; - } - } - } - - /** - * - * - */ - function set_remise($user, $remise) - { - if ($user->rights->commande->creer) - { - - $remise = ereg_replace(",",".",$remise); - - $sql = "UPDATE ".MAIN_DB_PREFIX."commande 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(); - return 1; - } - else - { - dolibarr_syslog("Commande::set_remise Erreur SQL"); - } - } - } - - /** - * - * - */ - function set_note($user, $note) - { - if ($user->rights->commande->creer) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."commande SET note = '".addslashes($note)."'"; - $sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; - if ($this->db->query($sql)) - { - $this->note = $note; - return 1; - } - else - { - dolibarr_print_error($this->db); - return 0; - } - } - else - { - return 0; - } - } - /** - * \brief Classe la facture comme facturée - * \return int <0 si ko, >0 si ok - */ - function classer_facturee() - { - $sql = "UPDATE ".MAIN_DB_PREFIX."commande SET facture = 1"; - $sql .= " WHERE rowid = ".$this->id." AND fk_statut > 0 ;"; - - if ($this->db->query($sql) ) - { - return 1; - } - else - { - dolibarr_print_error($this->db); - } - } - - /** - * Mettre à jour le prix - * - */ - function update_price() - { - include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php"; - - /* - * Liste des produits a ajouter - */ - $sql = "SELECT price, qty, tva_tx FROM ".MAIN_DB_PREFIX."commandedet WHERE fk_commande = $this->id"; - if ( $this->db->query($sql) ) - { - $num = $this->db->num_rows(); - $i = 0; - - while ($i < $num) - { - $obj = $this->db->fetch_object(); - $products[$i][0] = $obj->price; - $products[$i][1] = $obj->qty; - $products[$i][2] = $obj->tva_tx; - $i++; - } - } - $calculs = calcul_price($products, $this->remise_percent); - - $totalht = $calculs[0]; - $totaltva = $calculs[1]; - $totalttc = $calculs[2]; - $total_remise = $calculs[3]; - - $this->remise = $total_remise; - $this->total_ht = $totalht; - $this->total_tva = $totaltva; - $this->total_ttc = $totalttc; - /* - * - */ - $sql = "UPDATE ".MAIN_DB_PREFIX."commande set"; - $sql .= " amount_ht ='".ereg_replace(",",".",$totalht)."'"; - $sql .= ", total_ht ='".ereg_replace(",",".",$totalht)."'"; - $sql .= ", tva ='".ereg_replace(",",".",$totaltva)."'"; - $sql .= ", total_ttc ='".ereg_replace(",",".",$totalttc)."'"; - $sql .= ", remise ='".ereg_replace(",",".",$total_remise)."'"; - $sql .= " WHERE rowid = $this->id"; - if ( $this->db->query($sql) ) - { - return 1; - } - else - { - print "Erreur mise à jour du prix

".$sql; - return -1; - } - } - - /** - * \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 update_line($rowid, $desc, $pu, $qty, $remise_percent=0, $tva_tx) - { - dolibarr_syslog("Commande::UpdateLine"); - - if ($this->brouillon) - { - $this->db->begin(); - - if (strlen(trim($qty))==0) - { - $qty=1; - } - $remise = 0; - $price = ereg_replace(",",".",$pu); - $subprice = $price; - if (trim(strlen($remise_percent)) > 0) - { - $remise = round(($pu * $remise_percent / 100), 2); - $price = $pu - $remise; - } - else - { - $remise_percent=0; - } - - $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet"; - $sql.= " SET description='".addslashes($desc)."',price=$price,subprice=$subprice,"; - $sql.= " remise=$remise,remise_percent=$remise_percent,qty=$qty,tva_tx='".$tva_tx."'"; - $sql.= " WHERE rowid = ".$rowid; - - $result=$this->db->query( $sql); - if ( $result ) - { - $this->update_price($this->id); - - $this->db->commit(); - - return $result; - } - else - { - $this->db->rollback(); - - dolibarr_print_error($this->db); - return -1; - } - - } - else { - return -2; - } - } - - - /** - * Supprime la commande - * - */ - function delete() - { - $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++; - } - - if ($err == 0) - { - $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; - } - else - { - $this->error=$this->db->error(); - return -1; - } - } - - + +/** + \class Commande + \brief Classe de gestion de commande +*/ +class Commande +{ + var $db ; + var $id ; + var $brouillon; + + // Pour board + var $nbtodo; + var $nbtodolate; + + /** + * \brief Constructeur + * \param DB Handler d'accès base + */ + function Commande($DB) + { + global $langs; + $langs->load('orders'); + $this->db = $DB; + $this->statuts[-1] = $langs->trans('StatusOrderCanceled'); + $this->statuts[0] = $langs->trans('StatusOrderDraft'); + $this->statuts[1] = $langs->trans('StatusOrderValidated'); + $this->statuts[2] = $langs->trans('StatusOrderOnProcess'); + $this->statuts[3] = $langs->trans('StatusOrderProcessed'); + + $this->status_label_short[-1] = $langs->trans('StatusOrderCanceled'); + $this->status_label_short[0] = $langs->trans('StatusOrderDraft'); + $this->status_label_short[1] = $langs->trans('StatusOrderValidated'); + $this->status_label_short[2] = $langs->trans('StatusOrderOnProcessShort'); + $this->status_label_short[3] = $langs->trans('StatusOrderProcessed'); + + $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->products = array(); + } + /** \brief Créé la facture 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) + { + $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(); + $CommLigne->libelle = $propal->lignes[$i]->libelle; + $CommLigne->price = $propal->lignes[$i]->subprice; + $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->product_id = $propal->lignes[$i]->product_id; + $this->lines[$i] = $CommLigne; + } + + $this->soc_id = $propal->soc_id; + + /* Définit la société comme un client */ + $soc = new Societe($this->db); + $soc->id = $this->soc_id; + $soc->set_as_client(); + $this->propale_id = $propal->id; + return $this->create($user); + } + + /** \brief Valide la commande + \param user Utilisateur qui valide + */ + function valid($user) + { + $result = 0; + if ($user->rights->commande->valider) + { + if (defined('COMMANDE_ADDON')) + { + if (is_readable(DOL_DOCUMENT_ROOT .'/includes/modules/commande/'.COMMANDE_ADDON.'.php')) + { + require_once DOL_DOCUMENT_ROOT .'/includes/modules/commande/'.COMMANDE_ADDON.'.php'; + + // Definition du nom de module de numerotation de commande + + // \todo Normer le nom des classes des modules de numérotation de ref de commande avec un nom du type NumRefCommandesXxxx + // + //$list=split('_',COMMANDE_ADDON); + //$numrefname=$list[2]; + //$modName = 'NumRefCommandes'.ucfirst($numrefname); + $modName=COMMANDE_ADDON; + + // Recuperation de la nouvelle reference + $objMod = new $modName($this->db); + $soc = new Societe($this->db); + $soc->fetch($this->soc_id); + $num = $objMod->commande_get_num($soc); + + $sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET ref='$num', fk_statut = 1, date_valid=now(), fk_user_valid=$user->id"; + $sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; + + if ($this->db->query($sql) ) + { + $result = 1; + } + else + { + $result = -1; + dolibarr_print_error($this->db); + } + + } + else + { + print 'Impossible de lire le module de numérotation'; + } + } + else + { + print 'Le module de numérotation n\'est pas défini' ; + } + } + return $result ; + } + /** + * \brief Cloture la commande + * \param user Objet utilisateur qui cloture + * \return int <0 si ko, >0 si ok + */ + function cloture($user) + { + if ($user->rights->commande->valider) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande'; + $sql.= ' SET fk_statut = 3,'; + $sql.= ' fk_user_cloture = '.$user->id.','; + $sql.= ' date_cloture = now()'; + $sql.= " WHERE rowid = $this->id AND fk_statut > 0 ;"; + + if ($this->db->query($sql) ) + { + return 1; + } + else + { + dolibarr_print_error($this->db); + return -1; + } + } + } + /** + * Annule la commande + * + */ + function cancel($user) + { + if ($user->rights->commande->valider) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande SET fk_statut = -1'; + $sql .= " WHERE rowid = $this->id AND fk_statut = 1 ;"; + + if ($this->db->query($sql) ) + { + return 1; + } + else + { + dolibarr_print_error($this->db); + } + } + } + /** + * Créé la commande + * + */ + function create($user) + { + /* On positionne en mode brouillon la commande */ + $this->brouillon = 1; + if (! $remise) + { + $remise = 0 ; + } + + if (! $this->projetid) + { + $this->projetid = 0; + } + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commande (fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note, ref_client) '; + $sql .= ' VALUES ('.$this->soc_id.', now(), '.$user->id.', '.$this->projetid.', '.$this->db->idate($this->date_commande).', '.$this->source.', \''.$this->note.'\', \''.$this->ref_client.'\')'; + + if ( $this->db->query($sql) ) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'commande'); + /* + * Insertion des produits dans la base + */ + for ($i = 0 ; $i < sizeof($this->products) ; $i++) + { + $prod = new Product($this->db, $this->products[$i]); + if ($prod->fetch($this->products[$i])) + { + $this->insert_product_generic($prod->libelle, + $prod->price, + $this->products_qty[$i], + $prod->tva_tx, + $this->products[$i], + $this->products_remise_percent[$i]); + } + } + $sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET ref='(PROV".$this->id.")' WHERE rowid=".$this->id; + if ($this->db->query($sql)) + { + if ($this->id && $this->propale_id) + { + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'co_pr (fk_commande, fk_propale) VALUES ('.$this->id.','.$this->propale_id.')'; + $this->db->query($sql); + } + /* + * Produits + * + */ + for ($i = 0 ; $i < sizeof($this->lines) ; $i++) + { + $result_insert = $this->insert_product_generic( + $this->lines[$i]->libelle, + $this->lines[$i]->price, + $this->lines[$i]->qty, + $this->lines[$i]->tva_tx, + $this->lines[$i]->product_id, + $this->lines[$i]->remise_percent); + if ( $result_insert < 0) + { + dolibarr_print_error($this->db); + } + } + return $this->id; + } + else + { + return -1; + } + } + else + { + dolibarr_print_error($this->db); + return 0; + } + } + /** + * Ajoute un produit + * + */ + function insert_product_generic($p_desc, $p_price, $p_qty, $p_tva_tx=19.6, $p_product_id=0, $remise_percent=0) + { + if ($this->statut == 0) + { + if (strlen(trim($p_qty)) == 0) + { + $p_qty = 1; + } + + $p_price = ereg_replace(',','.',$p_price); + + $price = $p_price; + $subprice = $p_price; + if ($remise_percent > 0) + { + $remise = round(($p_price * $remise_percent / 100), 2); + $price = $p_price - $remise; + } + + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet (fk_commande, fk_product, qty, price, tva_tx, description, remise_percent, subprice) VALUES '; + $sql .= " ('".$this->id."', '$p_product_id','". $p_qty."','". $price."','".$p_tva_tx."','".addslashes($p_desc)."','$remise_percent', '$subprice') ; "; + if ($this->db->query($sql) ) + { + if ($this->update_price() > 0) + { + return 1; + } + else + { + return -1; + } + } + else + { + dolibarr_print_error($this->db); + return -2; + } + } + } + + /** + * Ajoute une ligne de commande + * + */ + function addline($desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0) + { + // Nettoyage parametres + $qty = ereg_replace(',','.',$qty); + $pu = ereg_replace(',','.',$pu); + $desc=trim($desc); + if (strlen(trim($qty))==0) + { + $qty=1; + } + + // Verifs + if (! $this->brouillon) return -1; + + $this->db->begin(); + + if ($fk_product > 0) + { + $prod = new Product($this->db, $fk_product); + if ($prod->fetch($fk_product) > 0) + { + $desc = $desc?$desc:$prod->libelle; + $pu = $prod->price; + $txtva = $prod->tva_tx; + } + } + + $remise = 0; + $price = round(ereg_replace(',','.',$pu), 2); + $subprice = $price; + if (trim(strlen($remise_percent)) > 0) + { + $remise = round(($pu * $remise_percent / 100), 2); + $price = $pu - $remise; + } + + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet (fk_commande,label,description,fk_product, price,qty,tva_tx, remise_percent, subprice, remise)'; + $sql .= " VALUES ($this->id, '" . addslashes($desc) . "','" . addslashes($desc) . "',$fk_product,".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."') ;"; + + if ( $this->db->query( $sql) ) + { + $this->update_price(); + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error(); + $this->db->rollback(); + return -1; + } + } + + /** + * Ajoute un produit dans la commande + * + */ + function add_product($idproduct, $qty, $remise_percent=0) + { + if ($idproduct > 0) + { + $i = sizeof($this->products); + $this->products[$i] = $idproduct; + if (!$qty) + { + $qty = 1 ; + } + $this->products_qty[$i] = $qty; + $this->products_remise_percent[$i] = $remise_percent; + } + } + + /** + * Lit une commande + * + */ + 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'; + $sql .= ', '.$this->db->pdate('c.date_commande').' as date_commande, c.fk_projet, c.remise_percent, c.source, c.facture, c.note, c.ref_client'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'commande as c'; + $sql .= ' WHERE c.rowid = '.$id; + + $result = $this->db->query($sql) ; + if ( $result ) + { + $obj = $this->db->fetch_object(); + $this->id = $obj->rowid; + $this->ref = $obj->ref; + $this->soc_id = $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_percent = $obj->remise_percent; + $this->source = $obj->source; + $this->facturee = $obj->facture; + $this->note = $obj->note; + $this->ref_client = $obj->ref_client; + $this->projet_id = $obj->fk_projet; + $this->db->free(); + if ($this->statut == 0) + $this->brouillon = 1; + /* + * Propale associée + */ + $sql = 'SELECT fk_propale FROM '.MAIN_DB_PREFIX.'co_pr WHERE 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 + { + dolibarr_print_error($this->db); + return -1; + } + } + /** + * + * + */ + function fetch_lignes($only_product=0) + { + $this->lignes = array(); + $sql = 'SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_tx, l.remise_percent, l.subprice'; + if ($only_product==1) + { + $sql .= ' FROM '.MAIN_DB_PREFIX.'commandedet as l LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON (p.rowid = l.fk_product) WHERE l.fk_commande = '.$this->id.' AND p.fk_product_type <> 1 ORDER BY l.rowid'; + } + else + { + $sql .= ' FROM '.MAIN_DB_PREFIX."commandedet as l WHERE l.fk_commande = $this->id ORDER BY l.rowid"; + } + $result = $this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows(); + $i = 0; + while ($i < $num) + { + $ligne = new CommandeLigne(); + $objp = $this->db->fetch_object($result); + $ligne->id = $objp->rowid; + $ligne->qty = $objp->qty; + $ligne->price = $objp->price; + $ligne->tva_tx = $objp->tva_tx; + $ligne->subprice = $objp->subprice; + $ligne->remise_percent = $objp->remise_percent; + $ligne->product_id = $objp->fk_product; + $ligne->description = stripslashes($objp->description); + $this->lignes[$i] = $ligne; + $i++; + } + $this->db->free(); + } + return $this->lignes; + } + /** + * Renvoie un tableau avec les livraison par ligne + * + * + */ + function livraison_array() + { + $this->livraisons = array(); + $sql = 'SELECT fk_product, sum(ed.qty)'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'expeditiondet as ed, '.MAIN_DB_PREFIX.'commande as c, '.MAIN_DB_PREFIX.'commandedet as cd'; + $sql .=' WHERE ed.fk_commande_ligne = cd .rowid AND cd.fk_commande = c.rowid'; + $sql .= ' AND cd.fk_commande =' .$this->id; + $sql .= ' GROUP BY fk_product '; + $result = $this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows(); + $i = 0; + while ($i < $num) + { + $row = $this->db->fetch_row( $i); + $this->livraisons[$row[0]] = $row[1]; + $i++; + } + $this->db->free(); + } + } + /** + * Renvoie un tableau avec les livraison par ligne + * + */ + function nb_expedition() + { + $sql = 'SELECT count(*) FROM '.MAIN_DB_PREFIX.'expedition as e'; + $sql .=" WHERE e.fk_commande = $this->id"; + + $result = $this->db->query($sql); + if ($result) + { + $row = $this->db->fetch_row(0); + return $row[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 = 'DELETE FROM '.MAIN_DB_PREFIX."commandedet WHERE rowid = $idligne"; + if ($this->db->query($sql) ) + { + $this->update_price(); + return 1; + } + else + { + return -1; + } + } + } + + /** + * + * + */ + function set_remise($user, $remise) + { + if ($user->rights->commande->creer) + { + $remise = ereg_replace(',','.',$remise); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande 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(); + return 1; + } + else + { + dolibarr_syslog('Commande::set_remise Erreur SQL'); + } + } + } + + /** + * + * + */ + function set_ref_client($user, $ref_client) + { + if ($user->rights->commande->creer) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande SET ref_client = '.(empty($ref_client) ? 'NULL' : '\''.addslashes($ref_client).'\''); + $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;'; + if ($this->db->query($sql) ) + { + $this->ref_client = $ref_client; + return 1; + } + else + { + dolibarr_syslog('Commande::set_ref_client Erreur SQL'); + } + } + } + /** + * + * + */ + function set_note($user, $note) + { + if ($user->rights->commande->creer) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET note = '".addslashes($note)."'"; + $sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; + if ($this->db->query($sql)) + { + $this->note = $note; + return 1; + } + else + { + dolibarr_print_error($this->db); + return 0; + } + } + else + { + return 0; + } + } + /** + * \brief Classe la facture comme facturée + * \return int <0 si ko, >0 si ok + */ + function classer_facturee() + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande SET facture = 1'; + $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut > 0 ;'; + if ($this->db->query($sql) ) + { + return 1; + } + else + { + dolibarr_print_error($this->db); + } + } + + /** + * Mettre à jour le prix + * + */ + function update_price() + { + include_once DOL_DOCUMENT_ROOT . '/lib/price.lib.php'; + /* + * Liste des produits a ajouter + */ + $sql = 'SELECT price, qty, tva_tx FROM '.MAIN_DB_PREFIX."commandedet WHERE fk_commande = $this->id"; + if ( $this->db->query($sql) ) + { + $num = $this->db->num_rows(); + $i = 0; + while ($i < $num) + { + $obj = $this->db->fetch_object(); + $products[$i][0] = $obj->price; + $products[$i][1] = $obj->qty; + $products[$i][2] = $obj->tva_tx; + $i++; + } + } + $calculs = calcul_price($products, $this->remise_percent); + + $totalht = $calculs[0]; + $totaltva = $calculs[1]; + $totalttc = $calculs[2]; + $total_remise = $calculs[3]; + + $this->remise = $total_remise; + $this->total_ht = $totalht; + $this->total_tva = $totaltva; + $this->total_ttc = $totalttc; + /* + * + */ + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande set'; + $sql .= " amount_ht ='".ereg_replace(',','.',$totalht)."'"; + $sql .= ", total_ht ='".ereg_replace(',','.',$totalht)."'"; + $sql .= ", tva ='".ereg_replace(',','.',$totaltva)."'"; + $sql .= ", total_ttc ='".ereg_replace(',','.',$totalttc)."'"; + $sql .= ", remise ='".ereg_replace(',','.',$total_remise)."'"; + $sql .= " WHERE rowid = $this->id"; + if ( $this->db->query($sql) ) + { + return 1; + } + else + { + print 'Erreur mise à jour du prix

'.$sql; + return -1; + } + } + /** + * \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 update_line($rowid, $desc, $pu, $qty, $remise_percent=0, $tva_tx) + { + dolibarr_syslog('Commande::UpdateLine'); + if ($this->brouillon) + { + $this->db->begin(); + if (strlen(trim($qty))==0) + { + $qty=1; + } + $remise = 0; + $price = ereg_replace(',','.',$pu); + $subprice = $price; + if (trim(strlen($remise_percent)) > 0) + { + $remise = round(($pu * $remise_percent / 100), 2); + $price = $pu - $remise; + } + else + { + $remise_percent=0; + } + + $sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet'; + $sql.= " SET description='".addslashes($desc)."',price=$price,subprice=$subprice,"; + $sql.= " remise=$remise,remise_percent=$remise_percent,qty=$qty,tva_tx='".$tva_tx."'"; + $sql.= ' WHERE rowid = '.$rowid; + + $result=$this->db->query( $sql); + if ( $result ) + { + $this->update_price($this->id); + $this->db->commit(); + return $result; + } + else + { + $this->db->rollback(); + dolibarr_print_error($this->db); + return -1; + } + } + else + { + return -2; + } + } + + /** + * Supprime la commande + * + */ + function delete() + { + $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++; + } + + if ($err == 0) + { + $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; + } + else + { + $this->error=$this->db->error(); + return -1; + } + } + /** * \brief Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord * \param user Objet user * \return int <0 si ko, >0 si ok */ - function load_board($user) - { - global $conf; - - $this->nbtodo=$this->nbtodolate=0; - $sql = "SELECT c.rowid,".$this->db->pdate("c.date_creation")." as datec"; - $sql.= " FROM ".MAIN_DB_PREFIX."commande as c"; - $sql.= " WHERE c.fk_statut BETWEEN 1 AND 2"; - if ($user->societe_id) $sql.=" AND fk_soc = ".$user->societe_id; - $resql=$this->db->query($sql); - if ($resql) - { - while ($obj=$this->db->fetch_object($resql)) - { - $this->nbtodo++; - if ($obj->datec < (time() - $conf->commande->traitement->warning_delay)) $this->nbtodolate++; - } - return 1; - } - else - { - $this->error=$this->db->error(); - return -1; - } - } - - + function load_board($user) + { + global $conf; + + $this->nbtodo=$this->nbtodolate=0; + $sql = 'SELECT c.rowid,'.$this->db->pdate('c.date_creation').' as datec'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'commande as c'; + $sql.= ' WHERE c.fk_statut BETWEEN 1 AND 2'; + if ($user->societe_id) $sql.=' AND fk_soc = '.$user->societe_id; + $resql=$this->db->query($sql); + if ($resql) + { + while ($obj=$this->db->fetch_object($resql)) + { + $this->nbtodo++; + if ($obj->datec < (time() - $conf->commande->traitement->warning_delay)) $this->nbtodolate++; + } + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } + } + + /** * \brief Retourne le libellé du statut de la commande * \return string Libellé */ - function getLibStatut() - { + function getLibStatut() + { return $this->LibStatut($this->statut); - } + } /** * \brief Renvoi le libellé d'un statut donné * \param statut id statut * \return string Libellé */ - function LibStatut($statut) - { - return $this->status_label_short[$statut]; - } + function LibStatut($statut) + { + return $this->status_label_short[$statut]; + } - /** * \brief Charge les informations d'ordre info dans l'objet commande * \param id Id de la commande a charger */ - function info($id) - { - $sql = "SELECT c.rowid, ".$this->db->pdate("date_creation")." as datec,"; - $sql.= " ".$this->db->pdate("date_valid")." as datev,"; - $sql.= " ".$this->db->pdate("date_cloture")." as datecloture,"; - $sql.= " fk_user_author, fk_user_valid, fk_user_cloture"; - $sql.= " FROM ".MAIN_DB_PREFIX."commande as c"; - $sql.= " WHERE c.rowid = ".$id; + function info($id) + { + $sql = 'SELECT c.rowid, '.$this->db->pdate('date_creation').' as datec,'; + $sql.= ' '.$this->db->pdate('date_valid').' as datev,'; + $sql.= ' '.$this->db->pdate('date_cloture').' as datecloture,'; + $sql.= ' fk_user_author, fk_user_valid, fk_user_cloture'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'commande as c'; + $sql.= ' WHERE c.rowid = '.$id; + $result=$this->db->query($sql); + if ($result) + { + if ($this->db->num_rows($result)) + { + $obj = $this->db->fetch_object($result); + $this->id = $obj->rowid; + if ($obj->fk_user_author) + { + $cuser = new User($this->db, $obj->fk_user_author); + $cuser->fetch(); + $this->user_creation = $cuser; + } - $result=$this->db->query($sql); - if ($result) - { - if ($this->db->num_rows($result)) - { - $obj = $this->db->fetch_object($result); + if ($obj->fk_user_valid) + { + $vuser = new User($this->db, $obj->fk_user_valid); + $vuser->fetch(); + $this->user_validation = $vuser; + } - $this->id = $obj->rowid; + if ($obj->fk_user_cloture) + { + $cluser = new User($this->db, $obj->fk_user_cloture); + $cluser->fetch(); + $this->user_cloture = $cluser; + } - if ($obj->fk_user_author) { - $cuser = new User($this->db, $obj->fk_user_author); - $cuser->fetch(); - $this->user_creation = $cuser; - } + $this->date_creation = $obj->datec; + $this->date_validation = $obj->datev; + $this->date_cloture = $obj->datecloture; + } - if ($obj->fk_user_valid) { - $vuser = new User($this->db, $obj->fk_user_valid); - $vuser->fetch(); - $this->user_validation = $vuser; - } - - if ($obj->fk_user_cloture) { - $cluser = new User($this->db, $obj->fk_user_cloture); - $cluser->fetch(); - $this->user_cloture = $cluser; - } + $this->db->free($result); - $this->date_creation = $obj->datec; - $this->date_validation = $obj->datev; - $this->date_cloture = $obj->datecloture; - } + } + else + { + dolibarr_print_error($this->db); + } + } - $this->db->free($result); +} - } - else - { - dolibarr_print_error($this->db); - } - } - -} - - - -/** - \class CommandeLigne - \brief Classe de gestion des lignes de commande -*/ - -class CommandeLigne -{ - function CommandeLigne() - { - } -} - -?> + + +/** + \class CommandeLigne + \brief Classe de gestion des lignes de commande +*/ + +class CommandeLigne +{ + function CommandeLigne() + { + } +} + +?> \ No newline at end of file diff --git a/htdocs/commande/fiche.php b/htdocs/commande/fiche.php index 27cae44468a..3d2d0045b3b 100644 --- a/htdocs/commande/fiche.php +++ b/htdocs/commande/fiche.php @@ -1,6 +1,7 @@ - * Copyright (C) 2004-2005 Laurent Destailleur +/* Copyright (C) 2003-2004 Rodolphe Quiedeville + * Copyright (C) 2004-2005 Laurent Destailleur + * Copyright (C) 2005 Marc Barilley / Ocebo * * 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 @@ -27,195 +28,202 @@ \version $Revision$ */ -require("./pre.inc.php"); +require('./pre.inc.php'); -$langs->load("orders"); -$langs->load("sendings"); -$langs->load("companies"); -$langs->load("bills"); -$langs->load("propal"); +$langs->load('orders'); +$langs->load('sendings'); +$langs->load('companies'); +$langs->load('bills'); +$langs->load('propal'); $user->getrights('commande'); $user->getrights('expedition'); if (!$user->rights->commande->lire) accessforbidden(); -require_once(DOL_DOCUMENT_ROOT."/project.class.php"); -require_once(DOL_DOCUMENT_ROOT."/propal.class.php"); -require_once(DOL_DOCUMENT_ROOT."/commande/commande.class.php"); +require_once(DOL_DOCUMENT_ROOT.'/project.class.php'); +require_once(DOL_DOCUMENT_ROOT.'/propal.class.php'); +require_once(DOL_DOCUMENT_ROOT.'/commande/commande.class.php'); // Sécurité accés client -if ($user->societe_id > 0) +if ($user->societe_id > 0) { - $action = ''; - $socidp = $user->societe_id; + $action = ''; + $socidp = $user->societe_id; } /* * Actions - */ -if ($_POST["action"] == 'classin' && $user->rights->commande->creer) + */ +if ($_POST['action'] == 'classin' && $user->rights->commande->creer) { - $commande = new Commande($db); - $commande->fetch($_GET["id"]); - $commande->classin($_POST["projetid"]); + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $commande->classin($_POST['projetid']); } /* * - */ -if ($_POST["action"] == 'add' && $user->rights->commande->creer) + */ +if ($_POST['action'] == 'add' && $user->rights->commande->creer) { - $datecommande = mktime(12, 0 , 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]); + $datecommande = mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); - $commande = new Commande($db); + $commande = new Commande($db); - $commande->soc_id = $_POST["soc_id"]; - $commande->date_commande = $datecommande; - $commande->note = $_POST["note"]; - $commande->source = $_POST["source_id"]; - $commande->projetid = $_POST["projetid"]; - $commande->remise_percent = $_POST["remise_percent"]; - - $commande->add_product($_POST["idprod1"],$_POST["qty1"],$_POST["remise_percent1"]); - $commande->add_product($_POST["idprod2"],$_POST["qty2"],$_POST["remise_percent2"]); - $commande->add_product($_POST["idprod3"],$_POST["qty3"],$_POST["remise_percent3"]); - $commande->add_product($_POST["idprod4"],$_POST["qty4"],$_POST["remise_percent4"]); - $commande->add_product($_POST["idprod5"],$_POST["qty5"],$_POST["remise_percent5"]); - $commande->add_product($_POST["idprod6"],$_POST["qty6"],$_POST["remise_percent6"]); - $commande->add_product($_POST["idprod7"],$_POST["qty7"],$_POST["remise_percent7"]); - $commande->add_product($_POST["idprod8"],$_POST["qty8"],$_POST["remise_percent8"]); - - $commande_id = $commande->create($user); - - $_GET["id"] = $commande->id; + $commande->soc_id = $_POST['soc_id']; + $commande->date_commande = $datecommande; + $commande->note = $_POST['note']; + $commande->source = $_POST['source_id']; + $commande->projetid = $_POST['projetid']; + $commande->remise_percent = $_POST['remise_percent']; + $commande->ref_client = $_POST['ref_client']; - $action = ''; + $commande->add_product($_POST['idprod1'],$_POST['qty1'],$_POST['remise_percent1']); + $commande->add_product($_POST['idprod2'],$_POST['qty2'],$_POST['remise_percent2']); + $commande->add_product($_POST['idprod3'],$_POST['qty3'],$_POST['remise_percent3']); + $commande->add_product($_POST['idprod4'],$_POST['qty4'],$_POST['remise_percent4']); + $commande->add_product($_POST['idprod5'],$_POST['qty5'],$_POST['remise_percent5']); + $commande->add_product($_POST['idprod6'],$_POST['qty6'],$_POST['remise_percent6']); + $commande->add_product($_POST['idprod7'],$_POST['qty7'],$_POST['remise_percent7']); + $commande->add_product($_POST['idprod8'],$_POST['qty8'],$_POST['remise_percent8']); + + $commande_id = $commande->create($user); + + $_GET['id'] = $commande->id; + + $action = ''; } -if ($_POST["action"] == 'setremise' && $user->rights->commande->creer) +if ($_POST['action'] == 'set_ref_client' && $user->rights->commande->creer) { - $commande = new Commande($db); - $commande->fetch($_GET["id"]); - - $commande->set_remise($user, $_POST["remise"]); -} - -if ($_POST["action"] == 'setnote' && $user->rights->commande->creer) -{ - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $commande->set_note($user, $_POST['note']); + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $commande->set_ref_client($user, $_POST['ref_client']); } -if ($_POST["action"] == 'addligne' && $user->rights->commande->creer) +if ($_POST['action'] == 'setremise' && $user->rights->commande->creer) { - /* - * Ajout d'une ligne produit dans la commande - */ - if ($_POST["qty"] && (($_POST["pu"] && $_POST["desc"]) || $_POST["p_idprod"])) - { - $commande = new Commande($db); - $ret=$commande->fetch($_POST["id"]); - - if (isset($_POST["p_idprod"])) - { - $result = $commande->addline( - $_POST["np_desc"], - $_POST["pu"], - $_POST["qty"], - $_POST["tva_tx"], - $_POST["p_idprod"], - $_POST["remise_percent"]); - } - else - { - $result = $commande->addline( - $_POST["desc"], - $_POST["pu"], - $_POST["qty"], - $_POST["tva_tx"], - 0, - $_POST["remise_percent"]); - } - } + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $commande->set_remise($user, $_POST['remise']); } -if ($_POST["action"] == 'updateligne' && $user->rights->commande->creer && $_POST["save"] == $langs->trans("Save")) +if ($_POST['action'] == 'setnote' && $user->rights->commande->creer) { - $commande = new Commande($db,"",$_POST["id"]); - if (! $commande->fetch($_POST["id"]) > 0) dolibarr_print_error($db); - - $result = $commande->update_line($_POST["elrowid"], - $_POST["eldesc"], - $_POST["elprice"], - $_POST["elqty"], - $_POST["elremise_percent"], - $_POST["eltva_tx"] - ); - - $_GET["id"]=$_POST["id"]; // Pour réaffichage de la fiche en cours d'édition + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $commande->set_note($user, $_POST['note']); } -if ($_POST["action"] == 'updateligne' && $user->rights->commande->creer && $_POST["cancel"] == $langs->trans("Cancel")) +if ($_POST['action'] == 'addligne' && $user->rights->commande->creer) { - Header("Location: fiche.php?id=".$_POST["id"]); // Pour réaffichage de la fiche en cours d'édition - exit; + /* + * Ajout d'une ligne produit dans la commande + */ + if ($_POST['qty'] && (($_POST['pu'] && $_POST['desc']) || $_POST['p_idprod'])) + { + $commande = new Commande($db); + $ret=$commande->fetch($_POST['id']); + + if (isset($_POST['p_idprod'])) + { + $result = $commande->addline( + $_POST['np_desc'], + $_POST['pu'], + $_POST['qty'], + $_POST['tva_tx'], + $_POST['p_idprod'], + $_POST['remise_percent']); + } + else + { + $result = $commande->addline( + $_POST['desc'], + $_POST['pu'], + $_POST['qty'], + $_POST['tva_tx'], + 0, + $_POST['remise_percent']); + } + } } -if ($_GET["action"] == 'deleteline' && $user->rights->commande->creer) +if ($_POST['action'] == 'updateligne' && $user->rights->commande->creer && $_POST['save'] == $langs->trans('Save')) { - $commande = new Commande($db); - $commande->fetch($_GET["id"]); - $result = $commande->delete_line($_GET["lineid"]); - Header("Location: fiche.php?id=".$_GET["id"]); + $commande = new Commande($db,'',$_POST['id']); + if (! $commande->fetch($_POST['id']) > 0) + dolibarr_print_error($db); + $result = $commande->update_line($_POST['elrowid'], + $_POST['eldesc'], + $_POST['elprice'], + $_POST['elqty'], + $_POST['elremise_percent'], + $_POST['eltva_tx'] + ); + + $_GET['id']=$_POST['id']; // Pour réaffichage de la fiche en cours d'édition } -if ($_POST["action"] == 'confirm_valid' && $_POST["confirm"] == 'yes' && $user->rights->commande->valider) +if ($_POST['action'] == 'updateligne' && $user->rights->commande->creer && $_POST['cancel'] == $langs->trans('Cancel')) { - $commande = new Commande($db); - $commande->fetch($_GET["id"]); - $soc = new Societe($db); - $soc->fetch($commande->soc_id); - $result = $commande->valid($user); + Header('Location: fiche.php?id='.$_POST['id']); // Pour réaffichage de la fiche en cours d'édition + exit; } -if ($_POST["action"] == 'confirm_close' && $_POST["confirm"] == 'yes' && $user->rights->commande->creer) +if ($_GET['action'] == 'deleteline' && $user->rights->commande->creer) { - $commande = new Commande($db); - $commande->fetch($_GET["id"]); - $result = $commande->cloture($user); + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $result = $commande->delete_line($_GET['lineid']); + Header('Location: fiche.php?id='.$_GET['id']); } -if ($_POST["action"] == 'confirm_cancel' && $_POST["confirm"] == 'yes' && $user->rights->commande->valider) +if ($_POST['action'] == 'confirm_valid' && $_POST['confirm'] == 'yes' && $user->rights->commande->valider) { - $commande = new Commande($db); - $commande->fetch($_GET["id"]); - $result = $commande->cancel($user); + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $soc = new Societe($db); + $soc->fetch($commande->soc_id); + $result = $commande->valid($user); } -if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == 'yes') +if ($_POST['action'] == 'confirm_close' && $_POST['confirm'] == 'yes' && $user->rights->commande->creer) { - if ($user->rights->commande->supprimer ) - { - $commande = new Commande($db); - $commande->id = $_GET["id"]; - $commande->delete(); - Header("Location: index.php"); - } + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $result = $commande->cloture($user); } -if ($_GET["action"] == 'pdf') +if ($_POST['action'] == 'confirm_cancel' && $_POST['confirm'] == 'yes' && $user->rights->commande->valider) { - /* - * Generation de la commande - * définit dans /includes/modules/commande/modules_commande.php - */ - commande_pdf_create($db, $_GET["id"]); -} + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $result = $commande->cancel($user); +} + +if ($_POST['action'] == 'confirm_delete' && $_POST['confirm'] == 'yes') +{ + if ($user->rights->commande->supprimer ) + { + $commande = new Commande($db); + $commande->id = $_GET['id']; + $commande->delete(); + Header('Location: index.php'); + } +} + +if ($_GET['action'] == 'pdf') +{ + /* + * Generation de la commande + * définit dans /includes/modules/commande/modules_commande.php + */ + commande_pdf_create($db, $_GET['id']); +} -llxHeader('',$langs->trans("OrderCard"),"Commande"); +llxHeader('',$langs->trans('OrderCard'),'Commande'); @@ -226,720 +234,723 @@ $html = new Form($db); * Mode creation * *********************************************************************/ -if ($_GET["action"] == 'create' && $user->rights->commande->creer) +if ($_GET['action'] == 'create' && $user->rights->commande->creer) { - print_titre($langs->trans("CreateOrder")); + print_titre($langs->trans('CreateOrder')); - $new_commande = new Commande($db); + $new_commande = new Commande($db); - if ($propalid) - { - $sql = "SELECT s.nom, s.prefix_comm, s.idp, p.price, p.remise, p.remise_percent, p.tva, p.total, p.ref, ".$db->pdate("p.datep")." as dp, c.id as statut, c.label as lst"; - $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."propal as p, ".MAIN_DB_PREFIX."c_propalst as c"; - $sql .= " WHERE p.fk_soc = s.idp AND p.fk_statut = c.id"; - $sql .= " AND p.rowid = $propalid"; - } - else - { - $sql = "SELECT s.nom, s.prefix_comm, s.idp "; - $sql .= "FROM ".MAIN_DB_PREFIX."societe as s "; - $sql .= "WHERE s.idp = ".$_GET["socidp"]; - } - - $resql = $db->query($sql); - - if ( $resql ) - { - $num = $db->num_rows($resql); - if ($num) + if ($propalid) { - $obj = $db->fetch_object($resql); - - $soc = new Societe($db); - $soc->fetch($obj->idp); - - print '

'; - print ''; - print '' ."\n"; - print ''; - print ''; - - print ''; - - print ''; - print ''; - - $nbrow=3; - if ($conf->projet->enabled) $nbrow++; - print ''; - - print '"; - - if ($conf->projet->enabled) - { - print '"; - } - - print '"; - - if ($propalid > 0) - { - $amount = ($obj->price); - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''; - - print ''; - print ''; - print '"; - print '"; - } - else - { - /* - * Services/produits prédéfinis - */ - $NBLINES=8; - - print ''; - print ''; - } - - /* - * - */ - print ''; - print "\n"; - print "
'.$langs->trans("Ref").' :Provisoire'.$langs->trans("Comments").' :
'.$langs->trans("Customer").' :'.$soc->nom_url.''; - print '
'.$langs->trans("Date").' :'; - $html->select_date(); - print "
'.$langs->trans("Project").' :'; - $html->select_projects($soc->id,'','projetid'); - print "
'.$langs->trans("Source").' :'; - $html->select_array("source_id",$new_commande->sources,2); - print "
'.$langs->trans("Ref").''.$obj->ref.'
'.$langs->trans("TotalTTC").''.price($amount).'
'.$langs->trans("VAT").''.price($obj->tva)."
'.$langs->trans("TotalTTC").''.price($obj->total)."
'.$langs->trans("Services").'/'.$langs->trans("Products").'
'; - - - print ''; - print ''; - for ($i = 1 ; $i <= $NBLINES ; $i++) - { - print ''; - print ''; - print ''; - } - - print '
'.$langs->trans("ProductsAndServices").''.$langs->trans("Qty").''.$langs->trans("Discount").'
'; - print $html->select_produits('','idprod'.$i); - print '%
'; - print '
\n"; - - if ($propalid) - { - /* - * Produits - */ - print_titre($langs->trans("Products")); - - print ''; - print ''; - print ''; - - $var=false; - - $sql = "SELECT pt.rowid, p.label as product, p.ref, pt.price, pt.qty, p.rowid as prodid, pt.remise_percent"; - $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pt, ".MAIN_DB_PREFIX."product as p WHERE pt.fk_product = p.rowid AND pt.fk_propal = $propalid"; - $sql .= " ORDER BY pt.rowid ASC"; - $result = $db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - $i = 0; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print "\n"; - print ''; - print ""; - print ''; - print "\n"; - $i++; - } - } - - $sql = "SELECT pt.rowid, pt.description as product, pt.price, pt.qty, pt.remise_percent"; - $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pt WHERE pt.fk_propal = $propalid AND pt.fk_product = 0"; - $sql .= " ORDER BY pt.rowid ASC"; - if ($db->query($sql)) - { - $num = $db->num_rows(); - $i = 0; - while ($i < $num) - { - $objp = $db->fetch_object(); - $var=!$var; - print "\n"; - print ''; - print ''; - print ''; - print "\n"; - $i++; - } - } - else - { - dolibarr_print_error($db); - } - - print '
'.$langs->trans("Ref").''.$langs->trans("Product").''.$langs->trans("Price").''.$langs->trans("Discount").''.$langs->trans("Qty").'
[$objp->ref]'.img_object($langs->trans("ShowProduct"),"product").' '.$objp->product.'".price($objp->price)."'.$objp->remise_percent.'%".$objp->qty."
 '.img_object($langs->trans("ShowProduct"),"product").' '.$objp->product.''.price($objp->price).''.$objp->remise_percent.'%".$objp->qty."
'; - } + $sql = 'SELECT s.nom, s.prefix_comm, s.idp, p.price, p.remise, p.remise_percent, p.tva, p.total, p.ref, '.$db->pdate('p.datep').' as dp, c.id as statut, c.label as lst'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'societe as s, '.MAIN_DB_PREFIX.'propal as p, '.MAIN_DB_PREFIX.'c_propalst as c'; + $sql .= ' WHERE p.fk_soc = s.idp AND p.fk_statut = c.id'; + $sql .= ' AND p.rowid = '.$propalid; } - } - else + else + { + $sql = 'SELECT s.nom, s.prefix_comm, s.idp '; + $sql .= 'FROM '.MAIN_DB_PREFIX.'societe as s '; + $sql .= 'WHERE s.idp = '.$_GET['socidp']; + } + $resql = $db->query($sql); + if ( $resql ) { - dolibarr_print_error($db); - } -} -else + $num = $db->num_rows($resql); + if ($num) + { + $obj = $db->fetch_object($resql); + + $soc = new Societe($db); + $soc->fetch($obj->idp); + + print '
'; + print ''; + print '' ."\n"; + print ''; + print ''; + + print ''; + + print ''; + print ''; + + $nbrow=4; + if ($conf->projet->enabled) $nbrow++; + print ''; + + print ''; + + if ($conf->projet->enabled) + { + print ''; + } + + print ''; + + print ''; + + if ($propalid > 0) + { + $amount = ($obj->price); + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''; + + print ''; + print ''; + print ''; + print ''; + } + else + { + /* + * Services/produits prédéfinis + */ + $NBLINES=8; + + print ''; + print ''; + } + + /* + * + */ + print ''; + print ''; + print '
'.$langs->trans('Ref').' :Provisoire'.$langs->trans('Comments').' :
'.$langs->trans('Customer').' :'.$soc->nom_url.''; + print '
'.$langs->trans('Date').' :'; + $html->select_date(); + print '
'.$langs->trans('Project').' :'; + $html->select_projects($soc->id,'','projetid'); + print '
'.$langs->trans('Source').' :'; + $html->select_array('source_id',$new_commande->sources,2); + print '
'.$langs->trans('RefCdeClient').' :'; + print ''; + print '
'.$langs->trans('Ref').''.$obj->ref.'
'.$langs->trans('TotalTTC').''.price($amount).'
'.$langs->trans('VAT').''.price($obj->tva).'
'.$langs->trans('TotalTTC').''.price($obj->total).'
'.$langs->trans('Services').'/'.$langs->trans('Products').'
'; + + + print ''; + print ''; + for ($i = 1 ; $i <= $NBLINES ; $i++) + { + print ''; + print ''; + print ''; + } + + print '
'.$langs->trans('ProductsAndServices').''.$langs->trans('Qty').''.$langs->trans('Discount').'
'; + print $html->select_produits('','idprod'.$i); + print '%
'; + print '
'; + + if ($propalid) + { + /* + * Produits + */ + print_titre($langs->trans('Products')); + print ''; + print ''; + print ''; + + $var=false; + + $sql = 'SELECT pt.rowid, p.label as product, p.ref, pt.price, pt.qty, p.rowid as prodid, pt.remise_percent'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt, '.MAIN_DB_PREFIX.'product as p WHERE pt.fk_product = p.rowid AND pt.fk_propal = '.$propalid; + $sql .= ' ORDER BY pt.rowid ASC'; + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + $i = 0; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + } + + $sql = 'SELECT pt.rowid, pt.description as product, pt.price, pt.qty, pt.remise_percent'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt WHERE pt.fk_propal = '.$propalid.' AND pt.fk_product = 0'; + $sql .= ' ORDER BY pt.rowid ASC'; + if ($db->query($sql)) + { + $num = $db->num_rows(); + $i = 0; + while ($i < $num) + { + $objp = $db->fetch_object(); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + } + else + { + dolibarr_print_error($db); + } + + print '
'.$langs->trans('Ref').''.$langs->trans('Product').''.$langs->trans('Price').''.$langs->trans('Discount').''.$langs->trans('Qty').'
['.$objp->ref.']'.img_object($langs->trans('ShowProduct'),'product').' '.$objp->product.''.price($objp->price).''.$objp->remise_percent.'%'.$objp->qty.'
 '.img_object($langs->trans('ShowProduct'),'product').' '.$objp->product.''.price($objp->price).''.$objp->remise_percent.'%'.$objp->qty.'
'; + } + } + } + else + { + dolibarr_print_error($db); + } +} +else /* *************************************************************************** */ /* */ /* Mode vue et edition */ /* */ /* *************************************************************************** */ -{ - $id = $_GET["id"]; - if ($id > 0) - { - $commande = new Commande($db); - if ( $commande->fetch($_GET["id"]) > 0) - { - $soc = new Societe($db); - $soc->fetch($commande->soc_id); - $author = new User($db); - $author->id = $commande->user_author_id; - $author->fetch(); +{ + $id = $_GET['id']; + if ($id > 0) + { + $commande = new Commande($db); + if ( $commande->fetch($_GET['id']) > 0) + { + $soc = new Societe($db); + $soc->fetch($commande->soc_id); + $author = new User($db); + $author->id = $commande->user_author_id; + $author->fetch(); - $h=0; + $h=0; - if ($conf->commande->enabled && $user->rights->commande->lire) - { - $head[$h][0] = DOL_URL_ROOT.'/commande/fiche.php?id='.$commande->id; - $head[$h][1] = $langs->trans("OrderCard"); - $hselected = $h; - $h++; - } - - if ($conf->expedition->enabled && $user->rights->expedition->lire) - { - $head[$h][0] = DOL_URL_ROOT.'/expedition/commande.php?id='.$commande->id; - $head[$h][1] = $langs->trans("SendingCard"); - $h++; - } + if ($conf->commande->enabled && $user->rights->commande->lire) + { + $head[$h][0] = DOL_URL_ROOT.'/commande/fiche.php?id='.$commande->id; + $head[$h][1] = $langs->trans('OrderCard'); + $hselected = $h; + $h++; + } - if ($conf->compta->enabled) - { - $head[$h][0] = DOL_URL_ROOT.'/compta/commande/fiche.php?id='.$commande->id; - $head[$h][1] = $langs->trans("ComptaCard"); - $h++; - } - - $head[$h][0] = DOL_URL_ROOT.'/commande/info.php?id='.$commande->id; - $head[$h][1] = $langs->trans("Info"); - $h++; + if ($conf->expedition->enabled && $user->rights->expedition->lire) + { + $head[$h][0] = DOL_URL_ROOT.'/expedition/commande.php?id='.$commande->id; + $head[$h][1] = $langs->trans('SendingCard'); + $h++; + } - dolibarr_fiche_head($head, $hselected, $langs->trans("Order").": $commande->ref"); + if ($conf->compta->enabled) + { + $head[$h][0] = DOL_URL_ROOT.'/compta/commande/fiche.php?id='.$commande->id; + $head[$h][1] = $langs->trans('ComptaCard'); + $h++; + } - /* - * Confirmation de la suppression de la commande - */ - if ($_GET["action"] == 'delete') - { - $html->form_confirm("fiche.php?id=$id",$langs->trans("DeleteOrder"),$langs->trans("ConfirmDeleteOrder"),"confirm_delete"); - print "
\n"; - } - - /* - * Confirmation de la validation - */ - if ($_GET["action"] == 'valid') - { - //$numfa = commande_get_num($soc); - $html->form_confirm("fiche.php?id=$id",$langs->trans("ValidateOrder"),$langs->trans("ConfirmValidateOrder"),"confirm_valid"); - print "
\n"; - } + $head[$h][0] = DOL_URL_ROOT.'/commande/info.php?id='.$commande->id; + $head[$h][1] = $langs->trans('Info'); + $h++; - /* - * Confirmation de la cloture - */ - if ($_GET["action"] == 'cloture') - { - //$numfa = commande_get_num($soc); - $html->form_confirm("fiche.php?id=$id",$langs->trans("CloseOrder"),$langs->trans("ConfirmCloseOrder"),"confirm_close"); - print "
\n"; - } + dolibarr_fiche_head($head, $hselected, $langs->trans('Order').': '.$commande->ref); - /* - * Confirmation de l'annulation - */ - if ($_GET["action"] == 'annuler') - { - $html->form_confirm("fiche.php?id=$id",$langs->trans("Cancel"),$langs->trans("ConfirmCancel"),"confirm_cancel"); - print "
\n"; - } + /* + * Confirmation de la suppression de la commande + */ + if ($_GET['action'] == 'delete') + { + $html->form_confirm('fiche.php?id='.$id, $langs->trans('DeleteOrder'), $langs->trans('ConfirmDeleteOrder'), 'confirm_delete'); + print '
'; + } - /* - * Commande - */ - print ''; - print '"; - print ''; - print '"; + /* + * Confirmation de la validation + */ + if ($_GET['action'] == 'valid') + { + //$numfa = commande_get_num($soc); + $html->form_confirm('fiche.php?id='.$id, $langs->trans('ValidateOrder'), $langs->trans('ConfirmValidateOrder'), 'confirm_valid'); + print '
'; + } - print ""; - print ''; - print ''; - - print ''; - print "\n"; - $nbrow=6; - if ($conf->projet->enabled) $nbrow++; - print ''; + /* + * Confirmation de la cloture + */ + if ($_GET['action'] == 'cloture') + { + //$numfa = commande_get_num($soc); + $html->form_confirm('fiche.php?id='.$id, $langs->trans('CloseOrder'), $langs->trans('ConfirmCloseOrder'), 'confirm_close'); + print '
'; + } - print ''; - print "\n"; - print ''; + /* + * Confirmation de l'annulation + */ + if ($_GET['action'] == 'annuler') + { + $html->form_confirm('fiche.php?id='.$id, $langs->trans('Cancel'), $langs->trans('ConfirmCancel'), 'confirm_cancel'); + print '
'; + } + + /* + * Commande + */ + print '
'.$langs->trans("Ref")."'.$commande->ref.''.$langs->trans("Source").' : ' . $commande->sources[$commande->source] ; - if ($commande->source == 0) - { - /* Propale */ - $propal = new Propal($db); - $propal->fetch($commande->propale_id); - print ' -> '.$propal->ref.''; - } - print "
".$langs->trans("Customer")."'; - print ''.$soc->nom.'
'.$langs->trans("Status").'".$commande->statuts[$commande->statut]."'.$langs->trans("Note").' :
'; - if ($commande->brouillon == 1 && $user->rights->commande->creer) - { - print '
'; - print ''; - print '
'; - print '
'; - print '
'; - } - else - { - print nl2br($commande->note); - } - print '
'.$langs->trans("Date").'".dolibarr_print_date($commande->date,"%A %d %B %Y")."
'; + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + + print ''; + print ''; + $nbrow=6; + if ($conf->projet->enabled) $nbrow++; + print ''; + + print ''; + print ''; + print ''; // Projet if ($conf->projet->enabled) { - $langs->load("projects"); + $langs->load('projects'); print '"; - } else { + print ''; + } + else + { print ''; } - // Lignes de 3 colonnes - print ''; - print ''; - print ''; - - print ''; + + print ''; + print ''; + print ''; + + print ''; + + print ''; + print ''; + print ''; + print ''; + + print '
'.$langs->trans('Ref').''.$commande->ref.''.$langs->trans('Source').' : ' . $commande->sources[$commande->source] ; + if ($commande->source == 0) + { + /* Propale */ + $propal = new Propal($db); + $propal->fetch($commande->propale_id); + print ' -> '.$propal->ref.''; + } + print '
'.$langs->trans('Customer').''; + print ''.$soc->nom.'
'.$langs->trans('Status').''.$commande->statuts[$commande->statut].''.$langs->trans('Note').' :
'; + if ($commande->brouillon == 1 && $user->rights->commande->creer) + { + print '
'; + print ''; + print '
'; + print '
'; + print '
'; + } + else + { + print nl2br($commande->note); + } + print '
'.$langs->trans('Date').''.dolibarr_print_date($commande->date,'%A %d %B %Y').'
'; print ''; - if ($_GET["action"] != "classer") print ''; + if ($_GET['action'] != 'classer') print ''; print '
'; - print $langs->trans("Project"); + print $langs->trans('Project'); print 'id.'">'.img_edit($langs->trans("SetProject")).''.img_edit($langs->trans('SetProject')).'
'; print '
'; - if ($_GET["action"] == "classer") + if ($_GET['action'] == 'classer') { - $html->form_project($_SERVER["PHP_SELF"]."?id=$commande->id",$commande->soc_id,$commande->projet_id,"projetid"); + $html->form_project($_SERVER['PHP_SELF'].'?id='.$commande->id, $commande->soc_id, $commande->projet_id, 'projetid'); } else { - $html->form_project($_SERVER["PHP_SELF"]."?id=$commande->id",$commande->soc_id,$commande->projet_id,"none"); + $html->form_project($_SERVER['PHP_SELF'].'?id='.$commande->id, $commande->soc_id, $commande->projet_id, 'none'); } - print "  
'.$langs->trans("AmountHT").''.price($commande->total_ht).''.$langs->trans("Currency".$conf->monnaie).'
'.$langs->trans("GlobalDiscount").''; - if ($commande->brouillon == 1 && $user->rights->commande->creer) - { - print '
'; - print ''; - print '%'; - print '
'; - print ''; - } + // Lignes de 3 colonnes + print '
'.$langs->trans('RefCdeClient').' :'; + if ($commande->brouillon == 1 && $user->rights->commande->creer) + { + print '
'; + print ''; + print ''; + print '
'; + print ''; + } + else + { + print $commande->ref_client.' '; + } + print '
'.$langs->trans('AmountHT').''.price($commande->total_ht).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('GlobalDiscount').''; + if ($commande->brouillon == 1 && $user->rights->commande->creer) + { + print '
'; + print ''; + print '%'; + print '
'; + print ''; + } + else + { + print $commande->remise_percent.'% '; + } + print '
'.$langs->trans('VAT').''.price($commande->total_tva).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('TotalTTC').''.price($commande->total_ttc).''.$langs->trans('Currency'.$conf->monnaie).'

'; + + /* + * Lignes de commandes + * + */ + $sql = 'SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_tx, l.remise_percent, l.subprice,'; + $sql.= ' p.label as product, p.ref, p.fk_product_type, p.rowid as prodid'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as l'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product=p.rowid'; + $sql.= ' WHERE l.fk_commande = '.$commande->id; + $sql.= ' ORDER BY l.rowid'; + + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + $i = 0; $total = 0; + + print ''; + if ($num) + { + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + } + $var=true; + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + + // Ligne en mode visu + if ($_GET['action'] != 'editline' || $_GET['rowid'] != $objp->rowid) + { + print ''; + if ($objp->fk_product > 0) + { + print ''; + } + else + { + print ''; + } + print ''; + print ''; + print ''; + if ($objp->remise_percent > 0) + { + print ''; + } + else + { + print ''; + } + print ''; + + // Icone d'edition et suppression + if ($commande->statut == 0 && $user->rights->commande->creer && $_GET['action'] == '') + { + print ''; + print ''; + } + else + { + print ''; + } + print ''; + } + + // Ligne en mode update + if ($_GET['action'] == 'editline' && $user->rights->commande->creer && $_GET['rowid'] == $objp->rowid) + { + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + } + + $total = $total + ($objp->qty * $objp->price); + $i++; + } + $db->free($resql); + } + else + { + dolibarr_print_error($db); + } + + /* + * Ajouter une ligne + */ + if ($commande->statut == 0 && $user->rights->commande->creer && $_GET['action'] == '') + { + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + // Ajout produit produits/services personalisés + print ''; + print ''; + print ''; + + $var=true; + print ''; + print ' '; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + + // Ajout de produits/services prédéfinis + print ''; + print ''; + print ''; + + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + } + print '
'.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('Discount').''.$langs->trans('AmountHT').'  
'; + if ($objp->fk_product_type) print img_object($langs->trans('ShowService'),'service'); + else print img_object($langs->trans('ShowProduct'),'product'); + print ' '.$objp->ref.' - '.stripslashes(nl2br($objp->product)); + print ($objp->description && $objp->description!=$objp->product)?'
'.$objp->description:''; + print '
'.stripslashes(nl2br($objp->description)); + print ''.$objp->tva_tx.'%'.price($objp->subprice).''.$objp->qty.''.$objp->remise_percent.'% '.price($objp->subprice*$objp->qty*(100-$objp->remise_percent)/100).''; + print img_edit(); + print ''; + print img_delete(); + print '  
'; + if ($objp->fk_product > 0) + { + print ''; + if ($objp->fk_product_type) print img_object($langs->trans('ShowService'),'service'); + else print img_object($langs->trans('ShowProduct'),'product'); + print ' '.$objp->ref.''; + print ' - '.stripslashes(nl2br($objp->product)); + print '
'; + } + print '
'; + print $html->select_tva('eltva_tx',$objp->tva_tx); + print '%'; + print '
'.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('Discount').'   
'; + print $html->select_tva('tva_tx',$conf->defaulttx); + print '%
'; + $html->select_produits('','p_idprod','',20); + print '
'; + print ''; + print '
 %
'; + print ''; + + /* + * Boutons actions + */ + if ($user->societe_id == 0 && $commande->statut < 3 && $_GET['action'] == '') + { + print '
'; + if ($conf->expedition->enabled && $commande->statut > 0 && $commande->statut < 3 && $user->rights->expedition->creer) + { + print ''.$langs->trans('Send').''; + } + + if ($commande->statut == 0) + { + if ($user->rights->commande->valider) + { + print ''.$langs->trans('Valid').''; + } + } + + if ($commande->statut == 1 || $commande->statut == 2) + { + if ($user->rights->commande->creer) + { + print ''.$langs->trans('Close').''; + } + } + + if ($commande->statut == 0 && $user->rights->commande->supprimer) + { + print ''.$langs->trans('Delete').''; + } + + if ($commande->statut == 1) + { + $nb_expedition = $commande->nb_expedition(); + if ($user->rights->commande->valider && $nb_expedition == 0) + { + print ''.$langs->trans('CancelOrder').''; + } + } + + print '
'; + } + print '
'; + + + print '
'; + + /* + * Documents générés + * + */ + $file = $conf->commande->dir_output . '/' . $commande->ref . '/' . $commande->ref . '.pdf'; + $relativepath = $commande->ref.'/'.$commande->ref.'.pdf'; + + $var=true; + + if (file_exists($file)) + { + print_titre($langs->trans('Documents')); + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print '
'.$langs->trans('Order').' PDF'.$commande->ref.'.pdf'.filesize($file). ' bytes'.strftime('%d %b %Y %H:%M:%S',filemtime($file)).'
'; + print '
'; + } + + /* + * Liste des factures + */ + $sql = 'SELECT f.rowid,f.facnumber, f.total_ttc, '.$db->pdate('f.datef').' as df'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'facture as f, '.MAIN_DB_PREFIX.'co_fa as cf'; + $sql .= ' WHERE f.rowid = cf.fk_facture AND cf.fk_commande = '. $commande->id; + + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + if ($num) + { + print_titre($langs->trans('RelatedBills')); + $i = 0; $total = 0; + print ''; + print '"; + print ''; + print ''; + print ''; + + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + $i++; + } + print '
'.$langs->trans('Ref')."'.$langs->trans('Date').''.$langs->trans('Price').'
'.img_object($langs->trans('ShowBill'),'bill').' '.$objp->facnumber.''.dolibarr_print_date($objp->df).''.$objp->total_ttc.'
'; + } + } + else + { + dolibarr_print_error($db); + } + print '
'; + + /* + * Liste des expéditions + */ + $sql = 'SELECT e.rowid,e.ref,'.$db->pdate('e.date_expedition').' as de'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'expedition as e'; + $sql .= ' WHERE e.fk_commande = '. $commande->id; + + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + if ($num) + { + print_titre($langs->trans('Sendings')); + $i = 0; $total = 0; + print ''; + print ''; + + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print ''; + print ''; + print ''; + $i++; + } + print '
'.$langs->trans('Sendings').''.$langs->trans('Date').'
'.img_object($langs->trans('ShowSending'),'sending').' '.$objp->ref.''.dolibarr_print_date($objp->de).'
'; + } + } + else + { + dolibarr_print_error($db); + } + print '
'; + } else - { - print $commande->remise_percent.'% '; - } - print ''; - - print ''.$langs->trans("VAT").''.price($commande->total_tva).''; - print ''.$langs->trans("Currency".$conf->monnaie).''; - print ''.$langs->trans("TotalTTC").''.price($commande->total_ttc).''; - print ''.$langs->trans("Currency".$conf->monnaie).''; - - print '
'; - - /* - * Lignes de commandes - * - */ - $sql = 'SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_tx, l.remise_percent, l.subprice,'; - $sql.= ' p.label as product, p.ref, p.fk_product_type, p.rowid as prodid'; - $sql.= ' FROM '.MAIN_DB_PREFIX."commandedet as l"; - $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product=p.rowid'; - $sql.= " WHERE l.fk_commande = ".$commande->id; - $sql.= " ORDER BY l.rowid"; - - $resql = $db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - $i = 0; $total = 0; - - print ''; - if ($num) - { - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print "\n"; - } - $var=true; - while ($i < $num) - { - $objp = $db->fetch_object($resql); - $var=!$var; - - // Ligne en mode visu - if ($_GET['action'] != 'editline' || $_GET['rowid'] != $objp->rowid) - { - print ''; - if ($objp->fk_product > 0) - { - print ''; - } - else - { - print '\n"; - } - print ''; - print '\n"; - print ''; - if ($objp->remise_percent > 0) - { - print '\n"; - } - else - { - print ''; - } - print '\n"; - - // Icone d'edition et suppression - if ($commande->statut == 0 && $user->rights->commande->creer && $_GET["action"] == '') - { - print ''; - print ''; - } - else - { - print ''; - } - print ''; - } - - // Ligne en mode update - if ($_GET["action"] == 'editline' && $user->rights->commande->creer && $_GET["rowid"] == $objp->rowid) - { - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - } - - $total = $total + ($objp->qty * $objp->price); - $i++; - } - $db->free($resql); - } - else - { - dolibarr_print_error($db); - } - - /* - * Ajouter une ligne - */ - if ($commande->statut == 0 && $user->rights->commande->creer && $_GET["action"] == '') - { - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print "\n"; - - // Ajout produit produits/services personalisés - print ""; - print ''; - print ''; - - $var=true; - print '\n"; - print ' '; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - print ''; - - // Ajout de produits/services prédéfinis - print ""; - print ''; - print ''; - - $var=!$var; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print "\n"; - - print ''; - } - - - print '
'.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('Discount').''.$langs->trans('AmountHT').'  
'; - if ($objp->fk_product_type) print img_object($langs->trans('ShowService'),'service'); - else print img_object($langs->trans('ShowProduct'),'product'); - print ' '.$objp->ref.' - '.stripslashes(nl2br($objp->product)); - print ($objp->description && $objp->description!=$objp->product)?'
'.$objp->description:''; - print '
'.stripslashes(nl2br($objp->description)); - print "'.$objp->tva_tx.'%'.price($objp->subprice)."'.$objp->qty.''.$objp->remise_percent."% '.price($objp->subprice*$objp->qty*(100-$objp->remise_percent)/100)."'; - print img_edit(); - print ''; - print img_delete(); - print '  
'; - if ($objp->fk_product > 0) - { - print ''; - if ($objp->fk_product_type) print img_object($langs->trans('ShowService'),'service'); - else print img_object($langs->trans('ShowProduct'),'product'); - print ' '.$objp->ref.''; - print ' - '.stripslashes(nl2br($objp->product)); - print '
'; - } - print '
'; - print $html->select_tva("eltva_tx",$objp->tva_tx); - print '%'; - print '
'.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('Discount').'   
'; - print $html->select_tva("tva_tx",$conf->defaulttx); - print '%
'; - $html->select_produits('','p_idprod','',20); - print '
'; - print ''; - print '
 %
'; - - - print ''; - - - /* - * Boutons actions - */ - if ($user->societe_id == 0 && $commande->statut < 3 && $_GET["action"] == '') - { - print '
'; - - if ($conf->expedition->enabled && $commande->statut > 0 && $commande->statut < 3 && $user->rights->expedition->creer) - { - print ''.$langs->trans("Send").''; - } - - - if ($commande->statut == 0) - { - if ($user->rights->commande->valider) - { - print ''.$langs->trans("Valid").''; - } - } - - if ($commande->statut == 1 || $commande->statut == 2) - { - if ($user->rights->commande->creer) - { - print ''.$langs->trans("Close").''; - } - } - - if ($commande->statut == 0 && $user->rights->commande->supprimer) - { - print ''.$langs->trans("Delete").''; - } - - if ($commande->statut == 1) - { - $nb_expedition = $commande->nb_expedition(); - if ($user->rights->commande->valider && $nb_expedition == 0) - { - print ''.$langs->trans("CancelOrder").''; - } - } - - print "
"; - } - print "
\n"; - - - print '
'; - - /* - * Documents générés - * - */ - $file = $conf->commande->dir_output . "/" . $commande->ref . "/" . $commande->ref . ".pdf"; - $relativepath = $commande->ref."/".$commande->ref.".pdf"; - - $var=true; - - if (file_exists($file)) - { - - print_titre($langs->trans("Documents")); - print ''; - - print ""; - print ''; - print ''; - print ''; - print ''; - - print "
".$langs->trans("Order")." PDF'.$commande->ref.'.pdf'.filesize($file). ' bytes'.strftime("%d %b %Y %H:%M:%S",filemtime($file)).'
\n"; - - print '
'; - } - - /* - * Liste des factures - */ - $sql = "SELECT f.rowid,f.facnumber, f.total_ttc, ".$db->pdate("f.datef")." as df"; - $sql .= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."co_fa as cf"; - $sql .= " WHERE f.rowid = cf.fk_facture AND cf.fk_commande = ". $commande->id; - - $result = $db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - if ($num) - { - print_titre($langs->trans("RelatedBills")); - $i = 0; $total = 0; - print ''; - print '"; - print ''; - print ''; - print "\n"; - - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print ""; - print ''; - print ''; - print ''; - $i++; - } - print "
'.$langs->trans("Ref")."'.$langs->trans("Date").''.$langs->trans("Price").'
'.img_object($langs->trans("ShowBill"),"bill").' '.$objp->facnumber.''.dolibarr_print_date($objp->df).''.$objp->total_ttc.'
"; - } - } - else - { - dolibarr_print_error($db); - } - - - print '
'; - - /* - * Liste des expéditions - */ - $sql = "SELECT e.rowid,e.ref,".$db->pdate("e.date_expedition")." as de"; - $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e"; - $sql .= " WHERE e.fk_commande = ". $commande->id; - - $result = $db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - if ($num) - { - print_titre($langs->trans("Sendings")); - $i = 0; $total = 0; - print ''; - print "\n"; - - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print ""; - print ''; - print "\n"; - $i++; - } - print "
".$langs->trans("Sendings")."".$langs->trans("Date")."
'.img_object($langs->trans("ShowSending"),"sending").' '.$objp->ref.'".dolibarr_print_date($objp->de)."
"; - } - } - else - { - dolibarr_print_error($db); - } - - print "
"; - - } - else - { - /* Commande non trouvée */ - print "Commande inexistante ou accés refusé"; - } - } + { + /* Commande non trouvée */ + print 'Commande inexistante ou accés refusé'; + } + } } $db->close(); diff --git a/htdocs/commande/index.php b/htdocs/commande/index.php index 7e9f5dd158e..144fe149cd9 100644 --- a/htdocs/commande/index.php +++ b/htdocs/commande/index.php @@ -19,7 +19,7 @@ * $Id$ * $Source$ */ - + /** \file htdocs/commande/index.php \ingroup compta @@ -93,9 +93,9 @@ if ( $db->query($sql) ) */ $sql = "SELECT c.rowid, c.ref, s.nom, s.idp FROM ".MAIN_DB_PREFIX."commande as c, ".MAIN_DB_PREFIX."societe as s"; $sql .= " WHERE c.fk_soc = s.idp AND c.fk_statut = 1"; -$sql .= " ORDER BY c.rowid ASC"; +$sql .= " ORDER BY c.rowid DESC"; -if ( $db->query($sql) ) +if ( $db->query($sql) ) { $num = $db->num_rows(); if ($num) @@ -109,7 +109,7 @@ if ( $db->query($sql) ) { $var=!$var; $obj = $db->fetch_object(); - print "rowid\">".img_object($langs->trans("ShowOrder"),"order")." ".$obj->ref.""; + print "rowid\">".img_object($langs->trans("ShowOrder"),"order")." ".$obj->ref.""; print ''.img_object($langs->trans("ShowCompany"),"company").' '.$obj->nom.''; $i++; } @@ -131,7 +131,7 @@ if ($socidp) $sql .= " AND c.fk_soc = $socidp"; } $sql .= " ORDER BY c.rowid DESC"; -if ( $db->query($sql) ) +if ( $db->query($sql) ) { $num = $db->num_rows(); if ($num) @@ -168,7 +168,7 @@ $sql.= " ORDER BY c.tms DESC"; $sql.= $db->plimit($max, 0); $resql=$db->query($sql); -if ($resql) +if ($resql) { $num = $db->num_rows($resql); if ($num) @@ -182,7 +182,7 @@ if ($resql) { $var=!$var; $obj = $db->fetch_object($resql); - print "rowid\">".img_object($langs->trans("ShowOrders"),"order").' '; + print "rowid\">".img_object($langs->trans("ShowOrders"),"order").' '; print $obj->ref.''; print ''.img_object($langs->trans("ShowCompany"),"company").' '.$obj->nom.''; print ''.dolibarr_print_date($obj->datec).''; diff --git a/htdocs/commande/liste.php b/htdocs/commande/liste.php index 4dd6586fd17..959dc236a7a 100644 --- a/htdocs/commande/liste.php +++ b/htdocs/commande/liste.php @@ -1,6 +1,7 @@ - * Copyright (C) 2004-2005 Laurent Destailleur +/* Copyright (C) 2001-2005 Rodolphe Quiedeville + * Copyright (C) 2004-2005 Laurent Destailleur + * Copyright (C) 2005 Marc Barilley / Ocebo * * 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 @@ -28,154 +29,168 @@ */ -require("./pre.inc.php"); +require('./pre.inc.php'); -$langs->load("orders"); +$langs->load('orders'); -if (!$user->rights->commande->lire) accessforbidden(); +if (!$user->rights->commande->lire) + accessforbidden(); -$sref=isset($_GET["sref"])?$_GET["sref"]:$_POST["sref"]; -$snom=isset($_GET["snom"])?$_GET["snom"]:$_POST["snom"]; -$sall=isset($_GET["sall"])?$_GET["sall"]:$_POST["sall"]; +$sref=isset($_GET['sref'])?$_GET['sref']:$_POST['sref']; +$sref_client=isset($_GET['sref_client'])?$_GET['sref_client']:(isset($_POST['sref_client'])?$_POST['sref_client']:''); +$snom=isset($_GET['snom'])?$_GET['snom']:$_POST['snom']; +$sall=isset($_GET['sall'])?$_GET['sall']:$_POST['sall']; // Sécurité accés client -$socidp = $_GET["socidp"]; -if ($user->societe_id > 0) +$socidp = $_GET['socidp']; +if ($user->societe_id > 0) { - $action = ''; - $socidp = $user->societe_id; + $action = ''; + $socidp = $user->societe_id; } llxHeader(); -$begin=$_GET["begin"]; -$sortorder=$_GET["sortorder"]; -$sortfield=$_GET["sortfield"]; +$begin=$_GET['begin']; +$sortorder=$_GET['sortorder']; +$sortfield=$_GET['sortfield']; -if (! $sortfield) $sortfield="c.rowid"; -if (! $sortorder) $sortorder="DESC"; +if (! $sortfield) $sortfield='c.rowid'; +if (! $sortorder) $sortorder='DESC'; $limit = $conf->liste_limit; -$offset = $limit * $_GET["page"] ; +$offset = $limit * $_GET['page'] ; -$sql = "SELECT s.nom, s.idp, c.rowid, c.ref, c.total_ht,".$db->pdate("c.date_commande")." as date_commande, c.fk_statut" ; -$sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."commande as c"; -$sql.= " WHERE c.fk_soc = s.idp"; +$sql = 'SELECT s.nom, s.idp, c.rowid, c.ref, c.total_ht,'.$db->pdate('c.date_commande').' as date_commande, c.fk_statut' ; +$sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s, '.MAIN_DB_PREFIX.'commande as c'; +$sql.= ' WHERE c.fk_soc = s.idp'; if ($sref) { - $sql .= " AND c.ref like '%".$sref."%'"; + $sql .= " AND c.ref like '%".$sref."%'"; } if ($sall) { - $sql .= " AND (c.ref like '%".$sall."%' OR c.note like '%".$sall."%')"; + $sql .= " AND (c.ref like '%".$sall."%' OR c.note like '%".$sall."%')"; } if ($socidp) -{ - $sql .= " AND s.idp = $socidp"; -} -if ($_GET["month"] > 0) { - $sql .= " AND date_format(c.date_commande, '%Y-%m') = '$year-$month'"; + $sql .= ' AND s.idp = '.$socidp; } -if ($_GET["year"] > 0) +if ($_GET['month'] > 0) { - $sql .= " AND date_format(c.date_commande, '%Y') = $year"; + $sql .= " AND date_format(c.date_commande, '%Y-%m') = '$year-$month'"; } -if (isset($_GET["status"])) +if ($_GET['year'] > 0) { - $sql .= " AND fk_statut = ".$_GET["status"]; + $sql .= " AND date_format(c.date_commande, '%Y') = $year"; } -if (isset($_GET["afacturer"])) +if (isset($_GET['status'])) { - $sql .= " AND c.facture = 0"; + $sql .= " AND fk_statut = ".$_GET['status']; } -if (strlen($_POST["sf_ref"]) > 0) +if (isset($_GET['afacturer'])) { - $sql .= " AND c.ref like '%".$_POST["sf_ref"] . "%'"; + $sql .= ' AND c.facture = 0'; +} +if (strlen($_POST['sf_ref']) > 0) +{ + $sql .= " AND c.ref like '%".$_POST['sf_ref'] . "%'"; +} +if (!empty($snom)) +{ + $sql .= ' AND s.nom like \'%'.$snom.'%\''; +} +if (!empty($sref_client)) +{ + $sql .= ' AND c.ref_client like \'%'.$sref_client.'%\''; } -$sql .= " ORDER BY $sortfield $sortorder"; +$sql .= ' ORDER BY '.$sortfield.' '.$sortorder; $sql .= $db->plimit($limit + 1,$offset); $resql = $db->query($sql); if ($resql) { - - if ($socidp) - { - $soc = new Societe($db); - $soc->fetch($socidp); - $title = $langs->trans("ListOfOrders") . " - ".$soc->nom; - } - else - { - $title = $langs->trans("ListOfOrders"); - } - if ($_GET["status"] == 3) $title.=" - ".$langs->trans("StatusOrderToBill"); - - $num = $db->num_rows($resql); - print_barre_liste($title, $_GET["page"], "liste.php","&socidp=$socidp",$sortfield,$sortorder,'',$num); - - $i = 0; - print ''; - print ''; - print_liste_field_titre($langs->trans("Ref"),"liste.php","c.ref","","&socidp=$socidp",'width="15%"',$sortfield); - print_liste_field_titre($langs->trans("Company"),"liste.php","s.nom","","&socidp=$socidp",'width="30%"',$sortfield); - print_liste_field_titre($langs->trans("Date"),"liste.php","c.date_commande","","&socidp=$socidp", 'width="25%" align="right" colspan="2"',$sortfield); - print_liste_field_titre($langs->trans("Status"),"liste.php","c.fk_statut","","&socidp=$socidp",'width="10%" align="center"',$sortfield); - print "\n"; - $var=True; - - $generic_commande = new Commande($db); - - while ($i < min($num,$limit)) - { - $objp = $db->fetch_object($resql); - - $var=!$var; - print ""; - print "\n"; - print "\n"; - - $now = time(); - $lim = 3600 * 24 * 15 ; - - if ( ($now - $objp->date_commande) > $lim && $objp->statutid == 1 ) + if ($socidp) { - print ""; + $soc = new Societe($db); + $soc->fetch($socidp); + $title = $langs->trans('ListOfOrders') . ' - '.$soc->nom; } - else + else { - print ""; + $title = $langs->trans('ListOfOrders'); } - - print "\n"; - - print ''; - print "\n"; - - $total = $total + $objp->price; - $subtotal = $subtotal + $objp->price; - - $i++; - } - - print "
rowid\">".img_object($langs->trans("ShowOrder"),"order")." ".$objp->ref."idp\">".img_object($langs->trans("ShowCompany"),"company")." ".$objp->nom." > 15 jours "; - $y = strftime("%Y",$objp->date_commande); - $m = strftime("%m",$objp->date_commande); - - print strftime("%d",$objp->date_commande)."\n"; - print " "; - print strftime("%B",$objp->date_commande)."\n"; - print " "; - print strftime("%Y",$objp->date_commande)."'.$generic_commande->status_label_short[$objp->fk_statut].'
"; - $db->free($resql); + if ($_GET['status'] == 3) + $title.=' - '.$langs->trans('StatusOrderToBill'); + $num = $db->num_rows($resql); + print_barre_liste($title, $_GET['page'], 'liste.php','&socidp='.$socidp,$sortfield,$sortorder,'',$num); + $i = 0; + print ''; + print ''; + print_liste_field_titre($langs->trans('Ref'),'liste.php','c.ref','','&socidp='.$socidp,'width="25%"',$sortfield); + print_liste_field_titre($langs->trans('Company'),'liste.php','s.nom','','&socidp='.$socidp,'width="30%"',$sortfield); + print_liste_field_titre($langs->trans('RefCdeClient'),'liste.php','c.ref_client','','&socidp='.$socidp,'width="15%"',$sortfield); + print_liste_field_titre($langs->trans('Date'),'liste.php','c.date_commande','','&socidp='.$socidp, 'width="20%" align="right" colspan="2"',$sortfield); + print_liste_field_titre($langs->trans('Status'),'liste.php','c.fk_statut','','&socidp='.$socidp,'width="10%" align="center"',$sortfield); + print ''; + // Lignes des champs de filtre + print ''; + print ''; + print ''; + print ''; + $var=True; + $generic_commande = new Commande($db); + while ($i < min($num,$limit)) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + $now = time(); + $lim = 3600 * 24 * 15 ; + if ( ($now - $objp->date_commande) > $lim && $objp->statutid == 1 ) + { + print ''; + } + else + { + print ''; + } + print ''; + print ''; + print ''; + $total = $total + $objp->price; + $subtotal = $subtotal + $objp->price; + $i++; + } + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ' '; + print ' '; + print ''; + print ''; + print '
'.img_object($langs->trans('ShowOrder'),'order').' '.$objp->ref.''.img_object($langs->trans('ShowCompany'),'company').' '.$objp->nom.''.$objp->ref_client.' > 15 jours '; + $y = strftime('%Y',$objp->date_commande); + $m = strftime('%m',$objp->date_commande); + print strftime('%d',$objp->date_commande); + print ' '; + print strftime('%B',$objp->date_commande).''; + print ' '; + print strftime('%Y',$objp->date_commande).''.$generic_commande->status_label_short[$objp->fk_statut].'
'; + $db->free($resql); } else { - print dolibarr_print_error($db); + print dolibarr_print_error($db); } $db->close(); diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 229964c8a74..fbcf844cab8 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -1,7 +1,8 @@ - * Copyright (C) 2004 Éric Seigne - * Copyright (C) 2004-2005 Laurent Destailleur +/* Copyright (C) 2002-2005 Rodolphe Quiedeville + * Copyright (C) 2004 Éric Seigne + * Copyright (C) 2004-2005 Laurent Destailleur + * Copyright (C) 2005 Marc Barilley / Ocebo * * 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 @@ -22,13 +23,13 @@ */ /** - \file htdocs/compta/facture.php - \ingroup facture - \brief Page de création d'une facture - \version $Revision$ + \file htdocs/compta/facture.php + \ingroup facture + \brief Page de création d'une facture + \version $Revision$ */ -require("./pre.inc.php"); +require('./pre.inc.php'); $user->getrights('facture'); $user->getrights('banque'); @@ -36,26 +37,26 @@ $user->getrights('banque'); if (!$user->rights->facture->lire) accessforbidden(); -$langs->load("bills"); +$langs->load('bills'); -require_once(DOL_DOCUMENT_ROOT."/facture.class.php"); -require_once(DOL_DOCUMENT_ROOT."/paiement.class.php"); -require_once(DOL_DOCUMENT_ROOT."/lib/CMailFile.class.php"); -if ($conf->projet->enabled) require_once(DOL_DOCUMENT_ROOT."/project.class.php"); -if ($conf->propal->enabled) require_once(DOL_DOCUMENT_ROOT."/propal.class.php"); -if ($conf->contrat->enabled) require_once(DOL_DOCUMENT_ROOT."/contrat/contrat.class.php"); -if ($conf->commande->enabled) require_once(DOL_DOCUMENT_ROOT."/commande/commande.class.php"); +require_once(DOL_DOCUMENT_ROOT.'/facture.class.php'); +require_once(DOL_DOCUMENT_ROOT.'/paiement.class.php'); +require_once(DOL_DOCUMENT_ROOT.'/lib/CMailFile.class.php'); +if ($conf->projet->enabled) require_once(DOL_DOCUMENT_ROOT.'/project.class.php'); +if ($conf->propal->enabled) require_once(DOL_DOCUMENT_ROOT.'/propal.class.php'); +if ($conf->contrat->enabled) require_once(DOL_DOCUMENT_ROOT.'/contrat/contrat.class.php'); +if ($conf->commande->enabled) require_once(DOL_DOCUMENT_ROOT.'/commande/commande.class.php'); -$sall=isset($_GET["sall"])?$_GET["sall"]:$_POST["sall"]; -if (isset($_GET["msg"])) { $msg=urldecode($_GET["msg"]); } -if ($_GET["socidp"]) { $socidp=$_GET["socidp"]; } +$sall=isset($_GET['sall'])?$_GET['sall']:$_POST['sall']; +if (isset($_GET['msg'])) { $msg=urldecode($_GET['msg']); } +if ($_GET['socidp']) { $socidp=$_GET['socidp']; } // Sécurité accés client if ($user->societe_id > 0) { - $action = ''; - $socidp = $user->societe_id; + $action = ''; + $socidp = $user->societe_id; } // Nombre de ligne pour choix de produit/service prédéfinis @@ -66,484 +67,493 @@ $NBLINES=4; * Actions */ -if ($_POST["action"] == 'classin') +if ($_POST['action'] == 'classin') { - $facture = new Facture($db); - $facture->fetch($_GET["facid"]); - $facture->classin($_POST["projetid"]); + $facture = new Facture($db); + $facture->fetch($_GET['facid']); + $facture->classin($_POST['projetid']); } -if ($_POST["action"] == 'setmode') +if ($_POST['action'] == 'setmode') { - $facture = new Facture($db); - $facture->fetch($_GET["facid"]); - $result=$facture->mode_reglement($_POST["mode_reglement_id"]); - if ($result < 0) dolibarr_print_error($db,$facture->error); + $facture = new Facture($db); + $facture->fetch($_GET['facid']); + $result=$facture->mode_reglement($_POST['mode_reglement_id']); + if ($result < 0) dolibarr_print_error($db,$facture->error); } -if ($_POST["action"] == 'setconditions') +if ($_POST['action'] == 'setconditions') { - $facture = new Facture($db); - $facture->fetch($_GET["facid"]); - $result=$facture->cond_reglement($_POST["cond_reglement_id"]); - if ($result < 0) dolibarr_print_error($db,$facture->error); + $facture = new Facture($db); + $facture->fetch($_GET['facid']); + $result=$facture->cond_reglement($_POST['cond_reglement_id']); + if ($result < 0) dolibarr_print_error($db,$facture->error); } -if ($_POST["action"] == 'classin') +if ($_POST['action'] == 'classin') { - $facture = new Facture($db); - $facture->fetch($_GET["facid"]); - $facture->classin($_POST["projetid"]); + $facture = new Facture($db); + $facture->fetch($_GET['facid']); + $facture->classin($_POST['projetid']); +} + +if ($_POST['action'] == 'set_ref_client') +{ + $facture = new Facture($db); + $facture->fetch($_GET['facid']); + $facture->set_ref_client($_POST['ref_client']); } /* * Insertion facture */ -if ($_POST["action"] == 'add') +if ($_POST['action'] == 'add') { - $datefacture = mktime(12, 0 , 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]); + $datefacture = mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); - $facture = new Facture($db, $_POST["socid"]); + $facture = new Facture($db, $_POST['socid']); - $facture->number = $_POST["facnumber"]; - $facture->date = $datefacture; - $facture->note = $_POST["note"]; + $facture->number = $_POST['facnumber']; + $facture->date = $datefacture; + $facture->note = $_POST['note']; - if ($_POST["fac_rec"] > 0) - { - // Facture récurrente - $facture->fac_rec = $_POST["fac_rec"]; - $facid = $facture->create($user); - } - else - { - $facture->projetid = $_POST["projetid"]; - $facture->cond_reglement_id = $_POST["cond_reglement_id"]; - $facture->mode_reglement_id = $_POST["mode_reglement_id"]; - $facture->amount = $_POST["amount"]; - $facture->remise = $_POST["remise"]; - $facture->remise_percent = $_POST["remise_percent"]; + if ($_POST['fac_rec'] > 0) + { + // Facture récurrente + $facture->fac_rec = $_POST['fac_rec']; + $facid = $facture->create($user); + } + else + { + $facture->projetid = $_POST['projetid']; + $facture->cond_reglement_id = $_POST['cond_reglement_id']; + $facture->mode_reglement_id = $_POST['mode_reglement_id']; + $facture->amount = $_POST['amount']; + $facture->remise = $_POST['remise']; + $facture->remise_percent = $_POST['remise_percent']; + $facture->ref_client = $_POST['ref_client']; - if (! $_POST["propalid"] && ! $_POST["commandeid"] && ! $_POST["contratid"]) - { - for ($i = 1 ; $i <= $NBLINES ; $i++) - { - if ($_POST["idprod${i}"]) - { - $startday=''; - $endday=''; - if ($_POST["date_start${i}year"] && $_POST["date_start${i}month"] && $_POST["date_start${i}day"]) { - $startday=$_POST["date_start${i}year"].'-'.$_POST["date_start${i}month"].'-'.$_POST["date_start${i}day"]; - } - if ($_POST["date_end${i}year"] && $_POST["date_end${i}month"] && $_POST["date_end${i}day"]) { - $endday=$_POST["date_end${i}year"].'-'.$_POST["date_end${i}month"].'-'.$_POST["date_end${i}day"]; - } - $facture->add_product($_POST["idprod${i}"],$_POST["qty${i}"],$_POST["remise_percent${i}"],$startday,$endday); + if (! $_POST['propalid'] && ! $_POST['commandeid'] && ! $_POST['contratid']) + { + for ($i = 1 ; $i <= $NBLINES ; $i++) + { + if ($_POST['idprod'.$i]) + { + $startday=''; + $endday=''; + if ($_POST['date_start'.$i.'year'] && $_POST['date_start'.$i.'month'] && $_POST['date_start'.$i.'day']) { + $startday=$_POST['date_start'.$i.'year'].'-'.$_POST['date_start'.$i.'month'].'-'.$_POST['date_start'.$i.'day']; + } + if ($_POST['date_end'.$i.'year'] && $_POST['date_end'.$i.'month'] && $_POST['date_end'.$i.'day']) { + $endday=$_POST['date_end'.$i.'year'].'-'.$_POST['date_end'.$i.'month'].'-'.$_POST['date_end'.$i.'day']; + } + $facture->add_product($_POST['idprod'.$i],$_POST['qty'.$i],$_POST['remise_percent'.$i],$startday,$endday); + } + } + $facid = $facture->create($user); - } - } - $facid = $facture->create($user); + if ($facid) + { + Header('Location: facture.php?facid='.$facid); + exit; + } + } + else + { + /* + * Si creation depuis propale + */ + if ($_POST['propalid']) + { + $facture->propalid = $_POST['propalid']; - if ($facid) - { - Header("Location: facture.php?facid=".$facid); - exit; - } - } - else - { - /* - * Si creation depuis propale - */ - if ($_POST["propalid"]) - { - $facture->propalid = $_POST["propalid"]; + $facid = $facture->create($user); - $facid = $facture->create($user); + if ($facid) + { + $prop = New Propal($db); + if ( $prop->fetch($_POST['propalid']) ) + { + for ($i = 0 ; $i < sizeof($prop->lignes) ; $i++) + { + $liblignefac=($prop->lignes[$i]->desc?$prop->lignes[$i]->desc:$prop->lignes[$i]->libelle); - if ($facid) - { - $prop = New Propal($db); - if ( $prop->fetch($_POST["propalid"]) ) - { - for ($i = 0 ; $i < sizeof($prop->lignes) ; $i++) - { - $liblignefac=($prop->lignes[$i]->desc?$prop->lignes[$i]->desc:$prop->lignes[$i]->libelle); - - $result = $facture->addline($facid, - addslashes($liblignefac), - $prop->lignes[$i]->subprice, - $prop->lignes[$i]->qty, - $prop->lignes[$i]->tva_tx, - $prop->lignes[$i]->product_id, - $prop->lignes[$i]->remise_percent); - } - } - else - { - print $langs->trans("UnknownError"); - } - } - } + $result = $facture->addline($facid, + addslashes($liblignefac), + $prop->lignes[$i]->subprice, + $prop->lignes[$i]->qty, + $prop->lignes[$i]->tva_tx, + $prop->lignes[$i]->product_id, + $prop->lignes[$i]->remise_percent); + } + } + else + { + print $langs->trans('UnknownError'); + } + } + } - /* - * Si création depuis commande - */ - if ($_POST["commandeid"]) - { - $facture->commandeid = $_POST["commandeid"]; - $facid = $facture->create($user); + /* + * Si création depuis commande + */ + if ($_POST['commandeid']) + { + $facture->commandeid = $_POST['commandeid']; + $facid = $facture->create($user); + if ($facid) + { + $comm = New Commande($db); + if ( $comm->fetch($_POST['commandeid']) ) + { + $lines = $comm->fetch_lignes(); + for ($i = 0 ; $i < sizeof($lines) ; $i++) + { + $result = $facture->addline($facid, + addslashes($lines[$i]->description), + $lines[$i]->subprice, + $lines[$i]->qty, + $lines[$i]->tva_tx, + $lines[$i]->product_id, + $lines[$i]->remise_percent); + } + } + else + { + print $langs->trans('UnknownError'); + } + } + else + { + dolibarr_print_error($db); + } + } - if ($facid) - { - $comm = New Commande($db); - if ( $comm->fetch($_POST["commandeid"]) ) - { - $lines = $comm->fetch_lignes(); - for ($i = 0 ; $i < sizeof($lines) ; $i++) - { - $result = $facture->addline($facid, - addslashes($lines[$i]->description), - $lines[$i]->subprice, - $lines[$i]->qty, - $lines[$i]->tva_tx, - $lines[$i]->product_id, - $lines[$i]->remise_percent); - } - } - else - { - print $langs->trans("UnknownError"); - } - } - else - { - dolibarr_print_error($db); - } - } + /* + * Si création depuis contrat + */ + if ($_POST['contratid']) + { + $facture->contratid = $_POST['contratid']; + $facid = $facture->create($user); - /* - * Si création depuis contrat - */ - if ($_POST["contratid"]) - { - $facture->contratid = $_POST["contratid"]; - $facid = $facture->create($user); + if ($facid) + { + $contrat = New Contrat($db); + if ( $contrat->fetch($_POST['contratid']) ) + { + $lines = $contrat->fetch_lignes(); + for ($i = 0 ; $i < sizeof($lines) ; $i++) + { + $liblignefac=($contrat->lignes[$i]->desc?$contrat->lignes[$i]->desc:$contrat->lignes[$i]->libelle); - if ($facid) - { - $contrat = New Contrat($db); - if ( $contrat->fetch($_POST["contratid"]) ) - { - $lines = $contrat->fetch_lignes(); - for ($i = 0 ; $i < sizeof($lines) ; $i++) - { - $liblignefac=($contrat->lignes[$i]->desc?$contrat->lignes[$i]->desc:$contrat->lignes[$i]->libelle); + // Plage de dates + $date_start=$contrat->lignes[$i]->date_debut_prevue; + if ($contrat->lignes[$i]->date_debut_reel) $date_start=$contrat->lignes[$i]->date_debut_reel; + $date_end=$contrat->lignes[$i]->date_fin_prevue; + if ($contrat->lignes[$i]->date_fin_reel) $date_end=$contrat->lignes[$i]->date_fin_reel; - // Plage de dates - $date_start=$contrat->lignes[$i]->date_debut_prevue; - if ($contrat->lignes[$i]->date_debut_reel) $date_start=$contrat->lignes[$i]->date_debut_reel; - $date_end=$contrat->lignes[$i]->date_fin_prevue; - if ($contrat->lignes[$i]->date_fin_reel) $date_end=$contrat->lignes[$i]->date_fin_reel; + $result = $facture->addline($facid, + addslashes($liblignefac), + $lines[$i]->subprice, + $lines[$i]->qty, + $lines[$i]->tva_tx, + $lines[$i]->product_id, + $lines[$i]->remise_percent, + $date_start, + $date_end); + } + } + else + { + print $langs->trans('UnknownError'); + } + } + else + { + dolibarr_print_error($db); + } + } - $result = $facture->addline($facid, - addslashes($liblignefac), - $lines[$i]->subprice, - $lines[$i]->qty, - $lines[$i]->tva_tx, - $lines[$i]->product_id, - $lines[$i]->remise_percent, - $date_start, - $date_end); - } - } - else - { - print $langs->trans("UnknownError"); - } - } - else - { - dolibarr_print_error($db); - } - } - - // Fin création facture, on l'affiche - if ($facid) - { - Header("Location: facture.php?facid=".$facid); - exit; - } - } - } + // Fin création facture, on l'affiche + if ($facid) + { + Header('Location: facture.php?facid='.$facid); + exit; + } + } + } } /* * */ -if ($_POST["action"] == 'confirm_valid' && $_POST["confirm"] == 'yes' && $user->rights->facture->valider) +if ($_POST['action'] == 'confirm_valid' && $_POST['confirm'] == 'yes' && $user->rights->facture->valider) { - $fac = new Facture($db); - $fac->fetch($_GET["facid"]); - $soc = new Societe($db); - $soc->fetch($fac->socidp); - $result = $fac->set_valid($fac->id, $user, $soc); - if ($result) - { - facture_pdf_create($db, $fac->id); - } + $fac = new Facture($db); + $fac->fetch($_GET['facid']); + $soc = new Societe($db); + $soc->fetch($fac->socidp); + $result = $fac->set_valid($fac->id, $user, $soc); + if ($result) + { + facture_pdf_create($db, $fac->id); + } } -if ($_GET["action"] == 'payed' && $user->rights->facture->paiement) +if ($_GET['action'] == 'payed' && $user->rights->facture->paiement) { - $fac = new Facture($db); - $result = $fac->set_payed($_GET["facid"]); + $fac = new Facture($db); + $result = $fac->set_payed($_GET['facid']); } -if ($_POST["action"] == 'setremise' && $user->rights->facture->creer) +if ($_POST['action'] == 'setremise' && $user->rights->facture->creer) { - $fac = new Facture($db); - $fac->fetch($_GET["facid"]); + $fac = new Facture($db); + $fac->fetch($_GET['facid']); - $fac->set_remise($user, $_POST["remise"]); + $fac->set_remise($user, $_POST['remise']); } -if ($_POST["action"] == 'addligne' && $user->rights->facture->creer) +if ($_POST['action'] == 'addligne' && $user->rights->facture->creer) { - if ($_POST["qty"] && (($_POST["pu"]>=0 && $_POST["desc"]) || $_POST["idprod"])) - { - $fac = new Facture($db); - $fac->fetch($_POST["facid"]); - $datestart=''; - $dateend=''; - if ($_POST["date_startyear"] && $_POST["date_startmonth"] && $_POST["date_startday"]) { - $datestart=$_POST["date_startyear"].'-'.$_POST["date_startmonth"].'-'.$_POST["date_startday"]; - } - if ($_POST["date_endyear"] && $_POST["date_endmonth"] && $_POST["date_endday"]) { - $dateend=$_POST["date_endyear"].'-'.$_POST["date_endmonth"].'-'.$_POST["date_endday"]; - } - $result = $fac->addline($_POST["facid"], - $_POST["desc"], - $_POST["pu"], - $_POST["qty"], - $_POST["tva_tx"], - 0, - $_POST["remise_percent"], - $datestart, - $dateend - ); - - } + if ($_POST['qty'] && (($_POST['pu']>=0 && $_POST['desc']) || $_POST['idprod'])) + { + $fac = new Facture($db); + $fac->fetch($_POST['facid']); + $datestart=''; + $dateend=''; + if ($_POST['date_startyear'] && $_POST['date_startmonth'] && $_POST['date_startday']) + { + $datestart=$_POST['date_startyear'].'-'.$_POST['date_startmonth'].'-'.$_POST['date_startday']; + } + if ($_POST['date_endyear'] && $_POST['date_endmonth'] && $_POST['date_endday']) + { + $dateend=$_POST['date_endyear'].'-'.$_POST['date_endmonth'].'-'.$_POST['date_endday']; + } + $result = $fac->addline($_POST['facid'], + $_POST['desc'], + $_POST['pu'], + $_POST['qty'], + $_POST['tva_tx'], + 0, + $_POST['remise_percent'], + $datestart, + $dateend + ); + } - $_GET["facid"]=$_POST["facid"]; // Pour réaffichage de la fiche en cours d'édition + $_GET['facid']=$_POST['facid']; // Pour réaffichage de la fiche en cours d'édition } -if ($_POST["action"] == 'updateligne' && $user->rights->facture->creer && $_POST["save"] == $langs->trans("Save")) +if ($_POST['action'] == 'updateligne' && $user->rights->facture->creer && $_POST['save'] == $langs->trans('Save')) { - $fac = new Facture($db,"",$_POST["facid"]); - $fac->fetch($_POST["facid"]); + $fac = new Facture($db,'',$_POST['facid']); + $fac->fetch($_POST['facid']); - $datestart=''; - $dateend=''; - if ($_POST["date_startyear"] && $_POST["date_startmonth"] && $_POST["date_startday"]) { - $datestart=$_POST["date_startyear"].'-'.$_POST["date_startmonth"].'-'.$_POST["date_startday"]; - } - if ($_POST["date_endyear"] && $_POST["date_endmonth"] && $_POST["date_endday"]) { - $dateend=$_POST["date_endyear"].'-'.$_POST["date_endmonth"].'-'.$_POST["date_endday"]; - } + $datestart=''; + $dateend=''; + if ($_POST['date_startyear'] && $_POST['date_startmonth'] && $_POST['date_startday']) { + $datestart=$_POST['date_startyear'].'-'.$_POST['date_startmonth'].'-'.$_POST['date_startday']; + } + if ($_POST['date_endyear'] && $_POST['date_endmonth'] && $_POST['date_endday']) { + $dateend=$_POST['date_endyear'].'-'.$_POST['date_endmonth'].'-'.$_POST['date_endday']; + } - $result = $fac->updateline($_POST["rowid"], - $_POST["desc"], - $_POST["price"], - $_POST["qty"], - $_POST["remise_percent"], - $datestart, - $dateend, - $_POST["tva_tx"] - ); + $result = $fac->updateline($_POST['rowid'], + $_POST['desc'], + $_POST['price'], + $_POST['qty'], + $_POST['remise_percent'], + $datestart, + $dateend, + $_POST['tva_tx'] + ); - $_GET["facid"]=$_POST["facid"]; // Pour réaffichage de la fiche en cours d'édition + $_GET['facid']=$_POST['facid']; // Pour réaffichage de la fiche en cours d'édition } -if ($_POST["action"] == 'updateligne' && $user->rights->facture->creer && $_POST["cancel"] == $langs->trans("Cancel")) +if ($_POST['action'] == 'updateligne' && $user->rights->facture->creer && $_POST['cancel'] == $langs->trans('Cancel')) { - Header("Location: facture.php?facid=".$_POST["facid"]); // Pour réaffichage de la fiche en cours d'édition - exit; + Header('Location: facture.php?facid='.$_POST['facid']); // Pour réaffichage de la fiche en cours d'édition + exit; } -if ($_GET["action"] == 'deleteline' && $user->rights->facture->creer) +if ($_GET['action'] == 'deleteline' && $user->rights->facture->creer) { - $fac = new Facture($db,"",$_GET["facid"]); - $fac->fetch($_GET["facid"]); - $result = $fac->deleteline($_GET["rowid"]); + $fac = new Facture($db,'',$_GET['facid']); + $fac->fetch($_GET['facid']); + $result = $fac->deleteline($_GET['rowid']); } -if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == 'yes') +if ($_POST['action'] == 'confirm_delete' && $_POST['confirm'] == 'yes') { - if ($user->rights->facture->supprimer ) - { - $fac = new Facture($db); - $fac->delete($_GET["facid"]); - $_GET["facid"] = 0 ; - Header("Location: facture.php"); - exit; - } + if ($user->rights->facture->supprimer ) + { + $fac = new Facture($db); + $fac->delete($_GET['facid']); + $_GET['facid'] = 0 ; + Header('Location: facture.php'); + exit; + } } -if ($_POST["action"] == 'confirm_canceled' && $_POST["confirm"] == 'yes') +if ($_POST['action'] == 'confirm_canceled' && $_POST['confirm'] == 'yes') { - if ($user->rights->facture->supprimer ) - { - $fac = new Facture($db); - $result = $fac->set_canceled($_GET["facid"]); - $_GET["facid"] = 0 ; - Header("Location: facture.php"); - exit; - } + if ($user->rights->facture->supprimer ) + { + $fac = new Facture($db); + $result = $fac->set_canceled($_GET['facid']); + $_GET['facid'] = 0 ; + Header('Location: facture.php'); + exit; + } } /* * Ordonnancement des lignes */ -if ($_GET["action"] == 'up' && $user->rights->facture->creer) +if ($_GET['action'] == 'up' && $user->rights->facture->creer) { - $fac = new Facture($db,"",$_GET["facid"]); - $fac->line_up($_GET["rowid"]); + $fac = new Facture($db,'',$_GET['facid']); + $fac->line_up($_GET['rowid']); } -if ($_GET["action"] == 'down' && $user->rights->facture->creer) +if ($_GET['action'] == 'down' && $user->rights->facture->creer) { - $fac = new Facture($db,"",$_GET["facid"]); - $fac->line_down($_GET["rowid"]); + $fac = new Facture($db,'',$_GET['facid']); + $fac->line_down($_GET['rowid']); } /* * Action envoi de mail */ -if ($_POST["action"] == 'send' || $_POST["action"] == 'relance') +if ($_POST['action'] == 'send' || $_POST['action'] == 'relance') { - $langs->load("mails"); + $langs->load('mails'); - $fac = new Facture($db,"",$_POST["facid"]); - if ( $fac->fetch($_POST["facid"]) ) - { - $facref = sanitize_string($fac->ref); - $file = $conf->facture->dir_output . "/" . $facref . "/" . $facref . ".pdf"; + $fac = new Facture($db,'',$_POST['facid']); + if ( $fac->fetch($_POST['facid']) ) + { + $facref = sanitize_string($fac->ref); + $file = $conf->facture->dir_output . '/' . $facref . '/' . $facref . '.pdf'; - if (is_readable($file)) - { - $soc = new Societe($db, $fac->socidp); + if (is_readable($file)) + { + $soc = new Societe($db, $fac->socidp); - if ($_POST["sendto"]) { - // Le destinataire a été fourni via le champ libre - $sendto = $_POST["sendto"]; - $sendtoid = 0; - } - elseif ($_POST["receiver"]) { - // Le destinataire a été fourni via la liste déroulante - $sendto = $soc->contact_get_email($_POST["receiver"]); - $sendtoid = $_POST["receiver"]; - } + if ($_POST['sendto']) { + // Le destinataire a été fourni via le champ libre + $sendto = $_POST['sendto']; + $sendtoid = 0; + } + elseif ($_POST['receiver']) { + // Le destinataire a été fourni via la liste déroulante + $sendto = $soc->contact_get_email($_POST['receiver']); + $sendtoid = $_POST['receiver']; + } - if (strlen($sendto)) - { - $from = $_POST["fromname"] . " <" . $_POST["frommail"] .">"; - $replyto = $_POST["replytoname"]. " <" . $_POST["replytomail"].">"; - $message = $_POST["message"]; - if ($_POST["action"] == 'send') { - $subject = $langs->trans("Bill")." $fac->ref"; - $actiontypeid=9; - $actionmsg ="Mail envoyé par $from à $sendto.
"; - if ($message) { - $actionmsg.="Texte utilisé dans le corps du message:
"; - $actionmsg.="$message"; - } - $actionmsg2="Envoi facture par mail"; - } - if ($_POST["action"] == 'relance') { - $subject = "Relance facture $fac->ref"; - $actiontypeid=10; - $actionmsg="Mail envoyé par $from à $sendto.
"; - if ($message) { - $actionmsg.="Texte utilisé dans le corps du message:
"; - $actionmsg.="$message"; - } - $actionmsg2="Relance facture par mail"; - } + if (strlen($sendto)) + { + $from = $_POST['fromname'] . ' <' . $_POST['frommail'] .'>'; + $replyto = $_POST['replytoname']. ' <' . $_POST['replytomail'].'>'; + $message = $_POST['message']; + if ($_POST['action'] == 'send') + { + $subject = $langs->trans('Bill').' '.$fac->ref; + $actiontypeid=9; + $actionmsg ='Mail envoyé par '.$from.' à '.$sendto.'.
'; + if ($message) { + $actionmsg.='Texte utilisé dans le corps du message:
'; + $actionmsg.=$message; + } + $actionmsg2='Envoi facture par mail'; + } + if ($_POST['action'] == 'relance') + { + $subject = 'Relance facture '.$fac->ref; + $actiontypeid=10; + $actionmsg='Mail envoyé par '.$from.' à '.$sendto.'.
'; + if ($message) { + $actionmsg.='Texte utilisé dans le corps du message:
'; + $actionmsg.=$message; + } + $actionmsg2='Relance facture par mail'; + } - $filepath[0] = $file; - $filename[0] = $fac->ref.".pdf"; - $mimetype[0] = "application/pdf"; - $filepath[1] = $_FILES['addedfile']['tmp_name']; - $filename[1] = $_FILES['addedfile']['name']; - $mimetype[1] = $_FILES['addedfile']['type']; + $filepath[0] = $file; + $filename[0] = $fac->ref.'.pdf'; + $mimetype[0] = 'application/pdf'; + $filepath[1] = $_FILES['addedfile']['tmp_name']; + $filename[1] = $_FILES['addedfile']['name']; + $mimetype[1] = $_FILES['addedfile']['type']; - // Envoi de la facture - $mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc); + // Envoi de la facture + $mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc); - if ($mailfile->sendfile()) - { - $msg='
'.$langs->trans("MailSuccessfulySent",$from,$sendto).'.
'; + if ($mailfile->sendfile()) + { + $msg='
'.$langs->trans('MailSuccessfulySent',$from,$sendto).'.
'; - // Insertion action - require_once(DOL_DOCUMENT_ROOT."/contact.class.php"); - include_once("../actioncomm.class.php"); - $actioncomm = new ActionComm($db); - $actioncomm->type_id = $actiontypeid; - $actioncomm->label = $actionmsg2; - $actioncomm->note = $actionmsg; - $actioncomm->date = time(); - $actioncomm->percent = 100; - $actioncomm->contact = new Contact($db,$sendtoid); - $actioncomm->societe = new Societe($db,$fac->socidp); - $actioncomm->user = $user; // User qui a fait l'action - $actioncomm->facid = $fac->id; + // Insertion action + require_once(DOL_DOCUMENT_ROOT.'/contact.class.php'); + include_once(DOL_DOCUMENT_ROOT.'/actioncomm.class.php'); + $actioncomm = new ActionComm($db); + $actioncomm->type_id = $actiontypeid; + $actioncomm->label = $actionmsg2; + $actioncomm->note = $actionmsg; + $actioncomm->date = time(); + $actioncomm->percent = 100; + $actioncomm->contact = new Contact($db,$sendtoid); + $actioncomm->societe = new Societe($db,$fac->socidp); + $actioncomm->user = $user; // User qui a fait l'action + $actioncomm->facid = $fac->id; - $ret=$actioncomm->add($user); // User qui saisi l'action + $ret=$actioncomm->add($user); // User qui saisit l'action - if ($ret < 0) - { - dolibarr_print_error($db); - } - else - { - // Renvoie sur la fiche - Header("Location: facture.php?facid=".$fac->id."&msg=".urlencode($msg)); - exit; - } - } - else - { - $msg='
'.$langs->trans("ErrorFailedToSendMail",$from,$sendto).' !
'; - } - } - else - { - $msg='
'.$langs->trans("ErrorMailRecipientIsEmpty").'
'; - dolibarr_syslog("Le mail du destinataire est vide"); - } + if ($ret < 0) + { + dolibarr_print_error($db); + } + else + { + // Renvoie sur la fiche + Header('Location: facture.php?facid='.$fac->id.'&msg='.urlencode($msg)); + exit; + } + } + else + { + $msg='
'.$langs->trans('ErrorFailedToSendMail',$from,$sendto).' !
'; + } + } + else + { + $msg='
'.$langs->trans('ErrorMailRecipientIsEmpty').'
'; + dolibarr_syslog('Le mail du destinataire est vide'); + } - } - else - { - dolibarr_syslog("Impossible de lire :".$file); - } - } - else - { - dolibarr_syslog("Impossible de lire les données de la facture. Le fichier facture n'a peut-être pas été généré."); - } + } + else + { + dolibarr_syslog('Impossible de lire :'.$file); + } + } + else + { + dolibarr_syslog('Impossible de lire les données de la facture. Le fichier facture n\'a peut-être pas été généré.'); + } } /* * Générer ou regénérer le PDF */ -if ($_GET["action"] == 'pdf') +if ($_GET['action'] == 'pdf') { - // Generation de la facture définie dans /includes/modules/facture/modules_facture.php - // Génère également le fichier meta dans le meme répertoire (pour faciliter les recherches et indexation) - facture_pdf_create($db, $_GET["facid"]); + // Generation de la facture définie dans /includes/modules/facture/modules_facture.php + // Génère également le fichier meta dans le meme répertoire (pour faciliter les recherches et indexation) + facture_pdf_create($db, $_GET['facid']); } @@ -556,25 +566,25 @@ if ($_GET["action"] == 'pdf') **********************************************************************/ function print_date_range($date_start,$date_end) { - global $langs; - - if ($date_start && $date_end) - { - print ' ('.$langs->trans("DateFromTo",dolibarr_print_date($date_start),dolibarr_print_date($date_end)).')'; - } - if ($date_start && ! $date_end) - { - print ' ('.$langs->trans("DateFrom",dolibarr_print_date($date_start)).')'; - } - if (! $date_start && $date_end) - { - print ' ('.$langs->trans("DateUntil",dolibarr_print_date($date_end)).')'; - } + global $langs; + + if ($date_start && $date_end) + { + print ' ('.$langs->trans('DateFromTo',dolibarr_print_date($date_start),dolibarr_print_date($date_end)).')'; + } + if ($date_start && ! $date_end) + { + print ' ('.$langs->trans('DateFrom',dolibarr_print_date($date_start)).')'; + } + if (! $date_start && $date_end) + { + print ' ('.$langs->trans('DateUntil',dolibarr_print_date($date_end)).')'; + } } -llxHeader('',$langs->trans("Bill"),'Facture'); +llxHeader('',$langs->trans('Bill'),'Facture'); $html = new Form($db); @@ -584,1447 +594,1462 @@ $html = new Form($db); * Mode creation * **********************************************************************/ -if ($_GET["action"] == 'create') +if ($_GET['action'] == 'create') { - print_titre($langs->trans("NewBill")); + print_titre($langs->trans('NewBill')); - $soc = new Societe($db); + $soc = new Societe($db); - if ($_GET["propalid"]) - { - $propal = New Propal($db); - $propal->fetch($_GET["propalid"]); - $societe_id = $propal->soc_id; - $projet=$propal->projetidp; - $soc->fetch($societe_id); - } - elseif ($_GET["commandeid"]) - { - $commande = New Commande($db); - $commande->fetch($_GET["commandeid"]); - $societe_id = $commande->soc_id; - $projet=$commande-> projet_id; - $soc->fetch($societe_id); - } - elseif ($_GET["contratid"]) - { - $contrat = New Contrat($db); - $contrat->fetch($_GET["contratid"]); - $societe_id = $contrat->societe->id; - $projet=$contrat->fk_projet; - $soc=$contrat->societe; - } - else - { - $societe_id=$socidp; - $soc->fetch($societe_id); - } + if ($_GET['propalid']) + { + $propal = New Propal($db); + $propal->fetch($_GET['propalid']); + $societe_id = $propal->soc_id; + $projet=$propal->projetidp; + $soc->fetch($societe_id); + } + elseif ($_GET['commandeid']) + { + $commande = New Commande($db); + $commande->fetch($_GET['commandeid']); + $societe_id = $commande->soc_id; + $projet=$commande-> projet_id; + $ref_client=$commande->ref_client; + $soc->fetch($societe_id); + } + elseif ($_GET['contratid']) + { + $contrat = New Contrat($db); + $contrat->fetch($_GET['contratid']); + $societe_id = $contrat->societe->id; + $projet=$contrat->fk_projet; + $soc=$contrat->societe; + } + else + { + $societe_id=$socidp; + $soc->fetch($societe_id); + } - print '
'; - print ''; - print '' ."\n"; + print ''; + print ''; + print '' ."\n"; - print ''; + print '
'; + print ''; + print ''; - print ''; - print ''; + print ''; + print ''; - print ''; - print ''; + print ''; + print ''; + print ''; - print ''; - print ''; - print ''; + print ''; - print ''; + // Notes + print ''; - // Notes - print ''; + // Conditions de réglement + $cond_reglement_id_defaut=1; + print ''; - // Conditions de réglement - $cond_reglement_id_defaut=1; - print ""; + // Mode de réglement + print ''; - // Mode de réglement - print ""; + // Projet + if ($conf->projet->enabled) + { + $langs->load('projects'); + print ''; + } + else + { + print ''; + } - // Projet - if ($conf->projet->enabled) - { - $langs->load("projects"); - print '"; - } - else - { - print ''; - } + print ''; - if ($_GET["propalid"] > 0) - { - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''; + if ($_GET['propalid'] > 0) + { + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''; - print ''; - print ''; - print ''; - print '"; - print '"; - } - elseif ($_GET["commandeid"] > 0) - { - $commande->remise_percent=$soc->remise_client; - print ''; - print ''."\n"; + print ''; + print ''; + print ''; + print '"; + print '"; + } + elseif ($_GET['commandeid'] > 0) + { + $commande->remise_percent=$soc->remise_client; + print ''; + print ''."\n"; - print ''; - print ''; - print '"; - print '"; - } - elseif ($_GET["contratid"] > 0) - { - // Calcul contrat->price (HT), contrat->total (TTC), contrat->tva - $contrat->remise=0; - $contrat->remise_percent=$soc->remise_client; - $contrat->update_price(); + print ''; + print ''; + print '"; + print '"; + } + elseif ($_GET['contratid'] > 0) + { + // Calcul contrat->price (HT), contrat->total (TTC), contrat->tva + $contrat->remise=0; + $contrat->remise_percent=$soc->remise_client; + $contrat->update_price(); - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''; + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''; - print ''; - print ''; - print '"; - print '"; - } - else - { - print ''; - print ''; + print ''; + print '"; + print '"; + } + else + { + print ''; + print ''; - } + print '
'.$langs->trans('Ref').''.$langs->trans('Draft').'
'.$langs->trans("Ref").''.$langs->trans("Draft").'
'.$langs->trans('Company').''.$soc->nom.'
'.$langs->trans("Company").''.$soc->nom.'
'.$langs->trans('Author').''.$user->fullname.''.$langs->trans('Comments').'
'.$langs->trans("Author").''.$user->fullname.''.$langs->trans("Comments").'
'.$langs->trans('Date').''; + $html->select_date(); + print '
'.$langs->trans("Date").''; - $html->select_date(); - print ''; + print '
'; - print '
'.$langs->trans('PaymentConditions').''; + $html->select_conditions_paiements($cond_reglement_id_defaut,'cond_reglement_id'); + print '
".$langs->trans("PaymentConditions").""; - $html->select_conditions_paiements($cond_reglement_id_defaut,'cond_reglement_id'); - print "
'.$langs->trans('PaymentMode').''; + $html->select_types_paiements('','mode_reglement_id'); + print '
".$langs->trans("PaymentMode").""; - $html->select_types_paiements('','mode_reglement_id'); - print "
'.$langs->trans('Project').''; + $html->select_projects($societe_id, $projet, 'projetid'); + print '
 
'.$langs->trans("Project").''; - $html->select_projects($societe_id, $projet, "projetid"); - print "
 
'.$langs->trans('RefCdeClient').''; + print ''; + print '
'.$langs->trans("Proposal").''.$propal->ref.'
'.$langs->trans("GlobalDiscount").''.$propal->remise_percent.'%
'.$langs->trans("TotalHT").''.price($propal->price).'
'.$langs->trans("VAT").''.price($propal->tva)."
'.$langs->trans("TotalTTC").''.price($propal->total)."
'.$langs->trans('Proposal').''.$propal->ref.'
'.$langs->trans('GlobalDiscount').''.$propal->remise_percent.'%
'.$langs->trans('TotalHT').''.price($propal->price).'
'.$langs->trans('VAT').''.price($propal->tva)."
'.$langs->trans('TotalTTC').''.price($propal->total)."
'.$langs->trans("Order").''.$commande->ref.'
'.$langs->trans("TotalHT").''.price($commande->total_ht).'
'.$langs->trans("VAT").''.price($commande->total_tva)."
'.$langs->trans("TotalTTC").''.price($commande->total_ttc)."
'.$langs->trans('Order').''.$commande->ref.'
'.$langs->trans('TotalHT').''.price($commande->total_ht).'
'.$langs->trans('VAT').''.price($commande->total_tva)."
'.$langs->trans('TotalTTC').''.price($commande->total_ttc)."
'.$langs->trans("Contract").''.$contrat->ref.'
'.$langs->trans("TotalHT").''.price($contrat->total_ht).'
'.$langs->trans("VAT").''.price($contrat->total_tva)."
'.$langs->trans("TotalTTC").''.price($contrat->total_ttc)."
 
'; + print '
'.$langs->trans('Contract').''.$contrat->ref.'
'.$langs->trans('TotalHT').''.price($contrat->total_ht).'
'.$langs->trans('VAT').''.price($contrat->total_tva)."
'.$langs->trans('TotalTTC').''.price($contrat->total_ttc)."
 
'; - print ''; - print ''; - if ($conf->service->enabled) - { - print ''; - } - for ($i = 1 ; $i <= $NBLINES ; $i++) - { - print ''; - print ''; - print ''; - print ''; - // Si le module service est actif, on propose des dates de début et fin à la ligne - if ($conf->service->enabled) { - print ''; - } - print "\n"; - } + print '
'.$langs->trans("ProductsAndServices").''.$langs->trans("Qty").''.$langs->trans("Discount").'     Si produit de type service à durée limitée
'; - $html->select_produits('',"idprod$i"); - print '% '; - print 'Du '; - print $html->select_date('',"date_start$i",0,0,1); - print '
au '; - print $html->select_date('',"date_end$i",0,0,1); - print '
'; + print ''; + if ($conf->service->enabled) + { + print ''; + } + for ($i = 1 ; $i <= $NBLINES ; $i++) + { + print ''; + print ''; + print ''; + print ''; + // Si le module service est actif, on propose des dates de début et fin à la ligne + if ($conf->service->enabled) + { + print ''; + } + print "\n"; + } - print '
'.$langs->trans('ProductsAndServices').''.$langs->trans('Qty').''.$langs->trans('Discount').'     Si produit de type service à durée limitée
'; + $html->select_produits('','idprod'.$i); + print '% '; + print 'Du '; + print $html->select_date('','date_start'.$i,0,0,1); + print '
au '; + print $html->select_date('','date_end'.$i,0,0,1); + print '
'; - print '
'; + print ''; + } - /* - * Factures récurrentes - */ - if ($_GET["propalid"] == 0 && $_GET["commandeid"] == 0 && $_GET["contratid"] == 0) - { - $sql = "SELECT r.rowid, r.titre, r.amount FROM ".MAIN_DB_PREFIX."facture_rec as r"; - $sql .= " WHERE r.fk_soc = ".$soc->id; - if ( $db->query($sql) ) - { - $num = $db->num_rows(); - $i = 0; + /* + * Factures récurrentes + */ + if ($_GET['propalid'] == 0 && $_GET['commandeid'] == 0 && $_GET['contratid'] == 0) + { + $sql = 'SELECT r.rowid, r.titre, r.amount FROM '.MAIN_DB_PREFIX.'facture_rec as r'; + $sql .= ' WHERE r.fk_soc = '.$soc->id; + if ( $db->query($sql) ) + { + $num = $db->num_rows(); + $i = 0; - if ($num > 0) - { - print 'Factures récurrentes : '; - } - $db->free(); - } - else - { - dolibarr_print_error($db); - } - } + if ($num > 0) + { + print 'Factures récurrentes : '; + } + $db->free(); + } + else + { + dolibarr_print_error($db); + } + } - // Bouton "Create Draft" - print ''; - print "
\n"; - print "\n"; + // Bouton "Create Draft" + print ''; + print "\n"; + print "\n"; - // Si creation depuis un propal - if ($_GET["propalid"]) - { - print '
'; - print_titre($langs->trans("ProductsAndServices")); + // Si creation depuis un propal + if ($_GET['propalid']) + { + print '
'; + print_titre($langs->trans('ProductsAndServices')); - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; + print '
'.$langs->trans("Ref").''.$langs->trans("Description").''.$langs->trans("VAT").''.$langs->trans("PriceUHT").''.$langs->trans("Qty").''.$langs->trans("Discount").'
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; - // Lignes de propal produits prédéfinis - $sql = "SELECT pt.rowid, p.label as product, p.ref, pt.tva_tx, pt.price, pt.qty, p.rowid as prodid, pt.remise_percent, pt.description"; - $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pt, ".MAIN_DB_PREFIX."product as p WHERE pt.fk_product = p.rowid AND pt.fk_propal = ".$_GET["propalid"]; - $sql .= " ORDER BY pt.rowid ASC"; - $result = $db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - $i = 0; - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print '\n"; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - $i++; - } - } - else - { - dolibarr_print_error($db); - } - // Lignes de propal non produits prédéfinis - $sql = "SELECT pt.rowid, pt.description as product, pt.tva_tx, pt.price, pt.qty, pt.remise_percent"; - $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pt "; - $sql .= " WHERE pt.fk_propal = ".$_GET["propalid"]; - $sql .= " AND (pt.fk_product = 0 or pt.fk_product is null)"; - $sql .= " ORDER BY pt.rowid ASC"; - $result=$db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - $i = 0; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print "\n"; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - $i++; - } - } - else - { - dolibarr_print_error($db); - } + // Lignes de propal produits prédéfinis + $sql = 'SELECT pt.rowid, p.label as product, p.ref, pt.tva_tx, pt.price, pt.qty, p.rowid as prodid, pt.remise_percent, pt.description'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt, '.MAIN_DB_PREFIX.'product as p WHERE pt.fk_product = p.rowid AND pt.fk_propal = '.$_GET['propalid']; + $sql .= ' ORDER BY pt.rowid ASC'; + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + $i = 0; + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print '\n"; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + } + else + { + dolibarr_print_error($db); + } + // Lignes de propal non produits prédéfinis + $sql = 'SELECT pt.rowid, pt.description as product, pt.tva_tx, pt.price, pt.qty, pt.remise_percent'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt '; + $sql .= ' WHERE pt.fk_propal = '.$_GET['propalid']; + $sql .= ' AND (pt.fk_product = 0 or pt.fk_product is null)'; + $sql .= ' ORDER BY pt.rowid ASC'; + $result=$db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + $i = 0; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + } + else + { + dolibarr_print_error($db); + } - print '
'.$langs->trans('Ref').''.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('Discount').'
'.img_object($langs->trans(""),"product")." ".$objp->ref.""; - print $objp->product?' - '.$objp->product:''; - print "'; - print dolibarr_trunc($objp->description,60); - print ''.$objp->tva_tx.'%'.price($objp->price).''.$objp->qty.''.$objp->remise_percent.'%
 '.dolibarr_trunc($objp->product,60).''.$objp->tva_tx.'%'.price($objp->price).''.$objp->qty.''.$objp->remise_percent.'%
'.img_object($langs->trans(''),'product').' '.$objp->ref.''; + print $objp->product?' - '.$objp->product:''; + print "'; + print dolibarr_trunc($objp->description,60); + print ''.$objp->tva_tx.'%'.price($objp->price).''.$objp->qty.''.$objp->remise_percent.'%
 '.dolibarr_trunc($objp->product,60).''.$objp->tva_tx.'%'.price($objp->price).''.$objp->qty.''.$objp->remise_percent.'%
'; - } + print ''; + } - // Si creation depuis une commande - if ($_GET["commandeid"]) - { - print '
'; - print_titre($langs->trans("Products")); + // Si creation depuis une commande + if ($_GET['commandeid']) + { + print '
'; + print_titre($langs->trans('Products')); - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; + print '
'.$langs->trans("Ref").''.$langs->trans("Description").''.$langs->trans("VAT").''.$langs->trans("PriceUHT").''.$langs->trans("Qty").''.$langs->trans("Discount").'
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; - $sql = "SELECT pt.rowid, pt.subprice, pt.tva_tx, pt.qty, pt.remise_percent, pt.description,"; - $sql.= " p.label as product, p.ref, p.rowid as prodid"; - $sql.= " FROM ".MAIN_DB_PREFIX."commandedet as pt, ".MAIN_DB_PREFIX."product as p"; - $sql.= " WHERE pt.fk_product = p.rowid AND pt.fk_commande = ".$commande->id; - $sql.= " ORDER BY pt.rowid ASC"; + $sql = 'SELECT pt.rowid, pt.subprice, pt.tva_tx, pt.qty, pt.remise_percent, pt.description,'; + $sql.= ' p.label as product, p.ref, p.rowid as prodid'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as pt, '.MAIN_DB_PREFIX.'product as p'; + $sql.= ' WHERE pt.fk_product = p.rowid AND pt.fk_commande = '.$commande->id; + $sql.= ' ORDER BY pt.rowid ASC'; - $result = $db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - $i = 0; - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print '\n"; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - $i++; - } - } - else - { - dolibarr_print_error($db); - } - // Lignes de commande non produits prédéfinis - $sql = "SELECT pt.rowid, pt.description as product, pt.tva_tx, pt.subprice, pt.qty, pt.remise_percent"; - $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as pt"; - $sql .= " WHERE pt.fk_commande = ".$commande->id; - $sql .= " AND (pt.fk_product = 0 or pt.fk_product is null)"; - $sql .= " ORDER BY pt.rowid ASC"; + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + $i = 0; + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print '\n"; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + } + else + { + dolibarr_print_error($db); + } + // Lignes de commande non produits prédéfinis + $sql = 'SELECT pt.rowid, pt.description as product, pt.tva_tx, pt.subprice, pt.qty, pt.remise_percent'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'commandedet as pt'; + $sql .= ' WHERE pt.fk_commande = '.$commande->id; + $sql .= ' AND (pt.fk_product = 0 or pt.fk_product is null)'; + $sql .= ' ORDER BY pt.rowid ASC'; - $result=$db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - $i = 0; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print "\n"; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - $i++; - } - } - else - { - dolibarr_print_error($db); - } + $result=$db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + $i = 0; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + } + else + { + dolibarr_print_error($db); + } - print '
'.$langs->trans('Ref').''.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('Discount').'
'.img_object($langs->trans(""),"product")." ".$objp->ref.""; - print $objp->product?' - '.$objp->product:''; - print "'; - print dolibarr_trunc($objp->description,60); - print ''.$objp->tva_tx.'%'.price($objp->subprice).''.$objp->qty.''.$objp->remise_percent.'%
'.img_object($langs->trans(''),'product').' '.$objp->ref.''; + print $objp->product?' - '.$objp->product:''; + print "'; + print dolibarr_trunc($objp->description,60); + print ''.$objp->tva_tx.'%'.price($objp->subprice).''.$objp->qty.''.$objp->remise_percent.'%
 '.dolibarr_trunc($objp->product,60).''.$objp->tva_tx.'%'.price($objp->subprice).''.$objp->qty.''.$objp->remise_percent.'%
 '.dolibarr_trunc($objp->product,60).''.$objp->tva_tx.'%'.price($objp->subprice).''.$objp->qty.''.$objp->remise_percent.'%
'; - } + print ''; + } - // Si creation depuis un contrat - if ($_GET["contratid"]) - { - print '
'; - print_titre($langs->trans("Services")); + // Si creation depuis un contrat + if ($_GET['contratid']) + { + print '
'; + print_titre($langs->trans('Services')); - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; + print '
'.$langs->trans("Ref").''.$langs->trans("Description").''.$langs->trans("VAT").''.$langs->trans("PriceUHT").''.$langs->trans("Qty").''.$langs->trans("Discount").'
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; - // Lignes de contrat produits prédéfinis - $sql = "SELECT pt.rowid, pt.subprice, pt.tva_tx, pt.qty, pt.remise_percent, pt.description,"; - $sql.= " pt.date_ouverture_prevue as date_debut_prevue, pt.date_ouverture as date_debut_reel,"; - $sql.= " pt.date_fin_validite as date_fin_prevue, pt.date_cloture as date_fin_reel,"; - $sql.= " p.label as product, p.ref, p.rowid as prodid"; - $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as pt, ".MAIN_DB_PREFIX."product as p"; - $sql.= " WHERE pt.fk_product = p.rowid AND pt.fk_contrat = ".$contrat->id; - $sql.= " ORDER BY pt.rowid ASC"; + // Lignes de contrat produits prédéfinis + $sql = 'SELECT pt.rowid, pt.subprice, pt.tva_tx, pt.qty, pt.remise_percent, pt.description,'; + $sql.= ' pt.date_ouverture_prevue as date_debut_prevue, pt.date_ouverture as date_debut_reel,'; + $sql.= ' pt.date_fin_validite as date_fin_prevue, pt.date_cloture as date_fin_reel,'; + $sql.= ' p.label as product, p.ref, p.rowid as prodid'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'contratdet as pt, '.MAIN_DB_PREFIX.'product as p'; + $sql.= ' WHERE pt.fk_product = p.rowid AND pt.fk_contrat = '.$contrat->id; + $sql.= ' ORDER BY pt.rowid ASC'; - $result = $db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - $i = 0; - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print '\n"; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - $i++; - } - } - else - { - dolibarr_print_error($db); - } - // Lignes de contrat non produits prédéfinis - $sql = "SELECT pt.rowid, pt.description as product, pt.tva_tx, pt.subprice, pt.qty, pt.remise_percent,"; - $sql.= " pt.date_ouverture_prevue as date_debut_prevue, pt.date_ouverture as date_debut_reel,"; - $sql.= " pt.date_fin_validite as date_fin_prevue, pt.date_cloture as date_fin_reel"; - $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as pt"; - $sql.= " WHERE pt.fk_contrat = ".$contrat->id; - $sql.= " AND (pt.fk_product = 0 or pt.fk_product is null)"; - $sql.= " ORDER BY pt.rowid ASC"; + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + $i = 0; + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print '\n"; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - $i++; - } - } - else - { - dolibarr_print_error($db); - } + print "\n"; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + } + else + { + dolibarr_print_error($db); + } + // Lignes de contrat non produits prédéfinis + $sql = 'SELECT pt.rowid, pt.description as product, pt.tva_tx, pt.subprice, pt.qty, pt.remise_percent,'; + $sql.= ' pt.date_ouverture_prevue as date_debut_prevue, pt.date_ouverture as date_debut_reel,'; + $sql.= ' pt.date_fin_validite as date_fin_prevue, pt.date_cloture as date_fin_reel'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'contratdet as pt'; + $sql.= ' WHERE pt.fk_contrat = '.$contrat->id; + $sql.= ' AND (pt.fk_product = 0 or pt.fk_product is null)'; + $sql.= ' ORDER BY pt.rowid ASC'; - print '
'.$langs->trans('Ref').''.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('Discount').'
'.img_object($langs->trans(""),"service")." ".$objp->ref.""; - print $objp->product?' - '.$objp->product:''; - // Plage de dates - $date_start=$objp->date_debut_prevue; - if ($objp->date_debut_reel) $date_start=$objp->date_debut_reel; - $date_end=$objp->date_fin_prevue; - if ($objp->date_fin_reel) $date_end=$objp->date_fin_reel; - print_date_range($date_start,$date_end); - - print "'; - print dolibarr_trunc($objp->description,60); - print ''.$objp->tva_tx.'%'.price($objp->subprice).''.$objp->qty.''.$objp->remise_percent.'%
'.img_object($langs->trans(''),'service').' '.$objp->ref.''; + print $objp->product?' - '.$objp->product:''; + // Plage de dates + $date_start=$objp->date_debut_prevue; + if ($objp->date_debut_reel) $date_start=$objp->date_debut_reel; + $date_end=$objp->date_fin_prevue; + if ($objp->date_fin_reel) $date_end=$objp->date_fin_reel; + print_date_range($date_start,$date_end); - $result=$db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - $i = 0; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print "
 '.dolibarr_trunc($objp->product,60).''.$objp->tva_tx.'%'.price($objp->subprice).''.$objp->qty.''.$objp->remise_percent.'%
'; + print dolibarr_trunc($objp->description,60); + print ''.$objp->tva_tx.'%'.price($objp->subprice).''.$objp->qty.''.$objp->remise_percent.'%
'; - } + $result=$db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + $i = 0; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print ' '; + print ''.dolibarr_trunc($objp->product,60).''; + print ''.$objp->tva_tx.'%'; + print ''.price($objp->subprice).''; + print ''.$objp->qty.''; + print ''.$objp->remise_percent.'%'; + print ''; + $i++; + } + } + else + { + dolibarr_print_error($db); + } + + print ''; + } } else { - if ($_GET["facid"] > 0) - { - /* *************************************************************************** */ - /* */ - /* Fiche en mode visu */ - /* */ - /* *************************************************************************** */ + if ($_GET['facid'] > 0) + { + /* *************************************************************************** */ + /* */ + /* Fiche en mode visu */ + /* */ + /* *************************************************************************** */ - if ($msg) print "$msg
"; + if ($msg) print $msg.'
'; - $fac = New Facture($db); - if ( $fac->fetch($_GET["facid"], $user->societe_id) > 0) - { + $fac = New Facture($db); + if ( $fac->fetch($_GET['facid'], $user->societe_id) > 0) + { + $soc = new Societe($db, $fac->socidp); + $soc->fetch($fac->socidp); - $soc = new Societe($db, $fac->socidp); - $soc->fetch($fac->socidp); + $author = new User($db); + $author->id = $fac->user_author; + $author->fetch(); - $author = new User($db); - $author->id = $fac->user_author; - $author->fetch(); + $h = 0; - $h = 0; + $head[$h][0] = DOL_URL_ROOT.'/compta/facture.php?facid='.$fac->id; + $head[$h][1] = $langs->trans('CardBill'); + $hselected = $h; + $h++; - $head[$h][0] = DOL_URL_ROOT.'/compta/facture.php?facid='.$fac->id; - $head[$h][1] = $langs->trans("CardBill"); - $hselected = $h; - $h++; + if ($conf->use_preview_tabs) + { + $head[$h][0] = DOL_URL_ROOT.'/compta/facture/apercu.php?facid='.$fac->id; + $head[$h][1] = $langs->trans('Preview'); + $h++; + } - if ($conf->use_preview_tabs) - { - $head[$h][0] = DOL_URL_ROOT.'/compta/facture/apercu.php?facid='.$fac->id; - $head[$h][1] = $langs->trans("Preview"); - $h++; - } - - if ($fac->mode_reglement_code == 'PRE') - { - $head[$h][0] = DOL_URL_ROOT.'/compta/facture/prelevement.php?facid='.$fac->id; - $head[$h][1] = $langs->trans("StandingOrders"); - $h++; - } - - $head[$h][0] = DOL_URL_ROOT.'/compta/facture/note.php?facid='.$fac->id; - $head[$h][1] = $langs->trans("Note"); - $h++; - $head[$h][0] = DOL_URL_ROOT.'/compta/facture/info.php?facid='.$fac->id; - $head[$h][1] = $langs->trans("Info"); - $h++; + if ($fac->mode_reglement_code == 'PRE') + { + $head[$h][0] = DOL_URL_ROOT.'/compta/facture/prelevement.php?facid='.$fac->id; + $head[$h][1] = $langs->trans('StandingOrders'); + $h++; + } - dolibarr_fiche_head($head, $hselected, $langs->trans("Bill")." : $fac->ref"); + $head[$h][0] = DOL_URL_ROOT.'/compta/facture/note.php?facid='.$fac->id; + $head[$h][1] = $langs->trans('Note'); + $h++; + $head[$h][0] = DOL_URL_ROOT.'/compta/facture/info.php?facid='.$fac->id; + $head[$h][1] = $langs->trans('Info'); + $h++; + + dolibarr_fiche_head($head, $hselected, $langs->trans('Bill').' : '.$fac->ref); - /* - * Confirmation de la suppression de la facture - * - */ - if ($_GET["action"] == 'delete') - { - $html->form_confirm($_SERVER["PHP_SELF"]."?facid=$fac->id",$langs->trans("DeleteBill"),$langs->trans("ConfirmDeleteBill"),"confirm_delete"); - print '
'; - } + /* + * Confirmation de la suppression de la facture + * + */ + if ($_GET['action'] == 'delete') + { + $html->form_confirm($_SERVER['PHP_SELF'].'?facid='.$fac->id,$langs->trans('DeleteBill'),$langs->trans('ConfirmDeleteBill'),'confirm_delete'); + print '
'; + } - /* - * Confirmation du classement abandonn - * - */ - if ($_GET["action"] == 'canceled') - { - $html->form_confirm($_SERVER["PHP_SELF"]."?facid=$fac->id",$langs->trans("CancelBill"),$langs->trans("ConfirmCancelBill"),"confirm_canceled"); - print '
'; - } + /* + * Confirmation du classement abandonn + * + */ + if ($_GET['action'] == 'canceled') + { + $html->form_confirm($_SERVER['PHP_SELF'].'?facid='.$fac->id,$langs->trans('CancelBill'),$langs->trans('ConfirmCancelBill'),'confirm_canceled'); + print '
'; + } - /* - * Confirmation de la validation - * - */ - if ($_GET["action"] == 'valid') - { - $numfa = facture_get_num($soc); - $html->form_confirm("facture.php?facid=$fac->id",$langs->trans("ValidateBill"),$langs->trans("ConfirmValidateBill",$numfa),"confirm_valid"); - print '
'; - } + /* + * Confirmation de la validation + * + */ + if ($_GET['action'] == 'valid') + { + $numfa = facture_get_num($soc); + $html->form_confirm('facture.php?facid='.$fac->id,$langs->trans('ValidateBill'),$langs->trans('ConfirmValidateBill',$numfa),'confirm_valid'); + print '
'; + } - /* - * Facture - */ - print ''; - print ''; - print ''; + /* + * Facture + */ + print '
'.$langs->trans("Company").''; - print ''.$soc->nom.'
'; + print ''; + print ''; - print ''; - print '\n"; - print ''; + print ''; + print ''; + print ''; - // Conditions et modes de réglement - print ''; - print ''; - print ''; + // Conditions et modes de réglement + print ''; + print ''; - // Projet - if ($conf->projet->enabled) - { - $langs->load("projects"); - print '"; - } - else - { - print ''; - } + // Projet + print ''; + if ($conf->projet->enabled) + { + $langs->load('projects'); + print ''; + } + else + { + print ''; + } - print ''; + print ''; + } + print '
'.$langs->trans('Company').''; + print ''.$soc->nom.'
'.$langs->trans("Date").''.dolibarr_print_date($fac->date,"%A %d %B %Y")."'.$langs->trans("DateClosing").'' . dolibarr_print_date($fac->date_lim_reglement,"%A %d %B %Y"); - if ($fac->date_lim_reglement < (time() - $conf->facture->client->warning_delay) && ! $fac->paye && $fac->statut == 1 && ! $fac->am) print img_warning($langs->trans("Late")); - print '
'.$langs->trans('Date').''.dolibarr_print_date($fac->date,'%A %d %B %Y').''.$langs->trans('DateClosing').'' . dolibarr_print_date($fac->date_lim_reglement,'%A %d %B %Y'); + if ($fac->date_lim_reglement < (time() - $conf->facture->client->warning_delay) && ! $fac->paye && $fac->statut == 1 && ! $fac->am) print img_warning($langs->trans('Late')); + print '
'; - print ''; - if ($_GET["action"] != "editconditions" && $fac->brouillon) print ''; - print '
'; - print $langs->trans("PaymentConditions"); - print ''.img_edit($langs->trans("SetConditions")).'
'; - print '
'; - if ($_GET["action"] == "editconditions") - { - $html->form_conditions_reglement($_SERVER["PHP_SELF"]."?facid=$fac->id",$fac->cond_reglement_id,"cond_reglement_id"); - } - else - { - $html->form_conditions_reglement($_SERVER["PHP_SELF"]."?facid=$fac->id",$fac->cond_reglement_id,"none"); - } - print ''; - print ''; - if ($_GET["action"] != "editmode" && $fac->brouillon) print ''; - print '
'; - print $langs->trans("PaymentMode"); - print ''.img_edit($langs->trans("SetMode")).'
'; - print '
'; - if ($_GET["action"] == "editmode") - { - $html->form_modes_reglement($_SERVER["PHP_SELF"]."?facid=$fac->id",$fac->mode_reglement_id,"mode_reglement_id"); - } - else - { - $html->form_modes_reglement($_SERVER["PHP_SELF"]."?facid=$fac->id",$fac->mode_reglement_id,"none"); - } - print '
'; + print ''; + if ($_GET['action'] != 'editconditions' && $fac->brouillon) print ''; + print '
'; + print $langs->trans('PaymentConditions'); + print ''.img_edit($langs->trans('SetConditions')).'
'; + print '
'; + if ($_GET['action'] == 'editconditions') + { + $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->cond_reglement_id,'cond_reglement_id'); + } + else + { + $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->cond_reglement_id,'none'); + } + print ''; + print ''; + if ($_GET['action'] != 'editmode' && $fac->brouillon) print ''; + print '
'; + print $langs->trans('PaymentMode'); + print ''.img_edit($langs->trans('SetMode')).'
'; + print '
'; + if ($_GET['action'] == 'editmode') + { + $html->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->mode_reglement_id,'mode_reglement_id'); + } + else + { + $html->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->mode_reglement_id,'none'); + } + print '
'; - print ''; - if ($_GET["action"] != "classer") print ''; - print '
'; - print $langs->trans("Project"); - print ''.img_edit($langs->trans("SetProject")).'
'; - print '
'; - if ($_GET["action"] == "classer") - { - $html->form_project($_SERVER["PHP_SELF"]."?facid=$fac->id",$fac->fk_soc,$fac->projetid,"projetid"); - } - else - { - $html->form_project($_SERVER["PHP_SELF"]."?facid=$fac->id",$fac->fk_soc,$fac->projetid,"none"); - } - print "  
'; + print ''; + if ($_GET['action'] != 'classer') print ''; + print '
'; + print $langs->trans('Project'); + print ''.img_edit($langs->trans('SetProject')).'
'; + print '
'; + if ($_GET['action'] == 'classer') + { + $html->form_project($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->fk_soc,$fac->projetid,'projetid'); + } + else + { + $html->form_project($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->fk_soc,$fac->projetid,'none'); + } + print '  '; + print ''; - /* - * Paiements - */ - print $langs->trans("Payments").' :
'; - $sql = "SELECT ".$db->pdate("datep")." as dp, pf.amount,"; - $sql.= " c.libelle as paiement_type, p.num_paiement, p.rowid"; - $sql.= " FROM ".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."c_paiement as c, ".MAIN_DB_PREFIX."paiement_facture as pf"; - $sql.= " WHERE pf.fk_facture = ".$fac->id." AND p.fk_paiement = c.id AND pf.fk_paiement = p.rowid"; - $sql.= " ORDER BY dp DESC"; + /* + * Paiements + */ + print $langs->trans('Payments').' :
'; + $sql = 'SELECT '.$db->pdate('datep').' as dp, pf.amount,'; + $sql.= ' c.libelle as paiement_type, p.num_paiement, p.rowid'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement as p, '.MAIN_DB_PREFIX.'c_paiement as c, '.MAIN_DB_PREFIX.'paiement_facture as pf'; + $sql.= ' WHERE pf.fk_facture = '.$fac->id.' AND p.fk_paiement = c.id AND pf.fk_paiement = p.rowid'; + $sql.= ' ORDER BY dp DESC'; - $result = $db->query($sql); + $result = $db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - $i = 0; $total = 0; - print ''; - print ''; - print ''; + if ($result) + { + $num = $db->num_rows($result); + $i = 0; $total = 0; + print '
'.$langs->trans("Date").''.$langs->trans("Type").''.$langs->trans("Amount").' 
'; + print ''; + print ''; - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print "\n"; - print "\n"; - print '\n"; - print ""; - $totalpaye += $objp->amount; - $i++; - } + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + $totalpaye += $objp->amount; + $i++; + } - if ($fac->paye == 0) - { - print "\n"; - print "\n"; + if ($fac->paye == 0) + { + print ''; + print ''; - $resteapayer = $fac->total_ttc - $totalpaye; + $resteapayer = $fac->total_ttc - $totalpaye; - print ""; - print "\n"; - } - print "
'.$langs->trans('Date').''.$langs->trans('Type').''.$langs->trans('Amount').' 
"; - print ''.img_object($langs->trans("ShowPayment"),"payment").''; - print " ".strftime("%d %B %Y",$objp->dp)."$objp->paiement_type $objp->num_paiement'.price($objp->amount)."".$langs->trans("Currency".$conf->monnaie)."
'; + print ''.img_object($langs->trans('ShowPayment'),'payment').''; + print ' '.strftime('%d %B %Y',$objp->dp).''.$objp->paiement_type.' '.$objp->num_paiement.''.price($objp->amount).''.$langs->trans('Currency'.$conf->monnaie).'
".$langs->trans("AlreadyPayed")." :".price($totalpaye)."".$langs->trans("Currency".$conf->monnaie)."
Facturé :".price($fac->total_ttc)."".$langs->trans("Currency".$conf->monnaie)."
'.$langs->trans('AlreadyPayed').' :'.price($totalpaye).''.$langs->trans('Currency'.$conf->monnaie).'
Facturé :'.price($fac->total_ttc).''.$langs->trans('Currency'.$conf->monnaie).'
".$langs->trans("RemainderToPay")." :".price($resteapayer)."".$langs->trans("Currency".$conf->monnaie)."
"; - $db->free($result); - } else { - dolibarr_print_error($db); - } + print '
'.$langs->trans('RemainderToPay').' :'.price($resteapayer).''.$langs->trans('Currency'.$conf->monnaie).'
'; + $db->free($result); + } + else + { + dolibarr_print_error($db); + } - print ""; + print ''; - print "".$langs->trans("Author")."$author->fullname"; + print ''.$langs->trans('Author').''.$author->fullname.''; - print ''.$langs->trans("GlobalDiscount").''; - if ($fac->brouillon == 1 && $user->rights->facture->creer) - { - print '
'; - print ''; - print '% '; - print ''; - print '
'; - } - else { - print ''.$fac->remise_percent.'%'; - } - print ''; + print ''.$langs->trans('GlobalDiscount').''; + if ($fac->brouillon == 1 && $user->rights->facture->creer) + { + print '
'; + print ''; + print '% '; + print ''; + print '
'; + } + else + { + print ''.$fac->remise_percent.'%'; + } + print ''; - print ''.$langs->trans("AmountHT").''; - print ''.price($fac->total_ht).''; - print ''.$langs->trans("Currency".$conf->monnaie).''; + print ''.$langs->trans('RefCdeClient').''; + if ($fac->brouillon == 1 && $user->rights->facture->creer) + { + print '
'; + print ''; + print ''; + print ''; + print '
'; + } + else + { + print ''.$fac->ref_client.''; + } - print ''.$langs->trans("VAT").''.price($fac->total_tva).''; - print ''.$langs->trans("Currency".$conf->monnaie).''; - print ''.$langs->trans("AmountTTC").''.price($fac->total_ttc).''; - print ''.$langs->trans("Currency".$conf->monnaie).''; + print ''.$langs->trans('AmountHT').''; + print ''.price($fac->total_ht).''; + print ''.$langs->trans('Currency'.$conf->monnaie).''; - // Statut - print ''.$langs->trans("Status").''.($fac->getLibStatut()).''; + print ''.$langs->trans('VAT').''.price($fac->total_tva).''; + print ''.$langs->trans('Currency'.$conf->monnaie).''; + print ''.$langs->trans('AmountTTC').''.price($fac->total_ttc).''; + print ''.$langs->trans('Currency'.$conf->monnaie).''; - if ($fac->note) - { - print ''.$langs->trans("Note").' : '.nl2br($fac->note).""; - } - else { - print ' '; - } + // Statut + print ''.$langs->trans('Status').''.($fac->getLibStatut()).''; - print "
"; + if ($fac->note) + { + print ''.$langs->trans('Note').' : '.nl2br($fac->note).''; + } + else + { + print ' '; + } - /* - * Lignes de factures - */ - $sql = "SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_taux,"; - $sql .= " l.remise_percent, l.subprice,"; - $sql .= " ".$db->pdate("l.date_start")." as date_start,"; - $sql .= " ".$db->pdate("l.date_end")." as date_end, "; - $sql .= " p.ref, p.fk_product_type, p.label as product"; - $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as l "; - $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product p ON l.fk_product=p.rowid"; - $sql .= " WHERE l.fk_facture = ".$fac->id; - $sql .= " ORDER BY l.rang ASC, l.rowid"; + print '
'; - $resql = $db->query($sql); - if ($resql) - { - $num_lignes = $db->num_rows($resql); - $i = 0; $total = 0; - - print ''; - if ($num_lignes) - { - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print "\n"; - } - $var=true; - while ($i < $num_lignes) - { - $objp = $db->fetch_object($resql); - $var=!$var; - - // Ligne en mode visu - if ($_GET["action"] != 'editline' || $_GET["rowid"] != $objp->rowid) - { - print ''; - if ($objp->fk_product > 0) - { - print ''; - } - else - { - print '\n"; - } - print ''; - print '\n"; - print ''; - if ($objp->remise_percent > 0) - { - print '\n"; - } - else - { - print ''; - } - print '\n"; - - // Icone d'edition et suppression - if ($fac->statut == 0 && $user->rights->facture->creer) - { - print ''; - print ''; - - print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + $var=!$var; + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + if ($conf->service->enabled) + { + print ''; + print ''; + print ''; + } + print ''; + } + print "
'.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('Discount').''.$langs->trans('AmountHT').' 
'; - if ($objp->fk_product_type) print img_object($langs->trans('ShowService'),'service'); - else print img_object($langs->trans('ShowProduct'),'product'); - print ' '.$objp->ref.''; - print ' - '.nl2br(stripslashes($objp->product)); - print_date_range($objp->date_start,$objp->date_end); - print ($objp->description && $objp->description!=$objp->product)?'
'.$objp->description:''; - print '
'.stripslashes(nl2br($objp->description)); - print_date_range($objp->date_start,$objp->date_end); - print "'.$objp->tva_taux.'%'.price($objp->subprice)."'.$objp->qty.''.$objp->remise_percent."% '.price($objp->subprice*$objp->qty*(100-$objp->remise_percent)/100)."'; - print img_edit(); - print ''; - print img_delete(); - print ''; - if ($i > 0) - { - print ''; - print img_up(); - print ''; - } - if ($i < $num_lignes-1) - { - print ''; - print img_down(); - print ''; - } + /* + * Lignes de factures + */ + $sql = 'SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_taux,'; + $sql .= ' l.remise_percent, l.subprice,'; + $sql .= ' '.$db->pdate('l.date_start').' as date_start,'; + $sql .= ' '.$db->pdate('l.date_end').' as date_end, '; + $sql .= ' p.ref, p.fk_product_type, p.label as product'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet as l '; + $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON l.fk_product=p.rowid'; + $sql .= ' WHERE l.fk_facture = '.$fac->id; + $sql .= ' ORDER BY l.rang ASC, l.rowid'; + + $resql = $db->query($sql); + if ($resql) + { + $num_lignes = $db->num_rows($resql); + $i = 0; $total = 0; + + print ''; + if ($num_lignes) + { + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print "\n"; + } + $var=true; + while ($i < $num_lignes) + { + $objp = $db->fetch_object($resql); + $var=!$var; + + // Ligne en mode visu + if ($_GET['action'] != 'editline' || $_GET['rowid'] != $objp->rowid) + { + print ''; + if ($objp->fk_product > 0) + { + print ''; - } - else - { - print ''; - } - print ''; - - } - - // Ligne en mode update - if ($_GET["action"] == 'editline' && $user->rights->facture->creer && $_GET["rowid"] == $objp->rowid) - { - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print '' . "\n"; - if ($conf->service->enabled) - { - print ""; - print ''; - print '' . "\n"; - } - print "\n"; - } - - $total = $total + ($objp->qty * $objp->price); - $i++; - } - - $db->free($resql); - } - else - { - dolibarr_print_error($db); - } + } + else + { + print '\n"; + } + print ''; + print '\n"; + print ''; + if ($objp->remise_percent > 0) + { + print '\n"; + } + else + { + print ''; + } + print '\n"; - /* - * Ajouter une ligne - */ - if ($fac->statut == 0 && $user->rights->facture->creer && $_GET["action"] <> 'valid') - { + // Icone d'edition et suppression + if ($fac->statut == 0 && $user->rights->facture->creer) + { + print ''; + print ''; + print ''; + } + else + { + print ''; + } + print ''; + } - print ""; - print ""; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print "\n"; - print ''; - print ''; + // Ligne en mode update + if ($_GET['action'] == 'editline' && $user->rights->facture->creer && $_GET['rowid'] == $objp->rowid) + { + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print '' . "\n"; + if ($conf->service->enabled) + { + print ''; + print ''; + print ''; + } + print "\n"; + } - $var=!$var; + $total = $total + ($objp->qty * $objp->price); + $i++; + } - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - if ($conf->service->enabled) - { - print ''; - print ''; - print ''; - } - print ""; - } - print "
'.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('Discount').''.$langs->trans('AmountHT').' 
'; + if ($objp->fk_product_type) print img_object($langs->trans('ShowService'),'service'); + else print img_object($langs->trans('ShowProduct'),'product'); + print ' '.$objp->ref.''; + print ' - '.nl2br(stripslashes($objp->product)); + print_date_range($objp->date_start,$objp->date_end); + print ($objp->description && $objp->description!=$objp->product)?'
'.$objp->description:''; print '
 
'; - if ($objp->fk_product > 0) - { - print ''; - if ($objp->fk_product_type) print img_object($langs->trans('ShowService'),'service'); - else print img_object($langs->trans('ShowProduct'),'product'); - print ' '.$objp->ref.''; - print ' - '.stripslashes(nl2br($objp->product)); - print '
'; - } - print '
'; - print $html->select_tva("tva_tx",$objp->tva_taux); - print '%'; - print '
Si produit de type service à durée limitée: Du '; - print $html->select_date($objp->date_start,"date_start",0,0,$objp->date_start?0:1); - print ' au '; - print $html->select_date($objp->date_end,"date_end",0,0,$objp->date_end?0:1); - print '
'.stripslashes(nl2br($objp->description)); + print_date_range($objp->date_start,$objp->date_end); + print "'.$objp->tva_taux.'%'.price($objp->subprice)."'.$objp->qty.''.$objp->remise_percent."% '.price($objp->subprice*$objp->qty*(100-$objp->remise_percent)/100)."'; + print img_edit(); + print ''; + print img_delete(); + print ''; + if ($i > 0) + { + print ''; + print img_up(); + print ''; + } + if ($i < $num_lignes-1) + { + print ''; + print img_down(); + print ''; + } + print ' 
'.$langs->trans("Description").''.$langs->trans("VAT").''.$langs->trans("PriceUHT").''.$langs->trans("Qty").''.$langs->trans("Discount").'    
'; + if ($objp->fk_product > 0) + { + print ''; + if ($objp->fk_product_type) print img_object($langs->trans('ShowService'),'service'); + else print img_object($langs->trans('ShowProduct'),'product'); + print ' '.$objp->ref.''; + print ' - '.stripslashes(nl2br($objp->product)); + print '
'; + } + print '
'; + print $html->select_tva("tva_tx",$objp->tva_taux); + print '%'; + print '
Si produit de type service à durée limitée: Du '; + print $html->select_date($objp->date_start,'date_start',0,0,$objp->date_start?0:1); + print ' au '; + print $html->select_date($objp->date_end,'date_end',0,0,$objp->date_end?0:1); + print '
'; - print $html->select_tva("tva_tx",$conf->defaulttx); - print '%
Si produit de type service à durée limitée: Du '; - print $html->select_date('',"date_start",0,0,1); - print ' au '; - print $html->select_date('',"date_end",0,0,1); - print '
\n"; + $db->free($resql); + } + else + { + dolibarr_print_error($db); + } + + /* + * Ajouter une ligne + */ + if ($fac->statut == 0 && $user->rights->facture->creer && $_GET['action'] <> 'valid') + { + print '
'; + print '
'.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('Discount').'    
'; + print $html->select_tva('tva_tx',$conf->defaulttx); + print '%
Si produit de type service à durée limitée: Du '; + print $html->select_date('','date_start',0,0,1); + print ' au '; + print $html->select_date('','date_end',0,0,1); + print '
\n"; - print "\n"; + print "\n"; - /* - * Boutons actions - */ + /* + * Boutons actions + */ - if ($user->societe_id == 0 && $_GET["action"] <> 'valid' && $_GET["action"] <> 'editline') - { - print "
\n"; + if ($user->societe_id == 0 && $_GET['action'] <> 'valid' && $_GET['action'] <> 'editline') + { + print '
'; + // Valider + if ($fac->statut == 0 && $num_lignes > 0) + { + if ($user->rights->facture->valider) + { + print ' '.$langs->trans('Validate').''; + } + } + else + { + // Générer + if ($fac->statut == 1 && ($fac->paye == 0 || $user->admin) && $user->rights->facture->creer) + { + if ($fac->paye == 0) + { + print ' '.$langs->trans('BuildPDF').''; + } + else + { + print ' '.$langs->trans('RebuildPDF').''; + } + } + } - // Valider - if ($fac->statut == 0 && $num_lignes > 0) - { - if ($user->rights->facture->valider) - { - print " id."&action=valid\">".$langs->trans("Validate")."\n"; - } - } - else - { - // Générer - if ($fac->statut == 1 && ($fac->paye == 0 || $user->admin) && $user->rights->facture->creer) - { - if ($fac->paye == 0) - { - print " id&action=pdf\">".$langs->trans("BuildPDF")."\n"; - } - else - { - print " id&action=pdf\">".$langs->trans("RebuildPDF")."\n"; - } - } - } + // Supprimer + if ($fac->statut == 0 && $user->rights->facture->supprimer && $_GET['action'] != 'delete') + { + print ''.$langs->trans('Delete').''; + } - // Supprimer - if ($fac->statut == 0 && $user->rights->facture->supprimer && $_GET["action"] != 'delete') - { - print "id&action=delete\">".$langs->trans("Delete").""; - } + // Envoyer + if ($fac->statut == 1 && $user->rights->facture->envoyer) + { + print ' '.$langs->trans('Send').''; + } - // Envoyer - if ($fac->statut == 1 && $user->rights->facture->envoyer) - { - print " id&action=presend\">".$langs->trans("Send")."\n"; - } + // Envoyer une relance + if ($fac->statut == 1 && price($resteapayer) > 0 && $user->rights->facture->envoyer) + { + print ' '.$langs->trans('SendRemind').''; + } - // Envoyer une relance - if ($fac->statut == 1 && price($resteapayer) > 0 && $user->rights->facture->envoyer) - { - print " id&action=prerelance\">".$langs->trans("SendRemind")."\n"; - } + // Emettre paiement + if ($fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->paiement) + { + print ' '.$langs->trans('DoPaiement').''; + } - // Emettre paiement - if ($fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->paiement) - { - print " id."&action=create\">".$langs->trans("DoPaiement")."\n"; - } + // Classer 'payé' + if ($fac->statut == 1 && price($resteapayer) <= 0 + && $fac->paye == 0 && $user->rights->facture->paiement) + { + print ' '.$langs->trans('ClassifyPayed').''; + } - // Classer 'payé' - if ($fac->statut == 1 && price($resteapayer) <= 0 - && $fac->paye == 0 && $user->rights->facture->paiement) - { - print " id&action=payed\">".$langs->trans("ClassifyPayed")."\n"; - } + // Classer 'abandonnée' (possible si validée et pas encore classer payée) + if ($fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->paiement) + { + print ' '.$langs->trans('ClassifyCanceled').''; + } - // Classer 'abandonnée' (possible si validée et pas encore classer payée) - if ($fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->paiement) - { - print " id&action=canceled\">".$langs->trans("ClassifyCanceled")."\n"; - } + // Récurrente + if (! defined('FACTURE_DISABLE_RECUR') || FACTURE_DISABLE_RECUR == 0) // Possibilité de désactiver les factures récurrentes + { + if ($fac->statut > 0) + { + print ' Récurrente'; + } + } - // Récurrente - if (! defined("FACTURE_DISABLE_RECUR") || FACTURE_DISABLE_RECUR == 0) // Possibilité de désactiver les factures récurrentes - { - if ($fac->statut > 0) - { - print " id."&action=create\">Récurrente\n"; - } - } + print '
'; + } - print "
\n"; + print '
'; - } - - print '
'; - - /* - * Documents générés - */ - $filename=sanitize_string($fac->ref); - $filedir=$conf->facture->dir_output . "/" . sanitize_string($fac->ref); - $urlsource=$_SERVER["PHP_SELF"]."?facid=".$fac->id; + /* + * Documents générés + */ + $filename=sanitize_string($fac->ref); + $filedir=$conf->facture->dir_output . '/' . sanitize_string($fac->ref); + $urlsource=$_SERVER['PHP_SELF'].'?facid='.$fac->id; // $genallowed=($fac->statut == 1 && ($fac->paye == 0 || $user->admin) && $user->rights->facture->creer); // $delallowed=$user->rights->facture->supprimer; - $genallowed=0; - $delallowed=0; - - $var=true; + $genallowed=0; + $delallowed=0; - print "
\n"; - $html->show_documents('facture',$filename,$filedir,$urlsource,$genallowed,$delallowed); + $var=true; + + print '
'; + $html->show_documents('facture',$filename,$filedir,$urlsource,$genallowed,$delallowed); + + /* + * Propales + */ + $sql = 'SELECT '.$db->pdate('p.datep').' as dp, p.price, p.ref, p.rowid as propalid'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'propal as p, '.MAIN_DB_PREFIX.'fa_pr as fp WHERE fp.fk_propal = p.rowid AND fp.fk_facture = '.$fac->id; + + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + if ($num) + { + $i = 0; $total = 0; + print '
'; + print_titre($langs->trans('RelatedCommercialProposals')); + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + $total = $total + $objp->price; + $i++; + } + print ''; + print '
'.$langs->trans('Ref').''.$langs->trans('Date').''.$langs->trans('Price').'
'.img_object($langs->trans('ShowPropal'),'propal').' '.$objp->ref.''.dolibarr_print_date($objp->dp).''.price($objp->price).'
 '.$langs->trans('TotalHT').''.price($total).'
'; + } + } + else + { + dolibarr_print_error($db); + } + + print '
'; + + /* + * Liste des actions propres à la facture + */ + $sql = 'SELECT id, '.$db->pdate('a.datea').' as da, a.label, a.note, code'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a, '.MAIN_DB_PREFIX.'user as u '; + $sql .= ' WHERE a.fk_user_author = u.rowid '; + $sql .= ' AND a.fk_action in (9,10) '; + $sql .= ' AND a.fk_soc = '.$fac->socidp ; + $sql .= ' AND a.fk_facture = '.$fac->id; + + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + if ($num) + { + print '
'; + print_titre($langs->trans('ActionsOnBill')); + + $i = 0; $total = 0; + print ''; + print ''; + print "\n"; + + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + print '
'.$langs->trans('Ref').''.$langs->trans('Date').''.$langs->trans('Action').''.$langs->trans('By').'
'.img_object($langs->trans('ShowTask'),'task').' '.$objp->id.''.dolibarr_print_date($objp->da).''.stripslashes($objp->label).''.$objp->code.'
'; + } + } + else + { + dolibarr_print_error($db); + } + + print '
'; - /* - * Propales - */ - $sql = "SELECT ".$db->pdate("p.datep")." as dp, p.price, p.ref, p.rowid as propalid"; - $sql .= " FROM ".MAIN_DB_PREFIX."propal as p, ".MAIN_DB_PREFIX."fa_pr as fp WHERE fp.fk_propal = p.rowid AND fp.fk_facture = $fac->id"; + /* + * Affiche formulaire mail + */ + if ($_GET['action'] == 'presend') + { + print '
'; + print_titre($langs->trans('SendBillByMail')); - $resql = $db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - if ($num) - { - $i = 0; $total = 0; - print "
"; - print_titre($langs->trans("RelatedCommercialProposals")); + $liste[0]=' '; + foreach ($soc->contact_email_array() as $key=>$value) + { + $liste[$key]=$value; + } - print ''; - print ''; - print ''; - print ''; - print ''; - print "\n"; + // Créé l'objet formulaire mail + include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); + $formmail = new FormMail($db); + $formmail->fromname = $user->fullname; + $formmail->frommail = $user->email; + $formmail->withfrom=1; + $formmail->withto=$liste; + $formmail->withcc=1; + $formmail->withtopic=$langs->trans('SendBillRef','__FACREF__'); + $formmail->withfile=1; + $formmail->withbody=1; + // Tableau des substitutions + $formmail->substit['__FACREF__']=$fac->ref; + // Tableau des paramètres complémentaires du post + $formmail->param['action']='send'; + $formmail->param['models']='facture_send'; + $formmail->param['facid']=$fac->id; + $formmail->param['returnurl']=DOL_URL_ROOT.'/compta/facture.php?facid='.$fac->id; - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($resql); - $var=!$var; - print ""; - print ''; - print "\n"; - print ''; - print ""; - $total = $total + $objp->price; - $i++; - } - print "\n"; - print "
'.$langs->trans("Ref").''.$langs->trans("Date").''.$langs->trans("Price").'
'.img_object($langs->trans("ShowPropal"),"propal").' '.$objp->ref.'".dolibarr_print_date($objp->dp)."'.price($objp->price).'
 ".$langs->trans("TotalHT")."".price($total)."
"; - } - } - else - { - dolibarr_print_error($db); - } + $formmail->show_form(); + print '
'; + } - print '
'; + if ($_GET['action'] == 'prerelance') + { + print '
'; + print_titre($langs->trans('SendReminderBillByMail')); - /* - * Liste des actions propres à la facture - */ - $sql = "SELECT id, ".$db->pdate("a.datea")." as da, a.label, a.note, code"; - $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a, ".MAIN_DB_PREFIX."user as u "; - $sql .= " WHERE a.fk_user_author = u.rowid "; - $sql .= " AND a.fk_action in (9,10) "; - $sql .= " AND a.fk_soc = ".$fac->socidp ; - $sql .= " AND a.fk_facture = ".$fac->id; + $liste[0]=' '; + foreach ($soc->contact_email_array() as $key=>$value) + { + $liste[$key]=$value; + } - $resql = $db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - if ($num) - { - print "
\n"; - print_titre($langs->trans("ActionsOnBill")); + // Créé l'objet formulaire mail + include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); + $formmail = new FormMail($db); + $formmail->fromname = $user->fullname; + $formmail->frommail = $user->email; + $formmail->withfrom=1; + $formmail->withto=$liste; + $formmail->withcc=1; + $formmail->withtopic=$langs->trans('SendReminderBillRef','__FACREF__'); + $formmail->withfile=1; + $formmail->withbody=1; + // Tableau des substitutions + $formmail->substit['__FACREF__']=$fac->ref; + // Tableau des paramètres complémentaires + $formmail->param['action']='relance'; + $formmail->param['models']='facture_relance'; + $formmail->param['facid']=$fac->id; + $formmail->param['returnurl']=DOL_URL_ROOT.'/compta/facture.php?facid='.$fac->id; - $i = 0; $total = 0; - print ''; - print ''; - print "\n"; + $formmail->show_form(); - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($resql); - $var=!$var; - print ""; - print ''; - print '\n"; - print ''; - print ''; - print "\n"; - $i++; - } - print "
'.$langs->trans("Ref").''.$langs->trans("Date").''.$langs->trans("Action").''.$langs->trans("By").'
'.img_object($langs->trans("ShowTask"),"task").' '.$objp->id.''.dolibarr_print_date($objp->da)."'.stripslashes($objp->label).''.$objp->code.'
\n"; - } - } - else - { - dolibarr_print_error($db); - } + print '
'; + } - print "
"; + } + else + { + dolibarr_print_error($db,$fac->error); + } + } + else + { + /*************************************************************************** + * * + * Mode Liste * + * * + ***************************************************************************/ + $page = $_GET['page']; + $sortorder=$_GET['sortorder']; + $sortfield=$_GET['sortfield']; + $month=$_GET['month']; + $year=$_GET['year']; + $fac=new Facture($db); - /* - * Affiche formulaire mail - */ - if ($_GET["action"] == 'presend') - { - print '
'; - print_titre($langs->trans("SendBillByMail")); + if ($page == -1) $page = 0 ; - $liste[0]=" "; - foreach ($soc->contact_email_array() as $key=>$value) { - $liste[$key]=$value; - } + if ($user->rights->facture->lire) + { + $limit = $conf->liste_limit; + $offset = $limit * $page ; - // Créé l'objet formulaire mail - include_once(DOL_DOCUMENT_ROOT."/html.formmail.class.php"); - $formmail = new FormMail($db); - $formmail->fromname = $user->fullname; - $formmail->frommail = $user->email; - $formmail->withfrom=1; - $formmail->withto=$liste; - $formmail->withcc=1; - $formmail->withtopic=$langs->trans("SendBillRef","__FACREF__"); - $formmail->withfile=1; - $formmail->withbody=1; - // Tableau des substitutions - $formmail->substit["__FACREF__"]=$fac->ref; - // Tableau des paramètres complémentaires du post - $formmail->param["action"]="send"; - $formmail->param["models"]="facture_send"; - $formmail->param["facid"]=$fac->id; - $formmail->param["returnurl"]=DOL_URL_ROOT."/compta/facture.php?facid=$fac->id"; + if (! $sortorder) $sortorder='DESC'; + if (! $sortfield) $sortfield='f.datef'; - $formmail->show_form(); + $sql = 'SELECT s.nom,s.idp,f.facnumber,f.increment,f.total,f.total_ttc,'; + $sql.= $db->pdate('f.datef').' as df, '.$db->pdate('f.date_lim_reglement').' as datelimite, '; + $sql.= ' f.paye as paye, f.rowid as facid, f.fk_statut'; + if (! $sall) $sql.= ' ,sum(pf.amount) as am'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s'; + $sql.= ','.MAIN_DB_PREFIX.'facture as f'; + if (! $sall) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiement_facture as pf ON f.rowid=pf.fk_facture '; + if ($sall) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'facturedet as fd ON f.rowid=fd.fk_facture '; + $sql.= ' WHERE f.fk_soc = s.idp'; + if ($socidp) $sql .= ' AND s.idp = '.$socidp; + if ($month > 0) $sql .= ' AND date_format(f.datef, \'%m\') = '.$month; + if ($_GET['filtre']) + { + $filtrearr = split(',', $_GET['filtre']); + foreach ($filtrearr as $fil) + { + $filt = split(':', $fil); + $sql .= ' AND ' . $filt[0] . ' = ' . $filt[1]; + } + } + if ($_GET['search_ref']) + { + $sql .= ' AND f.facnumber like \'%'.$_GET['search_ref'].'%\''; + } + if ($_GET['search_societe']) + { + $sql .= ' AND s.nom like \'%'.$_GET['search_societe'].'%\''; + } + if ($_GET['search_montant_ht']) + { + $sql .= ' AND f.total = \''.$_GET['search_montant_ht'].'\''; + } + if ($_GET['search_montant_ttc']) + { + $sql .= ' AND f.total_ttc = \''.$_GET['search_montant_ttc'].'\''; + } + if ($year > 0) + { + $sql .= ' AND date_format(f.datef, \'%Y\') = '.$year; + } + if ($_POST['sf_ref']) + { + $sql .= ' AND f.facnumber like \'%'.$_POST['sf_ref'] . '%\''; + } + if ($sall) + { + $sql .= ' AND (s.nom like \'%'.$sall.'%\' OR f.facnumber like \'%'.$sall.'%\' OR f.note like \'%'.$sall.'%\' OR fd.description like \'%'.$sall.'%\')'; + } - print '
'; - } + $sql .= ' GROUP BY f.facnumber'; - if ($_GET["action"] == 'prerelance') - { - print '
'; - print_titre($langs->trans("SendReminderBillByMail")); + $sql .= ' ORDER BY '; + $listfield=split(',',$sortfield); + foreach ($listfield as $key => $value) + $sql.= $listfield[$key].' '.$sortorder.','; + $sql .= ' f.rowid DESC '; - $liste[0]=" "; - foreach ($soc->contact_email_array() as $key=>$value) - { - $liste[$key]=$value; - } + $sql .= $db->plimit($limit+1,$offset); - // Créé l'objet formulaire mail - include_once("../html.formmail.class.php"); - $formmail = new FormMail($db); - $formmail->fromname = $user->fullname; - $formmail->frommail = $user->email; - $formmail->withfrom=1; - $formmail->withto=$liste; - $formmail->withcc=1; - $formmail->withtopic=$langs->trans("SendReminderBillRef","__FACREF__"); - $formmail->withfile=1; - $formmail->withbody=1; - // Tableau des substitutions - $formmail->substit["__FACREF__"]=$fac->ref; - // Tableau des paramètres complémentaires - $formmail->param["action"]="relance"; - $formmail->param["models"]="facture_relance"; - $formmail->param["facid"]=$fac->id; - $formmail->param["returnurl"]=DOL_URL_ROOT."/compta/facture.php?facid=$fac->id"; + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); - $formmail->show_form(); + if ($socidp) + { + $soc = new Societe($db); + $soc->fetch($socidp); + } - print '
'; - } + print_barre_liste($langs->trans('BillsCustomers').' '.($socidp?' '.$soc->nom:''),$page,'facture.php','&socidp='.$socidp,$sortfield,$sortorder,'',$num); - } - else - { - dolibarr_print_error($db,$fac->error); - } - } - else - { - /*************************************************************************** - * * - * Mode Liste * - * * - ***************************************************************************/ - $page = $_GET["page"]; - $sortorder=$_GET["sortorder"]; - $sortfield=$_GET["sortfield"]; - $month=$_GET["month"]; - $year=$_GET["year"]; + $i = 0; + print ''; + print ''; + print_liste_field_titre($langs->trans('Ref'),$_SERVER['PHP_SELF'],'f.facnumber','','&socidp='.$socidp,'',$sortfield); + print_liste_field_titre($langs->trans('Date'),$_SERVER['PHP_SELF'],'f.datef','','&socidp='.$socidp,'align="center"',$sortfield); + print_liste_field_titre($langs->trans('Company'),$_SERVER['PHP_SELF'],'s.nom','','&socidp='.$socidp,'',$sortfield); + print_liste_field_titre($langs->trans('AmountHT'),$_SERVER['PHP_SELF'],'f.total','','&socidp='.$socidp,'align="right"',$sortfield); + print_liste_field_titre($langs->trans('AmountTTC'),$_SERVER['PHP_SELF'],'f.total_ttc','','&socidp='.$socidp,'align="right"',$sortfield); + print_liste_field_titre($langs->trans('Received'),$_SERVER['PHP_SELF'],'am','','&socidp='.$socidp,'align="right"',$sortfield); + print_liste_field_titre($langs->trans('Status'),$_SERVER['PHP_SELF'],'fk_statut,paye','','&socidp='.$socidp,'align="right"',$sortfield); + print ''; - $fac=new Facture($db); + // Lignes des champs de filtre + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; - if ($page == -1) $page = 0 ; + if ($num > 0) + { + $var=True; + $total=0; + $totalrecu=0; - if ($user->rights->facture->lire) - { - $limit = $conf->liste_limit; - $offset = $limit * $page ; + while ($i < min($num,$limit)) + { + $objp = $db->fetch_object($resql); + $var=!$var; - if (! $sortorder) $sortorder="DESC"; - if (! $sortfield) $sortfield="f.datef"; + print ''; + if ($objp->paye) + { + $class = 'normal'; + } + else + { + if ($objp->fk_statut == 0) + { + $class = 'normal'; + } + else + { + $class = 'impayee'; + } + } - $sql = "SELECT s.nom,s.idp,f.facnumber,f.increment,f.total,f.total_ttc,"; - $sql.= $db->pdate("f.datef")." as df, ".$db->pdate("f.date_lim_reglement")." as datelimite, "; - $sql.= " f.paye as paye, f.rowid as facid, f.fk_statut"; - if (! $sall) $sql.= " ,sum(pf.amount) as am"; + print ''; - $sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; - $sql.= ",".MAIN_DB_PREFIX."facture as f"; - if (! $sall) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid=pf.fk_facture "; - if ($sall) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facturedet as fd ON f.rowid=fd.fk_facture "; + if ($objp->df > 0 ) + { + print ''; + } + else + { + print ''; + } + print ''; + print ''; + print ''; + print ''; - $sql.= " WHERE f.fk_soc = s.idp"; - if ($socidp) $sql .= " AND s.idp = $socidp"; - if ($month > 0) $sql .= " AND date_format(f.datef, '%m') = $month"; - if ($_GET["filtre"]) - { - $filtrearr = split(",", $_GET["filtre"]); - foreach ($filtrearr as $fil) - { - $filt = split(":", $fil); - $sql .= " AND " . $filt[0] . " = " . $filt[1]; - } - } - if ($_GET["search_ref"]) - { - $sql .= " AND f.facnumber like '%".$_GET["search_ref"]."%'"; - } - if ($_GET["search_societe"]) - { - $sql .= " AND s.nom like '%".$_GET["search_societe"]."%'"; - } - if ($_GET["search_montant_ht"]) - { - $sql .= " AND f.total = '".$_GET["search_montant_ht"]."'"; - } - if ($_GET["search_montant_ttc"]) - { - $sql .= " AND f.total_ttc = '".$_GET["search_montant_ttc"]."'"; - } - if ($year > 0) $sql .= " AND date_format(f.datef, '%Y') = $year"; - if ($_POST["sf_ref"]) - { - $sql .= " AND f.facnumber like '%".$_POST["sf_ref"] . "%'"; - } - if ($sall) - { - $sql .= " AND (s.nom like '%".$sall."%' OR f.facnumber like '%".$sall."%' OR f.note like '%".$sall."%' OR fd.description like '%".$sall."%')"; - } + // Affiche statut de la facture + if (! $objp->paye) + { + if ($objp->fk_statut == 0) + { + print ''; + } + elseif ($objp->fk_statut == 3) + { + print ''; + } + else + { + print ''; + } + } + else + { + print ''; + } - $sql .= " GROUP BY f.facnumber"; + print ''; + $total+=$objp->total; + $total_ttc+=$objp->total_ttc; + $totalrecu+=$objp->am; + $i++; + } - $sql .= " ORDER BY "; - $listfield=split(',',$sortfield); - foreach ($listfield as $key => $value) $sql.="$listfield[$key] $sortorder,"; - $sql .= " f.rowid DESC "; + if (($offset + $num) <= $limit) + { + // Print total + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + } + } - $sql .= $db->plimit($limit+1,$offset); - - $resql = $db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - - if ($socidp) - { - $soc = new Societe($db); - $soc->fetch($socidp); - } - - print_barre_liste($langs->trans("BillsCustomers")." ".($socidp?" $soc->nom":""),$page,"facture.php","&socidp=$socidp",$sortfield,$sortorder,'',$num); - - $i = 0; - print '
'; + print ''; + print ' '; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print '
'.img_object($langs->trans('ShowBill'),'bill').' '; + print ''.$objp->facnumber.''.$objp->increment; + if ($objp->datelimite < (time() - $conf->facture->client->warning_delay) && ! $objp->paye && $objp->fk_statut == 1 && ! $objp->am) print img_warning($langs->trans('Late')); + print ''; + $y = strftime('%Y',$objp->df); + $m = strftime('%m',$objp->df); + print strftime('%d',$objp->df); + print ' '; + print substr(strftime('%B',$objp->df),0,3).''; + print ' '; + print strftime('%Y',$objp->df).'!!!'.img_object($langs->trans('ShowCompany'),'company').' '.$objp->nom.''.price($objp->total).''.price($objp->total_ttc).''.price($objp->am).''.$langs->trans('BillShortStatusDraft').''.$langs->trans('BillShortStatusCanceled').''.($objp->am?$langs->trans('BillShortStatusStarted'):$langs->trans('BillShortStatusNotPayed')).''.$langs->trans('BillShortStatusPayed').'
'.$langs->trans('Total').''.price($total).''.price($total_ttc).''.price($totalrecu).' 
'; - print ''; - print_liste_field_titre($langs->trans("Ref"),$_SERVER["PHP_SELF"],"f.facnumber","","&socidp=$socidp","",$sortfield); - print_liste_field_titre($langs->trans("Date"),$_SERVER["PHP_SELF"],"f.datef","","&socidp=$socidp",'align="center"',$sortfield); - print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","","&socidp=$socidp","",$sortfield); - print_liste_field_titre($langs->trans("AmountHT"),$_SERVER["PHP_SELF"],"f.total","","&socidp=$socidp",'align="right"',$sortfield); - print_liste_field_titre($langs->trans("AmountTTC"),$_SERVER["PHP_SELF"],"f.total_ttc","","&socidp=$socidp",'align="right"',$sortfield); - print_liste_field_titre($langs->trans("Received"),$_SERVER["PHP_SELF"],"am","","&socidp=$socidp",'align="right"',$sortfield); - print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"fk_statut,paye","","&socidp=$socidp",'align="right"',$sortfield); - print "\n"; - - // Lignes des champs de filtre - print ''; - print ''; - print ''; - print ''; - print "\n"; - print ''; - - - if ($num > 0) - { - $var=True; - $total=0; - $totalrecu=0; - - while ($i < min($num,$limit)) - { - $objp = $db->fetch_object($resql); - $var=!$var; - - print ""; - if ($objp->paye) - { - $class = "normal"; - } - else - { - if ($objp->fk_statut == 0) - { - $class = "normal"; - } - else - { - $class = "impayee"; - } - } - - print '\n"; - - if ($objp->df > 0 ) - { - print "\n"; - } - else - { - print "\n"; - } - print ''; - - print ""; - print ""; - print ""; - - // Affiche statut de la facture - if (! $objp->paye) - { - if ($objp->fk_statut == 0) - { - print ''; - } - elseif ($objp->fk_statut == 3) - { - print ''; - } - else - { - print ''; - } - } - else - { - print ''; - } - - print "\n"; - $total+=$objp->total; - $total_ttc+=$objp->total_ttc; - $totalrecu+=$objp->am; - $i++; - } - - if (($offset + $num) <= $limit) - { - // Print total - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print "\n"; - } - } - - print "
'; - print ''; - print ' '; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print '
'.img_object($langs->trans("ShowBill"),"bill").' '; - print ''.$objp->facnumber.''.$objp->increment; - if ($objp->datelimite < (time() - $conf->facture->client->warning_delay) && ! $objp->paye && $objp->fk_statut == 1 && ! $objp->am) print img_warning($langs->trans("Late")); - print ""; - $y = strftime("%Y",$objp->df); - $m = strftime("%m",$objp->df); - - print strftime("%d",$objp->df)."\n"; - print ' '; - print substr(strftime("%B",$objp->df),0,3)."\n"; - print ' '; - print strftime("%Y",$objp->df)."!!!'.img_object($langs->trans("ShowCompany"),"company").' '.$objp->nom.'".price($objp->total)."".price($objp->total_ttc)."".price($objp->am)."'.$langs->trans("BillShortStatusDraft").''.$langs->trans("BillShortStatusCanceled").''.($objp->am?$langs->trans("BillShortStatusStarted"):$langs->trans("BillShortStatusNotPayed")).''.$langs->trans("BillShortStatusPayed").'
'.$langs->trans("Total").''.price($total).''.price($total_ttc).''.price($totalrecu).' 
"; - $db->free($resql); - } - else - { - dolibarr_print_error($db); - } - } - } + print ''; + $db->free($resql); + } + else + { + dolibarr_print_error($db); + } + } + } } $db->close(); diff --git a/htdocs/facture.class.php b/htdocs/facture.class.php index 03200a1fdfa..f872a3f726a 100644 --- a/htdocs/facture.class.php +++ b/htdocs/facture.class.php @@ -1,8 +1,9 @@ - * Copyright (C) 2004-2005 Laurent Destailleur - * Copyright (C) 2004 Sebastien Di Cintio - * Copyright (C) 2004 Benoit Mortier +/* Copyright (C) 2002-2005 Rodolphe Quiedeville + * Copyright (C) 2004-2005 Laurent Destailleur + * Copyright (C) 2004 Sebastien Di Cintio + * Copyright (C) 2004 Benoit Mortier + * Copyright (C) 2005 Marc Barilley / Ocebo * * 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 @@ -29,189 +30,188 @@ \version $Revision$ */ -require_once(DOL_DOCUMENT_ROOT ."/notify.class.php"); +require_once(DOL_DOCUMENT_ROOT .'/notify.class.php'); require_once(DOL_DOCUMENT_ROOT ."/product.class.php"); /** - \class Facture - \brief Classe permettant la gestion des factures clients + \class Facture + \brief Classe permettant la gestion des factures clients */ class Facture { - var $id; - var $db; - var $socidp; - var $number; - var $author; - var $date; - var $ref; - var $amount; - var $remise; - var $tva; - var $total; - var $note; - var $paye; - var $propalid; - var $projetid; - var $prefixe_facture; - var $cond_reglement_id; - var $cond_reglement_code; - var $mode_reglement_id; - var $mode_reglement_code; + var $id; + var $db; + var $socidp; + var $number; + var $author; + var $date; + var $ref; + var $amount; + var $remise; + var $tva; + var $total; + var $note; + var $paye; + var $propalid; + var $projetid; + var $prefixe_facture; + var $cond_reglement_id; + var $cond_reglement_code; + var $mode_reglement_id; + var $mode_reglement_code; - /** - * \brief Constructeur de la classe - * \param DB handler accès base de données - * \param soc_idp id societe ("" par defaut) - * \param facid id facture ("" par defaut) - */ - function Facture($DB, $soc_idp="", $facid="") - { - $this->db = $DB ; - $this->socidp = $soc_idp; - $this->products = array(); // Tableau de lignes de factures + /** + * \brief Constructeur de la classe + * \param DB handler accès base de données + * \param soc_idp id societe ('' par defaut) + * \param facid id facture ('' par defaut) + */ + function Facture($DB, $soc_idp='', $facid='') + { + $this->db = $DB ; + $this->socidp = $soc_idp; + $this->products = array(); // Tableau de lignes de factures - $this->amount = 0; - $this->remise = 0; - $this->remise_percent = 0; - $this->tva = 0; - $this->total = 0; - $this->propalid = 0; - $this->projetid = 0; - $this->id = $facid; - $this->prefixe_facture = ''; // utilisé dans le module de numérotation saturne - $this->remise_exceptionnelle = 0; - } + $this->amount = 0; + $this->remise = 0; + $this->remise_percent = 0; + $this->tva = 0; + $this->total = 0; + $this->propalid = 0; + $this->projetid = 0; + $this->id = $facid; + $this->prefixe_facture = ''; // utilisé dans le module de numérotation saturne + $this->remise_exceptionnelle = 0; + } - /** - * \brief Création de la facture en base - * \param user object utilisateur qui crée - */ - function create($user) - { - $this->db->begin(); - - /* On positionne en mode brouillon la facture */ - $this->brouillon = 1; + /** + * \brief Création de la facture en base + * \param user object utilisateur qui crée + */ + function create($user) + { + $this->db->begin(); - /* Facture récurrente */ - if ($this->fac_rec > 0) - { - require_once DOL_DOCUMENT_ROOT . '/compta/facture/facture-rec.class.php'; - $_facrec = new FactureRec($this->db, $this->fac_rec); - $_facrec->fetch($this->fac_rec); + /* On positionne en mode brouillon la facture */ + $this->brouillon = 1; - $this->projetid = $_facrec->projetid; - $this->cond_reglement = $_facrec->cond_reglement_id; - $this->cond_reglement_id = $_facrec->cond_reglement_id; - $this->mode_reglement = $_facrec->mode_reglement_id; - $this->mode_reglement_id = $_facrec->mode_reglement_id; - $this->amount = $_facrec->amount; - $this->remise = $_facrec->remise; - $this->remise_percent = $_facrec->remise_percent; - } + /* Facture récurrente */ + if ($this->fac_rec > 0) + { + require_once DOL_DOCUMENT_ROOT . '/compta/facture/facture-rec.class.php'; + $_facrec = new FactureRec($this->db, $this->fac_rec); + $_facrec->fetch($this->fac_rec); - // Definition de la date limite - $datelim=$this->calculate_date_lim_reglement(); + $this->projetid = $_facrec->projetid; + $this->cond_reglement = $_facrec->cond_reglement_id; + $this->cond_reglement_id = $_facrec->cond_reglement_id; + $this->mode_reglement = $_facrec->mode_reglement_id; + $this->mode_reglement_id = $_facrec->mode_reglement_id; + $this->amount = $_facrec->amount; + $this->remise = $_facrec->remise; + $this->remise_percent = $_facrec->remise_percent; + } - /* - * Insertion dans la base - */ - $socid = $this->socidp; - $number = $this->number; - $amount = $this->amount; - $remise = $this->remise; + // Definition de la date limite + $datelim=$this->calculate_date_lim_reglement(); - if (! $remise) $remise = 0 ; - if (strlen($this->mode_reglement_id)==0) $this->mode_reglement_id = 0; - if (! $this->projetid) $this->projetid = "NULL"; + /* + * Insertion dans la base + */ + $socid = $this->socidp; + $number = $this->number; + $amount = $this->amount; + $remise = $this->remise; - $totalht = ($amount - $remise); - $tva = tva($totalht); - $total = $totalht + $tva; + if (! $remise) $remise = 0 ; + if (strlen($this->mode_reglement_id)==0) $this->mode_reglement_id = 0; + if (! $this->projetid) $this->projetid = 'NULL'; - $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture (facnumber, fk_soc, datec, amount, remise, remise_percent"; - $sql .= ", datef, note, fk_user_author,fk_projet"; - $sql .= ", fk_cond_reglement, fk_mode_reglement, date_lim_reglement) "; - $sql .= " VALUES ('$number','$socid', now(), '$totalht', '$remise'"; - $sql .= ",'$this->remise_percent', ".$this->db->idate($this->date); - $sql .= ",'".addslashes($this->note)."',$user->id, $this->projetid"; - $sql .= ",".$this->cond_reglement_id.",".$this->mode_reglement_id.",".$this->db->idate($datelim).")"; - if ( $this->db->query($sql) ) - { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture"); + $totalht = ($amount - $remise); + $tva = tva($totalht); + $total = $totalht + $tva; - $sql = "UPDATE ".MAIN_DB_PREFIX."facture SET facnumber='(PROV".$this->id.")' WHERE rowid=".$this->id; - $this->db->query($sql); + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture (facnumber, fk_soc, datec, amount, remise, remise_percent'; + $sql .= ', datef, note, fk_user_author, fk_projet'; + $sql .= ', fk_cond_reglement, fk_mode_reglement, date_lim_reglement, ref_client) '; + $sql .= " VALUES ('$number','$socid', now(), '$totalht', '$remise'"; + $sql .= ",'$this->remise_percent', ".$this->db->idate($this->date); + $sql .= ",'".addslashes($this->note)."',$user->id, $this->projetid"; + $sql .= ','.$this->cond_reglement_id.','.$this->mode_reglement_id.','.$this->db->idate($datelim).', \''.$this->ref_client.'\')'; + if ( $this->db->query($sql) ) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture'); - if ($this->id && $this->propalid) - { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."fa_pr (fk_facture, fk_propal) VALUES (".$this->id.",".$this->propalid.")"; - $this->db->query($sql); - } + $sql = 'UPDATE '.MAIN_DB_PREFIX."facture SET facnumber='(PROV".$this->id.")' WHERE rowid=".$this->id; + $this->db->query($sql); - if ($this->id && $this->commandeid) - { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."co_fa (fk_facture, fk_commande) VALUES (".$this->id.",".$this->commandeid.")"; - $this->db->query($sql); - } + if ($this->id && $this->propalid) + { + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'fa_pr (fk_facture, fk_propal) VALUES ('.$this->id.','.$this->propalid.')'; + $this->db->query($sql); + } + if ($this->id && $this->commandeid) + { + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'co_fa (fk_facture, fk_commande) VALUES ('.$this->id.','.$this->commandeid.')'; + $this->db->query($sql); + } - /* - * Produits/services - * - */ - for ($i = 0 ; $i < sizeof($this->products) ; $i++) - { - $prod = new Product($this->db, $this->products[$i]); - $prod->fetch($this->products[$i]); + /* + * Produits/services + * + */ + for ($i = 0 ; $i < sizeof($this->products) ; $i++) + { + $prod = new Product($this->db, $this->products[$i]); + $prod->fetch($this->products[$i]); - $result_insert = $this->addline($this->id, - $prod->libelle, - $prod->price, - $this->products_qty[$i], - $prod->tva_tx, - $this->products[$i], - $this->products_remise_percent[$i], - $this->products_date_start[$i], - $this->products_date_end[$i] - ); + $result_insert = $this->addline($this->id, + $prod->libelle, + $prod->price, + $this->products_qty[$i], + $prod->tva_tx, + $this->products[$i], + $this->products_remise_percent[$i], + $this->products_date_start[$i], + $this->products_date_end[$i] + ); - if ( $result_insert < 0) - { - dolibarr_print_error($this->db); - } - } - - /* - * Produits de la facture récurrente - * - */ - if ($this->fac_rec > 0) - { - for ($i = 0 ; $i < sizeof($_facrec->lignes) ; $i++) - { - if ($_facrec->lignes[$i]->produit_id) - { - $prod = new Product($this->db, $_facrec->lignes[$i]->produit_id); - $prod->fetch($_facrec->lignes[$i]->produit_id); - } + if ( $result_insert < 0) + { + dolibarr_print_error($this->db); + } + } - $result_insert = $this->addline($this->id, - addslashes($_facrec->lignes[$i]->desc), - $_facrec->lignes[$i]->subprice, - $_facrec->lignes[$i]->qty, - $_facrec->lignes[$i]->tva_taux, - $_facrec->lignes[$i]->produit_id, - $_facrec->lignes[$i]->remise_percent); + /* + * Produits de la facture récurrente + * + */ + if ($this->fac_rec > 0) + { + for ($i = 0 ; $i < sizeof($_facrec->lignes) ; $i++) + { + if ($_facrec->lignes[$i]->produit_id) + { + $prod = new Product($this->db, $_facrec->lignes[$i]->produit_id); + $prod->fetch($_facrec->lignes[$i]->produit_id); + } - if ($result_insert < 0) - { - dolibarr_print_error($this->db); - } - } - } + $result_insert = $this->addline($this->id, + addslashes($_facrec->lignes[$i]->desc), + $_facrec->lignes[$i]->subprice, + $_facrec->lignes[$i]->qty, + $_facrec->lignes[$i]->tva_taux, + $_facrec->lignes[$i]->produit_id, + $_facrec->lignes[$i]->remise_percent); + + if ( $result_insert < 0) + { + dolibarr_print_error($this->db); + } + } + } $result=$this->updateprice($this->id); if ($result) @@ -239,364 +239,363 @@ class Facture } - /* - * \brief Affecte la remise exceptionnelle - */ - function _affect_remise_exceptionnelle() - { - $error = 0; + /* + * \brief Affecte la remise exceptionnelle + */ + function _affect_remise_exceptionnelle() + { + $error = 0; + $this->db->begin(); + + if ($this->remise_exceptionnelle[1] > 0) + { + // Calcul valeur de remise a appliquer (remise) et reliquat + if ($this->remise_exceptionnelle[1] > ($this->total_ht * 0.9)) + { + $remise = floor($this->total_ht * 0.9); + $reliquat = $this->remise_exceptionnelle[1] - $remise; + } + else + { + $remise = $this->remise_exceptionnelle[1]; + $reliquat=0; + } + + $result_insert = $this->addline($this->id, + addslashes('Remise exceptionnelle'), + (0 - $remise), + 1, + '0'); // Une remise est un négatif sur le TTC, on ne doit pas appliquer de TVA, + // sinon on impute une TVA négative. + + if ($result_insert < 0) + { + $error++; + } + + $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except'; + $sql .= ' SET fk_facture = '.$this->id; + $sql .= " ,amount_ht = '".ereg_replace(',','.',$remise)."'"; + $sql .= ' WHERE rowid ='.$this->remise_exceptionnelle[0]; + $sql .= ' AND fk_soc ='. $this->socidp; + + if (! $this->db->query( $sql)) + { + $error++; + } + + if ($reliquat > 0) + { + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'societe_remise_except'; + $sql .= ' (fk_soc, datec, amount_ht, fk_user) '; + $sql .= ' VALUES '; + $sql .= ' ('.$this->socidp; + $sql .= ' ,now()'; + $sql .= " ,'".ereg_replace(',','.',$reliquat)."'"; + $sql .= ' ,'.$this->remise_exceptionnelle[3]; + $sql .= ')'; + + if (! $this->db->query( $sql) ) + { + $error++; + } + } + } + + if (! $error) + { + $this->db->commit(); + } + else + { + $this->db->rollback(); + } + + return $error; + } + + /** + * \brief Recupére l'objet facture et ses lignes de factures + * \param rowid id de la facture a récupérer + * \param societe_id id de societe + * \return int 1 si ok, < 0 si erreur + */ + function fetch($rowid, $societe_id=0) + { + //dolibarr_syslog("Facture::Fetch rowid : $rowid, societe_id : $societe_id"); + + $sql = 'SELECT f.fk_soc,f.facnumber,f.amount,f.tva,f.total,f.total_ttc,f.remise,f.remise_percent'; + $sql .= ','.$this->db->pdate('f.datef').' as df, f.fk_projet'; + $sql .= ','.$this->db->pdate('f.date_lim_reglement').' as dlr'; + $sql .= ', f.note, f.paye, f.fk_statut, f.fk_user_author'; + $sql .= ', f.fk_mode_reglement, f.ref_client, p.code as mode_reglement_code, p.libelle as mode_reglement_libelle'; + $sql .= ', f.fk_cond_reglement, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_facture'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'facture as f, '.MAIN_DB_PREFIX.'cond_reglement as c'; + $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON f.fk_mode_reglement = p.id'; + $sql .= ' WHERE f.rowid='.$rowid.' AND c.rowid = f.fk_cond_reglement'; + if ($societe_id > 0) + { + $sql .= ' AND f.fk_soc = '.$societe_id; + } + $result = $this->db->query($sql); + + if ($result) + { + if ($this->db->num_rows($result)) + { + $obj = $this->db->fetch_object($result); + //print strftime('%Y%m%d%H%M%S',$obj->df).' '.$obj->df.' '.dolibarr_print_date($obj->df); + + $this->id = $rowid; + $this->datep = $obj->dp; + $this->date = $obj->df; + $this->ref = $obj->facnumber; + $this->ref_client = $obj->ref_client; + $this->amount = $obj->amount; + $this->remise = $obj->remise; + $this->total_ht = $obj->total; + $this->total_tva = $obj->tva; + $this->total_ttc = $obj->total_ttc; + $this->paye = $obj->paye; + $this->remise_percent = $obj->remise_percent; + $this->socidp = $obj->fk_soc; + $this->statut = $obj->fk_statut; + $this->date_lim_reglement = $obj->dlr; + $this->mode_reglement_id = $obj->fk_mode_reglement; + $this->mode_reglement_code = $obj->mode_reglement_code; + $this->mode_reglement = $obj->mode_reglement_libelle; + $this->cond_reglement_id = $obj->fk_cond_reglement; + $this->cond_reglement = $obj->cond_reglement_libelle; + $this->cond_reglement_facture = $obj->cond_reglement_libelle_facture; + $this->projetid = $obj->fk_projet; + $this->note = stripslashes($obj->note); + $this->user_author = $obj->fk_user_author; + $this->lignes = array(); + + if ($this->statut == 0) + { + $this->brouillon = 1; + } + + /* + * Lignes + */ + $sql = 'SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_taux, l.remise, l.remise_percent, l.subprice, '.$this->db->pdate('l.date_start').' as date_start,'.$this->db->pdate('l.date_end').' as date_end,'; + $sql.= ' p.label as label, p.description as product_desc'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet as l'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid'; + $sql.= ' WHERE l.fk_facture = '.$this->id; + $sql.= ' ORDER BY l.rang'; + $result2 = $this->db->query($sql); + if ($result2) + { + $num = $this->db->num_rows($result2); + $i = 0; $total = 0; + while ($i < $num) + { + $objp = $this->db->fetch_object($result2); + $faclig = new FactureLigne($this->db); + $faclig->desc = stripslashes($objp->description); // Description ligne + $faclig->libelle = stripslashes($objp->label); // Label produit + $faclig->product_desc = stripslashes($objp->product_desc); // Description produit + $faclig->qty = $objp->qty; + $faclig->price = $objp->price; + $faclig->subprice = $objp->subprice; + $faclig->tva_taux = $objp->tva_taux; + $faclig->remise = $objp->remise; + $faclig->remise_percent = $objp->remise_percent; + $faclig->produit_id = $objp->fk_product; + $faclig->date_start = $objp->date_start; + $faclig->date_end = $objp->date_end; + $this->lignes[$i] = $faclig; + $i++; + } + $this->db->free($result2); + $this->db->free($result); + return 1; + } + else + { + dolibarr_syslog('Erreur Facture::Fetch rowid='.$rowid.', Erreur dans fetch des lignes'); + $this->error=$this->db->error(); + return -3; + } + } + else + { + dolibarr_syslog('Erreur Facture::Fetch rowid='.$rowid.' numrows=0 sql='.$sql); + $this->error='Bill with id '.$rowid.' not found sql='.$sql; + return -2; + } + $this->db->free($result); + } + else + { + dolibarr_syslog('Erreur Facture::Fetch rowid='.$rowid.' Erreur dans fetch de la facture'); + $this->error=$this->db->error(); + return -1; + } + } + + /** + * \brief Recupére l'objet client lié à la facture + * + */ + function fetch_client() + { + $client = new Societe($this->db); + $client->fetch($this->socidp); + $this->client = $client; + } + + /** + * \brief Valide la facture + * \param userid id de l'utilisateur qui valide + */ + function valid($userid) + { + $error = 0; + + if ($this->db->begin()) + { + /* + * Lecture de la remise exceptionnelle + * + */ + $sql = 'SELECT rowid, rc.amount_ht, fk_soc, fk_user'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc'; + $sql .= ' WHERE rc.fk_soc ='. $this->socidp; + $sql .= ' AND fk_facture IS NULL'; + $resql = $this->db->query($sql) ; + if ( $resql) + { + $nurmx = $this->db->num_rows($resql); + if ($nurmx > 0) + { + $row = $this->db->fetch_row($resql); + $this->remise_exceptionnelle = $row; + } + $this->db->free($resql); + } + else + { + dolibarr_syslog('Facture::Valide Erreur lecture Remise'); + $error++; + } + + /* + * Affectation de la remise exceptionnelle + */ + if ( $this->_affect_remise_exceptionnelle() <> 0) + { + $error++; + } + else + { + $this->updateprice($this->id); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture SET fk_statut = 1, date_valid=now(), fk_user_valid='.$userid; + $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;'; + if (! $this->db->query($sql) ) + { + $error++; + dolibarr_syslog('Facture::Valide Erreur '); + } + } + + if ($error == 0) + { + $this->db->commit(); + } + else + { + $this->db->rollback(); + } + } + else + { + $error++; + } + + if ($error > 0) + { + return 0; + } + else + { + return 1; + } + } + + /** + * \brief Classe la facture dans un projet + * \param projid Id du projet dans lequel classer la facture + */ + function classin($projid) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; + if ($projid) $sql.= ' SET fk_projet = '.$projid; + else $sql.= ' SET fk_projet = NULL'; + $sql.= ' WHERE rowid = '.$this->id; + if ($this->db->query($sql)) + { + return 1; + } + else + { + dolibarr_print_error($this->db); + return -1; + } + } + + function set_ref_client($ref_client) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; + if (empty($ref_client)) + $sql .= ' SET ref_client = NULL'; + else + $sql .= ' SET ref_client = \''.$ref_client.'\''; + $sql .= ' WHERE rowid = '.$this->id; + if ($this->db->query($sql)) + { + $this->ref_client = $ref_client; + return 1; + } + else + { + dolibarr_print_error($this->db); + return -1; + } + } + + /** + * \brief Supprime la facture + * \param rowid id de la facture à supprimer + */ + function delete($rowid) + { $this->db->begin(); - - if ($this->remise_exceptionnelle[1] > 0) - { - // Calcul valeur de remise a appliquer (remise) et reliquat - if ($this->remise_exceptionnelle[1] > ($this->total_ht * 0.9)) - { - $remise = floor($this->total_ht * 0.9); - $reliquat = $this->remise_exceptionnelle[1] - $remise; - } - else - { - $remise = $this->remise_exceptionnelle[1]; - $reliquat=0; - } + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_tva_sum WHERE fk_facture = '.$rowid; - $result_insert = $this->addline($this->id, - addslashes("Remise exceptionnelle"), - (0 - $remise), - 1, - '0'); // Une remise est un négatif sur le TTC, on ne doit pas appliquer de TVA, - // sinon on impute une TVA négative. - - if ($result_insert < 0) - { - $error++; - } - - $sql = "UPDATE ".MAIN_DB_PREFIX."societe_remise_except"; - $sql .= " SET fk_facture = ".$this->id; - $sql .= " ,amount_ht = '".ereg_replace(",",".",$remise)."'"; - $sql .= " WHERE rowid =".$this->remise_exceptionnelle[0]; - $sql .= " AND fk_soc =". $this->socidp; - - if (! $this->db->query( $sql)) - { - $error++; - } - - if ($reliquat > 0) - { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise_except"; - $sql .= " (fk_soc, datec, amount_ht, fk_user) "; - $sql .= " VALUES "; - $sql .= " (".$this->socidp; - $sql .= " ,now()"; - $sql .= " ,'".ereg_replace(",",".",$reliquat)."'"; - $sql .= " ,".$this->remise_exceptionnelle[3]; - $sql .= ")"; - - if (! $this->db->query( $sql) ) - { - $error++; - } - } - - } - - if (! $error) - { - $this->db->commit(); - } - else - { - $this->db->rollback(); - } - - return $error; - } - - /** - * \brief Recupére l'objet facture et ses lignes de factures - * \param rowid id de la facture a récupérer - * \param societe_id id de societe - * \return int 1 si ok, < 0 si erreur - */ - function fetch($rowid, $societe_id=0) - { - //dolibarr_syslog("Facture::Fetch rowid : $rowid, societe_id : $societe_id"); - - $sql = "SELECT f.fk_soc,f.facnumber,f.amount,f.tva,f.total,f.total_ttc,f.remise,f.remise_percent"; - $sql .= ",".$this->db->pdate("f.datef")." as df, f.fk_projet"; - $sql .= ",".$this->db->pdate("f.date_lim_reglement")." as dlr"; - $sql .= ", f.note, f.paye, f.fk_statut, f.fk_user_author"; - $sql .= ", f.fk_mode_reglement, p.code as mode_reglement_code, p.libelle as mode_reglement_libelle"; - $sql .= ", f.fk_cond_reglement, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_facture"; - $sql .= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."cond_reglement as c"; - $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as p ON f.fk_mode_reglement = p.id"; - $sql .= " WHERE f.rowid=$rowid AND c.rowid = f.fk_cond_reglement"; - if ($societe_id > 0) - { - $sql .= " AND f.fk_soc = ".$societe_id; - } - $result = $this->db->query($sql); - - if ($result) - { - if ($this->db->num_rows($result)) - { - $obj = $this->db->fetch_object($result); - //print strftime("%Y%m%d%H%M%S",$obj->df)." ".$obj->df." ".dolibarr_print_date($obj->df); - - $this->id = $rowid; - $this->datep = $obj->dp; - $this->date = $obj->df; - $this->ref = $obj->facnumber; - $this->amount = $obj->amount; - $this->remise = $obj->remise; - $this->total_ht = $obj->total; - $this->total_tva = $obj->tva; - $this->total_ttc = $obj->total_ttc; - $this->paye = $obj->paye; - $this->remise_percent = $obj->remise_percent; - $this->socidp = $obj->fk_soc; - $this->statut = $obj->fk_statut; - $this->date_lim_reglement = $obj->dlr; - $this->mode_reglement_id = $obj->fk_mode_reglement; - $this->mode_reglement_code = $obj->mode_reglement_code; - $this->mode_reglement = $obj->mode_reglement_libelle; - $this->cond_reglement_id = $obj->fk_cond_reglement; - $this->cond_reglement = $obj->cond_reglement_libelle; - $this->cond_reglement_facture = $obj->cond_reglement_libelle_facture; - $this->projetid = $obj->fk_projet; - $this->note = stripslashes($obj->note); - $this->user_author = $obj->fk_user_author; - $this->lignes = array(); - - if ($this->statut == 0) - { - $this->brouillon = 1; - } - - - /* - * Lignes - */ - - $sql = "SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_taux, l.remise, l.remise_percent, l.subprice, ".$this->db->pdate("l.date_start")." as date_start,".$this->db->pdate("l.date_end")." as date_end,"; - $sql.= " p.label as label, p.description as product_desc"; - $sql.= " FROM ".MAIN_DB_PREFIX."facturedet as l"; - $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON l.fk_product = p.rowid"; - $sql.= " WHERE l.fk_facture = ".$this->id; - $sql.= " ORDER BY l.rang"; - - $result2 = $this->db->query($sql); - if ($result2) - { - $num = $this->db->num_rows($result2); - $i = 0; $total = 0; - - while ($i < $num) - { - $objp = $this->db->fetch_object($result2); - $faclig = new FactureLigne($this->db); - $faclig->desc = stripslashes($objp->description); // Description ligne - $faclig->libelle = stripslashes($objp->label); // Label produit - $faclig->product_desc = stripslashes($objp->product_desc); // Description produit - $faclig->qty = $objp->qty; - $faclig->price = $objp->price; - $faclig->subprice = $objp->subprice; - $faclig->tva_taux = $objp->tva_taux; - $faclig->remise = $objp->remise; - $faclig->remise_percent = $objp->remise_percent; - $faclig->produit_id = $objp->fk_product; - $faclig->date_start = $objp->date_start; - $faclig->date_end = $objp->date_end; - $this->lignes[$i] = $faclig; - $i++; - } - - $this->db->free($result2); - $this->db->free($result); - - return 1; - } - else - { - dolibarr_syslog("Erreur Facture::Fetch rowid=$rowid, Erreur dans fetch des lignes"); - $this->error=$this->db->error(); - return -3; - } - } - else - { - dolibarr_syslog("Erreur Facture::Fetch rowid=$rowid numrows=0 sql=$sql"); - $this->error="Bill with id $rowid not found sql=$sql"; - return -2; - } - - $this->db->free($result); - } - else - { - dolibarr_syslog("Erreur Facture::Fetch rowid=$rowid Erreur dans fetch de la facture"); - $this->error=$this->db->error(); - return -1; - } - } - - /** - * \brief Recupére l'objet client lié à la facture - * - */ - function fetch_client() - { - $client = new Societe($this->db); - $client->fetch($this->socidp); - $this->client = $client; - } - - /** - * \brief Valide la facture - * \param userid id de l'utilisateur qui valide - */ - function valid($userid) - { - $error = 0; - - if ($this->db->begin()) - { - /* - * Lecture de la remise exceptionnelle - * - */ - $sql = "SELECT rowid, rc.amount_ht, fk_soc, fk_user"; - $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc"; - $sql .= " WHERE rc.fk_soc =". $this->socidp; - $sql .= " AND fk_facture IS NULL"; - - $resql = $this->db->query($sql) ; - - if ( $resql) - { - $nurmx = $this->db->num_rows($resql); - - if ($nurmx > 0) - { - $row = $this->db->fetch_row($resql); - $this->remise_exceptionnelle = $row; - } - $this->db->free($resql); - } - else - { - dolibarr_syslog("Facture::Valide Erreur lecture Remise"); - $error++; - } - - /* - * Affectation de la remise exceptionnelle - */ - if ( $this->_affect_remise_exceptionnelle() <> 0) - { - $error++; - } - else - { - $this->updateprice($this->id); - - $sql = "UPDATE ".MAIN_DB_PREFIX."facture SET fk_statut = 1, date_valid=now(), fk_user_valid=$userid"; - - $sql .= " WHERE rowid = ".$this->id." AND fk_statut = 0 ;"; - - if (! $this->db->query($sql) ) - { - $error++; - dolibarr_syslog("Facture::Valide Erreur "); - } - } - - if ($error == 0) - { - $this->db->commit(); - } - else - { - $this->db->rollback(); - } - } - else - { - $error++; - } - - if ($error > 0) - { - return 0; - } - else - { - return 1; - } - } - - /** - * \brief Classe la facture dans un projet - * \param projid Id du projet dans lequel classer la facture - */ - function classin($projid) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."facture"; - if ($projid) $sql.= " SET fk_projet = $projid"; - else $sql.= " SET fk_projet = NULL"; - $sql.= " WHERE rowid = ".$this->id; - - if ($this->db->query($sql)) - { - return 1; - } - else - { - dolibarr_print_error($this->db); - return -1; - } - } - - /** - * \brief Supprime la facture - * \param rowid id de la facture à supprimer - */ - function delete($rowid) - { - $this->db->begin(); - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_tva_sum WHERE fk_facture = $rowid;"; - - if ( $this->db->query( $sql) ) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."fa_pr WHERE fk_facture = $rowid;"; - - if ($this->db->query( $sql) ) - { - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."co_fa WHERE fk_facture = $rowid;"; - - if ($this->db->query( $sql) ) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet WHERE fk_facture = $rowid;"; - - if ($this->db->query( $sql) ) - { - /* - * On repositionne la remise - */ - $sql = "UPDATE ".MAIN_DB_PREFIX."societe_remise_except"; - $sql .= " SET fk_facture = NULL WHERE fk_facture = $rowid"; - - if ($this->db->query( $sql) ) - { - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture WHERE rowid = $rowid AND fk_statut = 0;"; + if ( $this->db->query( $sql) ) + { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'fa_pr WHERE fk_facture = '.$rowid; + if ($this->db->query( $sql) ) + { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'co_fa WHERE fk_facture = '.$rowid; + if ($this->db->query( $sql) ) + { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facturedet WHERE fk_facture = '.$rowid; + if ($this->db->query( $sql) ) + { + /* + * On repositionne la remise + */ + $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except'; + $sql .= ' SET fk_facture = NULL WHERE fk_facture = '.$rowid; + if ($this->db->query( $sql) ) + { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture WHERE rowid = '.$rowid.' AND fk_statut = 0'; $resql=$this->db->query($sql) ; if ($resql) @@ -648,167 +647,160 @@ class Facture } - /** - * \brief Renvoi une date limite de reglement de facture en fonction des - * conditions de reglements de la facture et date de facturation - * \param cond_reglement_id Condition de reglement à utiliser, 0=Condition actuelle de la facture - * \return date Date limite de réglement si ok, <0 si ko - */ - function calculate_date_lim_reglement($cond_reglement_id=0) - { - if (! $cond_reglement_id) $cond_reglement_id=$this->cond_reglement_id; - - $sqltemp = "SELECT c.fdm,c.nbjour"; - $sqltemp.= " FROM ".MAIN_DB_PREFIX."cond_reglement as c"; - $sqltemp.= " WHERE c.rowid=".$cond_reglement_id; - $resqltemp=$this->db->query($sqltemp); - if ($resqltemp) - { - if ($this->db->num_rows($resqltemp)) - { - $obj = $this->db->fetch_object($resqltemp); - $cdr_nbjour = $obj->nbjour; - $cdr_fdm = $obj->fdm; - } - } - else - { - $this->error=$this->db->error(); - return -1; - } - $this->db->free($resqltemp); - // Definition de la date limite - $datelim = $this->date + ( $cdr_nbjour * 3600 * 24 ); - if ($cdr_fdm) - { - $mois=date('m', $datelim); - $annee=date('Y', $datelim); - $fins=array(31,28,31,30,31,30,31,31,30,31,30,31); - $datelim=mktime(12,0,0,$mois,$fins[$mois-1],$annee); - } + /** + * \brief Renvoi une date limite de reglement de facture en fonction des + * conditions de reglements de la facture et date de facturation + * \param cond_reglement_id Condition de reglement à utiliser, 0=Condition actuelle de la facture + * \return date Date limite de réglement si ok, <0 si ko + */ + function calculate_date_lim_reglement($cond_reglement_id=0) + { + if (! $cond_reglement_id) + $cond_reglement_id=$this->cond_reglement_id; + $sqltemp = 'SELECT c.fdm,c.nbjour'; + $sqltemp.= ' FROM '.MAIN_DB_PREFIX.'cond_reglement as c'; + $sqltemp.= ' WHERE c.rowid='.$cond_reglement_id; + $resqltemp=$this->db->query($sqltemp); + if ($resqltemp) + { + if ($this->db->num_rows($resqltemp)) + { + $obj = $this->db->fetch_object($resqltemp); + $cdr_nbjour = $obj->nbjour; + $cdr_fdm = $obj->fdm; + } + } + else + { + $this->error=$this->db->error(); + return -1; + } + $this->db->free($resqltemp); + // Definition de la date limite + $datelim = $this->date + ( $cdr_nbjour * 3600 * 24 ); + if ($cdr_fdm) + { + $mois=date('m', $datelim); + $annee=date('Y', $datelim); + $fins=array(31,28,31,30,31,30,31,31,30,31,30,31); + $datelim=mktime(12,0,0,$mois,$fins[$mois-1],$annee); + } + return $datelim; + } - return $datelim; - } - - /** - * \brief Tag la facture comme payée complètement - * \param rowid id de la facture à modifier - */ - function set_payed($rowid) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."facture set paye=1 WHERE rowid = ".$rowid ; - $return = $this->db->query( $sql); - } - /** - * \brief Tag la facture comme payée complètement - * \param rowid id de la facture à modifier - */ - function set_unpayed($rowid) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."facture set paye=0 WHERE rowid = ".$rowid ; - $return = $this->db->query( $sql); - } - /** - * \brief Tag la facture comme paiement commencée - * \param rowid id de la facture à modifier - */ - function set_paiement_started($rowid) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."facture set fk_statut=2 WHERE rowid = ".$rowid; - $return = $this->db->query( $sql); - } + /** + * \brief Tag la facture comme payée complètement + * \param rowid id de la facture à modifier + */ + function set_payed($rowid) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture set paye=1 WHERE rowid = '.$rowid ; + $return = $this->db->query( $sql); + } + /** + * \brief Tag la facture comme payée complètement + * \param rowid id de la facture à modifier + */ + function set_unpayed($rowid) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture set paye=0 WHERE rowid = '.$rowid ; + $return = $this->db->query( $sql); + } + /** + * \brief Tag la facture comme paiement commencée + * \param rowid id de la facture à modifier + */ + function set_paiement_started($rowid) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture set fk_statut=2 WHERE rowid = '.$rowid; + $return = $this->db->query( $sql); + } - /** - * \brief Tag la facture comme abandonnée - * \param rowid id de la facture à modifier - */ - function set_canceled($rowid) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."facture set fk_statut=3 WHERE rowid = ".$rowid; - $return = $this->db->query( $sql); - } + /** + * \brief Tag la facture comme abandonnée + * \param rowid id de la facture à modifier + */ + function set_canceled($rowid) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture set fk_statut=3 WHERE rowid = '.$rowid; + $return = $this->db->query( $sql); + } - /** - * \brief Tag la facture comme validée et valide la facture - * \param rowid id de la facture à valider - * \param user utilisateur qui valide la facture - * \param soc societe - * \param force_number force le numéro de facture - */ - function set_valid($rowid, $user, $soc, $force_number='') - { - global $conf; - $error = 0; - - if ($this->brouillon) - { - $action_notify = 2; // ne pas modifier cette valeur - - if ($force_number) - { - $numfa=$force_number; - } - else - { - $numfa = facture_get_num($soc, $this->prefixe_facture); // définit dans includes/modules/facture - } + /** + * \brief Tag la facture comme validée et valide la facture + * \param rowid id de la facture à valider + * \param user utilisateur qui valide la facture + * \param soc societe + * \param force_number force le numéro de facture + */ + function set_valid($rowid, $user, $soc, $force_number='') + { + global $conf; + $error = 0; + if ($this->brouillon) + { + $action_notify = 2; // ne pas modifier cette valeur + if ($force_number) + { + $numfa=$force_number; + } + else + { + $numfa = facture_get_num($soc, $this->prefixe_facture); // définit dans includes/modules/facture + } $this->db->begin(); - /* - * Lecture de la remise exceptionnelle - */ - $sql = "SELECT rowid, rc.amount_ht, fk_soc, fk_user"; - $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc"; - $sql .= " WHERE rc.fk_soc =". $this->socidp; - $sql .= " AND fk_facture IS NULL"; + /* + * Lecture de la remise exceptionnelle + * + */ + $sql = 'SELECT rowid, rc.amount_ht, fk_soc, fk_user'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc'; + $sql .= ' WHERE rc.fk_soc ='. $this->socidp; + $sql .= ' AND fk_facture IS NULL'; + $resql = $this->db->query($sql) ; + if ($resql) + { + $nurmx = $this->db->num_rows($resql); + if ($nurmx > 0) + { + $row = $this->db->fetch_row($resql); + $this->remise_exceptionnelle = $row; + } + $this->db->free($resql); + } + else + { + dolibarr_syslog('Facture::Valide Erreur lecture Remise'); + $error++; + } - $resql = $this->db->query($sql) ; - if ($resql) - { - $nurmx = $this->db->num_rows($resql); + /* Affectation de la remise exceptionnelle */ + if ( $this->_affect_remise_exceptionnelle() <> 0) + { + $error++; + } + else + { + $this->updateprice($this->id); + } - if ($nurmx > 0) - { - $row = $this->db->fetch_row($resql); - $this->remise_exceptionnelle = $row; - } - $this->db->free($resql); - } - else - { - dolibarr_syslog("Facture::Valide Erreur lecture Remise"); - $error++; - } + /* Validation de la facture */ + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture '; + $sql .= " SET facnumber='$numfa', fk_statut = 1, fk_user_valid = $user->id"; - /* Affectation de la remise exceptionnelle */ - if ( $this->_affect_remise_exceptionnelle() <> 0) - { - $error++; - } - else - { - $this->updateprice($this->id); - } - - /* Validation de la facture */ - $sql = "UPDATE ".MAIN_DB_PREFIX."facture "; - $sql .= " SET facnumber='$numfa', fk_statut = 1, fk_user_valid = $user->id"; - - /* Si l'option est activée on force la date de facture */ - if (defined("FAC_FORCE_DATE_VALIDATION") && FAC_FORCE_DATE_VALIDATION == "1") - { - $this->date=time(); - $datelim=$this->calculate_date_lim_reglement(); - - $sql .= ", datef=".$this->db->idate($this->date); - $sql .= ", date_lim_reglement=".$this->db->idate($datelim); - } - - $sql .= " WHERE rowid = $rowid ;"; - - $resql = $this->db->query($sql); + /* Si l'option est activée on force la date de facture */ + if (defined('FAC_FORCE_DATE_VALIDATION') && FAC_FORCE_DATE_VALIDATION == '1') + { + $this->date=time(); + $datelim=$this->calculate_date_lim_reglement(); + $sql .= ', datef='.$this->db->idate($this->date); + $sql .= ', date_lim_reglement='.$this->db->idate($datelim); + } + $sql .= ' WHERE rowid = '.$rowid; + $resql = $this->db->query($sql); if (! $resql) { dolibarr_syslog("Facture::set_valid() Echec - 10"); @@ -819,8 +811,8 @@ class Facture /* * Update Stats */ - $sql = "SELECT fk_product FROM ".MAIN_DB_PREFIX."facturedet WHERE fk_facture = ".$this->id; - $sql .= " AND fk_product > 0"; + $sql = 'SELECT fk_product FROM '.MAIN_DB_PREFIX.'facturedet WHERE fk_facture = '.$this->id; + $sql .= ' AND fk_product > 0'; $resql = $this->db->query($sql); if ($resql) @@ -830,10 +822,8 @@ class Facture while ($i < $num) { $obj = $this->db->fetch_object($resql); - - $sql = "UPDATE ".MAIN_DB_PREFIX."product SET nbvente=nbvente+1 WHERE rowid = ".$obj->fk_product; + $sql = 'UPDATE '.MAIN_DB_PREFIX.'product SET nbvente=nbvente+1 WHERE rowid = '.$obj->fk_product; $resql2 = $this->db->query($sql); - $i++; } } @@ -856,12 +846,12 @@ class Facture * Notify */ $facref = sanitize_string($this->ref); - $filepdf = FAC_OUTPUTDIR . "/" . $facref . "/" . $facref . ".pdf"; + $filepdf = FAC_OUTPUTDIR . '/' . $facref . '/' . $facref . '.pdf'; - $mesg = "La facture ".$this->ref." a été validée.\n"; + $mesg = 'La facture '.$this->ref." a été validée.\n"; $notify = New Notify($this->db); - $notify->send($action_notify, $this->socidp, $mesg, "facture", $rowid, $filepdf); + $notify->send($action_notify, $this->socidp, $mesg, 'facture', $rowid, $filepdf); return 1; } @@ -874,903 +864,840 @@ class Facture } } - /** - * \brief Ajoute un produit dans l'objet facture - * \param idproduct - * \param qty - * \param remise_percent - * \param datestart - * \param dateend - */ - function add_product($idproduct, $qty, $remise_percent, $datestart='', $dateend='') - { - if ($idproduct > 0) - { - $i = sizeof($this->products); // On recupere nb de produit deja dans tableau products - $this->products[$i] = $idproduct; // On ajoute a la suite - if (!$qty) - { - $qty = 1 ; - } - $this->products_qty[$i] = $qty; - $this->products_remise_percent[$i] = $remise_percent; - if ($datestart) { $this->products_date_start[$i] = $datestart; } - if ($dateend) { $this->products_date_end[$i] = $dateend; } - } - } + /** + * \brief Ajoute un produit dans l'objet facture + * \param idproduct + * \param qty + * \param remise_percent + * \param datestart + * \param dateend + */ + function add_product($idproduct, $qty, $remise_percent, $datestart='', $dateend='') + { + if ($idproduct > 0) + { + $i = sizeof($this->products); // On recupere nb de produit deja dans tableau products + $this->products[$i] = $idproduct; // On ajoute a la suite + if (!$qty) + { + $qty = 1 ; + } + $this->products_qty[$i] = $qty; + $this->products_remise_percent[$i] = $remise_percent; + if ($datestart) { $this->products_date_start[$i] = $datestart; } + if ($dateend) { $this->products_date_end[$i] = $dateend; } + } + } - /** - * \brief Ajoute une ligne de facture (associé à aucun produit/service prédéfini) - * \param facid id de la facture - * \param desc description de la ligne - * \param pu prix unitaire - * \param qty quantit - * \param txtva taux de tva - * \param fk_product id du produit/service predéfini - * \param remise_percent pourcentage de remise de la ligne - * \param datestart date de debut de validité du service - * \param dateend date de fin de validité du service - * \param ventil code de ventilation comptable - */ - function addline($facid, $desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0, $datestart='', $dateend='', $ventil = 0) - { - dolibarr_syslog("facture.class.php::addline($facid,$desc,$pu,$qty,$txtva,$fk_product,$remise_percent,$datestart,$dateend,$ventil)"); - if ($this->brouillon) - { - // Nettoyage paramètres - if (strlen(trim($qty))==0) $qty=1; - $remise = 0; - $_price = $pu; - $subprice = $pu; - - $remise_percent = trim($remise_percent); - - if ($this->socidp) - { - $soc = new Societe($this->db); - $soc->fetch($this->socidp); - $remise_client = $soc->remise_client; - if ($remise_client > $remise_percent) - { - $remise_percent = $remise_client ; - } - } - - if ($remise_percent > 0) - { - $remise = ($pu * $remise_percent / 100); - $_price = ($pu - $remise); - } - - // Lecture du rang max de la facture - $sql = "SELECT max(rang) FROM ".MAIN_DB_PREFIX."facturedet"; - $sql .= " WHERE fk_facture =".$facid; - $resql = $this->db->query($sql); - if ($resql) - { - $row = $this->db->fetch_row($resql); - $rangmax = $row[0]; - } - - // Formatage des prix - $_price = ereg_replace(",",".",$_price); - $subprice = ereg_replace(",",".",$subprice); - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet "; - $sql .= " (fk_facture,description,price,qty,tva_taux, fk_product, remise_percent, subprice, remise, date_start, date_end, fk_code_ventilation, rang)"; - $sql .= " VALUES ($facid, '".addslashes($desc)."','$_price','$qty','$txtva',$fk_product,'$remise_percent','$subprice','$remise', "; - - if ($datestart) { $sql.= "'$datestart', "; } - else { $sql.=" null, "; } - if ($dateend) { $sql.= "'$dateend' "; } - else { $sql.=" null "; } - - $sql .= ",".$ventil; - $sql .=",".($rangmax + 1).")"; - - if ( $this->db->query( $sql) ) - { - $this->updateprice($facid); - return 1; - } - else - { - dolibarr_print_error($this->db); - } - } - } + /** + * \brief Ajoute une ligne de facture (associé à aucun produit/service prédéfini) + * \param facid id de la facture + * \param desc description de la ligne + * \param pu prix unitaire + * \param qty quantit + * \param txtva taux de tva + * \param fk_product id du produit/service predéfini + * \param remise_percent pourcentage de remise de la ligne + * \param datestart date de debut de validité du service + * \param dateend date de fin de validité du service + * \param ventil code de ventilation comptable + */ + function addline($facid, $desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0, $datestart='', $dateend='', $ventil = 0) + { + dolibarr_syslog("facture.class.php::addline($facid,$desc,$pu,$qty,$txtva,$fk_product,$remise_percent,$datestart,$dateend,$ventil)"); + if ($this->brouillon) + { + // Nettoyage paramètres + if (strlen(trim($qty))==0) $qty=1; + $remise = 0; + $_price = $pu; + $subprice = $pu; - /** - * \brief Mets à jour une ligne de facture - * \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 datestart Date de debut de validité du service - * \param dateend Date de fin de validité du service - * \param tva_tx Taux TVA - * \return int < 0 si erreur, > 0 si ok - */ - function updateline($rowid, $desc, $pu, $qty, $remise_percent=0, $datestart, $dateend, $tva_tx) - { - dolibarr_syslog("Facture::UpdateLine"); - - if ($this->brouillon) - { - $this->db->begin(); - - if (strlen(trim($qty))==0) - { - $qty=1; - } - $remise = 0; - $price = ereg_replace(",",".",$pu); - $subprice = $price; - if (trim(strlen($remise_percent)) > 0) - { - $remise = round(($pu * $remise_percent / 100), 2); - $price = $pu - $remise; - } - else - { - $remise_percent=0; - } - - $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set description='".addslashes($desc)."'"; - $sql .= ",price='" . ereg_replace(",",".",$price)."'"; - $sql .= ",subprice='" . ereg_replace(",",".",$subprice)."'"; - $sql .= ",remise='". ereg_replace(",",".",$remise)."'"; - $sql .= ",remise_percent='".ereg_replace(",",".",$remise_percent)."'"; - $sql .= ",tva_taux='". ereg_replace(",",".",$tva_tx)."'"; - $sql .= ",qty='$qty'"; - - if ($datestart) { $sql.= ",date_start='$datestart'"; } - else { $sql.=",date_start=null"; } - if ($dateend) { $sql.= ",date_end='$dateend'"; } - else { $sql.=",date_end=null"; } - - $sql .= " WHERE rowid = $rowid ;"; - - $result = $this->db->query( $sql); - if ($result) - { - $this->updateprice($this->id); - - $this->db->commit(); - - return $result; - } - else - { - $this->db->rollback(); - - dolibarr_print_error($this->db); - return -1; - } - - } - else { - return -2; - } - } + $remise_percent = trim($remise_percent); - /** - * \brief Supprime une ligne facture de la base - * \param rowid id de la ligne de facture a supprimer - */ - function deleteline($rowid) - { - if ($this->brouillon) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet WHERE rowid = $rowid;"; - $result = $this->db->query( $sql); + if ($this->socidp) + { + $soc = new Societe($this->db); + $soc->fetch($this->socidp); + $remise_client = $soc->remise_client; + if ($remise_client > $remise_percent) + { + $remise_percent = $remise_client ; + } + } - $this->updateprice($this->id); - } - } + if ($remise_percent > 0) + { + $remise = ($pu * $remise_percent / 100); + $_price = ($pu - $remise); + } - /** - * \brief Mise à jour des sommes de la facture - * \param facid id de la facture a modifier - */ - function updateprice($facid) - { - include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php"; - $err=0; - $sql = "SELECT price, qty, tva_taux FROM ".MAIN_DB_PREFIX."facturedet WHERE fk_facture = $facid;"; + // Lecture du rang max de la facture + $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'facturedet'; + $sql .= ' WHERE fk_facture ='.$facid; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + $rangmax = $row[0]; + } - $result = $this->db->query($sql); + // Formatage des prix + $_price = ereg_replace(',','.',$_price); + $subprice = ereg_replace(',','.',$subprice); - if ($result) - { - $num = $this->db->num_rows($result); - $i = 0; - while ($i < $num) - { - $obj = $this->db->fetch_object($result); + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facturedet '; + $sql .= ' (fk_facture,description,price,qty,tva_taux, fk_product, remise_percent, subprice, remise, date_start, date_end, fk_code_ventilation, rang)'; + $sql .= " VALUES ($facid, '".addslashes($desc)."','$_price','$qty','$txtva',$fk_product,'$remise_percent','$subprice','$remise', "; + if ($datestart) { $sql.= "'$datestart', "; } + else { $sql.=' null, '; } + if ($dateend) { $sql.= "'$dateend' "; } + else { $sql.=' null '; } + $sql .= ','.$ventil; + $sql .=','.($rangmax + 1).')'; + if ( $this->db->query( $sql) ) + { + $this->updateprice($facid); + return 1; + } + else + { + dolibarr_print_error($this->db); + } + } + } - $products[$i][0] = $obj->price; - $products[$i][1] = $obj->qty; - $products[$i][2] = $obj->tva_taux; + /** + * \brief Mets à jour une ligne de facture + * \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 datestart Date de debut de validité du service + * \param dateend Date de fin de validité du service + * \param tva_tx Taux TVA + * \return int < 0 si erreur, > 0 si ok + */ + function updateline($rowid, $desc, $pu, $qty, $remise_percent=0, $datestart, $dateend, $tva_tx) + { + dolibarr_syslog('Facture::UpdateLine'); - $i++; - } + if ($this->brouillon) + { + $this->db->begin(); + if (strlen(trim($qty))==0) + { + $qty=1; + } + $remise = 0; + $price = ereg_replace(',','.',$pu); + $subprice = $price; + if (trim(strlen($remise_percent)) > 0) + { + $remise = round(($pu * $remise_percent / 100), 2); + $price = $pu - $remise; + } + else + { + $remise_percent=0; + } - $this->db->free($result); - /* - * - */ - $calculs = calcul_price($products, $this->remise_percent); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facturedet set description=\''.addslashes($desc).'\''; + $sql .= ",price='" . ereg_replace(',','.',$price)."'"; + $sql .= ",subprice='" . ereg_replace(',','.',$subprice)."'"; + $sql .= ",remise='". ereg_replace(',','.',$remise)."'"; + $sql .= ",remise_percent='".ereg_replace(',','.',$remise_percent)."'"; + $sql .= ",tva_taux='". ereg_replace(',','.',$tva_tx)."'"; + $sql .= ",qty='$qty'"; + if ($datestart) { $sql.= ",date_start='$datestart'"; } + else { $sql.=',date_start=null'; } + if ($dateend) { $sql.= ",date_end='$dateend'"; } + else { $sql.=',date_end=null'; } + $sql .= ' WHERE rowid = '.$rowid; + $result = $this->db->query( $sql); + if ($result) + { + $this->updateprice($this->id); + $this->db->commit(); + return $result; + } + else + { + $this->db->rollback(); + dolibarr_print_error($this->db); + return -1; + } + } + else + { + return -2; + } + } - $this->total_remise = $calculs[3]; - $this->amount_ht = $calculs[4]; - $this->total_ht = $calculs[0]; - $this->total_tva = $calculs[1]; - $this->total_ttc = $calculs[2]; - $tvas = $calculs[5]; + /** + * \brief Supprime une ligne facture de la base + * \param rowid id de la ligne de facture a supprimer + */ + function deleteline($rowid) + { + if ($this->brouillon) + { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facturedet WHERE rowid = '.$rowid; + $result = $this->db->query( $sql); + $this->updateprice($this->id); + } + } - /* - * - */ + /** + * \brief Mise à jour des sommes de la facture + * \param facid id de la facture a modifier + */ + function updateprice($facid) + { + include_once DOL_DOCUMENT_ROOT . '/lib/price.lib.php'; + $err=0; + $sql = 'SELECT price, qty, tva_taux FROM '.MAIN_DB_PREFIX.'facturedet WHERE fk_facture = '.$facid; + $result = $this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + $i = 0; + while ($i < $num) + { + $obj = $this->db->fetch_object($result); + $products[$i][0] = $obj->price; + $products[$i][1] = $obj->qty; + $products[$i][2] = $obj->tva_taux; + $i++; + } - $sql = "UPDATE ".MAIN_DB_PREFIX."facture "; - $sql .= "SET amount ='".ereg_replace(",",".",$this->amount_ht)."'"; - $sql .= ", remise='". ereg_replace(",",".",$this->total_remise)."'"; - $sql .= ", total='". ereg_replace(",",".",$this->total_ht)."'"; - $sql .= ", tva='". ereg_replace(",",".",$this->total_tva)."'"; - $sql .= ", total_ttc='".ereg_replace(",",".",$this->total_ttc)."'"; + $this->db->free($result); + /* + * + */ + $calculs = calcul_price($products, $this->remise_percent); + $this->total_remise = $calculs[3]; + $this->amount_ht = $calculs[4]; + $this->total_ht = $calculs[0]; + $this->total_tva = $calculs[1]; + $this->total_ttc = $calculs[2]; + $tvas = $calculs[5]; - $sql .= " WHERE rowid = $facid ;"; + /* + * + */ - if ( $this->db->query($sql) ) - { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture '; + $sql .= "SET amount ='".ereg_replace(',','.',$this->amount_ht)."'"; + $sql .= ", remise='". ereg_replace(',','.',$this->total_remise)."'"; + $sql .= ", total='". ereg_replace(',','.',$this->total_ht)."'"; + $sql .= ", tva='". ereg_replace(',','.',$this->total_tva)."'"; + $sql .= ", total_ttc='".ereg_replace(',','.',$this->total_ttc)."'"; + $sql .= ' WHERE rowid = '.$facid; + if ( $this->db->query($sql) ) + { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_tva_sum WHERE fk_facture='.$this->id; + if ( $this->db->query($sql) ) + { + foreach ($tvas as $key => $value) + { + $sql_del = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_tva_sum where fk_facture ='.$this->id; + $this->db->query($sql_del); + $sql = 'INSERT INTO '.MAIN_DB_PREFIX."facture_tva_sum (fk_facture,amount,tva_tx) values ($this->id,'".ereg_replace(',','.',$tvas[$key])."','".ereg_replace(',','.',$key)."');"; + // $sql = "REPLACE INTO ".MAIN_DB_PREFIX."facture_tva_sum SET fk_facture=".$this->id; + // $sql .= ", amount = '".$tvas[$key]."'"; + // $sql .= ", tva_tx='".$key."'"; + if (! $this->db->query($sql) ) + { + dolibarr_print_error($this->db); + $err++; + } + } + } + else + { + $err++; + } - $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_tva_sum WHERE fk_facture=".$this->id; + if ($err == 0) + { + return 1; + } + else + { + return -3; + } + } + else + { + dolibarr_print_error($this->db); + } + } + else + { + dolibarr_print_error($this->db); + } + } - if ( $this->db->query($sql) ) - { - foreach ($tvas as $key => $value) - { + /** + * \brief Applique une remise + * \param user + * \param remise + */ + function set_remise($user, $remise) + { + if ($user->rights->facture->creer) + { + $this->remise_percent = $remise ; + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture SET remise_percent = '.ereg_replace(',','.',$remise); + $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;'; - $sql_del = "DELETE FROM ".MAIN_DB_PREFIX."facture_tva_sum where fk_facture =$this->id;"; - $this->db->query($sql_del); - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_tva_sum (fk_facture,amount,tva_tx) values ($this->id,'".ereg_replace(",",".",$tvas[$key])."','".ereg_replace(",",".",$key)."');"; - - // $sql = "REPLACE INTO ".MAIN_DB_PREFIX."facture_tva_sum SET fk_facture=".$this->id; - // $sql .= ", amount = '".$tvas[$key]."'"; - // $sql .= ", tva_tx='".$key."'"; - - if (! $this->db->query($sql) ) - { - dolibarr_print_error($this->db); - $err++; - } - } - } - else - { - $err++; - } - - if ($err == 0) - { - return 1; - } - else - { - return -3; - } - } - else - { - dolibarr_print_error($this->db); - } - } - else - { - dolibarr_print_error($this->db); - } - } - - /** - * \brief Applique une remise - * \param user - * \param remise - */ - function set_remise($user, $remise) - { - if ($user->rights->facture->creer) - { - - $this->remise_percent = $remise ; - - $sql = "UPDATE ".MAIN_DB_PREFIX."facture SET remise_percent = ".ereg_replace(",",".",$remise); - $sql .= " WHERE rowid = $this->id AND fk_statut = 0 ;"; - - if ($this->db->query($sql) ) - { - $this->updateprice($this->id); - return 1; - } - else - { - dolibarr_print_error($this->db); - } - } - } + if ($this->db->query($sql) ) + { + $this->updateprice($this->id); + return 1; + } + else + { + dolibarr_print_error($this->db); + } + } + } - /** - * \brief Renvoie la liste des sommes de tva - */ - function getSumTva() - { - $sql = "SELECT amount, tva_tx FROM ".MAIN_DB_PREFIX."facture_tva_sum WHERE fk_facture = ".$this->id; - if ($this->db->query($sql)) - { - $num = $this->db->num_rows(); - $i = 0; - while ($i < $num) - { - $row = $this->db->fetch_row($i); - $tvs[$row[1]] = $row[0]; - $i++; - } + /** + * \brief Renvoie la liste des sommes de tva + */ + function getSumTva() + { + $sql = 'SELECT amount, tva_tx FROM '.MAIN_DB_PREFIX.'facture_tva_sum WHERE fk_facture = '.$this->id; + if ($this->db->query($sql)) + { + $num = $this->db->num_rows(); + $i = 0; + while ($i < $num) + { + $row = $this->db->fetch_row($i); + $tvs[$row[1]] = $row[0]; + $i++; + } + return $tvs; + } + else + { + return -1; + } + } - return $tvs; - } - else - { - return -1; - } - } + /** + * \brief Renvoie la sommes des paiements deja effectués + * \remarks Utilisé entre autre par certains modèles de factures + */ + function getSommePaiement() + { + $sql = 'SELECT sum(amount) FROM '.MAIN_DB_PREFIX.'paiement_facture WHERE fk_facture = '.$this->id; + if ($this->db->query($sql)) + { + $row = $this->db->fetch_row(0); + return $row[0]; + } + else + { + return -1; + } + } - /** - * \brief Renvoie la sommes des paiements deja effectués - * \remarks Utilisé entre autre par certains modèles de factures - */ - function getSommePaiement() - { - $sql = "SELECT sum(amount) FROM ".MAIN_DB_PREFIX."paiement_facture WHERE fk_facture = ".$this->id; - if ($this->db->query($sql)) - { - $row = $this->db->fetch_row(0); - return $row[0]; - } - else - { - return -1; - } - } + /** + * \brief Retourne le libellé du statut d'une facture (brouillon, validée, abandonnée, payée) + * \return string Libell + */ + function getLibStatut() + { + return $this->LibStatut($this->paye,$this->statut); + } - /** - * \brief Retourne le libellé du statut d'une facture (brouillon, validée, abandonnée, payée) - * \return string Libell - */ - function getLibStatut() - { - return $this->LibStatut($this->paye,$this->statut); - } + /** + * \brief Renvoi le libellé d'un statut donn + * \param paye Etat paye + * \param statut Id statut + * \param mode 0=libellé long, 1=libellé court + * \return string Libellé du statut + */ + function LibStatut($paye,$statut,$mode=0) + { + global $langs; + $langs->load('bills'); - /** - * \brief Renvoi le libellé d'un statut donn - * \param paye Etat paye - * \param statut Id statut - * \param mode 0=libellé long, 1=libellé court - * \return string Libellé du statut - */ - function LibStatut($paye,$statut,$mode=0) - { - global $langs; - $langs->load("bills"); - - $prefix=""; - if ($mode == 1) $prefix="Short"; - - if (! $paye) - { - if ($statut == 0) return $langs->trans("Bill${prefix}StatusDraft"); - if ($statut == 3) return $langs->trans("Bill${prefix}StatusCanceled"); - return $langs->trans("Bill${prefix}StatusValidated"); - } - else - { - return $langs->trans("Bill${prefix}StatusPayed"); - } - } + $prefix=''; + if ($mode == 1) + $prefix='Short'; + if (! $paye) + { + if ($statut == 0) return $langs->trans('Bill'.$prefix.'StatusDraft'); + if ($statut == 3) return $langs->trans('Bill'.$prefix.'StatusCanceled'); + return $langs->trans('Bill'.$prefix.'StatusValidated'); + } + else + { + return $langs->trans('Bill'.$prefix.'StatusPayed'); + } + } - /** - * \brief Renvoi le libellé court d'un statut donné - * \param paye etat paye - * \param statut id statut - * \param amount amount already payed - * \return string Libellé court du statut - */ - function PayedLibStatut($paye,$statut,$amount=0) - { - global $langs; - $langs->load("bills"); - if (! $paye) - { - if ($statut == 0) return $langs->trans("BillShortStatusDraft"); - if ($statut == 3) return $langs->trans("BillStatusCanceled"); - if ($amount) return $langs->trans("BillStatusStarted"); - return $langs->trans("BillStatusNotPayed"); - } - else - { - return $langs->trans("BillStatusPayed"); - } - } + /** + * \brief Renvoi le libellé court d'un statut donné + * \param paye etat paye + * \param statut id statut + * \param amount amount already payed + * \return string Libellé court du statut + */ + function PayedLibStatut($paye,$statut,$amount=0) + { + global $langs; + $langs->load('bills'); + if (! $paye) + { + if ($statut == 0) return $langs->trans('BillShortStatusDraft'); + if ($statut == 3) return $langs->trans('BillStatusCanceled'); + if ($amount) return $langs->trans('BillStatusStarted'); + return $langs->trans('BillStatusNotPayed'); + } + else + { + return $langs->trans('BillStatusPayed'); + } + } - /** - * \brief Mets à jour les commentaires - * \param note note - * \return int <0 si erreur, >0 si ok - */ - function update_note($note) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."facture SET note = '".addslashes($note)."'"; - $sql .= " WHERE rowid =". $this->id; + /** + * \brief Mets à jour les commentaires + * \param note note + * \return int <0 si erreur, >0 si ok + */ + function update_note($note) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX."facture SET note = '".addslashes($note)."'"; + $sql .= ' WHERE rowid ='. $this->id; - if ($this->db->query($sql) ) - { - $this->note = "$note"; - return 1; - } - else - { - return -1; - } - } + if ($this->db->query($sql) ) + { + $this->note = $note; + return 1; + } + else + { + return -1; + } + } - /** - * \brief Charge les informations d'ordre info dans l'objet facture - * \param id Id de la facture a charger - */ - function info($id) - { - $sql = "SELECT c.rowid, ".$this->db->pdate("datec")." as datec"; - $sql .= ", fk_user_author, fk_user_valid"; - $sql .= " FROM ".MAIN_DB_PREFIX."facture as c"; - $sql .= " WHERE c.rowid = ".$id; + /** + * \brief Charge les informations d'ordre info dans l'objet facture + * \param id Id de la facture a charger + */ + function info($id) + { + $sql = 'SELECT c.rowid, '.$this->db->pdate('datec').' as datec'; + $sql .= ', fk_user_author, fk_user_valid'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'facture as c'; + $sql .= ' WHERE c.rowid = '.$id; - $result=$this->db->query($sql); - if ($result) - { - if ($this->db->num_rows($result)) - { - $obj = $this->db->fetch_object($result); + $result=$this->db->query($sql); + if ($result) + { + if ($this->db->num_rows($result)) + { + $obj = $this->db->fetch_object($result); + $this->id = $obj->rowid; + if ($obj->fk_user_author) + { + $cuser = new User($this->db, $obj->fk_user_author); + $cuser->fetch(); + $this->user_creation = $cuser; + } + if ($obj->fk_user_valid) + { + $vuser = new User($this->db, $obj->fk_user_valid); + $vuser->fetch(); + $this->user_validation = $vuser; + } + $this->date_creation = $obj->datec; + //$this->date_validation = $obj->datev; \todo La date de validation n'est pas encore gérée + } + $this->db->free($result); + } + else + { + dolibarr_print_error($this->db); + } + } - $this->id = $obj->rowid; - - if ($obj->fk_user_author) { - $cuser = new User($this->db, $obj->fk_user_author); - $cuser->fetch(); - $this->user_creation = $cuser; - } - - if ($obj->fk_user_valid) { - $vuser = new User($this->db, $obj->fk_user_valid); - $vuser->fetch(); - $this->user_validation = $vuser; - } - - $this->date_creation = $obj->datec; - //$this->date_validation = $obj->datev; \todo La date de validation n'est pas encore gérée - - } - - $this->db->free($result); - - } - else - { - dolibarr_print_error($this->db); - } - } - - /** - * \brief Change les conditions de réglement de la facture - * \param cond_reglement_id Id de la nouvelle condition de réglement - * \return int >0 si ok, <0 si ko - */ - function cond_reglement($cond_reglement_id) - { - dolibarr_syslog("Facture::cond_reglement($cond_reglement_id)"); - if ($this->statut >= 0 && $this->paye == 0) - { - $datelim=$this->calculate_date_lim_reglement($cond_reglement_id); - - $sql = "UPDATE ".MAIN_DB_PREFIX."facture"; - $sql .= " SET fk_cond_reglement = ".$cond_reglement_id; - $sql .= ", date_lim_reglement=".$this->db->idate($datelim); - $sql .= " WHERE rowid=".$this->id; - - if ( $this->db->query($sql) ) - { - $this->cond_reglement_id = $cond_reglement_id; - return 1; - } - else - { - dolibarr_syslog("Facture::cond_reglement Erreur $sql - ".$this->db->error()); - $this->error=$this->db->error(); - return -1; - } - } - else - { - dolibarr_syslog("Facture::cond_reglement, etat facture incompatible"); - $this->error="Etat facture incompatible $this->statut $this->paye"; - return -2; - } - } + /** + * \brief Change les conditions de réglement de la facture + * \param cond_reglement_id Id de la nouvelle condition de réglement + * \return int >0 si ok, <0 si ko + */ + function cond_reglement($cond_reglement_id) + { + dolibarr_syslog('Facture::cond_reglement('.$cond_reglement_id.')'); + if ($this->statut >= 0 && $this->paye == 0) + { + $datelim=$this->calculate_date_lim_reglement($cond_reglement_id); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; + $sql .= ' SET fk_cond_reglement = '.$cond_reglement_id; + $sql .= ', date_lim_reglement='.$this->db->idate($datelim); + $sql .= ' WHERE rowid='.$this->id; + if ( $this->db->query($sql) ) + { + $this->cond_reglement_id = $cond_reglement_id; + return 1; + } + else + { + dolibarr_syslog('Facture::cond_reglement Erreur '.$sql.' - '.$this->db->error()); + $this->error=$this->db->error(); + return -1; + } + } + else + { + dolibarr_syslog('Facture::cond_reglement, etat facture incompatible'); + $this->error='Etat facture incompatible '.$this->statut.' '.$this->paye; + return -2; + } + } - /** - * \brief Change le mode de réglement - * \param mode Id du nouveau mode - * \return int >0 si ok, <0 si ko - */ - function mode_reglement($mode_reglement_id) - { - dolibarr_syslog("Facture::mode_reglement($mode_reglement_id)"); - if ($this->statut >= 0 && $this->paye == 0) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."facture"; - $sql .= " SET fk_mode_reglement = ".$mode_reglement_id; - $sql .= " WHERE rowid=".$this->id; + /** + * \brief Change le mode de réglement + * \param mode Id du nouveau mode + * \return int >0 si ok, <0 si ko + */ + function mode_reglement($mode_reglement_id) + { + dolibarr_syslog('Facture::mode_reglement('.$mode_reglement_id.')'); + if ($this->statut >= 0 && $this->paye == 0) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; + $sql .= ' SET fk_mode_reglement = '.$mode_reglement_id; + $sql .= ' WHERE rowid='.$this->id; + if ( $this->db->query($sql) ) + { + $this->mode_reglement_id = $mode_reglement_id; + return 1; + } + else + { + dolibarr_syslog('Facture::mode_reglement Erreur '.$sql.' - '.$this->db->error()); + $this->error=$this->db->error(); + return -1; + } + } + else + { + dolibarr_syslog('Facture::mode_reglement, etat facture incompatible'); + $this->error='Etat facture incompatible '.$this->statut.' '.$this->paye; + return -2; + } + } + - if ( $this->db->query($sql) ) - { - $this->mode_reglement_id = $mode_reglement_id; - return 1; - } - else - { - dolibarr_syslog("Facture::mode_reglement Erreur $sql - ".$this->db->error()); - $this->error=$this->db->error(); - return -1; - } - } - else - { - dolibarr_syslog("Facture::mode_reglement, etat facture incompatible"); - $this->error="Etat facture incompatible $this->statut $this->paye"; - return -2; - } - } - - /** - * \brief Créé une demande de prélèvement + * \brief Créé une demande de prélèvement * \param user Utilisateur créant la demande * \return int <0 si ko, >0 si ok - */ - function demande_prelevement($user) - { + */ + function demande_prelevement($user) + { dolibarr_syslog("Facture::demande_prelevement"); - $soc = new Societe($this->db); - $soc->id = $this->socidp; - $soc->rib(); - - - if ($this->statut > 0 && $this->paye == 0 && $this->mode_reglement_id == 3) - { - $sql = "SELECT count(*) FROM ".MAIN_DB_PREFIX."prelevement_facture_demande"; - $sql .= " WHERE fk_facture=".$this->id; - $sql .= " AND traite = 0"; - - if ( $this->db->query( $sql) ) - { - $row = $this->db->fetch_row(); - - if ($row[0] == 0) - { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_facture_demande"; - $sql .= " (fk_facture, amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib)"; - $sql .= " VALUES (".$this->id; - $sql .= ",'".ereg_replace(",",".",$this->total_ttc)."'"; - $sql .= ",now(),".$user->id.""; - $sql .= ",'".$soc->bank_account->code_banque."'"; - $sql .= ",'".$soc->bank_account->code_guichet."'"; - $sql .= ",'".$soc->bank_account->number."'"; - $sql .= ",'".$soc->bank_account->cle_rib."')"; - - if ( $this->db->query( $sql) ) - { + $soc = new Societe($this->db); + $soc->id = $this->socidp; + $soc->rib(); + if ($this->statut > 0 && $this->paye == 0 && $this->mode_reglement_id == 3) + { + $sql = 'SELECT count(*) FROM '.MAIN_DB_PREFIX.'prelevement_facture_demande'; + $sql .= ' WHERE fk_facture='.$this->id; + $sql .= ' AND traite = 0'; + if ( $this->db->query( $sql) ) + { + $row = $this->db->fetch_row(); + if ($row[0] == 0) + { + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'prelevement_facture_demande'; + $sql .= ' (fk_facture, amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib)'; + $sql .= ' VALUES ('.$this->id; + $sql .= ",'".ereg_replace(',','.',$this->total_ttc)."'"; + $sql .= ',now(),'.$user->id; + $sql .= ",'".$soc->bank_account->code_banque."'"; + $sql .= ",'".$soc->bank_account->code_guichet."'"; + $sql .= ",'".$soc->bank_account->number."'"; + $sql .= ",'".$soc->bank_account->cle_rib."')"; + if ( $this->db->query( $sql) ) + { return 1; - } - else - { + } + else + { $this->error=$this->db->error(); - dolibarr_syslog("Facture::DemandePrelevement Erreur"); - return -1; - } - } - else - { + dolibarr_syslog('Facture::DemandePrelevement Erreur'); + return -1; + } + } + else + { $this->error="Une demande existe déjà"; - dolibarr_syslog("Facture::DemandePrelevement Impossible de créer une demande, demande déja en cours"); - } - } - else - { + dolibarr_syslog('Facture::DemandePrelevement Impossible de créer une demande, demande déja en cours'); + } + } + else + { $this->error=$this->db->error(); - dolibarr_syslog("Facture::DemandePrelevement Erreur -2"); - return -2; - } - } - else - { + dolibarr_syslog('Facture::DemandePrelevement Erreur -2'); + return -2; + } + } + else + { $this->error="Etat facture incompatible avec l'action"; dolibarr_syslog("Facture::DemandePrelevement Etat facture incompatible $this->statut, $this->paye, $this->mode_reglement_id"); - return -3; - } - } - /** - * \brief Supprime une demande de prélèvement - * \param user utilisateur créant la demande - * \param did id de la demande a supprimer - */ - function demande_prelevement_delete($user, $did) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."prelevement_facture_demande"; - $sql .= " WHERE rowid = ".$did; - $sql .= " AND traite = 0"; + return -3; + } + } - if ( $this->db->query( $sql) ) - { - return 0; - } - else - { - dolibarr_syslog("Facture::DemandePrelevement Erreur"); - return -1; - } - } + /** + * \brief Supprime une demande de prélèvement + * \param user utilisateur créant la demande + * \param did id de la demande a supprimer + */ + function demande_prelevement_delete($user, $did) + { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'prelevement_facture_demande'; + $sql .= ' WHERE rowid = '.$did; + $sql .= ' AND traite = 0'; + if ( $this->db->query( $sql) ) + { + return 0; + } + else + { + dolibarr_syslog('Facture::DemandePrelevement Erreur'); + return -1; + } + } - /** - * \brief Stocke un numéro de rand pour toutes les lignes de - * detail d'une facture qui n'en ont pas. - */ - function line_order() - { - $sql = "SELECT count(rowid) FROM ".MAIN_DB_PREFIX."facturedet"; - $sql .= " WHERE fk_facture=".$this->id; - $sql .= " AND rang = 0"; + /** + * \brief Stocke un numéro de rand pour toutes les lignes de + * detail d'une facture qui n'en ont pas. + */ + function line_order() + { + $sql = 'SELECT count(rowid) FROM '.MAIN_DB_PREFIX.'facturedet'; + $sql .= ' WHERE fk_facture='.$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.'facturedet'; + $sql .= ' WHERE fk_facture='.$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.'facturedet SET rang = '.($i+1); + $sql .= ' WHERE rowid = '.$li[$i]; + if (!$this->db->query($sql) ) + { + dolibarr_syslog($this->db->error()); + } + } + } + } - $resql = $this->db->query($sql); + function line_up($rowid) + { + $this->line_order(); - if ($resql) - { - $row = $this->db->fetch_row($resql); - $nl = $row[0]; - } + /* Lecture du rang de la ligne */ + $sql = 'SELECT rang FROM '.MAIN_DB_PREFIX.'facturedet'; + $sql .= ' WHERE rowid ='.$rowid; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + $rang = $row[0]; + } - if ($nl > 0) - { - $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facturedet"; - $sql .= " WHERE fk_facture=".$this->id; - $sql .= " ORDER BY rang ASC, rowid ASC"; + if ($rang > 1 ) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facturedet SET rang = '.$rang ; + $sql .= ' WHERE fk_facture = '.$this->id; + $sql .= ' AND rang = '.($rang - 1); + if ($this->db->query($sql) ) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facturedet SET rang = '.($rang - 1); + $sql .= ' WHERE rowid = '.$rowid; + if (! $this->db->query($sql) ) + { + dolibarr_print_error($this->db); + } + } + else + { + dolibarr_print_error($this->db); + } + } + } - $resql = $this->db->query($sql); + function line_down($rowid) + { + $this->line_order(); - if ($resql) - { - $num = $this->db->num_rows($resql); - $i = 0; + /* Lecture du rang de la ligne */ + $sql = 'SELECT rang FROM '.MAIN_DB_PREFIX.'facturedet'; + $sql .= ' WHERE rowid ='.$rowid; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + $rang = $row[0]; + } - while ($i < $num) - { - $row = $this->db->fetch_row($resql); - $li[$i] = $row[0]; - $i++; - } - } + /* Lecture du rang max de la facture */ + $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'facturedet'; + $sql .= ' WHERE fk_facture ='.$this->id; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + $max = $row[0]; + } - for ($i = 0 ; $i < sizeof($li) ; $i++) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet SET rang = ".($i+1); - $sql .= " WHERE rowid = ".$li[$i]; + if ($rang < $max ) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facturedet SET rang = '.$rang; + $sql .= ' WHERE fk_facture = '.$this->id; + $sql .= ' AND rang = '.($rang+1); + if ($this->db->query($sql) ) + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'facturedet SET rang = '.($rang+1); + $sql .= ' WHERE rowid = '.$rowid; + if (! $this->db->query($sql) ) + { + dolibarr_print_error($this->db); + } + } + else + { + dolibarr_print_error($this->db); + } + } + } - if (!$this->db->query($sql) ) - { - dolibarr_syslog($this->db->error()); - } - } - } - } + /** + * \brief Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord + * \param user Objet user + * \return int <0 si ko, >0 si ok + */ + function load_board($user) + { + global $conf; - function line_up($rowid) - { - $this->line_order(); - - /* Lecture du rang de la ligne */ - - $sql = "SELECT rang FROM ".MAIN_DB_PREFIX."facturedet"; - $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."facturedet SET rang = ".$rang ; - $sql .= " WHERE fk_facture = ".$this->id; - $sql .= " AND rang = ".($rang - 1); - - if ($this->db->query($sql) ) - { - - $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet 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."facturedet"; - $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."facturedet"; - $sql .= " WHERE fk_facture =".$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."facturedet SET rang = ".$rang; - $sql .= " WHERE fk_facture = ".$this->id; - $sql .= " AND rang = ".($rang+1); - - if ($this->db->query($sql) ) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet 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 Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord - * \param user Objet user - * \return int <0 si ko, >0 si ok - */ - function load_board($user) - { - global $conf; - - $this->nbtodo=$this->nbtodolate=0; - $sql = "SELECT f.rowid,".$this->db->pdate("f.date_lim_reglement")." as datefin"; - $sql.= " FROM ".MAIN_DB_PREFIX."facture as f"; - $sql.= " WHERE f.paye=0 AND f.fk_statut = 1"; - if ($user->societe_id) $sql.=" AND fk_soc = ".$user->societe_id; - $resql=$this->db->query($sql); - if ($resql) - { - while ($obj=$this->db->fetch_object($resql)) - { - $this->nbtodo++; - if ($obj->datefin < (time() - $conf->facture->client->warning_delay)) $this->nbtodolate++; - } - return 1; - } - else - { - dolibarr_print_error($this->db); - $this->error=$this->db->error(); - return -1; - } - } + $this->nbtodo=$this->nbtodolate=0; + $sql = 'SELECT f.rowid,'.$this->db->pdate('f.date_lim_reglement').' as datefin'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'facture as f'; + $sql.= ' WHERE f.paye=0 AND f.fk_statut = 1'; + if ($user->societe_id) $sql.=' AND fk_soc = '.$user->societe_id; + $resql=$this->db->query($sql); + if ($resql) + { + while ($obj=$this->db->fetch_object($resql)) + { + $this->nbtodo++; + if ($obj->datefin < (time() - $conf->facture->client->warning_delay)) $this->nbtodolate++; + } + return 1; + } + else + { + dolibarr_print_error($this->db); + $this->error=$this->db->error(); + return -1; + } + } } /** - \class FactureLigne - \brief Classe permettant la gestion des lignes de factures + \class FactureLigne + \brief Classe permettant la gestion des lignes de factures */ class FactureLigne { - var $subprice; // Prix unitaire HT - var $price; // Prix HT apres remise % + var $subprice; // Prix unitaire HT + var $price; // Prix HT apres remise % - /** - * \brief Constructeur d'objets ligne de facture - * \param DB handler d'accès base de donnée - */ - function FactureLigne($DB) - { - $this->db= $DB ; - } + /** + * \brief Constructeur d'objets ligne de facture + * \param DB handler d'accès base de donnée + */ + function FactureLigne($DB) + { + $this->db= $DB ; + } - /** - * \brief Recupére l'objet ligne de facture - * \param rowid id de la ligne de facture - * \param societe_id id de la societe - */ - function fetch($rowid, $societe_id=0) - { - $sql = "SELECT fk_product, description, price, qty, rowid, tva_taux, remise, remise_percent,"; - $sql.= " subprice, ".$this->db->pdate("date_start")." as date_start,".$this->db->pdate("date_end")." as date_end"; - $sql.= " FROM ".MAIN_DB_PREFIX."facturedet WHERE rowid = ".$rowid; - - $result = $this->db->query($sql); - if ($result) - { - $objp = $this->db->fetch_object($result); - - $this->desc = stripslashes($objp->description); - $this->qty = $objp->qty; - $this->price = $objp->price; - $this->price_ttc = $objp->price_ttc; - $this->subprice = $objp->subprice; - $this->tva_taux = $objp->tva_taux; - $this->remise = $objp->remise; - $this->remise_percent = $objp->remise_percent; - $this->produit_id = $objp->fk_product; - $this->date_start = $objp->date_start; - $this->date_end = $objp->date_end; - $i++; - - $this->db->free($result); - } - else { - dolibarr_print_error($this->db); - } - - } + /** + * \brief Recupére l'objet ligne de facture + * \param rowid id de la ligne de facture + * \param societe_id id de la societe + */ + function fetch($rowid, $societe_id=0) + { + $sql = 'SELECT fk_product, description, price, qty, rowid, tva_taux, remise, remise_percent,'; + $sql.= ' subprice, '.$this->db->pdate('date_start').' as date_start,'.$this->db->pdate('date_end').' as date_end'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet WHERE rowid = '.$rowid; + $result = $this->db->query($sql); + if ($result) + { + $objp = $this->db->fetch_object($result); + $this->desc = stripslashes($objp->description); + $this->qty = $objp->qty; + $this->price = $objp->price; + $this->price_ttc = $objp->price_ttc; + $this->subprice = $objp->subprice; + $this->tva_taux = $objp->tva_taux; + $this->remise = $objp->remise; + $this->remise_percent = $objp->remise_percent; + $this->produit_id = $objp->fk_product; + $this->date_start = $objp->date_start; + $this->date_end = $objp->date_end; + $i++; + $this->db->free($result); + } + else + { + dolibarr_print_error($this->db); + } + } } diff --git a/mysql/migration/1.1.0-2.0.0.sql b/mysql/migration/1.1.0-2.0.0.sql index 6cfceee4af6..eda75c503ad 100644 --- a/mysql/migration/1.1.0-2.0.0.sql +++ b/mysql/migration/1.1.0-2.0.0.sql @@ -1833,3 +1833,5 @@ insert into llx_c_type_contact(rowid, code, element, libelle, active ) values (2 insert into llx_c_type_contact(rowid, code, element, libelle, active ) values (80, 'PROJECTLEADER', 'projet', 'Chef de Projet', 1); +alter table llx_commande add ref_client varchar(30); +alter table llx_facture add ref_client varchar(30);