diff --git a/htdocs/expedition/expedition.class.php b/htdocs/expedition/expedition.class.php
index 971c1073013..464174c04ec 100644
--- a/htdocs/expedition/expedition.class.php
+++ b/htdocs/expedition/expedition.class.php
@@ -182,7 +182,7 @@ class Expedition
}
/**
*
- * Lit une commande
+ * Lit une expedition
*
*/
function fetch ($id)
@@ -190,9 +190,11 @@ class Expedition
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 .= ", ".$this->db->pdate("e.date_expedition")." as date_expedition, c.fk_adresse_livraison";
$sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
+ $sql .= " FROM ".MAIN_DB_PREFIX."commande as c";
$sql .= " WHERE e.rowid = $id";
+ $sql .= " AND e.fk_commande = c.rowid";
$result = $this->db->query($sql) ;
@@ -200,16 +202,59 @@ class Expedition
{
$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->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->adresse_livraison_id = $obj->fk_adresse_livraison;
$this->db->free();
if ($this->statut == 0) $this->brouillon = 1;
+
+ // ligne de produit associée à une expédition
+ $this->lignes = array();
+ $sql = "SELECT c.description, c.qty as qtycom, e.qty as qtyexp";
+ $sql .= ", c.fk_product, c.label, p.ref";
+ $sql .= " FROM ".MAIN_DB_PREFIX."expeditiondet as e";
+ $sql .= " , ".MAIN_DB_PREFIX."commandedet as c";
+ $sql .= " , ".MAIN_DB_PREFIX."product as p";
+ $sql .= " WHERE e.fk_expedition = ".$this->id;
+ $sql .= " AND e.fk_commande_ligne = c.rowid";
+ $sql .= " AND c.fk_product = p.rowid";
+
+ $result = $this->db->query($sql);
+
+ if ($result)
+ {
+ $num = $this->db->num_rows($result);
+ $i = 0;
+
+ while ($i < $num)
+ {
+ $objp = $this->db->fetch_object($result);
+
+ $ligne = new ExpeditionLigne();
+
+ $ligne->product_desc = $objp->description; // Description ligne
+ $ligne->qty_commande = $objp->qtycom;
+ $ligne->product_id = $objp->fk_product;
+
+ $ligne->libelle = $objp->label; // Label produit
+ $ligne->ref = $objp->ref;
+
+ $this->lignes[$i] = $ligne;
+ $i++;
+ }
+ $this->db->free($result);
+ }
+ else
+ {
+ dolibarr_syslog("Propal::Fetch Erreur lecture des produits");
+ return -1;
+ }
$file = $conf->expedition->dir_output . "/" .get_exdir($expedition->id) . "/" . $this->id.".pdf";
$this->pdf_filename = $file;
@@ -365,6 +410,32 @@ class Expedition
}
}
}
+
+ /**
+ * \brief Crée un bon de livraison à partir de l'expédition
+ * \param user Utilisateur
+ * \return int <0 si ko, >=0 si ok
+ */
+ function create_delivery($user)
+ {
+ global $conf;
+
+ if ($conf->livraison->enabled)
+ {
+ if ($this->statut == 1)
+ {
+ // Expédition validée
+ include_once(DOL_DOCUMENT_ROOT."/livraison/livraison.class.php");
+ $livraison = new Livraison($this->db);
+ $result=$livraison->create_from_sending($user, $this->id);
+
+ return $result;
+ }
+ else return 0;
+ }
+ else return 0;
+ }
+
/**
* Ajoute une ligne
*
@@ -506,6 +577,21 @@ class Expedition
class ExpeditionLigne
{
+ // From llx_expeditiondet
+ var $qty;
+ var $qty_expedition;
+ var $product_id;
+
+ // From llx_commandedet
+ var $qty_commande;
+ var $libelle; // Label produit
+ var $product_desc; // Description produit
+ var $ref;
+
+ function ExpeditionLigne()
+ {
+
+ }
}
diff --git a/htdocs/expedition/fiche.php b/htdocs/expedition/fiche.php
index 96a1dc8b964..85e550770bb 100644
--- a/htdocs/expedition/fiche.php
+++ b/htdocs/expedition/fiche.php
@@ -98,6 +98,16 @@ if ($_POST["action"] == 'add')
}
}
+/*
+ * Génère un bon de livraison
+ */
+if ($_POST["action"] == 'create_delivery' && $conf->livraison->enabled && $user->rights->expedition->livraison->creer)
+{
+ $expedition = new Expedition($db);
+ $expedition->fetch($_GET["id"]);
+ $expedition->create_delivery($user);
+}
+
if ($_POST["action"] == 'confirm_valid' && $_POST["confirm"] == 'yes' && $user->rights->expedition->valider)
{
$expedition = new Expedition($db);
@@ -497,6 +507,11 @@ else
{
print ''.$langs->trans("Validate").'';
}
+
+ if ($conf->livraison->enabled && $expedition->statut == 1 && $user->rights->expedition->livraison->creer)
+ {
+ print ''.$langs->trans("DeliveryOrder").'';
+ }
print ''.$langs->trans('BuildPDF').'';
diff --git a/htdocs/livraison/livraison.class.php b/htdocs/livraison/livraison.class.php
index b41135b917f..45d7f3313c0 100644
--- a/htdocs/livraison/livraison.class.php
+++ b/htdocs/livraison/livraison.class.php
@@ -77,11 +77,11 @@ class Livraison
$this->db->begin();
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."livraison (date_creation, fk_user_author";
+ $sql = "INSERT INTO ".MAIN_DB_PREFIX."livraison (date_creation, fk_user_author, fk_adresse_livraison";
if ($this->commande_id) $sql.= ", fk_commande";
if ($this->expedition_id) $sql.= ", fk_expedition";
$sql.= ")";
- $sql.= " VALUES (now(), $user->id";
+ $sql.= " VALUES (now(), $user->id, $this->adresse_livraison_id";
if ($this->commande_id) $sql.= ", $this->commande_id";
if ($this->expedition_id) $sql.= ", $this->expedition_id";
$sql.= ")";
@@ -95,6 +95,7 @@ class Livraison
if ($this->db->query($sql))
{
+ /* //test
if ($conf->expedition->enabled)
{
$this->expedition = new Expedition($this->db);
@@ -102,6 +103,8 @@ class Livraison
$this->expedition->fetch_lignes();
}
else
+ */
+ if (!$conf->expedition->enabled)
{
$this->commande = new Commande($this->db);
$this->commande->id = $this->commande_id;
@@ -114,10 +117,10 @@ class Livraison
for ($i = 0 ; $i < sizeof($this->lignes) ; $i++)
{
//TODO
- if (! $this->create_line(0, $this->lignes[$i]->commande_ligne_id, $this->lignes[$i]->qty))
- {
- $error++;
- }
+ if (! $this->create_line(0, $this->lignes[$i]->commande_ligne_id, $this->lignes[$i]->qty))
+ {
+ $error++;
+ }
}
/*
@@ -368,6 +371,35 @@ class Livraison
return 1;
}
+ /** \brief Créé le bon de livraison depuis une expédition existante
+ \param user Utilisateur qui crée
+ \param sending_id id de l'expédition qui sert de modèle
+ */
+ function create_from_sending($user, $sending_id)
+ {
+ $expedition = new Expedition($this->db);
+ $expedition->fetch($sending_id);
+ $this->lines = array();
+ $this->date_livraison = time();
+ $this->expedition_id = $sending_id;
+ for ($i = 0 ; $i < sizeof($expedition->lignes) ; $i++)
+ {
+ $LivraisonLigne = new LivraisonLigne();
+ $LivraisonLigne->libelle = $expedition->lignes[$i]->libelle;
+ $LivraisonLigne->description = $expedition->lignes[$i]->product_desc;
+ $LivraisonLigne->qty = $expedition->lignes[$i]->qty_commande;
+ $LivraisonLigne->product_id = $expedition->lignes[$i]->product_id;
+ $LivraisonLigne->ref = $expedition->lignes[$i]->ref;
+ $this->lines[$i] = $LivraisonLigne;
+ }
+
+ $this->note = $expedition->note;
+ $this->projetid = $expedition->projetidp;
+ $this->date_livraison = $expedition->date_livraison;
+ $this->adresse_livraison_id = $expedition->adresse_livraison_id;
+
+ return $this->create($user);
+ }
/**
* Ajoute un produit