dolibarr 23.0.3
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 // 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, $hookmanager;
616
617 dol_syslog(get_class($this)."::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
618
619 $parameters = array(
620 'company' => $company,
621 'user' => $user,
622 'filter' => $filter,
623 'maxvalue' => $maxvalue,
624 'discount_type' => $discount_type,
625 'multicurrency' => $multicurrency
626 );
627
628 $reshook = $hookmanager->executeHooks('getAvailableDiscounts', $parameters);
629 if (empty($reshook)) {
630 $sql = "SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
631 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
632 $sql .= " WHERE rc.entity = ".$conf->entity;
633 $sql .= " AND rc.discount_type=".((int) $discount_type);
634 if (!empty($discount_type)) {
635 $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)"; // Available from supplier
636 } else {
637 $sql .= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available to customer
638 }
639 if (is_object($company)) {
640 $sql .= " AND rc.fk_soc = ".((int) $company->id);
641 }
642 if (is_object($user)) {
643 $sql .= " AND rc.fk_user = ".((int) $user->id);
644 }
645 if ($filter) {
646 $sql .= " AND (".$filter.")";
647 }
648 if ($maxvalue) {
649 $sql .= ' AND rc.amount_ttc <= '.((float) price2num($maxvalue));
650 }
651 } else {
652 $sql = $hookmanager->resArray['sql'];
653 }
654
655 $resql = $this->db->query($sql);
656 if ($resql) {
657 $obj = $this->db->fetch_object($resql);
658 //while ($obj)
659 //{
660 //print 'zz'.$obj->amount;
661 //$obj = $this->db->fetch_object($resql);
662 //}
663 if ($multicurrency) {
664 return $obj->multicurrency_amount;
665 }
666
667 return $obj->amount;
668 }
669 return -1;
670 }
671
680 public function getSumDepositsUsed($invoice, $multicurrency = 0)
681 {
682 dol_syslog(get_class($this)."::getSumDepositsUsed", LOG_DEBUG);
683
684 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
685 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
686 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
687 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
688 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
689 } elseif ($invoice->element == 'invoice_supplier') {
690 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
691 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
692 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
693 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
694 } else {
695 $this->error = get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
696 dol_print_error($this->db, $this->error);
697 return -1;
698 }
699
700 $resql = $this->db->query($sql);
701 if ($resql) {
702 $obj = $this->db->fetch_object($resql);
703 if ($multicurrency == 1) {
704 return $obj->multicurrency_amount;
705 } else {
706 return $obj->amount;
707 }
708 } else {
709 $this->error = $this->db->lasterror();
710 return -1;
711 }
712 }
713
721 public function getSumCreditNotesUsed($invoice, $multicurrency = 0)
722 {
723 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
724
725 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
726 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
727 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
728 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
729 $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
730 } elseif ($invoice->element == 'invoice_supplier') {
731 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
732 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
733 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
734 $sql .= " AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.", ".$invoice::TYPE_CREDIT_NOTE).")"; // Find discount coming from credit note or excess paid
735 } else {
736 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
737 dol_print_error($this->db, $this->error);
738 return 'ErrorBadElementType';
739 }
740
741 $resql = $this->db->query($sql);
742 if ($resql) {
743 $obj = $this->db->fetch_object($resql);
744 if ($multicurrency == 1) {
745 return $obj->multicurrency_amount;
746 } else {
747 return $obj->amount;
748 }
749 } else {
750 $this->error = $this->db->lasterror();
751 return 'ErrorBadSQLquery';
752 }
753 }
761 public function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency = 0)
762 {
763 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
764
765 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
766 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
767 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
768 $sql .= " WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
769 } elseif ($invoice->element == 'invoice_supplier') {
770 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
771 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
772 $sql .= " WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
773 } else {
774 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
775 dol_print_error($this->db, $this->error);
776 return -1;
777 }
778
779 $resql = $this->db->query($sql);
780 if ($resql) {
781 $obj = $this->db->fetch_object($resql);
782 if ($multicurrency) {
783 return $obj->multicurrency_amount;
784 } else {
785 return $obj->amount;
786 }
787 } else {
788 $this->error = $this->db->lasterror();
789 return -1;
790 }
791 }
792
800 public function getNomUrl($withpicto = 0, $option = 'invoice')
801 {
802 global $langs;
803
804 $result = '';
805 $link = '';
806 $linkend = '';
807 $label = '';
808 $picto = '';
809 $ref = '';
810
811 if ($option == 'invoice') {
812 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
813 $link = !empty($this->discount_type) ? '/fourn/facture/card.php' : '/compta/facture/card.php';
814 $label = $langs->trans("ShowSourceInvoice").': '.$this->ref_facture_source;
815 $link = '<a href="'.DOL_URL_ROOT.$link.'?facid='.$facid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
816 $linkend = '</a>';
817 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
818 $picto = 'bill';
819 }
820 if ($option == 'discount') {
821 $label = $langs->trans("Discount");
822 $link = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->socid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
823 $linkend = '</a>';
824 $ref = $langs->trans("Discount");
825 $picto = 'generic';
826 }
827
828
829 if ($withpicto) {
830 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
831 }
832 if ($withpicto && $withpicto != 2) {
833 $result .= ' ';
834 }
835 $result .= $link.$ref.$linkend;
836 return $result;
837 }
838
839
847 public function initAsSpecimen()
848 {
849 $this->socid = 1;
850 $this->amount_ht = 10;
851 $this->amount_tva = 1.96;
852 $this->amount_ttc = 11.96;
853 $this->tva_tx = 19.6;
854 $this->description = 'Specimen discount';
855
856 return 1;
857 }
858}
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.
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.
dol_now($mode='gmt')
Return date for now.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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...