Attached Files | ContractService.diff [^] (12,592 bytes) 2016-06-29 10:06 [Show Content] [Hide Content]diff -r 46c8b3b1f291 src/org/openbravo/contract/services/process/UpdateInvoicePlanBackground.java
--- /src/org/openbravo/contract/services/process/UpdateInvoicePlanBackground.java Fri Jun 12 15:07:15 2015 +0200
+++ /src/org/openbravo/contract/services/process/UpdateInvoicePlanBackground.java Fri Jun 24 20:13:07 2016 -0300
@@ -13,6 +13,8 @@
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
+import org.hibernate.ScrollMode;
+import org.hibernate.ScrollableResults;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.openbravo.base.exception.OBException;
@@ -20,9 +22,12 @@
import org.openbravo.contract.services.process.dao.UpdateInvoicePlanBackgroundDao;
import org.openbravo.contract.services.utilities.ContractDateUtils;
import org.openbravo.contract.services.utilities.SCConstants;
+import org.openbravo.contract.utility.CConstants;
import org.openbravo.contract.utility.GenericDateUtils;
import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.security.OrganizationStructureProvider;
import org.openbravo.dal.service.OBDal;
+import org.openbravo.dal.service.OBQuery;
import org.openbravo.erpCommon.utility.OBDateUtils;
import org.openbravo.erpCommon.utility.OBError;
import org.openbravo.erpCommon.utility.OBMessageUtils;
@@ -63,12 +68,14 @@
isManual = true;
}
- final List<Project> contracts;
+ boolean moreThanOneContracts = false;
+ List<Project> contracts = null;
final String contractId = (String) bundleBG.getParams().get(PARAM_CONTRACT);
if (StringUtils.isBlank(contractId)) {
- contracts = bgDao.getRenewableContracts(today, client, org);
- logger.logln(String.format(OBMessageUtils.messageBD("OBSCNTR_LOG_BG_NUMBER_CONTRACTS"),
- contracts.size(), OBDateUtils.formatDate(today)));
+ // contracts = bgDao.getRenewableContracts(today, client, org);
+ // logger.logln(String.format(OBMessageUtils.messageBD("OBSCNTR_LOG_BG_NUMBER_CONTRACTS"),
+ // contracts.size(), OBDateUtils.formatDate(today)));
+ moreThanOneContracts = true;
} else {
final Project contract = OBDal.getInstance().get(Project.class, contractId);
contracts = new ArrayList<Project>(1);
@@ -76,57 +83,156 @@
}
int renewedContracts = 0;
- for (final Project contract : contracts) {
+ int i = 0;
+ if (moreThanOneContracts) {
+ OBContext.setAdminMode();
- final Date oldRenewalDate = contract.getObscntrRenewaldate();
- int interval;
- if (today.compareTo(oldRenewalDate) == 0) {
- interval = 0;
- } else if (today.compareTo(oldRenewalDate) > 0) {
- // The user can enter any renewal date, but we change by the most accurate valid renewal
- // day
- Date expectedRenewalDate = oldRenewalDate;
- Date actualRenewalDate = oldRenewalDate;
- while (expectedRenewalDate.compareTo(today) <= 0) {
- actualRenewalDate = expectedRenewalDate;
- expectedRenewalDate = ContractDateUtils.addDuration(expectedRenewalDate, contract
- .getOBSCNTRDuration().intValue(), contract.getOBSCNTRDurationUnit());
+ final StringBuffer hql = new StringBuffer();
+ hql.append(" as cntr ");
+ hql.append(" where cntr.startingDate <= :date ");
+ hql.append(" and (cntr.endingDate >= :date or cntr.endingDate is null) ");
+ hql.append(" and cntr.obscntrRenewaldate <= :date ");
+ hql.append(" and cntr.active = true ");
+ hql.append(" and cntr.projectCategory = :contractCategory ");
+ hql.append(" and cntr.oBCNTRContractStatus = :contractStatus ");
+ hql.append(" and cntr.client = :client ");
+ hql.append(" and cntr.organization.id in (:orgs) ");
+ hql.append(" and cntr.oBSCNTRAutomaticRenewal = true ");
+ hql.append(" order by cntr.name asc, cntr.id ");
+ final OBQuery<Project> obQuery = OBDal.getInstance().createQuery(Project.class,
+ hql.toString());
+ obQuery.setNamedParameter("date", today);
+ obQuery.setNamedParameter("contractCategory", CConstants.CONTRACT_HEADER);
+ obQuery.setNamedParameter("contractStatus", SCConstants.CONTRACT_STATUS_ACTIVE);
+ obQuery.setNamedParameter("client", client);
+ obQuery.setNamedParameter("orgs",
+ new OrganizationStructureProvider().getChildTree(org.getId(), true));
+ obQuery.setFetchSize(100);
+ final ScrollableResults scroller = obQuery.scroll(ScrollMode.FORWARD_ONLY);
+ try {
+
+ while (scroller.next()) {
+ i++;
+ if ((i % 100) == 0) {
+ OBDal.getInstance().flush();
+ OBDal.getInstance().getSession().clear();
+ }
+ Project contract = (Project) scroller.get()[0];
+ final Date oldRenewalDate = contract.getObscntrRenewaldate();
+ int interval;
+ if (today.compareTo(oldRenewalDate) == 0) {
+ interval = 0;
+ } else if (today.compareTo(oldRenewalDate) > 0) {
+ // The user can enter any renewal date, but we change by the most accurate valid
+ // renewal
+ // day
+ Date expectedRenewalDate = oldRenewalDate;
+ Date actualRenewalDate = oldRenewalDate;
+ while (expectedRenewalDate.compareTo(today) <= 0) {
+ actualRenewalDate = expectedRenewalDate;
+ expectedRenewalDate = ContractDateUtils.addDuration(expectedRenewalDate,
+ contract.getOBSCNTRDuration().intValue(), contract.getOBSCNTRDurationUnit());
+ }
+ interval = Days
+ .daysBetween(new DateTime(oldRenewalDate), new DateTime(actualRenewalDate))
+ .getDays();
+ } else {
+ final OBError msg = new OBError();
+ msg.setType("Error");
+ msg.setTitle(OBMessageUtils.messageBD("Error"));
+ final String message = OBMessageUtils.messageBD("OBSCNTR_ASOFDATE_LESS_THAN_RENEWAL");
+ msg.setMessage(message);
+ bundleBG.setResult(msg);
+ throw new OBException(message);
+ }
+
+ final Date oldCoverToDate = contract.getObscntrCovertodate();
+ final Date endDate = contract.getEndingDate();
+
+ if (endDate == null || oldCoverToDate.compareTo(endDate) < 0) {
+ contract.setObscntrCovertodate(calculateContractCoverToDate(
+ contract.getObscntrCovertodate(), contract.getOBSCNTRDuration().intValue(),
+ contract.getOBSCNTRDurationUnit(), interval));
+ contract.setObscntrRenewaldate(ContractDateUtils.calculateRenewalDate(contract));
+ bundleBG.getParams().put(PARAM_CONTRACT, contract.getId());
+ super.doExecute(bundleBG);
+ renewedContracts++;
+ } else {
+ if (isManual) {
+ final OBError msg = new OBError();
+ msg.setType("Error");
+ msg.setTitle(OBMessageUtils.messageBD("Error"));
+ final String message = OBMessageUtils
+ .messageBD("OBSCNTR_NORENEW_FOR_TERMINATED_CONTRACT");
+ msg.setMessage(message);
+ bundleBG.setResult(msg);
+ throw new OBException(message);
+ } else {
+ // For automatic process we don't do anything (we shouldn't have errors)
+ }
+ }
}
- interval = Days
- .daysBetween(new DateTime(oldRenewalDate), new DateTime(actualRenewalDate)).getDays();
- } else {
- final OBError msg = new OBError();
- msg.setType("Error");
- msg.setTitle(OBMessageUtils.messageBD("Error"));
- final String message = OBMessageUtils.messageBD("OBSCNTR_ASOFDATE_LESS_THAN_RENEWAL");
- msg.setMessage(message);
- bundleBG.setResult(msg);
- throw new OBException(message);
+ // scroller.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ scroller.close();
+ OBContext.restorePreviousMode();
}
+ } else {
+ for (final Project contract : contracts) {
- final Date oldCoverToDate = contract.getObscntrCovertodate();
- final Date endDate = contract.getEndingDate();
-
- if (endDate == null || oldCoverToDate.compareTo(endDate) < 0) {
- contract.setObscntrCovertodate(calculateContractCoverToDate(
- contract.getObscntrCovertodate(), contract.getOBSCNTRDuration().intValue(),
- contract.getOBSCNTRDurationUnit(), interval));
- contract.setObscntrRenewaldate(ContractDateUtils.calculateRenewalDate(contract));
- bundleBG.getParams().put(PARAM_CONTRACT, contract.getId());
- super.doExecute(bundleBG);
- renewedContracts++;
- } else {
- if (isManual) {
+ final Date oldRenewalDate = contract.getObscntrRenewaldate();
+ int interval;
+ if (today.compareTo(oldRenewalDate) == 0) {
+ interval = 0;
+ } else if (today.compareTo(oldRenewalDate) > 0) {
+ // The user can enter any renewal date, but we change by the most accurate valid renewal
+ // day
+ Date expectedRenewalDate = oldRenewalDate;
+ Date actualRenewalDate = oldRenewalDate;
+ while (expectedRenewalDate.compareTo(today) <= 0) {
+ actualRenewalDate = expectedRenewalDate;
+ expectedRenewalDate = ContractDateUtils.addDuration(expectedRenewalDate,
+ contract.getOBSCNTRDuration().intValue(), contract.getOBSCNTRDurationUnit());
+ }
+ interval = Days
+ .daysBetween(new DateTime(oldRenewalDate), new DateTime(actualRenewalDate))
+ .getDays();
+ } else {
final OBError msg = new OBError();
msg.setType("Error");
msg.setTitle(OBMessageUtils.messageBD("Error"));
- final String message = OBMessageUtils
- .messageBD("OBSCNTR_NORENEW_FOR_TERMINATED_CONTRACT");
+ final String message = OBMessageUtils.messageBD("OBSCNTR_ASOFDATE_LESS_THAN_RENEWAL");
msg.setMessage(message);
bundleBG.setResult(msg);
throw new OBException(message);
+ }
+
+ final Date oldCoverToDate = contract.getObscntrCovertodate();
+ final Date endDate = contract.getEndingDate();
+
+ if (endDate == null || oldCoverToDate.compareTo(endDate) < 0) {
+ contract.setObscntrCovertodate(calculateContractCoverToDate(
+ contract.getObscntrCovertodate(), contract.getOBSCNTRDuration().intValue(),
+ contract.getOBSCNTRDurationUnit(), interval));
+ contract.setObscntrRenewaldate(ContractDateUtils.calculateRenewalDate(contract));
+ bundleBG.getParams().put(PARAM_CONTRACT, contract.getId());
+ super.doExecute(bundleBG);
+ renewedContracts++;
} else {
- // For automatic process we don't do anything (we shouldn't have errors)
+ if (isManual) {
+ final OBError msg = new OBError();
+ msg.setType("Error");
+ msg.setTitle(OBMessageUtils.messageBD("Error"));
+ final String message = OBMessageUtils
+ .messageBD("OBSCNTR_NORENEW_FOR_TERMINATED_CONTRACT");
+ msg.setMessage(message);
+ bundleBG.setResult(msg);
+ throw new OBException(message);
+ } else {
+ // For automatic process we don't do anything (we shouldn't have errors)
+ }
}
}
}
@@ -142,9 +248,8 @@
Date date = ContractDateUtils.addDuration(oldCoverToDate, duration, durationUnit);
// fix end date when duration unit is Month
- if (durationUnit.equals(SCConstants.CONTRACT_UOM_MONTH)
- && GenericDateUtils.getLastDateOfMonth(oldCoverToDate) == GenericDateUtils
- .getDayOfTheMonth(oldCoverToDate)) {
+ if (durationUnit.equals(SCConstants.CONTRACT_UOM_MONTH) && GenericDateUtils
+ .getLastDateOfMonth(oldCoverToDate) == GenericDateUtils.getDayOfTheMonth(oldCoverToDate)) {
date = DateUtils.setDays(date, GenericDateUtils.getLastDateOfMonth(date));
}
|