Commit 5af6b0d9 authored by Hasith Yoman's avatar Hasith Yoman

change controllers

parent 27062319
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Audit;
use App\Models\Queue;
use App\Models\Fueltype;
use App\Models\Station;
use App\Models\User;
use App\Models\Fuelcapacity;
class AuditController extends Controller
{
// This function for get Audit
public function get_audit(Request $req){
$response = [
"amount" => '',
"filled" => '',
];
if($req->role == "station"){
$station = Station::where('user_id',$req->id)->first();
$response = [
"amount" => Audit::where('station_id',$station->id)->sum('amount'),
"filled" => Audit::where('station_id',$station->id)->count()
];
}else if($req->role == "user"){
$response = [
"amount" => Audit::where('user_id',$req->id)->sum('amount'),
"filled" => Audit::where('user_id',$req->id)->count(),
"list" => Audit::join('stations','stations.id','=','audits.station_id')
->join('fueltypes','fueltypes.id','=','audits.fueltype_id')->join('vehicles','vehicles.id','=','audits.vehicle_id')
->join('users','users.id','=','audits.user_id')->where('audits.user_id',$req->id)
->get(['audits.id as aid','users.name as uname','stations.name as sname', 'vehicles.type as vtype','vehicles.vehicle_no as vno','fueltypes.name as ftyoe','audits.amount','audits.qty','audits.created_at'])
];
}
return response()->json(['response'=>$response]);
}
// This function for create a new Audit
public function create_audit(Request $req){
$fueltype = Fueltype::find($req->fueltype_id);
$audit = new Audit;
$audit->qty = $req->qty;
$audit->amount = $fueltype->price * $req->qty;
$audit->user_id = $req->user_id;
$audit->vehicle_id = $req->vehicle_id;
$audit->station_id = $req->station_id;
$audit->fueltype_id = $req->fueltype_id;
$audit->save();
$fuelcapacity = Fuelcapacity::where('fueltype_id',$req->fueltype_id)->where('station_id',$req->station_id)->first();
$fuelcapacity->update([
'current_qty' => $fuelcapacity->current_qty - $req->qty,
]);
$queue = Queue::find($req->qid);
$queue->update([
'status' => 1,
]);
$message = 'success';
return response()->json(['message'=>$message]);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Fueltype;
use App\Models\Fuelcapacity;
use App\Models\Station;
class FuelController extends Controller
{
// This function for get Fuel Types
public function get_fuel_type(Request $req){
$fueltype = "";
if($req->id){
$fueltype = Fueltype::find($req->id);
}else{
$fueltype = Fueltype::all()->orderby('id','DESC');
}
return response()->json(['respond'=>$fueltype]);
}
// This function for create a new Fuel type
public function create_fuel_type(Request $req){
$message = '';
if($this->check_empty($req->name,$req->price)){
//create new User
$fueltype = new Fueltype;
$fueltype->name = $req->name;
$fueltype->price = $req->price;
$fueltype->save();
$message = 'Fuel Type successfully created';
}else{
$message = 'Type or Price cannot be empty';
}
return response()->json(['message'=>$message]);
}
// This function for delete a Fuel type
public function delete_fuel_type(Request $req){
$fueltype = Fueltype::find($req->id);
$fueltype->delete();
return response()->json(['message'=>'success']);
}
// This function for update a Fuel type
public function update_fuel_type(Request $req){
$fueltype = Fueltype::find($req->id);
$fueltype->update([
'name' => $req->name,
'price' => $req->price,
]);
return response()->json(['message'=>'success']);
}
// This function for check given fields are empty or not
public function check_empty($name,$price){
if($name != '' && $name != null && $price != '' && $price != null){
return true;
}else{
return false;
}
}
// This function for get Fuel Types For Dropdowns
public function get_capacity_fuel_type(Request $req){
$fueltype = Fueltype::get(['name as label','price as value'])->orderby('id','DESC');
return response()->json(['respond'=>$fueltype]);
}
// This function for create a new Fuel type
public function create_fuel_capacity(Request $req){
$station = Station::where('user_id',$req->uid)->first();
$message = '';
if(Fuelcapacity::Where('fueltype_id',$req->fid)->Where('station_id',$station->id)->exists()){
$message = 'Already Created';
}else{
//create new Stock
$fuelcapacity = new Fuelcapacity;
$fuelcapacity->fueltype_id = $req->fid;
$fuelcapacity->ini_qty = $req->qty;
$fuelcapacity->current_qty = $req->qty;
$fuelcapacity->station_id = $station->id;
$fuelcapacity->save();
$message = 'success';
}
return response()->json(['message'=>$message]);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment