@Override
  public final void withdrawal(final Map<String, Object> pAddParam,
    final IMakingWarehouseEntry pEntity,
      final WarehouseSite pWhSiteFrom) throws Exception {
    if (!pEntity.getIdDatabaseBirth()
      .equals(getSrvOrm().getIdDatabase())) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "can_not_make_ws_entry_for_foreign_src");
    }
    String langDef = (String) pAddParam.get("langDef");
    DateFormat dateFormat = DateFormat.getDateTimeInstance(
      DateFormat.MEDIUM, DateFormat.SHORT, new Locale(langDef));
    ColumnsValues cvs = null;
    if (this.fastLoc) {
      cvs = new ColumnsValues();
      cvs.getFormula().add("itsVersion");
      cvs.getFormula().add("theRest");
      cvs.put("itsVersion", "ITSVERSION+1");
    }
    if (pWhSiteFrom != null) {
      WarehouseRest wr = getSrvOrm().retrieveEntityWithConditions(pAddParam,
        WarehouseRest.class, "where THEREST>0 and INVITEM="
          + pEntity.getInvItem().getItsId() + " and UNITOFMEASURE="
            + pEntity.getUnitOfMeasure().getItsId() + " and WAREHOUSESITE="
              + pWhSiteFrom.getItsId());
      if (wr == null
        || wr.getTheRest().compareTo(pEntity.getItsQuantity()) < 0) {
        throw new ExceptionWithCode(PurchaseInvoice.THERE_IS_NO_GOODS,
          "There is no goods in stock, Item ID/UOM ID/ WS ID/ quantity"
            + pEntity.getInvItem().getItsId() + "/" + pEntity.getUnitOfMeasure()
              .getItsId() + "/" + pEntity.getItsQuantity());
      }
      if (this.fastLoc) {
        cvs.put("theRest", "THEREST-" + pEntity.getItsQuantity());
        this.srvDb.executeUpdate("WAREHOUSEREST", cvs, "WAREHOUSESITE="
      + wr.getWarehouseSite().getItsId() + " and INVITEM=" + wr.getInvItem()
    .getItsId() + " and UNITOFMEASURE=" + wr.getUnitOfMeasure().getItsId());
      } else {
        wr.setTheRest(wr.getTheRest().subtract(pEntity.getItsQuantity()));
        getSrvOrm().updateEntity(pAddParam, wr);
      }
      WarehouseEntry wm = new WarehouseEntry();
      wm.setIdDatabaseBirth(getSrvOrm().getIdDatabase());
      wm.setSourceId(pEntity.getItsId());
      wm.setSourceType(pEntity.constTypeCode());
      wm.setWarehouseSiteFrom(wr.getWarehouseSite());
      wm.setUnitOfMeasure(wr.getUnitOfMeasure());
      wm.setInvItem(wr.getInvItem());
      wm.setItsQuantity(pEntity.getItsQuantity());
      wm.setSourceOwnerId(pEntity.getOwnerId());
      wm.setSourceOwnerType(pEntity.getOwnerType());
      wm.setDescription(makeDescription(pEntity, langDef, dateFormat));
      getSrvOrm().insertEntity(pAddParam, wm);
      wm.setIsNew(false);
    } else {
      List<WarehouseRest> wrl = getSrvOrm().retrieveListWithConditions(
        pAddParam, WarehouseRest.class, "where THEREST>0 and INVITEM="
          + pEntity.getInvItem().getItsId() + " and UNITOFMEASURE="
            + pEntity.getUnitOfMeasure().getItsId());
      BigDecimal theRest = BigDecimal.ZERO;
      for (WarehouseRest wr : wrl) {
        theRest = theRest.add(wr.getTheRest());
        if (theRest.compareTo(pEntity.getItsQuantity()) >= 0) {
          break;
        }
      }
      if (theRest.compareTo(pEntity.getItsQuantity()) < 0) {
        throw new ExceptionWithCode(PurchaseInvoice.THERE_IS_NO_GOODS,
          "there_is_no_goods_in_stock");
      }
      BigDecimal quantityToLeaveRest = pEntity.getItsQuantity();
      for (WarehouseRest wr : wrl) {
        if (quantityToLeaveRest.doubleValue() == 0) {
          break;
        }
        BigDecimal quantityToLeave;
        if (wr.getTheRest().compareTo(quantityToLeaveRest) <= 0) {
          quantityToLeave = wr.getTheRest();
        } else {
          quantityToLeave = quantityToLeaveRest;
        }
        if (this.fastLoc) {
          cvs.put("theRest", "THEREST-" + quantityToLeave);
          this.srvDb.executeUpdate("WAREHOUSEREST", cvs, "WAREHOUSESITE="
        + wr.getWarehouseSite().getItsId() + " and INVITEM=" + wr.getInvItem()
      .getItsId() + " and UNITOFMEASURE=" + wr.getUnitOfMeasure().getItsId());
        } else {
          wr.setTheRest(wr.getTheRest().subtract(quantityToLeave));
          getSrvOrm().updateEntity(pAddParam, wr);
        }
        WarehouseEntry wm = new WarehouseEntry();
        wm.setIdDatabaseBirth(getSrvOrm().getIdDatabase());
        wm.setSourceId(pEntity.getItsId());
        wm.setSourceType(pEntity.constTypeCode());
        wm.setWarehouseSiteFrom(wr.getWarehouseSite());
        wm.setUnitOfMeasure(wr.getUnitOfMeasure());
        wm.setInvItem(wr.getInvItem());
        wm.setItsQuantity(quantityToLeave);
        wm.setSourceOwnerId(pEntity.getOwnerId());
        wm.setSourceOwnerType(pEntity.getOwnerType());
        wm.setDescription(makeDescription(pEntity, langDef, dateFormat));
        getSrvOrm().insertEntity(pAddParam, wm);
        wm.setIsNew(false);
        quantityToLeaveRest = quantityToLeaveRest.subtract(quantityToLeave);
      }
    }
  }
@ Override public final void withdrawal ( final Map < String , Object > pAddParam , final IMakingWarehouseEntry pEntity , final WarehouseSite pWhSiteFrom ) throws Exception { if ( ! pEntity . getIdDatabaseBirth ( ) . equals ( getSrvOrm ( ) . getIdDatabase ( ) ) ) { throw new ExceptionWithCode ( ExceptionWithCode . WRONG_PARAMETER , "can_not_make_ws_entry_for_foreign_src" ) ; } String langDef = ( String ) pAddParam . get ( "langDef" ) ; DateFormat dateFormat = DateFormat . getDateTimeInstance ( DateFormat . MEDIUM , DateFormat . SHORT , new Locale ( langDef ) ) ; ColumnsValues cvs = null ; if ( this . fastLoc ) { cvs = new ColumnsValues ( ) ; cvs . getFormula ( ) . add ( "itsVersion" ) ; cvs . getFormula ( ) . add ( "theRest" ) ; cvs . put ( "itsVersion" , "ITSVERSION+1" ) ; } if ( pWhSiteFrom != null ) { WarehouseRest wr = getSrvOrm ( ) . retrieveEntityWithConditions ( pAddParam , WarehouseRest . class , "where THEREST>0 and INVITEM=" + pEntity . getInvItem ( ) . getItsId ( ) + " and UNITOFMEASURE=" + pEntity . getUnitOfMeasure ( ) . getItsId ( ) + " and WAREHOUSESITE=" + pWhSiteFrom . getItsId ( ) ) ; if ( wr == null || wr . getTheRest ( ) . compareTo ( pEntity . getItsQuantity ( ) ) < 0 ) { throw new ExceptionWithCode ( PurchaseInvoice . THERE_IS_NO_GOODS , "There is no goods in stock, Item ID/UOM ID/ WS ID/ quantity" + pEntity . getInvItem ( ) . getItsId ( ) + "/" + pEntity . getUnitOfMeasure ( ) . getItsId ( ) + "/" + pEntity . getItsQuantity ( ) ) ; } if ( this . fastLoc ) { cvs . put ( "theRest" , "THEREST-" + pEntity . getItsQuantity ( ) ) ; this . srvDb . executeUpdate ( "WAREHOUSEREST" , cvs , "WAREHOUSESITE=" + wr . getWarehouseSite ( ) . getItsId ( ) + " and INVITEM=" + wr . getInvItem ( ) . getItsId ( ) + " and UNITOFMEASURE=" + wr . getUnitOfMeasure ( ) . getItsId ( ) ) ; } else { wr . setTheRest ( wr . getTheRest ( ) . subtract ( pEntity . getItsQuantity ( ) ) ) ; getSrvOrm ( ) . updateEntity ( pAddParam , wr ) ; } WarehouseEntry wm = new WarehouseEntry ( ) ; wm . setIdDatabaseBirth ( getSrvOrm ( ) . getIdDatabase ( ) ) ; wm . setSourceId ( pEntity . getItsId ( ) ) ; wm . setSourceType ( pEntity . constTypeCode ( ) ) ; wm . setWarehouseSiteFrom ( wr . getWarehouseSite ( ) ) ; wm . setUnitOfMeasure ( wr . getUnitOfMeasure ( ) ) ; wm . setInvItem ( wr . getInvItem ( ) ) ; wm . setItsQuantity ( pEntity . getItsQuantity ( ) ) ; wm . setSourceOwnerId ( pEntity . getOwnerId ( ) ) ; wm . setSourceOwnerType ( pEntity . getOwnerType ( ) ) ; wm . setDescription ( makeDescription ( pEntity , langDef , dateFormat ) ) ; getSrvOrm ( ) . insertEntity ( pAddParam , wm ) ; wm . setIsNew ( false ) ; } else { List < WarehouseRest > wrl = getSrvOrm ( ) . retrieveListWithConditions ( pAddParam , WarehouseRest . class , "where THEREST>0 and INVITEM=" + pEntity . getInvItem ( ) . getItsId ( ) + " and UNITOFMEASURE=" + pEntity . getUnitOfMeasure ( ) . getItsId ( ) ) ; BigDecimal theRest = BigDecimal . ZERO ; for ( WarehouseRest wr : wrl ) { theRest = theRest . add ( wr . getTheRest ( ) ) ; if ( theRest . compareTo ( pEntity . getItsQuantity ( ) ) >= 0 ) { break ; } } if ( theRest . compareTo ( pEntity . getItsQuantity ( ) ) < 0 ) { throw new ExceptionWithCode ( PurchaseInvoice . THERE_IS_NO_GOODS , "there_is_no_goods_in_stock" ) ; } BigDecimal quantityToLeaveRest = pEntity . getItsQuantity ( ) ; for ( WarehouseRest wr : wrl ) { if ( quantityToLeaveRest . doubleValue ( ) == 0 ) { break ; } BigDecimal quantityToLeave ; if ( wr . getTheRest ( ) . compareTo ( quantityToLeaveRest ) <= 0 ) { quantityToLeave = wr . getTheRest ( ) ; } else { quantityToLeave = quantityToLeaveRest ; } if ( this . fastLoc ) { cvs . put ( "theRest" , "THEREST-" + quantityToLeave ) ; this . srvDb . executeUpdate ( "WAREHOUSEREST" , cvs , "WAREHOUSESITE=" + wr . getWarehouseSite ( ) . getItsId ( ) + " and INVITEM=" + wr . getInvItem ( ) . getItsId ( ) + " and UNITOFMEASURE=" + wr . getUnitOfMeasure ( ) . getItsId ( ) ) ; } else { wr . setTheRest ( wr . getTheRest ( ) . subtract ( quantityToLeave ) ) ; getSrvOrm ( ) . updateEntity ( pAddParam , wr ) ; } WarehouseEntry wm = new WarehouseEntry ( ) ; wm . setIdDatabaseBirth ( getSrvOrm ( ) . getIdDatabase ( ) ) ; wm . setSourceId ( pEntity . getItsId ( ) ) ; wm . setSourceType ( pEntity . constTypeCode ( ) ) ; wm . setWarehouseSiteFrom ( wr . getWarehouseSite ( ) ) ; wm . setUnitOfMeasure ( wr . getUnitOfMeasure ( ) ) ; wm . setInvItem ( wr . getInvItem ( ) ) ; wm . setItsQuantity ( quantityToLeave ) ; wm . setSourceOwnerId ( pEntity . getOwnerId ( ) ) ; wm . setSourceOwnerType ( pEntity . getOwnerType ( ) ) ; wm . setDescription ( makeDescription ( pEntity , langDef , dateFormat ) ) ; getSrvOrm ( ) . insertEntity ( pAddParam , wm ) ; wm . setIsNew ( false ) ; quantityToLeaveRest = quantityToLeaveRest . subtract ( quantityToLeave ) ; } } }
<p>Withdrawal warehouse item to outside (or use/loss).</p>
@param pAddParam additional param
@param pEntity movement
@param pWhSiteFrom Site From, if null - automatically find sites
@throws Exception - an exception