tva.class.php 23 KB

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