paymentsalary.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. <?php
  2. /* Copyright (C) 2011-2022 Alexandre Spangaro <aspangaro@open-dsi.fr>
  3. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/salaries/class/paymentsalary.class.php
  21. * \ingroup salaries
  22. * \brief File of class to manage payment of salaries
  23. */
  24. // Put here all includes required by your class file
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
  27. /**
  28. * Class to manage payments of salaries
  29. */
  30. class PaymentSalary extends CommonObject
  31. {
  32. /**
  33. * @var string ID to identify managed object
  34. */
  35. public $element = 'payment_salary';
  36. /**
  37. * @var string Name of table without prefix where object is stored
  38. */
  39. public $table_element = 'payment_salary';
  40. /**
  41. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  42. */
  43. public $picto = 'payment';
  44. /**
  45. * @var int ID
  46. */
  47. public $fk_salary;
  48. public $datec = '';
  49. public $tms = '';
  50. /**
  51. * @var int|string date of payment
  52. * @deprecated
  53. * @see datep
  54. */
  55. public $datepaye = '';
  56. /**
  57. * @var int|string date of payment
  58. */
  59. public $datep = '';
  60. /**
  61. * @deprecated
  62. * @see $amount
  63. */
  64. public $total;
  65. public $amount; // Total amount of payment
  66. public $amounts = array(); // Array of amounts
  67. /**
  68. * @var int ID
  69. */
  70. public $fk_typepayment;
  71. /**
  72. * @var string
  73. * @deprecated
  74. */
  75. public $num_paiement;
  76. /**
  77. * @var string
  78. */
  79. public $num_payment;
  80. /**
  81. * @var int ID
  82. */
  83. public $fk_bank;
  84. /**
  85. * @var int ID of bank_line
  86. */
  87. public $bank_line;
  88. /**
  89. * @var int ID
  90. */
  91. public $fk_user_author;
  92. /**
  93. * @var int ID
  94. */
  95. public $fk_user_modif;
  96. /**
  97. * @var int Types paiement
  98. */
  99. public $type_code;
  100. /**
  101. * @var int Paiement label
  102. */
  103. public $type_label;
  104. /**
  105. * @var int bank account description
  106. */
  107. public $bank_account;
  108. /**
  109. * @var int|string validation date
  110. */
  111. public $datev = '';
  112. /**
  113. * @var int chid
  114. * @deprecated
  115. * @see $id from CommonObject
  116. */
  117. public $chid;
  118. /**
  119. * @var array
  120. */
  121. public $fields = array(
  122. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'index'=>1, 'position'=>1, 'comment'=>'Id'),
  123. );
  124. /**
  125. * Constructor
  126. *
  127. * @param DoliDB $db Database handler
  128. */
  129. public function __construct($db)
  130. {
  131. $this->db = $db;
  132. }
  133. /**
  134. * Create payment of salary into database.
  135. * Use this->amounts to have list of lines for the payment
  136. *
  137. * @param User $user User making payment
  138. * @param int $closepaidcontrib 1=Also close payed contributions to paid, 0=Do nothing more
  139. * @return int <0 if KO, id of payment if OK
  140. */
  141. public function create($user, $closepaidcontrib = 0)
  142. {
  143. global $conf;
  144. $error = 0;
  145. $now = dol_now();
  146. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  147. //deprecatd
  148. if (!empty($this->datepaye) && empty($this->datep)) {
  149. dol_syslog(__METHOD__.": using datepaye is deprecated, please use datep instead", LOG_WARNING);
  150. $this->datep = $this->datepaye;
  151. }
  152. // Validate parametres
  153. if (empty($this->datep)) {
  154. $this->error = 'ErrorBadValueForParameterCreatePaymentSalary';
  155. return -1;
  156. }
  157. // Clean parameters
  158. if (isset($this->fk_salary)) $this->fk_salary = (int) $this->fk_salary;
  159. if (isset($this->amount)) $this->amount = trim($this->amount);
  160. if (isset($this->fk_typepayment)) $this->fk_typepayment = (int) $this->fk_typepayment;
  161. if (isset($this->num_paiement)) $this->num_paiement = trim($this->num_paiement); // deprecated
  162. if (isset($this->num_payment)) $this->num_payment = trim($this->num_payment);
  163. if (isset($this->note)) $this->note = trim($this->note);
  164. if (isset($this->fk_bank)) $this->fk_bank = (int) $this->fk_bank;
  165. if (isset($this->fk_user_author)) $this->fk_user_author = (int) $this->fk_user_author;
  166. if (isset($this->fk_user_modif)) $this->fk_user_modif = (int) $this->fk_user_modif;
  167. $totalamount = 0;
  168. foreach ($this->amounts as $key => $value) { // How payment is dispatch
  169. $newvalue = price2num($value, 'MT');
  170. $this->amounts[$key] = $newvalue;
  171. $totalamount += $newvalue;
  172. }
  173. $totalamount = price2num($totalamount);
  174. // Check parameters
  175. if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
  176. $this->db->begin();
  177. if ($totalamount != 0) {
  178. $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_salary (entity, fk_salary, datec, datep, amount,";
  179. $sql .= " fk_typepayment, num_payment, note, fk_user_author, fk_bank)";
  180. $sql .= " VALUES (".((int) $conf->entity).", ".((int) $this->fk_salary).", '".$this->db->idate($now)."',";
  181. $sql .= " '".$this->db->idate($this->datep)."',";
  182. $sql .= " ".price2num($totalamount).",";
  183. $sql .= " ".((int) $this->fk_typepayment).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".((int) $user->id).",";
  184. $sql .= " 0)";
  185. $resql = $this->db->query($sql);
  186. if ($resql) {
  187. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_salary");
  188. // Insere tableau des montants / factures
  189. foreach ($this->amounts as $key => $amount) {
  190. $contribid = $key;
  191. if (is_numeric($amount) && $amount <> 0) {
  192. $amount = price2num($amount);
  193. // If we want to closed payed invoices
  194. if ($closepaidcontrib) {
  195. $tmpsalary = new Salary($this->db);
  196. $tmpsalary->fetch($contribid);
  197. $paiement = $tmpsalary->getSommePaiement();
  198. //$creditnotes=$tmpsalary->getSumCreditNotesUsed();
  199. $creditnotes = 0;
  200. //$deposits=$tmpsalary->getSumDepositsUsed();
  201. $deposits = 0;
  202. $alreadypayed = price2num($paiement + $creditnotes + $deposits, 'MT');
  203. $remaintopay = price2num($tmpsalary->amount - $paiement - $creditnotes - $deposits, 'MT');
  204. if ($remaintopay == 0) {
  205. $result = $tmpsalary->setPaid($user);
  206. } else {
  207. dol_syslog("Remain to pay for conrib ".$contribid." not null. We do nothing.");
  208. }
  209. }
  210. }
  211. }
  212. } else {
  213. $error++;
  214. }
  215. }
  216. $result = $this->call_trigger('PAYMENTSALARY_CREATE', $user);
  217. if ($result < 0) $error++;
  218. if ($totalamount != 0 && !$error) {
  219. $this->amount = $totalamount;
  220. $this->total = $totalamount; // deprecated
  221. $this->db->commit();
  222. return $this->id;
  223. } else {
  224. $this->error = $this->db->error();
  225. $this->db->rollback();
  226. return -1;
  227. }
  228. }
  229. /**
  230. * Load object in memory from database
  231. *
  232. * @param int $id Id object
  233. * @return int <0 if KO, >0 if OK
  234. */
  235. public function fetch($id)
  236. {
  237. global $langs;
  238. $sql = "SELECT";
  239. $sql .= " t.rowid,";
  240. $sql .= " t.fk_salary,";
  241. $sql .= " t.datec,";
  242. $sql .= " t.tms,";
  243. $sql .= " t.datep,";
  244. $sql .= " t.amount,";
  245. $sql .= " t.fk_typepayment,";
  246. $sql .= " t.num_payment as num_payment,";
  247. $sql .= " t.note,";
  248. $sql .= " t.fk_bank,";
  249. $sql .= " t.fk_user_author,";
  250. $sql .= " t.fk_user_modif,";
  251. $sql .= " pt.code as type_code, pt.libelle as type_label,";
  252. $sql .= ' b.fk_account';
  253. $sql .= " FROM ".MAIN_DB_PREFIX."payment_salary as t LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
  254. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
  255. $sql .= " WHERE t.rowid = ".((int) $id);
  256. // TODO link on entity of tax;
  257. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  258. $resql = $this->db->query($sql);
  259. if ($resql) {
  260. if ($this->db->num_rows($resql)) {
  261. $obj = $this->db->fetch_object($resql);
  262. $this->id = $obj->rowid;
  263. $this->ref = $obj->rowid;
  264. $this->fk_salary = $obj->fk_salary;
  265. $this->datec = $this->db->jdate($obj->datec);
  266. $this->tms = $this->db->jdate($obj->tms);
  267. $this->datepaye = $this->db->jdate($obj->datep);
  268. $this->amount = $obj->amount;
  269. $this->fk_typepayment = $obj->fk_typepayment;
  270. $this->num_paiement = $obj->num_payment;
  271. $this->num_payment = $obj->num_payment;
  272. $this->note = $obj->note;
  273. $this->note_private = $obj->note;
  274. $this->fk_bank = $obj->fk_bank;
  275. $this->fk_user_author = $obj->fk_user_author;
  276. $this->fk_user_modif = $obj->fk_user_modif;
  277. $this->type_code = $obj->type_code;
  278. $this->type_label = $obj->type_label;
  279. $this->bank_account = $obj->fk_account;
  280. $this->bank_line = $obj->fk_bank;
  281. }
  282. $this->db->free($resql);
  283. return 1;
  284. } else {
  285. $this->error = "Error ".$this->db->lasterror();
  286. return -1;
  287. }
  288. }
  289. /**
  290. * Update database
  291. *
  292. * @param User $user User that modify
  293. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  294. * @return int <0 if KO, >0 if OK
  295. */
  296. public function update($user = null, $notrigger = 0)
  297. {
  298. global $conf, $langs;
  299. $error = 0;
  300. // Clean parameters
  301. if (isset($this->fk_salary)) $this->fk_salary = (int) $this->fk_salary;
  302. if (isset($this->amount)) $this->amount = trim($this->amount);
  303. if (isset($this->fk_typepayment)) $this->fk_typepayment = (int) $this->fk_typepayment;
  304. if (isset($this->num_paiement)) $this->num_paiement = trim($this->num_paiement); // deprecated
  305. if (isset($this->num_payment)) $this->num_payment = trim($this->num_payment);
  306. if (isset($this->note)) $this->note = trim($this->note);
  307. if (isset($this->fk_bank)) $this->fk_bank = (int) $this->fk_bank;
  308. if (isset($this->fk_user_author)) $this->fk_user_author = (int) $this->fk_user_author;
  309. if (isset($this->fk_user_modif)) $this->fk_user_modif = (int) $this->fk_user_modif;
  310. // Check parameters
  311. // Put here code to add control on parameters values
  312. // Update request
  313. $sql = "UPDATE ".MAIN_DB_PREFIX."payment_salary SET";
  314. $sql .= " fk_salary=".(isset($this->fk_salary) ? $this->fk_salary : "null").",";
  315. $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
  316. $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
  317. $sql .= " datep=".(dol_strlen($this->datepaye) != 0 ? "'".$this->db->idate($this->datepaye)."'" : 'null').",";
  318. $sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
  319. $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
  320. $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
  321. $sql .= " note=".(isset($this->note) ? "'".$this->db->escape($this->note)."'" : "null").",";
  322. $sql .= " fk_bank=".(isset($this->fk_bank) ? ((int) $this->fk_bank) : "null").",";
  323. $sql .= " fk_user_author=".(isset($this->fk_user_author) ? ((int) $this->fk_user_author) : "null").",";
  324. $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null");
  325. $sql .= " WHERE rowid=".((int) $this->id);
  326. $this->db->begin();
  327. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  328. $resql = $this->db->query($sql);
  329. if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
  330. // Commit or rollback
  331. if ($error) {
  332. foreach ($this->errors as $errmsg) {
  333. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  334. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  335. }
  336. $this->db->rollback();
  337. return -1 * $error;
  338. } else {
  339. $this->db->commit();
  340. return 1;
  341. }
  342. }
  343. /**
  344. * Delete object in database
  345. *
  346. * @param User $user User that delete
  347. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  348. * @return int <0 if KO, >0 if OK
  349. */
  350. public function delete($user, $notrigger = 0)
  351. {
  352. global $conf, $langs;
  353. $error = 0;
  354. dol_syslog(get_class($this)."::delete");
  355. $this->db->begin();
  356. if ($this->bank_line > 0) {
  357. $accline = new AccountLine($this->db);
  358. $accline->fetch($this->bank_line);
  359. $result = $accline->delete();
  360. if ($result < 0) {
  361. $this->errors[] = $accline->error;
  362. $error++;
  363. }
  364. }
  365. if (!$error) {
  366. $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_salary";
  367. $sql .= " WHERE rowid=".((int) $this->id);
  368. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  369. $resql = $this->db->query($sql);
  370. if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
  371. }
  372. // Commit or rollback
  373. if ($error) {
  374. foreach ($this->errors as $errmsg) {
  375. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  376. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  377. }
  378. $this->db->rollback();
  379. return -1 * $error;
  380. } else {
  381. $this->db->commit();
  382. return 1;
  383. }
  384. }
  385. /**
  386. * Load an object from its id and create a new one in database
  387. *
  388. * @param User $user User making the clone
  389. * @param int $fromid Id of object to clone
  390. * @return int New id of clone
  391. */
  392. public function createFromClone(User $user, $fromid)
  393. {
  394. $error = 0;
  395. $object = new PaymentSalary($this->db);
  396. $this->db->begin();
  397. // Load source object
  398. $object->fetch($fromid);
  399. $object->id = 0;
  400. $object->statut = 0;
  401. // Clear fields
  402. // ...
  403. // Create clone
  404. $object->context['createfromclone'] = 'createfromclone';
  405. $result = $object->create($user);
  406. // Other options
  407. if ($result < 0) {
  408. $this->error = $object->error;
  409. $error++;
  410. }
  411. unset($object->context['createfromclone']);
  412. // End
  413. if (!$error) {
  414. $this->db->commit();
  415. return $object->id;
  416. } else {
  417. $this->db->rollback();
  418. return -1;
  419. }
  420. }
  421. /**
  422. * Initialise an instance with random values.
  423. * Used to build previews or test instances.
  424. * id must be 0 if object instance is a specimen.
  425. *
  426. * @return void
  427. */
  428. public function initAsSpecimen()
  429. {
  430. $this->id = 0;
  431. $this->fk_salary = '';
  432. $this->datec = '';
  433. $this->tms = '';
  434. $this->datepaye = '';
  435. $this->amount = '';
  436. $this->fk_typepayment = '';
  437. $this->num_payment = '';
  438. $this->note_private = '';
  439. $this->note_public = '';
  440. $this->fk_bank = '';
  441. $this->fk_user_author = '';
  442. $this->fk_user_modif = '';
  443. }
  444. /**
  445. * Add record into bank for payment with links between this bank record and invoices of payment.
  446. * All payment properties must have been set first like after a call to create().
  447. *
  448. * @param User $user Object of user making payment
  449. * @param string $mode 'payment_salary'
  450. * @param string $label Label to use in bank record
  451. * @param int $accountid Id of bank account to do link with
  452. * @param string $emetteur_nom Name of transmitter
  453. * @param string $emetteur_banque Name of bank
  454. * @return int <0 if KO, >0 if OK
  455. */
  456. public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
  457. {
  458. global $conf, $langs;
  459. // Clean data
  460. $this->num_payment = trim($this->num_payment ? $this->num_payment : $this->num_paiement);
  461. $error = 0;
  462. if (isModEnabled("banque")) {
  463. include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  464. $acc = new Account($this->db);
  465. $acc->fetch($accountid);
  466. $total = $this->amount;
  467. // Insert payment into llx_bank
  468. $bank_line_id = $acc->addline(
  469. $this->datep,
  470. $this->fk_typepayment, // Payment mode id or code ("CHQ or VIR for example")
  471. $label,
  472. -$total,
  473. $this->num_payment,
  474. '',
  475. $user,
  476. $emetteur_nom,
  477. $emetteur_banque,
  478. '',
  479. $this->datev
  480. );
  481. // Update fk_bank into llx_paiement_salary.
  482. // so we know the payment that was used to generated the bank entry.
  483. if ($bank_line_id > 0) {
  484. $result = $this->update_fk_bank($bank_line_id);
  485. if ($result <= 0) {
  486. $error++;
  487. dol_print_error($this->db);
  488. }
  489. // Add link 'payment_salary' in bank_url between payment and bank transaction
  490. $url = '';
  491. if ($mode == 'payment_salary') {
  492. $url = DOL_URL_ROOT.'/salaries/payment_salary/card.php?id=';
  493. }
  494. if ($url) {
  495. $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
  496. if ($result <= 0) {
  497. $error++;
  498. dol_print_error($this->db);
  499. }
  500. }
  501. // Add link 'user' in bank_url between user and bank transaction
  502. foreach ($this->amounts as $key => $value) {
  503. if (!$error) {
  504. if ($mode == 'payment_salary') {
  505. $salary = new Salary($this->db);
  506. $salary->fetch($key);
  507. $salary->fetch_user($salary->fk_user);
  508. $fuser = $salary->user;
  509. if ($fuser->id > 0) {
  510. $result = $acc->add_url_line(
  511. $bank_line_id,
  512. $fuser->id,
  513. DOL_URL_ROOT.'/user/card.php?id=',
  514. $fuser->getFullName($langs),
  515. 'user'
  516. );
  517. }
  518. if ($result <= 0) {
  519. $this->error = $this->db->lasterror();
  520. dol_syslog(get_class($this) . '::addPaymentToBank ' . $this->error);
  521. $error++;
  522. }
  523. }
  524. }
  525. }
  526. } else {
  527. $this->error = $acc->error;
  528. $error++;
  529. }
  530. }
  531. if (!$error) {
  532. return 1;
  533. } else {
  534. return -1;
  535. }
  536. }
  537. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  538. /**
  539. * Mise a jour du lien entre le paiement de salaire et la ligne dans llx_bank generee
  540. *
  541. * @param int $id_bank Id if bank
  542. * @return int >0 if OK, <=0 if KO
  543. */
  544. public function update_fk_bank($id_bank)
  545. {
  546. // phpcs:enable
  547. $sql = "UPDATE ".MAIN_DB_PREFIX."payment_salary SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
  548. dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
  549. $result = $this->db->query($sql);
  550. if ($result) {
  551. return 1;
  552. } else {
  553. $this->error = $this->db->error();
  554. return 0;
  555. }
  556. }
  557. /**
  558. * Updates the payment date.
  559. * Old name of function is update_date()
  560. *
  561. * @param int $date New date
  562. * @return int <0 if KO, 0 if OK
  563. */
  564. public function updatePaymentDate($date)
  565. {
  566. $error = 0;
  567. if (!empty($date)) {
  568. $this->db->begin();
  569. dol_syslog(get_class($this)."::updatePaymentDate with date = ".$date, LOG_DEBUG);
  570. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
  571. $sql .= " SET datep = '".$this->db->idate($date)."'";
  572. $sql .= " WHERE rowid = ".((int) $this->id);
  573. $result = $this->db->query($sql);
  574. if (!$result) {
  575. $error++;
  576. $this->error = 'Error -1 '.$this->db->error();
  577. }
  578. $type = $this->element;
  579. $sql = "UPDATE ".MAIN_DB_PREFIX.'bank';
  580. $sql .= " SET dateo = '".$this->db->idate($date)."', datev = '".$this->db->idate($date)."'";
  581. $sql .= " WHERE rowid IN (SELECT fk_bank FROM ".MAIN_DB_PREFIX."bank_url WHERE type = '".$this->db->escape($type)."' AND url_id = ".((int) $this->id).")";
  582. $sql .= " AND rappro = 0";
  583. $result = $this->db->query($sql);
  584. if (!$result) {
  585. $error++;
  586. $this->error = 'Error -1 '.$this->db->error();
  587. }
  588. if (!$error) {
  589. }
  590. if (!$error) {
  591. $this->datepaye = $date;
  592. $this->db->commit();
  593. return 0;
  594. } else {
  595. $this->db->rollback();
  596. return -2;
  597. }
  598. }
  599. return -1; //no date given or already validated
  600. }
  601. /**
  602. * Return the label of the status
  603. *
  604. * @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
  605. * @return string Label of status
  606. */
  607. public function getLibStatut($mode = 0)
  608. {
  609. return $this->LibStatut($this->statut, $mode);
  610. }
  611. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  612. /**
  613. * Return the status
  614. *
  615. * @param int $status Id status
  616. * @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
  617. * @return string Label of status
  618. */
  619. public function LibStatut($status, $mode = 0)
  620. {
  621. // phpcs:enable
  622. global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
  623. $langs->load('compta');
  624. /*if ($mode == 0)
  625. {
  626. if ($status == 0) return $langs->trans('ToValidate');
  627. if ($status == 1) return $langs->trans('Validated');
  628. }
  629. if ($mode == 1)
  630. {
  631. if ($status == 0) return $langs->trans('ToValidate');
  632. if ($status == 1) return $langs->trans('Validated');
  633. }
  634. if ($mode == 2)
  635. {
  636. if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
  637. if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
  638. }
  639. if ($mode == 3)
  640. {
  641. if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
  642. if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
  643. }
  644. if ($mode == 4)
  645. {
  646. if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
  647. if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
  648. }
  649. if ($mode == 5)
  650. {
  651. if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
  652. if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
  653. }
  654. if ($mode == 6)
  655. {
  656. if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
  657. if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
  658. }*/
  659. return '';
  660. }
  661. /**
  662. * Return clicable name (with picto eventually)
  663. *
  664. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  665. * @param int $maxlen Longueur max libelle
  666. * @param int $notooltip 1=Disable tooltip
  667. * @param string $morecss Add more css on link
  668. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  669. * @return string Chaine avec URL
  670. */
  671. public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  672. {
  673. global $conf, $langs, $hookmanager;
  674. $option = '';
  675. if (!empty($conf->dol_no_mouse_hover)) {
  676. $notooltip = 1; // Force disable tooltips
  677. }
  678. $result = '';
  679. $params = [
  680. 'id' => $this->id,
  681. 'objecttype' => $this->element.($this->module ? '@'.$this->module : ''),
  682. //'option' => $option,
  683. ];
  684. $classfortooltip = 'classfortooltip';
  685. $dataparams = '';
  686. if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
  687. $classfortooltip = 'classforajaxtooltip';
  688. $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
  689. $label = '';
  690. } else {
  691. $label = implode($this->getTooltipContentArray($params));
  692. }
  693. $url = DOL_URL_ROOT.'/salaries/payment_salary/card.php?id='.$this->id;
  694. if ($option !== 'nolink') {
  695. // Add param to save lastsearch_values or not
  696. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  697. if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  698. $add_save_lastsearch_values = 1;
  699. }
  700. if ($url && $add_save_lastsearch_values) {
  701. $url .= '&save_lastsearch_values=1';
  702. }
  703. }
  704. $linkclose = '';
  705. if (empty($notooltip)) {
  706. if (getDolGlobalInt('MAIN_OPTIMIZEFORTEXTBROWSER')) {
  707. $label = $langs->trans("SalaryPayment");
  708. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  709. }
  710. $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
  711. $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
  712. } else {
  713. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  714. }
  715. if ($option == 'nolink' || empty($url)) {
  716. $linkstart = '<span';
  717. } else {
  718. $linkstart = '<a href="'.$url.'"';
  719. }
  720. $linkstart .= $linkclose.'>';
  721. if ($option == 'nolink' || empty($url)) {
  722. $linkend = '</span>';
  723. } else {
  724. $linkend = '</a>';
  725. }
  726. $result .= $linkstart;
  727. if (empty($this->showphoto_on_popup)) {
  728. if ($withpicto) {
  729. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1);
  730. }
  731. }
  732. if ($withpicto != 2) {
  733. $result .= $this->ref;
  734. }
  735. $result .= $linkend;
  736. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  737. global $action, $hookmanager;
  738. $hookmanager->initHooks(array($this->element.'dao'));
  739. $parameters = array('id' => $this->id, 'getnomurl' => &$result);
  740. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  741. if ($reshook > 0) {
  742. $result = $hookmanager->resPrint;
  743. } else {
  744. $result .= $hookmanager->resPrint;
  745. }
  746. /*
  747. if (empty($this->ref)) $this->ref = $this->lib;
  748. $label = img_picto('', $this->picto).' <u>'.$langs->trans("SalaryPayment").'</u>';
  749. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  750. if (!empty($this->label)) {
  751. $labeltoshow = $this->label;
  752. $reg = array();
  753. if (preg_match('/^\((.*)\)$/i', $this->label, $reg)) {
  754. // Label generique car entre parentheses. On l'affiche en le traduisant
  755. if ($reg[1] == 'paiement') $reg[1] = 'Payment';
  756. $labeltoshow = $langs->trans($reg[1]);
  757. }
  758. $label .= '<br><b>'.$langs->trans('Label').':</b> '.$labeltoshow;
  759. }
  760. if ($this->datep) {
  761. $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->datep, 'day');
  762. }
  763. if (!empty($this->id)) {
  764. $link = '<a href="'.DOL_URL_ROOT.'/salaries/payment_salary/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  765. $linkend = '</a>';
  766. if ($withpicto) $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend);
  767. if ($withpicto != 2) $result .= $link.($maxlen ?dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
  768. }
  769. */
  770. return $result;
  771. }
  772. /**
  773. * getTooltipContentArray
  774. *
  775. * @param array $params params to construct tooltip data
  776. * @return array
  777. */
  778. public function getTooltipContentArray($params)
  779. {
  780. global $conf, $langs, $user;
  781. $langs->load('salaries');
  782. $datas = [];
  783. if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
  784. return ['optimize' => $langs->trans("SalaryPayment")];
  785. }
  786. if ($user->hasRight('salaries', 'read')) {
  787. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("SalaryPayment").'</u>';
  788. if (isset($this->status)) {
  789. $datas['status'] = ' '.$this->getLibStatut(5);
  790. }
  791. $datas['Ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  792. if (!empty($this->total_ttc)) {
  793. $datas['AmountTTC'] = '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
  794. }
  795. if (!empty($this->datepaye)) {
  796. $datas['Date'] = '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->datepaye, 'day');
  797. }
  798. }
  799. return $datas;
  800. }
  801. /**
  802. * Return clicable link of object (with eventually picto)
  803. *
  804. * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
  805. * @param array $arraydata Array of data
  806. * @return string HTML Code for Kanban thumb.
  807. */
  808. public function getKanbanView($option = '', $arraydata = null)
  809. {
  810. global $langs;
  811. $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
  812. $return = '<div class="box-flex-item box-flex-grow-zero">';
  813. $return .= '<div class="info-box info-box-sm">';
  814. $return .= '<span class="info-box-icon bg-infobox-action">';
  815. $return .= img_picto('', $this->picto);
  816. $return .= '</span>';
  817. $return .= '<div class="info-box-content">';
  818. $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
  819. if ($selected >= 0) {
  820. $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
  821. }
  822. if (property_exists($this, 'fk_bank')) {
  823. $return .= ' | <span class="info-box-label">'.$this->fk_bank.'</span>';
  824. }
  825. if (property_exists($this, 'fk_user_author')) {
  826. $return .= '<br><span class="info-box-status">'.$this->fk_user_author.'</span>';
  827. }
  828. if (property_exists($this, 'fk_typepayment')) {
  829. $return .= '<br><span class="opacitymedium">'.$langs->trans("PaymentMode").'</span> : <span class="info-box-label">'.$this->fk_typepayment.'</span>';
  830. }
  831. if (property_exists($this, 'amount')) {
  832. $return .= '<br><span class="opacitymedium">'.$langs->trans("Amount").'</span> : <span class="info-box-label amount">'.price($this->amount).'</span>';
  833. }
  834. $return .= '</div>';
  835. $return .= '</div>';
  836. $return .= '</div>';
  837. return $return;
  838. }
  839. }