<?php

namespace Bill\Lib;

use Dever;

class Cron
{
    # 校正金额和业绩
    public function jz_api()
    {
        $w['state'] = 1;
        $id = Dever::input('id');
        if ($id) {
            $w['id'] = $id;
        }
        $member = Dever::db('agent/member')->select($w);
        if ($member) {
            foreach ($member as $v) {
                $where = array();
                $where['mid'] = $v['id'];

                /*
                $data = Dever::db('bill/cash')->getCash($where);
                if ($data && $data['total']) {
                    Dever::db('agent/member')->update(array('where_id' => $v['id'], 'cash' => $data['total']));
                }
                */

                $update = array();
                $where['type'] = 1;
                $sell = Dever::db('bill/sell')->getTotal($where);
                if ($sell && $sell['total']) {
                    $update['sell'] = $sell['total'];
                }
                $where['type'] = 2;
                $group_sell = Dever::db('bill/sell')->getTotal($where);
                if ($group_sell && $group_sell['total']) {
                    $update['group_sell'] = $group_sell['total'];
                }
                if ($update) {
                    $update['where_id'] = $v['id'];
                    Dever::db('agent/member')->update($update);
                }
            }
        }

        return 'ok';
        
    }

    public function sell_api()
    {
    	# 按月份统计团队业绩和直推业绩
        $month = Dever::input('month', date('Y-m'));
        $start_time = $month .'-01 00:00:00';
		$end_time = date('Y-m-d', strtotime($start_time. ' +1 month -1 day')).' 23:59:59';
		// $where['type'] = '2';
        $where['start'] = Dever::maketime($start_time);
        $where['end'] = Dever::maketime($end_time);
        $data = Dever::db('bill/sell')->getNumByTime($where);
        if ($data) {
        	foreach ($data as $k => $v) {

                if ($v['num'] > 0) {
                    $w = array();
                    $w['mid'] = $v['mid'];
                    $w['month'] = $where['start'];
                    $w['clear'] = true;
                    $info = Dever::db('bill/sell_stat')->find($w);
                    
                    $where['mid'] = $v['mid'];
                    $where['type'] = $v['type'];
                    $total = Dever::db('bill/sell')->getNumByTimeOne($where);

                    if ($v['type'] == 1) {
                        $w['new_ztnum'] = $v['num'];
                        if ($total) {
                            $w['zt_num'] = $total['num'];
                        }
                    } else {
                        $w['new_num'] = $v['num'];
                        if ($total) {
                            $w['num'] = $total['num'];
                        }
                    }
                    
                    if ($info) {
                        $w['where_id'] = $info['id'];
                        Dever::db('bill/sell_stat')->update($w);
                    } else {
                        Dever::db('bill/sell_stat')->insert($w);
                    }
                }
        	}
        }

        return 'ok';
    }

    # 权益使用情况统计：类型、商品名、总发放数量、总使用数量
    public function goods_api()
    {
        $num = Dever::input('num', 5);
        $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
        $end = Dever::input('end', date('Y-m-d'));

        $start = Dever::maketime($start . ' 00:00:00');
        $end = Dever::maketime($end . ' 23:59:59');
        $day = intval(($end - $start)/86400);

        for($i=0; $i<=$day; $i++) {
            $where['start'] = $start + 86400*$i;
            $where['end'] = $start + 86400*$i + 86399;
            $goods = Dever::db('agent/member_goods')->findAll($where);
            $dh_order = Dever::db('agent/dh_order')->getDhNum(array('status'=>3,'start'=>$where['start'],'end'=>$where['end'],'state'=>1));
            // print_R($dh_order);die;
            if ($goods) {
                foreach ($goods as $k => $v) {
                    $data = array();
                    $data['day'] = $where['start'];
                    $data['type'] = $v['type'];
                    $data['type_id'] = $v['type_id'];
                    $data['sku_id'] = $v['sku_id'];
                    $info = Dever::db('bill/goods_stat')->find($data);

                    $data['total_num'] = $v['total_num'];
                    $data['sell_num'] = $v['sell_num'];
                    $data['cash'] = $v['cash'];
                    $data['y_cash'] = $v['y_cash'];
                    $data['w_cash'] = $v['cash'] - $v['y_cash'];
                    $data['name'] = $v['name'];
                    $data['sell_nonum'] = $dh_order;

                    if ($info) {
                        $data['where_id'] = $info['id'];
                        Dever::db('bill/goods_stat')->update($data);
                    } else {
                        Dever::db('bill/goods_stat')->insert($data);
                    }
                }
            }
        }

        return 'ok';
    }

    # 区域分润
    /*
    区域毛利  =  区域内所有门店的采购商品的零售价总额  x  30%

    城市分润  =  区域毛利 x 1%

    区县分润  =  区域毛利 x 3%

    街道分润  =  区域毛利 x 5%
    */
    public function area_api()
    {
        # 获取所有代理商
        $where['status'] = 2;
        $member = Dever::db('agent/member')->select($where);
        if ($member) {
            foreach ($member as $k => $v) {
                # 获取区域内门店的利润
                $role = Dever::db('setting/role')->find($v['role']);
                $profit = $role['profit'];
                if (!$profit) {
                    continue;
                }
                $temp = explode(',', $v['area']);
                if ($role['type'] == 2) {
                    $area = '';
                    if ($role['area'] == 1 && isset($temp[1])) {
                        # 城市
                        $area = $temp[0] . ',' . $temp[1];
                    } elseif ($role['area'] == 2 && isset($temp[2])) {
                        # 区县
                        $area = $temp[0] . ',' . $temp[1] . ',' . $temp[2];
                    } elseif ($role['area'] == 3 && isset($temp[3])) {
                        # 街道
                        $area = $temp[0] . ',' . $temp[1] . ',' . $temp[2] . ',' . $temp[3];
                    }
                    if ($area) {
                        
                    }
                }
            }
        }
    }

    # 营业额报表：日期，订单数，确认订单数，确认订单额度，新增代理商数量
    public function order_api()
    {
        $num = Dever::input('num', 5);
        $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
        $end = Dever::input('end', date('Y-m-d'));

        $start = Dever::maketime($start . ' 00:00:00');
        $end = Dever::maketime($end . ' 23:59:59');
        $day = intval(($end - $start)/86400);

        for($i=0; $i<=$day; $i++) {
            $where = array();
            $where['fstart'] = $start + 86400*$i;
            $where['fend'] = $start + 86400*$i + 86399;
            $data = array();
            $data['day'] = $where['fstart'];
            $info = Dever::db('bill/order_stat')->find($data);
            $data['num'] = Dever::db('agent/order')->getNum($where);
            $where['status'] = 4;
            $data['yes_num'] = Dever::db('agent/order')->getNum($where);

            $cash = Dever::db('agent/order')->getCash($where);
            if ($cash) {
                $data['cash'] = $cash['total'];
            } else {
                $data['cash'] = 0;
            }
            $agent_cash = Dever::db('agent/order')->getDailiCash($where);
            if ($agent_cash) {
                $data['agent_cash'] = $agent_cash['total'];
            } else {
                $data['agent_cash'] = 0;
            }
            $where['status'] = 2;
            $data['daili_num'] = Dever::db('agent/member')->getNum($where);
            $where['status'] = '-2,1,2,3,6';//驳回不加入
            $where['start'] = $where['fstart'];
            $where['end'] = $where['fend'];
            unset($where['fstart']);
            unset($where['fend']);

            $data['no_num'] = Dever::db('agent/order')->getNum($where);
            $no_cash = Dever::db('agent/order')->getCash($where);
            if ($no_cash) {
                $data['no_cash'] = $no_cash['total'];
            } else {
                $data['no_cash'] = 0;
            }
            if (!$info) {
                Dever::db('bill/order_stat')->insert($data);
            } else {
                $data['where_id'] = $info['id'];
                Dever::db('bill/order_stat')->update($data);
            }
        }
    }

    # 代理商统计：日期，新增代理商，城市代理商，区县代理商，街道代理商，创客，消客
    public function member_api()
    {
        $num = Dever::input('num', 5);
        $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
        $end = Dever::input('end', date('Y-m-d'));

        $start = Dever::maketime($start . ' 00:00:00');
        $end = Dever::maketime($end . ' 23:59:59');
        $day = intval(($end - $start)/86400);
        
        for($i=0; $i<=$day; $i++) {
            $where = array();
            $where['status'] = 2;
            $where['fstart'] = $start + 86400*$i;
            $where['fend'] = $start + 86400*$i + 86399;
            $data = array();
            $data['day'] = $where['fstart'];
            $info = Dever::db('bill/member_stat')->find($data);
            $data['num'] = Dever::db('agent/member')->getNum($where);
            $where['role'] = 2;
            $data['city_num'] = Dever::db('agent/member')->getNum($where);

            $where['role'] = 3;
            $data['county_num'] = Dever::db('agent/member')->getNum($where);

            $where['role'] = 4;
            $data['town_num'] = Dever::db('agent/member')->getNum($where);

            $where['role'] = 5;
            $data['cf_num'] = Dever::db('agent/member')->getNum($where);

            $where['role'] = 6;
            $data['ck_num'] = Dever::db('agent/member')->getNum($where);

            $where['role'] = 8;
            $data['cv_num'] = Dever::db('agent/member')->getNum($where);


            if (!$info) {
                Dever::db('bill/member_stat')->insert($data);
            } else {
                $data['where_id'] = $info['id'];
                Dever::db('bill/member_stat')->update($data);
            }
        }
    }

    # 资金账户统计：日期，总收入，代理费，软件服务费，赠送商品价值，直推佣金，市场培训费
    # 总收入金额 = 代理费金额 + 软件服务费 - 赠送商品价值 - 直推分佣 - 市场培训费 - 活动分佣 - 手动发放
    public function cash_in_api()
    {
        $num = Dever::input('num', 5);
        $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
        $end = Dever::input('end', date('Y-m-d'));

        $start = Dever::maketime($start . ' 00:00:00');
        $end = Dever::maketime($end . ' 23:59:59');
        $day = intval(($end - $start)/86400);
        
        for($i=0; $i<=$day; $i++) {
            $where = array();
            $where['fstart'] = $start + 86400*$i;
            $where['fend'] = $start + 86400*$i + 86399;
            $data = array();
            $data['day'] = $where['fstart'];
            $info = Dever::db('bill/cash_in_stat')->find($data);

            $where['status'] = 4;
            $data['daili_cash'] = 0;
            $cash = Dever::db('agent/order')->getDailiCash($where);
            if ($cash) {
                $data['daili_cash'] = $cash['total'];
            }
            $data['soft_cash'] = 0;
            $cash = Dever::db('agent/order')->getSoftCash($where);
            if ($cash) {
                $data['soft_cash'] = $cash['total'];
            }
            $data['goods_cash'] = 0;
            $cash = Dever::db('agent/order')->getGoodsCash($where);
            if ($cash) {
                $data['goods_cash'] = $cash['total'];
            }
            unset($where['status']);
            $where['start'] = $where['fstart'];
            $where['end'] = $where['fend'];

            $where['type'] = 1;

            $data['zt_cash'] = 0;
            $cash = Dever::db('bill/cash')->getCash($where);
            if ($cash) {
                $data['zt_cash'] = $cash['total'];
            }

            $where['type'] = 2;

            $data['sc_cash'] = 0;
            $cash = Dever::db('bill/cash')->getCash($where);
            if ($cash) {
                $data['sc_cash'] = $cash['total'];
            }

            $where['type'] = 4;

            $data['hd_cash'] = 0;
            $cash = Dever::db('bill/cash')->getCash($where);
            if ($cash) {
                $data['hd_cash'] = $cash['total'];
            }

            $where['type'] = 10;

            $data['sd_cash'] = 0;
            $cash = Dever::db('bill/cash')->getCash($where);
            if ($cash) {
                $data['sd_cash'] = $cash['total'];
            }
            #间推
            $where['type'] =3;
            $data['jt_cash'] =0;
            $cash = Dever::db('bill/cash')->getCash($where);
            if ($cash) {
                $data['jt_cash'] = $cash['total'];
            }

            $data['cash'] = $data['daili_cash'] + $data['soft_cash'] - $data['goods_cash'] - $data['sc_cash'] - $data['zt_cash'] - $data['hd_cash'] - $data['sd_cash'] - $data['jt_cash'];

            if (!$info) {
                Dever::db('bill/cash_in_stat')->insert($data);
            } else {
                $data['where_id'] = $info['id'];
                Dever::db('bill/cash_in_stat')->update($data);
            }
        }
        return 'ok';
    }

    # 应付金额 = 现存余额 + 未审核提现 + 已审核待发放提现。
    /*public function cash_out_api()
    {
        $num = Dever::input('num', 1);
        $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
        $end = Dever::input('end', date('Y-m-d'));

        $start = Dever::maketime($start . ' 00:00:00');
        $end = Dever::maketime($end . ' 23:59:59');
        $day = intval(($end - $start)/86400);
        
        for($i=0; $i<=$day; $i++) {
            $where = array();
            $where['start'] = $start + 86400*$i;
            $where['end'] = $start + 86400*$i + 86399;
            $data = array();
            $data['day'] = $where['start'];
            $info = Dever::db('bill/cash_in_stat')->find($data);

            $where['status'] = 4;
            $data['daili_cash'] = 0;
            $cash = Dever::db('agent/order')->getDailiCash($where);
            if ($cash) {
                $data['daili_cash'] = $cash['total'];
            }
            $data['soft_cash'] = 0;
            $cash = Dever::db('agent/order')->getSoftCash($where);
            if ($cash) {
                $data['soft_cash'] = $cash['total'];
            }
            $data['goods_cash'] = 0;
            $cash = Dever::db('agent/order')->getGoodsCash($where);
            if ($cash) {
                $data['goods_cash'] = $cash['total'];
            }
            unset($where['status']);

            $where['type'] = 1;

            $data['zt_cash'] = 0;
            $cash = Dever::db('bill/cash')->getCash($where);
            if ($cash) {
                $data['zt_cash'] = $cash['total'];
            }

            $where['type'] = 2;

            $data['sc_cash'] = 0;
            $cash = Dever::db('bill/cash')->getCash($where);
            if ($cash) {
                $data['sc_cash'] = $cash['total'];
            }

            $data['cash'] = $data['daili_cash'] + $data['soft_cash'] - $data['goods_cash'] - $data['sc_cash'] - $data['zt_cash'];

            if (!$info) {
                Dever::db('bill/cash_in_stat')->insert($data);
            } else {
                $data['where_id'] = $info['id'];
                Dever::db('bill/cash_in_stat')->update($data);
            }
        }
    }
    */
    #财务应付报表：
    #应付金额 = 现存余额 + 未审核提现 + 已审核待发放提现
     public function cash_up_api()
    {
        $num = Dever::input('num', 5);
        $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
        $end = Dever::input('end', date('Y-m-d'));

        $start = Dever::maketime($start . ' 00:00:00');
        $end = Dever::maketime($end . ' 23:59:59');
        $day = intval(($end - $start)/86400);
        
        for($i=0; $i<=$day; $i++) {
            $where = array();
            $where['start'] = $start + 86400*$i;
            $where['end'] = $start + 86400*$i + 86399;
            $data = array();
            $data['day'] = $where['start'];
            $info = Dever::db('bill/cash_up_stat')->find($data);
            /*
             现存余额=tixian.php下的cash
             未审核提现=cash下的status=1，type=11
             已审核待发放提现=tixian.php下的status=1,cash
             */
            #现存余额
            $data['xc_cash']=0;
            $where['status'] = 2;
            $cash=Dever::db('bill/tixian')->gettxCash($where);
            if($cash){
                $data['xc_cash']=$cash['total']*(-1);
            }
            #未审核提现
            $where['status']=-1;
            $data['ws_cash']=0;
            // print_R($where);die;
            $tixian=Dever::db('bill/tixian')->getwsCash($where);
            if($tixian){
                // $where['status']=1;
                // $where['type']=11;
                // $where['mid']=$tixian['mid'];
                // $cash=Dever::db('bill/cash')->getwsCash($where);
                $data['ws_cash']=$tixian['total']*(-1);
            }
            #已审核待发放提现
            $data['df_cash']=0;
            $where['status']=1;
            $where['state'] = 1;
            $cash=Dever::db('bill/tixian')->getdfCash($where);
            if($cash){
                $data['df_cash'] = $cash['total'] *(-1);
            }
            $data['cash'] = $data['xc_cash'] + $data['ws_cash'] + $data['df_cash'];

            if (!$info) {
                Dever::db('bill/cash_up_stat')->insert($data);
            } else {
                $data['where_id'] = $info['id'];
                Dever::db('bill/cash_up_stat')->update($data);
            }
        }
    }
    public function getShopInfo ($data, $name, $param)
    {
        $manage = array();
        $month = Dever::param('month', $param);
        if ($data) {
            $delete = array('一键生成归店信息');
            foreach ($data as $k => $v) {
                if ($v['cash_status'] == 1) {
                    # 已发放
                    $delete[] = '对账确认';
                    $delete[] = '确认发放';
                    $delete[] = '重新生成归店信息';
                    break;
                } else {
                    if ($v['status'] == 1) {
                        # 已确认
                        $delete[] = '对账确认';
                        $delete[] = '重新生成归店信息';
                    } else {
                        $delete[] = '确认发放';
                    }
                }
            }
            $log = Dever::db('bill/shop_stat_log')->find(array('month' => $month));
            if ($log) {
                $audit = array();
                if ($log['create_admin']) {
                    $admin = Dever::db('manage/admin')->find($log['create_admin']);
                    if ($admin) {
                        $date = date('Y-m-d H:i', $log['create_date']);
                        $audit[] = '创建人：' . $admin['username'] . ' 创建时间：' . $date;
                    }
                }
                if ($log['confirm_admin']) {
                    $admin = Dever::db('manage/admin')->find($log['confirm_admin']);
                    if ($admin) {
                        $date = date('Y-m-d H:i', $log['confirm_date']);
                        $audit[] = '对账人：' . $admin['username'] . ' 对账时间：' . $date;
                    }
                }
                if ($log['send_admin']) {
                    $admin = Dever::db('manage/admin')->find($log['send_admin']);
                    if ($admin) {
                        $date = date('Y-m-d H:i', $log['send_date']);
                        $audit[] = '发放人：' . $admin['username'] . ' 发放时间：' . $date;
                    }
                }
                if ($audit) {
                    $audit = '<br />' . implode('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $audit);
                } else {
                    $audit = '';
                }
                
                $manage['info'] = '代理商总数=创V数+代理商数量，创V数包含名额外创V数，创V数减去名额外创V就是名额内创V数，代理商数量包含创客数'. $audit;

            }
            
            $manage['deleteButton'] = $delete;
        } else {
            Dever::setInput('day', Dever::input('search_option_month'));
            list($start, $end) = Dever::month();
           
            $manage['deleteButton'] = array('重新生成归店信息', '对账确认', '确认发放');
        }
        Dever::config('base')->manage = $manage;
    }
    #归店统计
    public function shop_stat_api(){
        // $month = Dever::input('month');

        // $y = date('Y');
        // if($month){
        //     $month = $month;
        // }else{
        //     $m = date('m')-1;
        //     if ($m == 0) {
        //         $y = $y - 1;
        //         $m = 12;
        //     }
        //     $month = $y.'-'.$m;
        // }
         # 传入day，获取月开始和月结束日期
        list($where['start'], $where['end']) = Dever::month();
        $month = date('Y-m', $where['start']);
        // print_R($month);die;
        $start_time = $month .'-01 00:00:00';
        $end_time = date('Y-m-d', strtotime($start_time. ' +1 month -1 day')).' 23:59:59';
        $start = Dever::maketime($start_time);
        $end = Dever::maketime($end_time);
        $shops = Dever::db('shop/info')->getguiAll(array('type'=>2));
        
        foreach ($shops as $k => $v) {
            #业绩补贴：除了创V0.02，创V>=10个0.05,创V>=20个0.08
            // $subsidy = 0;
            $w = array('end' => $end,'new_shop_id'=> $v['id']);
            $member = Dever::db('agent/member_shop_log')->getAllByShop($w);
            if ($member) {
                $member_mid = implode(',', array_keys($member));

                $w['start'] = $start;
                $cmember = Dever::db('agent/member_shop_log')->getAllByShop($w);

                $insert = array();
                $insert['shop_id'] = $v['id'];
                $insert['month'] = $start;
                $insert['c_money'] = 0;
                $insert['mids'] = $member_mid;
                $insert['month_mids'] = array();
                $insert['num'] = 0;
                $insert['dl_num'] = 0;
                $insert['v_num'] = 0;
                $insert['ck_num'] = 0;
                $insert['out_num'] = 0;
                $insert['out_money'] = 0;
                $insert['nei_money'] = 0;

                $insert['subsidy'] = 0;
                $insert['dl_subsidy'] = 0;
                $insert['v_subsidy'] = 0;

                $out_member = array();
                $nei_member = array();
                if ($cmember) {
                    foreach ($cmember as $k1 => $v1) {
                        $vinfo = Dever::db('agent/member')->find($v1['mid']);
                        if ($vinfo) {
                            $insert['month_mids'][] = $v1['mid'];
                            $insert['num'] += 1;
                            if ($vinfo['role'] == 8) {
                                $insert['v_num'] += 1;
                                if ($vinfo['prize_type'] == 1) {
                                    $insert['out_num'] += 1;
                                }
                            } else {
                                $insert['dl_num'] += 1;
                            }
                            if ($vinfo['role'] == 6) {
                                $insert['ck_num'] += 1;
                            }
                        }
                    }
                    $insert['month_mids'] = implode(',', $insert['month_mids']);
                }

                foreach ($member as $k1 => $v1) {
                    $vinfo = Dever::db('agent/order')->select(array('parent_mid' => $v1['mid'], 'role' => 8));
                    if ($vinfo) {
                        foreach ($vinfo as $v2) {
                            if ($v2['prize_type'] == 1) {
                                $out_member[] = $v2['mid'];
                            } else {
                                $nei_member[] = $v2['mid'];
                            }
                        }
                    }
                }

                $c_money = Dever::db('shop/buy_order')->getCashNum(array('type'=>1,'type_id'=>$v['id'],'start' => $start, 'end' => $end, 'status' => '3,4,5,6'));
                if ($c_money) {
                    $insert['c_money'] = $c_money['total'];
                }

                $where = array
                (
                    'parent_mid' => $member_mid,
                    'start' => $start,
                    'end' => $end
                );
                

                $money = Dever::db('agent/order')->getCount($where);
                if ($money) {
                    $insert['money'] = $money['total'];
                }
                $where['role'] = '1,2,3,4,5,6';
                // $where['role'] = '1,2,3,4,5,6,7';
                $dl_money = Dever::db('agent/order')->getCount($where);
                if ($dl_money) {
                    $insert['dl_money'] = $dl_money['total'];
                } else {
                    $insert['dl_money'] = 0;
                }
                #除去创V的补贴
                if ($insert['dl_money']) {
                    $insert['dl_subsidy'] = $insert['dl_money'] * 0.02;
                }
                $where['role'] = 8;
                unset($where['parent_mid']);
                if ($out_member) {
                    $where['mid'] = $out_member;
                    $out_money = Dever::db('agent/order')->getCount($where);
                    if ($out_money) {
                        $insert['out_money'] = $out_money['total'];
                    }
                }
                if ($insert['out_money']) {
                    if ($insert['out_money'] >= 100000 && $insert['out_money'] <200000) {
                        $insert['v_subsidy'] = $insert['out_money'] * 0.05;
                    } elseif ($insert['out_money'] >=200000) {
                        $insert['v_subsidy'] = $insert['out_money'] * 0.08;
                    }
                }
                if ($nei_member) {
                    $where['mid'] = $nei_member;
                    $nei_money = Dever::db('agent/order')->getCount($where);
                    if ($nei_money) {
                        $insert['nei_money'] = $nei_money['total'];
                    }
                }
                // print_R($insert);die;

                $res = Dever::db('bill/shop_stat')->find(array('month'=>$insert['month'],'shop_id'=>$v['id']));
                if ($res) {
                    $insert['where_id'] = $res['id'];
                    Dever::db('bill/shop_stat')->update($insert);
                } else {
                    Dever::db('bill/shop_stat')->insert($insert);
                }
            } else {
                $insert = array();
                $insert['shop_id'] = $v['id'];
                $insert['month'] = $start;
                $insert['c_money'] = 0;
                $insert['mids'] = '';
                $insert['month_mids'] = array();
                $insert['num'] = 0;
                $insert['dl_num'] = 0;
                $insert['v_num'] = 0;
                $insert['ck_num'] = 0;
                $insert['out_num'] = 0;
                $insert['out_money'] = 0;
                $insert['nei_money'] = 0;
                $insert['subsidy'] = 0;
                $insert['dl_subsidy'] = 0;
                $insert['v_subsidy'] = 0;
                $res = Dever::db('bill/shop_stat')->find(array('month'=>$insert['month'],'shop_id'=>$v['id']));
                if ($res) {
                    $insert['where_id'] = $res['id'];
                    Dever::db('bill/shop_stat')->update($insert);
                } else {
                    Dever::db('bill/shop_stat')->insert($insert);
                }
            }
            
        }
        $this->addAreaLog('create', $month);
        return 'reload';
    }
    private function addAreaLog($col, $month)
    {
        $admin = Dever::load('manage/auth.info');
        $update['month'] = $month;
        $log = Dever::db('bill/shop_stat_log')->find(array('month' => $month));
        $update[$col . '_date'] = time();
        $update[$col . '_admin'] = $admin['id'];
        
        if ($log) {
            $update['where_id'] = $log['id'];
            Dever::db('bill/shop_stat_log')->update($update);
        } else {
            Dever::db('bill/shop_stat_log')->insert($update);
        }
    }
    # 确认对账
    public function shopYes_commit_api()
    {
        $month = Dever::input('month');
        if ($month) {
            $month = Dever::maketime($month);
            $state = Dever::db('bill/shop_stat')->updates(array('option_month' => $month, 'option_status' => 2, 'set_status' => 1));
            if ($state) {
                $this->addAreaLog('confirm', $month);
            }
        }
        return 'reload';
    }

    # 确认发放
    public function areaCash_commit_api()
    {
        $shops = Dever::db('shop/info')->getguiAll(array('type'=>2));
        if ($shops) {
            $shop_data = array();
            foreach ($shops as $k => $v) {
                if ($v['agent_mid'] <1) {
                    $shop_data[$v['id']] = $v['name'];
                }
            }
            if ($shop_data) {
                Dever::alert('以下门店没有绑定代理商：' . implode(',', $shop_data));
            }
        }
        $month = Dever::input('month');
        if ($month) {
            $month = Dever::maketime($month);
            $data = Dever::db('bill/shop_stat')->select(array('month' => $month, 'cash_status' => 2));
            if ($data) {
                $date = date('Y年m月', $month);
                foreach ($data as $k => $v) {
                    if ($v['shop_id']) {
                        $shop = Dever::db('shop/info')->find($v['shop_id']);
                        if (isset($shop) && $shop && isset($shop['agent_mid']) && $shop['agent_mid']) {
                            $member = Dever::db('agent/member')->find($shop['agent_mid']);
                            $cash = $v['dl_subsidy'] + $v['v_subsidy'];
                            if ($member && $member['status'] == 2) {
                                if ($v['dl_subsidy'] > 0) {
                                    $desc = $shop['name'].$date.'区域招商补贴';
                                    $state = Dever::load('bill/lib/cash')->up($member['id'], 13, $v['dl_subsidy'], $member['role'], $v['id'], $desc, 2, true);
                                } 
                                if ($v['v_subsidy'] >0) {
                                    $desc = $shop['name'].$date.'名额外创V补贴';
                                    $state = Dever::load('bill/lib/cash')->up($member['id'], 14, $v['v_subsidy'], $member['role'], $v['id'], $desc, 2, true);
                                }
                                if (isset($state) && $state) {
                                    // $where = array();
                                    // $where['where_id'] = $shop['id'];
                                    // $where['price'] = $shop['price'] + $cash;
                                    // $where['clear'] = true;
                                    // Dever::db('shop/info')->upCash($where);
                                    Dever::db('bill/shop_stat')->update(array('where_id' => $v['id'], 'cash_status' => 1));
                                }
                            }
                        }

                    }
                }
                $this->addAreaLog('send', $month);
            }
        }
        return 'reload';
    }
    # 角色统计
    public function role_api()
    {
        $num = Dever::input('num', 5);
        $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
        $end = Dever::input('end', date('Y-m-d'));

        $start = Dever::maketime($start . ' 00:00:00');
        $end = Dever::maketime($end . ' 23:59:59');
        $day = intval(($end - $start)/86400);
        $status = 4;

        $role = Dever::load('setting/role-state');

        for($i=0; $i<=$day; $i++) {
            $where = array();
            $where['fstart'] = $start + 86400*$i;
            $where['fend'] = $start + 86400*$i + 86399;
            $where['status'] = 4;

            foreach ($role as $k => $v) {
                $data = array();
                $data['day'] = $where['fstart'];
                $data['role'] = $v['id'];
                $data['clear'] = true;
                $where['role'] = $v['id'];
                $info = Dever::db('bill/role_stat')->find($data);
                $data['num'] = Dever::db('agent/order')->getNum($where);
                if ($data['num'] && $data['num'] > 0) {
                    $cash = Dever::db('agent/order')->getCash($where);
                    if ($cash) {
                        $data['cash'] = $cash['total'];
                    } else {
                        $data['cash'] = 0;
                    }
                    $data['touru'] = $data['cash'];
                    $cash = Dever::db('agent/member')->getCash($where);
                    if ($cash) {
                        $data['shouru'] = $cash['total'];
                    } else {
                        $data['shouru'] = 0;
                    }
                    if (!$info) {
                        Dever::db('bill/role_stat')->insert($data);
                    } else {
                        $data['where_id'] = $info['id'];
                        Dever::db('bill/role_stat')->update($data);
                    }
                }
            }
        }

        return 'ok';
    }
}