paymentsalary.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. /* Copyright (C) 2011-2018 Alexandre Spangaro <aspangaro@zendsi.com>
  3. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/compta/salaries/class/paymentsalary.class.php
  20. * \ingroup salaries
  21. * \brief Class for salaries module payment
  22. */
  23. // Put here all includes required by your class file
  24. require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
  25. /**
  26. * Class to manage salary payments
  27. */
  28. class PaymentSalary extends CommonObject
  29. {
  30. /**
  31. * @var string ID to identify managed object
  32. */
  33. public $element='payment_salary';
  34. /**
  35. * @var string Name of table without prefix where object is stored
  36. */
  37. public $table_element='payment_salary';
  38. /**
  39. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  40. */
  41. public $picto='payment';
  42. public $tms;
  43. /**
  44. * @var int User ID
  45. */
  46. public $fk_user;
  47. public $datep;
  48. public $datev;
  49. public $amount;
  50. public $fk_project;
  51. public $type_payment;
  52. public $num_payment;
  53. /**
  54. * @var string salary payments label
  55. */
  56. public $label;
  57. public $datesp;
  58. public $dateep;
  59. public $fk_bank;
  60. public $fk_user_author;
  61. public $fk_user_modif;
  62. /**
  63. * Constructor
  64. *
  65. * @param DoliDB $db Database handler
  66. */
  67. function __construct($db)
  68. {
  69. $this->db = $db;
  70. $this->element = 'payment_salary';
  71. $this->table_element = 'payment_salary';
  72. }
  73. /**
  74. * Update database
  75. *
  76. * @param User $user User that modify
  77. * @param int $notrigger 0=no, 1=yes (no update trigger)
  78. * @return int <0 if KO, >0 if OK
  79. */
  80. function update($user=null, $notrigger=0)
  81. {
  82. global $conf, $langs;
  83. $error=0;
  84. // Clean parameters
  85. $this->fk_user=trim($this->fk_user);
  86. $this->amount=trim($this->amount);
  87. $this->label=trim($this->label);
  88. $this->note=trim($this->note);
  89. $this->fk_bank=trim($this->fk_bank);
  90. $this->fk_user_author=trim($this->fk_user_author);
  91. $this->fk_user_modif=trim($this->fk_user_modif);
  92. // Check parameters
  93. if (empty($this->fk_user) || $this->fk_user < 0)
  94. {
  95. $this->error='ErrorBadParameter';
  96. return -1;
  97. }
  98. $this->db->begin();
  99. // Update request
  100. $sql = "UPDATE ".MAIN_DB_PREFIX."payment_salary SET";
  101. $sql.= " tms='".$this->db->idate($this->tms)."',";
  102. $sql.= " fk_user=".$this->fk_user.",";
  103. $sql.= " datep='".$this->db->idate($this->datep)."',";
  104. $sql.= " datev='".$this->db->idate($this->datev)."',";
  105. $sql.= " amount=".price2num($this->amount).",";
  106. $sql.= " fk_projet='".$this->db->escape($this->fk_project)."',";
  107. $sql.= " fk_typepayment=".$this->fk_typepayment."',";
  108. $sql.= " num_payment='".$this->db->escape($this->num_payment)."',";
  109. $sql.= " label='".$this->db->escape($this->label)."',";
  110. $sql.= " datesp='".$this->db->idate($this->datesp)."',";
  111. $sql.= " dateep='".$this->db->idate($this->dateep)."',";
  112. $sql.= " note='".$this->db->escape($this->note)."',";
  113. $sql.= " fk_bank=".($this->fk_bank > 0 ? "'".$this->db->escape($this->fk_bank)."'":"null").",";
  114. $sql.= " fk_user_author=".$this->fk_user_author.",";
  115. $sql.= " fk_user_modif=".$this->fk_user_modif;
  116. $sql.= " WHERE rowid=".$this->id;
  117. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  118. $resql = $this->db->query($sql);
  119. if (! $resql)
  120. {
  121. $this->error="Error ".$this->db->lasterror();
  122. return -1;
  123. }
  124. if (! $notrigger)
  125. {
  126. // Call trigger
  127. $result=$this->call_trigger('PAYMENT_SALARY_MODIFY',$user);
  128. if ($result < 0) $error++;
  129. // End call triggers
  130. }
  131. if (! $error)
  132. {
  133. $this->db->commit();
  134. return 1;
  135. }
  136. else
  137. {
  138. $this->db->rollback();
  139. return -1;
  140. }
  141. }
  142. /**
  143. * Load object in memory from database
  144. *
  145. * @param int $id id object
  146. * @param User $user User that load
  147. * @return int <0 if KO, >0 if OK
  148. */
  149. function fetch($id, $user=null)
  150. {
  151. global $langs;
  152. $sql = "SELECT";
  153. $sql.= " s.rowid,";
  154. $sql.= " s.tms,";
  155. $sql.= " s.fk_user,";
  156. $sql.= " s.datep,";
  157. $sql.= " s.datev,";
  158. $sql.= " s.amount,";
  159. $sql.= " s.fk_projet as fk_project,";
  160. $sql.= " s.fk_typepayment,";
  161. $sql.= " s.num_payment,";
  162. $sql.= " s.label,";
  163. $sql.= " s.datesp,";
  164. $sql.= " s.dateep,";
  165. $sql.= " s.note,";
  166. $sql.= " s.fk_bank,";
  167. $sql.= " s.fk_user_author,";
  168. $sql.= " s.fk_user_modif,";
  169. $sql.= " b.fk_account,";
  170. $sql.= " b.fk_type,";
  171. $sql.= " b.rappro";
  172. $sql.= " FROM ".MAIN_DB_PREFIX."payment_salary as s";
  173. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON s.fk_bank = b.rowid";
  174. $sql.= " WHERE s.rowid = ".$id;
  175. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  176. $resql=$this->db->query($sql);
  177. if ($resql)
  178. {
  179. if ($this->db->num_rows($resql))
  180. {
  181. $obj = $this->db->fetch_object($resql);
  182. $this->id = $obj->rowid;
  183. $this->ref = $obj->rowid;
  184. $this->tms = $this->db->jdate($obj->tms);
  185. $this->fk_user = $obj->fk_user;
  186. $this->datep = $this->db->jdate($obj->datep);
  187. $this->datev = $this->db->jdate($obj->datev);
  188. $this->amount = $obj->amount;
  189. $this->fk_project = $obj->fk_project;
  190. $this->type_payement = $obj->fk_typepayment;
  191. $this->num_payment = $obj->num_payment;
  192. $this->label = $obj->label;
  193. $this->datesp = $this->db->jdate($obj->datesp);
  194. $this->dateep = $this->db->jdate($obj->dateep);
  195. $this->note = $obj->note;
  196. $this->fk_bank = $obj->fk_bank;
  197. $this->fk_user_author = $obj->fk_user_author;
  198. $this->fk_user_modif = $obj->fk_user_modif;
  199. $this->fk_account = $obj->fk_account;
  200. $this->fk_type = $obj->fk_type;
  201. $this->rappro = $obj->rappro;
  202. }
  203. $this->db->free($resql);
  204. return 1;
  205. }
  206. else
  207. {
  208. $this->error="Error ".$this->db->lasterror();
  209. return -1;
  210. }
  211. }
  212. /**
  213. * Delete object in database
  214. *
  215. * @param User $user User that delete
  216. * @return int <0 if KO, >0 if OK
  217. */
  218. function delete($user)
  219. {
  220. global $conf, $langs;
  221. $error=0;
  222. // Call trigger
  223. $result=$this->call_trigger('PAYMENT_SALARY_DELETE',$user);
  224. if ($result < 0) return -1;
  225. // End call triggers
  226. $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_salary";
  227. $sql.= " WHERE rowid=".$this->id;
  228. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  229. $resql = $this->db->query($sql);
  230. if (! $resql)
  231. {
  232. $this->error="Error ".$this->db->lasterror();
  233. return -1;
  234. }
  235. return 1;
  236. }
  237. /**
  238. * Initialise an instance with random values.
  239. * Used to build previews or test instances.
  240. * id must be 0 if object instance is a specimen.
  241. *
  242. * @return void
  243. */
  244. function initAsSpecimen()
  245. {
  246. $this->id=0;
  247. $this->tms='';
  248. $this->fk_user='';
  249. $this->datep='';
  250. $this->datev='';
  251. $this->amount='';
  252. $this->label='';
  253. $this->datesp='';
  254. $this->dateep='';
  255. $this->note='';
  256. $this->fk_bank='';
  257. $this->fk_user_author='';
  258. $this->fk_user_modif='';
  259. }
  260. /**
  261. * Create in database
  262. *
  263. * @param User $user User that create
  264. * @return int <0 if KO, >0 if OK
  265. */
  266. function create($user)
  267. {
  268. global $conf,$langs;
  269. $error=0;
  270. $now=dol_now();
  271. // Clean parameters
  272. $this->amount=price2num(trim($this->amount));
  273. $this->label=trim($this->label);
  274. $this->note=trim($this->note);
  275. $this->fk_bank=trim($this->fk_bank);
  276. $this->fk_user_author=trim($this->fk_user_author);
  277. $this->fk_user_modif=trim($this->fk_user_modif);
  278. // Check parameters
  279. if (! $this->label)
  280. {
  281. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label"));
  282. return -3;
  283. }
  284. if ($this->fk_user < 0 || $this->fk_user == '')
  285. {
  286. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Employee"));
  287. return -4;
  288. }
  289. if ($this->amount < 0 || $this->amount == '')
  290. {
  291. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Amount"));
  292. return -5;
  293. }
  294. if (! empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0))
  295. {
  296. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Account"));
  297. return -6;
  298. }
  299. if (! empty($conf->banque->enabled) && (empty($this->type_payment) || $this->type_payment <= 0))
  300. {
  301. $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
  302. return -7;
  303. }
  304. $this->db->begin();
  305. // Insert into llx_payment_salary
  306. $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_salary (fk_user";
  307. $sql.= ", datep";
  308. $sql.= ", datev";
  309. $sql.= ", amount";
  310. $sql.= ", fk_projet";
  311. $sql.= ", salary";
  312. $sql.= ", fk_typepayment";
  313. $sql.= ", num_payment";
  314. if ($this->note) $sql.= ", note";
  315. $sql.= ", label";
  316. $sql.= ", datesp";
  317. $sql.= ", dateep";
  318. $sql.= ", fk_user_author";
  319. $sql.= ", datec";
  320. $sql.= ", fk_bank";
  321. $sql.= ", entity";
  322. $sql.= ") ";
  323. $sql.= " VALUES (";
  324. $sql.= "'".$this->db->escape($this->fk_user)."'";
  325. $sql.= ", '".$this->db->idate($this->datep)."'";
  326. $sql.= ", '".$this->db->idate($this->datev)."'";
  327. $sql.= ", ".$this->amount;
  328. $sql.= ", ".($this->fk_project > 0? $this->fk_project : 0);
  329. $sql.= ", ".($this->salary > 0 ? $this->salary : "null");
  330. $sql.= ", ".$this->db->escape($this->type_payment);
  331. $sql.= ", '".$this->db->escape($this->num_payment)."'";
  332. if ($this->note) $sql.= ", '".$this->db->escape($this->note)."'";
  333. $sql.= ", '".$this->db->escape($this->label)."'";
  334. $sql.= ", '".$this->db->idate($this->datesp)."'";
  335. $sql.= ", '".$this->db->idate($this->dateep)."'";
  336. $sql.= ", '".$this->db->escape($user->id)."'";
  337. $sql.= ", '".$this->db->idate($now)."'";
  338. $sql.= ", NULL";
  339. $sql.= ", ".$conf->entity;
  340. $sql.= ")";
  341. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  342. $result = $this->db->query($sql);
  343. if ($result)
  344. {
  345. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_salary");
  346. if ($this->id > 0)
  347. {
  348. if (! empty($conf->banque->enabled) && ! empty($this->amount))
  349. {
  350. // Insert into llx_bank
  351. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  352. $acc = new Account($this->db);
  353. $result=$acc->fetch($this->accountid);
  354. if ($result <= 0) dol_print_error($this->db);
  355. // Insert payment into llx_bank
  356. // Add link 'payment_salary' in bank_url between payment and bank transaction
  357. $bank_line_id = $acc->addline(
  358. $this->datep,
  359. $this->type_payment,
  360. $this->label,
  361. -abs($this->amount),
  362. $this->num_payment,
  363. '',
  364. $user,
  365. '',
  366. '',
  367. '',
  368. $this->datev
  369. );
  370. // Update fk_bank into llx_paiement.
  371. // So we know the payment which has generate the banking ecriture
  372. if ($bank_line_id > 0)
  373. {
  374. $this->update_fk_bank($bank_line_id);
  375. }
  376. else
  377. {
  378. $this->error=$acc->error;
  379. $error++;
  380. }
  381. if (! $error)
  382. {
  383. // Add link 'payment_salary' in bank_url between payment and bank transaction
  384. $url=DOL_URL_ROOT.'/compta/salaries/card.php?id=';
  385. $result=$acc->add_url_line($bank_line_id, $this->id, $url, "(SalaryPayment)", "payment_salary");
  386. if ($result <= 0)
  387. {
  388. $this->error=$acc->error;
  389. $error++;
  390. }
  391. }
  392. $fuser=new User($this->db);
  393. $fuser->fetch($this->fk_user);
  394. // Add link 'user' in bank_url between operation and bank transaction
  395. $result=$acc->add_url_line(
  396. $bank_line_id,
  397. $this->fk_user,
  398. DOL_URL_ROOT.'/user/card.php?id=',
  399. $fuser->getFullName($langs),
  400. // $langs->trans("SalaryPayment").' '.$fuser->getFullName($langs).' '.dol_print_date($this->datesp,'dayrfc').' '.dol_print_date($this->dateep,'dayrfc'),
  401. 'user'
  402. );
  403. if ($result <= 0)
  404. {
  405. $this->error=$acc->error;
  406. $error++;
  407. }
  408. }
  409. // Call trigger
  410. $result=$this->call_trigger('PAYMENT_SALARY_CREATE',$user);
  411. if ($result < 0) $error++;
  412. // End call triggers
  413. }
  414. else $error++;
  415. if (! $error)
  416. {
  417. $this->db->commit();
  418. return $this->id;
  419. }
  420. else
  421. {
  422. $this->db->rollback();
  423. return -2;
  424. }
  425. }
  426. else
  427. {
  428. $this->error=$this->db->error();
  429. $this->db->rollback();
  430. return -1;
  431. }
  432. }
  433. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  434. /**
  435. * Update link between payment salary and line generate into llx_bank
  436. *
  437. * @param int $id_bank Id bank account
  438. * @return int <0 if KO, >0 if OK
  439. */
  440. function update_fk_bank($id_bank)
  441. {
  442. // phpcs:enable
  443. $sql = 'UPDATE '.MAIN_DB_PREFIX.'payment_salary SET fk_bank = '.$id_bank;
  444. $sql.= ' WHERE rowid = '.$this->id;
  445. $result = $this->db->query($sql);
  446. if ($result)
  447. {
  448. return 1;
  449. }
  450. else
  451. {
  452. dol_print_error($this->db);
  453. return -1;
  454. }
  455. }
  456. /**
  457. * Send name clicable (with possibly the picto)
  458. *
  459. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  460. * @param string $option link option
  461. * @return string Chaine with URL
  462. */
  463. function getNomUrl($withpicto=0,$option='')
  464. {
  465. global $langs;
  466. $result='';
  467. $label=$langs->trans("ShowSalaryPayment").': '.$this->ref;
  468. $linkstart = '<a href="'.DOL_URL_ROOT.'/compta/salaries/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  469. $linkend='</a>';
  470. $result .= $linkstart;
  471. if ($withpicto) $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);
  472. if ($withpicto != 2) $result.= $this->ref;
  473. $result .= $linkend;
  474. return $result;
  475. }
  476. /**
  477. * Information on record
  478. *
  479. * @param int $id Id of record
  480. * @return void
  481. */
  482. function info($id)
  483. {
  484. $sql = 'SELECT ps.rowid, ps.datec, ps.fk_user_author';
  485. $sql.= ' FROM '.MAIN_DB_PREFIX.'payment_salary as ps';
  486. $sql.= ' WHERE ps.rowid = '.$id;
  487. dol_syslog(get_class($this).'::info', LOG_DEBUG);
  488. $result = $this->db->query($sql);
  489. if ($result)
  490. {
  491. if ($this->db->num_rows($result))
  492. {
  493. $obj = $this->db->fetch_object($result);
  494. $this->id = $obj->rowid;
  495. if ($obj->fk_user_author)
  496. {
  497. $cuser = new User($this->db);
  498. $cuser->fetch($obj->fk_user_author);
  499. $this->user_creation = $cuser;
  500. }
  501. $this->date_creation = $this->db->jdate($obj->datec);
  502. }
  503. $this->db->free($result);
  504. }
  505. else
  506. {
  507. dol_print_error($this->db);
  508. }
  509. }
  510. /**
  511. * Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
  512. *
  513. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  514. * @return string Libelle
  515. */
  516. function getLibStatut($mode=0)
  517. {
  518. return $this->LibStatut($this->statut,$mode);
  519. }
  520. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  521. /**
  522. * Renvoi le libelle d'un statut donne
  523. *
  524. * @param int $status Statut
  525. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  526. * @return string Libelle du statut
  527. */
  528. function LibStatut($status,$mode=0)
  529. {
  530. // phpcs:enable
  531. global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
  532. $langs->load('compta');
  533. /*if ($mode == 0)
  534. {
  535. if ($status == 0) return $langs->trans('ToValidate');
  536. if ($status == 1) return $langs->trans('Validated');
  537. }
  538. if ($mode == 1)
  539. {
  540. if ($status == 0) return $langs->trans('ToValidate');
  541. if ($status == 1) return $langs->trans('Validated');
  542. }
  543. if ($mode == 2)
  544. {
  545. if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
  546. if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
  547. }
  548. if ($mode == 3)
  549. {
  550. if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
  551. if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
  552. }
  553. if ($mode == 4)
  554. {
  555. if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
  556. if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
  557. }
  558. if ($mode == 5)
  559. {
  560. if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
  561. if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
  562. }
  563. if ($mode == 6)
  564. {
  565. if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
  566. if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
  567. }*/
  568. return '';
  569. }
  570. }