app/Http/Controllers/AutomationPurchaseRequestMappingController.php
// if input balance = output balance, normal case, if input balance < output balance, as minimum quantity	
if input balance = output balance, normal case, if input balance < output balance, as minimum quantity
if input balance = output balance normal case if input balance < output balance as minimum quantity
public function processBlanketPurchaseAgreement($balance, $itemID, $requestID, $branchID)
	{
		$agreementList = [];
		(int)$currentBalance = $balance;
		foreach(AgreementLine::where('itemID',$itemID)->orderBy('price','asc')->orderBy('balance','desc')->get()->pluck('agreementID') as $value)
		{
			$agreementHeader = AgreementHeader::where("agreementID", $value)->where("status","Active")->where("type","Blanket Purchase Agreement")->orderBy('expiryDate','asc')->orderBy('createdDate','asc')->first();
			if($agreementHeader != null) {
				if($agreementHeader->expiryDate > Carbon::today())
				{
					$agreement = AgreementLine::where('agreementID', $value)->where('revision', $agreementHeader->revision)->where('itemID', $itemID)->first();
					if($agreement != null) {
					if($agreement->balance >= $currentBalance && $currentBalance >= $agreement->minimumOrderQuantity) 
					{
						$discount = (float)AgreementPriceBreak::where('agreementID', $value)->where('revision', $agreementHeader->revision)->where('itemID', $itemID)->where('priceBreak','<=',$currentBalance)->orderBy('priceBreak','desc')->first()->discount;
						if($discount != null) {
							$amount = $currentBalance * (int)$agreement->price * $discount;
						}else {
							$amount = $currentBalance * (int)$agreement->price;
						}
						$this->updateAgreementLine($agreement, (int)($agreement->balance - $currentBalance));
						$this->updatePurchaseOrder($requestID, $agreement, $agreementHeader, $branchID);
						$this->updatePurchaseOrderItem($itemID, $currentBalance, $amount, $currentBalance);
						$this->updatePurchaseRequest($requestID);
						$this->updatePurchaseRequestItem($requestID, $itemID, 0);
						return 0;
					}
					else 
					{
						$currentBalance = $currentBalance - $agreement->balance;
						$discount = (float)AgreementPriceBreak::where('agreementID', $value)->where('revision', $agreementHeader->revision)->where('itemID', $itemID)->where('priceBreak','<=',$agreement->balance)->orderBy('priceBreak','desc')->first()->discount;
						$amount = (int)$agreement->balance * $agreement->price * $discount;
						$this->updateAgreementLine($agreement,0);
						$this->updatePurchaseOrder($requestID, $agreement, $agreementHeader, $branchID);
						$this->updatePurchaseOrderItem($itemID, $agreement->balance, $amount, $agreement->balance);
						$this->updatePurchaseRequestItem($requestID, $itemID, $currentBalance);
					}
					}
				}
			}
		}
		return $currentBalance;
	}
public function processBlanketPurchaseAgreement ( $ balance , $ itemID , $ requestID , $ branchID ) { $ agreementList = [ ] ; ( int ) $ currentBalance = $ balance ; foreach ( AgreementLine :: where ( 'itemID' , $ itemID ) -> orderBy ( 'price' , 'asc' ) -> orderBy ( 'balance' , 'desc' ) -> get ( ) -> pluck ( 'agreementID' ) as $ value ) { $ agreementHeader = AgreementHeader :: where ( " agreementID " , $ value ) -> where ( " status " , " Active " ) -> where ( " type " , " Blanket Purchase Agreement " ) -> orderBy ( 'expiryDate' , 'asc' ) -> orderBy ( 'createdDate' , 'asc' ) -> first ( ) ; if ( $ agreementHeader != null ) { if ( $ agreementHeader -> expiryDate > Carbon :: today ( ) ) { $ agreement = AgreementLine :: where ( 'agreementID' , $ value ) -> where ( 'revision' , $ agreementHeader -> revision ) -> where ( 'itemID' , $ itemID ) -> first ( ) ; if ( $ agreement != null ) { if ( $ agreement -> balance >= $ currentBalance && $ currentBalance >= $ agreement -> minimumOrderQuantity ) { $ discount = ( float ) AgreementPriceBreak :: where ( 'agreementID' , $ value ) -> where ( 'revision' , $ agreementHeader -> revision ) -> where ( 'itemID' , $ itemID ) -> where ( 'priceBreak' , '<=' , $ currentBalance ) -> orderBy ( 'priceBreak' , 'desc' ) -> first ( ) -> discount ; if ( $ discount != null ) { $ amount = $ currentBalance * ( int ) $ agreement -> price * $ discount ; } else { $ amount = $ currentBalance * ( int ) $ agreement -> price ; } $ this -> updateAgreementLine ( $ agreement , ( int ) ( $ agreement -> balance - $ currentBalance ) ) ; $ this -> updatePurchaseOrder ( $ requestID , $ agreement , $ agreementHeader , $ branchID ) ; $ this -> updatePurchaseOrderItem ( $ itemID , $ currentBalance , $ amount , $ currentBalance ) ; $ this -> updatePurchaseRequest ( $ requestID ) ; $ this -> updatePurchaseRequestItem ( $ requestID , $ itemID , 0 ) ; return 0 ; } else { $ currentBalance = $ currentBalance - $ agreement -> balance ; $ discount = ( float ) AgreementPriceBreak :: where ( 'agreementID' , $ value ) -> where ( 'revision' , $ agreementHeader -> revision ) -> where ( 'itemID' , $ itemID ) -> where ( 'priceBreak' , '<=' , $ agreement -> balance ) -> orderBy ( 'priceBreak' , 'desc' ) -> first ( ) -> discount ; $ amount = ( int ) $ agreement -> balance * $ agreement -> price * $ discount ; $ this -> updateAgreementLine ( $ agreement , 0 ) ; $ this -> updatePurchaseOrder ( $ requestID , $ agreement , $ agreementHeader , $ branchID ) ; $ this -> updatePurchaseOrderItem ( $ itemID , $ agreement -> balance , $ amount , $ agreement -> balance ) ; $ this -> updatePurchaseRequestItem ( $ requestID , $ itemID , $ currentBalance ) ; } } } } } return $ currentBalance ; }
if input balance = output balance, normal case, if input balance < output balance, as minimum quantity
if input balance = output balance normal case if input balance < output balance as minimum quantity
//echo "Balance:".$balance." , itemID:".$itemID." , requestID:".$requestID." , branchID:".$branchID; // currentBalance > Agreement->balance