dolibarr 21.0.0-alpha
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-2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29// Put here all includes required by your class file
30require_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
34
41{
45 public $element = 'ProductStockEntrepot';
46
50 public $table_element = 'product_warehouse_properties';
51
55 public $fk_product;
56
60 public $fk_entrepot;
61
62 public $seuil_stock_alerte;
63 public $desiredstock;
64 public $import_key;
65
66
72 public function __construct(DoliDB $db)
73 {
74 $this->db = $db;
75 }
76
84 public function create(User $user, $notrigger = 0)
85 {
86 dol_syslog(__METHOD__, LOG_DEBUG);
87
88 $error = 0;
89
90 // Clean parameters
91
92 if (isset($this->fk_product)) {
93 $this->fk_product = (int) $this->fk_product;
94 }
95 if (isset($this->fk_entrepot)) {
96 $this->fk_entrepot = (int) $this->fk_entrepot;
97 }
98 if (isset($this->seuil_stock_alerte)) {
99 $this->seuil_stock_alerte = trim($this->seuil_stock_alerte);
100 }
101 if (isset($this->desiredstock)) {
102 $this->desiredstock = trim($this->desiredstock);
103 }
104 if (isset($this->import_key)) {
105 $this->import_key = trim($this->import_key);
106 }
107
108 // Check parameters
109 // Put here code to add control on parameters values
110
111 // Insert request
112 $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
113 $sql .= 'fk_product,';
114 $sql .= 'fk_entrepot,';
115 $sql .= 'seuil_stock_alerte,';
116 $sql .= 'desiredstock,';
117 $sql .= 'import_key';
118 $sql .= ') VALUES (';
119 $sql .= ' '.(!isset($this->fk_product) ? 'NULL' : $this->fk_product).',';
120 $sql .= ' '.(!isset($this->fk_entrepot) ? 'NULL' : $this->fk_entrepot).',';
121 $sql .= ' '.(!isset($this->seuil_stock_alerte) ? '0' : $this->seuil_stock_alerte).',';
122 $sql .= ' '.(!isset($this->desiredstock) ? '0' : $this->desiredstock).',';
123 $sql .= ' '.(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
124 $sql .= ')';
125
126 $this->db->begin();
127
128 $resql = $this->db->query($sql);
129 if (!$resql) {
130 $error++;
131 $this->errors[] = 'Error '.$this->db->lasterror();
132 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
133 }
134
135 if (!$error) {
136 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
137
138 //if (!$notrigger) {
139 // Uncomment this and change MYOBJECT to your own tag if you
140 // want this action to call a trigger.
141
143 //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
144 //if ($result < 0) $error++;
146 //}
147 }
148
149 // Commit or rollback
150 if ($error) {
151 $this->db->rollback();
152
153 return -1 * $error;
154 } else {
155 $this->db->commit();
156
157 return $this->id;
158 }
159 }
160
169 public function fetch($id, $fk_product = 0, $fk_entrepot = 0)
170 {
171 if (empty($id) && (empty($fk_product) || empty($fk_entrepot))) {
172 return -1;
173 }
174
175 dol_syslog(__METHOD__, LOG_DEBUG);
176
177 $sql = "SELECT";
178 $sql .= " t.rowid,";
179 $sql .= " t.tms,";
180 $sql .= " t.fk_product,";
181 $sql .= " t.fk_entrepot,";
182 $sql .= " t.seuil_stock_alerte,";
183 $sql .= " t.desiredstock,";
184 $sql .= " t.import_key";
185 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
186 if (!empty($id)) {
187 $sql .= " WHERE t.rowid = ".((int) $id);
188 } else {
189 $sql .= " WHERE t.fk_product = ".((int) $fk_product)." AND t.fk_entrepot = ".((int) $fk_entrepot);
190 }
191
192 $resql = $this->db->query($sql);
193 if ($resql) {
194 $numrows = $this->db->num_rows($resql);
195 if ($numrows) {
196 $obj = $this->db->fetch_object($resql);
197
198 $this->id = $obj->rowid;
199
200 $this->tms = $this->db->jdate($obj->tms);
201 $this->fk_product = $obj->fk_product;
202 $this->fk_entrepot = $obj->fk_entrepot;
203 $this->seuil_stock_alerte = $obj->seuil_stock_alerte;
204 $this->desiredstock = $obj->desiredstock;
205 $this->import_key = $obj->import_key;
206 }
207
208 // Retrieve all extrafield
209 // fetch optionals attributes and labels
210 $this->fetch_optionals();
211
212 // $this->fetch_lines();
213
214 $this->db->free($resql);
215
216 if ($numrows) {
217 return 1;
218 } else {
219 return 0;
220 }
221 } else {
222 $this->errors[] = 'Error '.$this->db->lasterror();
223 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
224
225 return -1;
226 }
227 }
228
242 public function fetchAll($fk_product = 0, $fk_entrepot = 0, $sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
243 {
244 dol_syslog(__METHOD__, LOG_DEBUG);
245
246 $sql = "SELECT";
247 $sql .= " t.rowid,";
248 $sql .= " t.tms,";
249 $sql .= " t.fk_product,";
250 $sql .= " t.fk_entrepot,";
251 $sql .= " t.seuil_stock_alerte,";
252 $sql .= " t.desiredstock,";
253 $sql .= " t.import_key";
254 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
255 $sql .= " WHERE 1=1";
256
257 // Manage filter
258 if (is_array($filter)) {
259 $sqlwhere = array();
260 if (count($filter) > 0) {
261 foreach ($filter as $key => $value) {
262 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
263 }
264 }
265 if (count($sqlwhere) > 0) {
266 $sql .= " AND ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
267 }
268
269 $filter = '';
270 }
271
272 // Manage filter
273 $errormessage = '';
274 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
275 if ($errormessage) {
276 $this->errors[] = $errormessage;
277 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
278 return -1;
279 }
280
281 if (!empty($fk_product) && $fk_product > 0) {
282 $sql .= " AND fk_product = ".((int) $fk_product);
283 } elseif (!empty($fk_entrepot) && $fk_entrepot > 0) {
284 $sql .= " AND fk_entrepot = ".((int) $fk_entrepot);
285 }
286 // "elseif" used instead of "if" because getting list with specified fk_product and specified fk_entrepot would be the same as doing a fetch
287
288 if (!empty($sortfield)) {
289 $sql .= $this->db->order($sortfield, $sortorder);
290 }
291 if (!empty($limit)) {
292 $sql .= $this->db->plimit($limit, $offset);
293 }
294
295 $lines = array();
296
297 $resql = $this->db->query($sql);
298 if ($resql) {
299 while ($obj = $this->db->fetch_object($resql)) {
300 $lines[$obj->rowid] = array(
301 'id' => $obj->rowid
302 ,'fk_product' => $obj->fk_product
303 ,'fk_entrepot' => $obj->fk_entrepot
304 ,'seuil_stock_alerte' => $obj->seuil_stock_alerte
305 ,'desiredstock' => $obj->desiredstock
306 );
307 }
308 $this->db->free($resql);
309
310 return $lines;
311 } else {
312 $this->errors[] = 'Error '.$this->db->lasterror();
313 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
314
315 return -1;
316 }
317 }
318
326 public function update(User $user, $notrigger = 0)
327 {
328 $error = 0;
329
330 dol_syslog(__METHOD__, LOG_DEBUG);
331
332 // Clean parameters
333
334 if (isset($this->fk_product)) {
335 $this->fk_product = (int) $this->fk_product;
336 }
337 if (isset($this->fk_entrepot)) {
338 $this->fk_entrepot = (int) $this->fk_entrepot;
339 }
340 if (isset($this->seuil_stock_alerte)) {
341 $this->seuil_stock_alerte = trim($this->seuil_stock_alerte);
342 }
343 if (isset($this->desiredstock)) {
344 $this->desiredstock = trim($this->desiredstock);
345 }
346 if (isset($this->import_key)) {
347 $this->import_key = trim($this->import_key);
348 }
349
350
351 // Check parameters
352 // Put here code to add a control on parameters values
353
354 // Update request
355 $sql = 'UPDATE '.$this->db->prefix().$this->table_element.' SET';
356
357 $sql .= ' tms = '.(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : "'".$this->db->idate(dol_now())."'").',';
358 $sql .= ' fk_product = '.(isset($this->fk_product) ? $this->fk_product : "null").',';
359 $sql .= ' fk_entrepot = '.(isset($this->fk_entrepot) ? $this->fk_entrepot : "null").',';
360 $sql .= ' seuil_stock_alerte = '.(isset($this->seuil_stock_alerte) ? $this->seuil_stock_alerte : "null").',';
361 $sql .= ' desiredstock = '.(isset($this->desiredstock) ? $this->desiredstock : "null").',';
362 $sql .= ' import_key = '.(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
363
364
365 $sql .= ' WHERE rowid='.((int) $this->id);
366
367 $this->db->begin();
368
369 $resql = $this->db->query($sql);
370 if (!$resql) {
371 $error++;
372 $this->errors[] = 'Error '.$this->db->lasterror();
373 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
374 }
375
376 //if (!$error && !$notrigger) {
377 // Uncomment this and change MYOBJECT to your own tag if you
378 // want this action calls a trigger.
379
381 //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
382 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
384 //}
385
386 // Commit or rollback
387 if ($error) {
388 $this->db->rollback();
389
390 return -1 * $error;
391 } else {
392 $this->db->commit();
393
394 return 1;
395 }
396 }
397
405 public function delete(User $user, $notrigger = 0)
406 {
407 dol_syslog(__METHOD__, LOG_DEBUG);
408
409 $error = 0;
410
411 $this->db->begin();
412
413 //if (!$error && !$notrigger) {
414 // Uncomment this and change MYOBJECT to your own tag if you
415 // want this action calls a trigger.
416
418 //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
419 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
421 //}
422
423 if (!$error) {
424 $sql = 'DELETE FROM '.$this->db->prefix().$this->table_element;
425 $sql .= ' WHERE rowid='.((int) $this->id);
426
427 $resql = $this->db->query($sql);
428 if (!$resql) {
429 $error++;
430 $this->errors[] = 'Error '.$this->db->lasterror();
431 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
432 }
433 }
434
435 // Commit or rollback
436 if ($error) {
437 $this->db->rollback();
438
439 return -1 * $error;
440 } else {
441 $this->db->commit();
442
443 return 1;
444 }
445 }
446
454 public function createFromClone(User $user, $fromid)
455 {
456 dol_syslog(__METHOD__, LOG_DEBUG);
457
458 $error = 0;
459 $object = new ProductStockEntrepot($this->db);
460
461 $this->db->begin();
462
463 // Load source object
464 $object->fetch($fromid);
465 // Reset object
466 $object->id = 0;
467
468 // Clear fields
469 // ...
470
471 // Create clone
472 $object->context['createfromclone'] = 'createfromclone';
473 $result = $object->create($user);
474
475 // Other options
476 if ($result < 0) {
477 $error++;
478 $this->errors = $object->errors;
479 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
480 }
481
482 unset($object->context['createfromclone']);
483
484 // End
485 if (!$error) {
486 $this->db->commit();
487
488 return $object->id;
489 } else {
490 $this->db->rollback();
491
492 return -1;
493 }
494 }
495
507 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
508 {
509 global $langs;
510
511 $result = '';
512
513 $label = '<u>'.$langs->trans("MyModule").'</u>';
514 $label .= '<div width="100%">';
515 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
516
517 $link = '<a href="'.DOL_URL_ROOT.'/ProductEntrepot/card.php?id='.$this->id.'"';
518 $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
519 $link .= '>';
520 $linkend = '</a>';
521
522 if ($withpicto) {
523 $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
524 if ($withpicto != 2) {
525 $result .= ' ';
526 }
527 }
528 $result .= $link.$this->ref.$linkend;
529
530 return $result;
531 }
532
539 public function getLibStatut($mode = 0)
540 {
541 return $this->LibStatut($this->status, $mode);
542 }
543
544 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
552 public function LibStatut($status, $mode = 0)
553 {
554 // phpcs:enable
555 global $langs;
556
557 if ($mode == 0) {
558 if ($status == 1) {
559 return $langs->trans('Enabled');
560 } elseif ($status == 0) {
561 return $langs->trans('Disabled');
562 }
563 } elseif ($mode == 1) {
564 if ($status == 1) {
565 return $langs->trans('Enabled');
566 } elseif ($status == 0) {
567 return $langs->trans('Disabled');
568 }
569 } elseif ($mode == 2) {
570 if ($status == 1) {
571 return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
572 } elseif ($status == 0) {
573 return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
574 }
575 } elseif ($mode == 3) {
576 if ($status == 1) {
577 return img_picto($langs->trans('Enabled'), 'statut4');
578 } elseif ($status == 0) {
579 return img_picto($langs->trans('Disabled'), 'statut5');
580 }
581 } elseif ($mode == 4) {
582 if ($status == 1) {
583 return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
584 } elseif ($status == 0) {
585 return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
586 }
587 } elseif ($mode == 5) {
588 if ($status == 1) {
589 return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
590 } elseif ($status == 0) {
591 return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
592 }
593 }
594
595 return '';
596 }
597
598
605 public function initAsSpecimen()
606 {
607 $this->id = 0;
608
609 $this->tms = dol_now();
610 $this->fk_product = 0;
611 $this->fk_entrepot = 0;
612 $this->seuil_stock_alerte = '';
613 $this->desiredstock = '';
614 $this->import_key = '';
615
616 return 1;
617 }
618}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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...
Class to manage Dolibarr database access.
Class ProductStockEntrepot.
update(User $user, $notrigger=0)
Update object into database.
fetch($id, $fk_product=0, $fk_entrepot=0)
Load object in memory from the database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
__construct(DoliDB $db)
Constructor.
getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
Return a link to the user card (with optionally the picto) Use this->id,this->lastname,...
getLibStatut($mode=0)
Return the label of the status.
fetchAll($fk_product=0, $fk_entrepot=0, $sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load object in memory from the database.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
create(User $user, $notrigger=0)
Create object into database.
LibStatut($status, $mode=0)
Renvoi le libelle d'un status donne.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:162
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.