<?php
/**
* 寄件
* @date: 2018年1月19日 下午8:59:46
* @author: MIn晓天 QQ416883181
* @param:
* @return:
*/
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$log = array();
$aid = $_GPC['aid'];//收件人地址
$type = $_GPC['type'];
$settings = $this->module['config'];
$sql = "SELECT * FROM" . tablename("evzg_express_shop") . "WHERE enabled='1' AND 	uniacid='{$_W['uniacid']}'";
$shop = pdo_fetchall($sql);
$sql = "SELECT * FROM" . tablename("evzg_express_member") . "WHERE openid='{$_W['openid']}' AND 	uniacid='{$_W['uniacid']}'";
$member = pdo_fetch($sql);
$address = pdo_get("evzg_express_member_address",array('defeult'=>1,'openid'=>$_W['openid']));
if(!empty($aid)&&$type=='saddress'){
  $express_address =pdo_get("evzg_express_member_address",array('id'=>$aid));
}
/* if(!empty($aid)&&$type=='jaddress'){
    $address =pdo_get("evzg_express_member_address",array('id'=>$aid));
} */
foreach ($shop as $v){
    $jsonshop.='{"title":"'.$v['name'].'","value":"'.$v['id'].'",},';
}
$jsonshop = rtrim($jsonshop,',');
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'list';
$openid = $_W['openid'];
if($op=='add'){
    $sid = intval($_GPC['sid']);
    if(empty($_GPC['sid'])){
        message("请选择门店",referer(),"error");
    }
    $sql = "SELECT * FROM" . tablename("evzg_express_shop") . "WHERE id='{$sid}' AND uniacid='{$_W['uniacid']}'";
    $shopcontent = pdo_fetch($sql);
    $shopset = pdo_fetch("SELECT * FROM".tablename("evzg_express_shop_set")."WHERE sid='{$sid}' AND uniacid='{$_W['uniacid']}'");
    //是否开启自动计算
    if($shopset['isset']==1){
        $citys = $_GPC['scity'];
        $cityarr= explode(" ",$citys);
        $province = $_GPC['province'];
        $weight = floatval($_GPC['weight']);
        //是否省内
        if(strstr($shopcontent['province'],$cityarr[0])){
            $provinceprice = $shopset['provinceprice'];
            $addprice = $shopset['addprice'];
        }else{
            $provinceprice = $shopset['outsideprice'];
            $addprice = $shopset['outsideaddprice'];
        }
        //是否大于1公斤
        if($weight>1){
            $price = ceil($weight-1)*$addprice+$provinceprice;
        }else{
            $price =$provinceprice;
        }
    }else{
        $price = '0';
    }
    //需要更新数据
    $data['openid'] = $_W['openid'];
    $data['orderno'] = order_no();
    $data['uniacid'] = $_W['uniacid'];
    $data['sid'] =intval($_GPC['sid']);
    $data['weight'] = floatval($_GPC['weight']);
    $data['goods'] = $_GPC['goods'];
    $data['name'] = $_GPC['name'];
    $data['mobile'] = $_GPC['mobile'];
    $data['address'] =$_GPC['city'].' '.$_GPC['address'];
    $data['sname'] = $_GPC['sname'];
    $data['smobile'] = $_GPC['smobile'];
    $data['saddress'] = $citys.' '.$_GPC['saddress'];
    $data['shipperCode'] = $shopcontent['shipperCode'];
    $data['lat'] = $_GPC['lat'];
    $data['lng'] = $_GPC['lng'];
    $data['price'] = $price;
    $data['createtime'] = time();
    $data['paystatus'] =0;
    $result = pdo_insert("evzg_express_expressorder",$data);
    $oid=pdo_insertid();
    $expreuser = pdo_fetchall("SELECT * FROM".tablename("evzg_express_expressuser")."WHERE sid='{$_GPC['sid']}'");
    foreach ($expreuser as $v){
        $tpl = array(
            'openid'=>$v['openid'],
            "template_id" => $settings['msg']['order_jd_yy'],
            "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('dyindex'),
            "data" => array(
                'first' => "亲 有个快递需要你接单",
                'keyword2' => date('Y年m月d日 H:i', time()),
                'keyword1' => "\n 地址：{$_GPC['address']} \n联系方式：{$_GPC['mobile']} \n物品：{$data['goods']} \n重量：{$_GPC['weight']}kg ",
                'remark' => "{$_GPC['description']}"
            )
        );
        $tplLog = sendMsgTpl($tpl);
        if($tplLog){
            $log['status'] = 1;
        }
        $log['oid'] = $oid;
        $log['sid'] = $_GPC['sid'];
        $log['openid'] = $v['openid'];
        $log['operator'] = "店铺：".$shop['name'];
        $log['content'] = "亲 有个快递需要你接单 地址：{$_GPC['address']} 联系方式：{$_GPC['mobile']} 物品：{$data['goods']} 重量：{$_GPC['weight']}kg";
        $log['addtime']=time();
        $log['msg'] = $tplLog;
        $log['type'] = 2;
        msgLog($log);
    }
    if($result){
        message("下单成功",$this->createMobileUrl('userorder'),"success");
    }else{
        message("下单失败",$this->createMobileUrl('userorder'),"error");
    }
}
if($op=='post'){
    $sid = intval($_GPC['sid']);
    $said = intval($_GPC['said']);
    $aid = intval($_GPC['aid']);
    if(empty($_GPC['sid'])){
        ijson(array('code'=>'4001','msg'=>'请选择门店'));
    }
    $sql = "SELECT * FROM" . tablename("evzg_express_shop") . "WHERE id='{$sid}' AND uniacid='{$_W['uniacid']}'";
    $shopcontent = pdo_fetch($sql);
    $shopset = pdo_fetch("SELECT * FROM".tablename("evzg_express_shop_set")."WHERE sid='{$sid}' AND uniacid='{$_W['uniacid']}'");
    $saddress = pdo_get("evzg_express_member_address",array('id'=>$said));//收件地址
    $address = pdo_get("evzg_express_member_address",array('id'=>$aid));//寄件地址
    //是否开启自动计算
    if($shopset['isset']==1){
        $province = $saddress['province'];
        $weight = floatval($_GPC['weight']);
        //是否省内
        if(strstr($shopcontent['province'],$saddress['province'])){
            $provinceprice = $shopset['provinceprice'];
            $addprice = $shopset['addprice'];
        }else{
            $provinceprice = $shopset['outsideprice'];
            $addprice = $shopset['outsideaddprice'];
        }
        //是否大于1公斤
        if($weight>1){
            $price = ceil($weight-1)*$addprice+$provinceprice;
        }else{
            $price =$provinceprice;
        }
    }else{
        $price = '0';
    }
    $data = array();
    //需要更新数据
    $data['openid'] = $_W['openid'];
    $data['orderno'] = order_no();
    $data['uniacid'] = $_W['uniacid'];
    $data['sid'] = $sid;
    $data['weight'] = floatval($_GPC['weight']);
    $data['goods'] = $_GPC['goods'];
    $data['name'] = $address['name'];
    $data['mobile'] = $address['mobile'];
    $data['address'] =$address['province'].' '.$address['city'].' '.$address['community'].' '.$address['address'];
    $data['sname'] = $saddress['name'];
    $data['smobile'] = $saddress['mobile'];
    $data['saddress'] = $saddress['province'].' '.$saddress['city'].' '.$saddress['community'].' '.$saddress['address'];
    $data['shipperCode'] = $shopcontent['shipperCode'];
    $data['lat'] = $_GPC['lat'];
    $data['lng'] = $_GPC['lng'];
    $data['price'] = $price;
    $data['createtime'] = time();
    $data['paystatus'] =0;
    $result = pdo_insert("evzg_express_expressorder",$data);
    $oid=pdo_insertid();
    $expreuser = pdo_fetchall("SELECT * FROM".tablename("evzg_express_expressuser")."WHERE sid='{$_GPC['sid']}'");
    $log['oid'] = $oid;
    $log['sid'] = $_GPC['sid'];
    $log['operator'] = "店铺：".$shop['name'];
    foreach ($expreuser as $v){
        $log['openid'] = $v['openid'];
        $tpl = array(
            'openid'=>$v['openid'],
            "template_id" => $settings['msg']['order_jd_yy'],
            "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('dyindex'),
            "data" => array(
                'first' => "亲 有个快递需要你接单",
                'keyword2' => date('Y年m月d日 H:i', time()),
                'keyword1' => "\n 地址：{$_GPC['address']} \n联系方式：{$_GPC['mobile']} \n物品：{$data['goods']} \n重量：{$_GPC['weight']}kg ",
                'remark' => "{$_GPC['description']}"
            )
        );
        $tplLog = sendMsgTpl($tpl,$log);
    }
    if($result){
        ijson(array('code'=>'200','msg'=>'下单成功'));
    }else{
        ijson(array('code'=>'4003','msg'=>'下单失败'));
    }
}
$tpl ='default/';
include $this->template($tpl.'userorderadd');