<?php

namespace App\Http\Controllers\API\Admin\YeWu;

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

class InvoiceController extends Controller
{
    public function GetList()
    {
        $searchInfo =request('searchInfo');

        $list=DB::table('y_invoice')->leftJoin('y_app', 'y_invoice.appid', '=', 'y_app.app_id')->where(['y_invoice.is_del'=>0])
            ->select('y_invoice.*', 'y_app.app_name');

        if(isset($searchInfo['status']) and $searchInfo['status'] !== null){
            $list=$list->where('y_invoice.status',$searchInfo['status']);
        }
        if(isset($searchInfo['unit_name'])){
            $list=$list->where('y_invoice.unit_name','like','%'.$searchInfo['unit_name'].'%');
        }
        if(isset($searchInfo['tel'])){
            $list=$list->where('y_invoice.tel','like','%'.$searchInfo['tel'].'%');
        }
        $list=$list->orderBy('y_invoice.id','desc')->get();
        return \Yz::Return(true,'',$list);
    }
    public function GetDetail()
    {
        $id =request('id');
        $list=DB::table('y_invoice')
            ->leftJoin('y_app', 'y_invoice.appid', '=', 'y_app.app_id')
            ->select('y_invoice.*', 'y_app.app_name')
            ->where(['y_invoice.id'=>$id,'y_invoice.is_del'=>0])
            ->first();
        return \Yz::Return(true,'',$list);
    }
    public function Save()
    {
        date_default_timezone_set('PRC');
        $time = date('Y-m-d H:i:s');
        $chuli_time=$time;

        $info=request('info');
        if(!isset($info['id'])) return \Yz::echoError1('缺失id');
        $kaipiao_time=$info['kaipiao_time'];
        if($info['status']==2 && ($info['kaipiao_time']==null or $info['kaipiao_time']=='')){
            $kaipiao_time=$time;
        }
        $u=DB::table('y_invoice')->where(['id'=>$info['id']])->update([
            'status'=>$info['status'],
            'admin_note'=>$info['admin_note'],
            'chuli_time'=>$chuli_time,
            'kaipiao_time'=>$kaipiao_time
        ]);
        if($u){
            return \Yz::Return(true,'操作成功',[]);
        }else{
            return \Yz::Return(false,'操作失败',[]);
        }
    }
    public function Del()
    {
        $id=request('id');
        $d=DB::table('y_invoice')->where(['id'=>$id])->update([
            'is_del'=>1
        ]);
        if($d){
            return \Yz::Return(true,'操作成功',[]);
        }else{
            return \Yz::Return(false,'删除失败',[]);
        }
    }
}
