<?php
/**
 *
 * @author min晓天
 * @url http://bbs.we7.cc/
 */
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$settings = $this->module['config'];
load()->func('tpl');
$params = array();
$operation = ! empty($_GPC['op']) ? $_GPC['op'] : 'data';
$sid = $_GPC['sid'];
mload()->model('dak');
if ($operation == 'data') {
    $where = "WHERE uniacid = :aid AND sid = :sid";
    $params[':aid'] = $_W['uniacid'];
    $params[':sid'] = $sid;
    $days = ((isset($_GPC['days']) ? intval($_GPC['days']) : 0));
    if ($days == -1)
    {
        $starttime = str_replace('-', '', trim($_GPC['stat_day']['start']));
        $endtime = str_replace('-', '', trim($_GPC['stat_day']['end']));
        $where .= ' and createtime >= :start_day and createtime <= :end_day';
        $params[':start_day'] = $starttime;
        $params[':end_day'] = $endtime;
    }
    else
    {
        $todaytime = strtotime(date('Y-m-d'));
        $starttime = strtotime('-' . $days . ' days', $todaytime);
        $endtime = $todaytime + 86399;
        $where .= ' and createtime >= :stat_day and createtime <= :end_day';
        $params[':stat_day'] = $starttime;
        $params[':end_day'] = $endtime;
    }
    //var_dump($params);die();
    $sql = "SELECT expressshop,shipperCode,COUNT(*) as expresnum FROM ".tablename("evzg_express_expres")."  {$where} GROUP BY expressshop HAVING COUNT(*) >=1";
    $channel_list = pdo_fetchall($sql,$params);
    $channelsall = pdo_fetchcolumn("SELECT COUNT(*) FROM".tablename("evzg_express_expres")."{$where}",$params);
    $channels = array();//定义现在有的快递类型
    $alldakprice =0;
    foreach ($channel_list as $v){
       if(empty($v['expressshop'])){
          $expressshop = "其他快递";
          $shipperCode = 'otherexpres';
       }else{
           $expressshop = $v['expressshop'];
           $shipperCode = $v['shipperCode'];
       }
       $dakprice = dak_charging($expressshop, $v['expresnum']);
       $alldakprice+=$dakprice;
       $channels[] = array(
           'expressshop'=>$expressshop,
           'expresnum'=>$v['expresnum'],
           'dakprice'=>$dakprice,
           'shipperCode'=>$shipperCode,
       );
    }
    if($_W['isajax']){
        $titles = array();
        $fields = array();
        $stat = array();
        $fields[] ='total_success_order';
        $stat[] = array('total_success_order'=>$channelsall);
        $titles[] = $channels[0]['expressshop'];
        foreach ($channels as $ks){
            $titles[] = $ks['expressshop'];
            $fields[] = $ks['shipperCode'];
            $stat[] = array($ks['shipperCode']=>$ks['expresnum']);
        }
        $chart = array('fields'=>$fields,'titles'=>$titles,'stat'=>$stat,'channels'=>$channels);
       message(error(0, $chart), '', 'ajax');
    }
    include $this->template('data');
}
if($operation=='settlement'){
    mload()->model('shop');
    $where = "WHERE uniacid = :aid AND sid = :sid AND issettlement= :issettlement";
    $params[':aid'] = $_W['uniacid'];
    $params[':sid'] = $sid;
    $params[':issettlement'] = 0;
    $days = ((isset($_GPC['days']) ? intval($_GPC['days']) : 0));
    if ($days == -1)
    {
        $starttime = str_replace('-', '', trim($_GPC['stat_day']['start']));
        $endtime = str_replace('-', '', trim($_GPC['stat_day']['end']));
        $where .= ' and createtime >= :start_day and createtime <= :end_day';
        $params[':start_day'] = $starttime;
        $params[':end_day'] = $endtime;
    }
    else
    {
        $todaytime = strtotime(date('Y-m-d'));
        $starttime = strtotime('-' . $days . ' days', $todaytime);
        $endtime = $todaytime + 86399;
        $where .= ' and createtime >= :stat_day and createtime <= :end_day';
        $params[':stat_day'] = $starttime;
        $params[':end_day'] = $endtime;
    }
    //var_dump($params);die();
    $sql = "SELECT expressshop,shipperCode,COUNT(*) as expresnum FROM ".tablename("evzg_express_expres")."  {$where} GROUP BY expressshop HAVING COUNT(*) >=1";
    $channel_list = pdo_fetchall($sql,$params);
    $sql = "SELECT * FROM ".tablename("evzg_express_expres")."  {$where} ";
    $express_list = pdo_fetchall($sql,$params);
    if(empty($express_list)){
        message("没有需要结算的项目或者已经结算过了",$this->createWebUrl('data',array('sid'=>$sid)),'error');
    }
    foreach ($express_list as $v){
        pdo_update("evzg_express_expres",array('issettlement'=>1),array('id'=>$v['id']));
    }
    $channelsall = pdo_fetchcolumn("SELECT COUNT(*) FROM".tablename("evzg_express_expres")."{$where}",$params);
    $channels = array();//定义现在有的快递类型
    $alldakprice =0;
    foreach ($channel_list as $v){
        if(empty($v['expressshop'])){
            $expressshop = "其他快递";
            $shipperCode = 'otherexpres';
        }else{
            $expressshop = $v['expressshop'];
            $shipperCode = $v['shipperCode'];
        }
        $dakprice = dak_charging($expressshop, $v['expresnum']);
        $alldakprice+=$dakprice;
        $channels[] = array(
            'expressshop'=>$expressshop,
            'expresnum'=>$v['expresnum'],
            'dakprice'=>$dakprice,
            'shipperCode'=>$shipperCode,
        );
    }
    $data = array();
    $data['uniacid'] = $_W['uniacid'];
    $data['sid'] = $sid;
    $data['channellist'] = json_encode($channels);
    $data['expresslist'] = json_encode($express_list);
    $data['alldakprice'] = $alldakprice;
    $data['startday'] = $starttime;
    $data['endday'] = $endtime;
    $data['createtime'] = time();
    $result = pdo_insert("evzg_express_settlement",$data);
    if($result){
       shop_update_credit2($sid, $alldakprice);
    }
    message("结算成功",$this->createWebUrl('data',array('sid'=>$sid)),'success');
}
if($operation=='export'){
    mload()->classs('phpexcelsheet');
    $where = "WHERE uniacid = :aid AND sid = :sid";
    $params[':aid'] = $_W['uniacid'];
    $params[':sid'] = $sid;
    $days = ((isset($_GPC['days']) ? intval($_GPC['days']) : 0));
    if ($days == -1)
    {
        $starttime = str_replace('-', '', trim($_GPC['stat_day']['start']));
        $endtime = str_replace('-', '', trim($_GPC['stat_day']['end']));
        $where .= ' and createtime >= :start_day and createtime <= :end_day';
        $params[':start_day'] = $starttime;
        $params[':end_day'] = $endtime;
    }
    else
    {
        $todaytime = strtotime(date('Y-m-d'));
        $starttime = strtotime('-' . $days . ' days', $todaytime);
        $endtime = $todaytime + 86399;
        $where .= ' and createtime >= :stat_day and createtime <= :end_day';
        $params[':stat_day'] = $starttime;
        $params[':end_day'] = $endtime;
    }
    //var_dump($params);die();
    $sql = "SELECT * FROM ".tablename("evzg_express_expres")."  {$where} ";
    $channel_list = pdo_fetchall($sql,$params);
    $channels = array();//定义现在有的快递类型
    $alldakprice =0;
    $xls = new Excel('manage'); //构造函数，参数为第一个sheet名称
    $tableheader = array('ID','单号','取件码','快递公司','手机号','状态','录入时间','取件时间');
    $xls->worksheets['manage']->addRow($tableheader); //添加一行，数据为1,2,3
    foreach ($channel_list as $mid => $value) {
        if(empty($v['expressshop'])){
            $expressshop = "其他快递";
            $shipperCode = 'otherexpres';
        }else{
            $expressshop = $v['expressshop'];
            $shipperCode = $v['shipperCode'];
        }

        if($value['status']==1){
            $status="已取件";
            $expresstime = date("Y-m-d H:i:s",$value['expresstime']);
        }else{
            $status="待取件";
            $expresstime="暂无";
        }
        $createtime = date("Y-m-d H:i:s",$value['createtime']);
        $html = array($value['id'],$value['expresssn'],$value['orderno'],$expressshop,$value['mobile'],$status,$createtime,$expresstime);

        $xls->worksheets['manage']->addRow($html); //添加一行，数据为1,2,3

    }
    $now = date('Y-m-d H:i:s', time());
    $filename ='订单信息_'.$now;
    $xls->generate($filename);//下载excel，参数为文件名
}