product.lib.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. <?php
  2. /* Copyright (C) 2006-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2009-2010 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  6. * Copyright (C) 2015-2016 Marcos García <marcosgdf@gmail.com>
  7. * Copyright (C) 2023 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. * or see https://www.gnu.org/
  22. */
  23. /**
  24. * \file htdocs/core/lib/product.lib.php
  25. * \brief Ensemble de fonctions de base pour le module produit et service
  26. * \ingroup product
  27. */
  28. /**
  29. * Prepare array with list of tabs
  30. *
  31. * @param Product $object Object related to tabs
  32. * @return array Array of tabs to show
  33. */
  34. function product_prepare_head($object)
  35. {
  36. global $db, $langs, $conf, $user;
  37. $langs->load("products");
  38. $label = $langs->trans('Product');
  39. $usercancreadprice = getDolGlobalString('MAIN_USE_ADVANCED_PERMS') ? $user->hasRight('product', 'product_advance', 'read_prices') : $user->hasRight('product', 'read');
  40. if ($object->isService()) {
  41. $label = $langs->trans('Service');
  42. $usercancreadprice = getDolGlobalString('MAIN_USE_ADVANCED_PERMS') ? $user->hasRight('service', 'service_advance', 'read_prices') : $user->hasRight('service', 'read');
  43. }
  44. $h = 0;
  45. $head = array();
  46. $head[$h][0] = DOL_URL_ROOT."/product/card.php?id=".$object->id;
  47. $head[$h][1] = $label;
  48. $head[$h][2] = 'card';
  49. $h++;
  50. // if (!empty($object->status)) {
  51. if ($usercancreadprice) {
  52. $head[$h][0] = DOL_URL_ROOT."/product/price.php?id=".$object->id;
  53. $head[$h][1] = $langs->trans("SellingPrices");
  54. $head[$h][2] = 'price';
  55. $h++;
  56. } else {
  57. $head[$h][0] = '#';
  58. $head[$h][1] = $langs->trans("SellingPrices");
  59. $head[$h][2] = 'price';
  60. $head[$h][5] = 'disabled';
  61. $h++;
  62. }
  63. // }
  64. // if (!empty($object->status_buy) || (isModEnabled('margin') && !empty($object->status))) { // If margin is on and product on sell, we may need the cost price even if product os not on purchase
  65. if ((isModEnabled("supplier_proposal") || isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) && ($user->hasRight('fournisseur', 'lire') || $user->hasRight('supplier_order', 'read') || $user->hasRight('supplier_invoice', 'read'))
  66. || (isModEnabled('margin') && $user->hasRight("margin", "liretous"))
  67. ) {
  68. if ($usercancreadprice) {
  69. $head[$h][0] = DOL_URL_ROOT."/product/fournisseurs.php?id=".$object->id;
  70. $head[$h][1] = $langs->trans("BuyingPrices");
  71. $head[$h][2] = 'suppliers';
  72. $h++;
  73. } else {
  74. $head[$h][0] = '#';
  75. $head[$h][1] = $langs->trans("BuyingPrices");
  76. $head[$h][2] = 'suppliers';
  77. $head[$h][5] = 'disabled';
  78. $h++;
  79. }
  80. }
  81. // }
  82. // Multilangs
  83. if (getDolGlobalInt('MAIN_MULTILANGS')) {
  84. $head[$h][0] = DOL_URL_ROOT."/product/traduction.php?id=".$object->id;
  85. $head[$h][1] = $langs->trans("Translations");
  86. $head[$h][2] = 'translation';
  87. $h++;
  88. }
  89. // Sub products
  90. if (getDolGlobalString('PRODUIT_SOUSPRODUITS')) {
  91. $head[$h][0] = DOL_URL_ROOT."/product/composition/card.php?id=".$object->id;
  92. $head[$h][1] = $langs->trans('AssociatedProducts');
  93. $nbFatherAndChild = $object->hasFatherOrChild();
  94. if ($nbFatherAndChild > 0) {
  95. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbFatherAndChild.'</span>';
  96. }
  97. $head[$h][2] = 'subproduct';
  98. $h++;
  99. }
  100. if (isModEnabled('variants') && ($object->isProduct() || $object->isService())) {
  101. global $db;
  102. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
  103. $prodcomb = new ProductCombination($db);
  104. if ($prodcomb->fetchByFkProductChild($object->id) <= 0) {
  105. $head[$h][0] = DOL_URL_ROOT."/variants/combinations.php?id=".$object->id;
  106. $head[$h][1] = $langs->trans('ProductCombinations');
  107. $head[$h][2] = 'combinations';
  108. $nbVariant = $prodcomb->countNbOfCombinationForFkProductParent($object->id);
  109. if ($nbVariant > 0) {
  110. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbVariant.'</span>';
  111. }
  112. }
  113. $h++;
  114. }
  115. if ($object->isProduct() || ($object->isService() && getDolGlobalString('STOCK_SUPPORTS_SERVICES'))) { // If physical product we can stock (or service with option)
  116. if (isModEnabled('stock') && $user->hasRight('stock', 'lire')) {
  117. $head[$h][0] = DOL_URL_ROOT."/product/stock/product.php?id=".$object->id;
  118. $head[$h][1] = $langs->trans("Stock");
  119. $head[$h][2] = 'stock';
  120. $h++;
  121. }
  122. }
  123. // Tab to link resources
  124. if (isModEnabled('resource')) {
  125. if ($object->isProduct() && getDolGlobalString('RESOURCE_ON_PRODUCTS')) {
  126. $head[$h][0] = DOL_URL_ROOT.'/resource/element_resource.php?element=product&ref='.$object->ref;
  127. $head[$h][1] = $langs->trans("Resources");
  128. $head[$h][2] = 'resources';
  129. $h++;
  130. }
  131. if ($object->isService() && getDolGlobalString('RESOURCE_ON_SERVICES')) {
  132. $head[$h][0] = DOL_URL_ROOT.'/resource/element_resource.php?element=service&ref='.$object->ref;
  133. $head[$h][1] = $langs->trans("Resources");
  134. $head[$h][2] = 'resources';
  135. $h++;
  136. }
  137. }
  138. $head[$h][0] = DOL_URL_ROOT."/product/stats/facture.php?showmessage=1&id=".$object->id;
  139. $head[$h][1] = $langs->trans('Referers');
  140. $head[$h][2] = 'referers';
  141. $h++;
  142. $head[$h][0] = DOL_URL_ROOT."/product/stats/card.php?id=".$object->id;
  143. $head[$h][1] = $langs->trans('Statistics');
  144. $head[$h][2] = 'stats';
  145. $h++;
  146. // Show more tabs from modules
  147. // Entries must be declared in modules descriptor with line
  148. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  149. // $this->tabs = array('entity:-tabname); to remove a tab
  150. complete_head_from_modules($conf, $langs, $object, $head, $h, 'product', 'add', 'core');
  151. // Notes
  152. if (!getDolGlobalString('MAIN_DISABLE_NOTES_TAB')) {
  153. $nbNote = 0;
  154. if (!empty($object->note_private)) {
  155. $nbNote++;
  156. }
  157. if (!empty($object->note_public)) {
  158. $nbNote++;
  159. }
  160. $head[$h][0] = DOL_URL_ROOT.'/product/note.php?id='.$object->id;
  161. $head[$h][1] = $langs->trans('Notes');
  162. if ($nbNote > 0) {
  163. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  164. }
  165. $head[$h][2] = 'note';
  166. $h++;
  167. }
  168. // Attachments
  169. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  170. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  171. if (isModEnabled("product") && ($object->type == Product::TYPE_PRODUCT)) {
  172. $upload_dir = $conf->product->multidir_output[$object->entity].'/'.dol_sanitizeFileName($object->ref);
  173. }
  174. if (isModEnabled("service") && ($object->type == Product::TYPE_SERVICE)) {
  175. $upload_dir = $conf->service->multidir_output[$object->entity].'/'.dol_sanitizeFileName($object->ref);
  176. }
  177. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  178. if (getDolGlobalInt('PRODUCT_USE_OLD_PATH_FOR_PHOTO')) {
  179. if (isModEnabled("product") && ($object->type == Product::TYPE_PRODUCT)) {
  180. $upload_dir = $conf->product->multidir_output[$object->entity].'/'.get_exdir($object->id, 2, 0, 0, $object, 'product').$object->id.'/photos';
  181. }
  182. if (isModEnabled("service") && ($object->type == Product::TYPE_SERVICE)) {
  183. $upload_dir = $conf->service->multidir_output[$object->entity].'/'.get_exdir($object->id, 2, 0, 0, $object, 'product').$object->id.'/photos';
  184. }
  185. $nbFiles += count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  186. }
  187. $nbLinks = Link::count($db, $object->element, $object->id);
  188. $head[$h][0] = DOL_URL_ROOT.'/product/document.php?id='.$object->id;
  189. $head[$h][1] = $langs->trans('Documents');
  190. if (($nbFiles + $nbLinks) > 0) {
  191. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  192. }
  193. $head[$h][2] = 'documents';
  194. $h++;
  195. // Log
  196. $head[$h][0] = DOL_URL_ROOT.'/product/agenda.php?id='.$object->id;
  197. $head[$h][1] = $langs->trans("Events");
  198. if (isModEnabled('agenda') && ($user->hasRight('agenda', 'myactions', 'read') || $user->hasRight('agenda', 'allactions', 'read'))) {
  199. $head[$h][1] .= '/';
  200. $head[$h][1] .= $langs->trans("Agenda");
  201. }
  202. $head[$h][2] = 'agenda';
  203. $h++;
  204. complete_head_from_modules($conf, $langs, $object, $head, $h, 'product', 'add', 'external');
  205. complete_head_from_modules($conf, $langs, $object, $head, $h, 'product', 'remove');
  206. return $head;
  207. }
  208. /**
  209. * Prepare array with list of tabs
  210. *
  211. * @param ProductLot $object Object related to tabs
  212. * @return array Array of tabs to show
  213. */
  214. function productlot_prepare_head($object)
  215. {
  216. global $db, $langs, $conf, $user;
  217. // Load translation files required by the page
  218. $langs->loadLangs(array("products", "productbatch"));
  219. $h = 0;
  220. $head = array();
  221. $head[$h][0] = DOL_URL_ROOT."/product/stock/productlot_card.php?id=".$object->id;
  222. $head[$h][1] = $langs->trans("Lot");
  223. $head[$h][2] = 'card';
  224. $h++;
  225. $head[$h][0] = DOL_URL_ROOT."/product/stock/stats/expedition.php?showmessage=1&id=".$object->id;
  226. $head[$h][1] = $langs->trans('Referers');
  227. $head[$h][2] = 'referers';
  228. $h++;
  229. // Attachments
  230. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  231. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  232. $upload_dir = $conf->productbatch->multidir_output[$object->entity].'/'.dol_sanitizeFileName($object->ref);
  233. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  234. $nbLinks = Link::count($db, $object->element, $object->id);
  235. $head[$h][0] = DOL_URL_ROOT."/product/stock/productlot_document.php?id=".$object->id;
  236. $head[$h][1] = $langs->trans("Documents");
  237. if (($nbFiles + $nbLinks) > 0) {
  238. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  239. }
  240. $head[$h][2] = 'documents';
  241. $h++;
  242. // Notes
  243. if (!getDolGlobalString('MAIN_DISABLE_NOTES_TAB')) {
  244. $nbNote = 0;
  245. if (!empty($object->note_private)) {
  246. $nbNote++;
  247. }
  248. if (!empty($object->note_public)) {
  249. $nbNote++;
  250. }
  251. $head[$h][0] = DOL_URL_ROOT .'/product/stock/productlot_note.php?id=' . $object->id;
  252. $head[$h][1] = $langs->trans('Notes');
  253. if ($nbNote > 0) {
  254. $head[$h][1] .= '<span class="badge marginleftonlyshort">' . $nbNote . '</span>';
  255. }
  256. $head[$h][2] = 'note';
  257. $h++;
  258. }
  259. // Show more tabs from modules
  260. // Entries must be declared in modules descriptor with line
  261. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  262. // $this->tabs = array('entity:-tabname); to remove a tab
  263. complete_head_from_modules($conf, $langs, $object, $head, $h, 'productlot');
  264. complete_head_from_modules($conf, $langs, $object, $head, $h, 'productlot', 'remove');
  265. // Log
  266. /*
  267. $head[$h][0] = DOL_URL_ROOT.'/product/info.php?id='.$object->id;
  268. $head[$h][1] = $langs->trans("Info");
  269. $head[$h][2] = 'info';
  270. $h++;
  271. */
  272. return $head;
  273. }
  274. /**
  275. * Return array head with list of tabs to view object informations.
  276. *
  277. * @return array head array with tabs
  278. */
  279. function product_admin_prepare_head()
  280. {
  281. global $langs, $conf, $user, $db;
  282. $extrafields = new ExtraFields($db);
  283. $extrafields->fetch_name_optionals_label('product');
  284. $extrafields->fetch_name_optionals_label('product_fournisseur_price');
  285. $h = 0;
  286. $head = array();
  287. $head[$h][0] = DOL_URL_ROOT."/product/admin/product.php";
  288. $head[$h][1] = $langs->trans('Parameters');
  289. $head[$h][2] = 'general';
  290. $h++;
  291. if (getDolGlobalString('PRODUIT_MULTIPRICES') && getDolGlobalString('PRODUIT_MULTIPRICES_ALLOW_AUTOCALC_PRICELEVEL')) {
  292. $head[$h] = array(
  293. 0 => DOL_URL_ROOT."/product/admin/price_rules.php",
  294. 1 => $langs->trans('MultipriceRules'),
  295. 2 => 'generator'
  296. );
  297. $h++;
  298. }
  299. // Show more tabs from modules
  300. // Entries must be declared in modules descriptor with line
  301. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  302. // $this->tabs = array('entity:-tabname); to remove a tab
  303. complete_head_from_modules($conf, $langs, null, $head, $h, 'product_admin');
  304. $head[$h][0] = DOL_URL_ROOT.'/product/admin/product_extrafields.php';
  305. $head[$h][1] = $langs->trans("ExtraFields");
  306. $nbExtrafields = $extrafields->attributes['product']['count'];
  307. if ($nbExtrafields > 0) {
  308. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
  309. }
  310. $head[$h][2] = 'attributes';
  311. $h++;
  312. $head[$h][0] = DOL_URL_ROOT.'/product/admin/product_supplier_extrafields.php';
  313. $head[$h][1] = $langs->trans("ProductSupplierExtraFields");
  314. $nbExtrafields = $extrafields->attributes['product_fournisseur_price']['count'];
  315. if ($nbExtrafields > 0) {
  316. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
  317. }
  318. $head[$h][2] = 'supplierAttributes';
  319. $h++;
  320. complete_head_from_modules($conf, $langs, null, $head, $h, 'product_admin', 'remove');
  321. return $head;
  322. }
  323. /**
  324. * Return array head with list of tabs to view object informations.
  325. *
  326. * @return array head array with tabs
  327. */
  328. function product_lot_admin_prepare_head()
  329. {
  330. global $langs, $conf, $user, $db;
  331. $extrafields = new ExtraFields($db);
  332. $extrafields->fetch_name_optionals_label('product_lot');
  333. $h = 0;
  334. $head = array();
  335. $head[$h][0] = DOL_URL_ROOT."/product/admin/product_lot.php";
  336. $head[$h][1] = $langs->trans('Parameters');
  337. $head[$h][2] = 'settings';
  338. $h++;
  339. // Show more tabs from modules
  340. // Entries must be declared in modules descriptor with line
  341. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  342. // $this->tabs = array('entity:-tabname); to remove a tab
  343. complete_head_from_modules($conf, $langs, null, $head, $h, 'product_lot_admin');
  344. $head[$h][0] = DOL_URL_ROOT.'/product/admin/product_lot_extrafields.php';
  345. $head[$h][1] = $langs->trans("ExtraFields");
  346. $nbExtrafields = $extrafields->attributes['product_lot']['count'];
  347. if ($nbExtrafields > 0) {
  348. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
  349. }
  350. $head[$h][2] = 'attributes';
  351. $h++;
  352. complete_head_from_modules($conf, $langs, null, $head, $h, 'product_lot_admin', 'remove');
  353. return $head;
  354. }
  355. /**
  356. * Show stats for company
  357. *
  358. * @param Product $product Product object
  359. * @param int $socid Thirdparty id
  360. * @return integer NB of lines shown into array
  361. */
  362. function show_stats_for_company($product, $socid)
  363. {
  364. global $conf, $langs, $user, $db, $hookmanager;
  365. $form = new Form($db);
  366. $nblines = 0;
  367. print '<tr class="liste_titre">';
  368. print '<td class="left" width="25%">'.$langs->trans("Referers").'</td>';
  369. print '<td class="right" width="25%">'.$langs->trans("NbOfThirdParties").'</td>';
  370. print '<td class="right" width="25%">'.$langs->trans("NbOfObjectReferers").'</td>';
  371. print '<td class="right" width="25%">'.$langs->trans("TotalQuantity").'</td>';
  372. print '</tr>';
  373. // Customer proposals
  374. if (isModEnabled("propal") && $user->hasRight('propal', 'lire')) {
  375. $nblines++;
  376. $ret = $product->load_stats_propale($socid);
  377. if ($ret < 0) {
  378. dol_print_error($db);
  379. }
  380. $langs->load("propal");
  381. print '<tr><td>';
  382. print '<a href="propal.php?id='.$product->id.'">'.img_object('', 'propal', 'class="pictofixedwidth"').$langs->trans("Proposals").'</a>';
  383. print '</td><td class="right">';
  384. print $product->stats_propale['customers'];
  385. print '</td><td class="right">';
  386. print $product->stats_propale['nb'];
  387. print '</td><td class="right">';
  388. print $product->stats_propale['qty'];
  389. print '</td>';
  390. print '</tr>';
  391. }
  392. // Supplier proposals
  393. if (isModEnabled('supplier_proposal') && $user->hasRight('supplier_proposal', 'lire')) {
  394. $nblines++;
  395. $ret = $product->load_stats_proposal_supplier($socid);
  396. if ($ret < 0) {
  397. dol_print_error($db);
  398. }
  399. $langs->load("supplier_proposal");
  400. print '<tr><td>';
  401. print '<a href="supplier_proposal.php?id='.$product->id.'">'.img_object('', 'supplier_proposal', 'class="pictofixedwidth"').$langs->trans("SupplierProposals").'</a>';
  402. print '</td><td class="right">';
  403. print $product->stats_proposal_supplier['suppliers'];
  404. print '</td><td class="right">';
  405. print $product->stats_proposal_supplier['nb'];
  406. print '</td><td class="right">';
  407. print $product->stats_proposal_supplier['qty'];
  408. print '</td>';
  409. print '</tr>';
  410. }
  411. // Sales orders
  412. if (isModEnabled('commande') && $user->hasRight('commande', 'lire')) {
  413. $nblines++;
  414. $ret = $product->load_stats_commande($socid);
  415. if ($ret < 0) {
  416. dol_print_error($db);
  417. }
  418. $langs->load("orders");
  419. print '<tr><td>';
  420. print '<a href="commande.php?id='.$product->id.'">'.img_object('', 'order', 'class="pictofixedwidth"').$langs->trans("CustomersOrders").'</a>';
  421. print '</td><td class="right">';
  422. print $product->stats_commande['customers'];
  423. print '</td><td class="right">';
  424. print $product->stats_commande['nb'];
  425. print '</td><td class="right">';
  426. print $product->stats_commande['qty'];
  427. print '</td>';
  428. print '</tr>';
  429. }
  430. // Supplier orders
  431. if ((isModEnabled("fournisseur") && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight('fournisseur', 'commande', 'lire')) || (isModEnabled("supplier_order") && $user->hasRight('supplier_order', 'lire'))) {
  432. $nblines++;
  433. $ret = $product->load_stats_commande_fournisseur($socid);
  434. if ($ret < 0) {
  435. dol_print_error($db);
  436. }
  437. $langs->load("orders");
  438. print '<tr><td>';
  439. print '<a href="commande_fournisseur.php?id='.$product->id.'">'.img_object('', 'supplier_order', 'class="pictofixedwidth"').$langs->trans("SuppliersOrders").'</a>';
  440. print '</td><td class="right">';
  441. print $product->stats_commande_fournisseur['suppliers'];
  442. print '</td><td class="right">';
  443. print $product->stats_commande_fournisseur['nb'];
  444. print '</td><td class="right">';
  445. print $product->stats_commande_fournisseur['qty'];
  446. print '</td>';
  447. print '</tr>';
  448. }
  449. // Customer invoices
  450. if (isModEnabled('facture') && $user->hasRight('facture', 'lire')) {
  451. $nblines++;
  452. $ret = $product->load_stats_facture($socid);
  453. if ($ret < 0) {
  454. dol_print_error($db);
  455. }
  456. $langs->load("bills");
  457. print '<tr><td>';
  458. print '<a href="facture.php?id='.$product->id.'">'.img_object('', 'bill', 'class="pictofixedwidth"').$langs->trans("CustomersInvoices").'</a>';
  459. print '</td><td class="right">';
  460. print $product->stats_facture['customers'];
  461. print '</td><td class="right">';
  462. print $product->stats_facture['nb'];
  463. print '</td><td class="right">';
  464. print $product->stats_facture['qty'];
  465. print '</td>';
  466. print '</tr>';
  467. }
  468. // Customer template invoices
  469. if (isModEnabled("facture") && $user->hasRight('facture', 'lire')) {
  470. $nblines++;
  471. $ret = $product->load_stats_facturerec($socid);
  472. if ($ret < 0) {
  473. dol_print_error($db);
  474. }
  475. $langs->load("bills");
  476. print '<tr><td>';
  477. print '<a href="facturerec.php?id='.$product->id.'">'.img_object('', 'bill', 'class="pictofixedwidth"').$langs->trans("RecurringInvoiceTemplate").'</a>';
  478. print '</td><td class="right">';
  479. print $product->stats_facture['customers'];
  480. print '</td><td class="right">';
  481. print $product->stats_facturerec['nb'];
  482. print '</td><td class="right">';
  483. print $product->stats_facturerec['qty'];
  484. print '</td>';
  485. print '</tr>';
  486. }
  487. // Supplier invoices
  488. if ((isModEnabled("fournisseur") && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight('fournisseur', 'facture', 'lire')) || (isModEnabled("supplier_invoice") && $user->hasRight('supplier_invoice', 'lire'))) {
  489. $nblines++;
  490. $ret = $product->load_stats_facture_fournisseur($socid);
  491. if ($ret < 0) {
  492. dol_print_error($db);
  493. }
  494. $langs->load("bills");
  495. print '<tr><td>';
  496. print '<a href="facture_fournisseur.php?id='.$product->id.'">'.img_object('', 'supplier_invoice', 'class="pictofixedwidth"').$langs->trans("SuppliersInvoices").'</a>';
  497. print '</td><td class="right">';
  498. print $product->stats_facture_fournisseur['suppliers'];
  499. print '</td><td class="right">';
  500. print $product->stats_facture_fournisseur['nb'];
  501. print '</td><td class="right">';
  502. print $product->stats_facture_fournisseur['qty'];
  503. print '</td>';
  504. print '</tr>';
  505. }
  506. // Contracts
  507. if (isModEnabled('contrat') && $user->hasRight('contrat', 'lire')) {
  508. $nblines++;
  509. $ret = $product->load_stats_contrat($socid);
  510. if ($ret < 0) {
  511. dol_print_error($db);
  512. }
  513. $langs->load("contracts");
  514. print '<tr><td>';
  515. print '<a href="contrat.php?id='.$product->id.'">'.img_object('', 'contract', 'class="pictofixedwidth"').$langs->trans("Contracts").'</a>';
  516. print '</td><td class="right">';
  517. print $product->stats_contrat['customers'];
  518. print '</td><td class="right">';
  519. print $product->stats_contrat['nb'];
  520. print '</td><td class="right">';
  521. print $product->stats_contrat['qty'];
  522. print '</td>';
  523. print '</tr>';
  524. }
  525. // BOM
  526. if (isModEnabled('bom') && $user->hasRight('bom', 'read')) {
  527. $nblines++;
  528. $ret = $product->load_stats_bom($socid);
  529. if ($ret < 0) {
  530. setEventMessage($product->error, 'errors');
  531. }
  532. $langs->load("mrp");
  533. print '<tr><td>';
  534. print '<a href="bom.php?id='.$product->id.'">'.img_object('', 'bom', 'class="pictofixedwidth"').$langs->trans("BOM").'</a>';
  535. print '</td><td class="right">';
  536. print '</td><td class="right">';
  537. print $form->textwithpicto($product->stats_bom['nb_toconsume'], $langs->trans("RowMaterial"));
  538. print $form->textwithpicto($product->stats_bom['nb_toproduce'], $langs->trans("Finished"));
  539. print '</td><td class="right">';
  540. print $form->textwithpicto($product->stats_bom['qty_toconsume'], $langs->trans("RowMaterial"));
  541. print $form->textwithpicto($product->stats_bom['qty_toproduce'], $langs->trans("Finished"));
  542. print '</td>';
  543. print '</tr>';
  544. }
  545. // MO
  546. if (isModEnabled('mrp') && $user->hasRight('mrp', 'read')) {
  547. $nblines++;
  548. $ret = $product->load_stats_mo($socid);
  549. if ($ret < 0) {
  550. setEventMessages($product->error, $product->errors, 'errors');
  551. }
  552. $langs->load("mrp");
  553. print '<tr><td>';
  554. print '<a href="mo.php?id='.$product->id.'">'.img_object('', 'mrp', 'class="pictofixedwidth"').$langs->trans("MO").'</a>';
  555. print '</td><td class="right">';
  556. print $form->textwithpicto($product->stats_mo['customers_toconsume'], $langs->trans("ToConsume"));
  557. print $form->textwithpicto($product->stats_mo['customers_consumed'], $langs->trans("QtyAlreadyConsumed"));
  558. print $form->textwithpicto($product->stats_mo['customers_toproduce'], $langs->trans("QtyToProduce"));
  559. print $form->textwithpicto($product->stats_mo['customers_produced'], $langs->trans("QtyAlreadyProduced"));
  560. print '</td><td class="right">';
  561. print $form->textwithpicto($product->stats_mo['nb_toconsume'], $langs->trans("ToConsume"));
  562. print $form->textwithpicto($product->stats_mo['nb_consumed'], $langs->trans("QtyAlreadyConsumed"));
  563. print $form->textwithpicto($product->stats_mo['nb_toproduce'], $langs->trans("QtyToProduce"));
  564. print $form->textwithpicto($product->stats_mo['nb_produced'], $langs->trans("QtyAlreadyProduced"));
  565. print '</td><td class="right">';
  566. print $form->textwithpicto($product->stats_mo['qty_toconsume'], $langs->trans("ToConsume"));
  567. print $form->textwithpicto($product->stats_mo['qty_consumed'], $langs->trans("QtyAlreadyConsumed"));
  568. print $form->textwithpicto($product->stats_mo['qty_toproduce'], $langs->trans("QtyToProduce"));
  569. print $form->textwithpicto($product->stats_mo['qty_produced'], $langs->trans("QtyAlreadyProduced"));
  570. print '</td>';
  571. print '</tr>';
  572. }
  573. $parameters = array('socid'=>$socid);
  574. $reshook = $hookmanager->executeHooks('addMoreProductStat', $parameters, $product, $nblines); // Note that $action and $object may have been modified by some hooks
  575. if ($reshook < 0) {
  576. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  577. }
  578. print $hookmanager->resPrint;
  579. return $nblines++;
  580. }
  581. /**
  582. * Show stats for product batch
  583. *
  584. * @param Productlot $batch Product batch object
  585. * @param int $socid Thirdparty id
  586. * @return integer NB of lines shown into array
  587. */
  588. function show_stats_for_batch($batch, $socid)
  589. {
  590. global $conf, $langs, $user, $db, $hookmanager;
  591. $langs->LoadLangs(array('sendings', 'orders', 'receptions'));
  592. $form = new Form($db);
  593. $nblines = 0;
  594. print '<tr class="liste_titre">';
  595. print '<td class="left" width="25%">'.$langs->trans("Referers").'</td>';
  596. print '<td class="right" width="25%">'.$langs->trans("NbOfThirdParties").'</td>';
  597. print '<td class="right" width="25%">'.$langs->trans("NbOfObjectReferers").'</td>';
  598. print '<td class="right" width="25%">'.$langs->trans("TotalQuantity").'</td>';
  599. print '</tr>';
  600. // Expeditions
  601. if (isModEnabled('expedition') && $user->hasRight('expedition', 'lire')) {
  602. $nblines++;
  603. $ret = $batch->loadStatsExpedition($socid);
  604. if ($ret < 0) {
  605. dol_print_error($db);
  606. }
  607. $langs->load("bills");
  608. print '<tr><td>';
  609. print '<a href="'.dol_buildpath('/product/stock/stats/expedition.php', 1).'?id='.$batch->id.'">'.img_object('', 'bill', 'class="pictofixedwidth"').$langs->trans("Shipments").'</a>';
  610. print '</td><td class="right">';
  611. print $batch->stats_expedition['customers'];
  612. print '</td><td class="right">';
  613. print $batch->stats_expedition['nb'];
  614. print '</td><td class="right">';
  615. print $batch->stats_expedition['qty'];
  616. print '</td>';
  617. print '</tr>';
  618. }
  619. if (isModEnabled("reception") && $user->hasRight('reception', 'lire')) {
  620. $nblines++;
  621. $ret = $batch->loadStatsReception($socid);
  622. if ($ret < 0) {
  623. dol_print_error($db);
  624. }
  625. $langs->load("bills");
  626. print '<tr><td>';
  627. print '<a href="'.dol_buildpath('/product/stock/stats/reception.php', 1).'?id='.$batch->id.'">'.img_object('', 'bill', 'class="pictofixedwidth"').$langs->trans("Receptions").'</a>';
  628. print '</td><td class="right">';
  629. print $batch->stats_reception['customers'];
  630. print '</td><td class="right">';
  631. print $batch->stats_reception['nb'];
  632. print '</td><td class="right">';
  633. print $batch->stats_reception['qty'];
  634. print '</td>';
  635. print '</tr>';
  636. } elseif (isModEnabled('supplier_order') && $user->hasRight('fournisseur', 'commande', 'lire')) {
  637. $nblines++;
  638. $ret = $batch->loadStatsSupplierOrder($socid);
  639. if ($ret < 0) {
  640. dol_print_error($db);
  641. }
  642. $langs->load("bills");
  643. print '<tr><td>';
  644. print '<a href="'.dol_buildpath('/product/stock/stats/commande_fournisseur.php', 1).'?id='.$batch->id.'">'.img_object('', 'bill', 'class="pictofixedwidth"').$langs->trans("SuppliersOrders").'</a>';
  645. print '</td><td class="right">';
  646. print $batch->stats_supplier_order['customers'];
  647. print '</td><td class="right">';
  648. print $batch->stats_supplier_order['nb'];
  649. print '</td><td class="right">';
  650. print $batch->stats_supplier_order['qty'];
  651. print '</td>';
  652. print '</tr>';
  653. }
  654. if (isModEnabled('mrp') && $user->hasRight('mrp', 'read')) {
  655. $nblines++;
  656. $ret = $batch->loadStatsMo($socid);
  657. if ($ret < 0) {
  658. dol_print_error($db);
  659. }
  660. $langs->load("mrp");
  661. print '<tr><td>';
  662. print '<a href="'.dol_buildpath('/product/stock/stats/mo.php', 1).'?id='.$batch->id.'">'.img_object('', 'mrp', 'class="pictofixedwidth"').$langs->trans("MO").'</a>';
  663. print '</td><td class="right">';
  664. // print $form->textwithpicto($batch->stats_mo['customers_toconsume'], $langs->trans("ToConsume")); Makes no sense with batch, at this moment we don't know batch number
  665. print $form->textwithpicto($batch->stats_mo['customers_consumed'], $langs->trans("QtyAlreadyConsumed"));
  666. // print $form->textwithpicto($batch->stats_mo['customers_toproduce'], $langs->trans("QtyToProduce")); Makes no sense with batch, at this moment we don't know batch number
  667. print $form->textwithpicto($batch->stats_mo['customers_produced'], $langs->trans("QtyAlreadyProduced"));
  668. print '</td><td class="right">';
  669. // print $form->textwithpicto($batch->stats_mo['nb_toconsume'], $langs->trans("ToConsume")); Makes no sense with batch, at this moment we don't know batch number
  670. print $form->textwithpicto($batch->stats_mo['nb_consumed'], $langs->trans("QtyAlreadyConsumed"));
  671. // print $form->textwithpicto($batch->stats_mo['nb_toproduce'], $langs->trans("QtyToProduce")); Makes no sense with batch, at this moment we don't know batch number
  672. print $form->textwithpicto($batch->stats_mo['nb_produced'], $langs->trans("QtyAlreadyProduced"));
  673. print '</td><td class="right">';
  674. // print $form->textwithpicto($batch->stats_mo['qty_toconsume'], $langs->trans("ToConsume")); Makes no sense with batch, at this moment we don't know batch number
  675. print $form->textwithpicto($batch->stats_mo['qty_consumed'], $langs->trans("QtyAlreadyConsumed"));
  676. // print $form->textwithpicto($batch->stats_mo['qty_toproduce'], $langs->trans("QtyToProduce")); Makes no sense with batch, at this moment we don't know batch number
  677. print $form->textwithpicto($batch->stats_mo['qty_produced'], $langs->trans("QtyAlreadyProduced"));
  678. print '</td>';
  679. print '</tr>';
  680. }
  681. $parameters = array('socid'=>$socid);
  682. $reshook = $hookmanager->executeHooks('addMoreBatchProductStat', $parameters, $batch, $nblines); // Note that $action and $object may have been modified by some hooks
  683. if ($reshook < 0) {
  684. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  685. }
  686. print $hookmanager->resPrint;
  687. return $nblines++;
  688. }
  689. /**
  690. * Return translation label of a unit key.
  691. * Function kept for backward compatibility.
  692. *
  693. * @param string $scale Scale of unit: '0', '-3', '6', ...
  694. * @param string $measuring_style Style of unit: weight, volume,...
  695. * @param int $unit ID of unit (rowid in llx_c_units table)
  696. * @param int $use_short_label 1=Use short label ('g' instead of 'gram'). Short labels are not translated.
  697. * @param Translate $outputlangs Language object
  698. * @return string Unit string
  699. * @see measuringUnitString() formproduct->selectMeasuringUnits()
  700. */
  701. function measuring_units_string($scale = '', $measuring_style = '', $unit = 0, $use_short_label = 0, $outputlangs = null)
  702. {
  703. return measuringUnitString($unit, $measuring_style, $scale, $use_short_label, $outputlangs);
  704. }
  705. /**
  706. * Return translation label of a unit key
  707. *
  708. * @param int $unit ID of unit (rowid in llx_c_units table)
  709. * @param string $measuring_style Style of unit: 'weight', 'volume', ..., '' = 'net_measure' for option PRODUCT_ADD_NET_MEASURE
  710. * @param string $scale Scale of unit: '0', '-3', '6', ...
  711. * @param int $use_short_label 1=Use short label ('g' instead of 'gram'). Short labels are not translated.
  712. * @param Translate $outputlangs Language object
  713. * @return string Unit string
  714. * @see formproduct->selectMeasuringUnits()
  715. */
  716. function measuringUnitString($unit, $measuring_style = '', $scale = '', $use_short_label = 0, $outputlangs = null)
  717. {
  718. global $langs, $db;
  719. global $measuring_unit_cache;
  720. if (empty($outputlangs)) {
  721. $outputlangs = $langs;
  722. }
  723. if (empty($measuring_unit_cache[$unit.'_'.$measuring_style.'_'.$scale.'_'.$use_short_label])) {
  724. require_once DOL_DOCUMENT_ROOT.'/core/class/cunits.class.php';
  725. $measuringUnits = new CUnits($db);
  726. if ($measuring_style == '' && $scale == '') {
  727. $arrayforfilter = array(
  728. 't.rowid' => $unit,
  729. 't.active' => 1
  730. );
  731. } elseif ($scale !== '') {
  732. $arrayforfilter = array(
  733. 't.scale' => $scale,
  734. 't.unit_type' => $measuring_style,
  735. 't.active' => 1
  736. );
  737. } else {
  738. $arrayforfilter = array(
  739. 't.rowid' => $unit,
  740. 't.unit_type' => $measuring_style,
  741. 't.active' => 1
  742. );
  743. }
  744. $result = $measuringUnits->fetchAll('', '', 0, 0, $arrayforfilter);
  745. if ($result < 0) {
  746. return -1;
  747. } else {
  748. if (is_array($measuringUnits->records) && count($measuringUnits->records) > 0) {
  749. if ($use_short_label) {
  750. $labeltoreturn = $measuringUnits->records[key($measuringUnits->records)]->short_label;
  751. } else {
  752. $labeltoreturn = $outputlangs->transnoentitiesnoconv($measuringUnits->records[key($measuringUnits->records)]->label);
  753. }
  754. } else {
  755. $labeltoreturn = '';
  756. }
  757. $measuring_unit_cache[$unit.'_'.$measuring_style.'_'.$scale.'_'.$use_short_label] = $labeltoreturn;
  758. return $labeltoreturn;
  759. }
  760. } else {
  761. return $measuring_unit_cache[$unit.'_'.$measuring_style.'_'.$scale.'_'.$use_short_label];
  762. }
  763. }
  764. /**
  765. * Transform a given unit scale into the square of that unit, if known.
  766. *
  767. * @param int $unit Unit scale key (-3,-2,-1,0,98,99...)
  768. * @return int Squared unit key (-6,-4,-2,0,98,99...)
  769. * @see formproduct->selectMeasuringUnits
  770. */
  771. function measuring_units_squared($unit)
  772. {
  773. $measuring_units = array();
  774. $measuring_units[0] = 0; // m -> m3
  775. $measuring_units[-1] = -2; // dm-> dm2
  776. $measuring_units[-2] = -4; // cm -> cm2
  777. $measuring_units[-3] = -6; // mm -> mm2
  778. $measuring_units[98] = 98; // foot -> foot2
  779. $measuring_units[99] = 99; // inch -> inch2
  780. return $measuring_units[$unit];
  781. }
  782. /**
  783. * Transform a given unit scale into the cube of that unit, if known
  784. *
  785. * @param int $unit Unit scale key (-3,-2,-1,0,98,99...)
  786. * @return int Cubed unit key (-9,-6,-3,0,88,89...)
  787. * @see formproduct->selectMeasuringUnits
  788. */
  789. function measuring_units_cubed($unit)
  790. {
  791. $measuring_units = array();
  792. $measuring_units[0] = 0; // m -> m2
  793. $measuring_units[-1] = -3; // dm-> dm3
  794. $measuring_units[-2] = -6; // cm -> cm3
  795. $measuring_units[-3] = -9; // mm -> mm3
  796. $measuring_units[98] = 88; // foot -> foot3
  797. $measuring_units[99] = 89; // inch -> inch3
  798. return $measuring_units[$unit];
  799. }