27 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
39 public $element =
'stocktransferline';
44 public $table_element =
'stocktransfer_stocktransferline';
50 public $ismultientitymanaged = 0;
55 public $isextrafieldmanaged = 1;
60 public $picto =
'stocktransferline@stocktransfer';
63 const STATUS_DRAFT = 0;
64 const STATUS_VALIDATED = 1;
65 const STATUS_CANCELED = 9;
98 'rowid' => array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>
'1',
'position'=>1,
'notnull'=>1,
'visible'=>0,
'noteditable'=>
'1',
'index'=>1,
'comment'=>
"Id"),
99 'amount' => array(
'type'=>
'price',
'label'=>
'Amount',
'enabled'=>
'1',
'position'=>40,
'notnull'=>0,
'visible'=>1,
'default'=>
'null',
'isameasure'=>
'1',
'help'=>
"Help text for amount",),
100 'qty' => array(
'type'=>
'real',
'label'=>
'Qty',
'enabled'=>
'1',
'position'=>45,
'notnull'=>0,
'visible'=>1,
'default'=>
'0',
'isameasure'=>
'1',
'css'=>
'maxwidth75imp',
'help'=>
"Help text for quantity",),
101 'fk_warehouse_destination' => array(
'type'=>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label'=>
'Entrepôt de destination',
'enabled'=>
'1',
'position'=>50,
'notnull'=>1,
'visible'=>1,),
102 'fk_warehouse_source' => array(
'type'=>
'integer:Entrepot:product/stock/class/entrepot.class.php',
'label'=>
'Entrepôt source',
'enabled'=>
'1',
'position'=>50,
'notnull'=>1,
'visible'=>1,),
103 'fk_stocktransfer' => array(
'type'=>
'integer:StockTransfer:stocktransfer/stock/class/stocktransfer.class.php',
'label'=>
'StockTransfer',
'enabled'=>
'1',
'position'=>50,
'notnull'=>1,
'visible'=>0,),
104 'fk_product' => array(
'type'=>
'integer:Product:product/class/product.class.php',
'label'=>
'Product',
'enabled'=>
'1',
'position'=>50,
'notnull'=>1,
'visible'=>1,),
105 'batch' => array(
'type'=>
'varchar(128)',
'label'=>
'Batch',
'enabled'=>
'1',
'position'=>1000,
'notnull'=>-1,
'visible'=>1,),
106 'pmp' => array(
'type'=>
'double',
'label'=>
'PMP',
'enabled'=>
'1',
'position'=>50,
'notnull'=>0,
'visible'=>1,),
107 'rang' => array(
'type'=>
'integer',
'label'=>
'Qty',
'enabled'=>
'1',
'position'=>45,
'notnull'=>0,
'visible'=>0,
'default'=>
'0',
'isameasure'=>
'1',
'css'=>
'maxwidth75imp',
'help'=>
"Help text for quantity",),
112 public $fk_warehouse_destination;
113 public $fk_warehouse_source;
114 public $fk_stocktransfer;
163 global $conf, $langs;
167 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid'])) $this->fields[
'rowid'][
'visible'] = 0;
168 if (empty($conf->multicompany->enabled) && isset($this->fields[
'entity'])) $this->fields[
'entity'][
'enabled'] = 0;
177 foreach ($this->fields as $key => $val) {
178 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
179 unset($this->fields[$key]);
184 if (is_object($langs)) {
185 foreach ($this->fields as $key => $val) {
186 if (is_array($val[
'arrayofkeyval'])) {
187 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
188 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
216 global $langs, $extrafields;
221 $object =
new self($this->db);
226 $result = $object->fetchCommon($fromid);
227 if ($result > 0 && !empty($object->table_element_line)) $object->fetchLines();
235 unset($object->fk_user_creat);
236 unset($object->import_key);
240 $object->ref = empty($this->fields[
'ref'][
'default']) ?
"copy_of_".$object->ref : $this->fields[
'ref'][
'default'];
241 $object->label = empty($this->fields[
'label'][
'default']) ? $langs->trans(
"CopyOf").
" ".$object->label : $this->fields[
'label'][
'default'];
242 $object->status = self::STATUS_DRAFT;
245 if (is_array($object->array_options) && count($object->array_options) > 0) {
246 $extrafields->fetch_name_optionals_label($this->table_element);
247 foreach ($object->array_options as $key => $option) {
248 $shortkey = preg_replace(
'/options_/',
'', $key);
249 if (!empty($extrafields->attributes[$this->table_element][
'unique'][$shortkey])) {
251 unset($object->array_options[$key]);
257 $object->context[
'createfromclone'] =
'createfromclone';
258 $result = $object->createCommon($user);
261 $this->error = $object->error;
262 $this->errors = $object->errors;
274 if (property_exists($this,
'socid') && $this->socid == $object->socid) {
280 unset($object->context[
'createfromclone']);
287 $this->
db->rollback();
299 public function fetch($id, $ref =
null)
302 if ($result > 0 && !empty($this->table_element_line)) $this->
fetchLines();
313 $this->lines = array();
331 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
341 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
342 if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) $sql .=
' WHERE t.entity IN ('.getEntity($this->table_element).
')';
343 else $sql .=
' WHERE 1 = 1';
346 if (count($filter) > 0) {
347 foreach ($filter as $key => $value) {
348 if ($key ==
't.rowid') {
349 $sqlwhere[] = $key.
'='.$value;
350 } elseif (strpos($key,
'date') !==
false) {
351 $sqlwhere[] = $key.
' = \''.$this->
db->idate($value).
'\'';
352 } elseif ($key ==
'customsql') {
353 $sqlwhere[] = $value;
355 $sqlwhere[] = $key.
' LIKE \'%'.$this->
db->escape($value).
'%\'';
359 if (count($sqlwhere) > 0) {
360 $sql .=
" AND (".implode(
" ".$filtermode.
" ", $sqlwhere).
")";
363 if (!empty($sortfield)) {
364 $sql .= $this->
db->order($sortfield, $sortorder);
366 if (!empty($limit)) {
367 $sql .=
' '.$this->db->plimit($limit, $offset);
374 while ($i < ($limit ? min($limit, $num) : $num)) {
375 $obj = $this->
db->fetch_object(
$resql);
377 $record =
new self($this->db);
378 $record->setVarsFromFetchObj($obj);
380 $records[$record->id] = $record;
388 $this->errors[] =
'Error '.$this->db->lasterror();
389 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
414 public function delete(
User $user, $notrigger =
false)
430 if ($this->status < 0) {
431 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
450 global $conf, $user, $langs;
452 require_once DOL_DOCUMENT_ROOT .
'/product/class/product.class.php';
453 include_once DOL_DOCUMENT_ROOT .
'/product/stock/class/mouvementstock.class.php';
454 include_once DOL_DOCUMENT_ROOT .
'/product/stock/stocktransfer/class/stocktransfer.class.php';
457 $p->fetch($this->fk_product);
459 $op[0] =
"+".trim($this->qty);
460 $op[1] =
"-".trim($this->qty);
463 $movementstock->origin_type = $st->origin_type;
464 $movementstock->origin_id = $this->fk_stocktransfer;
466 if (empty($this->batch)) {
479 $result = $movementstock->_create($user,
484 empty($direction) ? $this->pmp : 0,
493 if ($p->hasbatch()) {
494 $arraybatchinfo = $p->loadBatchInfo($this->batch);
495 if (count($arraybatchinfo) > 0) {
496 $firstrecord = array_shift($arraybatchinfo);
497 $dlc = $firstrecord[
'eatby'];
498 $dluo = $firstrecord[
'sellby'];
518 $result = $movementstock->_create($user,
523 empty($direction) ? $this->pmp : 0,
536 setEventMessages($langs->trans(
'StockTransferNoBatchForProduct', $p->getNomUrl()),
'',
'errors');
553 global $conf, $langs;
555 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
560 if ($this->status == self::STATUS_VALIDATED) {
561 dol_syslog(get_class($this).
"::validate action abandonned: already validated", LOG_WARNING);
578 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
583 $this->newref = $num;
587 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
588 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
589 $sql .=
" status = ".self::STATUS_VALIDATED;
590 if (!empty($this->fields[
'date_validation'])) $sql .=
", date_validation = '".$this->db->idate($now).
"',";
591 if (!empty($this->fields[
'fk_user_valid'])) $sql .=
", fk_user_valid = ".((int) $user->id);
592 $sql .=
" WHERE rowid = ".((int) $this->
id);
594 dol_syslog(get_class($this).
"::validate()", LOG_DEBUG);
598 $this->error = $this->
db->lasterror();
602 if (!$error && !$notrigger) {
604 $result = $this->
call_trigger(
'STOCKTRANSFERLINE_VALIDATE', $user);
605 if ($result < 0) $error++;
611 $this->oldref = $this->ref;
614 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
616 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filename = CONCAT('".$this->
db->escape($this->newref).
"', SUBSTR(filename, ".(strlen($this->
ref) + 1).
")), filepath = 'stocktransferline/".$this->
db->escape($this->newref).
"'";
617 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'stocktransferline/".$this->
db->escape($this->
ref).
"' and entity = ".((int) $conf->entity);
619 if (!
$resql) { $error++; $this->error = $this->
db->lasterror(); }
624 $dirsource = $conf->stocktransfer->dir_output.
'/stocktransferline/'.$oldref;
625 $dirdest = $conf->stocktransfer->dir_output.
'/stocktransferline/'.$newref;
626 if (!$error && file_exists($dirsource)) {
627 dol_syslog(get_class($this).
"::validate() rename dir ".$dirsource.
" into ".$dirdest);
629 if (@rename($dirsource, $dirdest)) {
632 $listoffiles =
dol_dir_list($conf->stocktransfer->dir_output.
'/stocktransferline/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
633 foreach ($listoffiles as $fileentry) {
634 $dirsource = $fileentry[
'name'];
635 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
636 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
637 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
638 @rename($dirsource, $dirdest);
648 $this->status = self::STATUS_VALIDATED;
655 $this->
db->rollback();
671 if ($this->status <= self::STATUS_DRAFT) {
682 return $this->
setStatusCommon($user, self::STATUS_DRAFT, $notrigger,
'STOCKTRANSFERLINE_UNVALIDATE');
692 public function cancel($user, $notrigger = 0)
695 if ($this->status != self::STATUS_VALIDATED) {
706 return $this->
setStatusCommon($user, self::STATUS_CANCELED, $notrigger,
'STOCKTRANSFERLINE_CLOSE');
716 public function reopen($user, $notrigger = 0)
719 if ($this->status != self::STATUS_CANCELED) {
730 return $this->
setStatusCommon($user, self::STATUS_VALIDATED, $notrigger,
'STOCKTRANSFERLINE_REOPEN');
743 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
745 global $conf, $langs, $hookmanager;
747 if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1;
751 $label =
'<u>'.$langs->trans(
"StockTransferLine").
'</u>';
753 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
754 if (isset($this->status)) {
755 $label .=
'<br><b>'.$langs->trans(
"Status").
":</b> ".$this->
getLibStatut(5);
758 $url =
dol_buildpath(
'/stocktransfer/stocktransferline_card.php', 1).
'?id='.$this->id;
760 if ($option !=
'nolink') {
762 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
763 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) $add_save_lastsearch_values = 1;
764 if ($add_save_lastsearch_values) $url .=
'&save_lastsearch_values=1';
768 if (empty($notooltip)) {
769 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
770 $label = $langs->trans(
"ShowStockTransferLine");
771 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
773 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
774 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
775 }
else $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
777 $linkstart =
'<a href="'.$url.
'"';
778 $linkstart .= $linkclose.
'>';
781 $result .= $linkstart;
783 if (empty($this->showphoto_on_popup)) {
784 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);
787 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
789 list($class, $module) = explode(
'@', $this->picto);
792 $filename = $filearray[0][
'name'];
793 if (!empty($filename)) {
794 $pospoint = strpos($filearray[0][
'name'],
'.');
796 $pathtophoto = $class.
'/'.$this->
ref.
'/thumbs/'.substr($filename, 0, $pospoint).
'_mini'.substr($filename, $pospoint);
797 if (empty($conf->global->{strtoupper($module.
'_'.$class).
'_FORMATLISTPHOTOSASUSERS'})) {
798 $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>';
800 $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>';
805 $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);
810 if ($withpicto != 2) $result .= $this->ref;
815 global $action, $hookmanager;
816 $hookmanager->initHooks(array(
'stocktransferlinedao'));
817 $parameters = array(
'id'=>$this->
id,
'getnomurl'=>$result);
818 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
819 if ($reshook > 0) $result = $hookmanager->resPrint;
820 else $result .= $hookmanager->resPrint;
833 return $this->LibStatut($this->status, $mode);
847 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
850 $this->labelStatus[self::STATUS_DRAFT] = $langs->trans(
'Draft');
851 $this->labelStatus[self::STATUS_VALIDATED] = $langs->trans(
'Enabled');
852 $this->labelStatus[self::STATUS_CANCELED] = $langs->trans(
'Disabled');
853 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->trans(
'Draft');
854 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->trans(
'Enabled');
855 $this->labelStatusShort[self::STATUS_CANCELED] = $langs->trans(
'Disabled');
858 $statusType =
'status'.$status;
860 if ($status == self::STATUS_CANCELED) $statusType =
'status6';
862 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
873 $sql =
'SELECT rowid, date_creation as datec, tms as datem,';
874 $sql .=
' fk_user_creat, fk_user_modif';
875 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
876 $sql .=
' WHERE t.rowid = '.((int) $id);
877 $result = $this->
db->query($sql);
879 if ($this->
db->num_rows($result)) {
880 $obj = $this->
db->fetch_object($result);
881 $this->
id = $obj->rowid;
883 $this->user_creation_id = $obj->fk_user_creat;
884 $this->user_modification_id = $obj->fk_user_modif;
885 $this->date_creation = $this->
db->jdate($obj->datec);
886 $this->date_modification = empty($obj->datem) ?
'' : $this->
db->jdate($obj->datem);
889 $this->
db->free($result);
903 $this->initAsSpecimenCommon();
913 $this->lines = array();
916 $result = $objectline->fetchAll(
'ASC',
'position', 0, 0, array(
'customsql'=>
'fk_stocktransferline = '.((
int) $this->
id)));
918 if (is_numeric($result)) {
919 $this->error = $this->error;
920 $this->errors = $this->errors;
923 $this->lines = $result;
935 global $langs, $conf;
936 $langs->load(
"stocks");
938 if (empty($conf->global->STOCKTRANSFER_STOCKTRANSFERLINE_ADDON)) {
939 $conf->global->STOCKTRANSFER_STOCKTRANSFERLINE_ADDON =
'mod_stocktransferline_standard';
942 if (!empty($conf->global->STOCKTRANSFER_STOCKTRANSFERLINE_ADDON)) {
945 $file = $conf->global->STOCKTRANSFER_STOCKTRANSFERLINE_ADDON.
".php";
946 $classname = $conf->global->STOCKTRANSFER_STOCKTRANSFERLINE_ADDON;
949 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
950 foreach ($dirmodels as $reldir) {
954 $mybool |= @include_once $dir.$file;
957 if ($mybool ===
false) {
962 if (class_exists($classname)) {
963 $obj =
new $classname();
964 $numref = $obj->getNextValue($this);
966 if ($numref !=
'' && $numref !=
'-1') {
969 $this->error = $obj->error;
974 print $langs->trans(
"Error").
" ".$langs->trans(
"ClassNotFound").
' '.$classname;
978 print $langs->trans(
"ErrorNumberingModuleNotSetup", $this->element);
994 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
996 global $conf, $langs;
999 $includedocgeneration = 0;
1001 $langs->load(
"stocks");
1004 $modele =
'standard_stocktransferline';
1006 if ($this->modelpdf) {
1007 $modele = $this->modelpdf;
1008 } elseif (!empty($conf->global->STOCKTRANSFERLINE_ADDON_PDF)) {
1009 $modele = $conf->global->STOCKTRANSFERLINE_ADDON_PDF;
1013 $modelpath =
"core/modules/stocktransfer/doc/";
1015 if ($includedocgeneration) {
1016 $result = $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
1031 global $conf, $langs;
1047 $this->
db->commit();
1064 public $isextrafieldmanaged = 0;