29require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
49 public $discount_type;
82 public $multicurrency_total_ht;
86 public $multicurrency_total_tva;
90 public $multicurrency_total_ttc;
95 public $multicurrency_amount_ht;
100 public $multicurrency_amount_tva;
105 public $multicurrency_amount_ttc;
110 public $multicurrency_subprice;
115 public $fk_invoice_supplier;
120 public $fk_invoice_supplier_line;
129 public $vat_src_code;
151 public $fk_facture_line;
161 public $fk_facture_source;
165 public $ref_facture_source;
169 public $type_facture_source;
174 public $fk_invoice_supplier_source;
178 public $ref_invoice_supplier_source;
182 public $type_invoice_supplier_source;
185 const TYPE_CUSTOMER = 0;
188 const TYPE_SUPPLIER = 1;
209 public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
212 if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
213 $this->error =
'ErrorBadParameters';
217 $sql =
"SELECT sr.rowid, sr.fk_soc, sr.discount_type,";
218 $sql .=
" sr.fk_user,";
219 $sql .=
" sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx, sr.vat_src_code,";
220 $sql .=
" sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
221 $sql .=
" sr.fk_facture_line, sr.fk_facture, sr.fk_facture_source, sr.fk_invoice_supplier_line, sr.fk_invoice_supplier, sr.fk_invoice_supplier_source, sr.description,";
222 $sql .=
" sr.datec,";
223 $sql .=
" f.ref as ref_facture_source, f.type as type_facture_source,";
224 $sql .=
" fsup.ref as ref_invoice_supplier_source, fsup.type as type_invoice_supplier_source";
225 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as sr";
226 $sql .=
" LEFT JOIN ".$this->db->prefix().
"facture as f ON sr.fk_facture_source = f.rowid";
227 $sql .=
" LEFT JOIN ".$this->db->prefix().
"facture_fourn as fsup ON sr.fk_invoice_supplier_source = fsup.rowid";
228 $sql .=
" WHERE sr.entity IN (".getEntity(
'invoice').
")";
230 $sql .=
" AND sr.rowid = ".((int) $rowid);
232 if ($fk_facture_source) {
233 $sql .=
" AND sr.fk_facture_source = ".((int) $fk_facture_source);
235 if ($fk_invoice_supplier_source) {
236 $sql .=
" AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
239 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
240 $resql = $this->db->query($sql);
242 if ($this->db->num_rows($resql)) {
243 $obj = $this->db->fetch_object($resql);
245 $this->
id = $obj->rowid;
246 $this->fk_soc = $obj->fk_soc;
247 $this->socid = $obj->fk_soc;
248 $this->discount_type = $obj->discount_type;
250 $this->total_ht = $obj->amount_ht;
251 $this->total_tva = $obj->amount_tva;
252 $this->total_ttc = $obj->amount_ttc;
254 $this->amount_ht = $this->total_ht;
255 $this->amount_tva = $this->total_tva;
256 $this->amount_ttc = $this->total_ttc;
258 $this->multicurrency_total_ht = $this->multicurrency_subprice = $obj->multicurrency_amount_ht;
259 $this->multicurrency_total_tva = $obj->multicurrency_amount_tva;
260 $this->multicurrency_total_ttc = $obj->multicurrency_amount_ttc;
262 $this->multicurrency_amount_ht = $this->multicurrency_total_ht;
263 $this->multicurrency_amount_tva = $this->multicurrency_total_tva;
264 $this->multicurrency_amount_ttc = $this->multicurrency_total_ttc;
266 $this->tva_tx = $obj->tva_tx;
267 $this->vat_src_code = $obj->vat_src_code;
269 $this->fk_user = $obj->fk_user;
270 $this->fk_facture_line = $obj->fk_facture_line;
271 $this->fk_facture = $obj->fk_facture;
272 $this->fk_facture_source = $obj->fk_facture_source;
273 $this->ref_facture_source = $obj->ref_facture_source;
274 $this->type_facture_source = $obj->type_facture_source;
275 $this->fk_invoice_supplier_line = $obj->fk_invoice_supplier_line;
276 $this->fk_invoice_supplier = $obj->fk_invoice_supplier;
277 $this->fk_invoice_supplier_source = $obj->fk_invoice_supplier_source;
278 $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source;
279 $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source;
281 $this->datec = $this->db->jdate($obj->datec);
283 $this->db->free($resql);
286 $this->db->free($resql);
290 $this->error = $this->db->error();
307 $this->amount_ht =
price2num($this->amount_ht);
308 $this->amount_tva =
price2num($this->amount_tva);
309 $this->amount_ttc =
price2num($this->amount_ttc);
311 $this->tva_tx =
price2num($this->tva_tx);
313 $this->multicurrency_amount_ht =
price2num($this->multicurrency_amount_ht);
314 $this->multicurrency_amount_tva =
price2num($this->multicurrency_amount_tva);
315 $this->multicurrency_amount_ttc =
price2num($this->multicurrency_amount_ttc);
317 if (empty($this->multicurrency_amount_ht)) {
318 $this->multicurrency_amount_ht = 0;
320 if (empty($this->multicurrency_amount_tva)) {
321 $this->multicurrency_amount_tva = 0;
323 if (empty($this->multicurrency_amount_ttc)) {
324 $this->multicurrency_amount_ttc = 0;
326 if (empty($this->tva_tx)) {
332 $this->error =
'BadValueForPropertyDescriptionOfDiscount';
333 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
338 if (!($userid > 0)) {
339 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
340 $tmpinvoice =
new Facture($this->db);
341 $tmpinvoice->fetch($this->fk_facture_source);
342 $userid = $tmpinvoice->fk_user_author;
346 $sql =
"INSERT INTO ".$this->db->prefix().
"societe_remise_except";
347 $sql .=
" (entity, datec, fk_soc, discount_type, fk_user, description,";
348 $sql .=
" amount_ht, amount_tva, amount_ttc, tva_tx, vat_src_code,";
349 $sql .=
" multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,";
350 $sql .=
" fk_facture_source, fk_invoice_supplier_source";
352 $sql .=
" VALUES (".$conf->entity.
", '".$this->db->idate($this->datec !=
'' ? $this->datec :
dol_now()).
"', ".((
int) $this->socid).
", ".(empty($this->discount_type) ? 0 : intval($this->discount_type)).
", ".((
int) $userid).
", '".$this->db->escape($this->description).
"',";
353 $sql .=
" ".price2num($this->amount_ht).
", ".
price2num($this->amount_tva).
", ".
price2num($this->amount_ttc).
", ".
price2num($this->tva_tx).
", '".$this->db->escape($this->vat_src_code).
"',";
354 $sql .=
" ".price2num($this->multicurrency_amount_ht).
", ".
price2num($this->multicurrency_amount_tva).
", ".
price2num($this->multicurrency_amount_ttc).
", ";
355 $sql .=
" ".($this->fk_facture_source ? ((int) $this->fk_facture_source) :
"null").
",";
356 $sql .=
" ".($this->fk_invoice_supplier_source ? ((int) $this->fk_invoice_supplier_source) :
"null");
359 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
360 $resql = $this->db->query($sql);
362 $this->
id = $this->db->last_insert_id($this->db->prefix().
"societe_remise_except");
365 $this->error = $this->db->lasterror().
' - sql='.$sql;
377 public function delete($user)
379 global
$conf, $langs;
382 if ($this->fk_facture_source) {
383 $sql =
"SELECT COUNT(rowid) as nb";
384 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except";
385 $sql .=
" WHERE (fk_facture_line IS NOT NULL";
386 $sql .=
" OR fk_facture IS NOT NULL)";
387 $sql .=
" AND fk_facture_source = ".((int) $this->fk_facture_source);
390 dol_syslog(get_class($this).
"::delete Check if we can remove discount", LOG_DEBUG);
391 $resql = $this->db->query($sql);
393 $obj = $this->db->fetch_object($resql);
395 $this->error =
'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
405 if ($this->fk_invoice_supplier_source) {
406 $sql =
"SELECT COUNT(rowid) as nb";
407 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except";
408 $sql .=
" WHERE (fk_invoice_supplier_line IS NOT NULL";
409 $sql .=
" OR fk_invoice_supplier IS NOT NULL)";
410 $sql .=
" AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
413 dol_syslog(get_class($this).
"::delete Check if we can remove discount", LOG_DEBUG);
414 $resql = $this->db->query($sql);
416 $obj = $this->db->fetch_object($resql);
418 $this->error =
'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
430 $sql =
"DELETE FROM ".$this->db->prefix().
"societe_remise_except ";
431 if ($this->fk_facture_source) {
432 $sql .=
" WHERE fk_facture_source = ".((int) $this->fk_facture_source);
433 } elseif ($this->fk_invoice_supplier_source) {
434 $sql .=
" WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
436 $sql .=
" WHERE rowid = ".((int) $this->
id);
438 $sql .=
" AND (fk_facture_line IS NULL";
439 $sql .=
" AND fk_facture IS NULL)";
440 $sql .=
" AND (fk_invoice_supplier_line IS NULL";
441 $sql .=
" AND fk_invoice_supplier IS NULL)";
443 dol_syslog(get_class($this).
"::delete Delete discount", LOG_DEBUG);
445 require_once DOL_DOCUMENT_ROOT.
'/core/class/commoninvoice.class.php';
446 $result = $this->db->query($sql);
449 if ($this->fk_facture_source) {
450 $sql =
"UPDATE ".$this->db->prefix().
"facture";
451 $sql .=
" set paye=0, fk_statut=1";
454 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
455 $result = $this->db->query($sql);
460 $this->error = $this->db->lasterror();
461 $this->db->rollback();
464 } elseif ($this->fk_invoice_supplier_source) {
465 $sql =
"UPDATE ".$this->db->prefix().
"facture_fourn";
466 $sql .=
" set paye=0, fk_statut=1";
469 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
470 $result = $this->db->query($sql);
475 $this->error = $this->db->lasterror();
476 $this->db->rollback();
484 $this->error = $this->db->lasterror();
485 $this->db->rollback();
509 if (!$rowidline && !$rowidinvoice) {
510 $this->error =
'ErrorBadParameters';
513 if ($rowidline && $rowidinvoice) {
514 $this->error =
'ErrorBadParameters';
518 $sql =
"UPDATE ".$this->db->prefix().
"societe_remise_except";
519 if (!empty($this->discount_type)) {
521 $sql .=
" SET fk_invoice_supplier_line = ".((int) $rowidline);
524 $sql .=
" SET fk_invoice_supplier = ".((int) $rowidinvoice);
528 $sql .=
" SET fk_facture_line = ".((int) $rowidline);
531 $sql .=
" SET fk_facture = ".((int) $rowidinvoice);
534 $sql .=
" WHERE rowid = ".((int) $this->
id);
536 dol_syslog(get_class($this).
"::link_to_invoice", LOG_DEBUG);
537 $resql = $this->db->query($sql);
539 if (!empty($this->discount_type)) {
540 $this->fk_invoice_supplier_line = $rowidline;
541 $this->fk_invoice_supplier = $rowidinvoice;
543 $this->fk_facture_line = $rowidline;
544 $this->fk_facture = $rowidinvoice;
548 $result = $this->
call_trigger(
'DISCOUNT_MODIFY', $user);
556 $this->error = $this->db->error();
575 $sql =
"UPDATE ".$this->db->prefix().
"societe_remise_except";
576 if (!empty($this->discount_type)) {
577 $sql .=
" SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
579 $sql .=
" SET fk_facture_line = NULL, fk_facture = NULL";
581 $sql .=
" WHERE rowid = ".((int) $this->
id);
583 dol_syslog(get_class($this).
"::unlink_invoice", LOG_DEBUG);
584 $resql = $this->db->query($sql);
588 $result = $this->
call_trigger(
'DISCOUNT_MODIFY', $user);
596 $this->error = $this->db->error();
613 public function getAvailableDiscounts($company =
null, $user =
null, $filter =
'', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
617 dol_syslog(get_class($this).
"::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
619 $sql =
"SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
620 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
621 $sql .=
" WHERE rc.entity = ".$conf->entity;
622 $sql .=
" AND rc.discount_type=".((int) $discount_type);
623 if (!empty($discount_type)) {
624 $sql .=
" AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)";
626 $sql .=
" AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)";
628 if (is_object($company)) {
629 $sql .=
" AND rc.fk_soc = ".((int) $company->id);
631 if (is_object($user)) {
632 $sql .=
" AND rc.fk_user = ".((int) $user->id);
635 $sql .=
" AND (".$filter.
")";
638 $sql .=
' AND rc.amount_ttc <= '.((float)
price2num($maxvalue));
641 $resql = $this->db->query($sql);
643 $obj = $this->db->fetch_object($resql);
649 if ($multicurrency) {
650 return $obj->multicurrency_amount;
669 dol_syslog(get_class($this).
"::getSumDepositsUsed", LOG_DEBUG);
671 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
672 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
673 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture as f";
674 $sql .=
" WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
675 $sql .=
" AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
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, ".$this->db->prefix().
"facture_fourn as f";
679 $sql .=
" WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
680 $sql .=
" AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
682 $this->error = get_class($this).
"::getSumDepositsUsed was called with a bad object as a first parameter";
687 $resql = $this->db->query($sql);
689 $obj = $this->db->fetch_object($resql);
690 if ($multicurrency == 1) {
691 return $obj->multicurrency_amount;
696 $this->error = $this->db->lasterror();
710 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
712 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
713 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
714 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture as f";
715 $sql .=
" WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
716 $sql .=
" AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.
", ".$invoice::TYPE_CREDIT_NOTE.
", ".$invoice::TYPE_SITUATION).
")";
717 } elseif ($invoice->element ==
'invoice_supplier') {
718 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
719 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture_fourn as f";
720 $sql .=
" WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
721 $sql .=
" AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.
", ".$invoice::TYPE_CREDIT_NOTE).
")";
723 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
728 $resql = $this->db->query($sql);
730 $obj = $this->db->fetch_object($resql);
731 if ($multicurrency == 1) {
732 return $obj->multicurrency_amount;
737 $this->error = $this->db->lasterror();
750 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
752 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
753 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
754 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
755 $sql .=
" WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
756 } elseif ($invoice->element ==
'invoice_supplier') {
757 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
758 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
759 $sql .=
" WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
761 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
766 $resql = $this->db->query($sql);
768 $obj = $this->db->fetch_object($resql);
769 if ($multicurrency) {
770 return $obj->multicurrency_amount;
775 $this->error = $this->db->lasterror();
787 public function getNomUrl($withpicto = 0, $option =
'invoice')
798 if ($option ==
'invoice') {
799 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
800 $link = !empty($this->discount_type) ?
'/fourn/facture/card.php' :
'/compta/facture/card.php';
801 $label = $langs->trans(
"ShowSourceInvoice").
': '.$this->ref_facture_source;
802 $link =
'<a href="'.DOL_URL_ROOT.$link.
'?facid='.$facid.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
804 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
807 if ($option ==
'discount') {
808 $label = $langs->trans(
"Discount");
809 $link =
'<a href="'.DOL_URL_ROOT.
'/comm/remx.php?id='.$this->socid.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
811 $ref = $langs->trans(
"Discount");
817 $result .= ($link.img_object($label, $picto,
'class="classfortooltip"').$linkend);
819 if ($withpicto && $withpicto != 2) {
822 $result .= $link.$ref.$linkend;
837 $this->amount_ht = 10;
838 $this->amount_tva = 1.96;
839 $this->amount_ttc = 11.96;
840 $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.
getNomUrl($withpicto=0, $option='invoice')
Return clickable ref of object (with picto or not)
create($user)
Create a discount into database.
getSumDepositsUsed($invoice, $multicurrency=0)
Return amount (with tax) of all deposits invoices used by invoice as a payment.
getSumCreditNotesUsed($invoice, $multicurrency=0)
Return amount (with tax) of all credit notes invoices + excess received used by invoice as a payment.
link_to_invoice($rowidline, $rowidinvoice, $notrigger=0)
Link the discount to a particular invoice line or a particular invoice.
__construct($db)
Constructor.
Class to manage invoices.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...