<?php

namespace App\Http\Controllers\API\H5;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class InvoiceController extends Controller
{
    public function Save(Request $request){
        date_default_timezone_set('PRC');
        $bitian = array('type',     'content','unit_type','unit_name','unit_num','price','tel');
        $bitian_zh = array('发票类型', '发票内容','单位类型',    '发票抬头',   '识别号',   '价格' ,'电话');
        $userid = $request->get('userid');//中间件产生的参数
        $appid = $request->get('role');//中间件产生的参数
        $invoiceInfo =request('invoiceInfo');
        foreach ($bitian as $key=> $field) { //必填项不能为空
            if (array_key_exists($field, $invoiceInfo) && $invoiceInfo[$field] !=='' && $invoiceInfo[$field] !==null ) {
            } else {
             return \Yz::echoError1($bitian_zh[$key].' 不能为空');
            }
        }
        $invoiceInfo['userid']=$userid;
        $invoiceInfo['appid']=$appid;
        $invoiceInfo['status']=1;
        $invoiceInfo['is_del']=0;
        $invoiceInfo['receive_type']=1;//接收方式1为其他0为邮寄
        if($invoiceInfo['type']==0){
            $invoiceInfo['receive_type']=0;
        }
        $invoiceInfo['shenqing_time'] = date('Y-m-d H:i:s');
        if(isset($invoiceInfo['link_order_num'])){
            //查看 同一个应用下 是否存在关联的订单如果有申请记录则拒绝
            $cha=DB::table('y_invoice')->where(['link_order_num'=>$invoiceInfo['link_order_num'],'appid'=>$appid,'is_del'=>0])->whereIn('status',[1,2])->get();
            if(count($cha)>0){
                return \Yz::echoError1('已有此订单记录，禁止重复申请。状态：'.$cha[0]->status);
            }
        }

        $i=DB::table('y_invoice')->insert($invoiceInfo);
        if($i){
            return \Yz::Return(true,'保存成功','');
        }else{
            return \Yz::echoError1('保存失败');
        }
    }
    public function GetList(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $status =request('status');
        $list=DB::table('y_invoice')->where(['userid'=>$userid,'is_del'=>0]) ;
        if($status<>9){
            $list=$list->where('status',$status);
        }
        $list=$list->orderBy('id','desc')->get();
        return \Yz::Return(true,'',$list);
    }

    public function GetDetail(Request $request)
    {
        $info=false;
        $id =request('id');
        $ordernum =request('ordernum');
        $userid = $request->get('userid');//中间件产生的参数
        $appid = $request->get('role');//中间件产生的参数
        if(isset($id)){
            $info=DB::table('y_invoice')->where(['userid'=>$userid,'id'=>$id,'is_del'=>0])->first();
        }
        if(isset($ordernum)){
            $info=DB::table('y_invoice')->where(['userid'=>$userid,'link_order_num'=>$ordernum,'appid'=>$appid,'is_del'=>0])->first();
        }

        if(!!$info){
            return \Yz::Return(true,'',$info);
        }else{
            return \Yz::echoError1("未找到相关发票信息");
        }

    }

    //获取上次填写的历史信息
    public function GetHistory(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $list=DB::table('y_invoice')->where(['userid'=>$userid])->orderBy('id','desc')->first();
        return \Yz::Return(true,'',$list);
    }
}
