sendings.lib.php 17 KB

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