<?php
define('IN_PHPMPS', true);
require_once dirname(__FILE__) . '/include/common.php';

$_REQUEST['act'] = $_REQUEST['act'] ? trim($_REQUEST['act']) : 'list_card' ;

$mids = array('1'=>'全部会员','2'=>'绑定卡会员','3'=>'非绑定卡会员');
$sids = array('1'=>'首次注册','2'=>'首次绑定手机','3'=>'消费满￥','4'=>'首次绑定卡');

switch ($_REQUEST['act'])
{
	//折扣卡
	case 'list_card':
		$page = empty($_REQUEST[page])? 1 : intval($_REQUEST['page']);
		$sql = "SELECT COUNT(*) FROM {$table}card";
		$count = $db->getOne($sql);
		$pager = get_pager('tools.php',array('act'=>'list_card'),$count,$page,'30');

		$sql = "SELECT * FROM {$table}card ORDER BY acttime DESC,id ASC LIMIT $pager[start],$pager[size]";
		$res = $db->query($sql);
		$cards = array();
		while($row = $db->fetchRow($res)) {
			$row['acttime'] = ($row['acttime']=='0') ? '未激活' : '<span style=color:red;font-weight:bold>'.date('Y-m-d',$row['acttime']).'</span>';
			$cards[] = $row;
		}
	    include tpl('list_tool_card');
	break;
	
	//在线购卡记录
	case 'list_shop_card':
		$page = empty($_REQUEST[page])? 1 : intval($_REQUEST['page']);
		$sql = "SELECT COUNT(*) FROM {$table}shop_card";
		$count = $db->getOne($sql);
		$pager = get_pager('tools.php',array('act'=>'list_shop_card'),$count,$page,'30');

		$sql = "SELECT * FROM {$table}shop_card ORDER BY addtime DESC LIMIT $pager[start],$pager[size]";
		$res = $db->query($sql);
		$cards = array();
		while($row = $db->fetchRow($res)) {
			$row['addtime'] = date('Y-m-d',$row['addtime']);
			$row['zt'] = $row['zt']==0? '未配送' : '已配送';
			$cards[] = $row;
		}
	    include tpl('list_tool_shop_card');
	break;
	
	//购卡记录操作
	case 'shopcard_batch':
		$id = is_array($_REQUEST['id']) ? join(',', $_REQUEST['id']) : intval($_REQUEST['id']);
		if(empty($id))show('没有选择记录');
		if(empty($_REQUEST['type']))show('没有选择操作');
		switch ($_REQUEST['type'])
		{
			case 'delete':	
				//删除评论
				$sql = "DELETE FROM {$table}shop_card WHERE id IN ($id)";
				$db->query($sql);
				admin_log("删除记录 $id 成功");
				show('删除购卡记录成功', $_SERVER['HTTP_REFERER']);
			break;

			case 'zt0':
				$sql = "UPDATE {$table}shop_card SET zt=0 WHERE id IN ($id)";
				$re = $db->query($sql);
				admin_log("变更未配送 $id 成功");
				show('操作成功', $_SERVER['HTTP_REFERER']);
			break;
			
			case 'zt1':
				$sql = "UPDATE {$table}shop_card SET zt=1 WHERE id IN ($id)";
				$re = $db->query($sql);
				admin_log("变更已配送 $id 成功");
				show('操作成功', $_SERVER['HTTP_REFERER']);
			break;
		}
	break;
	
	case 'add_card':
	    $cards = $_POST['cards'] ? trim($_POST['cards']) : '';
		if($cards=="ok") {
		$snum   = $_POST['snum'] ? intval($_POST['snum']) : '';
        $enum   = $_POST['enum'] ? intval($_POST['enum']) : '';
        $nonum  = $_POST['nonum'] ? trim($_POST['nonum']) : '';
		$comid  = $_POST['comid'] ? intval($_POST['comid']) : '';
		if(!$snum || !$enum)show("起始号码两项不能含有空值！");
        $arr = array();
            for($i=$snum; $i<$enum+1; $i++){
			$rand = random(8);//rand(00000000,99999999);
            $arr[] = $rand;
	          if (strpos($i,$nonum)==false) {
                 $db->query("INSERT INTO {$table}card (`cardnum`,`cardkey`,`comid`) VALUES('$i','$rand','$comid')");
	          }
            }
		admin_log("批量生成卡号成功");
        show("卡号生成成功！","tools.php?act=list_card");
		}else{
			include tpl('add_tool_card');
		}		
	break;
	
	case 'card_batch':
		$id = is_array($_REQUEST['id']) ? join(',', $_REQUEST['id']) : intval($_REQUEST['id']);
		$comid = $_REQUEST['comid'] ? intval($_REQUEST['comid']) : '0';
		if(empty($id))show('没有选择记录');
		$sql = "UPDATE {$table}card SET comid=$comid WHERE id IN ($id)";
		$re = $db->query($sql);
		admin_log("批量移库成功");
		$link = 'tools.php?act=list_card';
		show('移库成功', $link);
	break;
	
	//代金券
	case 'list_juan':
		$page = empty($_REQUEST[page])? 1 : intval($_REQUEST['page']);
		$sql = "SELECT COUNT(*) FROM {$table}juan";
		$count = $db->getOne($sql);
		$pager = get_pager('tools.php',array('act'=>'list_juan'),$count,$page,'30');
		$sql = "SELECT a.*,count(c.jid) as sendnum FROM {$table}juan as a left join {$table}juan_pay as c on a.id=c.jid group by a.id ORDER BY id DESC LIMIT $pager[start],$pager[size]";
		$res = $db->query($sql);
		$juans = array();
		while($row = $db->fetchRow($res)) {
			$row['mid'] = $mids[$row['mid']];
			$row['klc'] = $row['klc']!=0 ? $row['klc'] : '不限';
			$row['smoney'] = $row['ssid']=='3' ? $row['smoney'] : '';
			$row['ssid'] = $sids[$row['ssid']].$row['smoney'];
			$row['acttime'] = date('Y-m-d',$row['acttime']);
			$juans[] = $row;
		}
	    include tpl('list_tool_juan');
	break;
	
	case 'list_userjuan':
		$page = empty($_REQUEST[page])? 1 : intval($_REQUEST['page']);
		$sql = "SELECT COUNT(*) FROM {$table}juan_pay";
		$count = $db->getOne($sql);
		$pager = get_pager('tools.php',array('act'=>'list_juan'),$count,$page,'30');	

		$sql = "SELECT a.*,c.* FROM {$table}juan as a left join {$table}juan_pay as c on a.id=c.jid where a.id=c.jid ORDER BY c.id DESC LIMIT $pager[start],$pager[size]";
		$res = $db->query($sql);
		$juans = array();
		while($row = $db->fetchRow($res)) {
			$row['mid'] = $mids[$row['mid']];
			$row['smoney'] = $row['ssid']=='3' ? $row['smoney'] : '';
			$row['ssid'] = $sids[$row['ssid']].$row['smoney'];
			$row['yxday'] = $row['paytime']+(3600*24*$row['yxday'])>=time() ? date('Y-m-d',$row['paytime']+(3600*24*$row['yxday'])) : '已过期';
			$row['acttime'] = $row['acttime']>0 ? date('Y-m-d',$row['acttime']) : '未使用';
			$juans[] = $row;
		}
	    include tpl('list_tool_userjuan');
	break;
	
	case 'add_juan':
	    $juans = $_POST['juans'] ? trim($_POST['juans']) : '';
		if($juans=="ok") {
		$money   = $_POST['money'] ? trim($_POST['money']) : '';
		$mid   = $_POST['mid'] ? intval($_POST['mid']) : '';
		$ssid   = $_POST['ssid'] ? intval($_POST['ssid']) : '';
		$smoney   = $_POST['smoney'] ? trim($_POST['smoney']) : '';
		$xfmy   = $_POST['xfmy'] ? trim($_POST['xfmy']) : '';
		$yxday   = $_POST['yxday'] ? intval($_POST['yxday']) : '';
		$klc   = $_POST['klc'] ? intval($_POST['klc']) : '';
		$comid   = $_POST['comid'] ? intval($_POST['comid']) : '';
		$nums   = $_POST['nums'] ? trim($_POST['nums']) : '';
		if(!$money || !$yxday)show("代金券金额或有效期不能含有空值！");
		$db->query("INSERT INTO {$table}juan (`money`,`mid`,`ssid`,`smoney`,`xfmy`,`yxday`,`comid`,`nums`,`klc`) VALUES('$money','$mid','$ssid','$smoney','$xfmy','$yxday','$comid','$nums','$klc')");
		admin_log("添加".$money."元代金券成功");
        show("代金券添加成功！","tools.php?act=list_juan");
		}else{
			include tpl('add_tool_juan');
		}		
	break;
	
	case 'edit_juan':
	    $id = intval($_REQUEST['id']);
		$sql = "SELECT * FROM {$table}juan WHERE id = '$id'";
		$juans = $db->getRow($sql);
		$here = "编辑代金券";
		include tpl('edit_tool_juan');
	break;
	
	case 'update_juan':
		if(empty($_POST['money']))show("请填写金额");
		if(empty($_POST['yxday']))show("请填写有效期");
		
		$id        = intval($_POST['id']);
		$money     = trim($_POST['money']);
		$mid        = intval($_POST['mid']);
		$ssid        = intval($_POST['ssid']);
		$smoney     = trim($_POST['smoney']);
		$xfmy     = trim($_POST['xfmy']);
		$yxday = intval($_POST['yxday']);
		$comid    = intval($_POST['comid']);
		$klc   = intval($_POST['klc']);
		$nums   = trim($_POST['nums']);
		$sql = "UPDATE {$table}juan SET 
				money='$money',
				mid='$mid',
				ssid='$ssid',
				smoney='$smoney',
				xfmy='$xfmy',
				yxday='$yxday',
				comid='$comid',
				klc='$klc',
				nums='$nums'
				WHERE id = '$id'";
		$res = $db->query($sql);

		admin_log("编辑".$money."元代金券成功");
		$link = "tools.php?act=list_juan";
		show('编辑代金券信息成功', $link);
	break;
	
	/*case 'del_juan':
	    $id = $_REQUEST['id'] ? intval($_REQUEST['id']) : '';
		if(empty($id))show('没有选择记录');
		$sql = "delete from {$table}juan where id=$id";
        $re = $db->query($sql);
		admin_log("删除代金券 $id 成功");
		$link = 'tools.php?act=list_juan';
		show('删除代金券成功', $link);
	break;*/
	
	case 'close_juan':
	    $id = $_REQUEST['id'] ? intval($_REQUEST['id']) : '';
		if(empty($id))show('没有选择记录');
		$sql = "UPDATE {$table}juan SET nums='0' where id=$id";
        $re = $db->query($sql);
		admin_log("关闭代金券 $id 成功");
		$link = 'tools.php?act=list_juan';
		show('关闭代金券成功', $link);
	break;
	
	//财务
	case 'pay_list':
	    $typeid = $_REQUEST['type'] ? trim($_REQUEST['type']) : '';
		$uid    = $_REQUEST['uid'] ? intval($_REQUEST['uid']) : '';
		$begindate = htmlspecialchars_deep($_REQUEST['begindate']);
		$enddate = htmlspecialchars_deep($_REQUEST['enddate']);
		if($typeid=="userids"){
		 if(!empty($uid)){ 
		  $typename .= " where 1 ";
          $typelist .= " and userid=".$uid;
		 }
		}elseif($typeid=="comids"){
		 if(!empty($uid)){ 
		  $typename .= " where 1 ";
          $typelist .= " and comid=".$uid;
		  $typelist1 .= " and a.comid=".$uid;
		 }
		}
		if(!empty($typeid)){
		if($begindate) {
			$begintime = strtotime($begindate.' 00:00:00');
			$sql .= " AND addtime>=$begintime ";
			$sql1.= " AND registertime>=$begintime ";
			$sql2.= " AND txtime>=$begintime ";
			$sql3.= " AND paytime>=$begintime ";
			$sql4.= " AND receivetime>=$begintime ";
		}
		if($enddate) {
			$endtime = strtotime($enddate.' 23:59:59');
			$sql .= " AND addtime<=$endtime";
			$sql1.= " AND registertime<=$endtime ";
			$sql2.= " AND txtime<=$endtime ";
			$sql3.= " AND paytime<=$endtime ";
			$sql4.= " AND receivetime<=$endtime ";
		}
		}
		$count_card_user = $db->getOne("SELECT COUNT(*) FROM {$table}member where cardnum>0");
		$re = $db->getRow("SELECT count(*) as jyl,sum(yjia) as zje,sum(value) as zkj,sum(soj) as soj,sum(lej) as lej,sum(jifen) as jifen,sum(fc) as fc,sum(value-fc) as siso FROM {$table}pay_exchange where 1 $typelist $sql");
		//$re['siso'] = number_format($re['siso'],2);
		if($typeid=="userids"){
		$count_user = $db->getOne("SELECT COUNT(*) FROM {$table}member where 1 $sql1");
		$cong_user = $db->getOne("SELECT sum(value) FROM {$table}pay_add where value>0 $typelist $sql");
		$info_user = $db->getRow("SELECT sum(money) as money,sum(credit) as jifen FROM {$table}member $typename $typelist");
		$jf_user = $db->getRow("SELECT sum(cnum) as jifen,sum(value) as money FROM {$table}pay_credit where tid=0 $typelist $sql");
		}
		$tx_user = $db->getOne("SELECT sum(money) FROM {$table}pay_tx where tid=2 $typelist $sql2");
		$juan_user = $db->getRow("SELECT sum(c.money) as money,count(a.jid) as nums FROM {$table}juan_pay as a left join {$table}juan as c on a.jid=c.id where a.jid=c.id $typelist1 $sql3");
		$com_credit = $db->getRow("SELECT sum(amount) as money,sum(nums) as nums FROM {$table}pay_online where moneytype='CRE' $typelist $sql4");
		$com_sms = $db->getRow("SELECT sum(amount) as money,sum(nums) as nums FROM {$table}pay_online where moneytype='SMS' $typelist $sql4");
		$com_sp = $db->getRow("SELECT sum(jf_num) as jf_num,sum(sms_num) as sms_num FROM {$table}com $typename $typelist");
		$tx_com = $db->getOne("SELECT sum(money) FROM {$table}pay_tx where tid=1 $typelist $sql2");
		$card_money = $db->getOne("SELECT sum(money) FROM {$table}shop_card where zt=1 $sql");
		$card_fc = $db->getOne("SELECT sum(fc) FROM {$table}card_fc $typename $typelist $sql");
		if($typeid=="comids"){
		$com_yuer = $db->getOne("SELECT sum(gold) as gold FROM {$table}com as a left join {$table}member as c on a.userid=c.userid $typename $typelist");
		}
		include tpl('list_tool_paylist');
	break;
	
	//佣金汇总
	case 'pay_fwf':
	    extract($_REQUEST);
        $page = empty($page)? 1 : intval($page);
		$payid = intval($payid);
        $sql = 'WHERE 1';
		if($begindate) {
        	$begintime = strtotime($begindate.' 00:00:00');
        	$sql .= " AND addtime>=$begintime ";
        }
        if($enddate) {
        	$endtime = strtotime($enddate.' 23:59:59');
        	$sql .= " AND addtime<=$endtime";
        }
		if($payid==1)$pays=' AND payid=1';
        $count = $db->getOne("select count(c.comid) from (select * from {$table}pay_exchange $sql $pays GROUP BY comid) as c ");
        $pager = get_pager('tools.php', array('act'=>'pay_fwf','begintime'=>$begintime,'enddate'=>$enddate,'payid'=>$payid), $count, $page,'20');
        $exchanges = array();
        $result = $db->query("SELECT a.comid,a.comname,sum(b.value) as all_value,sum(b.fc) as all_fc,sum(b.lej) as all_lej,c.fc from {$table}com as a left join {$table}pay_exchange as b on a.comid=b.comid left join {$table}com_config as c on a.comid=c.comid $sql and a.comid=b.comid $pays group by a.comid ORDER BY all_fc DESC LIMIT $pager[start],$pager[size]");
        while($r = $db->fetchrow($result)) {
            $r['all_value'] = number_format($r['all_value'],2);
			$r['all_fc'] = number_format($r['all_fc'],2);
			$r['all_lej'] = number_format($r['all_lej'],2);
        	$exchanges[] = $r;
        }
        include tpl('list_fw_exchange');
	break;
}
?>