<?php

namespace web\admin\ap;

class cash_out_transfer {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_cash_out');
        $liid = objint($query, 'liid');
        if ($liid > 0)
            $csql->where('cashstatus', $liid);
        else
            $csql->where('cashstatus=50');
        $val = objstr($query, 'vuser');
        if (!empty($val)) {
            $csqlt = new \ciy\sql('ap_user');
            $csqlt->where('name like', $val);
            $trow = $db->getone($csqlt);
            if (is_array($trow)) {
                $csql->where('vuser', $trow['id']);
                $query['vuser'] = $trow['name'];
            } else {
                $csql->where('vuser=0');
            }
        }
        $csql->where('cashtype', objstr($query, 'cashtype'));
        $csql->wherenumrange('cashmoney', objstr($query, 'cashmoney_1'), objstr($query, 'cashmoney_2'), 100);
        $csql->wheredaterange('addtimes', objstr($query, 'addtimes'));
        $csql->where('cashno like', objstr($query, 'cashno'));
        $order = objstr($query, 'order', 'id desc');
        $csql->order($order);
        $query['order'] = $order;
        return [$query, $csql];
    }

    public static function json_list() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        list($where, $csql) = self::setwhere($db, $post);
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $mainrowcount = $post->getint('count');
        $rows = $db->get($csql, $mainrowcount);
        if ($rows === false)
            return errjson($db->error);
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'ap_cash_out');
            foreach ($field as $fr => $v) {
                if (get('_' . $fr))
                    $field[$fr]['c'] = ',' . $field[$fr]['c'];
            }
            $fshow = fieldadd($fshow, $field, 0, '_btn', '操作');
            $field['invfile']['c'] = '';
            $field['invcode']['c'] = '';
            $field['cashmoney']['c'] = '打款金额|,CNY';
            $field['taxmoney']['c'] = '';
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }
        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array('type' => 'input', 'form' => 'vuser', 'name' => '所属用户', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'select', 'form' => 'cashtype', 'name' => '提现方式', 'all' => '全部', 'select' => '银行.微信');
            $input[] = array('type' => 'num', 'form' => 'cashmoney', 'name' => '金额', 'prop' => ' style="width:4em;"');
            $input[] = array('type' => 'daterange', 'form' => 'addtimes', 'name' => '申请时间');
            $input[] = array('type' => 'input', 'form' => 'cashno', 'name' => '支付编号', 'prop' => ' style="width:8em;"');
            $ret['searchinput'] = $input;
        }
        $ret['ap_user'] = getrelation($db, $rows, 'ap_user', 'vuser');
        return succjson($ret);
    }
    public static function json_audit() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p u'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $auditstatus = $post->getint('auditstatus');
        $auditmsg = $post->get('auditmsg');
        $cashno = $post->get('cashno');
        $id = $post->getint('id');
        if ($auditstatus == 90) {
            if (empty($auditmsg))
                return errjson('请填写拒绝原因');
        } else {
            if (empty($cashno))
                return errjson('请填写支付流水号');
        }
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('id', $id);
        $row = $db->getone($csql);
        if (!is_array($row))
            return errjson('数据不存在');
        if ($row['cashstatus'] != 50)
            return errjson('数据状态不是待支付');
        $ids = array();
        try {
            $db->begin();
            if ($auditstatus == 90) { //退余额
                $updata = array();
                \web\cwebcomon::usercashoe($db, 11, $row['vuser'], $row['cashmoney'], '提现支付拒绝');
            }
            $id = $row['id'];
            $updata = array();
            if ($auditstatus == 90) {
                $updata['cashstatus'] = 90;
                $updata['auditmsg'] = $auditmsg;
            } else {
                $updata['cashstatus'] = 100;
                $updata['cashtimes'] = tostamp();
                $updata['cashno'] = $cashno;
            }
            $csql = new \ciy\sql('ap_cash_out');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新失败:' . $db->error);
            $updata['id'] = $id;
            $ids[] = $id;
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        $ret['ids'] = $ids;
        return succjson($ret);
    }

    public static function json_del() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p d'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                if ($row['cashstatus'] < 90)
                    continue;
                if ($row['addtimes'] > time() - 86400 * 30)
                    continue;
                $delid = $row['id'];
                delme($db, $delid, 'ap_cash_out');
                savelogdb($db, $rsuser['id'], 'ap_cash_out', $row, null);
                $vids[] = $delid;
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['ids'] = $vids;
        return succjson($ret);
    }
    public static function json_exportxls() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p e'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        list($where, $csql) = self::setwhere($db, $post);
        $rows = $db->get($csql);
        if (count($rows) > 10000)
            return errjson('将导出' . count($rows) . '条，不建议超过1万条，请筛选缩小范围');
        $fields = array();
        $fields[] = array('style' => 'l', 'width' => 60,  'field' => 'id', 'name' => '行码');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'vuser', 'name' => '所属用户');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'cashmoney', 'name' => '打款金额');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'bankaccount', 'name' => '账户名称');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'bankno', 'name' => '银行卡号');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'bankname', 'name' => '开户银行');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'bankcode', 'name' => '联行号');
        $code_cashstatus = getcatas($db, 'cashstatus');
        $code_vuser = getrelation($db, $rows, 'ap_user', 'vuser', 'id,name');
        $code_cashtype = $db->getdbcodes('ap_cash_out', 'cashtype');
        $datas = array();
        foreach ($rows as $row) {
            $dat = array();
            foreach ($fields as $field) {
                $field = $field['field'];
                $val = isset($row[$field]) ? $row[$field] : '';
                if ($field == 'id')
                    $val = enid($val);
                if ($field == 'cashstatus')
                    $val = ccode($code_cashstatus, $val);
                if ($field == 'vuser')
                    $val = ccode($code_vuser, $val);
                if ($field == 'cashtype')
                    $val = ($val == 1 ? $code_cashtype[0]['name'] : @$code_cashtype[1]['name']);
                if ($field == 'cashmoney') {
                    $val = $row[$field];
                    if ($row['taxmoney'] > 0)
                        $val -= $row['taxmoney'];
                    $val = number_format($val / 100, 2);
                }
                if ($field == 'addtimes')
                    $val = ($val == 0 ? '--' : date('Y-m-d H:i', $val));
                if ($field == 'cashtimes')
                    $val = ($val == 0 ? '--' : date('Y-m-d H:i', $val));
                $dat[] = $val;
            }
            $datas[] = $dat;
        }
        $param = array();
        $param['field'] = $fields;
        $param['data'] = $datas;
        $param['sheetname'] = '数据报表';
        $param['titleheight'] = '25'; //列头高度
        $param['landscape'] = true; //横向打印
        $param['fixtopage'] = true; //打印整个工作表
        $param['toptitle'] = 'Demo数据报表';
        $str = \ciy\excel::general_excel_xml($param);
        $filename = '';
        if (empty($filename))
            $filename = date('Y-m-d_H-i-s') . rand(1000, 9999);
        $filename .= '.xls';
        file_put_contents(PATH_WEB . 'ud/tmp/' . $filename, $str);
        return succjson(array('url' => '/ud/tmp/' . $filename));
    }
    public static function json_exporttaxxls() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p e'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $month = $post->getint('month');
        $monthend = strtotime("+1 month", $month);
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('cashstatus', 100);
        $csql->where('taxmoney>=0');
        $csql->where('cashtimes>=', $month);
        $csql->where('cashtimes<', $monthend);
        $csql->order('vuser,cashtimes');
        $rows = $db->get($csql);
        //姓名与账户名称不符，且未银行卡的，风控提示

        $fields = array();
        $fields[] = array('style' => 'l', 'width' => 60,  'field' => 'id', 'name' => '行码');
        $fields[] = array('style' => 'c', 'width' => 100,  'field' => 'name', 'name' => '姓名');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'idid', 'name' => '身份证号');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'cashmoney', 'name' => '涉税金额');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'taxmoney', 'name' => '个税金额');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'bankno', 'name' => '银行卡号');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'bankname', 'name' => '开户银行');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'addtimes', 'name' => '申请时间');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'cashtimes', 'name' => '支付时间');
        $code_vuser = getrelation($db, $rows, 'ap_usr_ext', 'vuser', 'id,truename,idid');
        $datas = array();
        foreach ($rows as $row) {
            $dat = array();
            foreach ($fields as $field) {
                $field = $field['field'];
                $val = isset($row[$field]) ? $row[$field] : '';
                if ($field == 'id')
                    $val = enid($val);
                if ($field == 'name')
                    $val = ccode($code_vuser, $row['vuser'], 'id', 'truename');
                if ($field == 'idid')
                    $val = ccode($code_vuser, $row['vuser'], 'id', 'idid');
                if ($field == 'cashmoney')
                    $val = number_format($val / 100, 2);
                if ($field == 'taxmoney')
                    $val = number_format($val / 100, 2);
                if ($field == 'addtimes')
                    $val = ($val == 0 ? '--' : date('Y-m-d H:i', $val));
                if ($field == 'cashtimes')
                    $val = ($val == 0 ? '--' : date('Y-m-d H:i', $val));
                $dat[] = $val;
            }
            $datas[] = $dat;
        }
        $param = array();
        $param['field'] = $fields;
        $param['data'] = $datas;
        $param['sheetname'] = '数据报表';
        $param['titleheight'] = '25'; //列头高度
        $param['landscape'] = true; //横向打印
        $param['fixtopage'] = true; //打印整个工作表
        $param['toptitle'] = '个税数据报表';
        $str = \ciy\excel::general_excel_xml($param);
        $filename = '';
        if (empty($filename))
            $filename = date('Y-m-d_H-i-s') . rand(1000, 9999);
        $filename .= '.xls';
        file_put_contents(PATH_WEB . 'ud/tmp/' . $filename, $str);
        return succjson(array('url' => '/ud/tmp/' . $filename));
    }
}
