1<?php 2/* Copyright (C) 2010-2018 Regis Houssin <regis.houssin@inodbox.com> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18/** 19 * \file htdocs/product/canvas/product/actions_card_product.class.php 20 * \ingroup produit 21 * \brief File with class of actions for canvas product 22 */ 23include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; 24 25 26/** 27 * Class with controller methods for product canvas 28 */ 29class ActionsCardProduct 30{ 31 public $targetmodule; 32 public $canvas; 33 public $card; 34 35 //! Template container 36 public $tpl = array(); 37 38 // List of fiels for action=list 39 public $field_list = array(); 40 public $list_datas = array(); 41 42 43 /** 44 * Constructor 45 * 46 * @param DoliDB $db Database handler 47 * @param string $dirmodule Name of directory of module 48 * @param string $targetmodule Name of directory where canvas is stored 49 * @param string $canvas Name of canvas 50 * @param string $card Name of tab (sub-canvas) 51 */ 52 public function __construct($db, $dirmodule, $targetmodule, $canvas, $card) 53 { 54 $this->db = $db; 55 $this->dirmodule = $dirmodule; 56 $this->targetmodule = $targetmodule; 57 $this->canvas = $canvas; 58 $this->card = $card; 59 60 $this->name = "product"; 61 $this->definition = "Product canvas (défaut)"; 62 $this->fieldListName = "product_default"; 63 $this->next_prev_filter = "canvas='product'"; 64 } 65 66 67 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps 68 /** 69 * Assign custom values for canvas (for example into this->tpl to be used by templates) 70 * 71 * @param string $action Type of action 72 * @param integer $id Id of object 73 * @param string $ref Ref of object 74 * @return void 75 */ 76 public function assign_values(&$action, $id = 0, $ref = '') 77 { 78 // phpcs:enable 79 global $limit, $offset, $sortfield, $sortorder; 80 global $conf, $langs, $user, $mysoc, $canvas; 81 global $form, $formproduct; 82 83 $tmpobject = new Product($this->db); 84 if (!empty($id) || !empty($ref)) { 85 $tmpobject->fetch($id, $ref); 86 } 87 $this->object = $tmpobject; 88 89 //parent::assign_values($action); 90 91 foreach ($this->object as $key => $value) { 92 $this->tpl[$key] = $value; 93 } 94 95 $this->tpl['error'] = get_htmloutput_errors($this->object->error, $this->object->errors); 96 97 // canvas 98 $this->tpl['canvas'] = $this->canvas; 99 100 // id 101 $this->tpl['id'] = $this->id; 102 103 // Ref 104 $this->tpl['ref'] = $this->ref; 105 106 // Label 107 $this->tpl['label'] = $this->label; 108 109 // Description 110 $this->tpl['description'] = nl2br($this->description); 111 112 // Statut 113 $this->tpl['status'] = $this->object->getLibStatut(2); 114 115 // Note 116 $this->tpl['note'] = nl2br($this->note); 117 118 if ($action == 'create') { 119 // Price 120 $this->tpl['price'] = $this->price; 121 $this->tpl['price_min'] = $this->price_min; 122 $this->tpl['price_base_type'] = $form->selectPriceBaseType($this->price_base_type, "price_base_type"); 123 124 // VAT 125 $this->tpl['tva_tx'] = $form->load_tva("tva_tx", -1, $mysoc, ''); 126 } 127 128 if ($action == 'view') { 129 $head = product_prepare_head($this->object); 130 131 $this->tpl['showrefnav'] = $form->showrefnav($this->object, 'ref', '', 1, 'ref'); 132 133 $titre = $langs->trans("CardProduct".$this->object->type); 134 $picto = ($this->object->type == Product::TYPE_SERVICE ? 'service' : 'product'); 135 $this->tpl['showhead'] = dol_get_fiche_head($head, 'card', $titre, 0, $picto); 136 $this->tpl['showend'] = dol_get_fiche_end(); 137 138 // Accountancy buy code 139 $this->tpl['accountancyBuyCodeKey'] = $form->editfieldkey("ProductAccountancyBuyCode", 'productaccountancycodesell', $this->accountancy_code_sell, $this, $user->rights->produit->creer); 140 $this->tpl['accountancyBuyCodeVal'] = $form->editfieldval("ProductAccountancyBuyCode", 'productaccountancycodesell', $this->accountancy_code_sell, $this, $user->rights->produit->creer); 141 142 // Accountancy sell code 143 $this->tpl['accountancySellCodeKey'] = $form->editfieldkey("ProductAccountancySellCode", 'productaccountancycodebuy', $this->accountancy_code_buy, $this, $user->rights->produit->creer); 144 $this->tpl['accountancySellCodeVal'] = $form->editfieldval("ProductAccountancySellCode", 'productaccountancycodebuy', $this->accountancy_code_buy, $this, $user->rights->produit->creer); 145 } 146 147 $this->tpl['finished'] = $this->object->finished; 148 $this->tpl['ref'] = $this->object->ref; 149 $this->tpl['label'] = $this->object->label; 150 $this->tpl['id'] = $this->object->id; 151 $this->tpl['type'] = $this->object->type; 152 $this->tpl['note'] = $this->object->note; 153 $this->tpl['seuil_stock_alerte'] = $this->object->seuil_stock_alerte; 154 155 if ($action == 'create') { 156 // Title 157 $this->tpl['title'] = $langs->trans("NewProduct"); 158 } 159 160 if ($action == 'edit') { 161 $this->tpl['title'] = $langs->trans('Modify').' '.$langs->trans('Product').' : '.$this->object->ref; 162 } 163 164 if ($action == 'create' || $action == 'edit') { 165 // Status 166 $statutarray = array('1' => $langs->trans("OnSell"), '0' => $langs->trans("NotOnSell")); 167 $this->tpl['status'] = $form->selectarray('statut', $statutarray, $this->object->status); 168 169 $statutarray = array('1' => $langs->trans("ProductStatusOnBuy"), '0' => $langs->trans("ProductStatusNotOnBuy")); 170 $this->tpl['status_buy'] = $form->selectarray('statut_buy', $statutarray, $this->object->status_buy); 171 172 $this->tpl['description'] = $this->description; 173 $this->tpl['note'] = $this->note; 174 175 // Finished 176 $statutarray = array('1' => $langs->trans("Finished"), '0' => $langs->trans("RowMaterial")); 177 $this->tpl['finished'] = $form->selectarray('finished', $statutarray, $this->object->finished); 178 179 // Weight 180 $this->tpl['weight'] = $this->object->weight; 181 $this->tpl['weight_units'] = $formproduct->selectMeasuringUnits("weight_units", "weight", $this->object->weight_units, 0, 2); 182 183 // Length 184 $this->tpl['length'] = $this->object->length; 185 $this->tpl['length_units'] = $formproduct->selectMeasuringUnits("length_units", "size", $this->object->length_units, 0, 2); 186 187 // Surface 188 $this->tpl['surface'] = $this->object->surface; 189 $this->tpl['surface_units'] = $formproduct->selectMeasuringUnits("surface_units", "surface", $this->object->surface_units, 0, 2); 190 191 // Volume 192 $this->tpl['volume'] = $this->object->volume; 193 $this->tpl['volume_units'] = $formproduct->selectMeasuringUnits("volume_units", "volume", $this->object->volume_units, 0, 2); 194 } 195 196 if ($action == 'view') { 197 // Photo 198 $this->tpl['nblines'] = 4; 199 if ($this->object->is_photo_available($conf->product->multidir_output[$this->object->entity])) { 200 $this->tpl['photos'] = $this->object->show_photos('product', $conf->product->multidir_output[$this->object->entity], 1, 1, 0, 0, 0, 80); 201 } 202 203 // Nature 204 $this->tpl['finished'] = $this->object->getLibFinished(); 205 206 // Weight 207 if ($this->object->weight != '') { 208 $this->tpl['weight'] = $this->object->weight." ".measuringUnitString(0, "weight", $this->object->weight_units); 209 } 210 211 // Length 212 if ($this->object->length != '') { 213 $this->tpl['length'] = $this->object->length." ".measuringUnitString(0, "size", $this->object->length_units); 214 } 215 216 // Surface 217 if ($this->object->surface != '') { 218 $this->tpl['surface'] = $this->object->surface." ".measuringUnitString(0, "surface", $this->object->surface_units); 219 } 220 221 // Volume 222 if ($this->object->volume != '') { 223 $this->tpl['volume'] = $this->object->volume." ".measuringUnitString(0, "volume", $this->object->volume_units); 224 } 225 226 $this->tpl['fiche_end'] = dol_get_fiche_end(); 227 } 228 229 if ($action == 'list') { 230 $this->LoadListDatas($limit, $offset, $sortfield, $sortorder); 231 } 232 } 233 234 235 /** 236 * Fetch field list 237 * 238 * @return void 239 */ 240 private function getFieldListCanvas() 241 { 242 global $conf, $langs; 243 244 $this->field_list = array(); 245 246 $sql = "SELECT rowid, name, alias, title, align, sort, search, visible, enabled, rang"; 247 $sql .= " FROM ".MAIN_DB_PREFIX."c_field_list"; 248 $sql .= " WHERE element = '".$this->db->escape($this->fieldListName)."'"; 249 $sql .= " AND entity = ".$conf->entity; 250 $sql .= " ORDER BY rang ASC"; 251 252 $resql = $this->db->query($sql); 253 if ($resql) { 254 $num = $this->db->num_rows($resql); 255 256 $i = 0; 257 while ($i < $num) { 258 $fieldlist = array(); 259 260 $obj = $this->db->fetch_object($resql); 261 262 $fieldlist["id"] = $obj->rowid; 263 $fieldlist["name"] = $obj->name; 264 $fieldlist["alias"] = $obj->alias; 265 $fieldlist["title"] = $langs->trans($obj->title); 266 $fieldlist["align"] = $obj->align; 267 $fieldlist["sort"] = $obj->sort; 268 $fieldlist["search"] = $obj->search; 269 $fieldlist["visible"] = $obj->visible; 270 $fieldlist["enabled"] = verifCond($obj->enabled); 271 $fieldlist["order"] = $obj->rang; 272 273 array_push($this->field_list, $fieldlist); 274 275 $i++; 276 } 277 $this->db->free($resql); 278 } else { 279 dol_print_error($this->db, $sql); 280 } 281 } 282 283 284 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps 285 /** 286 * Fetch datas list and save into ->list_datas 287 * 288 * @param int $limit Limit number of responses 289 * @param int $offset Offset for first response 290 * @param string $sortfield Sort field 291 * @param string $sortorder Sort order ('ASC' or 'DESC') 292 * @return void 293 */ 294 public function LoadListDatas($limit, $offset, $sortfield, $sortorder) 295 { 296 // phpcs:enable 297 global $conf, $langs; 298 299 $this->getFieldListCanvas(); 300 301 $this->list_datas = array(); 302 303 // Clean parameters 304 $sall = trim((GETPOST('search_all', 'alphanohtml') != '') ?GETPOST('search_all', 'alphanohtml') : GETPOST('sall', 'alphanohtml')); 305 306 foreach ($this->field_list as $field) { 307 if ($field['enabled']) { 308 $fieldname = "s".$field['alias']; 309 $$fieldname = GETPOST($fieldname); 310 } 311 } 312 313 $sql = 'SELECT DISTINCT '; 314 315 // Fields requiered 316 $sql .= 'p.rowid, p.price_base_type, p.fk_product_type, p.seuil_stock_alerte, p.entity'; 317 318 // Fields not requiered 319 foreach ($this->field_list as $field) { 320 if ($field['enabled']) { 321 $sql .= ", ".$field['name']." as ".$field['alias']; 322 } 323 } 324 325 $sql .= ' FROM '.MAIN_DB_PREFIX.'product as p'; 326 $sql .= " WHERE p.entity IN (".getEntity('product').")"; 327 328 if ($sall) { 329 $clause = ''; 330 $sql .= " AND ("; 331 foreach ($this->field_list as $field) { 332 if ($field['enabled']) { 333 $sql .= $clause." ".$field['name']." LIKE '%".$this->db->escape($sall)."%'"; 334 if ($clause == '') { 335 $clause = ' OR'; 336 } 337 } 338 } 339 $sql .= ")"; 340 } 341 342 // Search fields 343 foreach ($this->field_list as $field) { 344 if ($field['enabled']) { 345 $fieldname = "s".$field['alias']; 346 if (${$fieldname}) { 347 $sql .= " AND ".$field['name']." LIKE '%".$this->db->escape(${$fieldname})."%'"; 348 } 349 } 350 } 351 352 if (GETPOSTISSET("tosell")) { 353 $sql .= " AND p.tosell = ".((int) GETPOST("tosell", "int")); 354 } 355 if (GETPOSTISSET("canvas")) { 356 $sql .= " AND p.canvas = '".$this->db->escape(GETPOST("canvas"))."'"; 357 } 358 $sql .= $this->db->order($sortfield, $sortorder); 359 $sql .= $this->db->plimit($limit + 1, $offset); 360 //print $sql; 361 362 $resql = $this->db->query($sql); 363 if ($resql) { 364 $num = $this->db->num_rows($resql); 365 366 $i = 0; 367 while ($i < min($num, $limit)) { 368 $datas = array(); 369 370 $obj = $this->db->fetch_object($resql); 371 372 $datas["id"] = $obj->rowid; 373 374 foreach ($this->field_list as $field) { 375 if ($field['enabled']) { 376 $alias = $field['alias']; 377 378 if ($alias == 'ref') { 379 $this->id = $obj->rowid; 380 $this->ref = $obj->$alias; 381 $this->type = $obj->fk_product_type; 382 $this->entity = $obj->entity; 383 $datas[$alias] = $this->getNomUrl(1, '', 24); 384 } elseif ($alias == 'stock') { 385 $this->load_stock(); 386 if ($this->stock_reel < $obj->seuil_stock_alerte) { 387 $datas[$alias] = $this->stock_reel.' '.img_warning($langs->trans("StockTooLow")); 388 } else { 389 $datas[$alias] = $this->stock_reel; 390 } 391 } elseif ($alias == 'label') { 392 $datas[$alias] = dol_trunc($obj->$alias, 40); 393 } elseif (preg_match('/price/i', $alias)) { 394 $datas[$alias] = price($obj->$alias); 395 } elseif ($alias == 'datem') { 396 $datas[$alias] = dol_print_date($this->db->jdate($obj->$alias), 'day'); 397 } elseif ($alias == 'status') { 398 $datas[$alias] = $this->LibStatut($obj->$alias, 5); 399 } else { 400 $datas[$alias] = $obj->$alias; 401 } 402 } 403 } 404 405 array_push($this->list_datas, $datas); 406 407 $i++; 408 } 409 $this->db->free($resql); 410 } else { 411 dol_print_error($this->db); 412 } 413 } 414} 415