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;
203 public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
206 if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
207 $this->error =
'ErrorBadParameters';
211 $sql =
"SELECT sr.rowid, sr.fk_soc, sr.discount_type,";
212 $sql .=
" sr.fk_user,";
213 $sql .=
" sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx, sr.vat_src_code,";
214 $sql .=
" sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
215 $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,";
216 $sql .=
" sr.datec,";
217 $sql .=
" f.ref as ref_facture_source, f.type as type_facture_source,";
218 $sql .=
" fsup.ref as ref_invoice_supplier_source, fsup.type as type_invoice_supplier_source";
219 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as sr";
220 $sql .=
" LEFT JOIN ".$this->db->prefix().
"facture as f ON sr.fk_facture_source = f.rowid";
221 $sql .=
" LEFT JOIN ".$this->db->prefix().
"facture_fourn as fsup ON sr.fk_invoice_supplier_source = fsup.rowid";
222 $sql .=
" WHERE sr.entity IN (".getEntity(
'invoice').
")";
224 $sql .=
" AND sr.rowid = ".((int) $rowid);
226 if ($fk_facture_source) {
227 $sql .=
" AND sr.fk_facture_source = ".((int) $fk_facture_source);
229 if ($fk_invoice_supplier_source) {
230 $sql .=
" AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
233 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
234 $resql = $this->db->query($sql);
236 if ($this->db->num_rows($resql)) {
237 $obj = $this->db->fetch_object($resql);
239 $this->
id = $obj->rowid;
240 $this->fk_soc = $obj->fk_soc;
241 $this->socid = $obj->fk_soc;
242 $this->discount_type = $obj->discount_type;
244 $this->total_ht = $obj->amount_ht;
245 $this->total_tva = $obj->amount_tva;
246 $this->total_ttc = $obj->amount_ttc;
248 $this->amount_ht = $this->total_ht;
249 $this->amount_tva = $this->total_tva;
250 $this->amount_ttc = $this->total_ttc;
252 $this->multicurrency_total_ht = $this->multicurrency_subprice = $obj->multicurrency_amount_ht;
253 $this->multicurrency_total_tva = $obj->multicurrency_amount_tva;
254 $this->multicurrency_total_ttc = $obj->multicurrency_amount_ttc;
256 $this->multicurrency_amount_ht = $this->multicurrency_total_ht;
257 $this->multicurrency_amount_tva = $this->multicurrency_total_tva;
258 $this->multicurrency_amount_ttc = $this->multicurrency_total_ttc;
260 $this->tva_tx = $obj->tva_tx;
261 $this->vat_src_code = $obj->vat_src_code;
263 $this->fk_user = $obj->fk_user;
264 $this->fk_facture_line = $obj->fk_facture_line;
265 $this->fk_facture = $obj->fk_facture;
266 $this->fk_facture_source = $obj->fk_facture_source;
267 $this->ref_facture_source = $obj->ref_facture_source;
268 $this->type_facture_source = $obj->type_facture_source;
269 $this->fk_invoice_supplier_line = $obj->fk_invoice_supplier_line;
270 $this->fk_invoice_supplier = $obj->fk_invoice_supplier;
271 $this->fk_invoice_supplier_source = $obj->fk_invoice_supplier_source;
272 $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source;
273 $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source;
275 $this->datec = $this->db->jdate($obj->datec);
277 $this->db->free($resql);
280 $this->db->free($resql);
284 $this->error = $this->db->error();
301 $this->amount_ht =
price2num($this->amount_ht);
302 $this->amount_tva =
price2num($this->amount_tva);
303 $this->amount_ttc =
price2num($this->amount_ttc);
305 $this->tva_tx =
price2num($this->tva_tx);
307 $this->multicurrency_amount_ht =
price2num($this->multicurrency_amount_ht);
308 $this->multicurrency_amount_tva =
price2num($this->multicurrency_amount_tva);
309 $this->multicurrency_amount_ttc =
price2num($this->multicurrency_amount_ttc);
311 if (empty($this->multicurrency_amount_ht)) {
312 $this->multicurrency_amount_ht = 0;
314 if (empty($this->multicurrency_amount_tva)) {
315 $this->multicurrency_amount_tva = 0;
317 if (empty($this->multicurrency_amount_ttc)) {
318 $this->multicurrency_amount_ttc = 0;
320 if (empty($this->tva_tx)) {
326 $this->error =
'BadValueForPropertyDescriptionOfDiscount';
327 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
332 if (!($userid > 0)) {
333 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
334 $tmpinvoice =
new Facture($this->db);
335 $tmpinvoice->fetch($this->fk_facture_source);
336 $userid = $tmpinvoice->fk_user_author;
340 $sql =
"INSERT INTO ".$this->db->prefix().
"societe_remise_except";
341 $sql .=
" (entity, datec, fk_soc, discount_type, fk_user, description,";
342 $sql .=
" amount_ht, amount_tva, amount_ttc, tva_tx, vat_src_code,";
343 $sql .=
" multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,";
344 $sql .=
" fk_facture_source, fk_invoice_supplier_source";
346 $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).
"',";
347 $sql .=
" ".price2num($this->amount_ht).
", ".
price2num($this->amount_tva).
", ".
price2num($this->amount_ttc).
", ".
price2num($this->tva_tx).
", '".$this->db->escape($this->vat_src_code).
"',";
348 $sql .=
" ".price2num($this->multicurrency_amount_ht).
", ".
price2num($this->multicurrency_amount_tva).
", ".
price2num($this->multicurrency_amount_ttc).
", ";
349 $sql .=
" ".($this->fk_facture_source ? ((int) $this->fk_facture_source) :
"null").
",";
350 $sql .=
" ".($this->fk_invoice_supplier_source ? ((int) $this->fk_invoice_supplier_source) :
"null");
353 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
354 $resql = $this->db->query($sql);
356 $this->
id = $this->db->last_insert_id($this->db->prefix().
"societe_remise_except");
359 $this->error = $this->db->lasterror().
' - sql='.$sql;
371 public function delete($user)
373 global
$conf, $langs;
376 if ($this->fk_facture_source) {
377 $sql =
"SELECT COUNT(rowid) as nb";
378 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except";
379 $sql .=
" WHERE (fk_facture_line IS NOT NULL";
380 $sql .=
" OR fk_facture IS NOT NULL)";
381 $sql .=
" AND fk_facture_source = ".((int) $this->fk_facture_source);
384 dol_syslog(get_class($this).
"::delete Check if we can remove discount", LOG_DEBUG);
385 $resql = $this->db->query($sql);
387 $obj = $this->db->fetch_object($resql);
389 $this->error =
'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
399 if ($this->fk_invoice_supplier_source) {
400 $sql =
"SELECT COUNT(rowid) as nb";
401 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except";
402 $sql .=
" WHERE (fk_invoice_supplier_line IS NOT NULL";
403 $sql .=
" OR fk_invoice_supplier IS NOT NULL)";
404 $sql .=
" AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
407 dol_syslog(get_class($this).
"::delete Check if we can remove discount", LOG_DEBUG);
408 $resql = $this->db->query($sql);
410 $obj = $this->db->fetch_object($resql);
412 $this->error =
'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
424 $sql =
"DELETE FROM ".$this->db->prefix().
"societe_remise_except ";
425 if ($this->fk_facture_source) {
426 $sql .=
" WHERE fk_facture_source = ".((int) $this->fk_facture_source);
427 } elseif ($this->fk_invoice_supplier_source) {
428 $sql .=
" WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
430 $sql .=
" WHERE rowid = ".((int) $this->
id);
432 $sql .=
" AND (fk_facture_line IS NULL";
433 $sql .=
" AND fk_facture IS NULL)";
434 $sql .=
" AND (fk_invoice_supplier_line IS NULL";
435 $sql .=
" AND fk_invoice_supplier IS NULL)";
437 dol_syslog(get_class($this).
"::delete Delete discount", LOG_DEBUG);
439 require_once DOL_DOCUMENT_ROOT.
'/core/class/commoninvoice.class.php';
440 $result = $this->db->query($sql);
443 if ($this->fk_facture_source) {
444 $sql =
"UPDATE ".$this->db->prefix().
"facture";
445 $sql .=
" set paye=0, fk_statut=1";
448 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
449 $result = $this->db->query($sql);
454 $this->error = $this->db->lasterror();
455 $this->db->rollback();
458 } elseif ($this->fk_invoice_supplier_source) {
459 $sql =
"UPDATE ".$this->db->prefix().
"facture_fourn";
460 $sql .=
" set paye=0, fk_statut=1";
463 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
464 $result = $this->db->query($sql);
469 $this->error = $this->db->lasterror();
470 $this->db->rollback();
478 $this->error = $this->db->lasterror();
479 $this->db->rollback();
503 if (!$rowidline && !$rowidinvoice) {
504 $this->error =
'ErrorBadParameters';
507 if ($rowidline && $rowidinvoice) {
508 $this->error =
'ErrorBadParameters';
512 $sql =
"UPDATE ".$this->db->prefix().
"societe_remise_except";
513 if (!empty($this->discount_type)) {
515 $sql .=
" SET fk_invoice_supplier_line = ".((int) $rowidline);
518 $sql .=
" SET fk_invoice_supplier = ".((int) $rowidinvoice);
522 $sql .=
" SET fk_facture_line = ".((int) $rowidline);
525 $sql .=
" SET fk_facture = ".((int) $rowidinvoice);
528 $sql .=
" WHERE rowid = ".((int) $this->
id);
530 dol_syslog(get_class($this).
"::link_to_invoice", LOG_DEBUG);
531 $resql = $this->db->query($sql);
533 if (!empty($this->discount_type)) {
534 $this->fk_invoice_supplier_line = $rowidline;
535 $this->fk_invoice_supplier = $rowidinvoice;
537 $this->fk_facture_line = $rowidline;
538 $this->fk_facture = $rowidinvoice;
542 $result = $this->
call_trigger(
'DISCOUNT_MODIFY', $user);
550 $this->error = $this->db->error();
569 $sql =
"UPDATE ".$this->db->prefix().
"societe_remise_except";
570 if (!empty($this->discount_type)) {
571 $sql .=
" SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
573 $sql .=
" SET fk_facture_line = NULL, fk_facture = NULL";
575 $sql .=
" WHERE rowid = ".((int) $this->
id);
577 dol_syslog(get_class($this).
"::unlink_invoice", LOG_DEBUG);
578 $resql = $this->db->query($sql);
582 $result = $this->
call_trigger(
'DISCOUNT_MODIFY', $user);
590 $this->error = $this->db->error();
607 public function getAvailableDiscounts($company =
null, $user =
null, $filter =
'', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
611 dol_syslog(get_class($this).
"::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
613 $sql =
"SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
614 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
615 $sql .=
" WHERE rc.entity = ".$conf->entity;
616 $sql .=
" AND rc.discount_type=".((int) $discount_type);
617 if (!empty($discount_type)) {
618 $sql .=
" AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)";
620 $sql .=
" AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)";
622 if (is_object($company)) {
623 $sql .=
" AND rc.fk_soc = ".((int) $company->id);
625 if (is_object($user)) {
626 $sql .=
" AND rc.fk_user = ".((int) $user->id);
629 $sql .=
" AND (".$filter.
")";
632 $sql .=
' AND rc.amount_ttc <= '.((float)
price2num($maxvalue));
635 $resql = $this->db->query($sql);
637 $obj = $this->db->fetch_object($resql);
643 if ($multicurrency) {
644 return $obj->multicurrency_amount;
663 dol_syslog(get_class($this).
"::getSumDepositsUsed", LOG_DEBUG);
665 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
666 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
667 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture as f";
668 $sql .=
" WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
669 $sql .=
" AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
670 } elseif ($invoice->element ==
'invoice_supplier') {
671 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
672 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture_fourn as f";
673 $sql .=
" WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
674 $sql .=
" AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
676 $this->error = get_class($this).
"::getSumDepositsUsed was called with a bad object as a first parameter";
681 $resql = $this->db->query($sql);
683 $obj = $this->db->fetch_object($resql);
684 if ($multicurrency == 1) {
685 return $obj->multicurrency_amount;
690 $this->error = $this->db->lasterror();
704 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
706 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
707 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
708 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture as f";
709 $sql .=
" WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
710 $sql .=
" AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.
", ".$invoice::TYPE_CREDIT_NOTE.
", ".$invoice::TYPE_SITUATION).
")";
711 } elseif ($invoice->element ==
'invoice_supplier') {
712 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
713 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture_fourn as f";
714 $sql .=
" WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
715 $sql .=
" AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.
", ".$invoice::TYPE_CREDIT_NOTE).
")";
717 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
722 $resql = $this->db->query($sql);
724 $obj = $this->db->fetch_object($resql);
725 if ($multicurrency == 1) {
726 return $obj->multicurrency_amount;
731 $this->error = $this->db->lasterror();
744 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
746 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
747 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
748 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
749 $sql .=
" WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
750 } elseif ($invoice->element ==
'invoice_supplier') {
751 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
752 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
753 $sql .=
" WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
755 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
760 $resql = $this->db->query($sql);
762 $obj = $this->db->fetch_object($resql);
763 if ($multicurrency) {
764 return $obj->multicurrency_amount;
769 $this->error = $this->db->lasterror();
781 public function getNomUrl($withpicto = 0, $option =
'invoice')
792 if ($option ==
'invoice') {
793 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
794 $link = !empty($this->discount_type) ?
'/fourn/facture/card.php' :
'/compta/facture/card.php';
795 $label = $langs->trans(
"ShowSourceInvoice").
': '.$this->ref_facture_source;
796 $link =
'<a href="'.DOL_URL_ROOT.$link.
'?facid='.$facid.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
798 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
801 if ($option ==
'discount') {
802 $label = $langs->trans(
"Discount");
803 $link =
'<a href="'.DOL_URL_ROOT.
'/comm/remx.php?id='.$this->socid.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
805 $ref = $langs->trans(
"Discount");
811 $result .= ($link.img_object($label, $picto,
'class="classfortooltip"').$linkend);
813 if ($withpicto && $withpicto != 2) {
816 $result .= $link.$ref.$linkend;
831 $this->amount_ht = 10;
832 $this->amount_tva = 1.96;
833 $this->amount_ttc = 11.96;
834 $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...