dolibarr  7.0.0-beta
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@capnetworks.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 
41  var $db;
42  var $param;
43 
44  var $info_box_head = array();
45  var $info_box_contents = array();
46 
47 
54  function __construct($db,$param)
55  {
56  global $conf, $user;
57 
58  $this->db=$db;
59 
60  $listofmodulesforexternal=explode(',',$conf->global->MAIN_MODULES_FOR_EXTERNAL);
61  $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');
62  $showmode=isVisibleToUserType(($user->societe_id > 0 ? 1 : 0), $tmpentry, $listofmodulesforexternal);
63  $this->hidden=($showmode != 1);
64  }
65 
72  function loadBox($max=5)
73  {
74  global $user, $langs, $db, $conf, $hookmanager;
75 
76  $this->max=$max;
77 
78  include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
79  $productstatic=new Product($db);
80 
81  $this->info_box_head = array('text' => $langs->trans("BoxTitleLastProducts",$max));
82 
83  if ($user->rights->produit->lire || $user->rights->service->lire)
84  {
85  $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";
86  $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
87  $sql.= ' WHERE p.entity IN ('.getEntity($productstatic->element, 1).')';
88  if (empty($user->rights->produit->lire)) $sql.=' AND p.fk_product_type != 0';
89  if (empty($user->rights->service->lire)) $sql.=' AND p.fk_product_type != 1';
90  // Add where from hooks
91  if (is_object($hookmanager))
92  {
93  $parameters=array('boxproductlist'=>1);
94  $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
95  $sql.=$hookmanager->resPrint;
96  }
97  $sql.= $db->order('p.datec', 'DESC');
98  $sql.= $db->plimit($max, 0);
99 
100  $result = $db->query($sql);
101  if ($result)
102  {
103  $num = $db->num_rows($result);
104  $line = 0;
105  while ($line < $num)
106  {
107  $objp = $db->fetch_object($result);
108  $datem=$db->jdate($objp->tms);
109 
110  // Multilangs
111  if (! empty($conf->global->MAIN_MULTILANGS)) // si l'option est active
112  {
113  $sqld = "SELECT label";
114  $sqld.= " FROM ".MAIN_DB_PREFIX."product_lang";
115  $sqld.= " WHERE fk_product=".$objp->rowid;
116  $sqld.= " AND lang='". $langs->getDefaultLang() ."'";
117  $sqld.= " LIMIT 1";
118 
119  $resultd = $db->query($sqld);
120  if ($resultd)
121  {
122  $objtp = $db->fetch_object($resultd);
123  if (isset($objtp->label) && $objtp->label != '')
124  $objp->label = $objtp->label;
125  }
126  }
127  $productstatic->id = $objp->rowid;
128  $productstatic->ref = $objp->ref;
129  $productstatic->type = $objp->fk_product_type;
130  $productstatic->label = $objp->label;
131  $productstatic->entity = $objp->entity;
132 
133  $this->info_box_contents[$line][] = array(
134  'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
135  'text' => $productstatic->getNomUrl(1),
136  'asis' => 1,
137  );
138 
139  $this->info_box_contents[$line][] = array(
140  'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
141  'text' => $objp->label,
142  );
143 
144  if (empty($conf->dynamicprices->enabled) || empty($objp->fk_price_expression)) {
145  $price_base_type=$langs->trans($objp->price_base_type);
146  $price=($objp->price_base_type == 'HT')?price($objp->price):$price=price($objp->price_ttc);
147  }
148  else //Parse the dynamic price
149  {
150  $productstatic->fetch($objp->rowid, '', '', 1);
151  $priceparser = new PriceParser($this->db);
152  $price_result = $priceparser->parseProduct($productstatic);
153  if ($price_result >= 0) {
154  if ($objp->price_base_type == 'HT')
155  {
156  $price_base_type=$langs->trans("HT");
157  }
158  else
159  {
160  $price_result = $price_result * (1 + ($productstatic->tva_tx / 100));
161  $price_base_type=$langs->trans("TTC");
162  }
163  $price=price($price_result);
164  }
165  }
166  $this->info_box_contents[$line][] = array(
167  'td' => 'class="right"',
168  'text' => $price,
169  );
170 
171  $this->info_box_contents[$line][] = array(
172  'td' => 'class="nowrap"',
173  'text' => $price_base_type,
174  );
175 
176  $this->info_box_contents[$line][] = array(
177  'td' => 'class="right"',
178  'text' => dol_print_date($datem,'day'),
179  );
180 
181  $this->info_box_contents[$line][] = array(
182  'td' => 'align="right" width="18"',
183  'text' => '<span class="statusrefsell">'.$productstatic->LibStatut($objp->tosell,3,0).'<span>',
184  'asis' => 1
185  );
186 
187  $this->info_box_contents[$line][] = array(
188  'td' => 'align="right" width="18"',
189  'text' => '<span class="statusrefbuy">'.$productstatic->LibStatut($objp->tobuy,3,1).'</span>',
190  'asis' => 1
191  );
192 
193  $line++;
194  }
195  if ($num==0)
196  $this->info_box_contents[$line][0] = array(
197  'td' => 'align="center"',
198  'text'=>$langs->trans("NoRecordedProducts"),
199  );
200 
201  $db->free($result);
202  } else {
203  $this->info_box_contents[0][0] = array(
204  'td' => '',
205  'maxlength'=>500,
206  'text' => ($db->error().' sql='.$sql),
207  );
208  }
209  } else {
210  $this->info_box_contents[0][0] = array(
211  'td' => 'align="left" class="nohover opacitymedium"',
212  'text' => $langs->trans("ReadPermissionNotAllowed")
213  );
214  }
215  }
216 
225  function showBox($head = null, $contents = null, $nooutput=0)
226  {
227  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
228  }
229 
230 }
231 
__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.