OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxprice.php
Go to the documentation of this file.
1 <?php
2 
6 class oxPrice
7 {
8 
14  protected $_dBrutto = 0.0;
15 
21  protected $_dNetto = 0.0;
22 
28  protected $_dVat = 0.0;
29 
30 
36  protected $_aDiscounts = null;
37 
38 
47  protected $_blNetPriceMode;
48 
56  public function __construct($dPrice = null)
57  {
58  $this->setNettoMode(oxRegistry::getConfig()->getConfigParam('blEnterNetPrice'));
59 
60  if (!is_null($dPrice)) {
61  $this->setPrice($dPrice);
62  }
63  }
64 
70  public function setNettoMode($blNetto = true)
71  {
72  $this->_blNetPriceMode = $blNetto;
73  }
74 
80  public function isNettoMode()
81  {
83  }
84 
88  public function setNettoPriceMode()
89  {
90  $this->setNettoMode();
91  }
92 
96  public function setBruttoPriceMode()
97  {
98  $this->setNettoMode(false);
99  }
100 
106  public function setVat($dVat)
107  {
108  $this->_dVat = (double) $dVat;
109  }
110 
121  public function setUserVat($newVat)
122  {
123  if (!$this->isNettoMode() && $newVat != $this->_dVat) {
124  $this->_dBrutto = self::Netto2Brutto(self::Brutto2Netto($this->_dBrutto, $this->_dVat), (double) $newVat);
125  }
126  $this->_dVat = (double) $newVat;
127  }
128 
134  public function getVat()
135  {
136  return $this->_dVat;
137  }
138 
146  public function setPrice($dPrice, $dVat = null)
147  {
148  if (!is_null($dVat)) {
149  $this->setVat($dVat);
150  }
151 
152  if ($this->isNettoMode()) {
153  $this->_dNetto = $dPrice;
154  } else {
155  $this->_dBrutto = $dPrice;
156  }
157  }
158 
164  public function getPrice()
165  {
166  if ($this->isNettoMode()) {
167  return $this->getNettoPrice();
168  } else {
169  return $this->getBruttoPrice();
170  }
171  }
172 
178  public function getBruttoPrice()
179  {
180  if ($this->isNettoMode()) {
181  return $this->getNettoPrice() + $this->getVatValue();
182  } else {
183  return oxRegistry::getUtils()->fRound($this->_dBrutto);
184  }
185  }
186 
192  public function getNettoPrice()
193  {
194  if ($this->isNettoMode()) {
195  return oxRegistry::getUtils()->fRound($this->_dNetto);
196  } else {
197  return $this->getBruttoPrice() - $this->getVatValue();
198  }
199  }
200 
206  public function getVatValue()
207  {
208  if ($this->isNettoMode()) {
209  $dVatValue = $this->getNettoPrice() * $this->getVat() / 100;
210  } else {
211  $dVatValue = $this->getBruttoPrice() * $this->getVat() / (100 + $this->getVat());
212  }
213 
214  return oxRegistry::getUtils()->fRound($dVatValue);
215  }
216 
223  public function subtractPercent($dValue)
224  {
225  $dPrice = $this->getPrice();
226  $this->setPrice($dPrice - self::percent($dPrice, $dValue));
227  }
228 
235  public function addPercent($dValue)
236  {
237  $this->subtractPercent(-$dValue);
238  }
239 
245  public function addPrice(oxPrice $oPrice)
246  {
247  if ($this->isNettoMode()) {
248  $this->add($oPrice->getNettoPrice());
249  } else {
250  $this->add($oPrice->getBruttoPrice());
251  }
252  }
253 
260  public function add($dValue)
261  {
262  $dPrice = $this->getPrice();
263  $this->setPrice($dPrice + $dValue);
264  }
265 
272  public function subtract($dValue)
273  {
274  $this->add(-$dValue);
275  }
276 
283  public function multiply($dValue)
284  {
285  $dPrice = $this->getPrice();
286  $this->setPrice($dPrice * $dValue);
287  }
288 
295  public function divide($dValue)
296  {
297  $dPrice = $this->getPrice();
298  $this->setPrice($dPrice / $dValue);
299  }
300 
312  public function compare(oxPrice $oPrice)
313  {
314  $dBruttoPrice1 = $this->getBruttoPrice();
315  $dBruttoPrice2 = $oPrice->getBruttoPrice();
316 
317  if ($dBruttoPrice1 == $dBruttoPrice2) {
318  $iRes = 0;
319  } elseif ($dBruttoPrice1 > $dBruttoPrice2) {
320  $iRes = 1;
321  } else {
322  $iRes = -1;
323  }
324 
325  return $iRes;
326  }
327 
336  public static function percent($dValue, $dPercent)
337  {
338  return ((double) $dValue * (double) $dPercent) / 100.0;
339  }
340 
353  public static function brutto2Netto($dBrutto, $dVat)
354  {
355  // if VAT = -100% Return 0 because we subtract all what we have.
356  // made to avoid division by zero in formula.
357  if ($dVat == -100) {
358  return 0;
359  }
360 
361  return (double) ((double) $dBrutto * 100.0) / (100.0 + (double) $dVat);
362  }
363 
374  public static function netto2Brutto($dNetto, $dVat)
375  {
376  return (double) $dNetto + self::percent($dNetto, $dVat);
377  }
378 
386  public static function getPriceInActCurrency($dPrice)
387  {
388  $oCur = oxRegistry::getConfig()->getActShopCurrencyObject();
389 
390  return (( double ) $dPrice) * $oCur->rate;
391  }
392 
393 
400  public function setDiscount($dValue, $sType)
401  {
402  $this->_aDiscounts[] = array('value' => $dValue, 'type' => $sType);
403  }
404 
410  public function getDiscounts()
411  {
412  return $this->_aDiscounts;
413  }
414 
418  protected function _flushDiscounts()
419  {
420  $this->_aDiscounts = null;
421  }
422 
426  public function calculateDiscount()
427  {
428  $dPrice = $this->getPrice();
429  $aDiscounts = $this->getDiscounts();
430 
431  if ($aDiscounts) {
432  foreach ($aDiscounts as $aDiscount) {
433 
434  if ($aDiscount['type'] == 'abs') {
435  $dPrice = $dPrice - $aDiscount['value'];
436  } else {
437  $dPrice = $dPrice * (100 - $aDiscount['value']) / 100;
438  }
439  }
440  if ($dPrice < 0) {
441  $this->setPrice(0);
442  } else {
443  $this->setPrice($dPrice);
444  }
445 
446  $this->_flushDiscounts();
447  }
448  }
449 }