diff --git a/htdocs/commande/commande.class.php b/htdocs/commande/commande.class.php index 8165130adb3..f120e5ddc4b 100644 --- a/htdocs/commande/commande.class.php +++ b/htdocs/commande/commande.class.php @@ -1033,11 +1033,11 @@ class Commande } /** - * \brief Renvoie un tableau avec les livraison par ligne + * \brief Renvoie un tableau avec les expéditions par ligne * \param filtre_statut Filtre sur statut * \return int 0 si OK, <0 si KO */ - function livraison_array($filtre_statut=-1) + function expedition_array($filtre_statut=-1) { $this->livraisons = array(); $sql = 'SELECT fk_product, sum(ed.qty)'; @@ -1048,6 +1048,54 @@ class Commande $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->expeditions[$row[0]] = $row[1]; + $i++; + } + $this->db->free(); + } + + return 0; + } + + /** + * Renvoie un tableau avec les expeditions 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 Renvoie un tableau avec les livraisons par ligne + * \param filtre_statut Filtre sur statut + * \return int 0 si OK, <0 si KO + */ + function livraison_array($filtre_statut=-1) + { + $this->livraisons = array(); + $sql = 'SELECT cd.fk_product, sum(ed.qty)'; + $sql.=' FROM '.MAIN_DB_PREFIX.'livraisondet as ld, '.MAIN_DB_PREFIX.'livraison as l, '.MAIN_DB_PREFIX.'commande as c, '.MAIN_DB_PREFIX.'commandedet as cd'; + $sql.=' WHERE ld.fk_livraison = l.rowid AND ld.fk_commande_ligne = cd .rowid AND cd.fk_commande = c.rowid'; + $sql.=' AND cd.fk_commande =' .$this->id; + if ($filtre_statut >= 0) $sql.=' AND l.fk_statut = '.$filtre_statut; + $sql .= ' GROUP BY cd.fk_product '; + $result = $this->db->query($sql); + if ($result) { $num = $this->db->num_rows(); $i = 0; @@ -1063,22 +1111,6 @@ class Commande return 0; } - /** - * 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 diff --git a/htdocs/expedition/fiche.php b/htdocs/expedition/fiche.php index bdb047931a8..9285f220f70 100644 --- a/htdocs/expedition/fiche.php +++ b/htdocs/expedition/fiche.php @@ -103,7 +103,7 @@ if ($_POST["action"] == 'confirm_valid' && $_POST["confirm"] == 'yes' && $user-> $expedition = new Expedition($db); $expedition->fetch($_GET["id"]); $result = $expedition->valid($user); - $expedition->PdfWrite(); + //$expedition->PdfWrite(); } if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == 'yes') @@ -156,7 +156,7 @@ if ($_GET["action"] == 'create') } $commande = new Commande($db); - $commande->livraison_array(); + $commande->expedition_array(); if ( $commande->fetch($_GET["commande_id"])) { @@ -209,8 +209,8 @@ if ($_GET["action"] == 'create') $lignes = $commande->fetch_lignes(1); - /* Lecture des livraisons déjà effectuées */ - $commande->livraison_array(); + /* Lecture des expeditions déjà effectuées */ + $commande->expedition_array(); $num = sizeof($commande->lignes); $i = 0; @@ -341,6 +341,13 @@ else $head[$h][1] = $langs->trans("SendingCard"); $hselected = $h; $h++; + + if ($conf->livraison->enable) + { + $head[$h][0] = DOL_URL_ROOT."/livraison/fiche.php?id=".$livraison->id; + $head[$h][1] = $langs->trans("DeliveryCard"); + $h++; + } dolibarr_fiche_head($head, $hselected, $langs->trans("Sending")); diff --git a/htdocs/livraison/fiche.php b/htdocs/livraison/fiche.php new file mode 100644 index 00000000000..a472faff6c2 --- /dev/null +++ b/htdocs/livraison/fiche.php @@ -0,0 +1,713 @@ + + * Copyright (C) 2005-2006 Laurent Destailleur + * Copyright (C) 2005 Simon TOSSER + * + * 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/livraison/fiche.php + \ingroup livraison + \brief Fiche descriptive d'un bon de livraison + \version $Revision$ +*/ + +require("./pre.inc.php"); +require_once(DOL_DOCUMENT_ROOT."/product.class.php"); + + +$langs->load("bills"); + +$user->getrights('expedition'); +if (!$user->rights->expedition->livraison->lire) + accessforbidden(); + + +// Sécurité accés client +if ($user->societe_id > 0) +{ + $action = ''; + $socidp = $user->societe_id; +} + + +/* + * Actions + */ + +if ($_POST["action"] == 'add') +{ + $db->begin(); + + // Creation de l'objet livraison + $livraison = new Livraison($db); + + $livraison->date_livraison = time(); + $livraison->note = $_POST["note"]; + $livraison->commande_id = $_POST["commande_id"]; + + // On boucle sur chaque ligne de commande pour compléter objet livraison + // avec qté à livrer + $commande = new Commande($db); + $commande->fetch($livraison->commande_id); + $commande->fetch_lignes(); + for ($i = 0 ; $i < sizeof($commande->lignes) ; $i++) + { + $qty = "qtyl".$i; + $idl = "idl".$i; + if ($_POST[$qty] > 0) + { + $livraison->addline($_POST[$idl],$_POST[$qty]); + } + } + + $ret=$livraison->create($user); + if ($ret > 0) + { + $db->commit(); + Header("Location: fiche.php?id=".$livraison->id); + exit; + } + else + { + $db->rollback(); + $mesg='
'.$livraison->error.'
'; + $_GET["commande_id"]=$_POST["commande_id"]; + $_GET["action"]='create'; + } +} + +if ($_POST["action"] == 'confirm_valid' && $_POST["confirm"] == 'yes' && $user->rights->expedition->livraison->valider) +{ + $livraison = new Livraison($db); + $livraison->fetch($_GET["id"]); + $result = $livraison->valid($user); + $livraison->PdfWrite(); +} + +if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == 'yes') +{ + if ($user->rights->expedition->livraison->supprimer ) + { + $livraison = new Livraison($db); + $livraison->id = $_GET["id"]; + $livraison->delete(); + Header("Location: liste.php"); + } +} + +/* + * Générer ou regénérer le PDF + */ +if ($_REQUEST['action'] == 'builddoc') // En get ou en post +{ + $outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs"); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + $result=livraison_pdf_create($db, $_REQUEST['id'],$_REQUEST['model'],$outputlangs); + if ($result <= 0) + { + dolibarr_print_error($db,$result); + exit; + } +} + + +/* + * + */ + +$html = new Form($db); + +/********************************************************************* + * + * Mode creation + * + *********************************************************************/ +if ($_GET["action"] == 'create') +{ + llxHeader('','Fiche expedition','ch-expedition.html',$form_search); + + print_titre($langs->trans("CreateADeliveryOrder")); + + if ($mesg) + { + print $mesg.'
'; + } + + $commande = new Commande($db); + $commande->livraison_array(); + + if ( $commande->fetch($_GET["commande_id"])) + { + $soc = new Societe($db); + $soc->fetch($commande->soc_id); + $author = new User($db); + $author->id = $commande->user_author_id; + $author->fetch(); + + //$entrepot = new Entrepot($db); + + /* + * Commande + */ + print '
'; + print ''; + print ''; + //print ''; + print ''; + print ''; + print ''; + + print '"; + + print ""; + print "\n"; + + print '\n"; + + print ''; + /* + print ''; + print ''; + */ + print "\n"; + + if ($commande->note) + { + print '"; + } + print "
'.$langs->trans("Customer").''.$soc->nom.''; + + print "
".$langs->trans("Date")."".strftime("%A %d %B %Y",$commande->date)."'.$langs->trans("Order").''.img_object($langs->trans("ShowOrder"),'order').' '.$commande->ref.''; + print "
'.$langs->trans("Warehouse").''; + $ents = $entrepot->list_array(); + print ''.img_object($langs->trans("ShowWarehouse"),'stock').' '.$ents[$_GET["entrepot_id"]].''; + print '".$langs->trans("Author")."$author->fullname
Note : '.nl2br($commande->note)."
"; + + /* + * Lignes de commandes + * + */ + echo '
'; + + $lignes = $commande->fetch_lignes(1); + + /* Lecture des livraisons déjà effectuées */ + $commande->livraison_array(); + + $num = sizeof($commande->lignes); + $i = 0; + + if ($num) + { + print ''; + print ''; + print ''; + print ''; + print ''; + if ($conf->stock->enabled) + { + print ''; + } + print "\n"; + } + $var=true; + while ($i < $num) + { + $ligne = $commande->lignes[$i]; + $var=!$var; + print "\n"; + if ($ligne->product_id > 0) + { + $product = new Product($db); + $product->fetch($ligne->product_id); + + print ''; + } + else + { + print "\n"; + } + + print ''; + /* + * + */ + print ''; + + $quantite_commandee = $ligne->qty; + $quantite_a_livrer = $quantite_commandee - $quantite_livree; + + if ($conf->stock->enabled) + { + $stock = $product->stock_entrepot[$_GET["entrepot_id"]]; + $stock+=0; // Convertit en numérique + + // Quantité à livrer + print ''; + + // Stock + if ($stock < $quantite_a_livrer) + { + print ''; + } + else + { + print ''; + } + } + else + { + // Quantité à livrer + print ''; + } + + print "\n"; + + $i++; + $var=!$var; + } + + /* + * + */ + + print ''; + print "
'.$langs->trans("Description").'Quan. commandéeQuan. livréeQuan. à livrer'.$langs->trans("Stock").'
'; + print ''.img_object($langs->trans("ShowProduct"),"product").' '.$product->ref.' - '.$product->libelle; + if ($ligne->description) print nl2br($ligne->description); + print '".nl2br($ligne->description)."'.$ligne->qty.''; + $quantite_livree = $commande->livraisons[$ligne->product_id]; + print $quantite_livree;; + print ''; + print ''; + print ''; + print ''.$stock.''.$stock.''; + print ''; + print ''; + print '

"; + print '
'; + } + else + { + dolibarr_print_error($db); + } +} +else +/* *************************************************************************** */ +/* */ +/* Mode vue et edition */ +/* */ +/* *************************************************************************** */ +{ + if ($_GET["id"] > 0) + { + $livraison = New Livraison($db); + $result = $livraison->fetch($_GET["id"]); + + if ( $livraison->id > 0) + { + $author = new User($db); + $author->id = $livraison->user_author_id; + $author->fetch(); + + llxHeader('','Fiche expedition','ch-expedition.html',$form_search,$author); + + $commande = New Commande($db); + $commande->fetch($livraison->commande_id); + + $soc = new Societe($db); + $soc->fetch($commande->soc_id); + + $h=0; + if ($conf->expedition->enable) + { + $head[$h][0] = DOL_URL_ROOT."/expedition/fiche.php?id=".$expedition->id; + $head[$h][1] = $langs->trans("SendingCard"); + $h++; + } + + $head[$h][0] = DOL_URL_ROOT."/livraison/fiche.php?id=".$livraison->id; + $head[$h][1] = $langs->trans("DeliveryCard"); + $hselected = $h; + $h++; + + dolibarr_fiche_head($head, $hselected, $langs->trans("Sending")); + + /* + * Confirmation de la suppression + * + */ + if ($_GET["action"] == 'delete') + { + $html->form_confirm("fiche.php?id=$livraison->id","Supprimer le bon de livraison","Etes-vous sûr de vouloir supprimer ce bon de livraison ?","confirm_delete"); + print '
'; + } + + /* + * Confirmation de la validation + * + */ + if ($_GET["action"] == 'valid') + { + $html->form_confirm("fiche.php?id=$livraison->id","Valider le bon de livraison","Etes-vous sûr de vouloir valider ce bon de livraison ?","confirm_valid"); + print '
'; + } + + + /* + * Commande + */ + if ($commande->brouillon == 1 && $user->rights->commande->creer) + { + print '
'; + } + + print ''; + + // Ref + print ''; + print ''; + + // Client + print ''; + print ''; + + // Auteur + print ''; + + print ""; + + // Commande liée + print ''; + print '\n"; + print ''; + + // Date + print ''; + print "\n"; +/* + // Entrepot + $entrepot = new Entrepot($db); + $entrepot->fetch($expedition->entrepot_id); + print ''; +*/ + print ''; + + print "
'.$langs->trans("Ref").''.$livraison->ref.'
'.$langs->trans("Customer").''; + print ''.$soc->nom.''.$langs->trans("Author").''.$author->fullname.'
'.$langs->trans("Order").''.$commande->ref."  
'.$langs->trans("Date").'".strftime("%A %d %B %Y",$livraison->date)."'.$langs->trans("Warehouse").''.$entrepot->libelle.'
\n"; + + /* + * Lignes produits + */ + echo '
'; + + $sql = "SELECT cd.fk_product, cd.description, cd.rowid, cd.qty as qty_commande"; + $sql .= " , ld.qty as qty_livre"; + $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd , ".MAIN_DB_PREFIX."livraisondet as ld"; + $sql .= " WHERE ld.fk_livraison = $livraison->id AND cd.rowid = ld.fk_commande_ligne "; + + $resql = $db->query($sql); + + if ($resql) + { + $num_prod = $db->num_rows($resql); + $i = 0; + + print ''; + print ''; + print ''; + print ''; + print "\n"; + + $var=true; + while ($i < $num_prod) + { + $objp = $db->fetch_object($resql); + + $var=!$var; + print ""; + if ($objp->fk_product > 0) + { + $product = new Product($db); + $product->fetch($objp->fk_product); + + print ''; + } + else + { + print "\n"; + } + print ''; + print ''; + + print ""; + + $i++; + $var=!$var; + } + $db->free($resql); + } + else + { + dolibarr_print_error($db); + } + + print "
'.$langs->trans("Products").''.$langs->trans("QtyOrdered").''.$langs->trans("QtyShipped").'
'; + print ''.img_object($langs->trans("ShowProduct"),"product").' '.$product->ref.' - '.$product->libelle; + if ($objp->description) print nl2br($objp->description); + print '".stripslashes(nl2br($objp->description))."'.$objp->qty_commande.''.$objp->qty_livre.'
\n"; + + print "\n\n"; + + + /* + * Boutons actions + */ + + if ($user->societe_id == 0) + { + print '
'; + + if ($livraison->statut == 0 && $user->rights->expedition->livraison->valider && $num_prod > 0) + { + print ''.$langs->trans("Validate").''; + } + + print ''.$langs->trans('BuildPDF').''; + + if ($livraison->brouillon && $user->rights->expedition->livraison->supprimer) + { + print ''.$langs->trans("Delete").''; + } + + print '
'; + } + print "\n"; + + print "
"; + + /* + * Documents générés + */ + + //mis en commentaire pour test du patch + //$filename=sanitize_string($expedition->id); + //$filedir=$conf->expedition->dir_output . "/" .get_exdir($expedition->id); + + $filename=sanitize_string($livraison->ref); + $filedir=$conf->livraison->dir_output . "/" .$livraison->ref; + + $urlsource=$_SERVER["PHP_SELF"]."?id=".$livraison->id; + + //$genallowed=$user->rights->expedition->creer; + //$delallowed=$user->rights->expedition->supprimer; + $genallowed=1; + $delallowed=0; + + $result=$html->show_documents('livraison',$filename,$filedir,$urlsource,$genallowed,$delallowed,$livraison->modelpdf); + + /* + * Déjà livre + */ + $sql = "SELECT cd.fk_product, cd.description, cd.rowid, cd.qty as qty_commande"; + $sql .= " , ld.qty as qty_livre, l.ref, ld.fk_livraison as livraison_id"; + $sql .= ",".$db->pdate("l.date_livraison")." as date_livraison"; + $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd"; + $sql .= " , ".MAIN_DB_PREFIX."livraisondet as ld, ".MAIN_DB_PREFIX."livraison as l"; + $sql .= " WHERE cd.fk_commande = ".$livraison->commande_id; + $sql .= " AND l.rowid <> ".$livraison->id; + $sql .= " AND cd.rowid = ld.fk_commande_ligne"; + $sql .= " AND ld.fk_livraison = l.rowid"; + $sql .= " AND l.fk_statut > 0"; + $sql .= " ORDER BY cd.fk_product"; + + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + $i = 0; + + if ($num) + { + print '
'; + + print_titre($langs->trans("OtherSendingsForSameOrder")); + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print "\n"; + + $var=True; + while ($i < $num) + { + $var=!$var; + $objp = $db->fetch_object($resql); + print ""; + print ''; + if ($objp->fk_product > 0) + { + $product = new Product($db); + $product->fetch($objp->fk_product); + + print ''; + } + else + { + print "\n"; + } + print ''; + print ''; + print ''; + $i++; + } + + print '
'.$langs->trans("Sending").''.$langs->trans("Description").''.$langs->trans("QtyShipped").''.$langs->trans("Date").'
'.img_object($langs->trans("ShowSending"),'sending').' '.$objp->ref.''; + print ''.img_object($langs->trans("ShowProduct"),"product").' '.$product->ref.' - '.$product->libelle; + if ($objp->description) print nl2br($objp->description); + print '".stripslashes(nl2br($objp->description))."'.$objp->qty_livre.''.dolibarr_print_date($objp->date_livraison).'
'; + } + $db->free($resql); + } + else { + dolibarr_print_error($db); + } + + /* + * Commandes associées + * + */ + $file = $conf->commande->dir_output . "/" . $commande->ref . "/" . $commande->ref . ".pdf"; + $relativepath = $commande->ref . "/" . $commande->ref . ".pdf"; + + $var=true; + + if (file_exists($file)) + { + print '
'; + print_titre("Orders"); + 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 '
'; + + + print_titre("Actions"); + /* + * Liste des actions + * + */ + $sql = "SELECT ".$db->pdate("a.datea")." as da, a.note"; + $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a"; + $sql .= " WHERE a.fk_soc = ".$commande->soc_id." AND a.fk_action in (9,10)"; + $sql .= " AND a.fk_commande = ".$livraison->id; + + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + if ($num) + { + $i = 0; + print ''; + print "\n"; + + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print ""; + print "\n"; + print ''; + print ""; + $i++; + } + print "
".$langs->trans("Date")."".$langs->trans("Action")."
".strftime("%d %B %Y",$objp->da)."'.stripslashes($objp->note).'
"; + } + $db->free($resql); + } + else + { + dolibarr_print_error($db); + } + + if ($action == 'presend') + { + $replytoname = $user->fullname; + $from_name = $replytoname; + + $replytomail = $user->email; + $from_mail = $replytomail; + + print "id&action=send\">\n"; + print ''; + print ''; + + print "

Envoyer le bon de livraison par mail"; + print ""; + print '"; + print ""; + print ""; + print "
Destinataire'; + + $form = new Form($db); + $form->select_array("destinataire",$soc->contact_email_array()); + + print "email\">
Expéditeur$from_name$from_mail
Reply-to$replytoname$replytomail
"; + + print ""; + } + + print '

'; + + } + else + { + /* Expedition non trouvée */ + llxHeader('','Fiche expedition','ch-expedition.html',$form_search); + print "Expedition inexistante ou accés refusé"; + } + } + else + { + /* Expedition non trouvée */ + llxHeader('','Fiche expedition','ch-expedition.html',$form_search); + print "Expedition inexistante ou accés refusé"; + } +} + +$db->close(); + +llxFooter('$Date$ - $Revision$'); +?> diff --git a/htdocs/livraison/livraison.class.php b/htdocs/livraison/livraison.class.php index 8a90cab904e..9836766ba32 100644 --- a/htdocs/livraison/livraison.class.php +++ b/htdocs/livraison/livraison.class.php @@ -58,13 +58,15 @@ class Livraison /** * \brief Créé bon de livraison en base - * \param user Objet du user qui cré + * \param user Objet du user qui crée * \return int <0 si erreur, id livraison créée si ok */ function create($user) { + global $conf; require_once DOL_DOCUMENT_ROOT ."/product/stock/mouvementstock.class.php"; $error = 0; + /* On positionne en mode brouillon le bon de livraison */ $this->brouillon = 1; @@ -72,25 +74,36 @@ class Livraison $this->db->begin(); - $sql = "INSERT INTO ".MAIN_DB_PREFIX."livraison (date_creation, fk_user_author, fk_expedition, fk_commande"; - if ($this->entrepot_id) $sql.= ", fk_entrepot"; + $sql = "INSERT INTO ".MAIN_DB_PREFIX."livraison (fk_soc, fk_soc_contact, date_creation, fk_user_author, fk_commande"; + if ($this->commande_id) $sql.= ", fk_commande"; + if ($this->expedition_id) $sql.= ", fk_expedition"; $sql.= ")"; - $sql.= " VALUES (now(), $user->id, ".$this->db->idate($this->date_expedition).",$this->commande_id"; - if ($this->entrepot_id) $sql.= ", $this->entrepot_id"; + $sql.= " VALUES ($this->soc_id, $this->contactid, now(), $user->id, $this->commande_id"; + if ($this->commande_id) $sql.= ", $this->commande_id"; + if ($this->expedition_id) $sql.= ", $this->expedition_id"; $sql.= ")"; $resql=$this->db->query($sql); if ($resql) { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."expedition"); + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."livraison"); - $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET ref='(PROV".$this->id.")' WHERE rowid=".$this->id; + $sql = "UPDATE ".MAIN_DB_PREFIX."livraison SET ref='(PROV".$this->id.")' WHERE rowid=".$this->id; if ($this->db->query($sql)) { - $this->commande = new Commande($this->db); - $this->commande->id = $this->commande_id; - $this->commande->fetch_lignes(); + if ($conf->expedition->enable) + { + $this->expedition = new Expedition($this->db); + $this->expedition->id = $this->expedition_id; + $this->expedition->fetch_lignes(); + } + else + { + $this->commande = new Commande($this->db); + $this->commande->id = $this->commande_id; + $this->commande->fetch_lignes(); + } /* * Insertion des produits dans la base @@ -162,7 +175,7 @@ class Livraison $j++; } - $sql = "INSERT INTO ".MAIN_DB_PREFIX."expeditiondet (fk_expedition, fk_commande_ligne, qty)"; + $sql = "INSERT INTO ".MAIN_DB_PREFIX."livraisondet (fk_livraison, fk_commande_ligne, qty)"; $sql .= " VALUES ($this->id,".$commande_ligne_id.",".$qty.")"; if (! $this->db->query($sql) ) @@ -177,17 +190,18 @@ class Livraison } /** * - * Lit une commande + * Lit un bon de livraison * */ function fetch ($id) { global $conf; - $sql = "SELECT e.rowid, e.date_creation, e.ref, e.fk_user_author, e.fk_statut, e.fk_commande, e.fk_entrepot"; - $sql .= ", ".$this->db->pdate("e.date_expedition")." as date_expedition "; - $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e"; - $sql .= " WHERE e.rowid = $id"; + $sql = "SELECT l.rowid, l.fk_soc, l.fk_soc_contact, l.date_creation, l.ref, l.fk_user_author,"; + $sql .=" l.fk_statut, l.fk_commande, l.fk_expedition, l.fk_user_valid, l.note, l.note_public"; + $sql .= ", ".$this->db->pdate("l.date_livraison")." as date_livraison, fk_adresse_livraison, model_pdf"; + $sql .= " FROM ".MAIN_DB_PREFIX."livraison as l"; + $sql .= " WHERE l.rowid = $id"; $result = $this->db->query($sql) ; @@ -195,18 +209,25 @@ class Livraison { $obj = $this->db->fetch_object($result); - $this->id = $obj->rowid; - $this->ref = $obj->ref; - $this->statut = $obj->fk_statut; - $this->commande_id = $obj->fk_commande; - $this->user_author_id = $obj->fk_user_author; - $this->date = $obj->date_expedition; - $this->entrepot_id = $obj->fk_entrepot; + $this->id = $obj->rowid; + $this->socid = $obj->fk_soc; + $this->contact_id = $obj->fk_soc_contact; + $this->ref = $obj->ref; + $this->statut = $obj->fk_statut; + $this->commande_id = $obj->fk_commande; + $this->expedition_id = $obj->fk_expedition; + $this->user_author_id = $obj->fk_user_author; + $this->user_valid_id = $obj->fk_user_valid; + $this->date = $obj->date_livraison; + $this->adresse_livraison_id = $obj->fk_entrepot; + $this->note = $obj->note; + $this->note_public = $obj->note_public; + $this->modelpdf = $obj->model_pdf; $this->db->free(); if ($this->statut == 0) $this->brouillon = 1; - $file = $conf->expedition->dir_output . "/" .get_exdir($expedition->id) . "/" . $this->id.".pdf"; + $file = $conf->livraison->dir_output . "/" .get_exdir($livraison->id) . "/" . $this->id.".pdf"; $this->pdf_filename = $file; return 1; @@ -235,24 +256,49 @@ class Livraison $error = 0; - if ($user->rights->expedition->valider) + if ($user->rights->expedition->livraison->valider) { - $this->ref = "EXP".$this->id; - + if (defined('LIVRAISON_ADDON')) + { + if (is_readable(DOL_DOCUMENT_ROOT .'/livraison/mods/'.LIVRAISON_ADDON.'.php')) + { + require_once DOL_DOCUMENT_ROOT .'/livraison/mods/'.LIVRAISON_ADDON.'.php'; + + // Definition du nom de module de numerotation de commande + $modName=COMMANDE_ADDON; + + // Recuperation de la nouvelle reference + $objMod = new $modName($this->db); + $soc = new Societe($this->db); + $soc->fetch($this->soc_id); + + // on vérifie si le bon de livraison est en numérotation provisoire + $comref = substr($this->ref, 1, 4); + if ($comref == PROV) + { + $num = $objMod->commande_get_num($soc); + } + else + { + $num = $this->ref; + } + // \todo Tester si non dejà au statut validé. Si oui, on arrete afin d'éviter // de décrémenter 2 fois le stock. - $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET ref='".$this->ref."', fk_statut = 1, date_valid=now(), fk_user_valid=$user->id"; + $sql = "UPDATE ".MAIN_DB_PREFIX."livraison 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) ) { +/* + // Si module stock géré et que expedition faite depuis un entrepot if ($conf->stock->enabled && $this->entrepot_id) { - /* - * Enregistrement d'un mouvement de stock pour chaque produit de l'expedition - */ + + //Enregistrement d'un mouvement de stock pour chaque produit de l'expedition + dolibarr_syslog("expedition.class.php::valid enregistrement des mouvements"); @@ -292,6 +338,8 @@ class Livraison return -2; } } +*/ + return 1; } else { @@ -405,10 +453,10 @@ class Livraison { $this->db->begin(); - $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet WHERE fk_expedition = $this->id ;"; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."livraisondet WHERE fk_livraison = $this->id ;"; if ( $this->db->query($sql) ) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."expedition WHERE rowid = $this->id;"; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."livraison WHERE rowid = $this->id;"; if ( $this->db->query($sql) ) { $this->db->commit(); @@ -426,25 +474,7 @@ class Livraison return -1; } } - /** - * Classe la commande - * - * - */ - 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 - { - print $this->db->error() . ' in ' . $sql; - } - } + /** * Genere le pdf