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-2021 Frederic France <frederic.france@netlogic.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 <https://www.gnu.org/licenses/>. 19 */ 20 21/** 22 * \file htdocs/core/boxes/box_produits.php 23 * \ingroup produits,services 24 * \brief Module to generate box of last products/services 25 */ 26 27include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php'; 28include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; 29 30 31/** 32 * Class to manage the box to show last products 33 */ 34class box_produits extends ModeleBoxes 35{ 36 public $boxcode = "lastproducts"; 37 public $boximg = "object_product"; 38 public $boxlabel = "BoxLastProducts"; 39 public $depends = array("produit"); 40 41 /** 42 * @var DoliDB Database handler. 43 */ 44 public $db; 45 46 public $param; 47 48 public $info_box_head = array(); 49 public $info_box_contents = array(); 50 51 52 /** 53 * Constructor 54 * 55 * @param DoliDB $db Database handler 56 * @param string $param More parameters 57 */ 58 public 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->socid > 0 ? 1 : 0), $tmpentry, $listofmodulesforexternal); 67 $this->hidden = ($showmode != 1); 68 } 69 70 /** 71 * Load data into info_box_contents array to show array later. 72 * 73 * @param int $max Maximum number of records to load 74 * @return void 75 */ 76 public function loadBox($max = 5) 77 { 78 global $user, $langs, $conf, $hookmanager; 79 80 $this->max = $max; 81 82 include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; 83 $productstatic = new Product($this->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 $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"; 89 $sql .= ", p.accountancy_code_sell"; 90 $sql .= ", p.accountancy_code_sell_intra"; 91 $sql .= ", p.accountancy_code_sell_export"; 92 $sql .= ", p.accountancy_code_buy"; 93 $sql .= ", p.accountancy_code_buy_intra"; 94 $sql .= ", p.accountancy_code_buy_export"; 95 $sql .= ', p.barcode'; 96 $sql .= " FROM ".MAIN_DB_PREFIX."product as p"; 97 $sql .= ' WHERE p.entity IN ('.getEntity($productstatic->element).')'; 98 if (empty($user->rights->produit->lire)) { 99 $sql .= ' AND p.fk_product_type != 0'; 100 } 101 if (empty($user->rights->service->lire)) { 102 $sql .= ' AND p.fk_product_type != 1'; 103 } 104 // Add where from hooks 105 if (is_object($hookmanager)) { 106 $parameters = array('boxproductlist'=>1); 107 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook 108 $sql .= $hookmanager->resPrint; 109 } 110 $sql .= $this->db->order('p.datec', 'DESC'); 111 $sql .= $this->db->plimit($max, 0); 112 113 $result = $this->db->query($sql); 114 if ($result) { 115 $num = $this->db->num_rows($result); 116 $line = 0; 117 while ($line < $num) { 118 $objp = $this->db->fetch_object($result); 119 $datem = $this->db->jdate($objp->tms); 120 121 // Multilangs 122 if (!empty($conf->global->MAIN_MULTILANGS)) { // si l'option est active 123 $sqld = "SELECT label"; 124 $sqld .= " FROM ".MAIN_DB_PREFIX."product_lang"; 125 $sqld .= " WHERE fk_product=".$objp->rowid; 126 $sqld .= " AND lang='".$this->db->escape($langs->getDefaultLang())."'"; 127 $sqld .= " LIMIT 1"; 128 129 $resultd = $this->db->query($sqld); 130 if ($resultd) { 131 $objtp = $this->db->fetch_object($resultd); 132 if (isset($objtp->label) && $objtp->label != '') { 133 $objp->label = $objtp->label; 134 } 135 } 136 } 137 $productstatic->id = $objp->rowid; 138 $productstatic->ref = $objp->ref; 139 $productstatic->type = $objp->fk_product_type; 140 $productstatic->label = $objp->label; 141 $productstatic->entity = $objp->entity; 142 $productstatic->status = $objp->tosell; 143 $productstatic->status_buy = $objp->tobuy; 144 $productstatic->barcode = $objp->barcode; 145 $productstatic->accountancy_code_sell = $objp->accountancy_code_sell; 146 $productstatic->accountancy_code_sell_intra = $objp->accountancy_code_sell_intra; 147 $productstatic->accountancy_code_sell_export = $objp->accountancy_code_sell_export; 148 $productstatic->accountancy_code_buy = $objp->accountancy_code_buy; 149 $productstatic->accountancy_code_buy_intra = $objp->accountancy_code_buy_intra; 150 $productstatic->accountancy_code_buy_export = $objp->accountancy_code_buy_export; 151 152 $this->info_box_contents[$line][] = array( 153 'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"', 154 'text' => $productstatic->getNomUrl(1), 155 'asis' => 1, 156 ); 157 158 $this->info_box_contents[$line][] = array( 159 'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"', 160 'text' => $objp->label, 161 ); 162 $price = ''; 163 $price_base_type = ''; 164 if (empty($conf->dynamicprices->enabled) || empty($objp->fk_price_expression)) { 165 $price_base_type = $langs->trans($objp->price_base_type); 166 $price = ($objp->price_base_type == 'HT') ?price($objp->price) : $price = price($objp->price_ttc); 167 } else { 168 //Parse the dynamic price 169 $productstatic->fetch($objp->rowid, '', '', 1); 170 $priceparser = new PriceParser($this->db); 171 $price_result = $priceparser->parseProduct($productstatic); 172 if ($price_result >= 0) { 173 if ($objp->price_base_type == 'HT') { 174 $price_base_type = $langs->trans("HT"); 175 } else { 176 $price_result = $price_result * (1 + ($productstatic->tva_tx / 100)); 177 $price_base_type = $langs->trans("TTC"); 178 } 179 $price = price($price_result); 180 } 181 } 182 $this->info_box_contents[$line][] = array( 183 'td' => 'class="right"', 184 'text' => $price, 185 ); 186 187 $this->info_box_contents[$line][] = array( 188 'td' => 'class="nowrap"', 189 'text' => $price_base_type, 190 ); 191 192 $this->info_box_contents[$line][] = array( 193 'td' => 'class="center nowraponall"', 194 'text' => dol_print_date($datem, 'day', 'tzuserrel'), 195 ); 196 197 $this->info_box_contents[$line][] = array( 198 'td' => 'class="right" width="18"', 199 'text' => '<span class="statusrefsell">'.$productstatic->LibStatut($objp->tosell, 3, 0).'</span>', 200 'asis' => 1 201 ); 202 203 $this->info_box_contents[$line][] = array( 204 'td' => 'class="right" width="18"', 205 'text' => '<span class="statusrefbuy">'.$productstatic->LibStatut($objp->tobuy, 3, 1).'</span>', 206 'asis' => 1 207 ); 208 209 $line++; 210 } 211 if ($num == 0) { 212 $this->info_box_contents[$line][0] = array( 213 'td' => 'class="center"', 214 'text'=>$langs->trans("NoRecordedProducts"), 215 ); 216 } 217 218 $this->db->free($result); 219 } else { 220 $this->info_box_contents[0][0] = array( 221 'td' => '', 222 'maxlength'=>500, 223 'text' => ($this->db->error().' sql='.$sql), 224 ); 225 } 226 } else { 227 $this->info_box_contents[0][0] = array( 228 'td' => 'class="nohover opacitymedium left"', 229 'text' => $langs->trans("ReadPermissionNotAllowed") 230 ); 231 } 232 } 233 234 /** 235 * Method to show box 236 * 237 * @param array $head Array with properties of box title 238 * @param array $contents Array with properties of box lines 239 * @param int $nooutput No print, only return string 240 * @return string 241 */ 242 public function showBox($head = null, $contents = null, $nooutput = 0) 243 { 244 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput); 245 } 246} 247