localtax.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <?php
  2. /* Copyright (C) 2011-2014 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 <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/compta/localtax/class/localtax.class.php
  19. * \ingroup tax
  20. */
  21. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  22. /**
  23. * Class to manage local tax
  24. */
  25. class Localtax extends CommonObject
  26. {
  27. /**
  28. * @var string ID to identify managed object
  29. */
  30. public $element = 'localtax';
  31. /**
  32. * @var string Name of table without prefix where object is stored
  33. */
  34. public $table_element = 'localtax';
  35. /**
  36. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  37. */
  38. public $picto = 'payment';
  39. public $ltt;
  40. public $tms;
  41. public $datep;
  42. public $datev;
  43. public $amount;
  44. /**
  45. * @var string local tax
  46. */
  47. public $label;
  48. /**
  49. * @var int ID
  50. */
  51. public $fk_bank;
  52. /**
  53. * @var int ID
  54. */
  55. public $fk_user_creat;
  56. /**
  57. * @var int ID
  58. */
  59. public $fk_user_modif;
  60. /**
  61. * Constructor
  62. *
  63. * @param DoliDB $db Database handler
  64. */
  65. public function __construct($db)
  66. {
  67. $this->db = $db;
  68. }
  69. /**
  70. * Create in database
  71. *
  72. * @param User $user User that create
  73. * @return int <0 if KO, >0 if OK
  74. */
  75. public function create($user)
  76. {
  77. global $conf, $langs;
  78. $error = 0;
  79. // Clean parameters
  80. $this->amount = trim($this->amount);
  81. $this->label = trim($this->label);
  82. $this->note = trim($this->note);
  83. // Insert request
  84. $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax(";
  85. $sql .= "localtaxtype,";
  86. $sql .= "tms,";
  87. $sql .= "datep,";
  88. $sql .= "datev,";
  89. $sql .= "amount,";
  90. $sql .= "label,";
  91. $sql .= "note,";
  92. $sql .= "fk_bank,";
  93. $sql .= "fk_user_creat,";
  94. $sql .= "fk_user_modif";
  95. $sql .= ") VALUES (";
  96. $sql .= " ".((int) $this->ltt).",";
  97. $sql .= " '".$this->db->idate($this->tms)."',";
  98. $sql .= " '".$this->db->idate($this->datep)."',";
  99. $sql .= " '".$this->db->idate($this->datev)."',";
  100. $sql .= " '".$this->db->escape($this->amount)."',";
  101. $sql .= " '".$this->db->escape($this->label)."',";
  102. $sql .= " '".$this->db->escape($this->note)."',";
  103. $sql .= " ".($this->fk_bank <= 0 ? "NULL" : (int) $this->fk_bank).",";
  104. $sql .= " ".((int) $this->fk_user_creat).",";
  105. $sql .= " ".((int) $this->fk_user_modif);
  106. $sql .= ")";
  107. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  108. $resql = $this->db->query($sql);
  109. if ($resql) {
  110. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax");
  111. // Call trigger
  112. $result = $this->call_trigger('LOCALTAX_CREATE', $user);
  113. if ($result < 0) {
  114. $error++;
  115. }
  116. // End call triggers
  117. if (!$error) {
  118. $this->db->commit();
  119. return $this->id;
  120. } else {
  121. $this->db->rollback();
  122. return -1;
  123. }
  124. } else {
  125. $this->error = "Error ".$this->db->lasterror();
  126. $this->db->rollback();
  127. return -1;
  128. }
  129. }
  130. /**
  131. * Update database
  132. *
  133. * @param User $user User that modify
  134. * @param int $notrigger 0=no, 1=yes (no update trigger)
  135. * @return int <0 if KO, >0 if OK
  136. */
  137. public function update(User $user, $notrigger = 0)
  138. {
  139. global $conf, $langs;
  140. $error = 0;
  141. // Clean parameters
  142. $this->amount = trim($this->amount);
  143. $this->label = trim($this->label);
  144. $this->note = trim($this->note);
  145. $this->db->begin();
  146. // Update request
  147. $sql = "UPDATE ".MAIN_DB_PREFIX."localtax SET";
  148. $sql .= " localtaxtype=".((int) $this->ltt).",";
  149. $sql .= " tms='".$this->db->idate($this->tms)."',";
  150. $sql .= " datep='".$this->db->idate($this->datep)."',";
  151. $sql .= " datev='".$this->db->idate($this->datev)."',";
  152. $sql .= " amount=".price2num($this->amount).",";
  153. $sql .= " label='".$this->db->escape($this->label)."',";
  154. $sql .= " note='".$this->db->escape($this->note)."',";
  155. $sql .= " fk_bank=".(int) $this->fk_bank.",";
  156. $sql .= " fk_user_creat=".(int) $this->fk_user_creat.",";
  157. $sql .= " fk_user_modif=".(int) $this->fk_user_modif;
  158. $sql .= " WHERE rowid=".((int) $this->id);
  159. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  160. $resql = $this->db->query($sql);
  161. if (!$resql) {
  162. $this->error = "Error ".$this->db->lasterror();
  163. $error++;
  164. }
  165. if (!$error && !$notrigger) {
  166. // Call trigger
  167. $result = $this->call_trigger('LOCALTAX_MODIFY', $user);
  168. if ($result < 0) {
  169. $error++;
  170. }
  171. // End call triggers
  172. }
  173. if (!$error) {
  174. $this->db->commit();
  175. return 1;
  176. } else {
  177. $this->db->rollback();
  178. return -1;
  179. }
  180. }
  181. /**
  182. * Load object in memory from database
  183. *
  184. * @param int $id Object id
  185. * @return int <0 if KO, >0 if OK
  186. */
  187. public function fetch($id)
  188. {
  189. global $langs;
  190. $sql = "SELECT";
  191. $sql .= " t.rowid,";
  192. $sql .= " t.localtaxtype,";
  193. $sql .= " t.tms,";
  194. $sql .= " t.datep,";
  195. $sql .= " t.datev,";
  196. $sql .= " t.amount,";
  197. $sql .= " t.label,";
  198. $sql .= " t.note,";
  199. $sql .= " t.fk_bank,";
  200. $sql .= " t.fk_user_creat,";
  201. $sql .= " t.fk_user_modif,";
  202. $sql .= " b.fk_account,";
  203. $sql .= " b.fk_type,";
  204. $sql .= " b.rappro";
  205. $sql .= " FROM ".MAIN_DB_PREFIX."localtax as t";
  206. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
  207. $sql .= " WHERE t.rowid = ".((int) $id);
  208. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  209. $resql = $this->db->query($sql);
  210. if ($resql) {
  211. if ($this->db->num_rows($resql)) {
  212. $obj = $this->db->fetch_object($resql);
  213. $this->id = $obj->rowid;
  214. $this->ref = $obj->rowid;
  215. $this->ltt = $obj->localtaxtype;
  216. $this->tms = $this->db->jdate($obj->tms);
  217. $this->datep = $this->db->jdate($obj->datep);
  218. $this->datev = $this->db->jdate($obj->datev);
  219. $this->amount = $obj->amount;
  220. $this->label = $obj->label;
  221. $this->note = $obj->note;
  222. $this->fk_bank = $obj->fk_bank;
  223. $this->fk_user_creat = $obj->fk_user_creat;
  224. $this->fk_user_modif = $obj->fk_user_modif;
  225. $this->fk_account = $obj->fk_account;
  226. $this->fk_type = $obj->fk_type;
  227. $this->rappro = $obj->rappro;
  228. }
  229. $this->db->free($resql);
  230. return 1;
  231. } else {
  232. $this->error = "Error ".$this->db->lasterror();
  233. return -1;
  234. }
  235. }
  236. /**
  237. * Delete object in database
  238. *
  239. * @param User $user User that delete
  240. * @return int <0 if KO, >0 if OK
  241. */
  242. public function delete($user)
  243. {
  244. // Call trigger
  245. $result = $this->call_trigger('LOCALTAX_DELETE', $user);
  246. if ($result < 0) {
  247. return -1;
  248. }
  249. // End call triggers
  250. $sql = "DELETE FROM ".MAIN_DB_PREFIX."localtax";
  251. $sql .= " WHERE rowid=".((int) $this->id);
  252. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  253. $resql = $this->db->query($sql);
  254. if (!$resql) {
  255. $this->error = "Error ".$this->db->lasterror();
  256. return -1;
  257. }
  258. return 1;
  259. }
  260. /**
  261. * Initialise an instance with random values.
  262. * Used to build previews or test instances.
  263. * id must be 0 if object instance is a specimen.
  264. *
  265. * @return void
  266. */
  267. public function initAsSpecimen()
  268. {
  269. global $user;
  270. $this->id = 0;
  271. $this->tms = '';
  272. $this->ltt = 0;
  273. $this->datep = '';
  274. $this->datev = '';
  275. $this->amount = '';
  276. $this->label = '';
  277. $this->note = '';
  278. $this->fk_bank = 0;
  279. $this->fk_user_creat = $user->id;
  280. $this->fk_user_modif = $user->id;
  281. }
  282. /**
  283. * Hum la fonction s'appelle 'Solde' elle doit a mon avis calcluer le solde de localtax, non ?
  284. *
  285. * @param int $year Year
  286. * @return int ???
  287. */
  288. public function solde($year = 0)
  289. {
  290. $reglee = $this->localtax_sum_reglee($year);
  291. $payee = $this->localtax_sum_payee($year);
  292. $collectee = $this->localtax_sum_collectee($year);
  293. $solde = $reglee - ($collectee - $payee);
  294. return $solde;
  295. }
  296. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  297. /**
  298. * Total de la localtax des factures emises par la societe.
  299. *
  300. * @param int $year Year
  301. * @return int ???
  302. */
  303. public function localtax_sum_collectee($year = 0)
  304. {
  305. // phpcs:enable
  306. $sql = "SELECT sum(f.localtax) as amount";
  307. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  308. $sql .= " WHERE f.paye = 1";
  309. if ($year) {
  310. $sql .= " AND f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, 'gmt'))."' AND '".$this->db->idate(dol_get_last_day($year, 1, 'gmt'))."'";
  311. }
  312. $result = $this->db->query($sql);
  313. if ($result) {
  314. if ($this->db->num_rows($result)) {
  315. $obj = $this->db->fetch_object($result);
  316. $ret = $obj->amount;
  317. $this->db->free($result);
  318. return $ret;
  319. } else {
  320. $this->db->free($result);
  321. return 0;
  322. }
  323. } else {
  324. print $this->db->lasterror();
  325. return -1;
  326. }
  327. }
  328. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  329. /**
  330. * Total of localtax paid in invoice
  331. *
  332. * @param int $year Year
  333. * @return int ???
  334. */
  335. public function localtax_sum_payee($year = 0)
  336. {
  337. // phpcs:enable
  338. $sql = "SELECT sum(f.total_localtax) as total_localtax";
  339. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  340. if ($year) {
  341. $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, 'gmt'))."' AND '".$this->db->idate(dol_get_last_day($year, 1, 'gmt'))."'";
  342. }
  343. $result = $this->db->query($sql);
  344. if ($result) {
  345. if ($this->db->num_rows($result)) {
  346. $obj = $this->db->fetch_object($result);
  347. $ret = $obj->total_localtax;
  348. $this->db->free($result);
  349. return $ret;
  350. } else {
  351. $this->db->free($result);
  352. return 0;
  353. }
  354. } else {
  355. print $this->db->lasterror();
  356. return -1;
  357. }
  358. }
  359. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  360. /**
  361. * Total of localtax paid
  362. *
  363. * @param int $year Year
  364. * @return int ???
  365. */
  366. public function localtax_sum_reglee($year = 0)
  367. {
  368. // phpcs:enable
  369. $sql = "SELECT sum(f.amount) as amount";
  370. $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
  371. if ($year) {
  372. $sql .= " WHERE f.datev BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, 'gmt'))."' AND '".$this->db->idate(dol_get_last_day($year, 1, 'gmt'))."'";
  373. }
  374. $result = $this->db->query($sql);
  375. if ($result) {
  376. if ($this->db->num_rows($result)) {
  377. $obj = $this->db->fetch_object($result);
  378. $ret = $obj->amount;
  379. $this->db->free($result);
  380. return $ret;
  381. } else {
  382. $this->db->free($result);
  383. return 0;
  384. }
  385. } else {
  386. print $this->db->lasterror();
  387. return -1;
  388. }
  389. }
  390. /**
  391. * Add a payment of localtax
  392. *
  393. * @param User $user Object user that insert
  394. * @return int <0 if KO, rowid in localtax table if OK
  395. */
  396. public function addPayment($user)
  397. {
  398. global $conf, $langs;
  399. $this->db->begin();
  400. // Check parameters
  401. $this->amount = price2num($this->amount);
  402. if (!$this->label) {
  403. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
  404. return -3;
  405. }
  406. if ($this->amount <= 0) {
  407. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
  408. return -4;
  409. }
  410. if (isModEnabled("banque") && (empty($this->accountid) || $this->accountid <= 0)) {
  411. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Account"));
  412. return -5;
  413. }
  414. if (isModEnabled("banque") && (empty($this->paymenttype) || $this->paymenttype <= 0)) {
  415. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
  416. return -5;
  417. }
  418. // Insertion dans table des paiement localtax
  419. $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax (localtaxtype, datep, datev, amount";
  420. if ($this->note) {
  421. $sql .= ", note";
  422. }
  423. if ($this->label) {
  424. $sql .= ", label";
  425. }
  426. $sql .= ", fk_user_creat, fk_bank";
  427. $sql .= ") ";
  428. $sql .= " VALUES (".$this->ltt.", '".$this->db->idate($this->datep)."',";
  429. $sql .= "'".$this->db->idate($this->datev)."',".$this->amount;
  430. if ($this->note) {
  431. $sql .= ", '".$this->db->escape($this->note)."'";
  432. }
  433. if ($this->label) {
  434. $sql .= ", '".$this->db->escape($this->label)."'";
  435. }
  436. $sql .= ", ".((int) $user->id).", NULL";
  437. $sql .= ")";
  438. dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
  439. $result = $this->db->query($sql);
  440. if ($result) {
  441. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax"); // TODO devrait s'appeler paiementlocaltax
  442. if ($this->id > 0) {
  443. $ok = 1;
  444. if (isModEnabled("banque")) {
  445. // Insertion dans llx_bank
  446. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  447. $acc = new Account($this->db);
  448. $result = $acc->fetch($this->accountid);
  449. if ($result <= 0) {
  450. dol_print_error($this->db);
  451. }
  452. $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs($this->amount), '', '', $user);
  453. // Mise a jour fk_bank dans llx_localtax. On connait ainsi la ligne de localtax qui a g�n�r� l'�criture bancaire
  454. if ($bank_line_id > 0) {
  455. $this->update_fk_bank($bank_line_id);
  456. } else {
  457. $this->error = $acc->error;
  458. $ok = 0;
  459. }
  460. // Mise a jour liens
  461. $result = $acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/localtax/card.php?id=', "(VATPayment)", "payment_vat");
  462. if ($result < 0) {
  463. $this->error = $acc->error;
  464. $ok = 0;
  465. }
  466. }
  467. if ($ok) {
  468. $this->db->commit();
  469. return $this->id;
  470. } else {
  471. $this->db->rollback();
  472. return -3;
  473. }
  474. } else {
  475. $this->error = $this->db->lasterror();
  476. $this->db->rollback();
  477. return -2;
  478. }
  479. } else {
  480. $this->error = $this->db->lasterror();
  481. $this->db->rollback();
  482. return -1;
  483. }
  484. }
  485. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  486. /**
  487. * Update the link betwen localtax payment and the line into llx_bank
  488. *
  489. * @param int $id Id bank account
  490. * @return int <0 if KO, >0 if OK
  491. */
  492. public function update_fk_bank($id)
  493. {
  494. // phpcs:enable
  495. $sql = 'UPDATE '.MAIN_DB_PREFIX.'localtax SET fk_bank = '.((int) $id);
  496. $sql .= ' WHERE rowid = '.((int) $this->id);
  497. $result = $this->db->query($sql);
  498. if ($result) {
  499. return 1;
  500. } else {
  501. dol_print_error($this->db);
  502. return -1;
  503. }
  504. }
  505. /**
  506. * Returns clickable name
  507. *
  508. * @param int $withpicto 0=Link, 1=Picto into link, 2=Picto
  509. * @param string $option Sur quoi pointe le lien
  510. * @return string Chaine avec URL
  511. */
  512. public function getNomUrl($withpicto = 0, $option = '')
  513. {
  514. global $langs;
  515. $result = '';
  516. $label = $langs->trans("ShowVatPayment").': '.$this->ref;
  517. $link = '<a href="'.DOL_URL_ROOT.'/compta/localtax/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  518. $linkend = '</a>';
  519. $picto = 'payment';
  520. if ($withpicto) {
  521. $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
  522. }
  523. if ($withpicto && $withpicto != 2) {
  524. $result .= ' ';
  525. }
  526. if ($withpicto != 2) {
  527. $result .= $link.$this->ref.$linkend;
  528. }
  529. return $result;
  530. }
  531. /**
  532. * Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
  533. *
  534. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  535. * @return string Libelle
  536. */
  537. public function getLibStatut($mode = 0)
  538. {
  539. return $this->LibStatut($this->statut, $mode);
  540. }
  541. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  542. /**
  543. * Renvoi le libelle d'un statut donne
  544. *
  545. * @param int $status Statut
  546. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  547. * @return string Libelle du statut
  548. */
  549. public function LibStatut($status, $mode = 0)
  550. {
  551. // phpcs:enable
  552. global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
  553. return '';
  554. }
  555. }