dolibarr  9.0.0
box_produits.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
27 include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
28 include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
29 
30 
35 {
36  var $boxcode="lastproducts";
37  var $boximg="object_product";
38  var $boxlabel="BoxLastProducts";
39  var $depends = array("produit");
40 
44  public $db;
45 
46  var $param;
47 
48  var $info_box_head = array();
49  var $info_box_contents = array();
50 
51 
58  function __construct($db,$param)
59  {
60  global $conf, $user;
61 
62  $this->db=$db;
63 
64  $listofmodulesforexternal=explode(',',$conf->global->MAIN_MODULES_FOR_EXTERNAL);
65  $tmpentry=array('enabled'=>(! empty($conf->product->enabled) || ! empty($conf->service->enabled)), 'perms'=>(! empty($user->rights->produit->lire) || ! empty($user->rights->service->lire)), 'module'=>'product|service');
66  $showmode=isVisibleToUserType(($user->societe_id > 0 ? 1 : 0), $tmpentry, $listofmodulesforexternal);
67  $this->hidden=($showmode != 1);
68  }
69 
76  function loadBox($max=5)
77  {
78  global $user, $langs, $db, $conf, $hookmanager;
79 
80  $this->max=$max;
81 
82  include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
83  $productstatic=new Product($db);
84 
85  $this->info_box_head = array('text' => $langs->trans("BoxTitleLastProducts",$max));
86 
87  if ($user->rights->produit->lire || $user->rights->service->lire)
88  {
89  $sql = "SELECT p.rowid, p.label, p.ref, p.price, p.price_base_type, p.price_ttc, p.fk_product_type, p.tms, p.tosell, p.tobuy, p.fk_price_expression, p.entity";
90  $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
91  $sql.= ' WHERE p.entity IN ('.getEntity($productstatic->element).')';
92  if (empty($user->rights->produit->lire)) $sql.=' AND p.fk_product_type != 0';
93  if (empty($user->rights->service->lire)) $sql.=' AND p.fk_product_type != 1';
94  // Add where from hooks
95  if (is_object($hookmanager))
96  {
97  $parameters=array('boxproductlist'=>1);
98  $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
99  $sql.=$hookmanager->resPrint;
100  }
101  $sql.= $db->order('p.datec', 'DESC');
102  $sql.= $db->plimit($max, 0);
103 
104  $result = $db->query($sql);
105  if ($result)
106  {
107  $num = $db->num_rows($result);
108  $line = 0;
109  while ($line < $num)
110  {
111  $objp = $db->fetch_object($result);
112  $datem=$db->jdate($objp->tms);
113 
114  // Multilangs
115  if (! empty($conf->global->MAIN_MULTILANGS)) // si l'option est active
116  {
117  $sqld = "SELECT label";
118  $sqld.= " FROM ".MAIN_DB_PREFIX."product_lang";
119  $sqld.= " WHERE fk_product=".$objp->rowid;
120  $sqld.= " AND lang='". $langs->getDefaultLang() ."'";
121  $sqld.= " LIMIT 1";
122 
123  $resultd = $db->query($sqld);
124  if ($resultd)
125  {
126  $objtp = $db->fetch_object($resultd);
127  if (isset($objtp->label) && $objtp->label != '')
128  $objp->label = $objtp->label;
129  }
130  }
131  $productstatic->id = $objp->rowid;
132  $productstatic->ref = $objp->ref;
133  $productstatic->type = $objp->fk_product_type;
134  $productstatic->label = $objp->label;
135  $productstatic->entity = $objp->entity;
136 
137  $this->info_box_contents[$line][] = array(
138  'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
139  'text' => $productstatic->getNomUrl(1),
140  'asis' => 1,
141  );
142 
143  $this->info_box_contents[$line][] = array(
144  'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
145  'text' => $objp->label,
146  );
147 
148  if (empty($conf->dynamicprices->enabled) || empty($objp->fk_price_expression)) {
149  $price_base_type=$langs->trans($objp->price_base_type);
150  $price=($objp->price_base_type == 'HT')?price($objp->price):$price=price($objp->price_ttc);
151  }
152  else //Parse the dynamic price
153  {
154  $productstatic->fetch($objp->rowid, '', '', 1);
155  $priceparser = new PriceParser($this->db);
156  $price_result = $priceparser->parseProduct($productstatic);
157  if ($price_result >= 0) {
158  if ($objp->price_base_type == 'HT')
159  {
160  $price_base_type=$langs->trans("HT");
161  }
162  else
163  {
164  $price_result = $price_result * (1 + ($productstatic->tva_tx / 100));
165  $price_base_type=$langs->trans("TTC");
166  }
167  $price=price($price_result);
168  }
169  }
170  $this->info_box_contents[$line][] = array(
171  'td' => 'class="right"',
172  'text' => $price,
173  );
174 
175  $this->info_box_contents[$line][] = array(
176  'td' => 'class="nowrap"',
177  'text' => $price_base_type,
178  );
179 
180  $this->info_box_contents[$line][] = array(
181  'td' => 'class="right"',
182  'text' => dol_print_date($datem,'day'),
183  );
184 
185  $this->info_box_contents[$line][] = array(
186  'td' => 'align="right" width="18"',
187  'text' => '<span class="statusrefsell">'.$productstatic->LibStatut($objp->tosell,3,0).'<span>',
188  'asis' => 1
189  );
190 
191  $this->info_box_contents[$line][] = array(
192  'td' => 'align="right" width="18"',
193  'text' => '<span class="statusrefbuy">'.$productstatic->LibStatut($objp->tobuy,3,1).'</span>',
194  'asis' => 1
195  );
196 
197  $line++;
198  }
199  if ($num==0)
200  $this->info_box_contents[$line][0] = array(
201  'td' => 'align="center"',
202  'text'=>$langs->trans("NoRecordedProducts"),
203  );
204 
205  $db->free($result);
206  } else {
207  $this->info_box_contents[0][0] = array(
208  'td' => '',
209  'maxlength'=>500,
210  'text' => ($db->error().' sql='.$sql),
211  );
212  }
213  } else {
214  $this->info_box_contents[0][0] = array(
215  'td' => 'align="left" class="nohover opacitymedium"',
216  'text' => $langs->trans("ReadPermissionNotAllowed")
217  );
218  }
219  }
220 
229  function showBox($head = null, $contents = null, $nooutput=0)
230  {
231  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
232  }
233 }
234 
__construct($db, $param)
Constructor.
Class to parse product price expressions.
Class to manage products or services.
Class ModeleBoxes.
loadBox($max=5)
Load data into info_box_contents array to show array later.
isVisibleToUserType($type_user, &$menuentry, &$listofmodulesforexternal)
Function to test if an entry is enabled or not.
Class to manage the box to show last products.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.