dolibarr 21.0.0-alpha
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
51 public $total_ht;
52 public $total_tva;
53 public $total_ttc;
54 public $amount_ht; // deprecated
55 public $amount_tva; // deprecated
56 public $amount_ttc; // deprecated
57
58 public $multicurrency_total_ht;
59 public $multicurrency_total_tva;
60 public $multicurrency_total_ttc;
61 public $multicurrency_amount_ht; // deprecated
62 public $multicurrency_amount_tva; // deprecated
63 public $multicurrency_amount_ttc; // deprecated
64
68 public $multicurrency_subprice;
69
73 public $fk_invoice_supplier;
74
78 public $fk_invoice_supplier_line;
79
80 // Vat rate
81 public $tva_tx;
82 public $vat_src_code;
83
87 public $fk_user;
88
92 public $description;
93
99 public $datec;
100
104 public $fk_facture_line;
105
109 public $fk_facture;
110
114 public $fk_facture_source;
115 public $ref_facture_source; // Ref credit note or deposit used to create the discount
116 public $type_facture_source;
117
118 public $fk_invoice_supplier_source;
119 public $ref_invoice_supplier_source; // Ref credit note or deposit used to create the discount
120 public $type_invoice_supplier_source;
121
127 public function __construct($db)
128 {
129 $this->db = $db;
130 }
131
132
141 public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
142 {
143 // Check parameters
144 if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
145 $this->error = 'ErrorBadParameters';
146 return -1;
147 }
148
149 $sql = "SELECT sr.rowid, sr.fk_soc, sr.discount_type,";
150 $sql .= " sr.fk_user,";
151 $sql .= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx, sr.vat_src_code,";
152 $sql .= " sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
153 $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,";
154 $sql .= " sr.datec,";
155 $sql .= " f.ref as ref_facture_source, f.type as type_facture_source,";
156 $sql .= " fsup.ref as ref_invoice_supplier_source, fsup.type as type_invoice_supplier_source";
157 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as sr";
158 $sql .= " LEFT JOIN ".$this->db->prefix()."facture as f ON sr.fk_facture_source = f.rowid";
159 $sql .= " LEFT JOIN ".$this->db->prefix()."facture_fourn as fsup ON sr.fk_invoice_supplier_source = fsup.rowid";
160 $sql .= " WHERE sr.entity IN (".getEntity('invoice').")";
161 if ($rowid) {
162 $sql .= " AND sr.rowid = ".((int) $rowid);
163 }
164 if ($fk_facture_source) {
165 $sql .= " AND sr.fk_facture_source = ".((int) $fk_facture_source);
166 }
167 if ($fk_invoice_supplier_source) {
168 $sql .= " AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
169 }
170
171 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
172 $resql = $this->db->query($sql);
173 if ($resql) {
174 if ($this->db->num_rows($resql)) {
175 $obj = $this->db->fetch_object($resql);
176
177 $this->id = $obj->rowid;
178 $this->fk_soc = $obj->fk_soc;
179 $this->socid = $obj->fk_soc;
180 $this->discount_type = $obj->discount_type;
181
182 $this->total_ht = $obj->amount_ht;
183 $this->total_tva = $obj->amount_tva;
184 $this->total_ttc = $obj->amount_ttc;
185 // For backward compatibility
186 $this->amount_ht = $this->total_ht;
187 $this->amount_tva = $this->total_tva;
188 $this->amount_ttc = $this->total_ttc;
189
190 $this->multicurrency_total_ht = $this->multicurrency_subprice = $obj->multicurrency_amount_ht;
191 $this->multicurrency_total_tva = $obj->multicurrency_amount_tva;
192 $this->multicurrency_total_ttc = $obj->multicurrency_amount_ttc;
193 // For backward compatibility
194 $this->multicurrency_amount_ht = $this->multicurrency_total_ht;
195 $this->multicurrency_amount_tva = $this->multicurrency_total_tva;
196 $this->multicurrency_amount_ttc = $this->multicurrency_total_ttc;
197
198 $this->tva_tx = $obj->tva_tx;
199 $this->vat_src_code = $obj->vat_src_code;
200
201 $this->fk_user = $obj->fk_user;
202 $this->fk_facture_line = $obj->fk_facture_line;
203 $this->fk_facture = $obj->fk_facture;
204 $this->fk_facture_source = $obj->fk_facture_source; // Id credit note or deposit source
205 $this->ref_facture_source = $obj->ref_facture_source; // Ref credit note or deposit source
206 $this->type_facture_source = $obj->type_facture_source; // Type credit note or deposit source
207 $this->fk_invoice_supplier_line = $obj->fk_invoice_supplier_line;
208 $this->fk_invoice_supplier = $obj->fk_invoice_supplier;
209 $this->fk_invoice_supplier_source = $obj->fk_invoice_supplier_source; // Id credit note or deposit source
210 $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source; // Ref credit note or deposit source
211 $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source; // Type credit note or deposit source
212 $this->description = $obj->description;
213 $this->datec = $this->db->jdate($obj->datec);
214
215 $this->db->free($resql);
216 return 1;
217 } else {
218 $this->db->free($resql);
219 return 0;
220 }
221 } else {
222 $this->error = $this->db->error();
223 return -1;
224 }
225 }
226
227
234 public function create($user)
235 {
236 global $conf;
237
238 // Clean parameters
239 $this->amount_ht = price2num($this->amount_ht);
240 $this->amount_tva = price2num($this->amount_tva);
241 $this->amount_ttc = price2num($this->amount_ttc);
242
243 $this->tva_tx = price2num($this->tva_tx);
244
245 $this->multicurrency_amount_ht = price2num($this->multicurrency_amount_ht);
246 $this->multicurrency_amount_tva = price2num($this->multicurrency_amount_tva);
247 $this->multicurrency_amount_ttc = price2num($this->multicurrency_amount_ttc);
248
249 if (empty($this->multicurrency_amount_ht)) {
250 $this->multicurrency_amount_ht = 0;
251 }
252 if (empty($this->multicurrency_amount_tva)) {
253 $this->multicurrency_amount_tva = 0;
254 }
255 if (empty($this->multicurrency_amount_ttc)) {
256 $this->multicurrency_amount_ttc = 0;
257 }
258 if (empty($this->tva_tx)) {
259 $this->tva_tx = 0;
260 }
261
262 // Check parameters
263 if (empty($this->description)) {
264 $this->error = 'BadValueForPropertyDescriptionOfDiscount';
265 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
266 return -1;
267 }
268
269 $userid = $user->id;
270 if (!($userid > 0)) { // For example when record is saved into an anonymous context with a not loaded object $user.
271 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
272 $tmpinvoice = new Facture($this->db);
273 $tmpinvoice->fetch($this->fk_facture_source);
274 $userid = $tmpinvoice->fk_user_author; // We use the author of invoice
275 }
276
277 // Insert request
278 $sql = "INSERT INTO ".$this->db->prefix()."societe_remise_except";
279 $sql .= " (entity, datec, fk_soc, discount_type, fk_user, description,";
280 $sql .= " amount_ht, amount_tva, amount_ttc, tva_tx, vat_src_code,";
281 $sql .= " multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,";
282 $sql .= " fk_facture_source, fk_invoice_supplier_source";
283 $sql .= ")";
284 $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)."',";
285 $sql .= " ".price2num($this->amount_ht).", ".price2num($this->amount_tva).", ".price2num($this->amount_ttc).", ".price2num($this->tva_tx).", '".$this->db->escape($this->vat_src_code)."',";
286 $sql .= " ".price2num($this->multicurrency_amount_ht).", ".price2num($this->multicurrency_amount_tva).", ".price2num($this->multicurrency_amount_ttc).", ";
287 $sql .= " ".($this->fk_facture_source ? ((int) $this->fk_facture_source) : "null").",";
288 $sql .= " ".($this->fk_invoice_supplier_source ? ((int) $this->fk_invoice_supplier_source) : "null");
289 $sql .= ")";
290
291 dol_syslog(get_class($this)."::create", LOG_DEBUG);
292 $resql = $this->db->query($sql);
293 if ($resql) {
294 $this->id = $this->db->last_insert_id($this->db->prefix()."societe_remise_except");
295 return $this->id;
296 } else {
297 $this->error = $this->db->lasterror().' - sql='.$sql;
298 return -1;
299 }
300 }
301
302
309 public function delete($user)
310 {
311 global $conf, $langs;
312
313 // Check if we can remove the discount
314 if ($this->fk_facture_source) {
315 $sql = "SELECT COUNT(rowid) as nb";
316 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
317 $sql .= " WHERE (fk_facture_line IS NOT NULL"; // Not used as absolute simple discount
318 $sql .= " OR fk_facture IS NOT NULL)"; // Not used as credit note and not used as deposit
319 $sql .= " AND fk_facture_source = ".((int) $this->fk_facture_source);
320 //$sql.=" AND rowid != ".$this->id;
321
322 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
323 $resql = $this->db->query($sql);
324 if ($resql) {
325 $obj = $this->db->fetch_object($resql);
326 if ($obj->nb > 0) {
327 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
328 return -2;
329 }
330 } else {
331 dol_print_error($this->db);
332 return -1;
333 }
334 }
335
336 // Check if we can remove the discount
337 if ($this->fk_invoice_supplier_source) {
338 $sql = "SELECT COUNT(rowid) as nb";
339 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
340 $sql .= " WHERE (fk_invoice_supplier_line IS NOT NULL"; // Not used as absolute simple discount
341 $sql .= " OR fk_invoice_supplier IS NOT NULL)"; // Not used as credit note and not used as deposit
342 $sql .= " AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
343 //$sql.=" AND rowid != ".$this->id;
344
345 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
346 $resql = $this->db->query($sql);
347 if ($resql) {
348 $obj = $this->db->fetch_object($resql);
349 if ($obj->nb > 0) {
350 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
351 return -2;
352 }
353 } else {
354 dol_print_error($this->db);
355 return -1;
356 }
357 }
358
359 $this->db->begin();
360
361 // Delete but only if not used
362 $sql = "DELETE FROM ".$this->db->prefix()."societe_remise_except ";
363 if ($this->fk_facture_source) {
364 $sql .= " WHERE fk_facture_source = ".((int) $this->fk_facture_source); // Delete all lines of same series
365 } elseif ($this->fk_invoice_supplier_source) {
366 $sql .= " WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source); // Delete all lines of same series
367 } else {
368 $sql .= " WHERE rowid = ".((int) $this->id); // Delete only line
369 }
370 $sql .= " AND (fk_facture_line IS NULL"; // Not used as absolute simple discount
371 $sql .= " AND fk_facture IS NULL)"; // Not used as credit note and not used as deposit
372 $sql .= " AND (fk_invoice_supplier_line IS NULL"; // Not used as absolute simple discount
373 $sql .= " AND fk_invoice_supplier IS NULL)"; // Not used as credit note and not used as deposit
374
375 dol_syslog(get_class($this)."::delete Delete discount", LOG_DEBUG);
376
377 require_once DOL_DOCUMENT_ROOT. '/core/class/commoninvoice.class.php';
378 $result = $this->db->query($sql);
379 if ($result) {
380 // If source of discount was a credit note or deposit, we change source statut.
381 if ($this->fk_facture_source) {
382 $sql = "UPDATE ".$this->db->prefix()."facture";
383 $sql .= " set paye=0, fk_statut=1";
384 $sql .= " WHERE type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_facture_source);
385
386 dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
387 $result = $this->db->query($sql);
388 if ($result) {
389 $this->db->commit();
390 return 1;
391 } else {
392 $this->error = $this->db->lasterror();
393 $this->db->rollback();
394 return -1;
395 }
396 } elseif ($this->fk_invoice_supplier_source) {
397 $sql = "UPDATE ".$this->db->prefix()."facture_fourn";
398 $sql .= " set paye=0, fk_statut=1";
399 $sql .= " WHERE type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_invoice_supplier_source);
400
401 dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
402 $result = $this->db->query($sql);
403 if ($result) {
404 $this->db->commit();
405 return 1;
406 } else {
407 $this->error = $this->db->lasterror();
408 $this->db->rollback();
409 return -1;
410 }
411 } else {
412 $this->db->commit();
413 return 1;
414 }
415 } else {
416 $this->error = $this->db->lasterror();
417 $this->db->rollback();
418 return -1;
419 }
420 }
421
422
423
424 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
435 public function link_to_invoice($rowidline, $rowidinvoice, $notrigger = 0)
436 {
437 // phpcs:enable
438 global $user;
439
440 // Check parameters
441 if (!$rowidline && !$rowidinvoice) {
442 $this->error = 'ErrorBadParameters';
443 return -1;
444 }
445 if ($rowidline && $rowidinvoice) {
446 $this->error = 'ErrorBadParameters';
447 return -2;
448 }
449
450 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
451 if (!empty($this->discount_type)) {
452 if ($rowidline) {
453 $sql .= " SET fk_invoice_supplier_line = ".((int) $rowidline);
454 }
455 if ($rowidinvoice) {
456 $sql .= " SET fk_invoice_supplier = ".((int) $rowidinvoice);
457 }
458 } else {
459 if ($rowidline) {
460 $sql .= " SET fk_facture_line = ".((int) $rowidline);
461 }
462 if ($rowidinvoice) {
463 $sql .= " SET fk_facture = ".((int) $rowidinvoice);
464 }
465 }
466 $sql .= " WHERE rowid = ".((int) $this->id);
467
468 dol_syslog(get_class($this)."::link_to_invoice", LOG_DEBUG);
469 $resql = $this->db->query($sql);
470 if ($resql) {
471 if (!empty($this->discount_type)) {
472 $this->fk_invoice_supplier_line = $rowidline;
473 $this->fk_invoice_supplier = $rowidinvoice;
474 } else {
475 $this->fk_facture_line = $rowidline;
476 $this->fk_facture = $rowidinvoice;
477 }
478 if (!$notrigger) {
479 // Call trigger
480 $result = $this->call_trigger('DISCOUNT_MODIFY', $user);
481 if ($result < 0) {
482 return -2;
483 }
484 // End call triggers
485 }
486 return 1;
487 } else {
488 $this->error = $this->db->error();
489 return -3;
490 }
491 }
492
493
494 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
502 public function unlink_invoice($notrigger = 0)
503 {
504 // phpcs:enable
505 global $user;
506
507 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
508 if (!empty($this->discount_type)) {
509 $sql .= " SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
510 } else {
511 $sql .= " SET fk_facture_line = NULL, fk_facture = NULL";
512 }
513 $sql .= " WHERE rowid = ".((int) $this->id);
514
515 dol_syslog(get_class($this)."::unlink_invoice", LOG_DEBUG);
516 $resql = $this->db->query($sql);
517 if ($resql) {
518 if (!$notrigger) {
519 // Call trigger
520 $result = $this->call_trigger('DISCOUNT_MODIFY', $user);
521 if ($result < 0) {
522 return -2;
523 }
524 // End call triggers
525 }
526 return 1;
527 } else {
528 $this->error = $this->db->error();
529 return -3;
530 }
531 }
532
533
545 public function getAvailableDiscounts($company = null, $user = null, $filter = '', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
546 {
547 global $conf;
548
549 dol_syslog(get_class($this)."::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
550
551 $sql = "SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
552 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
553 $sql .= " WHERE rc.entity = ".$conf->entity;
554 $sql .= " AND rc.discount_type=".((int) $discount_type);
555 if (!empty($discount_type)) {
556 $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)"; // Available from supplier
557 } else {
558 $sql .= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available to customer
559 }
560 if (is_object($company)) {
561 $sql .= " AND rc.fk_soc = ".((int) $company->id);
562 }
563 if (is_object($user)) {
564 $sql .= " AND rc.fk_user = ".((int) $user->id);
565 }
566 if ($filter) {
567 $sql .= " AND (".$filter.")";
568 }
569 if ($maxvalue) {
570 $sql .= ' AND rc.amount_ttc <= '.((float) price2num($maxvalue));
571 }
572
573 $resql = $this->db->query($sql);
574 if ($resql) {
575 $obj = $this->db->fetch_object($resql);
576 //while ($obj)
577 //{
578 //print 'zz'.$obj->amount;
579 //$obj = $this->db->fetch_object($resql);
580 //}
581 if ($multicurrency) {
582 return $obj->multicurrency_amount;
583 }
584
585 return $obj->amount;
586 }
587 return -1;
588 }
589
590
599 public function getSumDepositsUsed($invoice, $multicurrency = 0)
600 {
601 dol_syslog(get_class($this)."::getSumDepositsUsed", LOG_DEBUG);
602
603 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
604 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
605 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
606 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
607 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
608 } elseif ($invoice->element == 'invoice_supplier') {
609 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
610 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
611 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
612 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
613 } else {
614 $this->error = get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
615 dol_print_error($this->db, $this->error);
616 return -1;
617 }
618
619 $resql = $this->db->query($sql);
620 if ($resql) {
621 $obj = $this->db->fetch_object($resql);
622 if ($multicurrency == 1) {
623 return $obj->multicurrency_amount;
624 } else {
625 return $obj->amount;
626 }
627 } else {
628 $this->error = $this->db->lasterror();
629 return -1;
630 }
631 }
632
640 public function getSumCreditNotesUsed($invoice, $multicurrency = 0)
641 {
642 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
643
644 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
645 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
646 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
647 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
648 $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
649 } elseif ($invoice->element == 'invoice_supplier') {
650 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
651 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
652 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
653 $sql .= " AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.", ".$invoice::TYPE_CREDIT_NOTE).")"; // Find discount coming from credit note or excess paid
654 } else {
655 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
656 dol_print_error($this->db, $this->error);
657 return -1;
658 }
659
660 $resql = $this->db->query($sql);
661 if ($resql) {
662 $obj = $this->db->fetch_object($resql);
663 if ($multicurrency == 1) {
664 return $obj->multicurrency_amount;
665 } else {
666 return $obj->amount;
667 }
668 } else {
669 $this->error = $this->db->lasterror();
670 return -1;
671 }
672 }
680 public function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency = 0)
681 {
682 dol_syslog(get_class($this)."::getSumCreditNotesUsed", 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";
687 $sql .= " WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
688 } elseif ($invoice->element == 'invoice_supplier') {
689 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
690 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
691 $sql .= " WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
692 } else {
693 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
694 dol_print_error($this->db, $this->error);
695 return -1;
696 }
697
698 $resql = $this->db->query($sql);
699 if ($resql) {
700 $obj = $this->db->fetch_object($resql);
701 if ($multicurrency) {
702 return $obj->multicurrency_amount;
703 } else {
704 return $obj->amount;
705 }
706 } else {
707 $this->error = $this->db->lasterror();
708 return -1;
709 }
710 }
711
719 public function getNomUrl($withpicto, $option = 'invoice')
720 {
721 global $langs;
722
723 $result = '';
724 $link = '';
725 $linkend = '';
726 $label = '';
727 $picto = '';
728 $ref = '';
729
730 if ($option == 'invoice') {
731 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
732 $link = !empty($this->discount_type) ? '/fourn/facture/card.php' : '/compta/facture/card.php';
733 $label = $langs->trans("ShowSourceInvoice").': '.$this->ref_facture_source;
734 $link = '<a href="'.DOL_URL_ROOT.$link.'?facid='.$facid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
735 $linkend = '</a>';
736 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
737 $picto = 'bill';
738 }
739 if ($option == 'discount') {
740 $label = $langs->trans("Discount");
741 $link = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->socid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
742 $linkend = '</a>';
743 $ref = $langs->trans("Discount");
744 $picto = 'generic';
745 }
746
747
748 if ($withpicto) {
749 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
750 }
751 if ($withpicto && $withpicto != 2) {
752 $result .= ' ';
753 }
754 $result .= $link.$ref.$linkend;
755 return $result;
756 }
757
758
766 public function initAsSpecimen()
767 {
768 $this->socid = 1;
769 $this->amount_ht = 10;
770 $this->amount_tva = 1.96;
771 $this->amount_ttc = 11.96;
772 $this->tva_tx = 19.6;
773 $this->description = 'Specimen discount';
774
775 return 1;
776 }
777}
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.
create($user)
Create a discount into database.
getSumDepositsUsed($invoice, $multicurrency=0)
Return amount (with tax) of all deposits invoices used by invoice as a payment.
getNomUrl($withpicto, $option='invoice')
Return clickable ref of object (with picto or not)
getSumCreditNotesUsed($invoice, $multicurrency=0)
Return amount (with tax) of all credit notes invoices + excess received used by invoice as a payment.
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...