paymentvarious.class.php 13 KB

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