salary.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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/salary.class.php
  21. * \ingroup salaries
  22. * \brief Class for salaries module payment
  23. */
  24. // Put here all includes required by your class file
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  26. /**
  27. * Class to manage salary payments
  28. */
  29. class Salary extends CommonObject
  30. {
  31. /**
  32. * @var string ID to identify managed object
  33. */
  34. public $element = 'salary';
  35. /**
  36. * @var string Name of table without prefix where object is stored
  37. */
  38. public $table_element = 'salary';
  39. /**
  40. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  41. */
  42. public $picto = 'salary';
  43. public $tms;
  44. // /**
  45. // * @var array List of child tables. To test if we can delete object.
  46. // */
  47. protected $childtables = array('payment_salary' => array('name'=>'SalaryPayment', 'fk_element'=>'fk_salary'));
  48. // /**
  49. // * @var array List of child tables. To know object to delete on cascade.
  50. // * If name matches '@ClassNAme:FilePathClass;ParentFkFieldName' it will
  51. // * call method deleteByParentField(parentId, ParentFkFieldName) to fetch and delete child object
  52. // */
  53. //protected $childtablesoncascade = array('mymodule_myobjectdet');
  54. /**
  55. * @var int User ID
  56. */
  57. public $fk_user;
  58. public $datep;
  59. public $datev;
  60. public $salary;
  61. public $amount;
  62. /**
  63. * @var int ID
  64. */
  65. public $fk_project;
  66. public $type_payment;
  67. /**
  68. * @var string salary payments label
  69. */
  70. public $label;
  71. public $datesp;
  72. public $dateep;
  73. /**
  74. * @var int ID
  75. */
  76. public $fk_bank;
  77. /**
  78. * @var int
  79. * @see $accountid
  80. */
  81. public $fk_account;
  82. /**
  83. * @var int
  84. */
  85. public $accountid;
  86. /**
  87. * @var int ID
  88. */
  89. public $fk_user_author;
  90. /**
  91. * @var int ID
  92. */
  93. public $fk_user_modif;
  94. /**
  95. * @var user User
  96. */
  97. public $user;
  98. /**
  99. * 1 if salary paid COMPLETELY, 0 otherwise (do not use it anymore, use statut and close_code)
  100. */
  101. public $paye;
  102. const STATUS_UNPAID = 0;
  103. const STATUS_PAID = 1;
  104. /**
  105. * Constructor
  106. *
  107. * @param DoliDB $db Database handler
  108. */
  109. public function __construct($db)
  110. {
  111. $this->db = $db;
  112. $this->element = 'salary';
  113. $this->table_element = 'salary';
  114. }
  115. /**
  116. * Update database
  117. *
  118. * @param User $user User that modify
  119. * @param int $notrigger 0=no, 1=yes (no update trigger)
  120. * @return int Return integer <0 if KO, >0 if OK
  121. */
  122. public function update($user = null, $notrigger = 0)
  123. {
  124. $error = 0;
  125. // Clean parameters
  126. $this->amount = trim($this->amount);
  127. $this->label = trim($this->label);
  128. $this->note = trim($this->note);
  129. // Check parameters
  130. if (empty($this->fk_user) || $this->fk_user < 0) {
  131. $this->error = 'ErrorBadParameter';
  132. return -1;
  133. }
  134. $this->db->begin();
  135. // Update request
  136. $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
  137. $sql .= " tms='".$this->db->idate(dol_now())."',";
  138. $sql .= " fk_user=".((int) $this->fk_user).",";
  139. /*$sql .= " datep='".$this->db->idate($this->datep)."',";
  140. $sql .= " datev='".$this->db->idate($this->datev)."',";*/
  141. $sql .= " amount=".price2num($this->amount).",";
  142. $sql .= " fk_projet=".((int) $this->fk_project).",";
  143. $sql .= " fk_typepayment=".((int) $this->type_payment).",";
  144. $sql .= " label='".$this->db->escape($this->label)."',";
  145. $sql .= " datesp='".$this->db->idate($this->datesp)."',";
  146. $sql .= " dateep='".$this->db->idate($this->dateep)."',";
  147. $sql .= " note='".$this->db->escape($this->note)."',";
  148. $sql .= " fk_bank=".($this->fk_bank > 0 ? (int) $this->fk_bank : "null").",";
  149. $sql .= " fk_user_author=".((int) $this->fk_user_author).",";
  150. $sql .= " fk_user_modif=".($this->fk_user_modif > 0 ? (int) $this->fk_user_modif : (int) $user->id);
  151. $sql .= " WHERE rowid=".((int) $this->id);
  152. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  153. $resql = $this->db->query($sql);
  154. if (!$resql) {
  155. $this->error = "Error ".$this->db->lasterror();
  156. return -1;
  157. }
  158. // Update extrafield
  159. if (!$error) {
  160. if (!$error) {
  161. $result = $this->insertExtraFields();
  162. if ($result < 0) {
  163. $error++;
  164. }
  165. }
  166. }
  167. if (!$notrigger) {
  168. // Call trigger
  169. $result = $this->call_trigger('SALARY_MODIFY', $user);
  170. if ($result < 0) {
  171. $error++;
  172. }
  173. // End call triggers
  174. }
  175. if (!$error) {
  176. $this->db->commit();
  177. return 1;
  178. } else {
  179. $this->db->rollback();
  180. return -1;
  181. }
  182. }
  183. /**
  184. * Load object in memory from database
  185. *
  186. * @param int $id id object
  187. * @param User $user User that load
  188. * @return int Return integer <0 if KO, >0 if OK
  189. */
  190. public function fetch($id, $user = null)
  191. {
  192. $sql = "SELECT";
  193. $sql .= " s.rowid,";
  194. $sql .= " s.tms,";
  195. $sql .= " s.fk_user,";
  196. $sql .= " s.datep,";
  197. $sql .= " s.datev,";
  198. $sql .= " s.amount,";
  199. $sql .= " s.fk_projet as fk_project,";
  200. $sql .= " s.fk_typepayment,";
  201. $sql .= " s.label,";
  202. $sql .= " s.datesp,";
  203. $sql .= " s.dateep,";
  204. $sql .= " s.note,";
  205. $sql .= " s.paye,";
  206. $sql .= " s.fk_bank,";
  207. $sql .= " s.fk_user_author,";
  208. $sql .= " s.fk_user_modif,";
  209. $sql .= " s.fk_account";
  210. $sql .= " FROM ".MAIN_DB_PREFIX."salary as s";
  211. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON s.fk_bank = b.rowid";
  212. $sql .= " WHERE s.rowid = ".((int) $id);
  213. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  214. $resql = $this->db->query($sql);
  215. if ($resql) {
  216. if ($this->db->num_rows($resql)) {
  217. $obj = $this->db->fetch_object($resql);
  218. $this->id = $obj->rowid;
  219. $this->ref = $obj->rowid;
  220. $this->tms = $this->db->jdate($obj->tms);
  221. $this->fk_user = $obj->fk_user;
  222. $this->datep = $this->db->jdate($obj->datep);
  223. $this->datev = $this->db->jdate($obj->datev);
  224. $this->amount = $obj->amount;
  225. $this->fk_project = $obj->fk_project;
  226. $this->type_payment = $obj->fk_typepayment;
  227. $this->label = $obj->label;
  228. $this->datesp = $this->db->jdate($obj->datesp);
  229. $this->dateep = $this->db->jdate($obj->dateep);
  230. $this->note = $obj->note;
  231. $this->paye = $obj->paye;
  232. $this->status = $obj->paye;
  233. $this->fk_bank = $obj->fk_bank;
  234. $this->fk_user_author = $obj->fk_user_author;
  235. $this->fk_user_modif = $obj->fk_user_modif;
  236. $this->fk_account = $obj->fk_account;
  237. $this->accountid = $obj->fk_account;
  238. // Retrieve all extrafield
  239. // fetch optionals attributes and labels
  240. $this->fetch_optionals();
  241. }
  242. $this->db->free($resql);
  243. return 1;
  244. } else {
  245. $this->error = "Error ".$this->db->lasterror();
  246. return -1;
  247. }
  248. }
  249. /**
  250. * Delete object in database
  251. *
  252. * @param User $user User that delete
  253. * @param bool $notrigger false=launch triggers after, true=disable triggers
  254. * @return int Return integer <0 if KO, >0 if OK
  255. */
  256. public function delete($user, $notrigger = 0)
  257. {
  258. return $this->deleteCommon($user, $notrigger);
  259. }
  260. /**
  261. * Initialise an instance with random values.
  262. * Used to build previews or test instances.
  263. * id must be 0 if object instance is a specimen.
  264. *
  265. * @return void
  266. */
  267. public function initAsSpecimen()
  268. {
  269. $this->id = 0;
  270. $this->tms = '';
  271. $this->fk_user = '';
  272. $this->datep = '';
  273. $this->datev = '';
  274. $this->amount = '';
  275. $this->label = '';
  276. $this->datesp = '';
  277. $this->dateep = '';
  278. $this->note = '';
  279. $this->fk_bank = '';
  280. $this->fk_user_author = '';
  281. $this->fk_user_modif = '';
  282. }
  283. /**
  284. * Create in database
  285. *
  286. * @param User $user User that create
  287. * @return int Return integer <0 if KO, >0 if OK
  288. */
  289. public function create($user)
  290. {
  291. global $conf, $langs;
  292. $error = 0;
  293. $now = dol_now();
  294. // Clean parameters
  295. $this->amount = price2num(trim($this->amount));
  296. $this->label = trim($this->label);
  297. $this->note = trim($this->note);
  298. $this->fk_bank = trim($this->fk_bank);
  299. $this->fk_user_author = trim($this->fk_user_author);
  300. $this->fk_user_modif = trim($this->fk_user_modif);
  301. $this->accountid = trim($this->accountid);
  302. $this->paye = trim($this->paye);
  303. // Check parameters
  304. if (!$this->label) {
  305. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
  306. return -3;
  307. }
  308. if ($this->fk_user < 0 || $this->fk_user == '') {
  309. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Employee"));
  310. return -4;
  311. }
  312. if ($this->amount == '') {
  313. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
  314. return -5;
  315. }
  316. /* if (isModEnabled("banque") && (empty($this->accountid) || $this->accountid <= 0))
  317. {
  318. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Account"));
  319. return -6;
  320. }
  321. if (isModEnabled("banque") && (empty($this->type_payment) || $this->type_payment <= 0))
  322. {
  323. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
  324. return -7;
  325. }*/
  326. $this->db->begin();
  327. // Insert into llx_salary
  328. $sql = "INSERT INTO ".MAIN_DB_PREFIX."salary (fk_user";
  329. //$sql .= ", datep";
  330. //$sql .= ", datev";
  331. $sql .= ", amount";
  332. $sql .= ", fk_projet";
  333. $sql .= ", salary";
  334. $sql .= ", fk_typepayment";
  335. $sql .= ", fk_account";
  336. if ($this->note) {
  337. $sql .= ", note";
  338. }
  339. $sql .= ", label";
  340. $sql .= ", datesp";
  341. $sql .= ", dateep";
  342. $sql .= ", fk_user_author";
  343. $sql .= ", datec";
  344. $sql .= ", fk_bank";
  345. $sql .= ", entity";
  346. $sql .= ") ";
  347. $sql .= " VALUES (";
  348. $sql .= "'".$this->db->escape($this->fk_user)."'";
  349. //$sql .= ", '".$this->db->idate($this->datep)."'";
  350. //$sql .= ", '".$this->db->idate($this->datev)."'";
  351. $sql .= ", ".((float) $this->amount);
  352. $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
  353. $sql .= ", ".($this->salary > 0 ? ((float) $this->salary) : "null");
  354. $sql .= ", ".($this->type_payment > 0 ? ((int) $this->type_payment) : 0);
  355. $sql .= ", ".($this->accountid > 0 ? ((int) $this->accountid) : "null");
  356. if ($this->note) {
  357. $sql .= ", '".$this->db->escape($this->note)."'";
  358. }
  359. $sql .= ", '".$this->db->escape($this->label)."'";
  360. $sql .= ", '".$this->db->idate($this->datesp)."'";
  361. $sql .= ", '".$this->db->idate($this->dateep)."'";
  362. $sql .= ", '".$this->db->escape($user->id)."'";
  363. $sql .= ", '".$this->db->idate($now)."'";
  364. $sql .= ", NULL";
  365. $sql .= ", ".((int) $conf->entity);
  366. $sql .= ")";
  367. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  368. $result = $this->db->query($sql);
  369. if ($result) {
  370. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."salary");
  371. if ($this->id > 0) {
  372. // Update extrafield
  373. if (!$error) {
  374. if (!$error) {
  375. $result = $this->insertExtraFields();
  376. if ($result < 0) {
  377. $error++;
  378. }
  379. }
  380. }
  381. // Call trigger
  382. $result = $this->call_trigger('SALARY_CREATE', $user);
  383. if ($result < 0) {
  384. $error++;
  385. }
  386. // End call triggers
  387. } else {
  388. $error++;
  389. }
  390. if (!$error) {
  391. $this->db->commit();
  392. return $this->id;
  393. } else {
  394. $this->db->rollback();
  395. return -2;
  396. }
  397. } else {
  398. $this->error = $this->db->error();
  399. $this->db->rollback();
  400. return -1;
  401. }
  402. }
  403. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  404. /**
  405. * Update link between payment salary and line generate into llx_bank
  406. *
  407. * @param int $id_bank Id bank account
  408. * @return int Return integer <0 if KO, >0 if OK
  409. */
  410. public function update_fk_bank($id_bank)
  411. {
  412. // phpcs:enable
  413. $sql = 'UPDATE '.MAIN_DB_PREFIX.'salary SET fk_bank = '.((int) $id_bank);
  414. $sql .= " WHERE rowid = ".((int) $this->id);
  415. $result = $this->db->query($sql);
  416. if ($result) {
  417. return 1;
  418. } else {
  419. dol_print_error($this->db);
  420. return -1;
  421. }
  422. }
  423. /**
  424. * getTooltipContentArray
  425. *
  426. * @param array $params params to construct tooltip data
  427. * @since v18
  428. * @return array
  429. */
  430. public function getTooltipContentArray($params)
  431. {
  432. global $langs;
  433. $langs->loadLangs(['salaries']);
  434. // Complete datas
  435. if (!empty($params['fromajaxtooltip']) && !isset($this->alreadypaid)) {
  436. // Load the alreadypaid field
  437. $this->alreadypaid = $this->getSommePaiement(0);
  438. }
  439. $datas = [];
  440. $datas['picto'] = '<u>'.$langs->trans("Salary").'</u>';
  441. if (isset($this->status) && isset($this->alreadypaid)) {
  442. $datas['picto'] .= ' '.$this->getLibStatut(5, $this->alreadypaid);
  443. }
  444. $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  445. return $datas;
  446. }
  447. /**
  448. * Send name clicable (with possibly the picto)
  449. *
  450. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  451. * @param string $option link option
  452. * @param int $notooltip 1=Disable tooltip
  453. * @param string $morecss Add more css on link
  454. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  455. * @return string Chaine with URL
  456. */
  457. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  458. {
  459. global $conf, $langs, $hookmanager;
  460. if (!empty($conf->dol_no_mouse_hover)) {
  461. $notooltip = 1;
  462. } // Force disable tooltips
  463. $result = '';
  464. $params = [
  465. 'id' => $this->id,
  466. 'objecttype' => $this->element,
  467. 'option' => $option,
  468. ];
  469. $classfortooltip = 'classfortooltip';
  470. $dataparams = '';
  471. if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
  472. $classfortooltip = 'classforajaxtooltip';
  473. $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
  474. $label = '';
  475. } else {
  476. $label = implode($this->getTooltipContentArray($params));
  477. }
  478. $url = DOL_URL_ROOT.'/salaries/card.php?id='.$this->id;
  479. if ($option != 'nolink') {
  480. // Add param to save lastsearch_values or not
  481. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  482. if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  483. $add_save_lastsearch_values = 1;
  484. }
  485. if ($add_save_lastsearch_values) {
  486. $url .= '&save_lastsearch_values=1';
  487. }
  488. }
  489. $linkclose = '';
  490. if (empty($notooltip)) {
  491. if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
  492. $label = $langs->trans("ShowMyObject");
  493. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  494. }
  495. $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
  496. $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
  497. } else {
  498. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  499. }
  500. $linkstart = '<a href="'.$url.'"';
  501. $linkstart .= $linkclose.'>';
  502. $linkend = '</a>';
  503. $result .= $linkstart;
  504. if ($withpicto) {
  505. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
  506. }
  507. if ($withpicto != 2) {
  508. $result .= $this->ref;
  509. }
  510. $result .= $linkend;
  511. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  512. global $action, $hookmanager;
  513. $hookmanager->initHooks(array('salarypayment'));
  514. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  515. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  516. if ($reshook > 0) {
  517. $result = $hookmanager->resPrint;
  518. } else {
  519. $result .= $hookmanager->resPrint;
  520. }
  521. return $result;
  522. }
  523. /**
  524. * Return amount of payments already done
  525. *
  526. * @param int $multicurrency Return multicurrency_amount instead of amount. -1=Return both.
  527. * @return float|int|array Amount of payment already done, <0 and set ->error if KO
  528. */
  529. public function getSommePaiement($multicurrency = 0)
  530. {
  531. $table = 'payment_salary';
  532. $field = 'fk_salary';
  533. $sql = "SELECT sum(amount) as amount";
  534. //sum(multicurrency_amount) as multicurrency_amount // Not yet supported
  535. $sql .= " FROM ".MAIN_DB_PREFIX.$table;
  536. $sql .= " WHERE ".$field." = ".((int) $this->id);
  537. dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
  538. $resql = $this->db->query($sql);
  539. if ($resql) {
  540. $obj = $this->db->fetch_object($resql);
  541. $this->db->free($resql);
  542. if ($obj) {
  543. if ($multicurrency < 0) {
  544. //$this->sumpayed = $obj->amount;
  545. //$this->sumpayed_multicurrency = $obj->multicurrency_amount;
  546. //return array('alreadypaid'=>(float) $obj->amount, 'alreadypaid_multicurrency'=>(float) $obj->multicurrency_amount);
  547. return array(); // Not yet supported
  548. } elseif ($multicurrency) {
  549. //$this->sumpayed_multicurrency = $obj->multicurrency_amount;
  550. //return (float) $obj->multicurrency_amount;
  551. return -1; // Not yet supported
  552. } else {
  553. //$this->sumpayed = $obj->amount;
  554. return (float) $obj->amount;
  555. }
  556. } else {
  557. return 0;
  558. }
  559. } else {
  560. $this->error = $this->db->lasterror();
  561. return -1;
  562. }
  563. }
  564. /**
  565. * Information on record
  566. *
  567. * @param int $id Id of record
  568. * @return void
  569. */
  570. public function info($id)
  571. {
  572. $sql = 'SELECT ps.rowid, ps.datec, ps.tms as datem, ps.fk_user_author, ps.fk_user_modif';
  573. $sql .= ' FROM '.MAIN_DB_PREFIX.'salary as ps';
  574. $sql .= ' WHERE ps.rowid = '.((int) $id);
  575. dol_syslog(get_class($this).'::info', LOG_DEBUG);
  576. $result = $this->db->query($sql);
  577. if ($result) {
  578. if ($this->db->num_rows($result)) {
  579. $obj = $this->db->fetch_object($result);
  580. $this->id = $obj->rowid;
  581. $this->user_creation_id = $obj->fk_user_author;
  582. $this->user_modification_id = $obj->fk_user_modif;
  583. $this->date_creation = $this->db->jdate($obj->datec);
  584. $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
  585. }
  586. $this->db->free($result);
  587. } else {
  588. dol_print_error($this->db);
  589. }
  590. }
  591. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  592. /**
  593. * Tag social contribution as payed completely
  594. *
  595. * @deprecated
  596. * @see setPaid()
  597. * @param User $user Object user making change
  598. * @return int Return integer <0 if KO, >0 if OK
  599. */
  600. public function set_paid($user)
  601. {
  602. // phpcs:enable
  603. dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
  604. return $this->setPaid($user);
  605. }
  606. /**
  607. * Tag social contribution as payed completely
  608. *
  609. * @param User $user Object user making change
  610. * @return int Return integer <0 if KO, >0 if OK
  611. */
  612. public function setPaid($user)
  613. {
  614. $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
  615. $sql .= " paye = 1";
  616. $sql .= " WHERE rowid = ".((int) $this->id);
  617. $return = $this->db->query($sql);
  618. if ($return) {
  619. $this->paye = 1;
  620. return 1;
  621. } else {
  622. return -1;
  623. }
  624. }
  625. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  626. /**
  627. * Remove tag payed on social contribution
  628. *
  629. * @param User $user Object user making change
  630. * @return int Return integer <0 if KO, >0 if OK
  631. */
  632. public function set_unpaid($user)
  633. {
  634. // phpcs:enable
  635. $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
  636. $sql .= " paye = 0";
  637. $sql .= " WHERE rowid = ".((int) $this->id);
  638. $return = $this->db->query($sql);
  639. if ($return) {
  640. $this->paye = 0;
  641. return 1;
  642. } else {
  643. return -1;
  644. }
  645. }
  646. /**
  647. * Return label of current status
  648. *
  649. * @param int $mode 0=label long, 1=labels short, 2=Picto + Label short, 3=Picto, 4=Picto + Label long, 5=Label short + Picto
  650. * @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)
  651. * @return string Label
  652. */
  653. public function getLibStatut($mode = 0, $alreadypaid = -1)
  654. {
  655. return $this->LibStatut($this->paye, $mode, $alreadypaid);
  656. }
  657. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  658. /**
  659. * Return label of a given status
  660. *
  661. * @param int $status Id status
  662. * @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
  663. * @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)
  664. * @return string Label
  665. */
  666. public function LibStatut($status, $mode = 0, $alreadypaid = -1)
  667. {
  668. // phpcs:enable
  669. global $langs;
  670. // Load translation files required by the page
  671. $langs->loadLangs(array("customers", "bills"));
  672. // We reinit status array to force to redefine them because label may change according to properties values.
  673. $this->labelStatus = array();
  674. $this->labelStatusShort = array();
  675. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  676. global $langs;
  677. //$langs->load("mymodule");
  678. $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('BillStatusNotPaid');
  679. $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv('BillStatusPaid');
  680. if ($status == self::STATUS_UNPAID && $alreadypaid != 0) {
  681. $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
  682. }
  683. $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('BillStatusNotPaid');
  684. $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv('BillStatusPaid');
  685. if ($status == self::STATUS_UNPAID && $alreadypaid != 0) {
  686. $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
  687. }
  688. }
  689. $statusType = 'status1';
  690. if ($status == 0 && $alreadypaid != 0) {
  691. $statusType = 'status3';
  692. }
  693. if ($status == 1) {
  694. $statusType = 'status6';
  695. }
  696. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  697. }
  698. /**
  699. * Return clicable link of object (with eventually picto)
  700. *
  701. * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
  702. * @param array $arraydata Array of data
  703. * @return string HTML Code for Kanban thumb.
  704. */
  705. public function getKanbanView($option = '', $arraydata = null)
  706. {
  707. global $langs;
  708. $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
  709. $return = '<div class="box-flex-item box-flex-grow-zero">';
  710. $return .= '<div class="info-box info-box-sm">';
  711. $return .= '<span class="info-box-icon bg-infobox-action">';
  712. $return .= img_picto('', $this->picto);
  713. $return .= '</span>';
  714. $return .= '<div class="info-box-content">';
  715. $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
  716. if ($selected >= 0) {
  717. $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
  718. }
  719. if (!empty($arraydata['user']) && is_object($arraydata['user'])) {
  720. $return .= '<br><span class="info-box-label">'.$arraydata['user']->getNomUrl(1, '', 0, 0, 16, 0, '', 'maxwidth100').'</span>';
  721. }
  722. if (property_exists($this, 'amount')) {
  723. $return .= '<br><span class="info-box-label amount">'.price($this->amount).'</span>';
  724. if (property_exists($this, 'type_payment') && !empty($this->type_payment)) {
  725. $return .= ' <span class="info-box-label opacitymedium small">';
  726. if ($langs->trans("PaymentTypeShort".$this->type_payment) != "PaymentTypeShort".$this->type_payment) {
  727. $return .= $langs->trans("PaymentTypeShort".$this->type_payment);
  728. } elseif ($langs->trans("PaymentType".$this->type_payment) != "PaymentType".$this->type_payment) {
  729. $return .= $langs->trans("PaymentType".$this->type_payment);
  730. }
  731. $return .= '</span>';
  732. }
  733. }
  734. if (method_exists($this, 'LibStatut')) {
  735. $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3, $this->alreadypaid).'</div>';
  736. }
  737. $return .= '</div>';
  738. $return .= '</div>';
  739. $return .= '</div>';
  740. return $return;
  741. }
  742. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  743. /**
  744. * Create a withdrawal request for a direct debit order or a credit transfer order.
  745. * Use the remain to pay excluding all existing open direct debit requests.
  746. *
  747. * @param User $fuser User asking the direct debit transfer
  748. * @param float $amount Amount we request direct debit for
  749. * @param string $type 'direct-debit' or 'bank-transfer'
  750. * @param string $sourcetype Source ('facture' or 'supplier_invoice')
  751. * @param int $checkduplicateamongall 0=Default (check among open requests only to find if request already exists). 1=Check also among requests completely processed and cancel if at least 1 request exists whatever is its status.
  752. * @return int Return integer <0 if KO, 0 if a request already exists, >0 if OK
  753. */
  754. public function demande_prelevement($fuser, $amount = 0, $type = 'direct-debit', $sourcetype = 'salaire', $checkduplicateamongall = 0)
  755. {
  756. // phpcs:enable
  757. global $conf, $mysoc;
  758. $error = 0;
  759. dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
  760. if ($this->paye == 0) {
  761. require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
  762. $bac = new CompanyBankAccount($this->db);
  763. $bac->fetch(0, $mysoc->id);
  764. $sql = "SELECT count(rowid) as nb";
  765. $sql .= " FROM ".$this->db->prefix()."prelevement_demande";
  766. if ($type == 'salaire') {
  767. $sql .= " WHERE fk_salary = ".((int) $this->id);
  768. } else {
  769. $sql .= " WHERE fk_facture = ".((int) $this->id);
  770. }
  771. $sql .= " AND type = 'ban'"; // To exclude record done for some online payments
  772. if (empty($checkduplicateamongall)) {
  773. $sql .= " AND traite = 0";
  774. }
  775. dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
  776. $resql = $this->db->query($sql);
  777. if ($resql) {
  778. $obj = $this->db->fetch_object($resql);
  779. if ($obj && $obj->nb == 0) { // If no request found yet
  780. $now = dol_now();
  781. $totalpaid = $this->getSommePaiement();
  782. // $totalcreditnotes = $this->getSumCreditNotesUsed();
  783. // $totaldeposits = $this->getSumDepositsUsed();
  784. //print "totalpaid=".$totalpaid." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits;
  785. // We can also use bcadd to avoid pb with floating points
  786. // For example print 239.2 - 229.3 - 9.9; does not return 0.
  787. //$resteapayer=bcadd($this->total_ttc,$totalpaid,$conf->global->MAIN_MAX_DECIMALS_TOT);
  788. //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
  789. // if (empty($amount)) {
  790. // $amount = price2num($this->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
  791. // }
  792. if (is_numeric($amount) && $amount != 0) {
  793. $sql = 'INSERT INTO '.$this->db->prefix().'prelevement_demande(';
  794. if ($type == 'salaire') {
  795. $sql .= 'fk_salary, ';
  796. } else {
  797. $sql .= 'fk_facture, ';
  798. }
  799. $sql .= ' amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib, sourcetype, type, entity)';
  800. $sql .= " VALUES (".((int) $this->id);
  801. $sql .= ", ".((float) price2num($amount));
  802. $sql .= ", '".$this->db->idate($now)."'";
  803. $sql .= ", ".((int) $fuser->id);
  804. $sql .= ", '".$this->db->escape($bac->code_banque)."'";
  805. $sql .= ", '".$this->db->escape($bac->code_guichet)."'";
  806. $sql .= ", '".$this->db->escape($bac->number)."'";
  807. $sql .= ", '".$this->db->escape($bac->cle_rib)."'";
  808. $sql .= ", '".$this->db->escape($sourcetype)."'";
  809. $sql .= ", 'ban'";
  810. $sql .= ", ".((int) $conf->entity);
  811. $sql .= ")";
  812. dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
  813. $resql = $this->db->query($sql);
  814. if (!$resql) {
  815. $this->error = $this->db->lasterror();
  816. dol_syslog(get_class($this).'::demandeprelevement Erreur');
  817. $error++;
  818. }
  819. } else {
  820. $this->error = 'WithdrawRequestErrorNilAmount';
  821. dol_syslog(get_class($this).'::demandeprelevement WithdrawRequestErrorNilAmount');
  822. $error++;
  823. }
  824. if (!$error) {
  825. // Force payment mode of invoice to withdraw
  826. $payment_mode_id = dol_getIdFromCode($this->db, ($type == 'bank-transfer' ? 'VIR' : 'PRE'), 'c_paiement', 'code', 'id', 1);
  827. if ($payment_mode_id > 0) {
  828. $result = $this->setPaymentMethods($payment_mode_id);
  829. }
  830. }
  831. if ($error) {
  832. return -1;
  833. }
  834. return 1;
  835. } else {
  836. $this->error = "A request already exists";
  837. dol_syslog(get_class($this).'::demandeprelevement Can t create a request to generate a direct debit, a request already exists.');
  838. return 0;
  839. }
  840. } else {
  841. $this->error = $this->db->error();
  842. dol_syslog(get_class($this).'::demandeprelevement Error -2');
  843. return -2;
  844. }
  845. } else {
  846. $this->error = "Status of invoice does not allow this";
  847. dol_syslog(get_class($this)."::demandeprelevement ".$this->error." $this->status, $this->paye, $this->mode_reglement_id");
  848. return -3;
  849. }
  850. }
  851. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  852. /**
  853. * Remove a direct debit request or a credit transfer request
  854. *
  855. * @param User $fuser User making delete
  856. * @param int $did ID of request to delete
  857. * @return int Return integer <0 if OK, >0 if KO
  858. */
  859. public function demande_prelevement_delete($fuser, $did)
  860. {
  861. // phpcs:enable
  862. $sql = 'DELETE FROM '.$this->db->prefix().'prelevement_demande';
  863. $sql .= ' WHERE rowid = '.((int) $did);
  864. $sql .= ' AND traite = 0';
  865. if ($this->db->query($sql)) {
  866. return 0;
  867. } else {
  868. $this->error = $this->db->lasterror();
  869. dol_syslog(get_class($this).'::demande_prelevement_delete Error '.$this->error);
  870. return -1;
  871. }
  872. }
  873. }