dolibarr  17.0.4
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 = 'BadValueForPropertyDescriptionOfDiscount';
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  $result = $this->db->query($sql);
370  if ($result) {
371  // If source of discount was a credit note or deposit, we change source statut.
372  if ($this->fk_facture_source) {
373  $sql = "UPDATE ".$this->db->prefix()."facture";
374  $sql .= " set paye=0, fk_statut=1";
375  $sql .= " WHERE (type = 2 or type = 3) AND rowid = ".((int) $this->fk_facture_source);
376 
377  dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
378  $result = $this->db->query($sql);
379  if ($result) {
380  $this->db->commit();
381  return 1;
382  } else {
383  $this->error = $this->db->lasterror();
384  $this->db->rollback();
385  return -1;
386  }
387  } elseif ($this->fk_invoice_supplier_source) {
388  $sql = "UPDATE ".$this->db->prefix()."facture_fourn";
389  $sql .= " set paye=0, fk_statut=1";
390  $sql .= " WHERE (type = 2 or type = 3) AND rowid = ".((int) $this->fk_invoice_supplier_source);
391 
392  dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
393  $result = $this->db->query($sql);
394  if ($result) {
395  $this->db->commit();
396  return 1;
397  } else {
398  $this->error = $this->db->lasterror();
399  $this->db->rollback();
400  return -1;
401  }
402  } else {
403  $this->db->commit();
404  return 1;
405  }
406  } else {
407  $this->error = $this->db->lasterror();
408  $this->db->rollback();
409  return -1;
410  }
411  }
412 
413 
414 
415  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
425  public function link_to_invoice($rowidline, $rowidinvoice)
426  {
427  // phpcs:enable
428  // Check parameters
429  if (!$rowidline && !$rowidinvoice) {
430  $this->error = 'ErrorBadParameters';
431  return -1;
432  }
433  if ($rowidline && $rowidinvoice) {
434  $this->error = 'ErrorBadParameters';
435  return -2;
436  }
437 
438  $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
439  if (!empty($this->discount_type)) {
440  if ($rowidline) {
441  $sql .= " SET fk_invoice_supplier_line = ".((int) $rowidline);
442  }
443  if ($rowidinvoice) {
444  $sql .= " SET fk_invoice_supplier = ".((int) $rowidinvoice);
445  }
446  } else {
447  if ($rowidline) {
448  $sql .= " SET fk_facture_line = ".((int) $rowidline);
449  }
450  if ($rowidinvoice) {
451  $sql .= " SET fk_facture = ".((int) $rowidinvoice);
452  }
453  }
454  $sql .= " WHERE rowid = ".((int) $this->id);
455 
456  dol_syslog(get_class($this)."::link_to_invoice", LOG_DEBUG);
457  $resql = $this->db->query($sql);
458  if ($resql) {
459  if (!empty($this->discount_type)) {
460  $this->fk_invoice_supplier_line = $rowidline;
461  $this->fk_invoice_supplier = $rowidinvoice;
462  } else {
463  $this->fk_facture_line = $rowidline;
464  $this->fk_facture = $rowidinvoice;
465  }
466  return 1;
467  } else {
468  $this->error = $this->db->error();
469  return -3;
470  }
471  }
472 
473 
474  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
481  public function unlink_invoice()
482  {
483  // phpcs:enable
484  $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
485  if (!empty($this->discount_type)) {
486  $sql .= " SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
487  } else {
488  $sql .= " SET fk_facture_line = NULL, fk_facture = NULL";
489  }
490  $sql .= " WHERE rowid = ".((int) $this->id);
491 
492  dol_syslog(get_class($this)."::unlink_invoice", LOG_DEBUG);
493  $resql = $this->db->query($sql);
494  if ($resql) {
495  return 1;
496  } else {
497  $this->error = $this->db->error();
498  return -3;
499  }
500  }
501 
502 
514  public function getAvailableDiscounts($company = '', $user = '', $filter = '', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
515  {
516  global $conf;
517 
518  dol_syslog(get_class($this)."::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
519 
520  $sql = "SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
521  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
522  $sql .= " WHERE rc.entity = ".$conf->entity;
523  $sql .= " AND rc.discount_type=".((int) $discount_type);
524  if (!empty($discount_type)) {
525  $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)"; // Available from supplier
526  } else {
527  $sql .= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available to customer
528  }
529  if (is_object($company)) {
530  $sql .= " AND rc.fk_soc = ".((int) $company->id);
531  }
532  if (is_object($user)) {
533  $sql .= " AND rc.fk_user = ".((int) $user->id);
534  }
535  if ($filter) {
536  $sql .= " AND (".$filter.")";
537  }
538  if ($maxvalue) {
539  $sql .= ' AND rc.amount_ttc <= '.((float) price2num($maxvalue));
540  }
541 
542  $resql = $this->db->query($sql);
543  if ($resql) {
544  $obj = $this->db->fetch_object($resql);
545  //while ($obj)
546  //{
547  //print 'zz'.$obj->amount;
548  //$obj = $this->db->fetch_object($resql);
549  //}
550  if ($multicurrency) {
551  return $obj->multicurrency_amount;
552  }
553 
554  return $obj->amount;
555  }
556  return -1;
557  }
558 
559 
568  public function getSumDepositsUsed($invoice, $multicurrency = 0)
569  {
570  dol_syslog(get_class($this)."::getSumDepositsUsed", LOG_DEBUG);
571 
572  if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
573  $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
574  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
575  $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
576  $sql .= " AND f.type = 3";
577  } elseif ($invoice->element == 'invoice_supplier') {
578  $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
579  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
580  $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
581  $sql .= " AND f.type = 3";
582  } else {
583  $this->error = get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
584  dol_print_error($this->db, $this->error);
585  return -1;
586  }
587 
588  $resql = $this->db->query($sql);
589  if ($resql) {
590  $obj = $this->db->fetch_object($resql);
591  if ($multicurrency == 1) {
592  return $obj->multicurrency_amount;
593  } else {
594  return $obj->amount;
595  }
596  } else {
597  $this->error = $this->db->lasterror();
598  return -1;
599  }
600  }
601 
609  public function getSumCreditNotesUsed($invoice, $multicurrency = 0)
610  {
611  dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
612 
613  if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
614  $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
615  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
616  $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
617  $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
618  } elseif ($invoice->element == 'invoice_supplier') {
619  $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
620  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
621  $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
622  $sql .= " AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.", ".$invoice::TYPE_CREDIT_NOTE).")"; // Find discount coming from credit note or excess paid
623  } else {
624  $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
625  dol_print_error($this->db, $this->error);
626  return -1;
627  }
628 
629  $resql = $this->db->query($sql);
630  if ($resql) {
631  $obj = $this->db->fetch_object($resql);
632  if ($multicurrency == 1) {
633  return $obj->multicurrency_amount;
634  } else {
635  return $obj->amount;
636  }
637  } else {
638  $this->error = $this->db->lasterror();
639  return -1;
640  }
641  }
649  public function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency = 0)
650  {
651  dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
652 
653  if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
654  $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
655  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
656  $sql .= " WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
657  } elseif ($invoice->element == 'invoice_supplier') {
658  $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
659  $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
660  $sql .= " WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
661  } else {
662  $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
663  dol_print_error($this->db, $this->error);
664  return -1;
665  }
666 
667  $resql = $this->db->query($sql);
668  if ($resql) {
669  $obj = $this->db->fetch_object($resql);
670  if ($multicurrency) {
671  return $obj->multicurrency_amount;
672  } else {
673  return $obj->amount;
674  }
675  } else {
676  $this->error = $this->db->lasterror();
677  return -1;
678  }
679  }
680 
688  public function getNomUrl($withpicto, $option = 'invoice')
689  {
690  global $langs;
691 
692  $result = '';
693  $link = '';
694  $linkend = '';
695  $label = '';
696  $picto = '';
697  $ref = '';
698 
699  if ($option == 'invoice') {
700  $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
701  $link = !empty($this->discount_type) ? '/fourn/facture/card.php' : '/compta/facture/card.php';
702  $label = $langs->trans("ShowSourceInvoice").': '.$this->ref_facture_source;
703  $link = '<a href="'.DOL_URL_ROOT.$link.'?facid='.$facid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
704  $linkend = '</a>';
705  $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
706  $picto = 'bill';
707  }
708  if ($option == 'discount') {
709  $label = $langs->trans("Discount");
710  $link = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->fk_soc.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
711  $linkend = '</a>';
712  $ref = $langs->trans("Discount");
713  $picto = 'generic';
714  }
715 
716 
717  if ($withpicto) {
718  $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
719  }
720  if ($withpicto && $withpicto != 2) {
721  $result .= ' ';
722  }
723  $result .= $link.$ref.$linkend;
724  return $result;
725  }
726 
727 
735  public function initAsSpecimen()
736  {
737  global $user, $langs, $conf;
738 
739  $this->fk_soc = 1;
740  $this->amount_ht = 10;
741  $this->amount_tva = 1.96;
742  $this->amount_ttc = 11.96;
743  $this->tva_tx = 19.6;
744  $this->description = 'Specimen discount';
745  }
746 }
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') &&!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
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
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.
$conf db
API class for accounts.
Definition: inc.php:41