dolibarr  16.0.5
actions_card_product.class.php
Go to the documentation of this file.
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 
23 include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
24 
25 
30 {
31  public $targetmodule;
32  public $canvas;
33  public $card;
34 
35  public $object;
36 
38  public $tpl = array();
39 
40  // List of fiels for action=list
41  public $field_list = array();
42  public $list_datas = array();
43 
44 
54  public function __construct($db, $dirmodule, $targetmodule, $canvas, $card)
55  {
56  $this->db = $db;
57  $this->dirmodule = $dirmodule;
58  $this->targetmodule = $targetmodule;
59  $this->canvas = $canvas;
60  $this->card = $card;
61 
62  $this->name = "product";
63  $this->definition = "Product canvas (défaut)";
64  $this->fieldListName = "product_default";
65  $this->next_prev_filter = "canvas='product'";
66  }
67 
68 
69  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
78  public function assign_values(&$action, $id = 0, $ref = '')
79  {
80  // phpcs:enable
81  global $limit, $offset, $sortfield, $sortorder;
82  global $conf, $langs, $user, $mysoc, $canvas;
83  global $form, $formproduct;
84 
85  $tmpobject = new Product($this->db);
86  if (!empty($id) || !empty($ref)) {
87  $tmpobject->fetch($id, $ref);
88  }
89  $this->object = $tmpobject;
90 
91  //parent::assign_values($action);
92 
93  foreach ($this->object as $key => $value) {
94  $this->tpl[$key] = $value;
95  }
96 
97  $this->tpl['error'] = get_htmloutput_errors($this->object->error, $this->object->errors);
98 
99  // canvas
100  $this->tpl['canvas'] = $this->canvas;
101 
102  // id
103  $this->tpl['id'] = $this->object->id;
104 
105  // Ref
106  $this->tpl['ref'] = $this->object->ref;
107 
108  // Label
109  $this->tpl['label'] = $this->object->label;
110 
111  // Description
112  $this->tpl['description'] = nl2br($this->description);
113 
114  // Statut
115  $this->tpl['status'] = $this->object->getLibStatut(2);
116 
117  // Note
118  $this->tpl['note'] = nl2br($this->object->note_private);
119 
120  if ($action == 'create') {
121  // Price
122  $this->tpl['price'] = $this->object->price;
123  $this->tpl['price_min'] = $this->object->price_min;
124  $this->tpl['price_base_type'] = $form->selectPriceBaseType($this->price_base_type, "price_base_type");
125 
126  // VAT
127  $this->tpl['tva_tx'] = $form->load_tva("tva_tx", -1, $mysoc, '');
128  }
129 
130  if ($action == 'view') {
131  $head = product_prepare_head($this->object);
132 
133  $this->tpl['showrefnav'] = $form->showrefnav($this->object, 'ref', '', 1, 'ref');
134 
135  $titre = $langs->trans("CardProduct".$this->object->type);
136  $picto = ($this->object->type == Product::TYPE_SERVICE ? 'service' : 'product');
137  $this->tpl['showhead'] = dol_get_fiche_head($head, 'card', $titre, 0, $picto);
138  $this->tpl['showend'] = dol_get_fiche_end();
139 
140  // Accountancy buy code
141  $this->tpl['accountancyBuyCodeKey'] = $form->editfieldkey("ProductAccountancyBuyCode", 'productaccountancycodesell', $this->accountancy_code_sell, $this, $user->rights->produit->creer);
142  $this->tpl['accountancyBuyCodeVal'] = $form->editfieldval("ProductAccountancyBuyCode", 'productaccountancycodesell', $this->accountancy_code_sell, $this, $user->rights->produit->creer);
143 
144  // Accountancy sell code
145  $this->tpl['accountancySellCodeKey'] = $form->editfieldkey("ProductAccountancySellCode", 'productaccountancycodebuy', $this->accountancy_code_buy, $this, $user->rights->produit->creer);
146  $this->tpl['accountancySellCodeVal'] = $form->editfieldval("ProductAccountancySellCode", 'productaccountancycodebuy', $this->accountancy_code_buy, $this, $user->rights->produit->creer);
147  }
148 
149  $this->tpl['finished'] = $this->object->finished;
150  $this->tpl['ref'] = $this->object->ref;
151  $this->tpl['label'] = $this->object->label;
152  $this->tpl['id'] = $this->object->id;
153  $this->tpl['type'] = $this->object->type;
154  $this->tpl['note'] = $this->object->note_private;
155  $this->tpl['seuil_stock_alerte'] = $this->object->seuil_stock_alerte;
156 
157  if ($action == 'create') {
158  // Title
159  $this->tpl['title'] = $langs->trans("NewProduct");
160  }
161 
162  if ($action == 'edit') {
163  $this->tpl['title'] = $langs->trans('Modify').' '.$langs->trans('Product').' : '.$this->object->ref;
164  }
165 
166  if ($action == 'create' || $action == 'edit') {
167  // Status
168  $statutarray = array('1' => $langs->trans("OnSell"), '0' => $langs->trans("NotOnSell"));
169  $this->tpl['status'] = $form->selectarray('statut', $statutarray, $this->object->status);
170 
171  $statutarray = array('1' => $langs->trans("ProductStatusOnBuy"), '0' => $langs->trans("ProductStatusNotOnBuy"));
172  $this->tpl['status_buy'] = $form->selectarray('statut_buy', $statutarray, $this->object->status_buy);
173 
174  $this->tpl['description'] = $this->object->description;
175  $this->tpl['note'] = $this->object->note;
176 
177  // Finished
178  $statutarray = array('1' => $langs->trans("Finished"), '0' => $langs->trans("RowMaterial"));
179  $this->tpl['finished'] = $form->selectarray('finished', $statutarray, $this->object->finished);
180 
181  // Weight
182  $this->tpl['weight'] = $this->object->weight;
183  $this->tpl['weight_units'] = $formproduct->selectMeasuringUnits("weight_units", "weight", $this->object->weight_units, 0, 2);
184 
185  // Length
186  $this->tpl['length'] = $this->object->length;
187  $this->tpl['length_units'] = $formproduct->selectMeasuringUnits("length_units", "size", $this->object->length_units, 0, 2);
188 
189  // Surface
190  $this->tpl['surface'] = $this->object->surface;
191  $this->tpl['surface_units'] = $formproduct->selectMeasuringUnits("surface_units", "surface", $this->object->surface_units, 0, 2);
192 
193  // Volume
194  $this->tpl['volume'] = $this->object->volume;
195  $this->tpl['volume_units'] = $formproduct->selectMeasuringUnits("volume_units", "volume", $this->object->volume_units, 0, 2);
196  }
197 
198  if ($action == 'view') {
199  // Photo
200  $this->tpl['nblines'] = 4;
201  if ($this->object->is_photo_available($conf->product->multidir_output[$this->object->entity])) {
202  $this->tpl['photos'] = $this->object->show_photos('product', $conf->product->multidir_output[$this->object->entity], 1, 1, 0, 0, 0, 80);
203  }
204 
205  // Nature
206  $this->tpl['finished'] = $this->object->getLibFinished();
207 
208  // Weight
209  if ($this->object->weight != '') {
210  $this->tpl['weight'] = $this->object->weight." ".measuringUnitString(0, "weight", $this->object->weight_units);
211  }
212 
213  // Length
214  if ($this->object->length != '') {
215  $this->tpl['length'] = $this->object->length." ".measuringUnitString(0, "size", $this->object->length_units);
216  }
217 
218  // Surface
219  if ($this->object->surface != '') {
220  $this->tpl['surface'] = $this->object->surface." ".measuringUnitString(0, "surface", $this->object->surface_units);
221  }
222 
223  // Volume
224  if ($this->object->volume != '') {
225  $this->tpl['volume'] = $this->object->volume." ".measuringUnitString(0, "volume", $this->object->volume_units);
226  }
227 
228  $this->tpl['fiche_end'] = dol_get_fiche_end();
229  }
230 
231  if ($action == 'list') {
232  $this->LoadListDatas($limit, $offset, $sortfield, $sortorder);
233  }
234  }
235 
236 
242  private function getFieldListCanvas()
243  {
244  global $conf, $langs;
245 
246  $this->field_list = array();
247 
248  $sql = "SELECT rowid, name, alias, title, align, sort, search, visible, enabled, rang";
249  $sql .= " FROM ".MAIN_DB_PREFIX."c_field_list";
250  $sql .= " WHERE element = '".$this->db->escape($this->fieldListName)."'";
251  $sql .= " AND entity = ".$conf->entity;
252  $sql .= " ORDER BY rang ASC";
253 
254  $resql = $this->db->query($sql);
255  if ($resql) {
256  $num = $this->db->num_rows($resql);
257 
258  $i = 0;
259  while ($i < $num) {
260  $fieldlist = array();
261 
262  $obj = $this->db->fetch_object($resql);
263 
264  $fieldlist["id"] = $obj->rowid;
265  $fieldlist["name"] = $obj->name;
266  $fieldlist["alias"] = $obj->alias;
267  $fieldlist["title"] = $langs->trans($obj->title);
268  $fieldlist["align"] = $obj->align;
269  $fieldlist["sort"] = $obj->sort;
270  $fieldlist["search"] = $obj->search;
271  $fieldlist["visible"] = $obj->visible;
272  $fieldlist["enabled"] = verifCond($obj->enabled);
273  $fieldlist["order"] = $obj->rang;
274 
275  array_push($this->field_list, $fieldlist);
276 
277  $i++;
278  }
279  $this->db->free($resql);
280  } else {
281  dol_print_error($this->db, $sql);
282  }
283  }
284 
285 
286  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
296  public function LoadListDatas($limit, $offset, $sortfield, $sortorder)
297  {
298  // phpcs:enable
299  global $conf, $langs;
300 
301  $this->getFieldListCanvas();
302 
303  $this->list_datas = array();
304 
305  // Clean parameters
306  $sall = trim((GETPOST('search_all', 'alphanohtml') != '') ?GETPOST('search_all', 'alphanohtml') : GETPOST('sall', 'alphanohtml'));
307 
308  foreach ($this->field_list as $field) {
309  if ($field['enabled']) {
310  $fieldname = "s".$field['alias'];
311  $$fieldname = GETPOST($fieldname);
312  }
313  }
314 
315  $sql = 'SELECT DISTINCT ';
316 
317  // Fields requiered
318  $sql .= 'p.rowid, p.price_base_type, p.fk_product_type, p.seuil_stock_alerte, p.entity';
319 
320  // Fields not requiered
321  foreach ($this->field_list as $field) {
322  if ($field['enabled']) {
323  $sql .= ", ".$field['name']." as ".$field['alias'];
324  }
325  }
326 
327  $sql .= ' FROM '.MAIN_DB_PREFIX.'product as p';
328  $sql .= " WHERE p.entity IN (".getEntity('product').")";
329 
330  if ($sall) {
331  $clause = '';
332  $sql .= " AND (";
333  foreach ($this->field_list as $field) {
334  if ($field['enabled']) {
335  $sql .= $clause." ".$field['name']." LIKE '%".$this->db->escape($sall)."%'";
336  if ($clause == '') {
337  $clause = ' OR';
338  }
339  }
340  }
341  $sql .= ")";
342  }
343 
344  // Search fields
345  foreach ($this->field_list as $field) {
346  if ($field['enabled']) {
347  $fieldname = "s".$field['alias'];
348  if (${$fieldname}) {
349  $sql .= " AND ".$field['name']." LIKE '%".$this->db->escape(${$fieldname})."%'";
350  }
351  }
352  }
353 
354  if (GETPOSTISSET("tosell")) {
355  $sql .= " AND p.tosell = ".((int) GETPOST("tosell", "int"));
356  }
357  if (GETPOSTISSET("canvas")) {
358  $sql .= " AND p.canvas = '".$this->db->escape(GETPOST("canvas"))."'";
359  }
360  $sql .= $this->db->order($sortfield, $sortorder);
361  $sql .= $this->db->plimit($limit + 1, $offset);
362  //print $sql;
363 
364  $resql = $this->db->query($sql);
365  if ($resql) {
366  $num = $this->db->num_rows($resql);
367 
368  $i = 0;
369  while ($i < min($num, $limit)) {
370  $datas = array();
371 
372  $obj = $this->db->fetch_object($resql);
373 
374  $datas["id"] = $obj->rowid;
375 
376  foreach ($this->field_list as $field) {
377  if ($field['enabled']) {
378  $alias = $field['alias'];
379 
380  if ($alias == 'ref') {
381  $this->id = $obj->rowid;
382  $this->ref = $obj->$alias;
383  $this->type = $obj->fk_product_type;
384  $this->entity = $obj->entity;
385  $datas[$alias] = $this->getNomUrl(1, '', 24);
386  } elseif ($alias == 'stock') {
387  $this->load_stock();
388  if ($this->stock_reel < $obj->seuil_stock_alerte) {
389  $datas[$alias] = $this->stock_reel.' '.img_warning($langs->trans("StockTooLow"));
390  } else {
391  $datas[$alias] = $this->stock_reel;
392  }
393  } elseif ($alias == 'label') {
394  $datas[$alias] = dol_trunc($obj->$alias, 40);
395  } elseif (preg_match('/price/i', $alias)) {
396  $datas[$alias] = price($obj->$alias);
397  } elseif ($alias == 'datem') {
398  $datas[$alias] = dol_print_date($this->db->jdate($obj->$alias), 'day');
399  } elseif ($alias == 'status') {
400  $datas[$alias] = $this->LibStatut($obj->$alias, 5);
401  } else {
402  $datas[$alias] = $obj->$alias;
403  }
404  }
405  }
406 
407  array_push($this->list_datas, $datas);
408 
409  $i++;
410  }
411  $this->db->free($resql);
412  } else {
413  dol_print_error($this->db);
414  }
415  }
416 }
db
$conf db
API class for accounts.
Definition: inc.php:41
get_htmloutput_errors
get_htmloutput_errors($mesgstring='', $mesgarray=array(), $keepembedded=0)
Get formated error messages to output (Used to show messages on html output).
Definition: functions.lib.php:8281
dol_trunc
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
Definition: functions.lib.php:3805
description
print *****$script_file(".$version.") pid cd cd cd description as description
Definition: email_expire_services_to_customers.php:83
ActionsCardProduct\__construct
__construct($db, $dirmodule, $targetmodule, $canvas, $card)
Constructor.
Definition: actions_card_product.class.php:54
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
ActionsCardProduct\getFieldListCanvas
getFieldListCanvas()
Fetch field list.
Definition: actions_card_product.class.php:242
verifCond
verifCond($strToEvaluate)
Verify if condition in string is ok or not.
Definition: functions.lib.php:8582
img_warning
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
Definition: functions.lib.php:4521
product_prepare_head
product_prepare_head($object)
Prepare array with list of tabs.
Definition: product.lib.php:35
ref
$object ref
Definition: info.php:77
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
name
$conf db name
Definition: repair.php:122
measuringUnitString
measuringUnitString($unit, $measuring_style='', $scale='', $use_short_label=0, $outputlangs=null)
Return translation label of a unit key.
Definition: product.lib.php:619
ActionsCardProduct\$tpl
$tpl
Template container.
Definition: actions_card_product.class.php:38
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
ActionsCardProduct\assign_values
assign_values(&$action, $id=0, $ref='')
Assign custom values for canvas (for example into this->tpl to be used by templates)
Definition: actions_card_product.class.php:78
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
Product
Class to manage products or services.
Definition: product.class.php:46
ActionsCardProduct\LoadListDatas
LoadListDatas($limit, $offset, $sortfield, $sortorder)
Fetch datas list and save into ->list_datas.
Definition: actions_card_product.class.php:296
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
price
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.
Definition: functions.lib.php:5541
Product\TYPE_SERVICE
const TYPE_SERVICE
Service.
Definition: product.class.php:504
ActionsCardProduct
Class with controller methods for product canvas.
Definition: actions_card_product.class.php:29
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119