Search in sources :

Example 91 with LocalDispatcher

use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.

the class FinAccountServices method createFinAccountForStore.

public static Map<String, Object> createFinAccountForStore(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    String productStoreId = (String) context.get("productStoreId");
    String finAccountTypeId = (String) context.get("finAccountTypeId");
    Locale locale = (Locale) context.get("locale");
    GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator);
    try {
        // get the product store id and use it to generate a unique fin account code
        GenericValue productStoreFinAccountSetting = EntityQuery.use(delegator).from("ProductStoreFinActSetting").where("productStoreId", productStoreId, "finAccountTypeId", finAccountTypeId).cache().queryOne();
        if (productStoreFinAccountSetting == null) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingFinAccountSetting", UtilMisc.toMap("productStoreId", productStoreId, "finAccountTypeId", finAccountTypeId), locale));
        }
        Long accountCodeLength = productStoreFinAccountSetting.getLong("accountCodeLength");
        Long accountValidDays = productStoreFinAccountSetting.getLong("accountValidDays");
        Long pinCodeLength = productStoreFinAccountSetting.getLong("pinCodeLength");
        String requirePinCode = productStoreFinAccountSetting.getString("requirePinCode");
        // automatically set the parameters for the create fin account service
        ModelService createService = dctx.getModelService("createFinAccount");
        Map<String, Object> inContext = createService.makeValid(context, ModelService.IN_PARAM);
        Timestamp now = UtilDateTime.nowTimestamp();
        // now use our values
        String finAccountCode = null;
        if (UtilValidate.isNotEmpty(accountCodeLength)) {
            finAccountCode = FinAccountHelper.getNewFinAccountCode(accountCodeLength.intValue(), delegator);
            inContext.put("finAccountCode", finAccountCode);
        }
        // with pin codes, the account code becomes the ID and the pin becomes the code
        if ("Y".equalsIgnoreCase(requirePinCode)) {
            String pinCode = FinAccountHelper.getNewFinAccountCode(pinCodeLength.intValue(), delegator);
            inContext.put("finAccountPin", pinCode);
        }
        // set the dates/userlogin
        if (UtilValidate.isNotEmpty(accountValidDays)) {
            inContext.put("thruDate", UtilDateTime.getDayEnd(now, accountValidDays));
        }
        inContext.put("fromDate", now);
        inContext.put("userLogin", userLogin);
        // product store payToPartyId
        String payToPartyId = ProductStoreWorker.getProductStorePayToPartyId(productStoreId, delegator);
        inContext.put("organizationPartyId", payToPartyId);
        inContext.put("currencyUomId", productStore.get("defaultCurrencyUomId"));
        Map<String, Object> createResult = dispatcher.runSync("createFinAccount", inContext);
        if (ServiceUtil.isError(createResult)) {
            return ServiceUtil.returnError(ServiceUtil.getErrorMessage(createResult));
        }
        Map<String, Object> result = ServiceUtil.returnSuccess();
        result.put("finAccountId", createResult.get("finAccountId"));
        result.put("finAccountCode", finAccountCode);
        return result;
    } catch (GenericEntityException | GenericServiceException ex) {
        return ServiceUtil.returnError(ex.getMessage());
    }
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Timestamp(java.sql.Timestamp) ModelService(org.apache.ofbiz.service.ModelService) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 92 with LocalDispatcher

use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.

the class FinAccountProductServices method createPartyFinAccountFromPurchase.

public static Map<String, Object> createPartyFinAccountFromPurchase(DispatchContext dctx, Map<String, Object> context) {
    // this service should always be called via FULFILLMENT_EXTASYNC
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Delegator delegator = dctx.getDelegator();
    Locale locale = (Locale) context.get("locale");
    GenericValue orderItem = (GenericValue) context.get("orderItem");
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    // order ID for tracking
    String orderId = orderItem.getString("orderId");
    String orderItemSeqId = orderItem.getString("orderItemSeqId");
    // the order header for store info
    GenericValue orderHeader;
    try {
        orderHeader = orderItem.getRelatedOne("OrderHeader", false);
    } catch (GenericEntityException e) {
        Debug.logError(e, "Unable to get OrderHeader from OrderItem", module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceOrderError, "OrderCannotGetOrderHeader", UtilMisc.toMap("orderId", orderId), locale));
    }
    String productId = orderItem.getString("productId");
    GenericValue featureAndAppl;
    try {
        List<GenericValue> featureAndAppls = EntityQuery.use(delegator).from("ProductFeatureAndAppl").where("productId", productId, "productFeatureTypeId", "TYPE", "productFeatureApplTypeId", "STANDARD_FEATURE").queryList();
        featureAndAppls = EntityUtil.filterByDate(featureAndAppls);
        featureAndAppl = EntityUtil.getFirst(featureAndAppls);
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    // financial account data; pulled from the TYPE feature of the product
    // default
    String finAccountTypeId = "BALANCE_ACCOUNT";
    String finAccountName = "Customer Financial Account";
    if (featureAndAppl != null) {
        if (UtilValidate.isNotEmpty(featureAndAppl.getString("idCode"))) {
            finAccountTypeId = featureAndAppl.getString("idCode");
        }
        if (UtilValidate.isNotEmpty(featureAndAppl.getString("description"))) {
            finAccountName = featureAndAppl.getString("description");
        }
    }
    // locate the financial account type
    GenericValue finAccountType;
    try {
        finAccountType = EntityQuery.use(delegator).from("FinAccountType").where("finAccountTypeId", finAccountTypeId).queryOne();
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    String replenishEnumId = finAccountType.getString("replenishEnumId");
    // get the order read helper
    OrderReadHelper orh = new OrderReadHelper(orderHeader);
    // get the currency
    String currency = orh.getCurrency();
    // make sure we have a currency
    if (currency == null) {
        currency = EntityUtilProperties.getPropertyValue("general", "currency.uom.id.default", "USD", delegator);
    }
    // get the product store
    String productStoreId = null;
    if (orderHeader != null) {
        productStoreId = orh.getProductStoreId();
    }
    if (productStoreId == null) {
        Debug.logFatal("Unable to create financial accout; no productStoreId on OrderHeader : " + orderId, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingFinAccountCannotCreate", UtilMisc.toMap("orderId", orderId), locale));
    }
    // party ID (owner)
    GenericValue billToParty = orh.getBillToParty();
    String partyId = null;
    if (billToParty != null) {
        partyId = billToParty.getString("partyId");
    }
    // payment method info
    List<GenericValue> payPrefs = orh.getPaymentPreferences();
    String paymentMethodId = null;
    if (payPrefs != null) {
        for (GenericValue pref : payPrefs) {
            // needs to be a CC or EFT account
            String type = pref.getString("paymentMethodTypeId");
            if ("CREDIT_CARD".equals(type) || "EFT_ACCOUNT".equals(type)) {
                paymentMethodId = pref.getString("paymentMethodId");
            }
        }
    }
    // some person data for expanding
    GenericValue partyGroup = null;
    GenericValue person = null;
    GenericValue party = null;
    if (billToParty != null) {
        try {
            party = billToParty.getRelatedOne("Party", false);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
        if (party != null) {
            String partyTypeId = party.getString("partyTypeId");
            if ("PARTY_GROUP".equals(partyTypeId)) {
                partyGroup = billToParty;
            } else if ("PERSON".equals(partyTypeId)) {
                person = billToParty;
            }
        }
    }
    // create the context for FSE
    Map<String, Object> expContext = new HashMap<>();
    expContext.put("orderHeader", orderHeader);
    expContext.put("orderItem", orderItem);
    expContext.put("party", party);
    expContext.put("person", person);
    expContext.put("partyGroup", partyGroup);
    // expand the name field to dynamically add information
    FlexibleStringExpander exp = FlexibleStringExpander.getInstance(finAccountName);
    finAccountName = exp.expandString(expContext);
    // price/amount/quantity to create initial deposit amount
    BigDecimal quantity = orderItem.getBigDecimal("quantity");
    BigDecimal price = orderItem.getBigDecimal("unitPrice");
    BigDecimal deposit = price.multiply(quantity).setScale(FinAccountHelper.decimals, FinAccountHelper.rounding);
    // create the financial account
    Map<String, Object> createCtx = new HashMap<>();
    String finAccountId;
    createCtx.put("finAccountTypeId", finAccountTypeId);
    createCtx.put("finAccountName", finAccountName);
    createCtx.put("productStoreId", productStoreId);
    createCtx.put("ownerPartyId", partyId);
    createCtx.put("currencyUomId", currency);
    createCtx.put("statusId", "FNACT_ACTIVE");
    createCtx.put("userLogin", userLogin);
    // if we auto-replenish this type; set the level to the initial deposit
    if (replenishEnumId != null && "FARP_AUTOMATIC".equals(replenishEnumId)) {
        createCtx.put("replenishLevel", deposit);
        createCtx.put("replenishPaymentId", paymentMethodId);
    }
    Map<String, Object> createResp;
    try {
        createResp = dispatcher.runSync("createFinAccountForStore", createCtx);
    } catch (GenericServiceException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    if (ServiceUtil.isError(createResp)) {
        Debug.logFatal(ServiceUtil.getErrorMessage(createResp), module);
        return ServiceUtil.returnError(ServiceUtil.getErrorMessage(createResp));
    }
    finAccountId = (String) createResp.get("finAccountId");
    // create the owner role
    Map<String, Object> roleCtx = new HashMap<>();
    roleCtx.put("partyId", partyId);
    roleCtx.put("roleTypeId", "OWNER");
    roleCtx.put("finAccountId", finAccountId);
    roleCtx.put("userLogin", userLogin);
    roleCtx.put("fromDate", UtilDateTime.nowTimestamp());
    Map<String, Object> roleResp;
    try {
        roleResp = dispatcher.runSync("createFinAccountRole", roleCtx);
    } catch (GenericServiceException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    if (ServiceUtil.isError(roleResp)) {
        Debug.logFatal(ServiceUtil.getErrorMessage(roleResp), module);
        return ServiceUtil.returnError(ServiceUtil.getErrorMessage(roleResp));
    }
    // create the initial deposit
    Map<String, Object> depositCtx = new HashMap<>();
    depositCtx.put("finAccountId", finAccountId);
    depositCtx.put("productStoreId", productStoreId);
    depositCtx.put("currency", currency);
    depositCtx.put("partyId", partyId);
    depositCtx.put("orderId", orderId);
    depositCtx.put("orderItemSeqId", orderItemSeqId);
    depositCtx.put("amount", deposit);
    depositCtx.put("reasonEnumId", "FATR_IDEPOSIT");
    depositCtx.put("userLogin", userLogin);
    Map<String, Object> depositResp;
    try {
        depositResp = dispatcher.runSync("finAccountDeposit", depositCtx);
    } catch (GenericServiceException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    if (ServiceUtil.isError(depositResp)) {
        Debug.logFatal(ServiceUtil.getErrorMessage(depositResp), module);
        return ServiceUtil.returnError(ServiceUtil.getErrorMessage(depositResp));
    }
    Map<String, Object> result = ServiceUtil.returnSuccess();
    result.put("finAccountId", finAccountId);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) OrderReadHelper(org.apache.ofbiz.order.order.OrderReadHelper) BigDecimal(java.math.BigDecimal) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) FlexibleStringExpander(org.apache.ofbiz.base.util.string.FlexibleStringExpander) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 93 with LocalDispatcher

use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.

the class InvoiceServices method checkPaymentInvoices.

public static Map<String, Object> checkPaymentInvoices(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    String paymentId = (String) context.get("paymentId");
    try {
        GenericValue payment = EntityQuery.use(delegator).from("Payment").where("paymentId", paymentId).queryOne();
        if (payment == null) {
            throw new GenericServiceException("Payment with ID [" + paymentId + "] not found!");
        }
        List<GenericValue> paymentApplications = payment.getRelated("PaymentApplication", null, null, false);
        if (UtilValidate.isEmpty(paymentApplications)) {
            return ServiceUtil.returnSuccess();
        }
        // TODO: this is inefficient -- instead use HashSet to construct a distinct Set of invoiceIds, then iterate over it and call checkInvoicePaymentAppls
        for (GenericValue paymentApplication : paymentApplications) {
            String invoiceId = paymentApplication.getString("invoiceId");
            if (invoiceId != null) {
                Map<String, Object> serviceResult = dispatcher.runSync("checkInvoicePaymentApplications", UtilMisc.<String, Object>toMap("invoiceId", invoiceId, "userLogin", userLogin));
                if (ServiceUtil.isError(serviceResult)) {
                    return serviceResult;
                }
            }
        }
        return ServiceUtil.returnSuccess();
    } catch (GenericServiceException | GenericEntityException se) {
        Debug.logError(se, se.getMessage(), module);
        return ServiceUtil.returnError(se.getMessage());
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 94 with LocalDispatcher

use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.

the class InvoiceServices method createInvoicesFromShipment.

public static Map<String, Object> createInvoicesFromShipment(DispatchContext dctx, Map<String, Object> context) {
    // Delegator delegator = dctx.getDelegator();
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    String shipmentId = (String) context.get("shipmentId");
    Locale locale = (Locale) context.get("locale");
    List<String> invoicesCreated;
    Map<String, Object> response = ServiceUtil.returnSuccess();
    GenericValue orderShipment = null;
    String invoicePerShipment = null;
    try {
        orderShipment = EntityQuery.use(delegator).from("OrderShipment").where("shipmentId", shipmentId).queryFirst();
    } catch (GenericEntityException e) {
        return ServiceUtil.returnError(e.getMessage());
    }
    if (orderShipment != null) {
        String orderId = orderShipment.getString("orderId");
        try {
            GenericValue orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).queryOne();
            invoicePerShipment = orderHeader.getString("invoicePerShipment");
        } catch (GenericEntityException e) {
            return ServiceUtil.returnError(e.getMessage());
        }
    }
    // In either case, use the default value from the properties
    if (invoicePerShipment == null) {
        invoicePerShipment = EntityUtilProperties.getPropertyValue("accounting", "create.invoice.per.shipment", delegator);
    }
    if ("Y".equals(invoicePerShipment)) {
        Map<String, Object> serviceContext = UtilMisc.toMap("shipmentIds", UtilMisc.toList(shipmentId), "eventDate", context.get("eventDate"), "userLogin", context.get("userLogin"));
        try {
            Map<String, Object> result = dispatcher.runSync("createInvoicesFromShipments", serviceContext);
            if (ServiceUtil.isError(result)) {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingCreateInvoicesFromShipmentService", UtilMisc.toMap("shipmentId", shipmentId), locale));
            }
            invoicesCreated = UtilGenerics.checkList(result.get("invoicesCreated"));
        } catch (GenericServiceException e) {
            Debug.logError(e, "Trouble calling createInvoicesFromShipment service; invoice not created for shipment [" + shipmentId + "]", module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingCreateInvoicesFromShipmentService", UtilMisc.toMap("shipmentId", shipmentId), locale));
        }
        response.put("invoicesCreated", invoicesCreated);
    }
    return response;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 95 with LocalDispatcher

use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.

the class InvoiceServices method readyInvoices.

public static Map<String, Object> readyInvoices(DispatchContext dctx, Map<String, Object> context) {
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    // Get invoices to make ready
    List<String> invoicesCreated = UtilGenerics.checkList(context.get("invoicesCreated"));
    String nextStatusId = "INVOICE_READY";
    try {
        for (String invoiceId : invoicesCreated) {
            Map<String, Object> setInvoiceStatusResult = dispatcher.runSync("setInvoiceStatus", UtilMisc.<String, Object>toMap("invoiceId", invoiceId, "statusId", nextStatusId, "userLogin", userLogin));
            if (ServiceUtil.isError(setInvoiceStatusResult)) {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingInvoiceCommissionError", locale), null, null, setInvoiceStatusResult);
            }
        }
    } catch (GenericServiceException e) {
        Debug.logError(e, "Entity/data problem creating commission invoice: " + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingInvoiceCommissionError", UtilMisc.toMap("reason", e.toString()), locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Aggregations

LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)427 GenericValue (org.apache.ofbiz.entity.GenericValue)356 Delegator (org.apache.ofbiz.entity.Delegator)324 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)321 Locale (java.util.Locale)296 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)270 HashMap (java.util.HashMap)214 BigDecimal (java.math.BigDecimal)135 GeneralException (org.apache.ofbiz.base.util.GeneralException)87 Timestamp (java.sql.Timestamp)81 LinkedList (java.util.LinkedList)79 IOException (java.io.IOException)59 Map (java.util.Map)51 HttpSession (javax.servlet.http.HttpSession)49 OrderReadHelper (org.apache.ofbiz.order.order.OrderReadHelper)28 ModelService (org.apache.ofbiz.service.ModelService)28 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)24 ShoppingCart (org.apache.ofbiz.order.shoppingcart.ShoppingCart)23 Security (org.apache.ofbiz.security.Security)20 ByteBuffer (java.nio.ByteBuffer)19