dolibarr  7.0.0-beta
discount.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
31 {
32  public $db;
33  public $error;
34 
35  public $id; // Id discount
36  public $fk_soc;
37  public $amount_ht; //
38  public $amount_tva; //
39  public $amount_ttc; //
40  public $tva_tx; // Vat rate
41  public $fk_user; // Id utilisateur qui accorde la remise
42  public $description; // Description libre
43  public $datec; // Date creation
44  public $fk_facture_line; // Id invoice line when a discount is used into an invoice line (for absolute discounts)
45  public $fk_facture; // Id invoice when a discount line is used into an invoice (for credit note)
46  public $fk_facture_source; // Id facture avoir a l'origine de la remise
47  public $ref_facture_source; // Ref facture avoir a l'origine de la remise
48 
54  function __construct($db)
55  {
56  $this->db = $db;
57  }
58 
59 
67  function fetch($rowid, $fk_facture_source=0)
68  {
69  global $conf;
70 
71  // Check parameters
72  if (! $rowid && ! $fk_facture_source)
73  {
74  $this->error='ErrorBadParameters';
75  return -1;
76  }
77 
78  $sql = "SELECT sr.rowid, sr.fk_soc,";
79  $sql.= " sr.fk_user,";
80  $sql.= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx,";
81  $sql.= " sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
82  $sql.= " sr.fk_facture_line, sr.fk_facture, sr.fk_facture_source, sr.description,";
83  $sql.= " sr.datec,";
84  $sql.= " f.facnumber as ref_facture_source";
85  $sql.= " FROM ".MAIN_DB_PREFIX."societe_remise_except as sr";
86  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON sr.fk_facture_source = f.rowid";
87  $sql.= " WHERE sr.entity = " . $conf->entity;
88  if ($rowid) $sql.= " AND sr.rowid=".$rowid;
89  if ($fk_facture_source) $sql.= " AND sr.fk_facture_source=".$fk_facture_source;
90 
91  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
92  $resql = $this->db->query($sql);
93  if ($resql)
94  {
95  if ($this->db->num_rows($resql))
96  {
97  $obj = $this->db->fetch_object($resql);
98 
99  $this->id = $obj->rowid;
100  $this->fk_soc = $obj->fk_soc;
101 
102  $this->amount_ht = $obj->amount_ht;
103  $this->amount_tva = $obj->amount_tva;
104  $this->amount_ttc = $obj->amount_ttc;
105 
106  $this->multicurrency_amount_ht = $obj->multicurrency_amount_ht;
107  $this->multicurrency_amount_tva = $obj->multicurrency_amount_tva;
108  $this->multicurrency_amount_ttc = $obj->multicurrency_amount_ttc;
109 
110  $this->tva_tx = $obj->tva_tx;
111  $this->fk_user = $obj->fk_user;
112  $this->fk_facture_line = $obj->fk_facture_line;
113  $this->fk_facture = $obj->fk_facture;
114  $this->fk_facture_source = $obj->fk_facture_source; // Id avoir source
115  $this->ref_facture_source = $obj->ref_facture_source; // Ref avoir source
116  $this->description = $obj->description;
117  $this->datec = $this->db->jdate($obj->datec);
118 
119  $this->db->free($resql);
120  return 1;
121  }
122  else
123  {
124  $this->db->free($resql);
125  return 0;
126  }
127  }
128  else
129  {
130  $this->error=$this->db->error();
131  return -1;
132  }
133  }
134 
135 
142  function create($user)
143  {
144  global $conf, $langs;
145 
146  // Clean parameters
147  $this->amount_ht=price2num($this->amount_ht);
148  $this->amount_tva=price2num($this->amount_tva);
149  $this->amount_ttc=price2num($this->amount_ttc);
150  $this->tva_tx=price2num($this->tva_tx);
151 
152  // Check parameters
153  if (empty($this->description))
154  {
155  $this->error='BadValueForPropertyDescription';
156  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
157  return -1;
158  }
159 
160  // Insert request
161  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise_except";
162  $sql.= " (entity, datec, fk_soc, fk_user, description,";
163  $sql.= " amount_ht, amount_tva, amount_ttc, tva_tx,";
164  $sql.= " fk_facture_source";
165  $sql.= ")";
166  $sql.= " VALUES (".$conf->entity.", '".$this->db->idate($this->datec!=''?$this->datec:dol_now())."', ".$this->fk_soc.", ".$user->id.", '".$this->db->escape($this->description)."',";
167  $sql.= " ".$this->amount_ht.", ".$this->amount_tva.", ".$this->amount_ttc.", ".$this->tva_tx.",";
168  $sql.= " ".($this->fk_facture_source ? "'".$this->db->escape($this->fk_facture_source)."'":"null");
169  $sql.= ")";
170 
171  dol_syslog(get_class($this)."::create", LOG_DEBUG);
172  $resql=$this->db->query($sql);
173  if ($resql)
174  {
175  $this->id=$this->db->last_insert_id(MAIN_DB_PREFIX."societe_remise_except");
176  return $this->id;
177  }
178  else
179  {
180  $this->error=$this->db->lasterror().' - sql='.$sql;
181  return -1;
182  }
183  }
184 
185 
192  function delete($user)
193  {
194  global $conf, $langs;
195 
196  // Check if we can remove the discount
197  if ($this->fk_facture_source)
198  {
199  $sql="SELECT COUNT(rowid) as nb";
200  $sql.=" FROM ".MAIN_DB_PREFIX."societe_remise_except";
201  $sql.=" WHERE (fk_facture_line IS NOT NULL"; // Not used as absolute simple discount
202  $sql.=" OR fk_facture IS NOT NULL)"; // Not used as credit note and not used as deposit
203  $sql.=" AND fk_facture_source = ".$this->fk_facture_source;
204  //$sql.=" AND rowid != ".$this->id;
205 
206  dol_syslog(get_class($this)."::delete Check if we can remove discount", LOG_DEBUG);
207  $resql=$this->db->query($sql);
208  if ($resql)
209  {
210  $obj = $this->db->fetch_object($resql);
211  if ($obj->nb > 0)
212  {
213  $this->error='ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved';
214  return -2;
215  }
216  }
217  else
218  {
219  dol_print_error($this->db);
220  return -1;
221  }
222  }
223 
224  $this->db->begin();
225 
226  // Delete but only if not used
227  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_remise_except ";
228  if ($this->fk_facture_source) $sql.= " WHERE fk_facture_source = ".$this->fk_facture_source; // Delete all lines of same serie
229  else $sql.= " WHERE rowid = ".$this->id; // Delete only line
230  $sql.= " AND (fk_facture_line IS NULL"; // Not used as absolute simple discount
231  $sql.= " AND fk_facture IS NULL)"; // Not used as credit note and not used as deposit
232 
233  dol_syslog(get_class($this)."::delete Delete discount", LOG_DEBUG);
234  $result=$this->db->query($sql);
235  if ($result)
236  {
237  // If source of discount was a credit note or deposit, we change source statut.
238  if ($this->fk_facture_source)
239  {
240  $sql = "UPDATE ".MAIN_DB_PREFIX."facture";
241  $sql.=" set paye=0, fk_statut=1";
242  $sql.=" WHERE (type = 2 or type = 3) AND rowid=".$this->fk_facture_source;
243 
244  dol_syslog(get_class($this)."::delete Update credit note or deposit invoice statut", LOG_DEBUG);
245  $result=$this->db->query($sql);
246  if ($result)
247  {
248  $this->db->commit();
249  return 1;
250  }
251  else
252  {
253  $this->error=$this->db->lasterror();
254  $this->db->rollback();
255  return -1;
256  }
257  }
258  else
259  {
260  $this->db->commit();
261  return 1;
262  }
263  }
264  else
265  {
266  $this->error=$this->db->lasterror();
267  $this->db->rollback();
268  return -1;
269  }
270  }
271 
272 
273 
283  function link_to_invoice($rowidline,$rowidinvoice)
284  {
285  // Check parameters
286  if (! $rowidline && ! $rowidinvoice)
287  {
288  $this->error='ErrorBadParameters';
289  return -1;
290  }
291  if ($rowidline && $rowidinvoice)
292  {
293  $this->error='ErrorBadParameters';
294  return -2;
295  }
296 
297  $sql ="UPDATE ".MAIN_DB_PREFIX."societe_remise_except";
298  if ($rowidline) $sql.=" SET fk_facture_line = ".$rowidline;
299  if ($rowidinvoice) $sql.=" SET fk_facture = ".$rowidinvoice;
300  $sql.=" WHERE rowid = ".$this->id;
301 
302  dol_syslog(get_class($this)."::link_to_invoice", LOG_DEBUG);
303  $resql = $this->db->query($sql);
304  if ($resql)
305  {
306  $this->fk_facture_line=$rowidline;
307  $this->fk_facture=$rowidinvoice;
308  return 1;
309  }
310  else
311  {
312  $this->error=$this->db->error();
313  return -3;
314  }
315  }
316 
317 
324  function unlink_invoice()
325  {
326  $sql ="UPDATE ".MAIN_DB_PREFIX."societe_remise_except";
327  $sql.=" SET fk_facture_line = NULL, fk_facture = NULL";
328  $sql.=" WHERE rowid = ".$this->id;
329 
330  dol_syslog(get_class($this)."::unlink_invoice", LOG_DEBUG);
331  $resql = $this->db->query($sql);
332  if ($resql)
333  {
334  return 1;
335  }
336  else
337  {
338  $this->error=$this->db->error();
339  return -3;
340  }
341  }
342 
343 
354  function getAvailableDiscounts($company='', $user='',$filter='', $maxvalue=0, $mode='customer')
355  {
356  global $conf;
357 
358  $sql = "SELECT SUM(rc.amount_ttc) as amount";
359  //$sql = "SELECT rc.amount_ttc as amount";
360  $sql.= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
361  $sql.= " WHERE rc.entity = " . $conf->entity;
362  if ($mode != 'supplier') $sql.= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available
363  else $sql.= " AND (rc.fk_suppler_invoice IS NULL AND rc.fk_supplier_invoice IS NULL)"; // Available
364  if (is_object($company)) $sql.= " AND rc.fk_soc = ".$company->id;
365  if (is_object($user)) $sql.= " AND rc.fk_user = ".$user->id;
366  if ($filter) $sql.=' AND ('.$filter.')';
367  if ($maxvalue) $sql.=' AND rc.amount_ttc <= '.price2num($maxvalue);
368 
369  dol_syslog(get_class($this)."::getAvailableDiscounts", LOG_DEBUG);
370  $resql=$this->db->query($sql);
371  if ($resql)
372  {
373  $obj = $this->db->fetch_object($resql);
374  //while ($obj)
375  //{
376  //print 'zz'.$obj->amount;
377  //$obj = $this->db->fetch_object($resql);
378  //}
379  return $obj->amount;
380  }
381  return -1;
382  }
383 
384 
393  function getSumDepositsUsed($invoice, $multicurrency=0)
394  {
395  dol_syslog(get_class($this)."::getSumDepositsUsed", LOG_DEBUG);
396 
397  if ($invoice->element == 'facture' || $invoice->element == 'invoice')
398  {
399  $sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount';
400  $sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc, '.MAIN_DB_PREFIX.'facture as f';
401  $sql.= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.$invoice->id;
402  $sql.= ' AND f.type = 3';
403  }
404  else if ($invoice->element == 'invoice_supplier')
405  {
406  $sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount';
407  $sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc, '.MAIN_DB_PREFIX.'facture_fourn as f';
408  $sql.= ' WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = '.$invoice->id;
409  $sql.= ' AND f.type = 3';
410  }
411  else
412  {
413  $this->error=get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
414  dol_print_error($this->error);
415  return -1;
416  }
417 
418  $resql=$this->db->query($sql);
419  if ($resql)
420  {
421  $obj = $this->db->fetch_object($resql);
422  if ($multicurrency) return $obj->multicurrency_amount;
423  else return $obj->amount;
424  }
425  else
426  {
427  $this->error = $this->db->lasterror();
428  return -1;
429  }
430  }
431 
439  function getSumCreditNotesUsed($invoice, $multicurrency=0)
440  {
441  dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
442 
443  if ($invoice->element == 'facture' || $invoice->element == 'invoice')
444  {
445  $sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount';
446  $sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc, '.MAIN_DB_PREFIX.'facture as f';
447  $sql.= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.$invoice->id;
448  $sql.= ' AND (f.type = 2 OR f.type = 0)'; // Find discount coming from credit note or excess received
449  }
450  else if ($invoice->element == 'invoice_supplier')
451  {
452  $sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount';
453  $sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc, '.MAIN_DB_PREFIX.'facture_fourn as f';
454  $sql.= ' WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = '.$invoice->id;
455  $sql.= ' AND (f.type = 2 OR f.type = 0)'; // Find discount coming from credit note or excess received
456  }
457  else
458  {
459  $this->error=get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
460  dol_print_error($this->error);
461  return -1;
462  }
463 
464  $resql=$this->db->query($sql);
465  if ($resql)
466  {
467  $obj = $this->db->fetch_object($resql);
468  if ($multicurrency) return $obj->multicurrency_amount;
469  else return $obj->amount;
470  }
471  else
472  {
473  $this->error = $this->db->lasterror();
474  return -1;
475  }
476  }
477 
485  function getNomUrl($withpicto,$option='invoice')
486  {
487  global $langs;
488 
489  $result='';
490 
491  if ($option == 'invoice') {
492  $label=$langs->trans("ShowDiscount").': '.$this->ref_facture_source;
493  $link = '<a href="'.DOL_URL_ROOT.'/compta/facture/card.php?facid='.$this->fk_facture_source.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
494  $linkend='</a>';
495  $ref=$this->ref_facture_source;
496  $picto='bill';
497  }
498  if ($option == 'discount') {
499  $label=$langs->trans("Discount");
500  $link = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$this->fk_soc.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
501  $linkend='</a>';
502  $ref=$langs->trans("Discount");
503  $picto='generic';
504  }
505 
506 
507  if ($withpicto) $result.=($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
508  if ($withpicto && $withpicto != 2) $result.=' ';
509  $result.=$link.$ref.$linkend;
510  return $result;
511  }
512 
513 
521  function initAsSpecimen()
522  {
523  global $user,$langs,$conf;
524 
525  $this->fk_soc = 1;
526  $this->amount_ht = 10;
527  $this->amount_tva = 1.96;
528  $this->amount_ttc = 11.96;
529  $this->tva_tx = 19.6;
530  $this->description = 'Specimen discount';
531  }
532 }
getSumCreditNotesUsed($invoice, $multicurrency=0)
Return amount (with tax) of all credit notes and deposits invoices used by invoice as a payment...
getSumDepositsUsed($invoice, $multicurrency=0)
Return amount (with tax) of all deposits invoices used by invoice as a payment.
initAsSpecimen()
Initialise an instance with random values.
</td >< tdclass="liste_titre"align="right"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow img yes disabled img no img no< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="'.$i.'"'.$disabled.'></td >< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"></td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:554
fetch($rowid, $fk_facture_source=0)
Load object from database into memory.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
getNomUrl($withpicto, $option='invoice')
Return clickable ref of object (with picto or not)
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
getAvailableDiscounts($company='', $user='', $filter='', $maxvalue=0, $mode='customer')
Return amount (with tax) of discounts currently available for a company, user or other criteria...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
dol_now($mode='gmt')
Return date for now.
create($user)
Create a discount into database.
link_to_invoice($rowidline, $rowidinvoice)
Link the discount to a particular invoice line or a particular invoice.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
Class to manage absolute discounts.
__construct($db)
Constructor.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...
unlink_invoice()
Link the discount to a particular invoice line or a particular invoice.