29 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
40 public $element =
'stock';
45 public $table_element =
'entrepot';
50 public $picto =
'stock';
55 public $ismultientitymanaged = 1;
119 public $statuts = array();
124 public $fields = array(
125 'rowid' =>array(
'type'=>
'integer',
'label'=>
'ID',
'enabled'=>1,
'visible'=>0,
'notnull'=>1,
'position'=>10),
126 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'enabled'=>1,
'visible'=>0,
'default'=>1,
'notnull'=>1,
'index'=>1,
'position'=>15),
127 'ref' =>array(
'type'=>
'varchar(255)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>1,
'showoncombobox'=>1,
'position'=>25,
'searchall'=>1),
128 'description' =>array(
'type'=>
'text',
'label'=>
'Description',
'enabled'=>1,
'visible'=>-2,
'position'=>35,
'searchall'=>1),
129 'lieu' =>array(
'type'=>
'varchar(64)',
'label'=>
'LocationSummary',
'enabled'=>1,
'visible'=>1,
'position'=>40,
'showoncombobox'=>2,
'searchall'=>1),
130 'fk_parent' =>array(
'type'=>
'integer:Entrepot:product/stock/class/entrepot.class.php:1:((statut:=:1) AND (entity:IN:__SHARED_ENTITIES__))',
'label'=>
'ParentWarehouse',
'enabled'=>1,
'visible'=>-2,
'position'=>41),
131 'fk_project' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label'=>
'Project',
'enabled'=>
'$conf->project->enabled',
'visible'=>-1,
'position'=>25),
132 'address' =>array(
'type'=>
'varchar(255)',
'label'=>
'Address',
'enabled'=>1,
'visible'=>-2,
'position'=>45,
'searchall'=>1),
133 'zip' =>array(
'type'=>
'varchar(10)',
'label'=>
'Zip',
'enabled'=>1,
'visible'=>-2,
'position'=>50,
'searchall'=>1),
134 'town' =>array(
'type'=>
'varchar(50)',
'label'=>
'Town',
'enabled'=>1,
'visible'=>-2,
'position'=>55,
'searchall'=>1),
135 'fk_departement' =>array(
'type'=>
'sellist:c_departements:label:rowid::active=1',
'label'=>
'State',
'enabled'=>1,
'visible'=>0,
'position'=>60),
136 'fk_pays' =>array(
'type'=>
'sellist:c_country:label:rowid::active=1',
'label'=>
'Country',
'enabled'=>1,
'visible'=>-2,
'position'=>65),
137 'phone' =>array(
'type'=>
'varchar(20)',
'label'=>
'Phone',
'enabled'=>1,
'visible'=>-2,
'position'=>70,
'searchall'=>1),
138 'fax' =>array(
'type'=>
'varchar(20)',
'label'=>
'Fax',
'enabled'=>1,
'visible'=>-2,
'position'=>75,
'searchall'=>1),
140 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'position'=>300),
141 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>301),
144 'statut' =>array(
'type'=>
'tinyint(4)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>1,
'position'=>500,
'css'=>
'minwidth50'),
180 if (!empty($conf->global->ENTREPOT_EXTRA_STATUS)) {
200 $this->label = trim(!empty($this->label) ? $this->label : $this->libelle);
203 if ($this->label ==
'') {
204 $this->error =
"ErrorFieldRequired";
212 $sql =
"INSERT INTO ".$this->db->prefix().
"entrepot (ref, entity, datec, fk_user_author, fk_parent, fk_project)";
213 $sql .=
" VALUES ('".$this->db->escape($this->label).
"', ".((int) $conf->entity).
", '".$this->
db->idate($now).
"', ".((int) $user->id).
", ".($this->fk_parent > 0 ? ((int) $this->fk_parent) :
"NULL").
", ".($this->fk_project > 0 ? ((
int) $this->fk_project) :
"NULL").
")";
215 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
216 $result = $this->
db->query($sql);
218 $id = $this->
db->last_insert_id($this->
db->prefix().
"entrepot");
223 $result = $this->
update($id, $user);
243 dol_syslog(get_class($this).
"::create return -3");
244 $this->
db->rollback();
248 $this->error =
"Failed to get insert id";
249 dol_syslog(get_class($this).
"::create return -2");
253 $this->error = $this->
db->error();
254 dol_syslog(get_class($this).
"::create Error ".$this->
db->error());
255 $this->
db->rollback();
276 if (empty($this->label)) {
277 $this->label = $this->libelle;
281 if (!empty($this->fk_parent)) {
282 $TChildWarehouses = array($id);
284 if (in_array($this->fk_parent, $TChildWarehouses)) {
285 $this->error =
'ErrorCannotAddThisParentWarehouse';
290 $this->label = trim(!empty($this->label) ? $this->label : $this->libelle);
294 $this->lieu = trim($this->lieu);
296 $this->address = trim($this->address);
297 $this->zip = trim($this->zip);
298 $this->town = trim($this->town);
299 $this->country_id = ($this->country_id > 0 ? $this->country_id : 0);
301 $sql =
"UPDATE ".$this->db->prefix().
"entrepot";
302 $sql .=
" SET ref = '".$this->db->escape($this->label).
"'";
303 $sql .=
", fk_parent = ".(($this->fk_parent > 0) ? $this->fk_parent :
"NULL");
304 $sql .=
", fk_project = ".(($this->fk_project > 0) ? $this->fk_project :
"NULL");
305 $sql .=
", description = '".$this->db->escape($this->
description).
"'";
306 $sql .=
", statut = ".((int) $this->statut);
307 $sql .=
", lieu = '".$this->db->escape($this->lieu).
"'";
308 $sql .=
", address = '".$this->db->escape($this->address).
"'";
309 $sql .=
", zip = '".$this->db->escape($this->zip).
"'";
310 $sql .=
", town = '".$this->db->escape($this->town).
"'";
311 $sql .=
", fk_pays = ".((int) $this->country_id);
312 $sql .=
", phone = '".$this->db->escape($this->phone).
"'";
313 $sql .=
", fax = '".$this->db->escape($this->fax).
"'";
314 $sql .=
" WHERE rowid = ".((int) $id);
318 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
323 $this->errors[] =
"Error ".$this->db->lasterror();
337 $this->
db->rollback();
338 $this->error = $this->
db->lasterror();
351 public function delete($user, $notrigger = 0)
357 dol_syslog(get_class($this).
"::delete id=".$this->
id, LOG_DEBUG);
361 if (!$error && empty($notrigger)) {
363 $result = $this->
call_trigger(
'WAREHOUSE_DELETE', $user);
371 $sql =
"DELETE FROM ".$this->db->prefix().
"product_batch";
372 $sql .=
" WHERE fk_product_stock IN (SELECT rowid FROM ".$this->db->prefix().
"product_stock as ps WHERE ps.fk_entrepot = ".((int) $this->
id).
")";
373 $result = $this->
db->query($sql);
376 $this->errors[] = $this->
db->lasterror();
381 $elements = array(
'stock_mouvement',
'product_stock');
382 foreach ($elements as $table) {
384 $sql =
"DELETE FROM ".$this->db->prefix().$table;
385 $sql .=
" WHERE fk_entrepot = ".((int) $this->
id);
387 $result = $this->
db->query($sql);
390 $this->errors[] = $this->
db->lasterror();
402 dol_syslog(get_class($this).
"::delete Error ".$this->error, LOG_ERR);
408 $sql =
"DELETE FROM ".$this->db->prefix().
"entrepot";
409 $sql .=
" WHERE rowid = ".((int) $this->
id);
410 $resql1 = $this->
db->query($sql);
413 $this->errors[] = $this->
db->lasterror();
414 dol_syslog(get_class($this).
"::delete Error ".$this->
db->lasterror(), LOG_ERR);
420 $sql =
"UPDATE ".$this->db->prefix().
"product as p SET stock = (SELECT SUM(ps.reel) FROM ".$this->
db->prefix().
"product_stock as ps WHERE ps.fk_product = p.rowid)";
421 $resql2 = $this->
db->query($sql);
424 $this->errors[] = $this->
db->lasterror();
425 dol_syslog(get_class($this).
"::delete Error ".$this->
db->lasterror(), LOG_ERR);
433 $this->
db->rollback();
446 public function fetch($id, $ref =
'')
450 dol_syslog(get_class($this).
"::fetch id=".$id.
" ref=".$ref);
454 $this->error =
'ErrorWrongParameters';
455 dol_syslog(get_class($this).
"::fetch ".$this->error);
459 $sql =
"SELECT rowid, entity, fk_parent, fk_project, ref as label, description, statut, lieu, address, zip, town, fk_pays as country_id, phone, fax,";
460 $sql .=
" model_pdf, import_key";
461 $sql .=
" FROM ".$this->db->prefix().
"entrepot";
463 $sql .=
" WHERE rowid = ".((int) $id);
465 $sql .=
" WHERE entity IN (".getEntity(
'stock').
")";
467 $sql .=
" AND ref = '".$this->db->escape($ref).
"'";
471 $result = $this->
db->query($sql);
473 if ($this->
db->num_rows($result) > 0) {
474 $obj = $this->
db->fetch_object($result);
476 $this->
id = $obj->rowid;
477 $this->entity = $obj->entity;
478 $this->fk_parent = $obj->fk_parent;
479 $this->fk_project = $obj->fk_project;
480 $this->
ref = $obj->label;
481 $this->label = $obj->label;
482 $this->libelle = $obj->label;
484 $this->statut = $obj->statut;
485 $this->lieu = $obj->lieu;
486 $this->address = $obj->address;
487 $this->zip = $obj->zip;
488 $this->town = $obj->town;
489 $this->country_id = $obj->country_id;
490 $this->phone = $obj->phone;
491 $this->fax = $obj->fax;
493 $this->model_pdf = $obj->model_pdf;
494 $this->import_key = $obj->import_key;
500 include_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
502 $this->country = $tmp[
'label'];
503 $this->country_code = $tmp[
'code'];
507 $this->error =
"Record Not Found";
511 $this->error = $this->
db->error();
525 $sql =
"SELECT e.rowid, e.datec, e.tms as datem, e.fk_user_author";
526 $sql .=
" FROM ".$this->db->prefix().
"entrepot as e";
527 $sql .=
" WHERE e.rowid = ".((int) $id);
529 dol_syslog(get_class($this).
"::info", LOG_DEBUG);
530 $result = $this->
db->query($sql);
532 if ($this->
db->num_rows($result)) {
533 $obj = $this->
db->fetch_object($result);
535 $this->
id = $obj->rowid;
537 $this->user_creation_id = $obj->fk_user_author;
538 $this->date_creation = $this->
db->jdate($obj->datec);
539 $this->date_modification = empty($obj->datem) ?
'' : $this->
db->jdate($obj->datem);
542 $this->
db->free($result);
561 $sql =
"SELECT rowid, ref as label";
562 $sql .=
" FROM ".$this->db->prefix().
"entrepot";
563 $sql .=
" WHERE entity IN (".getEntity(
'stock').
")";
564 $sql .=
" AND statut = ".((int) $status);
566 $result = $this->
db->query($sql);
568 $num = $this->
db->num_rows($result);
571 $row = $this->
db->fetch_row($result);
572 $liste[$row[0]] = $row[1];
575 $this->
db->free($result);
591 $sql =
"SELECT count(distinct p.rowid) as nb";
592 $sql .=
" FROM ".$this->db->prefix().
"product_stock as ps";
593 $sql .=
", ".$this->db->prefix().
"product as p";
594 $sql .=
" WHERE ps.fk_entrepot = ".((int) $this->
id);
595 $sql .=
" AND ps.fk_product = p.rowid";
598 $result = $this->
db->query($sql);
600 $obj = $this->
db->fetch_object($result);
601 $ret[
'nb'] = $obj->nb;
602 $this->
db->free($result);
604 $this->error = $this->
db->lasterror();
624 $separatedPMP =
false;
625 if (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED) && !empty($conf->global->MULTICOMPANY_PMP_PER_ENTITY_ENABLED)) {
626 $separatedPMP =
true;
630 $sql =
"SELECT sum(ps.reel) as nb, sum(ps.reel * pa.pmp) as value";
632 $sql =
"SELECT sum(ps.reel) as nb, sum(ps.reel * p.pmp) as value";
634 $sql .=
" FROM ".$this->db->prefix().
"product_stock as ps";
635 $sql .=
", ".$this->db->prefix().
"product as p";
637 $sql .=
", ".$this->db->prefix().
"product_perentity as pa";
639 $sql .=
" WHERE ps.fk_entrepot = ".((int) $this->
id);
641 $sql .=
" AND pa.fk_product = p.rowid AND pa.entity = ". (int) $conf->entity;
643 $sql .=
" AND ps.fk_product = p.rowid";
645 $result = $this->
db->query($sql);
647 $obj = $this->
db->fetch_object($result);
648 $ret[
'nb'] = $obj->nb;
649 $ret[
'value'] = $obj->value;
650 $this->
db->free($result);
652 $this->error = $this->
db->lasterror();
667 return $this->
LibStatut($this->statut, $mode);
683 $statusType =
'status5';
685 $statusType =
'status4';
688 $langs->load(
'stocks');
689 $label = $langs->transnoentitiesnoconv($this->statuts[$status]);
690 $labelshort = $langs->transnoentitiesnoconv($this->statuts[$status]);
692 return dolGetStatus($label, $labelshort,
'', $statusType, $mode);
707 public function getNomUrl($withpicto = 0, $option =
'', $showfullpath = 0, $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
709 global $conf, $langs, $hookmanager;
710 $langs->load(
"stocks");
712 if (!empty($conf->dol_no_mouse_hover)) {
716 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) && $withpicto) {
722 $label =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Warehouse").
'</u>';
723 if (isset($this->statut)) {
724 $label .=
' '.$this->getLibStatut(5);
726 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.(empty($this->
ref) ? (empty($this->label) ? $this->libelle : $this->label) : $this->
ref);
727 if (!empty($this->lieu)) {
728 $label .=
'<br><b>'.$langs->trans(
'LocationSummary').
':</b> '.$this->lieu;
731 $url = DOL_URL_ROOT.
'/product/stock/card.php?id='.$this->id;
733 if ($option !=
'nolink') {
735 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
736 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
737 $add_save_lastsearch_values = 1;
739 if ($add_save_lastsearch_values) {
740 $url .=
'&save_lastsearch_values=1';
745 if (empty($notooltip)) {
746 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
747 $label = $langs->trans(
"Warehouse");
748 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
750 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
751 $linkclose .=
' class="classfortooltip"';
754 $linkstart =
'<a href="'.$url.
'"';
755 $linkstart .= $linkclose.
'>';
758 $result .= $linkstart;
760 $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);
762 if ($withpicto != 2) {
763 $result .= (($showfullpath || !empty($conf->global->STOCK_ALWAYS_SHOW_FULL_ARBO)) ? $this->
get_full_arbo() : (empty($this->label) ? $this->libelle : $this->label));
768 $hookmanager->initHooks(array(
'warehousedao'));
769 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result,
'withpicto' => $withpicto,
'option' => $option,
'showfullpath' => $showfullpath,
'notooltip'=> $notooltip);
770 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
772 $result = $hookmanager->resPrint;
774 $result .= $hookmanager->resPrint;
789 global $user, $langs, $conf, $mysoc;
795 $this->label =
'WAREHOUSE SPECIMEN';
796 $this->
description =
'WAREHOUSE SPECIMEN '.dol_print_date($now,
'dayhourlog');
800 $this->lieu =
'Location test';
801 $this->address =
'21 jump street';
802 $this->zip =
'99999';
803 $this->town =
'MyTown';
804 $this->country_id = 1;
805 $this->country_code =
'FR';
817 global $user, $langs, $conf;
819 $TArbo = array(empty($this->label) ? $this->libelle : $this->label);
825 $parentid = $this->fk_parent;
827 while ($parentid > 0 && $i < $protection) {
828 $sql =
"SELECT fk_parent FROM ".$this->db->prefix().
"entrepot WHERE rowid = ".((int) $parentid);
831 $objarbo = $this->
db->fetch_object(
$resql);
833 $warehousetmp->fetch($parentid);
834 $TArbo[] = $warehousetmp->label;
835 $parentid = $objarbo->fk_parent;
846 return implode(
' >> ', array_reverse($TArbo));
862 FROM ".$this->db->prefix().
"entrepot
863 WHERE fk_parent = ".((int) $id);
867 while ($res = $this->
db->fetch_object(
$resql)) {
868 $TChildWarehouses[] = $res->rowid;
873 return $TChildWarehouses;
886 public function generateDocument($modele, $outputlangs =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
888 global $conf, $user, $langs;
890 $langs->load(
"stocks");
891 $outputlangs->load(
"products");
894 $modele =
'standard';
896 if ($this->model_pdf) {
897 $modele = $this->model_pdf;
898 } elseif (!empty($conf->global->STOCK_ADDON_PDF)) {
899 $modele = $conf->global->STOCK_ADDON_PDF;
903 $modelpath =
"core/modules/stock/doc/";
905 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
920 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
921 return parent::setCategoriesCommon($categories, Categorie::TYPE_WAREHOUSE);