From 13d26966b63ea2b31fe09bea80be9f00eb6a528e Mon Sep 17 00:00:00 2001 From: hengsin Date: Fri, 30 Sep 2022 18:25:20 +0800 Subject: [PATCH] IDEMPIERE-5440 Add unit test for Matching form (#1510) --- .../org/adempiere/webui/apps/form/WMatch.java | 41 ++--- .../adempiere/webui/apps/form/WPayPrint.java | 2 - .../src/org/compiere/apps/form/Match.java | 23 ++- .../org/idempiere/test/base/MatchPOTest.java | 79 +++------ .../idempiere/test/form/MatchFormTest.java | 166 ++++++++++++++++++ 5 files changed, 227 insertions(+), 84 deletions(-) create mode 100644 org.idempiere.test/src/org/idempiere/test/form/MatchFormTest.java diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/form/WMatch.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/form/WMatch.java index a5150b6211..9a7665f51f 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/form/WMatch.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/form/WMatch.java @@ -81,11 +81,6 @@ import org.zkoss.zul.Vlayout; public class WMatch extends Match implements IFormController, EventListener, WTableModelListener { - /** - * - */ - @SuppressWarnings("unused") - private static final long serialVersionUID = -6383121932802974801L; private CustomForm form = new CustomForm(); /** @@ -94,8 +89,9 @@ public class WMatch extends Match public WMatch() { m_WindowNo = form.getWindowNo(); - log.info("WinNo=" + m_WindowNo - + " - AD_Client_ID=" + m_AD_Client_ID + ", AD_Org_ID=" + m_AD_Org_ID + ", By=" + m_by); + if (log.isLoggable(Level.INFO)) + log.info("WinNo=" + m_WindowNo + + " - AD_Client_ID=" + m_AD_Client_ID + ", AD_Org_ID=" + m_AD_Org_ID + ", By=" + m_by); Env.setContext(Env.getCtx(), m_WindowNo, "IsSOTrx", "N"); try @@ -122,7 +118,7 @@ public class WMatch extends Match { ClientInfo.onClientInfo(form, this::onClientInfo); } - } // init + } /** Window No */ private int m_WindowNo = 0; @@ -146,11 +142,6 @@ public class WMatch extends Match private static final int MODE_NOTMATCHED = 0; private static final int MODE_MATCHED = 1; - /** Indexes in Table */ - private static final int I_QTY = 6; - private static final int I_MATCHED = 7; - - private BigDecimal m_xMatched = Env.ZERO; private BigDecimal m_xMatchedTo = Env.ZERO; @@ -293,7 +284,7 @@ public class WMatch extends Match ZKUpdateUtil.setVflex(xMatchedTable, true); centerPanel.setStyle("min-height: 300px;"); - } // jbInit + } protected void layoutParameterAndSummary() { setupParameterColumns(); @@ -391,16 +382,7 @@ public class WMatch extends Match */ private void dynInit() { - ColumnInfo[] layout = new ColumnInfo[] { - new ColumnInfo(" ", ".", IDColumn.class, false, false, ""), - new ColumnInfo(Msg.translate(Env.getCtx(), "DocumentNo"), ".", String.class), // 1 - new ColumnInfo(Msg.translate(Env.getCtx(), "Date"), ".", Timestamp.class), - new ColumnInfo(Msg.translate(Env.getCtx(), "C_BPartner_ID"),".", KeyNamePair.class, "."), // 3 - new ColumnInfo(Msg.translate(Env.getCtx(), "Line"), ".", KeyNamePair.class, "."), - new ColumnInfo(Msg.translate(Env.getCtx(), "M_Product_ID"), ".", KeyNamePair.class, "."), // 5 - new ColumnInfo(Msg.translate(Env.getCtx(), "Qty"), ".", Double.class), - new ColumnInfo(Msg.translate(Env.getCtx(), "Matched"), ".", Double.class) - }; + ColumnInfo[] layout = getColumnLayout(); xMatchedTable.prepareTable(layout, "", "", false, ""); xMatchedToTable.prepareTable(layout, "", "", true, ""); @@ -417,7 +399,6 @@ public class WMatch extends Match sameProduct.addActionListener(this); sameQty.addActionListener(this); - // Init Yvonne String selection = (String)matchFrom.getSelectedItem().getValue(); SimpleListModel model = new SimpleListModel(cmd_matchFrom((String)matchFrom.getSelectedItem().getLabel())); matchTo.setItemRenderer(model); @@ -477,6 +458,7 @@ public class WMatch extends Match * Action Listener * @param e event */ + @Override public void onEvent (Event e) { Integer product = onlyProduct.getValue()!=null?(Integer)onlyProduct.getValue():null; @@ -485,7 +467,6 @@ public class WMatch extends Match Timestamp to = dateTo.getValue()!=null?(Timestamp)dateTo.getValue():null; if (e.getTarget() == matchFrom) { - //cmd_matchFrom((String)matchFrom.getSelectedItem().getLabel()); String selection = (String)matchFrom.getSelectedItem().getValue(); SimpleListModel model = new SimpleListModel(cmd_matchFrom((String)matchFrom.getSelectedItem().getLabel())); matchTo.setItemRenderer(model); @@ -503,7 +484,6 @@ public class WMatch extends Match cmd_matchTo(); else if (e.getTarget() == bSearch) { - //cmd_search(); xMatchedTable = (WListbox)cmd_search(xMatchedTable, matchFrom.getSelectedIndex(), (String)matchTo.getSelectedItem().getLabel(), product, vendor, from, to, matchMode.getSelectedIndex() == MODE_MATCHED); xMatched.setValue(Env.ZERO); @@ -516,7 +496,6 @@ public class WMatch extends Match } else if (e.getTarget() == bProcess) { - //cmd_process(); cmd_process(xMatchedTable, xMatchedToTable, matchMode.getSelectedIndex(), matchFrom.getSelectedIndex(), matchTo.getSelectedItem().getLabel(), m_xMatched); xMatchedTable = (WListbox) cmd_search(xMatchedTable, matchFrom.getSelectedIndex(), (String)matchTo.getSelectedItem().getLabel(), product, vendor, from, to, matchMode.getSelectedIndex() == MODE_MATCHED); xMatched.setValue(Env.ZERO); @@ -533,7 +512,7 @@ public class WMatch extends Match cmd_searchTo(); else if (AEnv.contains(xMatchedTable, e.getTarget())) cmd_searchTo(); - } // actionPerformed + } /** @@ -541,7 +520,6 @@ public class WMatch extends Match */ private void cmd_matchTo() { - // log.fine( "VMatch.cmd_matchTo"); int index = matchTo.getSelectedIndex(); String selection = (String)matchTo.getModel().getElementAt(index); xMatchedToBorder.setValue(selection); @@ -596,6 +574,7 @@ public class WMatch extends Match * Table Model Listener - calculate matchd Qty * @param e event */ + @Override public void tableChanged (WTableModelEvent e) { if (e.getColumn() != 0) @@ -638,7 +617,7 @@ public class WMatch extends Match statusBar.setStatusDB(noRows + ""); } // tableChanged - + @Override public ADForm getForm() { return form; } diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/form/WPayPrint.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/form/WPayPrint.java index c7ca4da62f..fcd0f33267 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/form/WPayPrint.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/form/WPayPrint.java @@ -349,7 +349,6 @@ public class WPayPrint extends PayPrint implements IFormController, EventListene return; String PaymentRule = pp.getValue(); - log.info("PaymentRule=" + PaymentRule); fNoPayments.setText(" "); String msg = loadPaymentRuleInfo(m_C_PaySelection_ID, PaymentRule); @@ -414,7 +413,6 @@ public class WPayPrint extends PayPrint implements IFormController, EventListene if (fPaymentRule.getSelectedItem() == null) return; String PaymentRule = fPaymentRule.getSelectedItem().toValueNamePair().getValue(); - log.info(PaymentRule); if (!getChecks(PaymentRule)) return; diff --git a/org.adempiere.ui/src/org/compiere/apps/form/Match.java b/org.adempiere.ui/src/org/compiere/apps/form/Match.java index 0f1082998f..1c7948128d 100644 --- a/org.adempiere.ui/src/org/compiere/apps/form/Match.java +++ b/org.adempiere.ui/src/org/compiere/apps/form/Match.java @@ -26,6 +26,7 @@ import org.adempiere.exceptions.AdempiereException; import org.adempiere.util.IReservationTracer; import org.adempiere.util.IReservationTracerFactory; import org.compiere.acct.Doc; +import org.compiere.minigrid.ColumnInfo; import org.compiere.minigrid.IDColumn; import org.compiere.minigrid.IMiniTable; import org.compiere.model.MClient; @@ -66,6 +67,9 @@ public class Match //private static final int MODE_MATCHED = 1; /** Indexes in Table */ + public static final int I_ID = 0; + public static final int I_DocumentNo = 1; + public static final int I_DocumentDate = 2; public static final int I_BPartner = 3; public static final int I_Line = 4; public static final int I_Product = 5; @@ -146,7 +150,6 @@ public class Match */ public void cmd_process(IMiniTable xMatchedTable, IMiniTable xMatchedToTable, int matchMode, int matchFrom, String matchTo, BigDecimal m_xMatched) { - log.config(""); // Matched From int matchedRow = xMatchedTable.getSelectedRow(); if (matchedRow < 0) @@ -608,4 +611,22 @@ public class Match return m_matchOptions[matchType]; return null; } + + /** + * + * @return {@link ColumnInfo} array + */ + public ColumnInfo[] getColumnLayout() { + ColumnInfo[] layout = new ColumnInfo[] { + new ColumnInfo(" ", ".", IDColumn.class, false, false, ""), + new ColumnInfo(Msg.translate(Env.getCtx(), "DocumentNo"), ".", String.class), // 1 + new ColumnInfo(Msg.translate(Env.getCtx(), "Date"), ".", Timestamp.class), + new ColumnInfo(Msg.translate(Env.getCtx(), "C_BPartner_ID"),".", KeyNamePair.class, "."), // 3 + new ColumnInfo(Msg.translate(Env.getCtx(), "Line"), ".", KeyNamePair.class, "."), + new ColumnInfo(Msg.translate(Env.getCtx(), "M_Product_ID"), ".", KeyNamePair.class, "."), // 5 + new ColumnInfo(Msg.translate(Env.getCtx(), "Qty"), ".", Double.class), + new ColumnInfo(Msg.translate(Env.getCtx(), "Matched"), ".", Double.class) + }; + return layout; + } } diff --git a/org.idempiere.test/src/org/idempiere/test/base/MatchPOTest.java b/org.idempiere.test/src/org/idempiere/test/base/MatchPOTest.java index c06060cf47..7a27b3bf0d 100644 --- a/org.idempiere.test/src/org/idempiere/test/base/MatchPOTest.java +++ b/org.idempiere.test/src/org/idempiere/test/base/MatchPOTest.java @@ -29,7 +29,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.math.BigDecimal; -import java.sql.Timestamp; import org.compiere.apps.form.Match; import org.compiere.minigrid.ColumnInfo; @@ -53,8 +52,6 @@ import org.compiere.process.DocAction; import org.compiere.process.ProcessInfo; import org.compiere.process.ServerProcessCtl; import org.compiere.util.Env; -import org.compiere.util.KeyNamePair; -import org.compiere.util.Msg; import org.compiere.wf.MWorkflow; import org.idempiere.test.AbstractTestCase; import org.idempiere.test.DictionaryIDs; @@ -572,31 +569,22 @@ public class MatchPOTest extends AbstractTestCase { newOnHand = MStorageOnHand.getQtyOnHand(product.get_ID(), getM_Warehouse_ID(), 0, getTrxName()).intValue(); assertEquals(initialOnHand+1, newOnHand, "Unexpected qty on hand value"); - MiniTableImpl fromTable = new MiniTableImpl(); - MiniTableImpl toTable = new MiniTableImpl(); - ColumnInfo[] layout = new ColumnInfo[] { - new ColumnInfo(" ", ".", IDColumn.class, false, false, ""), - new ColumnInfo(Msg.translate(Env.getCtx(), "DocumentNo"), ".", String.class), // 1 - new ColumnInfo(Msg.translate(Env.getCtx(), "Date"), ".", Timestamp.class), - new ColumnInfo(Msg.translate(Env.getCtx(), "C_BPartner_ID"),".", KeyNamePair.class, "."), // 3 - new ColumnInfo(Msg.translate(Env.getCtx(), "Line"), ".", KeyNamePair.class, "."), - new ColumnInfo(Msg.translate(Env.getCtx(), "M_Product_ID"), ".", KeyNamePair.class, "."), // 5 - new ColumnInfo(Msg.translate(Env.getCtx(), "Qty"), ".", Double.class), - new ColumnInfo(Msg.translate(Env.getCtx(), "Matched"), ".", Double.class) - }; - fromTable.prepareTable(layout, null, null, false, null); - toTable.prepareTable(layout, null, null, false, null); Match match = new Match(); match.setTrxName(getTrxName()); + MiniTableImpl fromTable = new MiniTableImpl(); + MiniTableImpl toTable = new MiniTableImpl(); + ColumnInfo[] layout = match.getColumnLayout(); + fromTable.prepareTable(layout, null, null, false, null); + toTable.prepareTable(layout, null, null, false, null); match.cmd_search(fromTable, Match.MATCH_SHIPMENT, match.getMatchTypeText(Match.MATCH_ORDER), product.get_ID(), bpartner.get_ID(), null, null, false); assertTrue(fromTable.getRowCount()>0, "Unexpected number of records for not matched Material Receipt: " + fromTable.getRowCount()); int selectedRow = -1; for(int i = 0; i < fromTable.getRowCount(); i++) { - String docNo = (String)fromTable.getValueAt(i, 1); + String docNo = (String)fromTable.getValueAt(i, Match.I_DocumentNo); if (receipt.getDocumentNo().equals(docNo)) { - int matched = ((Number)fromTable.getValueAt(i, 7)).intValue(); + int matched = ((Number)fromTable.getValueAt(i, Match.I_MATCHED)).intValue(); assertEquals(0, matched, "Unexpected matched qty for Material Receipt line"); - int qty = ((Number)fromTable.getValueAt(i, 6)).intValue(); + int qty = ((Number)fromTable.getValueAt(i, Match.I_QTY)).intValue(); assertEquals(receiptLine.getMovementQty().intValue(), qty, "Unexpected qty for Material Receipt line"); selectedRow = i; break; @@ -608,11 +596,11 @@ public class MatchPOTest extends AbstractTestCase { assertTrue(toTable.getRowCount()>0, "Unexpected number of records for not matched Order Line: " + fromTable.getRowCount()); int selectedOrderRow = -1; for(int i = 0; i < toTable.getRowCount(); i++) { - String docNo = (String)toTable.getValueAt(i, 1); + String docNo = (String)toTable.getValueAt(i, Match.I_DocumentNo); if (order.getDocumentNo().equals(docNo)) { - int matched = ((Number)toTable.getValueAt(i, 7)).intValue(); + int matched = ((Number)toTable.getValueAt(i, Match.I_MATCHED)).intValue(); assertEquals(0, matched, "Unexpected matched qty for PO line"); - int qty = ((Number)toTable.getValueAt(i, 6)).intValue(); + int qty = ((Number)toTable.getValueAt(i, Match.I_QTY)).intValue(); assertEquals(orderLine.getQtyOrdered().intValue(), qty, "Unexpected qty for PO line"); selectedOrderRow = i; break; @@ -648,11 +636,11 @@ public class MatchPOTest extends AbstractTestCase { assertTrue(fromTable.getRowCount()>0, "Unexpected number of records for not matched Material Receipt: " + fromTable.getRowCount()); selectedRow = -1; for(int i = 0; i < fromTable.getRowCount(); i++) { - String docNo = (String)fromTable.getValueAt(i, 1); + String docNo = (String)fromTable.getValueAt(i, Match.I_DocumentNo); if (receipt.getDocumentNo().equals(docNo)) { - int matched = ((Number)fromTable.getValueAt(i, 7)).intValue(); + int matched = ((Number)fromTable.getValueAt(i, Match.I_MATCHED)).intValue(); assertEquals(0, matched, "Unexpected matched qty for Material Receipt line"); - int qty = ((Number)fromTable.getValueAt(i, 6)).intValue(); + int qty = ((Number)fromTable.getValueAt(i, Match.I_QTY)).intValue(); assertEquals(receiptLine.getMovementQty().intValue(), qty, "Unexpected qty for Material Receipt line"); selectedRow = i; break; @@ -664,11 +652,11 @@ public class MatchPOTest extends AbstractTestCase { assertTrue(toTable.getRowCount()>0, "Unexpected number of records for not matched Order Line: " + fromTable.getRowCount()); selectedOrderRow = -1; for(int i = 0; i < toTable.getRowCount(); i++) { - String docNo = (String)toTable.getValueAt(i, 1); + String docNo = (String)toTable.getValueAt(i, Match.I_DocumentNo); if (order.getDocumentNo().equals(docNo)) { - int matched = ((Number)toTable.getValueAt(i, 7)).intValue(); + int matched = ((Number)toTable.getValueAt(i, Match.I_MATCHED)).intValue(); assertEquals(0, matched, "Unexpected matched qty for PO line"); - int qty = ((Number)toTable.getValueAt(i, 6)).intValue(); + int qty = ((Number)toTable.getValueAt(i, Match.I_QTY)).intValue(); assertEquals(orderLine.getQtyOrdered().intValue(), qty, "Unexpected qty for PO line"); selectedOrderRow = i; break; @@ -676,7 +664,7 @@ public class MatchPOTest extends AbstractTestCase { } assertTrue(selectedOrderRow >= 0, "Can't find not matched PO line"); - IDColumn idColumn = (IDColumn)toTable.getValueAt(selectedOrderRow, 0); + IDColumn idColumn = (IDColumn)toTable.getValueAt(selectedOrderRow, Match.I_ID); idColumn.setSelected(true); match.cmd_process(fromTable, toTable, Match.MODE_NOTMATCHED, Match.MATCH_SHIPMENT, match.getMatchTypeText(Match.MATCH_ORDER), new BigDecimal(1)); @@ -769,22 +757,13 @@ public class MatchPOTest extends AbstractTestCase { newOnHand = MStorageOnHand.getQtyOnHand(product.get_ID(), getM_Warehouse_ID(), 0, getTrxName()).intValue(); assertEquals(initialOnHand+1, newOnHand, "Unexpected qty on hand value"); + Match match = new Match(); + match.setTrxName(getTrxName()); MiniTableImpl fromTable = new MiniTableImpl(); MiniTableImpl toTable = new MiniTableImpl(); - ColumnInfo[] layout = new ColumnInfo[] { - new ColumnInfo(" ", ".", IDColumn.class, false, false, ""), - new ColumnInfo(Msg.translate(Env.getCtx(), "DocumentNo"), ".", String.class), // 1 - new ColumnInfo(Msg.translate(Env.getCtx(), "Date"), ".", Timestamp.class), - new ColumnInfo(Msg.translate(Env.getCtx(), "C_BPartner_ID"),".", KeyNamePair.class, "."), // 3 - new ColumnInfo(Msg.translate(Env.getCtx(), "Line"), ".", KeyNamePair.class, "."), - new ColumnInfo(Msg.translate(Env.getCtx(), "M_Product_ID"), ".", KeyNamePair.class, "."), // 5 - new ColumnInfo(Msg.translate(Env.getCtx(), "Qty"), ".", Double.class), - new ColumnInfo(Msg.translate(Env.getCtx(), "Matched"), ".", Double.class) - }; + ColumnInfo[] layout = match.getColumnLayout(); fromTable.prepareTable(layout, null, null, false, null); - toTable.prepareTable(layout, null, null, false, null); - Match match = new Match(); - match.setTrxName(getTrxName()); + toTable.prepareTable(layout, null, null, false, null); match.cmd_search(fromTable, Match.MATCH_SHIPMENT, match.getMatchTypeText(Match.MATCH_ORDER), product.get_ID(), bpartner.get_ID(), null, null, false); assertTrue(fromTable.getRowCount()>0, "Unexpected number of records for not matched Material Receipt: " + fromTable.getRowCount()); int selectedRow = -1; @@ -843,11 +822,11 @@ public class MatchPOTest extends AbstractTestCase { assertTrue(fromTable.getRowCount()>0, "Unexpected number of records for not matched Material Receipt: " + fromTable.getRowCount()); selectedRow = -1; for(int i = 0; i < fromTable.getRowCount(); i++) { - String docNo = (String)fromTable.getValueAt(i, 1); + String docNo = (String)fromTable.getValueAt(i, Match.I_DocumentNo); if (receipt.getDocumentNo().equals(docNo)) { - int matched = ((Number)fromTable.getValueAt(i, 7)).intValue(); + int matched = ((Number)fromTable.getValueAt(i, Match.I_MATCHED)).intValue(); assertEquals(0, matched, "Unexpected matched qty for Material Receipt line"); - int qty = ((Number)fromTable.getValueAt(i, 6)).intValue(); + int qty = ((Number)fromTable.getValueAt(i, Match.I_QTY)).intValue(); assertEquals(receiptLine.getMovementQty().intValue(), qty, "Unexpected qty for Material Receipt line"); selectedRow = i; break; @@ -859,11 +838,11 @@ public class MatchPOTest extends AbstractTestCase { assertTrue(toTable.getRowCount()>0, "Unexpected number of records for not matched Order Line: " + fromTable.getRowCount()); selectedOrderRow = -1; for(int i = 0; i < toTable.getRowCount(); i++) { - String docNo = (String)toTable.getValueAt(i, 1); + String docNo = (String)toTable.getValueAt(i, Match.I_DocumentNo); if (order.getDocumentNo().equals(docNo)) { - int matched = ((Number)toTable.getValueAt(i, 7)).intValue(); + int matched = ((Number)toTable.getValueAt(i, Match.I_MATCHED)).intValue(); assertEquals(0, matched, "Unexpected matched qty for PO line"); - int qty = ((Number)toTable.getValueAt(i, 6)).intValue(); + int qty = ((Number)toTable.getValueAt(i, Match.I_QTY)).intValue(); assertEquals(orderLine.getQtyOrdered().intValue(), qty, "Unexpected qty for PO line"); selectedOrderRow = i; break; @@ -871,7 +850,7 @@ public class MatchPOTest extends AbstractTestCase { } assertTrue(selectedOrderRow >= 0, "Can't find not matched PO line"); - IDColumn idColumn = (IDColumn)toTable.getValueAt(selectedOrderRow, 0); + IDColumn idColumn = (IDColumn)toTable.getValueAt(selectedOrderRow, Match.I_ID); idColumn.setSelected(true); match.cmd_process(fromTable, toTable, Match.MODE_NOTMATCHED, Match.MATCH_SHIPMENT, match.getMatchTypeText(Match.MATCH_ORDER), new BigDecimal(1)); diff --git a/org.idempiere.test/src/org/idempiere/test/form/MatchFormTest.java b/org.idempiere.test/src/org/idempiere/test/form/MatchFormTest.java new file mode 100644 index 0000000000..f4f45999af --- /dev/null +++ b/org.idempiere.test/src/org/idempiere/test/form/MatchFormTest.java @@ -0,0 +1,166 @@ +/*********************************************************************** + * This file is part of iDempiere ERP Open Source * + * http://www.idempiere.org * + * * + * Copyright (C) Contributors * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 2 * + * of the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * + * MA 02110-1301, USA. * + * * + * Contributors: * + * - hengsin * + **********************************************************************/ +package org.idempiere.test.form; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.math.BigDecimal; + +import org.compiere.apps.form.Match; +import org.compiere.minigrid.ColumnInfo; +import org.compiere.minigrid.IDColumn; +import org.compiere.model.MBPartner; +import org.compiere.model.MInOut; +import org.compiere.model.MInOutLine; +import org.compiere.model.MMatchPO; +import org.compiere.model.MOrder; +import org.compiere.model.MOrderLine; +import org.compiere.model.MProduct; +import org.compiere.model.MWarehouse; +import org.compiere.process.DocAction; +import org.compiere.process.ProcessInfo; +import org.compiere.util.Env; +import org.compiere.wf.MWorkflow; +import org.idempiere.test.AbstractTestCase; +import org.idempiere.test.DictionaryIDs; +import org.idempiere.test.ui.MiniTableImpl; +import org.junit.jupiter.api.Test; + +public class MatchFormTest extends AbstractTestCase { + + public MatchFormTest() { + } + + @Test + public void testMatchReceiptToPO() { + MBPartner bpartner = MBPartner.get(Env.getCtx(), DictionaryIDs.C_BPartner.TREE_FARM.id); + MProduct product = MProduct.get(Env.getCtx(), DictionaryIDs.M_Product.AZALEA_BUSH.id); + + MOrder order = new MOrder(Env.getCtx(), 0, getTrxName()); + order.setBPartner(bpartner); + order.setIsSOTrx(false); + order.setC_DocTypeTarget_ID(); + order.setDocStatus(DocAction.STATUS_Drafted); + order.setDocAction(DocAction.ACTION_Complete); + order.saveEx(); + + BigDecimal orderQty = new BigDecimal("1"); + MOrderLine orderLine = new MOrderLine(order); + orderLine.setLine(10); + orderLine.setProduct(product); + orderLine.setQty(orderQty); + orderLine.saveEx(); + + ProcessInfo info = MWorkflow.runDocumentActionWorkflow(order, DocAction.ACTION_Complete); + assertFalse(info.isError(), info.getSummary()); + order.load(getTrxName()); + assertEquals(DocAction.STATUS_Completed, order.getDocStatus()); + orderLine.load(getTrxName()); + assertEquals(1, orderLine.getQtyReserved().intValue(), "Unexpected order line qty ordered value"); + assertEquals(0, orderLine.getQtyDelivered().intValue(), "Unexpected order line qty delivered value"); + + MInOut receipt = new MInOut(Env.getCtx(), 0, getTrxName()); + receipt.setBPartner(bpartner); + receipt.setIsSOTrx(false); + receipt.setC_DocType_ID(DictionaryIDs.C_DocType.MM_RECEIPT.id); + receipt.setM_Warehouse_ID(getM_Warehouse_ID()); + receipt.saveEx(); + MWarehouse wh = MWarehouse.get(Env.getCtx(), receipt.getM_Warehouse_ID()); + int M_Locator_ID = wh.getDefaultLocator().getM_Locator_ID(); + BigDecimal receiptQty = new BigDecimal("1"); + MInOutLine receiptLine = new MInOutLine(receipt); + receiptLine.setProduct(product); + receiptLine.setM_Locator_ID(M_Locator_ID); + receiptLine.setLine(10); + receiptLine.setQty(receiptQty); + receiptLine.saveEx(); + + info = MWorkflow.runDocumentActionWorkflow(receipt, DocAction.ACTION_Complete); + assertFalse(info.isError(), info.getSummary()); + receipt.load(getTrxName()); + assertEquals(DocAction.STATUS_Completed, receipt.getDocStatus()); + orderLine.load(getTrxName()); + assertEquals(1, orderLine.getQtyReserved().intValue(), "Unexpected order line qty ordered value"); + assertEquals(0, orderLine.getQtyDelivered().intValue(), "Unexpected order line qty delivered value"); + + Match match = new Match(); + match.setTrxName(getTrxName()); + ColumnInfo[] columnLayout = match.getColumnLayout(); + MiniTableImpl fromTable = new MiniTableImpl(); + MiniTableImpl toTable = new MiniTableImpl(); + fromTable.prepareTable(columnLayout, null, null, false, null); + toTable.prepareTable(columnLayout, null, null, false, null); + + //load not matched shipments + match.cmd_search(fromTable, Match.MATCH_SHIPMENT, match.getMatchTypeText(Match.MATCH_ORDER), product.get_ID(), bpartner.get_ID(), null, null, false); + assertTrue(fromTable.getRowCount()>0, "Unexpected number of records for not matched Material Receipt: " + fromTable.getRowCount()); + int selectedRow = -1; + for(int i = 0; i < fromTable.getRowCount(); i++) { + String docNo = (String)fromTable.getValueAt(i, Match.I_DocumentNo); + if (receipt.getDocumentNo().equals(docNo)) { + int matched = ((Number)fromTable.getValueAt(i, Match.I_MATCHED)).intValue(); + assertEquals(0, matched, "Unexpected matched qty for Material Receipt line"); + int qty = ((Number)fromTable.getValueAt(i, Match.I_QTY)).intValue(); + assertEquals(receiptLine.getMovementQty().intValue(), qty, "Unexpected qty for Material Receipt line"); + selectedRow = i; + break; + } + } + assertTrue(selectedRow >= 0, "Can't find not matched Material Receipt line"); + fromTable.setSelectedRow(selectedRow); + + //load not matched orders + match.cmd_searchTo(fromTable, toTable, match.getMatchTypeText(Match.MATCH_ORDER), Match.MATCH_SHIPMENT, true, true, true, false); + assertTrue(toTable.getRowCount()>0, "Unexpected number of records for not matched Order Line: " + fromTable.getRowCount()); + int selectedOrderRow = -1; + for(int i = 0; i < toTable.getRowCount(); i++) { + String docNo = (String)toTable.getValueAt(i, Match.I_DocumentNo); + if (order.getDocumentNo().equals(docNo)) { + int matched = ((Number)toTable.getValueAt(i, Match.I_MATCHED)).intValue(); + assertEquals(0, matched, "Unexpected matched qty for PO line"); + int qty = ((Number)toTable.getValueAt(i, Match.I_QTY)).intValue(); + assertEquals(orderLine.getQtyOrdered().intValue(), qty, "Unexpected qty for PO line"); + selectedOrderRow = i; + break; + } + } + assertTrue(selectedOrderRow >= 0, "Can't find not matched PO line"); + + //select and process matching + IDColumn idColumn = (IDColumn)toTable.getValueAt(selectedOrderRow, Match.I_ID); + idColumn.setSelected(true); + match.cmd_process(fromTable, toTable, Match.MODE_NOTMATCHED, Match.MATCH_SHIPMENT, match.getMatchTypeText(Match.MATCH_ORDER), new BigDecimal(1)); + + orderLine.load(getTrxName()); + assertEquals(0, orderLine.getQtyReserved().intValue(), "Unexpected order line qty ordered value"); + assertEquals(1, orderLine.getQtyDelivered().intValue(), "Unexpected order line qty delivered value"); + receiptLine.load(getTrxName()); + assertEquals(orderLine.getC_OrderLine_ID(), receiptLine.getC_OrderLine_ID(), "Unexpected order line ID value for receipt line"); + MMatchPO[] matchPOs = MMatchPO.getOrderLine(Env.getCtx(), orderLine.get_ID(), getTrxName()); + assertEquals(1, matchPOs.length, "Unexpected number of MatchPO for order line"); + } +}