app/Http/Controllers/CustomerManageRenewalsController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Customer  $customer
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Customer $customer, $token, Request $request)
    {
        $this->authorize('manageRenewal', [$customer, $token]);
        $this->validate($request, $this->rules);
        $renewalTransition = $request->only('renewal_id')['renewal_id'];
        $renewals = $customer->renewals()->with('service')->whereIn('renewals.id', array_keys($renewalTransition))->get();
        $renewals->each(function($renewal) use($renewalTransition){
            try {
                $renewal->transition($renewalTransition[$renewal->id]);
                $service = $renewal->service;
                if( 
                    $renewal->stateIs() == RenewalSM::S_to_bill &&
                    ($service->lastRenewalInsert()->deadline == $renewal->deadline)
                ){
                    $service->renewals()->create([
                        'deadline' => $service->calcNextDeadline(),
                        'amount' => $renewal->amount
                    ]);
                } elseif ( 
                    $renewal->stateIs() == RenewalSM::S_suspended
                ){
                    $transitionToPerform = RenewalSM::T_suspend;
                    $service->renewals()->allNextFromDate($renewal)->get()->each( function($nextRenewal) use($transitionToPerform){
                        if($nextRenewal->transitionAllowed($transitionToPerform)){
                            try {
                                $nextRenewal->transition($transitionToPerform);
                            } catch(\Exception $e) {
                                return abort(500, $e->getMessage());
                            }
                        }
                    });
                }
            } catch(\Exception $e) {
                return abort(500, $e->getMessage());
            }
        });
        return redirect()->route('manage-renewals', ['customer'=>$customer, 'token'=>$token]);
    }
public function update ( Customer $ customer , $ token , Request $ request ) { $ this -> authorize ( 'manageRenewal' , [ $ customer , $ token ] ) ; $ this -> validate ( $ request , $ this -> rules ) ; $ renewalTransition = $ request -> only ( 'renewal_id' ) [ 'renewal_id' ] ; $ renewals = $ customer -> renewals ( ) -> with ( 'service' ) -> whereIn ( 'renewals.id' , array_keys ( $ renewalTransition ) ) -> get ( ) ; $ renewals -> each ( function ( $ renewal ) use ( $ renewalTransition ) { try { $ renewal -> transition ( $ renewalTransition [ $ renewal -> id ] ) ; $ service = $ renewal -> service ; if ( $ renewal -> stateIs ( ) == RenewalSM :: S_to_bill && ( $ service -> lastRenewalInsert ( ) -> deadline == $ renewal -> deadline ) ) { $ service -> renewals ( ) -> create ( [ 'deadline' => $ service -> calcNextDeadline ( ) , 'amount' => $ renewal -> amount ] ) ; } elseif ( $ renewal -> stateIs ( ) == RenewalSM :: S_suspended ) { $ transitionToPerform = RenewalSM :: T_suspend ; $ service -> renewals ( ) -> allNextFromDate ( $ renewal ) -> get ( ) -> each ( function ( $ nextRenewal ) use ( $ transitionToPerform ) { if ( $ nextRenewal -> transitionAllowed ( $ transitionToPerform ) ) { try { $ nextRenewal -> transition ( $ transitionToPerform ) ; } catch ( \ Exception $ e ) { return abort ( 500 , $ e -> getMessage ( ) ) ; } } } ) ; } } catch ( \ Exception $ e ) { return abort ( 500 , $ e -> getMessage ( ) ) ; } } ) ; return redirect ( ) -> route ( 'manage-renewals' , [ 'customer' => $ customer , 'token' => $ token ] ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
// If the state is to_bill and doesn't exist a new renewal // If the state is S_suspended search next renewals for suspend them