PageRenderTime 44ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/class/commonobject.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 2750 lines | 1800 code | 287 blank | 663 comment | 580 complexity | 600cdc8b234b25507da49b2a6db75b39 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2013 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  6. * Copyright (C) 2011-2012 Philippe Grand <philippe.grand@atoo-net.com>
  7. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
  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 <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/core/class/commonobject.class.php
  24. * \ingroup core
  25. * \brief File of parent class of all other business classes (invoices, contracts, proposals, orders, ...)
  26. */
  27. /**
  28. * Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
  29. */
  30. abstract class CommonObject {
  31. protected $db;
  32. public $error;
  33. public $errors;
  34. public $canvas; // Contains canvas name if it is
  35. /**
  36. * class constructor
  37. *
  38. * @param couchClient $db Database handler
  39. */
  40. function __construct($db = '') {
  41. global $conf;
  42. $this->db = $db;
  43. return 1;
  44. }
  45. /**
  46. * Return full name (civility+' '+name+' '+lastname)
  47. *
  48. * @param Translate $langs Language object for translation of civility
  49. * @param int $option 0=No option, 1=Add civility
  50. * @param int $nameorder -1=Auto, 0=Lastname+Firstname, 1=Firstname+Lastname
  51. * @param int $maxlen Maximum length
  52. * @return string String with full name
  53. */
  54. function getFullName($langs, $option = 0, $nameorder = -1, $maxlen = 0) {
  55. global $conf;
  56. $lastname = $this->Lastname;
  57. $firstname = $this->Firstname;
  58. if (empty($lastname))
  59. $lastname = ($this->name ? $this->name : $this->nom);
  60. if (empty($firstname))
  61. $firstname = $this->prenom;
  62. $ret = '';
  63. if ($option && $this->civilite_id) {
  64. if ($langs->transnoentitiesnoconv("Civility" . $this->civilite_id) != "Civility" . $this->civilite_id)
  65. $ret.=$langs->transnoentitiesnoconv("Civility" . $this->civilite_id) . ' ';
  66. else
  67. $ret.=$this->civilite_id . ' ';
  68. }
  69. // If order not defined, we use the setup
  70. if ($nameorder < 0)
  71. $nameorder = (empty($conf->global->MAIN_FIRSTNAME_NAME_POSITION));
  72. if ($nameorder) {
  73. $ret.=$firstname;
  74. if ($firstname && $lastname)
  75. $ret.=' ';
  76. $ret.=$lastname;
  77. }
  78. else {
  79. $ret.=$lastname;
  80. if ($firstname && $lastname)
  81. $ret.=' ';
  82. $ret.=$firstname;
  83. }
  84. return dol_trunc($ret, $maxlen);
  85. }
  86. /**
  87. * Check if ref is used.
  88. *
  89. * @return int <0 if KO, 0 if not found, >0 if found
  90. */
  91. function verifyNumRef() {
  92. global $conf;
  93. $sql = "SELECT rowid";
  94. $sql.= " FROM " . MAIN_DB_PREFIX . $this->table_element;
  95. $sql.= " WHERE ref = '" . $this->ref . "'";
  96. $sql.= " AND entity = " . $conf->entity;
  97. dol_syslog(get_class($this) . "::verifyNumRef sql=" . $sql, LOG_DEBUG);
  98. $resql = $this->db->query($sql);
  99. if ($resql) {
  100. $num = $this->db->num_rows($resql);
  101. return $num;
  102. } else {
  103. $this->error = $this->db->lasterror();
  104. dol_syslog(get_class($this) . "::verifyNumRef " . $this->error, LOG_ERR);
  105. return -1;
  106. }
  107. }
  108. /**
  109. * Add a link between element $this->element and a contact
  110. *
  111. * @param int $fk_socpeople Id of contact to link
  112. * @param int $type_contact Type of contact (code or id)
  113. * @param int $source external=Contact extern (llx_socpeople), internal=Contact intern (llx_user)
  114. * @param int $notrigger Disable all triggers
  115. * @return int <0 if KO, >0 if OK
  116. */
  117. function add_contact($fk_socpeople, $type_contact, $source = 'external', $notrigger = 0) {
  118. global $user, $conf, $langs;
  119. $error = 0;
  120. dol_syslog(get_class($this) . "::add_contact $fk_socpeople, $type_contact, $source");
  121. // Check parameters
  122. if ($fk_socpeople <= 0) {
  123. $this->error = $langs->trans("ErrorWrongValueForParameter", "1");
  124. dol_syslog(get_class($this) . "::add_contact " . $this->error, LOG_ERR);
  125. return -1;
  126. }
  127. if (!$type_contact) {
  128. $this->error = $langs->trans("ErrorWrongValueForParameter", "2");
  129. dol_syslog(get_class($this) . "::add_contact " . $this->error, LOG_ERR);
  130. return -2;
  131. }
  132. $id_type_contact = 0;
  133. if (is_numeric($type_contact)) {
  134. $id_type_contact = $type_contact;
  135. } else {
  136. // On recherche id type_contact
  137. $sql = "SELECT tc.rowid";
  138. $sql.= " FROM " . MAIN_DB_PREFIX . "c_type_contact as tc";
  139. $sql.= " WHERE element='" . $this->element . "'";
  140. $sql.= " AND source='" . $source . "'";
  141. $sql.= " AND code='" . $type_contact . "' AND active=1";
  142. $resql = $this->db->query($sql);
  143. if ($resql) {
  144. $obj = $this->db->fetch_object($resql);
  145. $id_type_contact = $obj->rowid;
  146. }
  147. }
  148. $datecreate = dol_now();
  149. // Insertion dans la base
  150. $sql = "INSERT INTO " . MAIN_DB_PREFIX . "element_contact";
  151. $sql.= " (element_id, fk_socpeople, datecreate, statut, fk_c_type_contact) ";
  152. $sql.= " VALUES (" . $this->id . ", " . $fk_socpeople . " , ";
  153. $sql.= $this->db->idate($datecreate);
  154. $sql.= ", 4, '" . $id_type_contact . "' ";
  155. $sql.= ")";
  156. dol_syslog(get_class($this) . "::add_contact sql=" . $sql);
  157. $resql = $this->db->query($sql);
  158. if ($resql) {
  159. if (!$notrigger) {
  160. // Call triggers
  161. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  162. $interface = new Interfaces($this->db);
  163. $result = $interface->run_triggers(strtoupper($this->element) . '_ADD_CONTACT', $this, $user, $langs, $conf);
  164. if ($result < 0) {
  165. $error++;
  166. $this->errors = $interface->errors;
  167. }
  168. // End call triggers
  169. }
  170. return 1;
  171. } else {
  172. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  173. $this->error = $this->db->errno();
  174. return -2;
  175. } else {
  176. $this->error = $this->db->error();
  177. dol_syslog($this->error, LOG_ERR);
  178. return -1;
  179. }
  180. }
  181. }
  182. /**
  183. * Update a link to contact line
  184. *
  185. * @param int $rowid Id of line contact-element
  186. * @param int $statut New status of link
  187. * @param int $type_contact_id Id of contact type (not modified if 0)
  188. * @return int <0 if KO, >= 0 if OK
  189. */
  190. function update_contact($rowid, $statut, $type_contact_id = 0) {
  191. // Insertion dans la base
  192. $sql = "UPDATE " . MAIN_DB_PREFIX . "element_contact set";
  193. $sql.= " statut = " . $statut;
  194. if ($type_contact_id)
  195. $sql.= ", fk_c_type_contact = '" . $type_contact_id . "'";
  196. $sql.= " where rowid = " . $rowid;
  197. $resql = $this->db->query($sql);
  198. if ($resql) {
  199. return 0;
  200. } else {
  201. $this->error = $this->db->lasterror();
  202. return -1;
  203. }
  204. }
  205. /**
  206. * Delete a link to contact line
  207. *
  208. * @param int $rowid Id of contact link line to delete
  209. * @param int $notrigger Disable all triggers
  210. * @return int >0 if OK, <0 if KO
  211. */
  212. function delete_contact($rowid, $notrigger = 0) {
  213. global $user, $langs, $conf;
  214. $error = 0;
  215. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "element_contact";
  216. $sql.= " WHERE rowid =" . $rowid;
  217. dol_syslog(get_class($this) . "::delete_contact sql=" . $sql);
  218. if ($this->db->query($sql)) {
  219. if (!$notrigger) {
  220. // Call triggers
  221. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  222. $interface = new Interfaces($this->db);
  223. $result = $interface->run_triggers(strtoupper($this->element) . '_DELETE_CONTACT', $this, $user, $langs, $conf);
  224. if ($result < 0) {
  225. $error++;
  226. $this->errors = $interface->errors;
  227. }
  228. // End call triggers
  229. }
  230. return 1;
  231. } else {
  232. $this->error = $this->db->lasterror();
  233. dol_syslog(get_class($this) . "::delete_contact error=" . $this->error, LOG_ERR);
  234. return -1;
  235. }
  236. }
  237. /**
  238. * Delete all links between an object $this and all its contacts
  239. *
  240. * @return int >0 if OK, <0 if KO
  241. */
  242. function delete_linked_contact() {
  243. $temp = array();
  244. $typeContact = $this->liste_type_contact('');
  245. foreach ($typeContact as $key => $value) {
  246. array_push($temp, $key);
  247. }
  248. $listId = implode(",", $temp);
  249. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "element_contact";
  250. $sql.= " WHERE element_id =" . $this->id;
  251. $sql.= " AND fk_c_type_contact IN (" . $listId . ")";
  252. dol_syslog(get_class($this) . "::delete_linked_contact sql=" . $sql, LOG_DEBUG);
  253. if ($this->db->query($sql)) {
  254. return 1;
  255. } else {
  256. $this->error = $this->db->lasterror();
  257. dol_syslog(get_class($this) . "::delete_linked_contact error=" . $this->error, LOG_ERR);
  258. return -1;
  259. }
  260. }
  261. /**
  262. * Get array of all contacts for an object
  263. *
  264. * @param int $statut Status of lines to get (-1=all)
  265. * @param string $source Source of contact: external or thirdparty (llx_socpeople) or internal (llx_user)
  266. * @param int $list 0:Return array contains all properties, 1:Return array contains just id
  267. * @return array Array of contacts
  268. */
  269. function liste_contact($statut = -1, $source = 'external', $list = 0) {
  270. global $langs;
  271. $tab = array();
  272. $sql = "SELECT ec.rowid, ec.statut, ec.fk_socpeople as id"; // This field contains id of llx_socpeople or id of llx_user
  273. if ($source == 'internal')
  274. $sql.=", '-1' as socid";
  275. if ($source == 'external' || $source == 'thirdparty')
  276. $sql.=", t.fk_soc as socid";
  277. $sql.= ", t.civilite as civility, t.name as lastname, t.firstname, t.email";
  278. $sql.= ", tc.source, tc.element, tc.code, tc.libelle";
  279. $sql.= " FROM " . MAIN_DB_PREFIX . "c_type_contact tc";
  280. $sql.= ", " . MAIN_DB_PREFIX . "element_contact ec";
  281. if ($source == 'internal')
  282. $sql.=" LEFT JOIN " . MAIN_DB_PREFIX . "user t on ec.fk_socpeople = t.rowid";
  283. if ($source == 'external' || $source == 'thirdparty')
  284. $sql.=" LEFT JOIN " . MAIN_DB_PREFIX . "socpeople t on ec.fk_socpeople = t.rowid";
  285. $sql.= " WHERE ec.element_id =" . $this->id;
  286. $sql.= " AND ec.fk_c_type_contact=tc.rowid";
  287. $sql.= " AND tc.element='" . $this->element . "'";
  288. if ($source == 'internal')
  289. $sql.= " AND tc.source = 'internal'";
  290. if ($source == 'external' || $source == 'thirdparty')
  291. $sql.= " AND tc.source = 'external'";
  292. $sql.= " AND tc.active=1";
  293. if ($statut >= 0)
  294. $sql.= " AND ec.statut = '" . $statut . "'";
  295. $sql.=" ORDER BY t.name ASC";
  296. dol_syslog(get_class($this) . "::liste_contact sql=" . $sql);
  297. $resql = $this->db->query($sql);
  298. if ($resql) {
  299. $num = $this->db->num_rows($resql);
  300. $i = 0;
  301. while ($i < $num) {
  302. $obj = $this->db->fetch_object($resql);
  303. if (!$list) {
  304. $transkey = "TypeContact_" . $obj->element . "_" . $obj->source . "_" . $obj->code;
  305. $libelle_type = ($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->libelle);
  306. $tab[$i] = array('source' => $obj->source, 'socid' => $obj->socid, 'id' => $obj->id,
  307. 'nom' => $obj->lastname, // For backward compatibility
  308. 'civility' => $obj->civility, 'lastname' => $obj->lastname, 'firstname' => $obj->firstname, 'email' => $obj->email,
  309. 'rowid' => $obj->rowid, 'code' => $obj->code, 'libelle' => $libelle_type, 'status' => $obj->statut);
  310. } else {
  311. $tab[$i] = $obj->id;
  312. }
  313. $i++;
  314. }
  315. return $tab;
  316. } else {
  317. $this->error = $this->db->error();
  318. dol_print_error($this->db);
  319. return -1;
  320. }
  321. }
  322. /**
  323. * Update status of a contact linked to object
  324. *
  325. * @param int $rowid Id of link between object and contact
  326. * @return int <0 if KO, >=0 if OK
  327. */
  328. function swapContactStatus($rowid) {
  329. $sql = "SELECT ec.datecreate, ec.statut, ec.fk_socpeople, ec.fk_c_type_contact,";
  330. $sql.= " tc.code, tc.libelle";
  331. //$sql.= ", s.fk_soc";
  332. $sql.= " FROM (" . MAIN_DB_PREFIX . "element_contact as ec, " . MAIN_DB_PREFIX . "c_type_contact as tc)";
  333. //$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as s ON ec.fk_socpeople=s.rowid"; // Si contact de type external, alors il est lie a une societe
  334. $sql.= " WHERE ec.rowid =" . $rowid;
  335. $sql.= " AND ec.fk_c_type_contact=tc.rowid";
  336. $sql.= " AND tc.element = '" . $this->element . "'";
  337. dol_syslog(get_class($this) . "::swapContactStatus sql=" . $sql);
  338. $resql = $this->db->query($sql);
  339. if ($resql) {
  340. $obj = $this->db->fetch_object($resql);
  341. $newstatut = ($obj->statut == 4) ? 5 : 4;
  342. $result = $this->update_contact($rowid, $newstatut);
  343. $this->db->free($resql);
  344. return $result;
  345. } else {
  346. $this->error = $this->db->error();
  347. dol_print_error($this->db);
  348. return -1;
  349. }
  350. }
  351. /**
  352. * Return array with list of possible values for type of contacts
  353. *
  354. * @param string $source 'internal', 'external' or 'all'
  355. * @param string $order Sort order by : 'code' or 'rowid'
  356. * @param string $option 0=Return array id->label, 1=Return array code->label
  357. * @return array Array list of type of contacts (id->label if option=0, code->label if option=1)
  358. */
  359. function liste_type_contact($source = 'internal', $order = 'code', $option = 0) {
  360. global $langs;
  361. $tab = array();
  362. $sql = "SELECT DISTINCT tc.rowid, tc.code, tc.libelle";
  363. $sql.= " FROM " . MAIN_DB_PREFIX . "c_type_contact as tc";
  364. $sql.= " WHERE tc.element='" . $this->element . "'";
  365. if (!empty($source))
  366. $sql.= " AND tc.source='" . $source . "'";
  367. $sql.= " ORDER by tc." . $order;
  368. //print "sql=".$sql;
  369. $resql = $this->db->query($sql);
  370. if ($resql) {
  371. $num = $this->db->num_rows($resql);
  372. $i = 0;
  373. while ($i < $num) {
  374. $obj = $this->db->fetch_object($resql);
  375. $transkey = "TypeContact_" . $this->element . "_" . $source . "_" . $obj->code;
  376. $libelle_type = ($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->libelle);
  377. if (empty($option))
  378. $tab[$obj->rowid] = $libelle_type;
  379. else
  380. $tab[$obj->code] = $libelle_type;
  381. $i++;
  382. }
  383. return $tab;
  384. }
  385. else {
  386. $this->error = $this->db->lasterror();
  387. //dol_print_error($this->db);
  388. return null;
  389. }
  390. }
  391. /**
  392. * Return id of contacts for a source and a contact code.
  393. * Example: contact client de facturation ('external', 'BILLING')
  394. * Example: contact client de livraison ('external', 'SHIPPING')
  395. * Example: contact interne suivi paiement ('internal', 'SALESREPFOLL')
  396. *
  397. * @param string $source 'external' or 'internal'
  398. * @param string $code 'BILLING', 'SHIPPING', 'SALESREPFOLL', ...
  399. * @param int $status limited to a certain status
  400. * @return array List of id for such contacts
  401. */
  402. function getIdContact($source, $code, $status = 0) {
  403. global $conf;
  404. $result = array();
  405. $i = 0;
  406. $sql = "SELECT ec.fk_socpeople";
  407. $sql.= " FROM " . MAIN_DB_PREFIX . "element_contact as ec,";
  408. if ($source == 'internal')
  409. $sql.= " " . MAIN_DB_PREFIX . "user as c,";
  410. if ($source == 'external')
  411. $sql.= " " . MAIN_DB_PREFIX . "socpeople as c,";
  412. $sql.= " " . MAIN_DB_PREFIX . "c_type_contact as tc";
  413. $sql.= " WHERE ec.element_id = " . $this->id;
  414. $sql.= " AND ec.fk_socpeople = c.rowid";
  415. if ($source == 'internal')
  416. $sql.= " AND c.entity IN (0," . $conf->entity . ")";
  417. if ($source == 'external')
  418. $sql.= " AND c.entity IN (" . getEntity('societe', 1) . ")";
  419. $sql.= " AND ec.fk_c_type_contact = tc.rowid";
  420. $sql.= " AND tc.element = '" . $this->element . "'";
  421. $sql.= " AND tc.source = '" . $source . "'";
  422. $sql.= " AND tc.code = '" . $code . "'";
  423. $sql.= " AND tc.active = 1";
  424. if ($status)
  425. $sql.= " AND ec.statut = " . $status;
  426. dol_syslog(get_class($this) . "::getIdContact sql=" . $sql);
  427. $resql = $this->db->query($sql);
  428. if ($resql) {
  429. while ($obj = $this->db->fetch_object($resql)) {
  430. $result[$i] = $obj->fk_socpeople;
  431. $i++;
  432. }
  433. } else {
  434. $this->error = $this->db->error();
  435. dol_syslog(get_class($this) . "::getIdContact " . $this->error, LOG_ERR);
  436. return null;
  437. }
  438. return $result;
  439. }
  440. /**
  441. * Charge le contact d'id $id dans this->contact
  442. *
  443. * @param int $contactid Id du contact
  444. * @return int <0 if KO, >0 if OK
  445. */
  446. function fetch_contact($contactid) {
  447. require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
  448. $contact = new Contact($this->db);
  449. $result = $contact->fetch($contactid);
  450. $this->contact = $contact;
  451. return $result;
  452. }
  453. /**
  454. * Load the third party of object from id $this->socid into this->thirdpary
  455. *
  456. * @return int <0 if KO, >0 if OK
  457. */
  458. function fetch_thirdparty() {
  459. global $conf;
  460. if (empty($this->socid))
  461. return 0;
  462. $thirdparty = new Societe($this->db);
  463. $result = $thirdparty->fetch($this->socid);
  464. //$this->client = $thirdparty; // deprecated
  465. $this->thirdparty = $thirdparty;
  466. // Use first price level if level not defined for third party
  467. if (!empty($conf->global->PRODUIT_MULTIPRICES) && empty($this->thirdparty->price_level)) {
  468. $this->client->price_level = 1; // deprecated
  469. $this->thirdparty->price_level = 1;
  470. }
  471. return $result;
  472. }
  473. /**
  474. * Load data for barcode
  475. *
  476. * @return int <0 if KO, >=0 if OK
  477. */
  478. function fetch_barcode() {
  479. global $conf;
  480. dol_syslog(get_class($this) . '::fetch_barcode this->element=' . $this->element . ' this->barcode_type=' . $this->barcode_type);
  481. $idtype = $this->barcode_type;
  482. if (!$idtype) {
  483. if ($this->element == 'product')
  484. $idtype = $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE;
  485. else if ($this->element == 'societe')
  486. $idtype = $conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY;
  487. else
  488. dol_print_error('', 'Call fetch_barcode with barcode_type not defined and cant be guessed');
  489. }
  490. if ($idtype > 0) {
  491. if (empty($this->barcode_type) || empty($this->barcode_type_code) || empty($this->barcode_type_label) || empty($this->barcode_type_coder)) { // If data not already loaded
  492. $sql = "SELECT rowid, code, libelle as label, coder";
  493. $sql.= " FROM " . MAIN_DB_PREFIX . "c_barcode_type";
  494. $sql.= " WHERE rowid = " . $idtype;
  495. dol_syslog(get_class($this) . '::fetch_barcode sql=' . $sql);
  496. $resql = $this->db->query($sql);
  497. if ($resql) {
  498. $obj = $this->db->fetch_object($resql);
  499. $this->barcode_type = $obj->rowid;
  500. $this->barcode_type_code = $obj->code;
  501. $this->barcode_type_label = $obj->label;
  502. $this->barcode_type_coder = $obj->coder;
  503. return 1;
  504. } else {
  505. dol_print_error($this->db);
  506. return -1;
  507. }
  508. }
  509. }
  510. else
  511. return 0;
  512. }
  513. /**
  514. * Charge le projet d'id $this->fk_project dans this->projet
  515. *
  516. * @return int <0 if KO, >=0 if OK
  517. */
  518. function fetch_projet() {
  519. if (empty($this->fk_project))
  520. return 0;
  521. $project = new Project($this->db);
  522. $result = $project->fetch($this->fk_project);
  523. $this->projet = $project;
  524. return $result;
  525. }
  526. /**
  527. * Charge le user d'id userid dans this->user
  528. *
  529. * @param int $userid Id du contact
  530. * @return int <0 if KO, >0 if OK
  531. */
  532. function fetch_user($userid) {
  533. $user = new User($this->db);
  534. $result = $user->fetch($userid);
  535. $this->user = $user;
  536. return $result;
  537. }
  538. /**
  539. * Read linked origin object
  540. *
  541. * @return void
  542. */
  543. function fetch_origin() {
  544. // TODO uniformise code
  545. if ($this->origin == 'shipping')
  546. $this->origin = 'expedition';
  547. if ($this->origin == 'delivery')
  548. $this->origin = 'livraison';
  549. $object = $this->origin;
  550. $classname = ucfirst($object);
  551. $this->$object = new $classname($this->db);
  552. $this->$object->fetch($this->origin_id);
  553. }
  554. /**
  555. * Load object from specific field
  556. *
  557. * @param string $table Table element or element line
  558. * @param string $field Field selected
  559. * @param string $key Import key
  560. * @return int <0 if KO, >0 if OK
  561. */
  562. function fetchObjectFrom($table, $field, $key) {
  563. global $conf;
  564. $result = false;
  565. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . $table;
  566. $sql.= " WHERE " . $field . " = '" . $key . "'";
  567. $sql.= " AND entity = " . $conf->entity;
  568. dol_syslog(get_class($this) . '::fetchObjectFrom sql=' . $sql);
  569. $resql = $this->db->query($sql);
  570. if ($resql) {
  571. $row = $this->db->fetch_row($resql);
  572. $result = $this->fetch($row[0]);
  573. }
  574. return $result;
  575. }
  576. /**
  577. * Load value from specific field
  578. *
  579. * @param string $table Table of element or element line
  580. * @param int $id Element id
  581. * @param string $field Field selected
  582. * @return int <0 if KO, >0 if OK
  583. */
  584. function getValueFrom($table, $id, $field) {
  585. $result = false;
  586. $sql = "SELECT " . $field . " FROM " . MAIN_DB_PREFIX . $table;
  587. $sql.= " WHERE rowid = " . $id;
  588. dol_syslog(get_class($this) . '::getValueFrom sql=' . $sql);
  589. $resql = $this->db->query($sql);
  590. if ($resql) {
  591. $row = $this->db->fetch_row($resql);
  592. $result = $row[0];
  593. }
  594. return $result;
  595. }
  596. /**
  597. * Update a specific field from an object
  598. *
  599. * @param string $field Field to update
  600. * @param mixte $value New value
  601. * @param string $table To force other table element or element line
  602. * @param int $id To force other object id
  603. * @param string $format Data format ('text' by default, 'date')
  604. * @return int <0 if KO, >0 if OK
  605. */
  606. function setValueFrom($field, $value, $table = '', $id = '', $format = 'text') {
  607. global $conf;
  608. if (empty($table))
  609. $table = $this->table_element;
  610. if (empty($id))
  611. $id = $this->id;
  612. $this->db->begin();
  613. $sql = "UPDATE " . MAIN_DB_PREFIX . $table . " SET ";
  614. if ($format == 'text')
  615. $sql.= $field . " = '" . $this->db->escape($value) . "'";
  616. else if ($format == 'date')
  617. $sql.= $field . " = '" . $this->db->idate($value) . "'";
  618. $sql.= " WHERE rowid = " . $id;
  619. dol_syslog(get_class($this) . "::setValueFrom sql=" . $sql, LOG_DEBUG);
  620. $resql = $this->db->query($sql);
  621. if ($resql) {
  622. $this->db->commit();
  623. return 1;
  624. } else {
  625. $this->error = $this->db->lasterror();
  626. $this->db->rollback();
  627. return -1;
  628. }
  629. }
  630. /**
  631. * Load properties id_previous and id_next
  632. *
  633. * @param string $filter Optional filter
  634. * @param int $fieldid Name of field to use for the select MAX and MIN
  635. * @return int <0 if KO, >0 if OK
  636. */
  637. function load_previous_next_ref($filter, $fieldid) {
  638. global $conf, $user;
  639. if (!$this->table_element) {
  640. dol_print_error('', get_class($this) . "::load_previous_next_ref was called on objet with property table_element not defined", LOG_ERR);
  641. return -1;
  642. }
  643. // this->ismultientitymanaged contains
  644. // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  645. $alias = 's';
  646. if ($this->element == 'societe')
  647. $alias = 'te';
  648. $sql = "SELECT MAX(te." . $fieldid . ")";
  649. $sql.= " FROM " . MAIN_DB_PREFIX . $this->table_element . " as te";
  650. if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 2 || ($this->element != 'societe' && empty($this->isnolinkedbythird) && empty($user->rights->societe->client->voir)))
  651. $sql.= ", " . MAIN_DB_PREFIX . "societe as s"; // If we need to link to societe to limit select to entity
  652. if (empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir)
  653. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON " . $alias . ".rowid = sc.fk_soc";
  654. $sql.= " WHERE te." . $fieldid . " < '" . $this->db->escape($this->ref) . "'";
  655. if (empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir)
  656. $sql.= " AND sc.fk_user = " . $user->id;
  657. if (!empty($filter))
  658. $sql.=" AND " . $filter;
  659. if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 2 || ($this->element != 'societe' && empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir))
  660. $sql.= ' AND te.fk_soc = s.rowid'; // If we need to link to societe to limit select to entity
  661. if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1)
  662. $sql.= ' AND te.entity IN (' . getEntity($this->element, 1) . ')';
  663. //print $sql."<br>";
  664. $result = $this->db->query($sql);
  665. if (!$result) {
  666. $this->error = $this->db->error();
  667. return -1;
  668. }
  669. $row = $this->db->fetch_row($result);
  670. $this->ref_previous = $row[0];
  671. $sql = "SELECT MIN(te." . $fieldid . ")";
  672. $sql.= " FROM " . MAIN_DB_PREFIX . $this->table_element . " as te";
  673. if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 2 || ($this->element != 'societe' && empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir))
  674. $sql.= ", " . MAIN_DB_PREFIX . "societe as s"; // If we need to link to societe to limit select to entity
  675. if (empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir)
  676. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON " . $alias . ".rowid = sc.fk_soc";
  677. $sql.= " WHERE te." . $fieldid . " > '" . $this->db->escape($this->ref) . "'";
  678. if (empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir)
  679. $sql.= " AND sc.fk_user = " . $user->id;
  680. if (!empty($filter))
  681. $sql.=" AND " . $filter;
  682. if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 2 || ($this->element != 'societe' && empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir))
  683. $sql.= ' AND te.fk_soc = s.rowid'; // If we need to link to societe to limit select to entity
  684. if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1)
  685. $sql.= ' AND te.entity IN (' . getEntity($this->element, 1) . ')';
  686. // Rem: Bug in some mysql version: SELECT MIN(rowid) FROM llx_socpeople WHERE rowid > 1 when one row in database with rowid=1, returns 1 instead of null
  687. //print $sql."<br>";
  688. $result = $this->db->query($sql);
  689. if (!$result) {
  690. $this->error = $this->db->error();
  691. return -2;
  692. }
  693. $row = $this->db->fetch_row($result);
  694. $this->ref_next = $row[0];
  695. return 1;
  696. }
  697. /**
  698. * Return list of id of contacts of project
  699. *
  700. * @param string $source Source of contact: external (llx_socpeople) or internal (llx_user) or thirdparty (llx_societe)
  701. * @return array Array of id of contacts (if source=external or internal)
  702. * Array of id of third parties with at least one contact on project (if source=thirdparty)
  703. */
  704. function getListContactId($source = 'external') {
  705. $contactAlreadySelected = array();
  706. $tab = $this->liste_contact(-1, $source);
  707. $num = count($tab);
  708. $i = 0;
  709. while ($i < $num) {
  710. if ($source == 'thirdparty')
  711. $contactAlreadySelected[$i] = $tab[$i]['socid'];
  712. else
  713. $contactAlreadySelected[$i] = $tab[$i]['id'];
  714. $i++;
  715. }
  716. return $contactAlreadySelected;
  717. }
  718. /**
  719. * Link element with a project
  720. *
  721. * @param int $projectid Project id to link element to
  722. * @return int <0 if KO, >0 if OK
  723. */
  724. function setProject($projectid) {
  725. if (!$this->table_element) {
  726. dol_syslog(get_class($this) . "::setProject was called on objet with property table_element not defined", LOG_ERR);
  727. return -1;
  728. }
  729. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element;
  730. if ($projectid)
  731. $sql.= ' SET fk_projet = ' . $projectid;
  732. else
  733. $sql.= ' SET fk_projet = NULL';
  734. $sql.= ' WHERE rowid = ' . $this->id;
  735. dol_syslog(get_class($this) . "::setProject sql=" . $sql);
  736. if ($this->db->query($sql)) {
  737. $this->fk_project = $projectid;
  738. return 1;
  739. } else {
  740. dol_print_error($this->db);
  741. return -1;
  742. }
  743. }
  744. /**
  745. * Change the payments methods
  746. *
  747. * @param int $id Id of new payment method
  748. * @return int >0 if OK, <0 if KO
  749. */
  750. function setPaymentMethods($id) {
  751. dol_syslog(get_class($this) . '::setPaymentMethods(' . $id . ')');
  752. if ($this->statut >= 0 || $this->element == 'societe') {
  753. // TODO uniformize field name
  754. $fieldname = 'fk_mode_reglement';
  755. if ($this->element == 'societe')
  756. $fieldname = 'mode_reglement';
  757. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element;
  758. $sql .= ' SET ' . $fieldname . ' = ' . $id;
  759. $sql .= ' WHERE rowid=' . $this->id;
  760. if ($this->db->query($sql)) {
  761. $this->mode_reglement_id = $id;
  762. $this->mode_reglement = $id; // for compatibility
  763. return 1;
  764. } else {
  765. dol_syslog(get_class($this) . '::setPaymentMethods Erreur ' . $sql . ' - ' . $this->db->error());
  766. $this->error = $this->db->error();
  767. return -1;
  768. }
  769. } else {
  770. dol_syslog(get_class($this) . '::setPaymentMethods, status of the object is incompatible');
  771. $this->error = 'Status of the object is incompatible ' . $this->statut;
  772. return -2;
  773. }
  774. }
  775. /**
  776. * Change the payments terms
  777. *
  778. * @param int $id Id of new payment terms
  779. * @return int >0 if OK, <0 if KO
  780. */
  781. function setPaymentTerms($id) {
  782. dol_syslog(get_class($this) . '::setPaymentTerms(' . $id . ')');
  783. if ($this->statut >= 0 || $this->element == 'societe') {
  784. // TODO uniformize field name
  785. $fieldname = 'fk_cond_reglement';
  786. if ($this->element == 'societe')
  787. $fieldname = 'cond_reglement';
  788. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element;
  789. $sql .= ' SET ' . $fieldname . ' = ' . $id;
  790. $sql .= ' WHERE rowid=' . $this->id;
  791. if ($this->db->query($sql)) {
  792. $this->cond_reglement_id = $id;
  793. $this->cond_reglement = $id; // for compatibility
  794. return 1;
  795. } else {
  796. dol_syslog(get_class($this) . '::setPaymentTerms Erreur ' . $sql . ' - ' . $this->db->error());
  797. $this->error = $this->db->error();
  798. return -1;
  799. }
  800. } else {
  801. dol_syslog(get_class($this) . '::setPaymentTerms, status of the object is incompatible');
  802. $this->error = 'Status of the object is incompatible ' . $this->statut;
  803. return -2;
  804. }
  805. }
  806. /**
  807. * Define delivery address
  808. *
  809. * @param int $id Address id
  810. * @return int <0 si ko, >0 si ok
  811. */
  812. function setDeliveryAddress($id) {
  813. $fieldname = 'fk_adresse_livraison';
  814. if ($this->element == 'delivery' || $this->element == 'shipping')
  815. $fieldname = 'fk_address';
  816. $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET " . $fieldname . " = " . $id;
  817. $sql.= " WHERE rowid = " . $this->id . " AND fk_statut = 0";
  818. if ($this->db->query($sql)) {
  819. $this->fk_delivery_address = $id;
  820. return 1;
  821. } else {
  822. $this->error = $this->db->error();
  823. dol_syslog(get_class($this) . '::setDeliveryAddress Erreur ' . $sql . ' - ' . $this->error);
  824. return -1;
  825. }
  826. }
  827. /**
  828. * Set last model used by doc generator
  829. *
  830. * @param User $user User object that make change
  831. * @param string $modelpdf Modele name
  832. * @return int <0 if KO, >0 if OK
  833. */
  834. function setDocModel($user, $modelpdf) {
  835. $newmodelpdf = dol_trunc($modelpdf, 255);
  836. if ($this->modelpdf != $newmodelpdf) {
  837. $this->modelpdf = $newmodelpdf;
  838. $this->record();
  839. }
  840. return 1;
  841. }
  842. /**
  843. * Save a new position (field rang) for details lines.
  844. * You can choose to ser position for lines with already a position or lines wihtout any position defined.
  845. * Call this function only for table that contains a field fk_parent_line.
  846. *
  847. * @param boolean $renum true to renum all already ordered lines, false to renum only not already ordered lines.
  848. * @param string $rowidorder ASC or DESC
  849. * @return void
  850. */
  851. function line_order($renum = false, $rowidorder = 'ASC') {
  852. if (!$this->table_element_line) {
  853. dol_syslog(get_class($this) . "::line_order was called on objet with property table_element_line not defined", LOG_ERR);
  854. return -1;
  855. }
  856. if (!$this->fk_element) {
  857. dol_syslog(get_class($this) . "::line_order was called on objet with property fk_element not defined", LOG_ERR);
  858. return -1;
  859. }
  860. // Count number of lines to reorder (according to choice $renum)
  861. $nl = 0;
  862. $sql = 'SELECT count(rowid) FROM ' . MAIN_DB_PREFIX . $this->table_element_line;
  863. $sql.= ' WHERE ' . $this->fk_element . '=' . $this->id;
  864. if (!$renum)
  865. $sql.= ' AND rang = 0';
  866. if ($renum)
  867. $sql.= ' AND rang <> 0';
  868. dol_syslog(get_class($this) . "::line_order sql=" . $sql, LOG_DEBUG);
  869. $resql = $this->db->query($sql);
  870. if ($resql) {
  871. $row = $this->db->fetch_row($resql);
  872. $nl = $row[0];
  873. }
  874. else
  875. dol_print_error($this->db);
  876. if ($nl > 0) {
  877. // The goal of this part is to reorder all lines, with all children lines sharing the same
  878. // counter that parents.
  879. $rows = array();
  880. // We frist search all lines that are parent lines (for multilevel details lines)
  881. $sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . $this->table_element_line;
  882. $sql.= ' WHERE ' . $this->fk_element . ' = ' . $this->id;
  883. $sql.= ' AND fk_parent_line IS NULL';
  884. $sql.= ' ORDER BY rang ASC, rowid ' . $rowidorder;
  885. dol_syslog(get_class($this) . "::line_order search all parent lines sql=" . $sql, LOG_DEBUG);
  886. $resql = $this->db->query($sql);
  887. if ($resql) {
  888. $i = 0;
  889. $num = $this->db->num_rows($resql);
  890. while ($i < $num) {
  891. $row = $this->db->fetch_row($resql);
  892. $rows[] = $row[0]; // Add parent line into array rows
  893. $childrens = $this->getChildrensOfLine($row[0]);
  894. if (!empty($childrens)) {
  895. foreach ($childrens as $child) {
  896. array_push($rows, $child);
  897. }
  898. }
  899. $i++;
  900. }
  901. // Now we set a new number for each lines (parent and children with children included into parent tree)
  902. if (!empty($rows)) {
  903. foreach ($rows as $key => $row) {
  904. $this->updateRangOfLine($row, ($key + 1));
  905. }
  906. }
  907. } else {
  908. dol_print_error($this->db);
  909. }
  910. }
  911. }
  912. /**
  913. * Get childrens of line
  914. *
  915. * @param int $id Id of parent line
  916. * @return array Array with list of child lines id
  917. */
  918. function getChildrensOfLine($id) {
  919. $rows = array();
  920. $sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . $this->table_element_line;
  921. $sql.= ' WHERE ' . $this->fk_element . ' = ' . $this->id;
  922. $sql.= ' AND fk_parent_line = ' . $id;
  923. $sql.= ' ORDER BY rang ASC';
  924. dol_syslog(get_class($this) . "::getChildrenOfLines search children lines for line " . $id . " sql=" . $sql, LOG_DEBUG);
  925. $resql = $this->db->query($sql);
  926. if ($resql) {
  927. $i = 0;
  928. $num = $this->db->num_rows($resql);
  929. while ($i < $num) {
  930. $row = $this->db->fetch_row($resql);
  931. $rows[$i] = $row[0];
  932. $i++;
  933. }
  934. }
  935. return $rows;
  936. }
  937. /**
  938. * Update a line to have a lower rank
  939. *
  940. * @param int $rowid Id of line
  941. * @return void
  942. */
  943. function line_up($rowid) {
  944. $this->line_order();
  945. // Get rang of line
  946. $rang = $this->getRangOfLine($rowid);
  947. // Update position of line
  948. $this->updateLineUp($rowid, $rang);
  949. }
  950. /**
  951. * Update a line to have a higher rank
  952. *
  953. * @param int $rowid Id of line
  954. * @return void
  955. */
  956. function line_down($rowid) {
  957. $this->line_order();
  958. // Get rang of line
  959. $rang = $this->getRangOfLine($rowid);
  960. // Get max value for rang
  961. $max = $this->line_max();
  962. // Update position of line
  963. $this->updateLineDown($rowid, $rang, $max);
  964. }
  965. /**
  966. * Update position of line (rang)
  967. *
  968. * @param int $rowid Id of line
  969. * @param int $rang Position
  970. * @return void
  971. */
  972. function updateRangOfLine($rowid, $rang) {
  973. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element_line . ' SET rang = ' . $rang;
  974. $sql.= ' WHERE rowid = ' . $rowid;
  975. dol_syslog(get_class($this) . "::updateRangOfLine sql=" . $sql, LOG_DEBUG);
  976. if (!$this->db->query($sql)) {
  977. dol_print_error($this->db);
  978. }
  979. }
  980. /**
  981. * Update position of line with ajax (rang)
  982. *
  983. * @param array $rows Array of rows
  984. * @return void
  985. */
  986. function line_ajaxorder($rows) {
  987. $num = count($rows);
  988. for ($i = 0; $i < $num; $i++) {
  989. $this->updateRangOfLine($rows[$i], ($i + 1));
  990. }
  991. }
  992. /**
  993. * Update position of line up (rang)
  994. *
  995. * @param int $rowid Id of line
  996. * @param int $rang Position
  997. * @return void
  998. */
  999. function updateLineUp($rowid, $rang) {
  1000. if ($rang > 1) {
  1001. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element_line . ' SET rang = ' . $rang;
  1002. $sql.= ' WHERE ' . $this->fk_element . ' = ' . $this->id;
  1003. $sql.= ' AND rang = ' . ($rang - 1);
  1004. if ($this->db->query($sql)) {
  1005. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element_line . ' SET rang = ' . ($rang - 1);
  1006. $sql.= ' WHERE rowid = ' . $rowid;
  1007. if (!$this->db->query($sql)) {
  1008. dol_print_error($this->db);
  1009. }
  1010. } else {
  1011. dol_print_error($this->db);
  1012. }
  1013. }
  1014. }
  1015. /**
  1016. * Update position of line down (rang)
  1017. *
  1018. * @param int $rowid Id of line
  1019. * @param int $rang Position
  1020. * @param int $max Max
  1021. * @return void
  1022. */
  1023. function updateLineDown($rowid, $rang, $max) {
  1024. if ($rang < $max) {
  1025. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element_line . ' SET rang = ' . $rang;
  1026. $sql.= ' WHERE ' . $this->fk_element . ' = ' . $this->id;
  1027. $sql.= ' AND rang = ' . ($rang + 1);
  1028. if ($this->db->query($sql)) {
  1029. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element_line . ' SET rang = ' . ($rang + 1);
  1030. $sql.= ' WHERE rowid = ' . $rowid;
  1031. if (!$this->db->query($sql)) {
  1032. dol_print_error($this->db);
  1033. }
  1034. } else {
  1035. dol_print_error($this->db);
  1036. }
  1037. }
  1038. }
  1039. /**
  1040. * Get position of line (rang)
  1041. *
  1042. * @param int $rowid Id of line
  1043. * @return int Value of rang in table of lines
  1044. */
  1045. function getRangOfLine($rowid) {
  1046. $sql = 'SELECT rang FROM ' . MAIN_DB_PREFIX . $this->table_element_line;
  1047. $sql.= ' WHERE rowid =' . $rowid;
  1048. dol_syslog(get_class($this) . "::getRangOfLine sql=" . $sql, LOG_DEBUG);
  1049. $resql = $this->db->query($sql);
  1050. if ($resql) {
  1051. $row = $this->db->fetch_row($resql);
  1052. return $row[0];
  1053. }
  1054. }
  1055. /**
  1056. * Get rowid of the line relative to its position
  1057. *
  1058. * @param int $rang Rang value
  1059. * @return int Rowid of the line
  1060. */
  1061. function getIdOfLine($rang) {
  1062. $sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . $this->table_element_line;
  1063. $sql.= ' WHERE ' . $this->fk_element . ' = ' . $this->id;
  1064. $sql.= ' AND rang = ' . $rang;
  1065. $resql = $this->db->query($sql);
  1066. if ($resql) {
  1067. $row = $this->db->fetch_row($resql);
  1068. return $row[0];
  1069. }
  1070. }
  1071. /**
  1072. * Get max value used for position of line (rang)
  1073. *
  1074. * @param int $fk_parent_line Parent line id
  1075. * @return int Max value of rang in table of lines
  1076. */
  1077. function line_max($fk_parent_line = 0) {
  1078. // Search the last rang with fk_parent_line
  1079. if ($fk_parent_line) {
  1080. $sql = 'SELECT max(rang) FROM ' . MAIN_DB_PREFIX . $this->table_element_line;
  1081. $sql.= ' WHERE ' . $this->fk_element . ' = ' . $this->id;
  1082. $sql.= ' AND fk_parent_line = ' . $fk_parent_line;
  1083. dol_syslog(get_class($this) . "::line_max sql=" . $sql, LOG_DEBUG);
  1084. $resql = $this->db->query($sql);
  1085. if ($resql) {
  1086. $row = $this->db->fetch_row($resql);
  1087. if (!empty($row[0])) {
  1088. return $row[0];
  1089. } else {
  1090. return $this->getRangOfLine($fk_parent_line);
  1091. }
  1092. }
  1093. }
  1094. // If not, search the last rang of element
  1095. else {
  1096. $sql = 'SELECT max(rang) FROM ' . MAIN_DB_PREFIX . $this->table_element_line;
  1097. $sql.= ' WHERE ' . $this->fk_element . ' = ' . $this->id;
  1098. dol_syslog(get_class($this) . "::line_max sql=" . $sql, LOG_DEBUG);
  1099. $resql = $this->db->query($sql);
  1100. if ($resql) {
  1101. $row = $this->db->fetch_row($resql);
  1102. return $row[0];
  1103. }
  1104. }
  1105. }
  1106. /**
  1107. * Update external ref of element
  1108. *
  1109. * @param string $ref_ext Update field ref_ext
  1110. * @return int <0 if KO, >0 if OK
  1111. */
  1112. function update_ref_ext($ref_ext) {
  1113. if (!$this->table_element) {
  1114. dol_syslog(get_class($this) . "::update_ref_ext was called on objet with property table_element not defined", LOG_ERR);
  1115. return -1;
  1116. }
  1117. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element;
  1118. $sql.= " SET ref_ext = '" . $this->db->escape($ref_ext) . "'";
  1119. $sql.= " WHERE " . (isset($this->table_rowid) ? $this->table_rowid : 'rowid') . " = " . $this->id;
  1120. dol_syslog(get_class($this) . "::update_ref_ext sql=" . $sql, LOG_DEBUG);
  1121. if ($this->db->query($sql)) {
  1122. $this->ref_ext = $ref_ext;
  1123. return 1;
  1124. } else {
  1125. $this->error = $this->db->error();
  1126. dol_syslog(get_class($this) . "::update_ref_ext error=" . $this->error, LOG_ERR);
  1127. return -1;
  1128. }
  1129. }
  1130. /**
  1131. * Update private note of element
  1132. *
  1133. * @param string $note New value for note
  1134. * @return int <0 if KO, >0 if OK
  1135. */
  1136. function update_note($note) {
  1137. if (!$this->table_element) {
  1138. dol_syslog(get_class($this) . "::update_note was called on objet with property table_element not defined", LOG_ERR);
  1139. return -1;
  1140. }
  1141. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element;
  1142. // TODO uniformize fields to note_private
  1143. if ($this->table_element == 'fichinter' || $this->table_element == 'projet' || $this->table_element == 'projet_task') {
  1144. $sql.= " SET note_private = '" . $this->db->escape($note) . "'";
  1145. } else {
  1146. $sql.= " SET note = '" . $this->db->escape($note) . "'";
  1147. }
  1148. $sql.= " WHERE rowid =" . $this->id;
  1149. dol_syslog(get_class($this) . "::update_note sql=" . $sql, LOG_DEBUG);
  1150. if ($this->db->query($sql)) {
  1151. $this->note = $note; // deprecated
  1152. $this->note_private = $note;
  1153. return 1;
  1154. } else {
  1155. $this->error = $this->db->error();
  1156. dol_syslog(get_class($this) . "::update_note error=" . $this->error, LOG_ERR);
  1157. return -1;
  1158. }
  1159. }
  1160. /**
  1161. * Update public note of element
  1162. *
  1163. * @param string $note_public New value for note
  1164. * @return int <0 if KO, >0 if OK
  1165. */
  1166. function update_note_public($note_public) {
  1167. if (!$this->table_element) {
  1168. dol_syslog(get_class($this) . "::update_note_public was called on objet with property table_element not defined", LOG_ERR);
  1169. return -1;
  1170. }
  1171. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element;
  1172. $sql.= " SET note_public = '" . $this->db->escape($note_public) . "'";
  1173. $sql.= " WHERE rowid =" . $this->id;
  1174. dol_syslog(get_class($this) . "::update_note_public sql=" . $sql);
  1175. if ($this->db->query($sql)) {
  1176. $this->note_public = $note_public;
  1177. return 1;
  1178. } else {
  1179. $this->error = $this->db->error();
  1180. return -1;
  1181. }
  1182. }
  1183. /**
  1184. * Update total_ht, total_ttc and total_vat for an object (sum of lines)
  1185. *
  1186. * @param int $exclspec Exclude special product (product_type=9)
  1187. * @param int $roundingadjust -1=Use default method (MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND or 0), 0=Use total of rounding, 1=Use rounding of total
  1188. * @param int $nodatabaseupdate 1=Do not update database. Update only properties of object.
  1189. * @return int <0 if KO, >0 if…

Large files files are truncated, but you can click here to view the full file