paymentvarious.class.php 13 KB

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