entrepot.class.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. <?php
  2. /* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2008 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2016 Francis Appels <francis.appels@yahoo.com>
  7. * Copyright (C) 2019-2020 Frédéric France <frederic.france@netlogic.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/product/stock/class/entrepot.class.php
  24. * \ingroup stock
  25. * \brief Fichier de la classe de gestion des entrepots
  26. */
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  28. /**
  29. * Class to manage warehouses
  30. */
  31. class Entrepot extends CommonObject
  32. {
  33. /**
  34. * @var string ID to identify managed object
  35. */
  36. public $element = 'stock';
  37. /**
  38. * @var string Name of table without prefix where object is stored
  39. */
  40. public $table_element = 'entrepot';
  41. /**
  42. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  43. */
  44. public $picto = 'stock';
  45. /**
  46. * @var int 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  47. */
  48. public $ismultientitymanaged = 1;
  49. /**
  50. * @var string Label
  51. * @deprecated
  52. * @see $label
  53. */
  54. public $libelle;
  55. /**
  56. * @var string Label
  57. */
  58. public $label;
  59. /**
  60. * @var string description
  61. */
  62. public $description;
  63. public $statut;
  64. /**
  65. * @var string Place
  66. */
  67. public $lieu;
  68. /**
  69. * @var string Address
  70. */
  71. public $address;
  72. /**
  73. * @var string Zipcode
  74. */
  75. public $zip;
  76. /**
  77. * @var string Town
  78. */
  79. public $town;
  80. /**
  81. * @var string Phone
  82. */
  83. public $phone;
  84. /**
  85. * @var string Fax
  86. */
  87. public $fax;
  88. /**
  89. * @var int ID of parent
  90. */
  91. public $fk_parent;
  92. /**
  93. * @var int ID of project
  94. */
  95. public $fk_project;
  96. /**
  97. * @var array List of short language codes for status
  98. */
  99. public $statuts = array();
  100. /**
  101. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  102. */
  103. public $fields = array(
  104. 'rowid' =>array('type'=>'integer', 'label'=>'ID', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>10),
  105. 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'default'=>1, 'notnull'=>1, 'index'=>1, 'position'=>15),
  106. 'ref' =>array('type'=>'varchar(255)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'showoncombobox'=>1, 'position'=>25, 'searchall'=>1),
  107. 'description' =>array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>-2, 'position'=>35, 'searchall'=>1),
  108. 'lieu' =>array('type'=>'varchar(64)', 'label'=>'LocationSummary', 'enabled'=>1, 'visible'=>1, 'position'=>40, 'showoncombobox'=>2, 'searchall'=>1),
  109. 'fk_parent' =>array('type'=>'integer:Entrepot:product/stock/class/entrepot.class.php:1:((status:=:1) AND (entity:IN:__SHARED_ENTITIES__))', 'label'=>'ParentWarehouse', 'enabled'=>1, 'visible'=>-2, 'position'=>41),
  110. '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),
  111. 'address' =>array('type'=>'varchar(255)', 'label'=>'Address', 'enabled'=>1, 'visible'=>-2, 'position'=>45, 'searchall'=>1),
  112. 'zip' =>array('type'=>'varchar(10)', 'label'=>'Zip', 'enabled'=>1, 'visible'=>-2, 'position'=>50, 'searchall'=>1),
  113. 'town' =>array('type'=>'varchar(50)', 'label'=>'Town', 'enabled'=>1, 'visible'=>-2, 'position'=>55, 'searchall'=>1),
  114. 'fk_departement' =>array('type'=>'sellist:c_departements:label:rowid::active=1', 'label'=>'State', 'enabled'=>1, 'visible'=>0, 'position'=>60),
  115. 'fk_pays' =>array('type'=>'sellist:c_country:label:rowid::active=1', 'label'=>'Country', 'enabled'=>1, 'visible'=>-2, 'position'=>65),
  116. 'phone' =>array('type'=>'varchar(20)', 'label'=>'Phone', 'enabled'=>1, 'visible'=>-2, 'position'=>70, 'searchall'=>1),
  117. 'fax' =>array('type'=>'varchar(20)', 'label'=>'Fax', 'enabled'=>1, 'visible'=>-2, 'position'=>75, 'searchall'=>1),
  118. //'fk_user_author' =>array('type'=>'integer', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-2, 'position'=>82),
  119. 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>300),
  120. 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>301),
  121. //'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000),
  122. //'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'ModelPDF', 'enabled'=>1, 'visible'=>0, 'position'=>1010),
  123. 'statut' =>array('type'=>'tinyint(4)', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'position'=>500, 'css'=>'minwidth50'),
  124. );
  125. /**
  126. * Warehouse closed, inactive
  127. */
  128. const STATUS_CLOSED = 0;
  129. /**
  130. * Warehouse open and any operations are allowed (customer shipping, supplier dispatch, internal stock transfers/corrections).
  131. */
  132. const STATUS_OPEN_ALL = 1;
  133. /**
  134. * Warehouse open and only operations for stock transfers/corrections allowed (not for customer shipping and supplier dispatch).
  135. */
  136. const STATUS_OPEN_INTERNAL = 2;
  137. /**
  138. * Warehouse open and any operations are allowed, but warehouse is not included into calculation of stock.
  139. */
  140. const STATUS_OPENEXT_ALL = 3; // TODO Implement this
  141. /**
  142. * Constructor
  143. *
  144. * @param DoliDB $db Database handler
  145. */
  146. public function __construct($db)
  147. {
  148. global $conf;
  149. $this->db = $db;
  150. $this->statuts[self::STATUS_CLOSED] = 'Closed2';
  151. if (!empty($conf->global->ENTREPOT_EXTRA_STATUS)) {
  152. $this->statuts[self::STATUS_OPEN_ALL] = 'OpenAnyMovement';
  153. $this->statuts[self::STATUS_OPEN_INTERNAL] = 'OpenInternal';
  154. } else {
  155. $this->statuts[self::STATUS_OPEN_ALL] = 'Opened';
  156. }
  157. }
  158. /**
  159. * Creation d'un entrepot en base
  160. *
  161. * @param User $user Object user that create the warehouse
  162. * @return int >0 if OK, =<0 if KO
  163. */
  164. public function create($user)
  165. {
  166. global $conf;
  167. $error = 0;
  168. $this->label = trim(!empty($this->label) ? $this->label : $this->libelle);
  169. // Error if label not defined
  170. if ($this->label == '') {
  171. $this->error = "ErrorFieldRequired";
  172. return 0;
  173. }
  174. $now = dol_now();
  175. $this->db->begin();
  176. $sql = "INSERT INTO ".$this->db->prefix()."entrepot (ref, entity, datec, fk_user_author, fk_parent, fk_project)";
  177. $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").")";
  178. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  179. $result = $this->db->query($sql);
  180. if ($result) {
  181. $id = $this->db->last_insert_id($this->db->prefix()."entrepot");
  182. if ($id > 0) {
  183. $this->id = $id;
  184. if (!$error) {
  185. $result = $this->update($id, $user);
  186. if ($result <= 0) {
  187. $error++;
  188. }
  189. }
  190. // Actions on extra fields
  191. if (!$error) {
  192. if (!$error) {
  193. $result = $this->insertExtraFields();
  194. if ($result < 0) {
  195. $error++;
  196. }
  197. }
  198. }
  199. if (!$error) {
  200. $this->db->commit();
  201. return $id;
  202. } else {
  203. dol_syslog(get_class($this)."::create return -3");
  204. $this->db->rollback();
  205. return -3;
  206. }
  207. } else {
  208. $this->error = "Failed to get insert id";
  209. dol_syslog(get_class($this)."::create return -2");
  210. return -2;
  211. }
  212. } else {
  213. $this->error = $this->db->error();
  214. dol_syslog(get_class($this)."::create Error ".$this->db->error());
  215. $this->db->rollback();
  216. return -1;
  217. }
  218. }
  219. /**
  220. * Update properties of a warehouse
  221. *
  222. * @param int $id id of warehouse to modify
  223. * @param User $user User object
  224. * @return int >0 if OK, <0 if KO
  225. */
  226. public function update($id, $user)
  227. {
  228. global $conf;
  229. $error = 0;
  230. if (empty($id)) {
  231. $id = $this->id;
  232. }
  233. if (empty($this->label)) {
  234. $this->label = $this->libelle; // For backward compatibility
  235. }
  236. // Check if new parent is already a child of current warehouse
  237. if (!empty($this->fk_parent)) {
  238. $TChildWarehouses = array($id);
  239. $TChildWarehouses = $this->get_children_warehouses($this->id, $TChildWarehouses);
  240. if (in_array($this->fk_parent, $TChildWarehouses)) {
  241. $this->error = 'ErrorCannotAddThisParentWarehouse';
  242. return -2;
  243. }
  244. }
  245. $this->label = trim(!empty($this->label) ? $this->label : $this->libelle);
  246. $this->description = trim($this->description);
  247. $this->lieu = trim($this->lieu);
  248. $this->address = trim($this->address);
  249. $this->zip = trim($this->zip);
  250. $this->town = trim($this->town);
  251. $this->country_id = ($this->country_id > 0 ? $this->country_id : 0);
  252. $sql = "UPDATE ".$this->db->prefix()."entrepot";
  253. $sql .= " SET ref = '".$this->db->escape($this->label)."'";
  254. $sql .= ", fk_parent = ".(($this->fk_parent > 0) ? $this->fk_parent : "NULL");
  255. $sql .= ", fk_project = ".(($this->fk_project > 0) ? $this->fk_project : "NULL");
  256. $sql .= ", description = '".$this->db->escape($this->description)."'";
  257. $sql .= ", statut = ".((int) $this->statut);
  258. $sql .= ", lieu = '".$this->db->escape($this->lieu)."'";
  259. $sql .= ", address = '".$this->db->escape($this->address)."'";
  260. $sql .= ", zip = '".$this->db->escape($this->zip)."'";
  261. $sql .= ", town = '".$this->db->escape($this->town)."'";
  262. $sql .= ", fk_pays = ".((int) $this->country_id);
  263. $sql .= ", phone = '".$this->db->escape($this->phone)."'";
  264. $sql .= ", fax = '".$this->db->escape($this->fax)."'";
  265. $sql .= " WHERE rowid = ".((int) $id);
  266. $this->db->begin();
  267. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  268. $resql = $this->db->query($sql);
  269. if (!$resql) {
  270. $error++;
  271. $this->errors[] = "Error ".$this->db->lasterror();
  272. }
  273. if (!$error) {
  274. $result = $this->insertExtraFields();
  275. if ($result < 0) {
  276. $error++;
  277. }
  278. }
  279. if (!$error) {
  280. $this->db->commit();
  281. return 1;
  282. } else {
  283. $this->db->rollback();
  284. $this->error = $this->db->lasterror();
  285. return -1;
  286. }
  287. }
  288. /**
  289. * Delete a warehouse
  290. *
  291. * @param User $user Object user that made deletion
  292. * @param int $notrigger 1=No trigger
  293. * @return int <0 if KO, >0 if OK
  294. */
  295. public function delete($user, $notrigger = 0)
  296. {
  297. global $conf;
  298. $error = 0;
  299. dol_syslog(get_class($this)."::delete id=".$this->id, LOG_DEBUG);
  300. $this->db->begin();
  301. if (!$error && empty($notrigger)) {
  302. // Call trigger
  303. $result = $this->call_trigger('WAREHOUSE_DELETE', $user);
  304. if ($result < 0) {
  305. $error++;
  306. }
  307. // End call triggers
  308. }
  309. $elements = array('stock_mouvement', 'product_stock', 'product_warehouse_properties');
  310. foreach ($elements as $table) {
  311. if (!$error) {
  312. $sql = "DELETE FROM ".$this->db->prefix().$table;
  313. $sql .= " WHERE fk_entrepot = ".((int) $this->id);
  314. $result = $this->db->query($sql);
  315. if (!$result) {
  316. $error++;
  317. $this->errors[] = $this->db->lasterror();
  318. }
  319. }
  320. }
  321. // Removed extrafields
  322. if (!$error) {
  323. if (!$error) {
  324. $result = $this->deleteExtraFields();
  325. if ($result < 0) {
  326. $error++;
  327. dol_syslog(get_class($this)."::delete Error ".$this->error, LOG_ERR);
  328. }
  329. }
  330. }
  331. if (!$error) {
  332. $sql = "DELETE FROM ".$this->db->prefix()."entrepot";
  333. $sql .= " WHERE rowid = ".((int) $this->id);
  334. $resql1 = $this->db->query($sql);
  335. if (!$resql1) {
  336. $error++;
  337. $this->errors[] = $this->db->lasterror();
  338. dol_syslog(get_class($this)."::delete Error ".$this->db->lasterror(), LOG_ERR);
  339. }
  340. }
  341. if (!$error) {
  342. // Update denormalized fields because we change content of produt_stock. Warning: Do not use "SET p.stock", does not works with pgsql
  343. $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)";
  344. $resql2 = $this->db->query($sql);
  345. if (!$resql2) {
  346. $error++;
  347. $this->errors[] = $this->db->lasterror();
  348. dol_syslog(get_class($this)."::delete Error ".$this->db->lasterror(), LOG_ERR);
  349. }
  350. }
  351. if (!$error) {
  352. $this->db->commit();
  353. return 1;
  354. } else {
  355. $this->db->rollback();
  356. return -1;
  357. }
  358. }
  359. /**
  360. * Load warehouse data
  361. *
  362. * @param int $id Warehouse id
  363. * @param string $ref Warehouse label
  364. * @return int >0 if OK, <0 if KO
  365. */
  366. public function fetch($id, $ref = '')
  367. {
  368. global $conf;
  369. dol_syslog(get_class($this)."::fetch id=".$id." ref=".$ref);
  370. // Check parameters
  371. if (!$id && !$ref) {
  372. $this->error = 'ErrorWrongParameters';
  373. dol_syslog(get_class($this)."::fetch ".$this->error);
  374. return -1;
  375. }
  376. $sql = "SELECT rowid, entity, fk_parent, fk_project, ref as label, description, statut, lieu, address, zip, town, fk_pays as country_id, phone, fax,";
  377. $sql .= " model_pdf, import_key";
  378. $sql .= " FROM ".$this->db->prefix()."entrepot";
  379. if ($id) {
  380. $sql .= " WHERE rowid = ".((int) $id);
  381. } else {
  382. $sql .= " WHERE entity IN (".getEntity('stock').")";
  383. if ($ref) {
  384. $sql .= " AND ref = '".$this->db->escape($ref)."'";
  385. }
  386. }
  387. $result = $this->db->query($sql);
  388. if ($result) {
  389. if ($this->db->num_rows($result) > 0) {
  390. $obj = $this->db->fetch_object($result);
  391. $this->id = $obj->rowid;
  392. $this->entity = $obj->entity;
  393. $this->fk_parent = $obj->fk_parent;
  394. $this->fk_project = $obj->fk_project;
  395. $this->ref = $obj->label;
  396. $this->label = $obj->label;
  397. $this->libelle = $obj->label; // deprecated
  398. $this->description = $obj->description;
  399. $this->statut = $obj->statut;
  400. $this->lieu = $obj->lieu;
  401. $this->address = $obj->address;
  402. $this->zip = $obj->zip;
  403. $this->town = $obj->town;
  404. $this->country_id = $obj->country_id;
  405. $this->phone = $obj->phone;
  406. $this->fax = $obj->fax;
  407. $this->model_pdf = $obj->model_pdf;
  408. $this->import_key = $obj->import_key;
  409. // Retrieve all extrafield
  410. // fetch optionals attributes and labels
  411. $this->fetch_optionals();
  412. include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  413. $tmp = getCountry($this->country_id, 'all');
  414. $this->country = $tmp['label'];
  415. $this->country_code = $tmp['code'];
  416. return 1;
  417. } else {
  418. $this->error = "Record Not Found";
  419. return 0;
  420. }
  421. } else {
  422. $this->error = $this->db->error();
  423. return -1;
  424. }
  425. }
  426. /**
  427. * Load warehouse info data
  428. *
  429. * @param int $id warehouse id
  430. * @return void
  431. */
  432. public function info($id)
  433. {
  434. $sql = "SELECT e.rowid, e.datec, e.tms as datem, e.fk_user_author";
  435. $sql .= " FROM ".$this->db->prefix()."entrepot as e";
  436. $sql .= " WHERE e.rowid = ".((int) $id);
  437. dol_syslog(get_class($this)."::info", LOG_DEBUG);
  438. $result = $this->db->query($sql);
  439. if ($result) {
  440. if ($this->db->num_rows($result)) {
  441. $obj = $this->db->fetch_object($result);
  442. $this->id = $obj->rowid;
  443. $this->user_creation_id = $obj->fk_user_author;
  444. $this->date_creation = $this->db->jdate($obj->datec);
  445. $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
  446. }
  447. $this->db->free($result);
  448. } else {
  449. dol_print_error($this->db);
  450. }
  451. }
  452. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  453. /**
  454. * Return list of all warehouses
  455. *
  456. * @param int $status Status
  457. * @return array Array list of warehouses
  458. */
  459. public function list_array($status = 1)
  460. {
  461. // phpcs:enable
  462. $liste = array();
  463. $sql = "SELECT rowid, ref as label";
  464. $sql .= " FROM ".$this->db->prefix()."entrepot";
  465. $sql .= " WHERE entity IN (".getEntity('stock').")";
  466. $sql .= " AND statut = ".((int) $status);
  467. $result = $this->db->query($sql);
  468. $i = 0;
  469. $num = $this->db->num_rows($result);
  470. if ($result) {
  471. while ($i < $num) {
  472. $row = $this->db->fetch_row($result);
  473. $liste[$row[0]] = $row[1];
  474. $i++;
  475. }
  476. $this->db->free($result);
  477. }
  478. return $liste;
  479. }
  480. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  481. /**
  482. * Return number of unique different product into a warehouse
  483. *
  484. * @return Array Array('nb'=>Nb, 'value'=>Value)
  485. */
  486. public function nb_different_products()
  487. {
  488. // phpcs:enable
  489. $ret = array();
  490. $sql = "SELECT count(distinct p.rowid) as nb";
  491. $sql .= " FROM ".$this->db->prefix()."product_stock as ps";
  492. $sql .= ", ".$this->db->prefix()."product as p";
  493. $sql .= " WHERE ps.fk_entrepot = ".((int) $this->id);
  494. $sql .= " AND ps.fk_product = p.rowid";
  495. //print $sql;
  496. $result = $this->db->query($sql);
  497. if ($result) {
  498. $obj = $this->db->fetch_object($result);
  499. $ret['nb'] = $obj->nb;
  500. $this->db->free($result);
  501. } else {
  502. $this->error = $this->db->lasterror();
  503. return -1;
  504. }
  505. return $ret;
  506. }
  507. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  508. /**
  509. * Return stock and value of warehosue
  510. *
  511. * @return Array Array('nb'=>Nb, 'value'=>Value)
  512. */
  513. public function nb_products()
  514. {
  515. global $conf;
  516. // phpcs:enable
  517. $ret = array();
  518. //For MultiCompany PMP per entity
  519. $separatedPMP = false;
  520. if (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED) && !empty($conf->global->MULTICOMPANY_PMP_PER_ENTITY_ENABLED)) {
  521. $separatedPMP = true;
  522. }
  523. if ($separatedPMP) {
  524. $sql = "SELECT sum(ps.reel) as nb, sum(ps.reel * pa.pmp) as value";
  525. } else {
  526. $sql = "SELECT sum(ps.reel) as nb, sum(ps.reel * p.pmp) as value";
  527. }
  528. $sql .= " FROM ".$this->db->prefix()."product_stock as ps";
  529. $sql .= ", ".$this->db->prefix()."product as p";
  530. if ($separatedPMP) {
  531. $sql .= ", ".$this->db->prefix()."product_perentity as pa";
  532. }
  533. $sql .= " WHERE ps.fk_entrepot = ".((int) $this->id);
  534. if ($separatedPMP) {
  535. $sql .= " AND pa.fk_product = p.rowid AND pa.entity = ". (int) $conf->entity;
  536. }
  537. $sql .= " AND ps.fk_product = p.rowid";
  538. //print $sql;
  539. $result = $this->db->query($sql);
  540. if ($result) {
  541. $obj = $this->db->fetch_object($result);
  542. $ret['nb'] = $obj->nb;
  543. $ret['value'] = $obj->value;
  544. $this->db->free($result);
  545. } else {
  546. $this->error = $this->db->lasterror();
  547. return -1;
  548. }
  549. return $ret;
  550. }
  551. /**
  552. * Return label of status of object
  553. *
  554. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  555. * @return string Label of status
  556. */
  557. public function getLibStatut($mode = 0)
  558. {
  559. return $this->LibStatut($this->statut, $mode);
  560. }
  561. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  562. /**
  563. * Return label of a given status
  564. *
  565. * @param int $status Id status
  566. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  567. * @return string Label of status
  568. */
  569. public function LibStatut($status, $mode = 0)
  570. {
  571. // phpcs:enable
  572. global $langs;
  573. $statusType = 'status5';
  574. if ($status > 0) {
  575. $statusType = 'status4';
  576. }
  577. $langs->load('stocks');
  578. $label = $langs->transnoentitiesnoconv($this->statuts[$status]);
  579. $labelshort = $langs->transnoentitiesnoconv($this->statuts[$status]);
  580. return dolGetStatus($label, $labelshort, '', $statusType, $mode);
  581. }
  582. /**
  583. * Return clickable name (possibility with the pictogram)
  584. *
  585. * @param int $withpicto with pictogram
  586. * @param string $option Where the link point to
  587. * @param int $showfullpath 0=Show ref only. 1=Show full path instead of Ref (this->fk_parent must be defined)
  588. * @param int $notooltip 1=Disable tooltip
  589. * @param string $morecss Add more css on link
  590. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  591. * @return string String with URL
  592. */
  593. public function getNomUrl($withpicto = 0, $option = '', $showfullpath = 0, $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  594. {
  595. global $conf, $langs, $hookmanager;
  596. $langs->load("stocks");
  597. if (!empty($conf->dol_no_mouse_hover)) {
  598. $notooltip = 1; // Force disable tooltips
  599. }
  600. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) && $withpicto) {
  601. $withpicto = 0;
  602. }
  603. $result = '';
  604. $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Warehouse").'</u>';
  605. if (isset($this->statut)) {
  606. $label .= ' '.$this->getLibStatut(5);
  607. }
  608. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.(empty($this->ref) ? (empty($this->label) ? $this->libelle : $this->label) : $this->ref);
  609. if (!empty($this->lieu)) {
  610. $label .= '<br><b>'.$langs->trans('LocationSummary').':</b> '.$this->lieu;
  611. }
  612. $url = DOL_URL_ROOT.'/product/stock/card.php?id='.$this->id;
  613. if ($option != 'nolink') {
  614. // Add param to save lastsearch_values or not
  615. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  616. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  617. $add_save_lastsearch_values = 1;
  618. }
  619. if ($add_save_lastsearch_values) {
  620. $url .= '&save_lastsearch_values=1';
  621. }
  622. }
  623. $linkclose = '';
  624. if (empty($notooltip)) {
  625. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  626. $label = $langs->trans("Warehouse");
  627. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  628. }
  629. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  630. $linkclose .= ' class="classfortooltip"';
  631. }
  632. $linkstart = '<a href="'.$url.'"';
  633. $linkstart .= $linkclose.'>';
  634. $linkend = '</a>';
  635. $result .= $linkstart;
  636. if ($withpicto) {
  637. $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);
  638. }
  639. if ($withpicto != 2) {
  640. $result .= (($showfullpath || !empty($conf->global->STOCK_ALWAYS_SHOW_FULL_ARBO)) ? $this->get_full_arbo() : (empty($this->label) ? $this->libelle : $this->label));
  641. }
  642. $result .= $linkend;
  643. global $action;
  644. $hookmanager->initHooks(array('warehousedao'));
  645. $parameters = array('id'=>$this->id, 'getnomurl' => &$result, 'withpicto' => $withpicto, 'option' => $option, 'showfullpath' => $showfullpath, 'notooltip'=> $notooltip);
  646. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  647. if ($reshook > 0) {
  648. $result = $hookmanager->resPrint;
  649. } else {
  650. $result .= $hookmanager->resPrint;
  651. }
  652. return $result;
  653. }
  654. /**
  655. * Initialise an instance with random values.
  656. * Used to build previews or test instances.
  657. * id must be 0 if object instance is a specimen.
  658. *
  659. * @return void
  660. */
  661. public function initAsSpecimen()
  662. {
  663. global $user, $langs, $conf, $mysoc;
  664. $now = dol_now();
  665. // Initialize parameters
  666. $this->id = 0;
  667. $this->label = 'WAREHOUSE SPECIMEN';
  668. $this->description = 'WAREHOUSE SPECIMEN '.dol_print_date($now, 'dayhourlog');
  669. $this->statut = 1;
  670. $this->specimen = 1;
  671. $this->lieu = 'Location test';
  672. $this->address = '21 jump street';
  673. $this->zip = '99999';
  674. $this->town = 'MyTown';
  675. $this->country_id = 1;
  676. $this->country_code = 'FR';
  677. }
  678. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  679. /**
  680. * Return full path to current warehouse
  681. *
  682. * @return string String full path to current warehouse separated by " >> "
  683. */
  684. public function get_full_arbo()
  685. {
  686. // phpcs:enable
  687. global $user, $langs, $conf;
  688. $TArbo = array(empty($this->label) ? $this->libelle : $this->label);
  689. $protection = 100; // We limit depth of warehouses to 100
  690. $warehousetmp = new Entrepot($this->db);
  691. $parentid = $this->fk_parent; // If parent_id not defined on current object, we do not start consecutive searches of parents
  692. $i = 0;
  693. while ($parentid > 0 && $i < $protection) {
  694. $sql = "SELECT fk_parent FROM ".$this->db->prefix()."entrepot WHERE rowid = ".((int) $parentid);
  695. $resql = $this->db->query($sql);
  696. if ($resql) {
  697. $objarbo = $this->db->fetch_object($resql);
  698. if ($objarbo) {
  699. $warehousetmp->fetch($parentid);
  700. $TArbo[] = $warehousetmp->label;
  701. $parentid = $objarbo->fk_parent;
  702. } else {
  703. break;
  704. }
  705. } else {
  706. dol_print_error($this->db);
  707. }
  708. $i++;
  709. }
  710. return implode(' >> ', array_reverse($TArbo));
  711. }
  712. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  713. /**
  714. * Return array of children warehouses ids from $id warehouse (recursive function)
  715. *
  716. * @param int $id id parent warehouse
  717. * @param integer[] $TChildWarehouses array which will contain all children (param by reference)
  718. * @return integer[] $TChildWarehouses array which will contain all children
  719. */
  720. public function get_children_warehouses($id, &$TChildWarehouses)
  721. {
  722. // phpcs:enable
  723. $sql = "SELECT rowid
  724. FROM ".$this->db->prefix()."entrepot
  725. WHERE fk_parent = ".((int) $id);
  726. $resql = $this->db->query($sql);
  727. if ($resql) {
  728. while ($res = $this->db->fetch_object($resql)) {
  729. $TChildWarehouses[] = $res->rowid;
  730. $this->get_children_warehouses($res->rowid, $TChildWarehouses);
  731. }
  732. }
  733. return $TChildWarehouses;
  734. }
  735. /**
  736. * Create object on disk
  737. *
  738. * @param string $modele force le modele a utiliser ('' to not force)
  739. * @param Translate $outputlangs Object langs to use for output
  740. * @param int $hidedetails Hide details of lines
  741. * @param int $hidedesc Hide description
  742. * @param int $hideref Hide ref
  743. * @return int 0 if KO, 1 if OK
  744. */
  745. public function generateDocument($modele, $outputlangs = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
  746. {
  747. global $conf, $user, $langs;
  748. $langs->load("stocks");
  749. $outputlangs->load("products");
  750. if (!dol_strlen($modele)) {
  751. $modele = 'standard';
  752. if ($this->model_pdf) {
  753. $modele = $this->model_pdf;
  754. } elseif (!empty($conf->global->STOCK_ADDON_PDF)) {
  755. $modele = $conf->global->STOCK_ADDON_PDF;
  756. }
  757. }
  758. $modelpath = "core/modules/stock/doc/";
  759. return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
  760. }
  761. /**
  762. * Sets object to supplied categories.
  763. *
  764. * Deletes object from existing categories not supplied.
  765. * Adds it to non existing supplied categories.
  766. * Existing categories are left untouch.
  767. *
  768. * @param int[]|int $categories Category or categories IDs
  769. * @return int <0 if KO, >0 if OK
  770. */
  771. public function setCategories($categories)
  772. {
  773. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  774. return parent::setCategoriesCommon($categories, Categorie::TYPE_WAREHOUSE);
  775. }
  776. }