List<EntityExpr> exprs = UtilMisc.toList(EntityCondition.makeCondition("finAccountTransTypeId", EntityOperator.EQUALS, "DEPOSIT"),
EntityCondition.makeCondition("finAccountId", EntityOperator.EQUALS, finAccountId));
EntityCondition condition = EntityCondition.makeCondition(exprs, EntityOperator.AND);
EntityListIterator eli = null;
try {
eli = delegator.find("FinAccountTrans", condition, null, null, UtilMisc.toList("-transactionDate"), null);
GenericValue trans;
while (remainingBalance.compareTo(FinAccountHelper.ZERO) < 0 && (trans = eli.next()) != null) {
String orderId = trans.getString("orderId");
String orderItemSeqId = trans.getString("orderItemSeqId");
// make sure there is an order available to refund
if (orderId != null && orderItemSeqId != null) {
GenericValue orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId",orderId));
GenericValue productStore = delegator.getRelatedOne("ProductStore", orderHeader);
GenericValue orderItem = delegator.findByPrimaryKey("OrderItem", UtilMisc.toMap("orderId", orderId, "orderItemSeqId", orderItemSeqId));
if (!"ITEM_CANCELLED".equals(orderItem.getString("statusId"))) {
// make sure the item hasn't already been returned
List<GenericValue> returnItems = orderItem.getRelated("ReturnItem");
if (UtilValidate.isEmpty(returnItems)) {
BigDecimal txAmt = trans.getBigDecimal("amount");
BigDecimal refAmt = txAmt;
if (remainingBalance.compareTo(txAmt) == -1) {
refAmt = remainingBalance;
}
remainingBalance = remainingBalance.subtract(refAmt);
refundAmount = refundAmount.add(refAmt);
// create the return header
Map<String, Object> rhCtx = UtilMisc.toMap("returnHeaderTypeId", "CUSTOMER_RETURN", "fromPartyId", finAccount.getString("ownerPartyId"), "toPartyId", productStore.getString("payToPartyId"), "userLogin", userLogin);
Map<String, Object> rhResp = dispatcher.runSync("createReturnHeader", rhCtx);
if (ServiceUtil.isError(rhResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(rhResp));
}
String returnId = (String) rhResp.get("returnId");
// create the return item
Map<String, Object> returnItemCtx = FastMap.newInstance();
returnItemCtx.put("returnId", returnId);
returnItemCtx.put("orderId", orderId);
returnItemCtx.put("description", orderItem.getString("itemDescription"));
returnItemCtx.put("orderItemSeqId", orderItemSeqId);
returnItemCtx.put("returnQuantity", BigDecimal.ONE);
returnItemCtx.put("receivedQuantity", BigDecimal.ONE);
returnItemCtx.put("returnPrice", refAmt);
returnItemCtx.put("returnReasonId", "RTN_NOT_WANT");
returnItemCtx.put("returnTypeId", "RTN_REFUND"); // refund return
returnItemCtx.put("returnItemTypeId", "RET_NPROD_ITEM");
returnItemCtx.put("userLogin", userLogin);
Map<String, Object> retItResp = dispatcher.runSync("createReturnItem", returnItemCtx);
if (ServiceUtil.isError(retItResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(retItResp));
}
String returnItemSeqId = (String) retItResp.get("returnItemSeqId");
// approve the return
Map<String, Object> appRet = UtilMisc.toMap("statusId", "RETURN_ACCEPTED", "returnId", returnId, "userLogin", userLogin);
Map<String, Object> appResp = dispatcher.runSync("updateReturnHeader", appRet);
if (ServiceUtil.isError(appResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(appResp));
}
// "receive" the return - should trigger the refund
Map<String, Object> recRet = UtilMisc.toMap("statusId", "RETURN_RECEIVED", "returnId", returnId, "userLogin", userLogin);
Map<String, Object> recResp = dispatcher.runSync("updateReturnHeader", recRet);
if (ServiceUtil.isError(recResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(recResp));
}
// get the return item
GenericValue returnItem = delegator.findByPrimaryKey("ReturnItem",
UtilMisc.toMap("returnId", returnId, "returnItemSeqId", returnItemSeqId));
GenericValue response = returnItem.getRelatedOne("ReturnItemResponse");
if (response == null) {
throw new GeneralException("No return response found for: " + returnItem.getPrimaryKey());
}
String paymentId = response.getString("paymentId");
// create the adjustment transaction
Map<String, Object> txCtx = FastMap.newInstance();
txCtx.put("finAccountTransTypeId", "ADJUSTMENT");
txCtx.put("finAccountId", finAccountId);
txCtx.put("orderId", orderId);
txCtx.put("orderItemSeqId", orderItemSeqId);
txCtx.put("paymentId", paymentId);
txCtx.put("amount", refAmt.negate());
txCtx.put("partyId", finAccount.getString("ownerPartyId"));
txCtx.put("userLogin", userLogin);
Map<String, Object> txResp = dispatcher.runSync("createFinAccountTrans", txCtx);
if (ServiceUtil.isError(txResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(txResp));
}
}
}
}
}
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
} finally {
if (eli != null) {
try {
eli.close();
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
}
}
}