dolibarr  13.0.2
commoninvoice.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
3  * Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
4  * Copyright (C) 2012-2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
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 
26 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
28 
32 abstract class CommonInvoice extends CommonObject
33 {
34  use CommonIncoterm;
35 
39  const TYPE_STANDARD = 0;
40 
44  const TYPE_REPLACEMENT = 1;
45 
49  const TYPE_CREDIT_NOTE = 2;
50 
54  const TYPE_DEPOSIT = 3;
55 
60  const TYPE_PROFORMA = 4;
61 
65  const TYPE_SITUATION = 5;
66 
70  const STATUS_DRAFT = 0;
71 
75  const STATUS_VALIDATED = 1;
76 
84  const STATUS_CLOSED = 2;
85 
93  const STATUS_ABANDONED = 3;
94 
95 
103  public function getRemainToPay($multicurrency = 0)
104  {
105  $alreadypaid = 0.0;
106  $alreadypaid += $this->getSommePaiement($multicurrency);
107  $alreadypaid += $this->getSumDepositsUsed($multicurrency);
108  $alreadypaid += $this->getSumCreditNotesUsed($multicurrency);
109 
110  $remaintopay = price2num($this->total_ttc - $alreadypaid, 'MT');
111  if ($this->statut == self::STATUS_CLOSED && $this->close_code == 'discount_vat') { // If invoice closed with discount for anticipated payment
112  $remaintopay = 0.0;
113  }
114  return $remaintopay;
115  }
116 
124  public function getSommePaiement($multicurrency = 0)
125  {
126  $table = 'paiement_facture';
127  $field = 'fk_facture';
128  if ($this->element == 'facture_fourn' || $this->element == 'invoice_supplier')
129  {
130  $table = 'paiementfourn_facturefourn';
131  $field = 'fk_facturefourn';
132  }
133 
134  $sql = 'SELECT sum(amount) as amount, sum(multicurrency_amount) as multicurrency_amount';
135  $sql .= ' FROM '.MAIN_DB_PREFIX.$table;
136  $sql .= ' WHERE '.$field.' = '.$this->id;
137 
138  dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
139  $resql = $this->db->query($sql);
140  if ($resql)
141  {
142  $obj = $this->db->fetch_object($resql);
143  $this->db->free($resql);
144  if ($multicurrency) return $obj->multicurrency_amount;
145  else return $obj->amount;
146  } else {
147  $this->error = $this->db->lasterror();
148  return -1;
149  }
150  }
151 
159  public function getSumDepositsUsed($multicurrency = 0)
160  {
161  if ($this->element == 'facture_fourn' || $this->element == 'invoice_supplier')
162  {
163  // TODO
164  return 0.0;
165  }
166 
167  require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
168 
169  $discountstatic = new DiscountAbsolute($this->db);
170  $result = $discountstatic->getSumDepositsUsed($this, $multicurrency);
171  if ($result >= 0) {
172  return $result;
173  } else {
174  $this->error = $discountstatic->error;
175  return -1;
176  }
177  }
178 
185  public function getSumCreditNotesUsed($multicurrency = 0)
186  {
187  require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
188 
189  $discountstatic = new DiscountAbsolute($this->db);
190  $result = $discountstatic->getSumCreditNotesUsed($this, $multicurrency);
191  if ($result >= 0) {
192  return $result;
193  } else {
194  $this->error = $discountstatic->error;
195  return -1;
196  }
197  }
198 
205  public function getSumFromThisCreditNotesNotUsed($multicurrency = 0)
206  {
207  require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
208 
209  $discountstatic = new DiscountAbsolute($this->db);
210  $result = $discountstatic->getSumFromThisCreditNotesNotUsed($this, $multicurrency);
211  if ($result >= 0) {
212  return $result;
213  } else {
214  $this->error = $discountstatic->error;
215  return -1;
216  }
217  }
218 
224  public function getListIdAvoirFromInvoice()
225  {
226  $idarray = array();
227 
228  $sql = 'SELECT rowid';
229  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element;
230  $sql .= ' WHERE fk_facture_source = '.$this->id;
231  $sql .= ' AND type = 2';
232  $resql = $this->db->query($sql);
233  if ($resql)
234  {
235  $num = $this->db->num_rows($resql);
236  $i = 0;
237  while ($i < $num)
238  {
239  $row = $this->db->fetch_row($resql);
240  $idarray[] = $row[0];
241  $i++;
242  }
243  } else {
244  dol_print_error($this->db);
245  }
246  return $idarray;
247  }
248 
255  public function getIdReplacingInvoice($option = '')
256  {
257  $sql = 'SELECT rowid';
258  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element;
259  $sql .= ' WHERE fk_facture_source = '.$this->id;
260  $sql .= ' AND type < 2';
261  if ($option == 'validated') $sql .= ' AND fk_statut = 1';
262  // PROTECTION BAD DATA
263  // In case the database is corrupted and there is a valid replectement invoice
264  // and another no, priority is given to the valid one.
265  // Should not happen (unless concurrent access and 2 people have created a
266  // replacement invoice for the same invoice at the same time)
267  $sql .= ' ORDER BY fk_statut DESC';
268 
269  $resql = $this->db->query($sql);
270  if ($resql)
271  {
272  $obj = $this->db->fetch_object($resql);
273  if ($obj)
274  {
275  // If there is any
276  return $obj->rowid;
277  } else {
278  // If no invoice replaces it
279  return 0;
280  }
281  } else {
282  return -1;
283  }
284  }
285 
292  public function getListOfPayments($filtertype = '')
293  {
294  $retarray = array();
295 
296  $table = 'paiement_facture';
297  $table2 = 'paiement';
298  $field = 'fk_facture';
299  $field2 = 'fk_paiement';
300  $field3 = ', p.ref_ext';
301  $sharedentity = 'facture';
302  if ($this->element == 'facture_fourn' || $this->element == 'invoice_supplier')
303  {
304  $table = 'paiementfourn_facturefourn';
305  $table2 = 'paiementfourn';
306  $field = 'fk_facturefourn';
307  $field2 = 'fk_paiementfourn';
308  $field3 = '';
309  $sharedentity = 'facture_fourn';
310  }
311 
312  $sql = 'SELECT p.ref, pf.amount, pf.multicurrency_amount, p.fk_paiement, p.datep, p.num_paiement as num, t.code'.$field3;
313  $sql .= ' FROM '.MAIN_DB_PREFIX.$table.' as pf, '.MAIN_DB_PREFIX.$table2.' as p, '.MAIN_DB_PREFIX.'c_paiement as t';
314  $sql .= ' WHERE pf.'.$field.' = '.$this->id;
315  //$sql.= ' WHERE pf.'.$field.' = 1';
316  $sql .= ' AND pf.'.$field2.' = p.rowid';
317  $sql .= ' AND p.fk_paiement = t.id';
318  $sql .= ' AND p.entity IN ('.getEntity($sharedentity).')';
319  if ($filtertype) $sql .= " AND t.code='PRE'";
320 
321  dol_syslog(get_class($this)."::getListOfPayments", LOG_DEBUG);
322  $resql = $this->db->query($sql);
323  if ($resql)
324  {
325  $num = $this->db->num_rows($resql);
326  $i = 0;
327  while ($i < $num)
328  {
329  $obj = $this->db->fetch_object($resql);
330  $tmp = array('amount'=>$obj->amount, 'type'=>$obj->code, 'date'=>$obj->datep, 'num'=>$obj->num, 'ref'=>$obj->ref);
331  if (!empty($field3)) {
332  $tmp['ref_ext'] = $obj->ref_ext;
333  }
334  $retarray[] = $tmp;
335  $i++;
336  }
337  $this->db->free($resql);
338 
339  //look for credit notes and discounts and deposits
340  $sql = '';
341  if ($this->element == 'facture' || $this->element == 'invoice')
342  {
343  $sql = 'SELECT rc.amount_ttc as amount, rc.multicurrency_amount_ttc as multicurrency_amount, rc.datec as date, f.ref as ref, rc.description as type';
344  $sql .= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc, '.MAIN_DB_PREFIX.'facture as f';
345  $sql .= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.$this->id;
346  $sql .= ' AND (f.type = 2 OR f.type = 0 OR f.type = 3)'; // Find discount coming from credit note or excess received or deposits (payments from deposits are always null except if FACTURE_DEPOSITS_ARE_JUST_PAYMENTS is set)
347  } elseif ($this->element == 'facture_fourn' || $this->element == 'invoice_supplier')
348  {
349  $sql = 'SELECT rc.amount_ttc as amount, rc.multicurrency_amount_ttc as multicurrency_amount, rc.datec as date, f.ref as ref, rc.description as type';
350  $sql .= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc, '.MAIN_DB_PREFIX.'facture_fourn as f';
351  $sql .= ' WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = '.$this->id;
352  $sql .= ' AND (f.type = 2 OR f.type = 0 OR f.type = 3)'; // Find discount coming from credit note or excess received or deposits (payments from deposits are always null except if FACTURE_DEPOSITS_ARE_JUST_PAYMENTS is set)
353  }
354 
355  if ($sql) {
356  $resql = $this->db->query($sql);
357  if ($resql)
358  {
359  $num = $this->db->num_rows($resql);
360  $i = 0;
361  while ($i < $num)
362  {
363  $obj = $this->db->fetch_object($resql);
364  if ($multicurrency) {
365  $retarray[] = array('amount'=>$obj->multicurrency_amount, 'type'=>$obj->type, 'date'=>$obj->date, 'num'=>'0', 'ref'=>$obj->ref);
366  } else {
367  $retarray[] = array('amount'=>$obj->amount, 'type'=>$obj->type, 'date'=>$obj->date, 'num'=>'', 'ref'=>$obj->ref);
368  }
369  $i++;
370  }
371  } else {
372  $this->error = $this->db->lasterror();
373  dol_print_error($this->db);
374  return array();
375  }
376  $this->db->free($resql);
377  }
378 
379  return $retarray;
380  } else {
381  $this->error = $this->db->lasterror();
382  dol_print_error($this->db);
383  return array();
384  }
385  }
386 
387 
388  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
402  public function is_erasable()
403  {
404  // phpcs:enable
405  global $conf;
406 
407  // We check if invoice is a temporary number (PROVxxxx)
408  $tmppart = substr($this->ref, 1, 4);
409 
410  if ($this->statut == self::STATUS_DRAFT && $tmppart === 'PROV') // If draft invoice and ref not yet defined
411  {
412  return 1;
413  }
414 
415  if (!empty($conf->global->INVOICE_CAN_NEVER_BE_REMOVED)) return 0;
416 
417  // If not a draft invoice and not temporary invoice
418  if ($tmppart !== 'PROV')
419  {
420  $ventilExportCompta = $this->getVentilExportCompta();
421  if ($ventilExportCompta != 0) return -1;
422 
423  // Get last number of validated invoice
424  if ($this->element != 'invoice_supplier')
425  {
426  if (empty($this->thirdparty)) $this->fetch_thirdparty(); // We need to have this->thirdparty defined, in case of numbering rule use tags that depend on thirdparty (like {t} tag).
427  $maxref = $this->getNextNumRef($this->thirdparty, 'last');
428 
429  // If there is no invoice into the reset range and not already dispatched, we can delete
430  // If invoice to delete is last one and not already dispatched, we can delete
431  if (empty($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED) && $maxref != '' && $maxref != $this->ref) return -2;
432 
433  // TODO If there is payment in bookkeeping, check payment is not dispatched in accounting
434  // ...
435 
436  if ($this->situation_cycle_ref && method_exists($this, 'is_last_in_cycle'))
437  {
438  $last = $this->is_last_in_cycle();
439  if (!$last) return -3;
440  }
441  }
442  }
443 
444  // Test if there is at least one payment. If yes, refuse to delete.
445  if (empty($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED) && $this->getSommePaiement() > 0) return -4;
446 
447  return 2;
448  }
449 
455  public function getVentilExportCompta()
456  {
457  $alreadydispatched = 0;
458 
459  $type = 'customer_invoice';
460  if ($this->element == 'invoice_supplier') $type = 'supplier_invoice';
461 
462  $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 = ".$this->id;
463  $resql = $this->db->query($sql);
464  if ($resql)
465  {
466  $obj = $this->db->fetch_object($resql);
467  if ($obj)
468  {
469  $alreadydispatched = $obj->nb;
470  }
471  } else {
472  $this->error = $this->db->lasterror();
473  return -1;
474  }
475 
476  if ($alreadydispatched)
477  {
478  return 1;
479  }
480  return 0;
481  }
482 
483 
489  public function getLibType()
490  {
491  global $langs;
492  if ($this->type == CommonInvoice::TYPE_STANDARD) return $langs->trans("InvoiceStandard");
493  elseif ($this->type == CommonInvoice::TYPE_REPLACEMENT) return $langs->trans("InvoiceReplacement");
494  elseif ($this->type == CommonInvoice::TYPE_CREDIT_NOTE) return $langs->trans("InvoiceAvoir");
495  elseif ($this->type == CommonInvoice::TYPE_DEPOSIT) return $langs->trans("InvoiceDeposit");
496  elseif ($this->type == CommonInvoice::TYPE_PROFORMA) return $langs->trans("InvoiceProForma"); // Not used.
497  elseif ($this->type == CommonInvoice::TYPE_SITUATION) return $langs->trans("InvoiceSituation");
498  return $langs->trans("Unknown");
499  }
500 
508  public function getLibStatut($mode = 0, $alreadypaid = -1)
509  {
510  return $this->LibStatut($this->paye, $this->statut, $mode, $alreadypaid, $this->type);
511  }
512 
513  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
524  public function LibStatut($paye, $status, $mode = 0, $alreadypaid = -1, $type = -1)
525  {
526  // phpcs:enable
527  global $langs;
528  $langs->load('bills');
529 
530  if ($type == -1) $type = $this->type;
531 
532  $statusType = 'status0';
533  $prefix = 'Short';
534  if (!$paye) {
535  if ($status == 0) {
536  $labelStatus = $langs->transnoentitiesnoconv('BillStatusDraft');
537  $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusDraft');
538  } elseif (($status == 3 || $status == 2) && $alreadypaid <= 0) {
539  $labelStatus = $langs->transnoentitiesnoconv('BillStatusClosedUnpaid');
540  $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusClosedUnpaid');
541  $statusType = 'status5';
542  } elseif (($status == 3 || $status == 2) && $alreadypaid > 0) {
543  $labelStatus = $langs->transnoentitiesnoconv('BillStatusClosedPaidPartially');
544  $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusClosedPaidPartially');
545  $statusType = 'status9';
546  } elseif ($alreadypaid == 0) {
547  $labelStatus = $langs->transnoentitiesnoconv('BillStatusNotPaid');
548  $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusNotPaid');
549  $statusType = 'status1';
550  } else {
551  $labelStatus = $langs->transnoentitiesnoconv('BillStatusStarted');
552  $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusStarted');
553  $statusType = 'status3';
554  }
555  } else {
556  $statusType = 'status6';
557 
558  if ($type == self::TYPE_CREDIT_NOTE) {
559  $labelStatus = $langs->transnoentitiesnoconv('BillStatusPaidBackOrConverted'); // credit note
560  $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusPaidBackOrConverted'); // credit note
561  } elseif ($type == self::TYPE_DEPOSIT) {
562  $labelStatus = $langs->transnoentitiesnoconv('BillStatusConverted'); // deposit invoice
563  $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusConverted'); // deposit invoice
564  } else {
565  $labelStatus = $langs->transnoentitiesnoconv('BillStatusPaid');
566  $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusPaid');
567  }
568  }
569 
570  return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
571  }
572 
573  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
581  public function calculate_date_lim_reglement($cond_reglement = 0)
582  {
583  // phpcs:enable
584  if (!$cond_reglement) $cond_reglement = $this->cond_reglement_code;
585  if (!$cond_reglement) $cond_reglement = $this->cond_reglement_id;
586 
587  $cdr_nbjour = 0;
588  $cdr_type = 0;
589  $cdr_decalage = 0;
590 
591  $sqltemp = 'SELECT c.type_cdr, c.nbjour, c.decalage';
592  $sqltemp .= ' FROM '.MAIN_DB_PREFIX.'c_payment_term as c';
593  if (is_numeric($cond_reglement)) $sqltemp .= " WHERE c.rowid=".$cond_reglement;
594  else {
595  $sqltemp .= " WHERE c.entity IN (".getEntity('c_payment_term').")";
596  $sqltemp .= " AND c.code='".$this->db->escape($cond_reglement)."'";
597  }
598 
599  dol_syslog(get_class($this).'::calculate_date_lim_reglement', LOG_DEBUG);
600  $resqltemp = $this->db->query($sqltemp);
601  if ($resqltemp)
602  {
603  if ($this->db->num_rows($resqltemp))
604  {
605  $obj = $this->db->fetch_object($resqltemp);
606  $cdr_nbjour = $obj->nbjour;
607  $cdr_type = $obj->type_cdr;
608  $cdr_decalage = $obj->decalage;
609  }
610  } else {
611  $this->error = $this->db->error();
612  return -1;
613  }
614  $this->db->free($resqltemp);
615 
616  /* Definition de la date limite */
617 
618  // 0 : adding the number of days
619  if ($cdr_type == 0)
620  {
621  $datelim = $this->date + ($cdr_nbjour * 3600 * 24);
622 
623  $datelim += ($cdr_decalage * 3600 * 24);
624  }
625  // 1 : application of the "end of the month" rule
626  elseif ($cdr_type == 1)
627  {
628  $datelim = $this->date + ($cdr_nbjour * 3600 * 24);
629 
630  $mois = date('m', $datelim);
631  $annee = date('Y', $datelim);
632  if ($mois == 12)
633  {
634  $mois = 1;
635  $annee += 1;
636  } else {
637  $mois += 1;
638  }
639  // We move at the beginning of the next month, and we take a day off
640  $datelim = dol_mktime(12, 0, 0, $mois, 1, $annee);
641  $datelim -= (3600 * 24);
642 
643  $datelim += ($cdr_decalage * 3600 * 24);
644  }
645  // 2 : application of the rule, the N of the current or next month
646  elseif ($cdr_type == 2 && !empty($cdr_decalage))
647  {
648  include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
649  $datelim = $this->date + ($cdr_nbjour * 3600 * 24);
650 
651  $date_piece = dol_mktime(0, 0, 0, date('m', $datelim), date('d', $datelim), date('Y', $datelim)); // Sans les heures minutes et secondes
652  $date_lim_current = dol_mktime(0, 0, 0, date('m', $datelim), $cdr_decalage, date('Y', $datelim)); // Sans les heures minutes et secondes
653  $date_lim_next = dol_time_plus_duree($date_lim_current, 1, 'm'); // Add 1 month
654 
655  $diff = $date_piece - $date_lim_current;
656 
657  if ($diff < 0) $datelim = $date_lim_current;
658  else $datelim = $date_lim_next;
659  } else {
660  return 'Bad value for type_cdr in database for record cond_reglement = '.$cond_reglement;
661  }
662 
663  return $datelim;
664  }
665 
666  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
677  public function demande_prelevement($fuser, $amount = 0, $type = 'direct-debit', $sourcetype = 'facture')
678  {
679  // phpcs:enable
680  global $conf;
681 
682  $error = 0;
683 
684  dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
685 
686  if ($this->statut > self::STATUS_DRAFT && $this->paye == 0)
687  {
688  require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
689  $bac = new CompanyBankAccount($this->db);
690  $bac->fetch(0, $this->socid);
691 
692  $sql = 'SELECT count(*)';
693  $sql .= ' FROM '.MAIN_DB_PREFIX.'prelevement_facture_demande';
694  if ($type == 'bank-transfer') {
695  $sql .= ' WHERE fk_facture_fourn = '.$this->id;
696  } else {
697  $sql .= ' WHERE fk_facture = '.$this->id;
698  }
699  $sql .= ' AND ext_payment_id IS NULL'; // To exclude record done for some online payments
700  $sql .= ' AND traite = 0';
701 
702  dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
703  $resql = $this->db->query($sql);
704  if ($resql)
705  {
706  $row = $this->db->fetch_row($resql);
707  if ($row[0] == 0)
708  {
709  $now = dol_now();
710 
711  $totalpaye = $this->getSommePaiement();
712  $totalcreditnotes = $this->getSumCreditNotesUsed();
713  $totaldeposits = $this->getSumDepositsUsed();
714  //print "totalpaye=".$totalpaye." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits;
715 
716  // We can also use bcadd to avoid pb with floating points
717  // For example print 239.2 - 229.3 - 9.9; does not return 0.
718  //$resteapayer=bcadd($this->total_ttc,$totalpaye,$conf->global->MAIN_MAX_DECIMALS_TOT);
719  //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
720  if (empty($amount)) $amount = price2num($this->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits, 'MT');
721 
722  if (is_numeric($amount) && $amount != 0)
723  {
724  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'prelevement_facture_demande(';
725  if ($type == 'bank-transfer') {
726  $sql .= 'fk_facture_fourn, ';
727  } else {
728  $sql .= 'fk_facture, ';
729  }
730  $sql .= ' amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib, sourcetype, entity)';
731  $sql .= ' VALUES ('.$this->id;
732  $sql .= ",'".price2num($amount)."'";
733  $sql .= ",'".$this->db->idate($now)."'";
734  $sql .= ",".$fuser->id;
735  $sql .= ",'".$this->db->escape($bac->code_banque)."'";
736  $sql .= ",'".$this->db->escape($bac->code_guichet)."'";
737  $sql .= ",'".$this->db->escape($bac->number)."'";
738  $sql .= ",'".$this->db->escape($bac->cle_rib)."'";
739  $sql .= ",'".$this->db->escape($sourcetype)."'";
740  $sql .= ",".$conf->entity;
741  $sql .= ")";
742 
743  dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
744  $resql = $this->db->query($sql);
745  if (!$resql) {
746  $this->error = $this->db->lasterror();
747  dol_syslog(get_class($this).'::demandeprelevement Erreur');
748  $error++;
749  }
750  } else {
751  $this->error = 'WithdrawRequestErrorNilAmount';
752  dol_syslog(get_class($this).'::demandeprelevement WithdrawRequestErrorNilAmount');
753  $error++;
754  }
755 
756  if (!$error) {
757  // Force payment mode of invoice to withdraw
758  $payment_mode_id = dol_getIdFromCode($this->db, ($type == 'bank-transfer' ? 'VIR' : 'PRE'), 'c_paiement', 'code', 'id', 1);
759  if ($payment_mode_id > 0) {
760  $result = $this->setPaymentMethods($payment_mode_id);
761  }
762  }
763 
764  if ($error) return -1;
765  return 1;
766  } else {
767  $this->error = "A request already exists";
768  dol_syslog(get_class($this).'::demandeprelevement Impossible de creer une demande, demande deja en cours');
769  return 0;
770  }
771  } else {
772  $this->error = $this->db->error();
773  dol_syslog(get_class($this).'::demandeprelevement Erreur -2');
774  return -2;
775  }
776  } else {
777  $this->error = "Status of invoice does not allow this";
778  dol_syslog(get_class($this)."::demandeprelevement ".$this->error." $this->statut, $this->paye, $this->mode_reglement_id");
779  return -3;
780  }
781  }
782 
783  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
791  public function demande_prelevement_delete($fuser, $did)
792  {
793  // phpcs:enable
794  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'prelevement_facture_demande';
795  $sql .= ' WHERE rowid = '.$did;
796  $sql .= ' AND traite = 0';
797  if ($this->db->query($sql))
798  {
799  return 0;
800  } else {
801  $this->error = $this->db->lasterror();
802  dol_syslog(get_class($this).'::demande_prelevement_delete Error '.$this->error);
803  return -1;
804  }
805  }
806 }
807 
808 
809 
810 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
811 
815 abstract class CommonInvoiceLine extends CommonObjectLine
816 {
821  public $label;
822 
827  public $ref; // Product ref (deprecated)
832  public $libelle; // Product label (deprecated)
833 
838  public $product_type = 0;
839 
844  public $product_ref;
845 
850  public $product_label;
851 
856  public $product_desc;
857 
862  public $qty;
863 
868  public $subprice;
869 
874  public $fk_product;
875 
880  public $vat_src_code;
881 
886  public $tva_tx;
887 
892  public $localtax1_tx;
893 
898  public $localtax2_tx;
899 
904  public $remise_percent;
905 
910  public $total_ht;
911 
916  public $total_tva;
917 
922  public $total_localtax1;
923 
928  public $total_localtax2;
929 
934  public $total_ttc;
935 
936  public $date_start_fill; // If set to 1, when invoice is created from a template invoice, it will also auto set the field date_start at creation
937  public $date_end_fill; // If set to 1, when invoice is created from a template invoice, it will also auto set the field date_end at creation
938 
945  public $info_bits = 0;
946 
947  public $special_code = 0;
948 
949  public $fk_multicurrency;
950  public $multicurrency_code;
951  public $multicurrency_subprice;
952  public $multicurrency_total_ht;
953  public $multicurrency_total_tva;
954  public $multicurrency_total_ttc;
955 }
calculate_date_lim_reglement($cond_reglement=0)
Returns an invoice payment deadline based on the invoice settlement conditions and billing date...
const STATUS_CLOSED
Classified paid.
const TYPE_STANDARD
Standard invoice.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
if(!empty($arrayfields['u.datec']['checked'])) print_liste_field_titre("DateCreationShort"u if(!empty($arrayfields['u.tms']['checked'])) print_liste_field_titre("DateModificationShort"u if(!empty($arrayfields['u.statut']['checked'])) print_liste_field_titre("Status"u statut
Definition: list.php:632
getSumFromThisCreditNotesNotUsed($multicurrency=0)
Return amount (with tax) of all converted amount for this credit note.
is_erasable()
Return if an invoice can be deleted Rule is: If invoice is draft and has a temporary ref -&gt; yes (1) I...
getListOfPayments($filtertype= '')
Return list of payments.
dol_now($mode= 'auto')
Return date for now.
demande_prelevement_delete($fuser, $did)
Remove a direct debit request or a credit transfer request.
Class to manage bank accounts description of third parties.
getLibStatut($mode=0, $alreadypaid=-1)
Return label of object status.
$label
Custom label of line.
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this-&gt;socid or $this-&gt;fk_soc, into this-&gt;thirdparty.
Parent class of all other business classes for details of elements (invoices, contracts, proposals, orders, ...)
$conf db
API class for accounts.
Definition: inc.php:54
getListIdAvoirFromInvoice()
Returns array of credit note ids from the invoice.
const TYPE_PROFORMA
Proforma invoice.
demande_prelevement($fuser, $amount=0, $type= 'direct-debit', $sourcetype= 'facture')
Create a withdrawal request for a direct debit order or a credit transfer order.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
getLibType()
Return label of type of invoice.
const TYPE_REPLACEMENT
Replacement invoice.
getRemainToPay($multicurrency=0)
Return remain amount to pay.
getSommePaiement($multicurrency=0)
Return amount of payments already done.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
getSumCreditNotesUsed($multicurrency=0)
Return amount (with tax) of all credit notes invoices + excess received used by invoice.
const STATUS_DRAFT
Draft status.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Superclass for invoices classes.
setPaymentMethods($id)
Change the payments methods.
dol_getIdFromCode($db, $key, $tablename, $fieldkey= 'code', $fieldid= 'id', $entityfilter=0)
Return an id or code from a code or id.
trait CommonIncoterm
Superclass for incoterm classes.
const STATUS_ABANDONED
Classified abandoned and no payment done.
getSumDepositsUsed($multicurrency=0)
Return amount (with tax) of all deposits invoices used by invoice.
const TYPE_SITUATION
Situation invoice.
getIdReplacingInvoice($option= '')
Returns the id of the invoice that replaces it.
getVentilExportCompta()
Return if an invoice was dispatched into bookkeeping.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Class to manage absolute discounts.
const STATUS_VALIDATED
Validated (need to be paid)
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
dol_time_plus_duree($time, $duration_value, $duration_unit)
Add a delay to a date.
Definition: date.lib.php:114
const TYPE_DEPOSIT
Deposit invoice.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
LibStatut($paye, $status, $mode=0, $alreadypaid=-1, $type=-1)
Return label of a status.
const TYPE_CREDIT_NOTE
Credit note invoice.