PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

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