44 public $errors = array();
56 public $discount_type;
65 public $multicurrency_total_ht;
66 public $multicurrency_total_tva;
67 public $multicurrency_total_ttc;
68 public $multicurrency_amount_ht;
69 public $multicurrency_amount_tva;
70 public $multicurrency_amount_ttc;
96 public $fk_facture_line;
106 public $fk_facture_source;
107 public $ref_facture_source;
108 public $type_facture_source;
110 public $fk_invoice_supplier_source;
111 public $ref_invoice_supplier_source;
112 public $type_invoice_supplier_source;
133 public function fetch($rowid, $fk_facture_source = 0, $fk_invoice_supplier_source = 0)
138 if (!$rowid && !$fk_facture_source && !$fk_invoice_supplier_source) {
139 $this->error =
'ErrorBadParameters';
143 $sql =
"SELECT sr.rowid, sr.fk_soc, sr.discount_type,";
144 $sql .=
" sr.fk_user,";
145 $sql .=
" sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx, sr.vat_src_code,";
146 $sql .=
" sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
147 $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,";
148 $sql .=
" sr.datec,";
149 $sql .=
" f.ref as ref_facture_source, f.type as type_facture_source,";
150 $sql .=
" fsup.ref as ref_invoice_supplier_source, fsup.type as type_invoice_supplier_source";
151 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as sr";
152 $sql .=
" LEFT JOIN ".$this->db->prefix().
"facture as f ON sr.fk_facture_source = f.rowid";
153 $sql .=
" LEFT JOIN ".$this->db->prefix().
"facture_fourn as fsup ON sr.fk_invoice_supplier_source = fsup.rowid";
154 $sql .=
" WHERE sr.entity IN (".getEntity(
'invoice').
")";
156 $sql .=
" AND sr.rowid = ".((int) $rowid);
158 if ($fk_facture_source) {
159 $sql .=
" AND sr.fk_facture_source = ".((int) $fk_facture_source);
161 if ($fk_invoice_supplier_source) {
162 $sql .=
" AND sr.fk_invoice_supplier_source = ".((int) $fk_invoice_supplier_source);
165 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
166 $resql = $this->db->query($sql);
168 if ($this->db->num_rows($resql)) {
169 $obj = $this->db->fetch_object($resql);
171 $this->
id = $obj->rowid;
172 $this->fk_soc = $obj->fk_soc;
173 $this->discount_type = $obj->discount_type;
175 $this->total_ht = $obj->amount_ht;
176 $this->total_tva = $obj->amount_tva;
177 $this->total_ttc = $obj->amount_ttc;
179 $this->amount_ht = $this->total_ht;
180 $this->amount_tva = $this->total_tva;
181 $this->amount_ttc = $this->total_ttc;
183 $this->multicurrency_total_ht = $this->multicurrency_subprice = $obj->multicurrency_amount_ht;
184 $this->multicurrency_total_tva = $obj->multicurrency_amount_tva;
185 $this->multicurrency_total_ttc = $obj->multicurrency_amount_ttc;
187 $this->multicurrency_amount_ht = $this->multicurrency_total_ht;
188 $this->multicurrency_amount_tva = $this->multicurrency_total_tva;
189 $this->multicurrency_amount_ttc = $this->multicurrency_total_ttc;
191 $this->tva_tx = $obj->tva_tx;
192 $this->vat_src_code = $obj->vat_src_code;
194 $this->fk_user = $obj->fk_user;
195 $this->fk_facture_line = $obj->fk_facture_line;
196 $this->fk_facture = $obj->fk_facture;
197 $this->fk_facture_source = $obj->fk_facture_source;
198 $this->ref_facture_source = $obj->ref_facture_source;
199 $this->type_facture_source = $obj->type_facture_source;
200 $this->fk_invoice_supplier_line = $obj->fk_invoice_supplier_line;
201 $this->fk_invoice_supplier = $obj->fk_invoice_supplier;
202 $this->fk_invoice_supplier_source = $obj->fk_invoice_supplier_source;
203 $this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source;
204 $this->type_invoice_supplier_source = $obj->type_invoice_supplier_source;
206 $this->datec = $this->db->jdate($obj->datec);
208 $this->db->free($resql);
211 $this->db->free($resql);
215 $this->error = $this->db->error();
229 global $conf, $langs;
232 $this->amount_ht =
price2num($this->amount_ht);
233 $this->amount_tva =
price2num($this->amount_tva);
234 $this->amount_ttc =
price2num($this->amount_ttc);
236 $this->tva_tx =
price2num($this->tva_tx);
238 $this->multicurrency_amount_ht =
price2num($this->multicurrency_amount_ht);
239 $this->multicurrency_amount_tva =
price2num($this->multicurrency_amount_tva);
240 $this->multicurrency_amount_ttc =
price2num($this->multicurrency_amount_ttc);
242 if (empty($this->multicurrency_amount_ht)) {
243 $this->multicurrency_amount_ht = 0;
245 if (empty($this->multicurrency_amount_tva)) {
246 $this->multicurrency_amount_tva = 0;
248 if (empty($this->multicurrency_amount_ttc)) {
249 $this->multicurrency_amount_ttc = 0;
251 if (empty($this->tva_tx)) {
257 $this->error =
'BadValueForPropertyDescriptionOfDiscount';
258 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
263 if (!($userid > 0)) {
264 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
265 $tmpinvoice =
new Facture($this->db);
266 $tmpinvoice->fetch($this->fk_facture_source);
267 $userid = $tmpinvoice->fk_user_author;
271 $sql =
"INSERT INTO ".$this->db->prefix().
"societe_remise_except";
272 $sql .=
" (entity, datec, fk_soc, discount_type, fk_user, description,";
273 $sql .=
" amount_ht, amount_tva, amount_ttc, tva_tx, vat_src_code,";
274 $sql .=
" multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,";
275 $sql .=
" fk_facture_source, fk_invoice_supplier_source";
277 $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).
"',";
278 $sql .=
" ".price2num($this->amount_ht).
", ".
price2num($this->amount_tva).
", ".
price2num($this->amount_ttc).
", ".
price2num($this->tva_tx).
", '".$this->db->escape($this->vat_src_code).
"',";
279 $sql .=
" ".price2num($this->multicurrency_amount_ht).
", ".
price2num($this->multicurrency_amount_tva).
", ".
price2num($this->multicurrency_amount_ttc).
", ";
280 $sql .=
" ".($this->fk_facture_source ? ((int) $this->fk_facture_source) :
"null").
",";
281 $sql .=
" ".($this->fk_invoice_supplier_source ? ((int) $this->fk_invoice_supplier_source) :
"null");
284 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
285 $resql = $this->db->query($sql);
287 $this->
id = $this->db->last_insert_id($this->db->prefix().
"societe_remise_except");
290 $this->error = $this->db->lasterror().
' - sql='.$sql;
302 public function delete($user)
304 global $conf, $langs;
307 if ($this->fk_facture_source) {
308 $sql =
"SELECT COUNT(rowid) as nb";
309 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except";
310 $sql .=
" WHERE (fk_facture_line IS NOT NULL";
311 $sql .=
" OR fk_facture IS NOT NULL)";
312 $sql .=
" AND fk_facture_source = ".((int) $this->fk_facture_source);
315 dol_syslog(get_class($this).
"::delete Check if we can remove discount", LOG_DEBUG);
316 $resql = $this->db->query($sql);
318 $obj = $this->db->fetch_object($resql);
320 $this->error =
'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
330 if ($this->fk_invoice_supplier_source) {
331 $sql =
"SELECT COUNT(rowid) as nb";
332 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except";
333 $sql .=
" WHERE (fk_invoice_supplier_line IS NOT NULL";
334 $sql .=
" OR fk_invoice_supplier IS NOT NULL)";
335 $sql .=
" AND fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
338 dol_syslog(get_class($this).
"::delete Check if we can remove discount", LOG_DEBUG);
339 $resql = $this->db->query($sql);
341 $obj = $this->db->fetch_object($resql);
343 $this->error =
'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
355 $sql =
"DELETE FROM ".$this->db->prefix().
"societe_remise_except ";
356 if ($this->fk_facture_source) {
357 $sql .=
" WHERE fk_facture_source = ".((int) $this->fk_facture_source);
358 } elseif ($this->fk_invoice_supplier_source) {
359 $sql .=
" WHERE fk_invoice_supplier_source = ".((int) $this->fk_invoice_supplier_source);
361 $sql .=
" WHERE rowid = ".((int) $this->
id);
363 $sql .=
" AND (fk_facture_line IS NULL";
364 $sql .=
" AND fk_facture IS NULL)";
365 $sql .=
" AND (fk_invoice_supplier_line IS NULL";
366 $sql .=
" AND fk_invoice_supplier IS NULL)";
368 dol_syslog(get_class($this).
"::delete Delete discount", LOG_DEBUG);
369 require_once DOL_DOCUMENT_ROOT.
'/core/class/commoninvoice.class.php';
370 $result = $this->db->query($sql);
373 if ($this->fk_facture_source) {
374 $sql =
"UPDATE ".$this->db->prefix().
"facture";
375 $sql .=
" set paye=0, fk_statut=1";
378 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
379 $result = $this->db->query($sql);
384 $this->error = $this->db->lasterror();
385 $this->db->rollback();
388 } elseif ($this->fk_invoice_supplier_source) {
389 $sql =
"UPDATE ".$this->db->prefix().
"facture_fourn";
390 $sql .=
" set paye=0, fk_statut=1";
393 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
394 $result = $this->db->query($sql);
399 $this->error = $this->db->lasterror();
400 $this->db->rollback();
408 $this->error = $this->db->lasterror();
409 $this->db->rollback();
430 if (!$rowidline && !$rowidinvoice) {
431 $this->error =
'ErrorBadParameters';
434 if ($rowidline && $rowidinvoice) {
435 $this->error =
'ErrorBadParameters';
439 $sql =
"UPDATE ".$this->db->prefix().
"societe_remise_except";
440 if (!empty($this->discount_type)) {
442 $sql .=
" SET fk_invoice_supplier_line = ".((int) $rowidline);
445 $sql .=
" SET fk_invoice_supplier = ".((int) $rowidinvoice);
449 $sql .=
" SET fk_facture_line = ".((int) $rowidline);
452 $sql .=
" SET fk_facture = ".((int) $rowidinvoice);
455 $sql .=
" WHERE rowid = ".((int) $this->
id);
457 dol_syslog(get_class($this).
"::link_to_invoice", LOG_DEBUG);
458 $resql = $this->db->query($sql);
460 if (!empty($this->discount_type)) {
461 $this->fk_invoice_supplier_line = $rowidline;
462 $this->fk_invoice_supplier = $rowidinvoice;
464 $this->fk_facture_line = $rowidline;
465 $this->fk_facture = $rowidinvoice;
469 $this->error = $this->db->error();
485 $sql =
"UPDATE ".$this->db->prefix().
"societe_remise_except";
486 if (!empty($this->discount_type)) {
487 $sql .=
" SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
489 $sql .=
" SET fk_facture_line = NULL, fk_facture = NULL";
491 $sql .=
" WHERE rowid = ".((int) $this->
id);
493 dol_syslog(get_class($this).
"::unlink_invoice", LOG_DEBUG);
494 $resql = $this->db->query($sql);
498 $this->error = $this->db->error();
515 public function getAvailableDiscounts($company =
'', $user =
'', $filter =
'', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
519 dol_syslog(get_class($this).
"::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
521 $sql =
"SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
522 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
523 $sql .=
" WHERE rc.entity = ".$conf->entity;
524 $sql .=
" AND rc.discount_type=".((int) $discount_type);
525 if (!empty($discount_type)) {
526 $sql .=
" AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)";
528 $sql .=
" AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)";
530 if (is_object($company)) {
531 $sql .=
" AND rc.fk_soc = ".((int) $company->id);
533 if (is_object($user)) {
534 $sql .=
" AND rc.fk_user = ".((int) $user->id);
537 $sql .=
" AND (".$filter.
")";
540 $sql .=
' AND rc.amount_ttc <= '.((float)
price2num($maxvalue));
543 $resql = $this->db->query($sql);
545 $obj = $this->db->fetch_object($resql);
551 if ($multicurrency) {
552 return $obj->multicurrency_amount;
571 dol_syslog(get_class($this).
"::getSumDepositsUsed", LOG_DEBUG);
573 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
574 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
575 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture as f";
576 $sql .=
" WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
577 $sql .=
" AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
578 } elseif ($invoice->element ==
'invoice_supplier') {
579 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
580 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture_fourn as f";
581 $sql .=
" WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
582 $sql .=
" AND f.type = ". (int) $invoice::TYPE_DEPOSIT;
584 $this->error = get_class($this).
"::getSumDepositsUsed was called with a bad object as a first parameter";
589 $resql = $this->db->query($sql);
591 $obj = $this->db->fetch_object($resql);
592 if ($multicurrency == 1) {
593 return $obj->multicurrency_amount;
598 $this->error = $this->db->lasterror();
612 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
614 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
615 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
616 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture as f";
617 $sql .=
" WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
618 $sql .=
" AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.
", ".$invoice::TYPE_CREDIT_NOTE.
", ".$invoice::TYPE_SITUATION).
")";
619 } elseif ($invoice->element ==
'invoice_supplier') {
620 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
621 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->db->prefix().
"facture_fourn as f";
622 $sql .=
" WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
623 $sql .=
" AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.
", ".$invoice::TYPE_CREDIT_NOTE).
")";
625 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
630 $resql = $this->db->query($sql);
632 $obj = $this->db->fetch_object($resql);
633 if ($multicurrency == 1) {
634 return $obj->multicurrency_amount;
639 $this->error = $this->db->lasterror();
652 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
654 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
655 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
656 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
657 $sql .=
" WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
658 } elseif ($invoice->element ==
'invoice_supplier') {
659 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
660 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
661 $sql .=
" WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
663 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
668 $resql = $this->db->query($sql);
670 $obj = $this->db->fetch_object($resql);
671 if ($multicurrency) {
672 return $obj->multicurrency_amount;
677 $this->error = $this->db->lasterror();
689 public function getNomUrl($withpicto, $option =
'invoice')
700 if ($option ==
'invoice') {
701 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
702 $link = !empty($this->discount_type) ?
'/fourn/facture/card.php' :
'/compta/facture/card.php';
703 $label = $langs->trans(
"ShowSourceInvoice").
': '.$this->ref_facture_source;
704 $link =
'<a href="'.DOL_URL_ROOT.$link.
'?facid='.$facid.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
706 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
709 if ($option ==
'discount') {
710 $label = $langs->trans(
"Discount");
711 $link =
'<a href="'.DOL_URL_ROOT.
'/comm/remx.php?id='.$this->fk_soc.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
713 $ref = $langs->trans(
"Discount");
719 $result .= ($link.img_object($label, $picto,
'class="classfortooltip"').$linkend);
721 if ($withpicto && $withpicto != 2) {
724 $result .= $link.$ref.$linkend;
738 global $user, $langs, $conf;
741 $this->amount_ht = 10;
742 $this->amount_tva = 1.96;
743 $this->amount_ttc = 11.96;
744 $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='', $user='', $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...