tva.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. <?php
  2. /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2011-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
  5. * Copyright (C) 2018 Philippe Grand <philippe.grand@atoo-net.com>
  6. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  7. * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/compta/tva/class/tva.class.php
  24. * \ingroup tax
  25. */
  26. // Put here all includes required by your class file
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  28. /**
  29. * Put here description of your class
  30. */
  31. class Tva extends CommonObject
  32. {
  33. /**
  34. * @var string ID to identify managed object
  35. */
  36. public $element = 'tva';
  37. /**
  38. * @var string Name of table without prefix where object is stored
  39. */
  40. public $table_element = 'tva';
  41. /**
  42. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  43. */
  44. public $picto = 'payment';
  45. /**
  46. * @deprecated
  47. * @see $amount
  48. */
  49. public $total;
  50. public $tms;
  51. public $datep;
  52. public $datev;
  53. public $amount;
  54. public $type_payment;
  55. public $num_payment;
  56. /**
  57. * @var integer|string totalpaid
  58. */
  59. public $totalpaid;
  60. /**
  61. * @var string label
  62. */
  63. public $label;
  64. /**
  65. * @var int ID
  66. */
  67. public $fk_bank;
  68. /**
  69. * @var int accountid
  70. */
  71. public $accountid;
  72. /**
  73. * @var int ID
  74. */
  75. public $fk_user_creat;
  76. /**
  77. * @var int ID
  78. */
  79. public $fk_user_modif;
  80. /**
  81. * @var integer|string paiementtype
  82. */
  83. public $paiementtype;
  84. const STATUS_UNPAID = 0;
  85. const STATUS_PAID = 1;
  86. /**
  87. * Constructor
  88. *
  89. * @param DoliDB $db Database handler
  90. */
  91. public function __construct($db)
  92. {
  93. $this->db = $db;
  94. }
  95. /**
  96. * Create in database
  97. *
  98. * @param User $user User that create
  99. * @return int <0 if KO, >0 if OK
  100. */
  101. public function create($user)
  102. {
  103. global $conf, $langs;
  104. $error = 0;
  105. $now = dol_now();
  106. // Clean parameters
  107. $this->amount = trim($this->amount);
  108. $this->label = trim($this->label);
  109. $this->type_payment = (int) $this->type_payment;
  110. $this->note = trim($this->note);
  111. $this->fk_account = (int) $this->fk_account;
  112. $this->fk_user_creat = (int) $this->fk_user_creat;
  113. $this->fk_user_modif = (int) $this->fk_user_modif;
  114. // Check parameters
  115. // Put here code to add control on parameters values
  116. $this->db->begin();
  117. // Insert request
  118. $sql = "INSERT INTO ".MAIN_DB_PREFIX."tva(";
  119. $sql .= "entity,";
  120. $sql .= "datec,";
  121. $sql .= "datep,";
  122. $sql .= "datev,";
  123. $sql .= "amount,";
  124. $sql .= "label,";
  125. $sql .= "note,";
  126. $sql .= "fk_account,";
  127. $sql .= "fk_typepayment,";
  128. $sql .= "fk_user_creat,";
  129. $sql .= "fk_user_modif";
  130. $sql .= ") VALUES (";
  131. $sql .= " ".((int) $conf->entity).", ";
  132. $sql .= " '".$this->db->idate($now)."',";
  133. $sql .= " '".$this->db->idate($this->datep)."',";
  134. $sql .= " '".$this->db->idate($this->datev)."',";
  135. $sql .= " '".$this->db->escape($this->amount)."',";
  136. $sql .= " '".$this->db->escape($this->label)."',";
  137. $sql .= " '".$this->db->escape($this->note)."',";
  138. $sql .= " '".$this->db->escape($this->fk_account)."',";
  139. $sql .= " '".$this->db->escape($this->type_payment)."',";
  140. $sql .= " ".($this->fk_user_creat > 0 ? (int) $this->fk_user_creat : (int) $user->id).",";
  141. $sql .= " ".($this->fk_user_modif > 0 ? (int) $this->fk_user_modif : (int) $user->id);
  142. $sql .= ")";
  143. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  144. $resql = $this->db->query($sql);
  145. if ($resql) {
  146. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."tva");
  147. // Call trigger
  148. $result = $this->call_trigger('TVA_CREATE', $user);
  149. if ($result < 0) {
  150. $error++;
  151. }
  152. // End call triggers
  153. if (!$error) {
  154. $this->db->commit();
  155. return $this->id;
  156. } else {
  157. $this->db->rollback();
  158. return -1;
  159. }
  160. } else {
  161. $this->error = "Error ".$this->db->lasterror();
  162. $this->db->rollback();
  163. return -1;
  164. }
  165. }
  166. /**
  167. * Update database
  168. *
  169. * @param User $user User that modify
  170. * @param int $notrigger 0=no, 1=yes (no update trigger)
  171. * @return int <0 if KO, >0 if OK
  172. */
  173. public function update($user, $notrigger = 0)
  174. {
  175. global $conf, $langs;
  176. $error = 0;
  177. // Clean parameters
  178. $this->amount = trim($this->amount);
  179. $this->label = trim($this->label);
  180. $this->note = trim($this->note);
  181. $this->fk_user_creat = (int) $this->fk_user_creat;
  182. $this->fk_user_modif = (int) $this->fk_user_modif;
  183. // Check parameters
  184. // Put here code to add control on parameters values
  185. $this->db->begin();
  186. // Update request
  187. $sql = "UPDATE ".MAIN_DB_PREFIX."tva SET";
  188. $sql .= " tms='".$this->db->idate($this->tms)."',";
  189. $sql .= " datep='".$this->db->idate($this->datep)."',";
  190. $sql .= " datev='".$this->db->idate($this->datev)."',";
  191. $sql .= " amount=".price2num($this->amount).",";
  192. $sql .= " label='".$this->db->escape($this->label)."',";
  193. $sql .= " note='".$this->db->escape($this->note)."',";
  194. $sql .= " fk_user_creat=".((int) $this->fk_user_creat).",";
  195. $sql .= " fk_user_modif=".($this->fk_user_modif > 0 ? $this->fk_user_modif : $user->id)."";
  196. $sql .= " WHERE rowid=".((int) $this->id);
  197. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  198. $resql = $this->db->query($sql);
  199. if (!$resql) {
  200. $this->error = "Error ".$this->db->lasterror();
  201. $error++;
  202. }
  203. if (!$error && !$notrigger) {
  204. // Call trigger
  205. $result = $this->call_trigger('TVA_MODIFY', $user);
  206. if ($result < 0) {
  207. $error++;
  208. }
  209. // End call triggers
  210. }
  211. if (!$error) {
  212. $this->db->commit();
  213. return 1;
  214. } else {
  215. $this->db->rollback();
  216. return -1;
  217. }
  218. }
  219. /**
  220. * Tag TVA as payed completely
  221. *
  222. * @param User $user Object user making change
  223. * @return int <0 if KO, >0 if OK
  224. */
  225. public function setPaid($user)
  226. {
  227. // phpcs:enable
  228. $sql = "UPDATE ".MAIN_DB_PREFIX."tva SET";
  229. $sql .= " paye = 1";
  230. $sql .= " WHERE rowid = ".((int) $this->id);
  231. $resql = $this->db->query($sql);
  232. if ($resql) {
  233. return 1;
  234. } else {
  235. return -1;
  236. }
  237. }
  238. /**
  239. * Remove tag payed on TVA
  240. *
  241. * @param User $user Object user making change
  242. * @return int <0 if KO, >0 if OK
  243. */
  244. public function setUnpaid($user)
  245. {
  246. // phpcs:enable
  247. $sql = "UPDATE ".MAIN_DB_PREFIX."tva SET";
  248. $sql .= " paye = 0";
  249. $sql .= " WHERE rowid = ".((int) $this->id);
  250. $resql = $this->db->query($sql);
  251. if ($resql) {
  252. return 1;
  253. } else {
  254. return -1;
  255. }
  256. }
  257. /**
  258. * Load object in memory from database
  259. *
  260. * @param int $id id object
  261. * @param User $user User that load
  262. * @return int <0 if KO, >0 if OK
  263. */
  264. public function fetch($id, $user = null)
  265. {
  266. global $langs;
  267. $sql = "SELECT";
  268. $sql .= " t.rowid,";
  269. $sql .= " t.tms,";
  270. $sql .= " t.datep,";
  271. $sql .= " t.datev,";
  272. $sql .= " t.amount,";
  273. $sql .= " t.fk_typepayment,";
  274. $sql .= " t.num_payment,";
  275. $sql .= " t.label,";
  276. $sql .= " t.note,";
  277. $sql .= " t.paye,";
  278. $sql .= " t.fk_user_creat,";
  279. $sql .= " t.fk_user_modif,";
  280. $sql .= " t.fk_account";
  281. $sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
  282. //$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
  283. $sql .= " WHERE t.rowid = ".((int) $id);
  284. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  285. $resql = $this->db->query($sql);
  286. if ($resql) {
  287. if ($this->db->num_rows($resql)) {
  288. $obj = $this->db->fetch_object($resql);
  289. $this->id = $obj->rowid;
  290. $this->ref = $obj->rowid;
  291. $this->tms = $this->db->jdate($obj->tms);
  292. $this->datep = $this->db->jdate($obj->datep);
  293. $this->datev = $this->db->jdate($obj->datev);
  294. $this->amount = $obj->amount;
  295. $this->type_payment = $obj->fk_typepayment;
  296. $this->num_payment = $obj->num_payment;
  297. $this->label = $obj->label;
  298. $this->paye = $obj->paye;
  299. $this->note = $obj->note;
  300. $this->fk_user_creat = $obj->fk_user_creat;
  301. $this->fk_user_modif = $obj->fk_user_modif;
  302. $this->fk_account = $obj->fk_account;
  303. $this->fk_type = $obj->fk_type;
  304. $this->rappro = $obj->rappro;
  305. }
  306. $this->db->free($resql);
  307. return 1;
  308. } else {
  309. $this->error = "Error ".$this->db->lasterror();
  310. return -1;
  311. }
  312. }
  313. /**
  314. * Delete object in database
  315. *
  316. * @param User $user User that delete
  317. * @return int <0 if KO, >0 if OK
  318. */
  319. public function delete($user)
  320. {
  321. global $conf, $langs;
  322. $error = 0;
  323. // Call trigger
  324. $result = $this->call_trigger('TVA_DELETE', $user);
  325. if ($result < 0) {
  326. return -1;
  327. }
  328. // End call triggers
  329. $sql = "DELETE FROM ".MAIN_DB_PREFIX."tva";
  330. $sql .= " WHERE rowid=".((int) $this->id);
  331. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  332. $resql = $this->db->query($sql);
  333. if (!$resql) {
  334. $this->error = "Error ".$this->db->lasterror();
  335. return -1;
  336. }
  337. return 1;
  338. }
  339. /**
  340. * Initialise an instance with random values.
  341. * Used to build previews or test instances.
  342. * id must be 0 if object instance is a specimen.
  343. *
  344. * @return void
  345. */
  346. public function initAsSpecimen()
  347. {
  348. $this->id = 0;
  349. $this->tms = '';
  350. $this->datep = '';
  351. $this->datev = '';
  352. $this->amount = '';
  353. $this->label = '';
  354. $this->note = '';
  355. $this->fk_bank = '';
  356. $this->fk_user_creat = '';
  357. $this->fk_user_modif = '';
  358. }
  359. /**
  360. * Balance of VAT
  361. *
  362. * @param int $year Year
  363. * @return double Amount
  364. */
  365. public function solde($year = 0)
  366. {
  367. $reglee = $this->tva_sum_reglee($year);
  368. $payee = $this->tva_sum_payee($year);
  369. $collectee = $this->tva_sum_collectee($year);
  370. $solde = $reglee - ($collectee - $payee);
  371. return $solde;
  372. }
  373. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  374. /**
  375. * Total of the VAT from invoices emitted by the thirdparty.
  376. *
  377. * @param int $year Year
  378. * @return double Amount
  379. */
  380. public function tva_sum_collectee($year = 0)
  381. {
  382. // phpcs:enable
  383. $sql = "SELECT sum(f.total_tva) as amount";
  384. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f WHERE f.paye = 1";
  385. if ($year) {
  386. $sql .= " AND f.datef >= '".$this->db->escape($year)."-01-01' AND f.datef <= '".$this->db->escape($year)."-12-31' ";
  387. }
  388. $result = $this->db->query($sql);
  389. if ($result) {
  390. if ($this->db->num_rows($result)) {
  391. $obj = $this->db->fetch_object($result);
  392. $ret = $obj->amount;
  393. $this->db->free($result);
  394. return $ret;
  395. } else {
  396. $this->db->free($result);
  397. return 0;
  398. }
  399. } else {
  400. print $this->db->lasterror();
  401. return -1;
  402. }
  403. }
  404. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  405. /**
  406. * VAT payed
  407. *
  408. * @param int $year Year
  409. * @return double Amount
  410. */
  411. public function tva_sum_payee($year = 0)
  412. {
  413. // phpcs:enable
  414. $sql = "SELECT sum(f.total_tva) as total_tva";
  415. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  416. if ($year) {
  417. $sql .= " WHERE f.datef >= '".$this->db->escape($year)."-01-01' AND f.datef <= '".$this->db->escape($year)."-12-31' ";
  418. }
  419. $result = $this->db->query($sql);
  420. if ($result) {
  421. if ($this->db->num_rows($result)) {
  422. $obj = $this->db->fetch_object($result);
  423. $ret = $obj->total_tva;
  424. $this->db->free($result);
  425. return $ret;
  426. } else {
  427. $this->db->free($result);
  428. return 0;
  429. }
  430. } else {
  431. print $this->db->lasterror();
  432. return -1;
  433. }
  434. }
  435. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  436. /**
  437. * Total of the VAT payed
  438. *
  439. * @param int $year Year
  440. * @return double Amount
  441. */
  442. public function tva_sum_reglee($year = 0)
  443. {
  444. // phpcs:enable
  445. $sql = "SELECT sum(f.amount) as amount";
  446. $sql .= " FROM ".MAIN_DB_PREFIX."tva as f";
  447. if ($year) {
  448. $sql .= " WHERE f.datev >= '".$this->db->escape($year)."-01-01' AND f.datev <= '".$this->db->escape($year)."-12-31' ";
  449. }
  450. $result = $this->db->query($sql);
  451. if ($result) {
  452. if ($this->db->num_rows($result)) {
  453. $obj = $this->db->fetch_object($result);
  454. $ret = $obj->amount;
  455. $this->db->free($result);
  456. return $ret;
  457. } else {
  458. $this->db->free($result);
  459. return 0;
  460. }
  461. } else {
  462. print $this->db->lasterror();
  463. return -1;
  464. }
  465. }
  466. /**
  467. * Create in database
  468. *
  469. * @param User $user Object user that insert
  470. * @return int <0 if KO, rowid in tva table if OK
  471. */
  472. public function addPayment($user)
  473. {
  474. global $conf, $langs;
  475. $this->db->begin();
  476. // Clean parameters
  477. $this->amount = price2num(trim($this->amount));
  478. $this->label = trim($this->label);
  479. $this->note = trim($this->note);
  480. $this->num_payment = trim($this->num_payment);
  481. $this->fk_bank = (int) $this->fk_bank;
  482. $this->fk_user_creat = (int) $this->fk_user_creat;
  483. $this->fk_user_modif = (int) $this->fk_user_modif;
  484. if (empty($this->datec)) {
  485. $this->datec = dol_now();
  486. }
  487. // Check parameters
  488. if (!$this->label) {
  489. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
  490. return -3;
  491. }
  492. if ($this->amount == '') {
  493. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
  494. return -4;
  495. }
  496. if (!empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0)) {
  497. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Account"));
  498. return -5;
  499. }
  500. if (!empty($conf->banque->enabled) && (empty($this->type_payment) || $this->type_payment <= 0)) {
  501. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
  502. return -5;
  503. }
  504. // Insert into llx_tva
  505. $sql = "INSERT INTO ".MAIN_DB_PREFIX."tva (";
  506. $sql .= "datec";
  507. $sql .= ", datep";
  508. $sql .= ", datev";
  509. $sql .= ", amount";
  510. $sql .= ", fk_typepayment";
  511. $sql .= ", num_payment";
  512. if ($this->note) {
  513. $sql .= ", note";
  514. }
  515. if ($this->label) {
  516. $sql .= ", label";
  517. }
  518. $sql .= ", fk_user_creat";
  519. $sql .= ", fk_bank";
  520. $sql .= ", entity";
  521. $sql .= ") ";
  522. $sql .= " VALUES (";
  523. $sql .= " '".$this->db->idate($this->datec)."'";
  524. $sql .= ", '".$this->db->idate($this->datep)."'";
  525. $sql .= ", '".$this->db->idate($this->datev)."'";
  526. $sql .= ", ".((float) $this->amount);
  527. $sql .= ", '".$this->db->escape($this->type_payment)."'";
  528. $sql .= ", '".$this->db->escape($this->num_payment)."'";
  529. if ($this->note) {
  530. $sql .= ", '".$this->db->escape($this->note)."'";
  531. }
  532. if ($this->label) {
  533. $sql .= ", '".$this->db->escape($this->label)."'";
  534. }
  535. $sql .= ", '".$this->db->escape($user->id)."'";
  536. $sql .= ", NULL";
  537. $sql .= ", ".((int) $conf->entity);
  538. $sql .= ")";
  539. dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
  540. $result = $this->db->query($sql);
  541. if ($result) {
  542. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."tva"); // TODO should be called 'payment_vat'
  543. // Call trigger
  544. //XXX: Should be done just befor commit no ?
  545. $result = $this->call_trigger('TVA_ADDPAYMENT', $user);
  546. if ($result < 0) {
  547. $this->id = 0;
  548. $ok = 0;
  549. }
  550. // End call triggers
  551. if ($this->id > 0) {
  552. $ok = 1;
  553. if (!empty($conf->banque->enabled) && !empty($this->amount)) {
  554. // Insert into llx_bank
  555. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  556. $acc = new Account($this->db);
  557. $result = $acc->fetch($this->accountid);
  558. if ($result <= 0) {
  559. dol_print_error($this->db);
  560. }
  561. if ($this->amount > 0) {
  562. $bank_line_id = $acc->addline($this->datep, $this->type_payment, $this->label, -abs($this->amount), $this->num_payment, '', $user);
  563. } else {
  564. $bank_line_id = $acc->addline($this->datep, $this->type_payment, $this->label, abs($this->amount), $this->num_payment, '', $user);
  565. }
  566. // Update fk_bank into llx_tva. So we know vat line used to generate bank transaction
  567. if ($bank_line_id > 0) {
  568. $this->update_fk_bank($bank_line_id);
  569. } else {
  570. $this->error = $acc->error;
  571. $ok = 0;
  572. }
  573. // Update links
  574. $result = $acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/tva/card.php?id=', "(VATPayment)", "payment_vat");
  575. if ($result < 0) {
  576. $this->error = $acc->error;
  577. $ok = 0;
  578. }
  579. }
  580. if ($ok) {
  581. $this->db->commit();
  582. return $this->id;
  583. } else {
  584. $this->db->rollback();
  585. return -3;
  586. }
  587. } else {
  588. $this->db->rollback();
  589. return -2;
  590. }
  591. } else {
  592. $this->error = $this->db->error();
  593. $this->db->rollback();
  594. return -1;
  595. }
  596. }
  597. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  598. /**
  599. * Update link between payment tva and line generate into llx_bank
  600. *
  601. * @param int $id_bank Id bank account
  602. * @return int <0 if KO, >0 if OK
  603. */
  604. public function update_fk_bank($id_bank)
  605. {
  606. // phpcs:enable
  607. $sql = 'UPDATE '.MAIN_DB_PREFIX.'tva SET fk_bank = '.(int) $id_bank;
  608. $sql .= ' WHERE rowid = '.(int) $this->id;
  609. $result = $this->db->query($sql);
  610. if ($result) {
  611. return 1;
  612. } else {
  613. dol_print_error($this->db);
  614. return -1;
  615. }
  616. }
  617. /**
  618. * Send name clicable (with possibly the picto)
  619. *
  620. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  621. * @param string $option link option
  622. * @param int $notooltip 1=Disable tooltip
  623. * @param string $morecss More CSS
  624. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  625. * @return string Chaine with URL
  626. */
  627. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  628. {
  629. global $langs, $conf;
  630. if (!empty($conf->dol_no_mouse_hover)) {
  631. $notooltip = 1; // Force disable tooltips
  632. }
  633. $result = '';
  634. $label = '<u>'.$langs->trans("ShowVatPayment").'</u>';
  635. $label .= '<br>';
  636. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  637. if (!empty($this->label)) {
  638. $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
  639. }
  640. $url = DOL_URL_ROOT.'/compta/tva/card.php?id='.$this->id;
  641. if ($option != 'nolink') {
  642. // Add param to save lastsearch_values or not
  643. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  644. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  645. $add_save_lastsearch_values = 1;
  646. }
  647. if ($add_save_lastsearch_values) {
  648. $url .= '&save_lastsearch_values=1';
  649. }
  650. }
  651. $linkclose = '';
  652. if (empty($notooltip)) {
  653. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  654. $label = $langs->trans("ShowMyObject");
  655. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  656. }
  657. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  658. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  659. } else {
  660. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  661. }
  662. $linkstart = '<a href="'.$url.'"';
  663. $linkstart .= $linkclose.'>';
  664. $linkend = '</a>';
  665. $picto = 'payment';
  666. $result .= $linkstart;
  667. if ($withpicto) {
  668. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  669. }
  670. if ($withpicto != 2) {
  671. $result .= $this->ref;
  672. }
  673. $result .= $linkend;
  674. return $result;
  675. }
  676. /**
  677. * Return amount of payments already done
  678. *
  679. * @return int Amount of payment already done, <0 if KO
  680. */
  681. public function getSommePaiement()
  682. {
  683. $table = 'payment_vat';
  684. $field = 'fk_tva';
  685. $sql = 'SELECT sum(amount) as amount';
  686. $sql .= ' FROM '.MAIN_DB_PREFIX.$table;
  687. $sql .= " WHERE ".$field." = ".((int) $this->id);
  688. dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
  689. $resql = $this->db->query($sql);
  690. if ($resql) {
  691. $amount = 0;
  692. $obj = $this->db->fetch_object($resql);
  693. if ($obj) {
  694. $amount = $obj->amount ? $obj->amount : 0;
  695. }
  696. $this->db->free($resql);
  697. return $amount;
  698. } else {
  699. return -1;
  700. }
  701. }
  702. /**
  703. * Informations of vat payment object
  704. *
  705. * @param int $id Id of vat payment
  706. * @return int <0 if KO, >0 if OK
  707. */
  708. public function info($id)
  709. {
  710. $sql = "SELECT t.rowid, t.tms, t.fk_user_modif, t.datec, t.fk_user_creat";
  711. $sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
  712. $sql .= " WHERE t.rowid = ".(int) $id;
  713. dol_syslog(get_class($this)."::info", LOG_DEBUG);
  714. $result = $this->db->query($sql);
  715. if ($result) {
  716. if ($this->db->num_rows($result)) {
  717. $obj = $this->db->fetch_object($result);
  718. $this->id = $obj->rowid;
  719. if ($obj->fk_user_creat) {
  720. $cuser = new User($this->db);
  721. $cuser->fetch($obj->fk_user_creat);
  722. $this->user_creation = $cuser;
  723. }
  724. if ($obj->fk_user_modif) {
  725. $muser = new User($this->db);
  726. $muser->fetch($obj->fk_user_modif);
  727. $this->user_modification = $muser;
  728. }
  729. $this->date_creation = $this->db->jdate($obj->datec);
  730. $this->date_modification = $this->db->jdate($obj->tms);
  731. $this->import_key = $obj->import_key;
  732. }
  733. $this->db->free($result);
  734. } else {
  735. dol_print_error($this->db);
  736. }
  737. }
  738. /**
  739. * Retourne le libelle du statut d'une TVA (impaye, payee)
  740. *
  741. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=short label + picto, 6=Long label + picto
  742. * @param double $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommand to put here amount payed if you have it, 1 otherwise)
  743. * @return string Label
  744. */
  745. public function getLibStatut($mode = 0, $alreadypaid = -1)
  746. {
  747. return $this->LibStatut($this->paye, $mode, $alreadypaid);
  748. }
  749. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  750. /**
  751. * Renvoi le libelle d'un statut donne
  752. *
  753. * @param int $status Id status
  754. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=short label + picto, 6=Long label + picto
  755. * @param double $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommand to put here amount payed if you have it, 1 otherwise)
  756. * @return string Label
  757. */
  758. public function LibStatut($status, $mode = 0, $alreadypaid = -1)
  759. {
  760. // phpcs:enable
  761. global $langs;
  762. // Load translation files required by the page
  763. $langs->loadLangs(array("customers", "bills"));
  764. // We reinit status array to force to redefine them because label may change according to properties values.
  765. $this->labelStatus = array();
  766. $this->labelStatusShort = array();
  767. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  768. global $langs;
  769. //$langs->load("mymodule");
  770. $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('BillStatusNotPaid');
  771. $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv('BillStatusPaid');
  772. if ($status == self::STATUS_UNPAID && $alreadypaid <> 0) {
  773. $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
  774. }
  775. $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('BillStatusNotPaid');
  776. $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv('BillStatusPaid');
  777. if ($status == self::STATUS_UNPAID && $alreadypaid <> 0) {
  778. $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
  779. }
  780. }
  781. $statusType = 'status1';
  782. if ($status == 0 && $alreadypaid <> 0) {
  783. $statusType = 'status3';
  784. }
  785. if ($status == 1) {
  786. $statusType = 'status6';
  787. }
  788. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  789. }
  790. }