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);
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);
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 =
'BadValueForPropertyDescription';
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';
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);
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);
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);
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 $result = $this->
db->query($sql);
372 if ($this->fk_facture_source) {
373 $sql =
"UPDATE ".$this->db->prefix().
"facture";
374 $sql .=
" set paye=0, fk_statut=1";
375 $sql .=
" WHERE (type = 2 or type = 3) AND rowid = ".((int) $this->fk_facture_source);
377 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
378 $result = $this->
db->query($sql);
383 $this->error = $this->
db->lasterror();
384 $this->
db->rollback();
387 } elseif ($this->fk_invoice_supplier_source) {
388 $sql =
"UPDATE ".$this->db->prefix().
"facture_fourn";
389 $sql .=
" set paye=0, fk_statut=1";
390 $sql .=
" WHERE (type = 2 or type = 3) AND rowid = ".((int) $this->fk_invoice_supplier_source);
392 dol_syslog(get_class($this).
"::delete Update credit note or deposit invoice statut", LOG_DEBUG);
393 $result = $this->
db->query($sql);
398 $this->error = $this->
db->lasterror();
399 $this->
db->rollback();
407 $this->error = $this->
db->lasterror();
408 $this->
db->rollback();
429 if (!$rowidline && !$rowidinvoice) {
430 $this->error =
'ErrorBadParameters';
433 if ($rowidline && $rowidinvoice) {
434 $this->error =
'ErrorBadParameters';
438 $sql =
"UPDATE ".$this->db->prefix().
"societe_remise_except";
439 if (!empty($this->discount_type)) {
441 $sql .=
" SET fk_invoice_supplier_line = ".((int) $rowidline);
444 $sql .=
" SET fk_invoice_supplier = ".((int) $rowidinvoice);
448 $sql .=
" SET fk_facture_line = ".((int) $rowidline);
451 $sql .=
" SET fk_facture = ".((int) $rowidinvoice);
454 $sql .=
" WHERE rowid = ".((int) $this->
id);
456 dol_syslog(get_class($this).
"::link_to_invoice", LOG_DEBUG);
459 if (!empty($this->discount_type)) {
460 $this->fk_invoice_supplier_line = $rowidline;
461 $this->fk_invoice_supplier = $rowidinvoice;
463 $this->fk_facture_line = $rowidline;
464 $this->fk_facture = $rowidinvoice;
468 $this->error = $this->
db->error();
484 $sql =
"UPDATE ".$this->db->prefix().
"societe_remise_except";
485 if (!empty($this->discount_type)) {
486 $sql .=
" SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
488 $sql .=
" SET fk_facture_line = NULL, fk_facture = NULL";
490 $sql .=
" WHERE rowid = ".((int) $this->
id);
492 dol_syslog(get_class($this).
"::unlink_invoice", LOG_DEBUG);
497 $this->error = $this->
db->error();
514 public function getAvailableDiscounts($company =
'', $user =
'', $filter =
'', $maxvalue = 0, $discount_type = 0, $multicurrency = 0)
518 dol_syslog(get_class($this).
"::getAvailableDiscounts discount_type=".$discount_type, LOG_DEBUG);
520 $sql =
"SELECT SUM(rc.amount_ttc) as amount, SUM(rc.multicurrency_amount_ttc) as multicurrency_amount";
521 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
522 $sql .=
" WHERE rc.entity = ".$conf->entity;
523 $sql .=
" AND rc.discount_type=".((int) $discount_type);
524 if (!empty($discount_type)) {
525 $sql .=
" AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL)";
527 $sql .=
" AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)";
529 if (is_object($company)) {
530 $sql .=
" AND rc.fk_soc = ".((int) $company->id);
532 if (is_object($user)) {
533 $sql .=
" AND rc.fk_user = ".((int) $user->id);
536 $sql .=
" AND (".$filter.
")";
539 $sql .=
' AND rc.amount_ttc <= '.((float)
price2num($maxvalue));
544 $obj = $this->
db->fetch_object(
$resql);
550 if ($multicurrency) {
551 return $obj->multicurrency_amount;
570 dol_syslog(get_class($this).
"::getSumDepositsUsed", LOG_DEBUG);
572 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
573 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
574 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->
db->prefix().
"facture as f";
575 $sql .=
" WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
576 $sql .=
" AND f.type = 3";
577 } elseif ($invoice->element ==
'invoice_supplier') {
578 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
579 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->
db->prefix().
"facture_fourn as f";
580 $sql .=
" WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
581 $sql .=
" AND f.type = 3";
583 $this->error = get_class($this).
"::getSumDepositsUsed was called with a bad object as a first parameter";
590 $obj = $this->
db->fetch_object(
$resql);
591 if ($multicurrency == 1) {
592 return $obj->multicurrency_amount;
597 $this->error = $this->
db->lasterror();
611 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
613 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
614 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
615 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc, ".$this->
db->prefix().
"facture as f";
616 $sql .=
" WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ".((int) $invoice->id);
617 $sql .=
" AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.
", ".$invoice::TYPE_CREDIT_NOTE.
", ".$invoice::TYPE_SITUATION).
")";
618 } elseif ($invoice->element ==
'invoice_supplier') {
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, ".$this->
db->prefix().
"facture_fourn as f";
621 $sql .=
" WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ".((int) $invoice->id);
622 $sql .=
" AND f.type IN (".$this->db->sanitize($invoice::TYPE_STANDARD.
", ".$invoice::TYPE_CREDIT_NOTE).
")";
624 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
631 $obj = $this->
db->fetch_object(
$resql);
632 if ($multicurrency == 1) {
633 return $obj->multicurrency_amount;
638 $this->error = $this->
db->lasterror();
651 dol_syslog(get_class($this).
"::getSumCreditNotesUsed", LOG_DEBUG);
653 if ($invoice->element ==
'facture' || $invoice->element ==
'invoice') {
654 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
655 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
656 $sql .=
" WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = ".((int) $invoice->id);
657 } elseif ($invoice->element ==
'invoice_supplier') {
658 $sql =
"SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount";
659 $sql .=
" FROM ".$this->db->prefix().
"societe_remise_except as rc";
660 $sql .=
" WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = ".((int) $invoice->id);
662 $this->error = get_class($this).
"::getSumCreditNotesUsed was called with a bad object as a first parameter";
669 $obj = $this->
db->fetch_object(
$resql);
670 if ($multicurrency) {
671 return $obj->multicurrency_amount;
676 $this->error = $this->
db->lasterror();
688 public function getNomUrl($withpicto, $option =
'invoice')
699 if ($option ==
'invoice') {
700 $facid = !empty($this->discount_type) ? $this->fk_invoice_supplier_source : $this->fk_facture_source;
701 $link = !empty($this->discount_type) ?
'/fourn/facture/card.php' :
'/compta/facture/card.php';
702 $label = $langs->trans(
"ShowSourceInvoice").
': '.$this->ref_facture_source;
703 $link =
'<a href="'.DOL_URL_ROOT.$link.
'?facid='.$facid.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
705 $ref = !empty($this->discount_type) ? $this->ref_invoice_supplier_source : $this->ref_facture_source;
708 if ($option ==
'discount') {
709 $label = $langs->trans(
"Discount");
710 $link =
'<a href="'.DOL_URL_ROOT.
'/comm/remx.php?id='.$this->fk_soc.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
712 $ref = $langs->trans(
"Discount");
718 $result .= ($link.img_object($label, $picto,
'class="classfortooltip"').$linkend);
720 if ($withpicto && $withpicto != 2) {
723 $result .= $link.$ref.$linkend;
737 global $user, $langs, $conf;
740 $this->amount_ht = 10;
741 $this->amount_tva = 1.96;
742 $this->amount_ttc = 11.96;
743 $this->tva_tx = 19.6;
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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
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.
$conf db
API class for accounts.