product.lib.php 27 KB

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