Beispiel #1
0
  @Override
  protected String doIt() throws Exception {
    final long startTime = System.currentTimeMillis();

    // note: delete the scheds via API to make sure that the appropriate relations are also removed
    final List<MMPurchaseSchedule> schedsToDelete =
        new Query(getCtx(), I_M_PurchaseSchedule.Table_Name, "", get_TrxName())
            .setClient_ID()
            .list();
    for (final MMPurchaseSchedule sched : schedsToDelete) {
      // addLog("Deleting " + sched);
      sched.deleteEx(false);
    }

    final long afterDeleteTime = System.currentTimeMillis();
    addLog(
        "Deleted "
            + schedsToDelete.size()
            + " @M_PurchaseSchedule_ID@ records in "
            + TimeUtil.formatElapsed(afterDeleteTime - startTime));

    final IOrderPA orderPA = Services.get(IOrderPA.class);
    final IPurchaseScheduleBL purchaseScheduleBL = Services.get(IPurchaseScheduleBL.class);

    int createCounter = 0;
    for (final I_C_Order order :
        orderPA.retrieveOpenOrders(DocAction.STATUS_Completed, get_TrxName())) {
      if (!order.isSOTrx()) {
        continue;
      }

      final Collection<MMPurchaseSchedule> purchaseScheds =
          purchaseScheduleBL.retrieveOrCreateForSO(getCtx(), order, get_TrxName());
      for (final MMPurchaseSchedule ps : purchaseScheds) {
        purchaseScheduleBL.updateStorageData(getCtx(), ps, get_TrxName());
        purchaseScheduleBL.updateQtyToOrder(ps);
        ps.saveEx();
      }

      addLog(
          "Created "
              + purchaseScheds.size()
              + " @M_PurchaseSchedule_ID@ records for @C_Order_ID@ "
              + order.getDocumentNo());
      createCounter += purchaseScheds.size();
    }

    addLog(
        "Created "
            + createCounter
            + " purchase schedule records in "
            + TimeUtil.formatElapsed(System.currentTimeMillis() - afterDeleteTime));
    return "@Success@";
  }
 @Override
 public Object[] apply(final I_C_Order document) {
   final I_C_BPartner bpartner = document.getC_BPartner();
   final String bpValue = bpartner.getValue();
   final String bpName = bpartner.getName();
   return new Object[] {TableRecordReference.of(document), bpValue, bpName};
 }
  @Override
  public void add(final I_C_Order order) {
    Loggables.get().addLog("@Created@ " + order.getDocumentNo());

    orderGeneratedNotifier.notify(order, defaultNotificationRecipientId);

    countOrders.incrementAndGet();
  }
  /**
   * Creates a counter document for an order. The counter document is also processed, if there is a
   * {@link I_C_DocTypeCounter} with a <code>DocAction</code> configured.
   *
   * <p>This implementation partially uses legacy code. I didn't yet get to refactor/remove/replace
   * it all.
   */
  @Override
  public DocAction createCounterDocument(final DocAction document) {
    final I_C_Order order = InterfaceWrapperHelper.create(document, I_C_Order.class);
    final MOrder orderPO = LegacyAdapters.convertToPO(order);

    final I_C_DocType counterDocType = retrieveCounterDocTypeOrNull(document);

    final I_AD_Org counterOrg = retrieveCounterOrgOrNull(document);

    final de.metas.adempiere.model.I_C_Order counterOrder =
        InterfaceWrapperHelper.newInstance(
            de.metas.adempiere.model.I_C_Order.class, document.getCtx());
    final MOrder counterOrderPO = (MOrder) LegacyAdapters.convertToPO(counterOrder);

    counterOrder.setAD_Org(counterOrg); // 09700

    //
    counterOrder.setC_DocTypeTarget(counterDocType);
    counterOrder.setIsSOTrx(counterDocType.isSOTrx());

    // the new order needs to figure out the pricing by itself
    counterOrder.setM_PricingSystem(null);
    counterOrder.setM_PriceList(null);

    counterOrder.setDateOrdered(order.getDateOrdered());
    counterOrder.setDateAcct(order.getDateAcct());
    counterOrder.setDatePromised(order.getDatePromised());

    counterOrder.setRef_Order_ID(order.getC_Order_ID());

    final I_C_BPartner counterBP = retrieveCounterPartnerOrNull(document);
    counterOrderPO.setBPartner(counterBP);

    final I_M_Warehouse counterWarehouse =
        Services.get(IWarehouseAdvisor.class).evaluateOrderWarehouse(counterOrder);
    counterOrder.setM_Warehouse(counterWarehouse);

    // References (should not be required)
    counterOrder.setSalesRep_ID(order.getSalesRep_ID());
    InterfaceWrapperHelper.save(counterOrder);

    // copy the order lines
    final boolean counter = true;
    final boolean copyASI = true;
    counterOrderPO.copyLinesFrom(orderPO, counter, copyASI);

    // Update copied lines
    final boolean requery = true;
    final MOrderLine[] counterLines = counterOrderPO.getLines(requery, null);
    for (int i = 0; i < counterLines.length; i++) {
      final MOrderLine counterLine = counterLines[i];
      counterLine.setOrder(counterOrderPO); // copies header values (BP, etc.)
      counterLine.setPrice();
      counterLine.setTax();
      InterfaceWrapperHelper.save(counterLine);
    }
    logger.debug(counterOrder.toString());

    // Document Action
    final MDocTypeCounter counterDT =
        MDocTypeCounter.getCounterDocType(document.getCtx(), order.getC_DocType_ID());
    if (counterDT != null) {
      if (counterDT.getDocAction() != null) {
        counterOrder.setDocAction(counterDT.getDocAction());
        Services.get(IDocActionBL.class)
            .processEx(
                counterOrder,
                counterDT.getDocAction(),
                null); // not expecting a particular docStatus (e.g. for prepay orders, it might be
                       // "waiting to payment")
      }
    }
    return counterOrderPO;
  }
 /**
  * Code taken from {@link org.compiere.model.MOrder}.
  *
  * @return <code>true</code> if <code>Ref_Order_ID() > 0</code>.
  */
 @Override
 public boolean isCounterDocument(final DocAction document) {
   final I_C_Order order = InterfaceWrapperHelper.create(document, I_C_Order.class);
   return order.getRef_Order_ID() > 0;
 }
  @Override
  public int getC_TaxCategory_ID(final org.compiere.model.I_C_InvoiceLine invoiceLine) {
    // FIXME: we need to retrieve the C_TaxCategory_ID by using Pricing Engine

    if (invoiceLine.getC_Charge_ID() > 0) {
      return invoiceLine.getC_Charge().getC_TaxCategory_ID();
    }

    final I_C_Invoice invoice = invoiceLine.getC_Invoice();

    final IPriceListDAO priceListDAO = Services.get(IPriceListDAO.class);
    final Boolean processedPLVFiltering =
        null; // task 09533: the user doesn't know about PLV's processed flag, so we can't filter by
              // it

    if (invoice.getM_PriceList_ID() != 100) // FIXME use PriceList_None constant
    {
      final I_M_PriceList priceList = invoice.getM_PriceList();

      final I_M_PriceList_Version priceListVersion =
          priceListDAO.retrievePriceListVersionOrNull(
              priceList, invoice.getDateInvoiced(), processedPLVFiltering);
      Check.errorIf(
          priceListVersion == null, "Missing PLV for M_PriceList and DateInvoiced of {}", invoice);

      final int m_Product_ID = invoiceLine.getM_Product_ID();
      Check.assume(m_Product_ID > 0, "M_Product_ID > 0 for {}", invoiceLine);

      final I_M_ProductPrice productPrice =
          priceListDAO.retrieveProductPrice(priceListVersion, m_Product_ID);

      return productPrice.getC_TaxCategory_ID();
    }

    // Fallback: try getting from Order Line
    if (invoiceLine.getC_OrderLine_ID() > 0) {
      return invoiceLine.getC_OrderLine().getC_TaxCategory_ID();
    }

    // Fallback: try getting from Invoice -> Order
    if (invoiceLine.getC_Invoice().getC_Order_ID() > 0) {
      final Properties ctx = InterfaceWrapperHelper.getCtx(invoiceLine);
      final String trxName = InterfaceWrapperHelper.getTrxName(invoiceLine);

      final I_C_Order order =
          InterfaceWrapperHelper.create(
              ctx, invoiceLine.getC_Invoice().getC_Order_ID(), I_C_Order.class, trxName);

      final I_M_PriceList priceList = order.getM_PriceList();

      final I_M_PriceList_Version priceListVersion =
          priceListDAO.retrievePriceListVersionOrNull(
              priceList, invoice.getDateInvoiced(), processedPLVFiltering);
      Check.errorIf(
          priceListVersion == null, "Missing PLV for M_PriceList and DateInvoiced of {}", invoice);

      final int m_Product_ID = invoiceLine.getM_Product_ID();
      Check.assume(m_Product_ID > 0, "M_Product_ID > 0 for {}", invoiceLine);

      final I_M_ProductPrice productPrice =
          priceListDAO.retrieveProductPrice(priceListVersion, m_Product_ID);

      return productPrice.getC_TaxCategory_ID();
    }

    throw new AdempiereException(
        "@NotFound@ @C_TaxCategory_ID@ (" + "@C_InvoiceLine_ID@:" + invoiceLine + ")");
  }