dolibarr  18.0.0
discount.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2018 Laurent Destailleur <eldy@users.sourceforge.net>
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 
30 {
34  public $db;
35 
39  public $error;
40 
44  public $errors = array();
45 
49  public $id;
50 
54  public $fk_soc;
55 
56  public $discount_type; // 0 => customer discount, 1 => supplier discount
57 
58  public $total_ht;
59  public $total_tva;
60  public $total_ttc;
61  public $amount_ht; // deprecated
62  public $amount_tva; // deprecated
63  public $amount_ttc; // deprecated
64 
65  public $multicurrency_total_ht;
66  public $multicurrency_total_tva;
67  public $multicurrency_total_ttc;
68  public $multicurrency_amount_ht; // deprecated
69  public $multicurrency_amount_tva; // deprecated
70  public $multicurrency_amount_ttc; // deprecated
71 
72  // Vat rate
73  public $tva_tx;
74  public $vat_src_code;
75 
79  public $fk_user;
80 
84  public $description;
85 
91  public $datec;
92 
96  public $fk_facture_line;
97 
101  public $fk_facture;
102 
106  public $fk_facture_source;
107  public $ref_facture_source; // Ref credit note or deposit used to create the discount
108  public $type_facture_source;
109 
110  public $fk_invoice_supplier_source;
111  public $ref_invoice_supplier_source; // Ref credit note or deposit used to create the discount
112  public $type_invoice_supplier_source;
113 
119  public function __construct($db)
120  {
121  $this->db = $db;
122  }
123 
124 
133  public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
134  {
135  global $conf;
136 
137  // Check parameters
138  if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
139  $this->error = 'ErrorBadParameters';
140  return -1;
141  }
142 
143  $sql = "SELECT sr.rowid, sr.fk_soc, sr.discount_type,";
144  $sql .= " sr.fk_user,";
145  $sql .= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx, sr.vat_src_code,";
146  $sql .= " sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
147  $sql .= " sr.fk_facture_line, sr.fk_facture, sr.fk_facture_source, sr.fk_invoice_supplier_line, sr.fk_invoice_supplier, sr.fk_invoice_supplier_source, sr.description,";
148  $sql .= " sr.datec,";
149  $sql .= " f.ref as ref_facture_source, f.type as type_facture_source,";
150  $sql .= " fsup.ref as ref_invoice_supplier_source, fsup.type as type_invoice_supplier_source";
151  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as sr";
152  $sql .= " LEFT JOIN ".$this->db->prefix()."facture as f ON sr.fk_facture_source = f.rowid";
153  $sql .= " LEFT JOIN ".$this->db->prefix()."facture_fourn as fsup ON sr.fk_invoice_supplier_source = fsup.rowid";
154  $sql .= " WHERE sr.entity IN (".getEntity('invoice').")";
155  if ($rowid) {
156  $sql .= " AND sr.rowid = ".((int) $rowid);
157  }
158  if ($fk_facture_source) {
159  $sql .= " AND sr.fk_facture_source = ".((int) $fk_facture_source);
160  }
161  if ($fk_invoice_supplier_source) {
162  $sql .= " AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
163  }
164 
165  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
166  $resql = $this->db->query($sql);
167  if ($resql) {
168  if ($this->db->num_rows($resql)) {
169  $obj = $this->db->fetch_object($resql);
170 
171  $this->id = $obj->rowid;
172  $this->fk_soc = $obj->fk_soc;
173  $this->discount_type = $obj->discount_type;
174 
175  $this->total_ht = $obj->amount_ht;
176  $this->total_tva = $obj->amount_tva;
177  $this->total_ttc = $obj->amount_ttc;
178  // For backward compatibility
179  $this->amount_ht = $this->total_ht;
180  $this->amount_tva = $this->total_tva;
181  $this->amount_ttc = $this->total_ttc;
182 
183  $this->multicurrency_total_ht = $this->multicurrency_subprice = $obj->multicurrency_amount_ht;
184  $this->multicurrency_total_tva = $obj->multicurrency_amount_tva;
185  $this->multicurrency_total_ttc = $obj->multicurrency_amount_ttc;
186  // For backward compatibility
187  $this->multicurrency_amount_ht = $this->multicurrency_total_ht;
188  $this->multicurrency_amount_tva = $this->multicurrency_total_tva;
189  $this->multicurrency_amount_ttc = $this->multicurrency_total_ttc;
190 
191  $this->tva_tx = $obj->tva_tx;
192  $this->vat_src_code = $obj->vat_src_code;
193 
194  $this->fk_user = $obj->fk_user;
195  $this->fk_facture_line = $obj->fk_facture_line;
196  $this->fk_facture = $obj->fk_facture;
197  $this->fk_facture_source = $obj->fk_facture_source; // Id credit note or deposit source
198  $this->ref_facture_source = $obj->ref_facture_source; // Ref credit note or deposit source
199  $this->type_facture_source = $obj->type_facture_source; // Type credit note or deposit source
200  $this->fk_invoice_supplier_line = $obj->fk_invoice_supplier_line;
201  $this->fk_invoice_supplier = $obj->fk_invoice_supplier;
202  $this->fk_invoice_supplier_source = $obj->fk_invoice_supplier_source; // Id credit note or deposit source
203  $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source; // Ref credit note or deposit source
204  $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source; // Type credit note or deposit source
205  $this->description = $obj->description;
206  $this->datec = $this->db->jdate($obj->datec);
207 
208  $this->db->free($resql);
209  return 1;
210  } else {
211  $this->db->free($resql);
212  return 0;
213  }
214  } else {
215  $this->error = $this->db->error();
216  return -1;
217  }
218  }
219 
220 
227  public function create($user)
228  {
229  global $conf, $langs;
230 
231  // Clean parameters
232  $this->amount_ht = price2num($this->amount_ht);
233  $this->amount_tva = price2num($this->amount_tva);
234  $this->amount_ttc = price2num($this->amount_ttc);
235 
236  $this->tva_tx = price2num($this->tva_tx);
237 
238  $this->multicurrency_amount_ht = price2num($this->multicurrency_amount_ht);
239  $this->multicurrency_amount_tva = price2num($this->multicurrency_amount_tva);
240  $this->multicurrency_amount_ttc = price2num($this->multicurrency_amount_ttc);
241 
242  if (empty($this->multicurrency_amount_ht)) {
243  $this->multicurrency_amount_ht = 0;
244  }
245  if (empty($this->multicurrency_amount_tva)) {
246  $this->multicurrency_amount_tva = 0;
247  }
248  if (empty($this->multicurrency_amount_ttc)) {
249  $this->multicurrency_amount_ttc = 0;
250  }
251  if (empty($this->tva_tx)) {
252  $this->tva_tx = 0;
253  }
254 
255  // Check parameters
256  if (empty($this->description)) {
257  $this->error = 'BadValueForPropertyDescription';
258  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
259  return -1;
260  }
261 
262  $userid = $user->id;
263  if (!($userid > 0)) { // For example when record is saved into an anonymous context with a not loaded object $user.
264  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
265  $tmpinvoice = new Facture($this->db);
266  $tmpinvoice->fetch($this->fk_facture_source);
267  $userid = $tmpinvoice->fk_user_author; // We use the author of invoice
268  }
269 
270  // Insert request
271  $sql = "INSERT INTO ".$this->db->prefix()."societe_remise_except";
272  $sql .= " (entity, datec, fk_soc, discount_type, fk_user, description,";
273  $sql .= " amount_ht, amount_tva, amount_ttc, tva_tx, vat_src_code,";
274  $sql .= " multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,";
275  $sql .= " fk_facture_source, fk_invoice_supplier_source";
276  $sql .= ")";
277  $sql .= " VALUES (".$conf->entity.", '".$this->db->idate($this->datec != '' ? $this->datec : dol_now())."', ".((int) $this->fk_soc).", ".(empty($this->discount_type) ? 0 : intval($this->discount_type)).", ".((int) $userid).", '".$this->db->escape($this->description)."',";
278  $sql .= " ".price2num($this->amount_ht).", ".price2num($this->amount_tva).", ".price2num($this->amount_ttc).", ".price2num($this->tva_tx).", '".$this->db->escape($this->vat_src_code)."',";
279  $sql .= " ".price2num($this->multicurrency_amount_ht).", ".price2num($this->multicurrency_amount_tva).", ".price2num($this->multicurrency_amount_ttc).", ";
280  $sql .= " ".($this->fk_facture_source ? ((int) $this->fk_facture_source) : "null").",";
281  $sql .= " ".($this->fk_invoice_supplier_source ? ((int) $this->fk_invoice_supplier_source) : "null");
282  $sql .= ")";
283 
284  dol_syslog(get_class($this)."::create", LOG_DEBUG);
285  $resql = $this->db->query($sql);
286  if ($resql) {
287  $this->id = $this->db->last_insert_id($this->db->prefix()."societe_remise_except");
288  return $this->id;
289  } else {
290  $this->error = $this->db->lasterror().' - sql='.$sql;
291  return -1;
292  }
293  }
294 
295 
302  public function delete($user)
303  {
304  global $conf, $langs;
305 
306  // Check if we can remove the discount
307  if ($this->fk_facture_source) {
308  $sql = "SELECT COUNT(rowid) as nb";
309  $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
310  $sql .= " WHERE (fk_facture_line IS NOT NULL"; // Not used as absolute simple discount
311  $sql .= " OR fk_facture IS NOT NULL)"; // Not used as credit note and not used as deposit
312  $sql .= " AND fk_facture_source = ".((int) $this->fk_facture_source);
313  //$sql.=" AND rowid != ".$this->id;
314 
315  dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
316  $resql = $this->db->query($sql);
317  if ($resql) {
318  $obj = $this->db->fetch_object($resql);
319  if ($obj->nb > 0) {
320  $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
321  return -2;
322  }
323  } else {
324  dol_print_error($this->db);
325  return -1;
326  }
327  }
328 
329  // Check if we can remove the discount
330  if ($this->fk_invoice_supplier_source) {
331  $sql = "SELECT COUNT(rowid) as nb";
332  $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
333  $sql .= " WHERE (fk_invoice_supplier_line IS NOT NULL"; // Not used as absolute simple discount
334  $sql .= " OR fk_invoice_supplier IS NOT NULL)"; // Not used as credit note and not used as deposit
335  $sql .= " AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
336  //$sql.=" AND rowid != ".$this->id;
337 
338  dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
339  $resql = $this->db->query($sql);
340  if ($resql) {
341  $obj = $this->db->fetch_object($resql);
342  if ($obj->nb > 0) {
343  $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
344  return -2;
345  }
346  } else {
347  dol_print_error($this->db);
348  return -1;
349  }
350  }
351 
352  $this->db->begin();
353 
354  // Delete but only if not used
355  $sql = "DELETE FROM ".$this->db->prefix()."societe_remise_except ";
356  if ($this->fk_facture_source) {
357  $sql .= " WHERE fk_facture_source = ".((int) $this->fk_facture_source); // Delete all lines of same serie
358  } elseif ($this->fk_invoice_supplier_source) {
359  $sql .= " WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source); // Delete all lines of same serie
360  } else {
361  $sql .= " WHERE rowid = ".((int) $this->id); // Delete only line
362  }
363  $sql .= " AND (fk_facture_line IS NULL"; // Not used as absolute simple discount
364  $sql .= " AND fk_facture IS NULL)"; // Not used as credit note and not used as deposit
365  $sql .= " AND (fk_invoice_supplier_line IS NULL"; // Not used as absolute simple discount
366  $sql .= " AND fk_invoice_supplier IS NULL)"; // Not used as credit note and not used as deposit
367 
368  dol_syslog(get_class($this)."::delete Delete discount", LOG_DEBUG);
369  require_once DOL_DOCUMENT_ROOT. '/core/class/commoninvoice.class.php';
370  $result = $this->db->query($sql);
371  if ($result) {
372  // If source of discount was a credit note or deposit, we change source statut.
373  if ($this->fk_facture_source) {
374  $sql = "UPDATE ".$this->db->prefix()."facture";
375  $sql .= " set paye=0, fk_statut=1";
376  $sql .= " WHERE (type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_facture_source);
377 
378  dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
379  $result = $this->db->query($sql);
380  if ($result) {
381  $this->db->commit();
382  return 1;
383  } else {
384  $this->error = $this->db->lasterror();
385  $this->db->rollback();
386  return -1;
387  }
388  } elseif ($this->fk_invoice_supplier_source) {
389  $sql = "UPDATE ".$this->db->prefix()."facture_fourn";
390  $sql .= " set paye=0, fk_statut=1";
391  $sql .= " WHERE (type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_invoice_supplier_source);
392 
393  dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
394  $result = $this->db->query($sql);
395  if ($result) {
396  $this->db->commit();
397  return 1;
398  } else {
399  $this->error = $this->db->lasterror();
400  $this->db->rollback();
401  return -1;
402  }
403  } else {
404  $this->db->commit();
405  return 1;
406  }
407  } else {
408  $this->error = $this->db->lasterror();
409  $this->db->rollback();
410  return -1;
411  }
412  }
413 
414 
415 
416  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
426  public function link_to_invoice($rowidline, $rowidinvoice)
427  {
428  // phpcs:enable
429  // Check parameters
430  if (!$rowidline && !$rowidinvoice) {
431  $this->error = 'ErrorBadParameters';
432  return -1;
433  }
434  if ($rowidline && $rowidinvoice) {
435  $this->error = 'ErrorBadParameters';
436  return -2;
437  }
438 
439  $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
440  if (!empty($this->discount_type)) {
441  if ($rowidline) {
442  $sql .= " SET fk_invoice_supplier_line = ".((int) $rowidline);
443  }
444  if ($rowidinvoice) {
445  $sql .= " SET fk_invoice_supplier = ".((int) $rowidinvoice);
446  }
447  } else {
448  if ($rowidline) {
449  $sql .= " SET fk_facture_line = ".((int) $rowidline);
450  }
451  if ($rowidinvoice) {
452  $sql .= " SET fk_facture = ".((int) $rowidinvoice);
453  }
454  }
455  $sql .= " WHERE rowid = ".((int) $this->id);
456 
457  dol_syslog(get_class($this)."::link_to_invoice", LOG_DEBUG);
458  $resql = $this->db->query($sql);
459  if ($resql) {
460  if (!empty($this->discount_type)) {
461  $this->fk_invoice_supplier_line = $rowidline;
462  $this->fk_invoice_supplier = $rowidinvoice;
463  } else {
464  $this->fk_facture_line = $rowidline;
465  $this->fk_facture = $rowidinvoice;
466  }
467  return 1;
468  } else {
469  $this->error = $this->db->error();
470  return -3;
471  }
472  }
473 
474 
475  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
482  public function unlink_invoice()
483  {
484  // phpcs:enable
485  $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
486  if (!empty($this->discount_type)) {
487  $sql .= " SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
488  } else {
489  $sql .= " SET fk_facture_line = NULL, fk_facture = NULL";
490  }
491  $sql .= " WHERE rowid = ".((int) $this->id);
492 
493  dol_syslog(get_class($this)."::unlink_invoice", LOG_DEBUG);
494  $resql = $this->db->query($sql);
495  if ($resql) {
496  return 1;
497  } else {
498  $this->error = $this->db->error();
499  return -3;
500  }
501  }
502 
503 
515  public function getAvailableDiscounts($company = '', $user = '', $filter = '', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
516  {
517  global $conf;
518 
519  dol_syslog(get_class($this)."::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
520 
521  $sql = "SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
522  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
523  $sql .= " WHERE rc.entity = ".$conf->entity;
524  $sql .= " AND rc.discount_type=".((int) $discount_type);
525  if (!empty($discount_type)) {
526  $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)"; // Available from supplier
527  } else {
528  $sql .= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available to customer
529  }
530  if (is_object($company)) {
531  $sql .= " AND rc.fk_soc = ".((int) $company->id);
532  }
533  if (is_object($user)) {
534  $sql .= " AND rc.fk_user = ".((int) $user->id);
535  }
536  if ($filter) {
537  $sql .= " AND (".$filter.")";
538  }
539  if ($maxvalue) {
540  $sql .= ' AND rc.amount_ttc <= '.((float) price2num($maxvalue));
541  }
542 
543  $resql = $this->db->query($sql);
544  if ($resql) {
545  $obj = $this->db->fetch_object($resql);
546  //while ($obj)
547  //{
548  //print 'zz'.$obj->amount;
549  //$obj = $this->db->fetch_object($resql);
550  //}
551  if ($multicurrency) {
552  return $obj->multicurrency_amount;
553  }
554 
555  return $obj->amount;
556  }
557  return -1;
558  }
559 
560 
569  public function getSumDepositsUsed($invoice, $multicurrency = 0)
570  {
571  dol_syslog(get_class($this)."::getSumDepositsUsed", LOG_DEBUG);
572 
573  if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
574  $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
575  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
576  $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
577  $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
578  } elseif ($invoice->element == 'invoice_supplier') {
579  $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
580  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
581  $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
582  $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
583  } else {
584  $this->error = get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
585  dol_print_error($this->error);
586  return -1;
587  }
588 
589  $resql = $this->db->query($sql);
590  if ($resql) {
591  $obj = $this->db->fetch_object($resql);
592  if ($multicurrency == 1) {
593  return $obj->multicurrency_amount;
594  } else {
595  return $obj->amount;
596  }
597  } else {
598  $this->error = $this->db->lasterror();
599  return -1;
600  }
601  }
602 
610  public function getSumCreditNotesUsed($invoice, $multicurrency = 0)
611  {
612  dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
613 
614  if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
615  $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
616  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
617  $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
618  $sql .= " AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.", ".$invoice::TYPE_CREDIT_NOTE.", ".$invoice::TYPE_SITUATION).")"; // Find discount coming from credit note or excess received
619  } elseif ($invoice->element == 'invoice_supplier') {
620  $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
621  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
622  $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
623  $sql .= " AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.", ".$invoice::TYPE_CREDIT_NOTE).")"; // Find discount coming from credit note or excess paid
624  } else {
625  $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
626  dol_print_error($this->error);
627  return -1;
628  }
629 
630  $resql = $this->db->query($sql);
631  if ($resql) {
632  $obj = $this->db->fetch_object($resql);
633  if ($multicurrency == 1) {
634  return $obj->multicurrency_amount;
635  } else {
636  return $obj->amount;
637  }
638  } else {
639  $this->error = $this->db->lasterror();
640  return -1;
641  }
642  }
650  public function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency = 0)
651  {
652  dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
653 
654  if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
655  $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
656  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
657  $sql .= " WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
658  } elseif ($invoice->element == 'invoice_supplier') {
659  $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
660  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
661  $sql .= " WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
662  } else {
663  $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
664  dol_print_error($this->error);
665  return -1;
666  }
667 
668  $resql = $this->db->query($sql);
669  if ($resql) {
670  $obj = $this->db->fetch_object($resql);
671  if ($multicurrency) {
672  return $obj->multicurrency_amount;
673  } else {
674  return $obj->amount;
675  }
676  } else {
677  $this->error = $this->db->lasterror();
678  return -1;
679  }
680  }
681 
689  public function getNomUrl($withpicto, $option = 'invoice')
690  {
691  global $langs;
692 
693  $result = '';
694  $link = '';
695  $linkend = '';
696  $label = '';
697  $picto = '';
698  $ref = '';
699 
700  if ($option == 'invoice') {
701  $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
702  $link = !empty($this->discount_type) ? '/fourn/facture/card.php' : '/compta/facture/card.php';
703  $label = $langs->trans("ShowSourceInvoice").': '.$this->ref_facture_source;
704  $link = '<a href="'.DOL_URL_ROOT.$link.'?facid='.$facid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
705  $linkend = '</a>';
706  $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
707  $picto = 'bill';
708  }
709  if ($option == 'discount') {
710  $label = $langs->trans("Discount");
711  $link = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->fk_soc.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
712  $linkend = '</a>';
713  $ref = $langs->trans("Discount");
714  $picto = 'generic';
715  }
716 
717 
718  if ($withpicto) {
719  $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
720  }
721  if ($withpicto && $withpicto != 2) {
722  $result .= ' ';
723  }
724  $result .= $link.$ref.$linkend;
725  return $result;
726  }
727 
728 
736  public function initAsSpecimen()
737  {
738  global $user, $langs, $conf;
739 
740  $this->fk_soc = 1;
741  $this->amount_ht = 10;
742  $this->amount_tva = 1.96;
743  $this->amount_ttc = 11.96;
744  $this->tva_tx = 19.6;
745  $this->description = 'Specimen discount';
746  }
747 }
const TYPE_CREDIT_NOTE
Credit note invoice.
const TYPE_DEPOSIT
Deposit invoice.
Class to manage absolute discounts.
link_to_invoice($rowidline, $rowidinvoice)
Link the discount to a particular invoice line or a particular invoice.
getAvailableDiscounts($company='', $user='', $filter='', $maxvalue=0, $discount_type=0, $multicurrency=0)
Return amount (with tax) of discounts currently available for a company, user or other criteria.
getSumFromThisCreditNotesNotUsed($invoice, $multicurrency=0)
initAsSpecimen()
Initialise an instance with random values.
unlink_invoice()
Link the discount to a particular invoice line or a particular invoice.
fetch($rowid, $fk_facture_source=0, $fk_invoice_supplier_source=0)
Load object from database into memory.
create($user)
Create a discount into database.
getSumDepositsUsed($invoice, $multicurrency=0)
Return amount (with tax) of all deposits invoices used by invoice as a payment.
getNomUrl($withpicto, $option='invoice')
Return clickable ref of object (with picto or not)
getSumCreditNotesUsed($invoice, $multicurrency=0)
Return amount (with tax) of all credit notes invoices + excess received used by invoice as a payment.
__construct($db)
Constructor.
Class to manage invoices.
if(isModEnabled('facture') && $user->hasRight('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') && $user->hasRight('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)) $sql
Social contributions to pay.
Definition: index.php:746
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
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...
dol_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...