/
home
/
sjslayjy
/
public_html
/
ccbfsoution
/
app
/
Http
/
Controllers
/
Admin
/
Upload File
HOME
<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Auth; class PostIrrigationController extends Controller { public function index(Request $request) { $user = Auth::user(); $role = $user->role; $defaultSiteId = $user->site_name; // Handle selected site $selectedSiteId = $request->get('site_id', $defaultSiteId); // Site list for dropdown (admin only) $sites = ($role == 1) ? DB::table('master_sites')->pluck('site_name', 'id') : DB::table('master_sites')->where('id', $defaultSiteId)->pluck('site_name', 'id'); // Blocks dropdown filter $blocksQuery = DB::table('post_irrigation'); if ($role != 1) { $blocksQuery->where('site_id', $defaultSiteId); } elseif ($request->filled('site_id')) { $blocksQuery->where('site_id', $selectedSiteId); } $blocks = $blocksQuery->select('block_name')->distinct()->orderBy('block_name')->get(); // Ensure block names are plain strings for the view $blocks = $blocks->pluck('block_name')->toArray(); // Convert collection of objects to an array of strings $selectedBlock = $request->input('block_name', null); // Changed to block_name to match your form $search = $request->input('search', ''); // Main data query $query = DB::table('post_irrigation') ->leftJoin('master_irrigation_types', 'post_irrigation.irrigation_type_id', '=', 'master_irrigation_types.id') ->leftJoin('water_sources', 'post_irrigation.water_source_id', '=', 'water_sources.id') ->leftJoin('users', 'post_irrigation.user_id', '=', 'users.id') ->select( 'post_irrigation.id', 'post_irrigation.block_name', 'post_irrigation.total_cost', 'post_irrigation.plot_name', 'post_irrigation.area', 'post_irrigation.irrigation_no', 'master_irrigation_types.type_name as irrigation_type', 'post_irrigation.date', 'water_sources.name as water_source', 'post_irrigation.capacity_lph as capacity', 'post_irrigation.day_ofter_swowing', 'post_irrigation.electricity_units', 'post_irrigation.electricity_cost', 'post_irrigation.hours_used as consumption_time', 'post_irrigation.unskilled', 'post_irrigation.semi_skilled_1', 'post_irrigation.semi_skilled_2', DB::raw('(post_irrigation.unskilled + post_irrigation.semi_skilled_1 + post_irrigation.semi_skilled_2) as man_power'), 'post_irrigation.major_maintenance', // Select as is for now 'users.name as user_name' ); // Apply site filter if ($role != 1) { $query->where('post_irrigation.site_id', $defaultSiteId); } elseif ($request->filled('site_id')) { $query->where('post_irrigation.site_id', $selectedSiteId); } // Block filter if ($selectedBlock) { $query->where('post_irrigation.block_name', $selectedBlock); } // Search filter if ($search) { $query->where(function ($q) use ($search) { $q->where('post_irrigation.plot_name', 'like', "%$search%") ->orWhere('post_irrigation.irrigation_no', 'like', "%$search%") ->orWhere('post_irrigation.major_maintenance', 'like', "%$search%") ->orWhere('users.name', 'like', "%$search%"); }); } $irrigationData = $query->orderBy('post_irrigation.date', 'desc') ->paginate(8) ->appends($request->all()); // Session filter if ($request->filled('session')) { $session = $request->session; switch ($session) { case 'kharif': $query->whereMonth('pre.date', '>=', 6) ->whereMonth('pre.date', '<=', 10); break; case 'rabi': $query->where(function($q) { $q->whereMonth('pre.date', '>=', 11) ->orWhereMonth('pre.date', '<=', 3); }); break; case 'zaid': $query->whereMonth('pre.date', '>=', 4) ->whereMonth('pre.date', '<=', 6); break; } } // Process major_maintenance after pagination $irrigationData->getCollection()->transform(function ($item) { if (isset($item->major_maintenance) && is_string($item->major_maintenance)) { $item->major_maintenance = json_decode($item->major_maintenance, true); } elseif (!is_array($item->major_maintenance)) { $item->major_maintenance = []; // Ensure it's an array if not already or null } return $item; }); return view('post-irrigation.index', compact( 'irrigationData', 'blocks', 'selectedBlock', 'search', 'sites', 'selectedSiteId', 'role' )); } }