paymentvarious.class.php 16 KB

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