<?php

namespace App\Http\Controllers;

use App\Exports\Export;
use App\WaybillFinancialSnapshot;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Maatwebsite\Excel\Facades\Excel;

class WaybillFinancialSnapshotsController extends Controller
{
    public function conditionQuery(Request $request,$waybillFinancialSnapshots){
        if ($request->input('created_at_start')){
            $created_at_start=$request->input('created_at_start')." 00:00:00";
            $waybillFinancialSnapshots=$waybillFinancialSnapshots->where('created_at','>=',$created_at_start);
        }
        if ($request->input('created_at_end')){
            $created_at_end=$request->input('created_at_end')." 23:59:59";
            $waybillFinancialSnapshots=$waybillFinancialSnapshots->where('created_at','<=',$created_at_end);
        }
        $waybillFinancialSnapshots=$waybillFinancialSnapshots->paginate(50);
        return $waybillFinancialSnapshots;
    }
    public function index(Request $request)
    {
        if(!Gate::allows('财务报表-查询')){ return redirect(url('/'));  }
        $waybillFinancialSnapshots=WaybillFinancialSnapshot::orderBy('id', 'DESC');
        $type='';
        if ($request->type=='ZF'){
            $waybillFinancialSnapshots=$waybillFinancialSnapshots->where('json_content','like','%直发车%');
            $type='ZF';
        }
        if ($request->type=='ZX'){
            $waybillFinancialSnapshots=$waybillFinancialSnapshots->where('json_content','like','%专线%');
            $type='ZX';
        }
        $waybillFinancialSnapshots=$this->conditionQuery($request,$waybillFinancialSnapshots);
        return view('waybill.waybillFinancialSnapshot.index',['waybillFinancialSnapshots'=>$waybillFinancialSnapshots,'filterData'=>$request->input(),'type'=>$type]);
    }
    public function export($id,Request $request){
        if(!Gate::allows('财务报表-查询')){ return '没有权限';  }
        if ($id==-1){
            $id=[];
            ini_set('max_execution_time',2500);
            ini_set('memory_limit','1526M');
            $waybillFinancialSnapshots=WaybillFinancialSnapshot::select('id');
            if ($request->input('created_at_start')){
                $waybillFinancialSnapshots=$waybillFinancialSnapshots->where('created_at','>=',$request->input('created_at_start'));
            }
            if ($request->input('created_at_end')){
                $waybillFinancialSnapshots=$waybillFinancialSnapshots->where('created_at','<=',$request->input('created_at_end'));
            }
            $waybillFinancialSnapshots=$waybillFinancialSnapshots->get();
            foreach ($waybillFinancialSnapshots as $waybillFinancialSnapshot){
                array_push($id,$waybillFinancialSnapshot->id);
            }
        }else $id = explode( ',',$id);
        if (!$id)return ;
        $row=[[
            'type'=>'运单类型',
            'waybill_number'=>'运单号',
            'owner'=>'货主',
            'wms_bill_number'=>'WMS单号',
            'origination'=>'始发地',
            'destination'=>'目的地',
            'recipient'=>'收件人',
            'recipient_mobile'=>'收件人电话',
            'charge'=>'收费(元)',
            'ordering_remark'=>'下单备注',
            'carrier'=>'承运商',
            'carrier_bill'=>'承运商单号',
            'origination_city'=>'始发市',
            'destination_city'=>'目的市',
            'warehouse_weight'=>'仓库计数(抛)',
            'warehouse_weight_other'=>'仓库计数二',
            'carrier_weight'=>'承运商计数(抛)',
            'carrier_weight_other'=>'承运商计数二',
            'carType'=>'车型',
            'car_owner_info'=>'车型',
            'fee'=>'运费(元)',
            'pick_up_fee'=>'提货费(元)',
            'other_fee'=>'其他费用(元)',
            'collect_fee'=>'到付金额(元)',
            'dispatch_remark'=>'调度备注',
            'waybillAuditor'=>'运单审核人',
            'dispatchAuditor'=>'调度审核人',
            'created_at'=>'运单创建时间',
            'total_receivable'=>'应收款(元)',
            'total_expense'=>'应付款(元)',
            'gross_margin'=>'毛利(元)',
            'gross_profit_rate'=>'毛利率(元)',
            'snapshotCreated_at'=>'报表生成时间',
        ]];
        $list=[];
        for ($i=0; $i<count($id);$i++){
            $waybillFinancialSnapshot=WaybillFinancialSnapshot::where('waybill_id','=',$id[$i])->first();
            $waybillFinancialSnapshot=json_decode($waybillFinancialSnapshot->json_content);
            $waybillAuditor='';
            $dispatchAuditor='';
            foreach ($waybillFinancialSnapshot->waybill->waybill_audit_logs as $waybill_audit_logs){
                if ($waybill_audit_logs->audit_stage=="运单阶段"){
                    $waybillAuditor=$waybill_audit_logs->user->name;
                }
                if ($waybill_audit_logs->audit_stage=="调度阶段"){
                    $dispatchAuditor=$waybill_audit_logs->user->name;
                }
            };
            $w=[
                'type'=>isset($waybillFinancialSnapshot->waybill->type)?$waybillFinancialSnapshot->waybill->type:'',
                'waybill_number'=>isset($waybillFinancialSnapshot->waybill->waybill_number)?$waybillFinancialSnapshot->waybill->waybill_number:'',
                'owner'=>isset($waybillFinancialSnapshot->waybill->owner->name)?$waybillFinancialSnapshot->waybill->owner->name:'',
                'wms_bill_number'=>isset($waybillFinancialSnapshot->waybill->wms_bill_number)?$waybillFinancialSnapshot->waybill->wms_bill_number:'',
                'origination'=>isset($waybillFinancialSnapshot->waybill->origination)?$waybillFinancialSnapshot->waybill->origination:'',
                'destination'=>isset($waybillFinancialSnapshot->waybill->destination)?$waybillFinancialSnapshot->waybill->destination:'',
                'recipient'=>isset($waybillFinancialSnapshot->waybill->recipient)?$waybillFinancialSnapshot->waybill->recipient:'',
                'recipient_mobile'=>isset($waybillFinancialSnapshot->waybill->recipient_mobile)?$waybillFinancialSnapshot->waybill->recipient_mobile:'',
                'charge'=>isset($waybillFinancialSnapshot->waybill->charge)?$waybillFinancialSnapshot->waybill->charge:'',
                'ordering_remark'=>isset($waybillFinancialSnapshot->waybill->ordering_remark)?$waybillFinancialSnapshot->waybill->ordering_remark:'',
                'carrier'=>isset($waybillFinancialSnapshot->waybill->carrier->name)?$waybillFinancialSnapshot->waybill->carrier->name:'',
                'carrier_bill'=>isset($waybillFinancialSnapshot->waybill->carrier_bill)?$waybillFinancialSnapshot->waybill->carrier_bill:'',
                'origination_city'=>isset($waybillFinancialSnapshot->waybill->origination_city->name)?$waybillFinancialSnapshot->waybill->origination_city->name:'',
                'destination_city'=>isset($waybillFinancialSnapshot->waybill->destination_city->name)?$waybillFinancialSnapshot->waybill->destination_city->name:'',
                'warehouse_weight'=>isset($waybillFinancialSnapshot->waybill->warehouse_weight)?
                        $waybillFinancialSnapshot->waybill->warehouse_weight.' '.(isset($waybillFinancialSnapshot->waybill->warehouse_weight_unit_name)?$waybillFinancialSnapshot->waybill->warehouse_weight_unit_name:''):'',
                'warehouse_weight_other'=>isset($waybillFinancialSnapshot->waybill->warehouse_weight_other)?
                    $waybillFinancialSnapshot->waybill->warehouse_weight_other.' '.(isset($waybillFinancialSnapshot->waybill->warehouse_weight_unit_other_name)?$waybillFinancialSnapshot->waybill->warehouse_weight_unit_other_name:''):'',
                'carrier_weight'=>isset($waybillFinancialSnapshot->waybill->carrier_weight)?
                        $waybillFinancialSnapshot->waybill->carrier_weight.' '.(isset($waybillFinancialSnapshot->waybill->warehouse_weight_unit_name)?$waybillFinancialSnapshot->waybill->warehouse_weight_unit_name:''):'',
                'carrier_weight_other'=>isset($waybillFinancialSnapshot->waybill->carrier_weight_other)?
                    $waybillFinancialSnapshot->waybill->carrier_weight_other.' '.(isset($waybillFinancialSnapshot->waybill->carrier_weight_unit_other_name)?$waybillFinancialSnapshot->waybill->carrier_weight_unit_other_name:''):'',
                'carType'=>isset($waybillFinancialSnapshot->waybill->carType->name)?$waybillFinancialSnapshot->waybill->carType->name.($waybillFinancialSnapshot->waybill->carType->length.'米'):'',
                'car_owner_info'=>isset($waybillFinancialSnapshot->waybill->car_owner_info)?$waybillFinancialSnapshot->waybill->car_owner_info:'',
                'fee'=>isset($waybillFinancialSnapshot->waybill->fee)?$waybillFinancialSnapshot->waybill->fee:'',
                'pick_up_fee'=>isset($waybillFinancialSnapshot->waybill->pick_up_fee)?$waybillFinancialSnapshot->waybill->pick_up_fee:'',
                'other_fee'=>isset($waybillFinancialSnapshot->waybill->other_fee)?$waybillFinancialSnapshot->waybill->other_fee:'',
                'collect_fee'=>isset($waybillFinancialSnapshot->waybill->collect_fee)?$waybillFinancialSnapshot->waybill->collect_fee:'',
                'dispatch_remark'=>isset($waybillFinancialSnapshot->waybill->dispatch_remark)?$waybillFinancialSnapshot->waybill->dispatch_remark:'',
                'waybillAuditor'=>isset($waybillAuditor)?$waybillAuditor:'',
                'dispatchAuditor'=>isset($dispatchAuditor)?$dispatchAuditor:'',
                'created_at'=>isset($waybillFinancialSnapshot->waybill->created_at)?$waybillFinancialSnapshot->waybill->created_at:'',
                'total_receivable'=>isset($waybillFinancialSnapshot->total_receivable)?$waybillFinancialSnapshot->total_receivable:'',
                'total_expense'=>isset($waybillFinancialSnapshot->total_expense)?$waybillFinancialSnapshot->total_expense:'',
                'gross_margin'=>isset($waybillFinancialSnapshot->gross_margin)?$waybillFinancialSnapshot->gross_margin:'',
                'gross_profit_rate'=>isset($waybillFinancialSnapshot->gross_profit_rate)?$waybillFinancialSnapshot->gross_profit_rate:'',
                'snapshotCreated_at'=>isset($waybillFinancialSnapshot->created_at)?$waybillFinancialSnapshot->created_at:'',
            ];
            $list[$i]=$w;
        }
        $this->log(__METHOD__,__FUNCTION__,json_encode($waybillFinancialSnapshot),Auth::user()['id']);
        return Excel::download(new Export($row,$list), date('Y:m:d ') . '财务报表.xlsx');
    }
}
