dolibarr 19.0.4
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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
30{
34 public $db;
35
39 public $error;
40
44 public $errors = array();
45
49 public $id;
50
55 public $fk_soc;
59 public $socid;
60
61 public $discount_type; // 0 => customer discount, 1 => supplier discount
62
63 public $total_ht;
64 public $total_tva;
65 public $total_ttc;
66 public $amount_ht; // deprecated
67 public $amount_tva; // deprecated
68 public $amount_ttc; // deprecated
69
70 public $multicurrency_total_ht;
71 public $multicurrency_total_tva;
72 public $multicurrency_total_ttc;
73 public $multicurrency_amount_ht; // deprecated
74 public $multicurrency_amount_tva; // deprecated
75 public $multicurrency_amount_ttc; // deprecated
76
80 public $multicurrency_subprice;
81
85 public $fk_invoice_supplier;
86
90 public $fk_invoice_supplier_line;
91
92 // Vat rate
93 public $tva_tx;
94 public $vat_src_code;
95
99 public $fk_user;
100
104 public $description;
105
111 public $datec;
112
116 public $fk_facture_line;
117
121 public $fk_facture;
122
126 public $fk_facture_source;
127 public $ref_facture_source; // Ref credit note or deposit used to create the discount
128 public $type_facture_source;
129
130 public $fk_invoice_supplier_source;
131 public $ref_invoice_supplier_source; // Ref credit note or deposit used to create the discount
132 public $type_invoice_supplier_source;
133
139 public function __construct($db)
140 {
141 $this->db = $db;
142 }
143
144
153 public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
154 {
155 // Check parameters
156 if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
157 $this->error = 'ErrorBadParameters';
158 return -1;
159 }
160
161 $sql = "SELECT sr.rowid, sr.fk_soc, sr.discount_type,";
162 $sql .= " sr.fk_user,";
163 $sql .= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx, sr.vat_src_code,";
164 $sql .= " sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
165 $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,";
166 $sql .= " sr.datec,";
167 $sql .= " f.ref as ref_facture_source, f.type as type_facture_source,";
168 $sql .= " fsup.ref as ref_invoice_supplier_source, fsup.type as type_invoice_supplier_source";
169 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as sr";
170 $sql .= " LEFT JOIN ".$this->db->prefix()."facture as f ON sr.fk_facture_source = f.rowid";
171 $sql .= " LEFT JOIN ".$this->db->prefix()."facture_fourn as fsup ON sr.fk_invoice_supplier_source = fsup.rowid";
172 $sql .= " WHERE sr.entity IN (".getEntity('invoice').")";
173 if ($rowid) {
174 $sql .= " AND sr.rowid = ".((int) $rowid);
175 }
176 if ($fk_facture_source) {
177 $sql .= " AND sr.fk_facture_source = ".((int) $fk_facture_source);
178 }
179 if ($fk_invoice_supplier_source) {
180 $sql .= " AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
181 }
182
183 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
184 $resql = $this->db->query($sql);
185 if ($resql) {
186 if ($this->db->num_rows($resql)) {
187 $obj = $this->db->fetch_object($resql);
188
189 $this->id = $obj->rowid;
190 $this->fk_soc = $obj->fk_soc;
191 $this->discount_type = $obj->discount_type;
192
193 $this->total_ht = $obj->amount_ht;
194 $this->total_tva = $obj->amount_tva;
195 $this->total_ttc = $obj->amount_ttc;
196 // For backward compatibility
197 $this->amount_ht = $this->total_ht;
198 $this->amount_tva = $this->total_tva;
199 $this->amount_ttc = $this->total_ttc;
200
201 $this->multicurrency_total_ht = $this->multicurrency_subprice = $obj->multicurrency_amount_ht;
202 $this->multicurrency_total_tva = $obj->multicurrency_amount_tva;
203 $this->multicurrency_total_ttc = $obj->multicurrency_amount_ttc;
204 // For backward compatibility
205 $this->multicurrency_amount_ht = $this->multicurrency_total_ht;
206 $this->multicurrency_amount_tva = $this->multicurrency_total_tva;
207 $this->multicurrency_amount_ttc = $this->multicurrency_total_ttc;
208
209 $this->tva_tx = $obj->tva_tx;
210 $this->vat_src_code = $obj->vat_src_code;
211
212 $this->fk_user = $obj->fk_user;
213 $this->fk_facture_line = $obj->fk_facture_line;
214 $this->fk_facture = $obj->fk_facture;
215 $this->fk_facture_source = $obj->fk_facture_source; // Id credit note or deposit source
216 $this->ref_facture_source = $obj->ref_facture_source; // Ref credit note or deposit source
217 $this->type_facture_source = $obj->type_facture_source; // Type credit note or deposit source
218 $this->fk_invoice_supplier_line = $obj->fk_invoice_supplier_line;
219 $this->fk_invoice_supplier = $obj->fk_invoice_supplier;
220 $this->fk_invoice_supplier_source = $obj->fk_invoice_supplier_source; // Id credit note or deposit source
221 $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source; // Ref credit note or deposit source
222 $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source; // Type credit note or deposit source
223 $this->description = $obj->description;
224 $this->datec = $this->db->jdate($obj->datec);
225
226 $this->db->free($resql);
227 return 1;
228 } else {
229 $this->db->free($resql);
230 return 0;
231 }
232 } else {
233 $this->error = $this->db->error();
234 return -1;
235 }
236 }
237
238
245 public function create($user)
246 {
247 global $conf, $langs;
248
249 // Clean parameters
250 $this->amount_ht = price2num($this->amount_ht);
251 $this->amount_tva = price2num($this->amount_tva);
252 $this->amount_ttc = price2num($this->amount_ttc);
253
254 $this->tva_tx = price2num($this->tva_tx);
255
256 $this->multicurrency_amount_ht = price2num($this->multicurrency_amount_ht);
257 $this->multicurrency_amount_tva = price2num($this->multicurrency_amount_tva);
258 $this->multicurrency_amount_ttc = price2num($this->multicurrency_amount_ttc);
259
260 if (empty($this->multicurrency_amount_ht)) {
261 $this->multicurrency_amount_ht = 0;
262 }
263 if (empty($this->multicurrency_amount_tva)) {
264 $this->multicurrency_amount_tva = 0;
265 }
266 if (empty($this->multicurrency_amount_ttc)) {
267 $this->multicurrency_amount_ttc = 0;
268 }
269 if (empty($this->tva_tx)) {
270 $this->tva_tx = 0;
271 }
272
273 // Check parameters
274 if (empty($this->description)) {
275 $this->error = 'BadValueForPropertyDescriptionOfDiscount';
276 dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
277 return -1;
278 }
279
280 $userid = $user->id;
281 if (!($userid > 0)) { // For example when record is saved into an anonymous context with a not loaded object $user.
282 include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
283 $tmpinvoice = new Facture($this->db);
284 $tmpinvoice->fetch($this->fk_facture_source);
285 $userid = $tmpinvoice->fk_user_author; // We use the author of invoice
286 }
287
288 // Insert request
289 $sql = "INSERT INTO ".$this->db->prefix()."societe_remise_except";
290 $sql .= " (entity, datec, fk_soc, discount_type, fk_user, description,";
291 $sql .= " amount_ht, amount_tva, amount_ttc, tva_tx, vat_src_code,";
292 $sql .= " multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,";
293 $sql .= " fk_facture_source, fk_invoice_supplier_source";
294 $sql .= ")";
295 $sql .= " VALUES (".$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)."',";
296 $sql .= " ".price2num($this->amount_ht).", ".price2num($this->amount_tva).", ".price2num($this->amount_ttc).", ".price2num($this->tva_tx).", '".$this->db->escape($this->vat_src_code)."',";
297 $sql .= " ".price2num($this->multicurrency_amount_ht).", ".price2num($this->multicurrency_amount_tva).", ".price2num($this->multicurrency_amount_ttc).", ";
298 $sql .= " ".($this->fk_facture_source ? ((int) $this->fk_facture_source) : "null").",";
299 $sql .= " ".($this->fk_invoice_supplier_source ? ((int) $this->fk_invoice_supplier_source) : "null");
300 $sql .= ")";
301
302 dol_syslog(get_class($this)."::create", LOG_DEBUG);
303 $resql = $this->db->query($sql);
304 if ($resql) {
305 $this->id = $this->db->last_insert_id($this->db->prefix()."societe_remise_except");
306 return $this->id;
307 } else {
308 $this->error = $this->db->lasterror().' - sql='.$sql;
309 return -1;
310 }
311 }
312
313
320 public function delete($user)
321 {
322 global $conf, $langs;
323
324 // Check if we can remove the discount
325 if ($this->fk_facture_source) {
326 $sql = "SELECT COUNT(rowid) as nb";
327 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
328 $sql .= " WHERE (fk_facture_line IS NOT NULL"; // Not used as absolute simple discount
329 $sql .= " OR fk_facture IS NOT NULL)"; // Not used as credit note and not used as deposit
330 $sql .= " AND fk_facture_source = ".((int) $this->fk_facture_source);
331 //$sql.=" AND rowid != ".$this->id;
332
333 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
334 $resql = $this->db->query($sql);
335 if ($resql) {
336 $obj = $this->db->fetch_object($resql);
337 if ($obj->nb > 0) {
338 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
339 return -2;
340 }
341 } else {
342 dol_print_error($this->db);
343 return -1;
344 }
345 }
346
347 // Check if we can remove the discount
348 if ($this->fk_invoice_supplier_source) {
349 $sql = "SELECT COUNT(rowid) as nb";
350 $sql .= " FROM ".$this->db->prefix()."societe_remise_except";
351 $sql .= " WHERE (fk_invoice_supplier_line IS NOT NULL"; // Not used as absolute simple discount
352 $sql .= " OR fk_invoice_supplier IS NOT NULL)"; // Not used as credit note and not used as deposit
353 $sql .= " AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
354 //$sql.=" AND rowid != ".$this->id;
355
356 dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
357 $resql = $this->db->query($sql);
358 if ($resql) {
359 $obj = $this->db->fetch_object($resql);
360 if ($obj->nb > 0) {
361 $this->error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
362 return -2;
363 }
364 } else {
365 dol_print_error($this->db);
366 return -1;
367 }
368 }
369
370 $this->db->begin();
371
372 // Delete but only if not used
373 $sql = "DELETE FROM ".$this->db->prefix()."societe_remise_except ";
374 if ($this->fk_facture_source) {
375 $sql .= " WHERE fk_facture_source = ".((int) $this->fk_facture_source); // Delete all lines of same serie
376 } elseif ($this->fk_invoice_supplier_source) {
377 $sql .= " WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source); // Delete all lines of same serie
378 } else {
379 $sql .= " WHERE rowid = ".((int) $this->id); // Delete only line
380 }
381 $sql .= " AND (fk_facture_line IS NULL"; // Not used as absolute simple discount
382 $sql .= " AND fk_facture IS NULL)"; // Not used as credit note and not used as deposit
383 $sql .= " AND (fk_invoice_supplier_line IS NULL"; // Not used as absolute simple discount
384 $sql .= " AND fk_invoice_supplier IS NULL)"; // Not used as credit note and not used as deposit
385
386 dol_syslog(get_class($this)."::delete Delete discount", LOG_DEBUG);
387 require_once DOL_DOCUMENT_ROOT. '/core/class/commoninvoice.class.php';
388 $result = $this->db->query($sql);
389 if ($result) {
390 // If source of discount was a credit note or deposit, we change source statut.
391 if ($this->fk_facture_source) {
392 $sql = "UPDATE ".$this->db->prefix()."facture";
393 $sql .= " set paye=0, fk_statut=1";
394 $sql .= " WHERE type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_facture_source);
395
396 dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
397 $result = $this->db->query($sql);
398 if ($result) {
399 $this->db->commit();
400 return 1;
401 } else {
402 $this->error = $this->db->lasterror();
403 $this->db->rollback();
404 return -1;
405 }
406 } elseif ($this->fk_invoice_supplier_source) {
407 $sql = "UPDATE ".$this->db->prefix()."facture_fourn";
408 $sql .= " set paye=0, fk_statut=1";
409 $sql .= " WHERE type IN (".$this->db->sanitize(CommonInvoice::TYPE_CREDIT_NOTE.", ".CommonInvoice::TYPE_DEPOSIT).") AND rowid = ".((int) $this->fk_invoice_supplier_source);
410
411 dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
412 $result = $this->db->query($sql);
413 if ($result) {
414 $this->db->commit();
415 return 1;
416 } else {
417 $this->error = $this->db->lasterror();
418 $this->db->rollback();
419 return -1;
420 }
421 } else {
422 $this->db->commit();
423 return 1;
424 }
425 } else {
426 $this->error = $this->db->lasterror();
427 $this->db->rollback();
428 return -1;
429 }
430 }
431
432
433
434 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
444 public function link_to_invoice($rowidline, $rowidinvoice)
445 {
446 // phpcs:enable
447 // Check parameters
448 if (!$rowidline && !$rowidinvoice) {
449 $this->error = 'ErrorBadParameters';
450 return -1;
451 }
452 if ($rowidline && $rowidinvoice) {
453 $this->error = 'ErrorBadParameters';
454 return -2;
455 }
456
457 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
458 if (!empty($this->discount_type)) {
459 if ($rowidline) {
460 $sql .= " SET fk_invoice_supplier_line = ".((int) $rowidline);
461 }
462 if ($rowidinvoice) {
463 $sql .= " SET fk_invoice_supplier = ".((int) $rowidinvoice);
464 }
465 } else {
466 if ($rowidline) {
467 $sql .= " SET fk_facture_line = ".((int) $rowidline);
468 }
469 if ($rowidinvoice) {
470 $sql .= " SET fk_facture = ".((int) $rowidinvoice);
471 }
472 }
473 $sql .= " WHERE rowid = ".((int) $this->id);
474
475 dol_syslog(get_class($this)."::link_to_invoice", LOG_DEBUG);
476 $resql = $this->db->query($sql);
477 if ($resql) {
478 if (!empty($this->discount_type)) {
479 $this->fk_invoice_supplier_line = $rowidline;
480 $this->fk_invoice_supplier = $rowidinvoice;
481 } else {
482 $this->fk_facture_line = $rowidline;
483 $this->fk_facture = $rowidinvoice;
484 }
485 return 1;
486 } else {
487 $this->error = $this->db->error();
488 return -3;
489 }
490 }
491
492
493 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
500 public function unlink_invoice()
501 {
502 // phpcs:enable
503 $sql = "UPDATE ".$this->db->prefix()."societe_remise_except";
504 if (!empty($this->discount_type)) {
505 $sql .= " SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
506 } else {
507 $sql .= " SET fk_facture_line = NULL, fk_facture = NULL";
508 }
509 $sql .= " WHERE rowid = ".((int) $this->id);
510
511 dol_syslog(get_class($this)."::unlink_invoice", LOG_DEBUG);
512 $resql = $this->db->query($sql);
513 if ($resql) {
514 return 1;
515 } else {
516 $this->error = $this->db->error();
517 return -3;
518 }
519 }
520
521
533 public function getAvailableDiscounts($company = null, $user = null, $filter = '', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
534 {
535 global $conf;
536
537 dol_syslog(get_class($this)."::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
538
539 $sql = "SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
540 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
541 $sql .= " WHERE rc.entity = ".$conf->entity;
542 $sql .= " AND rc.discount_type=".((int) $discount_type);
543 if (!empty($discount_type)) {
544 $sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)"; // Available from supplier
545 } else {
546 $sql .= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available to customer
547 }
548 if (is_object($company)) {
549 $sql .= " AND rc.fk_soc = ".((int) $company->id);
550 }
551 if (is_object($user)) {
552 $sql .= " AND rc.fk_user = ".((int) $user->id);
553 }
554 if ($filter) {
555 $sql .= " AND (".$filter.")";
556 }
557 if ($maxvalue) {
558 $sql .= ' AND rc.amount_ttc <= '.((float) price2num($maxvalue));
559 }
560
561 $resql = $this->db->query($sql);
562 if ($resql) {
563 $obj = $this->db->fetch_object($resql);
564 //while ($obj)
565 //{
566 //print 'zz'.$obj->amount;
567 //$obj = $this->db->fetch_object($resql);
568 //}
569 if ($multicurrency) {
570 return $obj->multicurrency_amount;
571 }
572
573 return $obj->amount;
574 }
575 return -1;
576 }
577
578
587 public function getSumDepositsUsed($invoice, $multicurrency = 0)
588 {
589 dol_syslog(get_class($this)."::getSumDepositsUsed", LOG_DEBUG);
590
591 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
592 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
593 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
594 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
595 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
596 } elseif ($invoice->element == 'invoice_supplier') {
597 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
598 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
599 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
600 $sql .= " AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
601 } else {
602 $this->error = get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
603 dol_print_error($this->db, $this->error);
604 return -1;
605 }
606
607 $resql = $this->db->query($sql);
608 if ($resql) {
609 $obj = $this->db->fetch_object($resql);
610 if ($multicurrency == 1) {
611 return $obj->multicurrency_amount;
612 } else {
613 return $obj->amount;
614 }
615 } else {
616 $this->error = $this->db->lasterror();
617 return -1;
618 }
619 }
620
628 public function getSumCreditNotesUsed($invoice, $multicurrency = 0)
629 {
630 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
631
632 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
633 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
634 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture as f";
635 $sql .= " WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
636 $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
637 } elseif ($invoice->element == 'invoice_supplier') {
638 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
639 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc, ".$this->db->prefix()."facture_fourn as f";
640 $sql .= " WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
641 $sql .= " AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.", ".$invoice::TYPE_CREDIT_NOTE).")"; // Find discount coming from credit note or excess paid
642 } else {
643 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
644 dol_print_error($this->db, $this->error);
645 return -1;
646 }
647
648 $resql = $this->db->query($sql);
649 if ($resql) {
650 $obj = $this->db->fetch_object($resql);
651 if ($multicurrency == 1) {
652 return $obj->multicurrency_amount;
653 } else {
654 return $obj->amount;
655 }
656 } else {
657 $this->error = $this->db->lasterror();
658 return -1;
659 }
660 }
668 public function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency = 0)
669 {
670 dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
671
672 if ($invoice->element == 'facture' || $invoice->element == 'invoice') {
673 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
674 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
675 $sql .= " WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
676 } elseif ($invoice->element == 'invoice_supplier') {
677 $sql = "SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
678 $sql .= " FROM ".$this->db->prefix()."societe_remise_except as rc";
679 $sql .= " WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
680 } else {
681 $this->error = get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
682 dol_print_error($this->db, $this->error);
683 return -1;
684 }
685
686 $resql = $this->db->query($sql);
687 if ($resql) {
688 $obj = $this->db->fetch_object($resql);
689 if ($multicurrency) {
690 return $obj->multicurrency_amount;
691 } else {
692 return $obj->amount;
693 }
694 } else {
695 $this->error = $this->db->lasterror();
696 return -1;
697 }
698 }
699
707 public function getNomUrl($withpicto, $option = 'invoice')
708 {
709 global $langs;
710
711 $result = '';
712 $link = '';
713 $linkend = '';
714 $label = '';
715 $picto = '';
716 $ref = '';
717
718 if ($option == 'invoice') {
719 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
720 $link = !empty($this->discount_type) ? '/fourn/facture/card.php' : '/compta/facture/card.php';
721 $label = $langs->trans("ShowSourceInvoice").': '.$this->ref_facture_source;
722 $link = '<a href="'.DOL_URL_ROOT.$link.'?facid='.$facid.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
723 $linkend = '</a>';
724 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
725 $picto = 'bill';
726 }
727 if ($option == 'discount') {
728 $label = $langs->trans("Discount");
729 $link = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->fk_soc.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
730 $linkend = '</a>';
731 $ref = $langs->trans("Discount");
732 $picto = 'generic';
733 }
734
735
736 if ($withpicto) {
737 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
738 }
739 if ($withpicto && $withpicto != 2) {
740 $result .= ' ';
741 }
742 $result .= $link.$ref.$linkend;
743 return $result;
744 }
745
746
754 public function initAsSpecimen()
755 {
756 $this->fk_soc = 1;
757 $this->socid = 1;
758 $this->amount_ht = 10;
759 $this->amount_tva = 1.96;
760 $this->amount_ttc = 11.96;
761 $this->tva_tx = 19.6;
762 $this->description = 'Specimen discount';
763 }
764}
const TYPE_CREDIT_NOTE
Credit note invoice.
const TYPE_DEPOSIT
Deposit invoice.
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_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_now($mode='auto')
Return date for now.
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...