PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/compta/localtax/class/localtax.class.php

https://github.com/asterix14/dolibarr
PHP | 595 lines | 402 code | 81 blank | 112 comment | 44 complexity | 5c77906262a82a0d71737895abce49f5 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/compta/localtax/class/localtax.class.php
  19. * \ingroup tax
  20. * \author Laurent Destailleur
  21. */
  22. require_once(DOL_DOCUMENT_ROOT ."/core/class/commonobject.class.php");
  23. /**
  24. * \class Localtax
  25. * \brief Put here description of your class
  26. */
  27. class localtax extends CommonObject
  28. {
  29. var $id;
  30. var $ref;
  31. var $tms;
  32. var $datep;
  33. var $datev;
  34. var $amount;
  35. var $label;
  36. var $note;
  37. var $fk_bank;
  38. var $fk_user_creat;
  39. var $fk_user_modif;
  40. /**
  41. * Constructor
  42. *
  43. * @param DoliDB $DB Database handler
  44. */
  45. function __construct($DB)
  46. {
  47. $this->db = $DB;
  48. }
  49. /**
  50. * Create in database
  51. *
  52. * @param User $user User that create
  53. * @return int <0 if KO, >0 if OK
  54. */
  55. function create($user)
  56. {
  57. global $conf, $langs;
  58. // Clean parameters
  59. $this->amount=trim($this->amount);
  60. $this->label=trim($this->label);
  61. $this->note=trim($this->note);
  62. $this->fk_bank=trim($this->fk_bank);
  63. $this->fk_user_creat=trim($this->fk_user_creat);
  64. $this->fk_user_modif=trim($this->fk_user_modif);
  65. // Insert request
  66. $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax(";
  67. $sql.= "tms,";
  68. $sql.= "datep,";
  69. $sql.= "datev,";
  70. $sql.= "amount,";
  71. $sql.= "label,";
  72. $sql.= "note,";
  73. $sql.= "fk_bank,";
  74. $sql.= "fk_user_creat,";
  75. $sql.= "fk_user_modif";
  76. $sql.= ") VALUES (";
  77. $sql.= " ".$this->db->idate($this->tms).",";
  78. $sql.= " ".$this->db->idate($this->datep).",";
  79. $sql.= " ".$this->db->idate($this->datev).",";
  80. $sql.= " '".$this->amount."',";
  81. $sql.= " '".$this->label."',";
  82. $sql.= " '".$this->note."',";
  83. $sql.= " ".($this->fk_bank <= 0 ? "NULL" : "'".$this->fk_bank."'").",";
  84. $sql.= " '".$this->fk_user_creat."',";
  85. $sql.= " '".$this->fk_user_modif."'";
  86. $sql.= ")";
  87. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  88. $resql=$this->db->query($sql);
  89. if ($resql)
  90. {
  91. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax");
  92. // Appel des triggers
  93. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  94. $interface=new Interfaces($this->db);
  95. $result=$interface->run_triggers('LOCALTAX_CREATE',$this,$user,$langs,$conf);
  96. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  97. // Fin appel triggers
  98. return $this->id;
  99. }
  100. else
  101. {
  102. $this->error="Error ".$this->db->lasterror();
  103. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  104. return -1;
  105. }
  106. }
  107. /**
  108. * Update database
  109. *
  110. * @param User $user User that modify
  111. * @param int $notrigger 0=no, 1=yes (no update trigger)
  112. * @return int <0 if KO, >0 if OK
  113. */
  114. function update($user=0, $notrigger=0)
  115. {
  116. global $conf, $langs;
  117. // Clean parameters
  118. $this->amount=trim($this->amount);
  119. $this->label=trim($this->label);
  120. $this->note=trim($this->note);
  121. $this->fk_bank=trim($this->fk_bank);
  122. $this->fk_user_creat=trim($this->fk_user_creat);
  123. $this->fk_user_modif=trim($this->fk_user_modif);
  124. // Update request
  125. $sql = "UPDATE ".MAIN_DB_PREFIX."localtax SET";
  126. $sql.= " tms=".$this->db->idate($this->tms).",";
  127. $sql.= " datep=".$this->db->idate($this->datep).",";
  128. $sql.= " datev=".$this->db->idate($this->datev).",";
  129. $sql.= " amount='".$this->amount."',";
  130. $sql.= " label='".$this->db->escape($this->label)."',";
  131. $sql.= " note='".$this->db->escape($this->note)."',";
  132. $sql.= " fk_bank='".$this->fk_bank."',";
  133. $sql.= " fk_user_creat='".$this->fk_user_creat."',";
  134. $sql.= " fk_user_modif='".$this->fk_user_modif."'";
  135. $sql.= " WHERE rowid=".$this->id;
  136. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  137. $resql = $this->db->query($sql);
  138. if (! $resql)
  139. {
  140. $this->error="Error ".$this->db->lasterror();
  141. dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
  142. return -1;
  143. }
  144. if (! $notrigger)
  145. {
  146. // Appel des triggers
  147. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  148. $interface=new Interfaces($this->db);
  149. $result=$interface->run_triggers('LOCALTAX_MODIFY',$this,$user,$langs,$conf);
  150. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  151. // Fin appel triggers
  152. }
  153. return 1;
  154. }
  155. /**
  156. * Load object in memory from database
  157. *
  158. * @param int $id Object id
  159. * @return int <0 if KO, >0 if OK
  160. */
  161. function fetch($id)
  162. {
  163. global $langs;
  164. $sql = "SELECT";
  165. $sql.= " t.rowid,";
  166. $sql.= " t.tms,";
  167. $sql.= " t.datep,";
  168. $sql.= " t.datev,";
  169. $sql.= " t.amount,";
  170. $sql.= " t.label,";
  171. $sql.= " t.note,";
  172. $sql.= " t.fk_bank,";
  173. $sql.= " t.fk_user_creat,";
  174. $sql.= " t.fk_user_modif,";
  175. $sql.= " b.fk_account,";
  176. $sql.= " b.fk_type,";
  177. $sql.= " b.rappro";
  178. $sql.= " FROM ".MAIN_DB_PREFIX."localtax as t";
  179. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
  180. $sql.= " WHERE t.rowid = ".$id;
  181. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  182. $resql=$this->db->query($sql);
  183. if ($resql)
  184. {
  185. if ($this->db->num_rows($resql))
  186. {
  187. $obj = $this->db->fetch_object($resql);
  188. $this->id = $obj->rowid;
  189. $this->ref = $obj->rowid;
  190. $this->tms = $this->db->jdate($obj->tms);
  191. $this->datep = $this->db->jdate($obj->datep);
  192. $this->datev = $this->db->jdate($obj->datev);
  193. $this->amount = $obj->amount;
  194. $this->label = $obj->label;
  195. $this->note = $obj->note;
  196. $this->fk_bank = $obj->fk_bank;
  197. $this->fk_user_creat = $obj->fk_user_creat;
  198. $this->fk_user_modif = $obj->fk_user_modif;
  199. $this->fk_account = $obj->fk_account;
  200. $this->fk_type = $obj->fk_type;
  201. $this->rappro = $obj->rappro;
  202. }
  203. $this->db->free($resql);
  204. return 1;
  205. }
  206. else
  207. {
  208. $this->error="Error ".$this->db->lasterror();
  209. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  210. return -1;
  211. }
  212. }
  213. /**
  214. * Delete object in database
  215. *
  216. * @param User $user User that delete
  217. * @return int <0 if KO, >0 if OK
  218. */
  219. function delete($user)
  220. {
  221. global $conf, $langs;
  222. $sql = "DELETE FROM ".MAIN_DB_PREFIX."localtax";
  223. $sql.= " WHERE rowid=".$this->id;
  224. dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
  225. $resql = $this->db->query($sql);
  226. if (! $resql)
  227. {
  228. $this->error="Error ".$this->db->lasterror();
  229. dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
  230. return -1;
  231. }
  232. // Appel des triggers
  233. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  234. $interface=new Interfaces($this->db);
  235. $result=$interface->run_triggers('LOCALTAX_DELETE',$this,$user,$langs,$conf);
  236. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  237. // Fin appel triggers
  238. return 1;
  239. }
  240. /**
  241. * Initialise an instance with random values.
  242. * Used to build previews or test instances.
  243. * id must be 0 if object instance is a specimen.
  244. *
  245. * @return void
  246. */
  247. function initAsSpecimen()
  248. {
  249. $this->id=0;
  250. $this->tms='';
  251. $this->datep='';
  252. $this->datev='';
  253. $this->amount='';
  254. $this->label='';
  255. $this->note='';
  256. $this->fk_bank='';
  257. $this->fk_user_creat='';
  258. $this->fk_user_modif='';
  259. }
  260. /**
  261. * Hum la fonction s'appelle 'Solde' elle doit a mon avis calcluer le solde de localtax, non ?
  262. *
  263. */
  264. function solde($year = 0)
  265. {
  266. $reglee = $this->localtax_sum_reglee($year);
  267. $payee = $this->localtax_sum_payee($year);
  268. $collectee = $this->localtax_sum_collectee($year);
  269. $solde = $reglee - ($collectee - $payee);
  270. return $solde;
  271. }
  272. /**
  273. * Total de la localtax des factures emises par la societe.
  274. *
  275. */
  276. function localtax_sum_collectee($year = 0)
  277. {
  278. $sql = "SELECT sum(f.localtax) as amount";
  279. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f WHERE f.paye = 1";
  280. if ($year)
  281. {
  282. $sql .= " AND f.datef >= '$year-01-01' AND f.datef <= '$year-12-31' ";
  283. }
  284. $result = $this->db->query($sql);
  285. if ($result)
  286. {
  287. if ($this->db->num_rows($result))
  288. {
  289. $obj = $this->db->fetch_object($result);
  290. return $obj->amount;
  291. }
  292. else
  293. {
  294. return 0;
  295. }
  296. $this->db->free($result);
  297. }
  298. else
  299. {
  300. print $this->db->error();
  301. return -1;
  302. }
  303. }
  304. /**
  305. * localtax payed
  306. *
  307. */
  308. function localtax_sum_payee($year = 0)
  309. {
  310. $sql = "SELECT sum(f.total_localtax) as total_localtax";
  311. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  312. if ($year)
  313. {
  314. $sql .= " WHERE f.datef >= '$year-01-01' AND f.datef <= '$year-12-31' ";
  315. }
  316. $result = $this->db->query($sql);
  317. if ($result)
  318. {
  319. if ($this->db->num_rows($result))
  320. {
  321. $obj = $this->db->fetch_object($result);
  322. return $obj->total_localtax;
  323. }
  324. else
  325. {
  326. return 0;
  327. }
  328. $this->db->free();
  329. }
  330. else
  331. {
  332. print $this->db->error();
  333. return -1;
  334. }
  335. }
  336. /**
  337. * localtax payed
  338. * Total de la localtax payed
  339. *
  340. */
  341. function localtax_sum_reglee($year = 0)
  342. {
  343. $sql = "SELECT sum(f.amount) as amount";
  344. $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
  345. if ($year)
  346. {
  347. $sql .= " WHERE f.datev >= '$year-01-01' AND f.datev <= '$year-12-31' ";
  348. }
  349. $result = $this->db->query($sql);
  350. if ($result)
  351. {
  352. if ($this->db->num_rows($result))
  353. {
  354. $obj = $this->db->fetch_object($result);
  355. return $obj->amount;
  356. }
  357. else
  358. {
  359. return 0;
  360. }
  361. $this->db->free();
  362. }
  363. else
  364. {
  365. print $this->db->error();
  366. return -1;
  367. }
  368. }
  369. /**
  370. * Add a payment of localtax
  371. *
  372. * @param User $user Object user that insert
  373. * @return int <0 if KO, rowid in localtax table if OK
  374. */
  375. function addPayment($user)
  376. {
  377. global $conf,$langs;
  378. $this->db->begin();
  379. // Check parameters
  380. $this->amount=price2num($this->amount);
  381. if (! $this->label)
  382. {
  383. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label"));
  384. return -3;
  385. }
  386. if ($this->amount <= 0)
  387. {
  388. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Amount"));
  389. return -4;
  390. }
  391. if ($conf->banque->enabled && (empty($this->accountid) || $this->accountid <= 0))
  392. {
  393. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Account"));
  394. return -5;
  395. }
  396. if ($conf->banque->enabled && (empty($this->paymenttype) || $this->paymenttype <= 0))
  397. {
  398. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
  399. return -5;
  400. }
  401. // Insertion dans table des paiement localtax
  402. $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax (datep, datev, amount";
  403. if ($this->note) $sql.=", note";
  404. if ($this->label) $sql.=", label";
  405. $sql.= ", fk_user_creat, fk_bank";
  406. $sql.= ") ";
  407. $sql.= " VALUES ('".$this->db->idate($this->datep)."',";
  408. $sql.= "'".$this->db->idate($this->datev)."'," . $this->amount;
  409. if ($this->note) $sql.=", '".$this->db->escape($this->note)."'";
  410. if ($this->label) $sql.=", '".$this->db->escape($this->label)."'";
  411. $sql.=", '".$user->id."', NULL";
  412. $sql.= ")";
  413. dol_syslog(get_class($this)."::addPayment sql=".$sql);
  414. $result = $this->db->query($sql);
  415. if ($result)
  416. {
  417. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax"); // TODO devrait s'appeler paiementlocaltax
  418. if ($this->id > 0)
  419. {
  420. $ok=1;
  421. if ($conf->banque->enabled)
  422. {
  423. // Insertion dans llx_bank
  424. require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php');
  425. $acc = new Account($this->db);
  426. $result=$acc->fetch($this->accountid);
  427. if ($result <= 0) dol_print_error($db);
  428. $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs($this->amount), '', '', $user);
  429. // Mise a jour fk_bank dans llx_localtax. On connait ainsi la ligne de localtax qui a g�n�r� l'�criture bancaire
  430. if ($bank_line_id > 0)
  431. {
  432. $this->update_fk_bank($bank_line_id);
  433. }
  434. else
  435. {
  436. $this->error=$acc->error;
  437. $ok=0;
  438. }
  439. // Mise a jour liens
  440. $result=$acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/localtax/fiche.php?id=', "(VATPayment)", "payment_vat");
  441. if ($result < 0)
  442. {
  443. $this->error=$acc->error;
  444. $ok=0;
  445. }
  446. }
  447. if ($ok)
  448. {
  449. $this->db->commit();
  450. return $this->id;
  451. }
  452. else
  453. {
  454. $this->db->rollback();
  455. return -3;
  456. }
  457. }
  458. else
  459. {
  460. $this->error=$this->db->error();
  461. $this->db->rollback();
  462. return -2;
  463. }
  464. }
  465. else
  466. {
  467. $this->error=$this->db->error();
  468. $this->db->rollback();
  469. return -1;
  470. }
  471. }
  472. /**
  473. * Update the link betwen localtax payment and the line into llx_bank
  474. *
  475. * @param int $id Id bank account
  476. * @return int <0 if KO, >0 if OK
  477. */
  478. function update_fk_bank($id)
  479. {
  480. $sql = 'UPDATE llx_localtax set fk_bank = '.$id;
  481. $sql.= ' WHERE rowid = '.$this->id;
  482. $result = $this->db->query($sql);
  483. if ($result)
  484. {
  485. return 1;
  486. }
  487. else
  488. {
  489. dol_print_error($this->db);
  490. return -1;
  491. }
  492. }
  493. /**
  494. * Returns clickable name
  495. *
  496. * @param int $withpicto 0=Link, 1=Picto into link, 2=Picto
  497. * @param string $option Sur quoi pointe le lien
  498. * @return string Chaine avec URL
  499. */
  500. function getNomUrl($withpicto=0, $option='')
  501. {
  502. global $langs;
  503. $result='';
  504. $lien = '<a href="'.DOL_URL_ROOT.'/compta/localtax/fiche.php?id='.$this->id.'">';
  505. $lienfin='</a>';
  506. $picto='payment';
  507. $label=$langs->trans("ShowVatPayment").': '.$this->ref;
  508. if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
  509. if ($withpicto && $withpicto != 2) $result.=' ';
  510. if ($withpicto != 2) $result.=$lien.$this->ref.$lienfin;
  511. return $result;
  512. }
  513. }
  514. ?>