29require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
49 public $discount_type;
58 public $multicurrency_total_ht;
59 public $multicurrency_total_tva;
60 public $multicurrency_total_ttc;
61 public $multicurrency_amount_ht;
62 public $multicurrency_amount_tva;
63 public $multicurrency_amount_ttc;
68 public $multicurrency_subprice;
73 public $fk_invoice_supplier;
78 public $fk_invoice_supplier_line;
104 public $fk_facture_line;
114 public $fk_facture_source;
115 public $ref_facture_source;
116 public $type_facture_source;
118 public $fk_invoice_supplier_source;
119 public $ref_invoice_supplier_source;
120 public $type_invoice_supplier_source;
141 public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
144 if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
145 $this->error =
'ErrorBadParameters';
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').
")";
162 $sql .=
" AND sr.rowid = ".((int) $rowid);
164 if ($fk_facture_source) {
165 $sql .=
" AND sr.fk_facture_source = ".((int) $fk_facture_source);
167 if ($fk_invoice_supplier_source) {
168 $sql .=
" AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
171 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
172 $resql = $this->db->query($sql);
174 if ($this->db->num_rows($resql)) {
175 $obj = $this->db->fetch_object($resql);
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;
182 $this->total_ht = $obj->amount_ht;
183 $this->total_tva = $obj->amount_tva;
184 $this->total_ttc = $obj->amount_ttc;
186 $this->amount_ht = $this->total_ht;
187 $this->amount_tva = $this->total_tva;
188 $this->amount_ttc = $this->total_ttc;
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;
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;
198 $this->tva_tx = $obj->tva_tx;
199 $this->vat_src_code = $obj->vat_src_code;
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;
205 $this->ref_facture_source = $obj->ref_facture_source;
206 $this->type_facture_source = $obj->type_facture_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;
210 $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source;
211 $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source;
213 $this->datec = $this->db->jdate($obj->datec);
215 $this->db->free($resql);
218 $this->db->free($resql);
222 $this->error = $this->db->error();
239 $this->amount_ht =
price2num($this->amount_ht);
240 $this->amount_tva =
price2num($this->amount_tva);
241 $this->amount_ttc =
price2num($this->amount_ttc);
243 $this->tva_tx =
price2num($this->tva_tx);
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);
249 if (empty($this->multicurrency_amount_ht)) {
250 $this->multicurrency_amount_ht = 0;
252 if (empty($this->multicurrency_amount_tva)) {
253 $this->multicurrency_amount_tva = 0;
255 if (empty($this->multicurrency_amount_ttc)) {
256 $this->multicurrency_amount_ttc = 0;
258 if (empty($this->tva_tx)) {
264 $this->error =
'BadValueForPropertyDescriptionOfDiscount';
265 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
270 if (!($userid > 0)) {
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;
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";
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");
291 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
292 $resql = $this->db->query($sql);
294 $this->
id = $this->db->last_insert_id($this->db->prefix().
"societe_remise_except");
297 $this->error = $this->db->lasterror().
' - sql='.$sql;
309 public function delete($user)
311 global $conf, $langs;
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";
318 $sql .=
" OR fk_facture IS NOT NULL)";
319 $sql .=
" AND fk_facture_source = ".((int) $this->fk_facture_source);
322 dol_syslog(get_class($this).
"::delete Check if we can remove discount", LOG_DEBUG);
323 $resql = $this->db->query($sql);
325 $obj = $this->db->fetch_object($resql);
327 $this->error =
'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
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";
341 $sql .=
" OR fk_invoice_supplier IS NOT NULL)";
342 $sql .=
" AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
345 dol_syslog(get_class($this).
"::delete Check if we can remove discount", LOG_DEBUG);
346 $resql = $this->db->query($sql);
348 $obj = $this->db->fetch_object($resql);
350 $this->error =
'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
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);
365 } elseif ($this->fk_invoice_supplier_source) {
366 $sql .=
" WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
368 $sql .=
" WHERE rowid = ".((int) $this->
id);
370 $sql .=
" AND (fk_facture_line IS NULL";
371 $sql .=
" AND fk_facture IS NULL)";
372 $sql .=
" AND (fk_invoice_supplier_line IS NULL";
373 $sql .=
" AND fk_invoice_supplier IS NULL)";
375 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);
381 if ($this->fk_facture_source) {
382 $sql =
"UPDATE ".$this->db->prefix().
"facture";
383 $sql .=
" set paye=0, fk_statut=1";
386 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
387 $result = $this->db->query($sql);
392 $this->error = $this->db->lasterror();
393 $this->db->rollback();
396 } elseif ($this->fk_invoice_supplier_source) {
397 $sql =
"UPDATE ".$this->db->prefix().
"facture_fourn";
398 $sql .=
" set paye=0, fk_statut=1";
401 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
402 $result = $this->db->query($sql);
407 $this->error = $this->db->lasterror();
408 $this->db->rollback();
416 $this->error = $this->db->lasterror();
417 $this->db->rollback();
441 if (!$rowidline && !$rowidinvoice) {
442 $this->error =
'ErrorBadParameters';
445 if ($rowidline && $rowidinvoice) {
446 $this->error =
'ErrorBadParameters';
450 $sql =
"UPDATE ".$this->db->prefix().
"societe_remise_except";
451 if (!empty($this->discount_type)) {
453 $sql .=
" SET fk_invoice_supplier_line = ".((int) $rowidline);
456 $sql .=
" SET fk_invoice_supplier = ".((int) $rowidinvoice);
460 $sql .=
" SET fk_facture_line = ".((int) $rowidline);
463 $sql .=
" SET fk_facture = ".((int) $rowidinvoice);
466 $sql .=
" WHERE rowid = ".((int) $this->
id);
468 dol_syslog(get_class($this).
"::link_to_invoice", LOG_DEBUG);
469 $resql = $this->db->query($sql);
471 if (!empty($this->discount_type)) {
472 $this->fk_invoice_supplier_line = $rowidline;
473 $this->fk_invoice_supplier = $rowidinvoice;
475 $this->fk_facture_line = $rowidline;
476 $this->fk_facture = $rowidinvoice;
480 $result = $this->
call_trigger(
'DISCOUNT_MODIFY', $user);
488 $this->error = $this->db->error();
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";
511 $sql .=
" SET fk_facture_line = NULL, fk_facture = NULL";
513 $sql .=
" WHERE rowid = ".((int) $this->
id);
515 dol_syslog(get_class($this).
"::unlink_invoice", LOG_DEBUG);
516 $resql = $this->db->query($sql);
520 $result = $this->
call_trigger(
'DISCOUNT_MODIFY', $user);
528 $this->error = $this->db->error();
545 public function getAvailableDiscounts($company =
null, $user =
null, $filter =
'', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
549 dol_syslog(get_class($this).
"::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
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)";
558 $sql .=
" AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)";
560 if (is_object($company)) {
561 $sql .=
" AND rc.fk_soc = ".((int) $company->id);
563 if (is_object($user)) {
564 $sql .=
" AND rc.fk_user = ".((int) $user->id);
567 $sql .=
" AND (".$filter.
")";
570 $sql .=
' AND rc.amount_ttc <= '.((float)
price2num($maxvalue));
573 $resql = $this->db->query($sql);
575 $obj = $this->db->fetch_object($resql);
581 if ($multicurrency) {
582 return $obj->multicurrency_amount;
601 dol_syslog(get_class($this).
"::getSumDepositsUsed", LOG_DEBUG);
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;
614 $this->error = get_class($this).
"::getSumDepositsUsed was called with a bad object as a first parameter";
619 $resql = $this->db->query($sql);
621 $obj = $this->db->fetch_object($resql);
622 if ($multicurrency == 1) {
623 return $obj->multicurrency_amount;
628 $this->error = $this->db->lasterror();
642 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
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).
")";
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).
")";
655 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
660 $resql = $this->db->query($sql);
662 $obj = $this->db->fetch_object($resql);
663 if ($multicurrency == 1) {
664 return $obj->multicurrency_amount;
669 $this->error = $this->db->lasterror();
682 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
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);
693 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
698 $resql = $this->db->query($sql);
700 $obj = $this->db->fetch_object($resql);
701 if ($multicurrency) {
702 return $obj->multicurrency_amount;
707 $this->error = $this->db->lasterror();
719 public function getNomUrl($withpicto, $option =
'invoice')
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">';
736 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
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">';
743 $ref = $langs->trans(
"Discount");
749 $result .= ($link.img_object($label, $picto,
'class="classfortooltip"').$linkend);
751 if ($withpicto && $withpicto != 2) {
754 $result .= $link.$ref.$linkend;
769 $this->amount_ht = 10;
770 $this->amount_tva = 1.96;
771 $this->amount_ttc = 11.96;
772 $this->tva_tx = 19.6;
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...