PageRenderTime 56ms CodeModel.GetById 20ms 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

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

  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;

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