r_detail.js 9.88 KB
var util = require('../../utils/util.js')
var uri=require('../../utils/uri.js')
var bookOrder=require('../../utils/bookCommon.js')
Page({
    data:{
        contactlist:[
            {
                type:'custom',
                title:'顾客构成',
                isactive:true
            },
            {
                type:'order',
                title:'订单构成',
                isactive:false
            },
            {
                type:'price',
                title:'收入构成',
                isactive:false
            },
            {
                type:'discount',
                title:'优惠构成',
                isactive:false
            }
        ],
        detaildim:'day',
        type:'custom',
        isloading:false

    },
    onLoad:function(opt){
        console.log(opt)
        var time;
        if(opt.selectdim=='days'){
            time=opt.dateall.split('-')[0]+opt.dateall.split('-')[1]+opt.dateall.split('-')[2]
        }else{
            if(opt.selectdim=='week'){
                var arr=opt.date.split('-');
                var begin=arr[0].split('.');
                var end=arr[1].split('.');
                time=''
                for(var i=0;i<begin.length;i++){
                    time+=begin[i]
                }
                time+='-';
                for(var i=0;i<end.length;i++){
                    time+=end[i]
                }
            }else{
                console.log(opt)
                arr=opt.dateall.split('.');
                time=''+arr[0]+arr[1]
            }
            console.log(time)
        }
        this.setData({
            opt:opt,
            time:time,
            sellerid:util.cache('sellerid'),
            selectdim:opt.selectdim,
            detaildim:opt.selectdim=='days' ? 'day' : opt.selectdim
        })
        console.log(opt)
        if(this.data.detaildim=='day'){
            util.setBarTitle('日报表')
        }else{
            if(this.data.detaildim=='week'){
                util.setBarTitle('周报表')
            }else{
                util.setBarTitle('月报表')
            }
        }

    },
    onShow:function(){
        if(util.cache('date')){
            if(this.data.detaildim=='day'){
                var thisyear=util.cache('date').split('-')[0];
                var thismonth=util.cache('date').split('-')[1][0]=='0' ? util.cache('date').split('-')[1][1] : util.cache('date').split('-')[1];
                var thisday=util.cache('date').split('-')[2][0]=='0' ? util.cache('date').split('-')[2][1] : util.cache('date').split('-')[2]
                this.setData({
                    time:thisyear+(thismonth.length==1 ? '0'+thismonth : thismonth)+(thisday.length==1 ? '0'+thisday : thisday),
                })
            }else{
                // if(this.data.detaildim=='week'){
                //     this.data.opt.date=util.cache('date')
                //     this.setData({
                //         time:util.cache('time'),
                //         opt:this.data.opt
                //     })
                // }
                this.data.opt.date=util.cache('date')
                this.setData({
                    time:util.cache('time'),
                    opt:this.data.opt
                })
            }

        }
        this.setData({
            isloading:false,
        })
        this.getreportdetail();
        var that=this;
        util.postSuccessFun=function(json,uri_code) {
            switch (uri_code) {
                case uri.CGI_USER_GETOPERDATEBYDAY__CODE:
                    console.log(json)
                    if(json.ret==200){
                        var list=json.data;
                        var reportlist=[];
                        var contactinfo='';
                        for(var key in list){
                            console.log(key)
                            var date,dd
                            if(that.data.selectdim=='days'){
                                date=bookOrder.getdate_num(key.split('-')[1]+'-'+key.split('-')[2]);
                                var thisdate=key+' 12:00:00';
                                var timestamp=bookOrder.gettimestamp(thisdate);
                                dd=bookOrder.changedate(timestamp*1000);
                            }
                            if(that.data.selectdim=='week'){
                                date=key;
                            }
                            if(that.data.selectdim=='month'){
                                var arr=key.split('.');
                                date=arr[0]+'年'+arr[1]+'月';
                            }
                            if(list[key]!=''){
                                var obj={
                                    date:date,
                                    dateall:key,
                                    totaldate:that.data.selectdim=='days' ? dd.year+'年'+date : that.data.opt.date,
                                    total_order:list[key].total_order,
                                    unit_price:Number(list[key].unit_price).toFixed(2),
                                    total_pnum:list[key].total_pnum ? list[key].total_pnum : null,
                                    total_price:list[key].total_price,

                            }
                            console.log(key)
                            contactinfo=list[key].contactinfo
                                    //x:新顾客  l:老顾客   w:外卖就餐   t:堂食就餐  tp:堂食总收入   wp:外卖总收入   mdp:免单  gzp:挂账  zdzkp:整单折扣  hyp:会员   zdyp:自定义
                                reportlist.push(obj)
                            }else{
                                reportlist.push({
                                    date:date,
                                    dateall:key,
                                    totaldate:dd.year+'年'+date
                                })
                            }

                        }
                        console.log(reportlist[0])
                        that.setData({
                            opt:reportlist[0],
                            isloading:true,
                            contactinfo:contactinfo
                        })
                        that.getformlist();

                    }
                    break;
            }
        }

    },
    selecttime:function(){
        console.log(this.data.opt)
        wx.navigateTo({
            url:'r_time?date='+this.data.opt.dateall+'&selectdim='+this.data.selectdim
        })
    },
    getreportdetail:function () {
        util.post(uri.CGI_USER_GETOPERDATEBYDAY,{sid:this.data.sellerid,dim:this.data.detaildim,time:this.data.time},uri.CGI_USER_GETOPERDATEBYDAY__CODE)
    },
    selectform:function(e){
        var type=e.currentTarget.dataset.type;
        var contactlist=this.data.contactlist;
        contactlist.forEach(function(v,i){
            v.isactive=false
            if(v.type==type){
                v.isactive=true
            }
        })
        this.setData({
            contactlist:contactlist,
            type:type
        })
        this.getformlist();

    },
    getformlist:function () {
       var that=this;
       var type=this.data.type;
       var reportinfo=this.data.opt;
       var sum=0;
       var contactinfo=this.data.contactinfo;
       console.log(contactinfo)
       var ifcondition=typeof contactinfo==='object' && !isNaN(contactinfo.length)
       if(ifcondition==false && contactinfo!=''){
           for(var key in contactinfo){
               if(type+'form'==key){
                   var list=contactinfo[key];
                   if(reportinfo!='' && list){
                       list.forEach(function(v,i){
                           if(!v.count){
                               v.count=0
                           }
                           if(type=='custom'){
                               if(v.count && v.count!=0){
                                   v.per=bookOrder.isInteger(v.count/Number(that.data.opt.total_pnum)*100)==true ? (v.count/Number(that.data.opt.total_pnum)*100) : (v.count/Number(that.data.opt.total_pnum)*100).toFixed(1)
                               }
                           }
                           if(type=='order'){
                               if(v.count && v.count!=0){
                                   v.per=bookOrder.isInteger(v.count/Number(that.data.opt.total_order)*100)==true ? (v.count/Number(that.data.opt.total_order)*100) : (v.count/Number(that.data.opt.total_order)*100).toFixed(1)
                               }
                           }
                           if(type=='price'){
                               if(v.count && v.count!=0){
                                   v.per=bookOrder.isInteger(v.count/Number(that.data.opt.total_price)*100)==true ? (v.count/Number(that.data.opt.total_price)*100) : (v.count/Number(that.data.opt.total_price)*100).toFixed(1)
                               }
                           }
                       })
                       list.sort(function(a,b){
                            var max=Number(a.count);
                            var min=Number(b.count);
                            if(max>=min) return 1;
                            if(max<min)  return -1;

                       })
                   }
               }
           }
           if(type=='discount'){
               contactinfo[type+'form'].forEach(function (v,i) {
                   console.log(typeof v.count)
                   sum+=Number(v.count);
               })
               contactinfo[type+'form'].forEach(function (v,i) {
                   v.per=bookOrder.isInteger(v.count/sum*100)==true ? (v.count/sum*100) : (v.count/sum*100).toFixed(1)
               })
           }
           this.setData({
               infolist:contactinfo[type+'form'] ? contactinfo[type+'form'].reverse() : []
           })
       }else{
           this.setData({
               infolist:[]
           })
       }


    },
    netchange:function(){
        this.onShow();
    }

})