paymentvarious.class.php 17 KB

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