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