Search in sources :

Example 1 with PartnerSaleService

use of com.axelor.apps.sale.service.PartnerSaleService in project axelor-open-suite by axelor.

the class PartnerSaleController method displayValues.

public void displayValues(ActionRequest request, ActionResponse response) {
    Partner customer = request.getContext().asType(Partner.class);
    try {
        customer = Beans.get(PartnerRepository.class).find(customer.getId());
        SortedSet<Map<String, Object>> saleDetailsByProduct = new TreeSet<Map<String, Object>>(Comparator.comparing(m -> (String) m.get("name")));
        PartnerSaleService partnerSaleService = Beans.get(PartnerSaleService.class);
        List<Product> productList = partnerSaleService.getProductBoughtByCustomer(customer);
        if (productList.isEmpty()) {
            response.setAttr("$saleDetailsByProduct", "hidden", true);
            return;
        }
        response.setAttr("$saleDetailsByProduct", "hidden", false);
        HashMap<String, BigDecimal> qtyAndPrice;
        for (Product product : productList) {
            qtyAndPrice = partnerSaleService.getTotalSaleQuantityAndPrice(customer, product);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", product.getName());
            map.put("$quantitySold", qtyAndPrice.get("qty"));
            map.put("$totalPrice", qtyAndPrice.get("price"));
            map.put("$averagePrice", qtyAndPrice.get("price").divide(qtyAndPrice.get("qty"), AppBaseService.DEFAULT_NB_DECIMAL_DIGITS, RoundingMode.HALF_EVEN));
            saleDetailsByProduct.add(map);
        }
        response.setValue("$saleDetailsByProduct", saleDetailsByProduct);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : SortedSet(java.util.SortedSet) TraceBackService(com.axelor.exception.service.TraceBackService) PartnerRepository(com.axelor.apps.base.db.repo.PartnerRepository) HashMap(java.util.HashMap) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) TreeSet(java.util.TreeSet) PartnerSaleService(com.axelor.apps.sale.service.PartnerSaleService) BigDecimal(java.math.BigDecimal) List(java.util.List) Beans(com.axelor.inject.Beans) Product(com.axelor.apps.base.db.Product) ActionResponse(com.axelor.rpc.ActionResponse) Map(java.util.Map) ActionRequest(com.axelor.rpc.ActionRequest) Comparator(java.util.Comparator) Partner(com.axelor.apps.base.db.Partner) Context(com.axelor.rpc.Context) Singleton(com.google.inject.Singleton) RoundingMode(java.math.RoundingMode) HashMap(java.util.HashMap) Product(com.axelor.apps.base.db.Product) BigDecimal(java.math.BigDecimal) TreeSet(java.util.TreeSet) PartnerSaleService(com.axelor.apps.sale.service.PartnerSaleService) Partner(com.axelor.apps.base.db.Partner) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Partner (com.axelor.apps.base.db.Partner)1 Product (com.axelor.apps.base.db.Product)1 PartnerRepository (com.axelor.apps.base.db.repo.PartnerRepository)1 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)1 PartnerSaleService (com.axelor.apps.sale.service.PartnerSaleService)1 TraceBackService (com.axelor.exception.service.TraceBackService)1 Beans (com.axelor.inject.Beans)1 ActionRequest (com.axelor.rpc.ActionRequest)1 ActionResponse (com.axelor.rpc.ActionResponse)1 Context (com.axelor.rpc.Context)1 Singleton (com.google.inject.Singleton)1 BigDecimal (java.math.BigDecimal)1 RoundingMode (java.math.RoundingMode)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)1