<?php
define('IN_PHPMPS', true);
require dirname(__FILE__) . '/include/common.inc.php';
require PHPMPS_ROOT . 'include/pay.fun.php';
get_login();
  $paycode = $_POST['paycode'] ? trim($_POST['paycode']) : '';
  if(empty($paycode) || $_SESSION['lastposttime']==$paycode) {
	showmsg('请勿多次提交，以免重复扣费！');
  }
  $comid  = $_POST['id'] ? intval($_POST['id']) : '';
  $code = $_POST['money'] ? trim($_POST['money']) : '';
  $juanid = $_POST['juan'] ? trim($_POST['juan']) : '';
  if($code <= 0)showmsg('支付金额必须大于0');
  $act_time = time();
  $sql = "select * from {$table}com_config where comid='$comid'";
  $cominfo = $db->getRow($sql);
  if(empty($cominfo))showmsg('错误，未知店铺，请重新扫描二维码支付！');
  $cominfo['jf_bl']   = explode('|',$cominfo['jf_bl']);
  $cominfo['sms_key'] = explode('|',$cominfo['sms_key']);
  $cominfo['card']    = member_info("cardnum,money",3);
  $cominfo['cards']   = $cominfo['card']['cardnum'];
  $sql = "SELECT userid,comname,jf_num,sms_num FROM {$table}com WHERE comid='$comid'";
  $coms = $db->getRow($sql);
  if($coms['jf_num'] < $cominfo['jifen'])showmsg('商家积分不足，终止付款！');
  
if($cominfo['cards']){ 
  $zk_list = array();
  foreach (explode('|',$cominfo['zk_list']) as $row){
	 $zk_list[] = explode(',',$row);
  }
  foreach($zk_list as $key=>$val){
   if($val[0]!=0){
	if($key==0){
	  if($code < $val[0]){
	   $zekou = '无';
	   $siso  = $code;
	  }
	}
	if($code >= $val[0]){
	  $zekou = $val[1];
	  $siso  = number_format($code*($val[1]/100),2);
	}
   }else{
	  $zekou = '无';
	  $siso  = $code; 
   }
  }
  
  if($cominfo['zk_sojian']>0 && $cominfo['zk_sojian']<$code){
	  $sql = "select count(*) from {$table}pay_exchange where userid='$_userid' and comid='$comid'";
	  if($db->getOne($sql) <1){
	      $siso = $siso-$cominfo['zk_sojian'];
		  $soj = $cominfo['zk_sojian'];
	  }
  }  
  
  $cominfo['jf_day']  = explode('|',$cominfo['jf_day']);
  $todays  = date('j', time());
  if(!empty($cominfo[jf_bl][0]) && !empty($cominfo[jf_bl][1])){
   if(strpos($cominfo['jf_day'][0],strval(",".$todays.","))!==false){
    $cominfo['jifen']   = floor($code/$cominfo['jf_bl'][0]*$cominfo['jf_bl'][1]*$cominfo['jf_day'][1]);
	$notemsg.= '-优惠日'.$cominfo['jf_day'][1].'倍积分';
   }else{
    $cominfo['jifen'] = floor($code/$cominfo['jf_bl'][0]*$cominfo['jf_bl'][1]);
   }
  }else{
    $cominfo['jifen'] = '0'; 
  }
  $db->query("UPDATE {$table}com SET jf_num=jf_num-".$cominfo['jifen']." WHERE comid='$comid'");
  $db->query("UPDATE {$table}member SET credit=credit+".$cominfo['jifen']." WHERE userid='$_userid'");
  $card_comid = $db->getOne("select comid from {$table}card where cardnum='".$cominfo['cards']."'");
}else{
    $siso = $code;
	$cominfo['jifen'] = '0';	
}
  $juanrow = $db->getRow("SELECT * FROM {$table}juan_pay WHERE userid='$_userid' and id='$juanid' and money<'$code' and paytime+(3600*24*yxday)>='$act_time' and acttime='' and '$code'>=xfmy and comid=(case when comid!=0 then '$comid' else '' end)");
  if($juanrow){
    $siso = $siso-$juanrow['money'];
	$lej = $juanrow['money'];
	//$notemsg.= '-'.$CFG['lejuan'].'减'.$juanrow['money'].'元';
	$db->query("UPDATE {$table}juan_pay SET acttime='$act_time',actcid='$comid' WHERE id='$juanid' and userid='$_userid'");
  }
  $sisos = number_format($siso-($siso*($cominfo['fc']/100)),2);//实入帐
  $fc    = number_format($siso*($cominfo['fc']/100),2);//分成金额
  if($card_comid>0 && $card_comid!=$comid){
	$shop_card_fc = number_format($siso*($CFG['com_shop_card']/100),2);
	card_gold_add($cominfo['cards'],$siso,$shop_card_fc,$card_comid);
  }
  
  $orderid = date('ymd').$comid.random(8,1); //订单号
  money_diff($_userid, $siso,'消费'.$notemsg,$zekou,$code,$soj,$lej,$cominfo['jifen'],$fc,$comid,$cominfo['cards'],$orderid);
  pay_juan(3,'',$code);
  $db->query("UPDATE {$table}member SET gold=gold+$sisos WHERE userid=".$coms['userid']);
  $userinfos = member_info("phone,money,credit,cardnum",'3');
   SendSMS($CFG['sms_ming'],$CFG['sms_msg_tplid'],'{"comname":"'.$coms['comname'].'","money":"'.$siso.'","jifen":"'.$cominfo['jifen'].'","yuer":"'.$userinfos['money'].'"}',$userinfos['phone'],'991',$comid);
  $_SESSION['lastposttime'] = $paycode; 
  $seo['title'] = $CFG['webname'] . '－支付结果';
  include tpl('act_com_pay');
?>