PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/class/discount.class.php

https://github.com/asterix14/dolibarr
PHP | 483 lines | 320 code | 49 blank | 114 comment | 40 complexity | e88635c14008e6787ba9b618df4d01a6 MD5 | raw file
Possible License(s): LGPL-2.0
  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 2 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. var $db;
  30. var $error;
  31. var $id; // Id discount
  32. var $fk_soc;
  33. var $amount_ht; //
  34. var $amount_tva; //
  35. var $amount_ttc; //
  36. var $tva_tx; // Vat rate
  37. var $fk_user; // Id utilisateur qui accorde la remise
  38. var $description; // Description libre
  39. var $datec; // Date creation
  40. var $fk_facture_line; // Id invoice line when a discount linked to invoice line (for absolute discounts)
  41. var $fk_facture; // Id invoice when a discoutn linked to invoice (for credit note)
  42. var $fk_facture_source; // Id facture avoir a l'origine de la remise
  43. var $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 DiscountAbsolute($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. // Check parameters
  63. if (! $rowid && ! $fk_facture_source)
  64. {
  65. $this->error='ErrorBadParameters';
  66. return -1;
  67. }
  68. $sql = "SELECT sr.rowid, sr.fk_soc,";
  69. $sql.= " sr.fk_user,";
  70. $sql.= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx,";
  71. $sql.= " sr.fk_facture_line, sr.fk_facture, sr.fk_facture_source, sr.description,";
  72. $sql.= " sr.datec,";
  73. $sql.= " f.facnumber as ref_facture_source";
  74. $sql.= " FROM ".MAIN_DB_PREFIX."societe_remise_except as sr";
  75. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON sr.fk_facture_source = f.rowid";
  76. $sql.= " WHERE";
  77. if ($rowid) $sql.= " sr.rowid=".$rowid;
  78. if ($fk_facture_source) $sql.= " sr.fk_facture_source=".$fk_facture_source;
  79. dol_syslog(get_class($this)."::fetch sql=".$sql);
  80. $resql = $this->db->query($sql);
  81. if ($resql)
  82. {
  83. if ($this->db->num_rows($resql))
  84. {
  85. $obj = $this->db->fetch_object($resql);
  86. $this->id = $obj->rowid;
  87. $this->fk_soc = $obj->fk_soc;
  88. $this->amount_ht = $obj->amount_ht;
  89. $this->amount_tva = $obj->amount_tva;
  90. $this->amount_ttc = $obj->amount_ttc;
  91. $this->tva_tx = $obj->tva_tx;
  92. $this->fk_user = $obj->fk_user;
  93. $this->fk_facture_line = $obj->fk_facture_line;
  94. $this->fk_facture = $obj->fk_facture;
  95. $this->fk_facture_source = $obj->fk_facture_source; // Id avoir source
  96. $this->ref_facture_source = $obj->ref_facture_source; // Ref avoir source
  97. $this->description = $obj->description;
  98. $this->datec = $this->db->jdate($obj->datec);
  99. $this->db->free($resql);
  100. return 1;
  101. }
  102. else
  103. {
  104. $this->db->free($resql);
  105. return 0;
  106. }
  107. }
  108. else
  109. {
  110. $this->error=$this->db->error();
  111. return -1;
  112. }
  113. }
  114. /**
  115. * Create a discount into database
  116. *
  117. * @param User $user User that create
  118. * @return int <0 if KO, >0 if OK
  119. */
  120. function create($user)
  121. {
  122. global $conf, $langs;
  123. // Clean parameters
  124. $this->amount_ht=price2num($this->amount_ht);
  125. $this->amount_tva=price2num($this->amount_tva);
  126. $this->amount_ttc=price2num($this->amount_ttc);
  127. $this->tva_tx=price2num($this->tva_tx);
  128. // Check parameters
  129. if (empty($this->description))
  130. {
  131. $this->error='BadValueForPropertyDescription';
  132. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  133. return -1;
  134. }
  135. // Insert request
  136. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise_except";
  137. $sql.= " (datec, fk_soc, fk_user, description,";
  138. $sql.= " amount_ht, amount_tva, amount_ttc, tva_tx,";
  139. $sql.= " fk_facture_source";
  140. $sql.= ")";
  141. $sql.= " VALUES (".$this->db->idate($this->datec!=''?$this->datec:dol_now()).", ".$this->fk_soc.", ".$user->id.", '".$this->db->escape($this->description)."',";
  142. $sql.= " ".$this->amount_ht.", ".$this->amount_tva.", ".$this->amount_ttc.", ".$this->tva_tx.",";
  143. $sql.= " ".($this->fk_facture_source?"'".$this->fk_facture_source."'":"null");
  144. $sql.= ")";
  145. dol_syslog(get_class($this)."::create sql=".$sql);
  146. $resql=$this->db->query($sql);
  147. if ($resql)
  148. {
  149. $this->id=$this->db->last_insert_id(MAIN_DB_PREFIX."societe_remise_except");
  150. return $this->id;
  151. }
  152. else
  153. {
  154. $this->error=$this->db->lasterror().' - sql='.$sql;
  155. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  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 sql=".$sql);
  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($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 sql=".$sql);
  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 sql=".$sql);
  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 sql=".$sql,LOG_DEBUG);
  265. $resql = $this->db->query($sql);
  266. if ($resql)
  267. {
  268. $this->fk_facture_source=$rowidline;
  269. $this->fk_facture=$rowidinvoice;
  270. return 1;
  271. }
  272. else
  273. {
  274. $this->error=$this->db->error();
  275. dol_syslog(get_class($this)."::link_to_invoice ".$this->error,LOG_ERR);
  276. return -3;
  277. }
  278. }
  279. /**
  280. * Link the discount to a particular invoice line or a particular invoice.
  281. * Do not call this if discount is linked to a reconcialiated invoice
  282. *
  283. * @return int <0 if KO, >0 if OK
  284. */
  285. function unlink_invoice()
  286. {
  287. $sql ="UPDATE ".MAIN_DB_PREFIX."societe_remise_except";
  288. $sql.=" SET fk_facture_line = NULL, fk_facture = NULL";
  289. $sql.=" WHERE rowid = ".$this->id;
  290. dol_syslog(get_class($this)."::unlink_invoice sql=".$sql,LOG_DEBUG);
  291. $resql = $this->db->query($sql);
  292. if ($resql)
  293. {
  294. return 1;
  295. }
  296. else
  297. {
  298. $this->error=$this->db->error();
  299. dol_syslog(get_class($this)."::unlink_invoice ".$this->error,LOG_ERR);
  300. return -3;
  301. }
  302. }
  303. /**
  304. * Renvoie montant TTC des reductions/avoirs en cours disponibles pour une sociĂŠtĂŠ, un user ou autre
  305. *
  306. * @param Societe $company Object third party for filter
  307. * @param User $user Filtre sur un user auteur des remises
  308. * @param string $filter Filtre autre
  309. * @param int $maxvalue Filter on max value for discount
  310. * @return int <0 if KO, amount otherwise
  311. */
  312. function getAvailableDiscounts($company='', $user='',$filter='', $maxvalue=0)
  313. {
  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.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available
  318. if (is_object($company)) $sql.= " AND rc.fk_soc = ".$company->id;
  319. if (is_object($user)) $sql.= " AND rc.fk_user = ".$user->id;
  320. if ($filter) $sql.=' AND ('.$filter.')';
  321. if ($maxvalue) $sql.=' AND rc.amount_ttc <= '.price2num($maxvalue);
  322. dol_syslog(get_class($this)."::getAvailableDiscounts sql=".$sql,LOG_DEBUG);
  323. $resql=$this->db->query($sql);
  324. if ($resql)
  325. {
  326. $obj = $this->db->fetch_object($resql);
  327. //while ($obj)
  328. //{
  329. //print 'zz'.$obj->amount;
  330. //$obj = $this->db->fetch_object($resql);
  331. //}
  332. return $obj->amount;
  333. }
  334. return -1;
  335. }
  336. /**
  337. * Return amount (with tax) of all credit notes and deposits invoices used by invoice
  338. *
  339. * @param Facture $invoice Object invoice
  340. * @return int <0 if KO, Sum of credit notes and deposits amount otherwise
  341. */
  342. function getSumCreditNotesUsed($invoice)
  343. {
  344. $sql = 'SELECT sum(rc.amount_ttc) as amount';
  345. $sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc, '.MAIN_DB_PREFIX.'facture as f';
  346. $sql.= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.$invoice->id;
  347. $sql.= ' AND f.type = 2';
  348. dol_syslog(get_class($this)."::getSumCreditNotesUsed sql=".$sql,LOG_DEBUG);
  349. $resql=$this->db->query($sql);
  350. if ($resql)
  351. {
  352. $obj = $this->db->fetch_object($resql);
  353. return $obj->amount;
  354. }
  355. else
  356. {
  357. return -1;
  358. }
  359. }
  360. /**
  361. * Return amount (with tax) of all deposits invoices used by invoice
  362. *
  363. * @param Facture $invoice Object invoice
  364. * @return int <0 if KO, Sum of credit notes and deposits amount otherwise
  365. */
  366. function getSumDepositsUsed($invoice)
  367. {
  368. $sql = 'SELECT sum(rc.amount_ttc) as amount';
  369. $sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc, '.MAIN_DB_PREFIX.'facture as f';
  370. $sql.= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.$invoice->id;
  371. $sql.= ' AND f.type = 3';
  372. dol_syslog(get_class($this)."::getSumDepositsUsed sql=".$sql,LOG_DEBUG);
  373. $resql=$this->db->query($sql);
  374. if ($resql)
  375. {
  376. $obj = $this->db->fetch_object($resql);
  377. return $obj->amount;
  378. }
  379. else
  380. {
  381. return -1;
  382. }
  383. }
  384. /**
  385. * Return clickable ref of object (with picto or not)
  386. *
  387. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Picto only
  388. * @param string $option Where to link to ('invoice' or 'discount')
  389. * @return string String with URL
  390. */
  391. function getNomUrl($withpicto,$option='invoice')
  392. {
  393. global $langs;
  394. $result='';
  395. if ($option == 'invoice')
  396. {
  397. $lien = '<a href="'.DOL_URL_ROOT.'/compta/facture.php?facid='.$this->fk_facture_source.'">';
  398. $lienfin='</a>';
  399. $label=$langs->trans("ShowDiscount").': '.$this->ref_facture_source;
  400. $ref=$this->ref_facture_source;
  401. $picto='bill';
  402. }
  403. if ($option == 'discount')
  404. {
  405. $lien = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->fk_soc.'">';
  406. $lienfin='</a>';
  407. $label=$langs->trans("Discount");
  408. $ref=$langs->trans("Discount");
  409. $picto='generic';
  410. }
  411. if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
  412. if ($withpicto && $withpicto != 2) $result.=' ';
  413. $result.=$lien.$ref.$lienfin;
  414. return $result;
  415. }
  416. /**
  417. * Initialise an instance with random values.
  418. * Used to build previews or test instances.
  419. * id must be 0 if object instance is a specimen.
  420. *
  421. * @return void
  422. */
  423. function initAsSpecimen()
  424. {
  425. global $user,$langs,$conf;
  426. $this->fk_soc = 1;
  427. $this->amount_ht = 10;
  428. $this->amount_tva = 1.96;
  429. $this->amount_ttc = 11.96;
  430. $this->tva_tx = 19.6;
  431. $this->description = 'Specimen discount';
  432. }
  433. }
  434. ?>