PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/compta/sociales/class/paymentsocialcontribution.class.php

https://github.com/asterix14/dolibarr
PHP | 600 lines | 382 code | 95 blank | 123 comment | 64 complexity | 182c44692d71607c74d820ae426396dc MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2007 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/compta/sociales/class/paymentsocialcontribution.class.php
  20. * \ingroup facture
  21. * \brief File of class to manage payment of social contributions
  22. */
  23. require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
  24. /** \class PaymentSocialContribution
  25. * \brief Class to manage payments of social contributions
  26. */
  27. class PaymentSocialContribution extends CommonObject
  28. {
  29. public $element='paiementcharge'; //!< Id that identify managed objects
  30. public $table_element='paiementcharge'; //!< Name of table without prefix where object is stored
  31. var $id;
  32. var $ref;
  33. var $fk_charge;
  34. var $datec='';
  35. var $tms='';
  36. var $datep='';
  37. var $amount; // Total amount of payment
  38. var $amounts=array(); // Array of amounts
  39. var $fk_typepaiement;
  40. var $num_paiement;
  41. var $note;
  42. var $fk_bank;
  43. var $fk_user_creat;
  44. var $fk_user_modif;
  45. /**
  46. * Constructor
  47. *
  48. * @param DoliDB $DB Database handler
  49. */
  50. function PaymentSocialContribution($DB)
  51. {
  52. $this->db = $DB;
  53. return 1;
  54. }
  55. /**
  56. * Create payment of social contribution into database.
  57. * Use this->amounts to have list of lines for the payment
  58. *
  59. * @param User $user User making payment
  60. * @return int <0 if KO, id of payment if OK
  61. */
  62. function create($user)
  63. {
  64. global $conf, $langs;
  65. $error=0;
  66. $now=dol_now();
  67. // Validate parametres
  68. if (! $this->datepaye)
  69. {
  70. $this->error='ErrorBadValueForParameters';
  71. return -1;
  72. }
  73. // Clean parameters
  74. if (isset($this->fk_charge)) $this->fk_charge=trim($this->fk_charge);
  75. if (isset($this->amount)) $this->amount=trim($this->amount);
  76. if (isset($this->fk_typepaiement)) $this->fk_typepaiement=trim($this->fk_typepaiement);
  77. if (isset($this->num_paiement)) $this->num_paiement=trim($this->num_paiement);
  78. if (isset($this->note)) $this->note=trim($this->note);
  79. if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
  80. if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
  81. if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
  82. $totalamount = 0;
  83. foreach ($this->amounts as $key => $value) // How payment is dispatch
  84. {
  85. $newvalue = price2num($value,'MT');
  86. $this->amounts[$key] = $newvalue;
  87. $totalamount += $newvalue;
  88. }
  89. $totalamount = price2num($totalamount);
  90. // Check parameters
  91. if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
  92. $this->db->begin();
  93. if ($totalamount != 0)
  94. {
  95. $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiementcharge (fk_charge, datec, datep, amount,";
  96. $sql.= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
  97. $sql.= " VALUES ($this->chid, '".$this->db->idate($now)."',";
  98. $sql.= " '".$this->db->idate($this->datepaye)."',";
  99. $sql.= " ".$totalamount.",";
  100. $sql.= " ".$this->paiementtype.", '".$this->db->escape($this->num_paiement)."', '".$this->db->escape($this->note)."', ".$user->id.",";
  101. $sql.= " 0)";
  102. dol_syslog(get_class($this)."::create sql=".$sql);
  103. $resql=$this->db->query($sql);
  104. if ($resql)
  105. {
  106. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."paiementcharge");
  107. }
  108. else
  109. {
  110. $error++;
  111. }
  112. }
  113. if ($totalamount != 0 && ! $error)
  114. {
  115. $this->amount=$totalamount;
  116. $this->total=$totalamount; // deprecated
  117. $this->db->commit();
  118. return $this->id;
  119. }
  120. else
  121. {
  122. $this->error=$this->db->error();
  123. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  124. $this->db->rollback();
  125. return -1;
  126. }
  127. }
  128. /**
  129. * \brief Load object in memory from database
  130. * \param id id object
  131. * \return int <0 if KO, >0 if OK
  132. */
  133. function fetch($id)
  134. {
  135. global $langs;
  136. $sql = "SELECT";
  137. $sql.= " t.rowid,";
  138. $sql.= " t.fk_charge,";
  139. $sql.= " t.datec,";
  140. $sql.= " t.tms,";
  141. $sql.= " t.datep,";
  142. $sql.= " t.amount,";
  143. $sql.= " t.fk_typepaiement,";
  144. $sql.= " t.num_paiement,";
  145. $sql.= " t.note,";
  146. $sql.= " t.fk_bank,";
  147. $sql.= " t.fk_user_creat,";
  148. $sql.= " t.fk_user_modif,";
  149. $sql.= " pt.code as type_code, pt.libelle as type_libelle,";
  150. $sql.= ' b.fk_account';
  151. $sql.= " FROM (".MAIN_DB_PREFIX."c_paiement as pt, ".MAIN_DB_PREFIX."paiementcharge as t)";
  152. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
  153. $sql.= " WHERE t.rowid = ".$id." AND t.fk_typepaiement = pt.id";
  154. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  155. $resql=$this->db->query($sql);
  156. if ($resql)
  157. {
  158. if ($this->db->num_rows($resql))
  159. {
  160. $obj = $this->db->fetch_object($resql);
  161. $this->id = $obj->rowid;
  162. $this->ref = $obj->rowid;
  163. $this->fk_charge = $obj->fk_charge;
  164. $this->datec = $this->db->jdate($obj->datec);
  165. $this->tms = $this->db->jdate($obj->tms);
  166. $this->datep = $this->db->jdate($obj->datep);
  167. $this->amount = $obj->amount;
  168. $this->fk_typepaiement = $obj->fk_typepaiement;
  169. $this->num_paiement = $obj->num_paiement;
  170. $this->note = $obj->note;
  171. $this->fk_bank = $obj->fk_bank;
  172. $this->fk_user_creat = $obj->fk_user_creat;
  173. $this->fk_user_modif = $obj->fk_user_modif;
  174. $this->type_code = $obj->type_code;
  175. $this->type_libelle = $obj->type_libelle;
  176. $this->bank_account = $obj->fk_account;
  177. $this->bank_line = $obj->fk_bank;
  178. }
  179. $this->db->free($resql);
  180. return 1;
  181. }
  182. else
  183. {
  184. $this->error="Error ".$this->db->lasterror();
  185. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  186. return -1;
  187. }
  188. }
  189. /**
  190. * \brief Update database
  191. * \param user User that modify
  192. * \param notrigger 0=launch triggers after, 1=disable triggers
  193. * \return int <0 if KO, >0 if OK
  194. */
  195. function update($user=0, $notrigger=0)
  196. {
  197. global $conf, $langs;
  198. $error=0;
  199. // Clean parameters
  200. if (isset($this->fk_charge)) $this->fk_charge=trim($this->fk_charge);
  201. if (isset($this->amount)) $this->amount=trim($this->amount);
  202. if (isset($this->fk_typepaiement)) $this->fk_typepaiement=trim($this->fk_typepaiement);
  203. if (isset($this->num_paiement)) $this->num_paiement=trim($this->num_paiement);
  204. if (isset($this->note)) $this->note=trim($this->note);
  205. if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
  206. if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
  207. if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
  208. // Check parameters
  209. // Put here code to add control on parameters values
  210. // Update request
  211. $sql = "UPDATE ".MAIN_DB_PREFIX."paiementcharge SET";
  212. $sql.= " fk_charge=".(isset($this->fk_charge)?$this->fk_charge:"null").",";
  213. $sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
  214. $sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
  215. $sql.= " datep=".(dol_strlen($this->datep)!=0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
  216. $sql.= " amount=".(isset($this->amount)?$this->amount:"null").",";
  217. $sql.= " fk_typepaiement=".(isset($this->fk_typepaiement)?$this->fk_typepaiement:"null").",";
  218. $sql.= " num_paiement=".(isset($this->num_paiement)?"'".$this->db->escape($this->num_paiement)."'":"null").",";
  219. $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
  220. $sql.= " fk_bank=".(isset($this->fk_bank)?$this->fk_bank:"null").",";
  221. $sql.= " fk_user_creat=".(isset($this->fk_user_creat)?$this->fk_user_creat:"null").",";
  222. $sql.= " fk_user_modif=".(isset($this->fk_user_modif)?$this->fk_user_modif:"null")."";
  223. $sql.= " WHERE rowid=".$this->id;
  224. $this->db->begin();
  225. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  226. $resql = $this->db->query($sql);
  227. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  228. if (! $error)
  229. {
  230. if (! $notrigger)
  231. {
  232. // Uncomment this and change MYOBJECT to your own tag if you
  233. // want this action call a trigger.
  234. //// Call triggers
  235. //include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  236. //$interface=new Interfaces($this->db);
  237. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  238. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  239. //// End call triggers
  240. }
  241. }
  242. // Commit or rollback
  243. if ($error)
  244. {
  245. foreach($this->errors as $errmsg)
  246. {
  247. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  248. $this->error.=($this->error?', '.$errmsg:$errmsg);
  249. }
  250. $this->db->rollback();
  251. return -1*$error;
  252. }
  253. else
  254. {
  255. $this->db->commit();
  256. return 1;
  257. }
  258. }
  259. /**
  260. * \brief Delete object in database
  261. * \param user User that delete
  262. * \param notrigger 0=launch triggers after, 1=disable triggers
  263. * \return int <0 if KO, >0 if OK
  264. */
  265. function delete($user, $notrigger=0)
  266. {
  267. global $conf, $langs;
  268. $error=0;
  269. $this->db->begin();
  270. if (! $error)
  271. {
  272. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
  273. $sql.= " WHERE type='payment_sc' AND url_id=".$this->id;
  274. dol_syslog(get_class($this)."::delete sql=".$sql);
  275. $resql = $this->db->query($sql);
  276. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  277. }
  278. if (! $error)
  279. {
  280. $sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge";
  281. $sql.= " WHERE rowid=".$this->id;
  282. dol_syslog(get_class($this)."::delete sql=".$sql);
  283. $resql = $this->db->query($sql);
  284. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  285. }
  286. if (! $error)
  287. {
  288. if (! $notrigger)
  289. {
  290. // Uncomment this and change MYOBJECT to your own tag if you
  291. // want this action call a trigger.
  292. //// Call triggers
  293. //include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  294. //$interface=new Interfaces($this->db);
  295. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  296. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  297. //// End call triggers
  298. }
  299. }
  300. // Commit or rollback
  301. if ($error)
  302. {
  303. foreach($this->errors as $errmsg)
  304. {
  305. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  306. $this->error.=($this->error?', '.$errmsg:$errmsg);
  307. }
  308. $this->db->rollback();
  309. return -1*$error;
  310. }
  311. else
  312. {
  313. $this->db->commit();
  314. return 1;
  315. }
  316. }
  317. /**
  318. * \brief Load an object from its id and create a new one in database
  319. * \param fromid Id of object to clone
  320. * \return int New id of clone
  321. */
  322. function createFromClone($fromid)
  323. {
  324. global $user,$langs;
  325. $error=0;
  326. $object=new PaymentSocialContribution($this->db);
  327. $this->db->begin();
  328. // Load source object
  329. $object->fetch($fromid);
  330. $object->id=0;
  331. $object->statut=0;
  332. // Clear fields
  333. // ...
  334. // Create clone
  335. $result=$object->create($user);
  336. // Other options
  337. if ($result < 0)
  338. {
  339. $this->error=$object->error;
  340. $error++;
  341. }
  342. if (! $error)
  343. {
  344. }
  345. // End
  346. if (! $error)
  347. {
  348. $this->db->commit();
  349. return $object->id;
  350. }
  351. else
  352. {
  353. $this->db->rollback();
  354. return -1;
  355. }
  356. }
  357. /**
  358. * Initialise an instance with random values.
  359. * Used to build previews or test instances.
  360. * id must be 0 if object instance is a specimen.
  361. *
  362. * @return void
  363. */
  364. function initAsSpecimen()
  365. {
  366. $this->id=0;
  367. $this->fk_charge='';
  368. $this->datec='';
  369. $this->tms='';
  370. $this->datep='';
  371. $this->amount='';
  372. $this->fk_typepaiement='';
  373. $this->num_paiement='';
  374. $this->note='';
  375. $this->fk_bank='';
  376. $this->fk_user_creat='';
  377. $this->fk_user_modif='';
  378. }
  379. /**
  380. * Add record into bank for payment with links between this bank record and invoices of payment.
  381. * All payment properties must have been set first like after a call to create().
  382. * @param user Object of user making payment
  383. * @param mode 'payment_sc'
  384. * @param label Label to use in bank record
  385. * @param accountid Id of bank account to do link with
  386. * @param emetteur_nom Name of transmitter
  387. * @param emetteur_banque Name of bank
  388. * @return int <0 if KO, >0 if OK
  389. */
  390. function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque)
  391. {
  392. global $conf;
  393. $error=0;
  394. if ($conf->banque->enabled)
  395. {
  396. require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php');
  397. $acc = new Account($this->db);
  398. $acc->fetch($accountid);
  399. $total=$this->total;
  400. if ($mode == 'payment_sc') $total=-$total;
  401. // Insert payment into llx_bank
  402. $bank_line_id = $acc->addline($this->datepaye,
  403. $this->paiementtype, // Payment mode id or code ("CHQ or VIR for example")
  404. $label,
  405. $total,
  406. $this->num_paiement,
  407. '',
  408. $user,
  409. $emetteur_nom,
  410. $emetteur_banque);
  411. // Mise a jour fk_bank dans llx_paiement.
  412. // On connait ainsi le paiement qui a genere l'ecriture bancaire
  413. if ($bank_line_id > 0)
  414. {
  415. $result=$this->update_fk_bank($bank_line_id);
  416. if ($result <= 0)
  417. {
  418. $error++;
  419. dol_print_error($this->db);
  420. }
  421. // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction
  422. $url='';
  423. if ($mode == 'payment_sc') $url=DOL_URL_ROOT.'/compta/payment_sc/fiche.php?id=';
  424. if ($url)
  425. {
  426. $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
  427. if ($result <= 0)
  428. {
  429. $error++;
  430. dol_print_error($this->db);
  431. }
  432. }
  433. // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
  434. $linkaddedforthirdparty=array();
  435. foreach ($this->amounts as $key => $value)
  436. {
  437. if ($mode == 'payment_sc')
  438. {
  439. $socialcontrib = new ChargeSociales($this->db);
  440. $socialcontrib->fetch($key);
  441. $result=$acc->add_url_line($bank_line_id, $socialcontrib->id,
  442. DOL_URL_ROOT.'/compta/charges.php?id=', $socialcontrib->type_libelle.(($socialcontrib->lib && $socialcontrib->lib!=$socialcontrib->type_libelle)?' ('.$socialcontrib->lib.')':''),'sc');
  443. if ($result <= 0) dol_print_error($this->db);
  444. }
  445. }
  446. }
  447. else
  448. {
  449. $this->error=$acc->error;
  450. $error++;
  451. }
  452. }
  453. if (! $error)
  454. {
  455. return 1;
  456. }
  457. else
  458. {
  459. return -1;
  460. }
  461. }
  462. /**
  463. * \brief Mise a jour du lien entre le paiement de charge et la ligne dans llx_bank generee
  464. * \param id_bank Id de la banque
  465. * \return int >0 si OK, <=0 si KO
  466. */
  467. function update_fk_bank($id_bank)
  468. {
  469. $sql = "UPDATE llx_paiementcharge set fk_bank = ".$id_bank." where rowid = ".$this->id;
  470. dol_syslog(get_class($this)."::update_fk_bank sql=".$sql);
  471. $result = $this->db->query($sql);
  472. if ($result)
  473. {
  474. return 1;
  475. }
  476. else
  477. {
  478. $this->error=$this->db->error();
  479. dol_syslog(get_class($this)."::update_fk_bank ".$this->error, LOG_ERR);
  480. return 0;
  481. }
  482. }
  483. /**
  484. * \brief Renvoie nom clicable (avec eventuellement le picto)
  485. * \param withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  486. * \param maxlen Longueur max libelle
  487. * \return string Chaine avec URL
  488. */
  489. function getNomUrl($withpicto=0,$maxlen=0)
  490. {
  491. global $langs;
  492. $result='';
  493. if (empty($this->ref)) $this->ref=$this->lib;
  494. if (!empty($this->id))
  495. {
  496. $lien = '<a href="'.DOL_URL_ROOT.'/compta/payment_sc/fiche.php?id='.$this->id.'">';
  497. $lienfin='</a>';
  498. if ($withpicto) $result.=($lien.img_object($langs->trans("ShowPayment").': '.$this->ref,'payment').$lienfin.' ');
  499. if ($withpicto && $withpicto != 2) $result.=' ';
  500. if ($withpicto != 2) $result.=$lien.($maxlen?dol_trunc($this->ref,$maxlen):$this->ref).$lienfin;
  501. }
  502. return $result;
  503. }
  504. }
  505. ?>