<?php

namespace App\Http\Controllers\API\Report;

use App\Http\Controllers\Controller;
use App\Models\ArtificialInsemination;
use App\Models\Employee;
use App\Models\Organization;
use App\Models\Herd;
use App\Models\Lot;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class GeneralReportController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|array
     * @throws \Illuminate\Validation\ValidationException
     */
    public function generalReport(Request $request)
    {
        if(Auth::user()->id == 1){
            $organizations = Organization::all();
        }else{
            $organizations = User::findOrfail(Auth::user()->id)->organizations;
        }

        //Organization
        $organizationId = array();
        $herdId = array();
        $lotId = array();
        if($request->organization_id == null && $request->herd_id == null && $request->lot_id == null){
            $organizationSelected = null;
            $herdSelected = null;
            $lotSelected = null;
            if(Auth::user()->id == 1){
                $organizationId = Organization::pluck('id');
                $herdId = Herd::pluck('id');
                $lotId = Lot::pluck('id');
            }else{
                $organizationId = DB::table('organization_user')->where('user_id',Auth::user()->id)->pluck('organization_id');
                $herdId = DB::table('herd_user')->where('user_id',Auth::user()->id)->pluck('herd_id');
                $lotId = DB::table('lot_user')->where('user_id',Auth::user()->id)->pluck('lot_id');
            }
        }elseif(!empty($request->organization_id) && $request->herd_id == null && $request->lot_id == null){
            $organizationSelected = Organization::whereIn('id',$request->organization_id)->get();
            $herdSelected = null;
            $lotSelected = null;
            if(Auth::user()->id == 1){
                $organizationId = Organization::whereIn('id',$request->organization_id)->pluck('id');
                $herdId = Herd::whereIn('organization_id',$organizationId)->pluck('id');
                $lotId = Lot::whereIn('herd_id',$herdId)->pluck('id');
            }else{
                $organizationId = DB::table('organization_user')->whereIn('organization_id',$request->organization_id)->where('user_id',Auth::user()->id)->pluck('organization_id');
                $herdId = DB::table('herd_user')->whereIn('herd_id',$organizationId)->where('user_id',Auth::user()->id)->pluck('herd_id');
                $lotId = DB::table('lot_user')->whereIn('lot_id',$herdId)->where('user_id',Auth::user()->id)->pluck('lot_id');
            }
        }elseif(!empty($request->organization_id) && !empty($request->herd_id) && $request->lot_id == null){
            $organizationSelected = Organization::whereIn('id',$request->organization_id)->get();
            $herdSelected = Herd::whereIn('id',$request->herd_id)->get();
            $lotSelected = null;
            if(Auth::user()->id == 1){
                $organizationId = Organization::whereIn('id',$request->organization_id)->pluck('id');
                $herdId = Herd::whereIn('id',$request->herd_id)->whereIn('organization_id',$organizationId)->pluck('id');
                $lotId = Lot::whereIn('herd_id',$herdId)->pluck('id');
            }else{
                $organizationId = DB::table('organization_user')->whereIn('organization_id',$request->organization_id)->where('user_id',Auth::user()->id)->pluck('organization_id');
                $herdId = DB::table('herd_user')->whereIn('herd_id',$request->herd_id)->where('user_id',Auth::user()->id)->pluck('herd_id');
                $lotId = DB::table('lot_user')->whereIn('lot_id',$herdId)->where('user_id',Auth::user()->id)->pluck('lot_id');
            }
        }else {
            $organizationSelected = Organization::whereIn('id',$request->organization_id)->get();
            $herdSelected = Herd::whereIn('id',$request->herd_id)->get();
            $lotSelected = Lot::whereIn('id',$request->lot_id)->get();
            $organizationId = $request->organization_id;
            $herdId = $request->herd_id;
            $lotId = $request->lot_id;
        }

        $status = array();
        if($request->sex == 'female' && $request->status == 'adult' && $request->adult_status == 'all' ){
            $status = array('pregnant','milking','dry');
        }elseif($request->sex == 'female' && $request->status == 'adult' && $request->adult_status == 'pregnant'){
            $status = array('pregnant');
        }elseif($request->sex == 'female' && $request->status == 'adult' && $request->adult_status == 'milking'){
            $status = array('milking');
        }elseif($request->sex == 'female' && $request->status == 'adult' && $request->adult_status == 'dry'){
            $status = array('dry');
        }elseif($request->sex == 'female' && $request->status == 'heifer'){
            $status = array('heifer');
        }elseif($request->status == 'calf'){
            $status = array('calf');
        }elseif($request->sex == 'female' && $request->status == 'adult' && $request->adult_status == 'calving'){
            $status = array('pregnant','milking','dry','heifer','calf');
        }elseif($request->sex == 'female' && $request->status == 'adult' && $request->adult_status == 'open_bred'){
            $status = array('milking','dry','heifer','calf');
        }elseif($request->sex == 'female' && $request->status == 'adult' && $request->adult_status == 'open_unbred'){
            $status = array('open_unbred');
        }elseif($request->sex == 'male' && $request->status == 'bull'){
            $status = array('bull');
        }elseif($request->sex == 'male' && $request->status == 'reproduction_stage'){
            $status = array('reproduction_stage');
        }else{
            $status = array('heifer','calf');
        }
        $open_unbred_employees = [];
        $open_bred_employees = [];
        $pregnant_employees = [];
        $calving_employees = [];
        $milking_employees = [];
        $dry_employees = [];
        if($request->start_date != null && $request->end_date != null ){
            $start_date = Carbon::createFromFormat('d-m-Y', $request->start_date)->format('Y-m-d');
            $end_date = Carbon::createFromFormat('d-m-Y', $request->end_date)->format('Y-m-d');

            if($request->status == 'adult' && $request->adult_status == 'pregnant'){
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::Join('pregnancy_diagnosises',function($join) use ($request) {
                    $join->on('employees.id','=','pregnancy_diagnosises.employee_id')
                        ->whereDate('pregnancy_diagnosises.created_at', '>=', Carbon::createFromFormat('d-m-Y', $request->start_date)->format('Y-m-d'))->whereDate('pregnancy_diagnosises.created_at', '<=', Carbon::createFromFormat('d-m-Y', $request->end_date)->format('Y-m-d'));
                })->where(function ($query) {
                        $query->where('pregnancy_diagnosises.pd1_result', 'pregnant')
                            ->orWhere('pregnancy_diagnosises.pd2_result', 'pregnant');
                    })
                    ->where('employees.sex',$request->sex)
                    ->whereIn('employees.organization_id',$organizationId)->whereIn('employees.herd_id',$herdId)->whereIn('employees.lot_id',$lotId)
                    ->whereIn('employees.status',$status)
                    ->select('employees.*',DB::raw('count(pregnancy_diagnosises.id) AS total_pregnant'),'pregnancy_diagnosises.expected_calving')
                    ->groupBy('pregnancy_diagnosises.employee_id')
                    ->orderBy('tag_id')
                    ->where('disposal_status', 0)
                    ->get();
            }elseif($request->status == 'adult' && $request->adult_status == 'milking'){
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::Join('milk_productions',function($join) use ($request) {
                    $join->on('employees.id','=','milk_productions.employee_id')
                        ->whereDate('milk_productions.date', '>=', Carbon::createFromFormat('d-m-Y', $request->start_date)->format('Y-m-d'))->whereDate('milk_productions.date', '<=', Carbon::createFromFormat('d-m-Y', $request->end_date)->format('Y-m-d'));
                })->where('employees.sex',$request->sex)
                    ->whereIn('employees.organization_id',$organizationId)->whereIn('employees.herd_id',$herdId)->whereIn('employees.lot_id',$lotId)
                    ->whereIn('employees.status',[$status, 'pregnant_milking'])
                    ->select('employees.*',DB::raw('sum(milk_productions.total_milk) AS total_milk'),DB::raw('count(milk_productions.id) AS total_day'))
                    ->groupBy('milk_productions.employee_id')
                    ->orderBy('tag_id')
                    ->where('disposal_status', 0)
                    ->get();
            }elseif($request->status == 'adult' && $request->adult_status == 'dry'){
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::Join('dry_offs',function($join) use ($request) {
                    $join->on('employees.id','=','dry_offs.employee_id')
                        ->whereDate('dry_offs.created_at', '>=', Carbon::createFromFormat('d-m-Y', $request->start_date)->format('Y-m-d'))->whereDate('dry_offs.created_at', '<=', Carbon::createFromFormat('d-m-Y', $request->end_date)->format('Y-m-d'));
                })->where('employees.sex',$request->sex)
                    ->whereIn('employees.organization_id',$organizationId)->whereIn('employees.herd_id',$herdId)->whereIn('employees.lot_id',$lotId)
                    ->whereIn('employees.status',$status)
                    ->select('employees.*',DB::raw('count(dry_offs.id) AS total_dry'),'dry_offs.dry_date')
                    ->groupBy('dry_offs.employee_id')
                    ->orderBy('tag_id')
                    ->where('disposal_status', 0)
                    ->get();
            }elseif($request->status == 'adult' && $request->adult_status == 'calving'){
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::Join('calvings',function($join) use ($request) {
                    $join->on('employees.id','=','calvings.employee_id')
                        ->whereDate('calvings.calving_date', '>=', Carbon::createFromFormat('d-m-Y', $request->start_date)->format('Y-m-d'))
                        ->whereDate('calvings.calving_date', '<=', Carbon::createFromFormat('d-m-Y', $request->end_date)->format('Y-m-d'));
                })->where('employees.sex',$request->sex)
                    ->whereIn('employees.organization_id',$organizationId)->whereIn('employees.herd_id',$herdId)->whereIn('employees.lot_id',$lotId)
                    ->whereIn('employees.status',$status)
                    ->select('employees.*',DB::raw('count(calvings.id) AS total_calving'),'calvings.calving_date')
                    ->groupBy('calvings.employee_id')
                    ->orderBy('tag_id')
                    ->where('disposal_status', 0)
                    ->get();
            }elseif($request->status == 'adult' && $request->adult_status == 'open_unbred'){
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::where('employees.sex',$request->sex)
                    ->where('employees.status',$request->adult_status)
                    ->whereIn('employees.organization_id',$organizationId)->whereIn('employees.herd_id',$herdId)->whereIn('employees.lot_id',$lotId)
                    ->where('disposal_status', 0)
                    ->orderBy('tag_id')
                    ->get();
            }elseif($request->status == 'adult' && $request->adult_status == 'open_bred'){
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::leftJoin('artificial_inseminations',function($join) use ($request) {
                    $join->on('employees.id','=','artificial_inseminations.employee_id')
                        ->whereDate('artificial_inseminations.created_at', '>=', Carbon::createFromFormat('d-m-Y', $request->start_date)->format('Y-m-d'))->whereDate('artificial_inseminations.created_at', '<=', Carbon::createFromFormat('d-m-Y', $request->end_date)->format('Y-m-d'));
                })->where('employees.sex',$request->sex)
                    ->where('employees.status',$request->adult_status)
                    ->whereIn('employees.organization_id',$organizationId)->whereIn('employees.herd_id',$herdId)->whereIn('employees.lot_id',$lotId)
                    ->where('employees.status',$request->adult_status)
                    ->where('employees.status', '!=', 'pregnant')
                    ->select('employees.*',DB::raw('count(artificial_inseminations.id) AS total_heat'),DB::raw('max(artificial_inseminations.heat_date) AS heat_date'))
                    ->groupBy('artificial_inseminations.employee_id')
                    ->orderBy('tag_id')
                    ->where('disposal_status', 0)
                    ->get();
            }elseif($request->sex == 'male' && $request->status == 'reproduction_stage' && empty($request->adult_status)){
                $employees = Employee::where('sex',$request->sex)->whereIn('organization_id',$organizationId)->whereIn('herd_id',$herdId)->whereIn('lot_id',$lotId)
                    ->whereIn('status',$status)->where('disposal_status', 0)
                    ->whereDate('created_at', '>=', $start_date)->whereDate('created_at', '<=', $end_date)
                    ->orderBy('tag_id')
                    ->get();
            }elseif($request->sex == 'male' && $request->status == 'bull' && empty($request->adult_status)){
                $employees = Employee::where('sex',$request->sex)->whereIn('organization_id',$organizationId)->whereIn('herd_id',$herdId)->whereIn('lot_id',$lotId)
                    ->whereIn('status',$status)->where('disposal_status', 0)
                    ->whereDate('created_at', '>=', $start_date)->whereDate('created_at', '<=', $end_date)
                    ->orderBy('tag_id')
                    ->get();
            }else{
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::where('sex',$request->sex)->where('disposal_status', 0)->whereIn('organization_id',$organizationId)->whereIn('herd_id',$herdId)->whereIn('lot_id',$lotId)
                    ->whereIn('status',$status)->whereDate('created_at', '>=', $start_date)->whereDate('created_at', '<=', $end_date)->orderBy('tag_id')->get();
            }


        }else{
            if($request->status == 'adult' && $request->adult_status == 'pregnant'){
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::Join('pregnancy_diagnosises',function($join) use ($request) {
                    $join->on('employees.id','=','pregnancy_diagnosises.employee_id');
                })
                    ->where(function ($query) {
                        $query->where('pregnancy_diagnosises.pd1_result', 'pregnant')
                            ->orWhere('pregnancy_diagnosises.pd2_result', 'pregnant');
                    })
                    ->where('employees.sex',$request->sex)
                    ->whereIn('employees.organization_id',$organizationId)->whereIn('employees.herd_id',$herdId)->whereIn('employees.lot_id',$lotId)
                    ->whereIn('employees.status',$status)
                    ->select('employees.*',DB::raw('count(pregnancy_diagnosises.id) AS total_pregnant'),'pregnancy_diagnosises.expected_calving')
                    ->groupBy('pregnancy_diagnosises.employee_id')
                    ->orderBy('tag_id')
                    ->where('disposal_status', 0)
                    ->get();
            }elseif($request->status == 'adult' && $request->adult_status == 'milking'){
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::Join('milk_productions',function($join) use ($request) {
                    $join->on('employees.id','=','milk_productions.employee_id');
                })
                    ->where('employees.sex',$request->sex)
                    ->whereIn('employees.organization_id',$organizationId)->whereIn('employees.herd_id',$herdId)->whereIn('employees.lot_id',$lotId)
                    ->whereIn('employees.status',$status)
                    ->select('employees.*',DB::raw('sum(milk_productions.total_milk) AS total_milk'),DB::raw('count(milk_productions.id) AS total_day'))
                    ->groupBy('milk_productions.employee_id')
                    ->orderBy('tag_id')
                    ->where('disposal_status', 0)
                    ->get();
            }elseif($request->status == 'adult' && $request->adult_status == 'dry'){
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::Join('dry_offs',function($join) use ($request) {
                    $join->on('employees.id','=','dry_offs.employee_id');
                })
                    ->where('employees.sex',$request->sex)
                    ->whereIn('employees.organization_id',$organizationId)->whereIn('employees.herd_id',$herdId)->whereIn('employees.lot_id',$lotId)
                    ->whereIn('employees.status',$status)
                    ->select('employees.*',DB::raw('count(dry_offs.id) AS total_dry'),'dry_offs.dry_date')
                    ->groupBy('dry_offs.employee_id')
                    ->orderBy('tag_id')
                    ->where('disposal_status', 0)
                    ->get();
            }elseif($request->status == 'adult' && $request->adult_status == 'calving'){
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::Join('calvings',function($join) use ($request) {
                    $join->on('employees.id','=','calvings.employee_id');
                })
                    ->where('employees.sex',$request->sex)
                    ->whereIn('employees.organization_id',$organizationId)->whereIn('employees.herd_id',$herdId)->whereIn('employees.lot_id',$lotId)
                    ->whereIn('employees.status',$status)
                    ->select('employees.*',DB::raw('count(calvings.id) AS total_calving'),'calvings.calving_date')
                    ->groupBy('calvings.employee_id')
                    ->orderBy('tag_id')
                    ->where('disposal_status', 0)
                    ->get();
            }elseif($request->status == 'adult' && $request->adult_status == 'open_unbred'){
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::where('employees.sex',$request->sex)
                    ->where('employees.status',$request->adult_status)
                    ->whereIn('employees.organization_id',$organizationId)->whereIn('employees.herd_id',$herdId)->whereIn('employees.lot_id',$lotId)
                    ->where('disposal_status', 0)
                    ->orderBy('tag_id')
                    ->get();
            }elseif($request->status == 'adult' && $request->adult_status == 'open_bred'){
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::leftJoin('artificial_inseminations',function($join) use ($request) {
                    $join->on('employees.id','=','artificial_inseminations.employee_id');
                })
                    ->where('employees.sex',$request->sex)
                    ->where('employees.status',$request->adult_status)
                    ->whereIn('employees.organization_id',$organizationId)->whereIn('employees.herd_id',$herdId)->whereIn('employees.lot_id',$lotId)
                    ->where('employees.status',$request->adult_status)
                    ->where('employees.status', '!=', 'pregnant')
                    ->select('employees.*',DB::raw('count(artificial_inseminations.id) AS total_heat'),DB::raw('max(artificial_inseminations.heat_date) AS heat_date'))
                    ->groupBy('artificial_inseminations.employee_id')
                    ->orderBy('tag_id')
                    ->where('disposal_status', 0)
                    ->get();
            }elseif($request->sex == 'male' && $request->status == 'reproduction_stage' && empty($request->adult_status)){
                $employees = Employee::where('sex',$request->sex)->whereIn('organization_id',$organizationId)->whereIn('herd_id',$herdId)->whereIn('lot_id',$lotId)
                    ->whereIn('status',$status)->where('disposal_status', 0)
                    ->orderBy('tag_id')
                    ->get();
            }elseif($request->sex == 'male' && $request->status == 'bull' && empty($request->adult_status)){
                $employees = Employee::where('sex',$request->sex)->whereIn('organization_id',$organizationId)->whereIn('herd_id',$herdId)->whereIn('lot_id',$lotId)
                    ->whereIn('status',$status)->where('disposal_status', 0)
                    ->orderBy('tag_id')
                    ->get();
            }else{
                $open_unbred_employees = $open_bred_employees =  $pregnant_employees =  $calving_employees =  $milking_employees =  $dry_employees = null;
                $employees = Employee::where('sex',$request->sex)->whereIn('organization_id',$organizationId)->whereIn('herd_id',$herdId)->whereIn('lot_id',$lotId)
                    ->whereIn('status',$status)->where('disposal_status', 0)
                    ->orderBy('tag_id')
                    ->get();
            }
        }

        /**
         * Process pregnant, open bred, open unbred, heifer, calf, milking, dry etc employee data.
         */
        $pregnantEmployeesData = [];
        $openBredEmployeesData = [];
        $openUnBredEmployeesData = [];
        $calvingEmployeesData = [];
        $milkingEmployeesData = [];
        $dryOffEmployeesData = [];
        $calving_interval = \App\Models\ActionPerameter::get('expected_calving');

        foreach ($employees as $key => $item) {
            $expected_calving = null;
            if ($item['id']) {
                $ai_employee = \App\Models\ArtificialInsemination::where('employee_id', $item['id'])->get()->last();
                if($ai_employee) {
                    $expected_calving =  \Carbon\Carbon::parse($ai_employee['heat_date'])->addDays($calving_interval)->format('d-m-Y');
                }else {
                    $expected_calving =  \Carbon\Carbon::parse($item['ai_date'])->addDays($calving_interval)->format('d-m-Y');
                }
            }
            $pregnantEmployeesData[] = [
                'tag_id'              => $item->tag->uid,
                'age'                 => ($item->birth_date) ? \Carbon\Carbon::parse($item->birth_date)->diff(\Carbon\Carbon::now())->format('%y Y, %m M, %d D') : 'N/A',
                'totalPregnancy'      => ($item->total_pregnant) ? $item->total_pregnant : '0',
                'expectedCalvingDate' => $expected_calving,
            ];
            $openBredEmployeesData[] = [
                'tag_id'     => $item->tag->uid,
                'age'        => ($item->birth_date) ? \Carbon\Carbon::parse($item->birth_date)->diff(\Carbon\Carbon::now())->format('%y Y, %m M, %d D') : 'N/A',
                'totalHeat'  => ($item->total_heat) ? $item->total_heat : '0',
                'heatDate'   => ($item->heat_date) ? \Carbon\Carbon::createFromFormat('Y-m-d', $item->heat_date)->format('d-m-Y') : 'N/A',
            ];
            $openUnBredEmployeesData[] = [
                'tag_id'     => $item->tag->uid,
                'age'        => ($item->birth_date) ? \Carbon\Carbon::parse($item->birth_date)->diff(\Carbon\Carbon::now())->format('%y Y, %m M, %d D') : 'N/A',
            ];
            $calvingEmployeesData[] = [
                'tag_id'            => $item->tag->uid,
                'age'               => ($item->birth_date) ? \Carbon\Carbon::parse($item->birth_date)->diff(\Carbon\Carbon::now())->format('%y Y, %m M, %d D') : 'N/A',
                'gender'            => ($item->calf_sex) ? $item->calf_sex : 'N/A',
                'lastCalvingStatus' => ($item->last_calving_status) ? $item->last_calving_status : 'N/A',
                'totalCalving'      => ($item->total_calving) ? $item->total_calving + ($item->no_of_calving ? $item->no_of_calving : 0) : (($item->no_of_calving) ? $item->no_of_calving : '0'),
                'lastCalvingDate'   => ($item->calving_date) ? \Carbon\Carbon::createFromFormat('Y-m-d', $item->calving_date)->format('d-m-Y') : 'N/A',
            ];
            $milkingEmployeesData[] = [
                'tag_id'             => $item->tag->uid,
                'age'                => ($item->birth_date) ? \Carbon\Carbon::parse($item->birth_date)->diff(\Carbon\Carbon::now())->format('%y Y, %m M, %d D') : 'N/A',
                'lactaionPeriod'     => ($item->total_day) ? $item->total_day : '0',
                'totalLactaionYield' => ($item->total_milk) ? number_format($item->total_milk, 2) : '0',
                'avgLactaionYield'   => ($item->total_calving) ? $item->total_calving + ($item->no_of_calving ? $item->no_of_calving : 0) : (($item->no_of_calving) ? $item->no_of_calving : '0'),
            ];
            $dryOffEmployeesData[] = [
                'tag_id'      => $item->tag->uid,
                'age'         => ($item->birth_date) ? \Carbon\Carbon::parse($item->birth_date)->diff(\Carbon\Carbon::now())->format('%y Y, %m M, %d D') : 'N/A',
                'totalDry'    => ($item->total_dry) ? $item->total_dry : '0',
                'lastDryDate' => ($item->dry_date) ? \Carbon\Carbon::createFromFormat('Y-m-d', $item->dry_date)->format('d-m-Y') : 'N/A',
            ];
        }

        $generalReportData = [
//            'organizations'             => $organizations,
//            'organizationSelected'      => $organizationSelected,
//            'herdSelected'      => $herdSelected,
//            'lotSelected'       => $lotSelected,
            'employees'           => $employees,
            'openUnbredEmployees' => $openUnBredEmployeesData,
            'openBredEmployees'   => $openBredEmployeesData,
            'pregnantEmployees'   => $pregnantEmployeesData,
            'calvingEmployees'    => $calvingEmployeesData,
            'milkingEmployees'    => $milkingEmployeesData,
            'dryEmployees'        => $dryOffEmployeesData,
        ];
        if ($generalReportData) {
            return ['result' => 1, 'message' => 'Resources data found.', 'data' => new JsonResponse($generalReportData)];
        }else {
            return response()->json(['result' => 0, 'message' => 'No data found.']);
        }
    }
}
