I am looking to clean up some of my code. Just to make things a little tighter. While my current code works fine, I would like to play around with something.
Currently I have 3 Button on a Attendance Roll, which button jumps to a different Public Function within a Controller, but the main different is the Value they insert into the record and the message shown
This is my controller
public function paid($id)
{
$r = Roll::find($id);
$rollid = Rollmapping::latest()->value('id');
if ($r != null)
{
$r->status = "C";
$r->paidrollid = $rollid;
$r->save();
Alert::Success('Member Paid', 'Member Paid Cash')->autoclose(1500);
return redirect(action('RollController@index'));
}
return redirect(action('RollController@index'));
}
public function voucher($id)
{
$r = Roll::find($id);
$rollid = Rollmapping::latest()->value('id');
if ($r != null)
{
// Check if ActiveKids Balance is not less than 0
if ($r->member->ActiveKids->sum('balance') >= 10)
{
// Update Roll Status
$r->status = "V";
$r->paidrollid = $rollid;
$r->save();
// Insert Record into ActiveKids Voucher
$voucher = new ActiveKids();
$voucher->member_id = $r->member_id;
$voucher->voucher_number = 'Weekly Subs';
$voucher->balance = -10;
$voucher->date_received = Carbon::now()->toDateString();
$voucher->save();
if(ActiveKids::Where('member_id','=', $r->member_id)->sum('balance') = 0)
{
Alert::Success("Paid", "Member Voucer Balance is now $0")->autoclose(1500);
}
else
{
Alert::Success("Paid", "Member paid from Voucher Balance")->autoclose(1500);
}
return redirect(action('RollController@index'));
}
else
{
//Not Enough money in the account
Alert::Error("Error", "Insufficient Active Kids Balance")->autoclose(1500);
return redirect(action('RollController@index'));
}
}
return redirect(action('RollController@index'));
}
public function notpaid($id)
{
$r = Roll::find($id);
if ($r != null)
{
$r->status = "P";
$r->save();
return redirect(action('RollController@index'))->with ('success', 'Member Present');
}
return redirect(action('RollController@index'));
}
What I would link to merge this into 1 function with a second value For example (if possible)
public function paid($id, $type)
Then I can use IF statements for example
if($type = 'C')
//Action for $type = C
elseif($type = 'V')
//Action for $type = V
elseif($type = 'P')
//Action for $type = P
endif
This is the 3 button in the view
<a href="{{action('RollController@paid', $r->id)}}" title="Paid" class="btn btn-success btn-round"><i class="material-icons">done</i></a>
<a href="{{action('RollController@voucher', $r->id)}}" title="Voucher" class="btn btn-info btn-round"><i class ="material-icons">local_activity</i></a>
<a href="{{action('RollController@notpaid', $r->id)}}" title="Not Paid" class="btn btn-danger btn-round"><i class="material-icons">close</i></a>
So I am hoping something like
<a href="{{action('RollController@paid', $r->id, 'C')}}" .....
<a href="{{action('RollController@paid', $r->id, 'V')}}" .....
<a href="{{action('RollController@paid', $r->id, 'P')}}" .....
Would be fine