PageRenderTime 32ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/compta/paiement/class/paiement.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 798 lines | 573 code | 77 blank | 148 comment | 123 complexity | edd477ccb0e7b0a8c4f460a2d2aafa61 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/compta/paiement/class/paiement.class.php
  21. * \ingroup facture
  22. * \brief File of class to manage payments of customers invoices
  23. */
  24. require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
  25. /** \class Paiement
  26. * \brief Classe permettant la gestion des paiements des factures clients
  27. */
  28. class Paiement extends nosqlDocument
  29. {
  30. public $element='payment';
  31. public $table_element='paiement';
  32. var $id;
  33. var $ref;
  34. var $facid;
  35. var $datepaye;
  36. var $total; // deprecated
  37. var $amount; // Total amount of payment
  38. var $amounts=array(); // Array of amounts
  39. var $author;
  40. var $paiementid; // Type de paiement. Stocke dans fk_paiement
  41. // de llx_paiement qui est lie aux types de
  42. //paiement de llx_c_paiement
  43. var $num_paiement; // Numero du CHQ, VIR, etc...
  44. var $bank_account; // Id compte bancaire du paiement
  45. var $bank_line; // Id de la ligne d'ecriture bancaire
  46. var $fk_account; // Id of bank account
  47. var $note;
  48. // fk_paiement dans llx_paiement est l'id du type de paiement (7 pour CHQ, ...)
  49. // fk_paiement dans llx_paiement_facture est le rowid du paiement
  50. /**
  51. * Constructor
  52. *
  53. * @param DoliDB $db Database handler
  54. */
  55. function __construct($db = '')
  56. {
  57. parent::__construct($db);
  58. }
  59. /**
  60. * Load payment from database
  61. *
  62. * @param int $id Id of payment to get
  63. * @return int <0 if KO, 0 if not found, >0 if OK
  64. */
  65. function fetch($id)
  66. {
  67. return parent::fetch($id);
  68. $sql = 'SELECT p.rowid, p.datep as dp, p.amount, p.statut, p.fk_bank,';
  69. $sql.= ' c.code as type_code, c.libelle as type_libelle,';
  70. $sql.= ' p.num_paiement, p.note,';
  71. $sql.= ' b.fk_account';
  72. $sql.= ' FROM '.MAIN_DB_PREFIX.'c_paiement as c, '.MAIN_DB_PREFIX.'paiement as p';
  73. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid ';
  74. $sql.= ' WHERE p.fk_paiement = c.id';
  75. $sql.= ' AND p.rowid = '.$id;
  76. dol_syslog(get_class($this)."::fetch sql=".$sql);
  77. $result = $this->db->query($sql);
  78. if ($result)
  79. {
  80. if ($this->db->num_rows($result))
  81. {
  82. $obj = $this->db->fetch_object($result);
  83. $this->id = $obj->rowid;
  84. $this->ref = $obj->rowid;
  85. $this->date = $this->db->jdate($obj->dp);
  86. $this->datepaye = $this->db->jdate($obj->dp);
  87. $this->numero = $obj->num_paiement;
  88. $this->montant = $obj->amount; // deprecated
  89. $this->amount = $obj->amount;
  90. $this->note = $obj->note;
  91. $this->type_libelle = $obj->type_libelle;
  92. $this->type_code = $obj->type_code;
  93. $this->statut = $obj->statut;
  94. $this->bank_account = $obj->fk_account;
  95. $this->bank_line = $obj->fk_bank;
  96. $this->db->free($result);
  97. return 1;
  98. }
  99. else
  100. {
  101. $this->db->free($result);
  102. return 0;
  103. }
  104. }
  105. else
  106. {
  107. dol_print_error($this->db);
  108. return -1;
  109. }
  110. }
  111. /**
  112. * Create payment of invoices into database.
  113. * Use this->amounts to have list of invoices for the payment
  114. *
  115. * @param User $user Object user
  116. * @param int $closepaidinvoices 1=Also close payed invoices to paid, 0=Do nothing more
  117. * @return int id of created payment, < 0 if error
  118. */
  119. function create($user,$closepaidinvoices=0)
  120. {
  121. global $conf, $langs;
  122. $error = 0;
  123. $now=dol_now();
  124. // Clean parameters
  125. $totalamount = 0;
  126. $atleastonepaymentnotnull = 0;
  127. foreach ($this->amounts as $key => $value) // How payment is dispatch
  128. {
  129. $newvalue = price2num($value,'MT');
  130. $this->amounts[$key] = $newvalue;
  131. $totalamount += $newvalue;
  132. if (! empty($newvalue)) $atleastonepaymentnotnull++;
  133. }
  134. $totalamount = price2num($totalamount);
  135. // Check parameters
  136. if (empty($totalamount) && empty($atleastonepaymentnotnull)) // We accept negative amounts for withdraw reject but not empty arrays
  137. {
  138. $this->error='TotalAmountEmpty';
  139. return -1;
  140. }
  141. $this->record();
  142. foreach ($this->amounts as $factureId => $amount) {
  143. $f = new Facture($this->db);
  144. $f->fetch($factureId);
  145. $f->addPayment();
  146. }
  147. // Appel des triggers
  148. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  149. $interface=new Interfaces($this->db);
  150. $result=$interface->run_triggers('PAYMENT_CUSTOMER_CREATE',$this,$user,$langs,$conf);
  151. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  152. // Fin appel triggers
  153. return $this->id;
  154. $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement (entity, datec, datep, amount, fk_paiement, num_paiement, note, fk_user_creat)";
  155. $sql.= " VALUES (".$conf->entity.", '".$this->db->idate($now)."', '".$this->db->idate($this->datepaye)."', '".$totalamount."', ".$this->paiementid.", '".$this->num_paiement."', '".$this->db->escape($this->note)."', ".$user->id.")";
  156. dol_syslog(get_class($this)."::Create insert paiement sql=".$sql);
  157. $resql = $this->db->query($sql);
  158. if ($resql)
  159. {
  160. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'paiement');
  161. // Insert links amount / invoices
  162. foreach ($this->amounts as $key => $amount)
  163. {
  164. $facid = $key;
  165. if (is_numeric($amount) && $amount <> 0)
  166. {
  167. $amount = price2num($amount);
  168. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiement_facture (fk_facture, fk_paiement, amount)';
  169. $sql .= ' VALUES ('.$facid.', '. $this->id.', \''.$amount.'\')';
  170. dol_syslog(get_class($this).'::Create Amount line '.$key.' insert paiement_facture sql='.$sql);
  171. $resql=$this->db->query($sql);
  172. if ($resql)
  173. {
  174. // If we want to closed payed invoices
  175. if ($closepaidinvoices)
  176. {
  177. $invoice=new Facture($this->db);
  178. $invoice->fetch($facid);
  179. $paiement = $invoice->getSommePaiement();
  180. $creditnotes=$invoice->getSumCreditNotesUsed();
  181. $deposits=$invoice->getSumDepositsUsed();
  182. $alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT');
  183. $remaintopay=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT');
  184. // If there is withdrawals request to do and not done yet, we wait before closing.
  185. $mustwait=0;
  186. $listofpayments=$invoice->getListOfPayments();
  187. foreach($listofpayments as $paym)
  188. {
  189. // This payment might be this one or a previous one
  190. if ($paym['type']=='PRE')
  191. {
  192. if (! empty($conf->prelevement->enabled))
  193. {
  194. // TODO Check if this payment has a withdraw request
  195. // if not, $mustwait++; // This will disable automatic close on invoice to allow to process
  196. }
  197. }
  198. }
  199. if ($invoice->type != 0 && $invoice->type != 1) dol_syslog("Invoice ".$facid." is not a standard nor replacement invoice. We do nothing more.");
  200. else if ($remaintopay) dol_syslog("Remain to pay for invoice ".$facid." not null. We do nothing more.");
  201. else if ($mustwait) dol_syslog("There is ".$mustwait." differed payment to process, we do nothing more.");
  202. else $result=$invoice->set_paid($user,'','');
  203. }
  204. }
  205. else
  206. {
  207. $this->error=$this->db->lasterror();
  208. dol_syslog(get_class($this).'::Create insert paiement_facture error='.$this->error, LOG_ERR);
  209. $error++;
  210. }
  211. }
  212. else
  213. {
  214. dol_syslog(get_class($this).'::Create Amount line '.$key.' not a number. We discard it.');
  215. }
  216. }
  217. if (! $error)
  218. {
  219. // Appel des triggers
  220. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  221. $interface=new Interfaces($this->db);
  222. $result=$interface->run_triggers('PAYMENT_CUSTOMER_CREATE',$this,$user,$langs,$conf);
  223. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  224. // Fin appel triggers
  225. }
  226. }
  227. else
  228. {
  229. $this->error=$this->db->lasterror();
  230. dol_syslog(get_class($this).'::Create insert paiement error='.$this->error, LOG_ERR);
  231. $error++;
  232. }
  233. if (! $error)
  234. {
  235. $this->amount=$totalamount;
  236. $this->total=$totalamount; // deprecated
  237. $this->db->commit();
  238. return $this->id;
  239. }
  240. else
  241. {
  242. $this->db->rollback();
  243. return -1;
  244. }
  245. }
  246. /**
  247. * Supprime un paiement ainsi que les lignes qu'il a genere dans comptes
  248. * Si le paiement porte sur un ecriture compte qui est rapprochee, on refuse
  249. * Si le paiement porte sur au moins une facture a "payee", on refuse
  250. *
  251. * @param int $notrigger No trigger
  252. * @return int <0 si ko, >0 si ok
  253. */
  254. function delete($notrigger=0)
  255. {
  256. global $conf, $user, $langs;
  257. $error=0;
  258. $bank_line_id = $this->bank_line;
  259. $this->db->begin();
  260. // Verifier si paiement porte pas sur une facture classee
  261. // Si c'est le cas, on refuse la suppression
  262. $billsarray=$this->getBillsArray('fk_statut > 1');
  263. if (is_array($billsarray))
  264. {
  265. if (count($billsarray))
  266. {
  267. $this->error="ErrorDeletePaymentLinkedToAClosedInvoiceNotPossible";
  268. $this->db->rollback();
  269. return -1;
  270. }
  271. }
  272. else
  273. {
  274. $this->db->rollback();
  275. return -2;
  276. }
  277. $accline = new AccountLine($this->db);
  278. // Delete bank urls. If payment is on a conciliated line, return error.
  279. if ($bank_line_id)
  280. {
  281. $result=$accline->fetch($bank_line_id);
  282. if ($result == 0) $accline->rowid=$bank_line_id; // If not found, we set artificially rowid to allow delete of llx_bank_url
  283. $result=$accline->delete_urls($user);
  284. if ($result < 0)
  285. {
  286. $this->error=$accline->error;
  287. $this->db->rollback();
  288. return -3;
  289. }
  290. }
  291. // Delete payment (into paiement_facture and paiement)
  292. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement_facture';
  293. $sql.= ' WHERE fk_paiement = '.$this->id;
  294. dol_syslog($sql);
  295. $result = $this->db->query($sql);
  296. if ($result)
  297. {
  298. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement';
  299. $sql.= ' WHERE rowid = '.$this->id;
  300. dol_syslog($sql);
  301. $result = $this->db->query($sql);
  302. if (! $result)
  303. {
  304. $this->error=$this->db->lasterror();
  305. $this->db->rollback();
  306. return -3;
  307. }
  308. // Supprimer l'ecriture bancaire si paiement lie a ecriture
  309. if ($bank_line_id)
  310. {
  311. $result=$accline->delete($user);
  312. if ($result < 0)
  313. {
  314. $this->error=$accline->error;
  315. $this->db->rollback();
  316. return -4;
  317. }
  318. }
  319. if (! $notrigger)
  320. {
  321. // Appel des triggers
  322. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  323. $interface=new Interfaces($this->db);
  324. $result=$interface->run_triggers('PAYMENT_DELETE',$this,$user,$langs,$conf);
  325. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  326. // Fin appel triggers
  327. }
  328. $this->db->commit();
  329. return 1;
  330. }
  331. else
  332. {
  333. $this->error=$this->db->error;
  334. $this->db->rollback();
  335. return -5;
  336. }
  337. }
  338. /**
  339. * A record into bank for payment with links between this bank record and invoices of payment.
  340. * All payment properties must have been set first like after a call to create().
  341. *
  342. * @param User $user Object of user making payment
  343. * @param string $mode 'payment', 'payment_supplier'
  344. * @param string $label Label to use in bank record
  345. * @param int $accountid Id of bank account to do link with
  346. * @param string $emetteur_nom Name of transmitter
  347. * @param string $emetteur_banque Name of bank
  348. * @param int $notrigger No trigger
  349. * @return int <0 if KO, bank_line_id if OK
  350. */
  351. function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque,$notrigger=0)
  352. {
  353. global $conf,$langs,$user;
  354. $error=0;
  355. $bank_line_id=0;
  356. $this->fk_account=$accountid;
  357. if (! empty($conf->banque->enabled))
  358. {
  359. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  360. dol_syslog("$user->id,$mode,$label,$this->fk_account,$emetteur_nom,$emetteur_banque");
  361. $acc = new Account($this->db);
  362. $acc->fetch($this->fk_account);
  363. $totalamount=$this->amount;
  364. if (empty($totalamount)) $totalamount=$this->total; // For backward compatibility
  365. if ($mode == 'payment') $totalamount=$totalamount;
  366. if ($mode == 'payment_supplier') $totalamount=-$totalamount;
  367. // Insert payment into llx_bank
  368. $bank_line_id = $acc->addline(
  369. $this->datepaye,
  370. $this->paiementid, // Payment mode id or code ("CHQ or VIR for example")
  371. $label,
  372. $totalamount,
  373. $this->num_paiement,
  374. '',
  375. $user,
  376. $emetteur_nom,
  377. $emetteur_banque
  378. );
  379. // Mise a jour fk_bank dans llx_paiement
  380. // On connait ainsi le paiement qui a genere l'ecriture bancaire
  381. if ($bank_line_id > 0)
  382. {
  383. $result=$this->update_fk_bank($bank_line_id);
  384. if ($result <= 0)
  385. {
  386. $error++;
  387. dol_print_error($this->db);
  388. }
  389. // Add link 'payment', 'payment_supplier' in bank_url between payment and bank transaction
  390. if ( ! $error)
  391. {
  392. $url='';
  393. if ($mode == 'payment') $url=DOL_URL_ROOT.'/compta/paiement/fiche.php?id=';
  394. if ($mode == 'payment_supplier') $url=DOL_URL_ROOT.'/fourn/paiement/fiche.php?id=';
  395. if ($url)
  396. {
  397. $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
  398. if ($result <= 0)
  399. {
  400. $error++;
  401. dol_print_error($this->db);
  402. }
  403. }
  404. }
  405. // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
  406. if (! $error)
  407. {
  408. $linkaddedforthirdparty=array();
  409. foreach ($this->amounts as $key => $value) // We should have always same third party but we loop in case of.
  410. {
  411. if ($mode == 'payment')
  412. {
  413. $fac = new Facture($this->db);
  414. $fac->fetch($key);
  415. $fac->fetch_thirdparty();
  416. if (! in_array($fac->thirdparty->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty
  417. {
  418. $result=$acc->add_url_line(
  419. $bank_line_id,
  420. $fac->thirdparty->id,
  421. DOL_URL_ROOT.'/comm/fiche.php?socid=',
  422. $fac->thirdparty->nom,
  423. 'company'
  424. );
  425. if ($result <= 0) dol_print_error($this->db);
  426. $linkaddedforthirdparty[$fac->thirdparty->id]=$fac->thirdparty->id; // Mark as done for this thirdparty
  427. }
  428. }
  429. if ($mode == 'payment_supplier')
  430. {
  431. $fac = new FactureFournisseur($this->db);
  432. $fac->fetch($key);
  433. $fac->fetch_thirdparty();
  434. if (! in_array($fac->thirdparty->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty
  435. {
  436. $result=$acc->add_url_line(
  437. $bank_line_id,
  438. $fac->thirdparty->id,
  439. DOL_URL_ROOT.'/fourn/fiche.php?socid=',
  440. $fac->thirdparty->nom,
  441. 'company'
  442. );
  443. if ($result <= 0) dol_print_error($this->db);
  444. $linkaddedforthirdparty[$fac->thirdparty->id]=$fac->thirdparty->id; // Mark as done for this thirdparty
  445. }
  446. }
  447. }
  448. }
  449. if (! $error && ! $notrigger)
  450. {
  451. // Appel des triggers
  452. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  453. $interface=new Interfaces($this->db);
  454. $result=$interface->run_triggers('PAYMENT_ADD_TO_BANK',$this,$user,$langs,$conf);
  455. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  456. // Fin appel triggers
  457. }
  458. }
  459. else
  460. {
  461. $this->error=$acc->error;
  462. $error++;
  463. }
  464. }
  465. if (! $error)
  466. {
  467. return $bank_line_id;
  468. }
  469. else
  470. {
  471. return -1;
  472. }
  473. }
  474. /**
  475. * Mise a jour du lien entre le paiement et la ligne generee dans llx_bank
  476. *
  477. * @param int $id_bank Id compte bancaire
  478. * @return int <0 if KO, >0 if OK
  479. */
  480. function update_fk_bank($id_bank)
  481. {
  482. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' set fk_bank = '.$id_bank;
  483. $sql.= ' WHERE rowid = '.$this->id;
  484. dol_syslog(get_class($this).'::update_fk_bank sql='.$sql);
  485. $result = $this->db->query($sql);
  486. if ($result)
  487. {
  488. return 1;
  489. }
  490. else
  491. {
  492. $this->error=$this->db->lasterror();
  493. dol_syslog(get_class($this).'::update_fk_bank '.$this->error);
  494. return -1;
  495. }
  496. }
  497. /**
  498. * Updates the payment date
  499. *
  500. * @param timestamp $date New date
  501. * @return int <0 if KO, 0 if OK
  502. */
  503. function update_date($date)
  504. {
  505. if (!empty($date) && $this->statut!=1)
  506. {
  507. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
  508. $sql.= " SET datep = ".$this->db->idate($date);
  509. $sql.= " WHERE rowid = ".$this->id;
  510. dol_syslog(get_class($this)."::update_date sql=".$sql);
  511. $result = $this->db->query($sql);
  512. if ($result)
  513. {
  514. $this->datepaye = $date;
  515. $this->date = $date;
  516. return 0;
  517. }
  518. else
  519. {
  520. $this->error='Error -1 '.$this->db->error();
  521. dol_syslog(get_class($this)."::update_date ".$this->error, LOG_ERR);
  522. return -2;
  523. }
  524. }
  525. return -1; //no date given or already validated
  526. }
  527. /**
  528. * Updates the payment number
  529. *
  530. * @param string $num New num
  531. * @return int <0 if KO, 0 if OK
  532. */
  533. function update_num($num)
  534. {
  535. if(!empty($num) && $this->statut!=1)
  536. {
  537. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
  538. $sql.= " SET num_paiement = '".$this->db->escape($num)."'";
  539. $sql.= " WHERE rowid = ".$this->id;
  540. dol_syslog(get_class($this)."::update_num sql=".$sql);
  541. $result = $this->db->query($sql);
  542. if ($result)
  543. {
  544. $this->numero = $this->db->escape($num);
  545. return 0;
  546. }
  547. else
  548. {
  549. $this->error='Error -1 '.$this->db->error();
  550. dol_syslog(get_class($this)."::update_num ".$this->error, LOG_ERR);
  551. return -2;
  552. }
  553. }
  554. return -1; //no num given or already validated
  555. }
  556. /**
  557. * Validate payment
  558. *
  559. * @return int <0 if KO, >0 if OK
  560. */
  561. function valide()
  562. {
  563. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET statut = 1 WHERE rowid = '.$this->id;
  564. dol_syslog(get_class($this).'::valide sql='.$sql);
  565. $result = $this->db->query($sql);
  566. if ($result)
  567. {
  568. return 1;
  569. }
  570. else
  571. {
  572. $this->error=$this->db->lasterror();
  573. dol_syslog(get_class($this).'::valide '.$this->error);
  574. return -1;
  575. }
  576. }
  577. /*
  578. * \brief Information sur l'objet
  579. * \param id id du paiement dont il faut afficher les infos
  580. */
  581. function info($id)
  582. {
  583. $sql = 'SELECT p.rowid, p.datec, p.fk_user_creat, p.fk_user_modif, p.tms';
  584. $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement as p';
  585. $sql.= ' WHERE p.rowid = '.$id;
  586. dol_syslog(get_class($this).'::info sql='.$sql);
  587. $result = $this->db->query($sql);
  588. if ($result)
  589. {
  590. if ($this->db->num_rows($result))
  591. {
  592. $obj = $this->db->fetch_object($result);
  593. $this->id = $obj->rowid;
  594. if ($obj->fk_user_creat)
  595. {
  596. $cuser = new User($this->db);
  597. $cuser->fetch($obj->fk_user_creat);
  598. $this->user_creation = $cuser;
  599. }
  600. if ($obj->fk_user_modif)
  601. {
  602. $muser = new User($this->db);
  603. $muser->fetch($obj->fk_user_modif);
  604. $this->user_modification = $muser;
  605. }
  606. $this->date_creation = $this->db->jdate($obj->datec);
  607. $this->date_modification = $this->db->jdate($obj->tms);
  608. }
  609. $this->db->free($result);
  610. }
  611. else
  612. {
  613. dol_print_error($this->db);
  614. }
  615. }
  616. /**
  617. * Retourne la liste des factures sur lesquels porte le paiement
  618. *
  619. * @param string $filter Critere de filtre
  620. * @return array Tableau des id de factures
  621. */
  622. function getBillsArray($filter='')
  623. {
  624. $sql = 'SELECT fk_facture';
  625. $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf, '.MAIN_DB_PREFIX.'facture as f';
  626. $sql.= ' WHERE pf.fk_facture = f.rowid AND fk_paiement = '.$this->id;
  627. if ($filter) $sql.= ' AND '.$filter;
  628. $resql = $this->db->query($sql);
  629. if ($resql)
  630. {
  631. $i=0;
  632. $num=$this->db->num_rows($resql);
  633. $billsarray=array();
  634. while ($i < $num)
  635. {
  636. $obj = $this->db->fetch_object($resql);
  637. $billsarray[$i]=$obj->fk_facture;
  638. $i++;
  639. }
  640. return $billsarray;
  641. }
  642. else
  643. {
  644. $this->error=$this->db->error();
  645. dol_syslog(get_class($this).'::getBillsArray Error '.$this->error.' - sql='.$sql);
  646. return -1;
  647. }
  648. }
  649. /**
  650. * Renvoie nom clicable (avec eventuellement le picto)
  651. *
  652. * @param int $withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  653. * @param string $option Sur quoi pointe le lien
  654. * @return string Chaine avec URL
  655. */
  656. function getNomUrl($withpicto=0,$option='')
  657. {
  658. global $langs;
  659. $result='';
  660. $lien = '<a href="'.DOL_URL_ROOT.'/compta/paiement/fiche.php?id='.$this->id.'">';
  661. $lienfin='</a>';
  662. if ($withpicto) $result.=($lien.img_object($langs->trans("ShowPayment"),'payment').$lienfin);
  663. if ($withpicto && $withpicto != 2) $result.=' ';
  664. if ($withpicto != 2) $result.=$lien.$this->ref.$lienfin;
  665. return $result;
  666. }
  667. /**
  668. * Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
  669. *
  670. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  671. * @return string Libelle
  672. */
  673. function getLibStatut($mode=0)
  674. {
  675. return $this->LibStatut($this->statut,$mode);
  676. }
  677. /**
  678. * Renvoi le libelle d'un statut donne
  679. *
  680. * @param int $status Statut
  681. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  682. * @return string Libelle du statut
  683. */
  684. function LibStatut($status,$mode=0)
  685. {
  686. global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
  687. $langs->load('compta');
  688. if ($mode == 0)
  689. {
  690. if ($status == 0) return $langs->trans('ToValidate');
  691. if ($status == 1) return $langs->trans('Validated');
  692. }
  693. if ($mode == 1)
  694. {
  695. if ($status == 0) return $langs->trans('ToValidate');
  696. if ($status == 1) return $langs->trans('Validated');
  697. }
  698. if ($mode == 2)
  699. {
  700. if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
  701. if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
  702. }
  703. if ($mode == 3)
  704. {
  705. if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
  706. if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
  707. }
  708. if ($mode == 4)
  709. {
  710. if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
  711. if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
  712. }
  713. if ($mode == 5)
  714. {
  715. if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
  716. if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
  717. }
  718. return $langs->trans('Unknown');
  719. }
  720. }
  721. ?>