26 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
27 require_once DOL_DOCUMENT_ROOT.
'/compta/tva/class/tva.class.php';
38 public $element =
'payment_vat';
43 public $table_element =
'payment_vat';
48 public $picto =
'payment';
66 public $amounts = array();
71 public $fk_typepaiement;
92 public $fk_user_creat;
97 public $fk_user_modif;
119 public $paiementtype;
139 public function create($user, $closepaidcontrib = 0)
141 global $conf, $langs;
147 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
150 if (!$this->datepaye) {
151 $this->error =
'ErrorBadValueForParameterCreatePaymentVAT';
156 if (isset($this->fk_tva)) {
157 $this->fk_tva = (int) $this->fk_tva;
159 if (isset($this->amount)) {
160 $this->amount = trim($this->amount);
162 if (isset($this->fk_typepaiement)) {
163 $this->fk_typepaiement = (int) $this->fk_typepaiement;
165 if (isset($this->num_paiement)) {
166 $this->num_paiement = trim($this->num_paiement);
168 if (isset($this->num_payment)) {
169 $this->num_payment = trim($this->num_payment);
171 if (isset($this->note)) {
172 $this->note = trim($this->note);
174 if (isset($this->fk_bank)) {
175 $this->fk_bank = (int) $this->fk_bank;
177 if (isset($this->fk_user_creat)) {
178 $this->fk_user_creat = (int) $this->fk_user_creat;
180 if (isset($this->fk_user_modif)) {
181 $this->fk_user_modif = (int) $this->fk_user_modif;
185 foreach ($this->amounts as $key => $value) {
187 $this->amounts[$key] = $newvalue;
188 $totalamount += $newvalue;
193 if ($totalamount == 0) {
200 if ($totalamount != 0) {
201 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"payment_vat (fk_tva, datec, datep, amount,";
202 $sql .=
" fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
203 $sql .=
" VALUES ($this->chid, '".$this->db->idate($now).
"',";
204 $sql .=
" '".$this->db->idate($this->datepaye).
"',";
205 $sql .=
" ".((float) $totalamount).
",";
206 $sql .=
" ".((int) $this->paiementtype).
", '".$this->
db->escape($this->num_payment).
"', '".$this->
db->escape($this->note).
"', ".$user->id.
",";
211 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"payment_vat");
214 foreach ($this->amounts as $key => $amount) {
216 if (is_numeric($amount) && $amount <> 0) {
220 if ($closepaidcontrib) {
221 $contrib =
new Tva($this->
db);
222 $contrib->fetch($contribid);
223 $paiement = $contrib->getSommePaiement();
228 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
229 $remaintopay =
price2num($contrib->amount - $paiement - $creditnotes - $deposits,
'MT');
230 if ($remaintopay == 0) {
231 $result = $contrib->setPaid($user);
233 dol_syslog(
"Remain to pay for conrib ".$contribid.
" not null. We do nothing.");
243 $result = $this->
call_trigger(
'PAYMENTVAT_CREATE', $user);
248 if ($totalamount != 0 && !$error) {
249 $this->amount = $totalamount;
250 $this->total = $totalamount;
254 $this->error = $this->
db->error();
255 $this->
db->rollback();
271 $sql .=
" t.fk_tva,";
275 $sql .=
" t.amount,";
276 $sql .=
" t.fk_typepaiement,";
277 $sql .=
" t.num_paiement as num_payment,";
279 $sql .=
" t.fk_bank,";
280 $sql .=
" t.fk_user_creat,";
281 $sql .=
" t.fk_user_modif,";
282 $sql .=
" pt.code as type_code, pt.libelle as type_label,";
283 $sql .=
' b.fk_account';
284 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_vat as t LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as pt ON t.fk_typepaiement = pt.id";
285 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'bank as b ON t.fk_bank = b.rowid';
286 $sql .=
" WHERE t.rowid = ".((int) $id);
289 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
293 $obj = $this->
db->fetch_object(
$resql);
295 $this->
id = $obj->rowid;
296 $this->
ref = $obj->rowid;
298 $this->fk_tva = $obj->fk_tva;
299 $this->datec = $this->
db->jdate($obj->datec);
300 $this->tms = $this->
db->jdate($obj->tms);
301 $this->datep = $this->
db->jdate($obj->datep);
302 $this->amount = $obj->amount;
303 $this->fk_typepaiement = $obj->fk_typepaiement;
304 $this->num_paiement = $obj->num_payment;
305 $this->num_payment = $obj->num_payment;
306 $this->note = $obj->note;
307 $this->fk_bank = $obj->fk_bank;
308 $this->fk_user_creat = $obj->fk_user_creat;
309 $this->fk_user_modif = $obj->fk_user_modif;
311 $this->type_code = $obj->type_code;
312 $this->type_label = $obj->type_label;
314 $this->bank_account = $obj->fk_account;
315 $this->bank_line = $obj->fk_bank;
321 $this->error =
"Error ".$this->db->lasterror();
334 public function update($user =
null, $notrigger = 0)
336 global $conf, $langs;
341 if (isset($this->fk_tva)) {
342 $this->fk_tva = (int) $this->fk_tva;
344 if (isset($this->amount)) {
345 $this->amount = trim($this->amount);
347 if (isset($this->fk_typepaiement)) {
348 $this->fk_typepaiement = (int) $this->fk_typepaiement;
350 if (isset($this->num_paiement)) {
351 $this->num_paiement = trim($this->num_paiement);
353 if (isset($this->num_payment)) {
354 $this->num_payment = trim($this->num_payment);
356 if (isset($this->note)) {
357 $this->note = trim($this->note);
359 if (isset($this->fk_bank)) {
360 $this->fk_bank = (int) $this->fk_bank;
362 if (isset($this->fk_user_creat)) {
363 $this->fk_user_creat = (int) $this->fk_user_creat;
365 if (isset($this->fk_user_modif)) {
366 $this->fk_user_modif = (int) $this->fk_user_modif;
375 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"payment_vat SET";
377 $sql .=
" fk_tva=".(isset($this->fk_tva) ? $this->fk_tva :
"null").
",";
378 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
379 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
380 $sql .=
" datep=".(dol_strlen($this->datep) != 0 ?
"'".$this->db->idate($this->datep).
"'" :
'null').
",";
381 $sql .=
" amount=".(isset($this->amount) ? $this->amount :
"null").
",";
382 $sql .=
" fk_typepaiement=".(isset($this->fk_typepaiement) ? $this->fk_typepaiement :
"null").
",";
383 $sql .=
" num_paiement=".(isset($this->num_paiement) ?
"'".$this->db->escape($this->num_paiement).
"'" :
"null").
",";
384 $sql .=
" note=".(isset($this->note) ?
"'".$this->db->escape($this->note).
"'" :
"null").
",";
385 $sql .=
" fk_bank=".(isset($this->fk_bank) ? $this->fk_bank :
"null").
",";
386 $sql .=
" fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat :
"null").
",";
387 $sql .=
" fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif :
"null").
"";
390 $sql .=
" WHERE rowid=".((int) $this->
id);
394 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
398 $this->errors[] =
"Error ".$this->db->lasterror();
403 foreach ($this->errors as $errmsg) {
404 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
405 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
407 $this->
db->rollback();
423 public function delete($user, $notrigger = 0)
425 global $conf, $langs;
432 if ($this->bank_line > 0) {
434 $accline->fetch($this->bank_line);
435 $result = $accline->delete();
437 $this->errors[] = $accline->error;
443 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"payment_vat";
444 $sql .=
" WHERE rowid=".((int) $this->
id);
446 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
450 $this->errors[] =
"Error ".$this->db->lasterror();
456 foreach ($this->errors as $errmsg) {
457 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
458 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
460 $this->
db->rollback();
486 $object->fetch($fromid);
494 $object->context[
'createfromclone'] =
'createfromclone';
495 $result = $object->create($user);
499 $this->error = $object->error;
503 unset($object->context[
'createfromclone']);
510 $this->
db->rollback();
532 $this->fk_typepaiement =
'';
533 $this->num_payment =
'';
534 $this->note_private =
'';
535 $this->note_public =
'';
537 $this->fk_user_creat = 0;
538 $this->fk_user_modif = 0;
554 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
559 $this->num_payment = trim($this->num_payment ? $this->num_payment : $this->num_paiement);
564 include_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
567 $acc->fetch($accountid);
570 if ($mode ==
'payment_vat') {
575 $bank_line_id = $acc->addline(
589 if ($bank_line_id > 0) {
598 if ($mode ==
'payment_vat') {
599 $url = DOL_URL_ROOT.
'/compta/payment_vat/card.php?id=';
602 $result = $acc->add_url_line($bank_line_id, $this->
id, $url,
'(paiement)', $mode);
610 $linkaddedforthirdparty = array();
611 foreach ($this->amounts as $key => $value) {
612 if ($mode ==
'payment_vat') {
613 $tva =
new Tva($this->
db);
615 $result = $acc->add_url_line($bank_line_id, $tva->id, DOL_URL_ROOT.
'/compta/tva/card.php?id=',
'('.$tva->label.
')',
'vat');
622 $this->error = $acc->error;
645 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"payment_vat SET fk_bank = ".((int) $id_bank).
" WHERE rowid = ".((int) $this->
id);
647 dol_syslog(get_class($this).
"::update_fk_bank", LOG_DEBUG);
648 $result = $this->
db->query($sql);
652 $this->error = $this->
db->error();
666 return $this->
LibStatut($this->statut, $mode);
682 $langs->load(
'compta');
734 if (empty($this->
ref)) {
735 $this->
ref = $this->lib;
738 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"VATPayment").
'</u>';
739 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
740 if (!empty($this->label)) {
741 $labeltoshow = $this->label;
743 if (preg_match(
'/^\((.*)\)$/i', $this->label, $reg)) {
745 if ($reg[1] ==
'paiement') {
748 $labeltoshow = $langs->trans($reg[1]);
750 $label .=
'<br><b>'.$langs->trans(
'Label').
':</b> '.$labeltoshow;
753 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->datep,
'day');
756 if (!empty($this->
id)) {
757 $link =
'<a href="'.DOL_URL_ROOT.
'/compta/payment_vat/card.php?id='.$this->
id.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
761 $result .= ($link.img_object($label,
'payment',
'class="classfortooltip"').$linkend.
' ');
763 if ($withpicto && $withpicto != 2) {
766 if ($withpicto != 2) {
767 $result .= $link.($maxlen ?
dol_trunc($this->
ref, $maxlen) : $this->ref).$linkend;