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 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'enabled'=>1,
'visible'=>0,
'notnull'=>1,
'position'=>30),
129 'description' =>array(
'type'=>
'text',
'label'=>
'Description',
'enabled'=>1,
'visible'=>-2,
'position'=>35,
'searchall'=>1),
130 'lieu' =>array(
'type'=>
'varchar(64)',
'label'=>
'LocationSummary',
'enabled'=>1,
'visible'=>1,
'position'=>40,
'showoncombobox'=>2,
'searchall'=>1),
131 '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),
132 '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),
133 'address' =>array(
'type'=>
'varchar(255)',
'label'=>
'Address',
'enabled'=>1,
'visible'=>-2,
'position'=>45,
'searchall'=>1),
134 'zip' =>array(
'type'=>
'varchar(10)',
'label'=>
'Zip',
'enabled'=>1,
'visible'=>-2,
'position'=>50,
'searchall'=>1),
135 'town' =>array(
'type'=>
'varchar(50)',
'label'=>
'Town',
'enabled'=>1,
'visible'=>-2,
'position'=>55,
'searchall'=>1),
136 'fk_departement' =>array(
'type'=>
'sellist:c_departements:label:rowid::active=1',
'label'=>
'State',
'enabled'=>1,
'visible'=>0,
'position'=>60),
137 'fk_pays' =>array(
'type'=>
'sellist:c_country:label:rowid::active=1',
'label'=>
'Country',
'enabled'=>1,
'visible'=>-2,
'position'=>65),
138 'phone' =>array(
'type'=>
'varchar(20)',
'label'=>
'Phone',
'enabled'=>1,
'visible'=>-2,
'position'=>70,
'searchall'=>1),
139 'fax' =>array(
'type'=>
'varchar(20)',
'label'=>
'Fax',
'enabled'=>1,
'visible'=>-2,
'position'=>75,
'searchall'=>1),
141 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'position'=>300),
142 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>301),
145 'statut' =>array(
'type'=>
'tinyint(4)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>1,
'position'=>500),
175 if (!empty($conf->global->ENTREPOT_EXTRA_STATUS)) {
195 $this->label = trim(!empty($this->label) ? $this->label : $this->libelle);
198 if ($this->label ==
'') {
199 $this->error =
"ErrorFieldRequired";
207 $sql =
"INSERT INTO ".$this->db->prefix().
"entrepot (ref, entity, datec, fk_user_author, fk_parent, fk_project)";
208 $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").
")";
210 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
211 $result = $this->
db->query($sql);
213 $id = $this->
db->last_insert_id($this->
db->prefix().
"entrepot");
218 $result = $this->
update($id, $user);
238 dol_syslog(get_class($this).
"::create return -3");
239 $this->
db->rollback();
243 $this->error =
"Failed to get insert id";
244 dol_syslog(get_class($this).
"::create return -2");
248 $this->error = $this->
db->error();
249 dol_syslog(get_class($this).
"::create Error ".$this->
db->error());
250 $this->
db->rollback();
271 if (empty($this->label)) {
272 $this->label = $this->libelle;
276 if (!empty($this->fk_parent)) {
277 $TChildWarehouses = array($id);
279 if (in_array($this->fk_parent, $TChildWarehouses)) {
280 $this->error =
'ErrorCannotAddThisParentWarehouse';
285 $this->label = trim(!empty($this->label) ? $this->label : $this->libelle);
289 $this->lieu = trim($this->lieu);
291 $this->address = trim($this->address);
292 $this->zip = trim($this->zip);
293 $this->town = trim($this->town);
294 $this->country_id = ($this->country_id > 0 ? $this->country_id : 0);
296 $sql =
"UPDATE ".$this->db->prefix().
"entrepot";
297 $sql .=
" SET ref = '".$this->db->escape($this->label).
"'";
298 $sql .=
", fk_parent = ".(($this->fk_parent > 0) ? $this->fk_parent :
"NULL");
299 $sql .=
", fk_project = ".(($this->fk_project > 0) ? $this->fk_project :
"NULL");
300 $sql .=
", description = '".$this->db->escape($this->
description).
"'";
301 $sql .=
", statut = ".((int) $this->statut);
302 $sql .=
", lieu = '".$this->db->escape($this->lieu).
"'";
303 $sql .=
", address = '".$this->db->escape($this->address).
"'";
304 $sql .=
", zip = '".$this->db->escape($this->zip).
"'";
305 $sql .=
", town = '".$this->db->escape($this->town).
"'";
306 $sql .=
", fk_pays = ".((int) $this->country_id);
307 $sql .=
", phone = '".$this->db->escape($this->phone).
"'";
308 $sql .=
", fax = '".$this->db->escape($this->fax).
"'";
309 $sql .=
" WHERE rowid = ".((int) $id);
313 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
318 $this->errors[] =
"Error ".$this->db->lasterror();
332 $this->
db->rollback();
333 $this->error = $this->
db->lasterror();
346 public function delete($user, $notrigger = 0)
352 dol_syslog(get_class($this).
"::delete id=".$this->
id, LOG_DEBUG);
356 if (!$error && empty($notrigger)) {
358 $result = $this->
call_trigger(
'WAREHOUSE_DELETE', $user);
365 $elements = array(
'stock_mouvement',
'product_stock',
'product_warehouse_properties');
366 foreach ($elements as $table) {
368 $sql =
"DELETE FROM ".$this->db->prefix().$table;
369 $sql .=
" WHERE fk_entrepot = ".((int) $this->
id);
371 $result = $this->
db->query($sql);
374 $this->errors[] = $this->
db->lasterror();
385 dol_syslog(get_class($this).
"::delete Error ".$this->error, LOG_ERR);
391 $sql =
"DELETE FROM ".$this->db->prefix().
"entrepot";
392 $sql .=
" WHERE rowid = ".((int) $this->
id);
393 $resql1 = $this->
db->query($sql);
396 $this->errors[] = $this->
db->lasterror();
397 dol_syslog(get_class($this).
"::delete Error ".$this->
db->lasterror(), LOG_ERR);
403 $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)";
404 $resql2 = $this->
db->query($sql);
407 $this->errors[] = $this->
db->lasterror();
408 dol_syslog(get_class($this).
"::delete Error ".$this->
db->lasterror(), LOG_ERR);
416 $this->
db->rollback();
429 public function fetch($id, $ref =
'')
433 dol_syslog(get_class($this).
"::fetch id=".$id.
" ref=".$ref);
437 $this->error =
'ErrorWrongParameters';
438 dol_syslog(get_class($this).
"::fetch ".$this->error);
442 $sql =
"SELECT rowid, entity, fk_parent, fk_project, ref as label, description, statut, lieu, address, zip, town, fk_pays as country_id, phone, fax,";
443 $sql .=
" model_pdf, import_key";
444 $sql .=
" FROM ".$this->db->prefix().
"entrepot";
446 $sql .=
" WHERE rowid = ".((int) $id);
448 $sql .=
" WHERE entity IN (".getEntity(
'stock').
")";
450 $sql .=
" AND ref = '".$this->db->escape($ref).
"'";
454 $result = $this->
db->query($sql);
456 if ($this->
db->num_rows($result) > 0) {
457 $obj = $this->
db->fetch_object($result);
459 $this->
id = $obj->rowid;
460 $this->entity = $obj->entity;
461 $this->fk_parent = $obj->fk_parent;
462 $this->fk_project = $obj->fk_project;
463 $this->
ref = $obj->label;
464 $this->label = $obj->label;
465 $this->libelle = $obj->label;
467 $this->statut = $obj->statut;
468 $this->lieu = $obj->lieu;
469 $this->address = $obj->address;
470 $this->zip = $obj->zip;
471 $this->town = $obj->town;
472 $this->country_id = $obj->country_id;
473 $this->phone = $obj->phone;
474 $this->fax = $obj->fax;
476 $this->model_pdf = $obj->model_pdf;
477 $this->import_key = $obj->import_key;
483 include_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
485 $this->country = $tmp[
'label'];
486 $this->country_code = $tmp[
'code'];
490 $this->error =
"Record Not Found";
494 $this->error = $this->
db->error();
508 $sql =
"SELECT e.rowid, e.datec, e.tms as datem, e.fk_user_author";
509 $sql .=
" FROM ".$this->db->prefix().
"entrepot as e";
510 $sql .=
" WHERE e.rowid = ".((int) $id);
512 dol_syslog(get_class($this).
"::info", LOG_DEBUG);
513 $result = $this->
db->query($sql);
515 if ($this->
db->num_rows($result)) {
516 $obj = $this->
db->fetch_object($result);
518 $this->
id = $obj->rowid;
520 $this->user_creation_id = $obj->fk_user_author;
521 $this->date_creation = $this->
db->jdate($obj->datec);
522 $this->date_modification = empty($obj->datem) ?
'' : $this->
db->jdate($obj->datem);
525 $this->
db->free($result);
544 $sql =
"SELECT rowid, ref as label";
545 $sql .=
" FROM ".$this->db->prefix().
"entrepot";
546 $sql .=
" WHERE entity IN (".getEntity(
'stock').
")";
547 $sql .=
" AND statut = ".((int) $status);
549 $result = $this->
db->query($sql);
551 $num = $this->
db->num_rows($result);
554 $row = $this->
db->fetch_row($result);
555 $liste[$row[0]] = $row[1];
558 $this->
db->free($result);
574 $sql =
"SELECT count(distinct p.rowid) as nb";
575 $sql .=
" FROM ".$this->db->prefix().
"product_stock as ps";
576 $sql .=
", ".$this->db->prefix().
"product as p";
577 $sql .=
" WHERE ps.fk_entrepot = ".((int) $this->
id);
578 $sql .=
" AND ps.fk_product = p.rowid";
581 $result = $this->
db->query($sql);
583 $obj = $this->
db->fetch_object($result);
584 $ret[
'nb'] = $obj->nb;
585 $this->
db->free($result);
587 $this->error = $this->
db->lasterror();
607 $separatedPMP =
false;
608 if (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED) && !empty($conf->global->MULTICOMPANY_PMP_PER_ENTITY_ENABLED)) {
609 $separatedPMP =
true;
613 $sql =
"SELECT sum(ps.reel) as nb, sum(ps.reel * pa.pmp) as value";
615 $sql =
"SELECT sum(ps.reel) as nb, sum(ps.reel * p.pmp) as value";
617 $sql .=
" FROM ".$this->db->prefix().
"product_stock as ps";
618 $sql .=
", ".$this->db->prefix().
"product as p";
620 $sql .=
", ".$this->db->prefix().
"product_perentity as pa";
622 $sql .=
" WHERE ps.fk_entrepot = ".((int) $this->
id);
624 $sql .=
" AND pa.fk_product = p.rowid AND pa.entity = ". (int) $conf->entity;
626 $sql .=
" AND ps.fk_product = p.rowid";
628 $result = $this->
db->query($sql);
630 $obj = $this->
db->fetch_object($result);
631 $ret[
'nb'] = $obj->nb;
632 $ret[
'value'] = $obj->value;
633 $this->
db->free($result);
635 $this->error = $this->
db->lasterror();
650 return $this->
LibStatut($this->statut, $mode);
666 $statusType =
'status5';
668 $statusType =
'status4';
671 $langs->load(
'stocks');
672 $label = $langs->transnoentitiesnoconv($this->statuts[$status]);
673 $labelshort = $langs->transnoentitiesnoconv($this->statuts[$status]);
675 return dolGetStatus($label, $labelshort,
'', $statusType, $mode);
690 public function getNomUrl($withpicto = 0, $option =
'', $showfullpath = 0, $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
692 global $conf, $langs, $hookmanager;
693 $langs->load(
"stocks");
695 if (!empty($conf->dol_no_mouse_hover)) {
699 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) && $withpicto) {
705 $label =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Warehouse").
'</u>';
706 if (isset($this->statut)) {
707 $label .=
' '.$this->getLibStatut(5);
709 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.(empty($this->
ref) ? (empty($this->label) ? $this->libelle : $this->label) : $this->
ref);
710 if (!empty($this->lieu)) {
711 $label .=
'<br><b>'.$langs->trans(
'LocationSummary').
':</b> '.$this->lieu;
714 $url = DOL_URL_ROOT.
'/product/stock/card.php?id='.$this->id;
716 if ($option !=
'nolink') {
718 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
719 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
720 $add_save_lastsearch_values = 1;
722 if ($add_save_lastsearch_values) {
723 $url .=
'&save_lastsearch_values=1';
728 if (empty($notooltip)) {
729 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
730 $label = $langs->trans(
"Warehouse");
731 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
733 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
734 $linkclose .=
' class="classfortooltip"';
737 $linkstart =
'<a href="'.$url.
'"';
738 $linkstart .= $linkclose.
'>';
741 $result .= $linkstart;
743 $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);
745 if ($withpicto != 2) {
746 $result .= (($showfullpath || !empty($conf->global->STOCK_ALWAYS_SHOW_FULL_ARBO)) ? $this->
get_full_arbo() : (empty($this->label) ? $this->libelle : $this->label));
751 $hookmanager->initHooks(array(
'warehousedao'));
752 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result,
'withpicto' => $withpicto,
'option' => $option,
'showfullpath' => $showfullpath,
'notooltip'=> $notooltip);
753 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
755 $result = $hookmanager->resPrint;
757 $result .= $hookmanager->resPrint;
772 global $user, $langs, $conf, $mysoc;
778 $this->label =
'WAREHOUSE SPECIMEN';
779 $this->
description =
'WAREHOUSE SPECIMEN '.dol_print_date($now,
'dayhourlog');
783 $this->lieu =
'Location test';
784 $this->address =
'21 jump street';
785 $this->zip =
'99999';
786 $this->town =
'MyTown';
787 $this->country_id = 1;
788 $this->country_code =
'FR';
800 global $user, $langs, $conf;
802 $TArbo = array(empty($this->label) ? $this->libelle : $this->label);
808 $parentid = $this->fk_parent;
810 while ($parentid > 0 && $i < $protection) {
811 $sql =
"SELECT fk_parent FROM ".$this->db->prefix().
"entrepot WHERE rowid = ".((int) $parentid);
814 $objarbo = $this->
db->fetch_object(
$resql);
816 $warehousetmp->fetch($parentid);
817 $TArbo[] = $warehousetmp->label;
818 $parentid = $objarbo->fk_parent;
829 return implode(
' >> ', array_reverse($TArbo));
845 FROM ".$this->db->prefix().
"entrepot
846 WHERE fk_parent = ".((int) $id);
850 while ($res = $this->
db->fetch_object(
$resql)) {
851 $TChildWarehouses[] = $res->rowid;
856 return $TChildWarehouses;
869 public function generateDocument($modele, $outputlangs =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
871 global $conf, $user, $langs;
873 $langs->load(
"stocks");
874 $outputlangs->load(
"products");
877 $modele =
'standard';
879 if ($this->model_pdf) {
880 $modele = $this->model_pdf;
881 } elseif (!empty($conf->global->STOCK_ADDON_PDF)) {
882 $modele = $conf->global->STOCK_ADDON_PDF;
886 $modelpath =
"core/modules/stock/doc/";
888 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
903 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
904 return parent::setCategoriesCommon($categories, Categorie::TYPE_WAREHOUSE);