OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxprice.php
Go to the documentation of this file.
1 <?php
2 
6 class oxPrice
7 {
13  protected $_dBrutto = 0.0;
14 
20  protected $_dNetto = 0.0;
21 
27  protected $_dVat = 0.0;
28 
29 
35  protected $_aDiscounts = null;
36 
37 
46  protected $_blNetPriceMode;
47 
55  public function __construct($dInitPrice = null)
56  {
57  $this->_blNetPriceMode = oxRegistry::getConfig()->getConfigParam( 'blEnterNetPrice' );
58 
59  if ($dInitPrice) {
60  $this->setPrice($dInitPrice);
61  }
62  }
63 
69  public function setNettoPriceMode()
70  {
71  $this->_blNetPriceMode = true;
72  }
73 
79  public function setBruttoPriceMode()
80  {
81  $this->_blNetPriceMode = false;
82  }
83 
91  public function setVat($newVat)
92  {
93  $this->_dVat = (double) $newVat;
94  }
95 
110  public function setUserVat($newVat)
111  {
112  if (!$this->_blNetPriceMode && $newVat != $this->_dVat) {
113  $this->_dBrutto = self::Netto2Brutto(self::Brutto2Netto($this->_dBrutto, $this->_dVat), (double) $newVat);
114  }
115  $this->_dVat = (double) $newVat;
116  }
117 
123  public function getVat()
124  {
125  return $this->_dVat;
126  }
127 
137  public function setPrice($dPrice, $dVat = null)
138  {
139  if (isset($dVat)) {
140  $this->_dVat = (double) $dVat;
141  }
142 
143  if ($this->_blNetPriceMode) {
144  $this->_dNetto = $dPrice;
145  } else {
146  $this->_dBrutto = $dPrice;
147  }
148  }
149 
155  public function getPrice()
156  {
157  if ( $this->_blNetPriceMode ) {
158  return $this->getNettoPrice();
159  } else {
160  return $this->getBruttoPrice();
161  }
162  }
163 
169  public function getBruttoPrice()
170  {
171  if ( $this->_blNetPriceMode ) {
172  return $this->getNettoPrice() + $this->getVatValue();
173  } else {
174  return oxRegistry::getUtils()->fRound($this->_dBrutto);
175  }
176  }
177 
183  public function getNettoPrice()
184  {
185  if ( $this->_blNetPriceMode ) {
186  return oxRegistry::getUtils()->fRound($this->_dNetto);
187  } else {
188  return $this->getBruttoPrice() - $this->getVatValue();
189  }
190  }
191 
197  public function getVatValue()
198  {
199  if ( $this->_blNetPriceMode ) {
200  $dVatValue = $this->getNettoPrice() * $this->getVat() / 100 ;
201  } else {
202  $dVatValue = $this->getBruttoPrice() * $this->getVat() / ( 100 + $this->getVat());
203  }
204 
205  return oxRegistry::getUtils()->fRound( $dVatValue );
206  }
207 
216  public function subtractPercent( $dValue )
217  {
218  $dPrice = $this->getPrice();
219  $this->setPrice( $dPrice - self::percent( $dPrice, $dValue) );
220  }
221 
230  public function addPercent($dValue)
231  {
232  $this->subtractPercent(-$dValue);
233  }
234 
242  public function addPrice( oxPrice $oPrice )
243  {
244  if ($this->_blNetPriceMode) {
245  $this->add( $oPrice->getNettoPrice() );
246  } else {
247  $this->add( $oPrice->getBruttoPrice() );
248  }
249  }
250 
259  public function add($dValue)
260  {
261  $dPrice = $this->getPrice();
262  $this->setPrice( $dPrice + $dValue );
263  }
264 
273  public function subtract($dValue)
274  {
275  $this->add(-$dValue);
276  }
277 
286  public function multiply($dValue)
287  {
288  $dPrice = $this->getPrice();
289  $this->setPrice( $dPrice * $dValue );
290  }
291 
300  public function divide($dValue)
301  {
302  $dPrice = $this->getPrice();
303  $this->setPrice( $dPrice / $dValue );
304  }
305 
317  public function compare(oxPrice $oPrice)
318  {
319  $dBruttoPrice1 = $this->getBruttoPrice();
320  $dBruttoPrice2 = $oPrice->getBruttoPrice();
321 
322  if ($dBruttoPrice1 == $dBruttoPrice2) {
323  $iRes = 0;
324  } elseif ($dBruttoPrice1 > $dBruttoPrice2) {
325  $iRes = 1;
326  } else {
327  $iRes = -1;
328  }
329 
330  return $iRes;
331  }
332 
341  public static function percent($dValue, $dPercent)
342  {
343  return ((double) $dValue * (double) $dPercent)/100.0;
344  }
345 
358  public static function brutto2Netto($dBrutto, $dVat)
359  {
360  // if VAT = -100% Return 0 because we subtract all what we have.
361  // made to avoid division by zero in formula.
362  if ($dVat == -100) {
363  return 0;
364  }
365 
366  return (double) ((double) $dBrutto*100.0)/(100.0 + (double) $dVat);
367  }
368 
379  public static function netto2Brutto($dNetto, $dVat)
380  {
381  return (double) $dNetto + self::percent($dNetto, $dVat);
382  }
383 
396  protected function _recalculate()
397  {
398  if ( $this->_blNetPriceMode ) {
399  $this->_dBrutto = self::netto2Brutto($this->_dNetto, $this->_dVat);
400  $this->_dNetto = oxRegistry::getUtils()->fRound($this->_dNetto);
401  } else {
402  $this->_dNetto = self::brutto2Netto($this->_dBrutto, $this->_dVat);
403  $this->_dBrutto = oxRegistry::getUtils()->fRound($this->_dBrutto);
404  }
405  }
406 
414  public static function getPriceInActCurrency( $dPrice )
415  {
416  $oCur = oxRegistry::getConfig()->getActShopCurrencyObject();
417  return ( ( double ) $dPrice ) * $oCur->rate;
418  }
419 
420 
429  public function setDiscount( $dValue, $sType )
430  {
431  $this->_aDiscounts[] = array( 'value' => $dValue, 'type' => $sType );
432  }
433 
439  public function getDiscounts()
440  {
441  return $this->_aDiscounts;
442  }
443 
449  protected function _flushDiscounts()
450  {
451  $this->_aDiscounts = null;
452  }
453 
459  public function calculateDiscount()
460  {
461  $dPrice = $this->getPrice();
462  $aDiscounts = $this->getDiscounts();
463 
464  if ($aDiscounts) {
465  foreach ($aDiscounts as $aDiscount) {
466 
467  if ($aDiscount['type'] == 'abs') {
468  $dPrice = $dPrice - $aDiscount['value'];
469  } else {
470  $dPrice = $dPrice * (100 - $aDiscount['value']) / 100;
471  }
472  }
473  if ($dPrice < 0 ) {
474  $this->setPrice( 0 );
475  } else {
476  $this->setPrice( $dPrice );
477  }
478 
479  $this->_flushDiscounts();
480  }
481  }
482 
483 }