/
home
/
sjslayjy
/
public_html
/
assets
/
app
/
Http
/
Controllers
/
Upload File
HOME
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Auth; use App\LocalUserMapping; use App\SparePartsMaster; use App\TrcStock; use App\HistoryTrcStock; use App\SparePartsInStock; use App\StockInCircleStore; use App\HistoryOfStockInCircleStore; use DB; use Maatwebsite\Excel\Facades\Excel; use App\Exports\AllSpareExport; class SpareController extends Controller { public $data; public function get_common(){ if(Auth::check()){ $user = Auth::user(); $this->data['userInfo'] = LocalUserMapping::where('LoginID',$user->loginId)->first(); } } public function get_all_spare_parts(){ $this->get_common(); $all_spare_parts = DB::table('spare_parts_masters')->get(); // $all_spare_parts = DB::table('spare_parts_masters as i') // ->select('i.id', 'i.code', 'i.hsn','i.name' ,'i.description','i.uom','i.type','i.rate','i.repaired_rate','i.gst_slab_rates','i.category as cat','i.product_type','i.model','i.specification','i.model','u.UOM as uom', 'c.name') // ->join('spare_category as c', 'c.id', 'i.category') // ->join('spart_value_type as u', 'u.id', 'i.uom') // // ->where('i.is_active', 1) // ->get(); $this->data = array_merge($this->data,['all_spare_parts'=>$all_spare_parts]); return view('admin.spare.spareParts',$this->data); } public function view_all_spares(){ $this->get_common(); $all_spare_parts = DB::table('spare_parts_masters')->get(); $this->data = array_merge($this->data,['all_spare_parts'=>$all_spare_parts]); return view('master.view-all-spare',$this->data); } public function export_all_spares() { $spare_parts = DB::table('spare_parts_masters')->get(); return Excel::download(new AllSpareExport($spare_parts), 'all-spare.xlsx'); } public function get_add_spare_part_page(){ $this->get_common(); $gst_slabs = DB::table('gst_slabs')->get(); $spart_value_types = DB::table('spart_value_type')->get(); $spare_category = DB::table('spare_category')->get(); $spare_product_category = DB::table('spare_product_category')->get(); $this->data = array_merge($this->data,['gst_slabs'=>$gst_slabs,'spart_value_types'=>$spart_value_types,'spare_category'=>$spare_category,'spare_product_categories'=>$spare_product_category]); return view('admin.spare.addSparePart',$this->data); } public function add_spare_part(Request $request){ $validatedData = $request->validate([ 'name' => 'required', 'hsn' => 'required', 'code' => 'required | unique:spare_parts_masters', 'description' => 'required', 'specification'=>'required', 'rate' => 'required', 'repaired_rate' => 'required', 'model'=>'required', 'gst_slab_rates' => 'required', 'uom' => 'required', 'category' => 'required', 'type' => 'required', 'productType' => 'required', ]); $spareCategory = \DB::table('spare_category') ->where('name', 'like', '%' . $request->category . '%') ->first(); $spareUom = \DB::table('spart_value_type') ->where('UOM', 'like', '%' . $request->uom . '%') ->first(); $spare = new SparePartsMaster(); $spare->name = $request->name; $spare->hsn = $request->hsn; $spare->code = $request->code; $spare->description = $request->description; $spare->specification = $request->specification; $spare->rate = $request->rate; $spare->repaired_rate = $request->repaired_rate; $spare->model=$request->model; $spare->gst_slab_rates = $request->gst_slab_rates; $spare->uom = $spareUom->id; $spare->category = $spareCategory->id; $spare->product_type = $request->productType; $spare->type = $request->type; //-------------To save data in SCM(items) table(Added on 26 Aug)--------- $apiUrl = "http://scm.aerialtelecom.in/api/item-list"; // Data to send in the POST request $postData = $request->all(); // Initialize cURL session $curl = curl_init(); // Set cURL options curl_setopt($curl, CURLOPT_URL, $apiUrl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_POST, true); // Set request method to POST curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postData)); // Set POST data // You can add more options like headers, authentication, etc. here // Execute cURL session and store the response $response = curl_exec($curl); // Check for cURL errors if(curl_errno($curl)) { echo 'Curl error: ' . curl_error($curl); } // Close cURL session curl_close($curl); $responseData = json_decode($response); if ($responseData->status == 200 && $responseData->flag == true) { if($spare->save()){ return redirect('admin/add-spare-part')->with('success','Spare parts has Added successfully.'); }else{ return redirect('admin/add-spare-part')->with('error','Something went wrong !'); } }else if($responseData->flag == false){ return redirect('admin/add-spare-part')->with('error','Something went wrong !'); } } public function get_edit_spare_part_page($id){ $is_spare_part = SparePartsMaster::where('id',$id)->count(); if($is_spare_part > 0){ $this->get_common(); $gst_slabs = DB::table('gst_slabs')->get(); $spart_value_types = DB::table('spart_value_type')->get(); $spare_category = DB::table('spare_category')->get(); $spare_product_category = DB::table('spare_product_category')->get(); $spare_part = SparePartsMaster::where('id',$id)->first(); $this->data = array_merge($this->data,['gst_slabs'=>$gst_slabs,'spart_value_types'=>$spart_value_types,'spare_category'=>$spare_category,'spare_part'=>$spare_part,'spare_product_categories'=>$spare_product_category]); return view('admin.spare.editSparePart',$this->data); }else{ return redirect('admin/all-spare-parts')->with('error','The spare part is not found in my database.'); } } public function update_spare_part(Request $request){ // dd($request->all()); $id = $request->spare_part_id; $validatedData = $request->validate([ // 'name' => 'required', 'hsn' => 'required', 'code' => 'required|unique:spare_parts_masters,code,'.$id.'id', 'description' => 'required', 'rate' => 'required', 'repaired_rate' => 'required', 'gst_slab_rates' => 'required', 'uom' => 'required', 'category' => 'required', 'type' => 'required', // 'productType' => 'required', ]); $spareCategory = \DB::table('spare_category')->where('name', 'like', '%' . $request->category . '%') ->first(); $spareUom = \DB::table('spart_value_type') ->where('UOM', 'like', '%' . $request->uom . '%') ->first(); $spare = SparePartsMaster::find($id); $spare->name = $request->name; $spare->hsn = $request->hsn; $spare->code = $request->code; $spare->description = $request->description; $spare->rate = $request->rate; $spare->repaired_rate = $request->repaired_rate; $spare->gst_slab_rates = $request->gst_slab_rates; $spare->uom = $spareUom->id; $spare->category = $spareCategory->id; $spare->product_type = $request->productType; $spare->type = $request->type; //-------------To save data in SCM(items) table(Added on 26 Aug)--------- $apiUrl = "http://scm.aerialtelecom.in/api/edit-item"; // Data to send in the POST request $postData = $request->all(); // Initialize cURL session $curl = curl_init(); // Set cURL options curl_setopt($curl, CURLOPT_URL, $apiUrl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_POST, true); // Set request method to POST curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postData)); // Set POST data // You can add more options like headers, authentication, etc. here // Execute cURL session and store the response $responseFromAPI = curl_exec($curl); // Check for cURL errors if(curl_errno($curl)) { echo 'Curl error: ' . curl_error($curl); } // Close cURL session curl_close($curl); $responseData = json_decode($responseFromAPI); if ($responseData->flag == true) { if($spare->save()){ return redirect('admin/edit-spare-part/'.$id)->with('success','Spare parts has Updated successfully.'); }else{ return redirect('admin/edit-spare-part/'.$id)->with('error','Something went wrong !'); } }else if($responseData->flag == false){ //dd($responseData); return redirect('admin/edit-spare-part/'.$id)->with('error',$responseData->message); } } public function delete_spare_part(Request $request){ $id = $request->spare_part_id; $spare = SparePartsMaster::find($id); if($spare->delete()){ return redirect('admin/edit-spare-part/'.$id)->with('success','Spare parts has Updated successfully.'); }else{ return redirect('admin/edit-spare-part/'.$id)->with('error','Something went wrong !'); } } public function import_spare(){ $this->get_common(); return view('admin.spare.import',$this->data); } public function spare_assigned_to_trc(){ $this->get_common(); $loginId = Auth::user()->loginId; $assigned_warehouses = DB::table('local_user_mapping') ->select('local_user_mapping.warehouseCode') ->where('LoginID', $loginId) ->first(); $warehouses = explode(',', $assigned_warehouses->warehouseCode); // $ware_houses = DB::table('ware_houses')->get(); $this->data = array_merge($this->data,['warehouses'=>$warehouses]); return view('admin.spare-assigned-to-trc',$this->data); } public function spare_at_trc(){ $this->get_common(); $loginId = Auth::user()->loginId; $assigned_warehouses = DB::table('local_user_mapping') ->select('local_user_mapping.warehouseCode') ->where('LoginID', $loginId) ->first(); $warehouses = explode(',', $assigned_warehouses->warehouseCode); // $ware_houses = DB::table('ware_houses')->get(); $spares = DB::table('trc_stocks')->join('spare_parts_masters','spare_parts_masters.code','trc_stocks.spare_code')->join('users','users.id','trc_stocks.added_by')->select('trc_stocks.*','spare_parts_masters.*','users.name')->where('trc_stocks.warehouse','SERPNT')->get(); $this->data = array_merge($this->data,['spares'=>$spares,'warehouses'=>$warehouses]); return view('admin.spare-at-trc',$this->data); } public function get_faulty_spare_of_warehouse(Request $request){ $warehouse = $request->warehouse; $spares = DB::table('trc_stocks')->join('spare_parts_masters','spare_parts_masters.code','trc_stocks.spare_code')->select('trc_stocks.*','spare_parts_masters.description')->where('trc_stocks.warehouse',$warehouse)->get(); return json_encode(['success'=>true, 'spares'=> $spares]); } public function assign_spare_to_trc(Request $request){ $user_id = Auth::user()->id; $warehouse = $request->warehouse; $is_valid = true; foreach ($request->items as $val) { $spcode = $val['code']; $stock_in_store = StockInCircleStore::where('spare_code',$spcode)->where('warehouse',$warehouse)->first(); if($stock_in_store != null){ if($stock_in_store->faulty_qty < $val['qty']){ $is_valid = false; $sp = $val['qty'].' Quantity '.$spcode." Faulty"; } }else{ $is_valid = false; $sp = $spcode." Faulty"; } } if($is_valid == true){ foreach ($request->items as $value) { $code = $value['code']; $is_added = TrcStock::where('spare_code',$code)->where('warehouse',$warehouse)->count(); if($is_added > 0){ $trc = TrcStock::where('spare_code',$code)->where('warehouse',$warehouse)->first(); $trc->qty = $trc->qty + $value['qty']; }else{ $trc = new TrcStock(); $trc->spare_code = $value['code']; $trc->qty = $value['qty']; } $trc->added_by = $user_id; $trc->warehouse = $warehouse; $trc->save(); $stock_in_store = StockInCircleStore::where('spare_code',$code)->where('warehouse',$warehouse)->count(); if($stock_in_store > 0){ $stock_in_store = StockInCircleStore::where('spare_code',$code)->where('warehouse',$warehouse)->first(); $stock_in_store->faulty_qty = $stock_in_store->faulty_qty - $value['qty']; $stock_in_store->save(); } $history = new HistoryTrcStock(); $history->spare_code = $value['code']; $history->qty = $value['qty']; $history->trc_date = date('Y-m-d H:i:s'); $history->added_by = $user_id; $history->save(); } return json_encode(['success'=> true, 'msg' => 'Spare Send to TRC Successfully !!']); }else{ return json_encode(['success'=> false, 'msg' => $sp.' Spare Not in TRC Stock of warehouse '.$warehouse]); } } public function spare_history_at_trc($spare_code){ $this->get_common(); //$user_id = Auth::user()->id; $all_history =DB::table('history_trc_stocks')->join('users','users.id','history_trc_stocks.added_by')->where('history_trc_stocks.spare_code',$spare_code)->select('history_trc_stocks.*','users.name')->get(); $this->data = array_merge($this->data,['all_history'=>$all_history,'spare_code' =>$spare_code]); return view('admin.trc-history',$this->data); } public function spare_move_to_stock(){ $this->get_common(); $loginId = Auth::user()->loginId; $assigned_warehouses = DB::table('local_user_mapping') ->select('local_user_mapping.warehouseCode') ->where('LoginID', $loginId) ->first(); $warehouses = explode(',', $assigned_warehouses->warehouseCode); $this->data = array_merge($this->data,['warehouses'=>$warehouses]); return view('admin.spare-move-to-stock',$this->data); } public function repaired_stock_move_to_stock(Request $request){ $user_id = Auth::user()->id; $warehouse = $request->warehouse; foreach ($request->items as $value) { $code = $value['code']; $is_added = TrcStock::where('spare_code',$code)->where('warehouse',$warehouse)->count(); if($is_added > 0){ $trc = TrcStock::where('spare_code',$code)->where('warehouse',$warehouse)->first(); $trc->qty = $trc->qty - ($value['qty']+$value['flqty']); $trc->save(); $stock = StockInCircleStore::where('spare_code',$code)->where('warehouse',$warehouse)->first(); if($stock != null){ if($stock->warehouse !='SERPNT'){ $stock->repaired_qty = $stock->repaired_qty + $value['qty']; $stock->faulty_qty = $stock->faulty_qty + $value['flqty']; $stock->save(); }else{ $stock->repaired_qty = $stock->repaired_qty + $value['qty']; $stock->save(); \DB::table('damaged_spares')->insert([ 'spare_code'=>$code, 'qty' => $value['flqty'], 'added_by' => $user_id ]); } }else{ $stock_in_store = new StockInCircleStore(); $stock_in_store->user_id = $user_id; $stock_in_store->warehouse = $warehouse; $stock_in_store->spare_id = $value['id']; $stock_in_store->spare_code = $value['code']; $stock_in_store->fresh_qty = 0; $stock_in_store->faulty_qty = $value['flqty']; $stock_in_store->repaired_qty = $value['qty']; $stock_in_store->save(); } $history = new HistoryOfStockInCircleStore(); $history->spare_code = $value['code']; $history->repaired_qty = $value['qty']; $history->faulty_qty = $value['flqty']; $history->receiving_type = 'move_from_trc_to_stock'; $history->receiving_date = date('Y-m-d H:i:s'); $history->added_by = $user_id; $history->save(); } } return json_encode(['success'=> true, 'msg' => 'repaired stock move from trc to stock Successfully']); } public function get_spare_part_for_trc(Request $request){ $code = $request->code; $warehouse = $request->warehouse; $is_spare_part = DB::table('stock_in_circle_stores')->where('spare_code',$code)->where('faulty_qty','>',0)->count(); if($is_spare_part > 0){ $spare_in_stock = DB::table('stock_in_circle_stores')->where('spare_code',$code)->first(); $spare_part = DB::table('spare_parts_masters')->where('code',$code)->first(); return json_encode(['success'=>true,'spare_part'=>$spare_part,'spare_in_stock'=>$spare_in_stock]); }else{ return json_encode(['success'=>false,'msg'=>'circle store has no any faulty spare of the given code. Try another spare Code.']); } } public function get_spare_part_from_trc(Request $request){ $code = $request->code; $warehouse = $request->warehouse; $is_spare_part = DB::table('trc_stocks')->where('spare_code',$code)->where('warehouse',$warehouse)->where('qty','>',0)->count(); if($is_spare_part > 0){ $trc_stock = DB::table('trc_stocks')->where('warehouse',$warehouse)->where('spare_code',$code)->first(); $spare_part = DB::table('spare_parts_masters')->where('code',$code)->first(); return json_encode(['success'=>true,'spare_part'=>$spare_part,'trc_stock'=>$trc_stock]); }else{ return json_encode(['success'=>false,'msg'=>'TRC has not the faulty item for the given Spare Code. Try another spare Code.']); } } public function get_update_spm_repaired_rate(Request $request){ $this->get_common(); $gst_slabs = DB::table('gst_slabs')->get(); $spart_value_types = DB::table('spart_value_type')->get(); $spare_category = DB::table('spare_category')->get(); $spare_product_category = DB::table('spare_product_category')->get(); $this->data = array_merge($this->data,['gst_slabs'=>$gst_slabs,'spart_value_types'=>$spart_value_types,'spare_category'=>$spare_category,'spare_product_categories'=>$spare_product_category]); return view('admin.spare.update-spm-repaired-rate',$this->data); } public function update_spm_repaired_rate(Request $request) { $newRepairedRate = $request->input('repaired_rate'); $newFaultyRate = $request->input('faulty_rate'); // Check if both values are not "null" and the type column is "returnable" if ($newRepairedRate !== null && $newFaultyRate !== null) { SparePartsMaster::query() ->where('type', 'returnable') // Replace 'returnable' with the actual value you want to check ->update([ 'repaired_rate' => $newRepairedRate, 'faulty_rate' => $newFaultyRate ]); return redirect('admin/update-spm-repaired-rate')->with('success', 'Repaired & Faulty Rate Updated successfully!'); } else { // Handle cases where only one value is provided // For 'repaired_rate' if ($newRepairedRate !== null) { SparePartsMaster::query() ->where('type', 'returnable') // Replace 'returnable' with the actual value you want to check ->update(['repaired_rate' => $newRepairedRate]); return redirect('admin/update-spm-repaired-rate')->with('success', 'Repaired Rate Updated successfully!'); } // For 'faulty_rate' if ($newFaultyRate !== null) { SparePartsMaster::query() ->where('type', 'returnable') // Replace 'returnable' with the actual value you want to check ->update(['faulty_rate' => $newFaultyRate]); return redirect('admin/update-spm-repaired-rate')->with('success', 'Faulty Rate Updated successfully!'); } } } }