0

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

Karan
  • 1,146
  • 1
  • 10
  • 24
Brendan
  • 95
  • 1
  • 15

1 Answers1

0

Yes you can do this assuming they are all the same requests type. You will need to handle the logic inside your controller then hand of to an event bus or function to do the rest of the work. for example

    if($request->input('feature') == $PAID){
      $this->paid($request);
    } elseif($request->input('feature') == $VOUCHER){
      $this->voucher($request);
    } else{
      $this->notPaid($request)
    }

I'm sure there is a nicer way this can be done rather then nesting if else statements however this should work fine for what you've asked.

however if you don't like this you could look at using reflection ( make sure you have good error handling if you choose this path )

Karan
  • 1,146
  • 1
  • 10
  • 24
  • Thanks, this may be a little above my skill level, still learning here. A little hard to picture the ($request->input('feature') ==$PAID) in my head and how it fits in, based on the existing code – Brendan Aug 02 '19 at 00:46