* Copyright (C) 2005-2008 Laurent Destailleur * Copyright (C) 2005 Simon TOSSER * Copyright (C) 2005-2008 Regis Houssin * * 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, see . */ // Code identique a /expedition/commande.php /** * \file htdocs/expedition/fiche.php * \ingroup expedition * \brief Fiche descriptive d'une expedition * \version $Id$ */ require("./pre.inc.php"); require_once(DOL_DOCUMENT_ROOT."/html.formfile.class.php"); require_once(DOL_DOCUMENT_ROOT."/html.formproduct.class.php"); require_once(DOL_DOCUMENT_ROOT."/lib/product.lib.php"); require_once(DOL_DOCUMENT_ROOT."/lib/sendings.lib.php"); if ($conf->produit->enabled) require_once(DOL_DOCUMENT_ROOT."/product.class.php"); if ($conf->propal->enabled) require_once(DOL_DOCUMENT_ROOT."/propal.class.php"); if ($conf->commande->enabled) require_once(DOL_DOCUMENT_ROOT."/commande/commande.class.php"); if ($conf->stock->enabled) require_once(DOL_DOCUMENT_ROOT."/product/stock/entrepot.class.php"); $langs->load("companies"); $langs->load("bills"); $langs->load('deliveries'); $langs->load('orders'); $langs->load('stocks'); $langs->load('other'); $langs->load('propal'); if (! $user->rights->expedition->lire) accessforbidden(); // Security customer access if ($user->societe_id > 0) { $action = ''; $socid = $user->societe_id; } $origin = $_GET["origin"]?$_GET["origin"]:$_POST["origin"]; // Example: commande, propal $origin_id = $_GET["object_id"]?$_GET["object_id"]:$_POST["object_id"]; // Id of order or propal /* * Actions */ if ($_POST["action"] == 'add') { $db->begin(); // Creation de l'objet expedition $expedition = new Expedition($db); $expedition->date_expedition = time(); $expedition->note = $_POST["note"]; $expedition->origin = $origin; $expedition->origin_id = $origin_id; $expedition->weight = $_POST["weight"]==""?"NULL":$_POST["weight"]; $expedition->sizeH = $_POST["sizeH"]==""?"NULL":$_POST["sizeH"]; $expedition->sizeW = $_POST["sizeW"]==""?"NULL":$_POST["sizeW"]; $expedition->sizeS = $_POST["sizeS"]==""?"NULL":$_POST["sizeS"]; $expedition->size_units = $_POST["size_units"]; $expedition->weight_units = $_POST["weight_units"]; // On boucle sur chaque ligne du document d'origine pour completer objet expedition // avec qte a livrer $class = ucfirst($expedition->origin); $object = new $class($db); $object->fetch($expedition->origin_id); //$object->fetch_lines(); $expedition->socid = $object->socid; $expedition->expedition_method_id = $_POST["expedition_method_id"]; $expedition->tracking_number = $_POST["tracking_number"]; for ($i = 0 ; $i < sizeof($object->lignes) ; $i++) { $ent = "entl".$i; $idl = "idl".$i; $qty = "qtyl".$i; $entrepot_id = $_POST[$ent]?$_POST[$ent]:$_POST["entrepot_id"]; if ($_POST[$qty] > 0) { $expedition->addline($entrepot_id,$_POST[$idl],$_POST[$qty]); } } $ret=$expedition->create($user); if ($ret > 0) { $db->commit(); Header("Location: fiche.php?id=".$expedition->id); exit; } else { $db->rollback(); $mesg='
'.$expedition->error.'
'; $_GET["commande_id"]=$_POST["commande_id"]; $_GET["action"]='create'; } } /* * Genere un bon de livraison */ if ($_GET["action"] == 'create_delivery' && $conf->livraison_bon->enabled && $user->rights->expedition->livraison->creer) { $expedition = new Expedition($db); $expedition->fetch($_GET["id"]); $result = $expedition->create_delivery($user); if ($result > 0) { Header("Location: ".DOL_URL_ROOT.'/livraison/fiche.php?id='.$result); exit; } else { $mesg=$expedition->error; } } if ($_POST["action"] == 'confirm_valid' && $_POST["confirm"] == 'yes' && $user->rights->expedition->valider) { $expedition = new Expedition($db); $expedition->fetch($_GET["id"]); $result = $expedition->valid($user); //$expedition->PdfWrite(); } if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == 'yes') { if ($user->rights->expedition->supprimer ) { $expedition = new Expedition($db); $expedition->fetch($_GET["id"]); $expedition->delete(); Header("Location: liste.php"); exit; } } /* * Build doc */ if ($_REQUEST['action'] == 'builddoc') // En get ou en post { require_once(DOL_DOCUMENT_ROOT."/includes/modules/expedition/pdf/ModelePdfExpedition.class.php"); // Sauvegarde le dernier modele choisi pour generer un document $expedition = new Expedition($db, 0, $_REQUEST['id']); $expedition->fetch($_REQUEST['id']); if ($_REQUEST['model']) { $expedition->setDocModel($user, $_REQUEST['model']); } $outputlangs = $langs; if (! empty($_REQUEST['lang_id'])) { $outputlangs = new Translate("",$conf); $outputlangs->setDefaultLang($_REQUEST['lang_id']); } $result=expedition_pdf_create($db,$expedition->id,$expedition->modelpdf,$outputlangs); if ($result <= 0) { dolibarr_print_error($db,$result); exit; } } /* * View */ llxHeader('',$langs->trans('Sending'),'Expedition'); $html = new Form($db); $formfile = new FormFile($db); $formproduct = new FormProduct($db); /********************************************************************* * * Mode creation * *********************************************************************/ if ($_GET["action"] == 'create') { $expe = new Expedition($db); print_titre($langs->trans("CreateASending")); if (! $origin) { $mesg='
'.$langs->trans("ErrorBadParameters").'
'; } if ($mesg) { print $mesg.'
'; } if ($origin) { $class = ucfirst($origin); $object = new $class($db); if ($object->fetch($origin_id)) { $soc = new Societe($db); $soc->fetch($object->socid); $author = new User($db); $author->id = $object->user_author_id; $author->fetch(); if ($conf->stock->enabled) $entrepot = new Entrepot($db); /* * Document source */ print '
'; print ''; print ''; print ''; if ($_GET["entrepot_id"]) { print ''; } print ''; // Ref print ''; print "\n"; // Ref client print ''; print ''; // Tiers print ''; print ''; print ''; // Date print ""; print '\n"; // Warehouse (id forced) if ($conf->stock->enabled && $_GET["entrepot_id"]) { print ''; print ''; } if ($object->note && ! $user->societe_id) { print '"; } print "
'; if ($conf->commande->enabled) { print $langs->trans("RefOrder").''.img_object($langs->trans("ShowOrder"),'order').' '.$object->ref; } else { print $langs->trans("RefProposal").''.img_object($langs->trans("ShowProposal"),'propal').' '.$object->ref; } print '
'; print $langs->trans('RefCustomer').''; print $object->ref_client; print '
'.$langs->trans('Company').''.$soc->getNomUrl(1).'
".$langs->trans("Date")."'.dolibarr_print_date($object->date,"day")."
'.$langs->trans("Warehouse").''; $ents = $entrepot->list_array(); print ''.img_object($langs->trans("ShowWarehouse"),'stock').' '.$ents[$_GET["entrepot_id"]].''; print '
'.$langs->trans("NotePrivate").': '.nl2br($object->note)."
"; print ''; print ''; print '
'; print $langs->trans("Weight"); print ''; print $formproduct->select_measuring_units("weight_units","weight"); print '
'; print $langs->trans("Width"); print '  
'; print $langs->trans("Height"); print ''; print $formproduct->select_measuring_units("size_units","size"); print '
'; print $langs->trans("Depth"); print ' 
'; print ''; // Delivery method print ""; print '\n"; // Tracking number print ""; print '\n"; print "
".$langs->trans("DeliveryMethod")."'; $expe->fetch_delivery_methods(); $expe->GetIdOfDefault(); $html->select_array("expedition_method_id",$expe->meths,$expe->default_method_id,0,0,0,0,"",1); print "
".$langs->trans("TrackingNumber")."'; print ''; print "
"; /* * Lignes de commandes * */ print '
'; //$lignes = $object->fetch_lines(1); $numAsked = sizeof($object->lignes); /* Lecture des expeditions deja effectuees */ $object->loadExpeditions(); if ($numAsked) { print ''; print ''; print ''; print ''; print ''; if ($conf->stock->enabled) { if ($_GET["entrepot_id"]) { print ''; } else { print ''; } } print "\n"; } $var=true; $indiceAsked = 0; while ($indiceAsked < $numAsked) { $product = new Product($db); $ligne = $object->lignes[$indiceAsked]; $var=!$var; print "\n"; if ($ligne->fk_product > 0) { $product->fetch($ligne->fk_product); print ''; } else { //var_dump($ligne); print "\n"; } print ''; $qtyProdCom=$ligne->qty; print ''; $quantityAsked = $ligne->qty; $quantityToBeDelivered = $quantityAsked - $quantityDelivered; if ($conf->stock->enabled) { $defaultqty=0; if ($_GET["entrepot_id"]) { $stock = $product->stock_entrepot[$_GET["entrepot_id"]]; $stock+=0; // Convertit en numerique $defaultqty=min($quantityToBeDelivered, $stock); if ($defaultqty < 0) $defaultqty=0; } // Quantity print ''; // Stock if ($_GET["entrepot_id"]) { print ''; } else { $array=array(); $sql = "SELECT e.rowid, e.label, ps.reel"; $sql.= " FROM ".MAIN_DB_PREFIX."product_stock as ps, ".MAIN_DB_PREFIX."entrepot as e"; $sql.= " WHERE ps.fk_entrepot = e.rowid AND fk_product = '".$product->id."'"; $result = $db->query($sql) ; if ($result) { $num = $db->num_rows($result); $i=0; if ($num > 0) { while ($i < $num) { $obj = $db->fetch_object($result); $array[$obj->rowid] = $obj->label.' ('.$obj->reel.')'; $i++; } } $db->free($result); } else { $this->error=$db->error(); return -1; } print ''; } } else { // Quantity print ''; } print "\n"; // associations sous produits if ($ligne->fk_product > 0) { $product->get_sousproduits_arbo (); $prods_arbo = $product->get_arbo_each_prod($qtyProdCom); if(sizeof($prods_arbo) > 0) { foreach($prods_arbo as $key => $value) { print $value[0]; } } } $indiceAsked++; } /* * */ print ''; print "
'.$langs->trans("Description").''.$langs->trans("QtyOrdered").''.$langs->trans("QtyShipped").''.$langs->trans("QtyToShip").''.$langs->trans("Stock").''.$langs->trans("Warehouse").'
'; print ''.img_object($langs->trans("ShowProduct"),"product").' '.$product->ref.' - '.$product->libelle; if ($ligne->desc) print dol_nl2br(dol_htmlcleanlastbr($ligne->desc),1); print '".nl2br($ligne->desc)."'.$ligne->qty.''; $quantityDelivered = $object->expeditions[$ligne->id]; print $quantityDelivered; print ''; print ''; print ''; print ''.$stock; if ($stock < $quantityToBeDelivered) { print ' '.img_warning($langs->trans("StockTooLow")); } print ''; $html->select_array('entl'.$i,$array,'',1,0,0); print ''; print ''; print ''; print '

"; print '
'; } else { dolibarr_print_error($db); } } } else /* *************************************************************************** */ /* */ /* Edit and view mode */ /* */ /* *************************************************************************** */ { if ($_GET["id"] > 0) { $expedition = new Expedition($db); $result = $expedition->fetch($_GET["id"]); if ($result < 0) { dolibarr_print_error($db,$expedition->error); exit -1; } $lignes = $expedition->lignes; $num_prod = sizeof($lignes); if ($expedition->id > 0) { $typeobject = $expedition->origin; $expedition->fetch_object(); if (strlen($expedition->tracking_number) && strlen($expedition->expedition_method_id)) { $expedition->GetUrlTrackingStatus(); } $soc = new Societe($db); $soc->fetch($expedition->socid); $h=0; $head[$h][0] = DOL_URL_ROOT."/expedition/fiche.php?id=".$expedition->id; $head[$h][1] = $langs->trans("SendingCard"); $hselected = $h; $h++; if ($conf->livraison_bon->enabled && $expedition->livraison_id) { $head[$h][0] = DOL_URL_ROOT."/livraison/fiche.php?id=".$expedition->livraison_id; $head[$h][1] = $langs->trans("DeliveryCard"); $h++; } dolibarr_fiche_head($head, $hselected, $langs->trans("Sending")); if ($mesg) print $mesg; /* * Confirmation de la suppression * */ if ($_GET["action"] == 'delete') { $html->form_confirm($_SERVER['PHP_SELF'].'?id='.$expedition->id,$langs->trans('DeleteSending'),$langs->trans("ConfirmDeleteSending",$expedition->ref),'confirm_delete'); print '
'; } /* * Confirmation de la validation * */ if ($_GET["action"] == 'valid') { $html->form_confirm($_SERVER['PHP_SELF'].'?id='.$expedition->id,$langs->trans('ValidateSending'),$langs->trans("ConfirmValidateSending",$expedition->ref),'confirm_valid'); print '
'; } /* * Confirmation de l'annulation * */ if ($_GET["action"] == 'annuler') { $html->form_confirm($_SERVER['PHP_SELF'].'?id='.$expedition->id,$langs->trans('CancelSending'),$langs->trans("ConfirmCancelSending",$expedition->ref),'confirm_cancel'); print '
'; } // Calcul du poids total et du volume total des produits $totalWeight = ''; $totalVolume = ''; for ($i = 0 ; $i < $num_prod ; $i++) { $weightUnit=0; $volumeUnit=0; if (! empty($lignes[$i]->weight_units)) $weightUnit = $lignes[$i]->weight_units; $trueWeightUnit=pow(10,$weightUnit); $totalWeight += $lignes[$i]->weight*$lignes[$i]->qty_shipped*$trueWeightUnit; if (! empty($lignes[$i]->volume_units)) $volumeUnit = $lignes[$i]->volume_units; $trueVolumeUnit=pow(10,$volumeUnit); $totalVolume += $lignes[$i]->volume*$lignes[$i]->qty_shipped*$trueVolumeUnit; } $totalVolume=$totalVolume; print ''; // Ref print ''; print ''; // Customer print ''; print ''; print ""; // Linked documents print ''; print '\n"; } else { $propal=new Propal($db); $propal->fetch($livraison->origin_id); print $langs->trans("RefProposal").''; print '\n"; } print ''; // Ref customer print ''; print '\n"; print ''; // Date print ''; print '\n"; print ''; // Weight print ''; print ''; // Volume Total print ''; print '\n"; print ''; // Taille print ''; print '\n"; print ''; // Status print ''; print '\n"; print ''; // Tracking Number print ''; print ''; if ($expedition->tracking_url) { print '\n"; } print ''; print "
'.$langs->trans("Ref").''.$expedition->ref.'
'.$langs->trans("Customer").''.$soc->getNomUrl(1).'
'; if ($conf->commande->enabled) { $order=new Commande($db); $order->fetch($expedition->$typeobject->id); print $langs->trans("RefOrder").''; print $order->getNomUrl(1,'commande'); print "'; print $propal->getNomUrl(1,'expedition'); print "
'.$langs->trans("RefCustomer").''.$object->ref_client."
'.$langs->trans("Date").''.dolibarr_print_date($expedition->date,"daytext")."
'.$langs->trans("TotalWeight").''; if ($expedition->trueWeight) { // If sending weigth defined print $expedition->trueWeight.' '.measuring_units_string($expedition->weight_units,"weight"); } else { // If sending Weight not defined we use sum of products // TODO Show in best unit if ($totalWeight > 0) print $totalWeight.' '.measuring_units_string(0,"weight"); else print ' '; } print '
'.$langs->trans("TotalVolume").''; if ($expedition->trueVolume) { // If sending volume defined print $expedition->trueVolume.' '.measuring_units_string($expedition->volumeUnit,"volume"); } else { // If sending volume not defined we use sum of products // TODO Show in best unit if ($totalVolume > 0) print $totalVolume.' '.measuring_units_string(0,"volume"); else print ' '; } print "
'.$langs->trans("Size").''; if ($expedition->trueWidth || $expedition->trueHeight || $expedition->trueDepth) { // If sending size defined print $expedition->trueSize.' '.measuring_units_string($expedition->size_units,"size"); } else print ' '; print "
'.$langs->trans("Status").''.$expedition->getLibStatut(4)."
'.$langs->trans("TrackingNumber").''.$expedition->tracking_number.''.$expedition->tracking_url."
\n"; /* * Lignes produits */ print '
'; print ''; print ''; print ''; if ($expedition->fk_statut <= 1) { print ''; } else { print ''; } print ''; print ''; if ($conf->stock->enabled) { print ''; } print "\n"; $var=false; for ($i = 0 ; $i < $num_prod ; $i++) { print ""; if ($lignes[$i]->fk_product > 0) { print ''; } else { print "\n"; } // Qte commande print ''; // Qte a expedier ou expedier print ''; // Poids print ''; // Volume print ''; // Entrepot source if ($conf->stock->enabled) { $entrepot = new Entrepot($db); $entrepot->fetch($lignes[$i]->entrepot_id); print ''; } print ""; $var=!$var; } } print "
'.$langs->trans("Products").''.$langs->trans("QtyOrdered").''.$langs->trans("QtyToShip").''.$langs->trans("QtyShipped").''.$langs->trans("Weight").''.$langs->trans("Volume").''.$langs->trans("WarehouseSource").'
'; print ''.img_object($langs->trans("ShowProduct"),"product").' '.$lignes[$i]->ref.' - '.$lignes[$i]->libelle; if ($lignes[$i]->description) print '
'.nl2br($lignes[$i]->description); print '
".nl2br($lignes[$i]->description)."'.$lignes[$i]->qty_asked.''.$lignes[$i]->qty_shipped.''.$lignes[$i]->weight*$lignes[$i]->qty_shipped.' '.measuring_units_string($lignes[$i]->weight_units,"weight").''.$lignes[$i]->volume*$lignes[$i]->qty_shipped.' '.measuring_units_string($lignes[$i]->volume_units,"volume").''.$entrepot->getNomUrl(1).'
\n"; print "\n\n"; /* * Boutons actions */ if ($user->societe_id == 0) { print '
'; if (! eregi('^(valid|delete)',$_REQUEST["action"])) { if ($expedition->statut == 0 && $user->rights->expedition->valider && $num_prod > 0) { print ''.$langs->trans("Validate").''; } if ($conf->livraison_bon->enabled && $expedition->statut == 1 && $user->rights->expedition->livraison->creer && !$expedition->livraison_id) { print ''.$langs->trans("DeliveryOrder").''; } if ($expedition->brouillon && $user->rights->expedition->supprimer) { print ''.$langs->trans("Delete").''; } } print '
'; } print "\n"; print "
"; /* * Documents generated */ if ($conf->expedition_bon->enabled) { $expeditionref = sanitizeFileName($expedition->ref); $filedir = $conf->expedition_bon->dir_output . "/" .$expeditionref; $urlsource = $_SERVER["PHP_SELF"]."?id=".$expedition->id; $genallowed=$user->rights->expedition->lire && ($expedition->statut > 0); $delallowed=$user->rights->expedition->supprimer; //$genallowed=1; //$delallowed=0; $somethingshown=$formfile->show_documents('expedition',$expeditionref,$filedir,$urlsource,$genallowed,$delallowed,$expedition->modelpdf); if ($genallowed && ! $somethingshown) $somethingshown=1; } print ''; // Rien a droite print '
'; print '
'; //show_list_sending_receive($expedition->origin,$expedition->origin_id," AND e.rowid <> ".$expedition->id); show_list_sending_receive($expedition->origin,$expedition->origin_id); } else { print "Expedition inexistante ou acces refuse"; } } $db->close(); llxFooter('$Date$ - $Revision$'); ?>