PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/propal/class/propal.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 2754 lines | 1295 code | 243 blank | 1216 comment | 258 complexity | 485e0b8725b731685c0de214e65b80f9 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  4. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2005 Marc Barilley <marc@ocebo.com>
  6. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  7. * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
  8. * Copyright (C) 2008 Raphael Bertrand <raphael.bertrand@resultic.fr>
  9. * Copyright (C) 2010-2012 Juanjo Menent <jmenent@2byte.es>
  10. * Copyright (C) 2010-2011 Philippe Grand <philippe.grand@atoo-net.com>
  11. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  12. * Copyright (C) 2012 David Moothen <dmoothen@websitti.fr>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. */
  27. /**
  28. * \file htdocs/comm/propal/class/propal.class.php
  29. * \brief Fichier de la classe des propales
  30. */
  31. require_once DOL_DOCUMENT_ROOT . '/core/class/abstractinvoice.class.php';
  32. require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  33. require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
  34. require_once DOL_DOCUMENT_ROOT . '/margin/lib/margins.lib.php';
  35. /**
  36. * \class Propal
  37. * \brief Classe permettant la gestion des propales
  38. */
  39. class Propal extends AbstractInvoice {
  40. public $element = 'propal';
  41. public $table_element = 'propal';
  42. // public $table_element_line = 'propaldet';
  43. // public $fk_element = 'fk_propal';
  44. protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  45. var $id;
  46. var $client; // Objet societe client (a charger par fetch_client)
  47. var $contactid;
  48. var $fk_project;
  49. var $author;
  50. var $ref;
  51. var $ref_client;
  52. //var $statut; // 0 (draft), 1 (validated), 2 (signed), 3 (not signed), 4 (billed)
  53. public $Status;
  54. var $datec; // Date of creation
  55. var $datev; // Date of validation
  56. var $date; // Date of proposal
  57. var $datep; // Same than date
  58. var $date_livraison;
  59. public $duree_validite;
  60. var $fin_validite;
  61. var $user_author_id;
  62. var $user_valid_id;
  63. var $user_close_id;
  64. var $total_ht; // Total net of tax
  65. var $total_tva; // Total VAT
  66. var $total_localtax1; // Total Local Taxes 1
  67. var $total_localtax2; // Total Local Taxes 2
  68. var $total_ttc; // Total with tax
  69. var $price; // deprecated (for compatibility)
  70. var $tva; // deprecated (for compatibility)
  71. var $total; // deprecated (for compatibility)
  72. var $cond_reglement_code;
  73. var $mode_reglement_code;
  74. var $remise;
  75. var $remise_percent;
  76. var $remise_absolue;
  77. var $note; // deprecated (for compatibility)
  78. var $note_private;
  79. var $note_public;
  80. var $fk_delivery_address; // deprecated (for compatibility)
  81. var $fk_address;
  82. var $address_type;
  83. var $adresse;
  84. var $availability_code;
  85. var $demand_reason_code;
  86. var $products = array();
  87. var $extraparams = array();
  88. var $lines = array();
  89. var $line;
  90. var $origin;
  91. var $origin_id;
  92. var $labelstatut = array();
  93. var $labelstatut_short = array();
  94. // Pour board
  95. var $nbtodo;
  96. var $nbtodolate;
  97. var $specimen;
  98. /**
  99. * Constructor
  100. *
  101. * @param DoliDB $db Database handler
  102. */
  103. function __construct($db = '') {
  104. parent::__construct($db);
  105. $this->no_save[] = 'thirdparty';
  106. $this->no_save[] = 'line';
  107. $this->fk_extrafields = new ExtraFields($db);
  108. $this->fk_extrafields->fetch(get_class($this));
  109. $this->remise = 0;
  110. $this->remise_percent = 0;
  111. $this->products = array();
  112. }
  113. /**
  114. * Add line into array products
  115. * $this->client doit etre charge
  116. * public $modelpdf = 'marbre';
  117. * @param int $idproduct Product Id to add
  118. * @param int $qty Quantity
  119. * @param int $remise_percent Discount effected on Product
  120. * @return int <0 if KO, >0 if OK
  121. *
  122. * TODO Remplacer les appels a cette fonction par generation objet Ligne
  123. * insere dans tableau $this->products
  124. */
  125. function add_product($idproduct, $qty, $remise_percent = 0) {
  126. global $conf, $mysoc;
  127. if (!$qty)
  128. $qty = 1;
  129. dol_syslog(get_class($this) . "::add_product $idproduct, $qty, $remise_percent");
  130. if ($idproduct > 0) {
  131. $prod = new Product($this->db);
  132. $prod->fetch($idproduct);
  133. $productdesc = $prod->description;
  134. $tva_tx = get_default_tva($mysoc, $this->client, $prod->id);
  135. // local taxes
  136. $localtax1_tx = get_default_localtax($mysoc, $this->client, 1, $prod->tva_tx);
  137. $localtax2_tx = get_default_localtax($mysoc, $this->client, 2, $prod->tva_tx);
  138. // multiprix
  139. if ($conf->global->PRODUIT_MULTIPRICES && $this->client->price_level) {
  140. $price = $prod->multiprices[$this->client->price_level];
  141. } else {
  142. $price = $prod->price;
  143. }
  144. $line = new PropalLine($this->db);
  145. $line->fk_product = $idproduct;
  146. $line->desc = $productdesc;
  147. $line->qty = $qty;
  148. $line->subprice = $price;
  149. $line->remise_percent = $remise_percent;
  150. $line->tva_tx = $tva_tx;
  151. $this->products[] = $line;
  152. }
  153. }
  154. /**
  155. * Adding line of fixed discount in the proposal in DB
  156. *
  157. * @param int $idremise Id of fixed discount
  158. * @return int >0 if OK, <0 if KO
  159. */
  160. function insert_discount($idremise) {
  161. global $langs;
  162. include_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php';
  163. include_once DOL_DOCUMENT_ROOT . '/core/class/discount.class.php';
  164. $this->db->begin();
  165. $remise = new DiscountAbsolute($this->db);
  166. $result = $remise->fetch($idremise);
  167. if ($result > 0) {
  168. if ($remise->fk_facture) { // Protection against multiple submission
  169. $this->error = $langs->trans("ErrorDiscountAlreadyUsed");
  170. $this->db->rollback();
  171. return -5;
  172. }
  173. $propalligne = new PropalLine($this->db);
  174. $propalligne->fk_propal = $this->id;
  175. $propalligne->fk_remise_except = $remise->id;
  176. $propalligne->desc = $remise->description; // Description ligne
  177. $propalligne->tva_tx = $remise->tva_tx;
  178. $propalligne->subprice = -$remise->amount_ht;
  179. $propalligne->fk_product = 0; // Id produit predefini
  180. $propalligne->qty = 1;
  181. $propalligne->remise = 0;
  182. $propalligne->remise_percent = 0;
  183. $propalligne->rang = -1;
  184. $propalligne->info_bits = 2;
  185. // TODO deprecated
  186. $propalligne->price = -$remise->amount_ht;
  187. $propalligne->total_ht = -$remise->amount_ht;
  188. $propalligne->total_tva = -$remise->amount_tva;
  189. $propalligne->total_ttc = -$remise->amount_ttc;
  190. $result = $propalligne->insert();
  191. if ($result > 0) {
  192. $result = $this->update_price(1);
  193. if ($result > 0) {
  194. $this->db->commit();
  195. return 1;
  196. } else {
  197. $this->db->rollback();
  198. return -1;
  199. }
  200. } else {
  201. $this->error = $propalligne->error;
  202. $this->db->rollback();
  203. return -2;
  204. }
  205. } else {
  206. $this->db->rollback();
  207. return -2;
  208. }
  209. }
  210. /**
  211. * Create commercial proposal into database
  212. * this->ref can be set or empty. If empty, we will use "(PROVid)"
  213. *
  214. * @param User $user User that create
  215. * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
  216. * @return int <0 if KO, >=0 if OK
  217. */
  218. function create($user = '', $notrigger = 0) {
  219. global $langs, $conf, $mysoc, $user;
  220. $error = 0;
  221. $now = dol_now();
  222. // Clean parameters
  223. if (empty($this->date))
  224. $this->date = $this->datep;
  225. $this->fin_validite = $this->date + ($this->duree_validite * 24 * 3600);
  226. dol_syslog(get_class($this) . "::create");
  227. // Check parameters
  228. $soc = new Societe($this->db);
  229. $result = $soc->fetch($this->socid);
  230. if ($result < 0) {
  231. $this->error = "Failed to fetch company";
  232. dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR);
  233. return -3;
  234. }
  235. $this->client = new stdClass();
  236. $this->client->id = $soc->id;
  237. $this->client->name = $soc->name;
  238. // Author
  239. $this->author = new stdClass();
  240. $this->author->id = $user->id;
  241. $this->author->name = $user->login;
  242. if (empty($this->date)) {
  243. $this->error = "Date of proposal is required";
  244. dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR);
  245. return -4;
  246. }
  247. $this->ref = $this->getNextNumRef($soc);
  248. // $this->db->begin();
  249. //
  250. // $this->fetch_thirdparty();
  251. //
  252. // // Insert into database
  253. // $sql = "INSERT INTO " . MAIN_DB_PREFIX . "propal (";
  254. // $sql.= "fk_soc";
  255. // $sql.= ", price";
  256. // $sql.= ", remise";
  257. // $sql.= ", remise_percent";
  258. // $sql.= ", remise_absolue";
  259. // $sql.= ", tva";
  260. // $sql.= ", total";
  261. // $sql.= ", datep";
  262. // $sql.= ", datec";
  263. // $sql.= ", ref";
  264. // $sql.= ", fk_user_author";
  265. // $sql.= ", note";
  266. // $sql.= ", note_public";
  267. // $sql.= ", model_pdf";
  268. // $sql.= ", fin_validite";
  269. // $sql.= ", fk_cond_reglement";
  270. // $sql.= ", fk_mode_reglement";
  271. // $sql.= ", ref_client";
  272. // $sql.= ", date_livraison";
  273. // $sql.= ", fk_availability";
  274. // $sql.= ", fk_input_reason";
  275. // $sql.= ", fk_projet";
  276. // $sql.= ", entity";
  277. // $sql.= ") ";
  278. // $sql.= " VALUES (";
  279. // $sql.= $this->socid;
  280. // $sql.= ", 0";
  281. // $sql.= ", " . $this->remise;
  282. // $sql.= ", " . ($this->remise_percent ? $this->remise_percent : 'null');
  283. // $sql.= ", " . ($this->remise_absolue ? $this->remise_absolue : 'null');
  284. // $sql.= ", 0";
  285. // $sql.= ", 0";
  286. // $sql.= ", '" . $this->db->idate($this->date) . "'";
  287. // $sql.= ", '" . $this->db->idate($now) . "'";
  288. // $sql.= ", '(PROV)'";
  289. // $sql.= ", " . ($user->id > 0 ? "'" . $user->id . "'" : "null");
  290. // $sql.= ", '" . $this->db->escape($this->note) . "'";
  291. // $sql.= ", '" . $this->db->escape($this->note_public) . "'";
  292. // $sql.= ", '" . $this->modelpdf . "'";
  293. // $sql.= ", " . ($this->fin_validite != '' ? "'" . $this->db->idate($this->fin_validite) . "'" : "null");
  294. // $sql.= ", " . $this->cond_reglement_id;
  295. // $sql.= ", " . $this->mode_reglement_id;
  296. // $sql.= ", '" . $this->db->escape($this->ref_client) . "'";
  297. // $sql.= ", " . ($this->date_livraison != '' ? "'" . $this->db->idate($this->date_livraison) . "'" : "null");
  298. // $sql.= ", " . $this->availability_id;
  299. // $sql.= ", " . $this->demand_reason_id;
  300. // $sql.= ", " . ($this->fk_project ? $this->fk_project : "null");
  301. // $sql.= ", " . $conf->entity;
  302. // $sql.= ")";
  303. //
  304. // dol_syslog(get_class($this) . "::create sql=" . $sql, LOG_DEBUG);
  305. // $resql = $this->db->query($sql);
  306. // if ($resql) {
  307. // $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "propal");
  308. $this->record();
  309. if (!$notrigger) {
  310. // Appel des triggers
  311. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  312. $interface = new Interfaces($this->db);
  313. $result = $interface->run_triggers('PROPAL_CREATE', $this, $user, $langs, $conf);
  314. if ($result < 0) {
  315. $error++;
  316. $this->errors = $interface->errors;
  317. }
  318. // Fin appel triggers
  319. }
  320. return $this->id;
  321. // if ($this->id) {
  322. // if (empty($this->ref))
  323. // $this->ref = '(PROV' . $this->id . ')';
  324. // $sql = 'UPDATE ' . MAIN_DB_PREFIX . "propal SET ref='" . $this->ref . "' WHERE rowid=" . $this->id;
  325. //
  326. // dol_syslog(get_class($this) . "::create sql=" . $sql);
  327. // $resql = $this->db->query($sql);
  328. // if (!$resql)
  329. // $error++;
  330. //
  331. // /*
  332. // * Insertion du detail des produits dans la base
  333. // */
  334. // if (!$error) {
  335. // $fk_parent_line = 0;
  336. // $num = count($this->lines);
  337. //
  338. // for ($i = 0; $i < $num; $i++) {
  339. // // Reset fk_parent_line for no child products and special product
  340. // if (($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line)) || $this->lines[$i]->product_type == 9) {
  341. // $fk_parent_line = 0;
  342. // }
  343. //
  344. // $result = $this->addline(
  345. // $this->id, $this->lines[$i]->desc, $this->lines[$i]->subprice, $this->lines[$i]->qty, $this->lines[$i]->tva_tx, $this->lines[$i]->localtax1_tx, $this->lines[$i]->localtax2_tx, $this->lines[$i]->fk_product, $this->lines[$i]->remise_percent, 'HT', 0, 0, $this->lines[$i]->product_type, $this->lines[$i]->rang, $this->lines[$i]->special_code, $fk_parent_line, $this->lines[$i]->fk_fournprice, $this->lines[$i]->pa_ht, $this->lines[$i]->label
  346. // );
  347. //
  348. // if ($result < 0) {
  349. // $error++;
  350. // $this->error = $this->db->error;
  351. // dol_print_error($this->db);
  352. // break;
  353. // }
  354. // // Defined the new fk_parent_line
  355. // if ($result > 0 && $this->lines[$i]->product_type == 9) {
  356. // $fk_parent_line = $result;
  357. // }
  358. // }
  359. // }
  360. //
  361. // // Add linked object
  362. // if (!$error && $this->origin && $this->origin_id) {
  363. // $ret = $this->add_object_linked();
  364. // if (!$ret)
  365. // dol_print_error($this->db);
  366. // }
  367. //
  368. // // Set delivery address
  369. // if (!$error && $this->fk_delivery_address) {
  370. // $sql = "UPDATE " . MAIN_DB_PREFIX . "propal";
  371. // $sql.= " SET fk_adresse_livraison = " . $this->fk_delivery_address;
  372. // $sql.= " WHERE ref = '" . $this->ref . "'";
  373. // $sql.= " AND entity = " . $conf->entity;
  374. //
  375. // $result = $this->db->query($sql);
  376. // }
  377. //
  378. // if (!$error) {
  379. // // Mise a jour infos denormalisees
  380. // $resql = $this->update_price(1);
  381. // if ($resql) {
  382. // if (!$notrigger) {
  383. // // Appel des triggers
  384. // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  385. // $interface = new Interfaces($this->db);
  386. // $result = $interface->run_triggers('PROPAL_CREATE', $this, $user, $langs, $conf);
  387. // if ($result < 0) {
  388. // $error++;
  389. // $this->errors = $interface->errors;
  390. // }
  391. // // Fin appel triggers
  392. // }
  393. // } else {
  394. // $error++;
  395. // }
  396. // }
  397. // } else {
  398. // $error++;
  399. // }
  400. //
  401. // if (!$error) {
  402. // $this->db->commit();
  403. // dol_syslog(get_class($this) . "::create done id=" . $this->id);
  404. // return $this->id;
  405. // } else {
  406. // $this->error = $this->db->error();
  407. // dol_syslog(get_class($this) . "::create -2 " . $this->error, LOG_ERR);
  408. // $this->db->rollback();
  409. // return -2;
  410. // }
  411. // } else {
  412. // $this->error = $this->db->error();
  413. // dol_syslog(get_class($this) . "::create -1 " . $this->error, LOG_ERR);
  414. // $this->db->rollback();
  415. // return -1;
  416. // }
  417. }
  418. /**
  419. * Insert into DB a proposal object completely defined by its data members (ex, results from copy).
  420. *
  421. * @param User $user User that create
  422. * @return int Id of the new object if ok, <0 if ko
  423. * @see create
  424. */
  425. function create_from($user) {
  426. $this->products = $this->lines;
  427. return $this->create();
  428. }
  429. /**
  430. * Load an object from its id and create a new one in database
  431. *
  432. * @param int $socid Id of thirdparty
  433. * @return int New id of clone
  434. */
  435. function createFromClone($socid = 0) {
  436. global $user, $langs, $conf, $hookmanager;
  437. $error = 0;
  438. $now = dol_now();
  439. // Load source object
  440. $objFrom = dol_clone($this);
  441. $objsoc = new Societe($this->db);
  442. // Change socid if needed
  443. if (!empty($socid) && $socid != $this->socid) {
  444. if ($objsoc->fetch($socid) > 0) {
  445. $this->socid = $objsoc->id;
  446. $this->cond_reglement_code = (!empty($objsoc->cond_reglement_code) ? $objsoc->cond_reglement_code : 'AV_NOW');
  447. $this->mode_reglement_code = (!empty($objsoc->mode_reglement_code) ? $objsoc->mode_reglement_code : 'TIP');
  448. $this->fk_project = '';
  449. $this->fk_delivery_address = '';
  450. }
  451. // TODO Change product price if multi-prices
  452. } else {
  453. $objsoc->fetch($this->socid);
  454. }
  455. unset($this->id);
  456. unset($this->_id);
  457. unset($this->_rev);
  458. $this->Status = 'DRAFT';
  459. if (empty($conf->global->PROPALE_ADDON) || !is_readable(DOL_DOCUMENT_ROOT . "/propal/core/modules/propale/" . $conf->global->PROPALE_ADDON . ".php")) {
  460. $this->error = 'ErrorSetupNotComplete';
  461. return -1;
  462. }
  463. // Clear fields
  464. $this->user_author = $user->id;
  465. $this->user_valid = '';
  466. $this->date = $now;
  467. $this->datep = $now; // deprecated
  468. $this->fin_validite = $this->date + ($this->duree_validite * 24 * 3600);
  469. $this->ref_client = '';
  470. // Set ref
  471. require_once DOL_DOCUMENT_ROOT . "/propal/core/modules/propale/" . $conf->global->PROPALE_ADDON . '.php';
  472. $obj = $conf->global->PROPALE_ADDON;
  473. $modPropale = new $obj;
  474. $this->ref = $modPropale->getNextValue($objsoc, $this);
  475. // Create clone
  476. $result = $this->create($user);
  477. if (!empty($result))
  478. $error++;
  479. if (!$error) {
  480. // Hook of thirdparty module
  481. if (is_object($hookmanager)) {
  482. $parameters = array('objFrom' => $objFrom);
  483. $action = '';
  484. $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  485. if ($reshook < 0)
  486. $error++;
  487. }
  488. // Appel des triggers
  489. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  490. $interface = new Interfaces($this->db);
  491. $result = $interface->run_triggers('PROPAL_CLONE', $this, $user, $langs, $conf);
  492. if ($result < 0) {
  493. $error++;
  494. $this->errors = $interface->errors;
  495. }
  496. // Fin appel triggers
  497. }
  498. // End
  499. if (!$error) {
  500. return $this->id;
  501. } else {
  502. return -1;
  503. }
  504. }
  505. /**
  506. * Load a proposal from database and its ligne array
  507. *
  508. * @param int $rowid id of object to load
  509. * @param string $ref Ref of proposal
  510. * @return int >0 if OK, <0 if KO
  511. */
  512. function fetch($rowid, $ref = '') {
  513. global $conf;
  514. return parent::fetch($rowid);
  515. // $sql = "SELECT p.rowid, p.ref, p.remise, p.remise_percent, p.remise_absolue, p.fk_soc";
  516. // $sql.= ", p.total, p.tva, p.localtax1, p.localtax2, p.total_ht";
  517. // $sql.= ", p.datec";
  518. // $sql.= ", p.date_valid as datev";
  519. // $sql.= ", p.datep as dp";
  520. // $sql.= ", p.fin_validite as dfv";
  521. // $sql.= ", p.date_livraison as date_livraison";
  522. // $sql.= ", p.model_pdf, p.ref_client, p.extraparams";
  523. // $sql.= ", p.note as note_private, p.note_public";
  524. // $sql.= ", p.fk_projet, p.fk_statut";
  525. // $sql.= ", p.fk_user_author, p.fk_user_valid, p.fk_user_cloture";
  526. // $sql.= ", p.fk_adresse_livraison";
  527. // $sql.= ", p.fk_availability";
  528. // $sql.= ", p.fk_input_reason";
  529. // $sql.= ", p.fk_cond_reglement";
  530. // $sql.= ", p.fk_mode_reglement";
  531. // $sql.= ", c.label as statut_label";
  532. // $sql.= ", ca.code as availability_code, ca.label as availability";
  533. // $sql.= ", dr.code as demand_reason_code, dr.label as demand_reason";
  534. // $sql.= ", cr.code as cond_reglement_code, cr.libelle as cond_reglement, cr.libelle_facture as cond_reglement_libelle_doc";
  535. // $sql.= ", cp.code as mode_reglement_code, cp.libelle as mode_reglement";
  536. // $sql.= " FROM " . MAIN_DB_PREFIX . "c_propalst as c, " . MAIN_DB_PREFIX . "propal as p";
  537. // $sql.= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_paiement as cp ON p.fk_mode_reglement = cp.id';
  538. // $sql.= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_payment_term as cr ON p.fk_cond_reglement = cr.rowid';
  539. // $sql.= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_availability as ca ON p.fk_availability = ca.rowid';
  540. // $sql.= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_input_reason as dr ON p.fk_input_reason = dr.rowid';
  541. // $sql.= " WHERE p.fk_statut = c.id";
  542. // $sql.= " AND p.entity = " . $conf->entity;
  543. // if ($ref)
  544. // $sql.= " AND p.ref='" . $ref . "'";
  545. // else
  546. // $sql.= " AND p.rowid=" . $rowid;
  547. //
  548. // dol_syslog(get_class($this) . "::fetch sql=" . $sql, LOG_DEBUG);
  549. // $resql = $this->db->query($sql);
  550. // if ($resql) {
  551. // if ($this->db->num_rows($resql)) {
  552. // $obj = $this->db->fetch_object($resql);
  553. //
  554. // $this->id = $obj->rowid;
  555. //
  556. // $this->ref = $obj->ref;
  557. // $this->ref_client = $obj->ref_client;
  558. // $this->remise = $obj->remise;
  559. // $this->remise_percent = $obj->remise_percent;
  560. // $this->remise_absolue = $obj->remise_absolue;
  561. // $this->total = $obj->total; // TODO obsolete
  562. // $this->total_ht = $obj->total_ht;
  563. // $this->total_tva = $obj->tva;
  564. // $this->total_localtax1 = $obj->localtax1;
  565. // $this->total_localtax2 = $obj->localtax2;
  566. // $this->total_ttc = $obj->total;
  567. // $this->socid = $obj->fk_soc;
  568. // $this->fk_project = $obj->fk_projet;
  569. // $this->modelpdf = $obj->model_pdf;
  570. // $this->note = $obj->note_private; // TODO obsolete
  571. // $this->note_private = $obj->note_private;
  572. // $this->note_public = $obj->note_public;
  573. // $this->statut = $obj->fk_statut;
  574. // $this->statut_libelle = $obj->statut_label;
  575. //
  576. // $this->datec = $this->db->jdate($obj->datec); // TODO obsolete
  577. // $this->datev = $this->db->jdate($obj->datev); // TODO obsolete
  578. // $this->date_creation = $this->db->jdate($obj->datec); //Creation date
  579. // $this->date_validation = $this->db->jdate($obj->datev); //Validation date
  580. // $this->date = $this->db->jdate($obj->dp); // Proposal date
  581. // $this->datep = $this->db->jdate($obj->dp); // deprecated
  582. // $this->fin_validite = $this->db->jdate($obj->dfv);
  583. // $this->date_livraison = $this->db->jdate($obj->date_livraison);
  584. // $this->availability_id = $obj->fk_availability;
  585. // $this->availability_code = $obj->availability_code;
  586. // $this->availability = $obj->availability;
  587. // $this->demand_reason_id = $obj->fk_input_reason;
  588. // $this->demand_reason_code = $obj->demand_reason_code;
  589. // $this->demand_reason = $obj->demand_reason;
  590. // $this->fk_delivery_address = $obj->fk_adresse_livraison; // TODO obsolete
  591. // $this->fk_address = $obj->fk_adresse_livraison;
  592. //
  593. // $this->mode_reglement_id = $obj->fk_mode_reglement;
  594. // $this->mode_reglement_code = $obj->mode_reglement_code;
  595. // $this->mode_reglement = $obj->mode_reglement;
  596. // $this->cond_reglement_id = $obj->fk_cond_reglement;
  597. // $this->cond_reglement_code = $obj->cond_reglement_code;
  598. // $this->cond_reglement = $obj->cond_reglement;
  599. // $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
  600. //
  601. // $this->extraparams = (array) json_decode($obj->extraparams, true);
  602. //
  603. // $this->user_author_id = $obj->fk_user_author;
  604. // $this->user_valid_id = $obj->fk_user_valid;
  605. // $this->user_close_id = $obj->fk_user_cloture;
  606. //
  607. // if ($obj->fk_statut == 0) {
  608. // $this->brouillon = 1;
  609. // }
  610. //
  611. // $this->db->free($resql);
  612. //
  613. // $this->lines = array();
  614. //
  615. // /*
  616. // * Lignes propales liees a un produit ou non
  617. // */
  618. // $sql = "SELECT d.rowid, d.fk_propal, d.fk_parent_line, d.label as custom_label, d.description, d.price, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.qty, d.fk_remise_except, d.remise_percent, d.subprice, d.fk_product,";
  619. // $sql.= " d.info_bits, d.total_ht, d.total_tva, d.total_localtax1, d.total_localtax2, d.total_ttc, d.fk_product_fournisseur_price as fk_fournprice, d.buy_price_ht as pa_ht, d.special_code, d.rang, d.product_type,";
  620. // $sql.= ' p.ref as product_ref, p.description as product_desc, p.fk_product_type, p.label as product_label';
  621. // $sql.= " FROM " . MAIN_DB_PREFIX . "propaldet as d";
  622. // $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON d.fk_product = p.rowid";
  623. // $sql.= " WHERE d.fk_propal = " . $this->id;
  624. // $sql.= " ORDER by d.rang";
  625. //
  626. // $result = $this->db->query($sql);
  627. // if ($result) {
  628. // $num = $this->db->num_rows($result);
  629. // $i = 0;
  630. //
  631. // while ($i < $num) {
  632. // $objp = $this->db->fetch_object($result);
  633. //
  634. // $line = new PropalLine($this->db);
  635. //
  636. // $line->rowid = $objp->rowid;
  637. // $line->fk_propal = $objp->fk_propal;
  638. // $line->fk_parent_line = $objp->fk_parent_line;
  639. // $line->product_type = $objp->product_type;
  640. // $line->label = $objp->custom_label;
  641. // $line->desc = $objp->description; // Description ligne
  642. // $line->qty = $objp->qty;
  643. // $line->tva_tx = $objp->tva_tx;
  644. // $line->localtax1_tx = $objp->localtax1_tx;
  645. // $line->localtax2_tx = $objp->localtax2_tx;
  646. // $line->subprice = $objp->subprice;
  647. // $line->fk_remise_except = $objp->fk_remise_except;
  648. // $line->remise_percent = $objp->remise_percent;
  649. // $line->price = $objp->price; // TODO deprecated
  650. //
  651. // $line->info_bits = $objp->info_bits;
  652. // $line->total_ht = $objp->total_ht;
  653. // $line->total_tva = $objp->total_tva;
  654. // $line->total_localtax1 = $objp->total_localtax1;
  655. // $line->total_localtax2 = $objp->total_localtax2;
  656. // $line->total_ttc = $objp->total_ttc;
  657. // $line->fk_fournprice = $objp->fk_fournprice;
  658. // $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
  659. // $line->pa_ht = $marginInfos[0];
  660. // $line->marge_tx = $marginInfos[1];
  661. // $line->marque_tx = $marginInfos[2];
  662. // $line->special_code = $objp->special_code;
  663. // $line->rang = $objp->rang;
  664. //
  665. // $line->fk_product = $objp->fk_product;
  666. //
  667. // $line->ref = $objp->product_ref; // TODO deprecated
  668. // $line->product_ref = $objp->product_ref;
  669. // $line->libelle = $objp->product_label; // TODO deprecated
  670. // $line->product_label = $objp->product_label;
  671. // $line->product_desc = $objp->product_desc; // Description produit
  672. // $line->fk_product_type = $objp->fk_product_type;
  673. //
  674. // $this->lines[$i] = $line;
  675. // //dol_syslog("1 ".$line->fk_product);
  676. // //print "xx $i ".$this->lines[$i]->fk_product;
  677. // $i++;
  678. // }
  679. // $this->db->free($result);
  680. // } else {
  681. // $this->error = $this->db->error();
  682. // dol_syslog(get_class($this) . "::fetch Error " . $this->error, LOG_ERR);
  683. // return -1;
  684. // }
  685. //
  686. // return 1;
  687. // }
  688. //
  689. // $this->error = "Record Not Found";
  690. // return 0;
  691. // } else {
  692. // $this->error = $this->db->error();
  693. // dol_syslog(get_class($this) . "::fetch Error " . $this->error, LOG_ERR);
  694. // return -1;
  695. // }
  696. }
  697. /**
  698. * Update propal
  699. *
  700. * @param User $user Objet user that make creation
  701. * @param int $notrigger Disable all triggers
  702. * @return int <0 if KO, >0 if OK
  703. */
  704. function update($user, $notrigger = 0) {
  705. global $conf, $langs, $mysoc;
  706. $error = 0;
  707. dol_syslog("Propal::update user=" . $user->id);
  708. // Clean parameters
  709. if (empty($this->date))
  710. $this->date = $this->datep;
  711. $this->fin_validite = $this->date + ($this->duree_validite * 24 * 3600);
  712. // Check parameters
  713. $soc = new Societe($this->db);
  714. $result = $soc->fetch($this->socid);
  715. if ($result < 0) {
  716. $this->error = "Failed to fetch company";
  717. dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR);
  718. return -3;
  719. }
  720. $this->client = new stdClass();
  721. $this->client->id = $soc->id;
  722. $this->client->name = $soc->name;
  723. if (empty($this->date)) {
  724. $this->error = "Date of proposal is required";
  725. dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR);
  726. return -4;
  727. }
  728. $this->record();
  729. if (!$notrigger) {
  730. // Appel des triggers
  731. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  732. $interface = new Interfaces($this->db);
  733. $result = $interface->run_triggers('PROPAL_UPDATE', $this, $user, $langs, $conf);
  734. if ($result < 0) {
  735. $error++;
  736. $this->errors = $interface->errors;
  737. }
  738. // Fin appel triggers
  739. }
  740. return 1;
  741. }
  742. /**
  743. * Set status to validated
  744. *
  745. * @param User $user Object user that validate
  746. * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
  747. * @return int <0 if KO, >=0 if OK
  748. */
  749. function valid($user, $notrigger = 0) {
  750. global $conf, $langs;
  751. $error = 0;
  752. $now = dol_now();
  753. if ($user->rights->propal->valider) {
  754. $this->Status = 'OPENED';
  755. $this->user_valid_login = $user->login;
  756. $this->datev = $now;
  757. $this->record();
  758. return 1;
  759. // $this->db->begin();
  760. //
  761. // $sql = "UPDATE " . MAIN_DB_PREFIX . "propal";
  762. // $sql.= " SET fk_statut = 1, date_valid='" . $this->db->idate($now) . "', fk_user_valid=" . $user->id;
  763. // $sql.= " WHERE rowid = " . $this->id . " AND fk_statut = 0";
  764. //
  765. // dol_syslog(get_class($this) . '::valid sql=' . $sql);
  766. // if ($this->db->query($sql)) {
  767. // if (!$notrigger) {
  768. // // Appel des triggers
  769. // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  770. // $interface = new Interfaces($this->db);
  771. // $result = $interface->run_triggers('PROPAL_VALIDATE', $this, $user, $langs, $conf);
  772. // if ($result < 0) {
  773. // $error++;
  774. // $this->errors = $interface->errors;
  775. // }
  776. // // Fin appel triggers
  777. // }
  778. //
  779. // if (!$error) {
  780. // $this->brouillon = 0;
  781. // $this->statut = 1;
  782. // $this->user_valid_id = $user->id;
  783. // $this->datev = $now;
  784. // $this->db->commit();
  785. // return 1;
  786. // } else {
  787. // $this->db->rollback();
  788. // return -2;
  789. // }
  790. } else {
  791. $this->db->rollback();
  792. return -1;
  793. }
  794. }
  795. /**
  796. * Define proposal date
  797. *
  798. * @param User $user Object user that modify
  799. * @param timestamp $date Date
  800. * @return int <0 if KO, >0 if OK
  801. */
  802. function set_date($user, $date) {
  803. if (empty($date)) {
  804. $this->error = 'ErrorBadParameter';
  805. dol_syslog(get_class($this) . "::set_date " . $this->error, LOG_ERR);
  806. return -1;
  807. }
  808. if ($user->rights->propal->creer) {
  809. $this->date = $date;
  810. $this->datep = $date;
  811. $this->record();
  812. return 1;
  813. // $sql = "UPDATE " . MAIN_DB_PREFIX . "propal SET datep = '" . $this->db->idate($date) . "'";
  814. // $sql.= " WHERE rowid = " . $this->id . " AND fk_statut = 0";
  815. //
  816. // dol_syslog(get_class($this) . "::set_date sql=" . $sql);
  817. // if ($this->db->query($sql)) {
  818. // $this->date = $date;
  819. // $this->datep = $date; // deprecated
  820. // return 1;
  821. // } else {
  822. // $this->error = $this->db->lasterror();
  823. // dol_syslog(get_class($this) . "::set_date " . $this->error, LOG_ERR);
  824. // return -1;
  825. // }
  826. }
  827. }
  828. /**
  829. * Define end validity date
  830. *
  831. * @param User $user Object user that modify
  832. * @param timestamp $date_fin_validite End of validity date
  833. * @return int <0 if KO, >0 if OK
  834. */
  835. function set_echeance($user, $date_fin_validite) {
  836. if ($user->rights->propal->creer) {
  837. $this->fin_validite = $date_fin_validite;
  838. $this->record();
  839. return 1;
  840. // $sql = "UPDATE " . MAIN_DB_PREFIX . "propal SET fin_validite = " . ($date_fin_validite != '' ? "'" . $this->db->idate($date_fin_validite) . "'" : 'null');
  841. // $sql.= " WHERE rowid = " . $this->id . " AND fk_statut = 0";
  842. // if ($this->db->query($sql)) {
  843. // $this->fin_validite = $date_fin_validite;
  844. // return 1;
  845. // } else {
  846. // $this->error = $this->db->error();
  847. // dol_syslog(get_class($this) . "::set_echeance Erreur SQL" . $this->error, LOG_ERR);
  848. // return -1;
  849. // }
  850. }
  851. }
  852. /**
  853. * Set delivery date
  854. *
  855. * @param User $user Object user that modify
  856. * @param timestamp $date_livraison Delivery date
  857. * @return int <0 if ko, >0 if ok
  858. */
  859. function set_date_livraison($user, $date_livraison) {
  860. if ($user->rights->propal->creer) {
  861. $this->date_livraison = $date_livraison;
  862. $this->record();
  863. return 1;
  864. // $sql = "UPDATE " . MAIN_DB_PREFIX . "propal ";
  865. // $sql.= " SET date_livraison = " . ($date_livraison != '' ? "'" . $this->db->idate($date_livraison) . "'" : 'null');
  866. // $sql.= " WHERE rowid = " . $this->id;
  867. //
  868. // if ($this->db->query($sql)) {
  869. // $this->date_livraison = $date_livraison;
  870. // return 1;
  871. // } else {
  872. // $this->error = $this->db->error();
  873. // dol_syslog(get_class($this) . "::set_date_livraison Erreur SQL");
  874. // return -1;
  875. // }
  876. }
  877. }
  878. /**
  879. * Set delivery
  880. *
  881. * @param User $user Object user that modify
  882. * @param int $id Availability id
  883. * @return int <0 if KO, >0 if OK
  884. */
  885. function set_availability($user, $id) {
  886. if ($user->rights->propal->creer) {
  887. $sql = "UPDATE " . MAIN_DB_PREFIX . "propal ";
  888. $sql.= " SET fk_availability = '" . $id . "'";
  889. $sql.= " WHERE rowid = " . $this->id;
  890. if ($this->db->query($sql)) {
  891. $this->fk_availability = $id;
  892. return 1;
  893. } else {
  894. $this->error = $this->db->error();
  895. dol_syslog(get_class($this) . "::set_availability Erreur SQL");
  896. return -1;
  897. }
  898. }
  899. }
  900. /**
  901. * Set source of demand
  902. *
  903. * @param User $user Object user that modify
  904. * @param int $id Input reason id
  905. * @return int <0 if KO, >0 if OK
  906. */
  907. function set_demand_reason($user, $id) {
  908. if ($user->rights->propal->creer) {
  909. $sql = "UPDATE " . MAIN_DB_PREFIX . "propal ";
  910. $sql.= " SET fk_input_reason = '" . $id . "'";
  911. $sql.= " WHERE rowid = " . $this->id;
  912. if ($this->db->query($sql)) {
  913. $this->fk_input_reason = $id;
  914. return 1;
  915. } else {
  916. $this->error = $this->db->error();
  917. dol_syslog(get_class($this) . "::set_demand_reason Erreur SQL");
  918. return -1;
  919. }
  920. }
  921. }
  922. /**
  923. * Set customer reference number
  924. *
  925. * @param User $user Object user that modify
  926. * @param string $ref_client Customer reference
  927. * @return int <0 if ko, >0 if ok
  928. */
  929. function set_ref_client($user, $ref_client) {
  930. if ($user->rights->propal->creer) {
  931. $this->ref_client = $ref_client;
  932. $this->record();
  933. return 1;
  934. // dol_syslog('Propale::set_ref_client this->id=' . $this->id . ', ref_client=' . $ref_client);
  935. //
  936. // $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'propal SET ref_client = ' . (empty($ref_client) ? 'NULL' : '\'' . $this->db->escape($ref_client) . '\'');
  937. // $sql.= ' WHERE rowid = ' . $this->id;
  938. // if ($this->db->query($sql)) {
  939. // $this->ref_client = $ref_client;
  940. // return 1;
  941. // } else {
  942. // $this->error = $this->db->error();
  943. // dol_syslog('Propale::set_ref_client Erreur ' . $this->error . ' - ' . $sql);
  944. // return -2;
  945. // }
  946. } else {
  947. return -1;
  948. }
  949. }
  950. /**
  951. * Set an overall discount on the proposal
  952. *
  953. * @param User $user Object user that modify
  954. * @param double $remise Amount discount
  955. * @return int <0 if ko, >0 if ok
  956. */
  957. function set_remise_percent($user, $remise) {
  958. $remise = trim($remise) ? trim($remise) : 0;
  959. if ($user->rights->propal->creer) {
  960. $remise = price2num($remise);
  961. $sql = "UPDATE " . MAIN_DB_PREFIX . "propal SET remise_percent = " . $remise;
  962. $sql.= " WHERE rowid = " . $this->id . " AND fk_statut = 0";
  963. if ($this->db->query($sql)) {
  964. $this->remise_percent = $remise;
  965. $this->update_price(1);
  966. return 1;
  967. } else {
  968. $this->error = $this->db->error();
  969. dol_syslog(get_class($this) . "::set_remise_percent Error sql=$sql");
  970. return -1;
  971. }
  972. }
  973. }
  974. /**
  975. * Set an absolute overall discount on the proposal
  976. *
  977. * @param User $user Object user that modify
  978. * @param double $remise Amount discount
  979. * @return int <0 if ko, >0 if ok
  980. */
  981. function set_remise_absolue($user, $remise) {
  982. $remise = trim($remise) ? trim($remise) : 0;
  983. if ($user->rights->propal->creer) {
  984. $remise = price2num($remise);
  985. $sql = "UPDATE " . MAIN_DB_PREFIX . "propal ";
  986. $sql.= " SET remise_absolue = " . $remise;
  987. $sql.= " WHERE rowid = " . $this->id . " AND fk_statut = 0";
  988. if ($this->db->query($sql)) {
  989. $this->remise_absolue = $remise;
  990. $this->update_price(1);
  991. return 1;
  992. } else {
  993. $this->error = $this->db->error();
  994. dol_syslog(get_class($this) . "::set_remise_absolue Error sql=$sql");
  995. return -1;
  996. }
  997. }
  998. }
  999. /**
  1000. * Close the commercial proposal
  1001. *
  1002. * @param User $user Object user that close
  1003. * @param int $statut Statut
  1004. * @param text $note Comment
  1005. * @return int <0 if KO, >0 if OK
  1006. */
  1007. function reopen($user, $statut, $note) {
  1008. global $langs, $conf;
  1009. $this->statut = $statut;
  1010. $error = 0;
  1011. $now = dol_now();
  1012. $this->db->begin();
  1013. $sql = "UPDATE " . MAIN_DB_PREFIX . "propal";
  1014. $sql.= " SET fk_statut = " . $statut . ", note = '" . $this->db->escape($note) . "', date_cloture=" . $this->db->idate($now) . ", fk_user_cloture=" . $user->id;
  1015. $sql.= " WHERE rowid = " . $this->id;
  1016. $resql = $this->db->query($sql);
  1017. if ($resql) {
  1018. }
  1019. }
  1020. /**
  1021. * Close the commercial proposal
  1022. *
  1023. * @param User $user Object user that close
  1024. * @param int $statut Statut
  1025. * @param text $note Comment
  1026. * @return int <0 if KO, >0 if OK
  1027. */
  1028. function cloture($user, $statut, $note) {
  1029. global $langs, $conf;
  1030. $error = 0;
  1031. $now = dol_now();
  1032. $this->Status = $statut;
  1033. $this->note = $note;
  1034. $this->record();
  1035. if ($this->Status == "SIGNED") {
  1036. $soc = new Societe($this->db);
  1037. $soc->id = $this->socid;
  1038. $result = $soc->set_as_client();
  1039. // Appel des triggers
  1040. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  1041. $interface = new Interfaces($this->db);
  1042. $result = $interface->run_triggers('PROPAL_CLOSE_SIGNED', $this, $user, $langs, $conf);
  1043. if ($result < 0) {
  1044. $error++;
  1045. $this->errors = $interface->errors;
  1046. }
  1047. } else {
  1048. // Appel des triggers
  1049. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  1050. $interface = new Interfaces($this->db);
  1051. $result = $interface->run_triggers('PROPAL_CLOSE_REFUSED', $this, $user, $langs, $conf);
  1052. if ($result < 0) {
  1053. $error++;
  1054. $this->errors = $interface->errors;
  1055. }
  1056. }
  1057. // $this->db->begin();
  1058. //
  1059. // $sql = "UPDATE " . MAIN_DB_PREFIX . "propal";
  1060. // $sql.= " SET fk_statut = " . $statut . ", note = '" . $this->db->escape($note) . "', date_cloture=" . $this->db->idate($now) . ", fk_user_cloture=" . $user->id;
  1061. // $sql.= " WHERE rowid = " . $this->id;
  1062. //
  1063. // $resql = $this->db->query($sql);
  1064. // if ($resql) {
  1065. // if ($statut == 2) {
  1066. // // Classe la societe rattachee comme client
  1067. // $soc = new Societe($this->db);
  1068. // $soc->id = $this->socid;
  1069. // $result = $soc->set_as_client();
  1070. //
  1071. // if ($result < 0) {
  1072. // $this->error = $this->db->error();
  1073. // $this->db->rollback();
  1074. // return -2;
  1075. // }
  1076. //
  1077. // // Appel des triggers
  1078. // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  1079. // $interface = new Interfaces($this->db);
  1080. // $result = $interface->run_triggers('PROPAL_CLOSE_SIGNED', $this, $user, $langs, $conf);
  1081. // if ($result < 0) {
  1082. // $error++;
  1083. // $this->errors = $interface->errors;
  1084. // }
  1085. // // Fin appel triggers
  1086. // } else {
  1087. // // Appel des triggers
  1088. // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  1089. // $interface = new Interfaces($this->db);
  1090. // $result = $interface->run_triggers('PROPAL_CLOSE_REFUSED', $this, $user, $langs, $conf);
  1091. // if ($result < 0) {
  1092. // $error++;
  1093. // $this->errors = $interface->errors;
  1094. // }
  1095. // // Fin appel triggers
  1096. // }
  1097. //
  1098. // $this->db->commit();
  1099. // return 1;
  1100. // } else {
  1101. // $this->error = $this->db->error();
  1102. // $this->db->rollback();
  1103. // return -1;
  1104. // }
  1105. }
  1106. /**
  1107. * Class invoiced the Propal
  1108. *
  1109. * @return int <0 si ko, >0 si ok
  1110. */
  1111. function classifyBilled() {
  1112. $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'propal SET fk_statut = 4';
  1113. $sql .= ' WHERE rowid = ' . $this->id . ' AND fk_statut > 0 ;';
  1114. if ($this->db->query($sql)) {
  1115. $this->statut = 4;
  1116. return 1;
  1117. } else {
  1118. dol_print_error($this->db);
  1119. }
  1120. }
  1121. /**
  1122. * Class invoiced the Propal
  1123. *
  1124. * @return int <0 si ko, >0 si ok
  1125. */
  1126. function classer_facturee() {
  1127. return $this->classifyBilled();
  1128. }
  1129. /**
  1130. * Set draft status
  1131. *
  1132. * @param User $user Object user that modify
  1133. * @return int <0 if KO, >0 if OK
  1134. */
  1135. function set_draft($user) {
  1136. global $conf, $langs;
  1137. $this->Status = "DRAFT";
  1138. $this->record();
  1139. return 1;
  1140. // $sql = "UPDATE " . MAIN_DB_PREFIX . "propal SET fk_statut = 0";
  1141. // $sql.= " WHERE rowid = " . $this->id;
  1142. //
  1143. // if ($this->db->query($sql)) {
  1144. // $this->statut = 0;
  1145. // $this->brouillon = 1;
  1146. // return 1;
  1147. // } else {
  1148. // return -1;
  1149. // }
  1150. }
  1151. /**
  1152. * Return list of proposal (eventually filtered on user) into an array
  1153. *
  1154. * @param int $shortlist 0=Return array[id]=ref, 1=Return array[](id=>id,ref=>ref)
  1155. * @param int $draft 0=not draft, 1=draft
  1156. * @param int $notcurrentuser 0=current user, 1=not current user
  1157. * @param int $socid Id third pary
  1158. * @param int $limit For pagination
  1159. * @param int $offset For pagination
  1160. * @param string $sortfield Sort criteria
  1161. * @param string $sortorder Sort order
  1162. * @return int -1 if KO, array with result if OK
  1163. */
  1164. function liste_array($shortlist = 0, $draft = 0, $notcurrentuser = 0, $socid = 0, $limit = 0, $offset = 0, $sortfield = 'p.datep', $sortorder = 'DESC') {
  1165. global $conf, $user;
  1166. $ga = array();
  1167. $sql = "SELECT s.nom, s.rowid, p.rowid as propalid, p.fk_statut, p.total_ht, p.ref, p.remise, ";
  1168. $sql.= " p.datep as dp, p.fin_validite as datelimite";
  1169. $sql.= " FROM " . MAIN_DB_PREFIX . "societe as s, " . MAIN_DB_PREFIX . "propal as p, " . MAIN_DB_PREFIX . "c_propalst as c";
  1170. $sql.= " WHERE p.entity = " . $conf->entity;
  1171. $sql.= " AND p.fk_soc = s.rowid";
  1172. $sql.= " AND p.fk_statut = c.id";
  1173. if ($socid)
  1174. $sql.= " AND s.rowid = " . $socid;
  1175. if ($draft)
  1176. $sql.= " AND p.fk_statut = 0";
  1177. if ($notcurrentuser)
  1178. $sql.= " AND p.fk_user_author <> " . $user->id;
  1179. $sql.= $this->db->order($sortfield, $sortorder);
  1180. $sql.= $this->db->plimit($limit, $offset);
  1181. $result = $this->db->query($sql);
  1182. if ($result) {
  1183. $num = $this->db->num_rows($result);
  1184. if ($num) {
  1185. $i = 0;
  1186. while ($i < $num) {
  1187. $obj = $this->db->fetch_object($result);
  1188. if ($shortlist) {
  1189. $ga[$obj->propalid] = $obj->ref;
  1190. } else {
  1191. $ga[$i]['id'] = $obj->propalid;
  1192. $ga[$i]['ref'] = $obj->ref;
  1193. }
  1194. $i++;
  1195. }
  1196. }
  1197. return $ga;
  1198. } else {
  1199. dol_print_error($this->db);
  1200. return -1;
  1201. }
  1202. }
  1203. /**
  1204. * Returns an array with the numbers of related invoices
  1205. *
  1206. * @return array Array of invoices
  1207. */
  1208. function getInvoiceArrayList() {
  1209. return $this->InvoiceArrayList($this->id);
  1210. }
  1211. /**
  1212. * Returns an array with id and ref of related invoices
  1213. *
  1214. * @param int $id Id propal
  1215. * @return array Array of invoices id
  1216. */
  1217. function InvoiceArrayList($id) {
  1218. $ga = array();
  1219. $linkedInvoices = array();
  1220. $this->fetchObjectLinked($id, $this->element);
  1221. foreach ($this->linkedObjectsIds as $objecttype => $objectid) {
  1222. $numi = count($objectid);
  1223. for ($i = 0; $i < $numi; $i++) {
  1224. // Cas des factures liees directement
  1225. if ($objecttype == 'facture') {
  1226. $linkedInvoices[] = $objectid[$i];
  1227. }
  1228. // Cas des factures liees via la commande
  1229. else {
  1230. $this->fetchObjectLinked($objectid[$i], $objecttype);
  1231. foreach ($this->linkedObjectsIds as $subobjecttype => $subobjectid) {
  1232. $numj = count($subobjectid);
  1233. for ($j = 0; $j < $numj; $j++) {
  1234. $linkedInvoices[] = $subobjectid[$j];
  1235. }
  1236. }
  1237. }
  1238. }
  1239. }
  1240. if (count($linkedInvoices) > 0) {
  1241. $sql = "SELECT rowid as facid, facnumber, total, datef as df, fk_user_author, fk_statut, paye";
  1242. $sql.= " FROM " . MAIN_DB_PREFIX . "facture";
  1243. $sql.= " WHERE rowid IN (" . implode(',', $linkedInvoices) . ")";
  1244. dol_syslog(get_class($this) . "::InvoiceArrayList sql=" . $sql);
  1245. $resql = $this->db->query($sql);
  1246. if ($resql) {
  1247. $tab_sqlobj = array();
  1248. $nump = $this->db->num_rows($resql);
  1249. for ($i = 0; $i < $nump; $i++) {
  1250. $sqlobj = $this->db->fetch_object($resql);
  1251. $tab_sqlobj[] = $sqlobj;
  1252. }
  1253. $this->db->free($resql);
  1254. $nump = count($tab_sqlobj);
  1255. if ($nump) {
  1256. $i = 0;
  1257. while ($i < $nump) {
  1258. $obj = array_shift($tab_sqlobj);
  1259. $ga[$i] = $obj;
  1260. $i++;
  1261. }
  1262. }
  1263. return $ga;
  1264. } else {
  1265. return -1;
  1266. }
  1267. }
  1268. else
  1269. return $ga;
  1270. }
  1271. /**
  1272. * Delete proposal
  1273. *
  1274. * @param User $user Object user that delete
  1275. * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
  1276. * @return int 1 if ok, otherwise if error
  1277. */
  1278. function delete($user, $notrigger = 0) {
  1279. global $conf, $langs;
  1280. require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  1281. $error = 0;
  1282. // $this->db->begin();
  1283. if (!$error && !$notrigger) {
  1284. // Call triggers
  1285. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  1286. $interface = new Interfaces($this->db);
  1287. $result = $interface->run_triggers('PROPAL_DELETE', $this, $user, $langs, $conf);
  1288. if ($result < 0) {
  1289. $error++;
  1290. $this->errors = $interface->errors;
  1291. }
  1292. // End call triggers
  1293. }
  1294. if (!$error) {
  1295. // Delete linked object
  1296. // Delete linked contacts
  1297. // Remove directory
  1298. $ref = dol_sanitizeFileName($this->ref);
  1299. if ($conf->propal->dir_output) {
  1300. $dir = $conf->propal->dir_output . "/" . $ref;
  1301. $file = $dir . "/" . $ref . ".pdf";
  1302. if (file_exists($file)) {
  1303. dol_delete_preview($this);
  1304. if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers
  1305. $this->error = 'ErrorFailToDeleteFile';
  1306. $this->errors = array('ErrorFailToDeleteFile');
  1307. return 0;
  1308. }
  1309. }
  1310. if (file_exists($dir)) {
  1311. $res = @dol_delete_dir_recursive($dir);
  1312. if (!$res) {
  1313. $this->error = 'ErrorFailToDeleteDir';
  1314. $this->errors = array('ErrorFailToDeleteDir');
  1315. return 0;
  1316. }
  1317. }
  1318. }
  1319. // Remove propal lines
  1320. $this->getLinesArray();
  1321. foreach ($this->lines as $line)
  1322. $line->delete();
  1323. // Remove propal
  1324. $this->deleteDoc();
  1325. return 1;
  1326. // $sql = "DELETE FROM " . MAIN_DB_PREFIX . "propaldet WHERE fk_propal = " . $this->id;
  1327. // if ($this->db->query($sql)) {
  1328. // $sql = "DELETE FROM " . MAIN_DB_PREFIX . "propal WHERE rowid = " . $this->id;
  1329. // if ($this->db->query($sql)) {
  1330. // // Delete linked object
  1331. // $res = $this->deleteObjectLinked();
  1332. // if ($res < 0)
  1333. // $error++;
  1334. //
  1335. // // Delete linked contacts
  1336. // $res = $this->delete_linked_contact();
  1337. // if ($res < 0)
  1338. // $error++;
  1339. //
  1340. // if (!$error) {
  1341. // // We remove directory
  1342. // $ref = dol_sanitizeFileName($this->ref);
  1343. // if ($conf->propal->dir_output) {
  1344. // $dir = $conf->propal->dir_output . "/" . $ref;
  1345. // $file = $dir . "/" . $ref . ".pdf";
  1346. // if (file_exists($file)) {
  1347. // dol_delete_preview($this);
  1348. //
  1349. // if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers
  1350. // $this->error = 'ErrorFailToDeleteFile';
  1351. // $this->errors = array('ErrorFailToDeleteFile');
  1352. // $this->db->rollback();
  1353. // return 0;
  1354. // }
  1355. // }
  1356. // if (file_exists($dir)) {
  1357. // $res = @dol_delete_dir_recursive($dir);
  1358. // if (!$res) {
  1359. // $this->error = 'ErrorFailToDeleteDir';
  1360. // $this->errors = array('ErrorFailToDeleteDir');
  1361. // $this->db->rollback();
  1362. // return 0;
  1363. // }
  1364. // }
  1365. // }
  1366. // }
  1367. //
  1368. // if (!$error) {
  1369. // dol_syslog(get_class($this) . "::delete $this->id by $user->id", LOG_DEBUG);
  1370. // $this->db->commit();
  1371. // return 1;
  1372. // } else {
  1373. // $this->error = $this->db->lasterror();
  1374. // dol_syslog(get_class($this) . "::delete " . $this->error, LOG_ERR);
  1375. // $this->db->rollback();
  1376. // return 0;
  1377. // }
  1378. // } else {
  1379. // $this->error = $this->db->lasterror();
  1380. // dol_syslog(get_class($this) . "::delete " . $this->error, LOG_ERR);
  1381. // $this->db->rollback();
  1382. // return -3;
  1383. // }
  1384. // } else {
  1385. // $this->error = $this->db->lasterror();
  1386. // dol_syslog(get_class($this) . "::delete " . $this->error, LOG_ERR);
  1387. // $this->db->rollback();
  1388. // return -2;
  1389. // }
  1390. } else {
  1391. $this->error = $this->db->lasterror();
  1392. dol_syslog(get_class($this) . "::delete " . $this->error, LOG_ERR);
  1393. // $this->db->rollback();
  1394. return -1;
  1395. }
  1396. }
  1397. /**
  1398. * Change the delivery time
  1399. *
  1400. * @param int $availability_code code of new delivery time
  1401. * @return int >0 if OK, <0 if KO
  1402. */
  1403. function availability($availability_code) {
  1404. dol_syslog('Propale::availability(' . $availability_code . ')');
  1405. if ($this->Status == "DRAFT") {
  1406. $this->availability_code = $availability_code;
  1407. $this->record();
  1408. return 1;
  1409. // $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'propal';
  1410. // $sql .= ' SET fk_availability = ' . $availability_id;
  1411. // $sql .= ' WHERE rowid=' . $this->id;
  1412. // if ($this->db->query($sql)) {
  1413. // $this->availability_id = $availability_id;
  1414. // return 1;
  1415. // } else {
  1416. // dol_syslog('Propale::availability Erreur ' . $sql . ' - ' . $this->db->error());
  1417. // $this->error = $this->db->error();
  1418. // return -1;
  1419. // }
  1420. } else {
  1421. dol_syslog('Propale::availability, etat propale incompatible');
  1422. $this->error = 'Etat propale incompatible ' . $this->statut;
  1423. return -2;
  1424. }
  1425. }
  1426. /**
  1427. * Change source demand
  1428. *
  1429. * @param int $demand_reason_code Code of new source demand
  1430. * @return int >0 si ok, <0 si ko
  1431. */
  1432. function demand_reason($demand_reason_code) {
  1433. dol_syslog('Propale::demand_reason(' . $demand_reason_code . ')');
  1434. if ($this->Status == "DRAFT") {
  1435. $this->demand_reason_code = $demand_reason_code;
  1436. $this->record();
  1437. return 1;
  1438. // $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'propal';
  1439. // $sql .= ' SET fk_input_reason = ' . $demand_reason_id;
  1440. // $sql .= ' WHERE rowid=' . $this->id;
  1441. // if ($this->db->query($sql)) {
  1442. // $this->demand_reason_id = $demand_reason_id;
  1443. // return 1;
  1444. // } else {
  1445. // dol_syslog('Propale::demand_reason Erreur ' . $sql . ' - ' . $this->db->error());
  1446. // $this->error = $this->db->error();
  1447. // return -1;
  1448. // }
  1449. } else {
  1450. dol_syslog('Propale::demand_reason, etat propale incompatible');
  1451. $this->error = 'Etat propale incompatible ' . $this->statut;
  1452. return -2;
  1453. }
  1454. }
  1455. /**
  1456. * Change payment terms
  1457. *
  1458. * @param string $code_reglement_code Code of new payment term
  1459. * @return int >0 si ok, <0 si ko
  1460. */
  1461. function setPaymentTerms($cond_reglement_code) {
  1462. if ($this->Status == "DRAFT") {
  1463. $this->cond_reglement_code = $cond_reglement_code;
  1464. $this->record();
  1465. return 1;
  1466. }
  1467. return -2;
  1468. }
  1469. /**
  1470. * Change payment methods
  1471. *
  1472. * @param string $mode_reglement_code Code of new payment term
  1473. * @return int >0 si ok, <0 si ko
  1474. */
  1475. function setPaymentMethods($mode_reglement_code) {
  1476. dol_syslog('Propale::setPaymentMethods(' . $mode_reglement_code . ')');
  1477. if ($this->Status == "DRAFT") {
  1478. $this->mode_reglement_code = $mode_reglement_code;
  1479. $this->record();
  1480. return 1;
  1481. // $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'propal';
  1482. // $sql .= ' SET fk_input_reason = ' . $demand_reason_id;
  1483. // $sql .= ' WHERE rowid=' . $this->id;
  1484. // if ($this->db->query($sql)) {
  1485. // $this->demand_reason_id = $demand_reason_id;
  1486. // return 1;
  1487. // } else {
  1488. // dol_syslog('Propale::demand_reason Erreur ' . $sql . ' - ' . $this->db->error());
  1489. // $this->error = $this->db->error();
  1490. // return -1;
  1491. // }
  1492. } else {
  1493. dol_syslog('Propale::demand_reason, etat propale incompatible');
  1494. $this->error = 'Etat propale incompatible ' . $this->statut;
  1495. return -2;
  1496. }
  1497. }
  1498. /**
  1499. * Object Proposal Information
  1500. *
  1501. * @param int $id Proposal id
  1502. * @return void
  1503. */
  1504. function info($id) {
  1505. $sql = "SELECT c.rowid, ";
  1506. $sql.= " c.datec, c.date_valid as datev, c.date_cloture as dateo,";
  1507. $sql.= " c.fk_user_author, c.fk_user_valid, c.fk_user_cloture";
  1508. $sql.= " FROM " . MAIN_DB_PREFIX . "propal as c";
  1509. $sql.= " WHERE c.rowid = " . $id;
  1510. $result = $this->db->query($sql);
  1511. if ($result) {
  1512. if ($this->db->num_rows($result)) {
  1513. $obj = $this->db->fetch_object($result);
  1514. $this->id = $obj->rowid;
  1515. $this->date_creation = $this->db->jdate($obj->datec);
  1516. $this->date_validation = $this->db->jdate($obj->datev);
  1517. $this->date_cloture = $this->db->jdate($obj->dateo);
  1518. $cuser = new User($this->db);
  1519. $cuser->fetch($obj->fk_user_author);
  1520. $this->user_creation = $cuser;
  1521. if ($obj->fk_user_valid) {
  1522. $vuser = new User($this->db);
  1523. $vuser->fetch($obj->fk_user_valid);
  1524. $this->user_validation = $vuser;
  1525. }
  1526. if ($obj->fk_user_cloture) {
  1527. $cluser = new User($this->db);
  1528. $cluser->fetch($obj->fk_user_cloture);
  1529. $this->user_cloture = $cluser;
  1530. }
  1531. }
  1532. $this->db->free($result);
  1533. } else {
  1534. dol_print_error($this->db);
  1535. }
  1536. }
  1537. /**
  1538. * Return label of status of proposal (draft, validated, ...)
  1539. *
  1540. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  1541. * @return string Label
  1542. */
  1543. function getLibStatut($mode = 0) {
  1544. return $this->LibStatut($this->statut, $mode);
  1545. }
  1546. /**
  1547. * Return label of a status (draft, validated, ...)
  1548. *
  1549. * @param int $statut id statut
  1550. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  1551. * @return string Label
  1552. */
  1553. function LibStatut($statut, $mode = 1) {
  1554. global $langs;
  1555. $langs->load("propal");
  1556. if ($mode == 0) {
  1557. return $this->labelstatut[$statut];
  1558. }
  1559. if ($mode == 1) {
  1560. return $this->labelstatut_short[$statut];
  1561. }
  1562. if ($mode == 2) {
  1563. if ($statut == 0)
  1564. return img_picto($langs->trans('PropalStatusDraftShort'), 'statut0') . ' ' . $this->labelstatut_short[$statut];
  1565. if ($statut == 1)
  1566. return img_picto($langs->trans('PropalStatusOpenedShort'), 'statut1') . ' ' . $this->labelstatut_short[$statut];
  1567. if ($statut == 2)
  1568. return img_picto($langs->trans('PropalStatusSignedShort'), 'statut3') . ' ' . $this->labelstatut_short[$statut];
  1569. if ($statut == 3)
  1570. return img_picto($langs->trans('PropalStatusNotSignedShort'), 'statut5') . ' ' . $this->labelstatut_short[$statut];
  1571. if ($statut == 4)
  1572. return img_picto($langs->trans('PropalStatusBilledShort'), 'statut6') . ' ' . $this->labelstatut_short[$statut];
  1573. }
  1574. if ($mode == 3) {
  1575. if ($statut == 0)
  1576. return img_picto($langs->trans('PropalStatusDraftShort'), 'statut0');
  1577. if ($statut == 1)
  1578. return img_picto($langs->trans('PropalStatusOpenedShort'), 'statut1');
  1579. if ($statut == 2)
  1580. return img_picto($langs->trans('PropalStatusSignedShort'), 'statut3');
  1581. if ($statut == 3)
  1582. return img_picto($langs->trans('PropalStatusNotSignedShort'), 'statut5');
  1583. if ($statut == 4)
  1584. return img_picto($langs->trans('PropalStatusBilledShort'), 'statut6');
  1585. }
  1586. if ($mode == 4) {
  1587. if ($statut == 0)
  1588. return img_picto($langs->trans('PropalStatusDraft'), 'statut0') . ' ' . $this->labelstatut[$statut];
  1589. if ($statut == 1)
  1590. return img_picto($langs->trans('PropalStatusOpened'), 'statut1') . ' ' . $this->labelstatut[$statut];
  1591. if ($statut == 2)
  1592. return img_picto($langs->trans('PropalStatusSigned'), 'statut3') . ' ' . $this->labelstatut[$statut];
  1593. if ($statut == 3)
  1594. return img_picto($langs->trans('PropalStatusNotSigned'), 'statut5') . ' ' . $this->labelstatut[$statut];
  1595. if ($statut == 4)
  1596. return img_picto($langs->trans('PropalStatusBilled'), 'statut6') . ' ' . $this->labelstatut[$statut];
  1597. }
  1598. if ($mode == 5) {
  1599. if ($statut == 0)
  1600. return $this->labelstatut_short[$statut] . ' ' . img_picto($langs->trans('PropalStatusDraftShort'), 'statut0');
  1601. if ($statut == 1)
  1602. return $this->labelstatut_short[$statut] . ' ' . img_picto($langs->trans('PropalStatusOpenedShort'), 'statut1');
  1603. if ($statut == 2)
  1604. return $this->labelstatut_short[$statut] . ' ' . img_picto($langs->trans('PropalStatusSignedShort'), 'statut3');
  1605. if ($statut == 3)
  1606. return $this->labelstatut_short[$statut] . ' ' . img_picto($langs->trans('PropalStatusNotSignedShort'), 'statut5');
  1607. if ($statut == 4)
  1608. return $this->labelstatut_short[$statut] . ' ' . img_picto($langs->trans('PropalStatusBilledShort'), 'statut6');
  1609. }
  1610. }
  1611. /**
  1612. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  1613. *
  1614. * @param User $user Object user
  1615. * @param int $mode "opened" for proposal to close, "signed" for proposal to invoice
  1616. * @return int <0 if KO, >0 if OK
  1617. */
  1618. function load_board($user, $mode) {
  1619. global $conf, $user;
  1620. $now = dol_now();
  1621. $this->nbtodo = $this->nbtodolate = 0;
  1622. $clause = " WHERE";
  1623. $sql = "SELECT p.rowid, p.ref, p.datec as datec, p.fin_validite as datefin";
  1624. $sql.= " FROM " . MAIN_DB_PREFIX . "propal as p";
  1625. if (!$user->rights->societe->client->voir && !$user->societe_id) {
  1626. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON p.fk_soc = sc.fk_soc";
  1627. $sql.= " WHERE sc.fk_user = " . $user->id;
  1628. $clause = " AND";
  1629. }
  1630. $sql.= $clause . " p.entity = " . $conf->entity;
  1631. if ($mode == 'opened')
  1632. $sql.= " AND p.fk_statut = 1";
  1633. if ($mode == 'signed')
  1634. $sql.= " AND p.fk_statut = 2";
  1635. if ($user->societe_id)
  1636. $sql.= " AND p.fk_soc = " . $user->societe_id;
  1637. $resql = $this->db->query($sql);
  1638. if ($resql) {
  1639. if ($mode == 'opened')
  1640. $delay_warning = $conf->propal->cloture->warning_delay;
  1641. if ($mode == 'signed')
  1642. $delay_warning = $conf->propal->facturation->warning_delay;
  1643. while ($obj = $this->db->fetch_object($resql)) {
  1644. $this->nbtodo++;
  1645. if ($mode == 'opened') {
  1646. $datelimit = $this->db->jdate($obj->datefin);
  1647. if ($datelimit < ($now - $delay_warning)) {
  1648. $this->nbtodolate++;
  1649. }
  1650. }
  1651. // TODO Definir regle des propales a facturer en retard
  1652. // if ($mode == 'signed' && ! count($this->FactureListeArray($obj->rowid))) $this->nbtodolate++;
  1653. }
  1654. return 1;
  1655. } else {
  1656. $this->error = $this->db->error();
  1657. return -1;
  1658. }
  1659. }
  1660. /**
  1661. * Initialise an instance with random values.
  1662. * Used to build previews or test instances.
  1663. * id must be 0 if object instance is a specimen.
  1664. *
  1665. * @return void
  1666. */
  1667. function initAsSpecimen() {
  1668. global $user, $langs, $conf;
  1669. // Charge tableau des produits prodids
  1670. $prodids = array();
  1671. $sql = "SELECT rowid";
  1672. $sql.= " FROM " . MAIN_DB_PREFIX . "product";
  1673. $sql.= " WHERE entity IN (" . getEntity('product', 1) . ")";
  1674. $resql = $this->db->query($sql);
  1675. if ($resql) {
  1676. $num_prods = $this->db->num_rows($resql);
  1677. $i = 0;
  1678. while ($i < $num_prods) {
  1679. $i++;
  1680. $row = $this->db->fetch_row($resql);
  1681. $prodids[$i] = $row[0];
  1682. }
  1683. }
  1684. // Initialise parametres
  1685. $this->id = 0;
  1686. $this->ref = 'SPECIMEN';
  1687. $this->ref_client = 'NEMICEPS';
  1688. $this->specimen = 1;
  1689. $this->socid = 1;
  1690. $this->date = time();
  1691. $this->fin_validite = $this->date + 3600 * 24 * 30;
  1692. $this->cond_reglement_id = 1;
  1693. $this->cond_reglement_code = 'RECEP';
  1694. $this->mode_reglement_id = 7;
  1695. $this->mode_reglement_code = 'CHQ';
  1696. $this->availability_id = 1;
  1697. $this->availability_code = 'DSP';
  1698. $this->demand_reason_id = 1;
  1699. $this->demand_reason_code = 'SRC_00';
  1700. $this->note_public = 'This is a comment (public)';
  1701. $this->note = 'This is a comment (private)';
  1702. // Lines
  1703. $nbp = 5;
  1704. $xnbp = 0;
  1705. while ($xnbp < $nbp) {
  1706. $line = new PropalLine($this->db);
  1707. $line->desc = $langs->trans("Description") . " " . $xnbp;
  1708. $line->qty = 1;
  1709. $line->subprice = 100;
  1710. $line->price = 100;
  1711. $line->tva_tx = 19.6;
  1712. $line->localtax1_tx = 0;
  1713. $line->localtax2_tx = 0;
  1714. if ($xnbp == 2) {
  1715. $line->total_ht = 50;
  1716. $line->total_ttc = 59.8;
  1717. $line->total_tva = 9.8;
  1718. $line->remise_percent = 50;
  1719. } else {
  1720. $line->total_ht = 100;
  1721. $line->total_ttc = 119.6;
  1722. $line->total_tva = 19.6;
  1723. $line->remise_percent = 00;
  1724. }
  1725. $prodid = rand(1, $num_prods);
  1726. $line->fk_product = $prodids[$prodid];
  1727. $this->lines[$xnbp] = $line;
  1728. $this->total_ht += $line->total_ht;
  1729. $this->total_tva += $line->total_tva;
  1730. $this->total_ttc += $line->total_ttc;
  1731. $xnbp++;
  1732. }
  1733. }
  1734. /**
  1735. * Charge indicateurs this->nb de tableau de bord
  1736. *
  1737. * @return int <0 if ko, >0 if ok
  1738. */
  1739. function load_state_board() {
  1740. global $conf, $user;
  1741. $this->nb = array();
  1742. $clause = "WHERE";
  1743. $sql = "SELECT count(p.rowid) as nb";
  1744. $sql.= " FROM " . MAIN_DB_PREFIX . "propal as p";
  1745. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as s ON p.fk_soc = s.rowid";
  1746. if (!$user->rights->societe->client->voir && !$user->societe_id) {
  1747. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as sc ON s.rowid = sc.fk_soc";
  1748. $sql.= " WHERE sc.fk_user = " . $user->id;
  1749. $clause = "AND";
  1750. }
  1751. $sql.= " " . $clause . " p.entity = " . $conf->entity;
  1752. $resql = $this->db->query($sql);
  1753. if ($resql) {
  1754. while ($obj = $this->db->fetch_object($resql)) {
  1755. $this->nb["proposals"] = $obj->nb;
  1756. }
  1757. return 1;
  1758. } else {
  1759. dol_print_error($this->db);
  1760. $this->error = $this->db->error();
  1761. return -1;
  1762. }
  1763. }
  1764. /**
  1765. * Returns the reference to the following non used Proposal used depending on the active numbering module
  1766. * defined into PROPALE_ADDON
  1767. *
  1768. * @param Societe $soc Object thirdparty
  1769. * @return string Reference libre pour la propale
  1770. */
  1771. function getNextNumRef($soc) {
  1772. global $conf, $db, $langs;
  1773. $langs->load("propal");
  1774. $dir = DOL_DOCUMENT_ROOT . "/propal/core/modules/propale/";
  1775. if (!empty($conf->global->PROPALE_ADDON)) {
  1776. $file = $conf->global->PROPALE_ADDON . ".php";
  1777. // Chargement de la classe de numerotation
  1778. $classname = $conf->global->PROPALE_ADDON;
  1779. require_once $dir . $file;
  1780. $obj = new $classname();
  1781. $numref = "";
  1782. $numref = $obj->getNextValue($soc, $this);
  1783. if ($numref != "") {
  1784. return $numref;
  1785. } else {
  1786. $this->error = $obj->error;
  1787. //dol_print_error($db,"Propale::getNextNumRef ".$obj->error);
  1788. return "";
  1789. }
  1790. } else {
  1791. $langs->load("errors");
  1792. print $langs->trans("Error") . " " . $langs->trans("ErrorModuleSetupNotComplete");
  1793. return "";
  1794. }
  1795. }
  1796. /**
  1797. * Return clicable link of object (with eventually picto)
  1798. *
  1799. * @param int $withpicto Add picto into link
  1800. * @param string $option Where point the link ('compta', 'expedition', 'document', ...)
  1801. * @param string $get_params Parametres added to url
  1802. * @return string String with URL
  1803. */
  1804. function getNomUrl($withpicto = 0, $option = '', $get_params = '') {
  1805. global $langs;
  1806. $result = '';
  1807. if ($option == '') {
  1808. $lien = '<a href="' . DOL_URL_ROOT . '/propal/propal.php?id=' . $this->id . $get_params . '">';
  1809. }
  1810. if ($option == 'compta') { // deprecated
  1811. $lien = '<a href="' . DOL_URL_ROOT . '/comm/propal.php?id=' . $this->id . $get_params . '">';
  1812. }
  1813. if ($option == 'expedition') {
  1814. $lien = '<a href="' . DOL_URL_ROOT . '/expedition/propal.php?id=' . $this->id . $get_params . '">';
  1815. }
  1816. if ($option == 'document') {
  1817. $lien = '<a href="' . DOL_URL_ROOT . '/propal/document.php?id=' . $this->id . $get_params . '">';
  1818. }
  1819. $lienfin = '</a>';
  1820. $picto = 'propal';
  1821. $label = $langs->trans("ShowPropal") . ': ' . $this->ref;
  1822. if ($withpicto)
  1823. $result.=($lien . img_object($label, $picto) . $lienfin);
  1824. if ($withpicto && $withpicto != 2)
  1825. $result.=' ';
  1826. $result.=$lien . $this->ref . $lienfin;
  1827. return $result;
  1828. }
  1829. /**
  1830. * Retrieve an array of propal lines
  1831. *
  1832. * @return int <0 if ko, >0 if ok
  1833. */
  1834. function getLinesArray() {
  1835. $this->lines = array();
  1836. $result = $this->getView("linesPerPropal", array("key" => $this->id));
  1837. foreach ($result->rows as $res) {
  1838. $l = new PropalLine($db);
  1839. $l->fetch($res->value->_id);
  1840. $this->lines[] = $l;
  1841. }
  1842. return 1;
  1843. // $sql = 'SELECT pt.rowid, pt.label as custom_label, pt.description, pt.fk_product, pt.fk_remise_except,';
  1844. // $sql.= ' pt.qty, pt.tva_tx, pt.remise_percent, pt.subprice, pt.info_bits,';
  1845. // $sql.= ' pt.total_ht, pt.total_tva, pt.total_ttc, pt.fk_product_fournisseur_price as fk_fournprice, pt.buy_price_ht as pa_ht, pt.special_code, pt.localtax1_tx, pt.localtax2_tx,';
  1846. // $sql.= ' pt.date_start, pt.date_end, pt.product_type, pt.rang, pt.fk_parent_line,';
  1847. // $sql.= ' p.label as product_label, p.ref, p.fk_product_type, p.rowid as prodid,';
  1848. // $sql.= ' p.description as product_desc';
  1849. // $sql.= ' FROM ' . MAIN_DB_PREFIX . 'propaldet as pt';
  1850. // $sql.= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON pt.fk_product=p.rowid';
  1851. // $sql.= ' WHERE pt.fk_propal = ' . $this->id;
  1852. // $sql.= ' ORDER BY pt.rang ASC, pt.rowid';
  1853. //
  1854. // $resql = $this->db->query($sql);
  1855. // if ($resql) {
  1856. // $num = $this->db->num_rows($resql);
  1857. // $i = 0;
  1858. //
  1859. // while ($i < $num) {
  1860. // $obj = $this->db->fetch_object($resql);
  1861. //
  1862. // $this->lines[$i] = new stdClass();
  1863. // $this->lines[$i]->id = $obj->rowid; // for backward compatibility
  1864. // $this->lines[$i]->rowid = $obj->rowid;
  1865. // $this->lines[$i]->label = $obj->custom_label;
  1866. // $this->lines[$i]->description = $obj->description;
  1867. // $this->lines[$i]->fk_product = $obj->fk_product;
  1868. // $this->lines[$i]->ref = $obj->ref;
  1869. // $this->lines[$i]->product_label = $obj->product_label;
  1870. // $this->lines[$i]->product_desc = $obj->product_desc;
  1871. // $this->lines[$i]->fk_product_type = $obj->fk_product_type; // deprecated
  1872. // $this->lines[$i]->product_type = $obj->product_type;
  1873. // $this->lines[$i]->qty = $obj->qty;
  1874. // $this->lines[$i]->subprice = $obj->subprice;
  1875. // $this->lines[$i]->fk_remise_except = $obj->fk_remise_except;
  1876. // $this->lines[$i]->remise_percent = $obj->remise_percent;
  1877. // $this->lines[$i]->tva_tx = $obj->tva_tx;
  1878. // $this->lines[$i]->info_bits = $obj->info_bits;
  1879. // $this->lines[$i]->total_ht = $obj->total_ht;
  1880. // $this->lines[$i]->total_tva = $obj->total_tva;
  1881. // $this->lines[$i]->total_ttc = $obj->total_ttc;
  1882. // $this->lines[$i]->fk_fournprice = $obj->fk_fournprice;
  1883. // $marginInfos = getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->lines[$i]->fk_fournprice, $obj->pa_ht);
  1884. // $this->lines[$i]->pa_ht = $marginInfos[0];
  1885. // $this->lines[$i]->marge_tx = $marginInfos[1];
  1886. // $this->lines[$i]->marque_tx = $marginInfos[2];
  1887. // $this->lines[$i]->fk_parent_line = $obj->fk_parent_line;
  1888. // $this->lines[$i]->special_code = $obj->special_code;
  1889. // $this->lines[$i]->rang = $obj->rang;
  1890. // $this->lines[$i]->date_start = $this->db->jdate($obj->date_start);
  1891. // $this->lines[$i]->date_end = $this->db->jdate($obj->date_end);
  1892. //
  1893. // $i++;
  1894. // }
  1895. // $this->db->free($resql);
  1896. //
  1897. // return 1;
  1898. // } else {
  1899. // $this->error = $this->db->error();
  1900. // dol_syslog(get_class($this) . "::getLinesArray Error sql=$sql, error=" . $this->error, LOG_ERR);
  1901. // return -1;
  1902. // }
  1903. }
  1904. public function getExtraFieldLabel($field) {
  1905. global $langs;
  1906. return $langs->trans($this->fk_extrafields->fields->{$field}->values->{$this->$field}->label);
  1907. }
  1908. function update_note_public($note_public) {
  1909. $this->note_public = $note_public;
  1910. $this->record();
  1911. return 1;
  1912. }
  1913. /**
  1914. * Set last model used by doc generator
  1915. *
  1916. * @param User $user User object that make change
  1917. * @param string $modelpdf Modele name
  1918. * @return int <0 if KO, >0 if OK
  1919. */
  1920. function setDocModel($user, $modelpdf) {
  1921. $this->modelpdf = $modelpdf;
  1922. $this->record();
  1923. return 1;
  1924. }
  1925. function setStatut($status) {
  1926. $this->Status = $status;
  1927. $this->record();
  1928. return 1;
  1929. }
  1930. public function getLinkedObject(){
  1931. $objects = array();
  1932. /* No variable $linked_objects ?
  1933. *
  1934. *
  1935. // Object stored in $this->linked_objects;
  1936. foreach ($this->linked_objects as $obj) {
  1937. switch ($obj->type) {
  1938. case 'commande':
  1939. $classname = 'Commande';
  1940. dol_include_once('commande/class/commande.class.php');
  1941. break;
  1942. }
  1943. $tmp = new $classname($this->db);
  1944. $tmp->fetch($obj->id);
  1945. $objects[$obj->type][] = $tmp;
  1946. }
  1947. *
  1948. */
  1949. // Objects that refer current propal in their $linked_objects variable.
  1950. $res = $this->getView('listLinkedObjects', array('key' => $this->id));
  1951. if (count($res->rows) > 0) {
  1952. foreach( $res->rows as $r) {
  1953. $classname = $r->value->class;
  1954. if ($classname == 'Commande')
  1955. require_once(DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php');
  1956. $obj = new $classname($this->db);
  1957. $obj->fetch($r->value->id);
  1958. $objects[strtolower($classname)][] = $obj;
  1959. }
  1960. }
  1961. return $objects;
  1962. }
  1963. public function printLinkedObjects(){
  1964. global $langs;
  1965. $objects = $this->getLinkedObject();
  1966. // Displaying linked orders
  1967. if (isset($objects['commande'])) {
  1968. $this->printLinkedObjectsType('commande', $objects['commande']);
  1969. }
  1970. }
  1971. public function printLinkedObjectsType($type, $data){
  1972. global $langs;
  1973. $title = 'LinkedObjects';
  1974. if ($type == 'commande')
  1975. $title = 'LinkedOrders';
  1976. print start_box($langs->trans($title), "six", $this->fk_extrafields->ico, false);
  1977. print '<table id="tablelines" class="noborder" width="100%">';
  1978. print '<tr>';
  1979. print '<th align="left">' . $langs->trans('Ref') . '</th>';
  1980. print '<th align="left">' . $langs->trans('Date') . '</th>';
  1981. print '<th align="left">' . $langs->trans('PriceHT') . '</th>';
  1982. print '<th align="left">' . $langs->trans('Status') . '</th>';
  1983. print '</tr>';
  1984. foreach ($data as $p) {
  1985. print '<tr>';
  1986. print '<td>' . $p->getNomUrl(1) . '</td>';
  1987. print '<td>' . dol_print_date($p->date) . '</td>';
  1988. print '<td>' . price($p->total_ht) . '</td>';
  1989. print '<td>' . $p->getExtraFieldLabel('Status') . '</td>';
  1990. print '</tr>';
  1991. }
  1992. print '</table>';
  1993. print end_box();
  1994. }
  1995. public function showLinkedObjects() {
  1996. global $langs;
  1997. print start_box($langs->trans("LinkedObjects"), "six", $this->fk_extrafields->ico, false);
  1998. print '<table class="display dt_act" id="listlinkedobjects" >';
  1999. // Ligne des titres
  2000. print '<thead>';
  2001. print'<tr>';
  2002. print'<th>';
  2003. print'</th>';
  2004. $obj->aoColumns[$i] = new stdClass();
  2005. $obj->aoColumns[$i]->mDataProp = "_id";
  2006. $obj->aoColumns[$i]->bUseRendered = false;
  2007. $obj->aoColumns[$i]->bSearchable = false;
  2008. $obj->aoColumns[$i]->bVisible = false;
  2009. $i++;
  2010. print'<th class="essential">';
  2011. print $langs->trans("Ref");
  2012. print'</th>';
  2013. $obj->aoColumns[$i] = new stdClass();
  2014. $obj->aoColumns[$i]->mDataProp = "ref";
  2015. $obj->aoColumns[$i]->bUseRendered = false;
  2016. $obj->aoColumns[$i]->bSearchable = true;
  2017. // $obj->aoColumns[$i]->fnRender = $this->datatablesFnRender("ref", "url");
  2018. $i++;
  2019. print'<th class="essential">';
  2020. print $langs->trans('Date');
  2021. print'</th>';
  2022. $obj->aoColumns[$i] = new stdClass();
  2023. $obj->aoColumns[$i]->mDataProp = "date";
  2024. $obj->aoColumns[$i]->sDefaultContent = "";
  2025. $obj->aoColumns[$i]->fnRender = $this->datatablesFnRender("date", "date");
  2026. $i++;
  2027. print'<th class="essential">';
  2028. print $langs->trans('PriceHT');
  2029. print'</th>';
  2030. $obj->aoColumns[$i] = new stdClass();
  2031. $obj->aoColumns[$i]->mDataProp = "total_ht";
  2032. $obj->aoColumns[$i]->sDefaultContent = "";
  2033. $obj->aoColumns[$i]->fnRender = $this->datatablesFnRender("total_ht", "price");
  2034. $i++;
  2035. print'<th class="essential">';
  2036. print $langs->trans('Status');
  2037. print'</th>';
  2038. $obj->aoColumns[$i] = new stdClass();
  2039. $obj->aoColumns[$i]->mDataProp = "Status";
  2040. $obj->aoColumns[$i]->sDefaultContent = "";
  2041. $obj->aoColumns[$i]->fnRender = $this->datatablesFnRender("Status", "status");
  2042. $i++;
  2043. print '</tr>';
  2044. print '</thead>';
  2045. print'<tfoot>';
  2046. print'</tfoot>';
  2047. print'<tbody>';
  2048. print'</tbody>';
  2049. print "</table>";
  2050. $obj->iDisplayLength = $max;
  2051. $obj->sAjaxSource = DOL_URL_ROOT . "/core/ajax/listdatatables.php?json=listLinkedObjects&class=" . get_class($this) . "&key=" . $this->id;
  2052. $this->datatablesCreate($obj, "listlinkedobjects", true);
  2053. print end_box();
  2054. }
  2055. public function addInPlace($obj){
  2056. global $user;
  2057. // Generating next ref
  2058. $this->ref = $obj->ref = $this->getNextNumRef();
  2059. // Converting date to timestamp
  2060. $date = explode('/', $this->date);
  2061. $this->date = $obj->date = dol_mktime(0, 0, 0, $date[1], $date[0], $date[2]);
  2062. // Setting author of propal
  2063. $this->author = new stdClass();
  2064. $this->author->id = $user->id;
  2065. $this->author->name = $user->login;
  2066. }
  2067. public function fetch_thirdparty(){
  2068. $thirdparty = new Societe($this->db);
  2069. $thirdparty->fetch($this->client->id);
  2070. $this->thirdparty = $thirdparty;
  2071. }
  2072. public function show($id) {
  2073. global $langs;
  2074. require_once(DOL_DOCUMENT_ROOT . '/propal/class/propal.class.php');
  2075. $propal = new Propal($this->db);
  2076. print start_box($langs->trans("Proposals"), $this->fk_extrafields->ico);
  2077. print '<table class="display dt_act" id="listpropals" >';
  2078. // Ligne des titres
  2079. print '<thead>';
  2080. print'<tr>';
  2081. print'<th>';
  2082. print'</th>';
  2083. $obj->aoColumns[$i] = new stdClass();
  2084. $obj->aoColumns[$i]->mDataProp = "_id";
  2085. $obj->aoColumns[$i]->bUseRendered = false;
  2086. $obj->aoColumns[$i]->bSearchable = false;
  2087. $obj->aoColumns[$i]->bVisible = false;
  2088. $i++;
  2089. print'<th class="essential">';
  2090. print $langs->trans("Ref");
  2091. print'</th>';
  2092. $obj->aoColumns[$i] = new stdClass();
  2093. $obj->aoColumns[$i]->mDataProp = "ref";
  2094. $obj->aoColumns[$i]->bUseRendered = false;
  2095. $obj->aoColumns[$i]->bSearchable = true;
  2096. $obj->aoColumns[$i]->fnRender = $propal->datatablesFnRender("ref", "url");
  2097. $i++;
  2098. print'<th class="essential">';
  2099. print $langs->trans('Date');
  2100. print'</th>';
  2101. $obj->aoColumns[$i] = new stdClass();
  2102. $obj->aoColumns[$i]->mDataProp = "date";
  2103. $obj->aoColumns[$i]->sDefaultContent = "";
  2104. $obj->aoColumns[$i]->fnRender = $propal->datatablesFnRender("date", "date");
  2105. $i++;
  2106. print'<th class="essential">';
  2107. print $langs->trans('PriceHT');
  2108. print'</th>';
  2109. $obj->aoColumns[$i] = new stdClass();
  2110. $obj->aoColumns[$i]->mDataProp = "total_ht";
  2111. $obj->aoColumns[$i]->sDefaultContent = "";
  2112. $obj->aoColumns[$i]->fnRender = $propal->datatablesFnRender("total_ht", "price");
  2113. $i++;
  2114. print'<th class="essential">';
  2115. print $langs->trans('Status');
  2116. print'</th>';
  2117. $obj->aoColumns[$i] = new stdClass();
  2118. $obj->aoColumns[$i]->mDataProp = "Status";
  2119. $obj->aoColumns[$i]->sDefaultContent = "";
  2120. $obj->aoColumns[$i]->fnRender = $propal->datatablesFnRender("Status", "status");
  2121. $i++;
  2122. print '</tr>';
  2123. print '</thead>';
  2124. print'<tfoot>';
  2125. print'</tfoot>';
  2126. print'<tbody>';
  2127. print'</tbody>';
  2128. print "</table>";
  2129. $obj->iDisplayLength = $max;
  2130. $obj->sAjaxSource = DOL_URL_ROOT . "/core/ajax/listdatatables.php?json=listBySociete&class=" . get_class($this) . "&key=" . $id;
  2131. $this->datatablesCreate($obj, "listpropals", true);
  2132. print end_box();
  2133. }
  2134. }
  2135. /**
  2136. * \class PropalLine
  2137. * \brief Class to manage commercial proposal lines
  2138. */
  2139. class PropalLine extends nosqlDocument {
  2140. public $class = "PropalLine";
  2141. var $db;
  2142. var $error;
  2143. var $oldline;
  2144. // From llx_propaldet
  2145. var $rowid;
  2146. var $fk_propal;
  2147. var $fk_parent_line;
  2148. var $desc; // Description ligne (deprecated)
  2149. public $description; // Description ligne
  2150. var $fk_product; // Id produit predefini
  2151. var $product_type = 0; // Type 0 = product, 1 = Service
  2152. var $qty;
  2153. var $tva_tx;
  2154. var $subprice;
  2155. var $remise_percent;
  2156. var $fk_remise_except;
  2157. var $rang = 0;
  2158. var $fk_fournprice;
  2159. var $pa_ht;
  2160. var $marge_tx;
  2161. var $marque_tx;
  2162. var $special_code; // Liste d'options non cumulabels:
  2163. // 1: frais de port
  2164. // 2: ecotaxe
  2165. // 3: ??
  2166. var $info_bits = 0; // Liste d'options cumulables:
  2167. // Bit 0: 0 si TVA normal - 1 si TVA NPR
  2168. // Bit 1: 0 ligne normale - 1 si ligne de remise fixe
  2169. var $total_ht; // Total HT de la ligne toute quantite et incluant la remise ligne
  2170. var $total_tva; // Total TVA de la ligne toute quantite et incluant la remise ligne
  2171. var $total_ttc; // Total TTC de la ligne toute quantite et incluant la remise ligne
  2172. // Ne plus utiliser
  2173. var $remise;
  2174. var $price;
  2175. // From llx_product
  2176. var $ref; // Reference produit
  2177. var $libelle; // Label produit
  2178. var $product_desc; // Description produit
  2179. var $localtax1_tx;
  2180. var $localtax2_tx;
  2181. var $total_localtax1;
  2182. var $total_localtax2;
  2183. var $skip_update_total; // Skip update price total for special lines
  2184. /**
  2185. * Class line Contructor
  2186. *
  2187. * @param DoliDB $db Database handler
  2188. */
  2189. function __construct($db = '') {
  2190. parent::__construct($db);
  2191. }
  2192. /**
  2193. * Retrieve the propal line object
  2194. *
  2195. * @param int $rowid Propal line id
  2196. * @return int <0 if KO, >0 if OK
  2197. */
  2198. function fetch($rowid) {
  2199. return parent::fetch($rowid);
  2200. // $sql = 'SELECT pd.rowid, pd.fk_propal, pd.fk_parent_line, pd.fk_product, pd.label as custom_label, pd.description, pd.price, pd.qty, pd.tva_tx,';
  2201. // $sql.= ' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice,';
  2202. // $sql.= ' pd.info_bits, pd.total_ht, pd.total_tva, pd.total_ttc, pd.fk_product_fournisseur_price as fk_fournprice, pd.buy_price_ht as pa_ht, pd.special_code, pd.rang,';
  2203. // $sql.= ' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,';
  2204. // $sql.= ' p.ref as product_ref, p.label as product_label, p.description as product_desc';
  2205. // $sql.= ' FROM ' . MAIN_DB_PREFIX . 'propaldet as pd';
  2206. // $sql.= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON pd.fk_product = p.rowid';
  2207. // $sql.= ' WHERE pd.rowid = ' . $rowid;
  2208. //
  2209. // $result = $this->db->query($sql);
  2210. // if ($result) {
  2211. // $objp = $this->db->fetch_object($result);
  2212. //
  2213. // $this->rowid = $objp->rowid;
  2214. // $this->fk_propal = $objp->fk_propal;
  2215. // $this->fk_parent_line = $objp->fk_parent_line;
  2216. // $this->label = $objp->custom_label;
  2217. // $this->desc = $objp->description;
  2218. // $this->qty = $objp->qty;
  2219. // $this->price = $objp->price; // deprecated
  2220. // $this->subprice = $objp->subprice;
  2221. // $this->tva_tx = $objp->tva_tx;
  2222. // $this->remise = $objp->remise;
  2223. // $this->remise_percent = $objp->remise_percent;
  2224. // $this->fk_remise_except = $objp->fk_remise_except;
  2225. // $this->fk_product = $objp->fk_product;
  2226. // $this->info_bits = $objp->info_bits;
  2227. //
  2228. // $this->total_ht = $objp->total_ht;
  2229. // $this->total_tva = $objp->total_tva;
  2230. // $this->total_ttc = $objp->total_ttc;
  2231. //
  2232. // $this->fk_fournprice = $objp->fk_fournprice;
  2233. //
  2234. // $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
  2235. // $this->pa_ht = $marginInfos[0];
  2236. // $this->marge_tx = $marginInfos[1];
  2237. // $this->marque_tx = $marginInfos[2];
  2238. //
  2239. // $this->special_code = $objp->special_code;
  2240. // $this->rang = $objp->rang;
  2241. //
  2242. // $this->ref = $objp->product_ref; // deprecated
  2243. // $this->product_ref = $objp->product_ref;
  2244. // $this->libelle = $objp->product_label; // deprecated
  2245. // $this->product_label = $objp->product_label;
  2246. // $this->product_desc = $objp->product_desc;
  2247. //
  2248. // $this->db->free($result);
  2249. // } else {
  2250. // dol_print_error($this->db);
  2251. // }
  2252. }
  2253. /**
  2254. * Insert object line propal in database
  2255. *
  2256. * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
  2257. * @return int <0 if KO, >0 if OK
  2258. */
  2259. function insert($notrigger = 0) {
  2260. global $conf, $langs, $user;
  2261. $error = 0;
  2262. dol_syslog("PropalLine::insert rang=" . $this->rang);
  2263. // Clean parameters
  2264. if (empty($this->tva_tx))
  2265. $this->tva_tx = 0;
  2266. if (empty($this->localtax1_tx))
  2267. $this->localtax1_tx = 0;
  2268. if (empty($this->localtax2_tx))
  2269. $this->localtax2_tx = 0;
  2270. if (empty($this->total_localtax1))
  2271. $this->total_localtax1 = 0;
  2272. if (empty($this->total_localtax2))
  2273. $this->total_localtax2 = 0;
  2274. if (empty($this->rang))
  2275. $this->rang = 0;
  2276. if (empty($this->remise))
  2277. $this->remise = 0;
  2278. if (empty($this->remise_percent))
  2279. $this->remise_percent = 0;
  2280. if (empty($this->info_bits))
  2281. $this->info_bits = 0;
  2282. if (empty($this->special_code))
  2283. $this->special_code = 0;
  2284. if (empty($this->fk_parent_line))
  2285. $this->fk_parent_line = 0;
  2286. if (empty($this->pa_ht))
  2287. $this->pa_ht = 0;
  2288. // si prix d'achat non renseigne et utilise pour calcul des marges alors prix achat = prix vente
  2289. if ($this->pa_ht == 0) {
  2290. if ($this->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1))
  2291. $this->pa_ht = $this->subprice * (1 - $this->remise_percent / 100);
  2292. }
  2293. // Check parameters
  2294. if ($this->product_type < 0)
  2295. return -1;
  2296. // $this->db->begin();
  2297. //
  2298. // // Insert line into database
  2299. // $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'propaldet';
  2300. // $sql.= ' (fk_propal, fk_parent_line, label, description, fk_product, product_type, fk_remise_except, qty, tva_tx, localtax1_tx, localtax2_tx,';
  2301. // $sql.= ' subprice, remise_percent, ';
  2302. // $sql.= ' info_bits, ';
  2303. // $sql.= ' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang)';
  2304. // $sql.= " VALUES (" . $this->fk_propal . ",";
  2305. // $sql.= " " . ($this->fk_parent_line > 0 ? "'" . $this->fk_parent_line . "'" : "null") . ",";
  2306. // $sql.= " " . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null") . ",";
  2307. // $sql.= " '" . $this->db->escape($this->desc) . "',";
  2308. // $sql.= " " . ($this->fk_product ? "'" . $this->fk_product . "'" : "null") . ",";
  2309. // $sql.= " '" . $this->product_type . "',";
  2310. // $sql.= " " . ($this->fk_remise_except ? "'" . $this->fk_remise_except . "'" : "null") . ",";
  2311. // $sql.= " " . price2num($this->qty) . ",";
  2312. // $sql.= " " . price2num($this->tva_tx) . ",";
  2313. // $sql.= " " . price2num($this->localtax1_tx) . ",";
  2314. // $sql.= " " . price2num($this->localtax2_tx) . ",";
  2315. // $sql.= " " . ($this->subprice ? price2num($this->subprice) : 'null') . ",";
  2316. // $sql.= " " . price2num($this->remise_percent) . ",";
  2317. // $sql.= " '" . $this->info_bits . "',";
  2318. // $sql.= " " . price2num($this->total_ht) . ",";
  2319. // $sql.= " " . price2num($this->total_tva) . ",";
  2320. // $sql.= " " . price2num($this->total_localtax1) . ",";
  2321. // $sql.= " " . price2num($this->total_localtax2) . ",";
  2322. // $sql.= " " . price2num($this->total_ttc) . ",";
  2323. // $sql.= " " . (isset($this->fk_fournprice) ? "'" . $this->fk_fournprice . "'" : "null") . ",";
  2324. // $sql.= " " . (isset($this->pa_ht) ? "'" . price2num($this->pa_ht) . "'" : "null") . ",";
  2325. // $sql.= ' ' . $this->special_code . ',';
  2326. // $sql.= ' ' . $this->rang;
  2327. // $sql.= ')';
  2328. //
  2329. // dol_syslog(get_class($this) . '::insert sql=' . $sql, LOG_DEBUG);
  2330. // $resql = $this->db->query($sql);
  2331. // if ($resql) {
  2332. // $this->rowid = $this->db->last_insert_id(MAIN_DB_PREFIX . 'propaldet');
  2333. $this->record();
  2334. if (!$notrigger) {
  2335. // Appel des triggers
  2336. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  2337. $interface = new Interfaces($this->db);
  2338. $result = $interface->run_triggers('LINEPROPAL_INSERT', $this, $user, $langs, $conf);
  2339. if ($result < 0) {
  2340. $error++;
  2341. $this->errors = $interface->errors;
  2342. }
  2343. // Fin appel triggers
  2344. }
  2345. // $this->db->commit();
  2346. return 1;
  2347. // } else {
  2348. // $this->error = $this->db->error() . " sql=" . $sql;
  2349. // dol_syslog(get_class($this) . '::insert Error ' . $this->error, LOG_ERR);
  2350. // $this->db->rollback();
  2351. // return -1;
  2352. // }
  2353. }
  2354. /**
  2355. * Delete line in database
  2356. *
  2357. * @return int <0 if ko, >0 if ok
  2358. */
  2359. function delete() {
  2360. global $conf, $langs, $user;
  2361. $error = 0;
  2362. $this->deleteDoc();
  2363. // $this->db->begin();
  2364. //
  2365. // $sql = "DELETE FROM " . MAIN_DB_PREFIX . "propaldet WHERE rowid = " . $this->rowid;
  2366. // dol_syslog("PropalLine::delete sql=" . $sql, LOG_DEBUG);
  2367. // if ($this->db->query($sql)) {
  2368. // Appel des triggers
  2369. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  2370. $interface = new Interfaces($this->db);
  2371. $result = $interface->run_triggers('LINEPROPAL_DELETE', $this, $user, $langs, $conf);
  2372. if ($result < 0) {
  2373. $error++;
  2374. $this->errors = $interface->errors;
  2375. }
  2376. // Fin appel triggers
  2377. // $this->db->commit();
  2378. return 1;
  2379. // } else {
  2380. // $this->error = $this->db->error() . " sql=" . $sql;
  2381. // dol_syslog("PropalLine::delete Error " . $this->error, LOG_ERR);
  2382. // $this->db->rollback();
  2383. // return -1;
  2384. // }
  2385. }
  2386. /**
  2387. * Update propal line object into DB
  2388. *
  2389. * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
  2390. * @return int <0 if ko, >0 if ok
  2391. */
  2392. function update($notrigger = 0) {
  2393. global $conf, $langs, $user;
  2394. $error = 0;
  2395. // Clean parameters
  2396. if (empty($this->tva_tx))
  2397. $this->tva_tx = 0;
  2398. if (empty($this->localtax1_tx))
  2399. $this->localtax1_tx = 0;
  2400. if (empty($this->localtax2_tx))
  2401. $this->localtax2_tx = 0;
  2402. if (empty($this->total_localtax1))
  2403. $this->total_localtax1 = 0;
  2404. if (empty($this->total_localtax2))
  2405. $this->total_localtax2 = 0;
  2406. if (empty($this->marque_tx))
  2407. $this->marque_tx = 0;
  2408. if (empty($this->marge_tx))
  2409. $this->marge_tx = 0;
  2410. if (empty($this->remise))
  2411. $this->remise = 0;
  2412. if (empty($this->remise_percent))
  2413. $this->remise_percent = 0;
  2414. if (empty($this->info_bits))
  2415. $this->info_bits = 0;
  2416. if (empty($this->special_code))
  2417. $this->special_code = 0;
  2418. if (empty($this->fk_parent_line))
  2419. $this->fk_parent_line = 0;
  2420. if (empty($this->pa_ht))
  2421. $this->pa_ht = 0;
  2422. // si prix d'achat non renseigne et utilise pour calcul des marges alors prix achat = prix vente
  2423. if ($this->pa_ht == 0) {
  2424. if ($this->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1))
  2425. $this->pa_ht = $this->subprice * (1 - $this->remise_percent / 100);
  2426. }
  2427. $this->record();
  2428. // $this->db->begin();
  2429. //
  2430. // // Mise a jour ligne en base
  2431. // $sql = "UPDATE " . MAIN_DB_PREFIX . "propaldet SET";
  2432. // $sql.= " description='" . $this->db->escape($this->desc) . "'";
  2433. // $sql.= " , label=" . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null");
  2434. // $sql.= " , tva_tx='" . price2num($this->tva_tx) . "'";
  2435. // $sql.= " , localtax1_tx=" . price2num($this->localtax1_tx);
  2436. // $sql.= " , localtax2_tx=" . price2num($this->localtax2_tx);
  2437. // $sql.= " , qty='" . price2num($this->qty) . "'";
  2438. // $sql.= " , subprice=" . price2num($this->subprice) . "";
  2439. // $sql.= " , remise_percent=" . price2num($this->remise_percent) . "";
  2440. // $sql.= " , price=" . price2num($this->price) . ""; // TODO A virer
  2441. // $sql.= " , remise=" . price2num($this->remise) . ""; // TODO A virer
  2442. // $sql.= " , info_bits='" . $this->info_bits . "'";
  2443. // if (empty($this->skip_update_total)) {
  2444. // $sql.= " , total_ht=" . price2num($this->total_ht) . "";
  2445. // $sql.= " , total_tva=" . price2num($this->total_tva) . "";
  2446. // $sql.= " , total_ttc=" . price2num($this->total_ttc) . "";
  2447. // $sql.= " , total_localtax1=" . price2num($this->total_localtax1) . "";
  2448. // $sql.= " , total_localtax2=" . price2num($this->total_localtax2) . "";
  2449. // }
  2450. // $sql.= " , fk_product_fournisseur_price='" . $this->fk_fournprice . "'";
  2451. // $sql.= " , buy_price_ht='" . price2num($this->pa_ht) . "'";
  2452. // $sql.= " , info_bits=" . $this->info_bits;
  2453. // if (strlen($this->special_code))
  2454. // $sql.= " , special_code=" . $this->special_code;
  2455. // $sql.= " , fk_parent_line=" . ($this->fk_parent_line > 0 ? $this->fk_parent_line : "null");
  2456. // if (!empty($this->rang))
  2457. // $sql.= ", rang=" . $this->rang;
  2458. // $sql.= " WHERE rowid = " . $this->rowid;
  2459. //
  2460. // dol_syslog(get_class($this) . "::update sql=" . $sql, LOG_DEBUG);
  2461. // $resql = $this->db->query($sql);
  2462. // if ($resql) {
  2463. if (!$notrigger) {
  2464. // Appel des triggers
  2465. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  2466. $interface = new Interfaces($this->db);
  2467. $result = $interface->run_triggers('LINEPROPAL_UPDATE', $this, $user, $langs, $conf);
  2468. if ($result < 0) {
  2469. $error++;
  2470. $this->errors = $interface->errors;
  2471. }
  2472. // Fin appel triggers
  2473. }
  2474. // $this->db->commit();
  2475. return 1;
  2476. // } else {
  2477. // $this->error = $this->db->error();
  2478. // dol_syslog(get_class($this) . "::update Error " . $this->error, LOG_ERR);
  2479. // $this->db->rollback();
  2480. // return -2;
  2481. // }
  2482. }
  2483. /**
  2484. * Update DB line fields total_xxx
  2485. * Used by migration
  2486. *
  2487. * @return int <0 if ko, >0 if ok
  2488. */
  2489. function update_total() {
  2490. $this->db->begin();
  2491. // Mise a jour ligne en base
  2492. $sql = "UPDATE " . MAIN_DB_PREFIX . "propaldet SET";
  2493. $sql.= " total_ht=" . price2num($this->total_ht, 'MT') . "";
  2494. $sql.= ",total_tva=" . price2num($this->total_tva, 'MT') . "";
  2495. $sql.= ",total_ttc=" . price2num($this->total_ttc, 'MT') . "";
  2496. $sql.= " WHERE rowid = " . $this->rowid;
  2497. dol_syslog("PropalLine::update_total sql=$sql");
  2498. $resql = $this->db->query($sql);
  2499. if ($resql) {
  2500. $this->db->commit();
  2501. return 1;
  2502. } else {
  2503. $this->error = $this->db->error();
  2504. dol_syslog("PropalLine::update_total Error " . $this->error, LOG_ERR);
  2505. $this->db->rollback();
  2506. return -2;
  2507. }
  2508. }
  2509. }
  2510. ?>