sendings.lib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /* Copyright (C) 2008-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/lib/sendings.lib.php
  20. * \ingroup expedition
  21. * \brief Library for expedition module
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
  24. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
  26. /**
  27. * Prepare array with list of tabs
  28. *
  29. * @param Object $object Object related to tabs
  30. * @return array Array of tabs to show
  31. */
  32. function shipping_prepare_head($object)
  33. {
  34. global $db, $langs, $conf, $user;
  35. $langs->load("sendings");
  36. $langs->load("deliveries");
  37. $h = 0;
  38. $head = array();
  39. $head[$h][0] = DOL_URL_ROOT."/expedition/card.php?id=".$object->id;
  40. $head[$h][1] = $langs->trans("SendingCard");
  41. $head[$h][2] = 'shipping';
  42. $h++;
  43. if ($conf->livraison_bon->enabled && $user->rights->expedition->livraison->lire)
  44. {
  45. // delivery link
  46. $object->fetchObjectLinked($object->id,$object->element);
  47. if (count($object->linkedObjectsIds['delivery']) > 0) // If there is a delivery
  48. {
  49. // Take first one element of array
  50. $tmp = reset($object->linkedObjectsIds['delivery']);
  51. $head[$h][0] = DOL_URL_ROOT."/livraison/card.php?id=".$tmp;
  52. $head[$h][1] = $langs->trans("DeliveryCard");
  53. $head[$h][2] = 'delivery';
  54. $h++;
  55. }
  56. }
  57. if (empty($conf->global->MAIN_DISABLE_CONTACTS_TAB))
  58. {
  59. $objectsrc = $object;
  60. if ($object->origin == 'commande' && $object->origin_id > 0)
  61. {
  62. $objectsrc = new Commande($db);
  63. $objectsrc->fetch($object->origin_id);
  64. }
  65. $nbContact = count($objectsrc->liste_contact(-1,'internal')) + count($objectsrc->liste_contact(-1,'external'));
  66. $head[$h][0] = DOL_URL_ROOT."/expedition/contact.php?id=".$object->id;
  67. $head[$h][1] = $langs->trans("ContactsAddresses");
  68. if ($nbContact > 0) $head[$h][1].= ' <span class="badge">'.$nbContact.'</span>';
  69. $head[$h][2] = 'contact';
  70. $h++;
  71. }
  72. $nbNote = 0;
  73. if (!empty($object->note_private)) $nbNote++;
  74. if (!empty($object->note_public)) $nbNote++;
  75. $head[$h][0] = DOL_URL_ROOT."/expedition/note.php?id=".$object->id;
  76. $head[$h][1] = $langs->trans("Notes");
  77. if ($nbNote > 0) $head[$h][1].= ' <span class="badge">'.$nbNote.'</span>';
  78. $head[$h][2] = 'note';
  79. $h++;
  80. // Show more tabs from modules
  81. // Entries must be declared in modules descriptor with line
  82. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  83. // $this->tabs = array('entity:-tabname); to remove a tab
  84. complete_head_from_modules($conf,$langs,$object,$head,$h,'delivery');
  85. complete_head_from_modules($conf,$langs,$object,$head,$h,'delivery','remove');
  86. return $head;
  87. }
  88. /**
  89. * Prepare array with list of tabs
  90. *
  91. * @param Object $object Object related to tabs
  92. * @return array Array of tabs to show
  93. */
  94. function delivery_prepare_head($object)
  95. {
  96. global $langs, $conf, $user;
  97. $langs->load("sendings");
  98. $langs->load("deliveries");
  99. $h = 0;
  100. $head = array();
  101. if ($conf->expedition_bon->enabled && $user->rights->expedition->lire)
  102. {
  103. $head[$h][0] = DOL_URL_ROOT."/expedition/card.php?id=".$object->origin_id;
  104. $head[$h][1] = $langs->trans("SendingCard");
  105. $head[$h][2] = 'shipping';
  106. $h++;
  107. }
  108. $head[$h][0] = DOL_URL_ROOT."/livraison/card.php?id=".$object->id;
  109. $head[$h][1] = $langs->trans("DeliveryCard");
  110. $head[$h][2] = 'delivery';
  111. $h++;
  112. $head[$h][0] = DOL_URL_ROOT."/expedition/contact.php?id=".$object->origin_id;
  113. $head[$h][1] = $langs->trans("ContactsAddresses");
  114. $head[$h][2] = 'contact';
  115. $h++;
  116. $head[$h][0] = DOL_URL_ROOT."/expedition/note.php?id=".$object->origin_id;
  117. $head[$h][1] = $langs->trans("Notes");
  118. $head[$h][2] = 'note';
  119. $h++;
  120. // Show more tabs from modules
  121. // Entries must be declared in modules descriptor with line
  122. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  123. // $this->tabs = array('entity:-tabname); to remove a tab
  124. // complete_head_from_modules use $object->id for this link so we temporary change it
  125. $tmpObjectId = $object->id;
  126. $object->id = $object->origin_id;
  127. complete_head_from_modules($conf,$langs,$object,$head,$h,'delivery');
  128. complete_head_from_modules($conf,$langs,$object,$head,$h,'delivery','remove');
  129. $object->id = $tmpObjectId;
  130. return $head;
  131. }
  132. /**
  133. * List sendings and receive receipts
  134. *
  135. * @param string $origin Origin ('commande', ...)
  136. * @param int $origin_id Origin id
  137. * @param string $filter Filter
  138. * @return int <0 if KO, >0 if OK
  139. */
  140. function show_list_sending_receive($origin,$origin_id,$filter='')
  141. {
  142. global $db, $conf, $langs, $bc;
  143. global $form;
  144. $product_static=new Product($db);
  145. $expedition=new Expedition($db);
  146. $warehousestatic=new Entrepot($db);
  147. $sql = "SELECT obj.rowid, obj.fk_product, obj.label, obj.description, obj.product_type as fk_product_type, obj.qty as qty_asked, obj.date_start, obj.date_end,";
  148. $sql.= " ed.rowid as edrowid, ed.qty as qty_shipped, ed.fk_expedition as expedition_id, ed.fk_origin_line, ed.fk_entrepot as warehouse_id,";
  149. $sql.= " e.rowid as sendingid, e.ref as exp_ref, e.date_creation, e.date_delivery, e.date_expedition,";
  150. //if ($conf->livraison_bon->enabled) $sql .= " l.rowid as livraison_id, l.ref as livraison_ref, l.date_delivery, ld.qty as qty_received,";
  151. $sql.= ' p.label as product_label, p.ref, p.fk_product_type, p.rowid as prodid, p.tobatch as product_tobatch,';
  152. $sql.= ' p.description as product_desc';
  153. $sql.= " FROM ".MAIN_DB_PREFIX."expeditiondet as ed";
  154. $sql.= ", ".MAIN_DB_PREFIX."expedition as e";
  155. $sql.= ", ".MAIN_DB_PREFIX.$origin."det as obj";
  156. //if ($conf->livraison_bon->enabled) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."livraison as l ON l.fk_expedition = e.rowid LEFT JOIN ".MAIN_DB_PREFIX."livraisondet as ld ON ld.fk_livraison = l.rowid AND obj.rowid = ld.fk_origin_line";
  157. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON obj.fk_product = p.rowid";
  158. //TODO Add link to expeditiondet_batch
  159. $sql.= " WHERE e.entity IN (".getEntity('expedition', 1).")";
  160. $sql.= " AND obj.fk_".$origin." = ".$origin_id;
  161. $sql.= " AND obj.rowid = ed.fk_origin_line";
  162. $sql.= " AND ed.fk_expedition = e.rowid";
  163. if ($filter) $sql.= $filter;
  164. $sql.= " ORDER BY obj.fk_product";
  165. dol_syslog("show_list_sending_receive", LOG_DEBUG);
  166. $resql = $db->query($sql);
  167. if ($resql)
  168. {
  169. $num = $db->num_rows($resql);
  170. $i = 0;
  171. if ($num)
  172. {
  173. if ($filter) print load_fiche_titre($langs->trans("OtherSendingsForSameOrder"));
  174. else print load_fiche_titre($langs->trans("SendingsAndReceivingForSameOrder"));
  175. print '<table class="liste" width="100%">';
  176. print '<tr class="liste_titre">';
  177. //print '<td align="left">'.$langs->trans("QtyOrdered").'</td>';
  178. print '<td align="left">'.$langs->trans("SendingSheet").'</td>';
  179. print '<td align="left">'.$langs->trans("Description").'</td>';
  180. print '<td align="center">'.$langs->trans("DateCreation").'</td>';
  181. print '<td align="center">'.$langs->trans("DateDeliveryPlanned").'</td>';
  182. print '<td align="center">'.$langs->trans("QtyPreparedOrShipped").'</td>';
  183. if (! empty($conf->stock->enabled))
  184. {
  185. print '<td>'.$langs->trans("Warehouse").'</td>';
  186. }
  187. /*TODO Add link to expeditiondet_batch
  188. if (! empty($conf->productbatch->enabled))
  189. {
  190. print '<td>';
  191. print '</td>';
  192. }*/
  193. if (! empty($conf->livraison_bon->enabled))
  194. {
  195. print '<td>'.$langs->trans("DeliveryOrder").'</td>';
  196. //print '<td align="center">'.$langs->trans("QtyReceived").'</td>';
  197. print '<td align="right">'.$langs->trans("DeliveryDate").'</td>';
  198. }
  199. print "</tr>\n";
  200. $var=True;
  201. while ($i < $num)
  202. {
  203. $objp = $db->fetch_object($resql);
  204. print '<tr class="oddeven">';
  205. // Sending id
  206. print '<td align="left" class="nowrap">';
  207. print '<a href="'.DOL_URL_ROOT.'/expedition/card.php?id='.$objp->expedition_id.'">'.img_object($langs->trans("ShowSending"),'sending').' '.$objp->exp_ref.'<a>';
  208. print '</td>';
  209. // Description
  210. if ($objp->fk_product > 0)
  211. {
  212. // Define output language
  213. if (! empty($conf->global->MAIN_MULTILANGS) && ! empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE))
  214. {
  215. $object = new $origin($db);
  216. $object->fetch($origin_id);
  217. $object->fetch_thirdparty();
  218. $prod = new Product($db);
  219. $prod->id=$objp->fk_product;
  220. $prod->getMultiLangs();
  221. $outputlangs = $langs;
  222. $newlang='';
  223. if (empty($newlang) && ! empty($_REQUEST['lang_id'])) $newlang=$_REQUEST['lang_id'];
  224. if (empty($newlang)) $newlang=$object->thirdparty->default_lang;
  225. if (! empty($newlang))
  226. {
  227. $outputlangs = new Translate("",$conf);
  228. $outputlangs->setDefaultLang($newlang);
  229. }
  230. $label = (! empty($prod->multilangs[$outputlangs->defaultlang]["label"])) ? $prod->multilangs[$outputlangs->defaultlang]["label"] : $objp->product_label;
  231. }
  232. else
  233. {
  234. $label = (! empty($objp->label)?$objp->label:$objp->product_label);
  235. }
  236. print '<td>';
  237. // Show product and description
  238. $product_static->type=$objp->fk_product_type;
  239. $product_static->id=$objp->fk_product;
  240. $product_static->ref=$objp->ref;
  241. $product_static->status_batch=$objp->product_tobatch;
  242. $text=$product_static->getNomUrl(1);
  243. $text.= ' - '.$label;
  244. $description=(! empty($conf->global->PRODUIT_DESC_IN_FORM)?'':dol_htmlentitiesbr($objp->description));
  245. print $form->textwithtooltip($text,$description,3,'','',$i);
  246. // Show range
  247. print_date_range($objp->date_start,$objp->date_end);
  248. // Add description in form
  249. if (! empty($conf->global->PRODUIT_DESC_IN_FORM))
  250. {
  251. print (! empty($objp->description) && $objp->description!=$objp->product)?'<br>'.dol_htmlentitiesbr($objp->description):'';
  252. }
  253. print '</td>';
  254. }
  255. else
  256. {
  257. print "<td>";
  258. if ($objp->fk_product_type==1) $text = img_object($langs->trans('Service'),'service');
  259. else $text = img_object($langs->trans('Product'),'product');
  260. if (! empty($objp->label)) {
  261. $text.= ' <strong>'.$objp->label.'</strong>';
  262. print $form->textwithtooltip($text,$objp->description,3,'','',$i);
  263. } else {
  264. print $text.' '.nl2br($objp->description);
  265. }
  266. // Show range
  267. print_date_range($objp->date_start,$objp->date_end);
  268. print "</td>\n";
  269. }
  270. //print '<td align="center">'.$objp->qty_asked.'</td>';
  271. // Date creation
  272. print '<td align="center" class="nowrap">'.dol_print_date($db->jdate($objp->date_creation),'day').'</td>';
  273. // Date shipping creation
  274. print '<td align="center" class="nowrap">'.dol_print_date($db->jdate($objp->date_delivery),'day').'</td>';
  275. // Qty shipped
  276. print '<td align="center">'.$objp->qty_shipped.'</td>';
  277. // Warehouse
  278. if (! empty($conf->stock->enabled))
  279. {
  280. print '<td>';
  281. if ($objp->warehouse_id > 0)
  282. {
  283. $warehousestatic->fetch($objp->warehouse_id);
  284. print $warehousestatic->getNomUrl(1);
  285. }
  286. print '</td>';
  287. }
  288. // Batch number managment
  289. /*TODO Add link to expeditiondet_batch
  290. if (! empty($conf->productbatch->enabled))
  291. {
  292. var_dump($objp->edrowid);
  293. $lines[$i]->detail_batch
  294. if (isset($lines[$i]->detail_batch))
  295. {
  296. print '<td>';
  297. if ($lines[$i]->product_tobatch)
  298. {
  299. $detail = '';
  300. foreach ($lines[$i]->detail_batch as $dbatch)
  301. {
  302. $detail.= $langs->trans("DetailBatchFormat",$dbatch->batch,dol_print_date($dbatch->eatby,"day"),dol_print_date($dbatch->sellby,"day"),$dbatch->dluo_qty).'<br/>';
  303. }
  304. print $form->textwithtooltip(img_picto('', 'object_barcode').' '.$langs->trans("DetailBatchNumber"),$detail);
  305. }
  306. else
  307. {
  308. print $langs->trans("NA");
  309. }
  310. print '</td>';
  311. } else {
  312. print '<td></td>';
  313. }
  314. }*/
  315. // Informations on receipt
  316. if (! empty($conf->livraison_bon->enabled))
  317. {
  318. include_once DOL_DOCUMENT_ROOT.'/livraison/class/livraison.class.php';
  319. $expedition->id=$objp->sendingid;
  320. $expedition->fetchObjectLinked($expedition->id,$expedition->element);
  321. //var_dump($expedition->linkedObjects);
  322. $receiving='';
  323. if (count($expedition->linkedObjects['delivery']) > 0) $receiving=reset($expedition->linkedObjects['delivery']); // Take first link
  324. if (! empty($receiving))
  325. {
  326. // $expedition->fk_origin_line = id of det line of order
  327. // $receiving->fk_origin_line = id of det line of order
  328. // $receiving->origin may be 'shipping'
  329. // $receiving->origin_id may be id of shipping
  330. // Ref
  331. print '<td>';
  332. print $receiving->getNomUrl($db);
  333. //print '<a href="'.DOL_URL_ROOT.'/livraison/card.php?id='.$livraison_id.'">'.img_object($langs->trans("ShowReceiving"),'sending').' '.$objp->livraison_ref.'<a>';
  334. print '</td>';
  335. // Qty received
  336. //print '<td align="center">';
  337. // TODO No solution for the moment to link a line det of receipt with a line det of shipping,
  338. // so no way to know the qty received for this line of shipping.
  339. //print $langs->trans("FeatureNotYetAvailable");
  340. //print '</td>';
  341. // Date shipping real
  342. print '<td align="right">';
  343. print dol_print_date($receiving->date_delivery,'day');
  344. print '</td>';
  345. }
  346. else
  347. {
  348. //print '<td>&nbsp;</td>';
  349. print '<td>&nbsp;</td>';
  350. print '<td>&nbsp;</td>';
  351. }
  352. }
  353. print '</tr>';
  354. $i++;
  355. }
  356. print '</table>';
  357. }
  358. $db->free($resql);
  359. }
  360. else
  361. {
  362. dol_print_error($db);
  363. }
  364. return 1;
  365. }