dolibarr  17.0.4
paymentvarious.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017-2021 Alexandre Spangaro <aspangaro@open-dsi.fr>
3  * Copyright (C) 2018-2020 Frédéric France <frederic.france@netlogic.fr>
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 
25 // Put here all includes required by your class file
26 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27 
28 
33 {
37  public $element = 'variouspayment';
38 
42  public $table_element = 'payment_various';
43 
47  public $picto = 'payment';
48 
52  public $id;
53 
57  public $ref;
58 
62  public $tms;
63  public $datep;
64  public $datev;
65 
69  public $sens;
70  public $amount;
71  public $type_payment;
72  public $num_payment;
73  public $chqemetteur;
74  public $chqbank;
75  public $category_transaction;
76 
80  public $label;
81 
85  public $accountancy_code;
86 
90  public $subledger_account;
91 
95  public $fk_project;
96 
100  public $fk_account;
101 
106  public $accountid;
107 
111  public $fk_bank;
112 
116  public $categorie_transaction;
117 
121  public $fk_user_author;
122 
126  public $fk_user_modif;
127 
128 
132  public $fk_type;
133 
137  public $rappro;
138 
142  public $bank_num_releve;
143 
144 
170  // BEGIN MODULEBUILDER PROPERTIES
174  public $fields = array(
175  // TODO: fill this array
176  );
177  // END MODULEBUILDER PROPERTIES
178 
182  const STATUS_DRAFT = 0;
183 
184 
190  public function __construct(DoliDB $db)
191  {
192  $this->db = $db;
193  $this->element = 'payment_various';
194  $this->table_element = 'payment_various';
195  }
196 
204  public function update($user = null, $notrigger = 0)
205  {
206  global $conf, $langs;
207 
208  $error = 0;
209 
210  // Clean parameters
211  $this->amount = trim($this->amount);
212  $this->label = trim($this->label);
213  $this->note = trim($this->note);
214  $this->fk_bank = (int) $this->fk_bank;
215  $this->fk_user_author = (int) $this->fk_user_author;
216  $this->fk_user_modif = (int) $this->fk_user_modif;
217 
218  $this->db->begin();
219 
220  // Update request
221  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_various SET";
222  if ($this->tms) {
223  $sql .= " tms='".$this->db->idate($this->tms)."',";
224  }
225  $sql .= " datep='".$this->db->idate($this->datep)."',";
226  $sql .= " datev='".$this->db->idate($this->datev)."',";
227  $sql .= " sens=".(int) $this->sens.",";
228  $sql .= " amount=".price2num($this->amount).",";
229  $sql .= " fk_typepayment=".(int) $this->type_payment.",";
230  $sql .= " num_payment='".$this->db->escape($this->num_payment)."',";
231  $sql .= " label='".$this->db->escape($this->label)."',";
232  $sql .= " note='".$this->db->escape($this->note)."',";
233  $sql .= " accountancy_code='".$this->db->escape($this->accountancy_code)."',";
234  $sql .= " subledger_account='".$this->db->escape($this->subledger_account)."',";
235  $sql .= " fk_projet='".$this->db->escape($this->fk_project)."',";
236  $sql .= " fk_bank=".($this->fk_bank > 0 ? $this->fk_bank : "null").",";
237  $sql .= " fk_user_author=".(int) $this->fk_user_author.",";
238  $sql .= " fk_user_modif=".(int) $this->fk_user_modif;
239  $sql .= " WHERE rowid=".((int) $this->id);
240 
241  dol_syslog(get_class($this)."::update", LOG_DEBUG);
242  $resql = $this->db->query($sql);
243  if (!$resql) {
244  $this->error = "Error ".$this->db->lasterror();
245  return -1;
246  }
247 
248  if (!$notrigger) {
249  // Call trigger
250  $result = $this->call_trigger('PAYMENT_VARIOUS_MODIFY', $user);
251  if ($result < 0) {
252  $error++;
253  }
254  // End call triggers
255  }
256 
257  if (!$error) {
258  $this->db->commit();
259  return 1;
260  } else {
261  $this->db->rollback();
262  return -1;
263  }
264  }
265 
266 
274  public function fetch($id, $user = null)
275  {
276  $sql = "SELECT";
277  $sql .= " v.rowid,";
278  $sql .= " v.tms,";
279  $sql .= " v.datep,";
280  $sql .= " v.datev,";
281  $sql .= " v.sens,";
282  $sql .= " v.amount,";
283  $sql .= " v.fk_typepayment,";
284  $sql .= " v.num_payment,";
285  $sql .= " v.label,";
286  $sql .= " v.note as note_private,";
287  $sql .= " v.accountancy_code,";
288  $sql .= " v.subledger_account,";
289  $sql .= " v.fk_projet as fk_project,";
290  $sql .= " v.fk_bank,";
291  $sql .= " v.fk_user_author,";
292  $sql .= " v.fk_user_modif,";
293  $sql .= " b.fk_account,";
294  $sql .= " b.fk_type,";
295  $sql .= " b.rappro,";
296  $sql .= " b.num_releve as bank_num_releve";
297  $sql .= " FROM ".MAIN_DB_PREFIX."payment_various as v";
298  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON v.fk_bank = b.rowid";
299  $sql .= " WHERE v.rowid = ".((int) $id);
300 
301  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
302  $resql = $this->db->query($sql);
303  if ($resql) {
304  if ($this->db->num_rows($resql)) {
305  $obj = $this->db->fetch_object($resql);
306 
307  $this->id = $obj->rowid;
308  $this->ref = $obj->rowid;
309  $this->tms = $this->db->jdate($obj->tms);
310  $this->datep = $this->db->jdate($obj->datep);
311  $this->datev = $this->db->jdate($obj->datev);
312  $this->sens = $obj->sens;
313  $this->amount = $obj->amount;
314  $this->type_payment = $obj->fk_typepayment;
315  $this->num_payment = $obj->num_payment;
316  $this->label = $obj->label;
317  $this->note = $obj->note_private; // For backward compatibility
318  $this->note_private = $obj->note_private;
319  $this->subledger_account = $obj->subledger_account;
320  $this->accountancy_code = $obj->accountancy_code;
321  $this->fk_project = $obj->fk_project;
322  $this->fk_bank = $obj->fk_bank;
323  $this->fk_user_author = $obj->fk_user_author;
324  $this->fk_user_modif = $obj->fk_user_modif;
325  $this->fk_account = $obj->fk_account;
326  $this->fk_type = $obj->fk_type;
327  $this->rappro = $obj->rappro;
328  $this->bank_num_releve = $obj->bank_num_releve;
329  }
330  $this->db->free($resql);
331 
332  return 1;
333  } else {
334  $this->error = "Error ".$this->db->lasterror();
335  return -1;
336  }
337  }
338 
339 
346  public function delete($user)
347  {
348  global $conf, $langs;
349 
350  $error = 0;
351 
352  // Call trigger
353  $result = $this->call_trigger('PAYMENT_VARIOUS_DELETE', $user);
354  if ($result < 0) {
355  return -1;
356  }
357  // End call triggers
358 
359 
360  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_various";
361  $sql .= " WHERE rowid=".((int) $this->id);
362 
363  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
364  $resql = $this->db->query($sql);
365  if (!$resql) {
366  $this->error = "Error ".$this->db->lasterror();
367  return -1;
368  }
369 
370  return 1;
371  }
372 
373 
381  public function initAsSpecimen()
382  {
383  $this->id = 0;
384 
385  $this->tms = '';
386  $this->datep = '';
387  $this->datev = '';
388  $this->sens = '';
389  $this->amount = '';
390  $this->label = '';
391  $this->accountancy_code = '';
392  $this->subledger_account = '';
393  $this->note = '';
394  $this->fk_bank = '';
395  $this->fk_user_author = '';
396  $this->fk_user_modif = '';
397  }
398 
404  public function check()
405  {
406  $newamount = price2num($this->amount, 'MT');
407 
408  // Validation of parameters
409  if (!($newamount) > 0 || empty($this->datep)) {
410  return false;
411  }
412 
413  return true;
414  }
415 
422  public function create($user)
423  {
424  global $conf, $langs;
425 
426  $error = 0;
427  $now = dol_now();
428 
429  // Clean parameters
430  $this->amount = price2num(trim($this->amount));
431  $this->label = trim($this->label);
432  $this->note = trim($this->note);
433  $this->fk_bank = (int) $this->fk_bank;
434  $this->fk_user_author = (int) $this->fk_user_author;
435  $this->fk_user_modif = (int) $this->fk_user_modif;
436  $this->fk_account = (int) $this->fk_account;
437  if (empty($this->fk_account) && isset($this->accountid)) { // For compatibility
438  $this->fk_account = $this->accountid;
439  }
440 
441  // Check parameters
442  if (!$this->label) {
443  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
444  return -3;
445  }
446  if ($this->amount < 0 || $this->amount == '') {
447  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
448  return -5;
449  }
450  if (isModEnabled("banque") && (empty($this->fk_account) || $this->fk_account <= 0)) {
451  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("BankAccount"));
452  return -6;
453  }
454  if (isModEnabled("banque") && (empty($this->type_payment) || $this->type_payment <= 0)) {
455  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
456  return -7;
457  }
458 
459  $this->db->begin();
460 
461  // Insert into llx_payment_various
462  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_various (";
463  $sql .= " datep";
464  $sql .= ", datev";
465  $sql .= ", sens";
466  $sql .= ", amount";
467  $sql .= ", fk_typepayment";
468  $sql .= ", num_payment";
469  if ($this->note) {
470  $sql .= ", note";
471  }
472  $sql .= ", label";
473  $sql .= ", accountancy_code";
474  $sql .= ", subledger_account";
475  $sql .= ", fk_projet";
476  $sql .= ", fk_user_author";
477  $sql .= ", datec";
478  $sql .= ", fk_bank";
479  $sql .= ", entity";
480  $sql .= ")";
481  $sql .= " VALUES (";
482  $sql .= "'".$this->db->idate($this->datep)."'";
483  $sql .= ", '".$this->db->idate($this->datev)."'";
484  $sql .= ", '".$this->db->escape($this->sens)."'";
485  $sql .= ", ".price2num($this->amount);
486  $sql .= ", '".$this->db->escape($this->type_payment)."'";
487  $sql .= ", '".$this->db->escape($this->num_payment)."'";
488  if ($this->note) {
489  $sql .= ", '".$this->db->escape($this->note)."'";
490  }
491  $sql .= ", '".$this->db->escape($this->label)."'";
492  $sql .= ", '".$this->db->escape($this->accountancy_code)."'";
493  $sql .= ", '".$this->db->escape($this->subledger_account)."'";
494  $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
495  $sql .= ", ".((int) $user->id);
496  $sql .= ", '".$this->db->idate($now)."'";
497  $sql .= ", NULL"; // Filled later
498  $sql .= ", ".((int) $conf->entity);
499  $sql .= ")";
500 
501  dol_syslog(get_class($this)."::create", LOG_DEBUG);
502  $result = $this->db->query($sql);
503  if ($result) {
504  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_various");
505  $this->ref = $this->id;
506 
507  if ($this->id > 0) {
508  if (isModEnabled("banque") && !empty($this->amount)) {
509  // Insert into llx_bank
510  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
511 
512  $acc = new Account($this->db);
513  $result = $acc->fetch($this->fk_account);
514  if ($result <= 0) {
515  dol_print_error($this->db);
516  }
517 
518  // Insert payment into llx_bank
519  // Add link 'payment_various' in bank_url between payment and bank transaction
520  $sign = 1;
521  if ($this->sens == '0') {
522  $sign = -1;
523  }
524 
525  $bank_line_id = $acc->addline(
526  $this->datep,
527  $this->type_payment,
528  $this->label,
529  $sign * abs($this->amount),
530  $this->num_payment,
531  ($this->category_transaction > 0 ? $this->category_transaction : 0),
532  $user,
533  $this->chqemetteur,
534  $this->chqbank,
535  '',
536  $this->datev
537  );
538 
539  // Update fk_bank into llx_payment_various
540  // So we know the payment which has generate the banking ecriture
541  if ($bank_line_id > 0) {
542  $this->update_fk_bank($bank_line_id);
543  } else {
544  $this->error = $acc->error;
545  $error++;
546  }
547 
548  if (!$error) {
549  // Add link 'payment_various' in bank_url between payment and bank transaction
550  $url = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id=';
551 
552  $result = $acc->add_url_line($bank_line_id, $this->id, $url, "(VariousPayment)", "payment_various");
553  if ($result <= 0) {
554  $this->error = $acc->error;
555  $error++;
556  }
557  }
558 
559  if ($result <= 0) {
560  $this->error = $acc->error;
561  $error++;
562  }
563  }
564 
565  // Call trigger
566  $result = $this->call_trigger('PAYMENT_VARIOUS_CREATE', $user);
567  if ($result < 0) {
568  $error++;
569  }
570  // End call triggers
571  } else {
572  $error++;
573  }
574 
575  if (!$error) {
576  $this->db->commit();
577  return $this->id;
578  } else {
579  $this->db->rollback();
580  return -2;
581  }
582  } else {
583  $this->error = $this->db->error();
584  $this->db->rollback();
585  return -1;
586  }
587  }
588 
589  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
596  public function update_fk_bank($id_bank)
597  {
598  // phpcs:enable
599  $sql = 'UPDATE '.MAIN_DB_PREFIX.'payment_various SET fk_bank = '.((int) $id_bank);
600  $sql .= " WHERE rowid = ".((int) $this->id);
601  $result = $this->db->query($sql);
602  if ($result) {
603  return 1;
604  } else {
605  dol_print_error($this->db);
606  return -1;
607  }
608  }
609 
610 
617  public function getLibStatut($mode = 0)
618  {
619  return $this->LibStatut($this->statut, $mode);
620  }
621 
622  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
630  public function LibStatut($status, $mode = 0)
631  {
632  // phpcs:enable
633  global $langs;
634 
635  if (empty($status)) {
636  $status = 0;
637  }
638 
639  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
640  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
641  //$this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
642  //$this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
643  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
644  //$this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
645  //$this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
646  }
647 
648  $statusType = 'status'.$status;
649 
650  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
651  }
652 
653 
664  public function getNomUrl($withpicto = 0, $option = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
665  {
666  global $db, $conf, $langs, $hookmanager;
667  global $langs;
668 
669  if (!empty($conf->dol_no_mouse_hover)) {
670  $notooltip = 1; // Force disable tooltips
671  }
672 
673  $result = '';
674 
675  $label = '<u>'.$langs->trans("ShowVariousPayment").'</u>';
676  $label .= '<br>';
677  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
678 
679  $url = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id='.$this->id;
680 
681  if ($option != 'nolink') {
682  // Add param to save lastsearch_values or not
683  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
684  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
685  $add_save_lastsearch_values = 1;
686  }
687  if ($add_save_lastsearch_values) {
688  $url .= '&save_lastsearch_values=1';
689  }
690  }
691 
692  $linkclose = '';
693  if (empty($notooltip)) {
694  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
695  $label = $langs->trans("ShowMyObject");
696  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
697  }
698  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
699  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
700  } else {
701  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
702  }
703 
704  $linkstart = '<a href="'.$url.'"';
705  $linkstart .= $linkclose.'>';
706  $linkend = '</a>';
707 
708  $result .= $linkstart;
709  if ($withpicto) {
710  $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);
711  }
712  if ($withpicto != 2) {
713  $result .= $this->ref;
714  }
715  $result .= $linkend;
716  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
717 
718  global $action;
719  $hookmanager->initHooks(array('variouspayment'));
720  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
721  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
722  if ($reshook > 0) {
723  $result = $hookmanager->resPrint;
724  } else {
725  $result .= $hookmanager->resPrint;
726  }
727 
728  return $result;
729  }
730 
737  public function info($id)
738  {
739  $sql = 'SELECT v.rowid, v.datec, v.fk_user_author';
740  $sql .= ' FROM '.MAIN_DB_PREFIX.'payment_various as v';
741  $sql .= ' WHERE v.rowid = '.((int) $id);
742 
743  dol_syslog(get_class($this).'::info', LOG_DEBUG);
744  $result = $this->db->query($sql);
745 
746  if ($result) {
747  if ($this->db->num_rows($result)) {
748  $obj = $this->db->fetch_object($result);
749  $this->id = $obj->rowid;
750  if ($obj->fk_user_author) {
751  $cuser = new User($this->db);
752  $cuser->fetch($obj->fk_user_author);
753  $this->user_creation = $cuser;
754  }
755  $this->date_creation = $this->db->jdate($obj->datec);
756  if ($obj->fk_user_modif) {
757  $muser = new User($this->db);
758  $muser->fetch($obj->fk_user_modif);
759  $this->user_modif = $muser;
760  }
761  $this->date_modif = $this->db->jdate($obj->tms);
762  }
763  $this->db->free($result);
764  } else {
765  dol_print_error($this->db);
766  }
767  }
768 
774  public function getVentilExportCompta()
775  {
776  $banklineid = $this->fk_bank;
777 
778  $alreadydispatched = 0;
779 
780  $type = 'bank';
781 
782  $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) $banklineid);
783  $resql = $this->db->query($sql);
784  if ($resql) {
785  $obj = $this->db->fetch_object($resql);
786  if ($obj) {
787  $alreadydispatched = $obj->nb;
788  }
789  } else {
790  $this->error = $this->db->lasterror();
791  return -1;
792  }
793 
794  if ($alreadydispatched) {
795  return 1;
796  }
797  return 0;
798  }
799 }
$object ref
Definition: info.php:78
Class to manage bank accounts.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage various payments.
update_fk_bank($id_bank)
Update link between payment various and line generate into llx_bank.
getNomUrl($withpicto=0, $option='', $save_lastsearch_value=-1, $notooltip=0, $morecss='')
Send name clicable (with possibly the picto)
initAsSpecimen()
Initialise an instance with random values.
const STATUS_DRAFT
Draft status.
create($user)
Create in database.
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
fetch($id, $user=null)
Load object in memory from database.
info($id)
Information on record.
check()
Check if a miscellaneous payment can be created into database.
__construct(DoliDB $db)
Constructor.
getVentilExportCompta()
Return if a various payment linked to a bank line id was dispatched into bookkeeping.
getLibStatut($mode=0)
Retourne le libelle du statut.
update($user=null, $notrigger=0)
Update database.
Class to manage Dolibarr users.
Definition: user.class.php:47
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_now($mode='auto')
Return date for now.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.
Definition: inc.php:41