public void storeAdDcCoop(AdDcCoopBean adcb) throws ApplicationException {
    boolean success = false;
    try {
      //			DBHelper.startTransaction(NFCCoreVariables.DATASOURCE_WAG_JNDI, false);

      // Get the AdDcCoop entry from the database
      AdDcCoopBean temp = getAdDcCoopByIdAndDivision(adcb.getCoopId(), adcb.getRegionMemberId());

      // If the AdDcCoop was not already in the database ...
      if (temp == null) {
        // Create the AdDcCoop entry in the database
        AdDcCoopManager.createDivisionCoop(
            adcb.getCoopId(),
            adcb.getRegionMemberId(),
            adcb.getInitialAmount(),
            adcb.getBilledAmount(),
            adcb.getEstimatedCases(),
            adcb.getActualCases(),
            adcb.getWorkFlowPhaseId(),
            adcb.getDivisionTaken(),
            adcb.getDivisionRejectReason(),
            adcb.getBillDate(),
            adcb.getBillSign(),
            adcb.getAccountRejectReason(),
            adcb.getAccountingExportWeek(),
            adcb.getAccountingExportPeriod(),
            adcb.getAccountingExportYear());
      } else {
        // Update the AdDcCoop entry
        AdDcCoopManager.updateDivisionCoop(
            adcb.getRegionMemberId(),
            adcb.getInitialAmount(),
            adcb.getBilledAmount(),
            adcb.getEstimatedCases(),
            adcb.getActualCases(),
            adcb.getWorkFlowPhaseId(),
            adcb.getDivisionTaken(),
            adcb.getDivisionRejectReason(),
            adcb.getBillDate(),
            adcb.getBillSign(),
            adcb.getAccountRejectReason(),
            adcb.getCoopId(),
            adcb.getAccountingExportWeek(),
            adcb.getAccountingExportPeriod(),
            adcb.getAccountingExportYear());
      }

      success = true;
    } finally {
      //			DBHelper.finalizeActiveTransaction(NFCCoreVariables.DATASOURCE_WAG_JNDI, success);
    }
  }
  public void updateDivisionCoopMonies(int coopId) throws ApplicationException {

    if (LOG.isLoggable(Level.FINE)) {
      LOG.entering(this.getClass().getName(), "updateDivisionCoopMonies", String.valueOf(coopId));
    }

    boolean success = false;

    try {
      //			DBHelper.startTransaction(NFCCoreVariables.DATASOURCE_WAG_JNDI, false);

      AdCoopBean coop = AdCoopManager.getCoopById(coopId);
      if (coop == null) throw new ApplicationException("No coop exists having id: " + coopId);

      ArrayList<AdDcCoopBean> divisionCoops = AdDcCoopManager.getDivisionCoops(coopId);

      if (coop.getType() == ContractsUtils.getCoopIdRegional()) {
        AdDcCoopBean divisionBean = null, lastAccepted = null;
        double acceptedDollars = 0.0;
        double runningTotal = 0.0;
        // Find the total sum for all accepted dollars
        for (int i = 0; i < divisionCoops.size(); i++) {
          divisionBean = divisionCoops.get(i);
          if (divisionBean.getDivisionTaken() == 1 && divisionBean.getAccountRejectReason() <= 0) {
            acceptedDollars += divisionBean.getInitialAmount();
          }
        }

        if (LOG.isLoggable(Level.FINER)) {
          LOG.finer("total amount of accepted coop dollars is: " + acceptedDollars);
        }

        for (int i = 0; i < divisionCoops.size(); i++) {
          divisionBean = (AdDcCoopBean) divisionCoops.get(i);
          if (divisionBean.getDivisionTaken() == 1
              && divisionBean.getAccountRejectReason() <= 0
              && acceptedDollars > 0.0) {
            lastAccepted = divisionBean;
            // Multiply the accepted amount by a scaling factor in order to reallocate the dollars
            // that were
            //   not accepted.  For example, if only $500 (acceptedDollars) out of $1000
            // (coop.getTotal)
            //   dollars were accepted, the amounts that WERE accepted would be multiplied by 2 =
            // 1000/500
            double newAmount =
                (divisionBean.getInitialAmount() * coop.getTotal()) / acceptedDollars;
            newAmount = Math.round(newAmount * 100.0D) / 100.0D; // get rid of extra digits
            runningTotal += newAmount;
            divisionBean.setBilledAmount(newAmount);
          } else {
            divisionBean.setBilledAmount(0.0);
          }
        }

        if (lastAccepted != null && runningTotal != coop.getTotal()) {
          if (LOG.isLoggable(Level.FINER)) {
            LOG.finer(
                "offsetting funds from rounding errors of "
                    + (coop.getTotal() - runningTotal)
                    + " to division "
                    + lastAccepted.getRegionMemberId());
          }
          double newAmount = lastAccepted.getBilledAmount() + coop.getTotal() - runningTotal;
          lastAccepted.setBilledAmount(Math.round(newAmount * 100.0D) / 100.0D);
        }
      } else {
        boolean isCaseRate = coop.getType() == ContractsUtils.getCoopIdCaseRate();

        for (int i = 0; i < divisionCoops.size(); i++) {
          AdDcCoopBean divisionBean = (AdDcCoopBean) divisionCoops.get(i);
          if (divisionBean.getDivisionTaken() == 1 && divisionBean.getAccountRejectReason() <= 0) {
            if (isCaseRate) {
              int actualCases =
                  divisionBean.getActualCases() <= 0 ? 0 : divisionBean.getActualCases();
              divisionBean.setBilledAmount(
                  Math.round(actualCases * divisionBean.getInitialAmount() * 100.0D) / 100.0D);
            } else {
              divisionBean.setBilledAmount(divisionBean.getInitialAmount());
            }
          } else {
            divisionBean.setBilledAmount(0.0);
          }
          LOG.finer("billed amount for division : " + divisionBean.getBilledAmount());
        }
      }

      this.storeAdDcCoops(
          (AdDcCoopBean[]) ArrayUtils.toTypedArray(divisionCoops, AdDcCoopBean.class));

      success = true;
    } finally {
      //			DBHelper.finalizeActiveTransaction(NFCCoreVariables.DATASOURCE_WAG_JNDI, success);
    }

    if (LOG.isLoggable(Level.FINE)) {
      LOG.exiting(this.getClass().getName(), "updateDivisionCoopMonies", String.valueOf(coopId));
    }
  }