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;
127 global $conf, $langs;
131 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid'])) $this->fields[
'rowid'][
'visible'] = 0;
132 if (!
isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) $this->fields[
'entity'][
'enabled'] = 0;
141 foreach ($this->fields as $key => $val) {
142 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
143 unset($this->fields[$key]);
148 if (is_object($langs)) {
149 foreach ($this->fields as $key => $val) {
150 if (is_array($val[
'arrayofkeyval'])) {
151 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
152 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
180 global $langs, $extrafields;
185 $object =
new self($this->db);
190 $result = $object->fetchCommon($fromid);
191 if ($result > 0 && !empty($object->table_element_line)) $object->fetchLines();
199 unset($object->import_key);
203 $object->ref = empty($this->fields[
'ref'][
'default']) ?
"copy_of_".$object->ref : $this->fields[
'ref'][
'default'];
204 $object->label = empty($this->fields[
'label'][
'default']) ? $langs->trans(
"CopyOf").
" ".$object->label : $this->fields[
'label'][
'default'];
205 $object->status = self::STATUS_DRAFT;
208 if (is_array($object->array_options) && count($object->array_options) > 0) {
209 $extrafields->fetch_name_optionals_label($this->table_element);
210 foreach ($object->array_options as $key => $option) {
211 $shortkey = preg_replace(
'/options_/',
'', $key);
212 if (!empty($extrafields->attributes[$this->table_element][
'unique'][$shortkey])) {
214 unset($object->array_options[$key]);
220 $object->context[
'createfromclone'] =
'createfromclone';
221 $result = $object->createCommon($user);
224 $this->error = $object->error;
225 $this->errors = $object->errors;
237 if (property_exists($this,
'socid') && $this->socid == $object->socid) {
243 unset($object->context[
'createfromclone']);
250 $this->db->rollback();
262 public function fetch($id, $ref =
null)
265 if ($result > 0 && !empty($this->table_element_line)) $this->
fetchLines();
276 $this->lines = array();
294 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
304 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
305 if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1)
$sql .=
' WHERE t.entity IN ('.getEntity($this->element).
')';
306 else $sql .=
' WHERE 1 = 1';
309 if (count($filter) > 0) {
310 foreach ($filter as $key => $value) {
311 if ($key ==
't.rowid') {
312 $sqlwhere[] = $key.
'='.$value;
313 } elseif (strpos($key,
'date') !==
false) {
314 $sqlwhere[] = $key.
' = \''.$this->db->idate($value).
'\'';
315 } elseif ($key ==
'customsql') {
316 $sqlwhere[] = $value;
318 $sqlwhere[] = $key.
' LIKE \'%'.$this->db->escape($value).
'%\'';
322 if (count($sqlwhere) > 0) {
323 $sql .=
" AND (".implode(
" ".$filtermode.
" ", $sqlwhere).
")";
326 if (!empty($sortfield)) {
327 $sql .= $this->db->order($sortfield, $sortorder);
329 if (!empty($limit)) {
330 $sql .=
' '.$this->db->plimit($limit, $offset);
333 $resql = $this->db->query(
$sql);
335 $num = $this->db->num_rows($resql);
337 while ($i < ($limit ? min($limit, $num) : $num)) {
338 $obj = $this->db->fetch_object($resql);
340 $record =
new self($this->db);
341 $record->setVarsFromFetchObj($obj);
343 $records[$record->id] = $record;
347 $this->db->free($resql);
351 $this->errors[] =
'Error '.$this->db->lasterror();
352 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
377 public function delete(
User $user, $notrigger =
false)
393 if ($this->status < 0) {
394 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
413 global $conf, $user, $langs;
415 require_once DOL_DOCUMENT_ROOT .
'/product/class/product.class.php';
416 include_once DOL_DOCUMENT_ROOT .
'/product/stock/class/mouvementstock.class.php';
417 include_once DOL_DOCUMENT_ROOT .
'/product/stock/stocktransfer/class/stocktransfer.class.php';
420 $p->fetch($this->fk_product);
422 $op[0] =
"+".trim($this->qty);
423 $op[1] =
"-".trim($this->qty);
426 $movementstock->origin_type = $st->origin_type;
427 $movementstock->origin_id = $this->fk_stocktransfer;
429 if (empty($this->batch)) {
442 $result = $movementstock->_create($user,
447 empty($direction) ? $this->pmp : 0,
456 if ($p->hasbatch()) {
457 $arraybatchinfo = $p->loadBatchInfo($this->batch);
458 if (count($arraybatchinfo) > 0) {
459 $firstrecord = array_shift($arraybatchinfo);
460 $dlc = $firstrecord[
'eatby'];
461 $dluo = $firstrecord[
'sellby'];
481 $result = $movementstock->_create($user,
486 empty($direction) ? $this->pmp : 0,
499 setEventMessages($langs->trans(
'StockTransferNoBatchForProduct', $p->getNomUrl()),
'',
'errors');
516 global $conf, $langs;
518 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
523 if ($this->status == self::STATUS_VALIDATED) {
524 dol_syslog(get_class($this).
"::validate action abandonned: already validated", LOG_WARNING);
541 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
546 $this->newref = $num;
550 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
551 $sql .=
" SET ref = '".$this->db->escape($num).
"',";
552 $sql .=
" status = ".self::STATUS_VALIDATED;
553 if (!empty($this->fields[
'date_validation']))
$sql .=
", date_validation = '".$this->db->idate($now).
"',";
554 if (!empty($this->fields[
'fk_user_valid']))
$sql .=
", fk_user_valid = ".((int) $user->id);
555 $sql .=
" WHERE rowid = ".((int) $this->
id);
557 dol_syslog(get_class($this).
"::validate()", LOG_DEBUG);
558 $resql = $this->db->query(
$sql);
561 $this->error = $this->db->lasterror();
565 if (!$error && !$notrigger) {
567 $result = $this->
call_trigger(
'STOCKTRANSFERLINE_VALIDATE', $user);
568 if ($result < 0) $error++;
574 $this->oldref = $this->ref;
577 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
579 $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).
"'";
580 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'stocktransferline/".$this->db->escape($this->
ref).
"' and entity = ".((int) $conf->entity);
581 $resql = $this->db->query(
$sql);
582 if (!$resql) { $error++; $this->error = $this->db->lasterror(); }
587 $dirsource = $conf->stocktransfer->dir_output.
'/stocktransferline/'.$oldref;
588 $dirdest = $conf->stocktransfer->dir_output.
'/stocktransferline/'.$newref;
589 if (!$error && file_exists($dirsource)) {
590 dol_syslog(get_class($this).
"::validate() rename dir ".$dirsource.
" into ".$dirdest);
592 if (@rename($dirsource, $dirdest)) {
595 $listoffiles =
dol_dir_list($conf->stocktransfer->dir_output.
'/stocktransferline/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
596 foreach ($listoffiles as $fileentry) {
597 $dirsource = $fileentry[
'name'];
598 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
599 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
600 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
601 @rename($dirsource, $dirdest);
611 $this->status = self::STATUS_VALIDATED;
618 $this->db->rollback();
634 if ($this->status <= self::STATUS_DRAFT) {
645 return $this->
setStatusCommon($user, self::STATUS_DRAFT, $notrigger,
'STOCKTRANSFERLINE_UNVALIDATE');
655 public function cancel($user, $notrigger = 0)
658 if ($this->status != self::STATUS_VALIDATED) {
669 return $this->
setStatusCommon($user, self::STATUS_CANCELED, $notrigger,
'STOCKTRANSFERLINE_CLOSE');
679 public function reopen($user, $notrigger = 0)
682 if ($this->status != self::STATUS_CANCELED) {
693 return $this->
setStatusCommon($user, self::STATUS_VALIDATED, $notrigger,
'STOCKTRANSFERLINE_REOPEN');
706 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
708 global $conf, $langs, $hookmanager;
710 if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1;
714 $label =
'<u>'.$langs->trans(
"StockTransferLine").
'</u>';
716 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
717 if (isset($this->status)) {
718 $label .=
'<br><b>'.$langs->trans(
"Status").
":</b> ".$this->
getLibStatut(5);
721 $url =
dol_buildpath(
'/stocktransfer/stocktransferline_card.php', 1).
'?id='.$this->id;
723 if ($option !=
'nolink') {
725 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
726 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) $add_save_lastsearch_values = 1;
727 if ($add_save_lastsearch_values) $url .=
'&save_lastsearch_values=1';
731 if (empty($notooltip)) {
732 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
733 $label = $langs->trans(
"ShowStockTransferLine");
734 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
736 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
737 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
738 }
else $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
740 $linkstart =
'<a href="'.$url.
'"';
741 $linkstart .= $linkclose.
'>';
744 $result .= $linkstart;
746 if (empty($this->showphoto_on_popup)) {
747 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);
750 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
752 list($class, $module) = explode(
'@', $this->picto);
755 $filename = $filearray[0][
'name'];
756 if (!empty($filename)) {
757 $pospoint = strpos($filearray[0][
'name'],
'.');
759 $pathtophoto = $class.
'/'.$this->
ref.
'/thumbs/'.substr($filename, 0, $pospoint).
'_mini'.substr($filename, $pospoint);
760 if (empty($conf->global->{strtoupper($module.
'_'.$class).
'_FORMATLISTPHOTOSASUSERS'})) {
761 $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>';
763 $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>';
768 $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);
773 if ($withpicto != 2) $result .= $this->ref;
778 global $action, $hookmanager;
779 $hookmanager->initHooks(array(
'stocktransferlinedao'));
780 $parameters = array(
'id'=>$this->
id,
'getnomurl'=>$result);
781 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
782 if ($reshook > 0) $result = $hookmanager->resPrint;
783 else $result .= $hookmanager->resPrint;
796 return $this->LibStatut($this->status, $mode);
810 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
813 $this->labelStatus[self::STATUS_DRAFT] = $langs->trans(
'Draft');
814 $this->labelStatus[self::STATUS_VALIDATED] = $langs->trans(
'Enabled');
815 $this->labelStatus[self::STATUS_CANCELED] = $langs->trans(
'Disabled');
816 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->trans(
'Draft');
817 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->trans(
'Enabled');
818 $this->labelStatusShort[self::STATUS_CANCELED] = $langs->trans(
'Disabled');
821 $statusType =
'status'.$status;
823 if ($status == self::STATUS_CANCELED) $statusType =
'status6';
825 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
836 $sql =
'SELECT rowid, date_creation as datec, tms as datem,';
837 $sql .=
' fk_user_creat, fk_user_modif';
838 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
839 $sql .=
' WHERE t.rowid = '.((int) $id);
840 $result = $this->db->query(
$sql);
842 if ($this->db->num_rows($result)) {
843 $obj = $this->db->fetch_object($result);
844 $this->
id = $obj->rowid;
846 $this->user_creation_id = $obj->fk_user_creat;
847 $this->user_modification_id = $obj->fk_user_modif;
848 $this->date_creation = $this->db->jdate($obj->datec);
849 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
852 $this->db->free($result);
866 $this->initAsSpecimenCommon();
876 $this->lines = array();
879 $result = $objectline->fetchAll(
'ASC',
'position', 0, 0, array(
'customsql'=>
'fk_stocktransferline = '.((
int) $this->
id)));
881 if (is_numeric($result)) {
882 $this->error = $objectline->error;
883 $this->errors = $objectline->errors;
886 $this->lines = $result;
898 global $langs, $conf;
899 $langs->load(
"stocks");
901 if (empty($conf->global->STOCKTRANSFER_STOCKTRANSFERLINE_ADDON)) {
902 $conf->global->STOCKTRANSFER_STOCKTRANSFERLINE_ADDON =
'mod_stocktransferline_standard';
905 if (!empty($conf->global->STOCKTRANSFER_STOCKTRANSFERLINE_ADDON)) {
908 $file = $conf->global->STOCKTRANSFER_STOCKTRANSFERLINE_ADDON.
".php";
909 $classname = $conf->global->STOCKTRANSFER_STOCKTRANSFERLINE_ADDON;
912 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
913 foreach ($dirmodels as $reldir) {
917 $mybool |= @include_once $dir.$file;
920 if ($mybool ===
false) {
925 if (class_exists($classname)) {
926 $obj =
new $classname();
927 $numref = $obj->getNextValue($this);
929 if ($numref !=
'' && $numref !=
'-1') {
932 $this->error = $obj->error;
937 print $langs->trans(
"Error").
" ".$langs->trans(
"ClassNotFound").
' '.$classname;
941 print $langs->trans(
"ErrorNumberingModuleNotSetup", $this->element);
957 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
959 global $conf, $langs;
962 $includedocgeneration = 0;
964 $langs->load(
"stocks");
967 $modele =
'standard_stocktransferline';
969 if ($this->modelpdf) {
970 $modele = $this->modelpdf;
971 } elseif (!empty($conf->global->STOCKTRANSFERLINE_ADDON_PDF)) {
972 $modele = $conf->global->STOCKTRANSFERLINE_ADDON_PDF;
976 $modelpath =
"core/modules/stocktransfer/doc/";
978 if ($includedocgeneration) {
979 $result = $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
994 global $conf, $langs;
1010 $this->db->commit();
1027 public $isextrafieldmanaged = 0;