PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/speedealing/speedealing
PHP | 605 lines | 405 code | 81 blank | 119 comment | 44 complexity | b3168b05201f0f231f5081bdf6cb0f8a MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  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 3 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 to manage local tax
  25. */
  26. class Localtax extends CommonObject
  27. {
  28. var $id;
  29. var $ref;
  30. var $tms;
  31. var $datep;
  32. var $datev;
  33. var $amount;
  34. var $label;
  35. var $note;
  36. var $fk_bank;
  37. var $fk_user_creat;
  38. var $fk_user_modif;
  39. /**
  40. * Constructor
  41. *
  42. * @param DoliDB $db Database handler
  43. */
  44. function __construct($db = '')
  45. {
  46. $this->db = $db;
  47. }
  48. /**
  49. * Create in database
  50. *
  51. * @param User $user User that create
  52. * @return int <0 if KO, >0 if OK
  53. */
  54. function create($user)
  55. {
  56. global $conf, $langs;
  57. $error=0;
  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. $error=0;
  118. // Clean parameters
  119. $this->amount=trim($this->amount);
  120. $this->label=trim($this->label);
  121. $this->note=trim($this->note);
  122. $this->fk_bank=trim($this->fk_bank);
  123. $this->fk_user_creat=trim($this->fk_user_creat);
  124. $this->fk_user_modif=trim($this->fk_user_modif);
  125. // Update request
  126. $sql = "UPDATE ".MAIN_DB_PREFIX."localtax SET";
  127. $sql.= " tms=".$this->db->idate($this->tms).",";
  128. $sql.= " datep=".$this->db->idate($this->datep).",";
  129. $sql.= " datev=".$this->db->idate($this->datev).",";
  130. $sql.= " amount='".$this->amount."',";
  131. $sql.= " label='".$this->db->escape($this->label)."',";
  132. $sql.= " note='".$this->db->escape($this->note)."',";
  133. $sql.= " fk_bank='".$this->fk_bank."',";
  134. $sql.= " fk_user_creat='".$this->fk_user_creat."',";
  135. $sql.= " fk_user_modif='".$this->fk_user_modif."'";
  136. $sql.= " WHERE rowid=".$this->id;
  137. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  138. $resql = $this->db->query($sql);
  139. if (! $resql)
  140. {
  141. $this->error="Error ".$this->db->lasterror();
  142. dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
  143. return -1;
  144. }
  145. if (! $notrigger)
  146. {
  147. // Appel des triggers
  148. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  149. $interface=new Interfaces($this->db);
  150. $result=$interface->run_triggers('LOCALTAX_MODIFY',$this,$user,$langs,$conf);
  151. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  152. // Fin appel triggers
  153. }
  154. return 1;
  155. }
  156. /**
  157. * Load object in memory from database
  158. *
  159. * @param int $id Object id
  160. * @return int <0 if KO, >0 if OK
  161. */
  162. function fetch($id)
  163. {
  164. global $langs;
  165. $sql = "SELECT";
  166. $sql.= " t.rowid,";
  167. $sql.= " t.tms,";
  168. $sql.= " t.datep,";
  169. $sql.= " t.datev,";
  170. $sql.= " t.amount,";
  171. $sql.= " t.label,";
  172. $sql.= " t.note,";
  173. $sql.= " t.fk_bank,";
  174. $sql.= " t.fk_user_creat,";
  175. $sql.= " t.fk_user_modif,";
  176. $sql.= " b.fk_account,";
  177. $sql.= " b.fk_type,";
  178. $sql.= " b.rappro";
  179. $sql.= " FROM ".MAIN_DB_PREFIX."localtax as t";
  180. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
  181. $sql.= " WHERE t.rowid = ".$id;
  182. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  183. $resql=$this->db->query($sql);
  184. if ($resql)
  185. {
  186. if ($this->db->num_rows($resql))
  187. {
  188. $obj = $this->db->fetch_object($resql);
  189. $this->id = $obj->rowid;
  190. $this->ref = $obj->rowid;
  191. $this->tms = $this->db->jdate($obj->tms);
  192. $this->datep = $this->db->jdate($obj->datep);
  193. $this->datev = $this->db->jdate($obj->datev);
  194. $this->amount = $obj->amount;
  195. $this->label = $obj->label;
  196. $this->note = $obj->note;
  197. $this->fk_bank = $obj->fk_bank;
  198. $this->fk_user_creat = $obj->fk_user_creat;
  199. $this->fk_user_modif = $obj->fk_user_modif;
  200. $this->fk_account = $obj->fk_account;
  201. $this->fk_type = $obj->fk_type;
  202. $this->rappro = $obj->rappro;
  203. }
  204. $this->db->free($resql);
  205. return 1;
  206. }
  207. else
  208. {
  209. $this->error="Error ".$this->db->lasterror();
  210. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  211. return -1;
  212. }
  213. }
  214. /**
  215. * Delete object in database
  216. *
  217. * @param User $user User that delete
  218. * @return int <0 if KO, >0 if OK
  219. */
  220. function delete($user)
  221. {
  222. global $conf, $langs;
  223. $error=0;
  224. $sql = "DELETE FROM ".MAIN_DB_PREFIX."localtax";
  225. $sql.= " WHERE rowid=".$this->id;
  226. dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
  227. $resql = $this->db->query($sql);
  228. if (! $resql)
  229. {
  230. $this->error="Error ".$this->db->lasterror();
  231. dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
  232. return -1;
  233. }
  234. // Appel des triggers
  235. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  236. $interface=new Interfaces($this->db);
  237. $result=$interface->run_triggers('LOCALTAX_DELETE',$this,$user,$langs,$conf);
  238. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  239. // Fin appel triggers
  240. return 1;
  241. }
  242. /**
  243. * Initialise an instance with random values.
  244. * Used to build previews or test instances.
  245. * id must be 0 if object instance is a specimen.
  246. *
  247. * @return void
  248. */
  249. function initAsSpecimen()
  250. {
  251. $this->id=0;
  252. $this->tms='';
  253. $this->datep='';
  254. $this->datev='';
  255. $this->amount='';
  256. $this->label='';
  257. $this->note='';
  258. $this->fk_bank='';
  259. $this->fk_user_creat='';
  260. $this->fk_user_modif='';
  261. }
  262. /**
  263. * Hum la fonction s'appelle 'Solde' elle doit a mon avis calcluer le solde de localtax, non ?
  264. *
  265. * @param int $year Year
  266. * @return int ???
  267. */
  268. function solde($year = 0)
  269. {
  270. $reglee = $this->localtax_sum_reglee($year);
  271. $payee = $this->localtax_sum_payee($year);
  272. $collectee = $this->localtax_sum_collectee($year);
  273. $solde = $reglee - ($collectee - $payee);
  274. return $solde;
  275. }
  276. /**
  277. * Total de la localtax des factures emises par la societe.
  278. *
  279. * @param int $year Year
  280. * @return int ???
  281. */
  282. function localtax_sum_collectee($year = 0)
  283. {
  284. $sql = "SELECT sum(f.localtax) as amount";
  285. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f WHERE f.paye = 1";
  286. if ($year)
  287. {
  288. $sql .= " AND f.datef >= '$year-01-01' AND f.datef <= '$year-12-31' ";
  289. }
  290. $result = $this->db->query($sql);
  291. if ($result)
  292. {
  293. if ($this->db->num_rows($result))
  294. {
  295. $obj = $this->db->fetch_object($result);
  296. return $obj->amount;
  297. }
  298. else
  299. {
  300. return 0;
  301. }
  302. $this->db->free($result);
  303. }
  304. else
  305. {
  306. print $this->db->error();
  307. return -1;
  308. }
  309. }
  310. /**
  311. * localtax payed
  312. *
  313. * @param int $year Year
  314. * @return int ???
  315. */
  316. function localtax_sum_payee($year = 0)
  317. {
  318. $sql = "SELECT sum(f.total_localtax) as total_localtax";
  319. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  320. if ($year)
  321. {
  322. $sql .= " WHERE f.datef >= '$year-01-01' AND f.datef <= '$year-12-31' ";
  323. }
  324. $result = $this->db->query($sql);
  325. if ($result)
  326. {
  327. if ($this->db->num_rows($result))
  328. {
  329. $obj = $this->db->fetch_object($result);
  330. return $obj->total_localtax;
  331. }
  332. else
  333. {
  334. return 0;
  335. }
  336. $this->db->free();
  337. }
  338. else
  339. {
  340. print $this->db->error();
  341. return -1;
  342. }
  343. }
  344. /**
  345. * localtax payed
  346. * Total de la localtax payed
  347. *
  348. * @param int $year Year
  349. * @return int ???
  350. */
  351. function localtax_sum_reglee($year = 0)
  352. {
  353. $sql = "SELECT sum(f.amount) as amount";
  354. $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
  355. if ($year)
  356. {
  357. $sql .= " WHERE f.datev >= '$year-01-01' AND f.datev <= '$year-12-31' ";
  358. }
  359. $result = $this->db->query($sql);
  360. if ($result)
  361. {
  362. if ($this->db->num_rows($result))
  363. {
  364. $obj = $this->db->fetch_object($result);
  365. return $obj->amount;
  366. }
  367. else
  368. {
  369. return 0;
  370. }
  371. $this->db->free();
  372. }
  373. else
  374. {
  375. print $this->db->error();
  376. return -1;
  377. }
  378. }
  379. /**
  380. * Add a payment of localtax
  381. *
  382. * @param User $user Object user that insert
  383. * @return int <0 if KO, rowid in localtax table if OK
  384. */
  385. function addPayment($user)
  386. {
  387. global $conf,$langs;
  388. $this->db->begin();
  389. // Check parameters
  390. $this->amount=price2num($this->amount);
  391. if (! $this->label)
  392. {
  393. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label"));
  394. return -3;
  395. }
  396. if ($this->amount <= 0)
  397. {
  398. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Amount"));
  399. return -4;
  400. }
  401. if (! empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0))
  402. {
  403. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Account"));
  404. return -5;
  405. }
  406. if (! empty($conf->banque->enabled) && (empty($this->paymenttype) || $this->paymenttype <= 0))
  407. {
  408. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
  409. return -5;
  410. }
  411. // Insertion dans table des paiement localtax
  412. $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax (datep, datev, amount";
  413. if ($this->note) $sql.=", note";
  414. if ($this->label) $sql.=", label";
  415. $sql.= ", fk_user_creat, fk_bank";
  416. $sql.= ") ";
  417. $sql.= " VALUES ('".$this->db->idate($this->datep)."',";
  418. $sql.= "'".$this->db->idate($this->datev)."'," . $this->amount;
  419. if ($this->note) $sql.=", '".$this->db->escape($this->note)."'";
  420. if ($this->label) $sql.=", '".$this->db->escape($this->label)."'";
  421. $sql.=", '".$user->id."', NULL";
  422. $sql.= ")";
  423. dol_syslog(get_class($this)."::addPayment sql=".$sql);
  424. $result = $this->db->query($sql);
  425. if ($result)
  426. {
  427. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax"); // TODO devrait s'appeler paiementlocaltax
  428. if ($this->id > 0)
  429. {
  430. $ok=1;
  431. if (! empty($conf->banque->enabled))
  432. {
  433. // Insertion dans llx_bank
  434. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  435. $acc = new Account($this->db);
  436. $result=$acc->fetch($this->accountid);
  437. if ($result <= 0) dol_print_error($this->db);
  438. $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs($this->amount), '', '', $user);
  439. // Mise a jour fk_bank dans llx_localtax. On connait ainsi la ligne de localtax qui a g�n�r� l'�criture bancaire
  440. if ($bank_line_id > 0)
  441. {
  442. $this->update_fk_bank($bank_line_id);
  443. }
  444. else
  445. {
  446. $this->error=$acc->error;
  447. $ok=0;
  448. }
  449. // Mise a jour liens
  450. $result=$acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/localtax/fiche.php?id=', "(VATPayment)", "payment_vat");
  451. if ($result < 0)
  452. {
  453. $this->error=$acc->error;
  454. $ok=0;
  455. }
  456. }
  457. if ($ok)
  458. {
  459. $this->db->commit();
  460. return $this->id;
  461. }
  462. else
  463. {
  464. $this->db->rollback();
  465. return -3;
  466. }
  467. }
  468. else
  469. {
  470. $this->error=$this->db->error();
  471. $this->db->rollback();
  472. return -2;
  473. }
  474. }
  475. else
  476. {
  477. $this->error=$this->db->error();
  478. $this->db->rollback();
  479. return -1;
  480. }
  481. }
  482. /**
  483. * Update the link betwen localtax payment and the line into llx_bank
  484. *
  485. * @param int $id Id bank account
  486. * @return int <0 if KO, >0 if OK
  487. */
  488. function update_fk_bank($id)
  489. {
  490. $sql = 'UPDATE '.MAIN_DB_PREFIX.'localtax SET fk_bank = '.$id;
  491. $sql.= ' WHERE rowid = '.$this->id;
  492. $result = $this->db->query($sql);
  493. if ($result)
  494. {
  495. return 1;
  496. }
  497. else
  498. {
  499. dol_print_error($this->db);
  500. return -1;
  501. }
  502. }
  503. /**
  504. * Returns clickable name
  505. *
  506. * @param int $withpicto 0=Link, 1=Picto into link, 2=Picto
  507. * @param string $option Sur quoi pointe le lien
  508. * @return string Chaine avec URL
  509. */
  510. function getNomUrl($withpicto=0, $option='')
  511. {
  512. global $langs;
  513. $result='';
  514. $lien = '<a href="'.DOL_URL_ROOT.'/compta/localtax/fiche.php?id='.$this->id.'">';
  515. $lienfin='</a>';
  516. $picto='payment';
  517. $label=$langs->trans("ShowVatPayment").': '.$this->ref;
  518. if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
  519. if ($withpicto && $withpicto != 2) $result.=' ';
  520. if ($withpicto != 2) $result.=$lien.$this->ref.$lienfin;
  521. return $result;
  522. }
  523. }
  524. ?>