28require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
48 public $discount_type;
57 public $multicurrency_total_ht;
58 public $multicurrency_total_tva;
59 public $multicurrency_total_ttc;
60 public $multicurrency_amount_ht;
61 public $multicurrency_amount_tva;
62 public $multicurrency_amount_ttc;
67 public $multicurrency_subprice;
72 public $fk_invoice_supplier;
77 public $fk_invoice_supplier_line;
103 public $fk_facture_line;
113 public $fk_facture_source;
114 public $ref_facture_source;
115 public $type_facture_source;
117 public $fk_invoice_supplier_source;
118 public $ref_invoice_supplier_source;
119 public $type_invoice_supplier_source;
140 public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
143 if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
144 $this->error =
'ErrorBadParameters';
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').
")";
161 $sql .=
" AND sr.rowid = ".((int) $rowid);
163 if ($fk_facture_source) {
164 $sql .=
" AND sr.fk_facture_source = ".((int) $fk_facture_source);
166 if ($fk_invoice_supplier_source) {
167 $sql .=
" AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
170 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
171 $resql = $this->db->query($sql);
173 if ($this->db->num_rows($resql)) {
174 $obj = $this->db->fetch_object($resql);
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;
181 $this->total_ht = $obj->amount_ht;
182 $this->total_tva = $obj->amount_tva;
183 $this->total_ttc = $obj->amount_ttc;
185 $this->amount_ht = $this->total_ht;
186 $this->amount_tva = $this->total_tva;
187 $this->amount_ttc = $this->total_ttc;
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;
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;
197 $this->tva_tx = $obj->tva_tx;
198 $this->vat_src_code = $obj->vat_src_code;
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;
204 $this->ref_facture_source = $obj->ref_facture_source;
205 $this->type_facture_source = $obj->type_facture_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;
209 $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source;
210 $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source;
212 $this->datec = $this->db->jdate($obj->datec);
214 $this->db->free($resql);
217 $this->db->free($resql);
221 $this->error = $this->db->error();
235 global $conf, $langs;
238 $this->amount_ht =
price2num($this->amount_ht);
239 $this->amount_tva =
price2num($this->amount_tva);
240 $this->amount_ttc =
price2num($this->amount_ttc);
242 $this->tva_tx =
price2num($this->tva_tx);
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);
248 if (empty($this->multicurrency_amount_ht)) {
249 $this->multicurrency_amount_ht = 0;
251 if (empty($this->multicurrency_amount_tva)) {
252 $this->multicurrency_amount_tva = 0;
254 if (empty($this->multicurrency_amount_ttc)) {
255 $this->multicurrency_amount_ttc = 0;
257 if (empty($this->tva_tx)) {
263 $this->error =
'BadValueForPropertyDescriptionOfDiscount';
264 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
269 if (!($userid > 0)) {
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;
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";
283 $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).
"',";
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");
290 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
291 $resql = $this->db->query($sql);
293 $this->
id = $this->db->last_insert_id($this->db->prefix().
"societe_remise_except");
296 $this->error = $this->db->lasterror().
' - sql='.$sql;
308 public function delete($user)
310 global $conf, $langs;
313 if ($this->fk_facture_source) {
314 $sql =
"SELECT COUNT(rowid) as nb";
315 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except";
316 $sql .=
" WHERE (fk_facture_line IS NOT NULL";
317 $sql .=
" OR fk_facture IS NOT NULL)";
318 $sql .=
" AND fk_facture_source = ".((int) $this->fk_facture_source);
321 dol_syslog(get_class($this).
"::delete Check if we can remove discount", LOG_DEBUG);
322 $resql = $this->db->query($sql);
324 $obj = $this->db->fetch_object($resql);
326 $this->error =
'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
336 if ($this->fk_invoice_supplier_source) {
337 $sql =
"SELECT COUNT(rowid) as nb";
338 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except";
339 $sql .=
" WHERE (fk_invoice_supplier_line IS NOT NULL";
340 $sql .=
" OR fk_invoice_supplier IS NOT NULL)";
341 $sql .=
" AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
344 dol_syslog(get_class($this).
"::delete Check if we can remove discount", LOG_DEBUG);
345 $resql = $this->db->query($sql);
347 $obj = $this->db->fetch_object($resql);
349 $this->error =
'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
361 $sql =
"DELETE FROM ".$this->db->prefix().
"societe_remise_except ";
362 if ($this->fk_facture_source) {
363 $sql .=
" WHERE fk_facture_source = ".((int) $this->fk_facture_source);
364 } elseif ($this->fk_invoice_supplier_source) {
365 $sql .=
" WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
367 $sql .=
" WHERE rowid = ".((int) $this->
id);
369 $sql .=
" AND (fk_facture_line IS NULL";
370 $sql .=
" AND fk_facture IS NULL)";
371 $sql .=
" AND (fk_invoice_supplier_line IS NULL";
372 $sql .=
" AND fk_invoice_supplier IS NULL)";
374 dol_syslog(get_class($this).
"::delete Delete discount", LOG_DEBUG);
375 require_once DOL_DOCUMENT_ROOT.
'/core/class/commoninvoice.class.php';
376 $result = $this->db->query($sql);
379 if ($this->fk_facture_source) {
380 $sql =
"UPDATE ".$this->db->prefix().
"facture";
381 $sql .=
" set paye=0, fk_statut=1";
384 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
385 $result = $this->db->query($sql);
390 $this->error = $this->db->lasterror();
391 $this->db->rollback();
394 } elseif ($this->fk_invoice_supplier_source) {
395 $sql =
"UPDATE ".$this->db->prefix().
"facture_fourn";
396 $sql .=
" set paye=0, fk_statut=1";
399 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
400 $result = $this->db->query($sql);
405 $this->error = $this->db->lasterror();
406 $this->db->rollback();
414 $this->error = $this->db->lasterror();
415 $this->db->rollback();
436 if (!$rowidline && !$rowidinvoice) {
437 $this->error =
'ErrorBadParameters';
440 if ($rowidline && $rowidinvoice) {
441 $this->error =
'ErrorBadParameters';
445 $sql =
"UPDATE ".$this->db->prefix().
"societe_remise_except";
446 if (!empty($this->discount_type)) {
448 $sql .=
" SET fk_invoice_supplier_line = ".((int) $rowidline);
451 $sql .=
" SET fk_invoice_supplier = ".((int) $rowidinvoice);
455 $sql .=
" SET fk_facture_line = ".((int) $rowidline);
458 $sql .=
" SET fk_facture = ".((int) $rowidinvoice);
461 $sql .=
" WHERE rowid = ".((int) $this->
id);
463 dol_syslog(get_class($this).
"::link_to_invoice", LOG_DEBUG);
464 $resql = $this->db->query($sql);
466 if (!empty($this->discount_type)) {
467 $this->fk_invoice_supplier_line = $rowidline;
468 $this->fk_invoice_supplier = $rowidinvoice;
470 $this->fk_facture_line = $rowidline;
471 $this->fk_facture = $rowidinvoice;
475 $this->error = $this->db->error();
491 $sql =
"UPDATE ".$this->db->prefix().
"societe_remise_except";
492 if (!empty($this->discount_type)) {
493 $sql .=
" SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
495 $sql .=
" SET fk_facture_line = NULL, fk_facture = NULL";
497 $sql .=
" WHERE rowid = ".((int) $this->
id);
499 dol_syslog(get_class($this).
"::unlink_invoice", LOG_DEBUG);
500 $resql = $this->db->query($sql);
504 $this->error = $this->db->error();
521 public function getAvailableDiscounts($company =
null, $user =
null, $filter =
'', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
525 dol_syslog(get_class($this).
"::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
527 $sql =
"SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
528 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
529 $sql .=
" WHERE rc.entity = ".$conf->entity;
530 $sql .=
" AND rc.discount_type=".((int) $discount_type);
531 if (!empty($discount_type)) {
532 $sql .=
" AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)";
534 $sql .=
" AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)";
536 if (is_object($company)) {
537 $sql .=
" AND rc.fk_soc = ".((int) $company->id);
539 if (is_object($user)) {
540 $sql .=
" AND rc.fk_user = ".((int) $user->id);
543 $sql .=
" AND (".$filter.
")";
546 $sql .=
' AND rc.amount_ttc <= '.((float)
price2num($maxvalue));
549 $resql = $this->db->query($sql);
551 $obj = $this->db->fetch_object($resql);
557 if ($multicurrency) {
558 return $obj->multicurrency_amount;
577 dol_syslog(get_class($this).
"::getSumDepositsUsed", LOG_DEBUG);
579 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
580 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
581 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture as f";
582 $sql .=
" WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
583 $sql .=
" AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
584 } elseif ($invoice->element ==
'invoice_supplier') {
585 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
586 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture_fourn as f";
587 $sql .=
" WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
588 $sql .=
" AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
590 $this->error = get_class($this).
"::getSumDepositsUsed was called with a bad object as a first parameter";
595 $resql = $this->db->query($sql);
597 $obj = $this->db->fetch_object($resql);
598 if ($multicurrency == 1) {
599 return $obj->multicurrency_amount;
604 $this->error = $this->db->lasterror();
618 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
620 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
621 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
622 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture as f";
623 $sql .=
" WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
624 $sql .=
" AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.
", ".$invoice::TYPE_CREDIT_NOTE.
", ".$invoice::TYPE_SITUATION).
")";
625 } elseif ($invoice->element ==
'invoice_supplier') {
626 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
627 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture_fourn as f";
628 $sql .=
" WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
629 $sql .=
" AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.
", ".$invoice::TYPE_CREDIT_NOTE).
")";
631 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
636 $resql = $this->db->query($sql);
638 $obj = $this->db->fetch_object($resql);
639 if ($multicurrency == 1) {
640 return $obj->multicurrency_amount;
645 $this->error = $this->db->lasterror();
658 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
660 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
661 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
662 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
663 $sql .=
" WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
664 } elseif ($invoice->element ==
'invoice_supplier') {
665 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
666 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
667 $sql .=
" WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
669 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
674 $resql = $this->db->query($sql);
676 $obj = $this->db->fetch_object($resql);
677 if ($multicurrency) {
678 return $obj->multicurrency_amount;
683 $this->error = $this->db->lasterror();
695 public function getNomUrl($withpicto, $option =
'invoice')
706 if ($option ==
'invoice') {
707 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
708 $link = !empty($this->discount_type) ?
'/fourn/facture/card.php' :
'/compta/facture/card.php';
709 $label = $langs->trans(
"ShowSourceInvoice").
': '.$this->ref_facture_source;
710 $link =
'<a href="'.DOL_URL_ROOT.$link.
'?facid='.$facid.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
712 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
715 if ($option ==
'discount') {
716 $label = $langs->trans(
"Discount");
717 $link =
'<a href="'.DOL_URL_ROOT.
'/comm/remx.php?id='.$this->socid.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
719 $ref = $langs->trans(
"Discount");
725 $result .= ($link.img_object($label, $picto,
'class="classfortooltip"').$linkend);
727 if ($withpicto && $withpicto != 2) {
730 $result .= $link.$ref.$linkend;
746 $this->amount_ht = 10;
747 $this->amount_tva = 1.96;
748 $this->amount_ttc = 11.96;
749 $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,...
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...