dolibarr 21.0.0-beta
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->fk_user_author; // 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";
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 .= ")";
358
359 dol_syslog(get_class($this)."::create", LOG_DEBUG);
360 $resql = $this->db->query($sql);
361 if ($resql) {
362 $this->id = $this->db->last_insert_id($this->db->prefix()."societe_remise_except");
363 return $this->id;
364 } else {
365 $this->error = $this->db->lasterror().' - sql='.$sql;
366 return -1;
367 }
368 }
369
370
377 public function delete($user)
378 {
379 global $conf, $langs;
380
381 // Check if we can remove the discount
382 if ($this->fk_facture_source) {
383 $sql = "SELECT COUNT(rowid) as nb";
384 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
385 $sql .= " WHERE (fk_facture_line IS NOT NULL"; // Not used as absolute simple discount
386 $sql .= " OR fk_facture IS NOT NULL)"; // Not used as credit note and not used as deposit
387 $sql .= " AND fk_facture_source = ".((int) $this->fk_facture_source);
388 //$sql.=" AND rowid != ".$this->id;
389
390 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
391 $resql = $this->db->query($sql);
392 if ($resql) {
393 $obj = $this->db->fetch_object($resql);
394 if ($obj->nb > 0) {
395 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
396 return -2;
397 }
398 } else {
399 dol_print_error($this->db);
400 return -1;
401 }
402 }
403
404 // Check if we can remove the discount
405 if ($this->fk_invoice_supplier_source) {
406 $sql = "SELECT COUNT(rowid) as nb";
407 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
408 $sql .= " WHERE (fk_invoice_supplier_line IS NOT NULL"; // Not used as absolute simple discount
409 $sql .= " OR fk_invoice_supplier IS NOT NULL)"; // Not used as credit note and not used as deposit
410 $sql .= " AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
411 //$sql.=" AND rowid != ".$this->id;
412
413 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
414 $resql = $this->db->query($sql);
415 if ($resql) {
416 $obj = $this->db->fetch_object($resql);
417 if ($obj->nb > 0) {
418 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
419 return -2;
420 }
421 } else {
422 dol_print_error($this->db);
423 return -1;
424 }
425 }
426
427 $this->db->begin();
428
429 // Delete but only if not used
430 $sql = "DELETE FROM ".$this->db->prefix()."societe_remise_except ";
431 if ($this->fk_facture_source) {
432 $sql .= " WHERE fk_facture_source = ".((int) $this->fk_facture_source); // Delete all lines of same series
433 } elseif ($this->fk_invoice_supplier_source) {
434 $sql .= " WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source); // Delete all lines of same series
435 } else {
436 $sql .= " WHERE rowid = ".((int) $this->id); // Delete only line
437 }
438 $sql .= " AND (fk_facture_line IS NULL"; // Not used as absolute simple discount
439 $sql .= " AND fk_facture IS NULL)"; // Not used as credit note and not used as deposit
440 $sql .= " AND (fk_invoice_supplier_line IS NULL"; // Not used as absolute simple discount
441 $sql .= " AND fk_invoice_supplier IS NULL)"; // Not used as credit note and not used as deposit
442
443 dol_syslog(get_class($this)."::delete Delete discount", LOG_DEBUG);
444
445 require_once DOL_DOCUMENT_ROOT. '/core/class/commoninvoice.class.php';
446 $result = $this->db->query($sql);
447 if ($result) {
448 // If source of discount was a credit note or deposit, we change source statut.
449 if ($this->fk_facture_source) {
450 $sql = "UPDATE ".$this->db->prefix()."facture";
451 $sql .= " set paye=0, fk_statut=1";
452 $sql .= " WHERE type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_facture_source);
453
454 dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
455 $result = $this->db->query($sql);
456 if ($result) {
457 $this->db->commit();
458 return 1;
459 } else {
460 $this->error = $this->db->lasterror();
461 $this->db->rollback();
462 return -1;
463 }
464 } elseif ($this->fk_invoice_supplier_source) {
465 $sql = "UPDATE ".$this->db->prefix()."facture_fourn";
466 $sql .= " set paye=0, fk_statut=1";
467 $sql .= " WHERE type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_invoice_supplier_source);
468
469 dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
470 $result = $this->db->query($sql);
471 if ($result) {
472 $this->db->commit();
473 return 1;
474 } else {
475 $this->error = $this->db->lasterror();
476 $this->db->rollback();
477 return -1;
478 }
479 } else {
480 $this->db->commit();
481 return 1;
482 }
483 } else {
484 $this->error = $this->db->lasterror();
485 $this->db->rollback();
486 return -1;
487 }
488 }
489
490
491
492 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
503 public function link_to_invoice($rowidline, $rowidinvoice, $notrigger = 0)
504 {
505 // phpcs:enable
506 global $user;
507
508 // Check parameters
509 if (!$rowidline && !$rowidinvoice) {
510 $this->error = 'ErrorBadParameters';
511 return -1;
512 }
513 if ($rowidline && $rowidinvoice) {
514 $this->error = 'ErrorBadParameters';
515 return -2;
516 }
517
518 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
519 if (!empty($this->discount_type)) {
520 if ($rowidline) {
521 $sql .= " SET fk_invoice_supplier_line = ".((int) $rowidline);
522 }
523 if ($rowidinvoice) {
524 $sql .= " SET fk_invoice_supplier = ".((int) $rowidinvoice);
525 }
526 } else {
527 if ($rowidline) {
528 $sql .= " SET fk_facture_line = ".((int) $rowidline);
529 }
530 if ($rowidinvoice) {
531 $sql .= " SET fk_facture = ".((int) $rowidinvoice);
532 }
533 }
534 $sql .= " WHERE rowid = ".((int) $this->id);
535
536 dol_syslog(get_class($this)."::link_to_invoice", LOG_DEBUG);
537 $resql = $this->db->query($sql);
538 if ($resql) {
539 if (!empty($this->discount_type)) {
540 $this->fk_invoice_supplier_line = $rowidline;
541 $this->fk_invoice_supplier = $rowidinvoice;
542 } else {
543 $this->fk_facture_line = $rowidline;
544 $this->fk_facture = $rowidinvoice;
545 }
546 if (!$notrigger) {
547 // Call trigger
548 $result = $this->call_trigger('DISCOUNT_MODIFY', $user);
549 if ($result < 0) {
550 return -2;
551 }
552 // End call triggers
553 }
554 return 1;
555 } else {
556 $this->error = $this->db->error();
557 return -3;
558 }
559 }
560
561
562 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
570 public function unlink_invoice($notrigger = 0)
571 {
572 // phpcs:enable
573 global $user;
574
575 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
576 if (!empty($this->discount_type)) {
577 $sql .= " SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
578 } else {
579 $sql .= " SET fk_facture_line = NULL, fk_facture = NULL";
580 }
581 $sql .= " WHERE rowid = ".((int) $this->id);
582
583 dol_syslog(get_class($this)."::unlink_invoice", LOG_DEBUG);
584 $resql = $this->db->query($sql);
585 if ($resql) {
586 if (!$notrigger) {
587 // Call trigger
588 $result = $this->call_trigger('DISCOUNT_MODIFY', $user);
589 if ($result < 0) {
590 return -2;
591 }
592 // End call triggers
593 }
594 return 1;
595 } else {
596 $this->error = $this->db->error();
597 return -3;
598 }
599 }
600
601
613 public function getAvailableDiscounts($company = null, $user = null, $filter = '', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
614 {
615 global $conf;
616
617 dol_syslog(get_class($this)."::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
618
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";
621 $sql .= " WHERE rc.entity = ".$conf->entity;
622 $sql .= " AND rc.discount_type=".((int) $discount_type);
623 if (!empty($discount_type)) {
624 $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)"; // Available from supplier
625 } else {
626 $sql .= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available to customer
627 }
628 if (is_object($company)) {
629 $sql .= " AND rc.fk_soc = ".((int) $company->id);
630 }
631 if (is_object($user)) {
632 $sql .= " AND rc.fk_user = ".((int) $user->id);
633 }
634 if ($filter) {
635 $sql .= " AND (".$filter.")";
636 }
637 if ($maxvalue) {
638 $sql .= ' AND rc.amount_ttc <= '.((float) price2num($maxvalue));
639 }
640
641 $resql = $this->db->query($sql);
642 if ($resql) {
643 $obj = $this->db->fetch_object($resql);
644 //while ($obj)
645 //{
646 //print 'zz'.$obj->amount;
647 //$obj = $this->db->fetch_object($resql);
648 //}
649 if ($multicurrency) {
650 return $obj->multicurrency_amount;
651 }
652
653 return $obj->amount;
654 }
655 return -1;
656 }
657
658
667 public function getSumDepositsUsed($invoice, $multicurrency = 0)
668 {
669 dol_syslog(get_class($this)."::getSumDepositsUsed", LOG_DEBUG);
670
671 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
672 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
673 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
674 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
675 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
676 } elseif ($invoice->element == 'invoice_supplier') {
677 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
678 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
679 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
680 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
681 } else {
682 $this->error = get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
683 dol_print_error($this->db, $this->error);
684 return -1;
685 }
686
687 $resql = $this->db->query($sql);
688 if ($resql) {
689 $obj = $this->db->fetch_object($resql);
690 if ($multicurrency == 1) {
691 return $obj->multicurrency_amount;
692 } else {
693 return $obj->amount;
694 }
695 } else {
696 $this->error = $this->db->lasterror();
697 return -1;
698 }
699 }
700
708 public function getSumCreditNotesUsed($invoice, $multicurrency = 0)
709 {
710 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
711
712 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
713 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
714 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
715 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
716 $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
717 } elseif ($invoice->element == 'invoice_supplier') {
718 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
719 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
720 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
721 $sql .= " AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.", ".$invoice::TYPE_CREDIT_NOTE).")"; // Find discount coming from credit note or excess paid
722 } else {
723 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
724 dol_print_error($this->db, $this->error);
725 return -1;
726 }
727
728 $resql = $this->db->query($sql);
729 if ($resql) {
730 $obj = $this->db->fetch_object($resql);
731 if ($multicurrency == 1) {
732 return $obj->multicurrency_amount;
733 } else {
734 return $obj->amount;
735 }
736 } else {
737 $this->error = $this->db->lasterror();
738 return -1;
739 }
740 }
748 public function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency = 0)
749 {
750 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
751
752 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
753 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
754 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
755 $sql .= " WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
756 } elseif ($invoice->element == 'invoice_supplier') {
757 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
758 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
759 $sql .= " WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
760 } else {
761 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
762 dol_print_error($this->db, $this->error);
763 return -1;
764 }
765
766 $resql = $this->db->query($sql);
767 if ($resql) {
768 $obj = $this->db->fetch_object($resql);
769 if ($multicurrency) {
770 return $obj->multicurrency_amount;
771 } else {
772 return $obj->amount;
773 }
774 } else {
775 $this->error = $this->db->lasterror();
776 return -1;
777 }
778 }
779
787 public function getNomUrl($withpicto = 0, $option = 'invoice')
788 {
789 global $langs;
790
791 $result = '';
792 $link = '';
793 $linkend = '';
794 $label = '';
795 $picto = '';
796 $ref = '';
797
798 if ($option == 'invoice') {
799 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
800 $link = !empty($this->discount_type) ? '/fourn/facture/card.php' : '/compta/facture/card.php';
801 $label = $langs->trans("ShowSourceInvoice").': '.$this->ref_facture_source;
802 $link = '<a href="'.DOL_URL_ROOT.$link.'?facid='.$facid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
803 $linkend = '</a>';
804 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
805 $picto = 'bill';
806 }
807 if ($option == 'discount') {
808 $label = $langs->trans("Discount");
809 $link = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->socid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
810 $linkend = '</a>';
811 $ref = $langs->trans("Discount");
812 $picto = 'generic';
813 }
814
815
816 if ($withpicto) {
817 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
818 }
819 if ($withpicto && $withpicto != 2) {
820 $result .= ' ';
821 }
822 $result .= $link.$ref.$linkend;
823 return $result;
824 }
825
826
834 public function initAsSpecimen()
835 {
836 $this->socid = 1;
837 $this->amount_ht = 10;
838 $this->amount_tva = 1.96;
839 $this->amount_ttc = 11.96;
840 $this->tva_tx = 19.6;
841 $this->description = 'Specimen discount';
842
843 return 1;
844 }
845}
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