dolibarr 21.0.0-beta
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
65 public $seuil_stock_alerte;
69 public $desiredstock;
73 public $import_key;
74
75
81 public function __construct(DoliDB $db)
82 {
83 $this->db = $db;
84 }
85
93 public function create(User $user, $notrigger = 0)
94 {
95 dol_syslog(__METHOD__, LOG_DEBUG);
96
97 $error = 0;
98
99 // Clean parameters
100
101 if (isset($this->fk_product)) {
102 $this->fk_product = (int) $this->fk_product;
103 }
104 if (isset($this->fk_entrepot)) {
105 $this->fk_entrepot = (int) $this->fk_entrepot;
106 }
107 if (isset($this->seuil_stock_alerte)) {
108 $this->seuil_stock_alerte = trim($this->seuil_stock_alerte);
109 }
110 if (isset($this->desiredstock)) {
111 $this->desiredstock = trim($this->desiredstock);
112 }
113 if (isset($this->import_key)) {
114 $this->import_key = trim($this->import_key);
115 }
116
117 // Check parameters
118 // Put here code to add control on parameters values
119
120 // Insert request
121 $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
122 $sql .= 'fk_product,';
123 $sql .= 'fk_entrepot,';
124 $sql .= 'seuil_stock_alerte,';
125 $sql .= 'desiredstock,';
126 $sql .= 'import_key';
127 $sql .= ') VALUES (';
128 $sql .= ' '.(!isset($this->fk_product) ? 'NULL' : $this->fk_product).',';
129 $sql .= ' '.(!isset($this->fk_entrepot) ? 'NULL' : $this->fk_entrepot).',';
130 $sql .= ' '.(!isset($this->seuil_stock_alerte) ? '0' : $this->seuil_stock_alerte).',';
131 $sql .= ' '.(!isset($this->desiredstock) ? '0' : $this->desiredstock).',';
132 $sql .= ' '.(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
133 $sql .= ')';
134
135 $this->db->begin();
136
137 $resql = $this->db->query($sql);
138 if (!$resql) {
139 $error++;
140 $this->errors[] = 'Error '.$this->db->lasterror();
141 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
142 }
143
144 if (!$error) {
145 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
146
147 //if (!$notrigger) {
148 // Uncomment this and change MYOBJECT to your own tag if you
149 // want this action to call a trigger.
150
152 //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
153 //if ($result < 0) $error++;
155 //}
156 }
157
158 // Commit or rollback
159 if ($error) {
160 $this->db->rollback();
161
162 return -1 * $error;
163 } else {
164 $this->db->commit();
165
166 return $this->id;
167 }
168 }
169
178 public function fetch($id, $fk_product = 0, $fk_entrepot = 0)
179 {
180 if (empty($id) && (empty($fk_product) || empty($fk_entrepot))) {
181 return -1;
182 }
183
184 dol_syslog(__METHOD__, LOG_DEBUG);
185
186 $sql = "SELECT";
187 $sql .= " t.rowid,";
188 $sql .= " t.tms,";
189 $sql .= " t.fk_product,";
190 $sql .= " t.fk_entrepot,";
191 $sql .= " t.seuil_stock_alerte,";
192 $sql .= " t.desiredstock,";
193 $sql .= " t.import_key";
194 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
195 if (!empty($id)) {
196 $sql .= " WHERE t.rowid = ".((int) $id);
197 } else {
198 $sql .= " WHERE t.fk_product = ".((int) $fk_product)." AND t.fk_entrepot = ".((int) $fk_entrepot);
199 }
200
201 $resql = $this->db->query($sql);
202 if ($resql) {
203 $numrows = $this->db->num_rows($resql);
204 if ($numrows) {
205 $obj = $this->db->fetch_object($resql);
206
207 $this->id = $obj->rowid;
208
209 $this->tms = $this->db->jdate($obj->tms);
210 $this->fk_product = $obj->fk_product;
211 $this->fk_entrepot = $obj->fk_entrepot;
212 $this->seuil_stock_alerte = $obj->seuil_stock_alerte;
213 $this->desiredstock = $obj->desiredstock;
214 $this->import_key = $obj->import_key;
215 }
216
217 // Retrieve all extrafield
218 // fetch optionals attributes and labels
219 $this->fetch_optionals();
220
221 // $this->fetch_lines();
222
223 $this->db->free($resql);
224
225 if ($numrows) {
226 return 1;
227 } else {
228 return 0;
229 }
230 } else {
231 $this->errors[] = 'Error '.$this->db->lasterror();
232 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
233
234 return -1;
235 }
236 }
237
251 public function fetchAll($fk_product = 0, $fk_entrepot = 0, $sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
252 {
253 dol_syslog(__METHOD__, LOG_DEBUG);
254
255 $sql = "SELECT";
256 $sql .= " t.rowid,";
257 $sql .= " t.tms,";
258 $sql .= " t.fk_product,";
259 $sql .= " t.fk_entrepot,";
260 $sql .= " t.seuil_stock_alerte,";
261 $sql .= " t.desiredstock,";
262 $sql .= " t.import_key";
263 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
264 $sql .= " WHERE 1=1";
265
266 // Manage filter
267 if (is_array($filter)) {
268 $sqlwhere = array();
269 if (count($filter) > 0) {
270 foreach ($filter as $key => $value) {
271 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
272 }
273 }
274 if (count($sqlwhere) > 0) {
275 $sql .= " AND ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
276 }
277
278 $filter = '';
279 }
280
281 // Manage filter
282 $errormessage = '';
283 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
284 if ($errormessage) {
285 $this->errors[] = $errormessage;
286 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
287 return -1;
288 }
289
290 if (!empty($fk_product) && $fk_product > 0) {
291 $sql .= " AND fk_product = ".((int) $fk_product);
292 } elseif (!empty($fk_entrepot) && $fk_entrepot > 0) {
293 $sql .= " AND fk_entrepot = ".((int) $fk_entrepot);
294 }
295 // "elseif" used instead of "if" because getting list with specified fk_product and specified fk_entrepot would be the same as doing a fetch
296
297 if (!empty($sortfield)) {
298 $sql .= $this->db->order($sortfield, $sortorder);
299 }
300 if (!empty($limit)) {
301 $sql .= $this->db->plimit($limit, $offset);
302 }
303
304 $lines = array();
305
306 $resql = $this->db->query($sql);
307 if ($resql) {
308 while ($obj = $this->db->fetch_object($resql)) {
309 $lines[$obj->rowid] = array(
310 'id' => $obj->rowid
311 ,'fk_product' => $obj->fk_product
312 ,'fk_entrepot' => $obj->fk_entrepot
313 ,'seuil_stock_alerte' => $obj->seuil_stock_alerte
314 ,'desiredstock' => $obj->desiredstock
315 );
316 }
317 $this->db->free($resql);
318
319 return $lines;
320 } else {
321 $this->errors[] = 'Error '.$this->db->lasterror();
322 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
323
324 return -1;
325 }
326 }
327
335 public function update(User $user, $notrigger = 0)
336 {
337 $error = 0;
338
339 dol_syslog(__METHOD__, LOG_DEBUG);
340
341 // Clean parameters
342
343 if (isset($this->fk_product)) {
344 $this->fk_product = (int) $this->fk_product;
345 }
346 if (isset($this->fk_entrepot)) {
347 $this->fk_entrepot = (int) $this->fk_entrepot;
348 }
349 if (isset($this->seuil_stock_alerte)) {
350 $this->seuil_stock_alerte = trim($this->seuil_stock_alerte);
351 }
352 if (isset($this->desiredstock)) {
353 $this->desiredstock = trim($this->desiredstock);
354 }
355 if (isset($this->import_key)) {
356 $this->import_key = trim($this->import_key);
357 }
358
359
360 // Check parameters
361 // Put here code to add a control on parameters values
362
363 // Update request
364 $sql = 'UPDATE '.$this->db->prefix().$this->table_element.' SET';
365
366 $sql .= ' tms = '.(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : "'".$this->db->idate(dol_now())."'").',';
367 $sql .= ' fk_product = '.(isset($this->fk_product) ? $this->fk_product : "null").',';
368 $sql .= ' fk_entrepot = '.(isset($this->fk_entrepot) ? $this->fk_entrepot : "null").',';
369 $sql .= ' seuil_stock_alerte = '.(isset($this->seuil_stock_alerte) ? $this->seuil_stock_alerte : "null").',';
370 $sql .= ' desiredstock = '.(isset($this->desiredstock) ? $this->desiredstock : "null").',';
371 $sql .= ' import_key = '.(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
372
373
374 $sql .= ' WHERE rowid='.((int) $this->id);
375
376 $this->db->begin();
377
378 $resql = $this->db->query($sql);
379 if (!$resql) {
380 $error++;
381 $this->errors[] = 'Error '.$this->db->lasterror();
382 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
383 }
384
385 //if (!$error && !$notrigger) {
386 // Uncomment this and change MYOBJECT to your own tag if you
387 // want this action calls a trigger.
388
390 //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
391 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
393 //}
394
395 // Commit or rollback
396 if ($error) {
397 $this->db->rollback();
398
399 return -1 * $error;
400 } else {
401 $this->db->commit();
402
403 return 1;
404 }
405 }
406
414 public function delete(User $user, $notrigger = 0)
415 {
416 dol_syslog(__METHOD__, LOG_DEBUG);
417
418 $error = 0;
419
420 $this->db->begin();
421
422 //if (!$error && !$notrigger) {
423 // Uncomment this and change MYOBJECT to your own tag if you
424 // want this action calls a trigger.
425
427 //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
428 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
430 //}
431
432 if (!$error) {
433 $sql = 'DELETE FROM '.$this->db->prefix().$this->table_element;
434 $sql .= ' WHERE rowid='.((int) $this->id);
435
436 $resql = $this->db->query($sql);
437 if (!$resql) {
438 $error++;
439 $this->errors[] = 'Error '.$this->db->lasterror();
440 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
441 }
442 }
443
444 // Commit or rollback
445 if ($error) {
446 $this->db->rollback();
447
448 return -1 * $error;
449 } else {
450 $this->db->commit();
451
452 return 1;
453 }
454 }
455
463 public function createFromClone(User $user, $fromid)
464 {
465 dol_syslog(__METHOD__, LOG_DEBUG);
466
467 $error = 0;
468 $object = new ProductStockEntrepot($this->db);
469
470 $this->db->begin();
471
472 // Load source object
473 $object->fetch($fromid);
474 // Reset object
475 $object->id = 0;
476
477 // Clear fields
478 // ...
479
480 // Create clone
481 $object->context['createfromclone'] = 'createfromclone';
482 $result = $object->create($user);
483
484 // Other options
485 if ($result < 0) {
486 $error++;
487 $this->errors = $object->errors;
488 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
489 }
490
491 unset($object->context['createfromclone']);
492
493 // End
494 if (!$error) {
495 $this->db->commit();
496
497 return $object->id;
498 } else {
499 $this->db->rollback();
500
501 return -1;
502 }
503 }
504
516 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
517 {
518 global $langs;
519
520 $result = '';
521
522 $label = '<u>'.$langs->trans("MyModule").'</u>';
523 $label .= '<div width="100%">';
524 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
525
526 $link = '<a href="'.DOL_URL_ROOT.'/ProductEntrepot/card.php?id='.$this->id.'"';
527 $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
528 $link .= '>';
529 $linkend = '</a>';
530
531 if ($withpicto) {
532 $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
533 if ($withpicto != 2) {
534 $result .= ' ';
535 }
536 }
537 $result .= $link.$this->ref.$linkend;
538
539 return $result;
540 }
541
548 public function getLibStatut($mode = 0)
549 {
550 return $this->LibStatut($this->status, $mode);
551 }
552
553 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
561 public function LibStatut($status, $mode = 0)
562 {
563 // phpcs:enable
564 global $langs;
565
566 if ($mode == 0) {
567 if ($status == 1) {
568 return $langs->trans('Enabled');
569 } elseif ($status == 0) {
570 return $langs->trans('Disabled');
571 }
572 } elseif ($mode == 1) {
573 if ($status == 1) {
574 return $langs->trans('Enabled');
575 } elseif ($status == 0) {
576 return $langs->trans('Disabled');
577 }
578 } elseif ($mode == 2) {
579 if ($status == 1) {
580 return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
581 } elseif ($status == 0) {
582 return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
583 }
584 } elseif ($mode == 3) {
585 if ($status == 1) {
586 return img_picto($langs->trans('Enabled'), 'statut4');
587 } elseif ($status == 0) {
588 return img_picto($langs->trans('Disabled'), 'statut5');
589 }
590 } elseif ($mode == 4) {
591 if ($status == 1) {
592 return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
593 } elseif ($status == 0) {
594 return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
595 }
596 } elseif ($mode == 5) {
597 if ($status == 1) {
598 return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
599 } elseif ($status == 0) {
600 return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
601 }
602 }
603
604 return '';
605 }
606
607
614 public function initAsSpecimen()
615 {
616 $this->id = 0;
617
618 $this->tms = dol_now();
619 $this->fk_product = 0;
620 $this->fk_entrepot = 0;
621 $this->seuil_stock_alerte = '';
622 $this->desiredstock = '';
623 $this->import_key = '';
624
625 return 1;
626 }
627}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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:171
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.