<?php namespace Goods\Lib;
use Dever;
use Place;
class Refund
{
    # 后台提交
    public function update($db, $data)
    {
        $detail = Dever::input('goods/order_refund_detail');
        $order = Dever::db('order', 'goods')->find($data['order_id']);
        if ($order && $order['type'] == 1 && $order['status'] <= 4) {
            $auth = new \Manage\Lib\Auth();
            $uid = $auth->uid;
            $log['type'] = 2;
            $log['uid'] = $uid;
            $this->up($detail, $order, $data['type'], $data['desc_type'], $data['cash'], $data['desc'], $log);
        }
        return 'end';
    }

    # 后台审核
    public function audit($db, $data)
    {
        $refund = Dever::db('order_refund', 'goods')->find($data['id']);
        if ($refund['status'] == 1) {
            if ($data['audit'] == 1) {
                $refund['status'] = $update['status'] = 2;
                $update['sdate'] = time();
            } else {
                $update['status'] = 5;
                $update['sdate'] = time();
                $update['fdate'] = time();
            }
            $update['audit_desc'] = $data['audit_desc'];
            Dever::db('order_refund', 'goods')->update($data['id'], $update);
            $auth = new \Manage\Lib\Auth();
            $msg = '退款：' . $update['audit_desc'];
            Dever::load('log', 'goods')->up(2, $auth->uid, $refund['order_id'], $msg);
            if ($refund['type'] == 2 && $refund['status'] == 2) {
                # 仅退款
                $order = Dever::db('order', 'goods')->find($refund['order_id']);
                $this->finish($order, $refund);
            }
        }
        return 'end';
    }

    # 买家发货
    public function express($order, $refund, $express_id, $number)
    {
        $refund = Dever::db('order_refund', 'goods')->find($refund['id']);
        if ($refund['status'] == 2) {
            $update['status'] = 3;
            $update['ddate'] = time();
            $where = array('order_refund_id' => $refund['id']);
            $info = Dever::db('order_refund_express', 'goods')->find($where);
            if (!$info) {
                $where['order_id'] = $order['id'];
                $where['express_id'] = $express_id;
                $where['number'] = $number;
                Dever::db('order_refund_express', 'goods')->insert($where);
                Dever::load('log', 'goods')->up(1, $order['uid'], $order['id'], '退款：买家已发货');
            }
            Dever::db('order_refund', 'goods')->update($refund['id'], $update);
        }
    }

    # 完成退款 如果是支付订单，原路退回
    public function finish($order, $refund)
    {
        $refund = Dever::db('order_refund', 'goods')->find($refund['id']);
        if ($refund['status'] == 2 || $refund['status'] == 3) {
            $update['status'] = 4;
            $update['fdate'] = time();
            $update['refund_status'] = 1;
            Dever::db('order_refund', 'goods')->update($refund['id'], $update);
            Dever::load('log', 'goods')->up(1, $order['uid'], $order['id'], '退款：已完成退款');

            # 检测订单是否全都退了
            $refund = Dever::db('order_refund', 'goods')->select(array('order_id' => $order['id']));
            $cash = 0;
            foreach ($refund as $k => $v) {
                $cash += $v['cash'];
            }

            if ($cash >= $order['cash']) {
                $refund = Dever::db('order', 'goods')->update($order['id'], array('status' => 8, 'fdate' => time(), 'refund_status' => 2));

                Dever::load('log', 'goods')->up(1, $order['uid'], $order['id'], '订单已退款');
                # 恢复库存
                Dever::load('order', 'goods')->resell($order['id']);
            }

            # 判断是否支付订单，如果是，走退款流程
            if ($order['type'] == 1) {

            }
        }
    }

    public function up($detail, $order, $type, $desc_type, $cash, $desc, $log = false)
    {
        if ($detail) {
            foreach ($detail as $k => $v) {
                $info = Dever::db('order_detail', 'goods')->find($v['detail_id']);
                $refund = Dever::db('order_refund_detail', 'goods')->find(array('detail_id' => $v['detail_id'], 'refund_status' => 1), array('col' => 'sum(num) as num'));
                if ($refund && $refund['num']) {
                    $info['num'] -= $refund['num'];
                }
                if ($v['num'] > $info['num']) {
                    Dever::error('退款数量不能大于下单商品数量');
                }
            }
        }
        $where = array('order_id' => $order['id'], 'refund_status' => 1);
        $refund = Dever::db('order_refund', 'goods')->find($where, array('col' => 'sum(cash) as cash'));
        if ($refund && $refund['cash']) {
            $order['cash'] -= $refund['cash'];
        }
        if ($cash > $order['cash']) {
            Dever::error('退款金额不能大于订单金额');
        }
        $update = $where;
        $update['refund_status'] = 2;
        $update['status'] = array('<', '5');
        $refund = Dever::db('order_refund', 'goods')->find($update);
        $update['type'] = $type;
        $update['desc_type'] = $desc_type;
        $update['cash'] = $cash;
        $update['desc'] = $desc;
        $update['status'] = 1;
        if ($refund) {
            Dever::error('您有退款进行中，请先处理');
        } else {
            $id = Dever::db('order_refund', 'goods')->insert($update);
            if ($id) {
                Dever::db('order', 'goods')->update($order['id'], array('refund_status' => 1));
                if ($detail) {
                    foreach ($detail as $k => $v) {
                        $v['order_refund_id'] = $id;
                        Dever::db('order_refund_detail', 'goods')->insert($v);
                    }
                }
                if ($log) {
                    $type = $log['type'];
                    $uid = $log['uid'];
                    $msg = '退款：提交退款申请单';
                    Dever::load('log', 'goods')->up($type, $uid, $order['id'], $msg);
                }
            }
        }
    }

    # 获取信息
    public function getList($order_id)
    {
        $log = Dever::db('order_log', 'goods')->select(array('order_id' => $order_id));
        if ($log) {
            foreach ($log as &$v) {
                $v = $this->getInfo($v);
            }
        }
        return $log;
    }

    # 获取信息
    public function getInfo($info)
    {
        if ($info['type'] == 1) {
            $user = Dever::db('member', 'place')->find($info['type_id']);
        } elseif ($info['type'] == 2) {
            $user = Dever::db('user', 'sector')->find($info['type_id']);
        } elseif ($info['type'] == 3) {
            $user = Dever::db('supplier', 'place')->find($info['type_id']);
        } else {
            $user['name'] = $user['mobile'] = '-';
        }
        $info['type'] = Dever::db('order_log', 'goods')->value('type', $info);
        $info['name'] = $user['name'];
        $info['mobile'] = $user['mobile'];
        $info['cdate_str'] = date('Y-m-d H:i:s', $info['cdate']);
        return $info;
    }
}