<?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 cashAuditUpdate($id, $name, $data)
    {
    	Dever::config('base')->hook = true;
        $audit = Dever::param('audit', $data);
        if ($audit > 1) {
        	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);
	        	}
	        } 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);
	        	}
	        }
        }
    }

    public function tixianAuditUpdate($id, $name, $data)
    {
    	Dever::config('base')->hook = true;
        $audit = Dever::param('audit', $data);
        if ($audit > 1) {
        	$info = Dever::db('bill/cash')->find(array('type' => 11, 'type_id' => $id));
        	if (!$info) {
        		Dever::alert('系统错误');
        	}
        	if ($audit == 2) {
	        	Dever::db('bill/tixian')->update(array('where_id' => $id, 'status' => 2, 'operdate' => time()));
	        	if ($info && $info['status'] == 1) {
	        		Dever::db('bill/cash')->update(array('where_id' => $id, 'status' => 2, 'operdate' => time()));
	        	}
	        }
        }
    }

    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);
        if ($mid && $num && $desc) {
        	$member = Dever::db('agent/member')->find($mid);
        	if ($member && $member['status'] == 2) {
        		Dever::load('bill/lib/sell')->up($mid, 2, $num, $member['role'], $id, $desc);
        		$where['where_id'] = $mid;
        		$where['group_sell'] = $num;
        		$where['sell'] = $num;
        		Dever::db('agent/member')->upGroupSellOne($where);
        	} 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;
    }
}