dolibarr  17.0.3
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  $sql .= 'fk_product,';
118  $sql .= 'fk_entrepot,';
119  $sql .= 'seuil_stock_alerte,';
120  $sql .= 'desiredstock,';
121  $sql .= 'import_key';
122  $sql .= ') VALUES (';
123  $sql .= ' '.(!isset($this->fk_product) ? 'NULL' : $this->fk_product).',';
124  $sql .= ' '.(!isset($this->fk_entrepot) ? 'NULL' : $this->fk_entrepot).',';
125  $sql .= ' '.(!isset($this->seuil_stock_alerte) ? '0' : $this->seuil_stock_alerte).',';
126  $sql .= ' '.(!isset($this->desiredstock) ? '0' : $this->desiredstock).',';
127  $sql .= ' '.(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
128  $sql .= ')';
129 
130  $this->db->begin();
131 
132  $resql = $this->db->query($sql);
133  if (!$resql) {
134  $error++;
135  $this->errors[] = 'Error '.$this->db->lasterror();
136  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
137  }
138 
139  if (!$error) {
140  $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
141 
142  //if (!$notrigger) {
143  // Uncomment this and change MYOBJECT to your own tag if you
144  // want this action to call a trigger.
145 
147  //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
148  //if ($result < 0) $error++;
150  //}
151  }
152 
153  // Commit or rollback
154  if ($error) {
155  $this->db->rollback();
156 
157  return -1 * $error;
158  } else {
159  $this->db->commit();
160 
161  return $this->id;
162  }
163  }
164 
173  public function fetch($id, $fk_product = 0, $fk_entrepot = 0)
174  {
175  if (empty($id) && (empty($fk_product) || empty($fk_entrepot))) {
176  return -1;
177  }
178 
179  dol_syslog(__METHOD__, LOG_DEBUG);
180 
181  $sql = "SELECT";
182  $sql .= " t.rowid,";
183  $sql .= " t.tms,";
184  $sql .= " t.fk_product,";
185  $sql .= " t.fk_entrepot,";
186  $sql .= " t.seuil_stock_alerte,";
187  $sql .= " t.desiredstock,";
188  $sql .= " t.import_key";
189  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
190  if (!empty($id)) {
191  $sql .= " WHERE t.rowid = ".((int) $id);
192  } else {
193  $sql .= " WHERE t.fk_product = ".((int) $fk_product)." AND t.fk_entrepot = ".((int) $fk_entrepot);
194  }
195 
196  $resql = $this->db->query($sql);
197  if ($resql) {
198  $numrows = $this->db->num_rows($resql);
199  if ($numrows) {
200  $obj = $this->db->fetch_object($resql);
201 
202  $this->id = $obj->rowid;
203 
204  $this->tms = $this->db->jdate($obj->tms);
205  $this->fk_product = $obj->fk_product;
206  $this->fk_entrepot = $obj->fk_entrepot;
207  $this->seuil_stock_alerte = $obj->seuil_stock_alerte;
208  $this->desiredstock = $obj->desiredstock;
209  $this->import_key = $obj->import_key;
210  }
211 
212  // Retrieve all extrafield
213  // fetch optionals attributes and labels
214  $this->fetch_optionals();
215 
216  // $this->fetch_lines();
217 
218  $this->db->free($resql);
219 
220  if ($numrows) {
221  return 1;
222  } else {
223  return 0;
224  }
225  } else {
226  $this->errors[] = 'Error '.$this->db->lasterror();
227  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
228 
229  return -1;
230  }
231  }
232 
247  public function fetchAll($fk_product = '', $fk_entrepot = '', $sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
248  {
249  dol_syslog(__METHOD__, LOG_DEBUG);
250 
251  $sql = "SELECT";
252  $sql .= " t.rowid,";
253 
254  $sql .= " t.tms,";
255  $sql .= " t.fk_product,";
256  $sql .= " t.fk_entrepot,";
257  $sql .= " t.seuil_stock_alerte,";
258  $sql .= " t.desiredstock,";
259  $sql .= " t.import_key";
260 
261 
262  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
263 
264  $sql .= " WHERE 1=1";
265 
266  // Manage filter
267  $sqlwhere = array();
268  if (count($filter) > 0) {
269  foreach ($filter as $key => $value) {
270  $sqlwhere [] = $key." LIKE '%".$this->db->escape($value)."%'";
271  }
272  }
273  if (count($sqlwhere) > 0) {
274  $sql .= " AND ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
275  }
276 
277  if (!empty($fk_product) && $fk_product > 0) {
278  $sql .= " AND fk_product = ".((int) $fk_product);
279  } elseif (!empty($fk_entrepot) && $fk_entrepot > 0) {
280  $sql .= " AND fk_entrepot = ".((int) $fk_entrepot);
281  }
282  // "elseif" used instead of "if" because getting list with specified fk_product and specified fk_entrepot would be the same as doing a fetch
283 
284  if (!empty($sortfield)) {
285  $sql .= $this->db->order($sortfield, $sortorder);
286  }
287  if (!empty($limit)) {
288  $sql .= $this->db->plimit($limit, $offset);
289  }
290 
291  $lines = array();
292 
293  $resql = $this->db->query($sql);
294  if ($resql) {
295  while ($obj = $this->db->fetch_object($resql)) {
296  $lines[$obj->rowid] = array(
297  'id'=>$obj->rowid
298  ,'fk_product'=>$obj->fk_product
299  ,'fk_entrepot'=>$obj->fk_entrepot
300  ,'seuil_stock_alerte'=>$obj->seuil_stock_alerte
301  ,'desiredstock'=>$obj->desiredstock
302  );
303  }
304  $this->db->free($resql);
305 
306  return $lines;
307  } else {
308  $this->errors[] = 'Error '.$this->db->lasterror();
309  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
310 
311  return -1;
312  }
313  }
314 
323  public function update(User $user, $notrigger = false)
324  {
325  $error = 0;
326 
327  dol_syslog(__METHOD__, LOG_DEBUG);
328 
329  // Clean parameters
330 
331  if (isset($this->fk_product)) {
332  $this->fk_product = (int) $this->fk_product;
333  }
334  if (isset($this->fk_entrepot)) {
335  $this->fk_entrepot = (int) $this->fk_entrepot;
336  }
337  if (isset($this->seuil_stock_alerte)) {
338  $this->seuil_stock_alerte = trim($this->seuil_stock_alerte);
339  }
340  if (isset($this->desiredstock)) {
341  $this->desiredstock = trim($this->desiredstock);
342  }
343  if (isset($this->import_key)) {
344  $this->import_key = trim($this->import_key);
345  }
346 
347 
348  // Check parameters
349  // Put here code to add a control on parameters values
350 
351  // Update request
352  $sql = 'UPDATE '.$this->db->prefix().$this->table_element.' SET';
353 
354  $sql .= ' tms = '.(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : "'".$this->db->idate(dol_now())."'").',';
355  $sql .= ' fk_product = '.(isset($this->fk_product) ? $this->fk_product : "null").',';
356  $sql .= ' fk_entrepot = '.(isset($this->fk_entrepot) ? $this->fk_entrepot : "null").',';
357  $sql .= ' seuil_stock_alerte = '.(isset($this->seuil_stock_alerte) ? $this->seuil_stock_alerte : "null").',';
358  $sql .= ' desiredstock = '.(isset($this->desiredstock) ? $this->desiredstock : "null").',';
359  $sql .= ' import_key = '.(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
360 
361 
362  $sql .= ' WHERE rowid='.((int) $this->id);
363 
364  $this->db->begin();
365 
366  $resql = $this->db->query($sql);
367  if (!$resql) {
368  $error++;
369  $this->errors[] = 'Error '.$this->db->lasterror();
370  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
371  }
372 
373  //if (!$error && !$notrigger) {
374  // Uncomment this and change MYOBJECT to your own tag if you
375  // want this action calls a trigger.
376 
378  //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
379  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
381  //}
382 
383  // Commit or rollback
384  if ($error) {
385  $this->db->rollback();
386 
387  return -1 * $error;
388  } else {
389  $this->db->commit();
390 
391  return 1;
392  }
393  }
394 
403  public function delete(User $user, $notrigger = false)
404  {
405  dol_syslog(__METHOD__, LOG_DEBUG);
406 
407  $error = 0;
408 
409  $this->db->begin();
410 
411  //if (!$error && !$notrigger) {
412  // Uncomment this and change MYOBJECT to your own tag if you
413  // want this action calls a trigger.
414 
416  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
417  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
419  //}
420 
421  if (!$error) {
422  $sql = 'DELETE FROM '.$this->db->prefix().$this->table_element;
423  $sql .= ' WHERE rowid='.((int) $this->id);
424 
425  $resql = $this->db->query($sql);
426  if (!$resql) {
427  $error++;
428  $this->errors[] = 'Error '.$this->db->lasterror();
429  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
430  }
431  }
432 
433  // Commit or rollback
434  if ($error) {
435  $this->db->rollback();
436 
437  return -1 * $error;
438  } else {
439  $this->db->commit();
440 
441  return 1;
442  }
443  }
444 
452  public function createFromClone(User $user, $fromid)
453  {
454  dol_syslog(__METHOD__, LOG_DEBUG);
455 
456  $error = 0;
457  $object = new ProductStockEntrepot($this->db);
458 
459  $this->db->begin();
460 
461  // Load source object
462  $object->fetch($fromid);
463  // Reset object
464  $object->id = 0;
465 
466  // Clear fields
467  // ...
468 
469  // Create clone
470  $object->context['createfromclone'] = 'createfromclone';
471  $result = $object->create($user);
472 
473  // Other options
474  if ($result < 0) {
475  $error++;
476  $this->errors = $object->errors;
477  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
478  }
479 
480  unset($object->context['createfromclone']);
481 
482  // End
483  if (!$error) {
484  $this->db->commit();
485 
486  return $object->id;
487  } else {
488  $this->db->rollback();
489 
490  return -1;
491  }
492  }
493 
505  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
506  {
507  global $langs, $conf, $db;
508  global $dolibarr_main_authentication, $dolibarr_main_demo;
509  global $menumanager;
510 
511 
512  $result = '';
513  $companylink = '';
514 
515  $label = '<u>'.$langs->trans("MyModule").'</u>';
516  $label .= '<div width="100%">';
517  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
518 
519  $link = '<a href="'.DOL_URL_ROOT.'/ProductEntrepot/card.php?id='.$this->id.'"';
520  $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
521  $link .= '>';
522  $linkend = '</a>';
523 
524  if ($withpicto) {
525  $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
526  if ($withpicto != 2) {
527  $result .= ' ';
528  }
529  }
530  $result .= $link.$this->ref.$linkend;
531  return $result;
532  }
533 
540  public function getLibStatut($mode = 0)
541  {
542  return $this->LibStatut($this->status, $mode);
543  }
544 
545  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
553  public function LibStatut($status, $mode = 0)
554  {
555  // phpcs:enable
556  global $langs;
557 
558  if ($mode == 0) {
559  if ($status == 1) {
560  return $langs->trans('Enabled');
561  } elseif ($status == 0) {
562  return $langs->trans('Disabled');
563  }
564  } elseif ($mode == 1) {
565  if ($status == 1) {
566  return $langs->trans('Enabled');
567  } elseif ($status == 0) {
568  return $langs->trans('Disabled');
569  }
570  } elseif ($mode == 2) {
571  if ($status == 1) {
572  return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
573  } elseif ($status == 0) {
574  return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
575  }
576  } elseif ($mode == 3) {
577  if ($status == 1) {
578  return img_picto($langs->trans('Enabled'), 'statut4');
579  } elseif ($status == 0) {
580  return img_picto($langs->trans('Disabled'), 'statut5');
581  }
582  } elseif ($mode == 4) {
583  if ($status == 1) {
584  return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
585  } elseif ($status == 0) {
586  return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
587  }
588  } elseif ($mode == 5) {
589  if ($status == 1) {
590  return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
591  } elseif ($status == 0) {
592  return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
593  }
594  }
595  }
596 
597 
604  public function initAsSpecimen()
605  {
606  $this->id = 0;
607 
608  $this->tms = '';
609  $this->fk_product = null;
610  $this->fk_entrepot = null;
611  $this->seuil_stock_alerte = '';
612  $this->desiredstock = '';
613  $this->import_key = '';
614  }
615 }
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:505
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:4024
ProductStockEntrepot\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: productstockentrepot.class.php:604
ProductStockEntrepot\create
create(User $user, $notrigger=false)
Create object into database.
Definition: productstockentrepot.class.php:88
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("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->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
ProductStockEntrepot\LibStatut
LibStatut($status, $mode=0)
Renvoi le libelle d'un status donne.
Definition: productstockentrepot.class.php:553
ProductStockEntrepot\update
update(User $user, $notrigger=false)
Update object into database.
Definition: productstockentrepot.class.php:323
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
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:6034
ProductStockEntrepot
Class ProductStockEntrepot.
Definition: productstockentrepot.class.php:41
User
Class to manage Dolibarr users.
Definition: user.class.php:46
ProductStockEntrepot\fetch
fetch($id, $fk_product=0, $fk_entrepot=0)
Load object in memory from the database.
Definition: productstockentrepot.class.php:173
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2951
ProductStockEntrepot\getLibStatut
getLibStatut($mode=0)
Retourne le libelle du status d'un user (actif, inactif)
Definition: productstockentrepot.class.php:540
ProductStockEntrepot\createFromClone
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
Definition: productstockentrepot.class.php:452
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:247