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
189 public function __construct($db)
190 {
191 $this->db = $db;
192 }
193
194
203 public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
204 {
205 // Check parameters
206 if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
207 $this->error = 'ErrorBadParameters';
208 return -1;
209 }
210
211 $sql = "SELECT sr.rowid, sr.fk_soc, sr.discount_type,";
212 $sql .= " sr.fk_user,";
213 $sql .= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx, sr.vat_src_code,";
214 $sql .= " sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
215 $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,";
216 $sql .= " sr.datec,";
217 $sql .= " f.ref as ref_facture_source, f.type as type_facture_source,";
218 $sql .= " fsup.ref as ref_invoice_supplier_source, fsup.type as type_invoice_supplier_source";
219 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as sr";
220 $sql .= " LEFT JOIN ".$this->db->prefix()."facture as f ON sr.fk_facture_source = f.rowid";
221 $sql .= " LEFT JOIN ".$this->db->prefix()."facture_fourn as fsup ON sr.fk_invoice_supplier_source = fsup.rowid";
222 $sql .= " WHERE sr.entity IN (".getEntity('invoice').")";
223 if ($rowid) {
224 $sql .= " AND sr.rowid = ".((int) $rowid);
225 }
226 if ($fk_facture_source) {
227 $sql .= " AND sr.fk_facture_source = ".((int) $fk_facture_source);
228 }
229 if ($fk_invoice_supplier_source) {
230 $sql .= " AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
231 }
232
233 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
234 $resql = $this->db->query($sql);
235 if ($resql) {
236 if ($this->db->num_rows($resql)) {
237 $obj = $this->db->fetch_object($resql);
238
239 $this->id = $obj->rowid;
240 $this->fk_soc = $obj->fk_soc;
241 $this->socid = $obj->fk_soc;
242 $this->discount_type = $obj->discount_type;
243
244 $this->total_ht = $obj->amount_ht;
245 $this->total_tva = $obj->amount_tva;
246 $this->total_ttc = $obj->amount_ttc;
247 // For backward compatibility
248 $this->amount_ht = $this->total_ht;
249 $this->amount_tva = $this->total_tva;
250 $this->amount_ttc = $this->total_ttc;
251
252 $this->multicurrency_total_ht = $this->multicurrency_subprice = $obj->multicurrency_amount_ht;
253 $this->multicurrency_total_tva = $obj->multicurrency_amount_tva;
254 $this->multicurrency_total_ttc = $obj->multicurrency_amount_ttc;
255 // For backward compatibility
256 $this->multicurrency_amount_ht = $this->multicurrency_total_ht;
257 $this->multicurrency_amount_tva = $this->multicurrency_total_tva;
258 $this->multicurrency_amount_ttc = $this->multicurrency_total_ttc;
259
260 $this->tva_tx = $obj->tva_tx;
261 $this->vat_src_code = $obj->vat_src_code;
262
263 $this->fk_user = $obj->fk_user;
264 $this->fk_facture_line = $obj->fk_facture_line;
265 $this->fk_facture = $obj->fk_facture;
266 $this->fk_facture_source = $obj->fk_facture_source; // Id credit note or deposit source
267 $this->ref_facture_source = $obj->ref_facture_source; // Ref credit note or deposit source
268 $this->type_facture_source = $obj->type_facture_source; // Type credit note or deposit source
269 $this->fk_invoice_supplier_line = $obj->fk_invoice_supplier_line;
270 $this->fk_invoice_supplier = $obj->fk_invoice_supplier;
271 $this->fk_invoice_supplier_source = $obj->fk_invoice_supplier_source; // Id credit note or deposit source
272 $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source; // Ref credit note or deposit source
273 $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source; // Type credit note or deposit source
274 $this->description = $obj->description;
275 $this->datec = $this->db->jdate($obj->datec);
276
277 $this->db->free($resql);
278 return 1;
279 } else {
280 $this->db->free($resql);
281 return 0;
282 }
283 } else {
284 $this->error = $this->db->error();
285 return -1;
286 }
287 }
288
289
296 public function create($user)
297 {
298 global $conf;
299
300 // Clean parameters
301 $this->amount_ht = price2num($this->amount_ht);
302 $this->amount_tva = price2num($this->amount_tva);
303 $this->amount_ttc = price2num($this->amount_ttc);
304
305 $this->tva_tx = price2num($this->tva_tx);
306
307 $this->multicurrency_amount_ht = price2num($this->multicurrency_amount_ht);
308 $this->multicurrency_amount_tva = price2num($this->multicurrency_amount_tva);
309 $this->multicurrency_amount_ttc = price2num($this->multicurrency_amount_ttc);
310
311 if (empty($this->multicurrency_amount_ht)) {
312 $this->multicurrency_amount_ht = 0;
313 }
314 if (empty($this->multicurrency_amount_tva)) {
315 $this->multicurrency_amount_tva = 0;
316 }
317 if (empty($this->multicurrency_amount_ttc)) {
318 $this->multicurrency_amount_ttc = 0;
319 }
320 if (empty($this->tva_tx)) {
321 $this->tva_tx = 0;
322 }
323
324 // Check parameters
325 if (empty($this->description)) {
326 $this->error = 'BadValueForPropertyDescriptionOfDiscount';
327 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
328 return -1;
329 }
330
331 $userid = $user->id;
332 if (!($userid > 0)) { // For example when record is saved into an anonymous context with a not loaded object $user.
333 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
334 $tmpinvoice = new Facture($this->db);
335 $tmpinvoice->fetch($this->fk_facture_source);
336 $userid = $tmpinvoice->fk_user_author; // We use the author of invoice
337 }
338
339 // Insert request
340 $sql = "INSERT INTO ".$this->db->prefix()."societe_remise_except";
341 $sql .= " (entity, datec, fk_soc, discount_type, fk_user, description,";
342 $sql .= " amount_ht, amount_tva, amount_ttc, tva_tx, vat_src_code,";
343 $sql .= " multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,";
344 $sql .= " fk_facture_source, fk_invoice_supplier_source";
345 $sql .= ")";
346 $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)."',";
347 $sql .= " ".price2num($this->amount_ht).", ".price2num($this->amount_tva).", ".price2num($this->amount_ttc).", ".price2num($this->tva_tx).", '".$this->db->escape($this->vat_src_code)."',";
348 $sql .= " ".price2num($this->multicurrency_amount_ht).", ".price2num($this->multicurrency_amount_tva).", ".price2num($this->multicurrency_amount_ttc).", ";
349 $sql .= " ".($this->fk_facture_source ? ((int) $this->fk_facture_source) : "null").",";
350 $sql .= " ".($this->fk_invoice_supplier_source ? ((int) $this->fk_invoice_supplier_source) : "null");
351 $sql .= ")";
352
353 dol_syslog(get_class($this)."::create", LOG_DEBUG);
354 $resql = $this->db->query($sql);
355 if ($resql) {
356 $this->id = $this->db->last_insert_id($this->db->prefix()."societe_remise_except");
357 return $this->id;
358 } else {
359 $this->error = $this->db->lasterror().' - sql='.$sql;
360 return -1;
361 }
362 }
363
364
371 public function delete($user)
372 {
373 global $conf, $langs;
374
375 // Check if we can remove the discount
376 if ($this->fk_facture_source) {
377 $sql = "SELECT COUNT(rowid) as nb";
378 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
379 $sql .= " WHERE (fk_facture_line IS NOT NULL"; // Not used as absolute simple discount
380 $sql .= " OR fk_facture IS NOT NULL)"; // Not used as credit note and not used as deposit
381 $sql .= " AND fk_facture_source = ".((int) $this->fk_facture_source);
382 //$sql.=" AND rowid != ".$this->id;
383
384 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
385 $resql = $this->db->query($sql);
386 if ($resql) {
387 $obj = $this->db->fetch_object($resql);
388 if ($obj->nb > 0) {
389 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
390 return -2;
391 }
392 } else {
393 dol_print_error($this->db);
394 return -1;
395 }
396 }
397
398 // Check if we can remove the discount
399 if ($this->fk_invoice_supplier_source) {
400 $sql = "SELECT COUNT(rowid) as nb";
401 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
402 $sql .= " WHERE (fk_invoice_supplier_line IS NOT NULL"; // Not used as absolute simple discount
403 $sql .= " OR fk_invoice_supplier IS NOT NULL)"; // Not used as credit note and not used as deposit
404 $sql .= " AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
405 //$sql.=" AND rowid != ".$this->id;
406
407 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
408 $resql = $this->db->query($sql);
409 if ($resql) {
410 $obj = $this->db->fetch_object($resql);
411 if ($obj->nb > 0) {
412 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
413 return -2;
414 }
415 } else {
416 dol_print_error($this->db);
417 return -1;
418 }
419 }
420
421 $this->db->begin();
422
423 // Delete but only if not used
424 $sql = "DELETE FROM ".$this->db->prefix()."societe_remise_except ";
425 if ($this->fk_facture_source) {
426 $sql .= " WHERE fk_facture_source = ".((int) $this->fk_facture_source); // Delete all lines of same series
427 } elseif ($this->fk_invoice_supplier_source) {
428 $sql .= " WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source); // Delete all lines of same series
429 } else {
430 $sql .= " WHERE rowid = ".((int) $this->id); // Delete only line
431 }
432 $sql .= " AND (fk_facture_line IS NULL"; // Not used as absolute simple discount
433 $sql .= " AND fk_facture IS NULL)"; // Not used as credit note and not used as deposit
434 $sql .= " AND (fk_invoice_supplier_line IS NULL"; // Not used as absolute simple discount
435 $sql .= " AND fk_invoice_supplier IS NULL)"; // Not used as credit note and not used as deposit
436
437 dol_syslog(get_class($this)."::delete Delete discount", LOG_DEBUG);
438
439 require_once DOL_DOCUMENT_ROOT. '/core/class/commoninvoice.class.php';
440 $result = $this->db->query($sql);
441 if ($result) {
442 // If source of discount was a credit note or deposit, we change source statut.
443 if ($this->fk_facture_source) {
444 $sql = "UPDATE ".$this->db->prefix()."facture";
445 $sql .= " set paye=0, fk_statut=1";
446 $sql .= " WHERE type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_facture_source);
447
448 dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
449 $result = $this->db->query($sql);
450 if ($result) {
451 $this->db->commit();
452 return 1;
453 } else {
454 $this->error = $this->db->lasterror();
455 $this->db->rollback();
456 return -1;
457 }
458 } elseif ($this->fk_invoice_supplier_source) {
459 $sql = "UPDATE ".$this->db->prefix()."facture_fourn";
460 $sql .= " set paye=0, fk_statut=1";
461 $sql .= " WHERE type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_invoice_supplier_source);
462
463 dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
464 $result = $this->db->query($sql);
465 if ($result) {
466 $this->db->commit();
467 return 1;
468 } else {
469 $this->error = $this->db->lasterror();
470 $this->db->rollback();
471 return -1;
472 }
473 } else {
474 $this->db->commit();
475 return 1;
476 }
477 } else {
478 $this->error = $this->db->lasterror();
479 $this->db->rollback();
480 return -1;
481 }
482 }
483
484
485
486 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
497 public function link_to_invoice($rowidline, $rowidinvoice, $notrigger = 0)
498 {
499 // phpcs:enable
500 global $user;
501
502 // Check parameters
503 if (!$rowidline && !$rowidinvoice) {
504 $this->error = 'ErrorBadParameters';
505 return -1;
506 }
507 if ($rowidline && $rowidinvoice) {
508 $this->error = 'ErrorBadParameters';
509 return -2;
510 }
511
512 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
513 if (!empty($this->discount_type)) {
514 if ($rowidline) {
515 $sql .= " SET fk_invoice_supplier_line = ".((int) $rowidline);
516 }
517 if ($rowidinvoice) {
518 $sql .= " SET fk_invoice_supplier = ".((int) $rowidinvoice);
519 }
520 } else {
521 if ($rowidline) {
522 $sql .= " SET fk_facture_line = ".((int) $rowidline);
523 }
524 if ($rowidinvoice) {
525 $sql .= " SET fk_facture = ".((int) $rowidinvoice);
526 }
527 }
528 $sql .= " WHERE rowid = ".((int) $this->id);
529
530 dol_syslog(get_class($this)."::link_to_invoice", LOG_DEBUG);
531 $resql = $this->db->query($sql);
532 if ($resql) {
533 if (!empty($this->discount_type)) {
534 $this->fk_invoice_supplier_line = $rowidline;
535 $this->fk_invoice_supplier = $rowidinvoice;
536 } else {
537 $this->fk_facture_line = $rowidline;
538 $this->fk_facture = $rowidinvoice;
539 }
540 if (!$notrigger) {
541 // Call trigger
542 $result = $this->call_trigger('DISCOUNT_MODIFY', $user);
543 if ($result < 0) {
544 return -2;
545 }
546 // End call triggers
547 }
548 return 1;
549 } else {
550 $this->error = $this->db->error();
551 return -3;
552 }
553 }
554
555
556 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
564 public function unlink_invoice($notrigger = 0)
565 {
566 // phpcs:enable
567 global $user;
568
569 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
570 if (!empty($this->discount_type)) {
571 $sql .= " SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
572 } else {
573 $sql .= " SET fk_facture_line = NULL, fk_facture = NULL";
574 }
575 $sql .= " WHERE rowid = ".((int) $this->id);
576
577 dol_syslog(get_class($this)."::unlink_invoice", LOG_DEBUG);
578 $resql = $this->db->query($sql);
579 if ($resql) {
580 if (!$notrigger) {
581 // Call trigger
582 $result = $this->call_trigger('DISCOUNT_MODIFY', $user);
583 if ($result < 0) {
584 return -2;
585 }
586 // End call triggers
587 }
588 return 1;
589 } else {
590 $this->error = $this->db->error();
591 return -3;
592 }
593 }
594
595
607 public function getAvailableDiscounts($company = null, $user = null, $filter = '', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
608 {
609 global $conf;
610
611 dol_syslog(get_class($this)."::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
612
613 $sql = "SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
614 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
615 $sql .= " WHERE rc.entity = ".$conf->entity;
616 $sql .= " AND rc.discount_type=".((int) $discount_type);
617 if (!empty($discount_type)) {
618 $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)"; // Available from supplier
619 } else {
620 $sql .= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available to customer
621 }
622 if (is_object($company)) {
623 $sql .= " AND rc.fk_soc = ".((int) $company->id);
624 }
625 if (is_object($user)) {
626 $sql .= " AND rc.fk_user = ".((int) $user->id);
627 }
628 if ($filter) {
629 $sql .= " AND (".$filter.")";
630 }
631 if ($maxvalue) {
632 $sql .= ' AND rc.amount_ttc <= '.((float) price2num($maxvalue));
633 }
634
635 $resql = $this->db->query($sql);
636 if ($resql) {
637 $obj = $this->db->fetch_object($resql);
638 //while ($obj)
639 //{
640 //print 'zz'.$obj->amount;
641 //$obj = $this->db->fetch_object($resql);
642 //}
643 if ($multicurrency) {
644 return $obj->multicurrency_amount;
645 }
646
647 return $obj->amount;
648 }
649 return -1;
650 }
651
652
661 public function getSumDepositsUsed($invoice, $multicurrency = 0)
662 {
663 dol_syslog(get_class($this)."::getSumDepositsUsed", LOG_DEBUG);
664
665 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
666 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
667 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
668 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
669 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
670 } elseif ($invoice->element == 'invoice_supplier') {
671 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
672 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
673 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
674 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
675 } else {
676 $this->error = get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
677 dol_print_error($this->db, $this->error);
678 return -1;
679 }
680
681 $resql = $this->db->query($sql);
682 if ($resql) {
683 $obj = $this->db->fetch_object($resql);
684 if ($multicurrency == 1) {
685 return $obj->multicurrency_amount;
686 } else {
687 return $obj->amount;
688 }
689 } else {
690 $this->error = $this->db->lasterror();
691 return -1;
692 }
693 }
694
702 public function getSumCreditNotesUsed($invoice, $multicurrency = 0)
703 {
704 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
705
706 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
707 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
708 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
709 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
710 $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
711 } elseif ($invoice->element == 'invoice_supplier') {
712 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
713 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
714 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
715 $sql .= " AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.", ".$invoice::TYPE_CREDIT_NOTE).")"; // Find discount coming from credit note or excess paid
716 } else {
717 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
718 dol_print_error($this->db, $this->error);
719 return -1;
720 }
721
722 $resql = $this->db->query($sql);
723 if ($resql) {
724 $obj = $this->db->fetch_object($resql);
725 if ($multicurrency == 1) {
726 return $obj->multicurrency_amount;
727 } else {
728 return $obj->amount;
729 }
730 } else {
731 $this->error = $this->db->lasterror();
732 return -1;
733 }
734 }
742 public function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency = 0)
743 {
744 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
745
746 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
747 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
748 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
749 $sql .= " WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
750 } elseif ($invoice->element == 'invoice_supplier') {
751 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
752 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
753 $sql .= " WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
754 } else {
755 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
756 dol_print_error($this->db, $this->error);
757 return -1;
758 }
759
760 $resql = $this->db->query($sql);
761 if ($resql) {
762 $obj = $this->db->fetch_object($resql);
763 if ($multicurrency) {
764 return $obj->multicurrency_amount;
765 } else {
766 return $obj->amount;
767 }
768 } else {
769 $this->error = $this->db->lasterror();
770 return -1;
771 }
772 }
773
781 public function getNomUrl($withpicto = 0, $option = 'invoice')
782 {
783 global $langs;
784
785 $result = '';
786 $link = '';
787 $linkend = '';
788 $label = '';
789 $picto = '';
790 $ref = '';
791
792 if ($option == 'invoice') {
793 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
794 $link = !empty($this->discount_type) ? '/fourn/facture/card.php' : '/compta/facture/card.php';
795 $label = $langs->trans("ShowSourceInvoice").': '.$this->ref_facture_source;
796 $link = '<a href="'.DOL_URL_ROOT.$link.'?facid='.$facid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
797 $linkend = '</a>';
798 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
799 $picto = 'bill';
800 }
801 if ($option == 'discount') {
802 $label = $langs->trans("Discount");
803 $link = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->socid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
804 $linkend = '</a>';
805 $ref = $langs->trans("Discount");
806 $picto = 'generic';
807 }
808
809
810 if ($withpicto) {
811 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
812 }
813 if ($withpicto && $withpicto != 2) {
814 $result .= ' ';
815 }
816 $result .= $link.$ref.$linkend;
817 return $result;
818 }
819
820
828 public function initAsSpecimen()
829 {
830 $this->socid = 1;
831 $this->amount_ht = 10;
832 $this->amount_tva = 1.96;
833 $this->amount_ttc = 11.96;
834 $this->tva_tx = 19.6;
835 $this->description = 'Specimen discount';
836
837 return 1;
838 }
839}
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