PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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