sendings.lib.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. /* Copyright (C) 2008-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.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 <https://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. // Load translation files required by the page
  36. $langs->loadLangs(array("sendings", "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->delivery_note->enabled && $user->rights->expedition->livraison->lire)
  44. {
  45. // delivery link
  46. $object->fetchObjectLinked($object->id, $object->element);
  47. if (is_array($object->linkedObjectsIds['delivery']) && 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."/delivery/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 marginleftonlyshort">'.$nbContact.'</span>';
  69. $head[$h][2] = 'contact';
  70. $h++;
  71. }
  72. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  73. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  74. $upload_dir = $conf->commande->dir_output."/".dol_sanitizeFileName($object->ref);
  75. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  76. $nbLinks = Link::count($db, $object->element, $object->id);
  77. $head[$h][0] = DOL_URL_ROOT.'/expedition/document.php?id='.$object->id;
  78. $head[$h][1] = $langs->trans('Documents');
  79. if (($nbFiles + $nbLinks) > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  80. $head[$h][2] = 'documents';
  81. $h++;
  82. $nbNote = 0;
  83. if (!empty($object->note_private)) $nbNote++;
  84. if (!empty($object->note_public)) $nbNote++;
  85. $head[$h][0] = DOL_URL_ROOT."/expedition/note.php?id=".$object->id;
  86. $head[$h][1] = $langs->trans("Notes");
  87. if ($nbNote > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  88. $head[$h][2] = 'note';
  89. $h++;
  90. // Show more tabs from modules
  91. // Entries must be declared in modules descriptor with line
  92. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  93. // $this->tabs = array('entity:-tabname); to remove a tab
  94. complete_head_from_modules($conf, $langs, $object, $head, $h, 'delivery');
  95. complete_head_from_modules($conf, $langs, $object, $head, $h, 'delivery', 'remove');
  96. return $head;
  97. }
  98. /**
  99. * Prepare array with list of tabs
  100. *
  101. * @param Object $object Object related to tabs
  102. * @return array Array of tabs to show
  103. */
  104. function delivery_prepare_head($object)
  105. {
  106. global $langs, $db, $conf, $user;
  107. // Load translation files required by the page
  108. $langs->loadLangs(array("sendings", "deliveries"));
  109. $h = 0;
  110. $head = array();
  111. if ($conf->expedition_bon->enabled && $user->rights->expedition->lire)
  112. {
  113. $head[$h][0] = DOL_URL_ROOT."/expedition/card.php?id=".$object->origin_id;
  114. $head[$h][1] = $langs->trans("SendingCard");
  115. $head[$h][2] = 'shipping';
  116. $h++;
  117. }
  118. $head[$h][0] = DOL_URL_ROOT."/delivery/card.php?id=".$object->id;
  119. $head[$h][1] = $langs->trans("DeliveryCard");
  120. $head[$h][2] = 'delivery';
  121. $h++;
  122. // Show more tabs from modules
  123. // Entries must be declared in modules descriptor with line
  124. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  125. // $this->tabs = array('entity:-tabname); to remove a tab
  126. // complete_head_from_modules use $object->id for this link so we temporary change it
  127. $savObjectId = $object->id;
  128. // Get parent object
  129. $tmpobject = null;
  130. if ($object->origin) {
  131. $tmpobject = new Expedition($db);
  132. $tmpobject->fetch($object->origin_id);
  133. } else {
  134. $tmpobject = $object;
  135. }
  136. $head[$h][0] = DOL_URL_ROOT."/expedition/contact.php?id=".$tmpobject->id;
  137. $head[$h][1] = $langs->trans("ContactsAddresses");
  138. $head[$h][2] = 'contact';
  139. $h++;
  140. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  141. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  142. $upload_dir = $conf->commande->dir_output."/".dol_sanitizeFileName($tmpobject->ref);
  143. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  144. $nbLinks = Link::count($db, $tmpobject->element, $tmpobject->id);
  145. $head[$h][0] = DOL_URL_ROOT.'/expedition/document.php?id='.$tmpobject->id;
  146. $head[$h][1] = $langs->trans('Documents');
  147. if (($nbFiles + $nbLinks) > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  148. $head[$h][2] = 'documents';
  149. $h++;
  150. $nbNote = 0;
  151. if (!empty($tmpobject->note_private)) $nbNote++;
  152. if (!empty($tmpobject->note_public)) $nbNote++;
  153. $head[$h][0] = DOL_URL_ROOT."/expedition/note.php?id=".$tmpobject->id;
  154. $head[$h][1] = $langs->trans("Notes");
  155. if ($nbNote > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  156. $head[$h][2] = 'note';
  157. $h++;
  158. $object->id = $tmpobject->id;
  159. complete_head_from_modules($conf, $langs, $object, $head, $h, 'delivery');
  160. complete_head_from_modules($conf, $langs, $object, $head, $h, 'delivery', 'remove');
  161. $object->id = $savObjectId;
  162. return $head;
  163. }
  164. /**
  165. * List sendings and receive receipts
  166. *
  167. * @param string $origin Origin ('commande', ...)
  168. * @param int $origin_id Origin id
  169. * @param string $filter Filter
  170. * @return int <0 if KO, >0 if OK
  171. */
  172. function show_list_sending_receive($origin, $origin_id, $filter = '')
  173. {
  174. global $db, $conf, $langs;
  175. global $form;
  176. $product_static = new Product($db);
  177. $expedition = new Expedition($db);
  178. $warehousestatic = new Entrepot($db);
  179. $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,";
  180. $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,";
  181. $sql .= " e.rowid as sendingid, e.ref as exp_ref, e.date_creation, e.date_delivery, e.date_expedition,";
  182. //if ($conf->delivery_note->enabled) $sql .= " l.rowid as livraison_id, l.ref as livraison_ref, l.date_delivery, ld.qty as qty_received,";
  183. $sql .= ' p.label as product_label, p.ref, p.fk_product_type, p.rowid as prodid, p.tobatch as product_tobatch,';
  184. $sql .= ' p.description as product_desc';
  185. $sql .= " FROM ".MAIN_DB_PREFIX."expeditiondet as ed";
  186. $sql .= ", ".MAIN_DB_PREFIX."expedition as e";
  187. $sql .= ", ".MAIN_DB_PREFIX.$origin."det as obj";
  188. //if ($conf->delivery_note->enabled) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."delivery as l ON l.fk_expedition = e.rowid LEFT JOIN ".MAIN_DB_PREFIX."deliverydet as ld ON ld.fk_delivery = l.rowid AND obj.rowid = ld.fk_origin_line";
  189. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON obj.fk_product = p.rowid";
  190. //TODO Add link to expeditiondet_batch
  191. $sql .= " WHERE e.entity IN (".getEntity('expedition').")";
  192. $sql .= " AND obj.fk_".$origin." = ".$origin_id;
  193. $sql .= " AND obj.rowid = ed.fk_origin_line";
  194. $sql .= " AND ed.fk_expedition = e.rowid";
  195. if ($filter) $sql .= $filter;
  196. $sql .= " ORDER BY obj.fk_product";
  197. dol_syslog("show_list_sending_receive", LOG_DEBUG);
  198. $resql = $db->query($sql);
  199. if ($resql)
  200. {
  201. $num = $db->num_rows($resql);
  202. $i = 0;
  203. if ($num)
  204. {
  205. if ($filter) print load_fiche_titre($langs->trans("OtherSendingsForSameOrder"));
  206. else print load_fiche_titre($langs->trans("SendingsAndReceivingForSameOrder"));
  207. print '<table class="liste centpercent">';
  208. print '<tr class="liste_titre">';
  209. //print '<td class="left">'.$langs->trans("QtyOrdered").'</td>';
  210. print '<td>'.$langs->trans("SendingSheet").'</td>';
  211. print '<td>'.$langs->trans("Description").'</td>';
  212. print '<td class="center">'.$langs->trans("DateCreation").'</td>';
  213. print '<td class="center">'.$langs->trans("DateDeliveryPlanned").'</td>';
  214. print '<td class="center">'.$langs->trans("QtyPreparedOrShipped").'</td>';
  215. if (!empty($conf->stock->enabled))
  216. {
  217. print '<td>'.$langs->trans("Warehouse").'</td>';
  218. }
  219. /*TODO Add link to expeditiondet_batch
  220. if (! empty($conf->productbatch->enabled))
  221. {
  222. print '<td>';
  223. print '</td>';
  224. }*/
  225. if (!empty($conf->delivery_note->enabled))
  226. {
  227. print '<td>'.$langs->trans("DeliveryOrder").'</td>';
  228. //print '<td class="center">'.$langs->trans("QtyReceived").'</td>';
  229. print '<td class="right">'.$langs->trans("DeliveryDate").'</td>';
  230. }
  231. print "</tr>\n";
  232. while ($i < $num)
  233. {
  234. $objp = $db->fetch_object($resql);
  235. print '<tr class="oddeven">';
  236. // Sending id
  237. print '<td class="nowrap left">';
  238. print '<a href="'.DOL_URL_ROOT.'/expedition/card.php?id='.$objp->expedition_id.'">'.img_object($langs->trans("ShowSending"), 'sending').' '.$objp->exp_ref.'<a>';
  239. print '</td>';
  240. // Description
  241. if ($objp->fk_product > 0)
  242. {
  243. // Define output language
  244. if (!empty($conf->global->MAIN_MULTILANGS) && !empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE))
  245. {
  246. $object = new $origin($db);
  247. $object->fetch($origin_id);
  248. $object->fetch_thirdparty();
  249. $prod = new Product($db);
  250. $prod->id = $objp->fk_product;
  251. $prod->getMultiLangs();
  252. $outputlangs = $langs;
  253. $newlang = '';
  254. if (empty($newlang) && !empty($_REQUEST['lang_id'])) $newlang = $_REQUEST['lang_id'];
  255. if (empty($newlang)) $newlang = $object->thirdparty->default_lang;
  256. if (!empty($newlang))
  257. {
  258. $outputlangs = new Translate("", $conf);
  259. $outputlangs->setDefaultLang($newlang);
  260. }
  261. $label = (!empty($prod->multilangs[$outputlangs->defaultlang]["label"])) ? $prod->multilangs[$outputlangs->defaultlang]["label"] : $objp->product_label;
  262. } else {
  263. $label = (!empty($objp->label) ? $objp->label : $objp->product_label);
  264. }
  265. print '<td>';
  266. // Show product and description
  267. $product_static->type = $objp->fk_product_type;
  268. $product_static->id = $objp->fk_product;
  269. $product_static->ref = $objp->ref;
  270. $product_static->status_batch = $objp->product_tobatch;
  271. $text = $product_static->getNomUrl(1);
  272. $text .= ' - '.$label;
  273. $description = (!empty($conf->global->PRODUIT_DESC_IN_FORM) ? '' : dol_htmlentitiesbr($objp->description));
  274. print $form->textwithtooltip($text, $description, 3, '', '', $i);
  275. // Show range
  276. print_date_range($objp->date_start, $objp->date_end);
  277. // Add description in form
  278. if (!empty($conf->global->PRODUIT_DESC_IN_FORM))
  279. {
  280. print (!empty($objp->description) && $objp->description != $objp->product) ? '<br>'.dol_htmlentitiesbr($objp->description) : '';
  281. }
  282. print '</td>';
  283. } else {
  284. print "<td>";
  285. if ($objp->fk_product_type == 1) $text = img_object($langs->trans('Service'), 'service');
  286. else $text = img_object($langs->trans('Product'), 'product');
  287. if (!empty($objp->label)) {
  288. $text .= ' <strong>'.$objp->label.'</strong>';
  289. print $form->textwithtooltip($text, $objp->description, 3, '', '', $i);
  290. } else {
  291. print $text.' '.nl2br($objp->description);
  292. }
  293. // Show range
  294. print_date_range($objp->date_start, $objp->date_end);
  295. print "</td>\n";
  296. }
  297. //print '<td class="center">'.$objp->qty_asked.'</td>';
  298. // Date creation
  299. print '<td class="nowrap center">'.dol_print_date($db->jdate($objp->date_creation), 'day').'</td>';
  300. // Date shipping creation
  301. print '<td class="nowrap center">'.dol_print_date($db->jdate($objp->date_delivery), 'day').'</td>';
  302. // Qty shipped
  303. print '<td class="center">'.$objp->qty_shipped.'</td>';
  304. // Warehouse
  305. if (!empty($conf->stock->enabled))
  306. {
  307. print '<td>';
  308. if ($objp->warehouse_id > 0)
  309. {
  310. $warehousestatic->fetch($objp->warehouse_id);
  311. print $warehousestatic->getNomUrl(1);
  312. }
  313. print '</td>';
  314. }
  315. // Batch number managment
  316. /*TODO Add link to expeditiondet_batch
  317. if (! empty($conf->productbatch->enabled))
  318. {
  319. var_dump($objp->edrowid);
  320. $lines[$i]->detail_batch
  321. if (isset($lines[$i]->detail_batch))
  322. {
  323. print '<td>';
  324. if ($lines[$i]->product_tobatch)
  325. {
  326. $detail = '';
  327. foreach ($lines[$i]->detail_batch as $dbatch)
  328. {
  329. $detail.= $langs->trans("Batch").': '.$dbatch->batch;
  330. $detail.= ' - '.$langs->trans("SellByDate").': '.dol_print_date($dbatch->sellby,"day");
  331. $detail.= ' - '.$langs->trans("EatByDate").': '.dol_print_date($dbatch->eatby,"day");
  332. $detail.= ' - '.$langs->trans("Qty").': '.$dbatch->qty;
  333. $detail.= '<br>';
  334. }
  335. print $form->textwithtooltip(img_picto('', 'object_barcode').' '.$langs->trans("DetailBatchNumber"),$detail);
  336. }
  337. else
  338. {
  339. print $langs->trans("NA");
  340. }
  341. print '</td>';
  342. } else {
  343. print '<td></td>';
  344. }
  345. }*/
  346. // Informations on receipt
  347. if (!empty($conf->delivery_note->enabled))
  348. {
  349. include_once DOL_DOCUMENT_ROOT.'/delivery/class/delivery.class.php';
  350. $expedition->id = $objp->sendingid;
  351. $expedition->fetchObjectLinked($expedition->id, $expedition->element);
  352. //var_dump($expedition->linkedObjects);
  353. $receiving = '';
  354. if (!empty($expedition->linkedObjects['delivery'])) $receiving = reset($expedition->linkedObjects['delivery']); // Take first link
  355. if (!empty($receiving))
  356. {
  357. // $expedition->fk_origin_line = id of det line of order
  358. // $receiving->fk_origin_line = id of det line of order
  359. // $receiving->origin may be 'shipping'
  360. // $receiving->origin_id may be id of shipping
  361. // Ref
  362. print '<td>';
  363. print $receiving->getNomUrl($db);
  364. //print '<a href="'.DOL_URL_ROOT.'/delivery/card.php?id='.$livraison_id.'">'.img_object($langs->trans("ShowReceiving"),'sending').' '.$objp->livraison_ref.'<a>';
  365. print '</td>';
  366. // Qty received
  367. //print '<td class="center">';
  368. // TODO No solution for the moment to link a line det of receipt with a line det of shipping,
  369. // so no way to know the qty received for this line of shipping.
  370. //print $langs->trans("FeatureNotYetAvailable");
  371. //print '</td>';
  372. // Date shipping real
  373. print '<td class="right">';
  374. print dol_print_date($receiving->date_delivery, 'day');
  375. print '</td>';
  376. } else {
  377. //print '<td>&nbsp;</td>';
  378. print '<td>&nbsp;</td>';
  379. print '<td>&nbsp;</td>';
  380. }
  381. }
  382. print '</tr>';
  383. $i++;
  384. }
  385. print '</table>';
  386. }
  387. $db->free($resql);
  388. } else {
  389. dol_print_error($db);
  390. }
  391. return 1;
  392. }