discount.class.php 24 KB

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