44 public $errors = array();
61 public $discount_type;
70 public $multicurrency_total_ht;
71 public $multicurrency_total_tva;
72 public $multicurrency_total_ttc;
73 public $multicurrency_amount_ht;
74 public $multicurrency_amount_tva;
75 public $multicurrency_amount_ttc;
80 public $multicurrency_subprice;
85 public $fk_invoice_supplier;
90 public $fk_invoice_supplier_line;
116 public $fk_facture_line;
126 public $fk_facture_source;
127 public $ref_facture_source;
128 public $type_facture_source;
130 public $fk_invoice_supplier_source;
131 public $ref_invoice_supplier_source;
132 public $type_invoice_supplier_source;
153 public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
156 if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
157 $this->error =
'ErrorBadParameters';
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').
")";
174 $sql .=
" AND sr.rowid = ".((int) $rowid);
176 if ($fk_facture_source) {
177 $sql .=
" AND sr.fk_facture_source = ".((int) $fk_facture_source);
179 if ($fk_invoice_supplier_source) {
180 $sql .=
" AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
183 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
184 $resql = $this->db->query($sql);
186 if ($this->db->num_rows($resql)) {
187 $obj = $this->db->fetch_object($resql);
189 $this->
id = $obj->rowid;
190 $this->fk_soc = $obj->fk_soc;
191 $this->discount_type = $obj->discount_type;
193 $this->total_ht = $obj->amount_ht;
194 $this->total_tva = $obj->amount_tva;
195 $this->total_ttc = $obj->amount_ttc;
197 $this->amount_ht = $this->total_ht;
198 $this->amount_tva = $this->total_tva;
199 $this->amount_ttc = $this->total_ttc;
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;
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;
209 $this->tva_tx = $obj->tva_tx;
210 $this->vat_src_code = $obj->vat_src_code;
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;
216 $this->ref_facture_source = $obj->ref_facture_source;
217 $this->type_facture_source = $obj->type_facture_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;
221 $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source;
222 $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source;
224 $this->datec = $this->db->jdate($obj->datec);
226 $this->db->free($resql);
229 $this->db->free($resql);
233 $this->error = $this->db->error();
247 global $conf, $langs;
250 $this->amount_ht =
price2num($this->amount_ht);
251 $this->amount_tva =
price2num($this->amount_tva);
252 $this->amount_ttc =
price2num($this->amount_ttc);
254 $this->tva_tx =
price2num($this->tva_tx);
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);
260 if (empty($this->multicurrency_amount_ht)) {
261 $this->multicurrency_amount_ht = 0;
263 if (empty($this->multicurrency_amount_tva)) {
264 $this->multicurrency_amount_tva = 0;
266 if (empty($this->multicurrency_amount_ttc)) {
267 $this->multicurrency_amount_ttc = 0;
269 if (empty($this->tva_tx)) {
275 $this->error =
'BadValueForPropertyDescriptionOfDiscount';
276 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
281 if (!($userid > 0)) {
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;
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";
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");
302 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
303 $resql = $this->db->query($sql);
305 $this->
id = $this->db->last_insert_id($this->db->prefix().
"societe_remise_except");
308 $this->error = $this->db->lasterror().
' - sql='.$sql;
320 public function delete($user)
322 global $conf, $langs;
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";
329 $sql .=
" OR fk_facture IS NOT NULL)";
330 $sql .=
" AND fk_facture_source = ".((int) $this->fk_facture_source);
333 dol_syslog(get_class($this).
"::delete Check if we can remove discount", LOG_DEBUG);
334 $resql = $this->db->query($sql);
336 $obj = $this->db->fetch_object($resql);
338 $this->error =
'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
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";
352 $sql .=
" OR fk_invoice_supplier IS NOT NULL)";
353 $sql .=
" AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
356 dol_syslog(get_class($this).
"::delete Check if we can remove discount", LOG_DEBUG);
357 $resql = $this->db->query($sql);
359 $obj = $this->db->fetch_object($resql);
361 $this->error =
'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
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);
376 } elseif ($this->fk_invoice_supplier_source) {
377 $sql .=
" WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
379 $sql .=
" WHERE rowid = ".((int) $this->
id);
381 $sql .=
" AND (fk_facture_line IS NULL";
382 $sql .=
" AND fk_facture IS NULL)";
383 $sql .=
" AND (fk_invoice_supplier_line IS NULL";
384 $sql .=
" AND fk_invoice_supplier IS NULL)";
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);
391 if ($this->fk_facture_source) {
392 $sql =
"UPDATE ".$this->db->prefix().
"facture";
393 $sql .=
" set paye=0, fk_statut=1";
396 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
397 $result = $this->db->query($sql);
402 $this->error = $this->db->lasterror();
403 $this->db->rollback();
406 } elseif ($this->fk_invoice_supplier_source) {
407 $sql =
"UPDATE ".$this->db->prefix().
"facture_fourn";
408 $sql .=
" set paye=0, fk_statut=1";
411 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
412 $result = $this->db->query($sql);
417 $this->error = $this->db->lasterror();
418 $this->db->rollback();
426 $this->error = $this->db->lasterror();
427 $this->db->rollback();
448 if (!$rowidline && !$rowidinvoice) {
449 $this->error =
'ErrorBadParameters';
452 if ($rowidline && $rowidinvoice) {
453 $this->error =
'ErrorBadParameters';
457 $sql =
"UPDATE ".$this->db->prefix().
"societe_remise_except";
458 if (!empty($this->discount_type)) {
460 $sql .=
" SET fk_invoice_supplier_line = ".((int) $rowidline);
463 $sql .=
" SET fk_invoice_supplier = ".((int) $rowidinvoice);
467 $sql .=
" SET fk_facture_line = ".((int) $rowidline);
470 $sql .=
" SET fk_facture = ".((int) $rowidinvoice);
473 $sql .=
" WHERE rowid = ".((int) $this->
id);
475 dol_syslog(get_class($this).
"::link_to_invoice", LOG_DEBUG);
476 $resql = $this->db->query($sql);
478 if (!empty($this->discount_type)) {
479 $this->fk_invoice_supplier_line = $rowidline;
480 $this->fk_invoice_supplier = $rowidinvoice;
482 $this->fk_facture_line = $rowidline;
483 $this->fk_facture = $rowidinvoice;
487 $this->error = $this->db->error();
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";
507 $sql .=
" SET fk_facture_line = NULL, fk_facture = NULL";
509 $sql .=
" WHERE rowid = ".((int) $this->
id);
511 dol_syslog(get_class($this).
"::unlink_invoice", LOG_DEBUG);
512 $resql = $this->db->query($sql);
516 $this->error = $this->db->error();
533 public function getAvailableDiscounts($company =
null, $user =
null, $filter =
'', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
537 dol_syslog(get_class($this).
"::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
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)";
546 $sql .=
" AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)";
548 if (is_object($company)) {
549 $sql .=
" AND rc.fk_soc = ".((int) $company->id);
551 if (is_object($user)) {
552 $sql .=
" AND rc.fk_user = ".((int) $user->id);
555 $sql .=
" AND (".$filter.
")";
558 $sql .=
' AND rc.amount_ttc <= '.((float)
price2num($maxvalue));
561 $resql = $this->db->query($sql);
563 $obj = $this->db->fetch_object($resql);
569 if ($multicurrency) {
570 return $obj->multicurrency_amount;
589 dol_syslog(get_class($this).
"::getSumDepositsUsed", LOG_DEBUG);
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;
602 $this->error = get_class($this).
"::getSumDepositsUsed was called with a bad object as a first parameter";
607 $resql = $this->db->query($sql);
609 $obj = $this->db->fetch_object($resql);
610 if ($multicurrency == 1) {
611 return $obj->multicurrency_amount;
616 $this->error = $this->db->lasterror();
630 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
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).
")";
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).
")";
643 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
648 $resql = $this->db->query($sql);
650 $obj = $this->db->fetch_object($resql);
651 if ($multicurrency == 1) {
652 return $obj->multicurrency_amount;
657 $this->error = $this->db->lasterror();
670 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
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);
681 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
686 $resql = $this->db->query($sql);
688 $obj = $this->db->fetch_object($resql);
689 if ($multicurrency) {
690 return $obj->multicurrency_amount;
695 $this->error = $this->db->lasterror();
707 public function getNomUrl($withpicto, $option =
'invoice')
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">';
724 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
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">';
731 $ref = $langs->trans(
"Discount");
737 $result .= ($link.img_object($label, $picto,
'class="classfortooltip"').$linkend);
739 if ($withpicto && $withpicto != 2) {
742 $result .= $link.$ref.$linkend;
758 $this->amount_ht = 10;
759 $this->amount_tva = 1.96;
760 $this->amount_ttc = 11.96;
761 $this->tva_tx = 19.6;
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...