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 testCostingJJJ() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int day3 = 15;
    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 BigDecimal quantity3 = new BigDecimal("50");
    final String costType = "STA";

    try {/*w  w  w.  ja  v  a 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, 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 goodsShipment1 = createGoodsShipment(product, price1, quantity2, day1);

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

        // Run price correction background
        runPriceBackground();

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment2 = createGoodsShipment(product, price3, 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, goodsReceipt.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price3));
        productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance()
                .get(ShipmentInOut.class, goodsShipment1.getId()).getMaterialMgmtShipmentInOutLineList().get(0),
                price1, price3));
        productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance()
                .get(ShipmentInOut.class, goodsShipment2.getId()).getMaterialMgmtShipmentInOutLineList().get(0),
                price3, 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()), day2, 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 testCostingV911() 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 price3 = new BigDecimal("35.00");
    final BigDecimal quantity1 = new BigDecimal("500");
    final BigDecimal quantity2 = new BigDecimal("400");
    final BigDecimal quantity3 = new BigDecimal("200");
    final BigDecimal quantity4 = new BigDecimal("300");
    final String costType = "AVA";

    try {//from w w w. j a va  2s.  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);

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

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

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

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

        // Assert product transactions
        List<ProductTransactionAssert> productTransactionAssertList = new ArrayList<ProductTransactionAssert>();
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsShipment.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price2, price2));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt1.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price2, price3, price1));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt2.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, 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));
        productCostingAssertList.add(new ProductCostingAssert(transactionList.get(1), price1, price2, price1,
                quantity2.negate().add(quantity3)));
        productCostingAssertList.add(new ProductCostingAssert(transactionList.get(2), price1, null, price1,
                quantity2.negate().add(quantity3).add(quantity4)));
        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(1), "PDC",
                quantity3.multiply(price1).add(quantity3.multiply(price2).negate()), day0, true));
        costAdjustmentAssertLineList1.add(new CostAdjustmentAssert(transactionList.get(1), "NSC",
                quantity3.multiply(price3).add(quantity3.multiply(price1).negate()), day2, false, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList1);
        List<CostAdjustmentAssert> costAdjustmentAssertLineList2 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList2
                .add(new CostAdjustmentAssert(transactionList.get(2), "NSC", amount0, day3, true, false));
        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",
                quantity3.multiply(price2).add(quantity3.multiply(price1).negate()), amount0, null));
        documentPostAssertList1.add(new DocumentPostAssert("35000", amount0,
                quantity3.multiply(price2).add(quantity3.multiply(price1).negate()), null));
        documentPostAssertList1.add(new DocumentPostAssert("61000", amount0,
                quantity3.multiply(price3).add(quantity3.multiply(price1).negate()), null));
        documentPostAssertList1.add(new DocumentPostAssert("35000",
                quantity3.multiply(price3).add(quantity3.multiply(price1).negate()), amount0, null));
        CostAdjustment costAdjustment1 = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList.get(0).getId());
        assertDocumentPost(costAdjustment1, 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 testCostingN0() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int day3 = 15;
    final int day4 = 20;
    final BigDecimal price1 = new BigDecimal("10.00");
    final BigDecimal price2 = new BigDecimal("20.00");
    final BigDecimal price3 = new BigDecimal("30.00");
    final BigDecimal price4 = new BigDecimal("15.00");
    final BigDecimal quantity1 = new BigDecimal("100");
    final BigDecimal quantity2 = new BigDecimal("5");
    final BigDecimal quantity3 = new BigDecimal("10");

    try {/*from www  .j  a v  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 goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment1 = createGoodsShipment(product, price1, quantity1, day2);

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

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

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

        // 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, goodsShipment1.getId()).getMaterialMgmtShipmentInOutLineList().get(0),
                price1, price1));
        productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance()
                .get(ShipmentInOut.class, goodsShipment2.getId()).getMaterialMgmtShipmentInOutLineList().get(0),
                price1, price1));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt2.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(transactionList.get(0), price1, null, price1, quantity1));
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(3), price2, 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(3), "NSC",
                quantity3.multiply(price4).add(quantity3.multiply(price2).negate()), day4, 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",
                quantity3.multiply(price2).add(quantity3.multiply(price4).negate()), amount0, null));
        documentPostAssertList1.add(new DocumentPostAssert("35000", amount0,
                quantity3.multiply(price2).add(quantity3.multiply(price4).negate()), 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 testCostingN5() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int day3 = 15;
    final int day4 = 20;
    final BigDecimal price1 = new BigDecimal("5.00");
    final BigDecimal price2 = new BigDecimal("7.00");
    final BigDecimal price3 = new BigDecimal("6.6667");
    final BigDecimal price4 = new BigDecimal("5.40");
    final BigDecimal quantity1 = new BigDecimal("10");
    final BigDecimal quantity2 = new BigDecimal("8");
    final BigDecimal quantity3 = new BigDecimal("12");

    try {/*from   w w  w .j  a  va  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);

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

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

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

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

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment2 = createGoodsShipment(product, 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, goodsReceipt1.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price1));
        productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance()
                .get(ShipmentInOut.class, goodsShipment2.getId()).getMaterialMgmtShipmentInOutLineList().get(0),
                price3, price1));
        productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance()
                .get(ShipmentInOut.class, goodsShipment1.getId()).getMaterialMgmtShipmentInOutLineList().get(0),
                price1, price1));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt2.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(transactionList.get(0), price1, null, price1, quantity1));
        productCostingAssertList.add(new ProductCostingAssert(transactionList.get(3), price2, price3, price2,
                quantity3.subtract(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), "BDT",
                quantity1.multiply(price1).add(quantity1.multiply(price3).negate()), day1, true));
        costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(3), "NSC",
                quantity1.multiply(price4).add(quantity1.multiply(price2).negate()), day4, false, 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("99900", amount0,
                quantity1.multiply(price3).add(quantity1.multiply(price1).negate()), null));
        documentPostAssertList1.add(new DocumentPostAssert("35000",
                quantity1.multiply(price3).add(quantity1.multiply(price1).negate()), amount0, null));
        documentPostAssertList1.add(new DocumentPostAssert("61000",
                quantity1.multiply(price2).add(quantity1.multiply(price4).negate()), amount0, null));
        documentPostAssertList1.add(new DocumentPostAssert("35000", amount0,
                quantity1.multiply(price2).add(quantity1.multiply(price4).negate()), 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 testCostingV11() 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("20.00");
    final BigDecimal price2 = new BigDecimal("15.00");
    final BigDecimal price3 = new BigDecimal("16.6667");
    final BigDecimal price4 = new BigDecimal("25.00");
    final BigDecimal price5 = new BigDecimal("18.3333");
    final BigDecimal price6 = new BigDecimal("21.6667");
    final BigDecimal quantity1 = new BigDecimal("100");
    final BigDecimal quantity2 = new BigDecimal("200");
    final BigDecimal quantity3 = new BigDecimal("300");
    final BigDecimal quantity4 = new BigDecimal("150");

    try {//from   w ww  . 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 purchaseOrder1 = createPurchaseOrder(product, price1, quantity1, day0);

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

        // Create goods receipt, run costing background, post it and assert it
        List<Order> purchaseOrderList = new ArrayList<Order>();
        purchaseOrderList.add(purchaseOrder1);
        purchaseOrderList.add(purchaseOrder2);
        ShipmentInOut goodsReceipt = createGoodsReceipt(purchaseOrderList, price3, quantity3, day2);

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

        // Create purchase invoice, post it and assert it
        createPurchaseInvoice(purchaseOrder1, price4, quantity1, day3);

        // Run price correction background
        runPriceBackground();

        // Create purchase invoice, post it and assert it
        createPurchaseInvoice(purchaseOrder2, price1, quantity2, day4);

        // 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, price4));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(1), price2, price1));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsShipment.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price3, price6));
        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(1), price1, price2, price1, quantity2));
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(0), price4, price3, price6, quantity3));
        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",
                quantity1.multiply(price4).add(quantity1.multiply(price1).negate()), day3, true));
        costAdjustmentAssertLineList1.add(new CostAdjustmentAssert(transactionList.get(2), "PDC",
                quantity4.multiply(price5).add(quantity4.multiply(price3).negate()), day5, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList1);
        List<CostAdjustmentAssert> costAdjustmentAssertLineList2 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList2.add(new CostAdjustmentAssert(transactionList.get(1), "PDC",
                quantity2.multiply(price1).add(quantity2.multiply(price2).negate()), day4, true));
        costAdjustmentAssertLineList2.add(new CostAdjustmentAssert(transactionList.get(2), "PDC",
                quantity4.multiply(price6).add(quantity4.multiply(price5).negate()), day5, false));
        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", amount0,
                quantity1.multiply(price4).add(quantity1.multiply(price1).negate()), null));
        documentPostAssertList1.add(new DocumentPostAssert("35000",
                quantity1.multiply(price4).add(quantity1.multiply(price1).negate()), amount0, null));
        documentPostAssertList1.add(new DocumentPostAssert("99900",
                quantity4.multiply(price5).add(quantity4.multiply(price3).negate()), amount0, null));
        documentPostAssertList1.add(new DocumentPostAssert("35000", amount0,
                quantity4.multiply(price5).add(quantity4.multiply(price3).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", amount0,
                quantity2.multiply(price1).add(quantity2.multiply(price2).negate()), null));
        documentPostAssertList2.add(new DocumentPostAssert("35000",
                quantity2.multiply(price1).add(quantity2.multiply(price2).negate()), amount0, null));
        documentPostAssertList2.add(new DocumentPostAssert("99900",
                quantity4.multiply(price6).add(quantity4.multiply(price5).negate()), amount0, null));
        documentPostAssertList2.add(new DocumentPostAssert("35000", amount0,
                quantity4.multiply(price6).add(quantity4.multiply(price5).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();
    }
}

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

@Test
public void testCostingF2() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int day3 = 15;
    final BigDecimal price1 = new BigDecimal("1230.00");
    final BigDecimal price2 = new BigDecimal("1200.00");
    final BigDecimal price3 = new BigDecimal("1500.00");
    final BigDecimal price4 = new BigDecimal("1174.50");
    final BigDecimal quantity1 = new BigDecimal("185");
    final BigDecimal quantity2 = new BigDecimal("85");

    try {/*w w  w .ja va  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);

        // 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, day3);

        // Create inventory amount update and run costing background
        InventoryAmountUpdate inventoryAmountUpdate = createInventoryAmountUpdate(product, price1, price2,
                quantity1, day2);

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

        // 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(InventoryAmountUpdate.class, inventoryAmountUpdate.getId())
                        .getInventoryAmountUpdateLineList().get(0),
                price1, price3));
        productTransactionAssertList.add(new ProductTransactionAssert(
                OBDal.getInstance().get(InventoryAmountUpdate.class, inventoryAmountUpdate.getId())
                        .getInventoryAmountUpdateLineList().get(0),
                price2, price2, true));
        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), price3, price1, price3, quantity1));
        productCostingAssertList
                .add(new ProductCostingAssert(transactionList.get(2), price2, price4, 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(1), "BDT", amount0, day2, true));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList1);
        List<CostAdjustmentAssert> costAdjustmentAssertLineList2 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList2
                .add(new CostAdjustmentAssert(transactionList.get(2), "BDT", amount0, day2, true));
        costAdjustmentAssertLineList2.add(new CostAdjustmentAssert(transactionList.get(3), "PDC",
                quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), day3, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList2);
        List<CostAdjustmentAssert> costAdjustmentAssertLineList3 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList3.add(new CostAdjustmentAssert(transactionList.get(0), "PDC",
                quantity1.multiply(price3).add(quantity1.multiply(price1).negate()), day2, true));
        costAdjustmentAssertLineList3.add(new CostAdjustmentAssert(transactionList.get(1), "PDC",
                quantity1.multiply(price3).add(quantity1.multiply(price1).negate()), day2, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList3);
        assertCostAdjustment(costAdjustmentList, costAdjustmentAssertList);

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

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

        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 testCostingV221() throws Exception {

    final int day0 = 0;
    final int day1 = 5;
    final int day2 = 10;
    final int day3 = 15;
    final int day4 = 20;
    final BigDecimal price1 = new BigDecimal("15.00");
    final BigDecimal price2 = new BigDecimal("12.00");
    final BigDecimal price3 = new BigDecimal("20.00");
    final BigDecimal price4 = new BigDecimal("18.00");
    final BigDecimal price5 = new BigDecimal("19.80");
    final BigDecimal quantity1 = new BigDecimal("500");
    final BigDecimal quantity2 = new BigDecimal("700");
    final BigDecimal quantity3 = new BigDecimal("150");
    final BigDecimal quantity4 = new BigDecimal("600");
    final BigDecimal quantity5 = new BigDecimal("50");
    final BigDecimal amount1 = new BigDecimal("1350.00");
    final BigDecimal amount2 = new BigDecimal("-450.00");

    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);

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

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

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

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

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

        // Create purchase invoice, post it and assert it
        List<Order> purchaseOrderList = new ArrayList<Order>();
        purchaseOrderList.add(purchaseOrder1);
        purchaseOrderList.add(purchaseOrder2);
        List<BigDecimal> priceList = new ArrayList<BigDecimal>();
        priceList.add(price3);
        priceList.add(price4);
        List<BigDecimal> quantityList = new ArrayList<BigDecimal>();
        quantityList.add(quantity3);
        quantityList.add(quantity1);
        createPurchaseInvoice(purchaseOrderList, priceList, quantityList, day4);

        // 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, price3));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsShipment.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price3));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt2.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price2, price5, price4));
        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), price3, price1, price3, quantity3));
        productCostingAssertList.add(
                new ProductCostingAssert(transactionList.get(2), price4, price1.negate(), price4, quantity5));
        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(2), "NSC", amount1, day4, true, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList1);
        List<CostAdjustmentAssert> costAdjustmentAssertLineList2 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList2.add(new CostAdjustmentAssert(transactionList.get(0), "PDC",
                quantity3.multiply(price3).add(quantity3.multiply(price1).negate()), day4, true));
        costAdjustmentAssertLineList2.add(new CostAdjustmentAssert(transactionList.get(2), "PDC",
                quantity4.multiply(price3).add(quantity4.multiply(price1).negate()), day4, true));
        costAdjustmentAssertLineList2.add(new CostAdjustmentAssert(transactionList.get(1), "PDC",
                quantity1.multiply(price4).add(quantity1.multiply(price2).negate()), day4, false));
        costAdjustmentAssertLineList2
                .add(new CostAdjustmentAssert(transactionList.get(2), "NSC", amount2, day4, false, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList2);
        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, amount1, null));
        documentPostAssertList1.add(new DocumentPostAssert("35000", amount1, amount0, null));
        CostAdjustment costAdjustment1 = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList.get(0).getId());
        assertDocumentPost(costAdjustment1, product.getId(), documentPostAssertList1);

        // Post cost adjustment and assert it
        postDocument(costAdjustmentList.get(1));
        List<DocumentPostAssert> documentPostAssertList2 = new ArrayList<DocumentPostAssert>();
        documentPostAssertList2.add(new DocumentPostAssert("99904", amount0,
                quantity3.multiply(price3).add(quantity3.multiply(price1).negate()), null));
        documentPostAssertList2.add(new DocumentPostAssert("35000",
                quantity3.multiply(price3).add(quantity3.multiply(price1).negate()), amount0, null));
        documentPostAssertList2.add(new DocumentPostAssert("99904", amount0,
                quantity4.multiply(price3).add(quantity4.multiply(price1).negate()), null));
        documentPostAssertList2.add(new DocumentPostAssert("35000",
                quantity4.multiply(price3).add(quantity4.multiply(price1).negate()), amount0, null));
        documentPostAssertList2.add(new DocumentPostAssert("99900",
                quantity1.multiply(price4).add(quantity1.multiply(price2).negate()), amount0, null));
        documentPostAssertList2.add(new DocumentPostAssert("35000", amount0,
                quantity1.multiply(price4).add(quantity1.multiply(price2).negate()), null));
        documentPostAssertList2.add(new DocumentPostAssert("61000", amount2.negate(), amount0, null));
        documentPostAssertList2.add(new DocumentPostAssert("35000", amount0, amount2.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();
    }
}

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

@Test
public void testCostingBD3() 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 int day7 = 35;
    final BigDecimal price1 = new BigDecimal("120.00");
    final BigDecimal price2 = new BigDecimal("135.00");
    final BigDecimal price3 = new BigDecimal("127.50");
    final BigDecimal price4 = new BigDecimal("128.0357");
    final BigDecimal price5 = new BigDecimal("132.50");
    final BigDecimal price6 = new BigDecimal("135.00");
    final BigDecimal price7 = new BigDecimal("133.00");
    final BigDecimal quantity1 = new BigDecimal("75");
    final BigDecimal quantity2 = new BigDecimal("10");
    final BigDecimal quantity3 = new BigDecimal("50");
    final BigDecimal quantity4 = new BigDecimal("25");
    final BigDecimal quantity5 = new BigDecimal("15");

    try {//from w w w  . ja  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 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(purchaseOrder2, price2, quantity1, day6);

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

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

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment2 = createGoodsShipment(product, price4, quantity3, day4);

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment3 = createGoodsShipment(product, price5, quantity4, day5);

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment4 = createGoodsShipment(product, price6, quantity5, day7);

        // Run price correction background
        runPriceBackground();

        // Assert product transactions
        List<ProductTransactionAssert> productTransactionAssertList = new ArrayList<ProductTransactionAssert>();
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt2.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price1));
        productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance()
                .get(ShipmentInOut.class, goodsShipment1.getId()).getMaterialMgmtShipmentInOutLineList().get(0),
                price3, price1));
        productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance()
                .get(ShipmentInOut.class, goodsShipment2.getId()).getMaterialMgmtShipmentInOutLineList().get(0),
                price4, price1));
        productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance()
                .get(ShipmentInOut.class, goodsShipment3.getId()).getMaterialMgmtShipmentInOutLineList().get(0),
                price5, price1));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt1.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price2, price7, price2));
        productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance()
                .get(ShipmentInOut.class, goodsShipment4.getId()).getMaterialMgmtShipmentInOutLineList().get(0),
                price6, price6));
        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, price3, price1, quantity1));
        productCostingAssertList.add(new ProductCostingAssert(transactionList.get(4), price2, price2, price2,
                quantity1.subtract(quantity2)));
        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), "BDT", amount0, day2, true));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList1);
        List<CostAdjustmentAssert> costAdjustmentAssertLineList2 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList2.add(new CostAdjustmentAssert(transactionList.get(1), "BDT",
                quantity2.multiply(price1).add(quantity2.multiply(price3).negate()), day3, true));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList2);
        List<CostAdjustmentAssert> costAdjustmentAssertLineList3 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList3.add(new CostAdjustmentAssert(transactionList.get(2), "BDT",
                quantity3.multiply(price1).add(quantity3.multiply(price4).negate()), day4, true));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList3);
        List<CostAdjustmentAssert> costAdjustmentAssertLineList4 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList4.add(new CostAdjustmentAssert(transactionList.get(3), "BDT",
                quantity4.multiply(price1).add(quantity4.multiply(price5).negate()), day5, true));
        costAdjustmentAssertLineList4.add(new CostAdjustmentAssert(transactionList.get(4), "NSC",
                quantity1.multiply(price7).add(quantity1.multiply(price2).negate()), day6, false, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList4);
        assertCostAdjustment(costAdjustmentList, costAdjustmentAssertList);

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

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

        // Post cost adjustment 3 and assert it
        postDocument(costAdjustmentList.get(3));
        List<DocumentPostAssert> documentPostAssertList3 = new ArrayList<DocumentPostAssert>();
        documentPostAssertList3.add(new DocumentPostAssert("99900", amount0,
                quantity4.multiply(price5).add(quantity4.multiply(price1).negate()), null));
        documentPostAssertList3.add(new DocumentPostAssert("35000",
                quantity4.multiply(price5).add(quantity4.multiply(price1).negate()), amount0, null));
        documentPostAssertList3.add(new DocumentPostAssert("61000",
                quantity1.multiply(price2).add(quantity1.multiply(price7).negate()), amount0, null));
        documentPostAssertList3.add(new DocumentPostAssert("35000", amount0,
                quantity1.multiply(price2).add(quantity1.multiply(price7).negate()), null));
        CostAdjustment costAdjustment3 = OBDal.getInstance().get(CostAdjustment.class,
                costAdjustmentList.get(3).getId());
        assertDocumentPost(costAdjustment3, product.getId(), documentPostAssertList3);

        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 testCostingV10() 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 int day7 = 35;
    final int day8 = 40;
    final int day9 = 45;
    final BigDecimal price1 = new BigDecimal("8.00");
    final BigDecimal price2 = new BigDecimal("9.00");
    final BigDecimal price3 = new BigDecimal("8.5493");
    final BigDecimal price4 = new BigDecimal("10.00");
    final BigDecimal price5 = new BigDecimal("8.6610");
    final BigDecimal price6 = new BigDecimal("9.3390");
    final BigDecimal price7 = new BigDecimal("9.4507");
    final BigDecimal price8 = new BigDecimal("9.0186");
    final BigDecimal quantity1 = new BigDecimal("1000");
    final BigDecimal quantity2 = new BigDecimal("150");
    final BigDecimal quantity3 = new BigDecimal("250");
    final BigDecimal quantity4 = new BigDecimal("10");
    final BigDecimal quantity5 = new BigDecimal("200");
    final BigDecimal quantity6 = new BigDecimal("120");
    final BigDecimal quantity7 = new BigDecimal("50");
    final BigDecimal quantity8 = new BigDecimal("280");

    try {//from www  .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);

        // 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 goodsReceipt1 = createGoodsReceipt(purchaseOrder, price1, quantity2, day1);

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

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

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

        // Run price correction background
        runPriceBackground();

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt3 = createGoodsReceipt(purchaseOrder, price1, quantity5, day5);

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt4 = createGoodsReceipt(purchaseOrder, price1, quantity6, day6);

        // Create goods shipment, run costing background, post it and assert it
        ShipmentInOut goodsShipment2 = createGoodsShipment(product, price3, quantity7, day7);

        // Create purchase invoice, post it and assert it
        List<ShipmentInOut> goodsReceiptList2 = new ArrayList<ShipmentInOut>();
        goodsReceiptList2.add(goodsReceipt3);
        goodsReceiptList2.add(goodsReceipt4);
        List<BigDecimal> priceList2 = new ArrayList<BigDecimal>();
        priceList2.add(price4);
        priceList2.add(price4);
        createPurchaseInvoice(goodsReceiptList2, priceList2, quantity5.add(quantity6), day8);

        // Run price correction background
        runPriceBackground();

        // Create goods receipt, run costing background, post it and assert it
        ShipmentInOut goodsReceipt5 = createGoodsReceipt(purchaseOrder, price1, quantity8, day9);

        // 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));
        productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance()
                .get(ShipmentInOut.class, goodsShipment1.getId()).getMaterialMgmtShipmentInOutLineList().get(0),
                price1, price2));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt3.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price4));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt4.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price4));
        productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance()
                .get(ShipmentInOut.class, goodsShipment2.getId()).getMaterialMgmtShipmentInOutLineList().get(0),
                price3, price7));
        productTransactionAssertList.add(
                new ProductTransactionAssert(OBDal.getInstance().get(ShipmentInOut.class, goodsReceipt5.getId())
                        .getMaterialMgmtShipmentInOutLineList().get(0), price1, price1));
        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, price1, price2,
                quantity2.add(quantity3)));
        productCostingAssertList.add(new ProductCostingAssert(transactionList.get(3), price4, price5, price6,
                quantity2.add(quantity3).add((quantity4).negate()).add(quantity5)));
        productCostingAssertList.add(new ProductCostingAssert(transactionList.get(4), price4, price3, price7,
                quantity2.add(quantity3).add((quantity4).negate()).add(quantity5).add(quantity6)));
        productCostingAssertList.add(new ProductCostingAssert(transactionList.get(6), price1, null, price8,
                quantity2.add(quantity3).add((quantity4).negate()).add(quantity5).add(quantity6)
                        .add((quantity7).negate()).add(quantity8)));
        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()), day4, true));
        costAdjustmentAssertLineList1.add(new CostAdjustmentAssert(transactionList.get(1), "PDC",
                quantity3.multiply(price2).add(quantity3.multiply(price1).negate()), day4, true));
        costAdjustmentAssertLineList1.add(new CostAdjustmentAssert(transactionList.get(2), "PDC",
                quantity4.multiply(price2).add(quantity4.multiply(price1).negate()), day4, false));
        costAdjustmentAssertList.add(costAdjustmentAssertLineList1);
        List<CostAdjustmentAssert> costAdjustmentAssertLineList2 = new ArrayList<CostAdjustmentAssert>();
        costAdjustmentAssertLineList2.add(new CostAdjustmentAssert(transactionList.get(3), "PDC",
                quantity5.multiply(price4).add(quantity5.multiply(price1).negate()), day8, true));
        costAdjustmentAssertLineList2.add(new CostAdjustmentAssert(transactionList.get(4), "PDC",
                quantity6.multiply(price4).add(quantity6.multiply(price1).negate()), day8, true));
        costAdjustmentAssertLineList2.add(new CostAdjustmentAssert(transactionList.get(5), "PDC",
                quantity7.multiply(price7).add(quantity7.multiply(price3).negate()), day8, false));
        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", amount0,
                quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), null));
        documentPostAssertList1.add(new DocumentPostAssert("35000",
                quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), amount0, null));
        documentPostAssertList1.add(new DocumentPostAssert("99904", amount0,
                quantity3.multiply(price2).add(quantity3.multiply(price1).negate()), null));
        documentPostAssertList1.add(new DocumentPostAssert("35000",
                quantity3.multiply(price2).add(quantity3.multiply(price1).negate()), amount0, null));
        documentPostAssertList1.add(new DocumentPostAssert("99900",
                quantity4.multiply(price2).add(quantity4.multiply(price1).negate()), amount0, null));
        documentPostAssertList1.add(new DocumentPostAssert("35000", amount0,
                quantity4.multiply(price2).add(quantity4.multiply(price1).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", amount0,
                quantity5.multiply(price4).add(quantity5.multiply(price1).negate()), null));
        documentPostAssertList2.add(new DocumentPostAssert("35000",
                quantity5.multiply(price4).add(quantity5.multiply(price1).negate()), amount0, null));
        documentPostAssertList2.add(new DocumentPostAssert("99904", amount0,
                quantity6.multiply(price4).add(quantity6.multiply(price1).negate()), null));
        documentPostAssertList2.add(new DocumentPostAssert("35000",
                quantity6.multiply(price4).add(quantity6.multiply(price1).negate()), amount0, null));
        documentPostAssertList2.add(new DocumentPostAssert("99900",
                quantity7.multiply(price7).add(quantity7.multiply(price3).negate()), amount0, null));
        documentPostAssertList2.add(new DocumentPostAssert("35000", amount0,
                quantity7.multiply(price7).add(quantity7.multiply(price3).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();
    }
}

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

@TransactionTimeout(5000)
public TreeMap<String, Object[]> aktualisiereLoseAusStueckliste(Integer stuecklisteIId,
        TheClientDto theClientDto) {//from   w w w  . j  a  va2s. co m

    TreeMap<String, Object[]> tmAktualisierteLose = new TreeMap<String, Object[]>();

    ParametermandantDto parameter = null;
    try {
        parameter = getParameterFac().getMandantparameter(theClientDto.getSMandantenwaehrung(),
                ParameterFac.KATEGORIE_STUECKLISTE,
                ParameterFac.PARAMETER_BEI_LOS_AKTUALISIERUNG_MATERIAL_NACHBUCHEN);
    } catch (RemoteException e1) {
        throwEJBExceptionLPRespectOld(e1);
    }
    boolean bMaterialNachbuchen = (Boolean) parameter.getCWertAsObject();

    Session session = FLRSessionFactory.getFactory().openSession();

    String queryString = "SELECT l FROM FLRLosReport l WHERE l.status_c_nr IN ('" + LocaleFac.STATUS_ANGELEGT
            + "','" + LocaleFac.STATUS_AUSGEGEBEN + "','" + LocaleFac.STATUS_IN_PRODUKTION
            + "') AND l.mandant_c_nr='" + theClientDto.getMandant() + "' AND l.stueckliste_i_id IS NOT NULL ";

    if (stuecklisteIId != null) {
        queryString += " AND l.stueckliste_i_id=" + stuecklisteIId;
    }

    org.hibernate.Query query = session.createQuery(queryString);
    List<?> results = query.list();
    Iterator<?> resultListIterator = results.iterator();
    while (resultListIterator.hasNext()) {
        FLRLosReport los = (FLRLosReport) resultListIterator.next();

        if (los.getStatus_c_nr().equals(LocaleFac.STATUS_ANGELEGT)) {
            aktualisiereSollArbeitsplanAusStueckliste(los.getI_id(), theClientDto);
            aktualisiereSollMaterialAusStueckliste(los.getI_id(), theClientDto);
        } else {

            LoslagerentnahmeDto[] laeger = loslagerentnahmeFindByLosIId(los.getI_id());

            aktualisiereSollArbeitsplanAusStueckliste(los.getI_id(), theClientDto);

            // Alle stuecklistenpositionen holen + Hilfsstueckliste und dann
            // verdichten
            HashMap<Integer, StuecklistepositionDto> hmStuecklistenposition = getFertigungFac()
                    .holeAlleLossollmaterialFuerStuecklistenAktualisierung(los.getStueckliste_i_id(),
                            los.getN_losgroesse(), 0, null, theClientDto);

            LossollmaterialDto[] sollmatDtos = lossollmaterialFindByLosIId(los.getI_id());

            for (int i = 0; i < sollmatDtos.length; i++) {
                try {
                    if (!Helper.short2boolean(sollmatDtos[i].getBNachtraeglich())) {
                        // War vor SP2000 --> &&
                        // sollmatDtos[i].getNMenge().doubleValue() > 0

                        LosistmaterialDto[] istmaterialDtos = losistmaterialFindByLossollmaterialIId(
                                sollmatDtos[i].getIId());
                        BigDecimal bdAusgegeben = getAusgegebeneMenge(sollmatDtos[i].getIId(), null,
                                theClientDto);
                        ArtikelDto artikelDto = getArtikelFac()
                                .artikelFindByPrimaryKeySmall(sollmatDtos[i].getArtikelIId(), theClientDto);

                        Object[] oZeileReport = new Object[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ANZAHL_SPALTEN];
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_LOSNUMMER] = los
                                .getC_nr();
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ARTIKELNUMMER] = artikelDto
                                .getCNr();
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_EINHEIT] = artikelDto
                                .getEinheitCNr();
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = sollmatDtos[i]
                                .getNMenge();
                        if (artikelDto.getArtikelsprDto() != null) {
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEZEICHNUNG] = artikelDto
                                    .getArtikelsprDto().getCBez();
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ZUSATZBEZEICHNUNG] = artikelDto
                                    .getArtikelsprDto().getCZbez();
                        }

                        if (hmStuecklistenposition.containsKey(sollmatDtos[i].getArtikelIId())) {

                            // Mengen abziehen
                            StuecklistepositionDto stklPos = hmStuecklistenposition
                                    .get(sollmatDtos[i].getArtikelIId());

                            sollmatDtos[i].setNMenge(stklPos.getNMenge());

                            Lossollmaterial sollmatBean = em.find(Lossollmaterial.class,
                                    sollmatDtos[i].getIId());

                            BigDecimal diffSollmenge = stklPos.getNMenge().subtract(sollmatBean.getNMenge());

                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = diffSollmenge;

                            sollmatBean.setNMenge(stklPos.getNMenge());
                            sollmatBean.setIBeginnterminoffset(stklPos.getIBeginnterminoffset());

                            BigDecimal diff = bdAusgegeben.subtract(sollmatDtos[i].getNMenge());

                            if (diff.doubleValue() == 0 && diffSollmenge.doubleValue() != 0) {
                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                        0);
                                tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                        los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                            } else {

                                if (bMaterialNachbuchen == true || (bMaterialNachbuchen == false && Helper
                                        .short2boolean(artikelDto.getBLagerbewirtschaftet()) == false)) {

                                    if (Helper.short2boolean(artikelDto.getBSeriennrtragend())
                                            || Helper.short2boolean(artikelDto.getBChargennrtragend())) {
                                        sollmatDtos[i] = null;

                                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                                0);
                                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEMERKUNG] = "Der Artikel ist SNR/CNR behaftet und wurde nicht ber\u00FCcksichtigt.";

                                        tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                                los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                                        continue;
                                    }

                                    if (diff.doubleValue() > 0) {

                                        for (int j = 0; j < istmaterialDtos.length; j++) {
                                            if (diff.doubleValue() > 0) {
                                                BigDecimal istmenge = istmaterialDtos[j].getNMenge();

                                                BigDecimal bdMengeNeu = null;

                                                if (diff.doubleValue() > istmenge.doubleValue()) {
                                                    bdMengeNeu = new BigDecimal(0);
                                                    diff = diff.subtract(istmenge);
                                                } else {
                                                    bdMengeNeu = istmenge.subtract(diff);
                                                    diff = new BigDecimal(0);
                                                }

                                                updateLosistmaterialMenge(istmaterialDtos[j].getIId(),
                                                        bdMengeNeu, theClientDto);
                                            }
                                        }

                                        BigDecimal bdAusgegebenNachher = getAusgegebeneMenge(
                                                sollmatDtos[i].getIId(), null, theClientDto);

                                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdAusgegebenNachher
                                                .subtract(bdAusgegeben);

                                        tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                                los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                                    } else {
                                        BigDecimal bdAbzubuchendeMenge = diff.abs();
                                        for (int j = 0; j < laeger.length; j++) {
                                            // wenn noch was abzubuchen ist
                                            // (Menge >
                                            // 0)
                                            if (bdAbzubuchendeMenge.compareTo(new BigDecimal(0)) == 1) {
                                                BigDecimal bdLagerstand = null;
                                                if (Helper
                                                        .short2boolean(artikelDto.getBLagerbewirtschaftet())) {

                                                    bdLagerstand = getLagerFac().getLagerstand(
                                                            artikelDto.getIId(), laeger[j].getLagerIId(),
                                                            theClientDto);

                                                } else {
                                                    bdLagerstand = new BigDecimal(999999999);
                                                }
                                                // wenn ein lagerstand da
                                                // ist
                                                if (bdLagerstand.compareTo(new BigDecimal(0)) == 1) {
                                                    BigDecimal bdMengeVonLager;
                                                    if (bdLagerstand.compareTo(bdAbzubuchendeMenge) == 1) {
                                                        // wenn mehr als
                                                        // ausreichend
                                                        // auf
                                                        // lager
                                                        bdMengeVonLager = bdAbzubuchendeMenge;
                                                    } else {
                                                        // dann nur den
                                                        // lagerstand
                                                        // entnehmen
                                                        bdMengeVonLager = bdLagerstand;
                                                    }
                                                    LosistmaterialDto istmat = new LosistmaterialDto();
                                                    istmat.setLagerIId(laeger[j].getLagerIId());
                                                    istmat.setLossollmaterialIId(sollmatDtos[i].getIId());
                                                    istmat.setNMenge(bdMengeVonLager);

                                                    if (sollmatDtos[i].getNMenge().doubleValue() > 0) {
                                                        istmat.setBAbgang(Helper.boolean2Short(true));
                                                    } else {
                                                        istmat.setBAbgang(Helper.boolean2Short(false));
                                                    }

                                                    // ist-wert anlegen und
                                                    // lagerbuchung
                                                    // durchfuehren
                                                    createLosistmaterial(istmat, null, theClientDto);
                                                    // menge reduzieren
                                                    bdAbzubuchendeMenge = bdAbzubuchendeMenge
                                                            .subtract(bdMengeVonLager);
                                                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdMengeVonLager;

                                                    tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                                            los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                                                } else {
                                                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                                            0);
                                                    tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                                            los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            hmStuecklistenposition.remove(sollmatDtos[i].getArtikelIId());
                            getFehlmengeFac().aktualisiereFehlmenge(LocaleFac.BELEGART_LOS,
                                    sollmatDtos[i].getIId(), false, theClientDto);
                        } else {

                            // Los-Sollmaterial loeschen

                            verknuepfungZuBestellpositionUndArbeitsplanLoeschen(sollmatDtos[i].getIId());

                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = sollmatDtos[i]
                                    .getNMenge().multiply(new BigDecimal(-1));

                            if (bMaterialNachbuchen == true || istmaterialDtos.length == 0
                                    || (bMaterialNachbuchen == false && Helper
                                            .short2boolean(artikelDto.getBLagerbewirtschaftet()) == false)) {
                                for (int j = 0; j < istmaterialDtos.length; j++) {
                                    removeLosistmaterial(istmaterialDtos[j], theClientDto);
                                }

                                removeLossollmaterial(sollmatDtos[i], theClientDto);

                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdAusgegeben
                                        .multiply(new BigDecimal(-1));
                                tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                        los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                            } else {
                                sollmatDtos[i].setNMenge(new BigDecimal(0));
                                updateLossollmaterial(sollmatDtos[i], theClientDto);
                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                        0);
                                tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                        los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                            }

                            getFehlmengeFac().aktualisiereFehlmenge(LocaleFac.BELEGART_LOS,
                                    sollmatDtos[i].getIId(), false, theClientDto);
                            sollmatDtos[i] = null;

                        }

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

            }

            // Nun alle uebrigen Sollmaterial=0 loeschen
            for (int i = 0; i < sollmatDtos.length; i++) {
                if (sollmatDtos[i] != null && !Helper.short2boolean(sollmatDtos[i].getBNachtraeglich())
                        && sollmatDtos[i].getNMenge().doubleValue() == 0) {

                    // Los-Sollmaterial loeschen
                    LosistmaterialDto[] istmaterialDtos = losistmaterialFindByLossollmaterialIId(
                            sollmatDtos[i].getIId());

                    boolean bAlleIstMaterialSindNull = true;

                    for (int z = 0; z < istmaterialDtos.length; z++) {
                        if (istmaterialDtos[z].getNMenge().doubleValue() != 0) {
                            bAlleIstMaterialSindNull = false;
                        }
                    }
                    ArtikelDto artikelDto = getArtikelFac()
                            .artikelFindByPrimaryKeySmall(sollmatDtos[i].getArtikelIId(), theClientDto);
                    if (bMaterialNachbuchen == true || bAlleIstMaterialSindNull == true
                            || (bMaterialNachbuchen == false
                                    && Helper.short2boolean(artikelDto.getBLagerbewirtschaftet()) == false)) {

                        BigDecimal bdAusgegeben = getAusgegebeneMenge(sollmatDtos[i].getIId(), null,
                                theClientDto);

                        for (int j = 0; j < istmaterialDtos.length; j++) {
                            removeLosistmaterial(istmaterialDtos[j], theClientDto);
                        }

                        verknuepfungZuBestellpositionUndArbeitsplanLoeschen(sollmatDtos[i].getIId());
                        removeLossollmaterial(sollmatDtos[i], theClientDto);

                        if (bdAusgegeben.doubleValue() != 0) {

                            Object[] oZeileReport = new Object[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ANZAHL_SPALTEN];
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_LOSNUMMER] = los
                                    .getC_nr();
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ARTIKELNUMMER] = artikelDto
                                    .getCNr();
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_EINHEIT] = artikelDto
                                    .getEinheitCNr();
                            if (artikelDto.getArtikelsprDto() != null) {
                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEZEICHNUNG] = artikelDto
                                        .getArtikelsprDto().getCBez();
                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ZUSATZBEZEICHNUNG] = artikelDto
                                        .getArtikelsprDto().getCZbez();
                            }
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdAusgegeben
                                    .multiply(new BigDecimal(-1));

                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = new BigDecimal(
                                    0);

                            tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                    los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                        }
                    }

                }
            }

            // Alle noch nicht verbrauchten neu eintragen

            Iterator it = hmStuecklistenposition.keySet().iterator();
            while (it.hasNext()) {

                try {
                    LagerDto lagerDtoMandant = getLagerFac().getHauptlagerDesMandanten(theClientDto);
                    Integer artikelIId = (Integer) it.next();

                    StuecklistepositionDto stklPos = hmStuecklistenposition.get(artikelIId);
                    BigDecimal bdAbzubuchendeMenge = stklPos.getNMenge();

                    ArtikelDto artikelDto = getArtikelFac()
                            .artikelFindByPrimaryKeySmall(stklPos.getArtikelIId(), theClientDto);

                    Object[] oZeileReport = new Object[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ANZAHL_SPALTEN];
                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_LOSNUMMER] = los
                            .getC_nr();
                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ARTIKELNUMMER] = artikelDto
                            .getCNr();
                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_EINHEIT] = artikelDto
                            .getEinheitCNr();
                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = stklPos
                            .getNMenge();

                    if (artikelDto.getArtikelsprDto() != null) {
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEZEICHNUNG] = artikelDto
                                .getArtikelsprDto().getCBez();
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ZUSATZBEZEICHNUNG] = artikelDto
                                .getArtikelsprDto().getCZbez();
                    }

                    LossollmaterialDto losMatDto = new LossollmaterialDto();
                    losMatDto.setArtikelIId(stklPos.getArtikelIId());
                    losMatDto.setBNachtraeglich(Helper.boolean2Short(false));
                    losMatDto.setCKommentar(stklPos.getCKommentar());
                    losMatDto.setCPosition(stklPos.getCPosition());
                    losMatDto.setFDimension1(stklPos.getFDimension1());
                    losMatDto.setFDimension2(stklPos.getFDimension2());
                    losMatDto.setFDimension3(stklPos.getFDimension3());
                    losMatDto.setILfdnummer(stklPos.getILfdnummer());
                    losMatDto.setEinheitCNr(stklPos.getEinheitCNr());
                    losMatDto.setLosIId(los.getI_id());
                    losMatDto.setMontageartIId(stklPos.getMontageartIId());
                    losMatDto.setNMenge(stklPos.getNMenge());
                    losMatDto.setISort(new Integer(0));

                    BigDecimal bdSollpreis = getLagerFac().getGemittelterGestehungspreisEinesLagers(
                            stklPos.getArtikelIId(), lagerDtoMandant.getIId(), theClientDto);
                    losMatDto.setNSollpreis(bdSollpreis);

                    Integer sollmatIId = createLossollmaterial(losMatDto, theClientDto).getIId();
                    if (bMaterialNachbuchen == true || (bMaterialNachbuchen == false
                            && Helper.short2boolean(artikelDto.getBLagerbewirtschaftet()) == false)) {

                        if (Helper.short2boolean(artikelDto.getBSeriennrtragend())
                                || Helper.short2boolean(artikelDto.getBChargennrtragend())) {
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEMERKUNG] = "Der Artikel ist SNR/CNR behaftet und wurde nicht ber\u00FCcksichtigt.";

                            tmAktualisierteLose.put(los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                            continue;
                        }

                        for (int j = 0; j < laeger.length; j++) {
                            // wenn noch was abzubuchen ist
                            // (Menge >
                            // 0)
                            if (bdAbzubuchendeMenge.compareTo(new BigDecimal(0)) == 1) {
                                BigDecimal bdLagerstand = null;
                                if (Helper.short2boolean(artikelDto.getBLagerbewirtschaftet())) {

                                    bdLagerstand = getLagerFac().getLagerstand(artikelDto.getIId(),
                                            laeger[j].getLagerIId(), theClientDto);

                                } else {
                                    bdLagerstand = new BigDecimal(999999999);
                                }
                                // wenn ein lagerstand da ist
                                if (bdLagerstand.compareTo(new BigDecimal(0)) == 1) {
                                    BigDecimal bdMengeVonLager;
                                    if (bdLagerstand.compareTo(bdAbzubuchendeMenge) == 1) {
                                        // wenn mehr als
                                        // ausreichend
                                        // auf
                                        // lager
                                        bdMengeVonLager = bdAbzubuchendeMenge;
                                    } else {
                                        // dann nur den
                                        // lagerstand
                                        // entnehmen
                                        bdMengeVonLager = bdLagerstand;
                                    }
                                    LosistmaterialDto istmat = new LosistmaterialDto();
                                    istmat.setLagerIId(laeger[j].getLagerIId());
                                    istmat.setLossollmaterialIId(sollmatIId);
                                    istmat.setNMenge(bdMengeVonLager);

                                    if (stklPos.getNMenge().doubleValue() > 0) {
                                        istmat.setBAbgang(Helper.boolean2Short(true));
                                    } else {
                                        istmat.setBAbgang(Helper.boolean2Short(false));
                                    }

                                    // ist-wert anlegen und
                                    // lagerbuchung
                                    // durchfuehren
                                    createLosistmaterial(istmat, null, theClientDto);
                                    // menge reduzieren
                                    bdAbzubuchendeMenge = bdAbzubuchendeMenge.subtract(bdMengeVonLager);

                                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdMengeVonLager;
                                    tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                            los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                                }
                            }
                        }
                    } else {
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                0);
                        tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                    }

                    getFehlmengeFac().aktualisiereFehlmenge(LocaleFac.BELEGART_LOS, losMatDto.getIId(), false,
                            theClientDto);

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

            // Los aktualisieren
            Los losUpd = em.find(Los.class, los.getI_id());
            losUpd.setTAktualisierungstueckliste(getTimestamp());

        }

    }

    return tmAktualisierteLose;
}