app/Http/Controllers/AgreementHeaderController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request)
    {
        $input = $request->all();
		$rules = array ('agreementID' => 'required|numeric',
						'revision' => 'required|numeric',
						'supplierID' => 'required|numeric',
						'type' => 'required',
						'createdDate' => 'required|date',
						'effectiveDate' => 'required|date',
						'expiryDate' => 'required|date',
						'status' => 'required',
						'currency' => 'required',
						'termsAndCondition' => 'required',
						'deliveryAddress' => 'required',);
		$messages = ['agreementID.required' => 'Please input the agreement id',
					 'agreementID.numeric' => 'Please input the agreement id in correct format',
					 'revision.required' => 'Please input the revision',
					 'revision.numeric' => 'Please input the revision in correct format',
					 'supplierID.required' => 'Please input the supplier id',
					 'supplierID.numeric' => 'Please input the supplier id in correct format',
					 'type.required' => 'Please input the type',
					 'createdDate.required' => 'Please input the created date',
					 'createdDate.date' => 'Please input the created date in correct date format',
					 'effectiveDate.date' => 'Please input the effective date in correct date format',
					 'effectiveDate.required' => 'Please input the effective date',
					 'expiryDate.date' => 'Please input the expiry date in correct date format',
					 'expiryDate.required' => 'Please input the expiry date',
					 'status.required' => 'Please input the status',
					 'currency.required' => 'Please input the currency',
					 'termsAndCondition.required' => 'Please input the terms and condition',
					 'deliveryAddress.required' => 'Please input the delivery address'];
        $validator = Validator::make($input, $rules, $messages);                 
        if ($validator->fails()) 
		{             
            return Redirect::to('agreementHeader')->withErrors($validator);        
        } 
		else
		{       
			$agreementHeader = AgreementHeader::where('agreementID', $request->agreementID)->where('revision', $request->revision)->first();
			$agreementHeaderRevision = AgreementHeader::where('agreementID', $request->agreementID)->orderBy('revision', 'desc')->first();
			if($agreementHeader->status === 'Disabled'){
				return back()->withErrors('Cannot update the disabled agreement');
			}else if($agreementHeader->revision !== $agreementHeaderRevision->revision){
				return back()->withErrors('Cannot update the susperseded agreement');
			}
			DB::table('agreement_header')->where('agreementID', $request->agreementID)->where('revision', $request->revision)->update(['status'=>'Superseded']);
			$newAgreementHeader= new AgreementHeader;
   			$newAgreementHeader->agreementID = $request->agreementID;
			$newAgreementHeader->revision = ($request->revision+1);
			$newAgreementHeader->supplierID = $request->supplierID;
			$newAgreementHeader->type = $request->type;
			$newAgreementHeader->createdDate = $request->createdDate;
			$newAgreementHeader->effectiveDate = $request->effectiveDate;
			$newAgreementHeader->expiryDate = $request->expiryDate;
			if ((($request->effectiveDate <= date("Y-m-d")) && (date("Y-m-d") <= $request->expiryDate))){
				$temp = "Active";
			} else if ($request->expiryDate <= date("Y-m-d")){
				$temp = "Expired";
			} else if ($request->effectiveDate >= date("Y-m-d")){
				$temp = "Inactive";
			}
			$newAgreementHeader->status = $temp;
			$newAgreementHeader->amountAgreed = $request->amountAgreed;
			$newAgreementHeader->currency = $request->currency;
			$newAgreementHeader->termsAndCondition = $request->termsAndCondition;
			$newAgreementHeader->tentativeSchedule = $request->tentativeSchedule;
			$newAgreementHeader->deliveryAddress = $request->deliveryAddress;
			$newAgreementHeader->save();   
			$itemID = $request->input('itemID', []);
            $promisedQuantity = $request->input('promisedQuantity', []);
		    $minimumOrderQuantity = $request->input('minimumOrderQuantity', []);
            $price = $request->input('price', []);
			$reference = $request->input('reference', []);
            for($item=0; $item < count($itemID); $item++) {
            	if(	($itemID[$item] != '') && ($promisedQuantity[$item] != '') && ($minimumOrderQuantity[$item] != '') && ($price[$item] != '')) {
					$agreementLine = new AgreementLine;
					$agreementLine->agreementID = $request->agreementID;
                    $agreementLine->revision = ($request->revision+1);
                    $agreementLine->itemID = $itemID[$item];
                    $agreementLine->promisedQuantity = $promisedQuantity[$item];
                    $agreementLine->minimumOrderQuantity = $minimumOrderQuantity[$item];
                    $agreementLine->balance = $promisedQuantity[$item];
                    $agreementLine->price = $price[$item];
                    $agreementLine->reference = $reference[$item];
                    $agreementLine->save();
            	}
            	$agreementPriceBreak = new AgreementPriceBreak;
            	$agreementPriceBreak->agreementID=$agreementLine->agreementID;
    		   	$agreementPriceBreak->revision=$agreementLine->revision;
    		   	$agreementPriceBreak->itemID=$agreementLine->itemID;
    		   	$agreementPriceBreak->priceBreak=0;	
    		   	$agreementPriceBreak->discount=1;
            	$agreementPriceBreak->save();
            }
            return redirect('agreementHeader')->with('success','Agreement updated successfully');
        }         
    }
public function update ( Request $ request ) { $ input = $ request -> all ( ) ; $ rules = array ( 'agreementID' => 'required|numeric' , 'revision' => 'required|numeric' , 'supplierID' => 'required|numeric' , 'type' => 'required' , 'createdDate' => 'required|date' , 'effectiveDate' => 'required|date' , 'expiryDate' => 'required|date' , 'status' => 'required' , 'currency' => 'required' , 'termsAndCondition' => 'required' , 'deliveryAddress' => 'required' , ) ; $ messages = [ 'agreementID.required' => 'Please input the agreement id' , 'agreementID.numeric' => 'Please input the agreement id in correct format' , 'revision.required' => 'Please input the revision' , 'revision.numeric' => 'Please input the revision in correct format' , 'supplierID.required' => 'Please input the supplier id' , 'supplierID.numeric' => 'Please input the supplier id in correct format' , 'type.required' => 'Please input the type' , 'createdDate.required' => 'Please input the created date' , 'createdDate.date' => 'Please input the created date in correct date format' , 'effectiveDate.date' => 'Please input the effective date in correct date format' , 'effectiveDate.required' => 'Please input the effective date' , 'expiryDate.date' => 'Please input the expiry date in correct date format' , 'expiryDate.required' => 'Please input the expiry date' , 'status.required' => 'Please input the status' , 'currency.required' => 'Please input the currency' , 'termsAndCondition.required' => 'Please input the terms and condition' , 'deliveryAddress.required' => 'Please input the delivery address' ] ; $ validator = Validator :: make ( $ input , $ rules , $ messages ) ; if ( $ validator -> fails ( ) ) { return Redirect :: to ( 'agreementHeader' ) -> withErrors ( $ validator ) ; } else { $ agreementHeader = AgreementHeader :: where ( 'agreementID' , $ request -> agreementID ) -> where ( 'revision' , $ request -> revision ) -> first ( ) ; $ agreementHeaderRevision = AgreementHeader :: where ( 'agreementID' , $ request -> agreementID ) -> orderBy ( 'revision' , 'desc' ) -> first ( ) ; if ( $ agreementHeader -> status === 'Disabled' ) { return back ( ) -> withErrors ( 'Cannot update the disabled agreement' ) ; } else if ( $ agreementHeader -> revision !== $ agreementHeaderRevision -> revision ) { return back ( ) -> withErrors ( 'Cannot update the susperseded agreement' ) ; } DB :: table ( 'agreement_header' ) -> where ( 'agreementID' , $ request -> agreementID ) -> where ( 'revision' , $ request -> revision ) -> update ( [ 'status' => 'Superseded' ] ) ; $ newAgreementHeader = new AgreementHeader ; $ newAgreementHeader -> agreementID = $ request -> agreementID ; $ newAgreementHeader -> revision = ( $ request -> revision + 1 ) ; $ newAgreementHeader -> supplierID = $ request -> supplierID ; $ newAgreementHeader -> type = $ request -> type ; $ newAgreementHeader -> createdDate = $ request -> createdDate ; $ newAgreementHeader -> effectiveDate = $ request -> effectiveDate ; $ newAgreementHeader -> expiryDate = $ request -> expiryDate ; if ( ( ( $ request -> effectiveDate <= date ( " Y-m-d " ) ) && ( date ( " Y-m-d " ) <= $ request -> expiryDate ) ) ) { $ temp = " Active " ; } else if ( $ request -> expiryDate <= date ( " Y-m-d " ) ) { $ temp = " Expired " ; } else if ( $ request -> effectiveDate >= date ( " Y-m-d " ) ) { $ temp = " Inactive " ; } $ newAgreementHeader -> status = $ temp ; $ newAgreementHeader -> amountAgreed = $ request -> amountAgreed ; $ newAgreementHeader -> currency = $ request -> currency ; $ newAgreementHeader -> termsAndCondition = $ request -> termsAndCondition ; $ newAgreementHeader -> tentativeSchedule = $ request -> tentativeSchedule ; $ newAgreementHeader -> deliveryAddress = $ request -> deliveryAddress ; $ newAgreementHeader -> save ( ) ; $ itemID = $ request -> input ( 'itemID' , [ ] ) ; $ promisedQuantity = $ request -> input ( 'promisedQuantity' , [ ] ) ; $ minimumOrderQuantity = $ request -> input ( 'minimumOrderQuantity' , [ ] ) ; $ price = $ request -> input ( 'price' , [ ] ) ; $ reference = $ request -> input ( 'reference' , [ ] ) ; for ( $ item = 0 ; $ item < count ( $ itemID ) ; $ item ++ ) { if ( ( $ itemID [ $ item ] != '' ) && ( $ promisedQuantity [ $ item ] != '' ) && ( $ minimumOrderQuantity [ $ item ] != '' ) && ( $ price [ $ item ] != '' ) ) { $ agreementLine = new AgreementLine ; $ agreementLine -> agreementID = $ request -> agreementID ; $ agreementLine -> revision = ( $ request -> revision + 1 ) ; $ agreementLine -> itemID = $ itemID [ $ item ] ; $ agreementLine -> promisedQuantity = $ promisedQuantity [ $ item ] ; $ agreementLine -> minimumOrderQuantity = $ minimumOrderQuantity [ $ item ] ; $ agreementLine -> balance = $ promisedQuantity [ $ item ] ; $ agreementLine -> price = $ price [ $ item ] ; $ agreementLine -> reference = $ reference [ $ item ] ; $ agreementLine -> save ( ) ; } $ agreementPriceBreak = new AgreementPriceBreak ; $ agreementPriceBreak -> agreementID = $ agreementLine -> agreementID ; $ agreementPriceBreak -> revision = $ agreementLine -> revision ; $ agreementPriceBreak -> itemID = $ agreementLine -> itemID ; $ agreementPriceBreak -> priceBreak = 0 ; $ agreementPriceBreak -> discount = 1 ; $ agreementPriceBreak -> save ( ) ; } return redirect ( 'agreementHeader' ) -> with ( 'success' , 'Agreement updated successfully' ) ; } }
Update the specified resource in storage.
Update the specified resource in storage .