discount.class.php 19 KB

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