protected PlanAndFactSumsByKindResult doGetTotalPlanAndFactSumsByKind(Phase contractPhase) {
    BigDecimal planShippedPayment = BigDecimal.ZERO;
    BigDecimal planReceivedPayment = BigDecimal.ZERO;
    BigDecimal planShippedGood = BigDecimal.ZERO;
    BigDecimal planReceivedGood = BigDecimal.ZERO;

    BigDecimal factShippedPayment = BigDecimal.ZERO;
    BigDecimal factReceivedPayment = BigDecimal.ZERO;
    BigDecimal factShippedGood = BigDecimal.ZERO;
    BigDecimal factReceivedGood = BigDecimal.ZERO;

    Projection projection =
        Projections.projectionList(
            Projections.sum("PlanSum"), // $NON-NLS-1$
            Projections.sum("FactSum"), // $NON-NLS-1$
            Projections.groupProperty("Kind")); // $NON-NLS-1$
    List<Object[]> phaseAgregateSums =
        OrmTemplate.getInstance()
            .findByCriteria(
                OrmTemplate.createCriteria(PhasePlanItem.class)
                    .setProjection(projection)
                    .add(Restrictions.eq("ContractPhase", contractPhase))); // $NON-NLS-1$

    for (Object[] agregateSum : phaseAgregateSums) {
      ItemKind kind = (ItemKind) agregateSum[2];
      if (kind == ItemKind.SHIPPED) {
        planShippedPayment = agregateSum[0] == null ? BigDecimal.ZERO : (BigDecimal) agregateSum[0];
        factShippedPayment = agregateSum[1] == null ? BigDecimal.ZERO : (BigDecimal) agregateSum[1];
        continue;
      }
      if (kind == ItemKind.RECEIVE) {
        planReceivedPayment =
            agregateSum[0] == null ? BigDecimal.ZERO : (BigDecimal) agregateSum[0];
        factReceivedPayment =
            agregateSum[1] == null ? BigDecimal.ZERO : (BigDecimal) agregateSum[1];
        continue;
      }
      if (kind == ItemKind.SHIPPEDGOOD) {
        planShippedGood = agregateSum[0] == null ? BigDecimal.ZERO : (BigDecimal) agregateSum[0];
        factShippedGood = agregateSum[1] == null ? BigDecimal.ZERO : (BigDecimal) agregateSum[1];
        continue;
      }
      if (kind == ItemKind.RECEIVEGOOD) {
        planReceivedGood = agregateSum[0] == null ? BigDecimal.ZERO : (BigDecimal) agregateSum[0];
        factReceivedGood = agregateSum[1] == null ? BigDecimal.ZERO : (BigDecimal) agregateSum[1];
      }
    }

    return new PlanAndFactSumsByKindResult(
        planShippedPayment,
        planReceivedPayment,
        planShippedGood,
        planReceivedGood,
        factShippedPayment,
        factReceivedPayment,
        factShippedGood,
        factReceivedGood);
  }
 private BigDecimal[] internalCalculateTotalPhaseSumByKind(Phase contractPhase) {
   Projection projection =
       Projections.projectionList(
           Projections.sum("PlanSum"),
           Projections.groupProperty("Kind")); // $NON-NLS-1$ //$NON-NLS-2$
   List<Object[]> phaseAgregateSums =
       OrmTemplate.getInstance()
           .findByCriteria(
               OrmTemplate.createCriteria(PhasePlanItem.class)
                   .setProjection(projection)
                   .add(Restrictions.eq("ContractPhase", contractPhase))); // $NON-NLS-1$
   return getSumsByKind(phaseAgregateSums);
 }
 /* (non-Javadoc)
  * @see com.mg.merp.reference.CatalogPriceServiceLocal#findActual(java.util.Date, com.mg.merp.reference.model.Catalog, com.mg.merp.reference.model.Currency)
  */
 @PermitAll
 public CatalogPrice findActual(Date actualityDate, Catalog catalog, Currency currency) {
   DetachedCriteria dc =
       DetachedCriteria.forClass(CatalogPrice.class, "cp")
           .setProjection(Projections.max("cp.InAction"))
           .add(Restrictions.eq("cp.Catalog", catalog))
           .add(Restrictions.eq("cp.Currency", currency))
           .add(Restrictions.le("cp.InAction", actualityDate));
   return OrmTemplate.getInstance()
       .findUniqueByCriteria(
           OrmTemplate.createCriteria(CatalogPrice.class)
               .add(Restrictions.eq("Catalog", catalog))
               .add(Subqueries.propertyEq("InAction", dc)));
 }
  /**
   * Проверить пересечение даным периодом других периодов
   *
   * @param calcPeriod - расчетный период
   * @return true - если данный период пересекает другие периоды
   */
  protected boolean isPeriodCross(CalcPeriod calcPeriod) {
    Criteria criteria =
        OrmTemplate.createCriteria(CalcPeriod.class)
            .setProjection(Projections.rowCount())
            .add(
                Restrictions.or(
                    Restrictions.and(
                        Restrictions.le("BeginDate", calcPeriod.getBeginDate()),
                        Restrictions.ge(
                            "EndDate", calcPeriod.getBeginDate())), // $NON-NLS-1$ //$NON-NLS-2$
                    Restrictions.and(
                        Restrictions.le("BeginDate", calcPeriod.getEndDate()),
                        Restrictions.ge(
                            "EndDate", calcPeriod.getEndDate())))); // $NON-NLS-1$ //$NON-NLS-2$

    // при редактировании периода исключаем изменяемый период из списка существующих периодов
    if (calcPeriod.getId() != null)
      criteria.add(Restrictions.ne("Id", calcPeriod.getId())); // $NON-NLS-1$

    Integer crossedPeriodsCount = OrmTemplate.getInstance().findUniqueByCriteria(criteria);

    if (crossedPeriodsCount > 0) return true;
    else return false;
  }