dolibarr  x.y.z
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 
154  // BEGIN MODULEBUILDER PROPERTIES
158  public $fields = array(
159  // TODO: fill this array
160  );
161  // END MODULEBUILDER PROPERTIES
162 
168  public function __construct(DoliDB $db)
169  {
170  $this->db = $db;
171  $this->element = 'payment_various';
172  $this->table_element = 'payment_various';
173  }
174 
182  public function update($user = null, $notrigger = 0)
183  {
184  global $conf, $langs;
185 
186  $error = 0;
187 
188  // Clean parameters
189  $this->amount = trim($this->amount);
190  $this->label = trim($this->label);
191  $this->note = trim($this->note);
192  $this->fk_bank = (int) $this->fk_bank;
193  $this->fk_user_author = (int) $this->fk_user_author;
194  $this->fk_user_modif = (int) $this->fk_user_modif;
195 
196  $this->db->begin();
197 
198  // Update request
199  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_various SET";
200  if ($this->tms) {
201  $sql .= " tms='".$this->db->idate($this->tms)."',";
202  }
203  $sql .= " datep='".$this->db->idate($this->datep)."',";
204  $sql .= " datev='".$this->db->idate($this->datev)."',";
205  $sql .= " sens=".(int) $this->sens.",";
206  $sql .= " amount=".price2num($this->amount).",";
207  $sql .= " fk_typepayment=".(int) $this->type_payment.",";
208  $sql .= " num_payment='".$this->db->escape($this->num_payment)."',";
209  $sql .= " label='".$this->db->escape($this->label)."',";
210  $sql .= " note='".$this->db->escape($this->note)."',";
211  $sql .= " accountancy_code='".$this->db->escape($this->accountancy_code)."',";
212  $sql .= " subledger_account='".$this->db->escape($this->subledger_account)."',";
213  $sql .= " fk_projet='".$this->db->escape($this->fk_project)."',";
214  $sql .= " fk_bank=".($this->fk_bank > 0 ? $this->fk_bank : "null").",";
215  $sql .= " fk_user_author=".(int) $this->fk_user_author.",";
216  $sql .= " fk_user_modif=".(int) $this->fk_user_modif;
217  $sql .= " WHERE rowid=".((int) $this->id);
218 
219  dol_syslog(get_class($this)."::update", LOG_DEBUG);
220  $resql = $this->db->query($sql);
221  if (!$resql) {
222  $this->error = "Error ".$this->db->lasterror();
223  return -1;
224  }
225 
226  if (!$notrigger) {
227  // Call trigger
228  $result = $this->call_trigger('PAYMENT_VARIOUS_MODIFY', $user);
229  if ($result < 0) {
230  $error++;
231  }
232  // End call triggers
233  }
234 
235  if (!$error) {
236  $this->db->commit();
237  return 1;
238  } else {
239  $this->db->rollback();
240  return -1;
241  }
242  }
243 
244 
252  public function fetch($id, $user = null)
253  {
254  global $langs;
255  $sql = "SELECT";
256  $sql .= " v.rowid,";
257  $sql .= " v.tms,";
258  $sql .= " v.datep,";
259  $sql .= " v.datev,";
260  $sql .= " v.sens,";
261  $sql .= " v.amount,";
262  $sql .= " v.fk_typepayment,";
263  $sql .= " v.num_payment,";
264  $sql .= " v.label,";
265  $sql .= " v.note,";
266  $sql .= " v.accountancy_code,";
267  $sql .= " v.subledger_account,";
268  $sql .= " v.fk_projet as fk_project,";
269  $sql .= " v.fk_bank,";
270  $sql .= " v.fk_user_author,";
271  $sql .= " v.fk_user_modif,";
272  $sql .= " b.fk_account,";
273  $sql .= " b.fk_type,";
274  $sql .= " b.rappro";
275  $sql .= " FROM ".MAIN_DB_PREFIX."payment_various as v";
276  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON v.fk_bank = b.rowid";
277  $sql .= " WHERE v.rowid = ".((int) $id);
278 
279  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
280  $resql = $this->db->query($sql);
281  if ($resql) {
282  if ($this->db->num_rows($resql)) {
283  $obj = $this->db->fetch_object($resql);
284 
285  $this->id = $obj->rowid;
286  $this->ref = $obj->rowid;
287  $this->tms = $this->db->jdate($obj->tms);
288  $this->datep = $this->db->jdate($obj->datep);
289  $this->datev = $this->db->jdate($obj->datev);
290  $this->sens = $obj->sens;
291  $this->amount = $obj->amount;
292  $this->type_payment = $obj->fk_typepayment;
293  $this->num_payment = $obj->num_payment;
294  $this->label = $obj->label;
295  $this->note = $obj->note;
296  $this->subledger_account = $obj->subledger_account;
297  $this->accountancy_code = $obj->accountancy_code;
298  $this->fk_project = $obj->fk_project;
299  $this->fk_bank = $obj->fk_bank;
300  $this->fk_user_author = $obj->fk_user_author;
301  $this->fk_user_modif = $obj->fk_user_modif;
302  $this->fk_account = $obj->fk_account;
303  $this->fk_type = $obj->fk_type;
304  $this->rappro = $obj->rappro;
305  }
306  $this->db->free($resql);
307 
308  return 1;
309  } else {
310  $this->error = "Error ".$this->db->lasterror();
311  return -1;
312  }
313  }
314 
315 
322  public function delete($user)
323  {
324  global $conf, $langs;
325 
326  $error = 0;
327 
328  // Call trigger
329  $result = $this->call_trigger('PAYMENT_VARIOUS_DELETE', $user);
330  if ($result < 0) {
331  return -1;
332  }
333  // End call triggers
334 
335 
336  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_various";
337  $sql .= " WHERE rowid=".((int) $this->id);
338 
339  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
340  $resql = $this->db->query($sql);
341  if (!$resql) {
342  $this->error = "Error ".$this->db->lasterror();
343  return -1;
344  }
345 
346  return 1;
347  }
348 
349 
357  public function initAsSpecimen()
358  {
359  $this->id = 0;
360 
361  $this->tms = '';
362  $this->datep = '';
363  $this->datev = '';
364  $this->sens = '';
365  $this->amount = '';
366  $this->label = '';
367  $this->accountancy_code = '';
368  $this->subledger_account = '';
369  $this->note = '';
370  $this->fk_bank = '';
371  $this->fk_user_author = '';
372  $this->fk_user_modif = '';
373  }
374 
380  public function check()
381  {
382  $newamount = price2num($this->amount, 'MT');
383 
384  // Validation of parameters
385  if (!($newamount) > 0 || empty($this->datep)) {
386  return false;
387  }
388 
389  return true;
390  }
391 
398  public function create($user)
399  {
400  global $conf, $langs;
401 
402  $error = 0;
403  $now = dol_now();
404 
405  // Clean parameters
406  $this->amount = price2num(trim($this->amount));
407  $this->label = trim($this->label);
408  $this->note = trim($this->note);
409  $this->fk_bank = (int) $this->fk_bank;
410  $this->fk_user_author = (int) $this->fk_user_author;
411  $this->fk_user_modif = (int) $this->fk_user_modif;
412  $this->fk_account = (int) $this->fk_account;
413  if (empty($this->fk_account) && isset($this->accountid)) { // For compatibility
414  $this->fk_account = $this->accountid;
415  }
416 
417  // Check parameters
418  if (!$this->label) {
419  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
420  return -3;
421  }
422  if ($this->amount < 0 || $this->amount == '') {
423  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
424  return -5;
425  }
426  if (isModEnabled("banque") && (empty($this->fk_account) || $this->fk_account <= 0)) {
427  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("BankAccount"));
428  return -6;
429  }
430  if (isModEnabled("banque") && (empty($this->type_payment) || $this->type_payment <= 0)) {
431  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
432  return -7;
433  }
434 
435  $this->db->begin();
436 
437  // Insert into llx_payment_various
438  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_various (";
439  $sql .= " datep";
440  $sql .= ", datev";
441  $sql .= ", sens";
442  $sql .= ", amount";
443  $sql .= ", fk_typepayment";
444  $sql .= ", num_payment";
445  if ($this->note) {
446  $sql .= ", note";
447  }
448  $sql .= ", label";
449  $sql .= ", accountancy_code";
450  $sql .= ", subledger_account";
451  $sql .= ", fk_projet";
452  $sql .= ", fk_user_author";
453  $sql .= ", datec";
454  $sql .= ", fk_bank";
455  $sql .= ", entity";
456  $sql .= ")";
457  $sql .= " VALUES (";
458  $sql .= "'".$this->db->idate($this->datep)."'";
459  $sql .= ", '".$this->db->idate($this->datev)."'";
460  $sql .= ", '".$this->db->escape($this->sens)."'";
461  $sql .= ", ".price2num($this->amount);
462  $sql .= ", '".$this->db->escape($this->type_payment)."'";
463  $sql .= ", '".$this->db->escape($this->num_payment)."'";
464  if ($this->note) {
465  $sql .= ", '".$this->db->escape($this->note)."'";
466  }
467  $sql .= ", '".$this->db->escape($this->label)."'";
468  $sql .= ", '".$this->db->escape($this->accountancy_code)."'";
469  $sql .= ", '".$this->db->escape($this->subledger_account)."'";
470  $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
471  $sql .= ", ".((int) $user->id);
472  $sql .= ", '".$this->db->idate($now)."'";
473  $sql .= ", NULL"; // Filled later
474  $sql .= ", ".((int) $conf->entity);
475  $sql .= ")";
476 
477  dol_syslog(get_class($this)."::create", LOG_DEBUG);
478  $result = $this->db->query($sql);
479  if ($result) {
480  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_various");
481  $this->ref = $this->id;
482 
483  if ($this->id > 0) {
484  if (isModEnabled("banque") && !empty($this->amount)) {
485  // Insert into llx_bank
486  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
487 
488  $acc = new Account($this->db);
489  $result = $acc->fetch($this->fk_account);
490  if ($result <= 0) {
491  dol_print_error($this->db);
492  }
493 
494  // Insert payment into llx_bank
495  // Add link 'payment_various' in bank_url between payment and bank transaction
496  $sign = 1;
497  if ($this->sens == '0') {
498  $sign = -1;
499  }
500 
501  $bank_line_id = $acc->addline(
502  $this->datep,
503  $this->type_payment,
504  $this->label,
505  $sign * abs($this->amount),
506  $this->num_payment,
507  ($this->category_transaction > 0 ? $this->category_transaction : 0),
508  $user,
509  $this->chqemetteur,
510  $this->chqbank,
511  '',
512  $this->datev
513  );
514 
515  // Update fk_bank into llx_payment_various
516  // So we know the payment which has generate the banking ecriture
517  if ($bank_line_id > 0) {
518  $this->update_fk_bank($bank_line_id);
519  } else {
520  $this->error = $acc->error;
521  $error++;
522  }
523 
524  if (!$error) {
525  // Add link 'payment_various' in bank_url between payment and bank transaction
526  $url = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id=';
527 
528  $result = $acc->add_url_line($bank_line_id, $this->id, $url, "(VariousPayment)", "payment_various");
529  if ($result <= 0) {
530  $this->error = $acc->error;
531  $error++;
532  }
533  }
534 
535  if ($result <= 0) {
536  $this->error = $acc->error;
537  $error++;
538  }
539  }
540 
541  // Call trigger
542  $result = $this->call_trigger('PAYMENT_VARIOUS_CREATE', $user);
543  if ($result < 0) {
544  $error++;
545  }
546  // End call triggers
547  } else {
548  $error++;
549  }
550 
551  if (!$error) {
552  $this->db->commit();
553  return $this->id;
554  } else {
555  $this->db->rollback();
556  return -2;
557  }
558  } else {
559  $this->error = $this->db->error();
560  $this->db->rollback();
561  return -1;
562  }
563  }
564 
565  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
572  public function update_fk_bank($id_bank)
573  {
574  // phpcs:enable
575  $sql = 'UPDATE '.MAIN_DB_PREFIX.'payment_various SET fk_bank = '.((int) $id_bank);
576  $sql .= " WHERE rowid = ".((int) $this->id);
577  $result = $this->db->query($sql);
578  if ($result) {
579  return 1;
580  } else {
581  dol_print_error($this->db);
582  return -1;
583  }
584  }
585 
586 
593  public function getLibStatut($mode = 0)
594  {
595  return $this->LibStatut($this->statut, $mode);
596  }
597 
598  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
606  public function LibStatut($status, $mode = 0)
607  {
608  // phpcs:enable
609  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
610  global $langs;
611  //$langs->load("mymodule@mymodule");
612  /*$this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
613  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
614  $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
615  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
616  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
617  $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');*/
618  }
619 
620  $statusType = 'status'.$status;
621 
622  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
623  }
624 
625 
636  public function getNomUrl($withpicto = 0, $option = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
637  {
638  global $db, $conf, $langs, $hookmanager;
639  global $langs;
640 
641  if (!empty($conf->dol_no_mouse_hover)) {
642  $notooltip = 1; // Force disable tooltips
643  }
644 
645  $result = '';
646 
647  $label = '<u>'.$langs->trans("ShowVariousPayment").'</u>';
648  $label .= '<br>';
649  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
650 
651  $url = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id='.$this->id;
652 
653  if ($option != 'nolink') {
654  // Add param to save lastsearch_values or not
655  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
656  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
657  $add_save_lastsearch_values = 1;
658  }
659  if ($add_save_lastsearch_values) {
660  $url .= '&save_lastsearch_values=1';
661  }
662  }
663 
664  $linkclose = '';
665  if (empty($notooltip)) {
666  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
667  $label = $langs->trans("ShowMyObject");
668  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
669  }
670  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
671  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
672  } else {
673  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
674  }
675 
676  $linkstart = '<a href="'.$url.'"';
677  $linkstart .= $linkclose.'>';
678  $linkend = '</a>';
679 
680  $result .= $linkstart;
681  if ($withpicto) {
682  $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);
683  }
684  if ($withpicto != 2) {
685  $result .= $this->ref;
686  }
687  $result .= $linkend;
688  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
689 
690  global $action;
691  $hookmanager->initHooks(array('variouspayment'));
692  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
693  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
694  if ($reshook > 0) {
695  $result = $hookmanager->resPrint;
696  } else {
697  $result .= $hookmanager->resPrint;
698  }
699 
700  return $result;
701  }
702 
709  public function info($id)
710  {
711  $sql = 'SELECT v.rowid, v.datec, v.fk_user_author';
712  $sql .= ' FROM '.MAIN_DB_PREFIX.'payment_various as v';
713  $sql .= ' WHERE v.rowid = '.((int) $id);
714 
715  dol_syslog(get_class($this).'::info', LOG_DEBUG);
716  $result = $this->db->query($sql);
717 
718  if ($result) {
719  if ($this->db->num_rows($result)) {
720  $obj = $this->db->fetch_object($result);
721  $this->id = $obj->rowid;
722  if ($obj->fk_user_author) {
723  $cuser = new User($this->db);
724  $cuser->fetch($obj->fk_user_author);
725  $this->user_creation = $cuser;
726  }
727  $this->date_creation = $this->db->jdate($obj->datec);
728  if ($obj->fk_user_modif) {
729  $muser = new User($this->db);
730  $muser->fetch($obj->fk_user_modif);
731  $this->user_modif = $muser;
732  }
733  $this->date_modif = $this->db->jdate($obj->tms);
734  }
735  $this->db->free($result);
736  } else {
737  dol_print_error($this->db);
738  }
739  }
740 
746  public function getVentilExportCompta()
747  {
748  $banklineid = $this->fk_bank;
749 
750  $alreadydispatched = 0;
751 
752  $type = 'bank';
753 
754  $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);
755  $resql = $this->db->query($sql);
756  if ($resql) {
757  $obj = $this->db->fetch_object($resql);
758  if ($obj) {
759  $alreadydispatched = $obj->nb;
760  }
761  } else {
762  $this->error = $this->db->lasterror();
763  return -1;
764  }
765 
766  if ($alreadydispatched) {
767  return 1;
768  }
769  return 0;
770  }
771 }
$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.
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:45
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