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 3 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 <https://www.gnu.org/licenses/>.
17 */
18
19/**
20 *      \file       htdocs/compta/sociales/class/paymentsocialcontribution.class.php
21 *		\ingroup    facture
22 *		\brief      File of class to manage payment of social contributions
23 */
24
25require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
27
28
29/**
30 *	Class to manage payments of social contributions
31 */
32class PaymentSocialContribution extends CommonObject
33{
34	/**
35	 * @var string ID to identify managed object
36	 */
37	public $element = 'paiementcharge';
38
39	/**
40	 * @var string Name of table without prefix where object is stored
41	 */
42	public $table_element = 'paiementcharge';
43
44	/**
45	 * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
46	 */
47	public $picto = 'payment';
48
49	/**
50	 * @var string	Label
51	 */
52	public $label;
53
54	/**
55	 * @var int ID
56	 */
57	public $fk_charge;
58
59	public $datec = '';
60	public $tms = '';
61	public $datep = '';
62
63	/**
64	 * @deprecated
65	 * @see $amount
66	 */
67	public $total;
68
69	public $amount; // Total amount of payment
70	public $amounts = array(); // Array of amounts
71
72	/**
73	 * @var int ID
74	 */
75	public $fk_typepaiement;
76
77	/**
78	 * @var string
79	 * @deprecated
80	 */
81	public $num_paiement;
82
83	/**
84	 * @var string
85	 */
86	public $num_payment;
87
88	/**
89	 * @var int ID
90	 */
91	public $fk_bank;
92
93	/**
94	 * @var int ID
95	 */
96	public $fk_user_creat;
97
98	/**
99	 * @var int ID
100	 */
101	public $fk_user_modif;
102
103	/**
104	 *	Constructor
105	 *
106	 *  @param		DoliDB		$db      Database handler
107	 */
108	public function __construct($db)
109	{
110		$this->db = $db;
111	}
112
113	/**
114	 *  Create payment of social contribution into database.
115	 *  Use this->amounts to have list of lines for the payment
116	 *
117	 *  @param      User	$user   				User making payment
118	 *	@param		int		$closepaidcontrib   	1=Also close paid contributions to paid, 0=Do nothing more
119	 *  @return     int     						<0 if KO, id of payment if OK
120	 */
121	public function create($user, $closepaidcontrib = 0)
122	{
123		global $conf, $langs;
124
125		$error = 0;
126
127		$now = dol_now();
128
129		dol_syslog(get_class($this)."::create", LOG_DEBUG);
130
131		// Validate parametres
132		if (!$this->datepaye) {
133			$this->error = 'ErrorBadValueForParameterCreatePaymentSocialContrib';
134			return -1;
135		}
136
137		// Clean parameters
138		if (isset($this->fk_charge)) {
139			$this->fk_charge = (int) $this->fk_charge;
140		}
141		if (isset($this->amount)) {
142			$this->amount = trim($this->amount);
143		}
144		if (isset($this->fk_typepaiement)) {
145			$this->fk_typepaiement = (int) $this->fk_typepaiement;
146		}
147		if (isset($this->num_payment)) {
148			$this->num_payment = trim($this->num_payment);
149		}
150		if (isset($this->note_private)) {
151			$this->note_private = trim($this->note_private);
152		}
153		if (isset($this->fk_bank)) {
154			$this->fk_bank = (int) $this->fk_bank;
155		}
156		if (isset($this->fk_user_creat)) {
157			$this->fk_user_creat = (int) $this->fk_user_creat;
158		}
159		if (isset($this->fk_user_modif)) {
160			$this->fk_user_modif = (int) $this->fk_user_modif;
161		}
162
163		$totalamount = 0;
164		foreach ($this->amounts as $key => $value) {  // How payment is dispatch
165			$newvalue = price2num($value, 'MT');
166			$this->amounts[$key] = $newvalue;
167			$totalamount += $newvalue;
168		}
169		$totalamount = price2num($totalamount);
170
171		// Check parameters
172		if ($totalamount == 0) {
173			return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
174		}
175
176
177		$this->db->begin();
178
179		if ($totalamount != 0) {
180			$sql = "INSERT INTO ".MAIN_DB_PREFIX."paiementcharge (fk_charge, datec, datep, amount,";
181			$sql .= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
182			$sql .= " VALUES ($this->chid, '".$this->db->idate($now)."',";
183			$sql .= " '".$this->db->idate($this->datepaye)."',";
184			$sql .= " ".((float) $totalamount).",";
185			$sql .= " ".((int) $this->paiementtype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".$user->id.",";
186			$sql .= " 0)";
187
188			$resql = $this->db->query($sql);
189			if ($resql) {
190				$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."paiementcharge");
191
192				// Insere tableau des montants / factures
193				foreach ($this->amounts as $key => $amount) {
194					$contribid = $key;
195					if (is_numeric($amount) && $amount <> 0) {
196						$amount = price2num($amount);
197
198						// If we want to closed paid invoices
199						if ($closepaidcontrib) {
200							$contrib = new ChargeSociales($this->db);
201							$contrib->fetch($contribid);
202							$paiement = $contrib->getSommePaiement();
203							//$creditnotes=$contrib->getSumCreditNotesUsed();
204							$creditnotes = 0;
205							//$deposits=$contrib->getSumDepositsUsed();
206							$deposits = 0;
207							$alreadypayed = price2num($paiement + $creditnotes + $deposits, 'MT');
208							$remaintopay = price2num($contrib->amount - $paiement - $creditnotes - $deposits, 'MT');
209							if ($remaintopay == 0) {
210								$result = $contrib->setPaid($user);
211							} else {
212								dol_syslog("Remain to pay for conrib ".$contribid." not null. We do nothing.");
213							}
214						}
215					}
216				}
217			} else {
218				$error++;
219			}
220		}
221
222		$result = $this->call_trigger('PAYMENTSOCIALCONTRIBUTION_CREATE', $user);
223		if ($result < 0) {
224			$error++;
225		}
226
227		if ($totalamount != 0 && !$error) {
228			$this->amount = $totalamount;
229			$this->total = $totalamount; // deprecated
230			$this->db->commit();
231			return $this->id;
232		} else {
233			$this->error = $this->db->error();
234			$this->db->rollback();
235			return -1;
236		}
237	}
238
239	/**
240	 *  Load object in memory from database
241	 *
242	 *  @param	int		$id         Id object
243	 *  @return int         		<0 if KO, >0 if OK
244	 */
245	public function fetch($id)
246	{
247		global $langs;
248		$sql = "SELECT";
249		$sql .= " t.rowid,";
250		$sql .= " t.fk_charge,";
251		$sql .= " t.datec,";
252		$sql .= " t.tms,";
253		$sql .= " t.datep,";
254		$sql .= " t.amount,";
255		$sql .= " t.fk_typepaiement,";
256		$sql .= " t.num_paiement as num_payment,";
257		$sql .= " t.note,";
258		$sql .= " t.fk_bank,";
259		$sql .= " t.fk_user_creat,";
260		$sql .= " t.fk_user_modif,";
261		$sql .= " pt.code as type_code, pt.libelle as type_label,";
262		$sql .= ' b.fk_account';
263		$sql .= " FROM ".MAIN_DB_PREFIX."paiementcharge as t LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepaiement = pt.id";
264		$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
265		$sql .= " WHERE t.rowid = ".((int) $id);
266		// TODO link on entity of tax;
267
268		dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
269		$resql = $this->db->query($sql);
270		if ($resql) {
271			if ($this->db->num_rows($resql)) {
272				$obj = $this->db->fetch_object($resql);
273
274				$this->id    = $obj->rowid;
275				$this->ref   = $obj->rowid;
276
277				$this->fk_charge = $obj->fk_charge;
278				$this->datec = $this->db->jdate($obj->datec);
279				$this->tms = $this->db->jdate($obj->tms);
280				$this->datep = $this->db->jdate($obj->datep);
281				$this->amount = $obj->amount;
282				$this->fk_typepaiement = $obj->fk_typepaiement;
283				$this->num_payment = $obj->num_payment;
284				$this->note_private = $obj->note;
285				$this->fk_bank = $obj->fk_bank;
286				$this->fk_user_creat = $obj->fk_user_creat;
287				$this->fk_user_modif = $obj->fk_user_modif;
288
289				$this->type_code = $obj->type_code;
290				$this->type_label = $obj->type_label;
291
292				$this->bank_account   = $obj->fk_account;
293				$this->bank_line      = $obj->fk_bank;
294			}
295			$this->db->free($resql);
296
297			return 1;
298		} else {
299			$this->error = "Error ".$this->db->lasterror();
300			return -1;
301		}
302	}
303
304
305	/**
306	 *  Update database
307	 *
308	 *  @param	User	$user        	User that modify
309	 *  @param  int		$notrigger	    0=launch triggers after, 1=disable triggers
310	 *  @return int         			<0 if KO, >0 if OK
311	 */
312	public function update($user = null, $notrigger = 0)
313	{
314		global $conf, $langs;
315		$error = 0;
316
317		// Clean parameters
318
319		if (isset($this->fk_charge)) {
320			$this->fk_charge = (int) $this->fk_charge;
321		}
322		if (isset($this->amount)) {
323			$this->amount = trim($this->amount);
324		}
325		if (isset($this->fk_typepaiement)) {
326			$this->fk_typepaiement = (int) $this->fk_typepaiement;
327		}
328		if (isset($this->num_payment)) {
329			$this->num_payment = trim($this->num_payment);
330		}
331		if (isset($this->note_private)) {
332			$this->note_private = trim($this->note_private);
333		}
334		if (isset($this->fk_bank)) {
335			$this->fk_bank = (int) $this->fk_bank;
336		}
337		if (isset($this->fk_user_creat)) {
338			$this->fk_user_creat = (int) $this->fk_user_creat;
339		}
340		if (isset($this->fk_user_modif)) {
341			$this->fk_user_modif = (int) $this->fk_user_modif;
342		}
343
344
345
346		// Check parameters
347		// Put here code to add control on parameters values
348
349		// Update request
350		$sql = "UPDATE ".MAIN_DB_PREFIX."paiementcharge SET";
351
352		$sql .= " fk_charge=".(isset($this->fk_charge) ? $this->fk_charge : "null").",";
353		$sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
354		$sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
355		$sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
356		$sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
357		$sql .= " fk_typepaiement=".(isset($this->fk_typepaiement) ? $this->fk_typepaiement : "null").",";
358		$sql .= " num_paiement=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
359		$sql .= " note=".(isset($this->note) ? "'".$this->db->escape($this->note)."'" : "null").",";
360		$sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
361		$sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
362		$sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null")."";
363
364
365		$sql .= " WHERE rowid=".((int) $this->id);
366
367		$this->db->begin();
368
369		dol_syslog(get_class($this)."::update", LOG_DEBUG);
370		$resql = $this->db->query($sql);
371		if (!$resql) {
372			$error++;
373			$this->errors[] = "Error ".$this->db->lasterror();
374		}
375
376		// Commit or rollback
377		if ($error) {
378			foreach ($this->errors as $errmsg) {
379				dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
380				$this->error .= ($this->error ? ', '.$errmsg : $errmsg);
381			}
382			$this->db->rollback();
383			return -1 * $error;
384		} else {
385			$this->db->commit();
386			return 1;
387		}
388	}
389
390
391	/**
392	 *  Delete object in database
393	 *
394	 *  @param	User	$user        	User that delete
395	 *  @param  int		$notrigger		0=launch triggers after, 1=disable triggers
396	 *  @return int						<0 if KO, >0 if OK
397	 */
398	public function delete($user, $notrigger = 0)
399	{
400		global $conf, $langs;
401		$error = 0;
402
403		dol_syslog(get_class($this)."::delete");
404
405		$this->db->begin();
406
407		if ($this->bank_line > 0) {
408			$accline = new AccountLine($this->db);
409			$accline->fetch($this->bank_line);
410			$result = $accline->delete();
411			if ($result < 0) {
412				$this->errors[] = $accline->error;
413				$error++;
414			}
415		}
416
417		if (!$error) {
418			$sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge";
419			$sql .= " WHERE rowid=".((int) $this->id);
420
421			dol_syslog(get_class($this)."::delete", LOG_DEBUG);
422			$resql = $this->db->query($sql);
423			if (!$resql) {
424				$error++;
425				$this->errors[] = "Error ".$this->db->lasterror();
426			}
427		}
428
429		// Commit or rollback
430		if ($error) {
431			foreach ($this->errors as $errmsg) {
432				dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
433				$this->error .= ($this->error ? ', '.$errmsg : $errmsg);
434			}
435			$this->db->rollback();
436			return -1 * $error;
437		} else {
438			$this->db->commit();
439			return 1;
440		}
441	}
442
443
444
445	/**
446	 *	Load an object from its id and create a new one in database
447	 *
448	 *  @param	User	$user		    User making the clone
449	 *	@param	int		$fromid     	Id of object to clone
450	 * 	@return	int						New id of clone
451	 */
452	public function createFromClone(User $user, $fromid)
453	{
454		$error = 0;
455
456		$object = new PaymentSocialContribution($this->db);
457
458		$this->db->begin();
459
460		// Load source object
461		$object->fetch($fromid);
462		$object->id = 0;
463		$object->statut = 0;
464
465		// Clear fields
466		// ...
467
468		// Create clone
469		$object->context['createfromclone'] = 'createfromclone';
470		$result = $object->create($user);
471
472		// Other options
473		if ($result < 0) {
474			$this->error = $object->error;
475			$error++;
476		}
477
478		unset($object->context['createfromclone']);
479
480		// End
481		if (!$error) {
482			$this->db->commit();
483			return $object->id;
484		} else {
485			$this->db->rollback();
486			return -1;
487		}
488	}
489
490
491	/**
492	 *  Initialise an instance with random values.
493	 *  Used to build previews or test instances.
494	 *	id must be 0 if object instance is a specimen.
495	 *
496	 *  @return	void
497	 */
498	public function initAsSpecimen()
499	{
500		$this->id = 0;
501
502		$this->fk_charge = '';
503		$this->datec = '';
504		$this->tms = '';
505		$this->datep = '';
506		$this->amount = '';
507		$this->fk_typepaiement = '';
508		$this->num_payment = '';
509		$this->note_private = '';
510		$this->note_public = '';
511		$this->fk_bank = '';
512		$this->fk_user_creat = '';
513		$this->fk_user_modif = '';
514	}
515
516
517	/**
518	 *      Add record into bank for payment with links between this bank record and invoices of payment.
519	 *      All payment properties must have been set first like after a call to create().
520	 *
521	 *      @param	User	$user               Object of user making payment
522	 *      @param  string	$mode               'payment_sc'
523	 *      @param  string	$label              Label to use in bank record
524	 *      @param  int		$accountid          Id of bank account to do link with
525	 *      @param  string	$emetteur_nom       Name of transmitter
526	 *      @param  string	$emetteur_banque    Name of bank
527	 *      @return int                 		<0 if KO, >0 if OK
528	 */
529	public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
530	{
531		global $conf;
532
533		// Clean data
534		$this->num_payment = trim($this->num_payment);
535
536		$error = 0;
537
538		if (!empty($conf->banque->enabled)) {
539			include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
540
541			$acc = new Account($this->db);
542			$acc->fetch($accountid);
543
544			$total = $this->total;
545			if ($mode == 'payment_sc') {
546				$total = -$total;
547			}
548
549			// Insert payment into llx_bank
550			$bank_line_id = $acc->addline(
551				$this->datepaye,
552				$this->paiementtype, // Payment mode id or code ("CHQ or VIR for example")
553				$label,
554				$total,
555				$this->num_payment,
556				'',
557				$user,
558				$emetteur_nom,
559				$emetteur_banque
560			);
561
562			// Mise a jour fk_bank dans llx_paiement.
563			// On connait ainsi le paiement qui a genere l'ecriture bancaire
564			if ($bank_line_id > 0) {
565				$result = $this->update_fk_bank($bank_line_id);
566				if ($result <= 0) {
567					$error++;
568					dol_print_error($this->db);
569				}
570
571				// Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction
572				$url = '';
573				if ($mode == 'payment_sc') {
574					$url = DOL_URL_ROOT.'/compta/payment_sc/card.php?id=';
575				}
576				if ($url) {
577					$result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
578					if ($result <= 0) {
579						$error++;
580						dol_print_error($this->db);
581					}
582				}
583
584				// Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
585				$linkaddedforthirdparty = array();
586				foreach ($this->amounts as $key => $value) {
587					if ($mode == 'payment_sc') {
588						$socialcontrib = new ChargeSociales($this->db);
589						$socialcontrib->fetch($key);
590						$result = $acc->add_url_line($bank_line_id, $socialcontrib->id, DOL_URL_ROOT.'/compta/charges.php?id=', $socialcontrib->type_label.(($socialcontrib->lib && $socialcontrib->lib != $socialcontrib->type_label) ? ' ('.$socialcontrib->lib.')' : ''), 'sc');
591						if ($result <= 0) {
592							dol_print_error($this->db);
593						}
594
595						if ($socialcontrib->fk_user) {
596							$fuser = new User($this->db);
597							$fuser->fetch($socialcontrib->fk_user);
598
599							// Add link 'user' in bank_url between operation and bank transaction
600							$result = $acc->add_url_line(
601								$bank_line_id,
602								$socialcontrib->fk_user,
603								DOL_URL_ROOT . '/user/card.php?id=',
604								$fuser->getFullName($langs),
605								'user'
606							);
607
608							if ($result <= 0) {
609								$this->error = $acc->error;
610								$error++;
611							}
612						}
613					}
614				}
615			} else {
616				$this->error = $acc->error;
617				$error++;
618			}
619		}
620
621		if (!$error) {
622			return 1;
623		} else {
624			return -1;
625		}
626	}
627
628
629	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
630	/**
631	 *  Mise a jour du lien entre le paiement de  charge et la ligne dans llx_bank generee
632	 *
633	 *  @param	int		$id_bank         Id if bank
634	 *  @return	int			             >0 if OK, <=0 if KO
635	 */
636	public function update_fk_bank($id_bank)
637	{
638		// phpcs:enable
639		$sql = "UPDATE ".MAIN_DB_PREFIX."paiementcharge SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
640
641		dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
642		$result = $this->db->query($sql);
643		if ($result) {
644			return 1;
645		} else {
646			$this->error = $this->db->error();
647			return 0;
648		}
649	}
650
651
652	/**
653	 * Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
654	 *
655	 * @param	int		$mode       0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
656	 * @return  string				Libelle
657	 */
658	public function getLibStatut($mode = 0)
659	{
660		return $this->LibStatut($this->statut, $mode);
661	}
662
663	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
664	/**
665	 * Renvoi le libelle d'un statut donne
666	 *
667	 * @param   int		$status     Statut
668	 * @param   int		$mode       0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
669	 * @return	string  		    Libelle du statut
670	 */
671	public function LibStatut($status, $mode = 0)
672	{
673		// phpcs:enable
674		global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
675
676		$langs->load('compta');
677		/*if ($mode == 0)
678			{
679			if ($status == 0) return $langs->trans('ToValidate');
680			if ($status == 1) return $langs->trans('Validated');
681			}
682			if ($mode == 1)
683			{
684			if ($status == 0) return $langs->trans('ToValidate');
685			if ($status == 1) return $langs->trans('Validated');
686			}
687			if ($mode == 2)
688			{
689			if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
690			if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
691			}
692			if ($mode == 3)
693			{
694			if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
695			if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
696			}
697			if ($mode == 4)
698			{
699			if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
700			if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
701			}
702			if ($mode == 5)
703			{
704			if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
705			if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
706			}
707			if ($mode == 6)
708			{
709			if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
710			if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
711			}*/
712		return '';
713	}
714
715	/**
716	 *  Return clicable name (with picto eventually)
717	 *
718	 *	@param	int		$withpicto		0=No picto, 1=Include picto into link, 2=Only picto
719	 * 	@param	int		$maxlen			Longueur max libelle
720	 *	@return	string					Chaine avec URL
721	 */
722	public function getNomUrl($withpicto = 0, $maxlen = 0)
723	{
724		global $langs;
725
726		$result = '';
727
728		if (empty($this->ref)) {
729			$this->ref = $this->label;
730		}
731
732		$label = img_picto('', $this->picto).' <u>'.$langs->trans("SocialContributionPayment").'</u>';
733		$label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
734		if (!empty($this->label)) {
735			$labeltoshow = $this->label;
736			$reg = array();
737			if (preg_match('/^\((.*)\)$/i', $this->label, $reg)) {
738				// Label generique car entre parentheses. On l'affiche en le traduisant
739				if ($reg[1] == 'paiement') {
740					$reg[1] = 'Payment';
741				}
742				$labeltoshow = $langs->trans($reg[1]);
743			}
744			$label .= '<br><b>'.$langs->trans('Label').':</b> '.$labeltoshow;
745		}
746		if ($this->datep) {
747			$label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->datep, 'day');
748		}
749
750		if (!empty($this->id)) {
751			$link = '<a href="'.DOL_URL_ROOT.'/compta/payment_sc/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
752			$linkend = '</a>';
753
754			if ($withpicto) {
755				$result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
756			}
757			if ($withpicto && $withpicto != 2) {
758				$result .= ' ';
759			}
760			if ($withpicto != 2) {
761				$result .= $link.($maxlen ?dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
762			}
763		}
764
765		return $result;
766	}
767
768
769	/**
770	 *	Return if object was dispatched into bookkeeping
771	 *
772	 *	@return     int         <0 if KO, 0=no, 1=yes
773	 */
774	public function getVentilExportCompta()
775	{
776		$alreadydispatched = 0;
777
778		$type = 'bank';
779
780		$sql = " SELECT COUNT(ab.rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab WHERE ab.doc_type='".$this->db->escape($type)."' AND ab.fk_doc = ".((int) $this->bank_line);
781		$resql = $this->db->query($sql);
782		if ($resql) {
783			$obj = $this->db->fetch_object($resql);
784			if ($obj) {
785				$alreadydispatched = $obj->nb;
786			}
787		} else {
788			$this->error = $this->db->lasterror();
789			return -1;
790		}
791
792		if ($alreadydispatched) {
793			return 1;
794		}
795		return 0;
796	}
797}
798