<?php
/** 店铺登录
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace App\Model;
use Think\Model;
class ShopbalanceLogModel extends Model{
    // 资金变更记录
    public function capital_flow($paramdata){
        $map=array();
        $pid=idtoname($paramdata['sid'],'ShopsAccount','pid','id',false);
        //        if($pid > 0){
        //            $map['sa_id']=intval($paramdata['sid']);
        //        }else{
        //            $mapsa=array();
        //            $mapsa['pid'] = $pid;
        //            $ids = M('ShopsAccount')->where($mapsa)->getField('id',true);
        //            $map['sa_id']=array('in',$ids);
        //        }
        $day = intval($paramdata['num']);
        if($day > 0){
            if($day==1){
                $start_time=date("Y-m-d",NOW_TIME);
            }else{
                $start_time=date("Y-m-d",strtotime("-$day day"));
            }

            $map['change_time'][]=array('egt',strtotime("$start_time 00:00:00"));
            $map['change_time'][]=array('elt',NOW_TIME);
            $map['change_type'] = 0;
        }
        $map['type']=6;
        $map['shop_id']=intval($paramdata['shop_id']);
        $map['status']=3;
        $pagesize=15;
        $list=page_list('ShopbalanceLog',$map,'change_time desc','id,shop_id,order_id,change_type,change_num,change_time',$pagesize,'',1);
        foreach($list['list'] as $k=>$v){
            $list['list'][$k]['change_time']=time_format($v['change_time']);
            $list['list'][$k]['change_num']=format_price($v['change_num']);
            $assist_code=idtoname($v['order_id'],'OrderInfo','assist_code');
            if($assist_code){
                $list['list'][$k]['pay_name']=$this->payname($assist_code);
            }else{
                $list['list'][$k]['pay_code']=$pay_code=strtoupper(idtoname($v['order_id'],'OrderInfo','pay_code'));
                $pay_name=idtoname($pay_code,'Payment','name','code',false);
                $list['list'][$k]['pay_name']=$pay_name?$pay_name:其他;
            }

        }
        return $list['list'];

    }
    /**资金明细
    * @param bool $is_return
    */
    public function money_list($postdata){
        $map=array();
        $day = intval($postdata['num']);
        if($day !=99){
            $between=$this->quick_time($day);
            $map['create_time']=array('between',array($between[0],$between[1]));
        }
        if($postdata['in_online']==1){$map['in_online']=0;}
        elseif($postdata['in_online']==2){$map['in_online']=1;}
        switch($postdata['type']){
            case 2:$map['type']=$postdata['type'];
          break;
            case 3:$map['type']=array('in','1,3,6,10,11,12,13,14,25');
          break;
            case 15:$map['type']=$postdata['type'];
          break;
            case 20:$map['type']=array('in','20,21,31');
          break;
            default:$map['type']=array('neq','30');
          break;
        }
        if(!empty($postdata['sale_id'])) $map['guide']=$postdata['sale_id'];
        if(!empty($postdata['shop_id'])) $map['shop_id']=$postdata['shop_id'];
        if(!empty($postdata['cashier_name'])) $map['cashier_name']=$postdata['cashier_name'];
        $map['is_paid']=1;//已完成
        $list=page_list('PayLog',$map,'id desc',true,15,'',1);
        if($list['list']){
            foreach ($list['list'] as $k => $v) {
                $list['list'][$k]=$this->format_cont($v);
            }
        }
        $return=$list['list'];
        return $return;
    }
    public function format_cont($data){
        if(empty($data))return false;
        if(is_numeric($data['order_id'])){
            $order_id=$data['order_id'];
        }else{
            $orderid=explode(',',$data['order_id']);
            $order_id=$orderid[1];
        }
        $data['order_id']=intval($order_id);

        $order_total_price=$data['use_money']+$data['cash_money']+$data['scan_money']+$data['bank_money']+$data['card_money']+$data['use_score_money'];
        if($order_total_price!=$data['order_total_price'] && $order_total_price>0)$data['order_total_price']=$order_total_price;
        $data['change_num']=format_price($data['order_total_price']);
        $data['change_time']=time_format($data['create_time']);
        $data['create_time']=time_format($data['create_time']);
        $data['change_type']=0;
        switch ($data['type']) {
            case 1:$data['pay_name']='商品';
                break;
            case 2:$data['pay_name']='储值';
                break;
            case 3:$data['pay_name']='积分购买';
                break;
            case 4:$data['pay_name']='其他';
                break;
            case 5:$data['pay_name']='门店充值';
                break;
            case 6:$data['pay_name']='拼团';
                break;
            case 10:$data['pay_name']='订货';
                break;
            case 11:$data['pay_name']='服务';
                break;
            case 12:$data['pay_name']='服务';
                break;
            case 13:$data['pay_name']='收银';
                break;
            case 14:$data['pay_name']='购买商品';
                break;
            case 15:$data['pay_name']='礼品卡销售';
                break;
            case 20:$data['pay_name']='取消订单';$data['change_type']=1;
                break;
            case 21:$data['pay_name']='退货';$data['change_type']=1;
                break;
            case 25:$data['pay_name']='购买商品';
                break;
            case 30:$data['pay_name']='店铺订货';$data['change_type']=1;
                break;
            case 31:$data['pay_name']='线下直接退货';$data['change_type']=1;
                break;
        }
        return $data;

    }
    function quick_time($type){
        $map=array();
        $start_time = strtotime(date('Y-m-d',NOW_TIME)); 
        if($type == 1){
            $date_from=$start_time;
            $date_to =$start_time+86399;
            $map[] = $date_from;
            $map[] = $date_to;
        }elseif($type == 2){
            $date_from=$start_time-86400;
            $date_to =$start_time-1;
            $map[] = $date_from;
            $map[] = $date_to;
        }elseif($type == 7){
            $date_from=$start_time-6*86400;
            $date_to =$start_time+86399;
            $map[] = $date_from;
            $map[] = $date_to;
        }elseif($type == 30){
            $date_from=$start_time-29*86400;
            $date_to =$start_time+86399;
            $map[] = $date_from;
            $map[] = $date_to;
        }elseif($type == -1){
            $date_from=$start_time-24*3600;
            $date_to =$start_time;
            $map[] = $date_from;
            $map[] = $date_to;
        }
        return $map;
    }
    public function info($paramdata){
        $return=array('status'=>0,'msg'=>'');
        $map=array();
        $map['order_id']=$paramdata['id'];
        $info=M('PayLog')->alias('pl')->where($map)->join('__ORDER_INFO__ as o on o.id=pl.order_id')->field('o.order_sn,o.pay_code,o.sa_id,o.goods_allmoney,o.bonus_money,o.member_discount,o.custom_discount,pl.create_time,pl.order_total_price,o.order_amount,pl.payment_fee,o.assist_code,o.guide,o.pay_status')->find();
        if($info){
            $info['cashier_name']=$info['sa_id']==0?'':idtoname($info['sa_id'],'ShopsAccount','name');
            $info['daogou_name']=$info['guide']==0?'':idtoname($info['guide'],'ShopsAccount','name');
            $info['goods_allmoney']=format_price($info['goods_allmoney']);
            $info['create_time']=time_format($info['create_time']);
            $info['order_total_price']=format_price($info['order_total_price']);
            $info['bonus_money']=format_price($info['bonus_money']);
            $info['member_discount']=format_price($info['member_discount']);
            $info['custom_discount']=format_price($info['custom_discount']);
            $info['pay_amount']=format_price($info['pay_amount']);
            $info['payment_fee']=format_price($info['payment_fee']);
            $info['order_amount']=format_price($info['order_amount']);
            if($info['assist_code']){
                $assist_code=$this->payname($info['assist_code']);
            }else{
                $info['pay_name']=idtoname($info['pay_code'],'Payment','name','code',false)==false?'':idtoname($info['pay_code'],'Payment','name','code',false);
            }

            $return['status']=1;
            $return['data']=$info;
        }else{
            $return['msg']='订单信息获取错误';
        }
        return $return;
    }
    public function bill($paramdata){
        $return=array('status'=>0,'msg'=>'');
        if(!$paramdata['shop_id']){
            return $return;
        }else{
            $return['status']=1;
        }
        $return['shop_name']=idtoname($paramdata['shop_id'],'Shops','name','id',false);
        $return['login_name']=idtoname($paramdata['shop_id'],'Shops','login_name','id',false);
        $return['thumb']=idtoname($paramdata['shop_id'],'Shops','thumb','id',false);
        $map=$map_td=$map7=$map_m=array();
        $today = date('Y-m-d',NOW_TIME);

        $map['change_type']=$map_td['change_type']=$map7['change_type']=$map_m['change_type']=0;
        $map['type']=$map_td['type']=$map7['type']=$map_m['type']=6;
        $map['status']=$map_td['status']=$map7['status']=$map_m['status']=3;
        $map['shop_id']=$map_td['shop_id']=$map7['shop_id']=$map_m['shop_id']=intval($paramdata['shop_id']);
        $start_timet=$today;
        $start_time7=date("Y-m-d",strtotime("-7 day"));
        $start_timem=date("Y-m-d",strtotime("-30 day"));
        $end_time=date('Y-m-d',NOW_TIME);

        $map_td['change_time'][]=array('egt',strtotime("$start_timet 00:00:00"));
        $map_td['change_time'][]=array('elt',strtotime("$end_time 23:59:59"));

        $map7['change_time'][]=array('egt',strtotime("$start_time7 00:00:00"));
        $map7['change_time'][]=array('elt',strtotime("$end_time 23:59:59"));

        $map_m['change_time'][]=array('egt',strtotime("$start_timem 00:00:00"));
        $map_m['change_time'][]=array('elt',strtotime("$end_time 23:59:59"));
        $return['td_income']=format_price(M('ShopbalanceLog')->where($map_td)->sum('change_num'));
        $aa=M('ShopbalanceLog')->where($map_td)->getField('change_num',true);
        $return['income7']=format_price(M('ShopbalanceLog')->where($map7)->sum('change_num'));
        $return['incomem']=format_price(M('ShopbalanceLog')->where($map_m)->sum('change_num'));

//       折线图
        $num=0;
        $day_array=array();
        for($i=0;$i<7;$i++){
            $day_day=$i-7;
            $day_array[]=date("m/d",strtotime("$day_day day"));//每隔一天赋值给一个数组
            $day=date("Y-m-d",strtotime("$day_day day"));
            $map['change_time']=array();
            $map['change_time'][]=array('egt',strtotime("$day 00:00:00"));
            $map['change_time'][]=array('elt',strtotime("$day 23:59:59"));
            $num=M('ShopbalanceLog')->where($map)->sum('change_num');
            if(!$num){
                $num=0;
            }
            $income[]=$num;
            $rows[]=array('日期'=>date("m/d",strtotime("$day_day day")),'营业额'=>$num);

        }
        $return['income']=$income;
        $return['day_array']=$day_array;
        rsort($income);
        $return['min']=$income[6];
        $return['max']=$this->getmax($income[0]);//坐标最大值
        $return['step']=$return['max']/5;
        $return['chartData']=array('columns'=>array('日期','营业额'),'rows'=>$rows);

        return $return;
    }
    private function getmax($max){
        $max_value = floor($max);
        $begin_val = substr($max_value,0,1);
        $max_length = strlen($max_value)-1;
        $begin_val = intval($begin_val)+1;
        $multi = "1";
        for($i=0;$i<$max_length;$i++)
        {
            $multi.="0";
        }
        $multi = intval($multi);
        $max_value = $begin_val*$multi;
        if($max_value<=10)$max_value = 10;
        if($max_value>10&&$max_value<=200)$max_value = 200;
        return $max_value;
    }

    public function payname($assist_code){
        $assist_code = strtoupper($assist_code);
        if(strpos($assist_code,'XIANJIN')!== false){
            $pay_name='现金';
        }elseif(strpos($assist_code,'ALIPAY')!== false){
            $pay_name='支付宝';
        }elseif(strpos($assist_code,'WEIXIN')!== false){
            $pay_name='微信';
        }elseif(strpos($assist_code,'CHINAUMS')!== false){
            $pay_name='银联';
        }elseif(strpos($assist_code,'DECCA')!== false){
            $pay_name='台卡';
        }elseif(strpos($assist_code,'CARD')!== false){
            $pay_name='刷卡';
        }elseif(strpos($assist_code,'HANCHAO')!== false){
            $pay_name='余额支付';
        }elseif(strpos($assist_code,'OTHER')!== false){
            $pay_name='其他';
        }
        return $pay_name;
    }
}
?>