|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2002-2004 Rodolphe Quiedeville <[email protected]> |
|
3
|
|
|
* Copyright (C) 2004-2007 Laurent Destailleur <[email protected]> |
|
4
|
|
|
* Copyright (C) 2005 Marc Barilley / Ocebo <[email protected]> |
|
5
|
|
|
* Copyright (C) 2005-2009 Regis Houssin <[email protected]> |
|
6
|
|
|
* Copyright (C) 2010-2011 Juanjo Menent <[email protected]> |
|
7
|
|
|
* Copyright (C) 2014 Marcos García <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU General Public License as published by |
|
11
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
12
|
|
|
* (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* \file htdocs/fourn/class/paiementfourn.class.php |
|
25
|
|
|
* \ingroup fournisseur, facture |
|
26
|
|
|
* \brief File of class to manage payments of suppliers invoices |
|
27
|
|
|
*/ |
|
28
|
|
|
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; |
|
29
|
|
|
require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php'; |
|
30
|
|
|
require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class to manage payments for supplier invoices |
|
34
|
|
|
*/ |
|
35
|
|
|
class PaiementFourn extends Paiement |
|
36
|
|
|
{ |
|
37
|
|
|
public $element='payment_supplier'; |
|
38
|
|
|
public $table_element='paiementfourn'; |
|
39
|
|
|
public $picto = 'payment'; |
|
40
|
|
|
|
|
41
|
|
|
var $statut; //Status of payment. 0 = unvalidated; 1 = validated |
|
42
|
|
|
// fk_paiement dans llx_paiement est l'id du type de paiement (7 pour CHQ, ...) |
|
43
|
|
|
// fk_paiement dans llx_paiement_facture est le rowid du paiement |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Label of payment type |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
public $type_libelle; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Code of Payment type |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
public $type_code; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Constructor |
|
59
|
|
|
* |
|
60
|
|
|
* @param DoliDB $db Database handler |
|
61
|
|
|
*/ |
|
62
|
|
|
function __construct($db) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->db = $db; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Load payment object |
|
69
|
|
|
* |
|
70
|
|
|
* @param int $id Id if payment to get |
|
71
|
|
|
* @param string $ref Ref of payment to get (currently ref = id but this may change in future) |
|
72
|
|
|
* @param int $fk_bank Id of bank line associated to payment |
|
73
|
|
|
* @return int <0 if KO, -2 if not found, >0 if OK |
|
74
|
|
|
*/ |
|
75
|
|
|
function fetch($id, $ref='', $fk_bank='') |
|
76
|
|
|
{ |
|
77
|
|
|
$error=0; |
|
78
|
|
|
|
|
79
|
|
|
$sql = 'SELECT p.rowid, p.ref, p.entity, p.datep as dp, p.amount, p.statut, p.fk_bank,'; |
|
80
|
|
|
$sql.= ' c.code as paiement_code, c.libelle as paiement_type,'; |
|
81
|
|
|
$sql.= ' p.num_paiement, p.note, b.fk_account'; |
|
82
|
|
|
$sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn as p'; |
|
83
|
|
|
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id AND c.entity IN ('.getEntity('c_paiement').')'; |
|
84
|
|
|
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid '; |
|
85
|
|
|
$sql.= ' WHERE p.entity IN ('.getEntity('facture_fourn').')'; |
|
86
|
|
|
if ($id > 0) |
|
87
|
|
|
$sql.= ' AND p.rowid = '.$id; |
|
88
|
|
|
else if ($ref) |
|
89
|
|
|
$sql.= ' AND p.rowid = '.$ref; |
|
90
|
|
|
else if ($fk_bank) |
|
91
|
|
|
$sql.= ' AND p.fk_bank = '.$fk_bank; |
|
92
|
|
|
//print $sql; |
|
93
|
|
|
|
|
94
|
|
|
$resql = $this->db->query($sql); |
|
95
|
|
|
if ($resql) |
|
96
|
|
|
{ |
|
97
|
|
|
$num = $this->db->num_rows($resql); |
|
98
|
|
|
if ($num > 0) |
|
99
|
|
|
{ |
|
100
|
|
|
$obj = $this->db->fetch_object($resql); |
|
101
|
|
|
$this->id = $obj->rowid; |
|
102
|
|
|
$this->ref = $obj->ref; |
|
103
|
|
|
$this->entity = $obj->entity; |
|
104
|
|
|
$this->date = $this->db->jdate($obj->dp); |
|
105
|
|
|
$this->numero = $obj->num_paiement; |
|
106
|
|
|
$this->bank_account = $obj->fk_account; |
|
107
|
|
|
$this->bank_line = $obj->fk_bank; |
|
108
|
|
|
$this->montant = $obj->amount; |
|
109
|
|
|
$this->note = $obj->note; |
|
110
|
|
|
$this->type_code = $obj->paiement_code; |
|
111
|
|
|
$this->type_libelle = $obj->paiement_type; |
|
112
|
|
|
$this->statut = $obj->statut; |
|
113
|
|
|
$error = 1; |
|
114
|
|
|
} |
|
115
|
|
|
else |
|
116
|
|
|
{ |
|
117
|
|
|
$error = -2; // TODO Use 0 instead |
|
118
|
|
|
} |
|
119
|
|
|
$this->db->free($resql); |
|
120
|
|
|
} |
|
121
|
|
|
else |
|
122
|
|
|
{ |
|
123
|
|
|
dol_print_error($this->db); |
|
124
|
|
|
$error = -1; |
|
125
|
|
|
} |
|
126
|
|
|
return $error; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Create payment in database |
|
131
|
|
|
* |
|
132
|
|
|
* @param User $user Object of creating user |
|
133
|
|
|
* @param int $closepaidinvoices 1=Also close payed invoices to paid, 0=Do nothing more |
|
134
|
|
|
* @return int id of created payment, < 0 if error |
|
135
|
|
|
*/ |
|
136
|
|
|
function create($user, $closepaidinvoices=0) |
|
137
|
|
|
{ |
|
138
|
|
|
global $langs,$conf; |
|
139
|
|
|
|
|
140
|
|
|
$error = 0; |
|
141
|
|
|
$way = $this->getWay(); |
|
142
|
|
|
|
|
143
|
|
|
// Clean parameters |
|
144
|
|
|
$totalamount = 0; |
|
145
|
|
|
$totalamount_converted = 0; |
|
146
|
|
|
|
|
147
|
|
|
dol_syslog(get_class($this)."::create", LOG_DEBUG); |
|
148
|
|
|
|
|
149
|
|
|
if ($way == 'dolibarr') |
|
150
|
|
|
{ |
|
151
|
|
|
$amounts = &$this->amounts; |
|
152
|
|
|
$amounts_to_update = &$this->multicurrency_amounts; |
|
153
|
|
|
} |
|
154
|
|
|
else |
|
155
|
|
|
{ |
|
156
|
|
|
$amounts = &$this->multicurrency_amounts; |
|
157
|
|
|
$amounts_to_update = &$this->amounts; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
foreach ($amounts as $key => $value) |
|
161
|
|
|
{ |
|
162
|
|
|
$value_converted = Multicurrency::getAmountConversionFromInvoiceRate($key, $value, $way, 'facture_fourn'); |
|
163
|
|
|
$totalamount_converted += $value_converted; |
|
164
|
|
|
$amounts_to_update[$key] = price2num($value_converted, 'MT'); |
|
165
|
|
|
|
|
166
|
|
|
$newvalue = price2num($value,'MT'); |
|
167
|
|
|
$amounts[$key] = $newvalue; |
|
168
|
|
|
$totalamount += $newvalue; |
|
169
|
|
|
} |
|
170
|
|
|
$totalamount = price2num($totalamount); |
|
171
|
|
|
$totalamount_converted = price2num($totalamount_converted); |
|
172
|
|
|
|
|
173
|
|
|
$this->db->begin(); |
|
174
|
|
|
|
|
175
|
|
|
if ($totalamount <> 0) // On accepte les montants negatifs |
|
176
|
|
|
{ |
|
177
|
|
|
$ref = $this->getNextNumRef(''); |
|
178
|
|
|
$now=dol_now(); |
|
179
|
|
|
|
|
180
|
|
|
if ($way == 'dolibarr') |
|
181
|
|
|
{ |
|
182
|
|
|
$total = $totalamount; |
|
183
|
|
|
$mtotal = $totalamount_converted; // Maybe use price2num with MT for the converted value |
|
184
|
|
|
} |
|
185
|
|
|
else |
|
186
|
|
|
{ |
|
187
|
|
|
$total = $totalamount_converted; // Maybe use price2num with MT for the converted value |
|
188
|
|
|
$mtotal = $totalamount; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn ('; |
|
192
|
|
|
$sql.= 'ref, entity, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, fk_user_author, fk_bank)'; |
|
193
|
|
|
$sql.= " VALUES ('".$this->db->escape($ref)."', ".$conf->entity.", '".$this->db->idate($now)."',"; |
|
194
|
|
|
$sql.= " '".$this->db->idate($this->datepaye)."', '".$total."', '".$mtotal."', ".$this->paiementid.", '".$this->num_paiement."', '".$this->db->escape($this->note)."', ".$user->id.", 0)"; |
|
195
|
|
|
|
|
196
|
|
|
$resql = $this->db->query($sql); |
|
197
|
|
|
if ($resql) |
|
198
|
|
|
{ |
|
199
|
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'paiementfourn'); |
|
200
|
|
|
|
|
201
|
|
|
// Insere tableau des montants / factures |
|
202
|
|
|
foreach ($this->amounts as $key => $amount) |
|
203
|
|
|
{ |
|
204
|
|
|
$facid = $key; |
|
205
|
|
|
if (is_numeric($amount) && $amount <> 0) |
|
206
|
|
|
{ |
|
207
|
|
|
$amount = price2num($amount); |
|
208
|
|
|
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, amount, multicurrency_amount)'; |
|
209
|
|
|
$sql .= ' VALUES ('.$facid.','. $this->id.',\''.$amount.'\', \''.$this->multicurrency_amounts[$key].'\')'; |
|
210
|
|
|
$resql=$this->db->query($sql); |
|
211
|
|
|
if ($resql) |
|
212
|
|
|
{ |
|
213
|
|
|
// If we want to closed payed invoices |
|
214
|
|
|
if ($closepaidinvoices) |
|
215
|
|
|
{ |
|
216
|
|
|
$invoice=new FactureFournisseur($this->db); |
|
217
|
|
|
$invoice->fetch($facid); |
|
218
|
|
|
$paiement = $invoice->getSommePaiement(); |
|
219
|
|
|
//$creditnotes=$invoice->getSumCreditNotesUsed(); |
|
220
|
|
|
$creditnotes=0; |
|
221
|
|
|
//$deposits=$invoice->getSumDepositsUsed(); |
|
222
|
|
|
$deposits=0; |
|
223
|
|
|
$alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT'); |
|
224
|
|
|
$remaintopay=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT'); |
|
225
|
|
|
if ($remaintopay == 0) |
|
226
|
|
|
{ |
|
227
|
|
|
$result=$invoice->set_paid($user, '', ''); |
|
228
|
|
|
} |
|
229
|
|
|
else dol_syslog("Remain to pay for invoice ".$facid." not null. We do nothing."); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
else |
|
233
|
|
|
{ |
|
234
|
|
|
dol_syslog('Paiement::Create Erreur INSERT dans paiement_facture '.$facid); |
|
235
|
|
|
$error++; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
else |
|
240
|
|
|
{ |
|
241
|
|
|
dol_syslog('PaiementFourn::Create Montant non numerique',LOG_ERR); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
if (! $error) |
|
246
|
|
|
{ |
|
247
|
|
|
// Call trigger |
|
248
|
|
|
$result=$this->call_trigger('PAYMENT_SUPPLIER_CREATE',$user); |
|
249
|
|
|
if ($result < 0) $error++; |
|
250
|
|
|
// End call triggers |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
else |
|
254
|
|
|
{ |
|
255
|
|
|
$this->error=$this->db->lasterror(); |
|
256
|
|
|
$error++; |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
else |
|
260
|
|
|
{ |
|
261
|
|
|
$this->error="ErrorTotalIsNull"; |
|
262
|
|
|
dol_syslog('PaiementFourn::Create Error '.$this->error, LOG_ERR); |
|
263
|
|
|
$error++; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
if ($totalamount <> 0 && $error == 0) // On accepte les montants negatifs |
|
267
|
|
|
{ |
|
268
|
|
|
$this->amount=$total; |
|
|
|
|
|
|
269
|
|
|
$this->total=$total; |
|
270
|
|
|
$this->multicurrency_amount=$mtotal; |
|
|
|
|
|
|
271
|
|
|
$this->db->commit(); |
|
272
|
|
|
dol_syslog('PaiementFourn::Create Ok Total = '.$this->total); |
|
273
|
|
|
return $this->id; |
|
274
|
|
|
} |
|
275
|
|
|
else |
|
276
|
|
|
{ |
|
277
|
|
|
$this->db->rollback(); |
|
278
|
|
|
return -1; |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Supprime un paiement ainsi que les lignes qu'il a genere dans comptes |
|
285
|
|
|
* Si le paiement porte sur un ecriture compte qui est rapprochee, on refuse |
|
286
|
|
|
* Si le paiement porte sur au moins une facture a "payee", on refuse |
|
287
|
|
|
* |
|
288
|
|
|
* @param int $notrigger No trigger |
|
289
|
|
|
* @return int <0 si ko, >0 si ok |
|
290
|
|
|
*/ |
|
291
|
|
|
function delete($notrigger=0) |
|
292
|
|
|
{ |
|
293
|
|
|
global $conf, $user, $langs; |
|
294
|
|
|
|
|
295
|
|
|
$bank_line_id = $this->bank_line; |
|
296
|
|
|
|
|
297
|
|
|
$this->db->begin(); |
|
298
|
|
|
|
|
299
|
|
|
// Verifier si paiement porte pas sur une facture a l'etat payee |
|
300
|
|
|
// Si c'est le cas, on refuse la suppression |
|
301
|
|
|
$billsarray=$this->getBillsArray('paye=1'); |
|
302
|
|
|
if (is_array($billsarray)) |
|
303
|
|
|
{ |
|
304
|
|
|
if (count($billsarray)) |
|
305
|
|
|
{ |
|
306
|
|
|
$this->error="ErrorCantDeletePaymentSharedWithPayedInvoice"; |
|
307
|
|
|
$this->db->rollback(); |
|
308
|
|
|
return -1; |
|
309
|
|
|
} |
|
310
|
|
|
} |
|
311
|
|
|
else |
|
312
|
|
|
{ |
|
313
|
|
|
$this->db->rollback(); |
|
314
|
|
|
return -2; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
// Verifier si paiement ne porte pas sur ecriture bancaire rapprochee |
|
318
|
|
|
// Si c'est le cas, on refuse le delete |
|
319
|
|
|
if ($bank_line_id) |
|
320
|
|
|
{ |
|
321
|
|
|
$accline = new AccountLine($this->db); |
|
322
|
|
|
$accline->fetch($bank_line_id); |
|
323
|
|
|
if ($accline->rappro) |
|
324
|
|
|
{ |
|
325
|
|
|
$this->error="ErrorCantDeletePaymentReconciliated"; |
|
326
|
|
|
$this->db->rollback(); |
|
327
|
|
|
return -3; |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
// Efface la ligne de paiement (dans paiement_facture et paiement) |
|
332
|
|
|
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn'; |
|
333
|
|
|
$sql.= ' WHERE fk_paiementfourn = '.$this->id; |
|
334
|
|
|
$resql = $this->db->query($sql); |
|
335
|
|
|
if ($resql) |
|
336
|
|
|
{ |
|
337
|
|
|
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiementfourn'; |
|
338
|
|
|
$sql.= ' WHERE rowid = '.$this->id; |
|
339
|
|
|
$result = $this->db->query($sql); |
|
340
|
|
|
if (! $result) |
|
341
|
|
|
{ |
|
342
|
|
|
$this->error=$this->db->error(); |
|
343
|
|
|
$this->db->rollback(); |
|
344
|
|
|
return -3; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
// Supprimer l'ecriture bancaire si paiement lie a ecriture |
|
348
|
|
|
if ($bank_line_id) |
|
349
|
|
|
{ |
|
350
|
|
|
$accline = new AccountLine($this->db); |
|
351
|
|
|
$result=$accline->fetch($bank_line_id); |
|
352
|
|
|
if ($result > 0) // If result = 0, record not found, we don't try to delete |
|
353
|
|
|
{ |
|
354
|
|
|
$result=$accline->delete($user); |
|
355
|
|
|
} |
|
356
|
|
|
if ($result < 0) |
|
357
|
|
|
{ |
|
358
|
|
|
$this->error=$accline->error; |
|
359
|
|
|
$this->db->rollback(); |
|
360
|
|
|
return -4; |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
if (! $notrigger) |
|
365
|
|
|
{ |
|
366
|
|
|
// Appel des triggers |
|
367
|
|
|
$result=$this->call_trigger('PAYMENT_SUPPLIER_DELETE', $user); |
|
368
|
|
|
if ($result < 0) |
|
369
|
|
|
{ |
|
370
|
|
|
$this->db->rollback(); |
|
371
|
|
|
return -1; |
|
372
|
|
|
} |
|
373
|
|
|
// Fin appel triggers |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
$this->db->commit(); |
|
377
|
|
|
return 1; |
|
378
|
|
|
} |
|
379
|
|
|
else |
|
380
|
|
|
{ |
|
381
|
|
|
$this->error=$this->db->error; |
|
382
|
|
|
$this->db->rollback(); |
|
383
|
|
|
return -5; |
|
384
|
|
|
} |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* Information on object |
|
389
|
|
|
* |
|
390
|
|
|
* @param int $id Id du paiement dont il faut afficher les infos |
|
391
|
|
|
* @return void |
|
392
|
|
|
*/ |
|
393
|
|
|
function info($id) |
|
394
|
|
|
{ |
|
395
|
|
|
$sql = 'SELECT c.rowid, datec, fk_user_author as fk_user_creat, tms'; |
|
396
|
|
|
$sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn as c'; |
|
397
|
|
|
$sql.= ' WHERE c.rowid = '.$id; |
|
398
|
|
|
|
|
399
|
|
|
$resql = $this->db->query($sql); |
|
400
|
|
|
if ($resql) |
|
401
|
|
|
{ |
|
402
|
|
|
$num = $this->db->num_rows($resql); |
|
403
|
|
|
if ($num) |
|
404
|
|
|
{ |
|
405
|
|
|
$obj = $this->db->fetch_object($resql); |
|
406
|
|
|
$this->id = $obj->rowid; |
|
407
|
|
|
|
|
408
|
|
|
if ($obj->fk_user_creat) |
|
409
|
|
|
{ |
|
410
|
|
|
$cuser = new User($this->db); |
|
411
|
|
|
$cuser->fetch($obj->fk_user_creat); |
|
412
|
|
|
$this->user_creation = $cuser; |
|
413
|
|
|
} |
|
414
|
|
|
if ($obj->fk_user_modif) |
|
415
|
|
|
{ |
|
416
|
|
|
$muser = new User($this->db); |
|
417
|
|
|
$muser->fetch($obj->fk_user_modif); |
|
418
|
|
|
$this->user_modification = $muser; |
|
419
|
|
|
} |
|
420
|
|
|
$this->date_creation = $this->db->jdate($obj->datec); |
|
421
|
|
|
$this->date_modification = $this->db->jdate($obj->tms); |
|
422
|
|
|
} |
|
423
|
|
|
$this->db->free($resql); |
|
424
|
|
|
} |
|
425
|
|
|
else |
|
426
|
|
|
{ |
|
427
|
|
|
dol_print_error($this->db); |
|
428
|
|
|
} |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
/** |
|
432
|
|
|
* Return list of supplier invoices the payment point to |
|
433
|
|
|
* |
|
434
|
|
|
* @param string $filter SQL filter |
|
435
|
|
|
* @return array Array of supplier invoice id |
|
436
|
|
|
*/ |
|
437
|
|
|
function getBillsArray($filter='') |
|
438
|
|
|
{ |
|
439
|
|
|
$sql = 'SELECT fk_facturefourn'; |
|
440
|
|
|
$sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf, '.MAIN_DB_PREFIX.'facture_fourn as f'; |
|
441
|
|
|
$sql.= ' WHERE pf.fk_facturefourn = f.rowid AND fk_paiementfourn = '.$this->id; |
|
442
|
|
|
if ($filter) $sql.= ' AND '.$filter; |
|
443
|
|
|
|
|
444
|
|
|
dol_syslog(get_class($this).'::getBillsArray', LOG_DEBUG); |
|
445
|
|
|
$resql = $this->db->query($sql); |
|
446
|
|
|
if ($resql) |
|
447
|
|
|
{ |
|
448
|
|
|
$i=0; |
|
449
|
|
|
$num=$this->db->num_rows($resql); |
|
450
|
|
|
$billsarray=array(); |
|
451
|
|
|
|
|
452
|
|
|
while ($i < $num) |
|
453
|
|
|
{ |
|
454
|
|
|
$obj = $this->db->fetch_object($resql); |
|
455
|
|
|
$billsarray[$i]=$obj->fk_facturefourn; |
|
456
|
|
|
$i++; |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
return $billsarray; |
|
460
|
|
|
} |
|
461
|
|
|
else |
|
462
|
|
|
{ |
|
463
|
|
|
$this->error=$this->db->error(); |
|
464
|
|
|
dol_syslog(get_class($this).'::getBillsArray Error '.$this->error); |
|
465
|
|
|
return -1; |
|
466
|
|
|
} |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
/** |
|
470
|
|
|
* Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee) |
|
471
|
|
|
* |
|
472
|
|
|
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
|
473
|
|
|
* @return string Libelle |
|
474
|
|
|
*/ |
|
475
|
|
|
function getLibStatut($mode=0) |
|
476
|
|
|
{ |
|
477
|
|
|
return $this->LibStatut($this->statut,$mode); |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
|
|
/** |
|
481
|
|
|
* Renvoi le libelle d'un statut donne |
|
482
|
|
|
* |
|
483
|
|
|
* @param int $status Statut |
|
484
|
|
|
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
|
485
|
|
|
* @return string Libelle du statut |
|
486
|
|
|
*/ |
|
487
|
|
|
function LibStatut($status,$mode=0) |
|
488
|
|
|
{ |
|
489
|
|
|
global $langs; |
|
490
|
|
|
|
|
491
|
|
|
$langs->load('compta'); |
|
492
|
|
|
/*if ($mode == 0) |
|
493
|
|
|
{ |
|
494
|
|
|
if ($status == 0) return $langs->trans('ToValidate'); |
|
495
|
|
|
if ($status == 1) return $langs->trans('Validated'); |
|
496
|
|
|
} |
|
497
|
|
|
if ($mode == 1) |
|
498
|
|
|
{ |
|
499
|
|
|
if ($status == 0) return $langs->trans('ToValidate'); |
|
500
|
|
|
if ($status == 1) return $langs->trans('Validated'); |
|
501
|
|
|
} |
|
502
|
|
|
if ($mode == 2) |
|
503
|
|
|
{ |
|
504
|
|
|
if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate'); |
|
505
|
|
|
if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated'); |
|
506
|
|
|
} |
|
507
|
|
|
if ($mode == 3) |
|
508
|
|
|
{ |
|
509
|
|
|
if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1'); |
|
510
|
|
|
if ($status == 1) return img_picto($langs->trans('Validated'),'statut4'); |
|
511
|
|
|
} |
|
512
|
|
|
if ($mode == 4) |
|
513
|
|
|
{ |
|
514
|
|
|
if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate'); |
|
515
|
|
|
if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated'); |
|
516
|
|
|
} |
|
517
|
|
|
if ($mode == 5) |
|
518
|
|
|
{ |
|
519
|
|
|
if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1'); |
|
520
|
|
|
if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4'); |
|
521
|
|
|
} |
|
522
|
|
|
if ($mode == 6) |
|
523
|
|
|
{ |
|
524
|
|
|
if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1'); |
|
525
|
|
|
if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4'); |
|
526
|
|
|
}*/ |
|
527
|
|
|
return ''; |
|
528
|
|
|
} |
|
529
|
|
|
|
|
530
|
|
|
|
|
531
|
|
|
/** |
|
532
|
|
|
* Return clicable name (with picto eventually) |
|
533
|
|
|
* |
|
534
|
|
|
* @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
|
535
|
|
|
* @param string $option Sur quoi pointe le lien |
|
536
|
|
|
* @param string $mode 'withlistofinvoices'=Include list of invoices into tooltip |
|
537
|
|
|
* @return string Chaine avec URL |
|
538
|
|
|
*/ |
|
539
|
|
|
function getNomUrl($withpicto=0,$option='',$mode='withlistofinvoices') |
|
540
|
|
|
{ |
|
541
|
|
|
global $langs; |
|
542
|
|
|
|
|
543
|
|
|
$result=''; |
|
544
|
|
|
$text=$this->ref; // Sometimes ref contains label |
|
545
|
|
|
if (preg_match('/^\((.*)\)$/i',$text,$reg)) { |
|
546
|
|
|
// Label generique car entre parentheses. On l'affiche en le traduisant |
|
547
|
|
|
if ($reg[1]=='paiement') $reg[1]='Payment'; |
|
548
|
|
|
$text=$langs->trans($reg[1]); |
|
549
|
|
|
} |
|
550
|
|
|
$label = $langs->trans("ShowPayment").': '.$text; |
|
551
|
|
|
|
|
552
|
|
|
$linkstart = '<a href="'.DOL_URL_ROOT.'/fourn/paiement/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">'; |
|
553
|
|
|
$linkend = '</a>'; |
|
554
|
|
|
|
|
555
|
|
|
$result .= $linkstart; |
|
556
|
|
|
if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1); |
|
|
|
|
|
|
557
|
|
|
if ($withpicto != 2) $result.= $this->ref; |
|
558
|
|
|
$result .= $linkend; |
|
559
|
|
|
|
|
560
|
|
|
return $result; |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
/** |
|
564
|
|
|
* Initialise an instance with random values. |
|
565
|
|
|
* Used to build previews or test instances. |
|
566
|
|
|
* id must be 0 if object instance is a specimen. |
|
567
|
|
|
* |
|
568
|
|
|
* @param string $option ''=Create a specimen invoice with lines, 'nolines'=No lines |
|
569
|
|
|
* @return void |
|
570
|
|
|
*/ |
|
571
|
|
|
function initAsSpecimen($option='') |
|
572
|
|
|
{ |
|
573
|
|
|
global $user,$langs,$conf; |
|
574
|
|
|
|
|
575
|
|
|
$now=dol_now(); |
|
576
|
|
|
$arraynow=dol_getdate($now); |
|
577
|
|
|
$nownotime=dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']); |
|
578
|
|
|
|
|
579
|
|
|
// Initialize parameters |
|
580
|
|
|
$this->id=0; |
|
581
|
|
|
$this->ref = 'SPECIMEN'; |
|
582
|
|
|
$this->specimen=1; |
|
583
|
|
|
$this->facid = 1; |
|
584
|
|
|
$this->datepaye = $nownotime; |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
/** |
|
588
|
|
|
* Return next reference of supplier invoice not already used (or last reference) |
|
589
|
|
|
* according to numbering module defined into constant SUPPLIER_PAYMENT_ADDON |
|
590
|
|
|
* |
|
591
|
|
|
* @param Societe $soc object company |
|
592
|
|
|
* @param string $mode 'next' for next value or 'last' for last value |
|
593
|
|
|
* @return string free ref or last ref |
|
594
|
|
|
*/ |
|
595
|
|
|
function getNextNumRef($soc,$mode='next') |
|
596
|
|
|
{ |
|
597
|
|
|
global $conf, $db, $langs; |
|
598
|
|
|
$langs->load("bills"); |
|
599
|
|
|
|
|
600
|
|
|
// Clean parameters (if not defined or using deprecated value) |
|
601
|
|
|
if (empty($conf->global->SUPPLIER_PAYMENT_ADDON)) $conf->global->SUPPLIER_PAYMENT_ADDON='mod_supplier_payment_bronan'; |
|
602
|
|
|
else if ($conf->global->SUPPLIER_PAYMENT_ADDON=='brodator') $conf->global->SUPPLIER_PAYMENT_ADDON='mod_supplier_payment_brodator'; |
|
603
|
|
|
else if ($conf->global->SUPPLIER_PAYMENT_ADDON=='bronan') $conf->global->SUPPLIER_PAYMENT_ADDON='mod_supplier_payment_bronan'; |
|
604
|
|
|
|
|
605
|
|
|
if (! empty($conf->global->SUPPLIER_PAYMENT_ADDON)) |
|
606
|
|
|
{ |
|
607
|
|
|
$mybool=false; |
|
608
|
|
|
|
|
609
|
|
|
$file = $conf->global->SUPPLIER_PAYMENT_ADDON.".php"; |
|
610
|
|
|
$classname = $conf->global->SUPPLIER_PAYMENT_ADDON; |
|
611
|
|
|
|
|
612
|
|
|
// Include file with class |
|
613
|
|
|
$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); |
|
614
|
|
|
|
|
615
|
|
|
foreach ($dirmodels as $reldir) { |
|
616
|
|
|
|
|
617
|
|
|
$dir = dol_buildpath($reldir."core/modules/supplier_payment/"); |
|
618
|
|
|
|
|
619
|
|
|
// Load file with numbering class (if found) |
|
620
|
|
|
if (is_file($dir.$file) && is_readable($dir.$file)) |
|
621
|
|
|
{ |
|
622
|
|
|
$mybool |= include_once $dir . $file; |
|
623
|
|
|
} |
|
624
|
|
|
} |
|
625
|
|
|
|
|
626
|
|
|
// For compatibility |
|
627
|
|
|
if (! $mybool) |
|
628
|
|
|
{ |
|
629
|
|
|
$file = $conf->global->SUPPLIER_PAYMENT_ADDON.".php"; |
|
630
|
|
|
$classname = "mod_supplier_payment_".$conf->global->SUPPLIER_PAYMENT_ADDON; |
|
631
|
|
|
$classname = preg_replace('/\-.*$/','',$classname); |
|
632
|
|
|
// Include file with class |
|
633
|
|
|
foreach ($conf->file->dol_document_root as $dirroot) |
|
634
|
|
|
{ |
|
635
|
|
|
$dir = $dirroot."/core/modules/supplier_payment/"; |
|
636
|
|
|
|
|
637
|
|
|
// Load file with numbering class (if found) |
|
638
|
|
|
if (is_file($dir.$file) && is_readable($dir.$file)) { |
|
639
|
|
|
$mybool |= include_once $dir . $file; |
|
640
|
|
|
} |
|
641
|
|
|
} |
|
642
|
|
|
} |
|
643
|
|
|
|
|
644
|
|
|
if (! $mybool) |
|
645
|
|
|
{ |
|
646
|
|
|
dol_print_error('',"Failed to include file ".$file); |
|
647
|
|
|
return ''; |
|
648
|
|
|
} |
|
649
|
|
|
|
|
650
|
|
|
$obj = new $classname(); |
|
651
|
|
|
$numref = ""; |
|
652
|
|
|
$numref = $obj->getNextValue($soc,$this); |
|
653
|
|
|
|
|
654
|
|
|
/** |
|
655
|
|
|
* $numref can be empty in case we ask for the last value because if there is no invoice created with the |
|
656
|
|
|
* set up mask. |
|
657
|
|
|
*/ |
|
658
|
|
|
if ($mode != 'last' && !$numref) { |
|
659
|
|
|
dol_print_error($db,"SupplierPayment::getNextNumRef ".$obj->error); |
|
660
|
|
|
return ""; |
|
661
|
|
|
} |
|
662
|
|
|
|
|
663
|
|
|
return $numref; |
|
664
|
|
|
} |
|
665
|
|
|
else |
|
666
|
|
|
{ |
|
667
|
|
|
$langs->load("errors"); |
|
668
|
|
|
print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete"); |
|
669
|
|
|
return ""; |
|
670
|
|
|
} |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
|
|
/** |
|
674
|
|
|
* Create a document onto disk according to template model. |
|
675
|
|
|
* |
|
676
|
|
|
* @param string $modele Force template to use ('' to not force) |
|
677
|
|
|
* @param Translate $outputlangs Object lang a utiliser pour traduction |
|
678
|
|
|
* @param int $hidedetails Hide details of lines |
|
679
|
|
|
* @param int $hidedesc Hide description |
|
680
|
|
|
* @param int $hideref Hide ref |
|
681
|
|
|
* @return int <0 if KO, 0 if nothing done, >0 if OK |
|
682
|
|
|
*/ |
|
683
|
|
|
public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0) |
|
684
|
|
|
{ |
|
685
|
|
|
global $conf, $user, $langs; |
|
686
|
|
|
|
|
687
|
|
|
$langs->load("suppliers"); |
|
688
|
|
|
|
|
689
|
|
|
// Set the model on the model name to use |
|
690
|
|
|
if (empty($modele)) |
|
691
|
|
|
{ |
|
692
|
|
|
if (! empty($conf->global->SUPPLIER_PAYMENT_ADDON_PDF)) |
|
693
|
|
|
{ |
|
694
|
|
|
$modele = $conf->global->SUPPLIER_PAYMENT_ADDON_PDF; |
|
695
|
|
|
} |
|
696
|
|
|
else |
|
697
|
|
|
{ |
|
698
|
|
|
$modele = ''; // No default value. For supplier invoice, we allow to disable all PDF generation |
|
699
|
|
|
} |
|
700
|
|
|
} |
|
701
|
|
|
|
|
702
|
|
|
if (empty($modele)) |
|
703
|
|
|
{ |
|
704
|
|
|
return 0; |
|
705
|
|
|
} |
|
706
|
|
|
else |
|
707
|
|
|
{ |
|
708
|
|
|
$modelpath = "core/modules/supplier_payment/doc/"; |
|
709
|
|
|
|
|
710
|
|
|
return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref); |
|
711
|
|
|
} |
|
712
|
|
|
} |
|
713
|
|
|
|
|
714
|
|
|
|
|
715
|
|
|
|
|
716
|
|
|
/** |
|
717
|
|
|
* get the right way of payment |
|
718
|
|
|
* |
|
719
|
|
|
* @return string 'dolibarr' if standard comportment or paid in dolibarr currency, 'customer' if payment received from multicurrency inputs |
|
720
|
|
|
*/ |
|
721
|
|
|
function getWay() |
|
722
|
|
|
{ |
|
723
|
|
|
global $conf; |
|
724
|
|
|
|
|
725
|
|
|
$way = 'dolibarr'; |
|
726
|
|
|
if (!empty($conf->multicurrency->enabled)) |
|
727
|
|
|
{ |
|
728
|
|
|
foreach ($this->multicurrency_amounts as $value) |
|
729
|
|
|
{ |
|
730
|
|
|
if (!empty($value)) // one value found then payment is in invoice currency |
|
731
|
|
|
{ |
|
732
|
|
|
$way = 'customer'; |
|
733
|
|
|
break; |
|
734
|
|
|
} |
|
735
|
|
|
} |
|
736
|
|
|
} |
|
737
|
|
|
|
|
738
|
|
|
return $way; |
|
739
|
|
|
} |
|
740
|
|
|
} |
|
741
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: