Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezmultiprice.php
Go to the documentation of this file.
1 <?php
31 class eZMultiPrice extends eZSimplePrice
32 {
36 public $IsDataDirty;
37 const CALCULATION_TYPE_VAT_INCLUDE = 1;
38 const CALCULATION_TYPE_VAT_EXCLUDE = 2;
39 const CALCULATION_TYPE_DISCOUNT_INCLUDE = 3;
40 const CALCULATION_TYPE_DISCOUNT_EXCLUDE = 4;
41
42 public function __construct( $classAttribute, $contentObjectAttribute, $storedPrice = null )
43 {
44 parent::__construct( $classAttribute, $contentObjectAttribute, $storedPrice );
45
46 $isVatIncluded = ( $classAttribute->attribute( eZMultiPriceType::INCLUDE_VAT_FIELD ) == 1 );
47 $VATID = $classAttribute->attribute( eZMultiPriceType::VAT_ID_FIELD );
48
49 $this->setVatIncluded( $isVatIncluded );
50 $this->setVatType( $VATID );
51
52 $this->IsDataDirty = false;
53 $this->ContentObjectAttribute = $contentObjectAttribute;
54 }
55
59 function attributes()
60 {
61 return array_unique( array_merge( array( 'currency_list',
62 'auto_currency_list',
63 'price_list',
64 'auto_price_list',
65 'custom_price_list',
66 'inc_vat_price_list',
67 'ex_vat_price_list',
68 'discount_inc_vat_price_list',
69 'discount_ex_vat_price_list' ),
70 eZSimplePrice::attributes() ) );
71 }
72
76 function hasAttribute( $attr )
77 {
78 return in_array( $attr, $this->attributes() );
79 }
80
84 function setAttribute( $attr, $value )
85 {
86 switch ( $attr )
87 {
88 case 'currency_list':
89 {
90 } break;
91
92 case 'auto_currency_list':
93 {
94 } break;
95
96 case 'price_list':
97 {
98 } break;
99
100 case 'auto_price_list':
101 {
102 } break;
103
104 case 'custom_price_list':
105 {
106 } break;
107
108 default:
109 {
110 eZSimplePrice::setAttribute( $attr, $value );
111 } break;
112 }
113 }
114
118 function attribute( $attr )
119 {
120 switch ( $attr )
121 {
122 case 'currency_list':
123 {
124 return $this->currencyList();
125 } break;
126
127 case 'auto_currency_list':
128 {
129 return $this->autoCurrencyList();
130 } break;
131
132 case 'price_list':
133 {
134 return $this->priceList();
135 } break;
136
137 case 'inc_vat_price_list':
138 {
139 return $this->incVATPriceList();
140 } break;
141
142 case 'ex_vat_price_list':
143 {
144 return $this->exVATPriceList();
145 } break;
146
147 case 'discount_inc_vat_price_list':
148 {
149 return $this->discountIncVATPriceList();
150 } break;
151
152 case 'discount_ex_vat_price_list':
153 {
154 return $this->discountExVATPriceList();
155 } break;
156
157 case 'auto_price_list':
158 {
159 return $this->autoPriceList();
160 } break;
161
162 case 'custom_price_list':
163 {
164 return $this->customPriceList();
165 } break;
166
167 default :
168 {
169 return eZSimplePrice::attribute( $attr );
170 } break;
171 }
172 }
173
178 function preferredCurrencyCode()
179 {
180 return eZShopFunctions::preferredCurrencyCode();
181 }
182
183 function currencyList()
184 {
185 if ( !isset( $this->CurrencyList ) )
186 {
187 $this->CurrencyList = eZCurrencyData::fetchList();
188 }
189
190 return $this->CurrencyList;
191 }
192
196 function autoCurrencyList()
197 {
198 // 'auto currencies' are the currencies used for 'auto' prices.
199 // 'auto currencies' = 'all currencies' - 'currencies of custom prices'
200
201 $autoCurrecyList = $this->currencyList();
202 $customPriceList = $this->customPriceList();
203 foreach ( $customPriceList as $price )
204 {
205 if ( $price )
206 {
207 $currencyCode = $price->attribute( 'currency_code' );
208 unset( $autoCurrecyList[$currencyCode] );
209 }
210 }
211
212 return $autoCurrecyList;
213 }
214
218 function customPriceList()
219 {
220 return $this->priceList( eZMultiPriceData::VALUE_TYPE_CUSTOM );
221 }
222
223 function autoPriceList()
224 {
225 return $this->priceList( eZMultiPriceData::VALUE_TYPE_AUTO );
226 }
227
228 function priceList( $type = false )
229 {
230 if ( !isset( $this->PriceList ) )
231 {
232 if ( is_object( $this->ContentObjectAttribute ) )
233 {
234 $this->PriceList = eZMultiPriceData::fetch( $this->ContentObjectAttribute->attribute( 'id' ),
235 $this->ContentObjectAttribute->attribute( 'version' ) );
236 }
237
238 if ( !$this->PriceList )
239 {
240 $this->PriceList = array();
241 }
242 }
243
244 $priceList = array();
245 if ( $type !== false )
246 {
247 foreach ( $this->priceList() as $currencyCode => $price )
248 {
249 if ( $price->attribute( 'type' ) == $type )
250 {
251 $priceList[$currencyCode] = $price;
252 }
253 }
254 }
255 else
256 {
257 $priceList = $this->PriceList;
258 }
259
260 return $priceList;
261 }
262
263 function incVATPriceList( $type = false )
264 {
265 return $this->calcPriceList( self::CALCULATION_TYPE_VAT_INCLUDE, $type );
266 }
267
268 function exVATPriceList( $type = false )
269 {
270 return $this->calcPriceList( self::CALCULATION_TYPE_VAT_EXCLUDE, $type );
271 }
272
273 function discountIncVATPriceList( $type = false )
274 {
275 return $this->calcPriceList( self::CALCULATION_TYPE_DISCOUNT_INCLUDE, $type );
276 }
277
278 function discountExVATPriceList( $type = false )
279 {
280 return $this->calcPriceList( self::CALCULATION_TYPE_DISCOUNT_EXCLUDE, $type );
281 }
282
283 function calcPriceList( $calculationType, $priceType )
284 {
285 $priceList = $this->priceList( $priceType );
286
287 $calculatedPriceList = array();
288 foreach ( $priceList as $key => $price )
289 {
290 switch ( $calculationType )
291 {
292 case self::CALCULATION_TYPE_VAT_INCLUDE :
293 {
294 $value = $this->calcIncVATPrice( $price->attribute( 'value' ) );
295 } break;
296
297 case self::CALCULATION_TYPE_VAT_EXCLUDE :
298 {
299 $value = $this->calcExVATPrice( $price->attribute( 'value' ) );
300 } break;
301
302 case self::CALCULATION_TYPE_DISCOUNT_INCLUDE :
303 {
304 $value = $this->calcDiscountIncVATPrice( $price->attribute( 'value' ) );
305 } break;
306
307 case self::CALCULATION_TYPE_DISCOUNT_EXCLUDE :
308 {
309 $value = $this->calcDiscountExVATPrice( $price->attribute( 'value' ) );
310 } break;
311
312 default:
313 {
314 // do nothing
315 } break;
316 }
317
318 $calculatedPrice = clone $price;
319 $calculatedPrice->setAttribute( 'value', $value );
320 $calculatedPriceList[$key] = $calculatedPrice;
321 }
322
323 return $calculatedPriceList;
324 }
325
326 static function removeByID( $objectAttributeID, $objectAttributeVersion = null )
327 {
328 eZMultiPriceData::removeByOAID( $objectAttributeID, $objectAttributeVersion );
329 }
330
331 function removePriceByCurrency( $currencyCode )
332 {
333 $price = $this->priceByCurrency( $currencyCode );
334 if ( $price )
335 {
336 $price->remove();
337 $priceList = $this->priceList();
338 unset( $priceList[$currencyCode] );
339 }
340 }
341
342 function setCustomPrice( $currencyCode, $value )
343 {
344 $this->setPriceByCurrency( $currencyCode, $value, eZMultiPriceData::VALUE_TYPE_CUSTOM );
345 }
346
347 function setAutoPrice( $currencyCode, $value )
348 {
349 $this->setPriceByCurrency( $currencyCode, $value, eZMultiPriceData::VALUE_TYPE_AUTO );
350 }
351
352 function setPriceByCurrency( $currencyCode, $value, $type )
353 {
354 if ( !$this->updatePrice( $currencyCode, $value, $type ) &&
355 !$this->addPrice( $currencyCode, $value, $type ) )
356 {
357 eZDebug::writeWarning( "Unable to set price in '$currencyCode'", __METHOD__ );
358 return false;
359 }
360
361 return true;
362 }
363
364 function setPrice( $value )
365 {
366 }
367
368 function updateAutoPriceList()
369 {
370 $converter = eZCurrencyConverter::instance();
371
372 $basePrice = $this->basePrice();
373 $basePriceValue = $basePrice ? $basePrice->attribute( 'value' ) : 0;
374 $baseCurrencyCode = $basePrice ? $basePrice->attribute( 'currency_code' ) : false;
375
376 $autoCurrencyList = $this->autoCurrencyList();
377 foreach( $autoCurrencyList as $currencyCode => $currency )
378 {
379 $autoValue = $converter->convert( $baseCurrencyCode, $currencyCode, $basePriceValue );
380 $this->setAutoPrice( $currencyCode, $autoValue );
381 }
382 }
383
384 function createPrice( $currencyCode, $value, $type )
385 {
386 if ( is_object( $this->ContentObjectAttribute ) && $this->currencyByCode( $currencyCode ) )
387 {
388 return eZMultiPriceData::create( $this->ContentObjectAttribute->attribute( 'id' ),
389 $this->ContentObjectAttribute->attribute( 'version' ),
390 $currencyCode,
391 $value,
392 $type );
393 }
394 return false;
395 }
396
397
398 function addPrice( $currencyCode, $value, $type )
399 {
400 $price = $this->createPrice( $currencyCode, $value, $type );
401 if( $price )
402 {
403 if ( $value === false )
404 $price->setAttribute( 'value', '0.00' );
405
406 $this->PriceList[$price->attribute( 'currency_code' )] = $price;
407 $this->setHasDirtyData( true );
408 }
409
410 return $price;
411 }
412
413 function updatePrice( $currencyCode, $value, $type )
414 {
415 $price = $this->priceByCurrency( $currencyCode );
416 if( $price )
417 {
418 if ( $value !== false )
419 {
420 $price->setAttribute( 'value', $value );
421 }
422
423 if ( $type !== false )
424 {
425 $price->setAttribute( 'type', $type );
426 }
427
428 $this->setHasDirtyData( true );
429 }
430
431 return $price;
432 }
433
434 function customPrice( $currencyCode )
435 {
436 return $this->priceByCurrency( $currencyCode, eZMultiPriceData::VALUE_TYPE_CUSTOM );
437 }
438
439 function autoPrice( $currencyCode )
440 {
441 return $this->priceByCurrency( $currencyCode, eZMultiPriceData::VALUE_TYPE_AUTO );
442 }
443
444 function priceByCurrency( $currencyCode, $type = false )
445 {
446 $price = false;
447 $priceList = $this->priceList();
448
449 if ( isset( $priceList[$currencyCode] ) )
450 {
451 if( $type === false || $priceList[$currencyCode]->attribute( 'type' ) == $type )
452 {
453 $price = $priceList[$currencyCode];
454 }
455 }
456
457 return $price;
458 }
459
460 function price()
461 {
462 $value = '0.0';
463 if ( $currencyCode = $this->preferredCurrencyCode() )
464 {
465 $price = $this->priceByCurrency( $currencyCode );
466 if ( $price )
467 {
468 $value = $price->attribute( 'value' );
469 }
470 }
471
472 return $value;
473 }
474
475 function currencyByCode( $currencyCode )
476 {
477 $currency = false;
478 $currencyList = $this->currencyList();
479 if ( isset( $currencyList[$currencyCode] ) )
480 {
481 $currency = $currencyList[$currencyCode];
482 }
483
484 return $currency;
485 }
486
487 function store()
488 {
489 if ( $this->hasDirtyData() )
490 {
491 $this->storePriceList();
492 $this->setHasDirtyData( false );
493 }
494 }
495
496 function storePriceList()
497 {
498 if ( isset( $this->PriceList ) && count( $this->PriceList ) > 0 )
499 {
500 $priceList = $this->priceList();
501 foreach ( $priceList as $price )
502 {
503 $price->store();
504 }
505 }
506 }
507
508 function hasDirtyData()
509 {
510 return $this->HasDataDirty;
511 }
512
513 function setHasDirtyData( $hasDirtyData )
514 {
515 $this->HasDataDirty = $hasDirtyData;
516 }
517
521 function baseCurrency()
522 {
523 // use value of the first custom price as
524 // base price and base currency.
525
526 $baseCurrency = false;
527 $customPriceList = $this->customPriceList();
528 $currencies = array_keys( $customPriceList );
529 if ( count( $currencies ) > 0 )
530 {
531 $baseCurrency = $currencies[0];
532 }
533
534 return $baseCurrency;
535 }
536
537 function basePrice()
538 {
539 $baseCurrencyCode = $this->baseCurrency();
540 return $this->priceByCurrency( $baseCurrencyCode );
541 }
542
543 function currency()
544 {
545 return $this->preferredCurrencyCode();
546 }
547
548 function DOMDocument()
549 {
550 $doc = new DOMDocument( '1.0', 'utf-8' );
551 $root = $doc->createElement( 'ezmultiprice' );
552 $doc->appendChild( $root );
553
554 $priceListNode = $doc->createElement( 'price-list' );
555
556 $priceList = $this->attribute( 'price_list' );
557 foreach ( $priceList as $price )
558 {
559 $currencyCode = $price->attribute( 'currency_code' );
560 $value = $price->attribute( 'value' );
561 $type = $price->attribute( 'type' );
562
563 $priceNode = $doc->createElement( 'price' );
564
565 $priceNode->setAttribute( 'currency-code', $currencyCode );
566 $priceNode->setAttribute( 'value', $value );
567 $priceNode->setAttribute( 'type', $type );
568
569 $priceListNode->appendChild( $priceNode );
570 unset( $priceNode );
571 }
572
573 $root->appendChild( $priceListNode );
574
575 return $doc;
576 }
577
578 function decodeDOMTree( $rootNode )
579 {
580 $priceNode = $rootNode->getElementsByTagName( 'price-list' )->item( 0 );
581 $priceNodes = $priceNode->getElementsByTagName( 'price' );
582 if ( $priceNodes->length > 0 )
583 {
584 foreach ( $priceNodes as $priceNode )
585 {
586 $currencyCode = $priceNode->getAttribute( 'currency-code' );
587 $value = $priceNode->getAttribute( 'value' );
588 $type = $priceNode->getAttribute( 'type' );
589
590 $this->setPriceByCurrency( $currencyCode, $value, $type );
591 }
592 }
593 }
594
596 public $PriceList;
597 public $CurrencyList;
598 public $HasDataDirty;
599 public $ContentObjectAttribute;
600 }
601
602 ?>
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:502
eZMultiPriceType\VAT_ID_FIELD
const VAT_ID_FIELD
Definition: ezmultipricetype.php:25
eZMultiPriceType\INCLUDE_VAT_FIELD
const INCLUDE_VAT_FIELD
Definition: ezmultipricetype.php:23
eZMultiPrice
Handles prices in different currencies with VAT and discounts.
Definition: ezmultiprice.php:32
eZMultiPrice\incVATPriceList
incVATPriceList( $type=false)
Definition: ezmultiprice.php:263
eZMultiPrice\setAutoPrice
setAutoPrice( $currencyCode, $value)
Definition: ezmultiprice.php:347
eZMultiPrice\discountIncVATPriceList
discountIncVATPriceList( $type=false)
Definition: ezmultiprice.php:273
eZMultiPrice\updatePrice
updatePrice( $currencyCode, $value, $type)
Definition: ezmultiprice.php:413
eZMultiPrice\attributes
attributes()
Definition: ezmultiprice.php:59
eZMultiPrice\$HasDataDirty
$HasDataDirty
Definition: ezmultiprice.php:598
eZMultiPrice\calcPriceList
calcPriceList( $calculationType, $priceType)
Definition: ezmultiprice.php:283
eZMultiPrice\basePrice
basePrice()
Definition: ezmultiprice.php:537
eZMultiPrice\currency
currency()
Definition: ezmultiprice.php:543
eZMultiPrice\customPrice
customPrice( $currencyCode)
Definition: ezmultiprice.php:434
eZMultiPrice\__construct
__construct( $classAttribute, $contentObjectAttribute, $storedPrice=null)
Definition: ezmultiprice.php:42
eZMultiPrice\setAttribute
setAttribute( $attr, $value)
Definition: ezmultiprice.php:84
eZMultiPrice\autoPriceList
autoPriceList()
Definition: ezmultiprice.php:223
eZMultiPrice\priceList
priceList( $type=false)
Definition: ezmultiprice.php:228
eZMultiPrice\$IsDataDirty
$IsDataDirty
Definition: ezmultiprice.php:36
eZMultiPrice\removePriceByCurrency
removePriceByCurrency( $currencyCode)
Definition: ezmultiprice.php:331
eZMultiPrice\addPrice
addPrice( $currencyCode, $value, $type)
Definition: ezmultiprice.php:398
eZMultiPrice\CALCULATION_TYPE_VAT_EXCLUDE
const CALCULATION_TYPE_VAT_EXCLUDE
Definition: ezmultiprice.php:38
eZMultiPrice\DOMDocument
DOMDocument()
Definition: ezmultiprice.php:548
eZMultiPrice\CALCULATION_TYPE_DISCOUNT_EXCLUDE
const CALCULATION_TYPE_DISCOUNT_EXCLUDE
Definition: ezmultiprice.php:40
eZMultiPrice\CALCULATION_TYPE_DISCOUNT_INCLUDE
const CALCULATION_TYPE_DISCOUNT_INCLUDE
Definition: ezmultiprice.php:39
eZMultiPrice\setCustomPrice
setCustomPrice( $currencyCode, $value)
Definition: ezmultiprice.php:342
eZMultiPrice\createPrice
createPrice( $currencyCode, $value, $type)
Definition: ezmultiprice.php:384
eZMultiPrice\currencyByCode
currencyByCode( $currencyCode)
Definition: ezmultiprice.php:475
eZMultiPrice\discountExVATPriceList
discountExVATPriceList( $type=false)
Definition: ezmultiprice.php:278
eZMultiPrice\decodeDOMTree
decodeDOMTree( $rootNode)
Definition: ezmultiprice.php:578
eZMultiPrice\baseCurrency
baseCurrency()
Definition: ezmultiprice.php:521
eZMultiPrice\$PriceList
$PriceList
Definition: ezmultiprice.php:596
eZMultiPrice\setHasDirtyData
setHasDirtyData( $hasDirtyData)
Definition: ezmultiprice.php:513
eZMultiPrice\priceByCurrency
priceByCurrency( $currencyCode, $type=false)
Definition: ezmultiprice.php:444
eZMultiPrice\customPriceList
customPriceList()
Definition: ezmultiprice.php:218
eZMultiPrice\autoPrice
autoPrice( $currencyCode)
Definition: ezmultiprice.php:439
eZMultiPrice\setPrice
setPrice( $value)
Definition: ezmultiprice.php:364
eZMultiPrice\CALCULATION_TYPE_VAT_INCLUDE
const CALCULATION_TYPE_VAT_INCLUDE
Definition: ezmultiprice.php:37
eZMultiPrice\exVATPriceList
exVATPriceList( $type=false)
Definition: ezmultiprice.php:268
eZMultiPrice\attribute
attribute( $attr)
Definition: ezmultiprice.php:118
eZMultiPrice\price
price()
Definition: ezmultiprice.php:460
eZMultiPrice\updateAutoPriceList
updateAutoPriceList()
Definition: ezmultiprice.php:368
eZMultiPrice\setPriceByCurrency
setPriceByCurrency( $currencyCode, $value, $type)
Definition: ezmultiprice.php:352
eZMultiPrice\autoCurrencyList
autoCurrencyList()
Definition: ezmultiprice.php:196
eZMultiPrice\preferredCurrencyCode
preferredCurrencyCode()
Definition: ezmultiprice.php:178
eZMultiPrice\hasDirtyData
hasDirtyData()
Definition: ezmultiprice.php:508
eZMultiPrice\store
store()
Definition: ezmultiprice.php:487
eZMultiPrice\hasAttribute
hasAttribute( $attr)
Definition: ezmultiprice.php:76
eZMultiPrice\currencyList
currencyList()
Definition: ezmultiprice.php:183
eZMultiPrice\removeByID
static removeByID( $objectAttributeID, $objectAttributeVersion=null)
Definition: ezmultiprice.php:326
eZMultiPrice\storePriceList
storePriceList()
Definition: ezmultiprice.php:496
eZMultiPrice\$CurrencyList
$CurrencyList
Definition: ezmultiprice.php:597
eZMultiPrice\$ContentObjectAttribute
$ContentObjectAttribute
Definition: ezmultiprice.php:599
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.