SergeyMiracle
8/6/2014 - 1:54 PM

SHKDiscount

SHKDiscount

$e = &$modx->Event;

$output = '';

$userID = $modx->getLoginUserID();

	if(empty($userID)) return;

$user = $modx->getWebUserInfo($id);
$userDiscount = $user['discount'];

$table_user = $modx->getFullTableName( 'web_user_attributes' );
$table_shop = $modx->getFullTableName( 'manager_shopkeeper' );

$orders = $modx->db->select('id,price', $table_shop, 'userid='.$userID.' AND status=4');
$ordersCount = $modx->db->getRecordCount( $orders ); 

	if($ordersCount == 0) return;

while( $row = $modx->db->getRow( $orders ) ) {  
	$totalAmount += $row['price'];  
}  


if ($e->name == 'OnSHKcalcTotalPrice') {
  if(isset($totalPrice)){
	  
	  if( ($ordersCount >= $ordersMax1 && $ordersCount < $ordersMax2) or  ($totalAmount >= $threshold1 && $totalAmount < $threshold2) ){
			
		if ($userDiscount == 0)
		{
			$output = round($totalPrice * (1-$discount1/100), 2);
			
		}elseif($userDiscount = $discount1)
		{
			$output = round($totalPrice * (1-$userDiscount/100), 2);
		}
		
	}elseif( ($ordersCount >= $ordersMax2) or ($totalAmount>=$threshold2) ){
	
		if ($userDiscount == 0 or $userDiscount == $discount1)
		{
			$output = round($totalPrice * (1-$discount2/100), 2);
			
		}elseif($userDiscount <= $discount2)
		{
			$output = round($totalPrice * (1-$userDiscount/100), 2);
		}
		
	}
	  
  }
  $e->output($output);
	
}

if($e->name == 'OnSHKbeforeSendOrder')
{
	  
	if( ($ordersCount >= $ordersMax1 && $ordersCount < $ordersMax2) or  ($totalAmount >= $threshold1 && $totalAmount < $threshold2) )
	{
		if( $userDiscount == 0 )
		{
			$fields = array('discount'  => $discount1);  
			$modx->db->update( $fields, $table_user, 'internalKey = "' . $userID . '"' );
			return;
		}else{return;}
		
	}elseif( ($ordersCount >= $ordersMax2) or ($totalAmount>=$threshold2) )
		{
			if( $userDiscount == 0 or $userDiscount <= $discount2 )
		{
			$fields = array('discount'  => $discount2);  
			$modx->db->update( $fields, $table_user, 'internalKey = "' . $userID . '"' );
			return;
		}else{return;}
		
	}
	
}



/*Settings
&ordersMax1=Количество заказов №1;integer;5 &discount1=Скидка №1 (%);string;2 &threshold1=Пороговая величина №1;integer;150000 &ordersMax2=Количество заказов №2;integer;10 &discount2=Скидка №2 (%);string;3 &threshold2=Пороговая величина №2;integer;250000
*/