Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

ezoptionvalue.php
Go to the documentation of this file.
1 <?php
3 //
4 // $Id: ezoptionvalue.php 9276 2002-02-26 14:41:12Z ce $
5 //
6 // Definition of eZOptionValue class
7 //
8 // Created on: <12-Sep-2000 15:52:19 bf>
9 //
10 // This source file is part of Exponential Basic, publishing software.
11 //
12 // Copyright (C) 1999-2001 eZ Systems. All rights reserved.
13 //
14 // This program is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU General Public License
16 // as published by the Free Software Foundation; either version 2
17 // of the License, or (at your option) any later version.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US
27 //
28
31
49 // include_once( "classes/ezdb.php" );
50 // include_once( "eztrade/classes/ezoption.php" );
51 // include_once( "eztrade/classes/ezpricegroup.php" );
52
53 class eZOptionValue
54 {
58 function __construct( $id=-1 )
59 {
60 if ( $id != -1 )
61 {
62 $this->ID = $id;
63 $this->get( $this->ID );
64 }
65 }
66
70 function store()
71 {
72 $ini = eZINI::instance( "site.ini" );
73 $db =& eZDB::globalDatabase();
74 $db->begin();
75 $qry_array = array();
76 $ret = array();
77
78 $this->OptionID = $db->escapeString( $this->OptionID );
79
80 $price = $this->Price == "" ? "NULL" : "'$this->Price'";
81
82 $ini->variable( "site", "DebugOutput" ) == "enabled" ? true : false;
83
84 if ( !isset( $this->ID ) )
85 {
86 $db->lock( "eZTrade_OptionValue" );
87 $db->array_query( $qry_array,
88 "SELECT Placement FROM eZTrade_OptionValue
89 WHERE OptionID='$this->OptionID' ORDER BY Placement DESC LIMIT 1" );
90 $placement = count( $qry_array ) == 0 ? 1 : $qry_array[0]["Placement"] + 1;
91 $nextID = $db->nextID( "eZTrade_OptionValue", "ID" );
92 $ret[] = $db->query( "INSERT INTO eZTrade_OptionValue
93 ( ID,
94 Price,
95 Placement,
96 RemoteID,
97 OptionID )
98 VALUES
99 ( '$nextID',
100 $price,
101 '$placement',
102 '$this->RemoteID',
103 '$this->OptionID' )" );
104 $db->unlock();
105 $this->ID = $nextID;
106 }
107 else
108 {
109 $ret[] = $db->query( "UPDATE eZTrade_OptionValue SET
110 Price=$price,
111 RemoteID='$this->RemoteID',
112 OptionID='$this->OptionID'
113 WHERE ID='$this->ID'" );
114 }
115
116 eZDB::finish( $ret, $db );
117
118 return true;
119 }
120
124 function get( $id=-1 )
125 {
126 $db =& eZDB::globalDatabase();
127
128 if ( $id != "-1" )
129 {
130 $db->array_query( $optionValue_array, "SELECT * FROM eZTrade_OptionValue WHERE ID='$id'" );
131 if ( count( $optionValue_array ) > 1 )
132 {
133 die( "Error: OptionValue's with the same ID was found in the database. This shouldent happen." );
134 }
135 else if ( count( $optionValue_array ) == 1 )
136 {
137 $this->ID =& $optionValue_array[0][$db->fieldName("ID")];
138 $this->Price =& $optionValue_array[0][$db->fieldName("Price")];
139 if ( $this->Price == "NULL" )
140 $this->Price = false;
141 $this->OptionID =& $optionValue_array[0][$db->fieldName("OptionID")];
142 $this->RemoteID =& $optionValue_array[0][$db->fieldName("RemoteID")];
143 }
144 }
145 }
146
150 function &getAll()
151 {
152 $db =& eZDB::globalDatabase();
153 $return_array = array();
154 $optionValue_array = array();
155
156 $db->array_query( $optionValue_array, "SELECT ID FROM eZTrade_OptionValue
157 ORDER BY Placement ASC" );
158
159 for ( $i=0; $i < count($optionValue_array); $i++ )
160 {
161 $return_array[$i] = new eZOptionValue( $optionValue_array[$i][$db->fieldName("ID")], 0 );
162 }
163
164 return $return_array;
165 }
166
172 function &getByOption( &$value, $as_object = true )
173 {
174 if ( is_a( $value, "eZOption" ) )
175 {
176 $db =& eZDB::globalDatabase();
177
178 $return_array = array();
179 $optionValue_array = array();
180
181 $id = $value->id();
182
183 $db->array_query( $optionValue_array,
184 "SELECT ID FROM eZTrade_OptionValue WHERE OptionID='$id' ORDER BY Placement ASC" );
185
186 for ( $i=0; $i < count($optionValue_array); $i++ )
187 {
188 $return_array[$i] = $as_object ? new eZOptionValue( $optionValue_array[$i][$db->fieldName("ID")], true ) : $optionValue_array[$i][$db->fieldName("ID")];
189 }
190 return $return_array;
191 }
192 else
193 {
194 return 0;
195 }
196 }
197
201 function setTotalQuantity( $quantity )
202 {
203 $id = $this->ID;
204 $db =& eZDB::globalDatabase();
205 $ret = array();
206 $db->begin();
207
208 $db->array_query( $qry_array,
209 "SELECT Q.ID
210 FROM eZTrade_Quantity AS Q, eZTrade_ValueQuantityDict AS VQD
211 WHERE Q.ID=VQD.QuantityID AND ValueID='$id'" );
212 $ret[] = $db->query( "DELETE FROM eZTrade_ValueQuantityDict WHERE ValueID='$id'" );
213 foreach( $qry_array as $row )
214 {
215 $q_id = $row[$db->fieldName("ID")];
216 $ret[] = $db->query( "DELETE FROM eZTrade_Quantity WHERE ID='$q_id'" );
217 }
218
219 if ( is_bool( $quantity ) and !$quantity )
220 return;
221
222 $db->lock( "eZTrade_Quantity" );
223
224 $nextID = $db->nextID( "eZTrade_Quantity", "ID" );
225
226 $ret[] = $db->query( "INSERT INTO eZTrade_Quantity
227 ( ID,
228 Quantity )
229 VALUES
230 ('$nextID',
231 '$quantity')" );
232
233 $q_id = $nextID;
234
235 $db->lock( "eZTrade_ValueQuantityDict" );
236 $ret[] = $db->query( "INSERT INTO eZTrade_ValueQuantityDict
237 ( ValueID,
238 QuantityID )
239 VALUES
240 ('$id',
241 '$q_id' )" );
242
243 $db->unlock();
244 eZDB::finish( $ret, $db );
245 }
246
251 function totalQuantity( $id = false )
252 {
253 if ( !$id )
254 $id = $this->ID;
255 $db =& eZDB::globalDatabase();
256 $db->array_query( $qry_array,
257 "SELECT Q.Quantity
258 FROM eZTrade_Quantity AS Q, eZTrade_ValueQuantityDict AS VQD
259 WHERE Q.ID=VQD.QuantityID AND ValueID='$id'" );
260 $quantity = 0;
261 if ( count( $qry_array ) > 0 )
262 {
263 foreach( $qry_array as $row )
264 {
265 if ( $row[$db->fieldName("Quantity")] == "NULL" )
266 return false;
267 $quantity += $row[$db->fieldName("Quantity")];
268 }
269 }
270 else
271 return false;
272 return $quantity;
273 }
274
279 function hasQuantity( $require = true, $id = false )
280 {
281 if ( !$id )
282 $id = $this->ID;
283 $quantity = eZOptionValue::totalQuantity( $id );
284 if ( (is_bool($quantity) and !$quantity) or
285 !$require or ( $require and $quantity > 0 ) )
286 return true;
287 return false;
288 }
289
294 function &descriptions( $id = false )
295 {
296 $db =& eZDB::globalDatabase();
297 if ( !$id )
298 $id = $this->ID;
299 $db->array_query( $qry_array,
300 "SELECT eZTrade_OptionValueContent.Value FROM eZTrade_OptionValueContent
301 WHERE ValueID='$id' GROUP By eZTrade_OptionValueContent.Value, eZTrade_OptionValueContent.Placement ORDER BY Placement ASC" );
302 $ret = array();
303 foreach( $qry_array as $row )
304 {
305 $ret[] = $row[$db->fieldName("Value")];
306 }
307 return $ret;
308 }
309
313 function removeDescriptions( $id = false )
314 {
315 $db =& eZDB::globalDatabase();
316 if ( !$id )
317 $id = $this->ID;
318 $db->begin();
319 $ret[] = $db->query( "DELETE FROM eZTrade_OptionValueContent WHERE ValueID='$id'" );
320 eZDB::finish( $ret, $db );
321 }
322
326 function addDescription( $description, $id = false )
327 {
328 $db =& eZDB::globalDatabase();
329 $db->begin();
330 if ( !$id )
331 $id = $this->ID;
332 if ( !is_array( $description ) )
333 $description = array( $description );
334 $db->array_query( $qry_array,
335 "SELECT Placement FROM eZTrade_OptionValueContent
336 WHERE ValueID='$id' ORDER BY Placement DESC LIMIT 1", 0, 1 );
337 $placement = count( $qry_array ) == 0 ? 1 : $qry_array[0][$db->fieldName("Placement")] + 1;
338 $db->lock( "eZTrade_OptionValueContent" );
339 foreach( $description as $desc )
340 {
341 $desc = $db->escapeString( $desc );
342 $nextID = $db->nextID( "eZTrade_OptionValueContent", "ID" );
343 $ret[] = $db->query( "INSERT INTO eZTrade_OptionValueContent
344 ( ID,
345 Value,
346 ValueID,
347 Placement )
348 VALUES
349 ( '$nextID',
350 '$desc',
351 '$id',
352 '$placement')" );
353 $placement++;
354 }
355
356 $db->unlock();
357 eZDB::finish( $ret, $db );
358 }
359
363 function delete( $id = false )
364 {
365 if ( !$id )
366 $id = $this->ID;
367 $ret = array();
368 $db =& eZDB::globalDatabase();
369 $db->begin();
370
371 $ret[] = $db->query( "DELETE FROM eZTrade_OptionValue
372 WHERE ID='$id'" );
373 $ret[] = $db->query( "DELETE FROM eZTrade_ProductPriceLink
374 WHERE ValueID='$id'" );
375 $ret[] = $db->query( "DELETE FROM eZTrade_OptionValueContent
376 WHERE ValueID='$id'" );
377 eZDB::finish( $ret, $db );
378 }
379
383 function getByRemoteID( $id )
384 {
385 $db =& eZDB::globalDatabase();
386
387 $value = false;
388
389 $db->array_query( $res, "SELECT ID FROM
390 eZTrade_OptionValue
391 WHERE RemoteID='$id'" );
392 if ( count( $res ) == 1 )
393 {
394 $value = new eZOptionValue( $res[0][$db->fieldName("ID")] );
395 }
396
397 return $value;
398 }
399
400
404 function id()
405 {
406 return $this->ID;
407 }
408
412 function price()
413 {
414 return $this->Price;
415 }
416
420 function &localePrice( $calcVAT, $inProduct )
421 {
422 $ini =& eZINI::instance( 'site.ini' );
423 $inLanguage = $ini->variable( "eZTradeMain", "Language" );
424
425 $locale = new eZLocale( $inLanguage );
426 $currency = new eZCurrency();
427
428 $price = $this->correctPrice( $calcVAT, $inProduct );
429
430 $currency->setValue( $price );
431 return $locale->format( $currency );
432 }
433
437 function correctPrice( $calcVAT, $inProduct, $withPriceGroups=true )
438 {
439 $inUser =& eZUser::currentUser();
440 $vat = $inProduct->vatPercentage();
441 $productHasVAT = $inProduct->includesVAT();
442
443 if ( is_a( $inUser, "eZUser" ) )
444 {
445 if ( $withPriceGroups == true )
446 $groups = eZPriceGroup::priceGroups( $inUser, false );
447 else
448 $groups = array();
449
450 $price = eZPriceGroup::correctPrice( $inProduct->id(), $groups, $this->OptionID, $this->ID );
451 }
452
453 if ( empty( $price ) )
454 {
455 $price = $this->Price;
456 }
457
458 if ( $calcVAT == true )
459 {
460 if ( $productHasVAT == false )
461 {
462 $price = ( $price * $vat / 100 ) + $price;
463 }
464 }
465 else
466 {
467 if ( $productHasVAT == true )
468 {
469 $price = $price - ( $price / ( $vat + 100 ) ) * $vat;
470 }
471
472 }
473 return $price;
474 }
475
479 function &localeSavings( $calcVAT, $inProduct )
480 {
481 $ini =& eZINI::instance( 'site.ini' );
482 $inLanguage = $ini->variable( "eZTradeMain", "Language" );
483
484 $locale = new eZLocale( $inLanguage );
485 $currency = new eZCurrency();
486
487 $price = $this->correctSavings( $calcVAT, $inProduct );
488
489 $currency->setValue( $price );
490 return $locale->format( $currency );
491 }
492
493
497 function correctSavings( $calcVAT, $inProduct )
498 {
499 $inUser =& eZUser::currentUser();
500 $vat = $inProduct->vatPercentage();
501 $productHasVAT = $inProduct->includesVAT();
502
503 if ( is_a( $inUser, "eZUser" ) )
504 {
505 $groups = eZPriceGroup::priceGroups( $inUser, false );
506
507 $price = eZPriceGroup::correctPrice( $inProduct->id(), $groups, $this->OptionID, $this->ID );
508 }
509
510 if ( empty( $price ) )
511 {
512 $price = $this->Price;
513 }
514
515 if ( isset( $maxPrice ) && $maxPrice <= $price )
516 {
517 $maxPrice = $this->Price;
518 }
519
520 if ( isset( $maxPrice ) && $maxPrice > $price )
521 {
522 $savings = $maxPrice - $price;
523 }
524 else
525 {
526 $savings = 0;
527 }
528
529 if ( $calcVAT == true )
530 {
531 if ( $productHasVAT == false )
532 {
533 $savings = ( $savings * $vat / 100 ) + $savings;
534 }
535 }
536 else
537 {
538 if ( $productHasVAT == true )
539 {
540 $savings = $savings - ( $savings / ( $vat + 100 ) ) * $vat;
541 }
542
543 }
544 return $savings;
545 }
546
550 function remoteID()
551 {
552 return $this->RemoteID;
553 }
554
558 function option()
559 {
560 return new eZOption( $this->OptionID );
561 }
562
566 function setPrice( $value )
567 {
568 $this->Price = $value;
569 }
570
574 function setRemoteID( $value )
575 {
576 $this->RemoteID = $value;
577 }
578
582 function setOptionID( $value )
583 {
584 $this->OptionID = $value;
585 setType( $this->OptionID, "integer" );
586 }
587
588 var $ID;
589 var $Price;
590 var $RemoteID;
591 var $OptionID;
592
593 }
594
595 ?>
$locale
$locale
Definition: admin/adlist.php:127
$i
$i
Definition: admin/adlist.php:91
eZCurrency
Definition: ezcurrency.php:46
eZDB\finish
static finish(&$resultArray, &$db)
Definition: ezdb.php:204
eZDB\globalDatabase
static & globalDatabase()
Definition: ezdb.php:134
eZLocale
Definition: ezlocale.php:119
eZOptionValue
Definition: ezoptionvalue.php:54
eZOptionValue\setTotalQuantity
setTotalQuantity( $quantity)
Definition: ezoptionvalue.php:201
eZOptionValue\store
store()
Definition: ezoptionvalue.php:70
eZOptionValue\$ID
$ID
Definition: ezoptionvalue.php:588
eZOptionValue\removeDescriptions
removeDescriptions( $id=false)
Definition: ezoptionvalue.php:313
eZOptionValue\price
price()
Definition: ezoptionvalue.php:412
eZOptionValue\getAll
& getAll()
Definition: ezoptionvalue.php:150
eZOptionValue\$OptionID
$OptionID
Definition: ezoptionvalue.php:591
eZOptionValue\getByRemoteID
getByRemoteID( $id)
Definition: ezoptionvalue.php:383
eZOptionValue\localeSavings
& localeSavings( $calcVAT, $inProduct)
Definition: ezoptionvalue.php:479
eZOptionValue\hasQuantity
hasQuantity( $require=true, $id=false)
Definition: ezoptionvalue.php:279
eZOptionValue\correctSavings
correctSavings( $calcVAT, $inProduct)
Definition: ezoptionvalue.php:497
eZOptionValue\setRemoteID
setRemoteID( $value)
Definition: ezoptionvalue.php:574
eZOptionValue\id
id()
Definition: ezoptionvalue.php:404
eZOptionValue\setPrice
setPrice( $value)
Definition: ezoptionvalue.php:566
eZOptionValue\descriptions
& descriptions( $id=false)
Definition: ezoptionvalue.php:294
eZOptionValue\option
option()
Definition: ezoptionvalue.php:558
eZOptionValue\remoteID
remoteID()
Definition: ezoptionvalue.php:550
eZOptionValue\totalQuantity
totalQuantity( $id=false)
Definition: ezoptionvalue.php:251
eZOptionValue\correctPrice
correctPrice( $calcVAT, $inProduct, $withPriceGroups=true)
Definition: ezoptionvalue.php:437
eZOptionValue\addDescription
addDescription( $description, $id=false)
Definition: ezoptionvalue.php:326
eZOptionValue\getByOption
& getByOption(&$value, $as_object=true)
Definition: ezoptionvalue.php:172
eZOptionValue\localePrice
& localePrice( $calcVAT, $inProduct)
Definition: ezoptionvalue.php:420
eZOptionValue\setOptionID
setOptionID( $value)
Definition: ezoptionvalue.php:582
eZOptionValue\$Price
$Price
Definition: ezoptionvalue.php:589
eZOptionValue\$RemoteID
$RemoteID
Definition: ezoptionvalue.php:590
eZOptionValue\__construct
__construct( $id=-1)
Definition: ezoptionvalue.php:58
eZOption
Definition: ezoption.php:82
eZPriceGroup\correctPrice
static correctPrice( $productid, $priceid, $optionid=0, $valueid=0)
Definition: ezpricegroup.php:414
eZPriceGroup\priceGroups
static & priceGroups( $inUser, $as_object=true)
Definition: ezpricegroup.php:161
eZUser\currentUser
static & currentUser()
Definition: ezuser.php:896
$ini
$ini
Definition: classes/admin/linklist.php:64
$desc
$desc
Definition: companytypelist.php:195
$id
$id
Definition: companytypelist.php:193
$currency
$currency
Definition: companyview.php:529
$db
$db
Definition: ezarticle/user/authorlist.php:53
$groups
if( $CategoryID !=0) $groups
Definition: ezbulkmail/admin/categoryedit.php:108
$row
$row
Definition: ezcalendar/user/dayview.php:403
$ret
$ret
Definition: ezsitemanager/admin/filelist.php:88
$res
$res
Definition: ezstats/admin/cron.php:45
$price
$price
Definition: pricegroupedit.php:39
$vat
foreach( $categoryArray as $catItem) $vat
Definition: productedit.php:1334
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.