diff --git a/client/src/org/compiere/apps/form/Charge.java b/client/src/org/compiere/apps/form/Charge.java new file mode 100644 index 0000000000..1db75fce76 --- /dev/null +++ b/client/src/org/compiere/apps/form/Charge.java @@ -0,0 +1,395 @@ +/****************************************************************************** + * Copyright (C) 2009 Low Heng Sin * + * Copyright (C) 2009 Idalica Corporation * + * 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.compiere.apps.form; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Vector; +import java.util.logging.Level; + +import org.compiere.minigrid.IMiniTable; +import org.compiere.model.MAccount; +import org.compiere.model.MAcctSchema; +import org.compiere.model.MCharge; +import org.compiere.model.MElementValue; +import org.compiere.util.CLogger; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.compiere.util.KeyNamePair; +import org.compiere.util.Msg; + +/** + * Create Charge from Accounts + * + * @author Jorg Janke + * @version $Id: Charge.java,v 1.3 2006/07/30 00:51:28 jjanke Exp $ + */ +public class Charge +{ + /** + * + */ + private static final long serialVersionUID = 2478440763968206819L; + + /** Window No */ + public int m_WindowNo = 0; +// /** FormFrame */ +// private FormFrame m_frame; + + /** Account Element */ + public int m_C_Element_ID = 0; + /** AccountSchema */ + private int m_C_AcctSchema_ID = 0; + /** Default Charge Tax Category */ + private int m_C_TaxCategory_ID = 0; + private int m_AD_Client_ID = 0; + private int m_AD_Org_ID = 0; + private int m_CreatedBy = 0; + private MAcctSchema m_acctSchema = null; + /** Logger */ + public static CLogger log = CLogger.getCLogger(Charge.class); + + /** + * Dynamic Init + * - Get defaults for primary AcctSchema + * - Create Table with Accounts + */ + public Vector> getData() + { + // Table + Vector> data = new Vector>(); + String sql = "SELECT C_ElementValue_ID,Value, Name, AccountType " + + "FROM C_ElementValue " + + "WHERE AccountType IN ('R','E')" + + " AND IsSummary='N'" + + " AND C_Element_ID=? " + + "ORDER BY 2"; + try + { + PreparedStatement pstmt = DB.prepareStatement(sql, null); + pstmt.setInt(1, m_C_Element_ID); + ResultSet rs = pstmt.executeQuery(); + while (rs.next()) + { + Vector line = new Vector(4); + line.add(new Boolean(false)); // 0-Selection + KeyNamePair pp = new KeyNamePair(rs.getInt(1), rs.getString(2)); + line.add(pp); // 1-Value + line.add(rs.getString(3)); // 2-Name + boolean isExpenseType = rs.getString(4).equals("E"); + line.add(new Boolean(isExpenseType)); // 3-Expense + data.add(line); + } + rs.close(); + pstmt.close(); + } + catch (SQLException e) + { + log.log(Level.SEVERE, sql, e); + } + + return data; + } + + /** + * Finds the Element Identifier for the current charge. + * + */ + public void findChargeElementID() + { + m_C_AcctSchema_ID = Env.getContextAsInt(Env.getCtx(), "$C_AcctSchema_ID"); + // get Element + String sql = "SELECT C_Element_ID " + + "FROM C_AcctSchema_Element " + + "WHERE ElementType='AC' AND C_AcctSchema_ID=?"; + + try + { + PreparedStatement pstmt = DB.prepareStatement(sql, null); + pstmt.setInt(1, m_C_AcctSchema_ID); + ResultSet rs = pstmt.executeQuery(); + if (rs.next()) + { + m_C_Element_ID = rs.getInt(1); + } + rs.close(); + pstmt.close(); + } + catch (SQLException exception) + { + log.log(Level.SEVERE, sql, exception); + } + } + + public Vector getColumnNames() + { + // Header Info + Vector columnNames = new Vector(4); + columnNames.add(Msg.getMsg(Env.getCtx(), "Select")); + columnNames.add(Msg.translate(Env.getCtx(), "Value")); + columnNames.add(Msg.translate(Env.getCtx(), "Name")); + columnNames.add(Msg.getMsg(Env.getCtx(), "Expense")); + + return columnNames; + } + + public void setColumnClass(IMiniTable dataTable) + { + dataTable.setColumnClass(0, Boolean.class, false); // 0-Selection + dataTable.setColumnClass(1, String.class, true); // 1-Value + dataTable.setColumnClass(2, String.class, true); // 2-Name + dataTable.setColumnClass(3, Boolean.class, true); // 3-Expense + // Table UI + dataTable.autoSize(); + } + + /** + * Finds the identifier for the tax category for the client. + */ + public void findTaxCategoryID() + { + // Other Defaults + m_AD_Client_ID = Env.getAD_Client_ID(Env.getCtx()); + m_AD_Org_ID = Env.getAD_Org_ID(Env.getCtx()); + m_CreatedBy = Env.getAD_User_ID(Env.getCtx()); + + // TaxCategory + String sql = "SELECT C_TaxCategory_ID FROM C_TaxCategory " + + "WHERE IsDefault='Y' AND AD_Client_ID=?"; + m_C_TaxCategory_ID = 0; + try + { + PreparedStatement pstmt = DB.prepareStatement(sql, null); + pstmt.setInt(1, m_AD_Client_ID); + ResultSet rs = pstmt.executeQuery(); + if (rs.next()) + m_C_TaxCategory_ID = rs.getInt(1); + rs.close(); + pstmt.close(); + } + catch (SQLException e) + { + log.log(Level.SEVERE, sql, e); + } + } // dynInit + + /************************************************************************** + * Create ElementValue for primary AcctSchema + * @param value value + * @param name name + * @param isExpenseType is expense + * @return element value + */ + protected int createElementValue (String value, String name, boolean isExpenseType) + { + log.config(name); + // + MElementValue ev = new MElementValue(Env.getCtx(), value, name, null, + isExpenseType ? MElementValue.ACCOUNTTYPE_Expense : MElementValue.ACCOUNTTYPE_Revenue, + MElementValue.ACCOUNTSIGN_Natural, + false, false, null); + ev.setAD_Org_ID(m_AD_Org_ID); + if (!ev.save()) + log.log(Level.WARNING, "C_ElementValue_ID not created"); + return ev.getC_ElementValue_ID(); + } // createElementValue + + /** + * Create Charge and account entries for primary Account Schema. + * + * @param name charge name + * @param elementValueId element value identifier + * @return charge identifier, or 0 if no charge created. + */ + protected int createCharge(String name, int elementValueId) + { + MCharge charge; + MAccount account; + + log.config(name + " - "); + // Charge + charge = new MCharge(Env.getCtx(), 0, null); + charge.setName(name); + charge.setC_TaxCategory_ID(m_C_TaxCategory_ID); + if (!charge.save()) + { + log.log(Level.SEVERE, name + " not created"); + return 0; + } + + refreshAccountSchema(); + if (!isAccountSchemaValid()) + { + return 0; + } + + // Target Account + account = getAccount(elementValueId, charge); + if (account == null) + { + return 0; + } + + updateAccount(charge, account); + + return charge.getC_Charge_ID(); + } // createCharge + + /** + * Updates the charge account details. + * @param charge the charge + * @param account the account + */ + private void updateAccount(MCharge charge, MAccount account) + { + StringBuffer sql = createUpdateAccountSql(charge, account); + // + int noAffectedRows = DB.executeUpdate(sql.toString(), null); + if (noAffectedRows != 1) + { + log.log(Level.SEVERE, "Update #" + noAffectedRows + "\n" + sql.toString()); + } + + return; + } + + + /** + * Queries whether the current account scheme is valid. + * @return false if the current account is null or + * its identifier is 0 (zero). + */ + private boolean isAccountSchemaValid() + { + if (m_acctSchema == null) + { + return false; + } + else if (m_acctSchema.getC_AcctSchema_ID() == 0) + { + return false; + } + + return true; + } + + + /** + * Creates the SQL statement for updating the account and charge. + * + * @param charge charge + * @param account account + * @return the SQL DML statement for updating the specified account and charge. + */ + private StringBuffer createUpdateAccountSql(MCharge charge, MAccount account) + { + StringBuffer sql = new StringBuffer("UPDATE C_Charge_Acct "); + sql.append("SET CH_Expense_Acct=").append(account.getC_ValidCombination_ID()); + sql.append(", CH_Revenue_Acct=").append(account.getC_ValidCombination_ID()); + sql.append(" WHERE C_Charge_ID=").append(charge.getC_Charge_ID()); + sql.append(" AND C_AcctSchema_ID=").append(m_C_AcctSchema_ID); + + return sql; + } + + + /** + * Refreshes the current account schema. + * + */ + private void refreshAccountSchema() + { + // Get AcctSchama + if (m_acctSchema == null) + { + m_acctSchema = new MAcctSchema(Env.getCtx(), m_C_AcctSchema_ID, null); + } + + return; + } + + + /** + * Gets the account for the specified charge and element value. + * The account is created if it doesn't already exist. + * @param elementValueId identifier for the element value + * @param charge charge + * @return the account + */ + private MAccount getAccount(int elementValueId, MCharge charge) + { + MAccount defaultAccount = MAccount.getDefault(m_acctSchema, true); // optional null + MAccount account = MAccount.get(Env.getCtx(), + charge.getAD_Client_ID(), + charge.getAD_Org_ID(), + m_acctSchema.getC_AcctSchema_ID(), + elementValueId, + defaultAccount.getC_SubAcct_ID(), + defaultAccount.getM_Product_ID(), + defaultAccount.getC_BPartner_ID(), + defaultAccount.getAD_OrgTrx_ID(), + defaultAccount.getC_LocFrom_ID(), + defaultAccount.getC_LocTo_ID(), + defaultAccount.getC_SalesRegion_ID(), + defaultAccount.getC_Project_ID(), + defaultAccount.getC_Campaign_ID(), + defaultAccount.getC_Activity_ID(), + defaultAccount.getUser1_ID(), + defaultAccount.getUser2_ID(), + defaultAccount.getUserElement1_ID(), + defaultAccount.getUserElement2_ID()); + + return account; + } + + public StringBuffer listCreated; + public StringBuffer listRejected; + + public void createAccount(IMiniTable dataTable) + { + log.config(""); + // + listCreated = new StringBuffer(); + listRejected = new StringBuffer(); + // + int rows = dataTable.getRowCount(); + for (int i = 0; i < rows; i++) + { + if (((Boolean)dataTable.getValueAt(i, 0)).booleanValue()) + { + KeyNamePair pp = (KeyNamePair)dataTable.getValueAt(i, 1); + int C_ElementValue_ID = pp.getKey(); + String name = (String)dataTable.getValueAt(i, 2); + // + int C_Charge_ID = createCharge(name, C_ElementValue_ID); + if (C_Charge_ID == 0) + { + if (listRejected.length() > 0) + listRejected.append(", "); + listRejected.append(name); + } + else + { + if (listCreated.length() > 0) + listCreated.append(", "); + listCreated.append(name); + } + // reset selection + dataTable.setValueAt(new Boolean(false), i, 0); + } + } + } // createAccount + +} // Charge diff --git a/client/src/org/compiere/apps/form/VCharge.java b/client/src/org/compiere/apps/form/VCharge.java index eee6d2487c..db79cc5cd2 100644 --- a/client/src/org/compiere/apps/form/VCharge.java +++ b/client/src/org/compiere/apps/form/VCharge.java @@ -60,7 +60,7 @@ import org.compiere.util.Msg; * @author Jorg Janke * @version $Id: VCharge.java,v 1.3 2006/07/30 00:51:28 jjanke Exp $ */ -public class VCharge extends CPanel +public class VCharge extends Charge implements FormPanel, ActionListener { /** @@ -68,6 +68,7 @@ public class VCharge extends CPanel */ private static final long serialVersionUID = 2478440763968206819L; + private CPanel panel = new CPanel(); /** * Initialize Panel * @param WindowNo window @@ -82,6 +83,7 @@ public class VCharge extends CPanel { jbInit(); dynInit(); + frame.getContentPane().add(mainPanel, BorderLayout.CENTER); frame.getContentPane().add(confirmPanel, BorderLayout.SOUTH); } @@ -91,24 +93,9 @@ public class VCharge extends CPanel } } // init - - /** Window No */ - private int m_WindowNo = 0; /** FormFrame */ private FormFrame m_frame; - /** Account Element */ - private int m_C_Element_ID = 0; - /** AccountSchema */ - private int m_C_AcctSchema_ID = 0; - /** Default Charge Tax Category */ - private int m_C_TaxCategory_ID = 0; - private int m_AD_Client_ID = 0; - private int m_AD_Org_ID = 0; - private int m_CreatedBy = 0; - private MAcctSchema m_acctSchema = null; - /** Logger */ - private static CLogger log = CLogger.getCLogger(VCharge.class); // private CPanel mainPanel = new CPanel(); private BorderLayout mainLayout = new BorderLayout(); @@ -137,7 +124,7 @@ public class VCharge extends CPanel */ private void jbInit() throws Exception { - CompiereColor.setBackground(this); + CompiereColor.setBackground(panel); newBorder = new TitledBorder(""); accountBorder = new TitledBorder(""); mainPanel.setLayout(mainLayout); @@ -181,109 +168,6 @@ public class VCharge extends CPanel dataPane.getViewport().add(dataTable, null); } // jbInit - /** - * Dynamic Init - * - Get defaults for primary AcctSchema - * - Create Table with Accounts - */ - private void dynInit() - { - m_C_AcctSchema_ID = Env.getContextAsInt(Env.getCtx(), "$C_AcctSchema_ID"); - // get Element - String sql = "SELECT C_Element_ID FROM C_AcctSchema_Element " - + "WHERE ElementType='AC' AND C_AcctSchema_ID=?"; - try - { - PreparedStatement pstmt = DB.prepareStatement(sql, null); - pstmt.setInt(1, m_C_AcctSchema_ID); - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) - m_C_Element_ID = rs.getInt(1); - rs.close(); - pstmt.close(); - } - catch (SQLException e) - { - log.log(Level.SEVERE, sql, e); - } - if (m_C_Element_ID == 0) - return; - - - // Table - Vector> data = new Vector>(); - sql = "SELECT C_ElementValue_ID,Value, Name, AccountType " - + "FROM C_ElementValue " - + "WHERE AccountType IN ('R','E')" - + " AND IsSummary='N'" - + " AND C_Element_ID=? " - + "ORDER BY 2"; - try - { - PreparedStatement pstmt = DB.prepareStatement(sql, null); - pstmt.setInt(1, m_C_Element_ID); - ResultSet rs = pstmt.executeQuery(); - while (rs.next()) - { - Vector line = new Vector(4); - line.add(new Boolean(false)); // 0-Selection - KeyNamePair pp = new KeyNamePair(rs.getInt(1), rs.getString(2)); - line.add(pp); // 1-Value - line.add(rs.getString(3)); // 2-Name - boolean isExpenseType = rs.getString(4).equals("E"); - line.add(new Boolean(isExpenseType)); // 3-Expense - data.add(line); - } - rs.close(); - pstmt.close(); - } - catch (SQLException e) - { - log.log(Level.SEVERE, sql, e); - } - // Header Info - Vector columnNames = new Vector(4); - columnNames.add(Msg.getMsg(Env.getCtx(), "Select")); - columnNames.add(Msg.translate(Env.getCtx(), "Value")); - columnNames.add(Msg.translate(Env.getCtx(), "Name")); - columnNames.add(Msg.getMsg(Env.getCtx(), "Expense")); - - // Set Model - DefaultTableModel model = new DefaultTableModel(data, columnNames); - dataTable.setModel(model); - // - dataTable.setColumnClass(0, Boolean.class, false); // 0-Selection - dataTable.setColumnClass(1, String.class, true); // 1-Value - dataTable.setColumnClass(2, String.class, true); // 2-Name - dataTable.setColumnClass(3, Boolean.class, true); // 3-Expense - // Table UI - dataTable.autoSize(); - - // Other Defaults - m_AD_Client_ID = Env.getAD_Client_ID(Env.getCtx()); - m_AD_Org_ID = Env.getAD_Org_ID(Env.getCtx()); - m_CreatedBy = Env.getAD_User_ID(Env.getCtx()); - - // TaxCategory - sql = "SELECT C_TaxCategory_ID FROM C_TaxCategory " - + "WHERE IsDefault='Y' AND AD_Client_ID=?"; - m_C_TaxCategory_ID = 0; - try - { - PreparedStatement pstmt = DB.prepareStatement(sql, null); - pstmt.setInt(1, m_AD_Client_ID); - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) - m_C_TaxCategory_ID = rs.getInt(1); - rs.close(); - pstmt.close(); - } - catch (SQLException e) - { - log.log(Level.SEVERE, sql, e); - } - } // dynInit - /** * Dispose */ @@ -331,20 +215,20 @@ public class VCharge extends CPanel return; } // Create Element - int C_ElementValue_ID = create_ElementValue (value, name, isExpense.isSelected()); + int C_ElementValue_ID = createElementValue (value, name, isExpense.isSelected()); if (C_ElementValue_ID == 0) { - ADialog.error(m_WindowNo, this, "ChargeNotCreated", name); + ADialog.error(m_WindowNo, panel, "ChargeNotCreated", name); return; } // Create Charge - int C_Charge_ID = create_Charge(name, C_ElementValue_ID); + int C_Charge_ID = createCharge(name, C_ElementValue_ID); if (C_Charge_ID == 0) { - ADialog.error(m_WindowNo, this, "ChargeNotCreated", name); + ADialog.error(m_WindowNo, panel, "ChargeNotCreated", name); return; } - ADialog.info(m_WindowNo, this, "ChargeCreated", name); + ADialog.info(m_WindowNo, panel, "ChargeCreated", name); } // createNew /** @@ -352,118 +236,26 @@ public class VCharge extends CPanel */ private void createAccount() { - log.config(""); - // - StringBuffer listCreated = new StringBuffer(); - StringBuffer listRejected = new StringBuffer(); - // - TableModel model = dataTable.getModel(); - int rows = model.getRowCount(); - for (int i = 0; i < rows; i++) - { - if (((Boolean)model.getValueAt(i, 0)).booleanValue()) - { - KeyNamePair pp = (KeyNamePair)model.getValueAt(i, 1); - int C_ElementValue_ID = pp.getKey(); - String name = (String)model.getValueAt(i, 2); - // - int C_Charge_ID = create_Charge(name, C_ElementValue_ID); - if (C_Charge_ID == 0) - { - if (listRejected.length() > 0) - listRejected.append(", "); - listRejected.append(name); - } - else - { - if (listCreated.length() > 0) - listCreated.append(", "); - listCreated.append(name); - } - // reset selection - model.setValueAt(new Boolean(false), i, 0); - } - } + createAccount(dataTable); + if (listCreated.length() > 0) - ADialog.info(m_WindowNo, this, "ChargeCreated", listCreated.toString()); + ADialog.info(m_WindowNo, panel, "ChargeCreated", listCreated.toString()); if (listRejected.length() > 0) - ADialog.error(m_WindowNo, this, "ChargeNotCreated", listRejected.toString()); + ADialog.error(m_WindowNo, panel, "ChargeNotCreated", listRejected.toString()); } // createAccount - - /************************************************************************** - * Create ElementValue for primary AcctSchema - * @param value value - * @param name name - * @param isExpenseType is expense - * @return element value - */ - private int create_ElementValue (String value, String name, boolean isExpenseType) - { - log.config(name); - // - MElementValue ev = new MElementValue(Env.getCtx(), value, name, null, - isExpenseType ? MElementValue.ACCOUNTTYPE_Expense : MElementValue.ACCOUNTTYPE_Revenue, - MElementValue.ACCOUNTSIGN_Natural, - false, false, null); - ev.setAD_Org_ID(m_AD_Org_ID); - if (!ev.save()) - log.log(Level.WARNING, "C_ElementValue_ID not created"); - return ev.getC_ElementValue_ID(); - } // create_ElementValue - /** - * Create Charge and account entries for primary AcctSchema. - * - * @param name name - * @param C_ElementValue_ID element value - * @return charge + * Dynamic Init + * - Get defaults for primary AcctSchema + * - Create Table with Accounts */ - private int create_Charge (String name, int C_ElementValue_ID) + private void dynInit() { - log.config(name + " - "); - // - MCharge charge = new MCharge(Env.getCtx(), 0, null); - charge.setName(name); - charge.setC_TaxCategory_ID(m_C_TaxCategory_ID); - if (!charge.save()) - { - log.log(Level.SEVERE, name + " not created"); - return 0; - } - - // Get AcctSchama - if (m_acctSchema == null) - m_acctSchema = new MAcctSchema(Env.getCtx(), m_C_AcctSchema_ID, null); - if (m_acctSchema == null || m_acctSchema.getC_AcctSchema_ID() == 0) - return 0; - - // Target Account - MAccount defaultAcct = MAccount.getDefault(m_acctSchema, true); // optional null - MAccount acct = MAccount.get(Env.getCtx(), - charge.getAD_Client_ID(), charge.getAD_Org_ID(), - m_acctSchema.getC_AcctSchema_ID(), - C_ElementValue_ID, defaultAcct.getC_SubAcct_ID(), - defaultAcct.getM_Product_ID(), defaultAcct.getC_BPartner_ID(), defaultAcct.getAD_OrgTrx_ID(), - defaultAcct.getC_LocFrom_ID(), defaultAcct.getC_LocTo_ID(), defaultAcct.getC_SalesRegion_ID(), - defaultAcct.getC_Project_ID(), defaultAcct.getC_Campaign_ID(), defaultAcct.getC_Activity_ID(), - defaultAcct.getUser1_ID(), defaultAcct.getUser2_ID(), - defaultAcct.getUserElement1_ID(), defaultAcct.getUserElement2_ID()); - if (acct == null) - return 0; - - // Update Accounts - StringBuffer sql = new StringBuffer("UPDATE C_Charge_Acct "); - sql.append("SET CH_Expense_Acct=").append(acct.getC_ValidCombination_ID()); - sql.append(", CH_Revenue_Acct=").append(acct.getC_ValidCombination_ID()); - sql.append(" WHERE C_Charge_ID=").append(charge.getC_Charge_ID()); - sql.append(" AND C_AcctSchema_ID=").append(m_C_AcctSchema_ID); - // - int no = DB.executeUpdate(sql.toString(), null); - if (no != 1) - log.log(Level.SEVERE, "Update #" + no + "\n" + sql.toString()); - // - return charge.getC_Charge_ID(); - } // create_Charge + findChargeElementID(); + DefaultTableModel model = new DefaultTableModel(getData(), getColumnNames()); + dataTable.setModel(model); + setColumnClass(dataTable); + findTaxCategoryID(); + } } // VCharge diff --git a/zkwebui/WEB-INF/src/org/adempiere/webui/apps/form/WCharge.java b/zkwebui/WEB-INF/src/org/adempiere/webui/apps/form/WCharge.java index a6f796dd00..8ed814751c 100644 --- a/zkwebui/WEB-INF/src/org/adempiere/webui/apps/form/WCharge.java +++ b/zkwebui/WEB-INF/src/org/adempiere/webui/apps/form/WCharge.java @@ -21,10 +21,6 @@ package org.adempiere.webui.apps.form; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Vector; import java.util.logging.Level; import org.adempiere.webui.component.Button; @@ -33,28 +29,32 @@ import org.adempiere.webui.component.Column; import org.adempiere.webui.component.Columns; import org.adempiere.webui.component.ConfirmPanel; import org.adempiere.webui.component.Grid; +import org.adempiere.webui.component.GridFactory; import org.adempiere.webui.component.Label; import org.adempiere.webui.component.ListModelTable; import org.adempiere.webui.component.Panel; import org.adempiere.webui.component.Row; import org.adempiere.webui.component.Rows; import org.adempiere.webui.component.Textbox; +import org.adempiere.webui.component.WAppsAction; import org.adempiere.webui.component.WListbox; import org.adempiere.webui.panel.ADForm; +import org.adempiere.webui.panel.CustomForm; +import org.adempiere.webui.panel.ICustomForm; import org.adempiere.webui.session.SessionManager; import org.adempiere.webui.window.FDialog; -import org.compiere.model.MAccount; -import org.compiere.model.MAcctSchema; -import org.compiere.model.MCharge; -import org.compiere.model.MElementValue; +import org.compiere.apps.form.Charge; import org.compiere.util.CLogger; -import org.compiere.util.DB; import org.compiere.util.Env; -import org.compiere.util.KeyNamePair; import org.compiere.util.Msg; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.Events; +import org.zkoss.zkex.zul.Borderlayout; +import org.zkoss.zkex.zul.Center; +import org.zkoss.zkex.zul.North; +import org.zkoss.zkex.zul.South; +import org.zkoss.zul.Separator; /** * This class represents the Custom Form for generating charges @@ -67,36 +67,25 @@ import org.zkoss.zk.ui.event.Events; * @author Andrew Kimball * */ -public class WCharge extends ADForm implements EventListener +public class WCharge extends Charge implements ICustomForm, EventListener { /** - * + * */ private static final long serialVersionUID = 4210542409436277344L; + + private CustomForm form = new CustomForm(); + /** AD_Message for "Create". */ private static final String AD_MESSAGE_CREATE = "Create"; /** Logger. */ private static CLogger log = CLogger.getCLogger(WCharge.class); - /** Account Element identifier. */ - private int m_elementId = 0; - /** Account Schema identifier. */ - private int m_accountSchemaId = 0; - /** Default Charge Tax Category. */ - private int m_taxCategoryId = 0; - /** Identifier for the client. */ - private int m_clientId = 0; - /** Identifier for the organisation. */ - private int m_organisationId = 0; - /** Accounting schema model. */ - private MAcctSchema m_acctSchema = null; - - /** Panel for holding other panels. */ - private Panel m_pnlMain = new Panel(); + private ListModelTable model; // new panel /** Grid for components for creating a new charge account. */ - private Grid m_grdNew = new Grid(); + private Grid m_grdNew = GridFactory.newGridLayout(); /** Title of new charge account grid. */ private Column m_clmNewTitle = new Column(); /** Value (key) field label. */ @@ -114,7 +103,7 @@ public class WCharge extends ADForm implements EventListener // account panel /** Grid for components for creating a charge form a selected account. **/ - private Grid m_grdAccount = new Grid(); + private Panel m_pnlAccount = new Panel(); /** Title of account grid. */ private Column m_clmAccountTitle = new Column(); /** Button to create charge from selected account. */ @@ -125,7 +114,7 @@ public class WCharge extends ADForm implements EventListener /** confirmation panel. */ private ConfirmPanel m_pnlConfirm = new ConfirmPanel(); /** Confirmation Grid. */ - private Grid m_grdConfirm = new Grid(); + private Grid m_grdConfirm = GridFactory.newGridLayout(); /** Enumeration of column names and indices. */ private enum EColumn @@ -194,6 +183,7 @@ public class WCharge extends ADForm implements EventListener public WCharge() { super(); + initForm(); } @@ -210,8 +200,7 @@ public class WCharge extends ADForm implements EventListener { staticInitialise(); dynamicInitialise(); - this.appendChild(m_pnlMain); - //this.appendChild(confirmPanel); + zkInit(); } catch(Exception e) { @@ -230,13 +219,31 @@ public class WCharge extends ADForm implements EventListener createNewChargePanel(); createAccountPanel(); createConfirmPanel(); - // TODO - m_pnlMain.appendChild(m_grdNew); - m_pnlMain.appendChild(m_grdAccount); - m_pnlMain.appendChild(m_grdConfirm); + return; } + private void zkInit() + { + Borderlayout contentPane = new Borderlayout(); + form.appendChild(contentPane); + + North north = new North(); + contentPane.appendChild(north); + north.appendChild(m_grdNew); + + Center center = new Center(); + contentPane.appendChild(center); + center.appendChild(m_pnlAccount); + + South south = new South(); + contentPane.appendChild(south); + Panel southPanel = new Panel(); + south.appendChild(southPanel); + southPanel.appendChild(new Separator()); + southPanel.appendChild(m_grdConfirm); + } + /** * Creates the account panel. * @@ -246,30 +253,35 @@ public class WCharge extends ADForm implements EventListener */ private void createAccountPanel() { - Row topRow = new Row(); - Row bottomRow = new Row(); - Rows rows = new Rows(); - Columns header = new Columns(); + Borderlayout borderlayout = new Borderlayout(); + borderlayout.setStyle("position: absolute"); + borderlayout.setWidth("100%"); + borderlayout.setHeight("100%"); + m_pnlAccount.appendChild(borderlayout); - // header - m_clmAccountTitle.setLabel(Msg.getMsg(Env.getCtx(), "ChargeFromAccount")); - header.appendChild(m_clmAccountTitle); + North north = new North(); + north.setBorder("none"); + borderlayout.appendChild(north); + Label label = new Label(Msg.getMsg(Env.getCtx(), "ChargeFromAccount")); + label.setStyle("font-weight: bold;"); + north.appendChild(label); - // top row - m_tblData.setRows(20); - topRow.appendChild(m_tblData); - rows.appendChild(topRow); + Center center = new Center(); + center.setBorder("none"); + center.setFlex(true); + center.setAutoscroll(true); + borderlayout.appendChild(center); + center.appendChild(m_tblData); - // bottom row - bottomRow.setAlign("right"); - m_btnAccount.setLabel(Msg.getMsg(Env.getCtx(), AD_MESSAGE_CREATE)); + South south = new South(); + south.setBorder("none"); + borderlayout.appendChild(south); + Panel southPanel = new Panel(); + southPanel.setAlign("right"); + south.appendChild(southPanel); + m_btnAccount.setLabel(Msg.getMsg(Env.getCtx(), AD_MESSAGE_CREATE)); m_btnAccount.addEventListener(Events.ON_CLICK, this); - bottomRow.appendChild(m_btnAccount); - rows.appendChild(bottomRow); - - // put it all together - m_grdAccount.appendChild(header); - m_grdAccount.appendChild(rows); + southPanel.appendChild(m_btnAccount); return; } @@ -284,38 +296,45 @@ public class WCharge extends ADForm implements EventListener { final int nameFieldColumns = 20; final int valueFieldColumns = 10; - Row topRow = new Row(); - Row bottomRow = new Row(); - Rows rows = new Rows(); - Columns header = new Columns(); - - // header - m_clmNewTitle.setLabel(Msg.getMsg(Env.getCtx(), "ChargeNewAccount")); - header.appendChild(m_clmNewTitle); // top row m_lblValue.setValue(Msg.translate(Env.getCtx(), EColumn.VALUE.title())); m_txbValueField.setCols(valueFieldColumns); m_chbIsExpense.setChecked(true); m_chbIsExpense.setLabel(Msg.getMsg(Env.getCtx(), EColumn.EXPENSE.title())); - topRow.appendChild(m_lblValue); - topRow.appendChild(m_txbValueField); - topRow.appendChild(m_chbIsExpense); - rows.appendChild(topRow); // bottom row m_lblName.setValue(Msg.translate(Env.getCtx(), EColumn.NAME.title())); m_txbNameField.setCols(nameFieldColumns); m_btnNew.setLabel(Msg.getMsg(Env.getCtx(), AD_MESSAGE_CREATE)); m_btnNew.addEventListener(Events.ON_CLICK, this); - bottomRow.appendChild(m_lblName); - bottomRow.appendChild(m_txbNameField); - bottomRow.appendChild(m_btnNew); - rows.appendChild(bottomRow); - // put it all together - m_grdNew.appendChild(header); - m_grdNew.appendChild(rows); + Rows rows = new Rows(); + m_grdNew.appendChild(rows); + + Row row = new Row(); + rows.appendChild(row); + row.setSpans("3"); + Label label = new Label(Msg.getMsg(Env.getCtx(), "ChargeNewAccount")); + label.setStyle("font-weight: bold;"); + row.appendChild(label); + + row = new Row(); + rows.appendChild(row); + row.appendChild(m_lblValue); + row.appendChild(m_txbValueField); + row.appendChild(m_chbIsExpense); + + row = new Row(); + rows.appendChild(row); + row.appendChild(m_lblName); + row.appendChild(m_txbNameField); + row.appendChild(m_btnNew); + + row = new Row(); + rows.appendChild(row); + row.setSpans("3"); + row.appendChild(new Separator()); return; } @@ -328,178 +347,15 @@ public class WCharge extends ADForm implements EventListener */ private void dynamicInitialise() { - String sql; - findChargeElementID(); - - if (m_elementId == 0) - { - return; - } - - // Table - Vector> data = new Vector>(); - sql = "SELECT C_ElementValue_ID,Value, Name, AccountType " - + "FROM C_ElementValue " - + "WHERE AccountType IN ('R','E')" - + " AND IsSummary='N'" - + " AND C_Element_ID=? " - + "ORDER BY 2"; - try - { - PreparedStatement pstmt = DB.prepareStatement(sql, null); - pstmt.setInt(1, m_elementId); - ResultSet rs = pstmt.executeQuery(); - - while (rs.next()) - { - Vector line = createDataLine(rs); - data.add(line); - } - rs.close(); - pstmt.close(); - } - catch (SQLException exception) - { - log.log(Level.SEVERE, sql, exception); - } - // Header Info - Vector columnNames = getColumnNames(); - - // Set Model - ListModelTable model = new ListModelTable(data); - m_tblData.setData(model, columnNames); - // - m_tblData.setColumnClass(EColumn.SELECT.index(), Boolean.class, false); // 0-Selection - m_tblData.setColumnClass(EColumn.VALUE.index(), String.class, true); // 1-Value - m_tblData.setColumnClass(EColumn.NAME.index(), String.class, true); // 2-Name - m_tblData.setColumnClass(EColumn.EXPENSE.index(), Boolean.class, true); // 3-Expense - // Table UI - //m_tblData.autoSize(); - - // Other Defaults - m_clientId = Env.getAD_Client_ID(Env.getCtx()); - m_organisationId = Env.getAD_Org_ID(Env.getCtx()); - - // TaxCategory - findTaxCategoryID(); + findChargeElementID(); + ListModelTable model = new ListModelTable(getData()); + m_tblData.setData(model, getColumnNames()); + setColumnClass(m_tblData); + findTaxCategoryID(); return; } // dynInit - - /** - * Finds the identifier for the tax category for the client. - */ - private void findTaxCategoryID() - { - final String sql = "SELECT C_TaxCategory_ID FROM C_TaxCategory " - + "WHERE IsDefault='Y' AND AD_Client_ID=?"; - m_taxCategoryId = 0; - try - { - PreparedStatement pstmt = DB.prepareStatement(sql, null); - pstmt.setInt(1, m_clientId); - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) - { - m_taxCategoryId = rs.getInt(1); - } - rs.close(); - pstmt.close(); - } - catch (SQLException exception) - { - log.log(Level.SEVERE, sql, exception); - } - - return; - } - - - /** - * Gets a vector of column names. - * The column names are used as column headings int he table. - * @return a vector of column names. - */ - private Vector getColumnNames() - { - Vector columnNames = new Vector(EColumn.count()); - - columnNames.add(Msg.getMsg(Env.getCtx(), EColumn.SELECT.title())); - columnNames.add(Msg.translate(Env.getCtx(), EColumn.VALUE.title())); - columnNames.add(Msg.translate(Env.getCtx(), EColumn.NAME.title())); - columnNames.add(Msg.getMsg(Env.getCtx(), EColumn.EXPENSE.title())); - - return columnNames; - } - - /** - * Creates a data line from the given ResultSet. - * - * @param rs result set containing details of an account. - * @return a vector containing details of an account. - * @throws SQLException if a database access error occurred - */ - private Vector createDataLine(ResultSet rs) throws SQLException - { - final int noFields = EColumn.count(); - final int valueIdIndex = 1; - final int valueIndex = 2; - final int nameIndex = 3; - final int expenseIndex = 4; - final String expenseType = "E"; - boolean isExpenseType; - Vector line = new Vector(noFields); - - // 0-Selection - line.add(new Boolean(false)); - - // 1-Value - KeyNamePair pp = new KeyNamePair(rs.getInt(valueIdIndex), - rs.getString(valueIndex)); - line.add(pp); - - // 2-Name - line.add(rs.getString(nameIndex)); - - // 3-Expense - isExpenseType = rs.getString(expenseIndex).equals(expenseType); - line.add(new Boolean(isExpenseType)); - - return line; - } - - - /** - * Finds the Element Identifier for the current charge. - * - */ - private void findChargeElementID() - { - m_accountSchemaId = Env.getContextAsInt(Env.getCtx(), "$C_AcctSchema_ID"); - // get Element - String sql = "SELECT C_Element_ID " - + "FROM C_AcctSchema_Element " - + "WHERE ElementType='AC' AND C_AcctSchema_ID=?"; - - try - { - PreparedStatement pstmt = DB.prepareStatement(sql, null); - pstmt.setInt(1, m_accountSchemaId); - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) - { - m_elementId = rs.getInt(1); - } - rs.close(); - pstmt.close(); - } - catch (SQLException exception) - { - log.log(Level.SEVERE, sql, exception); - } - } - /** * Event Listener. * @@ -509,7 +365,7 @@ public class WCharge extends ADForm implements EventListener { log.info(event.getName()); // - if (event.getTarget().getId().equals(ConfirmPanel.A_OK) || m_elementId == 0) + if (event.getTarget().getId().equals(ConfirmPanel.A_OK) || m_C_Element_ID == 0) { close(); } @@ -557,204 +413,19 @@ public class WCharge extends ADForm implements EventListener int elementValueId = createElementValue (value, name, m_chbIsExpense.isChecked()); if (elementValueId == 0) { - FDialog.error(m_WindowNo, this, "ChargeNotCreated", name); + FDialog.error(form.getWindowNo(), form, "ChargeNotCreated", name); return; } // Create Charge int chargeId = createCharge(name, elementValueId); if (chargeId == 0) { - FDialog.error(m_WindowNo, this, "ChargeNotCreated", name); + FDialog.error(form.getWindowNo(), form, "ChargeNotCreated", name); return; } - FDialog.info(m_WindowNo, this, "ChargeCreated", name); + FDialog.info(form.getWindowNo(), form, "ChargeCreated", name); } // createNew - - - /** - * Create Element Value for primary Account Schema. - * @param value account key - * @param name account name - * @param isExpenseType is expense account - * @return element value identifier - */ - private int createElementValue (String value, String name, boolean isExpenseType) - { - MElementValue elementValue; - - log.config(name); - // - elementValue = new MElementValue(Env.getCtx(), - value, - name, - null, - isExpenseType ? MElementValue.ACCOUNTTYPE_Expense - : MElementValue.ACCOUNTTYPE_Revenue, - MElementValue.ACCOUNTSIGN_Natural, - false, - false, - null); - - elementValue.setAD_Org_ID(m_organisationId); - if (!elementValue.save()) - { - log.log(Level.WARNING, "C_ElementValue_ID not created"); - } - - return elementValue.getC_ElementValue_ID(); - } // create_ElementValue - - /** - * Create Charge and account entries for primary Account Schema. - * - * @param name charge name - * @param elementValueId element value identifier - * @return charge identifier, or 0 if no charge created. - */ - private int createCharge(String name, int elementValueId) - { - MCharge charge; - MAccount account; - - log.config(name + " - "); - // Charge - charge = new MCharge(Env.getCtx(), 0, null); - charge.setName(name); - charge.setC_TaxCategory_ID(m_taxCategoryId); - if (!charge.save()) - { - log.log(Level.SEVERE, name + " not created"); - return 0; - } - - refreshAccountSchema(); - if (!isAccountSchemaValid()) - { - return 0; - } - - // Target Account - account = getAccount(elementValueId, charge); - if (account == null) - { - return 0; - } - - updateAccount(charge, account); - - return charge.getC_Charge_ID(); - } // createCharge - - - /** - * Updates the charge account details. - * @param charge the charge - * @param account the account - */ - private void updateAccount(MCharge charge, MAccount account) - { - StringBuffer sql = createUpdateAccountSql(charge, account); - // - int noAffectedRows = DB.executeUpdate(sql.toString(), null); - if (noAffectedRows != 1) - { - log.log(Level.SEVERE, "Update #" + noAffectedRows + "\n" + sql.toString()); - } - - return; - } - - - /** - * Queries whether the current account scheme is valid. - * @return false if the current account is null or - * its identifier is 0 (zero). - */ - private boolean isAccountSchemaValid() - { - if (m_acctSchema == null) - { - return false; - } - else if (m_acctSchema.getC_AcctSchema_ID() == 0) - { - return false; - } - - return true; - } - - - /** - * Creates the SQL statement for updating the account and charge. - * - * @param charge charge - * @param account account - * @return the SQL DML statement for updating the specified account and charge. - */ - private StringBuffer createUpdateAccountSql(MCharge charge, MAccount account) - { - StringBuffer sql = new StringBuffer("UPDATE C_Charge_Acct "); - sql.append("SET CH_Expense_Acct=").append(account.getC_ValidCombination_ID()); - sql.append(", CH_Revenue_Acct=").append(account.getC_ValidCombination_ID()); - sql.append(" WHERE C_Charge_ID=").append(charge.getC_Charge_ID()); - sql.append(" AND C_AcctSchema_ID=").append(m_accountSchemaId); - - return sql; - } - - - /** - * Refreshes the current account schema. - * - */ - private void refreshAccountSchema() - { - // Get AcctSchama - if (m_acctSchema == null) - { - m_acctSchema = new MAcctSchema(Env.getCtx(), m_accountSchemaId, null); - } - - return; - } - - - /** - * Gets the account for the specified charge and element value. - * The account is created if it doesn't already exist. - * @param elementValueId identifier for the element value - * @param charge charge - * @return the account - */ - private MAccount getAccount(int elementValueId, MCharge charge) - { - MAccount defaultAccount = MAccount.getDefault(m_acctSchema, true); // optional null - MAccount account = MAccount.get(Env.getCtx(), - charge.getAD_Client_ID(), - charge.getAD_Org_ID(), - m_acctSchema.getC_AcctSchema_ID(), - elementValueId, - defaultAccount.getC_SubAcct_ID(), - defaultAccount.getM_Product_ID(), - defaultAccount.getC_BPartner_ID(), - defaultAccount.getAD_OrgTrx_ID(), - defaultAccount.getC_LocFrom_ID(), - defaultAccount.getC_LocTo_ID(), - defaultAccount.getC_SalesRegion_ID(), - defaultAccount.getC_Project_ID(), - defaultAccount.getC_Campaign_ID(), - defaultAccount.getC_Activity_ID(), - defaultAccount.getUser1_ID(), - defaultAccount.getUser2_ID(), - defaultAccount.getUserElement1_ID(), - defaultAccount.getUserElement2_ID()); - - return account; - } - - /** * Creates Charges from Accounts. * Charges are created for the selected accounts. @@ -762,125 +433,19 @@ public class WCharge extends ADForm implements EventListener */ private void createAccount() { - StringBuffer listCreated = new StringBuffer(); - StringBuffer listRejected = new StringBuffer(); - - log.config(""); - - int noCharges = getNoCharges(); - - for (int chargeIndex = 0; chargeIndex < noCharges; chargeIndex++) - { - if (isRowSelected(chargeIndex)) - { - String name = getChargeName(chargeIndex); - int chargeId = createCharge(chargeIndex); - if (chargeId == 0) - { - appendName(listRejected, name); - } - else - { - appendName(listCreated, name); - } - setRowUnselected(chargeIndex); - } - } + createAccount(m_tblData); if (listCreated.length() > 0) { - FDialog.info(m_WindowNo, this, "ChargeCreated", listCreated.toString()); + FDialog.info(form.getWindowNo(), form, "ChargeCreated", listCreated.toString()); } if (listRejected.length() > 0) { - FDialog.error(m_WindowNo, this, "ChargeNotCreated", listRejected.toString()); + FDialog.error(form.getWindowNo(), form, "ChargeNotCreated", listRejected.toString()); } return; } // createAccount - - /** - * Gets the number of charges in the table. - * @return the number of charges in the table. - */ - private int getNoCharges() - { - int noCharges = m_tblData.getRowCount(); - - return noCharges; - } - - /** - * Creates a charge for specified table row. - * - * @param rowIndex index of the row for which a charge is to be created. - * @return the charge identifier. - */ - private int createCharge(int rowIndex) - { - KeyNamePair pp = (KeyNamePair)m_tblData.getValueAt(rowIndex, EColumn.VALUE.index()); - int elementValueId = pp.getKey(); - String name = getChargeName(rowIndex); - int chargeID = createCharge(name, elementValueId); - - return chargeID; - } - - - /** - * Gets the name for a specified table row. - * @param rowIndex the table row for which to get the name. - * @return the charge name. - */ - private String getChargeName(int rowIndex) - { - String name = (String)m_tblData.getValueAt(rowIndex, EColumn.NAME.index()); - - return name; - } - - /** - * Appends the name to the nameList. - * @param nameList a list of names - * @param name the name to append - */ - private void appendName(StringBuffer nameList, String name) - { - if (nameList.length() > 0) - { - nameList.append(", "); - } - nameList.append(name); - - return; - } - - - /** - * Sets a row at rowIndex as unselected. - * @param rowIndex index of the row to deselect. - */ - private void setRowUnselected(int rowIndex) - { - ListModelTable model = m_tblData.getModel(); - model.setDataAt(Boolean.valueOf(false), rowIndex, EColumn.SELECT.index()); - - return; - } - - /** - * Queries whether a row is selected. - * @param rowIndex index of the row to query. - * @return true if the row is selected, false otherwise. - */ - private boolean isRowSelected(int rowIndex) - { - ListModelTable model = m_tblData.getModel(); - Boolean isSelected = (Boolean)model.getDataAt(rowIndex, EColumn.SELECT.index()); - - return isSelected.booleanValue(); - } - /** * Create Confirmation Panel with OK Button. */ @@ -901,6 +466,11 @@ public class WCharge extends ADForm implements EventListener { SessionManager.getAppDesktop().closeActiveWindow(); } + + + public ADForm getForm() { + return form; + } }