public function fabrics_newReceiveEntry_Store(Request $request) { // // dd($request->all()); // // Start with the query builder instance // $query = FabricsChallanDataInput::query(); // // Add your conditional filters // if (request('buyer_id')) { // $query->where('buyer_id', 'LIKE', '%' . request('buyer_id') . '%'); // } // if (request('order_style')) { // $query->where('order_style', 'LIKE', '%' . request('order_style') . '%'); // } // if (request('book_no')) { // $query->where('book_no', 'LIKE', '%' . request('book_no') . '%'); // } // if (request('season')) { // $query->where('season', 'LIKE', '%' . request('season') . '%'); // } // // Apply the orderBy method and get the results // $fabricsChallanDataInputs = $query->distinct()->get(); // // dd($fabricsChallanDataInputs); // $fabrics_receive = FabricsReceive::whereIN('book_no', $fabricsChallanDataInputs->pluck('book_no')) // ->where('buyer_id', $fabricsChallanDataInputs->pluck('buyer_id')) // ->where('order_style', $fabricsChallanDataInputs->pluck('order_style')) // ->where('batch_id', $fabricsChallanDataInputs->pluck('batch_id')) // ->groupBy('book_no', 'buyer_id', 'order_style', 'batch_id', 'item_name', 'color_name', 'lot', 'dia', 'gsm', 'cons_dz')->get(); // dd($fabrics_receive); $query = FabricsChallanDataInput::query(); // Add your conditional filters if (request('buyer_id')) { $query->where('buyer_id', 'LIKE', '%' . request('buyer_id') . '%'); } if (request('order_style')) { $query->where('order_style', 'LIKE', '%' . request('order_style') . '%'); } if (request('book_no')) { $query->where('book_no', 'LIKE', '%' . request('book_no') . '%'); } if (request('season')) { $query->where('season', 'LIKE', '%' . request('season') . '%'); } // Get the results $fabricsChallanDataInputs = $query->get(); $results = []; // Iterate through each FabricsChallanDataInput record foreach ($fabricsChallanDataInputs as $fabricsChallanDataInput) { $fabrics_receive = FabricsReceive::where('book_no', $fabricsChallanDataInput->book_no) ->where('buyer_id', $fabricsChallanDataInput->buyer_id) ->where('order_style', $fabricsChallanDataInput->order_style) ->where('batch_id', $fabricsChallanDataInput->batch_id) ->groupBy( 'book_no', 'buyer_id', 'order_style', 'batch_id', 'item_name', 'color_name', 'lot', 'dia', 'gsm', 'cons_dz', 'rack_no', 'self_bin_no', ) ->selectRaw(' book_no, buyer_id, order_style, batch_id, item_name, color_name, lot, dia, gsm, cons_dz, rack_no, self_bin_no, SUM(receive_qty) as total_receive_qty, MAX(booking_qty) as last_booking_qty ') ->get(); $results = array_merge($results, $fabrics_receive->toArray()); } $fabrics_receive = $results; // dd($fabrics_receive); try { if ($fabricsChallanDataInputs->count() > 0) { // Check if the batch ID exists if (Auth::user()->role_id == 1) { $suppliers = Supplier::all(); $uoms = UMO::all(); } else { $suppliers = Supplier::where( 'company_id', Auth::user()->company_id )->get(); $uoms = UMO::where('company_id', Auth::user()->company_id)->get(); } return view('backend.library.dataEntry.newReceiveEntry', compact('fabricsChallanDataInputs', 'suppliers', 'uoms', 'fabrics_receive')); } else { return redirect()->route('fabricsChallanDataInputs.index')->withErrors('No data found!'); } } catch (\Illuminate\Database\QueryException $e) { return redirect()->back()->withErrors(['error' => $e->getMessage()]); } } public function new_Receive_submit(Request $request) { try { // dd($request->all()); $request->validate([ 'book_no.*' => 'required', 'color_name.*' => 'required', 'rack_no.*' => 'required', 'self_bin_no.*' => 'required', 'uom.*' => 'required', 'booking_qty.*' => 'required', 'receive_qty.*' => 'required', ]); // Loop through each item in the input arrays foreach ($request->book_no as $index => $item) { // Create a new instance of the FabricsReceive model $fabrics_receive = new FabricsReceive(); // Set the attributes for the model based on the input data $fabrics_receive->division_id = $request->division_id; $fabrics_receive->division_name = $request->division_name; $fabrics_receive->company_id = $request->company_id; $fabrics_receive->company_name = $request->company_name; $fabrics_receive->buyer_id = $request->buyer_id; $fabrics_receive->buyer_name = $request->buyer_name; $fabrics_receive->batch_id = $request->batch_id; $fabrics_receive->order_style = ucwords($request->order_style); $fabrics_receive->book_no = $item; $fabrics_receive->item_name = ItemList::findOrFail($item)->name; $fabrics_receive->color_name = ucwords($request->color_name[$index]); $fabrics_receive->dia = $request->dia; $fabrics_receive->lot = $request->lot; $fabrics_receive->receive_date = $request->receive_date; $fabrics_receive->chalan_no = ucwords($request->chalan_no[$index]); $fabrics_receive->rack_no = ucwords($request->rack_no[$index]); $fabrics_receive->self_bin_no = ucwords($request->self_bin_no[$index]); $fabrics_receive->gsm = $request->gsm[$index]; $fabrics_receive->cons_dz = UMO::findOrFail($request->gsm[$index])->name; $fabrics_receive->booking_qty = $request->booking_qty[$index]; $fabrics_receive->receive_qty = $request->receive_qty[$index]; $fabrics_receive->remarks = ucwords($request->remarks[$index]); $fabrics_receive->created_by = Auth::user()->name; $fabrics_receive->created_date = now(); // Save the model to the database $fabrics_receive->save(); // Update the FabricsChallanDataInput model with the received quantity FabricsChallanDataInput::where('buyer_id', $request->buyer_id)->where('order_style', $request->order_style)->where('book_no', $item) ->update([ 'receive_qty' => DB::raw('receive_qty + ' . $request->receive_qty[$index]), ]); } // Redirect return redirect()->route('fabricsChallanDataInputs.index')->withMessage('Received Item List Booking and Received updated successfully!'); } catch (\Illuminate\Database\QueryException $e) { return redirect()->back()->withErrors(['error' => $e->getMessage()]); } } public function newIssue(Request $request) { try { // Start with the query builder instance $query = FabricsChallanDataInput::query(); // Add your conditional filters if (request('buyer_id')) { $query->where('buyer_id', 'LIKE', '%' . request('buyer_id') . '%'); } if (request('order_style')) { $query->where('order_style', 'LIKE', '%' . request('order_style') . '%'); } if (request('season')) { $query->where('season', 'LIKE', '%' . request('season') . '%'); } // Get the results $fabricsChallanDataInputs = $query->get(); $results = []; // Iterate through each FabricsChallanDataInput record foreach ($fabricsChallanDataInputs as $fabricsChallanDataInput) { $fabrics_receive = FabricsReceive::where('book_no', $fabricsChallanDataInput->book_no) ->where('buyer_id', $fabricsChallanDataInput->buyer_id) ->where('order_style', $fabricsChallanDataInput->order_style) ->where('batch_id', $fabricsChallanDataInput->batch_id) ->groupBy( 'book_no', 'buyer_id', 'buyer_name', 'order_style', 'batch_id', 'item_name', 'color_name', 'lot', 'dia', 'gsm', 'cons_dz', 'rack_no', 'self_bin_no', ) ->selectRaw(' book_no, buyer_id, buyer_name, order_style, batch_id, item_name, color_name, lot, dia, gsm, cons_dz, rack_no, self_bin_no, SUM(receive_qty) as total_receive_qty, MAX(booking_qty) as last_booking_qty ') ->get(); $results = array_merge($results, $fabrics_receive->toArray()); } $fabrics_receive = $results; // dd($fabrics_receive); // dd($fabricsChallanDataInputs); if ($fabricsChallanDataInputs->count() > 0) { return view('backend.library.dataEntry.newIssue', compact('fabricsChallanDataInputs', 'fabrics_receive')); } else { return redirect()->route('fabricsChallanDataInputs.index')->withErrors('No data found!'); } } catch (\Illuminate\Database\QueryException $e) { return redirect()->back()->withErrors(['error' => $e->getMessage()]); } } public function Issue_Store(Request $request) { try { // dd($request->all()); $request->validate([ 'item_name.*' => 'required', 'issue_qty.*' => 'required', 'issue_balance.*' => 'required', ]); // Loop through each item in the input arrays foreach ($request->item_name as $index => $item) { // Create a new instance of the FabricsReceive model $fabrics_issues = new FabricsIssue(); //if issue qty is 0 or null then skip the loop if ($request->issue_qty[$index] == 0 || $request->issue_qty[$index] == null) { continue; } $fabrics_receive_data = FabricsReceive::where('book_no', $item)->where('buyer_id', $request->buyer_id)->where('order_style', $request->order_style)->where('batch_id', $request->batch_id)->where('color_name', $request->color_name[$index])->first(); // Set the attributes for the model based on the input data $fabrics_issues->division_id = $fabrics_receive_data->division_id; $fabrics_issues->division_name = $fabrics_receive_data->division_name; $fabrics_issues->company_id = $fabrics_receive_data->company_id; $fabrics_issues->company_name = $fabrics_receive_data->company_name; $fabrics_issues->buyer_id = $request->buyer_id; $fabrics_issues->buyer_name = Buyer::findorFail($request->buyer_id)->name; $fabrics_issues->batch_id = $request->batch_id; $fabrics_issues->order_style = ucwords($request->order_style); $fabrics_issues->book_no = $item; $fabrics_issues->item_name = ItemList::findOrFail($item)->name; $fabrics_issues->lot = $fabrics_receive_data->lot; $fabrics_issues->dia = $fabrics_receive_data->dia; $fabrics_issues->gsm = $fabrics_receive_data->gsm; $fabrics_issues->cons_dz = $fabrics_receive_data->cons_dz; $fabrics_issues->color_name = ucwords($request->color_name[$index]); $fabrics_issues->self_bin_no = ucwords($request->self_bin_no[$index]); $fabrics_issues->rack_no = ucwords($request->rack_no[$index]); $fabrics_issues->requsition_no = ucwords($request->requsition_no); $fabrics_issues->issue_date = now()->format('Y-m-d'); $fabrics_issues->remarks = ucwords($request->remarks[$index]); $fabrics_issues->issue_qty = $request->issue_qty[$index]; $fabrics_issues->in_hand_qty = $request->issue_balance[$index]; $fabrics_issues->created_by = Auth::user()->name; $fabrics_issues->created_date = now(); // Save the model to the database $fabrics_issues->save(); // Update the FabricsChallanDataInput model with the issue quantity FabricsChallanDataInput::where('buyer_id', $request->buyer_id)->where('order_style', $request->order_style)->where('book_no', $item) ->update([ 'issue_qty' => DB::raw('issue_qty + ' . $request->issue_qty[$index]), ]); } // Redirect return redirect()->route('fabricsChallanDataInputs.index')->withMessage('Received Item List Booking and Received updated successfully!'); } catch (\Illuminate\Database\QueryException $e) { return redirect()->back()->withErrors(['error' => $e->getMessage()]); } } public function history() { try { // Start with the query builder instance $query = FabricsChallanDataInput::query(); // Add your conditional filters if (request('buyer_id')) { $query->where('buyer_id', 'LIKE', '%' . request('buyer_id') . '%'); } if (request('order_style')) { $query->where('order_style', 'LIKE', '%' . request('order_style') . '%'); } if (request('season')) { $query->where('season', 'LIKE', '%' . request('season') . '%'); } // Apply the orderBy method and get the results $fabricsChallanDataInputs = $query->orderBy('id', 'desc')->get(); if ($fabricsChallanDataInputs->count() > 0) { return view('backend.library.dataEntry.history', compact('fabricsChallanDataInputs')); } else { return redirect()->route('fabricsChallanDataInputs.index')->withErrors('No data found!'); } } catch (\Illuminate\Database\QueryException $e) { return redirect()->back()->withErrors(['error' => $e->getMessage()]); } } public function FabricsReceiveHistory(Request $request) { try { $fabricsChallanDataInputs = FabricsReceive::select('division_name', 'company_name', 'buyer_name', 'dia', 'item_name', 'batch_id', 'color_name', 'order_style', 'cons_dz', 'chalan_no', 'receive_date', 'rack_no', 'self_bin_no', 'booking_qty', 'receive_qty', 'rcv_bal_qty', 'remarks') ->where('buyer_id', $request->buyer_id) ->where('order_style', $request->order_style) ->groupBy('division_name', 'company_name', 'buyer_name', 'dia', 'item_name', 'batch_id', 'color_name', 'order_style', 'cons_dz', 'chalan_no', 'receive_date', 'rack_no', 'self_bin_no', 'booking_qty', 'receive_qty', 'rcv_bal_qty', 'remarks') ->get(); if ($fabricsChallanDataInputs->count() > 0) { return view('backend.library.dataEntry.FabricsReceiveHistory', compact('fabricsChallanDataInputs')); } else { return redirect()->route('fabricsChallanDataInputs.index')->withErrors('No data found!'); } } catch (\Illuminate\Database\QueryException $e) { return redirect()->back()->withErrors(['error' => $e->getMessage()]); } } public function FabricsIssueHistory(Request $request) { try { $fabricsChallanDataInputs = FabricsIssue::select('division_name', 'company_name', 'buyer_name', 'dia', 'item_name', 'batch_id', 'color_name', 'order_style', 'cons_dz', 'requsition_no', 'issue_date', 'remarks', 'issue_qty', 'in_hand_qty') ->where('buyer_id', $request->buyer_id) ->where('order_style', $request->order_style) ->groupBy('division_name', 'company_name', 'buyer_name', 'dia', 'item_name', 'batch_id', 'color_name', 'order_style', 'cons_dz', 'requsition_no', 'issue_date', 'remarks', 'issue_qty', 'in_hand_qty') ->get(); if ($fabricsChallanDataInputs->count() > 0) { return view('backend.library.dataEntry.FabricsIssueHistory', compact('fabricsChallanDataInputs')); } else { return redirect()->route('fabricsChallanDataInputs.index')->withErrors('No data found!'); } } catch (\Illuminate\Database\QueryException $e) { return redirect()->back()->withErrors(['error' => $e->getMessage()]); } } public function fabricsChallanDataInputs_edit(Request $request) { try { // dd($request->all()); $fabricsChallanDataInput = FabricsChallanDataInput::where('buyer_id', $request->buyer_id)->where('order_style', $request->order_style)->where('season', $request->season)->get(); $batch_id = FabricsChallanDataInput::where('buyer_id', $request->buyer_id)->where('order_style', $request->order_style)->where('season', $request->season)->get()->pluck('batch_id')->first(); $FabricsIssue = FabricsIssue::where('buyer_id', $request->buyer_id) ->where('order_style', $request->order_style) ->where('batch_id', $batch_id) ->get(); return view('backend.library.dataEntry.edit', compact('fabricsChallanDataInput', 'FabricsIssue')); } catch (\Illuminate\Database\QueryException $e) { return redirect()->back()->withErrors(['error' => $e->getMessage()]); } } public function fabricsChallanDataInputs_update(Request $request) { try { // dd($request->all()); $fabricsChallanDataInput = FabricsChallanDataInput::where('buyer_id', $request->buyer_id)->where('order_style', $request->style_id)->where('season', $request->season)->where('receive_date', $request->old_receive_date)->where('chalan_no', $request->old_chalan_no)->get(); $fabrics_receive = FabricsReceive::where('buyer_id', $request->buyer_id)->where('order_style', $request->style_id)->where('receive_date', $request->old_receive_date)->where('chalan_no', $request->old_chalan_no)->get(); // dd($fabricsChallanDataInput); foreach ($fabricsChallanDataInput as $fabricsChallanDataInput) { $fabricsChallanDataInput->order_qty = $request->order_qty; $fabricsChallanDataInput->receive_date = $request->receive_date; $fabricsChallanDataInput->chalan_no = ucwords($request->chalan_no); $fabricsChallanDataInput->save(); } foreach ($fabrics_receive as $fabrics_receive) { $fabrics_receive->chalan_no = ucwords($request->chalan_no); $fabrics_receive->save(); } return redirect()->route('fabricsChallanDataInputs.index')->withMessage('Fabrics Challan Data Input updated successfully!'); } catch (\Illuminate\Database\QueryException $e) { return redirect()->back()->withErrors(['error' => $e->getMessage()]); } } public function itemDelete($item) { // Get the specific FabricsIssue entry you want to delete $fabrics_issue = FabricsIssue::find($item); // dd($fabrics_issue); // Find the latest old fabrics issue with the same criteria $latest_old_fabrics_issue = FabricsIssue::where('division_id', $fabrics_issue->division_id) ->where('company_id', $fabrics_issue->company_id) ->where('buyer_id', $fabrics_issue->buyer_id) ->where('book_no', $fabrics_issue->book_no) ->where('lot', $fabrics_issue->lot) ->where('gsm', $fabrics_issue->gsm) ->where('order_style', $fabrics_issue->order_style) ->where('id', '<', $item) // Ensuring it's an older entry ->latest() // Get the latest among the older entries ->first(); if ($latest_old_fabrics_issue) { // Update the issue_qty and in_hand_qty with values from the found latest old fabrics issue $fabrics_issue->issue_qty = $latest_old_fabrics_issue->issue_qty; $fabrics_issue->in_hand_qty = $latest_old_fabrics_issue->in_hand_qty; // Save the changes $fabrics_issue->save(); } // Delete the specified FabricsIssue entry FabricsIssue::destroy($item); return redirect()->route('fabricsChallanDataInputs.index')->with('message', 'issue and related data deleted successfully!'); } public function get_Order_Style($buyer_id) { try { $orderStyles = FabricsChallanDataInput::where('buyer_id', $buyer_id) ->select('order_style') ->distinct() ->orderBy('order_style') // Sort order styles ->pluck('order_style'); return response()->json($orderStyles); } catch (\Exception $e) { return response()->json(['error' => $e->getMessage()], 500); } } public function getSeason($buyer_id, $order_style) { try { $seasons = FabricsChallanDataInput::where('buyer_id', $buyer_id) ->where('order_style', $order_style) ->distinct() ->orderBy('season') // Sort seasons ->pluck('season'); return response()->json($seasons); } catch (\Exception $e) { return response()->json(['error' => $e->getMessage()], 500); } }