<?php

namespace App\Http\Controllers;

use App\Exports\Export;
use App\Owner;
use App\ProcessMethod;
use App\ProcessStatistic;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Maatwebsite\Excel\Facades\Excel;

class ProcessStatisticController extends Controller
{
    public function conditionQuery(Request $request,$processStatistics){
        if ($request->input('started_at')){
            $processStatistics=$processStatistics->where('started_at','>=',$request->input('started_at'));
        }
        if ($request->input('owner_id')){
            $owner_id=$request->input('owner_id');
            $processStatistics=$processStatistics->whereHas('process',function (Builder $query)use($owner_id){
                $query->where('owner_id',$owner_id);
            });
        }
        if ($request->input('ended_at')){
            $processStatistics=$processStatistics->where('ended_at','<=',$request->input('ended_at'));
        }
        if ($request->input('code')){
            $code=$request->input('code');
            $processStatistics=$processStatistics->whereHas('process',function (Builder $query)use($code){
                $query->where('code','like',$code."%");
            });
        }
        if ($request->input('status')){
            $status=$request->input('status');
            $processStatistics=$processStatistics->whereHas('process',function (Builder $query)use($status){
                $query->where('status',$status);
            });
        }
        $processStatistics=$processStatistics->paginate($request->input('paginate')?$request->input('paginate'):50);
        return $processStatistics;
    }

    public function index(Request $request){
        $processStatistics=ProcessStatistic::with('process')->orderBy('process_id','DESC');
        if ($request->input('checkSign')){
            $excel=$this->isExport($request,$processStatistics);
            return $excel;
        }
        $processStatistics=$this->conditionQuery($request,$processStatistics);
        $owners=Owner::get();
        $processMethods=ProcessMethod::get();
        return view('process.statistic',['processStatistics'=>$processStatistics,'owners'=>$owners,'processMethods'=>$processMethods]);
    }

    //获取导出数据
    public function isExport(Request $request,$processStatistics){
        if(!Gate::allows('二次加工管理-查询')){ return redirect(url('/'));  }
        if ($request->input('checkSign')=="-1"){
            $processStatistics=$this->conditionQuery($request,$processStatistics);
            $excel=$this->export($processStatistics);
            return $excel;
        }
        $id = explode( ',',$request->input('checkSign'));
        $processStatistics=$processStatistics->whereIn('process_id',$id)->get();
        $excel=$this->export($processStatistics);
        return $excel;
    }

    //执行
    public function export($processStatistics){
        if(!Gate::allows('二次加工管理-查询')){ return '没有权限';  }
        $row=[[
            'process_id'=>'ID',
            'code'=>'任务号',
            'owner_name'=>'货主',
            'started_at'=>'开始日期',
            'ended_at'=>'完成日期',
            'unit_price'=>'单价',
            'amount'=>'预期数量',
            'completed_amount'=>'完成数量',
            'revenue'=>'收入合计',
            'duration_days'=>'完成时间(天)',
            'duration_man_hours'=>'总工时',
            'process_method_name'=>'加工类型',
            'top_capacity'=>'最高日产能',
            'bottom_capacity'=>'最低日产能',
            'average_capacity'=>'日均产能',
            'total_cost'=>'合计成本',
            'gross_profit'=>'毛利润',
            'gross_profit_rate'=>'毛利率',
            'status'=>'状态',
        ]];
        $list=[];
        $i=0;
        foreach ($processStatistics as $processStatistic){
            $w=[
                'process_id'=>$processStatistic->process_id,
                'code'=>$processStatistic->process?$processStatistic->process->code:'',
                'owner_name'=>$processStatistic->process?$processStatistic->process->owner_name:'',
                'started_at'=>$processStatistic->started_at,
                'ended_at'=>$processStatistic->ended_at,
                'unit_price'=>$processStatistic->process?$processStatistic->process->unit_price:'',
                'amount'=>$processStatistic->process?$processStatistic->process->amount:'',
                'completed_amount'=>$processStatistic->process?$processStatistic->process->completed_amount:'',
                'revenue'=>$processStatistic->revenue,
                'duration_days'=>$processStatistic->duration_days,
                'duration_man_hours'=>$processStatistic->duration_man_hours,
                'process_method_name'=>$processStatistic->process?$processStatistic->process->process_method_name:'',
                'top_capacity'=>$processStatistic->top_capacity,
                'bottom_capacity'=>$processStatistic->bottom_capacity,
                'average_capacity'=>$processStatistic->average_capacity,
                'total_cost'=>$processStatistic->total_cost,
                'gross_profit'=>$processStatistic->gross_profit,
                'gross_profit_rate'=>round(($processStatistic->gross_profit_rate)*100,2).'%',
                'status'=>$processStatistic->process?$processStatistic->process->status:'',
            ];
            $list[$i]=$w;
            $i++;
        }
        return Excel::download(new Export($row,$list),date('YmdHis', time()).'-二次加工单.xls');
    }
}
