sendings.lib.php 17 KB

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