<p>Retrieve entries for document.</p>
@param pAddParam additional param
@param pEntity a document
@return warehouse entries made by this document
@throws Exception - an exception
17871
@Override
  public final List<WarehouseEntry> retrieveEntriesFor(
    final Map<String, Object> pAddParam,
      final IDocWarehouse pEntity) throws Exception {
    String tblNm = WarehouseEntry.class.getSimpleName().toUpperCase();
    List<WarehouseEntry> result = null;
    if (pEntity instanceof IMakingWarehouseEntry) {
      //e.g. Manufacture
      String where = " where SOURCETYPE=" + pEntity.constTypeCode()
        + " and " + tblNm + ".IDDATABASEBIRTH=" + getSrvOrm().getIdDatabase()
          + " and SOURCEID=" + pEntity.getItsId();
      result = getSrvOrm().retrieveListWithConditions(pAddParam,
        WarehouseEntry.class, where);
    }
    if (pEntity.getLinesWarehouseType() != null) {
      //e.g. PurchaseInvoice
      if (result == null) {
        result = retrieveEntriesForOwner(pAddParam, pEntity.constTypeCode(),
          pEntity.getItsId());
      } else {
        result.addAll(retrieveEntriesForOwner(pAddParam,
          pEntity.constTypeCode(), pEntity.getItsId()));
      }
    }
    return result;
  }
758745
@Override
  public final List<? extends ADrawItemSourcesLine>
    getDrawItemSources(
      final Map<String, Object> pAddParam) throws Exception {
    return getSrvAccSettings().lazyGetAccSettings(pAddParam)
      .getDrawMaterialSources();
  }
487705
@Override
  public final void fill(final Map<String, Object> pAddParam,
    final Object pEntity, final String pFieldName,
      final String pFieldStrValue) throws Exception {
    try {
      @SuppressWarnings("unchecked")
      IOwned<Cart> cartItem = (IOwned<Cart>) pEntity;
      Cart ownedEntity = new Cart();
      OnlineBuyer buyer = new OnlineBuyer();
      buyer.setItsId(Long.valueOf(pFieldStrValue));
      ownedEntity.setBuyer(buyer);
      cartItem.setItsOwner(ownedEntity);
    } catch (Exception ex) {
      throw new ExceptionWithCode(ExceptionWithCode
        .WRONG_PARAMETER, "Can not fill field: " + pEntity + "/" + pFieldName
          + "/" + pFieldStrValue + ", " + ex.getMessage(), ex);
    }
  }
568031
protected final PrcAccDocUseMaterialRetrieve
    lazyGetPrcAccDocUseMaterialRetrieve(
      final Map<String, Object> pAddParam) throws Exception {
    @SuppressWarnings("unchecked")
    PrcAccDocUseMaterialRetrieve<RS, IDocWarehouse> proc =
      (PrcAccDocUseMaterialRetrieve<RS, IDocWarehouse>)
        this.processorsMap
          .get(PrcAccDocUseMaterialRetrieve.class.getSimpleName());
    if (proc == null) {
      proc = new PrcAccDocUseMaterialRetrieve<RS, IDocWarehouse>();
      proc.setSrvWarehouseEntry(getSrvWarehouseEntry());
      proc.setSrvUseMaterialEntry(getSrvUseMaterialEntry());
      @SuppressWarnings("unchecked")
      IEntityProcessor<IDocWarehouse, Long> delegate =
        (IEntityProcessor<IDocWarehouse, Long>)
          lazyGetPrcAccDocRetrieve(pAddParam);
      proc.setPrcAccDocRetrieve(delegate);
      //assigning fully initialized object:
      this.processorsMap
        .put(PrcAccDocUseMaterialRetrieve.class.getSimpleName(), proc);
    }
    return proc;
  }
1141471
protected final PrcBankStatementLineGfe<RS> lazyGetPrcBankStatementLineGfe(
    final Map<String, Object> pAddParam) throws Exception {
    @SuppressWarnings("unchecked")
    PrcBankStatementLineGfe<RS> proc = (PrcBankStatementLineGfe<RS>)
      this.processorsMap.get(PrcBankStatementLineGfe.class.getSimpleName());
    if (proc == null) {
      proc = new PrcBankStatementLineGfe<RS>();
      proc.setSrvOrm(getSrvOrm());
      proc.setSrvTypeCode(getSrvTypeCode());
      @SuppressWarnings("unchecked")
      PrcEntityPbEditDelete<RS, BankStatementLine> procDlg =
    (PrcEntityPbEditDelete<RS, BankStatementLine>) this.fctBnEntitiesProcessors
  .lazyGet(pAddParam, PrcEntityPbEditDelete.class.getSimpleName());
      proc.setPrcEntityPbEditDelete(procDlg);
      //assigning fully initialized object:
      this.processorsMap
        .put(PrcBankStatementLineGfe.class.getSimpleName(), proc);
    }
    return proc;
  }
773284
protected final PrcSalesReturnLineSave<RS> lazyGetPrcSalesReturnLineSave(
    final Map<String, Object> pAddParam) throws Exception {
    @SuppressWarnings("unchecked")
    PrcSalesReturnLineSave<RS> proc = (PrcSalesReturnLineSave<RS>)
      this.processorsMap.get(PrcSalesReturnLineSave.class.getSimpleName());
    if (proc == null) {
      proc = new PrcSalesReturnLineSave<RS>();
      proc.setSrvAccSettings(getSrvAccSettings());
      proc.setSrvOrm(getSrvOrm());
      proc.setUtlInvLine(lazyGetUtlSalRetGdLn(pAddParam));
      proc.setSrvWarehouseEntry(getSrvWarehouseEntry());
      //assigning fully initialized object:
      this.processorsMap
        .put(PrcSalesReturnLineSave.class.getSimpleName(), proc);
    }
    return proc;
  }
808596
public final synchronized void retrieveAccSettings(
    final Map<String, Object> pAddParam) throws Exception {
    this.accSettings = new AccSettings();
    this.accSettings.setItsId(1L);
    this.accSettings = getSrvOrm().retrieveEntity(pAddParam, this.accSettings);
    if (this.accSettings == null) {
      throw new ExceptionWithCode(ExceptionWithCode.CONFIGURATION_MISTAKE,
        "There is no accounting settings!!!");
    }
    DrawMaterialSourcesLine dmsl = new DrawMaterialSourcesLine();
    dmsl.setItsOwner(this.accSettings);
    List<DrawMaterialSourcesLine> drawMaterialSources = getSrvOrm()
      .retrieveListForField(pAddParam, dmsl, "itsOwner");
    this.accSettings.setDrawMaterialSources(drawMaterialSources);
    CogsItemSourcesLine cisl = new CogsItemSourcesLine();
    cisl.setItsOwner(this.accSettings);
    List<CogsItemSourcesLine> cogsItemSources = getSrvOrm()
      .retrieveListForField(pAddParam, cisl, "itsOwner");
    this.accSettings.setCogsItemSources(cogsItemSources);
    AccEntriesSourcesLine aesl = new AccEntriesSourcesLine();
    aesl.setItsOwner(this.accSettings);
    List<AccEntriesSourcesLine> accEntriesSources = getSrvOrm()
      .retrieveListForField(pAddParam, aesl, "itsOwner");
    this.accSettings.setAccEntriesSources(accEntriesSources);
  }
583781
protected final PrcSalesInvoiceLineGfr lazyGetPrcSalesInvoiceLineGfr(
    final Map<String, Object> pAddParam) throws Exception {
    @SuppressWarnings("unchecked")
    PrcSalesInvoiceLineGfr<RS> proc = (PrcSalesInvoiceLineGfr<RS>)
      this.processorsMap.get(PrcSalesInvoiceLineGfr.class.getSimpleName());
    if (proc == null) {
      proc = new PrcSalesInvoiceLineGfr<RS>();
      @SuppressWarnings("unchecked")
      PrcEntityPbCopy<RS, SalesInvoiceLine> procDlg =
        (PrcEntityPbCopy<RS, SalesInvoiceLine>)
          this.fctBnEntitiesProcessors
            .lazyGet(pAddParam, PrcEntityPbCopy.class.getSimpleName());
      proc.setPrcAccEntityPbCopy(procDlg);
      //assigning fully initialized object:
      this.processorsMap
        .put(PrcSalesInvoiceLineGfr.class.getSimpleName(), proc);
    }
    return proc;
  }
282633
public final <T> void prepareColumnValuesForUpdate(
    final ColumnsValues pColumnsValues,
      final T pEntity) throws Exception {
    //remove old version and ID columns:
    pColumnsValues.getLongsMap().remove(ISrvOrm.VERSIONOLD_NAME);
    TableSql tableSql = this.tablesMap.get(pEntity.getClass().getSimpleName());
    for (String idFldNm : tableSql.getIdColumnsNames()) {
      pColumnsValues.remove(idFldNm);
    }
  }
93900
@Override
  public final void makeEntries(final Map<String, Object> pAddParam,
    final IDoc pEntity) throws Exception {
    Calendar calCurrYear = Calendar.getInstance(new Locale("en", "US"));
    calCurrYear.setTime(getSrvAccSettings().lazyGetAccSettings(pAddParam)
      .getCurrentAccYear());
    calCurrYear.set(Calendar.MONTH, 0);
    calCurrYear.set(Calendar.DAY_OF_MONTH, 1);
    calCurrYear.set(Calendar.HOUR_OF_DAY, 0);
    calCurrYear.set(Calendar.MINUTE, 0);
    calCurrYear.set(Calendar.SECOND, 0);
    calCurrYear.set(Calendar.MILLISECOND, 0);
    Calendar calDoc = Calendar.getInstance(new Locale("en", "US"));
    calDoc.setTime(pEntity.getItsDate());
    calDoc.set(Calendar.MONTH, 0);
    calDoc.set(Calendar.DAY_OF_MONTH, 1);
    calDoc.set(Calendar.HOUR_OF_DAY, 0);
    calDoc.set(Calendar.MINUTE, 0);
    calDoc.set(Calendar.SECOND, 0);
    calDoc.set(Calendar.MILLISECOND, 0);
    if (calCurrYear.getTime().getTime() != calDoc.getTime().getTime()) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "wrong_year");
    }
    StringBuffer sb = new StringBuffer();
    List<AccEntriesSourcesLine> sourcesLines = srvAccSettings
      .lazyGetAccSettings(pAddParam).getAccEntriesSources();
    java.util.Collections.sort(sourcesLines, this.cmprAccSourcesByType);
    int i = 0;
    for (AccEntriesSourcesLine sourcesLine : sourcesLines) {
      if (sourcesLine.getIsUsed()
        && sourcesLine.getSourceType().equals(pEntity.constTypeCode())) {
        String query = lazyGetQuery(sourcesLine.getFileName());
        String idName = "ITSID";
        if (pEntity.getIdBirth() != null) {
          idName = "IDBIRTH";
        }
        query = query.replace(":IDNAME", idName);
        //for foreign document this algorithm is also right:
        String strWhereDocId = sourcesLine.getSourceIdName() + " = "
          + pEntity.getItsId().toString();
        if (query.contains(":WHEREADD")) {
          query = query.replace(":WHEREADD", " and " + strWhereDocId);
        } else if (query.contains(":WHERE")) {
          query = query.replace(":WHERE", " where " + strWhereDocId);
        }
        if (i++ > 0) {
          sb.append("\nunion all\n\n");
        }
        sb.append(query);
      }
    }
    String langDef = (String) pAddParam.get("langDef");
    DateFormat dateFormat = DateFormat.getDateTimeInstance(
      DateFormat.MEDIUM, DateFormat.SHORT, new Locale(langDef));
    String query = sb.toString();
    if (query.trim().length() == 0) {
      throw new ExceptionWithCode(ExceptionWithCode.CONFIGURATION_MISTAKE,
        "there_is_no_accounting_sources");
    }
    IRecordSet<RS> recordSet = null;
    try {
      recordSet = getSrvDatabase().retrieveRecords(query);
      if (recordSet.moveToFirst()) {
        Long itsDateLong = null;
        do {
          AccountingEntry accEntry = new AccountingEntry();
          accEntry.setIdDatabaseBirth(getSrvDatabase().getIdDatabase());
          if (itsDateLong == null) {
            itsDateLong = pEntity.getItsDate().getTime();
            getSrvBalance().handleNewAccountEntry(pAddParam, null, null,
              new Date(itsDateLong)); //This is for SrvBalanceStd only!!!
          }
          Date itsDate = new Date(itsDateLong++);
          accEntry.setItsDate(itsDate);
          accEntry.setSourceType(pEntity.constTypeCode());
          if (pEntity.getIdBirth() != null) { //accounting foreign document
            accEntry.setSourceId(pEntity.getIdBirth());
          } else {
            accEntry.setSourceId(pEntity.getItsId());
          }
          accEntry.setSourceDatabaseBirth(pEntity.getIdDatabaseBirth());
          String accDebitId = recordSet.getString("ACCDEBIT");
          Account accDebit = new Account();
          accDebit.setItsId(accDebitId);
          accEntry.setAccDebit(accDebit);
          accEntry.setSubaccDebitType(recordSet.getInteger("SUBACCDEBITTYPE"));
          accEntry.setSubaccDebitId(recordSet.getLong("SUBACCDEBITID"));
          accEntry.setSubaccDebit(recordSet.getString("SUBACCDEBIT"));
          accEntry.setDebit(BigDecimal.valueOf(recordSet.getDouble("DEBIT"))
            .setScale(getSrvAccSettings().lazyGetAccSettings(pAddParam)
              .getCostPrecision(), getSrvAccSettings()
                .lazyGetAccSettings(pAddParam).getRoundingMode()));
          String accCreditId = recordSet.getString("ACCCREDIT");
          Account accCredit = new Account();
          accCredit.setItsId(accCreditId);
          accEntry.setAccCredit(accCredit);
          accEntry
            .setSubaccCreditType(recordSet.getInteger("SUBACCCREDITTYPE"));
          accEntry.setSubaccCreditId(recordSet.getLong("SUBACCCREDITID"));
          accEntry.setSubaccCredit(recordSet.getString("SUBACCCREDIT"));
          accEntry.setCredit(BigDecimal
            .valueOf(recordSet.getDouble("CREDIT")).setScale(
              getSrvAccSettings().lazyGetAccSettings(pAddParam)
                .getCostPrecision(), getSrvAccSettings()
                  .lazyGetAccSettings(pAddParam).getRoundingMode()));
          String docId = pEntity.getItsId().toString();
          if (pEntity.getIdBirth() != null) {
            docId = pEntity.getIdBirth().toString();
          }
          String descr = "";
          if (pEntity.getDescription() != null) {
            descr = " " + pEntity.getDescription();
          }
          accEntry.setDescription(getSrvI18n().getMsg(pEntity.getClass().
      getSimpleName() + "short", langDef) + " #" + pEntity.getIdDatabaseBirth()
    + "-" + docId + ", " + dateFormat.format(pEntity.getItsDate()) + descr);
          this.srvOrm.insertEntity(pAddParam, accEntry);
          accEntry.setIsNew(false);
        } while (recordSet.moveToNext());
      }
    } finally {
      if (recordSet != null) {
        recordSet.close();
      }
    }
    pEntity.setHasMadeAccEntries(true);
    srvOrm.updateEntity(pAddParam, pEntity);
  }
1196461
protected final PrcRevealTaxCat<RS>
    createPutPrcRevealTaxCat(
      final Map<String, Object> pAddParam) throws Exception {
    PrcRevealTaxCat<RS> proc = new PrcRevealTaxCat<RS>();
    proc.setLogger(getLogger());
    proc.setSrvDatabase(getSrvDatabase());
    //assigning fully initialized object:
    this.processorsMap
      .put(PrcRevealTaxCat.class.getSimpleName(), proc);
    return proc;
  }
922112
@Override
  public final <T> Integer evalRowCount(
    final Map<String, Object> pAddParam,
      final Class<T> pEntityClass) throws Exception {
    String query = "select count(*) as TOTALROWS from " + pEntityClass
      .getSimpleName().toUpperCase() + ";";
    return evalRowCountByQuery(pAddParam, pEntityClass, query);
  }
184592
@Override
  public final void process(final Map<String, Object> pAddParam,
    final IRequestData pRequestData) throws Exception {
    String retrieverName = pRequestData.getParameter("nmRet");
    ICsvDataRetriever ret = this.retrievers.get(retrieverName);
    if (ret == null) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "Can't find retriever " + retrieverName);
    }
    pRequestData.setAttribute("dataTree", ret.getSampleDataRow(pAddParam));
  }
1022346
public final <T> String evalWhereId(
    final T pEntity, final ColumnsValues pColumnsValues) throws Exception {
    TableSql tableSql = this.tablesMap.get(pEntity.getClass().getSimpleName());
    StringBuffer sbWhereId = new StringBuffer("");
    for (int i = 0; i < tableSql.getIdColumnsNames().length; i++) {
      if (i > 0) {
        sbWhereId.append(" and ");
      }
      sbWhereId.append(pEntity.getClass().getSimpleName().toUpperCase()
        + "." + tableSql.getIdColumnsNames()[i].toUpperCase() + "="
          + pColumnsValues.evalSqlValue(tableSql.getIdColumnsNames()[i]));
    }
    return sbWhereId.toString();
  }
865436
@Override
  public final LedgerPrevious retrievePrevious(
    final Map<String, Object> pAddParam, final Account pAccount,
      final Date pDate1, final String pSubaccId) throws Exception {
    getSrvBalance().recalculateAllIfNeed(pAddParam, pDate1);
    LedgerPrevious result = new LedgerPrevious();
    if (this.queryPrevious == null) {
      String flName = "/" + "accounting" + "/" + "ledger"
//+ "/" + "queryPrevious.sql";fast query cause error due changing subacc name
        + "/" + "queryPreviousSl.sql";
      this.queryPrevious = loadString(flName);
    }
    String query = queryPrevious.replace(":DATEBALANCE",
      String.valueOf(getSrvBalance()
        .evalDatePeriodStartFor(pAddParam, pDate1).getTime()));
    query = query.replace(":DATE1", String.valueOf(pDate1.getTime()));
    query = query.replace(":ACCID", "'" + pAccount.getItsId() + "'");
    String whereSubaccDebit = "";
    String whereSubaccCredit = "";
    String whereSubacc = "";
    if (pSubaccId != null && pSubaccId.length() > 0) {
      whereSubaccDebit = " and SUBACCDEBITID='" + pSubaccId + "'";
      whereSubaccCredit = " and SUBACCCREDITID='" + pSubaccId + "'";
      whereSubacc = " and SUBACCOUNT='" + pSubaccId + "'";
    }
    query = query.replace(":SUBACCDEBIT", whereSubaccDebit);
    query = query.replace(":SUBACCCREDIT", whereSubaccCredit);
    query = query.replace(":SUBACC", whereSubacc);
    IRecordSet<RS> recordSet = null;
    try {
      recordSet = getSrvDatabase().retrieveRecords(query);
      if (recordSet.moveToFirst()) {
        do {
          LedgerPreviousLine lpl = new LedgerPreviousLine();
          String subaccName = recordSet
            .getString("SUBACC");
          lpl.setDebit(BigDecimal.valueOf(recordSet.getDouble("DEBIT"))
            .setScale(getSrvAccSettings().lazyGetAccSettings(pAddParam)
              .getCostPrecision(), getSrvAccSettings()
                .lazyGetAccSettings(pAddParam).getRoundingMode()));
          lpl.setCredit(BigDecimal.valueOf(recordSet.getDouble("CREDIT"))
            .setScale(getSrvAccSettings().lazyGetAccSettings(pAddParam)
              .getCostPrecision(), getSrvAccSettings()
                .lazyGetAccSettings(pAddParam).getRoundingMode()));
          if (pAccount.getNormalBalanceType() == ENormalBalanceType.DEBIT) {
            lpl.setBalance(lpl.getDebit().subtract(lpl.getCredit()));
          } else {
            lpl.setBalance(lpl.getCredit().subtract(lpl.getDebit()));
          }
          result.getLinesMap().put(subaccName, lpl);
          result.setDebitAcc(result.getDebitAcc().add(lpl.getDebit()));
          result.setCreditAcc(result.getCreditAcc().add(lpl.getCredit()));
        } while (recordSet.moveToNext());
      }
    } finally {
      if (recordSet != null) {
        recordSet.close();
      }
    }
    if (pAccount.getNormalBalanceType() == ENormalBalanceType.DEBIT) {
      result.setBalanceAcc(result.getDebitAcc()
        .subtract(result.getCreditAcc()));
    } else {
      result.setBalanceAcc(result.getCreditAcc()
        .subtract(result.getDebitAcc()));
    }
    return result;
  }
795456
@Override
  public final T process(final Map<String, Object> pReqVars,
    final T pEntity, final IRequestData pRequestData) throws Exception {
    SeSeller ses = findSeSeller.find(pReqVars, pRequestData.getUserName());
    pEntity.setSeller(ses);
    if (pEntity.getIsNew()) {
      this.srvOrm.insertEntity(pReqVars, pEntity);
      pEntity.setIsNew(false);
    } else {
      T entOld = this.srvOrm.retrieveEntity(pReqVars, pEntity);
      if (!entOld.getSeller().getItsId().getItsId()
        .equals(pEntity.getSeller().getItsId().getItsId())) {
        throw new ExceptionWithCode(ExceptionWithCode.FORBIDDEN,
        "Attempt to update smb. else's entity: user/entity/EID/SEOLDID/SEID - "
          + pRequestData.getUserName() + "/" + pEntity.getClass()
           .getSimpleName() + "/" + pEntity.getItsId() + "/" + entOld
            .getSeller().getItsId().getItsId() + "/" + pEntity.getSeller()
              .getItsId().getItsId());
      }
      this.srvOrm.updateEntity(pReqVars, pEntity);
    }
    return pEntity;
  }
343323
@Override
  public final <T> void insertEntity(final Map<String, Object> pAddParam,
    final T pEntity) throws Exception {
    ColumnsValues columnsValues = evalColumnsValues(pAddParam, pEntity);
    long result = getSrvDatabase().executeInsert(pEntity.getClass()
      .getSimpleName().toUpperCase(), columnsValues);
    if (result != 1) {
      String query = getHlpInsertUpdate().evalSqlInsert(pEntity.getClass()
        .getSimpleName().toUpperCase(), columnsValues);
      throw new ExceptionWithCode(ISrvDatabase.ERROR_INSERT_UPDATE,
        "It should be 1 row inserted but it is "
          + result + ", query:\n" + query + ";\n");
    }
    String[] idName = columnsValues.getIdColumnsNames();
    if (idName.length == 1) { // if non-composite PK
      Field fieldId = getUtlReflection()
        .retrieveField(pEntity.getClass(), idName[0]);
      fieldId.setAccessible(true);
      Object idValue = fieldId.get(pEntity);
      if (idValue == null) {
        //It must be an autogenerated Integer or Long ID
        IRecordSet<RS> recordSet = null;
        try {
          String insertedIdName = "INSERTEDID";
          recordSet = getSrvDatabase().retrieveRecords(
            "select SCOPE_IDENTITY() as " + insertedIdName + ";");
          recordSet.moveToFirst();
          if (fieldId.getType() == Long.class) {
            fieldId.set(pEntity, recordSet.getLong(insertedIdName));
          } else if (fieldId.getType() == Integer.class) {
            fieldId.set(pEntity, recordSet.getInteger(insertedIdName));
          } else {
            String msg = "There is no rule to fill ID "
              + fieldId.getName().toUpperCase()
                + " of " + fieldId.getType() + " in "
                  + pEntity;
            throw new ExceptionWithCode(ExceptionWithCode.NOT_YET_IMPLEMENTED,
              msg);
          }
        } finally {
          if (recordSet != null) {
            recordSet.close();
          }
        }
      }
    }
  }