dolibarr 22.0.5
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 * Copyright (C) 2024 Noé Cendrier <noe.cendrier@altairis.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
30
35{
40 public $fk_soc;
44 public $socid;
45
49 public $discount_type; // 0 => customer discount, 1 => supplier discount
50
54 public $total_ht;
58 public $total_tva;
62 public $total_ttc;
67 public $amount_ht; // deprecated
72 public $amount_tva; // deprecated
77 public $amount_ttc; // deprecated
78
82 public $multicurrency_total_ht;
86 public $multicurrency_total_tva;
90 public $multicurrency_total_ttc;
95 public $multicurrency_amount_ht; // deprecated
100 public $multicurrency_amount_tva; // deprecated
105 public $multicurrency_amount_ttc; // deprecated
106
110 public $multicurrency_subprice;
111
115 public $fk_invoice_supplier;
116
120 public $fk_invoice_supplier_line;
121
125 public $tva_tx;
129 public $vat_src_code;
130
134 public $fk_user;
135
139 public $description;
140
146 public $datec;
147
151 public $fk_facture_line;
152
156 public $fk_facture;
157
161 public $fk_facture_source;
165 public $ref_facture_source; // Ref credit note or deposit used to create the discount
169 public $type_facture_source;
170
174 public $fk_invoice_supplier_source;
178 public $ref_invoice_supplier_source; // Ref credit note or deposit used to create the discount
182 public $type_invoice_supplier_source;
183
184 /* Customer Discount */
185 const TYPE_CUSTOMER = 0;
186
187 /* Supplier Discount */
188 const TYPE_SUPPLIER = 1;
189
195 public function __construct($db)
196 {
197 $this->db = $db;
198 }
199
200
209 public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
210 {
211 // Check parameters
212 if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
213 $this->error = 'ErrorBadParameters';
214 return -1;
215 }
216
217 $sql = "SELECT sr.rowid, sr.fk_soc, sr.discount_type,";
218 $sql .= " sr.fk_user,";
219 $sql .= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx, sr.vat_src_code,";
220 $sql .= " sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
221 $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,";
222 $sql .= " sr.datec,";
223 $sql .= " f.ref as ref_facture_source, f.type as type_facture_source,";
224 $sql .= " fsup.ref as ref_invoice_supplier_source, fsup.type as type_invoice_supplier_source";
225 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as sr";
226 $sql .= " LEFT JOIN ".$this->db->prefix()."facture as f ON sr.fk_facture_source = f.rowid";
227 $sql .= " LEFT JOIN ".$this->db->prefix()."facture_fourn as fsup ON sr.fk_invoice_supplier_source = fsup.rowid";
228 $sql .= " WHERE sr.entity IN (".getEntity('invoice').")";
229 if ($rowid) {
230 $sql .= " AND sr.rowid = ".((int) $rowid);
231 }
232 if ($fk_facture_source) {
233 $sql .= " AND sr.fk_facture_source = ".((int) $fk_facture_source);
234 }
235 if ($fk_invoice_supplier_source) {
236 $sql .= " AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
237 }
238
239 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
240 $resql = $this->db->query($sql);
241 if ($resql) {
242 if ($this->db->num_rows($resql)) {
243 $obj = $this->db->fetch_object($resql);
244
245 $this->id = $obj->rowid;
246 $this->fk_soc = $obj->fk_soc;
247 $this->socid = $obj->fk_soc;
248 $this->discount_type = $obj->discount_type;
249
250 $this->total_ht = $obj->amount_ht;
251 $this->total_tva = $obj->amount_tva;
252 $this->total_ttc = $obj->amount_ttc;
253 // For backward compatibility
254 $this->amount_ht = $this->total_ht;
255 $this->amount_tva = $this->total_tva;
256 $this->amount_ttc = $this->total_ttc;
257
258 $this->multicurrency_total_ht = $this->multicurrency_subprice = $obj->multicurrency_amount_ht;
259 $this->multicurrency_total_tva = $obj->multicurrency_amount_tva;
260 $this->multicurrency_total_ttc = $obj->multicurrency_amount_ttc;
261 // For backward compatibility
262 $this->multicurrency_amount_ht = $this->multicurrency_total_ht;
263 $this->multicurrency_amount_tva = $this->multicurrency_total_tva;
264 $this->multicurrency_amount_ttc = $this->multicurrency_total_ttc;
265
266 $this->tva_tx = $obj->tva_tx;
267 $this->vat_src_code = $obj->vat_src_code;
268
269 $this->fk_user = $obj->fk_user;
270 $this->fk_facture_line = $obj->fk_facture_line;
271 $this->fk_facture = $obj->fk_facture;
272 $this->fk_facture_source = $obj->fk_facture_source; // Id credit note or deposit source
273 $this->ref_facture_source = $obj->ref_facture_source; // Ref credit note or deposit source
274 $this->type_facture_source = $obj->type_facture_source; // Type credit note or deposit source
275 $this->fk_invoice_supplier_line = $obj->fk_invoice_supplier_line;
276 $this->fk_invoice_supplier = $obj->fk_invoice_supplier;
277 $this->fk_invoice_supplier_source = $obj->fk_invoice_supplier_source; // Id credit note or deposit source
278 $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source; // Ref credit note or deposit source
279 $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source; // Type credit note or deposit source
280 $this->description = $obj->description;
281 $this->datec = $this->db->jdate($obj->datec);
282
283 $this->db->free($resql);
284 return 1;
285 } else {
286 $this->db->free($resql);
287 return 0;
288 }
289 } else {
290 $this->error = $this->db->error();
291 return -1;
292 }
293 }
294
295
302 public function create($user)
303 {
304 global $conf;
305
306 // Clean parameters
307 $this->amount_ht = price2num($this->amount_ht);
308 $this->amount_tva = price2num($this->amount_tva);
309 $this->amount_ttc = price2num($this->amount_ttc);
310
311 $this->tva_tx = price2num($this->tva_tx);
312
313 $this->multicurrency_amount_ht = price2num($this->multicurrency_amount_ht);
314 $this->multicurrency_amount_tva = price2num($this->multicurrency_amount_tva);
315 $this->multicurrency_amount_ttc = price2num($this->multicurrency_amount_ttc);
316
317 if (empty($this->multicurrency_amount_ht)) {
318 $this->multicurrency_amount_ht = 0;
319 }
320 if (empty($this->multicurrency_amount_tva)) {
321 $this->multicurrency_amount_tva = 0;
322 }
323 if (empty($this->multicurrency_amount_ttc)) {
324 $this->multicurrency_amount_ttc = 0;
325 }
326 if (empty($this->tva_tx)) {
327 $this->tva_tx = 0;
328 }
329
330 // Check parameters
331 if (empty($this->description)) {
332 $this->error = 'BadValueForPropertyDescriptionOfDiscount';
333 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
334 return -1;
335 }
336
337 $userid = $user->id;
338 if (!($userid > 0)) { // For example when record is saved into an anonymous context with a not loaded object $user.
339 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
340 $tmpinvoice = new Facture($this->db);
341 $tmpinvoice->fetch($this->fk_facture_source);
342 $userid = $tmpinvoice->user_creation_id; // We use the author of invoice
343 }
344
345 // Insert request
346 $sql = "INSERT INTO ".$this->db->prefix()."societe_remise_except";
347 $sql .= " (entity, datec, fk_soc, discount_type, fk_user, description,";
348 $sql .= " amount_ht, amount_tva, amount_ttc, tva_tx, vat_src_code,";
349 $sql .= " multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,";
350 $sql .= " fk_facture_source, fk_invoice_supplier_source, multicurrency_code, multicurrency_tx";
351 $sql .= ")";
352 $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)."',";
353 $sql .= " ".price2num($this->amount_ht).", ".price2num($this->amount_tva).", ".price2num($this->amount_ttc).", ".price2num($this->tva_tx).", '".$this->db->escape($this->vat_src_code)."',";
354 $sql .= " ".price2num($this->multicurrency_amount_ht).", ".price2num($this->multicurrency_amount_tva).", ".price2num($this->multicurrency_amount_ttc).", ";
355 $sql .= " ".($this->fk_facture_source ? ((int) $this->fk_facture_source) : "null").",";
356 $sql .= " ".($this->fk_invoice_supplier_source ? ((int) $this->fk_invoice_supplier_source) : "null").",";
357 $sql .= " ".($this->multicurrency_code ? "'".$this->db->escape($this->multicurrency_code)."'" : "null").",";
358 $sql .= " ".($this->multicurrency_tx ? price2num($this->multicurrency_tx) : "null");
359 $sql .= ")";
360
361 dol_syslog(get_class($this)."::create", LOG_DEBUG);
362 $resql = $this->db->query($sql);
363 if ($resql) {
364 $this->id = $this->db->last_insert_id($this->db->prefix()."societe_remise_except");
365 return $this->id;
366 } else {
367 $this->error = $this->db->lasterror().' - sql='.$sql;
368 return -1;
369 }
370 }
371
372
379 public function delete($user)
380 {
381 global $conf, $langs;
382
383 // Check if we can remove the discount
384 if ($this->fk_facture_source) {
385 $sql = "SELECT COUNT(rowid) as nb";
386 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
387 $sql .= " WHERE (fk_facture_line IS NOT NULL"; // Not used as absolute simple discount
388 $sql .= " OR fk_facture IS NOT NULL)"; // Not used as credit note and not used as deposit
389 $sql .= " AND fk_facture_source = ".((int) $this->fk_facture_source);
390 //$sql.=" AND rowid != ".$this->id;
391
392 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
393 $resql = $this->db->query($sql);
394 if ($resql) {
395 $obj = $this->db->fetch_object($resql);
396 if ($obj->nb > 0) {
397 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
398 return -2;
399 }
400 } else {
401 dol_print_error($this->db);
402 return -1;
403 }
404 }
405
406 // Check if we can remove the discount
407 if ($this->fk_invoice_supplier_source) {
408 $sql = "SELECT COUNT(rowid) as nb";
409 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
410 $sql .= " WHERE (fk_invoice_supplier_line IS NOT NULL"; // Not used as absolute simple discount
411 $sql .= " OR fk_invoice_supplier IS NOT NULL)"; // Not used as credit note and not used as deposit
412 $sql .= " AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
413 //$sql.=" AND rowid != ".$this->id;
414
415 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
416 $resql = $this->db->query($sql);
417 if ($resql) {
418 $obj = $this->db->fetch_object($resql);
419 if ($obj->nb > 0) {
420 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
421 return -2;
422 }
423 } else {
424 dol_print_error($this->db);
425 return -1;
426 }
427 }
428
429 $this->db->begin();
430
431 // Delete but only if not used
432 $sql = "DELETE FROM ".$this->db->prefix()."societe_remise_except ";
433 if ($this->fk_facture_source) {
434 $sql .= " WHERE fk_facture_source = ".((int) $this->fk_facture_source); // Delete all lines of same series
435 } elseif ($this->fk_invoice_supplier_source) {
436 $sql .= " WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source); // Delete all lines of same series
437 } else {
438 $sql .= " WHERE rowid = ".((int) $this->id); // Delete only line
439 }
440 $sql .= " AND (fk_facture_line IS NULL"; // Not used as absolute simple discount
441 $sql .= " AND fk_facture IS NULL)"; // Not used as credit note and not used as deposit
442 $sql .= " AND (fk_invoice_supplier_line IS NULL"; // Not used as absolute simple discount
443 $sql .= " AND fk_invoice_supplier IS NULL)"; // Not used as credit note and not used as deposit
444
445 dol_syslog(get_class($this)."::delete Delete discount", LOG_DEBUG);
446
447 require_once DOL_DOCUMENT_ROOT. '/core/class/commoninvoice.class.php';
448 $result = $this->db->query($sql);
449 if ($result) {
450 // If source of discount was a credit note or deposit, we change source statut.
451 if ($this->fk_facture_source) {
452 $sql = "UPDATE ".$this->db->prefix()."facture";
453 $sql .= " set paye=0, fk_statut=1";
454 $sql .= " WHERE type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_facture_source);
455
456 dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
457 $result = $this->db->query($sql);
458 if ($result) {
459 $this->db->commit();
460 return 1;
461 } else {
462 $this->error = $this->db->lasterror();
463 $this->db->rollback();
464 return -1;
465 }
466 } elseif ($this->fk_invoice_supplier_source) {
467 $sql = "UPDATE ".$this->db->prefix()."facture_fourn";
468 $sql .= " set paye=0, fk_statut=1";
469 $sql .= " WHERE type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_invoice_supplier_source);
470
471 dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
472 $result = $this->db->query($sql);
473 if ($result) {
474 $this->db->commit();
475 return 1;
476 } else {
477 $this->error = $this->db->lasterror();
478 $this->db->rollback();
479 return -1;
480 }
481 } else {
482 $this->db->commit();
483 return 1;
484 }
485 } else {
486 $this->error = $this->db->lasterror();
487 $this->db->rollback();
488 return -1;
489 }
490 }
491
492
493
494 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
505 public function link_to_invoice($rowidline, $rowidinvoice, $notrigger = 0)
506 {
507 // phpcs:enable
508 global $user;
509
510 // Check parameters
511 if (!$rowidline && !$rowidinvoice) {
512 $this->error = 'ErrorBadParameters';
513 return -1;
514 }
515 if ($rowidline && $rowidinvoice) {
516 $this->error = 'ErrorBadParameters';
517 return -2;
518 }
519
520 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
521 if (!empty($this->discount_type)) {
522 if ($rowidline) {
523 $sql .= " SET fk_invoice_supplier_line = ".((int) $rowidline);
524 }
525 if ($rowidinvoice) {
526 $sql .= " SET fk_invoice_supplier = ".((int) $rowidinvoice);
527 }
528 } else {
529 if ($rowidline) {
530 $sql .= " SET fk_facture_line = ".((int) $rowidline);
531 }
532 if ($rowidinvoice) {
533 $sql .= " SET fk_facture = ".((int) $rowidinvoice);
534 }
535 }
536 $sql .= " WHERE rowid = ".((int) $this->id);
537
538 dol_syslog(get_class($this)."::link_to_invoice", LOG_DEBUG);
539 $resql = $this->db->query($sql);
540 if ($resql) {
541 if (!empty($this->discount_type)) {
542 $this->fk_invoice_supplier_line = $rowidline;
543 $this->fk_invoice_supplier = $rowidinvoice;
544 } else {
545 $this->fk_facture_line = $rowidline;
546 $this->fk_facture = $rowidinvoice;
547 }
548 if (!$notrigger) {
549 // Call trigger
550 $result = $this->call_trigger('DISCOUNT_MODIFY', $user);
551 if ($result < 0) {
552 return -2;
553 }
554 // End call triggers
555 }
556 return 1;
557 } else {
558 $this->error = $this->db->error();
559 return -3;
560 }
561 }
562
563
564 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
572 public function unlink_invoice($notrigger = 0)
573 {
574 // phpcs:enable
575 global $user;
576
577 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
578 if (!empty($this->discount_type)) {
579 $sql .= " SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
580 } else {
581 $sql .= " SET fk_facture_line = NULL, fk_facture = NULL";
582 }
583 $sql .= " WHERE rowid = ".((int) $this->id);
584
585 dol_syslog(get_class($this)."::unlink_invoice", LOG_DEBUG);
586 $resql = $this->db->query($sql);
587 if ($resql) {
588 if (!$notrigger) {
589 // Call trigger
590 $result = $this->call_trigger('DISCOUNT_MODIFY', $user);
591 if ($result < 0) {
592 return -2;
593 }
594 // End call triggers
595 }
596 return 1;
597 } else {
598 $this->error = $this->db->error();
599 return -3;
600 }
601 }
602
603
615 public function getAvailableDiscounts($company = null, $user = null, $filter = '', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
616 {
617 global $conf;
618
619 dol_syslog(get_class($this)."::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
620
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";
623 $sql .= " WHERE rc.entity = ".$conf->entity;
624 $sql .= " AND rc.discount_type=".((int) $discount_type);
625 if (!empty($discount_type)) {
626 $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)"; // Available from supplier
627 } else {
628 $sql .= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available to customer
629 }
630 if (is_object($company)) {
631 $sql .= " AND rc.fk_soc = ".((int) $company->id);
632 }
633 if (is_object($user)) {
634 $sql .= " AND rc.fk_user = ".((int) $user->id);
635 }
636 if ($filter) {
637 $sql .= " AND (".$filter.")";
638 }
639 if ($maxvalue) {
640 $sql .= ' AND rc.amount_ttc <= '.((float) price2num($maxvalue));
641 }
642
643 $resql = $this->db->query($sql);
644 if ($resql) {
645 $obj = $this->db->fetch_object($resql);
646 //while ($obj)
647 //{
648 //print 'zz'.$obj->amount;
649 //$obj = $this->db->fetch_object($resql);
650 //}
651 if ($multicurrency) {
652 return $obj->multicurrency_amount;
653 }
654
655 return $obj->amount;
656 }
657 return -1;
658 }
659
660
669 public function getSumDepositsUsed($invoice, $multicurrency = 0)
670 {
671 dol_syslog(get_class($this)."::getSumDepositsUsed", LOG_DEBUG);
672
673 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
674 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
675 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
676 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
677 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
678 } elseif ($invoice->element == 'invoice_supplier') {
679 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
680 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
681 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
682 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
683 } else {
684 $this->error = get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
685 dol_print_error($this->db, $this->error);
686 return -1;
687 }
688
689 $resql = $this->db->query($sql);
690 if ($resql) {
691 $obj = $this->db->fetch_object($resql);
692 if ($multicurrency == 1) {
693 return $obj->multicurrency_amount;
694 } else {
695 return $obj->amount;
696 }
697 } else {
698 $this->error = $this->db->lasterror();
699 return -1;
700 }
701 }
702
710 public function getSumCreditNotesUsed($invoice, $multicurrency = 0)
711 {
712 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
713
714 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
715 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
716 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
717 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
718 $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
719 } elseif ($invoice->element == 'invoice_supplier') {
720 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
721 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
722 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
723 $sql .= " AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.", ".$invoice::TYPE_CREDIT_NOTE).")"; // Find discount coming from credit note or excess paid
724 } else {
725 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
726 dol_print_error($this->db, $this->error);
727 return 'ErrorBadElementType';
728 }
729
730 $resql = $this->db->query($sql);
731 if ($resql) {
732 $obj = $this->db->fetch_object($resql);
733 if ($multicurrency == 1) {
734 return $obj->multicurrency_amount;
735 } else {
736 return $obj->amount;
737 }
738 } else {
739 $this->error = $this->db->lasterror();
740 return 'ErrorBadSQLquery';
741 }
742 }
750 public function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency = 0)
751 {
752 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
753
754 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
755 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
756 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
757 $sql .= " WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
758 } elseif ($invoice->element == 'invoice_supplier') {
759 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
760 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
761 $sql .= " WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
762 } else {
763 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
764 dol_print_error($this->db, $this->error);
765 return -1;
766 }
767
768 $resql = $this->db->query($sql);
769 if ($resql) {
770 $obj = $this->db->fetch_object($resql);
771 if ($multicurrency) {
772 return $obj->multicurrency_amount;
773 } else {
774 return $obj->amount;
775 }
776 } else {
777 $this->error = $this->db->lasterror();
778 return -1;
779 }
780 }
781
789 public function getNomUrl($withpicto = 0, $option = 'invoice')
790 {
791 global $langs;
792
793 $result = '';
794 $link = '';
795 $linkend = '';
796 $label = '';
797 $picto = '';
798 $ref = '';
799
800 if ($option == 'invoice') {
801 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
802 $link = !empty($this->discount_type) ? '/fourn/facture/card.php' : '/compta/facture/card.php';
803 $label = $langs->trans("ShowSourceInvoice").': '.$this->ref_facture_source;
804 $link = '<a href="'.DOL_URL_ROOT.$link.'?facid='.$facid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
805 $linkend = '</a>';
806 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
807 $picto = 'bill';
808 }
809 if ($option == 'discount') {
810 $label = $langs->trans("Discount");
811 $link = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->socid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
812 $linkend = '</a>';
813 $ref = $langs->trans("Discount");
814 $picto = 'generic';
815 }
816
817
818 if ($withpicto) {
819 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
820 }
821 if ($withpicto && $withpicto != 2) {
822 $result .= ' ';
823 }
824 $result .= $link.$ref.$linkend;
825 return $result;
826 }
827
828
836 public function initAsSpecimen()
837 {
838 $this->socid = 1;
839 $this->amount_ht = 10;
840 $this->amount_tva = 1.96;
841 $this->amount_ttc = 11.96;
842 $this->tva_tx = 19.6;
843 $this->description = 'Specimen discount';
844
845 return 1;
846 }
847}
const TYPE_CREDIT_NOTE
Credit note invoice.
const TYPE_DEPOSIT
Deposit invoice.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage absolute discounts.
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.
fetch($rowid, $fk_facture_source=0, $fk_invoice_supplier_source=0)
Load object from database into memory.
unlink_invoice($notrigger=0)
Link the discount to a particular invoice line or a particular invoice.
getNomUrl($withpicto=0, $option='invoice')
Return clickable ref of object (with picto or not)
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.
getSumCreditNotesUsed($invoice, $multicurrency=0)
Return amount (with tax) of all credit notes invoices + excess received used by invoice as a payment.
link_to_invoice($rowidline, $rowidinvoice, $notrigger=0)
Link the discount to a particular invoice line or a particular invoice.
__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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79