discount.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2018 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/class/discount.class.php
  20. * \ingroup core propal facture commande
  21. * \brief File of class to manage absolute discounts
  22. */
  23. /**
  24. * Class to manage absolute discounts
  25. */
  26. class DiscountAbsolute
  27. {
  28. /**
  29. * @var DoliDB Database handler.
  30. */
  31. public $db;
  32. /**
  33. * @var string Error code (or message)
  34. */
  35. public $error;
  36. /**
  37. * @var string[] Array of error strings
  38. */
  39. public $errors=array();
  40. /**
  41. * @var int ID discount
  42. */
  43. public $id;
  44. /**
  45. * @var int Thirdparty ID
  46. */
  47. public $fk_soc;
  48. public $discount_type; // 0 => customer discount, 1 => supplier discount
  49. public $amount_ht; //
  50. public $amount_tva; //
  51. public $amount_ttc; //
  52. public $tva_tx; // Vat rate
  53. /**
  54. * @var int User ID Id utilisateur qui accorde la remise
  55. */
  56. public $fk_user;
  57. /**
  58. * @var string description
  59. */
  60. public $description;
  61. public $datec; // Date creation
  62. /**
  63. * @var int ID invoice line when a discount is used into an invoice line (for absolute discounts)
  64. */
  65. public $fk_facture_line;
  66. /**
  67. * @var int ID invoice when a discount line is used into an invoice (for credit note)
  68. */
  69. public $fk_facture;
  70. /**
  71. * @var int ID credit note having caused the discount
  72. */
  73. public $fk_facture_source;
  74. public $ref_facture_source; // Ref credit note having caused the discount
  75. public $ref_invoice_supplier_source;
  76. /**
  77. * Constructor
  78. *
  79. * @param DoliDB $db Database handler
  80. */
  81. function __construct($db)
  82. {
  83. $this->db = $db;
  84. }
  85. /**
  86. * Load object from database into memory
  87. *
  88. * @param int $rowid id discount to load
  89. * @param int $fk_facture_source fk_facture_source
  90. * @param int $fk_invoice_supplier_source fk_invoice_supplier_source
  91. * @return int <0 if KO, =0 if not found, >0 if OK
  92. */
  93. function fetch($rowid, $fk_facture_source=0, $fk_invoice_supplier_source=0)
  94. {
  95. global $conf;
  96. // Check parameters
  97. if (! $rowid && ! $fk_facture_source && ! $fk_invoice_supplier_source)
  98. {
  99. $this->error='ErrorBadParameters';
  100. return -1;
  101. }
  102. $sql = "SELECT sr.rowid, sr.fk_soc, sr.discount_type,";
  103. $sql.= " sr.fk_user,";
  104. $sql.= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx,";
  105. $sql.= " sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
  106. $sql.= " sr.fk_facture_line, sr.fk_facture, sr.fk_facture_source, sr.fk_invoice_supplier_line, sr.fk_invoice_supplier, sr.fk_invoice_supplier_source, sr.description,";
  107. $sql.= " sr.datec,";
  108. $sql.= " f.facnumber as ref_facture_source, fsup.facnumber as ref_invoice_supplier_source";
  109. $sql.= " FROM ".MAIN_DB_PREFIX."societe_remise_except as sr";
  110. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON sr.fk_facture_source = f.rowid";
  111. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fsup ON sr.fk_invoice_supplier_source = fsup.rowid";
  112. $sql.= " WHERE sr.entity = " . $conf->entity;
  113. if ($rowid) $sql.= " AND sr.rowid=".$rowid;
  114. if ($fk_facture_source) $sql.= " AND sr.fk_facture_source=".$fk_facture_source;
  115. if ($fk_invoice_supplier_source) $sql.= " AND sr.fk_invoice_supplier_source=".$fk_invoice_supplier_source;
  116. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  117. $resql = $this->db->query($sql);
  118. if ($resql)
  119. {
  120. if ($this->db->num_rows($resql))
  121. {
  122. $obj = $this->db->fetch_object($resql);
  123. $this->id = $obj->rowid;
  124. $this->fk_soc = $obj->fk_soc;
  125. $this->discount_type = $obj->discount_type;
  126. $this->amount_ht = $obj->amount_ht;
  127. $this->amount_tva = $obj->amount_tva;
  128. $this->amount_ttc = $obj->amount_ttc;
  129. $this->multicurrency_amount_ht = $this->multicurrency_subprice = $obj->multicurrency_amount_ht;
  130. $this->multicurrency_amount_tva = $obj->multicurrency_amount_tva;
  131. $this->multicurrency_amount_ttc = $obj->multicurrency_amount_ttc;
  132. $this->tva_tx = $obj->tva_tx;
  133. $this->fk_user = $obj->fk_user;
  134. $this->fk_facture_line = $obj->fk_facture_line;
  135. $this->fk_facture = $obj->fk_facture;
  136. $this->fk_facture_source = $obj->fk_facture_source; // Id avoir source
  137. $this->ref_facture_source = $obj->ref_facture_source; // Ref avoir source
  138. $this->fk_invoice_supplier_line = $obj->fk_invoice_supplier_line;
  139. $this->fk_invoice_supplier = $obj->fk_invoice_supplier;
  140. $this->fk_invoice_supplier_source = $obj->fk_invoice_supplier_source; // Id avoir source
  141. $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source; // Ref avoir source
  142. $this->description = $obj->description;
  143. $this->datec = $this->db->jdate($obj->datec);
  144. $this->db->free($resql);
  145. return 1;
  146. }
  147. else
  148. {
  149. $this->db->free($resql);
  150. return 0;
  151. }
  152. }
  153. else
  154. {
  155. $this->error=$this->db->error();
  156. return -1;
  157. }
  158. }
  159. /**
  160. * Create a discount into database
  161. *
  162. * @param User $user User that create
  163. * @return int <0 if KO, >0 if OK
  164. */
  165. function create($user)
  166. {
  167. global $conf, $langs;
  168. // Clean parameters
  169. $this->amount_ht=price2num($this->amount_ht);
  170. $this->amount_tva=price2num($this->amount_tva);
  171. $this->amount_ttc=price2num($this->amount_ttc);
  172. $this->tva_tx=price2num($this->tva_tx);
  173. $this->multicurrency_amount_ht=price2num($this->multicurrency_amount_ht);
  174. $this->multicurrency_amount_tva=price2num($this->multicurrency_amount_tva);
  175. $this->multicurrency_amount_ttc=price2num($this->multicurrency_amount_ttc);
  176. if (empty($this->multicurrency_amount_ht)) $this->multicurrency_amount_ht=0;
  177. if (empty($this->multicurrency_amount_tva)) $this->multicurrency_amount_tva=0;
  178. if (empty($this->multicurrency_amount_ttc)) $this->multicurrency_amount_ttc=0;
  179. // Check parameters
  180. if (empty($this->description))
  181. {
  182. $this->error='BadValueForPropertyDescription';
  183. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  184. return -1;
  185. }
  186. // Insert request
  187. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise_except";
  188. $sql.= " (entity, datec, fk_soc, discount_type, fk_user, description,";
  189. $sql.= " amount_ht, amount_tva, amount_ttc, tva_tx,";
  190. $sql.= " multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,";
  191. $sql.= " fk_facture_source, fk_invoice_supplier_source";
  192. $sql.= ")";
  193. $sql.= " VALUES (".$conf->entity.", '".$this->db->idate($this->datec!=''?$this->datec:dol_now())."', ".$this->fk_soc.", ".(empty($this->discount_type)?0:intval($this->discount_type)).", ".$user->id.", '".$this->db->escape($this->description)."',";
  194. $sql.= " ".$this->amount_ht.", ".$this->amount_tva.", ".$this->amount_ttc.", ".$this->tva_tx.",";
  195. $sql.= " ".$this->multicurrency_amount_ht.", ".$this->multicurrency_amount_tva.", ".$this->multicurrency_amount_ttc.", ";
  196. $sql.= " ".($this->fk_facture_source ? "'".$this->db->escape($this->fk_facture_source)."'":"null").",";
  197. $sql.= " ".($this->fk_invoice_supplier_source ? "'".$this->db->escape($this->fk_invoice_supplier_source)."'":"null");
  198. $sql.= ")";
  199. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  200. $resql=$this->db->query($sql);
  201. if ($resql)
  202. {
  203. $this->id=$this->db->last_insert_id(MAIN_DB_PREFIX."societe_remise_except");
  204. return $this->id;
  205. }
  206. else
  207. {
  208. $this->error=$this->db->lasterror().' - sql='.$sql;
  209. return -1;
  210. }
  211. }
  212. /**
  213. * Delete object in database. If fk_facture_source is defined, we delete all familiy with same fk_facture_source. If not, only with id is removed
  214. *
  215. * @param User $user Object of user asking to delete
  216. * @return int <0 if KO, >0 if OK
  217. */
  218. function delete($user)
  219. {
  220. global $conf, $langs;
  221. // Check if we can remove the discount
  222. if ($this->fk_facture_source)
  223. {
  224. $sql="SELECT COUNT(rowid) as nb";
  225. $sql.=" FROM ".MAIN_DB_PREFIX."societe_remise_except";
  226. $sql.=" WHERE (fk_facture_line IS NOT NULL"; // Not used as absolute simple discount
  227. $sql.=" OR fk_facture IS NOT NULL)"; // Not used as credit note and not used as deposit
  228. $sql.=" AND fk_facture_source = ".$this->fk_facture_source;
  229. //$sql.=" AND rowid != ".$this->id;
  230. dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
  231. $resql=$this->db->query($sql);
  232. if ($resql)
  233. {
  234. $obj = $this->db->fetch_object($resql);
  235. if ($obj->nb > 0)
  236. {
  237. $this->error='ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
  238. return -2;
  239. }
  240. }
  241. else
  242. {
  243. dol_print_error($this->db);
  244. return -1;
  245. }
  246. }
  247. // Check if we can remove the discount
  248. if ($this->fk_invoice_supplier_source)
  249. {
  250. $sql="SELECT COUNT(rowid) as nb";
  251. $sql.=" FROM ".MAIN_DB_PREFIX."societe_remise_except";
  252. $sql.=" WHERE (fk_invoice_supplier_line IS NOT NULL"; // Not used as absolute simple discount
  253. $sql.=" OR fk_invoice_supplier IS NOT NULL)"; // Not used as credit note and not used as deposit
  254. $sql.=" AND fk_invoice_supplier_source = ".$this->fk_invoice_supplier_source;
  255. //$sql.=" AND rowid != ".$this->id;
  256. dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
  257. $resql=$this->db->query($sql);
  258. if ($resql)
  259. {
  260. $obj = $this->db->fetch_object($resql);
  261. if ($obj->nb > 0)
  262. {
  263. $this->error='ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
  264. return -2;
  265. }
  266. }
  267. else
  268. {
  269. dol_print_error($this->db);
  270. return -1;
  271. }
  272. }
  273. $this->db->begin();
  274. // Delete but only if not used
  275. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_remise_except ";
  276. if ($this->fk_facture_source) $sql.= " WHERE fk_facture_source = ".$this->fk_facture_source; // Delete all lines of same serie
  277. elseif ($this->fk_invoice_supplier_source) $sql.= " WHERE fk_invoice_supplier_source = ".$this->fk_invoice_supplier_source; // Delete all lines of same serie
  278. else $sql.= " WHERE rowid = ".$this->id; // Delete only line
  279. $sql.= " AND (fk_facture_line IS NULL"; // Not used as absolute simple discount
  280. $sql.= " AND fk_facture IS NULL)"; // Not used as credit note and not used as deposit
  281. $sql.= " AND (fk_invoice_supplier_line IS NULL"; // Not used as absolute simple discount
  282. $sql.= " AND fk_invoice_supplier IS NULL)"; // Not used as credit note and not used as deposit
  283. dol_syslog(get_class($this)."::delete Delete discount", LOG_DEBUG);
  284. $result=$this->db->query($sql);
  285. if ($result)
  286. {
  287. // If source of discount was a credit note or deposit, we change source statut.
  288. if ($this->fk_facture_source)
  289. {
  290. $sql = "UPDATE ".MAIN_DB_PREFIX."facture";
  291. $sql.=" set paye=0, fk_statut=1";
  292. $sql.=" WHERE (type = 2 or type = 3) AND rowid=".$this->fk_facture_source;
  293. dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
  294. $result=$this->db->query($sql);
  295. if ($result)
  296. {
  297. $this->db->commit();
  298. return 1;
  299. }
  300. else
  301. {
  302. $this->error=$this->db->lasterror();
  303. $this->db->rollback();
  304. return -1;
  305. }
  306. }
  307. elseif($this->fk_invoice_supplier_source) {
  308. $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn";
  309. $sql.=" set paye=0, fk_statut=1";
  310. $sql.=" WHERE (type = 2 or type = 3) AND rowid=".$this->fk_invoice_supplier_source;
  311. dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
  312. $result=$this->db->query($sql);
  313. if ($result)
  314. {
  315. $this->db->commit();
  316. return 1;
  317. }
  318. else
  319. {
  320. $this->error=$this->db->lasterror();
  321. $this->db->rollback();
  322. return -1;
  323. }
  324. }
  325. else
  326. {
  327. $this->db->commit();
  328. return 1;
  329. }
  330. }
  331. else
  332. {
  333. $this->error=$this->db->lasterror();
  334. $this->db->rollback();
  335. return -1;
  336. }
  337. }
  338. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  339. /**
  340. * Link the discount to a particular invoice line or a particular invoice.
  341. * When discount is a global discount used as an invoice line, we link using rowidline.
  342. * When discount is from a credit note used to reduce payment of an invoice, we link using rowidinvoice
  343. *
  344. * @param int $rowidline Invoice line id (To use discount into invoice lines)
  345. * @param int $rowidinvoice Invoice id (To use discount as a credit note to reduc payment of invoice)
  346. * @return int <0 if KO, >0 if OK
  347. */
  348. function link_to_invoice($rowidline,$rowidinvoice)
  349. {
  350. // phpcs:enable
  351. // Check parameters
  352. if (! $rowidline && ! $rowidinvoice)
  353. {
  354. $this->error='ErrorBadParameters';
  355. return -1;
  356. }
  357. if ($rowidline && $rowidinvoice)
  358. {
  359. $this->error='ErrorBadParameters';
  360. return -2;
  361. }
  362. $sql ="UPDATE ".MAIN_DB_PREFIX."societe_remise_except";
  363. if(! empty($this->discount_type)) {
  364. if ($rowidline) $sql.=" SET fk_invoice_supplier_line = ".$rowidline;
  365. if ($rowidinvoice) $sql.=" SET fk_invoice_supplier = ".$rowidinvoice;
  366. } else {
  367. if ($rowidline) $sql.=" SET fk_facture_line = ".$rowidline;
  368. if ($rowidinvoice) $sql.=" SET fk_facture = ".$rowidinvoice;
  369. }
  370. $sql.=" WHERE rowid = ".$this->id;
  371. dol_syslog(get_class($this)."::link_to_invoice", LOG_DEBUG);
  372. $resql = $this->db->query($sql);
  373. if ($resql)
  374. {
  375. if(! empty($this->discount_type)) {
  376. $this->fk_invoice_supplier_line=$rowidline;
  377. $this->fk_invoice_supplier=$rowidinvoice;
  378. } else {
  379. $this->fk_facture_line=$rowidline;
  380. $this->fk_facture=$rowidinvoice;
  381. }
  382. return 1;
  383. }
  384. else
  385. {
  386. $this->error=$this->db->error();
  387. return -3;
  388. }
  389. }
  390. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  391. /**
  392. * Link the discount to a particular invoice line or a particular invoice.
  393. * Do not call this if discount is linked to a reconcialiated invoice
  394. *
  395. * @return int <0 if KO, >0 if OK
  396. */
  397. function unlink_invoice()
  398. {
  399. // phpcs:enable
  400. $sql ="UPDATE ".MAIN_DB_PREFIX."societe_remise_except";
  401. if(! empty($this->discount_type)) {
  402. $sql.=" SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
  403. } else {
  404. $sql.=" SET fk_facture_line = NULL, fk_facture = NULL";
  405. }
  406. $sql.=" WHERE rowid = ".$this->id;
  407. dol_syslog(get_class($this)."::unlink_invoice", LOG_DEBUG);
  408. $resql = $this->db->query($sql);
  409. if ($resql)
  410. {
  411. return 1;
  412. }
  413. else
  414. {
  415. $this->error=$this->db->error();
  416. return -3;
  417. }
  418. }
  419. /**
  420. * Return amount (with tax) of discounts currently available for a company, user or other criteria
  421. *
  422. * @param Societe $company Object third party for filter
  423. * @param User $user Filtre sur un user auteur des remises
  424. * @param string $filter Filtre autre
  425. * @param int $maxvalue Filter on max value for discount
  426. * @param int $discount_type 0 => customer discount, 1 => supplier discount
  427. * @return int <0 if KO, amount otherwise
  428. */
  429. function getAvailableDiscounts($company='', $user='',$filter='', $maxvalue=0, $discount_type=0)
  430. {
  431. global $conf;
  432. $sql = "SELECT SUM(rc.amount_ttc) as amount";
  433. //$sql = "SELECT rc.amount_ttc as amount";
  434. $sql.= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
  435. $sql.= " WHERE rc.entity = " . $conf->entity;
  436. $sql.= " AND rc.discount_type=".intval($discount_type);
  437. if (! empty($discount_type)) {
  438. $sql.= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)"; // Available from supplier
  439. } else {
  440. $sql.= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available to customer
  441. }
  442. if (is_object($company)) $sql.= " AND rc.fk_soc = ".$company->id;
  443. if (is_object($user)) $sql.= " AND rc.fk_user = ".$user->id;
  444. if ($filter) $sql.=' AND ('.$filter.')';
  445. if ($maxvalue) $sql.=' AND rc.amount_ttc <= '.price2num($maxvalue);
  446. dol_syslog(get_class($this)."::getAvailableDiscounts", LOG_DEBUG);
  447. $resql=$this->db->query($sql);
  448. if ($resql)
  449. {
  450. $obj = $this->db->fetch_object($resql);
  451. //while ($obj)
  452. //{
  453. //print 'zz'.$obj->amount;
  454. //$obj = $this->db->fetch_object($resql);
  455. //}
  456. return $obj->amount;
  457. }
  458. return -1;
  459. }
  460. /**
  461. * Return amount (with tax) of all deposits invoices used by invoice as a payment.
  462. * Should always be empty, except if option FACTURE_DEPOSITS_ARE_JUST_PAYMENTS is on (not recommended).
  463. *
  464. * @param CommonInvoice $invoice Object invoice (customer of supplier)
  465. * @param int $multicurrency Return multicurrency_amount instead of amount
  466. * @return int <0 if KO, Sum of credit notes and deposits amount otherwise
  467. */
  468. function getSumDepositsUsed($invoice, $multicurrency=0)
  469. {
  470. dol_syslog(get_class($this)."::getSumDepositsUsed", LOG_DEBUG);
  471. if ($invoice->element == 'facture' || $invoice->element == 'invoice')
  472. {
  473. $sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount';
  474. $sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc, '.MAIN_DB_PREFIX.'facture as f';
  475. $sql.= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.$invoice->id;
  476. $sql.= ' AND f.type = 3';
  477. }
  478. else if ($invoice->element == 'invoice_supplier')
  479. {
  480. $sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount';
  481. $sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc, '.MAIN_DB_PREFIX.'facture_fourn as f';
  482. $sql.= ' WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = '.$invoice->id;
  483. $sql.= ' AND f.type = 3';
  484. }
  485. else
  486. {
  487. $this->error=get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
  488. dol_print_error($this->error);
  489. return -1;
  490. }
  491. $resql=$this->db->query($sql);
  492. if ($resql)
  493. {
  494. $obj = $this->db->fetch_object($resql);
  495. if ($multicurrency) return $obj->multicurrency_amount;
  496. else return $obj->amount;
  497. }
  498. else
  499. {
  500. $this->error = $this->db->lasterror();
  501. return -1;
  502. }
  503. }
  504. /**
  505. * Return amount (with tax) of all credit notes invoices + excess received used by invoice as a payment
  506. *
  507. * @param CommonInvoice $invoice Object invoice
  508. * @param int $multicurrency Return multicurrency_amount instead of amount
  509. * @return int <0 if KO, Sum of credit notes and excess received amount otherwise
  510. */
  511. function getSumCreditNotesUsed($invoice, $multicurrency=0)
  512. {
  513. dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
  514. if ($invoice->element == 'facture' || $invoice->element == 'invoice')
  515. {
  516. $sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount';
  517. $sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc, '.MAIN_DB_PREFIX.'facture as f';
  518. $sql.= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.$invoice->id;
  519. $sql.= ' AND (f.type = 2 OR f.type = 0)'; // Find discount coming from credit note or excess received
  520. }
  521. else if ($invoice->element == 'invoice_supplier')
  522. {
  523. $sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount';
  524. $sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc, '.MAIN_DB_PREFIX.'facture_fourn as f';
  525. $sql.= ' WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = '.$invoice->id;
  526. $sql.= ' AND (f.type = 2 OR f.type = 0)'; // Find discount coming from credit note or excess paid
  527. }
  528. else
  529. {
  530. $this->error=get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
  531. dol_print_error($this->error);
  532. return -1;
  533. }
  534. $resql=$this->db->query($sql);
  535. if ($resql)
  536. {
  537. $obj = $this->db->fetch_object($resql);
  538. if ($multicurrency) return $obj->multicurrency_amount;
  539. else return $obj->amount;
  540. }
  541. else
  542. {
  543. $this->error = $this->db->lasterror();
  544. return -1;
  545. }
  546. }
  547. /**
  548. * Return clickable ref of object (with picto or not)
  549. *
  550. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Picto only
  551. * @param string $option Where to link to ('invoice' or 'discount')
  552. * @return string String with URL
  553. */
  554. function getNomUrl($withpicto,$option='invoice')
  555. {
  556. global $langs;
  557. $result='';
  558. if ($option == 'invoice') {
  559. $facid=! empty($this->discount_type)?$this->fk_invoice_supplier_source:$this->fk_facture_source;
  560. $link=! empty($this->discount_type)?'/fourn/facture/card.php':'/compta/facture/card.php';
  561. $label=$langs->trans("ShowDiscount").': '.$this->ref_facture_source;
  562. $link = '<a href="'.DOL_URL_ROOT.$link.'?facid='.$facid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  563. $linkend='</a>';
  564. $ref=! empty($this->discount_type)?$this->ref_invoice_supplier_source:$this->ref_facture_source;
  565. $picto='bill';
  566. }
  567. if ($option == 'discount') {
  568. $label=$langs->trans("Discount");
  569. $link = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->fk_soc.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  570. $linkend='</a>';
  571. $ref=$langs->trans("Discount");
  572. $picto='generic';
  573. }
  574. if ($withpicto) $result.=($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
  575. if ($withpicto && $withpicto != 2) $result.=' ';
  576. $result.=$link.$ref.$linkend;
  577. return $result;
  578. }
  579. /**
  580. * Initialise an instance with random values.
  581. * Used to build previews or test instances.
  582. * id must be 0 if object instance is a specimen.
  583. *
  584. * @return void
  585. */
  586. function initAsSpecimen()
  587. {
  588. global $user,$langs,$conf;
  589. $this->fk_soc = 1;
  590. $this->amount_ht = 10;
  591. $this->amount_tva = 1.96;
  592. $this->amount_ttc = 11.96;
  593. $this->tva_tx = 19.6;
  594. $this->description = 'Specimen discount';
  595. }
  596. }