dolibarr 20.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 * Copyright (C) 2024 Alexandre Janniaux <alexandre.janniaux@gmail.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
29
34{
39 public $fk_soc;
43 public $socid;
44
48 public $discount_type; // 0 => customer discount, 1 => supplier discount
49
50 public $total_ht;
51 public $total_tva;
52 public $total_ttc;
53 public $amount_ht; // deprecated
54 public $amount_tva; // deprecated
55 public $amount_ttc; // deprecated
56
57 public $multicurrency_total_ht;
58 public $multicurrency_total_tva;
59 public $multicurrency_total_ttc;
60 public $multicurrency_amount_ht; // deprecated
61 public $multicurrency_amount_tva; // deprecated
62 public $multicurrency_amount_ttc; // deprecated
63
67 public $multicurrency_subprice;
68
72 public $fk_invoice_supplier;
73
77 public $fk_invoice_supplier_line;
78
79 // Vat rate
80 public $tva_tx;
81 public $vat_src_code;
82
86 public $fk_user;
87
91 public $description;
92
98 public $datec;
99
103 public $fk_facture_line;
104
108 public $fk_facture;
109
113 public $fk_facture_source;
114 public $ref_facture_source; // Ref credit note or deposit used to create the discount
115 public $type_facture_source;
116
117 public $fk_invoice_supplier_source;
118 public $ref_invoice_supplier_source; // Ref credit note or deposit used to create the discount
119 public $type_invoice_supplier_source;
120
126 public function __construct($db)
127 {
128 $this->db = $db;
129 }
130
131
140 public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
141 {
142 // Check parameters
143 if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
144 $this->error = 'ErrorBadParameters';
145 return -1;
146 }
147
148 $sql = "SELECT sr.rowid, sr.fk_soc, sr.discount_type,";
149 $sql .= " sr.fk_user,";
150 $sql .= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx, sr.vat_src_code,";
151 $sql .= " sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
152 $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,";
153 $sql .= " sr.datec,";
154 $sql .= " f.ref as ref_facture_source, f.type as type_facture_source,";
155 $sql .= " fsup.ref as ref_invoice_supplier_source, fsup.type as type_invoice_supplier_source";
156 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as sr";
157 $sql .= " LEFT JOIN ".$this->db->prefix()."facture as f ON sr.fk_facture_source = f.rowid";
158 $sql .= " LEFT JOIN ".$this->db->prefix()."facture_fourn as fsup ON sr.fk_invoice_supplier_source = fsup.rowid";
159 $sql .= " WHERE sr.entity IN (".getEntity('invoice').")";
160 if ($rowid) {
161 $sql .= " AND sr.rowid = ".((int) $rowid);
162 }
163 if ($fk_facture_source) {
164 $sql .= " AND sr.fk_facture_source = ".((int) $fk_facture_source);
165 }
166 if ($fk_invoice_supplier_source) {
167 $sql .= " AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
168 }
169
170 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
171 $resql = $this->db->query($sql);
172 if ($resql) {
173 if ($this->db->num_rows($resql)) {
174 $obj = $this->db->fetch_object($resql);
175
176 $this->id = $obj->rowid;
177 $this->fk_soc = $obj->fk_soc;
178 $this->socid = $obj->fk_soc;
179 $this->discount_type = $obj->discount_type;
180
181 $this->total_ht = $obj->amount_ht;
182 $this->total_tva = $obj->amount_tva;
183 $this->total_ttc = $obj->amount_ttc;
184 // For backward compatibility
185 $this->amount_ht = $this->total_ht;
186 $this->amount_tva = $this->total_tva;
187 $this->amount_ttc = $this->total_ttc;
188
189 $this->multicurrency_total_ht = $this->multicurrency_subprice = $obj->multicurrency_amount_ht;
190 $this->multicurrency_total_tva = $obj->multicurrency_amount_tva;
191 $this->multicurrency_total_ttc = $obj->multicurrency_amount_ttc;
192 // For backward compatibility
193 $this->multicurrency_amount_ht = $this->multicurrency_total_ht;
194 $this->multicurrency_amount_tva = $this->multicurrency_total_tva;
195 $this->multicurrency_amount_ttc = $this->multicurrency_total_ttc;
196
197 $this->tva_tx = $obj->tva_tx;
198 $this->vat_src_code = $obj->vat_src_code;
199
200 $this->fk_user = $obj->fk_user;
201 $this->fk_facture_line = $obj->fk_facture_line;
202 $this->fk_facture = $obj->fk_facture;
203 $this->fk_facture_source = $obj->fk_facture_source; // Id credit note or deposit source
204 $this->ref_facture_source = $obj->ref_facture_source; // Ref credit note or deposit source
205 $this->type_facture_source = $obj->type_facture_source; // Type credit note or deposit source
206 $this->fk_invoice_supplier_line = $obj->fk_invoice_supplier_line;
207 $this->fk_invoice_supplier = $obj->fk_invoice_supplier;
208 $this->fk_invoice_supplier_source = $obj->fk_invoice_supplier_source; // Id credit note or deposit source
209 $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source; // Ref credit note or deposit source
210 $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source; // Type credit note or deposit source
211 $this->description = $obj->description;
212 $this->datec = $this->db->jdate($obj->datec);
213
214 $this->db->free($resql);
215 return 1;
216 } else {
217 $this->db->free($resql);
218 return 0;
219 }
220 } else {
221 $this->error = $this->db->error();
222 return -1;
223 }
224 }
225
226
233 public function create($user)
234 {
235 global $conf, $langs;
236
237 // Clean parameters
238 $this->amount_ht = price2num($this->amount_ht);
239 $this->amount_tva = price2num($this->amount_tva);
240 $this->amount_ttc = price2num($this->amount_ttc);
241
242 $this->tva_tx = price2num($this->tva_tx);
243
244 $this->multicurrency_amount_ht = price2num($this->multicurrency_amount_ht);
245 $this->multicurrency_amount_tva = price2num($this->multicurrency_amount_tva);
246 $this->multicurrency_amount_ttc = price2num($this->multicurrency_amount_ttc);
247
248 if (empty($this->multicurrency_amount_ht)) {
249 $this->multicurrency_amount_ht = 0;
250 }
251 if (empty($this->multicurrency_amount_tva)) {
252 $this->multicurrency_amount_tva = 0;
253 }
254 if (empty($this->multicurrency_amount_ttc)) {
255 $this->multicurrency_amount_ttc = 0;
256 }
257 if (empty($this->tva_tx)) {
258 $this->tva_tx = 0;
259 }
260
261 // Check parameters
262 if (empty($this->description)) {
263 $this->error = 'BadValueForPropertyDescriptionOfDiscount';
264 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
265 return -1;
266 }
267
268 $userid = $user->id;
269 if (!($userid > 0)) { // For example when record is saved into an anonymous context with a not loaded object $user.
270 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
271 $tmpinvoice = new Facture($this->db);
272 $tmpinvoice->fetch($this->fk_facture_source);
273 $userid = $tmpinvoice->fk_user_author; // We use the author of invoice
274 }
275
276 // Insert request
277 $sql = "INSERT INTO ".$this->db->prefix()."societe_remise_except";
278 $sql .= " (entity, datec, fk_soc, discount_type, fk_user, description,";
279 $sql .= " amount_ht, amount_tva, amount_ttc, tva_tx, vat_src_code,";
280 $sql .= " multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,";
281 $sql .= " fk_facture_source, fk_invoice_supplier_source";
282 $sql .= ")";
283 $sql .= " VALUES (".$conf->entity.", '".$this->db->idate($this->datec != '' ? $this->datec : dol_now())."', ".((int) $this->socid).", ".(empty($this->discount_type) ? 0 : intval($this->discount_type)).", ".((int) $userid).", '".$this->db->escape($this->description)."',";
284 $sql .= " ".price2num($this->amount_ht).", ".price2num($this->amount_tva).", ".price2num($this->amount_ttc).", ".price2num($this->tva_tx).", '".$this->db->escape($this->vat_src_code)."',";
285 $sql .= " ".price2num($this->multicurrency_amount_ht).", ".price2num($this->multicurrency_amount_tva).", ".price2num($this->multicurrency_amount_ttc).", ";
286 $sql .= " ".($this->fk_facture_source ? ((int) $this->fk_facture_source) : "null").",";
287 $sql .= " ".($this->fk_invoice_supplier_source ? ((int) $this->fk_invoice_supplier_source) : "null");
288 $sql .= ")";
289
290 dol_syslog(get_class($this)."::create", LOG_DEBUG);
291 $resql = $this->db->query($sql);
292 if ($resql) {
293 $this->id = $this->db->last_insert_id($this->db->prefix()."societe_remise_except");
294 return $this->id;
295 } else {
296 $this->error = $this->db->lasterror().' - sql='.$sql;
297 return -1;
298 }
299 }
300
301
308 public function delete($user)
309 {
310 global $conf, $langs;
311
312 // Check if we can remove the discount
313 if ($this->fk_facture_source) {
314 $sql = "SELECT COUNT(rowid) as nb";
315 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
316 $sql .= " WHERE (fk_facture_line IS NOT NULL"; // Not used as absolute simple discount
317 $sql .= " OR fk_facture IS NOT NULL)"; // Not used as credit note and not used as deposit
318 $sql .= " AND fk_facture_source = ".((int) $this->fk_facture_source);
319 //$sql.=" AND rowid != ".$this->id;
320
321 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
322 $resql = $this->db->query($sql);
323 if ($resql) {
324 $obj = $this->db->fetch_object($resql);
325 if ($obj->nb > 0) {
326 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
327 return -2;
328 }
329 } else {
330 dol_print_error($this->db);
331 return -1;
332 }
333 }
334
335 // Check if we can remove the discount
336 if ($this->fk_invoice_supplier_source) {
337 $sql = "SELECT COUNT(rowid) as nb";
338 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
339 $sql .= " WHERE (fk_invoice_supplier_line IS NOT NULL"; // Not used as absolute simple discount
340 $sql .= " OR fk_invoice_supplier IS NOT NULL)"; // Not used as credit note and not used as deposit
341 $sql .= " AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
342 //$sql.=" AND rowid != ".$this->id;
343
344 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
345 $resql = $this->db->query($sql);
346 if ($resql) {
347 $obj = $this->db->fetch_object($resql);
348 if ($obj->nb > 0) {
349 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
350 return -2;
351 }
352 } else {
353 dol_print_error($this->db);
354 return -1;
355 }
356 }
357
358 $this->db->begin();
359
360 // Delete but only if not used
361 $sql = "DELETE FROM ".$this->db->prefix()."societe_remise_except ";
362 if ($this->fk_facture_source) {
363 $sql .= " WHERE fk_facture_source = ".((int) $this->fk_facture_source); // Delete all lines of same series
364 } elseif ($this->fk_invoice_supplier_source) {
365 $sql .= " WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source); // Delete all lines of same series
366 } else {
367 $sql .= " WHERE rowid = ".((int) $this->id); // Delete only line
368 }
369 $sql .= " AND (fk_facture_line IS NULL"; // Not used as absolute simple discount
370 $sql .= " AND fk_facture IS NULL)"; // Not used as credit note and not used as deposit
371 $sql .= " AND (fk_invoice_supplier_line IS NULL"; // Not used as absolute simple discount
372 $sql .= " AND fk_invoice_supplier IS NULL)"; // Not used as credit note and not used as deposit
373
374 dol_syslog(get_class($this)."::delete Delete discount", LOG_DEBUG);
375 require_once DOL_DOCUMENT_ROOT. '/core/class/commoninvoice.class.php';
376 $result = $this->db->query($sql);
377 if ($result) {
378 // If source of discount was a credit note or deposit, we change source statut.
379 if ($this->fk_facture_source) {
380 $sql = "UPDATE ".$this->db->prefix()."facture";
381 $sql .= " set paye=0, fk_statut=1";
382 $sql .= " WHERE type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_facture_source);
383
384 dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
385 $result = $this->db->query($sql);
386 if ($result) {
387 $this->db->commit();
388 return 1;
389 } else {
390 $this->error = $this->db->lasterror();
391 $this->db->rollback();
392 return -1;
393 }
394 } elseif ($this->fk_invoice_supplier_source) {
395 $sql = "UPDATE ".$this->db->prefix()."facture_fourn";
396 $sql .= " set paye=0, fk_statut=1";
397 $sql .= " WHERE type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_invoice_supplier_source);
398
399 dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
400 $result = $this->db->query($sql);
401 if ($result) {
402 $this->db->commit();
403 return 1;
404 } else {
405 $this->error = $this->db->lasterror();
406 $this->db->rollback();
407 return -1;
408 }
409 } else {
410 $this->db->commit();
411 return 1;
412 }
413 } else {
414 $this->error = $this->db->lasterror();
415 $this->db->rollback();
416 return -1;
417 }
418 }
419
420
421
422 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
432 public function link_to_invoice($rowidline, $rowidinvoice)
433 {
434 // phpcs:enable
435 // Check parameters
436 if (!$rowidline && !$rowidinvoice) {
437 $this->error = 'ErrorBadParameters';
438 return -1;
439 }
440 if ($rowidline && $rowidinvoice) {
441 $this->error = 'ErrorBadParameters';
442 return -2;
443 }
444
445 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
446 if (!empty($this->discount_type)) {
447 if ($rowidline) {
448 $sql .= " SET fk_invoice_supplier_line = ".((int) $rowidline);
449 }
450 if ($rowidinvoice) {
451 $sql .= " SET fk_invoice_supplier = ".((int) $rowidinvoice);
452 }
453 } else {
454 if ($rowidline) {
455 $sql .= " SET fk_facture_line = ".((int) $rowidline);
456 }
457 if ($rowidinvoice) {
458 $sql .= " SET fk_facture = ".((int) $rowidinvoice);
459 }
460 }
461 $sql .= " WHERE rowid = ".((int) $this->id);
462
463 dol_syslog(get_class($this)."::link_to_invoice", LOG_DEBUG);
464 $resql = $this->db->query($sql);
465 if ($resql) {
466 if (!empty($this->discount_type)) {
467 $this->fk_invoice_supplier_line = $rowidline;
468 $this->fk_invoice_supplier = $rowidinvoice;
469 } else {
470 $this->fk_facture_line = $rowidline;
471 $this->fk_facture = $rowidinvoice;
472 }
473 return 1;
474 } else {
475 $this->error = $this->db->error();
476 return -3;
477 }
478 }
479
480
481 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
488 public function unlink_invoice()
489 {
490 // phpcs:enable
491 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
492 if (!empty($this->discount_type)) {
493 $sql .= " SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
494 } else {
495 $sql .= " SET fk_facture_line = NULL, fk_facture = NULL";
496 }
497 $sql .= " WHERE rowid = ".((int) $this->id);
498
499 dol_syslog(get_class($this)."::unlink_invoice", LOG_DEBUG);
500 $resql = $this->db->query($sql);
501 if ($resql) {
502 return 1;
503 } else {
504 $this->error = $this->db->error();
505 return -3;
506 }
507 }
508
509
521 public function getAvailableDiscounts($company = null, $user = null, $filter = '', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
522 {
523 global $conf;
524
525 dol_syslog(get_class($this)."::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
526
527 $sql = "SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
528 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
529 $sql .= " WHERE rc.entity = ".$conf->entity;
530 $sql .= " AND rc.discount_type=".((int) $discount_type);
531 if (!empty($discount_type)) {
532 $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)"; // Available from supplier
533 } else {
534 $sql .= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available to customer
535 }
536 if (is_object($company)) {
537 $sql .= " AND rc.fk_soc = ".((int) $company->id);
538 }
539 if (is_object($user)) {
540 $sql .= " AND rc.fk_user = ".((int) $user->id);
541 }
542 if ($filter) {
543 $sql .= " AND (".$filter.")";
544 }
545 if ($maxvalue) {
546 $sql .= ' AND rc.amount_ttc <= '.((float) price2num($maxvalue));
547 }
548
549 $resql = $this->db->query($sql);
550 if ($resql) {
551 $obj = $this->db->fetch_object($resql);
552 //while ($obj)
553 //{
554 //print 'zz'.$obj->amount;
555 //$obj = $this->db->fetch_object($resql);
556 //}
557 if ($multicurrency) {
558 return $obj->multicurrency_amount;
559 }
560
561 return $obj->amount;
562 }
563 return -1;
564 }
565
566
575 public function getSumDepositsUsed($invoice, $multicurrency = 0)
576 {
577 dol_syslog(get_class($this)."::getSumDepositsUsed", LOG_DEBUG);
578
579 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
580 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
581 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
582 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
583 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
584 } elseif ($invoice->element == 'invoice_supplier') {
585 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
586 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
587 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
588 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
589 } else {
590 $this->error = get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
591 dol_print_error($this->db, $this->error);
592 return -1;
593 }
594
595 $resql = $this->db->query($sql);
596 if ($resql) {
597 $obj = $this->db->fetch_object($resql);
598 if ($multicurrency == 1) {
599 return $obj->multicurrency_amount;
600 } else {
601 return $obj->amount;
602 }
603 } else {
604 $this->error = $this->db->lasterror();
605 return -1;
606 }
607 }
608
616 public function getSumCreditNotesUsed($invoice, $multicurrency = 0)
617 {
618 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
619
620 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
621 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
622 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
623 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
624 $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
625 } elseif ($invoice->element == 'invoice_supplier') {
626 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
627 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
628 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
629 $sql .= " AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.", ".$invoice::TYPE_CREDIT_NOTE).")"; // Find discount coming from credit note or excess paid
630 } else {
631 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
632 dol_print_error($this->db, $this->error);
633 return -1;
634 }
635
636 $resql = $this->db->query($sql);
637 if ($resql) {
638 $obj = $this->db->fetch_object($resql);
639 if ($multicurrency == 1) {
640 return $obj->multicurrency_amount;
641 } else {
642 return $obj->amount;
643 }
644 } else {
645 $this->error = $this->db->lasterror();
646 return -1;
647 }
648 }
656 public function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency = 0)
657 {
658 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
659
660 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
661 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
662 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
663 $sql .= " WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
664 } elseif ($invoice->element == 'invoice_supplier') {
665 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
666 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
667 $sql .= " WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
668 } else {
669 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
670 dol_print_error($this->db, $this->error);
671 return -1;
672 }
673
674 $resql = $this->db->query($sql);
675 if ($resql) {
676 $obj = $this->db->fetch_object($resql);
677 if ($multicurrency) {
678 return $obj->multicurrency_amount;
679 } else {
680 return $obj->amount;
681 }
682 } else {
683 $this->error = $this->db->lasterror();
684 return -1;
685 }
686 }
687
695 public function getNomUrl($withpicto, $option = 'invoice')
696 {
697 global $langs;
698
699 $result = '';
700 $link = '';
701 $linkend = '';
702 $label = '';
703 $picto = '';
704 $ref = '';
705
706 if ($option == 'invoice') {
707 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
708 $link = !empty($this->discount_type) ? '/fourn/facture/card.php' : '/compta/facture/card.php';
709 $label = $langs->trans("ShowSourceInvoice").': '.$this->ref_facture_source;
710 $link = '<a href="'.DOL_URL_ROOT.$link.'?facid='.$facid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
711 $linkend = '</a>';
712 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
713 $picto = 'bill';
714 }
715 if ($option == 'discount') {
716 $label = $langs->trans("Discount");
717 $link = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->socid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
718 $linkend = '</a>';
719 $ref = $langs->trans("Discount");
720 $picto = 'generic';
721 }
722
723
724 if ($withpicto) {
725 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
726 }
727 if ($withpicto && $withpicto != 2) {
728 $result .= ' ';
729 }
730 $result .= $link.$ref.$linkend;
731 return $result;
732 }
733
734
742 public function initAsSpecimen()
743 {
744 $this->fk_soc = 1;
745 $this->socid = 1;
746 $this->amount_ht = 10;
747 $this->amount_tva = 1.96;
748 $this->amount_ttc = 11.96;
749 $this->tva_tx = 19.6;
750 $this->description = 'Specimen discount';
751
752 return 1;
753 }
754}
const TYPE_CREDIT_NOTE
Credit note invoice.
const TYPE_DEPOSIT
Deposit invoice.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Class to manage absolute discounts.
link_to_invoice($rowidline, $rowidinvoice)
Link the discount to a particular invoice line or a particular invoice.
getAvailableDiscounts($company=null, $user=null, $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.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after 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_now($mode='auto')
Return date for now.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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...