tva.class.php 24 KB

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