dolibarr 20.0.5
discount.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2018 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2024 Alexandre Janniaux <alexandre.janniaux@gmail.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
29
34{
39 public $fk_soc;
43 public $socid;
44
48 public $discount_type; // 0 => customer discount, 1 => supplier discount
49
50 public $total_ht;
51 public $total_tva;
52 public $total_ttc;
53 public $amount_ht; // deprecated
54 public $amount_tva; // deprecated
55 public $amount_ttc; // deprecated
56
57 public $multicurrency_total_ht;
58 public $multicurrency_total_tva;
59 public $multicurrency_total_ttc;
60 public $multicurrency_amount_ht; // deprecated
61 public $multicurrency_amount_tva; // deprecated
62 public $multicurrency_amount_ttc; // deprecated
63
67 public $multicurrency_subprice;
68
72 public $fk_invoice_supplier;
73
77 public $fk_invoice_supplier_line;
78
79 // Vat rate
80 public $tva_tx;
81 public $vat_src_code;
82
86 public $fk_user;
87
91 public $description;
92
98 public $datec;
99
103 public $fk_facture_line;
104
108 public $fk_facture;
109
113 public $fk_facture_source;
114 public $ref_facture_source; // Ref credit note or deposit used to create the discount
115 public $type_facture_source;
116
117 public $fk_invoice_supplier_source;
118 public $ref_invoice_supplier_source; // Ref credit note or deposit used to create the discount
119 public $type_invoice_supplier_source;
120
126 public function __construct($db)
127 {
128 $this->db = $db;
129 }
130
131
140 public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
141 {
142 // Check parameters
143 if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
144 $this->error = 'ErrorBadParameters';
145 return -1;
146 }
147
148 $sql = "SELECT sr.rowid, sr.fk_soc, sr.discount_type,";
149 $sql .= " sr.fk_user,";
150 $sql .= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx, sr.vat_src_code,";
151 $sql .= " sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
152 $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,";
153 $sql .= " sr.datec,";
154 $sql .= " f.ref as ref_facture_source, f.type as type_facture_source,";
155 $sql .= " fsup.ref as ref_invoice_supplier_source, fsup.type as type_invoice_supplier_source";
156 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as sr";
157 $sql .= " LEFT JOIN ".$this->db->prefix()."facture as f ON sr.fk_facture_source = f.rowid";
158 $sql .= " LEFT JOIN ".$this->db->prefix()."facture_fourn as fsup ON sr.fk_invoice_supplier_source = fsup.rowid";
159 $sql .= " WHERE sr.entity IN (".getEntity('invoice').")";
160 if ($rowid) {
161 $sql .= " AND sr.rowid = ".((int) $rowid);
162 }
163 if ($fk_facture_source) {
164 $sql .= " AND sr.fk_facture_source = ".((int) $fk_facture_source);
165 }
166 if ($fk_invoice_supplier_source) {
167 $sql .= " AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
168 }
169
170 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
171 $resql = $this->db->query($sql);
172 if ($resql) {
173 if ($this->db->num_rows($resql)) {
174 $obj = $this->db->fetch_object($resql);
175
176 $this->id = $obj->rowid;
177 $this->fk_soc = $obj->fk_soc;
178 $this->socid = $obj->fk_soc;
179 $this->discount_type = $obj->discount_type;
180
181 $this->total_ht = $obj->amount_ht;
182 $this->total_tva = $obj->amount_tva;
183 $this->total_ttc = $obj->amount_ttc;
184 // For backward compatibility
185 $this->amount_ht = $this->total_ht;
186 $this->amount_tva = $this->total_tva;
187 $this->amount_ttc = $this->total_ttc;
188
189 $this->multicurrency_total_ht = $this->multicurrency_subprice = $obj->multicurrency_amount_ht;
190 $this->multicurrency_total_tva = $obj->multicurrency_amount_tva;
191 $this->multicurrency_total_ttc = $obj->multicurrency_amount_ttc;
192 // For backward compatibility
193 $this->multicurrency_amount_ht = $this->multicurrency_total_ht;
194 $this->multicurrency_amount_tva = $this->multicurrency_total_tva;
195 $this->multicurrency_amount_ttc = $this->multicurrency_total_ttc;
196
197 $this->tva_tx = $obj->tva_tx;
198 $this->vat_src_code = $obj->vat_src_code;
199
200 $this->fk_user = $obj->fk_user;
201 $this->fk_facture_line = $obj->fk_facture_line;
202 $this->fk_facture = $obj->fk_facture;
203 $this->fk_facture_source = $obj->fk_facture_source; // Id credit note or deposit source
204 $this->ref_facture_source = $obj->ref_facture_source; // Ref credit note or deposit source
205 $this->type_facture_source = $obj->type_facture_source; // Type credit note or deposit source
206 $this->fk_invoice_supplier_line = $obj->fk_invoice_supplier_line;
207 $this->fk_invoice_supplier = $obj->fk_invoice_supplier;
208 $this->fk_invoice_supplier_source = $obj->fk_invoice_supplier_source; // Id credit note or deposit source
209 $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source; // Ref credit note or deposit source
210 $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source; // Type credit note or deposit source
211 $this->description = $obj->description;
212 $this->datec = $this->db->jdate($obj->datec);
213
214 $this->db->free($resql);
215 return 1;
216 } else {
217 $this->db->free($resql);
218 return 0;
219 }
220 } else {
221 $this->error = $this->db->error();
222 return -1;
223 }
224 }
225
226
233 public function create($user)
234 {
235 global $conf, $langs;
236
237 // Clean parameters
238 $this->amount_ht = price2num($this->amount_ht);
239 $this->amount_tva = price2num($this->amount_tva);
240 $this->amount_ttc = price2num($this->amount_ttc);
241
242 $this->tva_tx = price2num($this->tva_tx);
243
244 $this->multicurrency_amount_ht = price2num($this->multicurrency_amount_ht);
245 $this->multicurrency_amount_tva = price2num($this->multicurrency_amount_tva);
246 $this->multicurrency_amount_ttc = price2num($this->multicurrency_amount_ttc);
247
248 if (empty($this->multicurrency_amount_ht)) {
249 $this->multicurrency_amount_ht = 0;
250 }
251 if (empty($this->multicurrency_amount_tva)) {
252 $this->multicurrency_amount_tva = 0;
253 }
254 if (empty($this->multicurrency_amount_ttc)) {
255 $this->multicurrency_amount_ttc = 0;
256 }
257 if (empty($this->tva_tx)) {
258 $this->tva_tx = 0;
259 }
260
261 // Check parameters
262 if (empty($this->description)) {
263 $this->error = 'BadValueForPropertyDescriptionOfDiscount';
264 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
265 return -1;
266 }
267
268 $userid = $user->id;
269 if (!($userid > 0)) { // For example when record is saved into an anonymous context with a not loaded object $user.
270 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
271 $tmpinvoice = new Facture($this->db);
272 $tmpinvoice->fetch($this->fk_facture_source);
273 $userid = $tmpinvoice->fk_user_author; // We use the author of invoice
274 }
275
276 // Insert request
277 $sql = "INSERT INTO ".$this->db->prefix()."societe_remise_except";
278 $sql .= " (entity, datec, fk_soc, discount_type, fk_user, description,";
279 $sql .= " amount_ht, amount_tva, amount_ttc, tva_tx, vat_src_code,";
280 $sql .= " multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,";
281 $sql .= " fk_facture_source, fk_invoice_supplier_source, multicurrency_code, multicurrency_tx";
282 $sql .= ")";
283 $sql .= " VALUES (".((int) $conf->entity).", '".$this->db->idate($this->datec != '' ? $this->datec : dol_now())."', ".((int) $this->fk_soc).", ".(empty($this->discount_type) ? 0 : intval($this->discount_type)).", ".((int) $userid).", '".$this->db->escape($this->description)."',";
284 $sql .= " ".price2num($this->amount_ht).", ".price2num($this->amount_tva).", ".price2num($this->amount_ttc).", ".price2num($this->tva_tx).", '".$this->db->escape($this->vat_src_code)."',";
285 $sql .= " ".price2num($this->multicurrency_amount_ht).", ".price2num($this->multicurrency_amount_tva).", ".price2num($this->multicurrency_amount_ttc).", ";
286 $sql .= " ".($this->fk_facture_source ? ((int) $this->fk_facture_source) : "null").",";
287 $sql .= " ".($this->fk_invoice_supplier_source ? ((int) $this->fk_invoice_supplier_source) : "null").",";
288 $sql .= " ".($this->multicurrency_code ? "'".$this->db->escape($this->multicurrency_code)."'" : "null").",";
289 $sql .= " ".($this->multicurrency_tx ? price2num($this->multicurrency_tx) : "null");
290 $sql .= ")";
291
292 dol_syslog(get_class($this)."::create", LOG_DEBUG);
293 $resql = $this->db->query($sql);
294 if ($resql) {
295 $this->id = $this->db->last_insert_id($this->db->prefix()."societe_remise_except");
296 return $this->id;
297 } else {
298 $this->error = $this->db->lasterror().' - sql='.$sql;
299 return -1;
300 }
301 }
302
303
310 public function delete($user)
311 {
312 global $conf, $langs;
313
314 // Check if we can remove the discount
315 if ($this->fk_facture_source) {
316 $sql = "SELECT COUNT(rowid) as nb";
317 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
318 $sql .= " WHERE (fk_facture_line IS NOT NULL"; // Not used as absolute simple discount
319 $sql .= " OR fk_facture IS NOT NULL)"; // Not used as credit note and not used as deposit
320 $sql .= " AND fk_facture_source = ".((int) $this->fk_facture_source);
321 //$sql.=" AND rowid != ".$this->id;
322
323 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
324 $resql = $this->db->query($sql);
325 if ($resql) {
326 $obj = $this->db->fetch_object($resql);
327 if ($obj->nb > 0) {
328 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
329 return -2;
330 }
331 } else {
332 dol_print_error($this->db);
333 return -1;
334 }
335 }
336
337 // Check if we can remove the discount
338 if ($this->fk_invoice_supplier_source) {
339 $sql = "SELECT COUNT(rowid) as nb";
340 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
341 $sql .= " WHERE (fk_invoice_supplier_line IS NOT NULL"; // Not used as absolute simple discount
342 $sql .= " OR fk_invoice_supplier IS NOT NULL)"; // Not used as credit note and not used as deposit
343 $sql .= " AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
344 //$sql.=" AND rowid != ".$this->id;
345
346 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
347 $resql = $this->db->query($sql);
348 if ($resql) {
349 $obj = $this->db->fetch_object($resql);
350 if ($obj->nb > 0) {
351 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
352 return -2;
353 }
354 } else {
355 dol_print_error($this->db);
356 return -1;
357 }
358 }
359
360 $this->db->begin();
361
362 // Delete but only if not used
363 $sql = "DELETE FROM ".$this->db->prefix()."societe_remise_except ";
364 if ($this->fk_facture_source) {
365 $sql .= " WHERE fk_facture_source = ".((int) $this->fk_facture_source); // Delete all lines of same series
366 } elseif ($this->fk_invoice_supplier_source) {
367 $sql .= " WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source); // Delete all lines of same series
368 } else {
369 $sql .= " WHERE rowid = ".((int) $this->id); // Delete only line
370 }
371 $sql .= " AND (fk_facture_line IS NULL"; // Not used as absolute simple discount
372 $sql .= " AND fk_facture IS NULL)"; // Not used as credit note and not used as deposit
373 $sql .= " AND (fk_invoice_supplier_line IS NULL"; // Not used as absolute simple discount
374 $sql .= " AND fk_invoice_supplier IS NULL)"; // Not used as credit note and not used as deposit
375
376 dol_syslog(get_class($this)."::delete Delete discount", LOG_DEBUG);
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
434 public function link_to_invoice($rowidline, $rowidinvoice)
435 {
436 // phpcs:enable
437 // Check parameters
438 if (!$rowidline && !$rowidinvoice) {
439 $this->error = 'ErrorBadParameters';
440 return -1;
441 }
442 if ($rowidline && $rowidinvoice) {
443 $this->error = 'ErrorBadParameters';
444 return -2;
445 }
446
447 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
448 if (!empty($this->discount_type)) {
449 if ($rowidline) {
450 $sql .= " SET fk_invoice_supplier_line = ".((int) $rowidline);
451 }
452 if ($rowidinvoice) {
453 $sql .= " SET fk_invoice_supplier = ".((int) $rowidinvoice);
454 }
455 } else {
456 if ($rowidline) {
457 $sql .= " SET fk_facture_line = ".((int) $rowidline);
458 }
459 if ($rowidinvoice) {
460 $sql .= " SET fk_facture = ".((int) $rowidinvoice);
461 }
462 }
463 $sql .= " WHERE rowid = ".((int) $this->id);
464
465 dol_syslog(get_class($this)."::link_to_invoice", LOG_DEBUG);
466 $resql = $this->db->query($sql);
467 if ($resql) {
468 if (!empty($this->discount_type)) {
469 $this->fk_invoice_supplier_line = $rowidline;
470 $this->fk_invoice_supplier = $rowidinvoice;
471 } else {
472 $this->fk_facture_line = $rowidline;
473 $this->fk_facture = $rowidinvoice;
474 }
475 return 1;
476 } else {
477 $this->error = $this->db->error();
478 return -3;
479 }
480 }
481
482
483 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
490 public function unlink_invoice()
491 {
492 // phpcs:enable
493 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
494 if (!empty($this->discount_type)) {
495 $sql .= " SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
496 } else {
497 $sql .= " SET fk_facture_line = NULL, fk_facture = NULL";
498 }
499 $sql .= " WHERE rowid = ".((int) $this->id);
500
501 dol_syslog(get_class($this)."::unlink_invoice", LOG_DEBUG);
502 $resql = $this->db->query($sql);
503 if ($resql) {
504 return 1;
505 } else {
506 $this->error = $this->db->error();
507 return -3;
508 }
509 }
510
511
523 public function getAvailableDiscounts($company = null, $user = null, $filter = '', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
524 {
525 global $conf;
526
527 dol_syslog(get_class($this)."::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
528
529 $sql = "SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
530 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
531 $sql .= " WHERE rc.entity = ".$conf->entity;
532 $sql .= " AND rc.discount_type=".((int) $discount_type);
533 if (!empty($discount_type)) {
534 $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)"; // Available from supplier
535 } else {
536 $sql .= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available to customer
537 }
538 if (is_object($company)) {
539 $sql .= " AND rc.fk_soc = ".((int) $company->id);
540 }
541 if (is_object($user)) {
542 $sql .= " AND rc.fk_user = ".((int) $user->id);
543 }
544 if ($filter) {
545 $sql .= " AND (".$filter.")";
546 }
547 if ($maxvalue) {
548 $sql .= ' AND rc.amount_ttc <= '.((float) price2num($maxvalue));
549 }
550
551 $resql = $this->db->query($sql);
552 if ($resql) {
553 $obj = $this->db->fetch_object($resql);
554 //while ($obj)
555 //{
556 //print 'zz'.$obj->amount;
557 //$obj = $this->db->fetch_object($resql);
558 //}
559 if ($multicurrency) {
560 return $obj->multicurrency_amount;
561 }
562
563 return $obj->amount;
564 }
565 return -1;
566 }
567
568
577 public function getSumDepositsUsed($invoice, $multicurrency = 0)
578 {
579 dol_syslog(get_class($this)."::getSumDepositsUsed", LOG_DEBUG);
580
581 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
582 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
583 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
584 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
585 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
586 } elseif ($invoice->element == 'invoice_supplier') {
587 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
588 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
589 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
590 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
591 } else {
592 $this->error = get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
593 dol_print_error($this->db, $this->error);
594 return -1;
595 }
596
597 $resql = $this->db->query($sql);
598 if ($resql) {
599 $obj = $this->db->fetch_object($resql);
600 if ($multicurrency == 1) {
601 return $obj->multicurrency_amount;
602 } else {
603 return $obj->amount;
604 }
605 } else {
606 $this->error = $this->db->lasterror();
607 return -1;
608 }
609 }
610
618 public function getSumCreditNotesUsed($invoice, $multicurrency = 0)
619 {
620 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
621
622 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
623 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
624 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
625 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
626 $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
627 } elseif ($invoice->element == 'invoice_supplier') {
628 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
629 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
630 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
631 $sql .= " AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.", ".$invoice::TYPE_CREDIT_NOTE).")"; // Find discount coming from credit note or excess paid
632 } else {
633 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
634 dol_print_error($this->db, $this->error);
635 return -1;
636 }
637
638 $resql = $this->db->query($sql);
639 if ($resql) {
640 $obj = $this->db->fetch_object($resql);
641 if ($multicurrency == 1) {
642 return $obj->multicurrency_amount;
643 } else {
644 return $obj->amount;
645 }
646 } else {
647 $this->error = $this->db->lasterror();
648 return -1;
649 }
650 }
658 public function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency = 0)
659 {
660 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
661
662 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
663 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
664 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
665 $sql .= " WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
666 } elseif ($invoice->element == 'invoice_supplier') {
667 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
668 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
669 $sql .= " WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
670 } else {
671 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
672 dol_print_error($this->db, $this->error);
673 return -1;
674 }
675
676 $resql = $this->db->query($sql);
677 if ($resql) {
678 $obj = $this->db->fetch_object($resql);
679 if ($multicurrency) {
680 return $obj->multicurrency_amount;
681 } else {
682 return $obj->amount;
683 }
684 } else {
685 $this->error = $this->db->lasterror();
686 return -1;
687 }
688 }
689
697 public function getNomUrl($withpicto, $option = 'invoice')
698 {
699 global $langs;
700
701 $result = '';
702 $link = '';
703 $linkend = '';
704 $label = '';
705 $picto = '';
706 $ref = '';
707
708 if ($option == 'invoice') {
709 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
710 $link = !empty($this->discount_type) ? '/fourn/facture/card.php' : '/compta/facture/card.php';
711 $label = $langs->trans("ShowSourceInvoice").': '.$this->ref_facture_source;
712 $link = '<a href="'.DOL_URL_ROOT.$link.'?facid='.$facid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
713 $linkend = '</a>';
714 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
715 $picto = 'bill';
716 }
717 if ($option == 'discount') {
718 $label = $langs->trans("Discount");
719 $link = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->socid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
720 $linkend = '</a>';
721 $ref = $langs->trans("Discount");
722 $picto = 'generic';
723 }
724
725
726 if ($withpicto) {
727 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
728 }
729 if ($withpicto && $withpicto != 2) {
730 $result .= ' ';
731 }
732 $result .= $link.$ref.$linkend;
733 return $result;
734 }
735
736
744 public function initAsSpecimen()
745 {
746 $this->fk_soc = 1;
747 $this->socid = 1;
748 $this->amount_ht = 10;
749 $this->amount_tva = 1.96;
750 $this->amount_ttc = 11.96;
751 $this->tva_tx = 19.6;
752 $this->description = 'Specimen discount';
753
754 return 1;
755 }
756}
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.
link_to_invoice($rowidline, $rowidinvoice)
Link the discount to a particular invoice line or a particular invoice.
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.
unlink_invoice()
Link the discount to a particular invoice line or a particular invoice.
fetch($rowid, $fk_facture_source=0, $fk_invoice_supplier_source=0)
Load object from database into memory.
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.
__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...