private static Map<String,Object> createBillingAccountFromReturn(GenericValue returnHeader,List<GenericValue> returnItems,DispatchContext dctx,Map<String,? extends Object> context){
  LocalDispatcher dispatcher=dctx.getDispatcher();
  GenericValue userLogin=(GenericValue)context.get("userLogin");
  Locale locale=(Locale)context.get("locale");
  try {
    List<GenericValue> orders=EntityUtil.getRelated("OrderHeader",null,returnItems,false);
    List<GenericValue> productStores=EntityUtil.getRelated("ProductStore",null,orders,false);
    Long storeCreditValidDays=null;
    for (    GenericValue productStore : productStores) {
      Long thisStoreValidDays=productStore.getLong("storeCreditValidDays");
      if (thisStoreValidDays == null)       continue;
      if (storeCreditValidDays == null) {
        storeCreditValidDays=thisStoreValidDays;
      }
 else       if (thisStoreValidDays.compareTo(storeCreditValidDays) < 0) {
        storeCreditValidDays=thisStoreValidDays;
      }
    }
    Timestamp thruDate=null;
    if (storeCreditValidDays != null)     thruDate=UtilDateTime.getDayEnd(UtilDateTime.nowTimestamp(),storeCreditValidDays);
    Map<String,Object> input=UtilMisc.<String,Object>toMap("accountLimit",BigDecimal.ZERO,"description","Credit Account for Return #" + returnHeader.get("returnId"),"userLogin",userLogin);
    input.put("accountCurrencyUomId",returnHeader.get("currencyUomId"));
    input.put("thruDate",thruDate);
    Map<String,Object> results=dispatcher.runSync("createBillingAccount",input);
    if (ServiceUtil.isError(results))     return results;
    String billingAccountId=(String)results.get("billingAccountId");
    input=UtilMisc.toMap("billingAccountId",billingAccountId,"partyId",returnHeader.get("fromPartyId"),"roleTypeId","BILL_TO_CUSTOMER","userLogin",userLogin);
    Map<String,Object> roleResults=dispatcher.runSync("createBillingAccountRole",input);
    if (ServiceUtil.isError(roleResults)) {
      Debug.logError("Error with createBillingAccountRole: " + roleResults.get(ModelService.ERROR_MESSAGE),module);
      return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderErrorWithCreateBillingAccountRole",locale) + roleResults.get(ModelService.ERROR_MESSAGE));
    }
    return results;
  }
 catch (  GenericEntityException e) {
    Debug.logError(e,"Entity error when creating BillingAccount: " + e.getMessage(),module);
    return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderProblemsCreatingBillingAccount",locale));
  }
catch (  GenericServiceException e) {
    Debug.logError(e,"Service error when creating BillingAccount: " + e.getMessage(),module);
    return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderProblemsCreatingBillingAccount",locale));
  }
}

private static Map < String , Object > createBillingAccountFromReturn ( GenericValue returnHeader , List < GenericValue > returnItems , DispatchContext dctx , Map < String , ? extends Object > context ) { LocalDispatcher dispatcher = dctx . getDispatcher ( ) ; GenericValue userLogin = ( GenericValue ) context . get ( "userLogin" ) ; Locale locale = ( Locale ) context . get ( "locale" ) ; try { List < GenericValue > orders = EntityUtil . getRelated ( "OrderHeader" , null , returnItems , false ) ; List < GenericValue > productStores = EntityUtil . getRelated ( "ProductStore" , null , orders , false ) ; Long storeCreditValidDays = null ; for ( GenericValue productStore : productStores ) { Long thisStoreValidDays = productStore . getLong ( "storeCreditValidDays" ) ; if ( thisStoreValidDays == null ) continue ; if ( storeCreditValidDays == null ) { storeCreditValidDays = thisStoreValidDays ; } else if ( thisStoreValidDays . compareTo ( storeCreditValidDays ) < 0 ) { storeCreditValidDays = thisStoreValidDays ; } } Timestamp thruDate = null ; if ( storeCreditValidDays != null ) thruDate = UtilDateTime . getDayEnd ( UtilDateTime . nowTimestamp ( ) , storeCreditValidDays ) ; Map < String , Object > input = UtilMisc . < String , Object > toMap ( "accountLimit" , BigDecimal . ZERO , "description" , "Credit Account for Return #" + returnHeader . get ( "returnId" ) , "userLogin" , userLogin ) ; input . put ( "accountCurrencyUomId" , returnHeader . get ( "currencyUomId" ) ) ; input . put ( "thruDate" , thruDate ) ; Map < String , Object > results = dispatcher . runSync ( "createBillingAccount" , input ) ; if ( ServiceUtil . isError ( results ) ) return results ; String billingAccountId = ( String ) results . get ( "billingAccountId" ) ; input = UtilMisc . toMap ( "billingAccountId" , billingAccountId , "partyId" , returnHeader . get ( "fromPartyId" ) , "roleTypeId" , "BILL_TO_CUSTOMER" , "userLogin" , userLogin ) ; Map < String , Object > roleResults = dispatcher . runSync ( "createBillingAccountRole" , input ) ; if ( ServiceUtil . isError ( roleResults ) ) { Debug . logError ( "Error with createBillingAccountRole: " + roleResults . get ( ModelService . ERROR_MESSAGE ) , module ) ; return ServiceUtil . returnError ( UtilProperties . getMessage ( resource_error , "OrderErrorWithCreateBillingAccountRole" , locale ) + roleResults . get ( ModelService . ERROR_MESSAGE ) ) ; } return results ; } catch ( GenericEntityException e ) { Debug . logError ( e , "Entity error when creating BillingAccount: " + e . getMessage ( ) , module ) ; return ServiceUtil . returnError ( UtilProperties . getMessage ( resource_error , "OrderProblemsCreatingBillingAccount" , locale ) ) ; } catch ( GenericServiceException e ) { Debug . logError ( e , "Service error when creating BillingAccount: " + e . getMessage ( ) , module ) ; return ServiceUtil . returnError ( UtilProperties . getMessage ( resource_error , "OrderProblemsCreatingBillingAccount" , locale ) ) ; } }
helper method to generate a billingaccount ( store credit ) from a return header . this method takes care of all business logic relating to the initialization of a billing account from the return data . the billingaccount . thrudate will be set to ( now + productstore . storecreditvaliddays + end of day ) . the product stores are obtained via the return orders , and the minimum storecreditvaliddays will be used . the default is to set thrudate to null , which implies no expiration . note that we set billingaccount . accountlimit to 0 . 0 for store credits . this is because the available balance of billingaccounts is calculated as accountlimit + sum of payments - sum of invoices .
