Example usage for java.math BigDecimal multiply

List of usage examples for java.math BigDecimal multiply

Introduction

In this page you can find the example usage for java.math BigDecimal multiply.

Prototype

public BigDecimal multiply(BigDecimal multiplicand) 

Source Link

Document

Returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()) .

Usage

From source file:org.openbravo.test.costing.TestCosting.java

@Test
public void testCostingH1() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int day3 = 15;
    final int day4 = 20;
    final int day5 = 25;
    final int day6 = 30;
    final BigDecimal price1 = new BigDecimal("105.00");
    final BigDecimal price2 = new BigDecimal("105.50");
    final BigDecimal price3 = new BigDecimal("105.25");
    final BigDecimal price4 = new BigDecimal("106.00");
    final BigDecimal quantity1 = new BigDecimal("100");
    final BigDecimal quantity2 = new BigDecimal("200");
    final BigDecimal quantity3 = new BigDecimal("150");

    try {//  w w w .j  a va2 s . c  o  m

        OBContext.setOBContext(USER_ID, ROLE_ID, CLIENT_ID, ORGANIZATION_ID);
        OBContext.setAdminMode(true);

        // Create a new product for the test
        Product product = createProduct(price1);

        // Create purchase order and book it
        Order purchaseOrder1 = createPurchaseOrder(product, price1, quantity1, day0);

        // Create purchase order and book it
        Order purchaseOrder2 = createPurchaseOrder(product, price2, quantity1, day1);

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt1 = createGoodsReceipt(purchaseOrder1, price1, quantity1, day2);

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt2 = createGoodsReceipt(purchaseOrder2, price2, quantity1, day3);

        // Create purchase invoice, post it and assert it
        List<ShipmentInOut> goodsReceiptList = new ArrayList<ShipmentInOut>();
        goodsReceiptList.add(goodsReceipt1);
        goodsReceiptList.add(goodsReceipt2);
        List<BigDecimal> priceList = new ArrayList<BigDecimal>();
        priceList.add(price1);
        priceList.add(price2);
        createPurchaseInvoice(goodsReceiptList, priceList, quantity2, day4);

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment = createGoodsShipment(product, price3, quantity3, day6);

        // Update transaction total cost amount
        manualCostAdjustment(getProductTransactions(product.getId()).get(1), quantity1.multiply(price4), false,
                day5);

        // Assert product transactions
        List<ProductTransactionAssert> productTransactionAssertList = new ArrayList<ProductTransactionAssert>();
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt1.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price1, true, false));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt2.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price2, price4, true, true));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsShipment.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price3, price2));
        assertProductTransaction(product.getId(), productTransactionAssertList);

        // Assert product costing
        List<MaterialTransaction> transactionList = getProductTransactions(product.getId());
        List<ProductCostingAssert> productCostingAssertList = new ArrayList<ProductCostingAssert>();
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(0), price1, null, price1, quantity1));
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(1), price4, price3, price2, quantity2));
        assertProductCosting(product.getId(), productCostingAssertList);

        // Assert cost adjustment
        List<CostAdjustment> costAdjustmentList = getCostAdjustment(product.getId());
        List<List<CostAdjustmentAssert>> costAdjustmentAssertList = new ArrayList<List<CostAdjustmentAssert>>();
        List<CostAdjustmentAssert> costAdjustmentAssertLineList = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(1), "MCC",
                quantity1.multiply(price4).add(quantity1.multiply(price2).negate()), day5, true));
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(2), "MCC",
                quantity3.multiply(price2).add(quantity3.multiply(price3).negate()), day6, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList);
        assertCostAdjustment(costAdjustmentList, costAdjustmentAssertList);

        // Post cost adjustment 1 and assert it
        postDocument(costAdjustmentList.get(0));
        List<DocumentPostAssert> documentPostAssertList = new ArrayList<DocumentPostAssert>();
        documentPostAssertList.add(new DocumentPostAssert("61000", amount0,
                quantity1.multiply(price4).add(quantity1.multiply(price2).negate()), null));
        documentPostAssertList.add(new DocumentPostAssert("35000",
                quantity1.multiply(price4).add(quantity1.multiply(price2).negate()), amount0, null));
        documentPostAssertList.add(new DocumentPostAssert("99900",
                quantity3.multiply(price2).add(quantity3.multiply(price3).negate()), amount0, null));
        documentPostAssertList.add(new DocumentPostAssert("35000", amount0,
                quantity3.multiply(price2).add(quantity3.multiply(price3).negate()), null));
        CostAdjustment costAdjustment = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList.get(0).getId());
        assertDocumentPost(costAdjustment, product.getId(), documentPostAssertList);

        OBDal.getInstance().commitAndClose();

    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw new OBException(e);
    }

    finally {
        OBContext.restorePreviousMode();
    }
}

From source file:com.lp.server.fertigung.ejbfac.FertigungFacBean.java

public AuftragNachkalkulationDto getWerteAusUnterlosen(LosDto losDto, BigDecimal bdMenge,
        AuftragNachkalkulationDto abNachkalkulationDto, TheClientDto theClientDto) {

    LossollmaterialDto[] sollmat = lossollmaterialFindByLosIId(losDto.getIId());

    try {//w  w w .  j ava  2 s. c  o m

        BigDecimal bdWert = new BigDecimal(0);
        for (int i = 0; i < sollmat.length; i++) {
            LossollmaterialDto lossollmaterialDto = sollmat[i];
            LosistmaterialDto[] istMatDtos = losistmaterialFindByLossollmaterialIId(
                    lossollmaterialDto.getIId());

            for (int j = 0; j < istMatDtos.length; j++) {
                LosistmaterialDto istMatDto = istMatDtos[j];
                Query query = em.createNamedQuery("LagerbewegungfindByBelegartCNrBelegartPositionIId");
                query.setParameter(1, LocaleFac.BELEGART_LOS);
                query.setParameter(2, istMatDto.getIId());
                Collection cl = query.getResultList();

                Iterator it = cl.iterator();

                while (it.hasNext()) {
                    Lagerbewegung l = (Lagerbewegung) it.next();

                    LagerabgangursprungDto[] dtos = getLagerFac()
                            .lagerabgangursprungFindByLagerbewegungIIdBuchung(l.getIIdBuchung());

                    for (int m = 0; m < dtos.length; m++) {
                        // nun die Ursprungsbuchungen suchen
                        Session session2 = FLRSessionFactory.getFactory().openSession();
                        String sQuery2 = "from FLRLagerbewegung lagerbewegung WHERE lagerbewegung.i_id_buchung="
                                + dtos[m].getILagerbewegungidursprung()
                                + " order by lagerbewegung.t_buchungszeit DESC";
                        org.hibernate.Query ursrungsbuchung = session2.createQuery(sQuery2);
                        ursrungsbuchung.setMaxResults(1);

                        java.util.List resultList2 = ursrungsbuchung.list();

                        Iterator itUrsprung = resultList2.iterator();

                        while (itUrsprung.hasNext()) {
                            com.lp.server.artikel.fastlanereader.generated.FLRLagerbewegung lagerbewegung_ursprung = (com.lp.server.artikel.fastlanereader.generated.FLRLagerbewegung) itUrsprung
                                    .next();

                            // Wenn diese aus einer Losablieferung kommt
                            if (lagerbewegung_ursprung.getC_belegartnr()
                                    .equals(LocaleFac.BELEGART_LOSABLIEFERUNG)) {

                                LosablieferungDto laDto = losablieferungFindByPrimaryKey(
                                        lagerbewegung_ursprung.getI_belegartpositionid(), true, theClientDto);

                                LosDto losDtoUnterlos = losFindByPrimaryKey(laDto.getLosIId());
                                BigDecimal sollsatzgroesse = lossollmaterialDto.getNMenge()
                                        .divide(losDto.getNLosgroesse(), 4, BigDecimal.ROUND_HALF_EVEN);

                                abNachkalkulationDto.setBdGestehungswertmaterialist(
                                        abNachkalkulationDto.getBdGestehungswertmaterialist().subtract(
                                                laDto.getNArbeitszeitwertdetailliert().multiply(bdMenge)));
                                abNachkalkulationDto.setBdGestehungswertarbeitist(
                                        abNachkalkulationDto.getBdGestehungswertarbeitist()
                                                .add(laDto.getNArbeitszeitwertdetailliert().multiply(bdMenge)));

                                BigDecimal bdGesamtAbgeliefert = getErledigteMenge(laDto.getLosIId(),
                                        theClientDto);
                                Double dPers = getZeiterfassungFac().getSummeZeitenEinesBeleges(
                                        LocaleFac.BELEGART_LOS, losDtoUnterlos.getIId(), null, null, null, null,
                                        theClientDto);

                                BigDecimal arbeitszeitsoll = new BigDecimal(0);

                                BigDecimal maschinenzeitsoll = new BigDecimal(0);
                                LossollarbeitsplanDto[] sollarbeitsplanDtos = getFertigungFac()
                                        .lossollarbeitsplanFindByLosIId(losDtoUnterlos.getIId());

                                for (int u = 0; u < sollarbeitsplanDtos.length; u++) {
                                    LossollarbeitsplanDto sollarbeitsplanDto = sollarbeitsplanDtos[u];

                                    BigDecimal menge = sollarbeitsplanDto.getNGesamtzeit()
                                            .divide(losDtoUnterlos.getNLosgroesse(), 4,
                                                    BigDecimal.ROUND_HALF_EVEN)
                                            .multiply(bdMenge);
                                    /*
                                     * ArtikelDto artikelDto =
                                     * getArtikelFac()
                                     * .artikelFindByPrimaryKeySmall(
                                     * sollarbeitsplanDto
                                     * .getArtikelIIdTaetigkeit(),
                                     * theClientDto);
                                     * 
                                     * myLogger.warn("Los:" +
                                     * losDto.getCNr() + " Unterlos:" +
                                     * losDtoUnterlos.getCNr() + " AZ:" +
                                     * artikelDto.getCNr() + " Zeit:" +
                                     * Helper.rundeKaufmaennisch( menge,
                                     * 4));
                                     */

                                    if (sollarbeitsplanDto.getMaschineIId() == null) {
                                        arbeitszeitsoll = arbeitszeitsoll.add(menge);
                                    } else {
                                        maschinenzeitsoll = maschinenzeitsoll.add(menge);
                                        if (!Helper.short2boolean(sollarbeitsplanDto.getBNurmaschinenzeit())) {
                                            arbeitszeitsoll = arbeitszeitsoll.add(menge);
                                        }

                                    }
                                }

                                abNachkalkulationDto
                                        .setDdArbeitszeitsoll(abNachkalkulationDto.getDdArbeitszeitsoll()
                                                + arbeitszeitsoll.doubleValue());
                                abNachkalkulationDto
                                        .setDdMaschinenzeitsoll(abNachkalkulationDto.getDdMaschinenzeitsoll()
                                                + maschinenzeitsoll.doubleValue());

                                // Zeit duch die Gesamtablieferungen
                                // dividieren und mal bMenge
                                dPers = dPers / bdGesamtAbgeliefert.doubleValue() * bdMenge.doubleValue();
                                abNachkalkulationDto.setDdArbeitszeitist(
                                        abNachkalkulationDto.getDdArbeitszeitist() + dPers);

                                Double dMasch = getZeiterfassungFac().getSummeMaschinenZeitenEinesBeleges(
                                        losDtoUnterlos.getIId(), null, null, theClientDto);

                                dMasch = dMasch / bdGesamtAbgeliefert.doubleValue() * bdMenge.doubleValue();
                                abNachkalkulationDto.setDdMaschinenzeitist(
                                        abNachkalkulationDto.getDdMaschinenzeitist() + dMasch);

                                /*
                                 * System.out.println("Los: " +
                                 * losDto.getCNr() + " Stueckliste: " +
                                 * stklDto.getArtikelDto().getCNr() +
                                 * " UnterlosLos: " +
                                 * losDtoUnterlos.getCNr() + " Artikel: " +
                                 * aDto.getCNr() + " Menge:" + bdMenge);
                                 */

                                abNachkalkulationDto = getWerteAusUnterlosen(losDtoUnterlos,
                                        sollsatzgroesse.multiply(bdMenge), abNachkalkulationDto, theClientDto);

                            }

                        }

                        session2.close();
                    }

                }

            }

        }
    } catch (RemoteException ex) {
        throwEJBExceptionLPRespectOld(ex);

    }
    return abNachkalkulationDto;
}

From source file:org.openbravo.test.costing.TestCosting.java

@Test
public void testCostingN1() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int year = 1;
    final BigDecimal price1 = new BigDecimal("20.00");
    final BigDecimal price2 = new BigDecimal("15.00");
    final BigDecimal price3 = new BigDecimal("10.00");
    final BigDecimal price4 = new BigDecimal("17.50");
    final BigDecimal quantity1 = new BigDecimal("100");
    final BigDecimal quantity2 = new BigDecimal("200");
    final String costType = "AVA";

    try {//from w  ww . j  av  a  2s.co  m

        OBContext.setOBContext(USER_ID, ROLE_ID, CLIENT_ID, ORGANIZATION_ID);
        OBContext.setAdminMode(true);

        // Create a new product for the test
        Product product = createProduct(price1, price1, costType, year);

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment = createGoodsShipment(product, price1, quantity1, day0);

        // Create purchase order and book it
        Order purchaseOrder = createPurchaseOrder(product, price2, quantity2, day1);

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt = createGoodsReceipt(purchaseOrder, price2, quantity2, day1);

        // Assert product transactions
        List<ProductTransactionAssert> productTransactionAssertList = new ArrayList<ProductTransactionAssert>();
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsShipment.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price1));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price2, price4, price2));
        assertProductTransaction(product.getId(), productTransactionAssertList);

        // Assert product costing
        List<MaterialTransaction> transactionList = getProductTransactions(product.getId());
        List<ProductCostingAssert> productCostingAssertList = new ArrayList<ProductCostingAssert>();
        productCostingAssertList.add(new ProductCostingAssert(null, null, null, price1, null, costType, year));
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(1), price2, price3, price2, quantity1));
        assertProductCosting(product.getId(), productCostingAssertList);

        // Assert cost adjustment
        List<CostAdjustment> costAdjustmentList = getCostAdjustment(product.getId());
        List<List<CostAdjustmentAssert>> costAdjustmentAssertList = new ArrayList<List<CostAdjustmentAssert>>();
        List<CostAdjustmentAssert> costAdjustmentAssertLineList = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(1), "NSC",
                quantity2.multiply(price4).add(quantity2.multiply(price2).negate()), day1, true, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList);
        assertCostAdjustment(costAdjustmentList, costAdjustmentAssertList);

        // Post cost adjustment and assert it
        postDocument(costAdjustmentList.get(0));
        List<DocumentPostAssert> documentPostAssertList1 = new ArrayList<DocumentPostAssert>();
        documentPostAssertList1.add(new DocumentPostAssert("61000", amount0,
                quantity2.multiply(price4).add(quantity2.multiply(price2).negate()), null));
        documentPostAssertList1.add(new DocumentPostAssert("35000",
                quantity2.multiply(price4).add(quantity2.multiply(price2).negate()), amount0, null));
        CostAdjustment costAdjustment = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList.get(0).getId());
        assertDocumentPost(costAdjustment, product.getId(), documentPostAssertList1);

        OBDal.getInstance().commitAndClose();

    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw new OBException(e);
    }

    finally {
        OBContext.restorePreviousMode();
    }
}

From source file:org.openbravo.test.costing.TestCosting.java

@Test
public void testCostingII() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int year = 1;
    final BigDecimal price1 = new BigDecimal("95.00");
    final BigDecimal price2 = new BigDecimal("100.00");
    final BigDecimal quantity1 = new BigDecimal("1500");
    final BigDecimal quantity2 = new BigDecimal("100");
    final String costType = "STA";

    try {/* w ww .  ja  v a  2  s .  c om*/

        OBContext.setOBContext(USER_ID, ROLE_ID, CLIENT_ID, ORGANIZATION_ID);
        OBContext.setAdminMode(true);

        // Create a new product for the test
        Product product = createProduct(price1, price2, costType, year);

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt = createGoodsReceipt(product, price2, quantity1, day0);

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment = createGoodsShipment(product, price2, quantity2, day2);

        // Create purchase invoice, post it and assert it
        createPurchaseInvoice(goodsReceipt, price1, quantity1, day1);

        // Run price correction background
        runPriceBackground();

        // Assert product transactions
        List<ProductTransactionAssert> productTransactionAssertList = new ArrayList<ProductTransactionAssert>();
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price2, price1));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsShipment.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price2, price1));
        assertProductTransaction(product.getId(), productTransactionAssertList);

        // Assert product costing
        List<MaterialTransaction> transactionList = getProductTransactions(product.getId());
        List<ProductCostingAssert> productCostingAssertList = new ArrayList<ProductCostingAssert>();
        productCostingAssertList.add(new ProductCostingAssert(null, null, null, price2, null, costType, year));
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(0), price1, price2, price1, quantity1));
        assertProductCosting(product.getId(), productCostingAssertList);

        // Assert cost adjustment
        List<CostAdjustment> costAdjustmentList = getCostAdjustment(product.getId());
        List<List<CostAdjustmentAssert>> costAdjustmentAssertList = new ArrayList<List<CostAdjustmentAssert>>();
        List<CostAdjustmentAssert> costAdjustmentAssertLineList = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(0), "PDC",
                quantity1.multiply(price1).add(quantity1.multiply(price2).negate()), day1, true));
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(1), "PDC",
                quantity2.multiply(price1).add(quantity2.multiply(price2).negate()), day2, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList);
        assertCostAdjustment(costAdjustmentList, costAdjustmentAssertList);

        // Post cost adjustment and assert it
        postDocument(costAdjustmentList.get(0));
        List<DocumentPostAssert> documentPostAssertList = new ArrayList<DocumentPostAssert>();
        documentPostAssertList.add(new DocumentPostAssert("99904",
                quantity1.multiply(price2).add(quantity1.multiply(price1).negate()), amount0, null));
        documentPostAssertList.add(new DocumentPostAssert("35000", amount0,
                quantity1.multiply(price2).add(quantity1.multiply(price1).negate()), null));
        documentPostAssertList.add(new DocumentPostAssert("99900", amount0,
                quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), null));
        documentPostAssertList.add(new DocumentPostAssert("35000",
                quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), amount0, null));
        CostAdjustment costAdjustment = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList.get(0).getId());
        assertDocumentPost(costAdjustment, product.getId(), documentPostAssertList);

        OBDal.getInstance().commitAndClose();

    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw new OBException(e);
    }

    finally {
        OBContext.restorePreviousMode();
    }
}

From source file:org.openbravo.test.costing.TestCosting.java

@Test
public void testCostingJJ() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int year = -1;
    final BigDecimal price1 = new BigDecimal("95.00");
    final BigDecimal price2 = new BigDecimal("100.00");
    final BigDecimal price3 = new BigDecimal("195.00");
    final BigDecimal quantity1 = new BigDecimal("1500");
    final BigDecimal quantity2 = new BigDecimal("500");
    final String costType = "STA";

    try {//from w  w  w  .  j  a v  a  2  s. c o m

        OBContext.setOBContext(USER_ID, ROLE_ID, CLIENT_ID, ORGANIZATION_ID);
        OBContext.setAdminMode(true);

        // Create a new product for the test
        Product product = createProduct(price1, price2, costType, year);

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt = createGoodsReceipt(product, price1, quantity1, day0);

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment = createGoodsShipment(product, price1, quantity2, day2);

        // Create purchase invoice, post it and assert it
        createPurchaseInvoice(goodsReceipt, price3, quantity1, day1);

        // Run price correction background
        runPriceBackground();

        // Assert product transactions
        List<ProductTransactionAssert> productTransactionAssertList = new ArrayList<ProductTransactionAssert>();
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price3));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsShipment.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price3));
        assertProductTransaction(product.getId(), productTransactionAssertList);

        // Assert product costing
        List<MaterialTransaction> transactionList = getProductTransactions(product.getId());
        List<ProductCostingAssert> productCostingAssertList = new ArrayList<ProductCostingAssert>();
        productCostingAssertList.add(new ProductCostingAssert(null, null, null, price2, null, costType, year));
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(0), price3, price1, price3, quantity1));
        assertProductCosting(product.getId(), productCostingAssertList);

        // Assert cost adjustment
        List<CostAdjustment> costAdjustmentList = getCostAdjustment(product.getId());
        List<List<CostAdjustmentAssert>> costAdjustmentAssertList = new ArrayList<List<CostAdjustmentAssert>>();
        List<CostAdjustmentAssert> costAdjustmentAssertLineList = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(0), "PDC",
                quantity1.multiply(price3).add(quantity1.multiply(price1).negate()), day1, true));
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(1), "PDC",
                quantity2.multiply(price3).add(quantity2.multiply(price1).negate()), day2, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList);
        assertCostAdjustment(costAdjustmentList, costAdjustmentAssertList);

        // Post cost adjustment and assert it
        postDocument(costAdjustmentList.get(0));
        List<DocumentPostAssert> documentPostAssertList = new ArrayList<DocumentPostAssert>();
        documentPostAssertList.add(new DocumentPostAssert("99904", amount0,
                quantity1.multiply(price3).add(quantity1.multiply(price1).negate()), null));
        documentPostAssertList.add(new DocumentPostAssert("35000",
                quantity1.multiply(price3).add(quantity1.multiply(price1).negate()), amount0, null));
        documentPostAssertList.add(new DocumentPostAssert("99900",
                quantity2.multiply(price3).add(quantity2.multiply(price1).negate()), amount0, null));
        documentPostAssertList.add(new DocumentPostAssert("35000", amount0,
                quantity2.multiply(price3).add(quantity2.multiply(price1).negate()), null));
        CostAdjustment costAdjustment = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList.get(0).getId());
        assertDocumentPost(costAdjustment, product.getId(), documentPostAssertList);

        OBDal.getInstance().commitAndClose();

    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw new OBException(e);
    }

    finally {
        OBContext.restorePreviousMode();
    }
}

From source file:org.openbravo.test.costing.TestCosting.java

@Test
public void testCostingC1() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int day3 = 15;
    final BigDecimal price1 = new BigDecimal("15.00");
    final BigDecimal price2 = new BigDecimal("25.00");
    final BigDecimal quantity1 = new BigDecimal("180");
    final BigDecimal quantity2 = new BigDecimal("80");

    try {/*from  w  w  w.ja v  a  2  s .co  m*/

        OBContext.setOBContext(USER_ID, ROLE_ID, CLIENT_ID, ORGANIZATION_ID);
        OBContext.setAdminMode(true);

        // Create a new product for the test
        Product product = createProduct(price1);

        // Create purchase order and book it
        Order purchaseOrder = createPurchaseOrder(product, price1, quantity1, day0);

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt = createGoodsReceipt(purchaseOrder, price1, quantity1, day1);

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment = createGoodsShipment(product, price1, quantity2, day2);

        // Update purchase order line product price
        updatePurchaseOrder(purchaseOrder, price2);

        // Create purchase invoice, post it and assert it
        createPurchaseInvoice(goodsReceipt, price2, quantity1, day3);

        // Run price correction background
        runPriceBackground();

        // Assert product transactions
        List<ProductTransactionAssert> productTransactionAssertList = new ArrayList<ProductTransactionAssert>();
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price2));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsShipment.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price2));
        assertProductTransaction(product.getId(), productTransactionAssertList);

        // Assert product costing
        List<MaterialTransaction> transactionList = getProductTransactions(product.getId());
        List<ProductCostingAssert> productCostingAssertList = new ArrayList<ProductCostingAssert>();
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(0), price2, price1, price2, quantity1));
        assertProductCosting(product.getId(), productCostingAssertList);

        // Assert cost adjustment
        List<CostAdjustment> costAdjustmentList = getCostAdjustment(product.getId());
        List<List<CostAdjustmentAssert>> costAdjustmentAssertList = new ArrayList<List<CostAdjustmentAssert>>();
        List<CostAdjustmentAssert> costAdjustmentAssertLineList = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(0), "PDC",
                quantity1.multiply(price2).add(quantity1.multiply(price1).negate()), day3, true));
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(1), "PDC",
                quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), day3, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList);
        assertCostAdjustment(costAdjustmentList, costAdjustmentAssertList);

        // Post cost adjustment and assert it
        postDocument(costAdjustmentList.get(0));
        List<DocumentPostAssert> documentPostAssertList = new ArrayList<DocumentPostAssert>();
        documentPostAssertList.add(new DocumentPostAssert("99904", amount0,
                quantity1.multiply(price2).add(quantity1.multiply(price1).negate()), null));
        documentPostAssertList.add(new DocumentPostAssert("35000",
                quantity1.multiply(price2).add(quantity1.multiply(price1).negate()), amount0, null));
        documentPostAssertList.add(new DocumentPostAssert("99900",
                quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), amount0, null));
        documentPostAssertList.add(new DocumentPostAssert("35000", amount0,
                quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), null));
        CostAdjustment costAdjustment = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList.get(0).getId());
        assertDocumentPost(costAdjustment, product.getId(), documentPostAssertList);

        OBDal.getInstance().commitAndClose();

    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw new OBException(e);
    }

    finally {
        OBContext.restorePreviousMode();
    }
}

From source file:org.openbravo.test.costing.TestCosting.java

@Test
public void testCostingK2() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int year = 1;
    final BigDecimal price1 = new BigDecimal("95.00");
    final BigDecimal price2 = new BigDecimal("100.00");
    final BigDecimal price3 = new BigDecimal("80.00");
    final BigDecimal quantity1 = new BigDecimal("100");
    final BigDecimal quantity2 = new BigDecimal("90");
    final BigDecimal quantity3 = new BigDecimal("45");
    final String costType = "STA";

    try {/*from w  w w.j a  v  a2 s.  co  m*/

        OBContext.setOBContext(USER_ID, ROLE_ID, CLIENT_ID, ORGANIZATION_ID);
        OBContext.setAdminMode(true);

        // Create a new product for the test
        Product product = createProduct(price1, price2, costType, year);

        // Create purchase order and book it
        createPurchaseOrder(product, price3, quantity1, day0);

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt = createGoodsReceipt(product, price3, quantity2, day1);

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment = createGoodsShipment(product, price3, quantity3, day2);

        // Create purchase invoice, post it and assert it
        createPurchaseInvoice(goodsReceipt, price1, quantity2, day2);

        // Run price correction background
        runPriceBackground();

        // Assert product transactions
        List<ProductTransactionAssert> productTransactionAssertList1 = new ArrayList<ProductTransactionAssert>();
        productTransactionAssertList1.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price3, price1));
        productTransactionAssertList1.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsShipment.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price3, price1));
        assertProductTransaction(product.getId(), productTransactionAssertList1);

        // Assert product costing
        List<MaterialTransaction> transactionList1 = getProductTransactions(product.getId());
        List<ProductCostingAssert> productCostingAssertList1 = new ArrayList<ProductCostingAssert>();
        productCostingAssertList1.add(new ProductCostingAssert(null, null, null, price2, null, costType, year));
        productCostingAssertList1
                .add(new ProductCostingAssert(transactionList1.get(0), price1, price3, price1, quantity2));
        assertProductCosting(product.getId(), productCostingAssertList1);

        // Assert cost adjustment
        List<CostAdjustment> costAdjustmentList1 = getCostAdjustment(product.getId());
        List<List<CostAdjustmentAssert>> costAdjustmentAssertList1 = new ArrayList<List<CostAdjustmentAssert>>();
        List<CostAdjustmentAssert> costAdjustmentAssertLineList1 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList1.add(new CostAdjustmentAssert(transactionList1.get(0), "PDC",
                quantity2.multiply(price1).add(quantity2.multiply(price3).negate()), day2, true));
        costAdjustmentAssertLineList1.add(new CostAdjustmentAssert(transactionList1.get(1), "PDC",
                quantity3.multiply(price1).add(quantity3.multiply(price3).negate()), day2, false));
        costAdjustmentAssertList1.add(costAdjustmentAssertLineList1);
        assertCostAdjustment(costAdjustmentList1, costAdjustmentAssertList1);

        // Post cost adjustment and assert it
        postDocument(costAdjustmentList1.get(0));
        List<DocumentPostAssert> documentPostAssertList1 = new ArrayList<DocumentPostAssert>();
        documentPostAssertList1.add(new DocumentPostAssert("99904", amount0,
                quantity2.multiply(price1).add(quantity2.multiply(price3).negate()), null));
        documentPostAssertList1.add(new DocumentPostAssert("35000",
                quantity2.multiply(price1).add(quantity2.multiply(price3).negate()), amount0, null));
        documentPostAssertList1.add(new DocumentPostAssert("99900",
                quantity3.multiply(price1).add(quantity3.multiply(price3).negate()), amount0, null));
        documentPostAssertList1.add(new DocumentPostAssert("35000", amount0,
                quantity3.multiply(price1).add(quantity3.multiply(price3).negate()), null));
        CostAdjustment costAdjustment = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList1.get(0).getId());
        assertDocumentPost(costAdjustment, product.getId(), documentPostAssertList1);

        // Cancel Cost Adjustment
        cancelCostAdjustment(costAdjustment);

        // Assert product transactions
        List<ProductTransactionAssert> productTransactionAssertList2 = new ArrayList<ProductTransactionAssert>();
        productTransactionAssertList2.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price3, price3));
        productTransactionAssertList2.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsShipment.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price3, price3));
        assertProductTransaction(product.getId(), productTransactionAssertList2);

        // Assert product costing
        List<MaterialTransaction> transactionList2 = getProductTransactions(product.getId());
        List<ProductCostingAssert> productCostingAssertList2 = new ArrayList<ProductCostingAssert>();
        productCostingAssertList2.add(new ProductCostingAssert(null, null, null, price2, null, costType, year));
        productCostingAssertList2
                .add(new ProductCostingAssert(transactionList2.get(0), price3, price3, price3, quantity2));
        assertProductCosting(product.getId(), productCostingAssertList2);

        // Assert cost adjustment
        List<CostAdjustment> costAdjustmentList2 = getCostAdjustment(product.getId());
        List<List<CostAdjustmentAssert>> costAdjustmentAssertList2 = new ArrayList<List<CostAdjustmentAssert>>();
        List<CostAdjustmentAssert> costAdjustmentAssertLineList21 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList21.add(new CostAdjustmentAssert(transactionList2.get(0), "PDC",
                quantity2.multiply(price1).add(quantity2.multiply(price3).negate()), day2, true, "VO"));
        costAdjustmentAssertLineList21.add(new CostAdjustmentAssert(transactionList2.get(1), "PDC",
                quantity3.multiply(price1).add(quantity3.multiply(price3).negate()), day2, false, "VO"));
        costAdjustmentAssertList2.add(costAdjustmentAssertLineList21);
        List<CostAdjustmentAssert> costAdjustmentAssertLineList22 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList22.add(new CostAdjustmentAssert(transactionList2.get(0), "PDC",
                quantity2.multiply(price3).add(quantity2.multiply(price1).negate()), day2, true, "VO"));
        costAdjustmentAssertLineList22.add(new CostAdjustmentAssert(transactionList2.get(1), "PDC",
                quantity3.multiply(price3).add(quantity3.multiply(price1).negate()), day2, false, "VO"));
        costAdjustmentAssertList2.add(costAdjustmentAssertLineList22);
        assertCostAdjustment(costAdjustmentList2, costAdjustmentAssertList2);

        // Post cost adjustment 1 and assert it
        List<DocumentPostAssert> documentPostAssertList21 = new ArrayList<DocumentPostAssert>();
        documentPostAssertList21.add(new DocumentPostAssert("99904", amount0,
                quantity2.multiply(price1).add(quantity2.multiply(price3).negate()), null));
        documentPostAssertList21.add(new DocumentPostAssert("35000",
                quantity2.multiply(price1).add(quantity2.multiply(price3).negate()), amount0, null));
        documentPostAssertList21.add(new DocumentPostAssert("99900",
                quantity3.multiply(price1).add(quantity3.multiply(price3).negate()), amount0, null));
        documentPostAssertList21.add(new DocumentPostAssert("35000", amount0,
                quantity3.multiply(price1).add(quantity3.multiply(price3).negate()), null));
        CostAdjustment costAdjustment21 = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList2.get(0).getId());
        assertDocumentPost(costAdjustment21, product.getId(), documentPostAssertList21);

        // Post cost adjustment 2 and assert it
        postDocument(costAdjustmentList2.get(1));
        List<DocumentPostAssert> documentPostAssertList22 = new ArrayList<DocumentPostAssert>();
        documentPostAssertList22.add(new DocumentPostAssert("99904",
                quantity2.multiply(price1).add(quantity2.multiply(price3).negate()), amount0, null));
        documentPostAssertList22.add(new DocumentPostAssert("35000", amount0,
                quantity2.multiply(price1).add(quantity2.multiply(price3).negate()), null));
        documentPostAssertList22.add(new DocumentPostAssert("99900", amount0,
                quantity3.multiply(price1).add(quantity3.multiply(price3).negate()), null));
        documentPostAssertList22.add(new DocumentPostAssert("35000",
                quantity3.multiply(price1).add(quantity3.multiply(price3).negate()), amount0, null));
        CostAdjustment costAdjustment22 = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList2.get(1).getId());
        assertDocumentPost(costAdjustment22, product.getId(), documentPostAssertList22);

        OBDal.getInstance().commitAndClose();

    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw new OBException(e);
    }

    finally {
        OBContext.restorePreviousMode();
    }
}

From source file:org.openbravo.test.costing.TestCosting.java

@Test
public void testCostingCC() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int day3 = 15;
    final BigDecimal price1 = new BigDecimal("15.00");
    final BigDecimal price2 = new BigDecimal("25.00");
    final BigDecimal quantity1 = new BigDecimal("180");
    final BigDecimal quantity2 = new BigDecimal("80");

    try {//from  w  w w. j  a v a 2 s .  com

        OBContext.setOBContext(USER_ID, ROLE_ID, CLIENT_ID, ORGANIZATION_ID);
        OBContext.setAdminMode(true);

        // Create a new product for the test
        Product product = createProduct(price1);

        // Create purchase order and book it
        Order purchaseOrder = createPurchaseOrder(product, price1, quantity1, day0);

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt = createGoodsReceipt(purchaseOrder, price1, quantity1, day1);

        // Create purchase invoice, post it and assert it
        Invoice purchaseInvoice = createPurchaseInvoice(goodsReceipt, price1, quantity1, day2);

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment = createGoodsShipment(product, price1, quantity2, day3);

        // Update purchase order line product price
        updatePurchaseOrder(purchaseOrder, price2);

        // Update purchase invoice line product price
        updatePurchaseInvoice(purchaseInvoice, price2);

        // Run price correction background
        runPriceBackground();

        // Assert product transactions
        List<ProductTransactionAssert> productTransactionAssertList = new ArrayList<ProductTransactionAssert>();
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price2));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsShipment.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price2));
        assertProductTransaction(product.getId(), productTransactionAssertList);

        // Assert product costing
        List<MaterialTransaction> transactionList = getProductTransactions(product.getId());
        List<ProductCostingAssert> productCostingAssertList = new ArrayList<ProductCostingAssert>();
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(0), price2, price1, price2, quantity1));
        assertProductCosting(product.getId(), productCostingAssertList);

        // Assert cost adjustment
        List<CostAdjustment> costAdjustmentList = getCostAdjustment(product.getId());
        List<List<CostAdjustmentAssert>> costAdjustmentAssertList = new ArrayList<List<CostAdjustmentAssert>>();
        List<CostAdjustmentAssert> costAdjustmentAssertLineList = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(0), "PDC",
                quantity1.multiply(price2).add(quantity1.multiply(price1).negate()), day2, true));
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(1), "PDC",
                quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), day3, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList);
        assertCostAdjustment(costAdjustmentList, costAdjustmentAssertList);

        // Post cost adjustment and assert it
        postDocument(costAdjustmentList.get(0));
        List<DocumentPostAssert> documentPostAssertList = new ArrayList<DocumentPostAssert>();
        documentPostAssertList.add(new DocumentPostAssert("99904", amount0,
                quantity1.multiply(price2).add(quantity1.multiply(price1).negate()), null));
        documentPostAssertList.add(new DocumentPostAssert("35000",
                quantity1.multiply(price2).add(quantity1.multiply(price1).negate()), amount0, null));
        documentPostAssertList.add(new DocumentPostAssert("99900",
                quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), amount0, null));
        documentPostAssertList.add(new DocumentPostAssert("35000", amount0,
                quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), null));
        CostAdjustment costAdjustment = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList.get(0).getId());
        assertDocumentPost(costAdjustment, product.getId(), documentPostAssertList);

        OBDal.getInstance().commitAndClose();

    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw new OBException(e);
    }

    finally {
        OBContext.restorePreviousMode();
    }
}

From source file:org.openbravo.test.costing.TestCosting.java

@Test
public void testCostingAAA() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int day3 = 15;
    final int day4 = 20;
    final int day5 = 25;
    final BigDecimal price1 = new BigDecimal("50.00");
    final BigDecimal price2 = new BigDecimal("70.00");
    final BigDecimal price3 = new BigDecimal("80.00");
    final BigDecimal price4 = new BigDecimal("62.00");
    final BigDecimal price5 = new BigDecimal("68.00");
    final BigDecimal quantity1 = new BigDecimal("100");
    final BigDecimal quantity2 = new BigDecimal("150");

    try {/*from  w  w  w . jav a 2s.com*/

        OBContext.setOBContext(USER_ID, ROLE_ID, CLIENT_ID, ORGANIZATION_ID);
        OBContext.setAdminMode(true);

        // Create a new product for the test
        Product product = createProduct(price1);

        // Create purchase order and book it
        Order purchaseOrder1 = createPurchaseOrder(product, price1, quantity1, day0);

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt1 = createGoodsReceipt(purchaseOrder1, price1, quantity1, day1);

        // Create purchase invoice, post it and assert it
        createPurchaseInvoice(goodsReceipt1, price1, quantity1, day2);

        // Create purchase order and book it
        Order purchaseOrder2 = createPurchaseOrder(product, price2, quantity2, day3);

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt2 = createGoodsReceipt(purchaseOrder2, price2, quantity2, day4);

        // Create purchase invoice, post it and assert it
        createPurchaseInvoice(goodsReceipt2, price3, quantity2, day5);

        // Run price correction background
        runPriceBackground();

        // Assert product transactions
        List<ProductTransactionAssert> productTransactionAssertList = new ArrayList<ProductTransactionAssert>();
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt1.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price1));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt2.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price2, price3));
        assertProductTransaction(product.getId(), productTransactionAssertList);

        // Assert product costing
        List<MaterialTransaction> transactionList = getProductTransactions(product.getId());
        List<ProductCostingAssert> productCostingAssertList = new ArrayList<ProductCostingAssert>();
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(0), price1, null, price1, quantity1));
        productCostingAssertList.add(new ProductCostingAssert(transactionList.get(1), price3, price4, price5,
                quantity1.add(quantity2)));
        assertProductCosting(product.getId(), productCostingAssertList);

        // Assert cost adjustment
        List<CostAdjustment> costAdjustmentList = getCostAdjustment(product.getId());
        List<List<CostAdjustmentAssert>> costAdjustmentAssertList = new ArrayList<List<CostAdjustmentAssert>>();
        List<CostAdjustmentAssert> costAdjustmentAssertLineList = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(1), "PDC",
                quantity2.multiply(price3).add(quantity2.multiply(price2).negate()), day5, true));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList);
        assertCostAdjustment(costAdjustmentList, costAdjustmentAssertList);

        // Post cost adjustment and assert it
        postDocument(costAdjustmentList.get(0));
        List<DocumentPostAssert> documentPostAssertList = new ArrayList<DocumentPostAssert>();
        documentPostAssertList.add(new DocumentPostAssert("99904", amount0,
                quantity2.multiply(price3).add(quantity2.multiply(price2).negate()), null));
        documentPostAssertList.add(new DocumentPostAssert("35000",
                quantity2.multiply(price3).add(quantity2.multiply(price2).negate()), amount0, null));
        CostAdjustment costAdjustment = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList.get(0).getId());
        assertDocumentPost(costAdjustment, product.getId(), documentPostAssertList);

        OBDal.getInstance().commitAndClose();

    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw new OBException(e);
    }

    finally {
        OBContext.restorePreviousMode();
    }
}

From source file:org.openbravo.test.costing.TestCosting.java

@Test
public void testCostingDDD() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int day3 = 15;
    final BigDecimal price1 = new BigDecimal("25.00");
    final BigDecimal price2 = new BigDecimal("20.00");
    final BigDecimal price3 = new BigDecimal("24.3103");
    final BigDecimal quantity1 = new BigDecimal("580");
    final BigDecimal quantity2 = new BigDecimal("80");
    final BigDecimal quantity3 = new BigDecimal("500");

    try {//w  ww  .  ja  va 2s.c om

        OBContext.setOBContext(USER_ID, ROLE_ID, CLIENT_ID, ORGANIZATION_ID);
        OBContext.setAdminMode(true);

        // Create a new product for the test
        Product product = createProduct(price1);

        // Create purchase order and book it
        Order purchaseOrder = createPurchaseOrder(product, price1, quantity1, day0);

        // Create purchase invoice, post it and assert it
        Invoice purchaseInvoice = createPurchaseInvoice(purchaseOrder, price1, quantity1, day1);

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt1 = createGoodsReceipt(purchaseInvoice, price1, quantity2, day2);

        // Update purchase invoice line product price
        updatePurchaseInvoice(purchaseInvoice, price2);

        // Run price correction background
        runPriceBackground();

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt2 = createGoodsReceipt(purchaseInvoice, price1, quantity3, day3);

        // Run price correction background
        runPriceBackground();

        // Assert product transactions
        List<ProductTransactionAssert> productTransactionAssertList = new ArrayList<ProductTransactionAssert>();
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt1.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price2));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt2.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price2));
        assertProductTransaction(product.getId(), productTransactionAssertList);

        // Assert product costing
        List<MaterialTransaction> transactionList = getProductTransactions(product.getId());
        List<ProductCostingAssert> productCostingAssertList = new ArrayList<ProductCostingAssert>();
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(0), price2, price1, price2, quantity2));
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(1), price2, price3, price2, quantity1));
        assertProductCosting(product.getId(), productCostingAssertList);

        // Assert cost adjustment
        List<CostAdjustment> costAdjustmentList = getCostAdjustment(product.getId());
        List<List<CostAdjustmentAssert>> costAdjustmentAssertList = new ArrayList<List<CostAdjustmentAssert>>();
        List<CostAdjustmentAssert> costAdjustmentAssertLineList1 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList1.add(new CostAdjustmentAssert(transactionList.get(0), "PDC",
                quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), day1, true));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList1);
        List<CostAdjustmentAssert> costAdjustmentAssertLineList2 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList2.add(new CostAdjustmentAssert(transactionList.get(1), "PDC",
                quantity3.multiply(price2).add(quantity3.multiply(price1).negate()), day1, true));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList2);
        assertCostAdjustment(costAdjustmentList, costAdjustmentAssertList);

        // Post cost adjustment 1 and assert it
        postDocument(costAdjustmentList.get(0));
        List<DocumentPostAssert> documentPostAssertList1 = new ArrayList<DocumentPostAssert>();
        documentPostAssertList1.add(new DocumentPostAssert("99904",
                quantity2.multiply(price1).add(quantity2.multiply(price2).negate()), amount0, null));
        documentPostAssertList1.add(new DocumentPostAssert("35000", amount0,
                quantity2.multiply(price1).add(quantity2.multiply(price2).negate()), null));
        CostAdjustment costAdjustment1 = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList.get(0).getId());
        assertDocumentPost(costAdjustment1, product.getId(), documentPostAssertList1);

        // Post cost adjustment 2 and assert it
        postDocument(costAdjustmentList.get(1));
        List<DocumentPostAssert> documentPostAssertList2 = new ArrayList<DocumentPostAssert>();
        documentPostAssertList2.add(new DocumentPostAssert("99904",
                quantity3.multiply(price1).add(quantity3.multiply(price2).negate()), amount0, null));
        documentPostAssertList2.add(new DocumentPostAssert("35000", amount0,
                quantity3.multiply(price1).add(quantity3.multiply(price2).negate()), null));
        CostAdjustment costAdjustment2 = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList.get(1).getId());
        assertDocumentPost(costAdjustment2, product.getId(), documentPostAssertList2);

        OBDal.getInstance().commitAndClose();

    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw new OBException(e);
    }

    finally {
        OBContext.restorePreviousMode();
    }
}