Search in sources :

Example 6 with PartnerAddress

use of com.axelor.apps.base.db.PartnerAddress in project axelor-open-suite by axelor.

the class PartnerServiceImpl method updatePartnerAddress.

/**
 * Updates M2O and O2M fields of partner that manage partner addresses. This method ensures
 * consistency between these two fields.
 *
 * @param partner
 * @throws AxelorException
 */
protected void updatePartnerAddress(Partner partner) throws AxelorException {
    Address address = partner.getMainAddress();
    if (!partner.getIsContact() && !partner.getIsEmployee()) {
        if (partner.getPartnerAddressList() != null) {
            partner.setMainAddress(checkDefaultAddress(partner));
        }
    } else if (address == null) {
        partner.removePartnerAddressListItem(JPA.all(PartnerAddress.class).filter("self.partner = :partnerId AND self.isDefaultAddr = 't'").bind("partnerId", partner.getId()).fetchOne());
    } else if (partner.getPartnerAddressList() != null && partner.getPartnerAddressList().stream().map(PartnerAddress::getAddress).noneMatch(address::equals)) {
        PartnerAddress mainAddress = new PartnerAddress();
        mainAddress.setAddress(address);
        mainAddress.setIsDefaultAddr(true);
        mainAddress.setIsDeliveryAddr(true);
        mainAddress.setIsInvoicingAddr(true);
        partner.addPartnerAddressListItem(mainAddress);
    }
}
Also used : EmailAddress(com.axelor.apps.message.db.EmailAddress) PartnerAddress(com.axelor.apps.base.db.PartnerAddress) Address(com.axelor.apps.base.db.Address) PartnerAddress(com.axelor.apps.base.db.PartnerAddress)

Example 7 with PartnerAddress

use of com.axelor.apps.base.db.PartnerAddress in project axelor-open-suite by axelor.

the class AddressController method createPartnerAddress.

public void createPartnerAddress(ActionRequest request, ActionResponse response) {
    Context context = request.getContext();
    Context parentContext = context.getParent();
    if (parentContext.isEmpty()) {
        return;
    }
    String parentModel = (String) parentContext.get("_model");
    LOG.debug("Create partner address : Parent model = {}", parentModel);
    String partnerField = PartnerAddressRepository.modelPartnerFieldMap.get(parentModel);
    LOG.debug("Create partner address : Parent field = {}", partnerField);
    Partner partner = null;
    if (parentContext.get(partnerField) instanceof Partner) {
        partner = (Partner) parentContext.get(partnerField);
    } else if (parentContext.get(partnerField) instanceof Map) {
        partner = Mapper.toBean(Partner.class, (Map<String, Object>) parentContext.get(partnerField));
    }
    LOG.debug("Create partner address : Partner = {}", partner);
    if (partner == null || partner.getId() == null) {
        return;
    }
    Address address = context.asType(Address.class);
    PartnerAddress partnerAddress = Beans.get(PartnerAddressRepository.class).all().filter("self.partner.id = ? AND self.address.id = ?", partner.getId(), address.getId()).fetchOne();
    LOG.debug("Create partner address : Partner Address = {}", partnerAddress);
    if (partnerAddress == null) {
        partner = Beans.get(PartnerRepository.class).find(partner.getId());
        address = Beans.get(AddressRepository.class).find(address.getId());
        Boolean invoicing = (Boolean) context.get("isInvoicingAddr");
        if (invoicing == null) {
            invoicing = false;
        }
        Boolean delivery = (Boolean) context.get("isDeliveryAddr");
        if (delivery == null) {
            delivery = false;
        }
        Boolean isDefault = (Boolean) context.get("isDefault");
        if (isDefault == null) {
            isDefault = false;
        }
        PartnerService partnerService = Beans.get(PartnerService.class);
        partnerService.addPartnerAddress(partner, address, isDefault, invoicing, delivery);
        partnerService.savePartner(partner);
    }
}
Also used : Context(com.axelor.rpc.Context) PartnerAddressRepository(com.axelor.apps.base.db.repo.PartnerAddressRepository) PartnerAddress(com.axelor.apps.base.db.PartnerAddress) Address(com.axelor.apps.base.db.Address) PartnerAddress(com.axelor.apps.base.db.PartnerAddress) PartnerService(com.axelor.apps.base.service.PartnerService) Partner(com.axelor.apps.base.db.Partner) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with PartnerAddress

use of com.axelor.apps.base.db.PartnerAddress in project axelor-open-suite by axelor.

the class MapRest method getPartner.

@Path("/partner/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonNode getPartner(@PathParam("id") long id) {
    ObjectNode mainNode = nodeFactory.objectNode();
    try {
        Partner partner = partnerRepo.find(id);
        if (partner == null) {
            throw new AxelorException(Partner.class, TraceBackRepository.CATEGORY_NO_VALUE, I18n.get(IExceptionMessage.PARTNER_NOT_FOUND));
        }
        ArrayNode arrayNode = nodeFactory.arrayNode();
        String pinColor = getPinColor(partner);
        List<PartnerAddress> partnerAddressList = partner.getPartnerAddressList() != null ? partner.getPartnerAddressList() : Collections.emptyList();
        for (PartnerAddress partnerAddress : partnerAddressList) {
            Address address = partnerAddress.getAddress();
            if (address == null || !address.getIsValidLatLong()) {
                continue;
            }
            ObjectNode objectNode = nodeFactory.objectNode();
            if (!StringUtils.isBlank(address.getFullName())) {
                String addressString = mapRestService.makeAddressString(address, objectNode);
                if (StringUtils.isBlank(addressString)) {
                    continue;
                }
                objectNode.put("address", addressString);
            } else {
                continue;
            }
            objectNode.put("fullName", getFullName(partnerAddress));
            if (partnerAddress.getIsDefaultAddr()) {
                if (!StringUtils.isBlank(partner.getFixedPhone())) {
                    objectNode.put("fixedPhone", partner.getFixedPhone());
                }
                if (partner.getEmailAddress() != null) {
                    objectNode.put("emailAddress", partner.getEmailAddress().getAddress());
                }
            }
            objectNode.put("pinColor", pinColor);
            objectNode.put("pinChar", getPinChar(partnerAddress));
            arrayNode.add(objectNode);
        }
        mapRestService.setData(mainNode, arrayNode);
    } catch (Exception e) {
        mapRestService.setError(mainNode, e);
    }
    return mainNode;
}
Also used : AxelorException(com.axelor.exception.AxelorException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PartnerAddress(com.axelor.apps.base.db.PartnerAddress) Address(com.axelor.apps.base.db.Address) PartnerAddress(com.axelor.apps.base.db.PartnerAddress) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Partner(com.axelor.apps.base.db.Partner) AxelorException(com.axelor.exception.AxelorException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 9 with PartnerAddress

use of com.axelor.apps.base.db.PartnerAddress in project axelor-open-suite by axelor.

the class SyncContactService method createPartnerAddress.

protected void createPartnerAddress(Partner partner, Address partnerAddr) {
    PartnerAddress partnerAddress = new PartnerAddress();
    partnerAddress.setAddress(partnerAddr);
    partnerAddress.setImportOrigin(importOrigin);
    partnerAddress.setPartner(partner);
    partnerAddress.setIsDefaultAddr(true);
    partnerAddress.setIsDeliveryAddr(true);
    partnerAddress.setIsInvoicingAddr(true);
}
Also used : PartnerAddress(com.axelor.apps.base.db.PartnerAddress)

Example 10 with PartnerAddress

use of com.axelor.apps.base.db.PartnerAddress in project axelor-open-suite by axelor.

the class PartnerServiceImpl method addPartnerAddress.

@Override
public Partner addPartnerAddress(Partner partner, Address address, Boolean isDefault, Boolean isInvoicing, Boolean isDelivery) {
    PartnerAddress partnerAddress = createPartnerAddress(address, isDefault);
    if (isDefault != null && isDefault) {
        LOG.debug("Add partner address : isDelivery = {}", isDelivery);
        LOG.debug("Add partner address : isInvoicing = {}", isInvoicing);
        String query = String.format(" AND self.isDeliveryAddr = %s AND self.isInvoicingAddr = %s", isDelivery, isInvoicing);
        resetDefaultAddress(partner, query);
    }
    partnerAddress.setIsInvoicingAddr(isInvoicing);
    partnerAddress.setIsDeliveryAddr(isDelivery);
    partnerAddress.setIsDefaultAddr(isDefault);
    partner.addPartnerAddressListItem(partnerAddress);
    return partner;
}
Also used : PartnerAddress(com.axelor.apps.base.db.PartnerAddress)

Aggregations

PartnerAddress (com.axelor.apps.base.db.PartnerAddress)10 Address (com.axelor.apps.base.db.Address)4 Partner (com.axelor.apps.base.db.Partner)3 PartnerAddressRepository (com.axelor.apps.base.db.repo.PartnerAddressRepository)3 EmailAddress (com.axelor.apps.message.db.EmailAddress)2 AxelorException (com.axelor.exception.AxelorException)2 PartnerService (com.axelor.apps.base.service.PartnerService)1 Context (com.axelor.rpc.Context)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Transactional (com.google.inject.persist.Transactional)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1