<p>Handle event cart delivering or line changed
and redone forced service if need.</p>
@param pRqVs request scoped vars
@param pCart cart
@param pTxRules Tax Rules
@throws Exception - an exception.
545190
@Override
  public final void hndCartChan(final Map<String, Object> pRqVs,
    final Cart pCart, final TaxDestination pTxRules) throws Exception {
    @SuppressWarnings("unchecked")
    List<Deliv> dlvMts = (List<Deliv>) pRqVs.get("dlvMts");
    Deliv cdl = null;
    for (Deliv dl : dlvMts) {
      if (dl.getItsId().equals(pCart.getDeliv())) {
        cdl = dl;
        break;
      }
    }
    if (cdl == null) {
      throw new Exception("wrong delivering!");
    }
    //it must be at least one item to add forced service:
    boolean crtEmpty = true;
    CartLn clFrc = null;
    CartLn clEm = null;
    for (CartLn cl : pCart.getItems()) {
      if (cl.getDisab()) {
        clEm = cl;
      } else if (!cl.getDisab() && cl.getForc()) {
        clFrc = cl;
      } else if (!cl.getDisab() && !cl.getForc()) {
        crtEmpty = false;
      }
    }
    if (clFrc == null && cdl.getFrcSr() == null || cdl.getApMt() == null) {
      return;
    }
    if (crtEmpty) {
      if (clFrc != null) {
        delLine(pRqVs, clFrc, pTxRules);
      }
      return;
    }
    int cartTot;
    AccSettings as = (AccSettings) pRqVs.get("accSet");
    TradingSettings ts = (TradingSettings) pRqVs.get("tradSet");
    BigDecimal ct = pCart.getTot();
    if (clFrc != null && clFrc.getTot().compareTo(BigDecimal.ZERO) == 1) {
      ct = ct.subtract(clFrc.getTot());
    }
    if (pCart.getExcRt().compareTo(BigDecimal.ONE) == 0) {
      cartTot = ct.intValue();
    } else {
      cartTot = ct.divide(pCart.getExcRt(), as.getPricePrecision(),
        as.getRoundingMode()).intValue();
    }
    if (cartTot >= cdl.getApMt()) {
      if (clFrc != null && clFrc.getTot().compareTo(BigDecimal.ZERO) == 1) {
        clFrc.setPrice(BigDecimal.ZERO);
        clFrc.setTot(BigDecimal.ZERO);
        clFrc.setTotTx(BigDecimal.ZERO);
        clFrc.setSubt(BigDecimal.ZERO);
        clFrc.setTxDsc(null);
        clFrc.setTxCat(null);
        this.srvOrm.updateEntity(pRqVs, clFrc);
        makeCartTotals(pRqVs, ts, clFrc, as, pTxRules);
      }
    } else {
      if (clFrc == null) {
        if (clEm == null) {
          clFrc = new CartLn();
          clFrc.setIsNew(true);
          clFrc.setItsOwner(pCart);
          pCart.getItems().add(clFrc);
        } else {
          clFrc = clEm;
        }
        clFrc.setSel(null);
        clFrc.setForc(true);
        clFrc.setDisab(false);
        clFrc.setItTyp(EShopItemType.SERVICE);
        clFrc.setItId(cdl.getFrcSr().getItsId());
        clFrc.setItsName(cdl.getFrcSr().getItsName());
        clFrc.setUom(cdl.getFrcSr().getDefUnitOfMeasure());
        clFrc.setAvQuan(BigDecimal.ONE);
        clFrc.setQuant(BigDecimal.ONE);
        clFrc.setUnStep(BigDecimal.ONE);
        makeCartLine(pRqVs, clFrc, as, ts, pTxRules, true, true);
        makeCartTotals(pRqVs, ts, clFrc, as, pTxRules);
      } else if (clFrc.getTot().compareTo(BigDecimal.ZERO) == 0) {
        makeCartLine(pRqVs, clFrc, as, ts, pTxRules, true, true);
        makeCartTotals(pRqVs, ts, clFrc, as, pTxRules);
      }
    }
  }
263941
public final Purch retNewOrds(final Map<String, Object> pRqVs,
    final OnlineBuyer pBur) throws Exception {
    Set<String> ndFl = new HashSet<String>();
    ndFl.add("itsId");
    ndFl.add("itsVersion");
    String tbn = CustOrder.class.getSimpleName();
    pRqVs.put(tbn + "neededFields", ndFl);
    List<CustOrder> orders = getSrvOrm().retrieveListWithConditions(pRqVs,
      CustOrder.class, "where STAT=0 and BUYER=" + pBur.getItsId());
    pRqVs.remove(tbn + "neededFields");
    for (CustOrder cuOr : orders) {
      //redo all lines:
      //itsOwner and other data will be set farther only for used lines!!!
      //unused lines will be removed from DB
      tbn = CustOrderTxLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      cuOr.setTaxes(getSrvOrm().retrieveListWithConditions(pRqVs,
        CustOrderTxLn.class, "where ITSOWNER=" + cuOr.getItsId()));
      pRqVs.remove(tbn + "neededFields");
      tbn = CustOrderGdLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      cuOr.setGoods(getSrvOrm().retrieveListWithConditions(pRqVs,
        CustOrderGdLn.class, "where ITSOWNER=" + cuOr.getItsId()));
      pRqVs.remove(tbn + "neededFields");
      tbn = CustOrderSrvLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      cuOr.setServs(getSrvOrm().retrieveListWithConditions(pRqVs,
        CustOrderSrvLn.class, "where ITSOWNER=" + cuOr.getItsId()));
      pRqVs.remove(tbn + "neededFields");
      tbn = CuOrGdTxLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      for (CustOrderGdLn gl : cuOr.getGoods()) {
        gl.setItTxs(getSrvOrm().retrieveListWithConditions(pRqVs,
          CuOrGdTxLn.class, "where ITSOWNER=" + gl.getItsId()));
      }
      pRqVs.remove(tbn + "neededFields");
      tbn = CuOrSrTxLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      for (CustOrderSrvLn sl : cuOr.getServs()) {
        sl.setItTxs(getSrvOrm().retrieveListWithConditions(pRqVs,
          CuOrSrTxLn.class, "where ITSOWNER=" + sl.getItsId()));
      }
      pRqVs.remove(tbn + "neededFields");
    }
    tbn = CuOrSe.class.getSimpleName();
    pRqVs.put(tbn + "neededFields", ndFl);
    List<CuOrSe> sorders = getSrvOrm().retrieveListWithConditions(pRqVs,
      CuOrSe.class, "where STAT=0 and BUYER=" + pBur.getItsId());
    pRqVs.remove(tbn + "neededFields");
    for (CuOrSe cuOr : sorders) {
      tbn = CuOrSeTxLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      cuOr.setTaxes(getSrvOrm().retrieveListWithConditions(pRqVs,
        CuOrSeTxLn.class, "where ITSOWNER=" + cuOr.getItsId()));
      pRqVs.remove(tbn + "neededFields");
      tbn = CuOrSeGdLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      cuOr.setGoods(getSrvOrm().retrieveListWithConditions(pRqVs,
        CuOrSeGdLn.class, "where ITSOWNER=" + cuOr.getItsId()));
      pRqVs.remove(tbn + "neededFields");
      tbn = CuOrSeSrLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      cuOr.setServs(getSrvOrm().retrieveListWithConditions(pRqVs,
        CuOrSeSrLn.class, "where ITSOWNER=" + cuOr.getItsId()));
      pRqVs.remove(tbn + "neededFields");
      tbn = CuOrSeGdTxLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      for (CuOrSeGdLn gl : cuOr.getGoods()) {
        gl.setItTxs(getSrvOrm().retrieveListWithConditions(pRqVs,
          CuOrSeGdTxLn.class, "where ITSOWNER=" + gl.getItsId()));
      }
      pRqVs.remove(tbn + "neededFields");
      tbn = CuOrSeSrTxLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      for (CuOrSeSrLn sl : cuOr.getServs()) {
        sl.setItTxs(getSrvOrm().retrieveListWithConditions(pRqVs,
          CuOrSeSrTxLn.class, "where ITSOWNER=" + sl.getItsId()));
      }
      pRqVs.remove(tbn + "neededFields");
    }
    Purch pur = new Purch();
    pur.setOrds(orders);
    pur.setSords(sorders);
    return pur;
  }
490766
@Override
  public final void makeOtherEntries(final Map<String, Object> pAddParam,
    final PaymentFrom pEntity, final IRequestData pRequestData,
      final boolean pIsNew) throws Exception {
    String actionAdd = pRequestData.getParameter("actionAdd");
    if ("makeAccEntries".equals(actionAdd)) {
      this.prcSalesInvoiceSave.calculateTotalPayment(pAddParam, pEntity
        .getSalesInvoice());
      getSrvOrm().updateEntity(pAddParam, pEntity.getSalesInvoice());
    }
  }
1034133
@Override
  public final void handle(final Map<String, Object> pRqVs,
    final IRequestData pRqDt) throws Exception {
    TradingSettings ts = srvTradingSettings
      .lazyGetTradingSettings(pRqVs);
    pRqVs.put("tradSet", ts);
    pRqVs.put("setAdd", srvSettingsAdd.lazyGetSettingsAdd(pRqVs));
    pRqDt.setAttribute("utlTradeJsp", this.utlTradeJsp);
    List<CurrRate> curRatesTmp = null;
    List<Deliv> dlvMtsTmp = null;
    if (ts.getUseAdvancedI18n()) {
      String lang = (String) pRqVs.get("lang");
      String langDef = (String) pRqVs.get("langDef");
      if (lang != null && langDef != null && !lang.equals(langDef)) {
        List<I18nWebStore> i18nTrTmp = null;
        List<I18nCatalogGs> i18nCtTmp;
        synchronized (this) {
          if (this.i18nWebStoreList == null) {
            try {
              this.logger.info(null, HndlTradeVarsRequest.class,
                "Refreshing I18N data...");
              this.srvDatabase.setIsAutocommit(false);
              this.srvDatabase.setTransactionIsolation(ISrvDatabase
                .TRANSACTION_READ_UNCOMMITTED);
              this.srvDatabase.beginTransaction();
              List<I18nWebStore> i18ntr = this.srvOrm.retrieveList(pRqVs,
                I18nWebStore.class);
              List<I18nCatalogGs> i18nct = this.srvOrm.retrieveList(pRqVs,
                I18nCatalogGs.class);
              List<CurrRate> cRnct = this.srvOrm.retrieveList(pRqVs,
                CurrRate.class);
              List<Deliv> dlvMt = getSrvOrm().retrieveList(pRqVs, Deliv.class);
              this.srvDatabase.commitTransaction();
              //assigning fully initialized data:
              this.i18nWebStoreList = i18ntr;
              this.i18nCatalogs = i18nct;
              this.currRates = cRnct;
              this.dlvMts = dlvMt;
            } catch (Exception ex) {
              if (!this.srvDatabase.getIsAutocommit()) {
                this.srvDatabase.rollBackTransaction();
              }
              throw ex;
            } finally {
              this.srvDatabase.releaseResources();
            }
          }
          i18nTrTmp = this.i18nWebStoreList;
          i18nCtTmp = this.i18nCatalogs;
          curRatesTmp = this.currRates;
          dlvMtsTmp = this.dlvMts;
        }
        pRqVs.put("i18nCatalogs", i18nCtTmp);
        pRqVs.put("i18nWebStoreList", i18nTrTmp);
      }
    }
    if (curRatesTmp == null) {
      synchronized (this) {
        try {
          this.srvDatabase.setIsAutocommit(false);
          this.srvDatabase.setTransactionIsolation(ISrvDatabase
            .TRANSACTION_READ_UNCOMMITTED);
          this.srvDatabase.beginTransaction();
          List<CurrRate> cRnct = this.srvOrm.retrieveList(pRqVs,
            CurrRate.class);
          List<Deliv> dlvMt = getSrvOrm().retrieveList(pRqVs, Deliv.class);
          this.srvDatabase.commitTransaction();
          //assigning fully initialized data:
          this.currRates = cRnct;
          this.dlvMts = dlvMt;
        } catch (Exception ex) {
          if (!this.srvDatabase.getIsAutocommit()) {
            this.srvDatabase.rollBackTransaction();
          }
          throw ex;
        } finally {
          this.srvDatabase.releaseResources();
        }
        curRatesTmp = this.currRates;
        dlvMtsTmp = this.dlvMts;
      }
    }
    pRqVs.put("currRates", curRatesTmp);
    pRqVs.put("dlvMts", dlvMtsTmp);
    Currency wscurr = null;
    if (curRatesTmp.size() > 0) {
      String wscurrs = pRqDt.getParameter("wscurr");
      if (wscurrs != null) {
        Long wscurrl = Long.parseLong(wscurrs);
        for (CurrRate cr : curRatesTmp) {
          if (cr.getCurr().getItsId().equals(wscurrl)) {
            wscurr = cr.getCurr();
            pRqDt.setCookieValue("wscurr", wscurr.getItsId().toString());
            break;
          }
        }
      } else {
        String  wscurrsc = pRqDt.getCookieValue("wscurr");
        if (wscurrsc != null) {
          Long wscurrl = Long.parseLong(wscurrsc);
          for (CurrRate cr : curRatesTmp) {
            if (cr.getCurr().getItsId().equals(wscurrl)) {
              wscurr = cr.getCurr();
              break;
            }
          }
        }
      }
    }
    if (wscurr == null) {
      AccSettings as = (AccSettings) pRqVs.get("accSet");
      wscurr = as.getCurrency();
      pRqDt.setCookieValue("wscurr", wscurr.getItsId().toString());
    }
    pRqVs.put("wscurr", wscurr);
    Boolean shTxDet;
    String shTxDets = pRqDt.getParameter("shTxDet");
    if (shTxDets == null) {
      String shTxDetsc = pRqDt.getCookieValue("shTxDet");
      if (shTxDetsc == null) {
        shTxDet = Boolean.FALSE;
        pRqDt.setCookieValue("shTxDet", shTxDet.toString());
      } else {
        shTxDet = Boolean.valueOf(shTxDetsc);
      }
    } else {
      shTxDet = Boolean.valueOf(shTxDets);
      pRqDt.setCookieValue("shTxDet", shTxDet.toString());
    }
    pRqVs.put("shTxDet", shTxDet);
  }
509318
@Override
  public final void process(final Map<String, Object> pRqVs,
    final IRequestData pRqDt) throws Exception {
    OnlineBuyer buyer = this.buySr.getAuthBuyr(pRqVs, pRqDt);
    if (buyer == null) {
      String procNm = pRqDt.getParameter("nmPrcRed");
      IProcessor proc = this.procFac.lazyGet(pRqVs, procNm);
      proc.process(pRqVs, pRqDt);
      return;
    }
    String orIdSt = pRqDt.getParameter("orId");
    String sorIdSt = pRqDt.getParameter("sorId");
    if (orIdSt != null || sorIdSt != null) { //print:
     if (orIdSt != null) { //order
        Long orId = Long.valueOf(orIdSt);
        CustOrder or = this.srvOrm.retrieveEntityById(pRqVs,
          CustOrder.class, orId);
        String tbn = CustOrderGdLn.class.getSimpleName();
        pRqVs.put(tbn + "itsOwnerdeepLevel", 1);
        or.setGoods(this.srvOrm.retrieveListWithConditions(
            pRqVs, CustOrderGdLn.class, "where ITSOWNER=" + orId));
        pRqVs.remove(tbn + "itsOwnerdeepLevel");
        tbn = CustOrderSrvLn.class.getSimpleName();
        pRqVs.put(tbn + "itsOwnerdeepLevel", 1);
        or.setServs(this.srvOrm.retrieveListWithConditions(
            pRqVs, CustOrderSrvLn.class, "where ITSOWNER=" + orId));
        pRqVs.remove(tbn + "itsOwnerdeepLevel");
        tbn = CustOrderTxLn.class.getSimpleName();
        pRqVs.put(tbn + "itsOwnerdeepLevel", 1);
        or.setTaxes(this.srvOrm.retrieveListWithConditions(
            pRqVs, CustOrderTxLn.class, "where ITSOWNER=" + orId));
        pRqVs.remove(tbn + "itsOwnerdeepLevel");
        pRqDt.setAttribute("entity", or);
        pRqDt.setAttribute("mngUvds", this.mngUvd);
        pRqDt.setAttribute("srvOrm", this.srvOrm);
        pRqDt.setAttribute("srvDate", this.srvDate);
        pRqDt.setAttribute("hldCnvFtfsNames", this.hldFldCnv);
        pRqDt.setAttribute("fctCnvFtfs", this.facFldCnv);
        Map<Class<?>, List<?>> olm = new LinkedHashMap<Class<?>, List<?>>();
        pRqDt.setAttribute("ownedListsMap", olm);
        olm.put(CustOrderGdLn.class, or.getGoods());
        olm.put(CustOrderSrvLn.class, or.getServs());
        olm.put(CustOrderTxLn.class, or.getTaxes());
      } else { //S.E. order:
        Long orId = Long.valueOf(sorIdSt);
        CuOrSe or = this.srvOrm.retrieveEntityById(pRqVs,
          CuOrSe.class, orId);
        String tbn = CuOrSeGdLn.class.getSimpleName();
        pRqVs.put(tbn + "itsOwnerdeepLevel", 1);
        or.setGoods(this.srvOrm.retrieveListWithConditions(
            pRqVs, CuOrSeGdLn.class, "where ITSOWNER=" + orId));
        pRqVs.remove(tbn + "itsOwnerdeepLevel");
        tbn = CuOrSeSrLn.class.getSimpleName();
        pRqVs.put(tbn + "itsOwnerdeepLevel", 1);
        or.setServs(this.srvOrm.retrieveListWithConditions(
            pRqVs, CuOrSeSrLn.class, "where ITSOWNER=" + orId));
        pRqVs.remove(tbn + "itsOwnerdeepLevel");
        tbn = CuOrSeTxLn.class.getSimpleName();
        pRqVs.put(tbn + "itsOwnerdeepLevel", 1);
        or.setTaxes(this.srvOrm.retrieveListWithConditions(
            pRqVs, CuOrSeTxLn.class, "where ITSOWNER=" + orId));
        pRqVs.remove(tbn + "itsOwnerdeepLevel");
        pRqDt.setAttribute("entity", or);
        pRqDt.setAttribute("mngUvds", this.mngUvd);
        pRqDt.setAttribute("srvOrm", this.srvOrm);
        pRqDt.setAttribute("srvDate", this.srvDate);
        pRqDt.setAttribute("hldCnvFtfsNames", this.hldFldCnv);
        pRqDt.setAttribute("fctCnvFtfs", this.facFldCnv);
        Map<Class<?>, List<?>> olm = new LinkedHashMap<Class<?>, List<?>>();
        pRqDt.setAttribute("ownedListsMap", olm);
        olm.put(CuOrSeGdLn.class, or.getGoods());
        olm.put(CuOrSeSrLn.class, or.getServs());
        olm.put(CuOrSeTxLn.class, or.getTaxes());
      }
    } else { //page:
      page(pRqVs, pRqDt, buyer);
    }
  }
413360
public final void procSeSrv(final Map<String, Object> pRqVs,
    final IRequestData pRqDt, final TradingSettings pTs,
      final OnlineBuyer pBuyer, final Long pItemId) throws Exception {
    List<SeServiceSpecifics> itemSpecLst;
    List<SeServicePlace> itemPlaceLst;
    itemSpecLst = retrieveItemSpecificsList(pRqVs, pTs, pItemId,
      SeServiceSpecifics.class, SeService.class.getSimpleName());
    //extract main image if exist:
    int miIdx = -1;
    for (int i = 0; i < itemSpecLst.size(); i++) {
      if (itemSpecLst.get(i).getSpecifics().getItsType()
        .equals(ESpecificsItemType.IMAGE)) {
        pRqDt.setAttribute("itemImage", itemSpecLst.get(i));
        miIdx = i;
        break;
      }
    }
    if (miIdx != -1) {
      itemSpecLst.remove(miIdx);
    }
    AItemPrice<?, ?> itemPrice = getSrvCart().revealItemPrice(pRqVs, pTs,
        pBuyer, EShopItemType.SESERVICE, pItemId);
    itemPlaceLst = getSrvOrm().retrieveListWithConditions(pRqVs,
        SeServicePlace.class, " where ITEM=" + pItemId);
    pRqDt.setAttribute("itemSpecLst", itemSpecLst);
    pRqDt.setAttribute("itemPlaceLst", itemPlaceLst);
    pRqDt.setAttribute("itemPrice", itemPrice);
  }
256756
public final CartLn makeTxdLine(final List<CartLn> pTxdLns, final Long pTdlId,
    final Long pCatId,  final Tax pTax, final Double pPercent,
      final AccSettings pAs) {
    CartLn txdLn = null;
    for (CartLn tdl : pTxdLns) {
      if (tdl.getItsId().equals(pTdlId)) {
        txdLn = tdl;
      }
    }
    if (txdLn == null) {
      txdLn = new CartLn();
      txdLn.setItsId(pTdlId);
      InvItemTaxCategory tc = new InvItemTaxCategory();
      tc.setItsId(pCatId);
      tc.setTaxes(new ArrayList<InvItemTaxCategoryLine>());
      txdLn.setTxCat(tc);
      pTxdLns.add(txdLn);
    }
    InvItemTaxCategoryLine itcl = new InvItemTaxCategoryLine();
    itcl.setTax(pTax);
    itcl.setItsPercentage(BigDecimal.valueOf(pPercent)
      .setScale(pAs.getTaxPrecision(), RoundingMode.HALF_UP));
    txdLn.getTxCat().getTaxes().add(itcl);
    txdLn.getTxCat().setAggrOnlyPercent(txdLn.getTxCat()
      .getAggrOnlyPercent().add(itcl.getItsPercentage()));
    return txdLn;
  }
270506
private void addException(ProjectCalendar mpxjCalendar, net.sf.mpxj.ganttproject.schema.Date date)
   {
      String year = date.getYear();
      if (year == null || year.isEmpty())
      {
         // In order to process recurring exceptions using MPXJ, we need a start and end date
         // to constrain the number of dates we generate.
         // May need to pre-process the tasks in order to calculate a start and finish date.
         // TODO: handle recurring exceptions
      }
      else
      {
         Calendar calendar = DateHelper.popCalendar();
         calendar.set(Calendar.YEAR, Integer.parseInt(year));
         calendar.set(Calendar.MONTH, NumberHelper.getInt(date.getMonth()));
         calendar.set(Calendar.DAY_OF_MONTH, NumberHelper.getInt(date.getDate()));
         Date exceptionDate = calendar.getTime();
         DateHelper.pushCalendar(calendar);
         ProjectCalendarException exception = mpxjCalendar.addCalendarException(exceptionDate, exceptionDate);

         // TODO: not sure how NEUTRAL should be handled
         if ("WORKING_DAY".equals(date.getType()))
         {
            exception.addRange(ProjectCalendarWeek.DEFAULT_WORKING_MORNING);
            exception.addRange(ProjectCalendarWeek.DEFAULT_WORKING_AFTERNOON);
         }
      }
   }
1352752
private void doUpdatePSQ() throws PageException {
	admin.updatePSQ(getBoolObject("admin", action, "psq"));
	store();
	adminSync.broadcast(attributes, config);
    }
1091581
public final void saveOrds(final Map<String, Object> pRqVs,
    final Purch pPur, final Cart pCart) throws Exception {
    List<CustOrder> dels = null;
    for (CustOrder co : pPur.getOrds()) {
      if (co.getCurr() == null) { //stored unused order
        //remove it and all its lines:
        for (CustOrderGdLn gl : co.getGoods()) {
          for (CuOrGdTxLn gtl : gl.getItTxs()) {
            getSrvOrm().deleteEntity(pRqVs, gtl);
          }
          getSrvOrm().deleteEntity(pRqVs, gl);
        }
        for (CustOrderSrvLn sl : co.getServs()) {
          for (CuOrSrTxLn stl : sl.getItTxs()) {
            getSrvOrm().deleteEntity(pRqVs, stl);
          }
          getSrvOrm().deleteEntity(pRqVs, sl);
        }
        for (CustOrderTxLn otlt : co.getTaxes()) {
          getSrvOrm().deleteEntity(pRqVs, otlt);
        }
        getSrvOrm().deleteEntity(pRqVs, co);
        if (dels == null) {
          dels = new ArrayList<CustOrder>();
        }
        dels.add(co);
        continue;
      }
      if (co.getIsNew()) {
        getSrvOrm().insertEntity(pRqVs, co);
      }
      BigDecimal tot = BigDecimal.ZERO;
      BigDecimal subt = BigDecimal.ZERO;
      List<CustOrderGdLn> delsGd = null;
      for (CustOrderGdLn gl : co.getGoods()) {
        gl.setItsOwner(co);
        if (gl.getIsNew()) {
          getSrvOrm().insertEntity(pRqVs, gl);
        }
        if (gl.getItTxs() != null && gl.getItTxs().size() > 0) {
          for (CuOrGdTxLn gtl : gl.getItTxs()) {
            gtl.setItsOwner(gl);
            if (gtl.getIsNew()) {
              getSrvOrm().insertEntity(pRqVs, gtl);
            } else if (gl.getGood() == null || gtl.getTax() == null) {
              getSrvOrm().deleteEntity(pRqVs, gtl);
            } else {
              getSrvOrm().updateEntity(pRqVs, gtl);
            }
          }
        }
        if (!gl.getIsNew() && gl.getGood() == null) {
          getSrvOrm().deleteEntity(pRqVs, gl);
          if (delsGd == null) {
            delsGd = new ArrayList<CustOrderGdLn>();
          }
          delsGd.add(gl);
        } else {
          tot = tot.add(gl.getTot());
          subt = subt.add(gl.getSubt());
          if (!gl.getIsNew()) {
            getSrvOrm().updateEntity(pRqVs, gl);
          }
        }
      }
      if (delsGd != null) {
        for (CustOrderGdLn gl : delsGd) {
          co.getGoods().remove(gl);
        }
      }
      List<CustOrderSrvLn> delsSr = null;
      for (CustOrderSrvLn sl : co.getServs()) {
        sl.setItsOwner(co);
        if (sl.getIsNew()) {
          getSrvOrm().insertEntity(pRqVs, sl);
        }
        if (sl.getItTxs() != null && sl.getItTxs().size() > 0) {
          for (CuOrSrTxLn stl : sl.getItTxs()) {
            stl.setItsOwner(sl);
            if (stl.getIsNew()) {
              getSrvOrm().insertEntity(pRqVs, stl);
            } else if (sl.getService() == null || stl.getTax() == null) {
              getSrvOrm().deleteEntity(pRqVs, stl);
            } else {
              getSrvOrm().updateEntity(pRqVs, stl);
            }
          }
        }
        if (!sl.getIsNew() && sl.getService() == null) {
          getSrvOrm().deleteEntity(pRqVs, sl);
          if (delsSr == null) {
            delsSr = new ArrayList<CustOrderSrvLn>();
          }
          delsSr.add(sl);
        } else {
          tot = tot.add(sl.getTot());
          subt = subt.add(sl.getSubt());
          if (!sl.getIsNew()) {
            getSrvOrm().updateEntity(pRqVs, sl);
          }
        }
      }
      if (delsSr != null) {
        for (CustOrderSrvLn sl : delsSr) {
          co.getServs().remove(sl);
        }
      }
      List<CustOrderTxLn> delsTx = null;
      BigDecimal totTx = BigDecimal.ZERO;
      for (CartTxLn ctl : pCart.getTaxes()) {
        if (ctl.getDisab() || ctl.getSeller() != null) {
          continue;
        }
        CustOrderTxLn otl = null;
        if (!co.getIsNew()) {
          for (CustOrderTxLn otlt : co.getTaxes()) {
            if (otlt.getTax() == null) {
              otl = otlt;
              break;
            }
          }
        }
        if (otl == null) {
          otl = new CustOrderTxLn();
          otl.setIsNew(true);
          co.getTaxes().add(otl);
        }
        otl.setItsOwner(co);
        Tax tx = new Tax();
        tx.setItsId(ctl.getTax().getItsId());
        tx.setItsName(ctl.getTax().getItsName());
        otl.setTax(tx);
        otl.setTot(ctl.getTot());
        otl.setTaxab(ctl.getTaxab());
        totTx = totTx.add(otl.getTot());
        if (otl.getIsNew()) {
          getSrvOrm().insertEntity(pRqVs, otl);
        } else {
          getSrvOrm().updateEntity(pRqVs, otl);
        }
      }
      if (!co.getIsNew()) {
        for (CustOrderTxLn otlt : co.getTaxes()) {
          if (otlt.getTax() == null) {
            getSrvOrm().deleteEntity(pRqVs, otlt);
            if (delsTx == null) {
              delsTx = new ArrayList<CustOrderTxLn>();
            }
            delsTx.add(otlt);
          }
        }
      }
      if (delsTx != null) {
        for (CustOrderTxLn tl : delsTx) {
          co.getTaxes().remove(tl);
        }
      }
      co.setTot(tot);
      co.setSubt(subt);
      co.setTotTx(totTx);
      getSrvOrm().updateEntity(pRqVs, co);
    }
    if (dels != null) {
      for (CustOrder co : dels) {
        pPur.getOrds().remove(co);
      }
    }
  }
1164611
@Override
  public final TaxDestination revealTaxRules(final Map<String, Object> pRqVs,
    final Cart pCart, final AccSettings pAs) throws Exception {
    if (pCart.getBuyer().getRegCustomer() == null) {
      //copy buyer info into non-persistable customer.
      pCart.getBuyer().setRegCustomer(new DebtorCreditor());
      pCart.getBuyer().getRegCustomer()
        .setIsForeigner(pCart.getBuyer().getForeig());
      pCart.getBuyer().getRegCustomer()
        .setRegZip(pCart.getBuyer().getRegZip());
      pCart.getBuyer().getRegCustomer()
        .setTaxDestination(pCart.getBuyer().getTaxDest());
    }
    TaxDestination txRules = null;
    if (pAs.getIsExtractSalesTaxFromSales()
      && !pCart.getBuyer().getRegCustomer().getIsForeigner()) {
      if (pCart.getBuyer().getRegCustomer().getTaxDestination() != null) {
        //override tax method:
        txRules = pCart.getBuyer().getRegCustomer().getTaxDestination();
      } else {
        txRules = new TaxDestination();
        txRules.setSalTaxIsInvoiceBase(pAs.getSalTaxIsInvoiceBase());
        txRules.setSalTaxUseAggregItBas(pAs.getSalTaxUseAggregItBas());
        txRules.setSalTaxRoundMode(pAs.getSalTaxRoundMode());
      }
    }
    return txRules;
  }
1135921
public final void retLines(final Map<String, Object> pRqVs,
    final OnlineBuyer pBur, final List<CustOrder> pOrds,
      final List<CuOrSe> pSords) throws Exception {
    StringBuffer ordIds = null;
    for (CustOrder co : pOrds) {
      co.setGoods(new ArrayList<CustOrderGdLn>());
      co.setServs(new ArrayList<CustOrderSrvLn>());
      if (ordIds == null) {
        ordIds = new StringBuffer();
        ordIds.append(co.getItsId().toString());
      } else {
        ordIds.append("," + co.getItsId());
      }
    }
    Set<String> ndFlNm = new HashSet<String>();
    ndFlNm.add("itsId");
    ndFlNm.add("itsName");
    Set<String> ndFl = new HashSet<String>();
    ndFl.add("itsId");
    ndFl.add("itsOwner");
    ndFl.add("itsName");
    ndFl.add("uom");
    ndFl.add("quant");
    ndFl.add("price");
    ndFl.add("tot");
    ndFl.add("totTx");
    String tbn;
    if (ordIds != null) {
      tbn = CustOrderGdLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      pRqVs.put(tbn + "itsOwnerdeepLevel", 1);
      pRqVs.put("UnitOfMeasureneededFields", ndFlNm);
      List<CustOrderGdLn> allGds = this.srvOrm.retrieveListWithConditions(pRqVs,
        CustOrderGdLn.class, "where ITSOWNER in (" + ordIds + ")");
      pRqVs.remove(tbn + "neededFields");
      pRqVs.remove(tbn + "itsOwnerdeepLevel");
      pRqVs.remove("UnitOfMeasureneededFields");
      for (CustOrderGdLn il : allGds) {
        for (CustOrder co : pOrds) {
          if (co.getItsId().equals(il.getItsOwner().getItsId())) {
            co.getGoods().add(il);
            break;
          }
        }
      }
      tbn = CustOrderSrvLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      pRqVs.put(tbn + "itsOwnerdeepLevel", 1);
      pRqVs.put("UnitOfMeasureneededFields", ndFlNm);
      List<CustOrderSrvLn> allSrs = this.srvOrm.retrieveListWithConditions(
        pRqVs, CustOrderSrvLn.class, "where ITSOWNER in (" + ordIds + ")");
      pRqVs.remove(tbn + "neededFields");
      pRqVs.remove(tbn + "itsOwnerdeepLevel");
      pRqVs.remove("UnitOfMeasureneededFields");
      for (CustOrderSrvLn il : allSrs) {
        for (CustOrder co : pOrds) {
          if (co.getItsId().equals(il.getItsOwner().getItsId())) {
            co.getServs().add(il);
            break;
          }
        }
      }
    }
    ordIds = null;
    for (CuOrSe co : pSords) {
      co.setGoods(new ArrayList<CuOrSeGdLn>());
      co.setServs(new ArrayList<CuOrSeSrLn>());
      if (ordIds == null) {
        ordIds = new StringBuffer();
        ordIds.append(co.getItsId().toString());
      } else {
        ordIds.append("," + co.getItsId());
      }
    }
    if (ordIds != null) {
      tbn = CuOrSeGdLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      pRqVs.put(tbn + "itsOwnerdeepLevel", 1);
      pRqVs.put("UnitOfMeasureneededFields", ndFlNm);
      List<CuOrSeGdLn> allGds = this.srvOrm.retrieveListWithConditions(pRqVs,
        CuOrSeGdLn.class, "where ITSOWNER in (" + ordIds + ")");
      pRqVs.remove(tbn + "neededFields");
      pRqVs.remove(tbn + "itsOwnerdeepLevel");
      pRqVs.remove("UnitOfMeasureneededFields");
      for (CuOrSeGdLn il : allGds) {
        for (CuOrSe co : pSords) {
          if (co.getItsId().equals(il.getItsOwner().getItsId())) {
            co.getGoods().add(il);
            break;
          }
        }
      }
      tbn = CuOrSeSrLn.class.getSimpleName();
      pRqVs.put(tbn + "neededFields", ndFl);
      pRqVs.put(tbn + "itsOwnerdeepLevel", 1);
      pRqVs.put("UnitOfMeasureneededFields", ndFlNm);
      List<CuOrSeSrLn> allSrs = this.srvOrm.retrieveListWithConditions(
        pRqVs, CuOrSeSrLn.class, "where ITSOWNER in (" + ordIds + ")");
      pRqVs.remove(tbn + "neededFields");
      pRqVs.remove(tbn + "itsOwnerdeepLevel");
      pRqVs.remove("UnitOfMeasureneededFields");
      for (CuOrSeSrLn il : allSrs) {
        for (CuOrSe co : pSords) {
          if (co.getItsId().equals(il.getItsOwner().getItsId())) {
            co.getServs().add(il);
            break;
          }
        }
      }
    }
  }
1001648
@Override
  public final void process(final Map<String, Object> pRqVs,
    final IRequestData pRqDt) throws Exception {
    OnlineBuyer buyer = this.buySr.getAuthBuyr(pRqVs, pRqDt);
    if (buyer == null) {
      String procNm = pRqDt.getParameter("nmPrcRed");
      IProcessor proc = this.procFac.lazyGet(pRqVs, procNm);
      proc.process(pRqVs, pRqDt);
      return;
    }
    SettingsAdd setAdd = (SettingsAdd) pRqVs.get("setAdd");
    String purIdStr = pRqDt.getParameter("pur");
    Long.parseLong(purIdStr);
    try {
      this.srvDb.setIsAutocommit(false);
      this.srvDb.setTransactionIsolation(setAdd.getBkTr());
      this.srvDb.beginTransaction();
      String tbn = CustOrder.class.getSimpleName();
      String whePuBr = "where PUR=" + purIdStr + " and BUYER="
        + buyer.getItsId();
      Set<String> ndFlNm = new HashSet<String>();
      ndFlNm.add("itsId");
      ndFlNm.add("itsName");
      pRqVs.put("PickUpPlaceneededFields", ndFlNm);
      pRqVs.put(tbn + "buyerdeepLevel", 1);
      List<CustOrder> ords = this.srvOrm.retrieveListWithConditions(pRqVs,
        CustOrder.class, whePuBr);
      pRqVs.remove(tbn + "buyerdeepLevel");
      tbn = CuOrSe.class.getSimpleName();
      Set<String> ndFlDc = new HashSet<String>();
      ndFlDc.add("seller");
      pRqVs.put("DebtorCreditorneededFields", ndFlNm);
      pRqVs.put("SeSellerneededFields", ndFlDc);
      pRqVs.put(tbn + "seldeepLevel", 3);
      pRqVs.put(tbn + "buyerdeepLevel", 1);
      List<CuOrSe> sords = this.srvOrm.retrieveListWithConditions(pRqVs,
        CuOrSe.class, whePuBr);
      pRqVs.remove(tbn + "buyerdeepLevel");
      pRqVs.remove("DebtorCreditorneededFields");
      pRqVs.remove("SeSellerneededFields");
      pRqVs.remove(tbn + "seldeepLevel");
      pRqVs.remove("PickUpPlaceneededFields");
      retLines(pRqVs, buyer, ords, sords);
      Purch pur = new Purch();
      pur.setOrds(ords);
      pur.setSords(sords);
      pRqDt.setAttribute("pur",  pur);
      this.srvDb.commitTransaction();
    } catch (Exception ex) {
      if (!this.srvDb.getIsAutocommit()) {
        this.srvDb.rollBackTransaction();
      }
      throw ex;
    } finally {
      this.srvDb.releaseResources();
    }
  }
547154
private static void _excute(final int index, final CancelableCountDownLatch counter, final Postcard postcard) {
        if (index < Warehouse.interceptors.size()) {
            IInterceptor iInterceptor = Warehouse.interceptors.get(index);
            iInterceptor.process(postcard, new InterceptorCallback() {
                @Override
                public void onContinue(Postcard postcard) {
                    // Last interceptor excute over with no exception.
                    counter.countDown();
                    _excute(index + 1, counter, postcard);  // When counter is down, it will be execute continue ,but index bigger than interceptors size, then U know.
                }

                @Override
                public void onInterrupt(Throwable exception) {
                    // Last interceptor excute over with fatal exception.

                    postcard.setTag(null == exception ? new HandlerException("No message.") : exception.getMessage());    // save the exception message for backup.
                    counter.cancel();
                    // Be attention, maybe the thread in callback has been changed,
                    // then the catch block(L207) will be invalid.
                    // The worst is the thread changed to main thread, then the app will be crash, if you throw this exception!
//                    if (!Looper.getMainLooper().equals(Looper.myLooper())) {    // You shouldn't throw the exception if the thread is main thread.
//                        throw new HandlerException(exception.getMessage());
//                    }
                }
            });
        }
    }
756710
protected void executeTx(final TxRunnable r, final TxRunnable errorHandler) throws Exception {
        Transaction transaction = beginTransaction();
        configureTransDefault(transaction);
        processTransactionError(transaction, t -> {
            try {
                r.run(t);
                t.commit();
            } catch (Throwable e) {
                t.rollback(e);
                throw e;
            } finally {
                t.end();
            }
            return null;
        }, errorHandler != null ? (TxCallable) t -> {
            errorHandler.run(t);
            return null;
        } : null);
    }
426741
@Override
  public final void delLine(final Map<String, Object> pRqVs,
    final CartLn pCartLn, final TaxDestination pTxRules) throws Exception {
    AccSettings as = (AccSettings) pRqVs.get("accSet");
    TradingSettings ts = (TradingSettings) pRqVs.get("tradSet");
    pCartLn.setDisab(true);
    String[] fieldsNames = new String[] {"itsId", "itsVersion", "disab"};
    pRqVs.put("fieldsNames", fieldsNames);
    getSrvOrm().updateEntity(pRqVs, pCartLn);
    if (pTxRules != null && pCartLn.getTxCat() != null && !pTxRules
      .getSalTaxIsInvoiceBase() && !pTxRules.getSalTaxUseAggregItBas()) {
      pRqVs.put("CartItTxLnitsOwnerdeepLevel", 1);
      List<CartItTxLn> itls = getSrvOrm().retrieveListWithConditions(
          pRqVs, CartItTxLn.class, "where DISAB=0 and ITSOWNER="
            + pCartLn.getItsId());
      pRqVs.remove("CartItTxLnitsOwnerdeepLevel");
      for (CartItTxLn itl : itls) {
        if (!itl.getDisab() && itl.getItsOwner().getItsId()
          .equals(pCartLn.getItsId())) {
          itl.setDisab(true);
          getSrvOrm().updateEntity(pRqVs, itl);
        }
      }
    }
    pRqVs.remove("fieldsNames");
    makeCartTotals(pRqVs, ts, pCartLn, as, pTxRules);
  }
772700
@Override
  public final void process(final Map<String, Object> pRqVs,
    final IRequestData pRqDt) throws Exception {
    TradingSettings ts = (TradingSettings) pRqVs.get("tradSet");
    Cart cart = this.srvCart.getShoppingCart(pRqVs, pRqDt, true, false);
    CartLn cartLn = null;
    String lnIdStr = pRqDt.getParameter("lnId");
    String quantStr = pRqDt.getParameter("quant");
    String avQuanStr = pRqDt.getParameter("avQuan");
    String unStepStr = pRqDt.getParameter("unStep");
    BigDecimal quant = new BigDecimal(quantStr);
    BigDecimal avQuan = new BigDecimal(avQuanStr);
    if (quant.compareTo(avQuan) == 1) {
      quant = avQuan;
    }
    BigDecimal unStep = new BigDecimal(unStepStr);
    String itIdStr = pRqDt.getParameter("itId");
    String itTypStr = pRqDt.getParameter("itTyp");
    Long itId = Long.valueOf(itIdStr);
    AccSettings as = (AccSettings) pRqVs.get("accSet");
    TaxDestination txRules = this.srvCart
      .revealTaxRules(pRqVs, cart, as);
    EShopItemType itTyp = EShopItemType.class.
      getEnumConstants()[Integer.parseInt(itTypStr)];
    boolean redoPr = false;
    if (lnIdStr != null) { //change quantity
      Long lnId = Long.valueOf(lnIdStr);
      cartLn = findCartItemById(cart, lnId);
    } else { //add
      redoPr = true;
      String uomIdStr = pRqDt.getParameter("uomId");
      Long uomId = Long.valueOf(uomIdStr);
      for (CartLn ci : cart.getItems()) {
        //check for duplicate cause "weird" but accepted request
        if (!ci.getDisab() && ci.getItTyp().equals(itTyp)
          && ci.getItId().equals(itId)) {
          cartLn = ci;
          break;
        }
      }
      if (cartLn == null) {
        for (CartLn ci : cart.getItems()) {
          if (ci.getDisab()) {
            cartLn = ci;
            cartLn.setDisab(false);
            cartLn.setForc(false);
            cartLn.setSeller(null);
            cartLn.setTxCat(null);
            cartLn.setTxDsc(null);
            break;
          }
        }
      }
      if (cartLn == null) {
        cartLn = createCartItem(cart);
      }
      UnitOfMeasure uom = new UnitOfMeasure();
      uom.setItsId(uomId);
      cartLn.setUom(uom);
      cartLn.setItId(itId);
      cartLn.setItTyp(itTyp);
    }
    if (!cartLn.getForc()) {
      cartLn.setAvQuan(avQuan);
      cartLn.setQuant(quant);
      cartLn.setUnStep(unStep);
      BigDecimal amount = cartLn.getPrice().multiply(cartLn.getQuant()).
        setScale(as.getPricePrecision(), as.getRoundingMode());
      if (ts.getTxExcl()) {
        cartLn.setSubt(amount);
      } else {
        cartLn.setTot(amount);
      }
      this.srvCart.makeCartLine(pRqVs, cartLn, as, ts, txRules, redoPr, true);
      this.srvCart.makeCartTotals(pRqVs, ts, cartLn, as, txRules);
      this.srvCart.hndCartChan(pRqVs, cart, txRules);
    }
    if (txRules != null) {
      pRqDt.setAttribute("txRules", txRules);
    }
    redir(pRqVs, pRqDt);
  }