discount.class.php 26 KB

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