<?php

namespace Bill\Lib;

use Dever;

class Manage
{
    public function cashUpdate($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $mid = Dever::param('mid', $data);
        $cash = Dever::param('cash', $data);
        $desc = Dever::param('desc', $data);
        if ($mid && $cash && $desc) {
        	$member = Dever::db('agent/member')->find($mid);
        	if ($member && $member['status'] == 2) {
        		Dever::load('bill/lib/cash')->up($mid, 10, $cash, $member['role'], $id, $desc, 2);
        		$where['where_id'] = $mid;
        		$where['cash'] = $cash;
        		Dever::db('agent/member')->upCash($where);
        	} else {
        		Dever::db('bill/push_cash')->update(array('where_id' => $id, 'status' => 2));
        		Dever::alert('代理商未审核通过，发放失败');
        	}
        }
    }

    public function cashAuditUpdateStart($param, $name, $data)
    {
        $mul_type = Dever::config('base')->mul_type;
        if ($mul_type == 2) {
            $list = Dever::db('bill/cash')->select(array('status' => 1));
        } else {
            $list = explode(',', $param['where_id']);
        }
    }

    public function cashAuditUpdate($id, $name, $data)
    {
    	Dever::config('base')->hook = true;
        $audit = Dever::param('audit', $data);
        if ($audit > 1) {
            $mul_type = Dever::config('base')->mul_type;
            if ($mul_type == 2) {
                $list = Dever::db('bill/cash')->select(array('status' => 1));
            } else {
                $list = explode(',', $id);
            }
            
            foreach ($list as $k => $v) {
                if (is_array($v)) {
                    $id = $v['id'];
                    $info  = Dever::db('bill/cash')->find(array('id' => $id, 'clear' => true));
                } else {
                    $id = $v;
                    $info  = Dever::db('bill/cash')->find(array('id' => $id, 'clear' => true));
                }
                
                if ($info['status'] != 1) {
                    continue;
                }
                #已审核
                if ($audit == 2) {
                    Dever::db('bill/cash')->update(array('where_id' => $id, 'status' => 2, 'operdate' => time()));
                    $info = Dever::db('bill/cash')->find($id);
                    if ($info && $info['type'] <= 10) {
                        $where['where_id'] = $info['mid'];
                        $where['cash'] = $info['cash'];
                        Dever::db('agent/member')->upCash($where);
                    }elseif ($info && $info['type'] == 11) {
                        $tixian = Dever::db('bill/tixian')->find($info['type_id']);
                        $where['where_id'] = $tixian['id'];
                        $where['status'] = 1;
                        $admin = Dever::load('manage/auth.data');
                        $where['audit_admin'] = $admin['id'];
                        Dever::db('bill/tixian')->update($where);
                    }
                } else {
                    #作废
                    Dever::db('bill/cash')->update(array('where_id' => $id, 'status' => 3, 'operdate' => time()));
                    $info = Dever::db('bill/cash')->find($id);
                    if ($info && $info['type'] == 11) {
                        $tixian = Dever::db('bill/tixian')->find($info['type_id']);
                        $where['where_id'] = $info['mid'];
                        $where['cash'] = -1*$tixian['ycash'];
                        Dever::db('agent/member')->upCash($where);
                        $update['where_id'] = $tixian['id'];
                        $update['status'] = 3;
                        $admin = Dever::load('manage/auth.data');
                        $update['audit_admin'] = $admin['id'];
                        Dever::db('bill/tixian')->update($update);
                    }
                }
            }
        }
    }

    public function tixianAuditUpdate($id, $name, $data)
    {
    	Dever::config('base')->hook = true;
        $audit = Dever::param('audit', $data);
        $desc = Dever::param('audit_desc', $data);
        if ($audit > 1) {
            $mul_type = Dever::config('base')->mul_type;
            if ($mul_type == 2) {
                $list = Dever::db('bill/tixian')->select(array('status' => 1));
            } else {
                $list = explode(',', $id);
            }
            
            foreach ($list as $k => $v) {
                if (is_array($v)) {
                    $id = $v['id'];
                    $tixian  = Dever::db('bill/tixian')->find(array('id' => $id, 'clear' => true));
                } else {
                    $id = $v;
                    $tixian  = Dever::db('bill/tixian')->find(array('id' => $id, 'clear' => true));
                }
                if (!$tixian) {
                    continue;
                }
                
                if ($tixian['status'] != 1) {
                    continue;
                }

                if ($tixian['type'] == 1) {
                    $info = Dever::db('bill/cash')->find(array('type' => 11, 'type_id' => $id));
                    $table = 'bill/cash';
                } elseif ($tixian['type'] == 2) {
                    $info = Dever::db('shop/record')->find(array('type' => 2, 'type_id' => $id));
                    $table = 'shop/record';
                } elseif ($tixian['type'] == 3) {
                    $info = Dever::db('shop/user_money')->find(array('type' => 11, 'type_id' => $id));
                    $table = 'shop/user_money';
                }
                
                if (!$info) {
                    continue;
                }
                if ($audit == 2) {
                    $admin = Dever::load('manage/auth.data');
                    $where['audit_admin'] = $admin['id'];
                    Dever::db('bill/tixian')->update(array('where_id' => $id, 'status' => 2, 'audit_admin' => $where['audit_admin'],'operdate' => time()));
                    if ($info && $info['status'] == 1) {
                        Dever::db($table)->update(array('where_id' => $id, 'status' => 2, 'operdate' => time()));
                    }
                    # 通知短信
                    /*
                    if (Dever::project('sms')) {
                        $member = Dever::db('agent/member')->find($info['mid']);
                        Dever::load('sms/api')->send('daili_tixian_yes', $member['mobile'], array('name' => $member['name']));
                    }
                    */
                } else {
                    $admin = Dever::load('manage/auth.data');
                    $where['audit_admin'] = $admin['id'];
                    Dever::db('bill/tixian')->update(array('where_id' => $id, 'status' => 3, 'audit_admin' => $where['audit_admin'],'operdate' => time()));
                    $w = array();
                    
                    if ($tixian['type'] == 1) {
                        $w['where_id'] = $info['mid'];
                        $w['cash'] = -1*$tixian['ycash'];
                        Dever::db('agent/member')->upCash($w);
                    } elseif ($tixian['type'] == 2) {
                        $w['where_id'] = $info['shop_id'];
                        $w['price'] = -1*$tixian['ycash'];
                        Dever::db('shop/info')->upCash($w);
                        Dever::db($table)->update(array('where_id' => $info['id'], 'status' => 3, 'operdate' => time()));
                    } elseif ($tixian['type'] == 3) {
                        $w['where_id'] = $info['uid'];
                        $w['cash'] = -1*$tixian['ycash'];
                        Dever::db('passport/user')->upCash($w);
                    }

                    # 通知短信
                    /*
                    if (Dever::project('sms')) {
                        $member = Dever::db('agent/member')->find($info['mid']);
                        Dever::load('sms/api')->send('daili_tixian_no', $member['mobile'], array('name' => $member['name'], 'content' => $desc));
                    }
                    */
                }
            }
        }
    }

    public function sellUpdate($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $mid = Dever::param('mid', $data);
        $num = Dever::param('num', $data);
        $desc = Dever::param('desc', $data);
        $type = Dever::param('type', $data);
        $cdate = Dever::param('cdate',$data);
        if(!$cdate){
            $cdate = false;
        } else {
            $cdate = Dever::maketime($cdate);
        }
        if ($mid && $num && $desc) {
        	$member = Dever::db('agent/member')->find($mid);
        	if ($member && $member['status'] == 2) {
                if ($type == 1) {
                    Dever::load('bill/lib/sell.push_one_commit', $mid, $num, $member['role'], $id, $desc,$cdate);
                } else {
                    Dever::load('bill/lib/sell.push_all_commit', $mid, $num, $member['role'], $id, $desc,$cdate);
                }
        	} else {
        		Dever::db('bill/push_sell')->update(array('where_id' => $id, 'status' => 2));
        		Dever::alert('代理商未审核通过，发放失败');
        	}
        }
    }

    # 获取新增业绩
    public function getSellNewNum($num, $month, $mid)
    {
        $where = array('month' => $month, 'mid' => $mid);
        $search = Dever::search_button();
        if ($search) {
            $where['config']['group'] = $search[0];
            $where['config']['col'] = str_replace('|id', '', $search[1]);
        }
        if ($num < 0) {
            $num = 0;
        }
        $new_num = 0;
        $prev = Dever::db('bill/sell_stat')->prev($where);
        if ($prev && $num > 0 && $prev['num'] >= 0) {
            $new_num = $num - $prev['num'];
        }

        return $new_num;
    }

    #提现导出
    public function tixian_out($data){
        $header = array('提现单号', '姓名', '手机号', '身份证号', '会员角色', '提现金额', '到账金额', '手续费金额', '申请时间', '审核时间', '审核状态', '发放时间','发放状态','审核人', '来源');    
        $body = array();
        foreach($data as $k => $v){
            $member = Dever::db('agent/member')->find(array('id'=>$v['mid']));
            $role = Dever::db('setting/role')->find(array('id'=>$member['role']));
            
            if($v['status'] == -1){
                $status = '未审核待发放';
                $audit = '待审核';
            }elseif($v['status'] == 1){
                $status = '已审核待发放';
                $audit = '已审核';
            }elseif($v['status'] == 2){
                $status = '已发放';
                $audit = '已审核';
            }else{
                 $status = '已作废';
                 $audit = '已作废';
            }
            $cdate = date('Y-m-d H:i',$v['cdate']);
            if ($v['type'] == 1) {
                $cash = Dever::db('bill/cash')->find($v['cash_id']);
            } elseif ($v['type'] == 2) {
                $cash = Dever::db('shop/record')->find($v['cash_id']);
            } elseif ($v['type'] == 3) {
                $cash = Dever::db('shop/user_money')->find($v['cash_id']);
            }
            
            if ($cash['operdate']) {
                $adate = date('Y-m-d H:i',$cash['operdate']);
            } else {
                $adate = '-';
            }

            if ($v['operdate']) {
                $operdate = date('Y-m-d H:i',$v['operdate']);
            } else {
                $operdate = '-';
            }
            
            if($v['audit_admin']){
                $admin = Dever::load('manage/admin-find',$v['audit_admin']);
                $username = $admin['username'];
            }else{
                $username = '';
            }
            
            if($v['ycash'] < 0){
                $ycash = $v['ycash'] * (-1);
            }else{
                $ycash = $v['ycash'];
            }
            if($v['cash'] < 0){
                $cash = $v['cash'] * (-1);
            }else{
                $cash = $v['cash'];
            }
            $d = array
            (
                $v['order_num'],
                $member['name'],
                $member['mobile'],
                $member['idcard'],
                $role['name'],
                $ycash,
                $cash,
                $ycash-$cash,
                $cdate,
                $adate,
                $audit,
                $operdate,
                $status,
                $username,
                '',
            );
             $body[] = $d;
        }
        $file = Dever::input('excel_name');
        return Dever::excelExport($body, $header, $file);
    }

    #归店统计导出
    public function out_shop_stat($data){
        $header = array('月份', '门店名称','门店编号','所属代理商', '采购金额','归店业绩（除创V外）','创客数','创V数','名额外创V','名额外创V业绩');
        $body = array();
        foreach($data as $k => $v){
            $month = date('Y-m',$v['month'])."\t";
            $shop = Dever::db('shop/info')->find(array('id'=>$v['shop_id']));
            if($shop['mid'] && $shop['mid'] >0){
                $member = Dever::db('agent/member')->find($shop['mid']);
                if($member){
                    $dl_mobile = $member['mobile'];
                }else{
                    $dl_mobile = '-';
                }
            }else{
                $dl_mobile = '-';
            }
            $d = array
            (
                $month,
                $shop['name'],
                $shop['sid'],
                $dl_mobile,
                $v['c_money'],
                $v['dl_money'],
                $v['ck_num'],
                $v['v_num'],
                $v['out_num'],
                $v['out_money'],
            );
            $body[] = $d;
        }
        $file = Dever::input('excel_name');
        return Dever::excelExport($body, $header, $file);
    }
    #归店列表显示
    public function shop_stat_api($id){
        $info = Dever::db('bill/shop_stat')->find($id);
        $shop = Dever::db('shop/info')->find($info['shop_id']);
        $html = $shop['name'];
        $html .= '<br />' .$shop['sid'];
        if($shop['agent_mid'] && $shop['agent_mid']>0){
            $member = Dever::db('agent/member')->find($shop['agent_mid']);
            if($member){
                $html .= '<br />' .$member['name'].$member['mobile'];
            }
        }
        return $html; 
    }
    public function guidian($mid){
        $member = Dever::db('agent/member')->find($mid);
        $area = Dever::load("area/api.string", $member['area']);
        $data = array();
        $data = $member['name'].'<br/>'.$member['mobile'].'<br/>'.$area;
        return $data;
    }
    public function role($mid){
        $member = Dever::db('agent/member')->find($mid);
        $role = Dever::db('setting/role')->find($member['role']);
        $data = array();
        $prize_type = '';
        if($member['role'] == 8 && $member['prize_type'] == 1){
            $prize_type = '(名额外)';
        }elseif($member['role'] == 8 && $member['prize_type'] == 2){
            $prize_type = '(名额内)';
        }
        $data = $role['name'].$prize_type;
        return $data;
    }
    public function yeji($mid){
        $start = Dever::input('search_option_start_dever_time');
        $end = Dever::input('search_option_dever_time');
        $where['start'] = $start;
        $where['end'] = $end;
        $where['mid'] = $mid;
        $where['state'] = 1;
        $money = Dever::db('agent/order')->getCount($where);
        return $money['total'];
    }
}