dolibarr  16.0.5
productstockentrepot.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2014-2016 Juanjo Menent <jmenent@2byte.es>
4  * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
5  * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
6  * Copyright (C) 2018-2019 Frédéric France <frederic.france@netlogic.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
29 // Put here all includes required by your class file
30 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
31 //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
32 //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
33 
42 {
46  public $element = 'ProductStockEntrepot';
47 
51  public $table_element = 'product_warehouse_properties';
52 
53  public $tms = '';
54 
58  public $fk_product;
59 
63  public $fk_entrepot;
64 
65  public $seuil_stock_alerte;
66  public $desiredstock;
67  public $import_key;
68 
69 
75  public function __construct(DoliDB $db)
76  {
77  $this->db = $db;
78  }
79 
88  public function create(User $user, $notrigger = false)
89  {
90  dol_syslog(__METHOD__, LOG_DEBUG);
91 
92  $error = 0;
93 
94  // Clean parameters
95 
96  if (isset($this->fk_product)) {
97  $this->fk_product = (int) $this->fk_product;
98  }
99  if (isset($this->fk_entrepot)) {
100  $this->fk_entrepot = (int) $this->fk_entrepot;
101  }
102  if (isset($this->seuil_stock_alerte)) {
103  $this->seuil_stock_alerte = trim($this->seuil_stock_alerte);
104  }
105  if (isset($this->desiredstock)) {
106  $this->desiredstock = trim($this->desiredstock);
107  }
108  if (isset($this->import_key)) {
109  $this->import_key = trim($this->import_key);
110  }
111 
112  // Check parameters
113  // Put here code to add control on parameters values
114 
115  // Insert request
116  $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
117 
118  $sql .= 'fk_product,';
119  $sql .= 'fk_entrepot,';
120  $sql .= 'seuil_stock_alerte,';
121  $sql .= 'desiredstock,';
122  $sql .= 'import_key';
123 
124 
125  $sql .= ') VALUES (';
126 
127  $sql .= ' '.(!isset($this->fk_product) ? 'NULL' : $this->fk_product).',';
128  $sql .= ' '.(!isset($this->fk_entrepot) ? 'NULL' : $this->fk_entrepot).',';
129  $sql .= ' '.(!isset($this->seuil_stock_alerte) ? '0' : $this->seuil_stock_alerte).',';
130  $sql .= ' '.(!isset($this->desiredstock) ? '0' : $this->desiredstock).',';
131  $sql .= ' '.(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
132 
133 
134  $sql .= ')';
135 
136  $this->db->begin();
137 
138  $resql = $this->db->query($sql);
139  if (!$resql) {
140  $error++;
141  $this->errors[] = 'Error '.$this->db->lasterror();
142  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
143  }
144 
145  if (!$error) {
146  $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
147 
148  //if (!$notrigger) {
149  // Uncomment this and change MYOBJECT to your own tag if you
150  // want this action to call a trigger.
151 
153  //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
154  //if ($result < 0) $error++;
156  //}
157  }
158 
159  // Commit or rollback
160  if ($error) {
161  $this->db->rollback();
162 
163  return -1 * $error;
164  } else {
165  $this->db->commit();
166 
167  return $this->id;
168  }
169  }
170 
179  public function fetch($id, $fk_product = 0, $fk_entrepot = 0)
180  {
181  if (empty($id) && (empty($fk_product) || empty($fk_entrepot))) {
182  return -1;
183  }
184 
185  dol_syslog(__METHOD__, LOG_DEBUG);
186 
187  $sql = "SELECT";
188  $sql .= " t.rowid,";
189  $sql .= " t.tms,";
190  $sql .= " t.fk_product,";
191  $sql .= " t.fk_entrepot,";
192  $sql .= " t.seuil_stock_alerte,";
193  $sql .= " t.desiredstock,";
194  $sql .= " t.import_key";
195  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
196  if (!empty($id)) {
197  $sql .= " WHERE t.rowid = ".((int) $id);
198  } else {
199  $sql .= " WHERE t.fk_product = ".((int) $fk_product)." AND t.fk_entrepot = ".((int) $fk_entrepot);
200  }
201 
202  $resql = $this->db->query($sql);
203  if ($resql) {
204  $numrows = $this->db->num_rows($resql);
205  if ($numrows) {
206  $obj = $this->db->fetch_object($resql);
207 
208  $this->id = $obj->rowid;
209 
210  $this->tms = $this->db->jdate($obj->tms);
211  $this->fk_product = $obj->fk_product;
212  $this->fk_entrepot = $obj->fk_entrepot;
213  $this->seuil_stock_alerte = $obj->seuil_stock_alerte;
214  $this->desiredstock = $obj->desiredstock;
215  $this->import_key = $obj->import_key;
216  }
217 
218  // Retrieve all extrafield
219  // fetch optionals attributes and labels
220  $this->fetch_optionals();
221 
222  // $this->fetch_lines();
223 
224  $this->db->free($resql);
225 
226  if ($numrows) {
227  return 1;
228  } else {
229  return 0;
230  }
231  } else {
232  $this->errors[] = 'Error '.$this->db->lasterror();
233  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
234 
235  return -1;
236  }
237  }
238 
253  public function fetchAll($fk_product = '', $fk_entrepot = '', $sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
254  {
255  dol_syslog(__METHOD__, LOG_DEBUG);
256 
257  $sql = "SELECT";
258  $sql .= " t.rowid,";
259 
260  $sql .= " t.tms,";
261  $sql .= " t.fk_product,";
262  $sql .= " t.fk_entrepot,";
263  $sql .= " t.seuil_stock_alerte,";
264  $sql .= " t.desiredstock,";
265  $sql .= " t.import_key";
266 
267 
268  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
269 
270  $sql .= " WHERE 1=1";
271 
272  // Manage filter
273  $sqlwhere = array();
274  if (count($filter) > 0) {
275  foreach ($filter as $key => $value) {
276  $sqlwhere [] = $key." LIKE '%".$this->db->escape($value)."%'";
277  }
278  }
279  if (count($sqlwhere) > 0) {
280  $sql .= " AND ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
281  }
282 
283  if (!empty($fk_product) && $fk_product > 0) {
284  $sql .= " AND fk_product = ".((int) $fk_product);
285  } elseif (!empty($fk_entrepot) && $fk_entrepot > 0) {
286  $sql .= " AND fk_entrepot = ".((int) $fk_entrepot);
287  }
288  // "elseif" used instead of "if" because getting list with specified fk_product and specified fk_entrepot would be the same as doing a fetch
289 
290  if (!empty($sortfield)) {
291  $sql .= $this->db->order($sortfield, $sortorder);
292  }
293  if (!empty($limit)) {
294  $sql .= $this->db->plimit($limit, $offset);
295  }
296 
297  $lines = array();
298 
299  $resql = $this->db->query($sql);
300  if ($resql) {
301  while ($obj = $this->db->fetch_object($resql)) {
302  $lines[$obj->rowid] = array(
303  'id'=>$obj->rowid
304  ,'fk_product'=>$obj->fk_product
305  ,'fk_entrepot'=>$obj->fk_entrepot
306  ,'seuil_stock_alerte'=>$obj->seuil_stock_alerte
307  ,'desiredstock'=>$obj->desiredstock
308  );
309  }
310  $this->db->free($resql);
311 
312  return $lines;
313  } else {
314  $this->errors[] = 'Error '.$this->db->lasterror();
315  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
316 
317  return -1;
318  }
319  }
320 
329  public function update(User $user, $notrigger = false)
330  {
331  $error = 0;
332 
333  dol_syslog(__METHOD__, LOG_DEBUG);
334 
335  // Clean parameters
336 
337  if (isset($this->fk_product)) {
338  $this->fk_product = (int) $this->fk_product;
339  }
340  if (isset($this->fk_entrepot)) {
341  $this->fk_entrepot = (int) $this->fk_entrepot;
342  }
343  if (isset($this->seuil_stock_alerte)) {
344  $this->seuil_stock_alerte = trim($this->seuil_stock_alerte);
345  }
346  if (isset($this->desiredstock)) {
347  $this->desiredstock = trim($this->desiredstock);
348  }
349  if (isset($this->import_key)) {
350  $this->import_key = trim($this->import_key);
351  }
352 
353 
354  // Check parameters
355  // Put here code to add a control on parameters values
356 
357  // Update request
358  $sql = 'UPDATE '.$this->db->prefix().$this->table_element.' SET';
359 
360  $sql .= ' tms = '.(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : "'".$this->db->idate(dol_now())."'").',';
361  $sql .= ' fk_product = '.(isset($this->fk_product) ? $this->fk_product : "null").',';
362  $sql .= ' fk_entrepot = '.(isset($this->fk_entrepot) ? $this->fk_entrepot : "null").',';
363  $sql .= ' seuil_stock_alerte = '.(isset($this->seuil_stock_alerte) ? $this->seuil_stock_alerte : "null").',';
364  $sql .= ' desiredstock = '.(isset($this->desiredstock) ? $this->desiredstock : "null").',';
365  $sql .= ' import_key = '.(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
366 
367 
368  $sql .= ' WHERE rowid='.((int) $this->id);
369 
370  $this->db->begin();
371 
372  $resql = $this->db->query($sql);
373  if (!$resql) {
374  $error++;
375  $this->errors[] = 'Error '.$this->db->lasterror();
376  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
377  }
378 
379  //if (!$error && !$notrigger) {
380  // Uncomment this and change MYOBJECT to your own tag if you
381  // want this action calls a trigger.
382 
384  //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
385  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
387  //}
388 
389  // Commit or rollback
390  if ($error) {
391  $this->db->rollback();
392 
393  return -1 * $error;
394  } else {
395  $this->db->commit();
396 
397  return 1;
398  }
399  }
400 
409  public function delete(User $user, $notrigger = false)
410  {
411  dol_syslog(__METHOD__, LOG_DEBUG);
412 
413  $error = 0;
414 
415  $this->db->begin();
416 
417  //if (!$error && !$notrigger) {
418  // Uncomment this and change MYOBJECT to your own tag if you
419  // want this action calls a trigger.
420 
422  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
423  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
425  //}
426 
427  if (!$error) {
428  $sql = 'DELETE FROM '.$this->db->prefix().$this->table_element;
429  $sql .= ' WHERE rowid='.((int) $this->id);
430 
431  $resql = $this->db->query($sql);
432  if (!$resql) {
433  $error++;
434  $this->errors[] = 'Error '.$this->db->lasterror();
435  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
436  }
437  }
438 
439  // Commit or rollback
440  if ($error) {
441  $this->db->rollback();
442 
443  return -1 * $error;
444  } else {
445  $this->db->commit();
446 
447  return 1;
448  }
449  }
450 
458  public function createFromClone(User $user, $fromid)
459  {
460  dol_syslog(__METHOD__, LOG_DEBUG);
461 
462  $error = 0;
463  $object = new ProductStockEntrepot($this->db);
464 
465  $this->db->begin();
466 
467  // Load source object
468  $object->fetch($fromid);
469  // Reset object
470  $object->id = 0;
471 
472  // Clear fields
473  // ...
474 
475  // Create clone
476  $object->context['createfromclone'] = 'createfromclone';
477  $result = $object->create($user);
478 
479  // Other options
480  if ($result < 0) {
481  $error++;
482  $this->errors = $object->errors;
483  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
484  }
485 
486  unset($object->context['createfromclone']);
487 
488  // End
489  if (!$error) {
490  $this->db->commit();
491 
492  return $object->id;
493  } else {
494  $this->db->rollback();
495 
496  return -1;
497  }
498  }
499 
511  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
512  {
513  global $langs, $conf, $db;
514  global $dolibarr_main_authentication, $dolibarr_main_demo;
515  global $menumanager;
516 
517 
518  $result = '';
519  $companylink = '';
520 
521  $label = '<u>'.$langs->trans("MyModule").'</u>';
522  $label .= '<div width="100%">';
523  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
524 
525  $link = '<a href="'.DOL_URL_ROOT.'/ProductEntrepot/card.php?id='.$this->id.'"';
526  $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
527  $link .= '>';
528  $linkend = '</a>';
529 
530  if ($withpicto) {
531  $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
532  if ($withpicto != 2) {
533  $result .= ' ';
534  }
535  }
536  $result .= $link.$this->ref.$linkend;
537  return $result;
538  }
539 
546  public function getLibStatut($mode = 0)
547  {
548  return $this->LibStatut($this->status, $mode);
549  }
550 
551  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
559  public function LibStatut($status, $mode = 0)
560  {
561  // phpcs:enable
562  global $langs;
563 
564  if ($mode == 0) {
565  if ($status == 1) {
566  return $langs->trans('Enabled');
567  } elseif ($status == 0) {
568  return $langs->trans('Disabled');
569  }
570  } elseif ($mode == 1) {
571  if ($status == 1) {
572  return $langs->trans('Enabled');
573  } elseif ($status == 0) {
574  return $langs->trans('Disabled');
575  }
576  } elseif ($mode == 2) {
577  if ($status == 1) {
578  return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
579  } elseif ($status == 0) {
580  return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
581  }
582  } elseif ($mode == 3) {
583  if ($status == 1) {
584  return img_picto($langs->trans('Enabled'), 'statut4');
585  } elseif ($status == 0) {
586  return img_picto($langs->trans('Disabled'), 'statut5');
587  }
588  } elseif ($mode == 4) {
589  if ($status == 1) {
590  return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
591  } elseif ($status == 0) {
592  return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
593  }
594  } elseif ($mode == 5) {
595  if ($status == 1) {
596  return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
597  } elseif ($status == 0) {
598  return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
599  }
600  }
601  }
602 
603 
610  public function initAsSpecimen()
611  {
612  $this->id = 0;
613 
614  $this->tms = '';
615  $this->fk_product = null;
616  $this->fk_entrepot = null;
617  $this->seuil_stock_alerte = '';
618  $this->desiredstock = '';
619  $this->import_key = '';
620  }
621 }
ProductStockEntrepot\__construct
__construct(DoliDB $db)
Constructor.
Definition: productstockentrepot.class.php:75
db
$conf db
API class for accounts.
Definition: inc.php:41
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
ProductStockEntrepot\getNomUrl
getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
Return a link to the user card (with optionaly the picto) Use this->id,this->lastname,...
Definition: productstockentrepot.class.php:511
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
ProductStockEntrepot\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: productstockentrepot.class.php:610
ProductStockEntrepot\create
create(User $user, $notrigger=false)
Create object into database.
Definition: productstockentrepot.class.php:88
ProductStockEntrepot\LibStatut
LibStatut($status, $mode=0)
Renvoi le libelle d'un status donne.
Definition: productstockentrepot.class.php:559
ProductStockEntrepot\update
update(User $user, $notrigger=false)
Update object into database.
Definition: productstockentrepot.class.php:329
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
CommonObject\fetch_optionals
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
Definition: commonobject.class.php:6007
ProductStockEntrepot
Class ProductStockEntrepot.
Definition: productstockentrepot.class.php:41
User
Class to manage Dolibarr users.
Definition: user.class.php:44
ProductStockEntrepot\fetch
fetch($id, $fk_product=0, $fk_entrepot=0)
Load object in memory from the database.
Definition: productstockentrepot.class.php:179
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$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
ProductStockEntrepot\getLibStatut
getLibStatut($mode=0)
Retourne le libelle du status d'un user (actif, inactif)
Definition: productstockentrepot.class.php:546
ProductStockEntrepot\createFromClone
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
Definition: productstockentrepot.class.php:458
ProductStockEntrepot\fetchAll
fetchAll($fk_product='', $fk_entrepot='', $sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load object in memory from the database.
Definition: productstockentrepot.class.php:253