Skip to content

Commit

Permalink
Add "CreateFrom in
Browse files Browse the repository at this point in the history
C_Invoice" like Dynamic Form
adempiere#114
* Set defaut info for CreateFromPanel
  • Loading branch information
yamelsenih committed Dec 3, 2015
1 parent c0eda4a commit 3e60440
Show file tree
Hide file tree
Showing 14 changed files with 527 additions and 166 deletions.
100 changes: 100 additions & 0 deletions client/src/org/compiere/apps/form/CreateFromHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* 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. *
* For the text or an alternative of this public license, you may reach us *
* Copyright (C) 2003-2015 E.R.P. Consultores y Asociados, C.A. *
* All Rights Reserved. *
* Contributor(s): Yamel Senih www.erpcya.com *
*****************************************************************************/
package org.compiere.apps.form;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair;

/**
* @author Yamel Senih, ysenih@erpcya.com, ERPCyA http://www.erpcya.com
* <li> FR [ 114 ] Change "Create From" UI for Form like Dialog in window without "hardcode"
* @see https://github.com/adempiere/adempiere/issues/114
*/
public class CreateFromHelper {
/** Logger */
protected CLogger log = CLogger.getCLogger(getClass());

/**
* Load PBartner dependent Order/Invoice/Shipment Field.
* @param C_BPartner_ID BPartner
* @param forInvoice for invoice
* @param p_M_Warehouse_ID
* Yamel Senih FR [ 114 ] moved from org.compiere.grid.CreateFrom
*/
protected ArrayList<KeyNamePair> loadOrderData (int C_BPartner_ID, boolean forInvoice, int p_M_Warehouse_ID) {
ArrayList<KeyNamePair> list = new ArrayList<KeyNamePair>();

// Display
StringBuffer display = new StringBuffer("o.DocumentNo||' - ' ||")
.append(DB.TO_CHAR("o.DateOrdered", DisplayType.Date, Env.getAD_Language(Env.getCtx())))
.append("||' - '||")
.append(DB.TO_CHAR("o.GrandTotal", DisplayType.Amount, Env.getAD_Language(Env.getCtx())));
//
String column = "ol.QtyDelivered";
if (forInvoice)
column = "ol.QtyInvoiced";
StringBuffer sql = new StringBuffer("SELECT o.C_Order_ID,").append(display)
.append(" FROM C_Order o "
+ "WHERE o.C_BPartner_ID=? AND o.IsSOTrx='N' AND o.DocStatus IN ('CL','CO')"
+ " AND o.C_Order_ID IN "
+ "(SELECT ol.C_Order_ID FROM C_OrderLine ol"
+ " WHERE ol.QtyOrdered - ").append(column).append(" != 0) ");
if(p_M_Warehouse_ID > 0)
{
sql = sql.append(" AND o.M_Warehouse_ID=? ");
}
sql = sql.append("ORDER BY o.DateOrdered");
//
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql.toString(), null);
pstmt.setInt(1, C_BPartner_ID);
if(p_M_Warehouse_ID > 0)
{
//only active for material receipts
pstmt.setInt(2, p_M_Warehouse_ID);
}
rs = pstmt.executeQuery();
while (rs.next())
{
list.add(new KeyNamePair(rs.getInt(1), rs.getString(2)));
}
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql.toString(), e);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}

return list;
} // initBPartnerOIS
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.compiere.grid;
package org.compiere.apps.form;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
Expand All @@ -21,8 +21,10 @@
import java.util.Vector;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.minigrid.IMiniTable;
import org.compiere.model.GridTab;
import org.compiere.model.I_C_Invoice;
import org.compiere.model.MDocType;
import org.compiere.model.MInOut;
import org.compiere.model.MInOutLine;
import org.compiere.model.MInvoice;
Expand All @@ -48,31 +50,122 @@
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
* <li>BF [ 1896947 ] Generate invoice from Order error
* <li>BF [ 2007837 ] VCreateFrom.save() should run in trx
* @author Yamel Senih, ysenih@erpcya.com, ERPCyA http://www.erpcya.com
* <li> FR [ 114 ] Change "Create From" UI for Form like Dialog in window without "hardcode"
* @see https://github.com/adempiere/adempiere/issues/114
*/
public class CreateFromInvoice extends CreateFrom
{
public class CreateFromInvoice extends CreateFromHelper {
/** Loaded Order */
private MOrder p_order = null;
/** Loaded RMA */
private MRMA m_rma = null;
/** Record Identifier */
private int m_Record_ID = 0;
/**
* Protected Constructor
* @param mTab MTab
* Valid Table and Record Identifier
* @param p_AD_Table_ID
* @param p_Record_ID
*/
public CreateFromInvoice(GridTab mTab)
{
super(mTab);
log.info(mTab.toString());
} // VCreateFromInvoice

protected void validTable(int p_AD_Table_ID, int p_Record_ID) {
// Valid Table
if(p_AD_Table_ID != I_C_Invoice.Table_ID) {
throw new AdempiereException("@AD_Table_ID@ @C_BankStatement_ID@ @NotFound@");
}
// Valid Record Identifier
if(p_Record_ID <= 0) {
throw new AdempiereException("@SaveErrorRowNotFound@");
}
// Default
m_Record_ID = p_Record_ID;
}

/**
* Dynamic Init
* @return true if initialized
* Load Data - Order
* @param C_Order_ID Order
* @param forInvoice true if for invoice vs. delivery qty
*/
public boolean dynInit() throws Exception
protected Vector<Vector<Object>> getOrderData (int C_Order_ID, boolean forInvoice)
{
log.config("");
setTitle(Msg.getElement(Env.getCtx(), "C_Invoice_ID", false) + " .. " + Msg.translate(Env.getCtx(), "CreateFrom"));
/**
* Selected - 0
* Qty - 1
* C_UOM_ID - 2
* M_Product_ID - 3
* VendorProductNo - 4
* OrderLine - 5
* ShipmentLine - 6
* InvoiceLine - 7
*/
log.config("C_Order_ID=" + C_Order_ID);
p_order = new MOrder (Env.getCtx(), C_Order_ID, null);

return true;
} // dynInit
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
StringBuffer sql = new StringBuffer("SELECT "
+ "l.QtyOrdered-SUM(COALESCE(m.Qty,0))," // 1
+ "CASE WHEN l.QtyOrdered=0 THEN 0 ELSE l.QtyEntered/l.QtyOrdered END," // 2
+ " l.C_UOM_ID,COALESCE(uom.UOMSymbol,uom.Name)," // 3..4
+ " COALESCE(l.M_Product_ID,0),COALESCE(p.Name,c.Name),po.VendorProductNo," // 5..7
+ " l.C_OrderLine_ID,l.Line " // 8..9
+ "FROM C_OrderLine l"
+ " LEFT OUTER JOIN M_Product_PO po ON (l.M_Product_ID = po.M_Product_ID AND l.C_BPartner_ID = po.C_BPartner_ID) "
+ " LEFT OUTER JOIN M_MatchPO m ON (l.C_OrderLine_ID=m.C_OrderLine_ID AND ");
sql.append(forInvoice ? "m.C_InvoiceLine_ID" : "m.M_InOutLine_ID");
sql.append(" IS NOT NULL)")
.append(" LEFT OUTER JOIN M_Product p ON (l.M_Product_ID=p.M_Product_ID)"
+ " LEFT OUTER JOIN C_Charge c ON (l.C_Charge_ID=c.C_Charge_ID)");
if (Env.isBaseLanguage(Env.getCtx(), "C_UOM"))
sql.append(" LEFT OUTER JOIN C_UOM uom ON (l.C_UOM_ID=uom.C_UOM_ID)");
else
sql.append(" LEFT OUTER JOIN C_UOM_Trl uom ON (l.C_UOM_ID=uom.C_UOM_ID AND uom.AD_Language='")
.append(Env.getAD_Language(Env.getCtx())).append("')");
//
sql.append(" WHERE l.C_Order_ID=? " // #1
+ "GROUP BY l.QtyOrdered,CASE WHEN l.QtyOrdered=0 THEN 0 ELSE l.QtyEntered/l.QtyOrdered END, "
+ "l.C_UOM_ID,COALESCE(uom.UOMSymbol,uom.Name),po.VendorProductNo, "
+ "l.M_Product_ID,COALESCE(p.Name,c.Name), l.Line,l.C_OrderLine_ID "
+ "ORDER BY l.Line");
//
log.finer(sql.toString());
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql.toString(), null);
pstmt.setInt(1, C_Order_ID);
rs = pstmt.executeQuery();
while (rs.next())
{
Vector<Object> line = new Vector<Object>();
line.add(new Boolean(false)); // 0-Selection
BigDecimal qtyOrdered = rs.getBigDecimal(1);
BigDecimal multiplier = rs.getBigDecimal(2);
BigDecimal qtyEntered = qtyOrdered.multiply(multiplier);
line.add(qtyEntered); // 1-Qty
KeyNamePair pp = new KeyNamePair(rs.getInt(3), rs.getString(4).trim());
line.add(pp); // 2-UOM
pp = new KeyNamePair(rs.getInt(5), rs.getString(6));
line.add(pp); // 3-Product
line.add(rs.getString(7)); // 4-VendorProductNo
pp = new KeyNamePair(rs.getInt(8), rs.getString(9));
line.add(pp); // 5-OrderLine
line.add(null); // 6-Ship
line.add(null); // 7-Invoice
data.add(line);
}
}
catch (Exception e)
{
log.log(Level.SEVERE, sql.toString(), e);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}

return data;
} // LoadOrder

/**
* Load PBartner dependent Order/Invoice/Shipment Field.
* @param C_BPartner_ID
Expand Down Expand Up @@ -321,13 +414,9 @@ protected Vector<Vector<Object>> getRMAData(int M_RMA_ID)
}

/**
* List number of rows selected
* Set Column Class for mini table
* @param miniTable
*/
public void info()
{

} // infoInvoice

protected void configureMiniTable (IMiniTable miniTable)
{
miniTable.setColumnClass(0, Boolean.class, false); // 0-Selection
Expand All @@ -349,8 +438,8 @@ protected void configureMiniTable (IMiniTable miniTable)
public boolean save(IMiniTable miniTable, String trxName)
{
// Invoice
int C_Invoice_ID = ((Integer)getGridTab().getValue("C_Invoice_ID")).intValue();
MInvoice invoice = new MInvoice (Env.getCtx(), C_Invoice_ID, trxName);
// Yamel Senih FR [ 114 ] get Record ID from record
MInvoice invoice = new MInvoice (Env.getCtx(), m_Record_ID, trxName);
log.config(invoice.toString());

if (p_order != null)
Expand Down Expand Up @@ -554,4 +643,33 @@ protected Vector<String> getOISColumnNames()
return columnNames;
}

/**
* Get Document Base Type from Document Type of Invoice
* @return
*/
protected String getDocBaseType() {
if(m_Record_ID <= 0)
return "";
// For other
MInvoice invoice = new MInvoice(Env.getCtx(), m_Record_ID, null);
MDocType docType = MDocType.get(Env.getCtx(), invoice.getC_DocTypeTarget_ID());
if(docType != null) {
return docType.getDocBaseType();
}
// Default
return "";
}

/**
* Get Business Partner from Invoice
* @return
*/
protected int getC_BPartner_ID() {
if(m_Record_ID <= 0)
return 0;
// For other
MInvoice invoice = new MInvoice(Env.getCtx(), m_Record_ID, null);
// Default
return invoice.getC_BPartner_ID();
}
}
3 changes: 2 additions & 1 deletion client/src/org/compiere/apps/form/ICreateFrom.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public interface ICreateFrom {

/**
* Prepare Information
* Return false if it is not used
*/
public void info();
public boolean info();

/**
* Save Data from table
Expand Down

0 comments on commit 3e60440

Please sign in to comment.