dolibarr  7.0.0-beta
product.lib.php
Go to the documentation of this file.
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@capnetworks.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 <http://www.gnu.org/licenses/>.
20  * or see http://www.gnu.org/
21  */
22 
35 function product_prepare_head($object)
36 {
37  global $db, $langs, $conf, $user;
38  $langs->load("products");
39 
40  $h = 0;
41  $head = array();
42 
43  $head[$h][0] = DOL_URL_ROOT."/product/card.php?id=".$object->id;
44  $head[$h][1] = $langs->trans("Card");
45  $head[$h][2] = 'card';
46  $h++;
47 
48  if (! empty($object->status))
49  {
50  $head[$h][0] = DOL_URL_ROOT."/product/price.php?id=".$object->id;
51  $head[$h][1] = $langs->trans("SellingPrices");
52  $head[$h][2] = 'price';
53  $h++;
54  }
55 
56  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
57  {
58  if ((! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->lire)
59  || (! empty($conf->margin->enabled) && $user->rights->margin->liretous)
60  )
61  {
62  $head[$h][0] = DOL_URL_ROOT."/product/fournisseurs.php?id=".$object->id;
63  $head[$h][1] = $langs->trans("BuyingPrices");
64  $head[$h][2] = 'suppliers';
65  $h++;
66  }
67  }
68 
69  // Multilangs
70  if (! empty($conf->global->MAIN_MULTILANGS))
71  {
72  $head[$h][0] = DOL_URL_ROOT."/product/traduction.php?id=".$object->id;
73  $head[$h][1] = $langs->trans("Translation");
74  $head[$h][2] = 'translation';
75  $h++;
76  }
77 
78  // Sub products
79  if (! empty($conf->global->PRODUIT_SOUSPRODUITS))
80  {
81  $head[$h][0] = DOL_URL_ROOT."/product/composition/card.php?id=".$object->id;
82  $head[$h][1] = $langs->trans('AssociatedProducts');
83 
84  $nbFatherAndChild = $object->hasFatherOrChild();
85  if ($nbFatherAndChild > 0) $head[$h][1].= ' <span class="badge">'.$nbFatherAndChild.'</span>';
86  $head[$h][2] = 'subproduct';
87  $h++;
88  }
89 
90  $head[$h][0] = DOL_URL_ROOT."/product/stats/card.php?id=".$object->id;
91  $head[$h][1] = $langs->trans('Statistics');
92  $head[$h][2] = 'stats';
93  $h++;
94 
95  $head[$h][0] = DOL_URL_ROOT."/product/stats/facture.php?showmessage=1&id=".$object->id;
96  $head[$h][1] = $langs->trans('Referers');
97  $head[$h][2] = 'referers';
98  $h++;
99 
100  if (!empty($conf->variants->enabled) && $object->isProduct()) {
101 
102  global $db;
103 
104  require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
105 
106  $prodcomb = new ProductCombination($db);
107 
108  if ($prodcomb->fetchByFkProductChild($object->id) == -1)
109  {
110  $head[$h][0] = DOL_URL_ROOT."/variants/combinations.php?id=".$object->id;
111  $head[$h][1] = $langs->trans('ProductCombinations');
112  $head[$h][2] = 'combinations';
113  $nbVariant = $prodcomb->countNbOfCombinationForFkProductParent($object->id);
114  if ($nbVariant > 0) $head[$h][1].= ' <span class="badge">'.$nbVariant.'</span>';
115  }
116 
117  $h++;
118  }
119 
120  if ($object->isProduct() || ($object->isService() && ! empty($conf->global->STOCK_SUPPORTS_SERVICES))) // If physical product we can stock (or service with option)
121  {
122  if (! empty($conf->stock->enabled) && $user->rights->stock->lire)
123  {
124  $head[$h][0] = DOL_URL_ROOT."/product/stock/product.php?id=".$object->id;
125  $head[$h][1] = $langs->trans("Stock");
126  $head[$h][2] = 'stock';
127  $h++;
128  }
129  }
130 
131  // Show more tabs from modules
132  // Entries must be declared in modules descriptor with line
133  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
134  // $this->tabs = array('entity:-tabname); to remove a tab
135  complete_head_from_modules($conf,$langs,$object,$head,$h,'product');
136 
137  // Notes
138  if (empty($conf->global->MAIN_DISABLE_NOTES_TAB))
139  {
140  $nbNote = 0;
141  if(!empty($object->note_private)) $nbNote++;
142  if(!empty($object->note_public)) $nbNote++;
143  $head[$h][0] = DOL_URL_ROOT.'/product/note.php?id='.$object->id;
144  $head[$h][1] = $langs->trans('Notes');
145  if ($nbNote > 0) $head[$h][1].= ' <span class="badge">'.$nbNote.'</span>';
146  $head[$h][2] = 'note';
147  $h++;
148  }
149 
150  // Attachments
151  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
152  require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
153  if (! empty($conf->product->enabled) && ($object->type==Product::TYPE_PRODUCT)) $upload_dir = $conf->product->multidir_output[$object->entity].'/'.dol_sanitizeFileName($object->ref);
154  if (! empty($conf->service->enabled) && ($object->type==Product::TYPE_SERVICE)) $upload_dir = $conf->service->multidir_output[$object->entity].'/'.dol_sanitizeFileName($object->ref);
155  $nbFiles = count(dol_dir_list($upload_dir,'files',0,'','(\.meta|_preview.*\.png)$'));
156  if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) {
157  if (! empty($conf->product->enabled) && ($object->type==Product::TYPE_PRODUCT)) $upload_dir = $conf->produit->multidir_output[$object->entity].'/'.get_exdir($object->id,2,0,0,$object,'product').$object->id.'/photos';
158  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';
159  $nbFiles += count(dol_dir_list($upload_dir,'files',0,'','(\.meta|_preview.*\.png)$'));
160  }
161  $nbLinks=Link::count($db, $object->element, $object->id);
162  $head[$h][0] = DOL_URL_ROOT.'/product/document.php?id='.$object->id;
163  $head[$h][1] = $langs->trans('Documents');
164  if (($nbFiles+$nbLinks) > 0) $head[$h][1].= ' <span class="badge">'.($nbFiles+$nbLinks).'</span>';
165  $head[$h][2] = 'documents';
166  $h++;
167 
168  complete_head_from_modules($conf,$langs,$object,$head,$h,'product', 'remove');
169 
170  // Log
171  $head[$h][0] = DOL_URL_ROOT.'/product/agenda.php?id='.$object->id;
172  $head[$h][1] = $langs->trans("Events");
173  if (! empty($conf->agenda->enabled) && (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read) ))
174  {
175  $head[$h][1].= '/';
176  $head[$h][1].= $langs->trans("Agenda");
177  }
178  $head[$h][2] = 'agenda';
179  $h++;
180 
181  return $head;
182 }
183 
190 function productlot_prepare_head($object)
191 {
192  global $db, $langs, $conf, $user;
193  $langs->load("products");
194  $langs->load("productbatch");
195 
196  $h = 0;
197  $head = array();
198 
199  $head[$h][0] = DOL_URL_ROOT."/product/stock/productlot_card.php?id=".$object->id;
200  $head[$h][1] = $langs->trans("Card");
201  $head[$h][2] = 'card';
202  $h++;
203 
204  // Show more tabs from modules
205  // Entries must be declared in modules descriptor with line
206  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
207  // $this->tabs = array('entity:-tabname); to remove a tab
208  complete_head_from_modules($conf,$langs,$object,$head,$h,'productlot');
209 
210  complete_head_from_modules($conf,$langs,$object,$head,$h,'productlot', 'remove');
211 
212  // Log
213  /*
214  $head[$h][0] = DOL_URL_ROOT.'/product/info.php?id='.$object->id;
215  $head[$h][1] = $langs->trans("Info");
216  $head[$h][2] = 'info';
217  $h++;
218  */
219 
220  return $head;
221 }
222 
223 
224 
231 {
232  global $langs, $conf, $user;
233 
234  $h = 0;
235  $head = array();
236 
237  $head[$h][0] = DOL_URL_ROOT."/product/admin/product.php";
238  $head[$h][1] = $langs->trans('Parameters');
239  $head[$h][2] = 'general';
240  $h++;
241 
242  if (!empty($conf->global->PRODUIT_MULTIPRICES) && ! empty($conf->global->PRODUIT_MULTIPRICES_ALLOW_AUTOCALC_PRICELEVEL))
243  {
244  $head[$h] = array(
245  0 => DOL_URL_ROOT."/product/admin/price_rules.php",
246  1 => $langs->trans('MultipriceRules'),
247  2 => 'generator'
248  );
249  $h++;
250  }
251 
252  // Show more tabs from modules
253  // Entries must be declared in modules descriptor with line
254  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
255  // $this->tabs = array('entity:-tabname); to remove a tab
256  complete_head_from_modules($conf,$langs,null,$head,$h,'product_admin');
257 
258  $head[$h][0] = DOL_URL_ROOT.'/product/admin/product_extrafields.php';
259  $head[$h][1] = $langs->trans("ExtraFields");
260  $head[$h][2] = 'attributes';
261  $h++;
262 
263  complete_head_from_modules($conf,$langs,null,$head,$h,'product_admin','remove');
264 
265  return $head;
266 }
267 
268 
269 
276 {
277  global $langs, $conf, $user;
278 
279  $h = 0;
280  $head = array();
281 
282  // Show more tabs from modules
283  // Entries must be declared in modules descriptor with line
284  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
285  // $this->tabs = array('entity:-tabname); to remove a tab
286  complete_head_from_modules($conf,$langs,null,$head,$h,'product_lot_admin');
287 
288  $head[$h][0] = DOL_URL_ROOT.'/product/admin/product_lot_extrafields.php';
289  $head[$h][1] = $langs->trans("ExtraFields");
290  $head[$h][2] = 'attributes';
291  $h++;
292 
293  complete_head_from_modules($conf,$langs,null,$head,$h,'product_lot_admin','remove');
294 
295  return $head;
296 }
297 
298 
299 
307 function show_stats_for_company($product,$socid)
308 {
309  global $conf,$langs,$user,$db;
310 
311  $nblines = 0;
312 
313  print '<tr class="liste_titre">';
314  print '<td align="left" class="tdtop" width="25%">'.$langs->trans("Referers").'</td>';
315  print '<td align="right" width="25%">'.$langs->trans("NbOfThirdParties").'</td>';
316  print '<td align="right" width="25%">'.$langs->trans("NbOfObjectReferers").'</td>';
317  print '<td align="right" width="25%">'.$langs->trans("TotalQuantity").'</td>';
318  print '</tr>';
319 
320  // Customer proposals
321  if (! empty($conf->propal->enabled) && $user->rights->propale->lire)
322  {
323  $nblines++;
324  $ret=$product->load_stats_propale($socid);
325  if ($ret < 0) dol_print_error($db);
326  $langs->load("propal");
327  print '<tr><td>';
328  print '<a href="propal.php?id='.$product->id.'">'.img_object('','propal').' '.$langs->trans("Proposals").'</a>';
329  print '</td><td align="right">';
330  print $product->stats_propale['customers'];
331  print '</td><td align="right">';
332  print $product->stats_propale['nb'];
333  print '</td><td align="right">';
334  print $product->stats_propale['qty'];
335  print '</td>';
336  print '</tr>';
337  }
338  // Supplier proposals
339  if (! empty($conf->supplier_proposal->enabled) && $user->rights->supplier_proposal->lire)
340  {
341  $nblines++;
342  $ret=$product->load_stats_proposal_supplier($socid);
343  if ($ret < 0) dol_print_error($db);
344  $langs->load("propal");
345  print '<tr><td>';
346  print '<a href="supplier_proposal.php?id='.$product->id.'">'.img_object('','propal').' '.$langs->trans("SupplierProposals").'</a>';
347  print '</td><td align="right">';
348  print $product->stats_proposal_supplier['suppliers'];
349  print '</td><td align="right">';
350  print $product->stats_proposal_supplier['nb'];
351  print '</td><td align="right">';
352  print $product->stats_proposal_supplier['qty'];
353  print '</td>';
354  print '</tr>';
355  }
356  // Customer orders
357  if (! empty($conf->commande->enabled) && $user->rights->commande->lire)
358  {
359  $nblines++;
360  $ret=$product->load_stats_commande($socid);
361  if ($ret < 0) dol_print_error($db);
362  $langs->load("orders");
363  print '<tr><td>';
364  print '<a href="commande.php?id='.$product->id.'">'.img_object('','order').' '.$langs->trans("CustomersOrders").'</a>';
365  print '</td><td align="right">';
366  print $product->stats_commande['customers'];
367  print '</td><td align="right">';
368  print $product->stats_commande['nb'];
369  print '</td><td align="right">';
370  print $product->stats_commande['qty'];
371  print '</td>';
372  print '</tr>';
373  }
374  // Supplier orders
375  if (! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->commande->lire)
376  {
377  $nblines++;
378  $ret=$product->load_stats_commande_fournisseur($socid);
379  if ($ret < 0) dol_print_error($db);
380  $langs->load("orders");
381  print '<tr><td>';
382  print '<a href="commande_fournisseur.php?id='.$product->id.'">'.img_object('','order').' '.$langs->trans("SuppliersOrders").'</a>';
383  print '</td><td align="right">';
384  print $product->stats_commande_fournisseur['suppliers'];
385  print '</td><td align="right">';
386  print $product->stats_commande_fournisseur['nb'];
387  print '</td><td align="right">';
388  print $product->stats_commande_fournisseur['qty'];
389  print '</td>';
390  print '</tr>';
391  }
392  // Customer invoices
393  if (! empty($conf->facture->enabled) && $user->rights->facture->lire)
394  {
395  $nblines++;
396  $ret=$product->load_stats_facture($socid);
397  if ($ret < 0) dol_print_error($db);
398  $langs->load("bills");
399  print '<tr><td>';
400  print '<a href="facture.php?id='.$product->id.'">'.img_object('','bill').' '.$langs->trans("CustomersInvoices").'</a>';
401  print '</td><td align="right">';
402  print $product->stats_facture['customers'];
403  print '</td><td align="right">';
404  print $product->stats_facture['nb'];
405  print '</td><td align="right">';
406  print $product->stats_facture['qty'];
407  print '</td>';
408  print '</tr>';
409  }
410  // Supplier invoices
411  if (! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire)
412  {
413  $nblines++;
414  $ret=$product->load_stats_facture_fournisseur($socid);
415  if ($ret < 0) dol_print_error($db);
416  $langs->load("bills");
417  print '<tr><td>';
418  print '<a href="facture_fournisseur.php?id='.$product->id.'">'.img_object('','bill').' '.$langs->trans("SuppliersInvoices").'</a>';
419  print '</td><td align="right">';
420  print $product->stats_facture_fournisseur['suppliers'];
421  print '</td><td align="right">';
422  print $product->stats_facture_fournisseur['nb'];
423  print '</td><td align="right">';
424  print $product->stats_facture_fournisseur['qty'];
425  print '</td>';
426  print '</tr>';
427  }
428 
429  // Contracts
430  if (! empty($conf->contrat->enabled) && $user->rights->contrat->lire)
431  {
432  $nblines++;
433  $ret=$product->load_stats_contrat($socid);
434  if ($ret < 0) dol_print_error($db);
435  $langs->load("contracts");
436  print '<tr><td>';
437  print '<a href="contrat.php?id='.$product->id.'">'.img_object('','contract').' '.$langs->trans("Contracts").'</a>';
438  print '</td><td align="right">';
439  print $product->stats_contrat['customers'];
440  print '</td><td align="right">';
441  print $product->stats_contrat['nb'];
442  print '</td><td align="right">';
443  print $product->stats_contrat['qty'];
444  print '</td>';
445  print '</tr>';
446  }
447 
448  return $nblines++;
449 }
450 
451 
460 function measuring_units_string($unit,$measuring_style='')
461 {
462  global $langs;
463 
464  $measuring_units=array();
465  if ($measuring_style == 'weight')
466  {
467  $measuring_units[3] = $langs->transnoentitiesnoconv("WeightUnitton");
468  $measuring_units[0] = $langs->transnoentitiesnoconv("WeightUnitkg");
469  $measuring_units[-3] = $langs->transnoentitiesnoconv("WeightUnitg");
470  $measuring_units[-6] = $langs->transnoentitiesnoconv("WeightUnitmg");
471  $measuring_units[98] = $langs->transnoentitiesnoconv("WeightUnitounce");
472  $measuring_units[99] = $langs->transnoentitiesnoconv("WeightUnitpound");
473  }
474  else if ($measuring_style == 'size')
475  {
476  $measuring_units[0] = $langs->transnoentitiesnoconv("SizeUnitm");
477  $measuring_units[-1] = $langs->transnoentitiesnoconv("SizeUnitdm");
478  $measuring_units[-2] = $langs->transnoentitiesnoconv("SizeUnitcm");
479  $measuring_units[-3] = $langs->transnoentitiesnoconv("SizeUnitmm");
480  $measuring_units[98] = $langs->transnoentitiesnoconv("SizeUnitfoot");
481  $measuring_units[99] = $langs->transnoentitiesnoconv("SizeUnitinch");
482  }
483  else if ($measuring_style == 'surface')
484  {
485  $measuring_units[0] = $langs->transnoentitiesnoconv("SurfaceUnitm2");
486  $measuring_units[-2] = $langs->transnoentitiesnoconv("SurfaceUnitdm2");
487  $measuring_units[-4] = $langs->transnoentitiesnoconv("SurfaceUnitcm2");
488  $measuring_units[-6] = $langs->transnoentitiesnoconv("SurfaceUnitmm2");
489  $measuring_units[98] = $langs->transnoentitiesnoconv("SurfaceUnitfoot2");
490  $measuring_units[99] = $langs->transnoentitiesnoconv("SurfaceUnitinch2");
491  }
492  else if ($measuring_style == 'volume')
493  {
494  $measuring_units[0] = $langs->transnoentitiesnoconv("VolumeUnitm3");
495  $measuring_units[-3] = $langs->transnoentitiesnoconv("VolumeUnitdm3");
496  $measuring_units[-6] = $langs->transnoentitiesnoconv("VolumeUnitcm3");
497  $measuring_units[-9] = $langs->transnoentitiesnoconv("VolumeUnitmm3");
498  $measuring_units[88] = $langs->transnoentitiesnoconv("VolumeUnitfoot3");
499  $measuring_units[89] = $langs->transnoentitiesnoconv("VolumeUnitinch3");
500  $measuring_units[97] = $langs->transnoentitiesnoconv("VolumeUnitounce");
501  $measuring_units[98] = $langs->transnoentitiesnoconv("VolumeUnitlitre");
502  $measuring_units[99] = $langs->transnoentitiesnoconv("VolumeUnitgallon");
503  }
504 
505  return $measuring_units[$unit];
506 }
507 
515 function measuring_units_squared($unit)
516 {
517  $measuring_units=array();
518  $measuring_units[0] = 0; // m -> m3
519  $measuring_units[-1] = -2; // dm-> dm2
520  $measuring_units[-2] = -4; // cm -> cm2
521  $measuring_units[-3] = -6; // mm -> mm2
522  $measuring_units[98] = 98; // foot -> foot2
523  $measuring_units[99] = 99; // inch -> inch2
524  return $measuring_units[$unit];
525 }
526 
527 
535 function measuring_units_cubed($unit)
536 {
537  $measuring_units=array();
538  $measuring_units[0] = 0; // m -> m2
539  $measuring_units[-1] = -3; // dm-> dm3
540  $measuring_units[-2] = -6; // cm -> cm3
541  $measuring_units[-3] = -9; // mm -> mm3
542  $measuring_units[98] = 88; // foot -> foot3
543  $measuring_units[99] = 89; // inch -> inch3
544  return $measuring_units[$unit];
545 }
product_lot_admin_prepare_head()
Return array head with list of tabs to view object informations.
productlot_prepare_head($object)
Prepare array with list of tabs.
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add')
Complete or removed entries into a head array (used to build tabs).
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart)
Return a path to have a the directory according to object where files are stored. ...
const TYPE_SERVICE
Service.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
const TYPE_PRODUCT
Regular product.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="")
Scan a directory and return a list of files/directories.
Definition: files.lib.php:58
show_stats_for_company($product, $socid)
Show stats for company.
measuring_units_squared($unit)
Transform a given unit into the square of that unit, if known.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Class ProductCombination Used to represent a product combination.
print
Draft customers invoices.
Definition: index.php:91
measuring_units_cubed($unit)
Transform a given unit into the cube of that unit, if known.
product_admin_prepare_head()
Return array head with list of tabs to view object informations.
measuring_units_string($unit, $measuring_style='')
Return translation label of a unit key.
product_prepare_head($object)
Prepare array with list of tabs.
Definition: product.lib.php:35