PageRenderTime 78ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/compta/tva/class/tva.class.php

https://github.com/asterix14/dolibarr
PHP | 622 lines | 403 code | 103 blank | 116 comment | 44 complexity | 8ab05f3e3da3fb63a78cf6d894f625d1 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2008 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/compta/tva/class/tva.class.php
  20. * \ingroup tax
  21. * \author Laurent Destailleur
  22. */
  23. // Put here all includes required by your class file
  24. require_once(DOL_DOCUMENT_ROOT ."/core/class/commonobject.class.php");
  25. /**
  26. \class Tva
  27. \brief Put here description of your class
  28. \remarks Initialy built by build_class_from_table on 2008-04-03 21:01
  29. */
  30. class Tva extends CommonObject
  31. {
  32. //public $element='tva'; //!< Id that identify managed objects
  33. //public $table_element='tva'; //!< Name of table without prefix where object is stored
  34. var $id;
  35. var $ref;
  36. var $tms;
  37. var $datep;
  38. var $datev;
  39. var $amount;
  40. var $label;
  41. var $note;
  42. var $fk_bank;
  43. var $fk_user_creat;
  44. var $fk_user_modif;
  45. /**
  46. * Constructor
  47. *
  48. * @param DoliDB $DB Database handler
  49. */
  50. function Tva($DB)
  51. {
  52. $this->db = $DB;
  53. return 1;
  54. }
  55. /**
  56. * Create in database
  57. *
  58. * @param User $user User that create
  59. * @return int <0 if KO, >0 if OK
  60. */
  61. function create($user)
  62. {
  63. global $conf, $langs;
  64. // Clean parameters
  65. $this->amount=trim($this->amount);
  66. $this->label=trim($this->label);
  67. $this->note=trim($this->note);
  68. $this->fk_bank=trim($this->fk_bank);
  69. $this->fk_user_creat=trim($this->fk_user_creat);
  70. $this->fk_user_modif=trim($this->fk_user_modif);
  71. // Check parameters
  72. // Put here code to add control on parameters values
  73. // Insert request
  74. $sql = "INSERT INTO ".MAIN_DB_PREFIX."tva(";
  75. $sql.= "tms,";
  76. $sql.= "datep,";
  77. $sql.= "datev,";
  78. $sql.= "amount,";
  79. $sql.= "label,";
  80. $sql.= "note,";
  81. $sql.= "fk_bank,";
  82. $sql.= "fk_user_creat,";
  83. $sql.= "fk_user_modif";
  84. $sql.= ") VALUES (";
  85. $sql.= " ".$this->db->idate($this->tms).",";
  86. $sql.= " ".$this->db->idate($this->datep).",";
  87. $sql.= " ".$this->db->idate($this->datev).",";
  88. $sql.= " '".$this->amount."',";
  89. $sql.= " '".$this->label."',";
  90. $sql.= " '".$this->note."',";
  91. $sql.= " ".($this->fk_bank <= 0 ? "NULL" : "'".$this->fk_bank."'").",";
  92. $sql.= " '".$this->fk_user_creat."',";
  93. $sql.= " '".$this->fk_user_modif."'";
  94. $sql.= ")";
  95. dol_syslog("Tva::create sql=".$sql, LOG_DEBUG);
  96. $resql=$this->db->query($sql);
  97. if ($resql)
  98. {
  99. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."tva");
  100. // Appel des triggers
  101. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  102. $interface=new Interfaces($this->db);
  103. $result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  104. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  105. // Fin appel triggers
  106. return $this->id;
  107. }
  108. else
  109. {
  110. $this->error="Error ".$this->db->lasterror();
  111. dol_syslog("Tva::create ".$this->error, LOG_ERR);
  112. return -1;
  113. }
  114. }
  115. /*
  116. * \brief Update database
  117. * \param user User that modify
  118. * \param notrigger 0=no, 1=yes (no update trigger)
  119. * \return int <0 if KO, >0 if OK
  120. */
  121. function update($user=0, $notrigger=0)
  122. {
  123. global $conf, $langs;
  124. // Clean parameters
  125. $this->amount=trim($this->amount);
  126. $this->label=trim($this->label);
  127. $this->note=trim($this->note);
  128. $this->fk_bank=trim($this->fk_bank);
  129. $this->fk_user_creat=trim($this->fk_user_creat);
  130. $this->fk_user_modif=trim($this->fk_user_modif);
  131. // Check parameters
  132. // Put here code to add control on parameters values
  133. // Update request
  134. $sql = "UPDATE ".MAIN_DB_PREFIX."tva SET";
  135. $sql.= " tms=".$this->db->idate($this->tms).",";
  136. $sql.= " datep=".$this->db->idate($this->datep).",";
  137. $sql.= " datev=".$this->db->idate($this->datev).",";
  138. $sql.= " amount='".$this->amount."',";
  139. $sql.= " label='".$this->db->escape($this->label)."',";
  140. $sql.= " note='".$this->db->escape($this->note)."',";
  141. $sql.= " fk_bank='".$this->fk_bank."',";
  142. $sql.= " fk_user_creat='".$this->fk_user_creat."',";
  143. $sql.= " fk_user_modif='".$this->fk_user_modif."'";
  144. $sql.= " WHERE rowid=".$this->id;
  145. dol_syslog("Tva::update sql=".$sql, LOG_DEBUG);
  146. $resql = $this->db->query($sql);
  147. if (! $resql)
  148. {
  149. $this->error="Error ".$this->db->lasterror();
  150. dol_syslog("Tva::update ".$this->error, LOG_ERR);
  151. return -1;
  152. }
  153. if (! $notrigger)
  154. {
  155. // Appel des triggers
  156. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  157. $interface=new Interfaces($this->db);
  158. $result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  159. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  160. // Fin appel triggers
  161. }
  162. return 1;
  163. }
  164. /*
  165. * \brief Load object in memory from database
  166. * \param id id object
  167. * \param user User that load
  168. * \return int <0 if KO, >0 if OK
  169. */
  170. function fetch($id, $user=0)
  171. {
  172. global $langs;
  173. $sql = "SELECT";
  174. $sql.= " t.rowid,";
  175. $sql.= " t.tms,";
  176. $sql.= " t.datep,";
  177. $sql.= " t.datev,";
  178. $sql.= " t.amount,";
  179. $sql.= " t.label,";
  180. $sql.= " t.note,";
  181. $sql.= " t.fk_bank,";
  182. $sql.= " t.fk_user_creat,";
  183. $sql.= " t.fk_user_modif,";
  184. $sql.= " b.fk_account,";
  185. $sql.= " b.fk_type,";
  186. $sql.= " b.rappro";
  187. $sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
  188. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
  189. $sql.= " WHERE t.rowid = ".$id;
  190. dol_syslog("Tva::fetch sql=".$sql, LOG_DEBUG);
  191. $resql=$this->db->query($sql);
  192. if ($resql)
  193. {
  194. if ($this->db->num_rows($resql))
  195. {
  196. $obj = $this->db->fetch_object($resql);
  197. $this->id = $obj->rowid;
  198. $this->ref = $obj->rowid;
  199. $this->tms = $this->db->jdate($obj->tms);
  200. $this->datep = $this->db->jdate($obj->datep);
  201. $this->datev = $this->db->jdate($obj->datev);
  202. $this->amount = $obj->amount;
  203. $this->label = $obj->label;
  204. $this->note = $obj->note;
  205. $this->fk_bank = $obj->fk_bank;
  206. $this->fk_user_creat = $obj->fk_user_creat;
  207. $this->fk_user_modif = $obj->fk_user_modif;
  208. $this->fk_account = $obj->fk_account;
  209. $this->fk_type = $obj->fk_type;
  210. $this->rappro = $obj->rappro;
  211. }
  212. $this->db->free($resql);
  213. return 1;
  214. }
  215. else
  216. {
  217. $this->error="Error ".$this->db->lasterror();
  218. dol_syslog("Tva::fetch ".$this->error, LOG_ERR);
  219. return -1;
  220. }
  221. }
  222. /*
  223. * \brief Delete object in database
  224. * \param user User that delete
  225. * \return int <0 if KO, >0 if OK
  226. */
  227. function delete($user)
  228. {
  229. global $conf, $langs;
  230. $sql = "DELETE FROM ".MAIN_DB_PREFIX."tva";
  231. $sql.= " WHERE rowid=".$this->id;
  232. dol_syslog("Tva::delete sql=".$sql);
  233. $resql = $this->db->query($sql);
  234. if (! $resql)
  235. {
  236. $this->error="Error ".$this->db->lasterror();
  237. dol_syslog("Tva::delete ".$this->error, LOG_ERR);
  238. return -1;
  239. }
  240. // Appel des triggers
  241. include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  242. $interface=new Interfaces($this->db);
  243. $result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  244. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  245. // Fin appel triggers
  246. return 1;
  247. }
  248. /**
  249. * Initialise an instance with random values.
  250. * Used to build previews or test instances.
  251. * id must be 0 if object instance is a specimen.
  252. *
  253. * @return void
  254. */
  255. function initAsSpecimen()
  256. {
  257. $this->id=0;
  258. $this->tms='';
  259. $this->datep='';
  260. $this->datev='';
  261. $this->amount='';
  262. $this->label='';
  263. $this->note='';
  264. $this->fk_bank='';
  265. $this->fk_user_creat='';
  266. $this->fk_user_modif='';
  267. }
  268. /*
  269. * \brief Hum la fonction s'appelle 'Solde' elle doit a mon avis calcluer le solde de TVA, non ?
  270. *
  271. */
  272. function solde($year = 0)
  273. {
  274. $reglee = $this->tva_sum_reglee($year);
  275. $payee = $this->tva_sum_payee($year);
  276. $collectee = $this->tva_sum_collectee($year);
  277. $solde = $reglee - ($collectee - $payee);
  278. return $solde;
  279. }
  280. /*
  281. * \brief Total de la TVA des factures emises par la societe.
  282. *
  283. */
  284. function tva_sum_collectee($year = 0)
  285. {
  286. $sql = "SELECT sum(f.tva) as amount";
  287. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f WHERE f.paye = 1";
  288. if ($year)
  289. {
  290. $sql .= " AND f.datef >= '$year-01-01' AND f.datef <= '$year-12-31' ";
  291. }
  292. $result = $this->db->query($sql);
  293. if ($result)
  294. {
  295. if ($this->db->num_rows($result))
  296. {
  297. $obj = $this->db->fetch_object($result);
  298. return $obj->amount;
  299. }
  300. else
  301. {
  302. return 0;
  303. }
  304. $this->db->free($result);
  305. }
  306. else
  307. {
  308. print $this->db->error();
  309. return -1;
  310. }
  311. }
  312. /*
  313. * \brief Tva pay�e
  314. *
  315. */
  316. function tva_sum_payee($year = 0)
  317. {
  318. $sql = "SELECT sum(f.total_tva) as total_tva";
  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_tva;
  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. * \brief Tva r�gl�e
  346. * Total de la TVA r�glee aupres de qui de droit
  347. *
  348. */
  349. function tva_sum_reglee($year = 0)
  350. {
  351. $sql = "SELECT sum(f.amount) as amount";
  352. $sql .= " FROM ".MAIN_DB_PREFIX."tva as f";
  353. if ($year)
  354. {
  355. $sql .= " WHERE f.datev >= '$year-01-01' AND f.datev <= '$year-12-31' ";
  356. }
  357. $result = $this->db->query($sql);
  358. if ($result)
  359. {
  360. if ($this->db->num_rows($result))
  361. {
  362. $obj = $this->db->fetch_object($result);
  363. return $obj->amount;
  364. }
  365. else
  366. {
  367. return 0;
  368. }
  369. $this->db->free();
  370. }
  371. else
  372. {
  373. print $this->db->error();
  374. return -1;
  375. }
  376. }
  377. /**
  378. * \brief Ajoute un paiement de TVA
  379. * \param user Object user that insert
  380. * \return int <0 if KO, rowid in tva table if OK
  381. */
  382. function addPayment($user)
  383. {
  384. global $conf,$langs;
  385. $this->db->begin();
  386. // Check parameters
  387. $this->amount=price2num($this->amount);
  388. if (! $this->label)
  389. {
  390. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label"));
  391. return -3;
  392. }
  393. if ($this->amount <= 0)
  394. {
  395. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Amount"));
  396. return -4;
  397. }
  398. if ($conf->banque->enabled && (empty($this->accountid) || $this->accountid <= 0))
  399. {
  400. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Account"));
  401. return -5;
  402. }
  403. if ($conf->banque->enabled && (empty($this->paymenttype) || $this->paymenttype <= 0))
  404. {
  405. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
  406. return -5;
  407. }
  408. // Insertion dans table des paiement tva
  409. $sql = "INSERT INTO ".MAIN_DB_PREFIX."tva (datep, datev, amount";
  410. if ($this->note) $sql.=", note";
  411. if ($this->label) $sql.=", label";
  412. $sql.= ", fk_user_creat, fk_bank";
  413. $sql.= ") ";
  414. $sql.= " VALUES ('".$this->db->idate($this->datep)."',";
  415. $sql.= "'".$this->db->idate($this->datev)."'," . $this->amount;
  416. if ($this->note) $sql.=", '".$this->db->escape($this->note)."'";
  417. if ($this->label) $sql.=", '".$this->db->escape($this->label)."'";
  418. $sql.=", '".$user->id."', NULL";
  419. $sql.= ")";
  420. dol_syslog("Tva::addPayment sql=".$sql);
  421. $result = $this->db->query($sql);
  422. if ($result)
  423. {
  424. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."tva"); // TODO devrait s'appeler paiementtva
  425. if ($this->id > 0)
  426. {
  427. $ok=1;
  428. if ($conf->banque->enabled)
  429. {
  430. // Insertion dans llx_bank
  431. require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php');
  432. $acc = new Account($this->db);
  433. $result=$acc->fetch($this->accountid);
  434. if ($result <= 0) dol_print_error($db);
  435. $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs($this->amount), '', '', $user);
  436. // Mise a jour fk_bank dans llx_tva. On connait ainsi la ligne de tva qui a g�n�r� l'�criture bancaire
  437. if ($bank_line_id > 0)
  438. {
  439. $this->update_fk_bank($bank_line_id);
  440. }
  441. else
  442. {
  443. $this->error=$acc->error;
  444. $ok=0;
  445. }
  446. // Mise a jour liens
  447. $result=$acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/tva/fiche.php?id=', "(VATPayment)", "payment_vat");
  448. if ($result < 0)
  449. {
  450. $this->error=$acc->error;
  451. $ok=0;
  452. }
  453. }
  454. if ($ok)
  455. {
  456. $this->db->commit();
  457. return $this->id;
  458. }
  459. else
  460. {
  461. $this->db->rollback();
  462. return -3;
  463. }
  464. }
  465. else
  466. {
  467. $this->error=$this->db->error();
  468. $this->db->rollback();
  469. return -2;
  470. }
  471. }
  472. else
  473. {
  474. $this->error=$this->db->error();
  475. $this->db->rollback();
  476. return -1;
  477. }
  478. }
  479. /**
  480. * \brief Mise a jour du lien entre le paiement tva et la ligne g�n�r�e dans llx_bank
  481. * \param id_bank Id compte bancaire
  482. * \return int <0 if KO, >0 if OK
  483. */
  484. function update_fk_bank($id_bank)
  485. {
  486. $sql = 'UPDATE llx_tva set fk_bank = '.$id_bank;
  487. $sql.= ' WHERE rowid = '.$this->id;
  488. $result = $this->db->query($sql);
  489. if ($result)
  490. {
  491. return 1;
  492. }
  493. else
  494. {
  495. dol_print_error($this->db);
  496. return -1;
  497. }
  498. }
  499. /**
  500. \brief Renvoie nom clicable (avec eventuellement le picto)
  501. \param withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  502. \param option Sur quoi pointe le lien
  503. \return string Chaine avec URL
  504. */
  505. function getNomUrl($withpicto=0,$option='')
  506. {
  507. global $langs;
  508. $result='';
  509. $lien = '<a href="'.DOL_URL_ROOT.'/compta/tva/fiche.php?id='.$this->id.'">';
  510. $lienfin='</a>';
  511. $picto='payment';
  512. $label=$langs->trans("ShowVatPayment").': '.$this->ref;
  513. if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
  514. if ($withpicto && $withpicto != 2) $result.=' ';
  515. if ($withpicto != 2) $result.=$lien.$this->ref.$lienfin;
  516. return $result;
  517. }
  518. }
  519. ?>