<?php
$thisDir=realpath(dirname(__FILE__).'/../');
require_once("{$thisDir}/webini.inc.php");
extract($_POST);
if($action==='checkNameForGrade'){
	if(isNull($v)){
		$result="请输入新等级的名称";
	}else{
		$sql="select * from grade where comId={$_SESSION['comId']} and name='{$v} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="名称可用";
		}else{
			$result="名称已经存在";
		}
	}
	echo $result;
}else if($action==='saveAddGrade'){
	if(isNull($aName)){
		$result="请输入新等级的名称";
	}else if(!is_numeric($aStart) || floatval($aStart)<0){
		$result="请正确输入达到当前等级所需的最低消费金额";
	}else if(!is_numeric($aEnd) || floatval($aEnd)<floatval($aStart)){
		$result="请正确输入达到当前等级所需消费的最高金额";
	}else if(!is_numeric($aSale) || floatval($aSale)<0){
		$result="请正确输入达到当前等级所需消费的最高金额";
	}else{
		$sql="select * from grade where comId={$_SESSION['comId']} and name='{$aName} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$sql="insert into grade(comId,name,start,end,sale,addUser,ip) values({$_SESSION['comId']},'{$aName}','{$aStart}','{$aEnd}',{$aSale},'{$_SESSION['userNick']}','{$cg['ip']}');";
			$result=$db->execSql($sql,false)?"保存成功":"保存失败";
		}else{
			$result="名称已经存在";
		}
	}
	echo $result;
}else if($action=='editGradeName'){
	if(isNull($v)){
		$result="请输入修改后等级的名称";
	}else{
		$sql="select * from grade where comId={$_SESSION['comId']} and name='{$v}' and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$sql="update grade set name='{$v}' where id={$objId} limit 1";
			$result=$db->execSql($sql,false)?"修改成功":"修改失败";
		}else{
			$result="已存在相同名称的等级";
		}
	}
	echo $result;
}else if($action=='editGradeStart'){
	if(!preg_match($zz_shuzhi,$v) || floatval($v)<0){
		$result="请正确输入达到当前等级需要的最低消费金额";
	}else{
		$sql="select * from grade where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的会员等级";
		}else{
			$v=floatval($v);
			if($v>=floatval($row[0]['end'])){
				$result="最低消费不能大于或等于最高消费";
			}else{
				$sql="select * from grade where comId={$_SESSION['comId']} and start={$v} and id<>{$objId} limit 1";
				$row=$db->myQuery($sql,false);
				if($row===false || count($row)==0){
					$sql="update grade set start={$v} where id={$objId}";
					$result=$db->execSql($sql,false)?"修改成功":"修改失败";
				}else{
					$result="已经存在其它等级与此相同的最低消费";
				}
			}
		}
	}
	echo $result;
}else if($action=='editGradeEnd'){
	if(!preg_match($zz_shuzhi,$v) || floatval($v)<0){
		$result="请正确输入达到当前等级需要的最高消费金额";
	}else{
		$sql="select * from grade where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的会员等级";
		}else{
			$v=floatval($v);
			if($v<=floatval($row[0]['start'])){
				$result="最高消费不能小于或等于最低消费";
			}else{
				$sql="select * from grade where comId={$_SESSION['comId']} and end={$v} and id<>{$objId} limit 1";
				$row=$db->myQuery($sql,false);
				if($row===false || count($row)==0){
					$sql="update grade set end={$v} where id={$objId}";
					$result=$db->execSql($sql,false)?"修改成功":"修改失败";
				}else{
					$result="已经存在其它等级与此相同的最高消费";
				}
			}
		}
	}
	echo $result;
}else if($action=='editGradeSale'){
	if(!preg_match($zz_shuzhi,$v) || floatval($v)<0){
		$result="请正确输入打折值";
	}else{
		$sql="select * from grade where comId={$_SESSION['comId']} and id={$objId} limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="不存在指定的会员等级";
		}else{
			if(floatval($v)!=floatval($row[0]['sale'])){
				$sql="update grade set sale={$v} where id={$objId}";
				$result=$db->execSql($sql,false)?"修改成功":"修改失败";
			}else{
				$result="值相同,不需要修改";
			}
		}
	}
	echo $result;
}else if($action==="checkTelephoneForAddMember"){
	if(!preg_match($zz_telephone,$v) && !preg_match($zz_mobile,$v)){
		$result="格式错误";
	}else{
		$sql="select * from member where comId={$v} and telephone='{$v}' limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$result="正确<script type=\"text/javascript\">$('#aAccount').val('{$v}');</script>";
		}else{
			$result="电话号码已存在";
		}
	}
	echo $result;
}else if($action==='setMonestaryForAddMember'){
	if(!is_numeric($v)){
		$result="参数错误";
	}else if(intval($v)==-1){
		$result="请选择会员等级";
	}else{
		$grade=getGrade($v);
		if($grade===false){
			$result="不存在指定的会员等级";
		}else{
			$result="<script type=\"text/javascript\">$('#aMonestary').val({$grade['start']});</script>";
		}
	}
	echo $result;
}else if($action==="checkAccountForAddMember"){
	if(!preg_match($zz_telephone,$v) && !preg_match($zz_mobile,$v)){
		$result="格式错误";
	}else{
		$sql="select * from member where comId={$v} and account='{$v}' limit 1";
		$row=$db->myQuery($sql,false);
		$result=($row===false || count($row)==0)?"正确":"账号已存在";
	}
	echo $result;
}else if($action==='saveAddMember'){
	if(isNull($aName) || (!preg_match($zz_telephone,$aTelephone) && !preg_match($zz_mobile,$aTelephone)) || !preg_match($zz_userNick,$aAccount) || !preg_match($zz_shuzhi,$aMeny) || floatval($aMeny)<1){
		$result="必填数据有错误";
	}else if(!is_numeric($aGrade) || intval($aGrade)==-1){
		$result="请选择新会员的初始等级";
	}else if(!is_numeric($aMonestary) || floatval($aMonestary)<0){
		$result="请正确输入新会员已消费金额";
	}else if(floatval($aMonestary)>floatval($aMeny)){
		$result="开户充值金额不足";
	}else{
		$sql="select * from member where comId={$_SESSION['comId']} and (telephone='{$aTelephone}' or account='{$aAccount}') limit 1";
		$row=$db->myQuery($sql,false);
		if($row===false || count($row)==0){
			$aMeny=floatval($aMeny)-floatval($aMonestary);
			$sql="insert into member(comId,branch,grade,account,name,sex,telephone,address,status,meny,monetary,addUser,ip) values({$_SESSION['comId']},'{$_SESSION['branch']}',{$aGrade},'{$aAccount}','{$aName}',{$aSex},'{$aTelephone}','{$aAddress}',1,{$aMeny},{$aMonestary},'{$_SESSION['userNick']}','{$cg['ip']}');";
			$result=$db->execSql($sql,false)?"保存成功":"保存失败";
		}else{
			$result="相同的电话号码或账号已经存在";
		}
	}
	echo $result;
}
?>