dolibarr  16.0.5
stocktransfer.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
4  * Copyright (C) ---Put here your own copyright and developer email---
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
26 // Put here all includes required by your class file
27 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
29 
30 //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
31 //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
32 
37 {
38  use CommonIncoterm;
42  public $element = 'stocktransfer';
43 
47  public $table_element = 'stocktransfer_stocktransfer';
48 
53  public $ismultientitymanaged = 0;
54 
58  public $isextrafieldmanaged = 1;
59 
63  public $picto = 'stock';
64 
65  public $date_prevue_depart;
66  public $date_prevue_arrivee;
67  public $date_reelle_depart;
68  public $date_reelle_arrivee;
69 
70 
71  const STATUS_DRAFT = 0;
72  const STATUS_VALIDATED = 1;
73  const STATUS_TRANSFERED = 2;
74  const STATUS_CLOSED = 3;
75 
76 
102  // BEGIN MODULEBUILDER PROPERTIES
106  public $fields=array(
107  'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>'1', 'position'=>1, 'notnull'=>1, 'visible'=>0, 'noteditable'=>'1', 'index'=>1, 'comment'=>"Id"),
108  'entity' => array('type'=>'integer', 'label'=>'Entity', 'enabled'=>'1', 'position'=>1, 'default'=>1, 'notnull'=>1, 'visible'=>0, 'noteditable'=>'1', 'index'=>1, 'comment'=>"Id"),
109  'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>'1', 'position'=>10, 'notnull'=>1, 'visible'=>4, 'noteditable'=>'1', 'default'=>'(PROV)', 'index'=>1, 'searchall'=>1, 'showoncombobox'=>'1', 'comment'=>"Reference of object"),
110  'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>'1', 'position'=>30, 'notnull'=>0, 'visible'=>1, 'searchall'=>1, 'css'=>'minwidth200'/*, 'help'=>"Help text"*/, 'showoncombobox'=>'1',),
111  'description' => array('type'=>'text', 'label'=>'Description', 'enabled'=>'1', 'position'=>31, 'notnull'=>0, 'visible'=>3,),
112  'fk_project' => array('type'=>'integer:Project:projet/class/project.class.php:1', 'label'=>'Project', 'enabled'=>'$conf->project->enabled', 'position'=>32, 'notnull'=>-1, 'visible'=>-1, 'index'=>1,),
113  'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php:1:status=1 AND entity IN (__SHARED_ENTITIES__)', 'label'=>'ThirdParty', 'enabled'=>'1', 'position'=>50, 'notnull'=>-1, 'visible'=>1, 'index'=>1/*, 'help'=>"LinkToThirparty"*/,),
114  'fk_warehouse_source' => array('type'=>'integer:Entrepot:product/stock/class/entrepot.class.php', 'label'=>'Entrepôt source', 'enabled'=>'1', 'position'=>50, 'notnull'=>0, 'visible'=>1, 'help'=>'HelpWarehouseStockTransferSource',),
115  'fk_warehouse_destination' => array('type'=>'integer:Entrepot:product/stock/class/entrepot.class.php', 'label'=>'Entrepôt de destination', 'enabled'=>'1', 'position'=>51, 'notnull'=>0, 'visible'=>1, 'help'=>'HelpWarehouseStockTransferDestination'),
116  'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>'1', 'position'=>61, 'notnull'=>0, 'visible'=>0,),
117  'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>'1', 'position'=>62, 'notnull'=>0, 'visible'=>0,),
118  'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>'1', 'position'=>500, 'notnull'=>1, 'visible'=>-2,),
119  'date_prevue_depart' => array('type'=>'date', 'label'=>'DatePrevueDepart', 'enabled'=>'1', 'position'=>100, 'notnull'=>0, 'visible'=>1,),
120  'date_reelle_depart' => array('type'=>'date', 'label'=>'DateReelleDepart', 'enabled'=>'1', 'position'=>101, 'notnull'=>0, 'visible'=>5,),
121  'date_prevue_arrivee' => array('type'=>'date', 'label'=>'DatePrevueArrivee', 'enabled'=>'1', 'position'=>102, 'notnull'=>0, 'visible'=>1,),
122  'date_reelle_arrivee' => array('type'=>'date', 'label'=>'DateReelleArrivee', 'enabled'=>'1', 'position'=>103, 'notnull'=>0, 'visible'=>5,),
123  'lead_time_for_warning' => array('type'=>'integer', 'label'=>'LeadTimeForWarning', 'enabled'=>'1', 'position'=>200, 'default'=>0, 'notnull'=>0, 'visible'=>1),
124  'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>'1', 'position'=>501, 'notnull'=>0, 'visible'=>-2,),
125  'fk_user_creat' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>'1', 'position'=>510, 'notnull'=>1, 'visible'=>-2, 'foreignkey'=>'user.rowid',),
126  'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'ChangedBy', 'enabled'=>'1', 'position'=>511, 'notnull'=>-1, 'visible'=>-2,),
127  'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>'1', 'position'=>1000, 'notnull'=>-1, 'visible'=>-2,),
128  'model_pdf' => array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>'1', 'position'=>1010, 'notnull'=>-1, 'visible'=>0,),
129  'fk_incoterms' =>array('type'=>'integer', 'label'=>'IncotermCode', 'enabled'=>'$conf->incoterm->enabled', 'visible'=>-2, 'position'=>220),
130  'location_incoterms' =>array('type'=>'varchar(255)', 'label'=>'IncotermLabel', 'enabled'=>'$conf->incoterm->enabled', 'visible'=>-2, 'position'=>225),
131  'status' => array('type'=>'smallint', 'label'=>'Status', 'enabled'=>'1', 'position'=>1000, 'notnull'=>1, 'visible'=>5, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Draft', '1'=>'Validated', '2'=>'StockStransferDecremented', '3'=>'StockStransferIncremented'),),
132  );
133  public $rowid;
134  public $ref;
135  public $label;
136  public $fk_soc;
137  public $fk_project;
138  public $description;
139  public $note_public;
140  public $note_private;
141  public $date_creation;
142  public $tms;
143  public $fk_user_creat;
144  public $fk_user_modif;
145  public $import_key;
146  public $model_pdf;
147  public $status;
148  // END MODULEBUILDER PROPERTIES
149 
150 
151  // If this object has a subtable with lines
152 
156  public $table_element_line = 'stocktransfer_stocktransferline';
157 
161  public $fk_element = 'fk_stocktransfer';
162 
166  //public $class_element_line = 'StockTransferline';
167 
171  //protected $childtables = array();
172 
178  protected $childtablesoncascade = array('stocktransfer_stocktransferline');
179 
183  //public $lines = array();
184 
185 
186 
192  public function __construct(DoliDB $db)
193  {
194  global $conf, $langs;
195 
196  $this->db = $db;
197  $this->origin_type = 'StockTransfer@product/stock/stocktransfer';
198 
199  if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) $this->fields['rowid']['visible'] = 0;
200  if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) $this->fields['entity']['enabled'] = 0;
201 
202  // Example to show how to set values of fields definition dynamically
203  /*if ($user->rights->stocktransfer->stocktransfer->read) {
204  $this->fields['myfield']['visible'] = 1;
205  $this->fields['myfield']['noteditable'] = 0;
206  }*/
207 
208  // Unset fields that are disabled
209  foreach ($this->fields as $key => $val) {
210  if (isset($val['enabled']) && empty($val['enabled'])) {
211  unset($this->fields[$key]);
212  }
213  }
214 
215  // Translate some data of arrayofkeyval
216  if (is_object($langs)) {
217  foreach ($this->fields as $key => $val) {
218  if (is_array($val['arrayofkeyval'])) {
219  foreach ($val['arrayofkeyval'] as $key2 => $val2) {
220  $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
221  }
222  }
223  }
224  }
225  }
226 
234  public function create(User $user, $notrigger = false)
235  {
236  $model_pdf = GETPOST('model');
237  if (!empty($model_pdf)) $this->model_pdf = $model_pdf;
238  $this->status = (int) $this->status;
239  if ($this->fk_warehouse_source <= 0) $this->fk_warehouse_source = 0;
240  if ($this->fk_warehouse_destination <= 0) $this->fk_warehouse_destination = 0;
241  return $this->createCommon($user, $notrigger);
242  }
243 
251  public function createFromClone(User $user, $fromid)
252  {
253  global $langs, $extrafields;
254  $error = 0;
255 
256  dol_syslog(__METHOD__, LOG_DEBUG);
257 
258  $object = new self($this->db);
259 
260  $this->db->begin();
261 
262  // Load source object
263  $result = $object->fetchCommon($fromid);
264  if ($result > 0 && !empty($object->table_element_line)) $object->fetchLines();
265 
266  // get lines so they will be clone
267  //foreach($this->lines as $line)
268  // $line->fetch_optionals();
269 
270  // Reset some properties
271  unset($object->id);
272  unset($object->fk_user_creat);
273  unset($object->import_key);
274  unset($object->date_prevue_depart);
275  unset($object->date_prevue_arrivee);
276  unset($object->date_reelle_depart);
277  unset($object->date_reelle_arrivee);
278 
279 
280  // Clear fields
281  $object->ref = empty($this->fields['ref']['default']) ? "copy_of_".$object->ref : $this->fields['ref']['default'];
282  $object->label = empty($this->fields['label']['default']) ? $langs->trans("CopyOf")." ".$object->label : $this->fields['label']['default'];
283  $object->status = self::STATUS_DRAFT;
284  // ...
285  // Clear extrafields that are unique
286  if (is_array($object->array_options) && count($object->array_options) > 0) {
287  $extrafields->fetch_name_optionals_label($this->table_element);
288  foreach ($object->array_options as $key => $option) {
289  $shortkey = preg_replace('/options_/', '', $key);
290  if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
291  //var_dump($key); var_dump($clonedObj->array_options[$key]); exit;
292  unset($object->array_options[$key]);
293  }
294  }
295  }
296 
297  // Create clone
298  $object->context['createfromclone'] = 'createfromclone';
299  $result = $object->createCommon($user);
300  if ($result < 0) {
301  $error++;
302  $this->error = $object->error;
303  $this->errors = $object->errors;
304  }
305 
306  if (!$error) {
307  // copy internal contacts
308  if ($this->copy_linked_contact($object, 'internal') < 0) {
309  $error++;
310  }
311  }
312 
313  if (!$error) {
314  // copy external contacts if same company
315  if (property_exists($this, 'socid') && $this->socid == $object->socid) {
316  if ($this->copy_linked_contact($object, 'external') < 0)
317  $error++;
318  }
319  }
320 
321  unset($object->context['createfromclone']);
322 
323  // End
324  if (!$error) {
325  $this->db->commit();
326  return $object;
327  } else {
328  $this->db->rollback();
329  return -1;
330  }
331  }
332 
340  public function fetch($id, $ref = null)
341  {
342  $result = $this->fetchCommon($id, $ref);
343  if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
344  return $result;
345  }
346 
352  public function fetchLines()
353  {
354  require_once DOL_DOCUMENT_ROOT . '/product/stock/stocktransfer/class/stocktransferline.class.php';
355  $this->lines = array();
356 
357  $result = $this->fetchLinesCommon();
358  usort($this->lines, array('stocktransfer', 'cmp'));
359  return $result;
360  }
361 
369  public function cmp($a, $b)
370  {
371  if ($a->rang == $b->rang) {
372  return 0;
373  }
374  return ($a->rang < $b->rang) ? -1 : 1;
375  }
376 
382  public function getValorisationTotale()
383  {
384 
385  $total_pmp = 0;
386 
387  if (empty($this->lines)) $this->fetchLines();
388  if (!empty($this->lines)) {
389  foreach ($this->lines as $l) $total_pmp+= ($l->pmp * $l->qty);
390  }
391 
392  return $total_pmp;
393  }
394 
406  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
407  {
408  global $conf;
409 
410  dol_syslog(__METHOD__, LOG_DEBUG);
411 
412  $records = array();
413 
414  $sql = 'SELECT ';
415  $sql .= $this->getFieldList();
416  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
417  if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) $sql .= ' WHERE t.entity IN ('.getEntity($this->table_element).')';
418  else $sql .= ' WHERE 1 = 1';
419  // Manage filter
420  $sqlwhere = array();
421  if (count($filter) > 0) {
422  foreach ($filter as $key => $value) {
423  if ($key == 't.rowid') {
424  $sqlwhere[] = $key.'='.$value;
425  } elseif (strpos($key, 'date') !== false) {
426  $sqlwhere[] = $key.' = \''.$this->db->idate($value).'\'';
427  } elseif ($key == 'customsql') {
428  $sqlwhere[] = $value;
429  } else {
430  $sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
431  }
432  }
433  }
434  if (count($sqlwhere) > 0) {
435  $sql .= " AND (".implode(" ".$filtermode." ", $sqlwhere).")";
436  }
437 
438  if (!empty($sortfield)) {
439  $sql .= $this->db->order($sortfield, $sortorder);
440  }
441  if (!empty($limit)) {
442  $sql .= ' '.$this->db->plimit($limit, $offset);
443  }
444 
445  $resql = $this->db->query($sql);
446  if ($resql) {
447  $num = $this->db->num_rows($resql);
448  $i = 0;
449  while ($i < ($limit ? min($limit, $num) : $num)) {
450  $obj = $this->db->fetch_object($resql);
451 
452  $record = new self($this->db);
453  $record->setVarsFromFetchObj($obj);
454 
455  $records[$record->id] = $record;
456 
457  $i++;
458  }
459  $this->db->free($resql);
460 
461  return $records;
462  } else {
463  $this->errors[] = 'Error '.$this->db->lasterror();
464  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
465 
466  return -1;
467  }
468  }
469 
477  public function update(User $user, $notrigger = false)
478  {
479  $this->tms = ''; // Will be done automatically because tms field is on update cascade
480  $res = $this->updateCommon($user, $notrigger);
481  if ($this->socid > 0 || $this->fk_soc > 0 && empty($this->thirdparty)) $this->fetch_thirdparty();
482  if (empty($this->socid) && empty($this->fk_soc)) unset($this->thirdparty);
483  return $res;
484  }
485 
493  public function delete(User $user, $notrigger = false)
494  {
495  if ($this->status > self::STATUS_VALIDATED) {
496  return 0;
497  } else {
498  return $this->deleteCommon($user, $notrigger);
499  }
500  }
501 
510  public function deleteLine(User $user, $idline, $notrigger = false)
511  {
512  if ($this->status < 0) {
513  $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
514  return -2;
515  }
516 
517  $res = $this->deleteLineCommon($user, $idline, $notrigger);
518  $this->line_order(true);
519  return $res;
520  }
521 
522 
530  public function validate($user, $notrigger = 0)
531  {
532  global $conf, $langs;
533 
534  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
535 
536  $error = 0;
537 
538  // Protection
539  if ($this->status == self::STATUS_VALIDATED) {
540  dol_syslog(get_class($this)."::validate action abandonned: already validated", LOG_WARNING);
541  return 0;
542  }
543 
544  /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->stocktransfer->stocktransfer->write))
545  || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->stocktransfer->stocktransfer->stocktransfer_advance->validate))))
546  {
547  $this->error='NotEnoughPermissions';
548  dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
549  return -1;
550  }*/
551 
552  $now = dol_now();
553 
554  $this->db->begin();
555 
556  // Define new ref
557  if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
558  $num = $this->getNextNumRef();
559  } else {
560  $num = $this->ref;
561  }
562  $this->newref = $num;
563 
564  if (!empty($num)) {
565  // Validate
566  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
567  $sql .= " SET ref = '".$this->db->escape($num)."',";
568  $sql .= " status = ".self::STATUS_VALIDATED;
569  if (!empty($this->fields['date_validation'])) $sql .= ", date_validation = '".$this->db->idate($now)."',";
570  if (!empty($this->fields['fk_user_valid'])) $sql .= ", fk_user_valid = ".((int) $user->id);
571  $sql .= " WHERE rowid = ".((int) $this->id);
572 
573  dol_syslog(get_class($this)."::validate()", LOG_DEBUG);
574  $resql = $this->db->query($sql);
575  if (!$resql) {
576  dol_print_error($this->db);
577  $this->error = $this->db->lasterror();
578  $error++;
579  }
580 
581  if (!$error && !$notrigger) {
582  // Call trigger
583  $result = $this->call_trigger('STOCKTRANSFER_VALIDATE', $user);
584  if ($result < 0) $error++;
585  // End call triggers
586  }
587  }
588 
589  if (!$error) {
590  $this->oldref = $this->ref;
591 
592  // Rename directory if dir was a temporary ref
593  if (preg_match('/^[\(]?PROV/i', $this->ref)) {
594  // Now we rename also files into index
595  $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'stocktransfer/".$this->db->escape($this->newref)."'";
596  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'stocktransfer/".$this->db->escape($this->ref)."' and entity = ".((int) $conf->entity);
597  $resql = $this->db->query($sql);
598  if (!$resql) { $error++; $this->error = $this->db->lasterror(); }
599 
600  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
601  $oldref = dol_sanitizeFileName($this->ref);
602  $newref = dol_sanitizeFileName($num);
603  $dirsource = $conf->stocktransfer->dir_output.'/stocktransfer/'.$oldref;
604  $dirdest = $conf->stocktransfer->dir_output.'/stocktransfer/'.$newref;
605  if (!$error && file_exists($dirsource)) {
606  dol_syslog(get_class($this)."::validate() rename dir ".$dirsource." into ".$dirdest);
607 
608  if (@rename($dirsource, $dirdest)) {
609  dol_syslog("Rename ok");
610  // Rename docs starting with $oldref with $newref
611  $listoffiles = dol_dir_list($conf->stocktransfer->dir_output.'/stocktransfer/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
612  foreach ($listoffiles as $fileentry) {
613  $dirsource = $fileentry['name'];
614  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
615  $dirsource = $fileentry['path'].'/'.$dirsource;
616  $dirdest = $fileentry['path'].'/'.$dirdest;
617  @rename($dirsource, $dirdest);
618  }
619  }
620  }
621  }
622  }
623 
624  // Set new ref and current status
625  if (!$error) {
626  $this->ref = $num;
627  $this->status = self::STATUS_VALIDATED;
628  }
629 
630  if (!$error) {
631  $this->db->commit();
632  return 1;
633  } else {
634  $this->db->rollback();
635  return -1;
636  }
637  }
638 
639 
647  public function setDraft($user, $notrigger = 0)
648  {
649  // Protection
650  if ($this->status <= self::STATUS_DRAFT) {
651  return 0;
652  }
653 
654  /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->stocktransfer->write))
655  || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->stocktransfer->stocktransfer_advance->validate))))
656  {
657  $this->error='Permission denied';
658  return -1;
659  }*/
660 
661  return $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'STOCKTRANSFER_UNVALIDATE');
662  }
663 
671  public function cancel($user, $notrigger = 0)
672  {
673  // Protection
674  if ($this->status != self::STATUS_VALIDATED) {
675  return 0;
676  }
677 
678  /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->stocktransfer->write))
679  || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->stocktransfer->stocktransfer_advance->validate))))
680  {
681  $this->error='Permission denied';
682  return -1;
683  }*/
684 
685  return $this->setStatusCommon($user, self::STATUS_CLOSED, $notrigger, 'STOCKTRANSFER_CLOSE');
686  }
687 
695  public function reopen($user, $notrigger = 0)
696  {
697  // Protection
698  if ($this->status != self::STATUS_CLOSED) {
699  return 0;
700  }
701 
702  /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->stocktransfer->write))
703  || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->stocktransfer->stocktransfer_advance->validate))))
704  {
705  $this->error='Permission denied';
706  return -1;
707  }*/
708 
709  return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'STOCKTRANSFER_REOPEN');
710  }
711 
722  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
723  {
724  global $conf, $langs, $hookmanager;
725 
726  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
727 
728  $result = '';
729 
730  $label = '<u>'.$langs->trans("StockTransfer").'</u>';
731  $label .= '<br>';
732  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
733  if (isset($this->status)) {
734  $label .= '<br><b>'.$langs->trans("Status").":</b> ".$this->getLibStatut(5);
735  }
736 
737  $url = dol_buildpath('/product/stock/stocktransfer/stocktransfer_card.php', 1).'?id='.$this->id;
738 
739  if ($option != 'nolink') {
740  // Add param to save lastsearch_values or not
741  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
742  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
743  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
744  }
745 
746  $linkclose = '';
747  if (empty($notooltip)) {
748  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
749  $label = $langs->trans("ShowStockTransfer");
750  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
751  }
752  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
753  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
754  } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
755 
756  $linkstart = '<a href="'.$url.'"';
757  $linkstart .= $linkclose.'>';
758  $linkend = '</a>';
759 
760  $result .= $linkstart;
761 
762  if (empty($this->showphoto_on_popup)) {
763  if ($withpicto) $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
764  } else {
765  if ($withpicto) {
766  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
767 
768  list($class, $module) = explode('@', $this->picto);
769  $upload_dir = $conf->$module->multidir_output[$conf->entity]."/$class/".dol_sanitizeFileName($this->ref);
770  $filearray = dol_dir_list($upload_dir, "files");
771  $filename = $filearray[0]['name'];
772  if (!empty($filename)) {
773  $pospoint = strpos($filearray[0]['name'], '.');
774 
775  $pathtophoto = $class.'/'.$this->ref.'/thumbs/'.substr($filename, 0, $pospoint).'_mini'.substr($filename, $pospoint);
776  if (empty($conf->global->{strtoupper($module.'_'.$class).'_FORMATLISTPHOTOSASUSERS'})) {
777  $result .= '<div class="floatleft inline-block valignmiddle divphotoref"><div class="photoref"><img class="photo'.$module.'" alt="No photo" border="0" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$module.'&entity='.$conf->entity.'&file='.urlencode($pathtophoto).'"></div></div>';
778  } else {
779  $result .= '<div class="floatleft inline-block valignmiddle divphotoref"><img class="photouserphoto userphoto" alt="No photo" border="0" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$module.'&entity='.$conf->entity.'&file='.urlencode($pathtophoto).'"></div>';
780  }
781 
782  $result .= '</div>';
783  } else {
784  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
785  }
786  }
787  }
788 
789  if ($withpicto != 2) $result .= $this->ref;
790 
791  $result .= $linkend;
792  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
793 
794  global $action, $hookmanager;
795  $hookmanager->initHooks(array('stocktransferdao'));
796  $parameters = array('id'=>$this->id, 'getnomurl'=>$result);
797  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
798  if ($reshook > 0) $result = $hookmanager->resPrint;
799  else $result .= $hookmanager->resPrint;
800 
801  return $result;
802  }
803 
810  public function getLibStatut($mode = 0)
811  {
812  return $this->LibStatut($this->status, $mode);
813  }
814 
815  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
823  public function LibStatut($status, $mode = 0)
824  {
825  // phpcs:enable
826  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
827  global $langs;
828  //$langs->load("stocktransfer@stocktransfer");
829  $this->labelStatus[self::STATUS_DRAFT] = $langs->trans('Draft');
830  $this->labelStatus[self::STATUS_VALIDATED] = $langs->trans('Validated');
831  $this->labelStatus[self::STATUS_TRANSFERED] = $langs->trans('StockStransferDecremented');
832  $this->labelStatus[self::STATUS_CLOSED] = $langs->trans('StockStransferIncremented');
833  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->trans('Draft');
834  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->trans('Validated');
835  $this->labelStatusShort[self::STATUS_TRANSFERED] = $langs->trans('StockStransferDecremented');
836  $this->labelStatusShort[self::STATUS_CLOSED] = $langs->trans('StockStransferIncremented');
837  }
838 
839  $statusType = 'status'.$status;
840  //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
841  if ($status == self::STATUS_CLOSED) $statusType = 'status6';
842 
843  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
844  }
845 
852  public function info($id)
853  {
854  $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
855  $sql .= ' fk_user_creat, fk_user_modif';
856  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
857  $sql .= ' WHERE t.rowid = '.((int) $id);
858  $result = $this->db->query($sql);
859  if ($result) {
860  if ($this->db->num_rows($result)) {
861  $obj = $this->db->fetch_object($result);
862  $this->id = $obj->rowid;
863 
864  $this->user_creation_id = $obj->fk_user_creat;
865  $this->user_modification_id = $obj->fk_user_modif;
866  $this->date_creation = $this->db->jdate($obj->datec);
867  $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
868  }
869 
870  $this->db->free($result);
871  } else {
872  dol_print_error($this->db);
873  }
874  }
875 
882  public function initAsSpecimen()
883  {
884  $this->initAsSpecimenCommon();
885  }
886 
892  public function getLinesArray()
893  {
894  $this->lines = array();
895 
896  $objectline = new StockTransferLine($this->db);
897  $result = $objectline->fetchAll('ASC', 'rang', 0, 0, array('customsql'=>'fk_stocktransfer = '.$this->id));
898 
899  if (is_numeric($result)) {
900  $this->error = $this->error;
901  $this->errors = $this->errors;
902  return $result;
903  } else {
904  $this->lines = $result;
905  return $this->lines;
906  }
907  }
908 
914  public function getNextNumRef()
915  {
916  global $langs, $conf;
917  $langs->load("stocks");
918 
919  if (empty($conf->global->STOCKTRANSFER_STOCKTRANSFER_ADDON)) {
920  $conf->global->STOCKTRANSFER_STOCKTRANSFER_ADDON = 'mod_stocktransfer_standard';
921  }
922 
923  if (!empty($conf->global->STOCKTRANSFER_STOCKTRANSFER_ADDON)) {
924  $mybool = false;
925 
926  $file = $conf->global->STOCKTRANSFER_STOCKTRANSFER_ADDON.".php";
927  $classname = $conf->global->STOCKTRANSFER_STOCKTRANSFER_ADDON;
928 
929  // Include file with class
930  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
931  foreach ($dirmodels as $reldir) {
932  $dir = dol_buildpath($reldir."core/modules/stocktransfer/");
933 
934  // Load file with numbering class (if found)
935  $mybool |= @include_once $dir.$file;
936  }
937 
938  if ($mybool === false) {
939  dol_print_error('', "Failed to include file ".$file);
940  return '';
941  }
942 
943  if (class_exists($classname)) {
944  $obj = new $classname();
945  $numref = $obj->getNextValue($this);
946 
947  if ($numref != '' && $numref != '-1') {
948  return $numref;
949  } else {
950  $this->error = $obj->error;
951  //dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
952  return "";
953  }
954  } else {
955  print $langs->trans("Error")." ".$langs->trans("ClassNotFound").' '.$classname;
956  return "";
957  }
958  } else {
959  print $langs->trans("ErrorNumberingModuleNotSetup", $this->element);
960  return "";
961  }
962  }
963 
975  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
976  {
977  global $conf, $langs;
978 
979  $result = 0;
980  $includedocgeneration = 1;
981 
982  $langs->load("stocks");
983 
984  if (!dol_strlen($modele)) {
985  $modele = 'eagle';
986 
987  if ($this->modelpdf) {
988  $modele = $this->modelpdf;
989  } elseif (!empty($conf->global->STOCKTRANSFER_ADDON_PDF)) {
990  $modele = $conf->global->STOCKTRANSFER_ADDON_PDF;
991  }
992  }
993 
994  $modelpath = "core/modules/stocktransfer/doc/";
995 
996  if ($includedocgeneration) {
997  $result = $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
998  }
999 
1000  return $result;
1001  }
1002 
1010  public function doScheduledJob()
1011  {
1012  global $conf, $langs;
1013 
1014  //$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
1015 
1016  $error = 0;
1017  $this->output = '';
1018  $this->error = '';
1019 
1020  dol_syslog(__METHOD__, LOG_DEBUG);
1021 
1022  $now = dol_now();
1023 
1024  $this->db->begin();
1025 
1026  // ...
1027 
1028  $this->db->commit();
1029 
1030  return $error;
1031  }
1032 }
1033 
1037 //class StockTransferLine
1038 //{
1039 // // To complete with content of an object StockTransferLine
1040 // // We should have a field rowid, fk_stocktransfer and position
1041 //
1042 // /**
1043 // * @var int Does object support extrafields ? 0=No, 1=Yes
1044 // */
1045 // public $isextrafieldmanaged = 0;
1046 //
1047 // /**
1048 // * Constructor
1049 // *
1050 // * @param DoliDb $db Database handler
1051 // */
1052 // public function __construct(DoliDB $db)
1053 // {
1054 // $this->db = $db;
1055 // }
1056 //}
CommonObject\deleteCommon
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
Definition: commonobject.class.php:9406
CommonObject\setStatusCommon
setStatusCommon($user, $status, $notrigger=0, $triggercode='')
Set to a status.
Definition: commonobject.class.php:9683
StockTransfer\info
info($id)
Load the info information in the object.
Definition: stocktransfer.class.php:852
db
$conf db
API class for accounts.
Definition: inc.php:41
StockTransfer\update
update(User $user, $notrigger=false)
Update object into database.
Definition: stocktransfer.class.php:477
CommonObject\fetchCommon
fetchCommon($id, $ref=null, $morewhere='')
Load object in memory from the database.
Definition: commonobject.class.php:9202
StockTransfer\generateDocument
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
Definition: stocktransfer.class.php:975
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1226
CommonObject\copy_linked_contact
copy_linked_contact($objFrom, $source='internal')
Copy contact from one element to current.
Definition: commonobject.class.php:1217
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
StockTransfer\getNextNumRef
getNextNumRef()
Returns the reference to the following non used object depending on the active numbering module.
Definition: stocktransfer.class.php:914
StockTransfer\fetch
fetch($id, $ref=null)
Load object in memory from the database.
Definition: stocktransfer.class.php:340
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
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
CommonObject\fetchLinesCommon
fetchLinesCommon($morewhere='')
Load object in memory from the database.
Definition: commonobject.class.php:9258
CommonIncoterm
trait CommonIncoterm
Superclass for incoterm classes.
Definition: commonincoterm.class.php:29
ref
$object ref
Definition: info.php:77
StockTransfer
Class for StockTransfer.
Definition: stocktransfer.class.php:36
StockTransfer\validate
validate($user, $notrigger=0)
Validate object.
Definition: stocktransfer.class.php:530
dol_dir_list
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
StockTransfer\setDraft
setDraft($user, $notrigger=0)
Set draft status.
Definition: stocktransfer.class.php:647
CommonObject\fetch_thirdparty
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
Definition: commonobject.class.php:1736
StockTransfer\reopen
reopen($user, $notrigger=0)
Set back to validated status.
Definition: stocktransfer.class.php:695
CommonObject\createCommon
createCommon(User $user, $notrigger=false)
Create object into database.
Definition: commonobject.class.php:9035
StockTransfer\LibStatut
LibStatut($status, $mode=0)
Return the status.
Definition: stocktransfer.class.php:823
StockTransferLine
Class for StockTransferLine.
Definition: stocktransferline.class.php:34
StockTransfer\getValorisationTotale
getValorisationTotale()
Used to get total PMP amount of all quantities of products of Stock Transfer.
Definition: stocktransfer.class.php:382
StockTransfer\fetchLines
fetchLines()
Load object lines in memory from the database.
Definition: stocktransfer.class.php:352
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
StockTransfer\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: stocktransfer.class.php:882
CommonObject\updateCommon
updateCommon(User $user, $notrigger=false)
Update object into database.
Definition: commonobject.class.php:9308
StockTransfer\deleteLine
deleteLine(User $user, $idline, $notrigger=false)
Delete a line of object in database.
Definition: stocktransfer.class.php:510
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
StockTransfer\getNomUrl
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
Definition: stocktransfer.class.php:722
User
Class to manage Dolibarr users.
Definition: user.class.php:44
StockTransfer\__construct
__construct(DoliDB $db)
Constructor.
Definition: stocktransfer.class.php:192
StockTransfer\getLinesArray
getLinesArray()
Create an array of lines.
Definition: stocktransfer.class.php:892
StockTransfer\create
create(User $user, $notrigger=false)
Create object into database.
Definition: stocktransfer.class.php:234
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10338
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
StockTransfer\getLibStatut
getLibStatut($mode=0)
Return label of the status.
Definition: stocktransfer.class.php:810
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
CommonObject\deleteLineCommon
deleteLineCommon(User $user, $idline, $notrigger=false)
Delete a line of object in database.
Definition: commonobject.class.php:9622
StockTransfer\cancel
cancel($user, $notrigger=0)
Set cancel status.
Definition: stocktransfer.class.php:671
StockTransfer\doScheduledJob
doScheduledJob()
Action executed by scheduler CAN BE A CRON TASK.
Definition: stocktransfer.class.php:1010
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
StockTransfer\cmp
cmp($a, $b)
Used to sort lines by rank.
Definition: stocktransfer.class.php:369
StockTransfer\createFromClone
createFromClone(User $user, $fromid)
Clone an object into another one.
Definition: stocktransfer.class.php:251
CommonObject\line_order
line_order($renum=false, $rowidorder='ASC', $fk_parent_line=true)
Save a new position (field rang) for details lines.
Definition: commonobject.class.php:3055
CommonObject\getFieldList
getFieldList($alias='')
Function to concat keys of fields.
Definition: commonobject.class.php:8987
StockTransfer\fetchAll
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load list of objects in memory from the database.
Definition: stocktransfer.class.php:406