00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class eZDiscount
00035 {
00036 function eZDiscount()
00037 {
00038 }
00039
00040
00041
00042
00043
00044
00045
00046
00047 function discountPercent( $user, $params )
00048 {
00049 include_once( 'lib/ezdb/classes/ezdb.php' );
00050 include_once( 'kernel/classes/ezuserdiscountrule.php' );
00051 include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00052
00053 $bestMatch = 0.0;
00054
00055 if ( is_object( $user ) )
00056 {
00057 $groups =& $user->groups();
00058 $idArray = array_merge( $groups, array( $user->attribute( 'contentobject_id' ) ) );
00059
00060
00061 $rules = eZUserDiscountRule::fetchByUserIDArray( $idArray );
00062
00063 if ( count( $rules ) > 0 )
00064 {
00065 $db =& eZDB::instance();
00066
00067 $i = 1;
00068 $subRuleStr = '';
00069 foreach ( $rules as $rule )
00070 {
00071 $subRuleStr .= $rule->attribute( 'id' );
00072 if ( $i < count( $rules ) )
00073 $subRuleStr .= ', ';
00074 $i++;
00075 }
00076
00077
00078 $subRules = $db->arrayQuery( "SELECT * FROM
00079 ezdiscountsubrule
00080 WHERE discountrule_id IN ( $subRuleStr )
00081 ORDER BY discount_percent DESC" );
00082
00083
00084 foreach ( $subRules as $subRule )
00085 {
00086 if ( $subRule['discount_percent'] > $bestMatch )
00087 {
00088
00089 if ( $subRule['limitation'] == '*' )
00090 $bestMatch = $subRule['discount_percent'];
00091 else
00092 {
00093
00094 $limitationArray = $db->arrayQuery( "SELECT * FROM
00095 ezdiscountsubrule_value
00096 WHERE discountsubrule_id='" . $subRule['id']. "'" );
00097
00098 $hasSectionLimitation = false;
00099 $hasClassLimitation = false;
00100 $hasObjectLimitation = false;
00101 $objectMatch = false;
00102 $sectionMatch = false;
00103 $classMatch = false;
00104 foreach ( $limitationArray as $limitation )
00105 {
00106 if ( $limitation['issection'] == '1' )
00107 {
00108 $hasSectionLimitation = true;
00109
00110 if ( isset( $params['section_id'] ) && $params['section_id'] == $limitation['value'] )
00111 $sectionMatch = true;
00112 }
00113 elseif ( $limitation['issection'] == '2' )
00114 {
00115 $hasObjectLimitation = true;
00116
00117 if ( isset( $params['contentobject_id'] ) && $params['contentobject_id'] == $limitation['value'] )
00118 $objectMatch = true;
00119 }
00120 else
00121 {
00122 $hasClassLimitation = true;
00123 if ( isset( $params['contentclass_id'] ) && $params['contentclass_id'] == $limitation['value'] )
00124 $classMatch = true;
00125 }
00126 }
00127
00128 $match = true;
00129 if ( ( $hasClassLimitation == true ) and ( $classMatch == false ) )
00130 $match = false;
00131
00132 if ( ( $hasSectionLimitation == true ) and ( $sectionMatch == false ) )
00133 $match = false;
00134
00135 if ( ( $hasObjectLimitation == true ) and ( $objectMatch == false ) )
00136 $match = false;
00137
00138 if ( $match == true )
00139 $bestMatch = $subRule['discount_percent'];
00140 }
00141 }
00142 }
00143 }
00144 }
00145 return $bestMatch;
00146 }
00147 }
00148
00149 ?>