pl.aptekhurt.service.PharmacyOrderService.java Source code

Java tutorial

Introduction

Here is the source code for pl.aptekhurt.service.PharmacyOrderService.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.aptekhurt.service;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.springframework.data.jpa.domain.Specifications.where;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pl.aptekhurt.domain.Percentage;
import pl.aptekhurt.domain.Pharmacy;
import pl.aptekhurt.domain.Pharmacy_order;
import pl.aptekhurt.domain.Product_option;
import pl.aptekhurt.domain.User;
import static pl.aptekhurt.domain.specification.Pharmacy_orderSpecifications.isNotRealised;
import static pl.aptekhurt.domain.specification.Pharmacy_orderSpecifications.productOptionIs;
import pl.aptekhurt.repository.Pharmacy_orderRepository;
import pl.aptekhurt.repository.Product_optionRepository;
import pl.aptekhurt.repository.UserRepository;
import pl.aptekhurt.repository.search.Pharmacy_orderSearchRepository;
import pl.aptekhurt.web.rest.Pharmacy_orderResource;
import pl.aptekhurt.web.rest.dto.NewPharmacyOrderDTO;
import pl.aptekhurt.web.rest.dto.PharmacyOrderDTO;
import pl.aptekhurt.web.rest.dto.ProductDTO;
import pl.aptekhurt.web.rest.dto.Product_optionDTO;

/**
 *
 * @author Szymon
 */
@Service
@Transactional
public class PharmacyOrderService {

    private final Logger log = LoggerFactory.getLogger(PharmacyOrderService.class);

    @PersistenceContext
    EntityManager em;

    @Inject
    private Product_optionRepository product_optionRepository;

    @Inject
    private UserRepository userRepository;

    @Inject
    private Pharmacy_orderRepository pharmacy_orderRepository;

    @Inject
    private MailService mailService;

    private static User ADMIN_USER;

    public void createPharmacyOrder(List<NewPharmacyOrderDTO> productOrderDTOs, Principal principal) {
        for (NewPharmacyOrderDTO productOrderDTO : productOrderDTOs) {
            Pharmacy_order pharmacy_order = new Pharmacy_order();
            Product_option productOption = product_optionRepository.getOne(productOrderDTO.getProductOptionId());
            pharmacy_order.setProduct_Option(productOption);
            pharmacy_order.setQuantity(productOrderDTO.getQuantity());
            pharmacy_order.setCreation_date(new DateTime(DateTimeZone.getDefault()));
            DateTime temporaryDateTime = new DateTime(DateTimeZone.getDefault());
            switch (productOrderDTO.getTermin()) {
            case 1:
                temporaryDateTime = new DateTime().dayOfMonth().withMinimumValue().withTimeAtStartOfDay();
                break;
            case 16:
                temporaryDateTime = new DateTime().withDayOfMonth(16).withTimeAtStartOfDay();
                break;
            }
            if (temporaryDateTime.isBeforeNow()) {
                temporaryDateTime = temporaryDateTime.plusMonths(1);
            }
            pharmacy_order.setTermin(temporaryDateTime);
            Pharmacy pharmacy = userRepository.findOneByLogin(principal.getName()).get().getPharmacy();
            pharmacy_order.setPharmacy(pharmacy);
            log.debug("REST request to save Pharmacy_order : {}", pharmacy_order);
            if (hasNewLimitBeenReached(productOption, productOrderDTO.getQuantity())) {
                if (ADMIN_USER == null) {
                    ADMIN_USER = userRepository.findOneByLogin("admin").get();
                }
                mailService.sendNewLimitReachedEmail(ADMIN_USER, productOption,
                        pharmacy_order.getQuantity() + productOrderDTO.getQuantity());
            }
            pharmacy_orderRepository.save(pharmacy_order);
        }
        //TODO: Send email with all that products
    }

    private boolean hasNewLimitBeenReached(Product_option oldVersionOfroductOption, Integer orderedQuantity) {
        Integer ordered = 0;
        Integer nextLevel = null;
        for (Pharmacy_order order : pharmacy_orderRepository
                .findAll(where(productOptionIs(oldVersionOfroductOption)).and(isNotRealised()))) {
            ordered = ordered + order.getQuantity();
        }

        for (Percentage perc : oldVersionOfroductOption.getPercentages()) {
            if (ordered < perc.getLevel()) {
                nextLevel = perc.getLevel();
            } else {
                break;
            }
        }
        if (nextLevel == null) {
            return false;
        }
        return ordered + orderedQuantity >= nextLevel;
    }

    private String newLimitMessage(Product_option productOption) {
        StringBuilder sb = new StringBuilder();
        sb.append("Produkt: ").append(productOption.getProduct().getName()).append(" ")
                .append(productOption.getName()).append(" osign nowy prg rabatowy!");
        return sb.toString();
    }

    public List<PharmacyOrderDTO> getPharmacyOrders(List<Pharmacy_order> pharmacyOrders) {
        List<PharmacyOrderDTO> ordersDTO = new ArrayList<>();
        for (Pharmacy_order order : pharmacyOrders) {
            PharmacyOrderDTO orderDTO = new PharmacyOrderDTO();
            orderDTO.setId(order.getId());
            orderDTO.setProductId(order.getProduct_Option().getProduct().getId());
            orderDTO.setProductOptionId(order.getProduct_Option().getId());
            orderDTO.setProductName(order.getProduct_Option().getProduct().getName());
            orderDTO.setProductOptionName(order.getProduct_Option().getName());
            orderDTO.setCreation_date(order.getCreation_date());
            orderDTO.setModification_date(order.getModification_date());
            orderDTO.setPharmacy(order.getPharmacy());
            orderDTO.setQuantity(order.getQuantity());
            orderDTO.setRealization_date(order.getRealization_date());
            orderDTO.setReceived_discount(order.getReceived_discount());
            orderDTO.setTermin(order.getTermin());
            if (order.getHurt_Order() != null) {
                orderDTO.setHurtOrderId(order.getHurt_Order().getId());
                orderDTO.setHurtName(order.getHurt_Order().getHurt().getName());
                orderDTO.setHurtId(order.getHurt_Order().getHurt().getId());
            }
            Product_optionDTO product_optionDTO = new Product_optionDTO();
            product_optionDTO.setId(order.getProduct_Option().getId());
            product_optionDTO.setName(order.getProduct_Option().getName());
            ProductDTO productDTO = new ProductDTO();
            productDTO.setId(order.getProduct_Option().getProduct().getId());
            productDTO.setName(order.getProduct_Option().getProduct().getName());
            product_optionDTO.setProduct(productDTO);
            orderDTO.setProduct_Option(product_optionDTO);
            ordersDTO.add(orderDTO);
        }
        return ordersDTO;
    }
}