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