<?php

namespace Cash\Lib;

use Dever;

class Order
{
	# 更新结算单
    public function up($order, $jstype = 1, $audit = 1, $refund = false, $pay_type = 1)
    {
        Dever::config('base')->hook = true;
        if (!$order) {
            return false;
        }
        if ($audit == 2) {
            $status = 2;
        } else {
            $status = 1;
        }
        if ($jstype == 2) {
            $audit_type = 2;
        } else {
            $audit_type = 1;
        }
        $where['source_order_id'] = $order['id'];
        $where['jstype'] = $jstype;
        if (!$refund) {
            $where['refund_id'] = -1;
        } else {
            $where['refund_id'] = $refund['id'];
        }
        $where['clear'] = true;

        $info = Dever::db('cash/order')->find($where);

        $update = $where;
        $update['pay_type'] = $pay_type;
        $update['status'] = $status;
        $update['audit'] = $audit;
        $update['audit_type'] = $audit_type;

        if ($update['status'] == 2) {
            $update['operdate'] = time();
            $update['fdate'] = $update['operdate'];
            if (isset($order['fdate']) && $order['fdate'] && ($order['status'] == 5 || $order['status'] == 6)) {
                $update['fdate'] = $order['fdate'];
            }
        }

        if ($info && $jstype == 3) {

        } else {
            
            $update['type'] = $order['type'];
            $update['type_id'] = $order['type_id'];
            if ($order['source_type']) {
                $update['source_type'] = $order['source_type'];
                $update['source_id'] = $order['source_id'];
            }

            $update['source_order_num'] = $order['order_num'];
            $update['num'] = $order['num'];

            if ($refund) {
                $update['cash'] = $refund['cash'];
                $update['p_cash'] = isset($refund['p_cash']) ? $refund['p_cash'] : 0;
            } else {
                $update['cash'] = $order['price'];
                $update['p_cash'] = $order['p_price'];
            }

            if ($jstype == 2) {
                $update['cash'] = -1*$update['cash'];
                $update['p_cash'] = -1*$update['p_cash'];
                $update['num'] = -1*$update['num'];
            }
        }

        if ($info) {
        	$update['where_id'] = $info['id'];
        	$state = Dever::db('cash/order')->update($update);
        } else {
        	$update['order_num'] = $this->getOrderId();
        	$state = Dever::db('cash/order')->insert($update);
        }

        if ($state && $jstype == 2 && $status == 2 && isset($order['status']) && $order['status'] == 8) {
            # 如果是退款结算，同时要把主订单也结算
            $where = array();
            $where['source_order_id'] = $order['id'];
            if ($pay_type > 1) {
                $where['jstype'] = 3;
            } else {
                $where['jstype'] = 1;
            }
            
            $where['audit'] = 1;
            $where['clear'] = true;
            $info = Dever::db('cash/order')->find($where);
            if ($info) {
                $set['status'] = 2;
                $set['audit'] = 2;
                $set['where_id'] = $info['id'];
                $set['operdate'] = time();
                $set['fdate'] = time();
                Dever::db('cash/order')->update($set);
            }
        }
    }

    # 生成订单号
    public function getOrderId()
    {
        $where['order_num'] = Dever::order('JS');
        $state = Dever::db('cash/order')->one($where);
        if (!$state) {
            return $where['order_num'];
        } else {
            return $this->getOrderId();
        }
    }
    #导出Excel
    public function out_order_api($data){
        $header = array('ID', '结算单号', '结算门店', '订货单编号', '数量', '结算金额', '结算类型', '入账状态', '审核状态', '支付类型', '结算时间','审核备注','生成时间');
        $body = array();
        foreach($data as $k => $v){
            $type_info = Dever::db('shop/info')->find(array('id'=>$v['type_id']));
            $config_jstype = Dever::db('cash/order')->config['config_jstype'];
            if(isset($v['jstype']) && $v['jstype']){
                $jstype = Dever::status($config_jstype,$v['jstype']);
            }
            
            $config_status = Dever::db('cash/order')->config['config_status'];
            if(isset($v['status']) && $v['status']){
                $status = Dever::status($config_status,$v['status']);
            }
            $config_audit = Dever::db('cash/order')->config['config_audit'];
            if(isset($v['audit']) && $v['audit']){
                $audit = Dever::status($config_audit,$v['audit']);
            }
            if(isset($v['pay_type']) && $v['pay_type']){
                if($v['pay_type'] == 1){
                    $pay_type_name = '在线支付';
                }elseif($v['pay_type'] == 2){
                    $pay_type_name = '礼品卡支付';
                }else{
                    $pay_type_name = '钱包支付';
                }
            }
            $operdate = '-';
            if(isset($v['operdate']) && $v['operdate']){
                $operdate = date('Y-m-d H:i',$v['operdate']);
            }
            $cdate = '-';
            if(isset($v['cdate']) && $v['cdate']){
                $cdate = date('Y-m-d H:i',$v['cdate']);
            }
            
            $d = array
            (
                $v['id'],
                $v['order_num'],
                $type_info['name'],
                $v['source_order_num'],
                $v['num'],
                $v['cash'],
                $jstype,
                $status,
                $audit,
                $pay_type_name,
                $operdate,
                $v['desc'],
                $cdate,
            );
            $body[] = $d;
        }
        $file = Dever::input('excel_name');
        return Dever::excelExport($body, $header, $file);
    }
}