27 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/salaries/class/salary.class.php';
39 public $element =
'payment_salary';
44 public $table_element =
'payment_salary';
49 public $picto =
'payment';
67 public $amounts = array();
72 public $fk_typepayment;
93 public $fk_user_author;
98 public $fk_user_modif;
103 public $fields = array(
104 'rowid' => array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'index'=>1,
'position'=>1,
'comment'=>
'Id'),
125 public function create($user, $closepaidcontrib = 0)
127 global $conf, $langs;
133 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
136 if (!$this->datepaye) {
137 $this->error =
'ErrorBadValueForParameterCreatePaymentSalary';
142 if (isset($this->fk_salary)) $this->fk_salary = (int) $this->fk_salary;
143 if (isset($this->amount)) $this->amount = trim($this->amount);
144 if (isset($this->fk_typepayment)) $this->fk_typepayment = (
int) $this->fk_typepayment;
145 if (isset($this->num_paiement)) $this->num_paiement = trim($this->num_paiement);
146 if (isset($this->num_payment)) $this->num_payment = trim($this->num_payment);
147 if (isset($this->note)) $this->note = trim($this->note);
148 if (isset($this->fk_bank)) $this->fk_bank = (
int) $this->fk_bank;
149 if (isset($this->fk_user_author)) $this->fk_user_author = (
int) $this->fk_user_author;
150 if (isset($this->fk_user_modif)) $this->fk_user_modif = (
int) $this->fk_user_modif;
153 foreach ($this->amounts as $key => $value) {
155 $this->amounts[$key] = $newvalue;
156 $totalamount += $newvalue;
161 if ($totalamount == 0)
return -1;
166 if ($totalamount != 0) {
167 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_salary (entity, fk_salary, datec, datep, amount,";
168 $sql .=
" fk_typepayment, num_payment, note, fk_user_author, fk_bank)";
169 $sql .=
" VALUES (".((int) $conf->entity).
", ".((int) $this->chid).
", '".$this->
db->idate($now).
"',";
170 $sql .=
" '".$this->db->idate($this->datepaye).
"',";
171 $sql .=
" ".price2num($totalamount).
",";
172 $sql .=
" ".((int) $this->paiementtype).
", '".$this->
db->escape($this->num_payment).
"', '".$this->
db->escape($this->note).
"', ".((int) $user->id).
",";
177 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"payment_salary");
180 foreach ($this->amounts as $key => $amount) {
182 if (is_numeric($amount) && $amount <> 0) {
186 if ($closepaidcontrib) {
188 $contrib->fetch($contribid);
189 $paiement = $contrib->getSommePaiement();
194 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
195 $remaintopay =
price2num($contrib->amount - $paiement - $creditnotes - $deposits,
'MT');
196 if ($remaintopay == 0) {
197 $result = $contrib->set_paid($user);
198 }
else dol_syslog(
"Remain to pay for conrib ".$contribid.
" not null. We do nothing.");
207 $result = $this->
call_trigger(
'PAYMENTSALARY_CREATE', $user);
208 if ($result < 0) $error++;
210 if ($totalamount != 0 && !$error) {
211 $this->amount = $totalamount;
212 $this->total = $totalamount;
216 $this->error = $this->
db->error();
217 $this->
db->rollback();
233 $sql .=
" t.fk_salary,";
237 $sql .=
" t.amount,";
238 $sql .=
" t.fk_typepayment,";
239 $sql .=
" t.num_payment as num_payment,";
241 $sql .=
" t.fk_bank,";
242 $sql .=
" t.fk_user_author,";
243 $sql .=
" t.fk_user_modif,";
244 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
245 $sql .=
' b.fk_account';
246 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_salary as t LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepayment = pt.id";
247 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
248 $sql .=
" WHERE t.rowid = ".((int) $id);
251 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
255 $obj = $this->
db->fetch_object(
$resql);
257 $this->
id = $obj->rowid;
258 $this->
ref = $obj->rowid;
260 $this->fk_salary = $obj->fk_salary;
261 $this->datec = $this->
db->jdate($obj->datec);
262 $this->tms = $this->
db->jdate($obj->tms);
263 $this->datep = $this->
db->jdate($obj->datep);
264 $this->amount = $obj->amount;
265 $this->fk_typepayment = $obj->fk_typepayment;
266 $this->num_paiement = $obj->num_payment;
267 $this->num_payment = $obj->num_payment;
268 $this->note = $obj->note;
269 $this->fk_bank = $obj->fk_bank;
270 $this->fk_user_author = $obj->fk_user_author;
271 $this->fk_user_modif = $obj->fk_user_modif;
273 $this->type_code = $obj->type_code;
274 $this->type_label = $obj->type_label;
276 $this->bank_account = $obj->fk_account;
277 $this->bank_line = $obj->fk_bank;
283 $this->error =
"Error ".$this->db->lasterror();
296 public function update($user =
null, $notrigger = 0)
298 global $conf, $langs;
303 if (isset($this->fk_salary)) $this->fk_salary = (int) $this->fk_salary;
304 if (isset($this->amount)) $this->amount = trim($this->amount);
305 if (isset($this->fk_typepayment)) $this->fk_typepayment = (
int) $this->fk_typepayment;
306 if (isset($this->num_paiement)) $this->num_paiement = trim($this->num_paiement);
307 if (isset($this->num_payment)) $this->num_payment = trim($this->num_payment);
308 if (isset($this->note)) $this->note = trim($this->note);
309 if (isset($this->fk_bank)) $this->fk_bank = (
int) $this->fk_bank;
310 if (isset($this->fk_user_author)) $this->fk_user_author = (
int) $this->fk_user_author;
311 if (isset($this->fk_user_modif)) $this->fk_user_modif = (
int) $this->fk_user_modif;
317 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"payment_salary SET";
318 $sql .=
" fk_salary=".(isset($this->fk_salary) ? $this->fk_salary :
"null").
",";
319 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
320 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
321 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
322 $sql .=
" amount=".(isset($this->amount) ? $this->amount :
"null").
",";
323 $sql .=
" fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment :
"null").
",";
324 $sql .=
" num_payment=".(isset($this->num_payment) ?
"'".$this->db->escape($this->num_payment).
"'" :
"null").
",";
325 $sql .=
" note=".(isset($this->note) ?
"'".$this->db->escape($this->note).
"'" :
"null").
",";
326 $sql .=
" fk_bank=".(isset($this->fk_bank) ? $this->fk_bank :
"null").
",";
327 $sql .=
" fk_user_author=".(isset($this->fk_user_author) ? $this->fk_user_author :
"null").
",";
328 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif :
"null").
"";
329 $sql .=
" WHERE rowid=".((int) $this->
id);
333 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
335 if (!
$resql) { $error++; $this->errors[] =
"Error ".$this->db->lasterror(); }
339 foreach ($this->errors as $errmsg) {
340 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
341 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
343 $this->
db->rollback();
359 public function delete($user, $notrigger = 0)
361 global $conf, $langs;
368 if ($this->bank_line > 0) {
370 $accline->fetch($this->bank_line);
371 $result = $accline->delete();
373 $this->errors[] = $accline->error;
379 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_salary";
380 $sql .=
" WHERE rowid=".((int) $this->
id);
382 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
384 if (!
$resql) { $error++; $this->errors[] =
"Error ".$this->db->lasterror(); }
389 foreach ($this->errors as $errmsg) {
390 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
391 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
393 $this->
db->rollback();
419 $object->fetch($fromid);
427 $object->context[
'createfromclone'] =
'createfromclone';
428 $result = $object->create($user);
432 $this->error = $object->error;
436 unset($object->context[
'createfromclone']);
443 $this->
db->rollback();
460 $this->fk_salary =
'';
465 $this->fk_typepayment =
'';
466 $this->num_payment =
'';
467 $this->note_private =
'';
468 $this->note_public =
'';
470 $this->fk_user_author =
'';
471 $this->fk_user_modif =
'';
487 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
489 global $conf, $langs;
492 $this->num_payment = trim($this->num_payment ? $this->num_payment : $this->num_paiement);
497 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
500 $acc->fetch($accountid);
505 $bank_line_id = $acc->addline(
521 if ($bank_line_id > 0) {
530 if ($mode ==
'payment_salary') {
531 $url = DOL_URL_ROOT.
'/salaries/payment_salary/card.php?id=';
535 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
543 foreach ($this->amounts as $key => $value) {
545 if ($mode ==
'payment_salary') {
547 $salary->fetch($key);
548 $salary->fetch_user($salary->fk_user);
550 $fuser = $salary->user;
552 if ($fuser->id > 0) {
553 $result = $acc->add_url_line(
556 DOL_URL_ROOT.
'/user/card.php?id=',
557 $fuser->getFullName($langs),
562 $this->error = $this->
db->lasterror();
563 dol_syslog(get_class($this) .
'::addPaymentToBank ' . $this->error);
570 $this->error = $acc->error;
593 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"payment_salary SET fk_bank = ".((int) $id_bank).
" WHERE rowid = ".((int) $this->
id);
595 dol_syslog(get_class($this).
"::update_fk_bank", LOG_DEBUG);
596 $result = $this->
db->query($sql);
600 $this->error = $this->
db->error();
614 return $this->
LibStatut($this->statut, $mode);
630 $langs->load(
'compta');
682 if (empty($this->
ref)) $this->
ref = $this->lib;
684 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"SalaryPayment").
'</u>';
685 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
686 if (!empty($this->label)) {
687 $labeltoshow = $this->label;
689 if (preg_match(
'/^\((.*)\)$/i', $this->label, $reg)) {
691 if ($reg[1] ==
'paiement') $reg[1] =
'Payment';
692 $labeltoshow = $langs->trans($reg[1]);
694 $label .=
'<br><b>'.$langs->trans(
'Label').
':</b> '.$labeltoshow;
697 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->datep,
'day');
700 if (!empty($this->
id)) {
701 $link =
'<a href="'.DOL_URL_ROOT.
'/salaries/payment_salary/card.php?id='.$this->
id.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
704 if ($withpicto) $result .= ($link.img_object($label,
'payment',
'class="classfortooltip"').$linkend.
' ');
705 if ($withpicto && $withpicto != 2) $result .=
' ';
706 if ($withpicto != 2) $result .= $link.($maxlen ?
dol_trunc($this->
ref, $maxlen) : $this->ref).$linkend;