/*
* XP.ERP Project
*
* Copyright (C) 2012 Xpand IT.
*
* This software is proprietary.
*/
package com.xpandit.openbravo.humanresources.handler;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.List;
import javax.enterprise.event.Observes;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.base.model.Property;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBCriteria;
import org.openbravo.dal.service.OBDal;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.model.common.businesspartner.BusinessPartner;
import org.openbravo.model.common.enterprise.OrganizationInformation;
import org.openbravo.model.common.invoice.Invoice;
import org.openbravo.model.common.invoice.InvoiceLine;
import org.openbravo.service.db.DalConnectionProvider;
/**
* The XphrPurchaseInvoiceHandler class. Using for apply rules on create and update purchase invoice
* lines.
*
* @author Joel Latino
*
*/
public class XphrPurchaseInvoiceHandler extends EntityPersistenceEventObserver {
private final static Logger LOG = Logger.getLogger(XphrPurchaseInvoiceHandler.class);
private final static String AL_EE = "AL_EE";
private final static String AL_CC = "AL_CC";
private final static String AL_EC = "AL_EC";
private final static String AL_CE = "AL_CE";
private final static String EMPLYOEE = "EMPLYOEE";
private final static String COMPANY = "COMPANY";
private static Entity[] entities = {
ModelProvider.getInstance().getEntity(InvoiceLine.ENTITY_NAME),
ModelProvider.getInstance().getEntity(Invoice.ENTITY_NAME) };
@Override
protected Entity[] getObservedEntities() {
return entities;
}
public void onSave(@Observes EntityNewEvent event) {
if (!isValidEvent(event)) {
return;
}
if (event.getTargetInstance() instanceof InvoiceLine) {
boolean isValid = isValidPayType(event);
if (!isValid) {
// throw exception interrupt the save action
String language = OBContext.getOBContext().getLanguage().getLanguage();
ConnectionProvider conn = new DalConnectionProvider(false);
throw new OBException(Utility.messageBD(conn, "XPHR_InvalidPayTypeInInvoiceLine", language));
}
} else if (event.getTargetInstance() instanceof Invoice) {
// Get's the current Invoice object
final Invoice invoice = (Invoice) event.getTargetInstance();
final List invoiceLineList = invoice.getInvoiceLineList();
for (InvoiceLine invoiceLine : invoiceLineList) {
boolean isValid = isValidPayType(invoiceLine);
if (!isValid) {
// throw exception interrupt the save action
String language = OBContext.getOBContext().getLanguage().getLanguage();
ConnectionProvider conn = new DalConnectionProvider(false);
final String message = Utility.messageBD(conn, "XPHR_InvalidPayTypeInInvoice", language);
throw new OBException(MessageFormat.format(message, invoiceLine.getLineNo().toString()));
}
}
}
}
@SuppressWarnings("unchecked")
public void onUpdate(@Observes EntityUpdateEvent event) {
if (!isValidEvent(event)) {
return;
}
if (event.getTargetInstance() instanceof InvoiceLine) {
boolean isValid = isValidPayType(event);
if (!isValid) {
// throw exception interrupt the save action
String language = OBContext.getOBContext().getLanguage().getLanguage();
ConnectionProvider conn = new DalConnectionProvider(false);
throw new OBException(Utility.messageBD(conn, "XPHR_InvalidPayTypeInInvoiceLine", language));
}
} else if (event.getTargetInstance() instanceof Invoice) {
// TODO: This code not working as expected
// Get's the current Invoice object
final Invoice invoice = (Invoice) event.getTargetInstance();
String invoicePayType = EMPLYOEE;
if (checkIsValidPayType(invoice, COMPANY)) {
invoicePayType = COMPANY;
}
final Entity invoiceEntity = ModelProvider.getInstance().getEntity(Invoice.ENTITY_NAME);
final Property invoiceLineListProperty = invoiceEntity
.getProperty(Invoice.PROPERTY_INVOICELINELIST);
final List invoiceLines = (List) event
.getCurrentState(invoiceLineListProperty);
for (int i = 0; i < invoiceLines.size(); i++) {
InvoiceLine il = invoiceLines.get(i);
if (checkIsValidPayType(il, COMPANY) && invoicePayType.equals(COMPANY)) {
il.setXphrPaidByType(AL_CC);
} else if (checkIsValidPayType(il, COMPANY) && invoicePayType.equals(EMPLYOEE)) {
il.setXphrPaidByType(AL_CE);
} else if (checkIsValidPayType(il, EMPLYOEE) && invoicePayType.equals(COMPANY)) {
il.setXphrPaidByType(AL_EC);
} else if (checkIsValidPayType(il, EMPLYOEE) && invoicePayType.equals(EMPLYOEE)) {
il.setXphrPaidByType(AL_EE);
}
invoiceLines.set(i, il);
}
event.setCurrentState(invoiceLineListProperty, invoiceLines);
/*
* for (int i = 0; i < invoice.getInvoiceLineList().size(); i++) { if
* (checkIsValidPayType(invoice.getInvoiceLineList().get(i), COMPANY) &&
* invoicePayType.equals(COMPANY)) { invoiceLines.get(i).setXphrPaidByType(AL_CC); } else if
* (checkIsValidPayType(invoice.getInvoiceLineList().get(i), COMPANY) &&
* invoicePayType.equals(EMPLYOEE)) { invoiceLines.get(i).setXphrPaidByType(AL_CE); } else if
* (checkIsValidPayType(invoice.getInvoiceLineList().get(i), EMPLYOEE) &&
* invoicePayType.equals(COMPANY)) { invoiceLines.get(i).setXphrPaidByType(AL_EC); } else if
* (checkIsValidPayType(invoice.getInvoiceLineList().get(i), EMPLYOEE) &&
* invoicePayType.equals(EMPLYOEE)) { invoiceLines.get(i).setXphrPaidByType(AL_EE); } }
*/
}
}
/**
* Check the pay type are corrected.
*
* @param event
* the EntityPersistenceEvent object.
* @return true if the values are corrected.
*/
private boolean isValidPayType(EntityPersistenceEvent event) {
// Get's the current InvoiceLine object
final InvoiceLine invoiceLine = (InvoiceLine) event.getTargetInstance();
return isValidPayType(invoiceLine);
}
/**
* Check the pay type are corrected.
*
* @param invoiceLine
* the InvoiceLine object.
* @return true if the values are corrected.
*/
private boolean isValidPayType(InvoiceLine invoiceLine) {
// Get's the current Invoice object
final Invoice invoice = invoiceLine.getInvoice();
// Check if there're value to validate
if (invoice.getSalesOrder() == null && invoiceLine.getXphrPaidByType() != null
&& (invoice.getXphrPaidBy() != null || invoiceLine.getXphrPaidBy() != null)) {
boolean payTypeInvoice = true;
boolean payTypeInvoiceLine = true;
// validate the association pay type
if (invoiceLine.getXphrPaidByType().equals(AL_CE)) {
payTypeInvoice = checkIsValidPayType(invoice, COMPANY);
payTypeInvoiceLine = checkIsValidPayType(invoiceLine, EMPLYOEE);
return payTypeInvoice && payTypeInvoiceLine;
} else if (invoiceLine.getXphrPaidByType().equals(AL_CC)) {
payTypeInvoice = checkIsValidPayType(invoice, COMPANY);
payTypeInvoiceLine = checkIsValidPayType(invoiceLine, COMPANY);
return payTypeInvoice && payTypeInvoiceLine;
} else if (invoiceLine.getXphrPaidByType().equals(AL_EC)) {
payTypeInvoice = checkIsValidPayType(invoice, EMPLYOEE);
payTypeInvoiceLine = checkIsValidPayType(invoiceLine, COMPANY);
return payTypeInvoice && payTypeInvoiceLine;
} else if (invoiceLine.getXphrPaidByType().equals(AL_EE)) {
payTypeInvoice = checkIsValidPayType(invoice, EMPLYOEE);
payTypeInvoiceLine = checkIsValidPayType(invoiceLine, EMPLYOEE);
return payTypeInvoice && payTypeInvoiceLine;
}
}
return true;
}
/**
* Check's the business partner will pay are corrected.
*
* @param invoiceObjectType
* Object should be only Invoice object or InvoiceLine object.
* @param payType
* String object should be EMPLYOEE or COMPANY of static values in this class.
* @return true if it's a corrected business partner.
*/
private boolean checkIsValidPayType(Object invoiceObjectType, String payType) {
// Get's business partner object
BusinessPartner businessPartner = getBusinessPartner(invoiceObjectType);
// If business partner are null, return true
if (businessPartner != null) {
final OBCriteria empOrgInfCriteria = OBDal.getInstance()
.createCriteria(OrganizationInformation.class);
empOrgInfCriteria.add(Restrictions.eq(OrganizationInformation.PROPERTY_BUSINESSPARTNER,
businessPartner));
if (payType.equals(COMPANY)) {
return empOrgInfCriteria != null && !empOrgInfCriteria.list().isEmpty();
} else if (payType.equals(EMPLYOEE)) {
return isEmployee(businessPartner)
&& (empOrgInfCriteria == null || empOrgInfCriteria.list().isEmpty());
}
}
return true;
}
/**
* Check the business partner object is a employee.
*
* @param businessPartner
* the Business Partner object.
* @return true if business partner is employee.
*/
private boolean isEmployee(BusinessPartner businessPartner) {
return businessPartner.isEmployee();
}
/**
* Get business partner in Invoice or InvoiceLine of XphrPaidBy field.
*
* @param invoiceObjectType
* the invoice object type (Invoice or InvoiceLine).
* @return the business partner.
*/
private BusinessPartner getBusinessPartner(Object invoiceObjectType) {
try {
// Get's business partner object
return (BusinessPartner) invoiceObjectType.getClass().getMethod("getXphrPaidBy")
.invoke(invoiceObjectType);
} catch (IllegalAccessException e) {
LOG.error(e.getMessage(), e);
} catch (IllegalArgumentException e) {
LOG.error(e.getMessage(), e);
} catch (InvocationTargetException e) {
LOG.error(e.getMessage(), e);
} catch (NoSuchMethodException e) {
LOG.error(e.getMessage(), e);
} catch (SecurityException e) {
LOG.error(e.getMessage(), e);
}
return null;
}
}