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);
}
}
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);
}
}
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;
}
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);
}
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;
}
Aggregations