From 4ed7def01aaffa548f64c914148fc068ef34910b Mon Sep 17 00:00:00 2001 From: teo_sarca Date: Mon, 8 Dec 2008 10:56:34 +0000 Subject: [PATCH] FR [ 1895317 ] InvoiceCreateInOut: you can create many receipts --- .../InvoiceFullyMatchedException.java | 30 ++++ base/src/org/compiere/model/MInvoiceLine.java | 13 ++ .../compiere/process/InvoiceCreateInOut.java | 131 ++++++++++++------ migration/352a-trunk/344_BF1895317.sql | 15 ++ .../352a-trunk/postgresql/344_BF1895317.sql | 15 ++ 5 files changed, 159 insertions(+), 45 deletions(-) create mode 100644 base/src/org/adempiere/exceptions/InvoiceFullyMatchedException.java create mode 100644 migration/352a-trunk/344_BF1895317.sql create mode 100644 migration/352a-trunk/postgresql/344_BF1895317.sql diff --git a/base/src/org/adempiere/exceptions/InvoiceFullyMatchedException.java b/base/src/org/adempiere/exceptions/InvoiceFullyMatchedException.java new file mode 100644 index 0000000000..902abe9d58 --- /dev/null +++ b/base/src/org/adempiere/exceptions/InvoiceFullyMatchedException.java @@ -0,0 +1,30 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 2008 SC ARHIPAC SERVICE SRL. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. 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., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + *****************************************************************************/ +package org.adempiere.exceptions; + +/** + * Throwed when an invoice is fully matched so no more receipts can be generated. + * @author Teo Sarca, www.arhipac.ro + */ +public class InvoiceFullyMatchedException extends AdempiereException +{ + private static final long serialVersionUID = 1L; + + public static final String AD_Message = "InvoiceFullyMatched"; + + public InvoiceFullyMatchedException() + { + super("@"+AD_Message+"@"); + } +} diff --git a/base/src/org/compiere/model/MInvoiceLine.java b/base/src/org/compiere/model/MInvoiceLine.java index 24c919a82e..31af0ff19d 100644 --- a/base/src/org/compiere/model/MInvoiceLine.java +++ b/base/src/org/compiere/model/MInvoiceLine.java @@ -1196,4 +1196,17 @@ public class MInvoiceLine extends X_C_InvoiceLine setC_Activity_ID(rmaLine.getC_Activity_ID()); setC_Campaign_ID(rmaLine.getC_Campaign_ID()); } + + /** + * @return matched qty + */ + public BigDecimal getMatchedQty() + { + String sql = "SELECT COALESCE(SUM("+MMatchInv.COLUMNNAME_Qty+"),0)" + +" FROM "+MMatchInv.Table_Name + +" WHERE "+MMatchInv.COLUMNNAME_C_InvoiceLine_ID+"=?" + +" AND "+MMatchInv.COLUMNNAME_Processed+"=?"; + return DB.getSQLValueBDEx(get_TrxName(), sql, getC_InvoiceLine_ID(), true); + } + } // MInvoiceLine diff --git a/base/src/org/compiere/process/InvoiceCreateInOut.java b/base/src/org/compiere/process/InvoiceCreateInOut.java index 74467a3953..9da6c41ed0 100644 --- a/base/src/org/compiere/process/InvoiceCreateInOut.java +++ b/base/src/org/compiere/process/InvoiceCreateInOut.java @@ -16,37 +16,50 @@ *****************************************************************************/ package org.compiere.process; -import java.util.logging.*; +import java.math.BigDecimal; +import java.util.logging.Level; -import org.compiere.util.*; -import org.compiere.model.*; +import org.adempiere.exceptions.AdempiereException; +import org.adempiere.exceptions.FillMandatoryException; +import org.adempiere.exceptions.InvoiceFullyMatchedException; +import org.compiere.model.MInOut; +import org.compiere.model.MInOutLine; +import org.compiere.model.MInvoice; +import org.compiere.model.MInvoiceLine; +import org.compiere.util.Env; /** - * Create (Generate) Shipment from Invoice + * Create (Generate) Shipment from Invoice * - * @author Jorg Janke - * @version $Id: InvoiceCreateInOut.java,v 1.2 2006/07/30 00:51:02 jjanke Exp $ + * @author Jorg Janke + * @version $Id: InvoiceCreateInOut.java,v 1.2 2006/07/30 00:51:02 jjanke Exp $ + * + * @author Teo Sarca, www.arhipac.ro + *
  • FR [ 1895317 ] InvoiceCreateInOut: you can create many receipts */ public class InvoiceCreateInOut extends SvrProcess { + public static final String PARAM_M_Warehouse_ID = MInOut.COLUMNNAME_M_Warehouse_ID; + /** Warehouse */ private int p_M_Warehouse_ID = 0; /** Invoice */ private int p_C_Invoice_ID = 0; + /** Receipt */ + private MInOut m_inout = null; /** * Prepare - e.g., get Parameters. */ protected void prepare() { - ProcessInfoParameter[] para = getParameter(); - for (int i = 0; i < para.length; i++) + for (ProcessInfoParameter para : getParameter()) { - String name = para[i].getParameterName(); - if (para[i].getParameter() == null) + String name = para.getParameterName(); + if (para.getParameter() == null) ; - else if (name.equals("M_Warehouse_ID")) - p_M_Warehouse_ID = para[i].getParameterAsInt(); + else if (name.equals(PARAM_M_Warehouse_ID)) + p_M_Warehouse_ID = para.getParameterAsInt(); else log.log(Level.SEVERE, "Unknown Parameter: " + name); } @@ -61,46 +74,74 @@ public class InvoiceCreateInOut extends SvrProcess */ protected String doIt () throws Exception { - log.info("C_Invoice_ID=" + p_C_Invoice_ID - + ", M_Warehouse_ID=" + p_M_Warehouse_ID); - if (p_C_Invoice_ID == 0) - throw new IllegalArgumentException("@NotFound@ @C_Invoice_ID@"); + log.info("C_Invoice_ID=" + p_C_Invoice_ID + ", M_Warehouse_ID=" + p_M_Warehouse_ID); + if (p_C_Invoice_ID <= 0) + throw new FillMandatoryException("C_Invoice_ID"); if (p_M_Warehouse_ID == 0) - throw new IllegalArgumentException("@NotFound@ @M_Warehouse_ID@"); + throw new FillMandatoryException(PARAM_M_Warehouse_ID); // MInvoice invoice = new MInvoice (getCtx(), p_C_Invoice_ID, null); - if (invoice.get_ID() == 0) - throw new IllegalArgumentException("@NotFound@ @C_Invoice_ID@"); + if (invoice.get_ID() <= 0) + throw new AdempiereException("@NotFound@ @C_Invoice_ID@"); if (!MInvoice.DOCSTATUS_Completed.equals(invoice.getDocStatus())) - throw new IllegalArgumentException("@InvoiceCreateDocNotCompleted@"); + throw new AdempiereException("@InvoiceCreateDocNotCompleted@"); // - MInOut ship = new MInOut (invoice, 0, null, p_M_Warehouse_ID); - if (!ship.save()) - throw new IllegalArgumentException("@SaveError@ Receipt"); - // - MInvoiceLine[] invoiceLines = invoice.getLines(false); - for (int i = 0; i < invoiceLines.length; i++) + for (MInvoiceLine invoiceLine : invoice.getLines(false)) { - MInvoiceLine invoiceLine = invoiceLines[i]; - MInOutLine sLine = new MInOutLine(ship); - sLine.setInvoiceLine(invoiceLine, 0, // Locator - invoice.isSOTrx() ? invoiceLine.getQtyInvoiced() : Env.ZERO); - sLine.setQtyEntered(invoiceLine.getQtyEntered()); - sLine.setMovementQty(invoiceLine.getQtyInvoiced()); - if (invoice.isCreditMemo()) - { - sLine.setQtyEntered(sLine.getQtyEntered().negate()); - sLine.setMovementQty(sLine.getMovementQty().negate()); - } - if (!sLine.save()) - throw new IllegalArgumentException("@SaveError@ @M_InOutLine_ID@"); - // - invoiceLine.setM_InOutLine_ID(sLine.getM_InOutLine_ID()); - if (!invoiceLine.save()) - throw new IllegalArgumentException("@SaveError@ @C_InvoiceLine_ID@"); + createLine(invoice, invoiceLine); } - - return ship.getDocumentNo(); + if (m_inout == null) + throw new InvoiceFullyMatchedException(); + // + return m_inout.getDocumentNo(); } // doIt + + /** + * Create Shipment/Receipt header + * @param invoice + * @return Shipment/Receipt header + */ + private MInOut getCreateHeader(MInvoice invoice) + { + if (m_inout != null) + return m_inout; + m_inout = new MInOut (invoice, 0, null, p_M_Warehouse_ID); + m_inout.saveEx(); + return m_inout; + } + /** + * Create shipment/receipt line + * @param invoice + * @param invoiceLine + * @return shipment/receipt line + */ + private MInOutLine createLine(MInvoice invoice, MInvoiceLine invoiceLine) + { + BigDecimal qtyMatched = invoiceLine.getMatchedQty(); + BigDecimal qtyInvoiced = invoiceLine.getQtyInvoiced(); + BigDecimal qtyNotMatched = qtyInvoiced.subtract(qtyMatched); + // If is fully matched don't create anything + if (qtyNotMatched.signum() == 0) + { + return null; + } + MInOut inout = getCreateHeader(invoice); + MInOutLine sLine = new MInOutLine(inout); + sLine.setInvoiceLine(invoiceLine, 0, // Locator + invoice.isSOTrx() ? qtyNotMatched : Env.ZERO); + sLine.setQtyEntered(qtyNotMatched); + sLine.setMovementQty(qtyNotMatched); + if (invoice.isCreditMemo()) + { + sLine.setQtyEntered(sLine.getQtyEntered().negate()); + sLine.setMovementQty(sLine.getMovementQty().negate()); + } + sLine.saveEx(); + // + invoiceLine.setM_InOutLine_ID(sLine.getM_InOutLine_ID()); + invoiceLine.saveEx(); + // + return sLine; + } } // InvoiceCreateInOut diff --git a/migration/352a-trunk/344_BF1895317.sql b/migration/352a-trunk/344_BF1895317.sql new file mode 100644 index 0000000000..16b7d6fddc --- /dev/null +++ b/migration/352a-trunk/344_BF1895317.sql @@ -0,0 +1,15 @@ +-- 08.12.2008 11:38:06 EET +-- BF [ 1895317 ] InvoiceCreateInOut: you can create many receipts +INSERT INTO AD_Message (AD_Client_ID,AD_Message_ID,AD_Org_ID,Created,CreatedBy,EntityType,IsActive,MsgText,MsgType,Updated,UpdatedBy,Value) VALUES (0,53047,0,TO_DATE('2008-12-08 11:37:49','YYYY-MM-DD HH24:MI:SS'),0,'D','Y','Invoice is fully matched.','E',TO_DATE('2008-12-08 11:37:49','YYYY-MM-DD HH24:MI:SS'),0,'InvoiceFullyMatched') +; + +-- 08.12.2008 11:38:07 EET +-- BF [ 1895317 ] InvoiceCreateInOut: you can create many receipts +INSERT INTO AD_Message_Trl (AD_Language,AD_Message_ID, MsgText,MsgTip, IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy) SELECT l.AD_Language,t.AD_Message_ID, t.MsgText,t.MsgTip, 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy FROM AD_Language l, AD_Message t WHERE l.IsActive='Y' AND l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N' AND t.AD_Message_ID=53047 AND EXISTS (SELECT * FROM AD_Message_Trl tt WHERE tt.AD_Language!=l.AD_Language OR tt.AD_Message_ID!=t.AD_Message_ID) +; + +-- 08.12.2008 11:39:11 EET +-- BF [ 1895317 ] InvoiceCreateInOut: you can create many receipts +UPDATE AD_Message_Trl SET IsTranslated='Y',MsgText='Factura este integral receptionata.',Updated=TO_DATE('2008-12-08 11:39:11','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=0 WHERE AD_Message_ID=53047 AND AD_Language='ro_RO' +; + diff --git a/migration/352a-trunk/postgresql/344_BF1895317.sql b/migration/352a-trunk/postgresql/344_BF1895317.sql new file mode 100644 index 0000000000..495a04621f --- /dev/null +++ b/migration/352a-trunk/postgresql/344_BF1895317.sql @@ -0,0 +1,15 @@ +-- 08.12.2008 11:38:06 EET +-- BF [ 1895317 ] InvoiceCreateInOut: you can create many receipts +INSERT INTO AD_Message (AD_Client_ID,AD_Message_ID,AD_Org_ID,Created,CreatedBy,EntityType,IsActive,MsgText,MsgType,Updated,UpdatedBy,Value) VALUES (0,53047,0,TO_TIMESTAMP('2008-12-08 11:37:49','YYYY-MM-DD HH24:MI:SS'),0,'D','Y','Invoice is fully matched.','E',TO_TIMESTAMP('2008-12-08 11:37:49','YYYY-MM-DD HH24:MI:SS'),0,'InvoiceFullyMatched') +; + +-- 08.12.2008 11:38:07 EET +-- BF [ 1895317 ] InvoiceCreateInOut: you can create many receipts +INSERT INTO AD_Message_Trl (AD_Language,AD_Message_ID, MsgText,MsgTip, IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy) SELECT l.AD_Language,t.AD_Message_ID, t.MsgText,t.MsgTip, 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy FROM AD_Language l, AD_Message t WHERE l.IsActive='Y' AND l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N' AND t.AD_Message_ID=53047 AND EXISTS (SELECT * FROM AD_Message_Trl tt WHERE tt.AD_Language!=l.AD_Language OR tt.AD_Message_ID!=t.AD_Message_ID) +; + +-- 08.12.2008 11:39:11 EET +-- BF [ 1895317 ] InvoiceCreateInOut: you can create many receipts +UPDATE AD_Message_Trl SET IsTranslated='Y',MsgText='Factura este integral receptionata.',Updated=TO_TIMESTAMP('2008-12-08 11:39:11','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=0 WHERE AD_Message_ID=53047 AND AD_Language='ro_RO' +; +