www.pudn.com > code_source_compiere_erp_crm_logiciel_java.zip > MCustomer.java
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.sql.*;
import java.util.*;
import org.apache.log4j.Logger;
import org.compiere.util.DB;
import org.compiere.util.Env;
/**
* Customer Model
* Based on Defaults of
*
* @author Jorg Janke
* @version $Id: MCustomer.java,v 1.1 2002/11/15 05:49:43 jjanke Exp $
*/
public class MCustomer
{
/**
* Get MCustomer
* @param EMail search criteris
* @param Password optional search criteria
* @return MCustomer if found or null
*/
public static MCustomer get (String EMail, String Password)
{
int C_BPartner_ID = 0;
int C_BPartner_Contact_ID = 0;
//
String sql = "SELECT C_BPartner_ID, C_BPartner_Contact_ID "
+ "FROM C_BPartner_Contact WHERE EMail=?";
if (Password != null)
sql += " AND Password=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setString (1, EMail);
if (Password != null)
pstmt.setString (2, Password);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
C_BPartner_ID = rs.getInt(1);
C_BPartner_Contact_ID = rs.getInt(2);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.error("get", e);
return null;
}
if (C_BPartner_ID == 0)
return null;
// Load it
MCustomer customer = new MCustomer (0);
customer.load(C_BPartner_ID, C_BPartner_Contact_ID);
return customer;
} // get
/**
* Constructor
* @param C_BPartner_ID
*/
public MCustomer (int C_BPartner_ID)
{
load (C_BPartner_ID);
} // MCustomer
/** Logger */
private static Logger log = Logger.getLogger(MCustomer.class);
/** AD_Message */
private String m_errorMessage = null;
private int C_BPartner_ID = 0;
private int C_BPartner_Contact_ID = 0;
private int C_BPartner_Location_ID = 0;
private int C_GreetingBP_ID = 0;
private int C_Greeting_ID = 0;
private int C_Location_ID = 0;
private String Value="", Name="", Name2="", Contact="", Title="",
Phone="", Fax="", Phone2="", EMail="", Password="";
private String Loc_Phone="", Loc_Phone2="", Loc_Fax="", BP_EMail="";
/**
* Load BPartner
* @para C_BPartner_ID - existing BPartner or 0 for new
*/
private boolean load (int C_BPartner_ID)
{
this.C_BPartner_ID = C_BPartner_ID;
m_errorMessage = null;
// we have it already or new
if (C_BPartner_ID == this.C_BPartner_ID
|| C_BPartner_ID == 0)
return true;
log.info("load - " + C_BPartner_ID);
String SQL = "SELECT p.Value,p.C_Greeting_ID AS C_GreetingBP_ID,p.Name,p.Name2,"
+ "c.Name AS Contact,c.C_Greeting_ID,c.Title,COALESCE(c.EMail,p.EMail),c.Password,"
+ "COALESCE(c.Phone,l.Phone) AS Phone, COALESCE(c.Phone2,l.Phone2) AS Phone2, COALESCE(c.Fax,l.Fax) AS Fax, "
+ "l.C_Location_ID,c.C_BPartner_Contact_ID,l.C_BPartner_Location_ID "
+ "FROM C_BPartner p, C_BPartner_Contact c, C_BPartner_Location l "
+ "WHERE p.C_BPartner_ID=c.C_BPartner_ID(+)"
+ " AND p.C_BPartner_ID=l.C_BPartner_ID(+)"
+ " AND p.C_BPartner_ID=?" // 1
+ " ORDER BY l.IsBillTo Desc"; // BillTo first
try
{
PreparedStatement pstmt = DB.prepareStatement(SQL);
pstmt.setInt(1, C_BPartner_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
load (rs);
else
m_errorMessage = "BPartnerNotFound";
rs.close();
pstmt.close();
}
catch (SQLException e)
{
m_errorMessage = "BPartnerNotFound";
log.error("load", e);
}
if (m_errorMessage == null)
this.C_BPartner_ID = 0;
return m_errorMessage == null;
} // load
/**
* Load BPartner
* @para C_BPartner_ID - existing BPartner or 0 for new
*/
private boolean load (int C_BPartner_ID, int C_BPartner_Contact_ID)
{
this.C_BPartner_ID = C_BPartner_ID;
m_errorMessage = null;
log.info("load - " + C_BPartner_ID + ", Contact=" + C_BPartner_Contact_ID);
String SQL = "SELECT p.Value,p.C_Greeting_ID AS C_GreetingBP_ID,p.Name,p.Name2, "
+ "c.Name AS Contact,c.C_Greeting_ID,c.Title,COALESCE(c.EMail,p.EMail) AS EMail,c.Password, "
+ "COALESCE(c.Phone,l.Phone) AS Phone, COALESCE(c.Phone2,l.Phone2) AS Phone2, COALESCE(c.Fax,l.Fax) AS Fax, "
+ "l.C_Location_ID,c.C_BPartner_Contact_ID,l.C_BPartner_Location_ID "
+ "FROM C_BPartner p, C_BPartner_Contact c, C_BPartner_Location l "
+ "WHERE p.C_BPartner_ID=c.C_BPartner_ID(+)"
+ " AND p.C_BPartner_ID=l.C_BPartner_ID(+)"
+ " AND p.C_BPartner_ID=?" // #1
+ " AND C_BPartner_Contact_ID=?" // #2
+ " ORDER BY l.IsBillTo Desc"; // BillTo first
try
{
PreparedStatement pstmt = DB.prepareStatement(SQL);
pstmt.setInt(1, C_BPartner_ID);
pstmt.setInt(2, C_BPartner_Contact_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
load (rs);
else
m_errorMessage = "BPartnerNotFound";
rs.close();
pstmt.close();
}
catch (SQLException e)
{
m_errorMessage = "BPartnerNotFound";
log.error("load", e);
}
return m_errorMessage == null;
} // load
/**
* Load BPartner
* @para C_BPartner_ID - existing BPartner or 0 for new
*/
private void load (ResultSet rs) throws SQLException
{
setValue (rs.getString("Value"));
setC_GreetingBP_ID (rs.getInt("C_GreetingBP_ID"));
setName (rs.getString("Name"));
setName2 (rs.getString("Name2"));
setC_Greeting_ID (rs.getInt("C_Greeting_ID"));
setContact (rs.getString("Contact"));
setTitle (rs.getString("Title"));
setPhone (rs.getString("Phone"));
setPhone2 (rs.getString("Phone2"));
setFax (rs.getString("Fax"));
setEMail (rs.getString("EMail"));
setPassword (rs.getString("Password"));
setC_Location_ID (rs.getInt("C_Location_ID"));
setC_BPartner_Contact_ID (rs.getInt("C_BPartner_Contact_ID"));
setC_BPartner_Location_ID (rs.getInt("C_BPartner_Location_ID"));
//
Loc_Phone = Phone;
Loc_Phone2 = Phone2;
Loc_Fax = Fax;
BP_EMail = EMail;
} // load
/**
* Save
* Checks mandatory fields and saves Partner, Contact and Location
*/
private boolean save (Properties ctx)
{
log.info("save " + C_BPartner_ID);
m_errorMessage = null;
// Check Mandatory fields
if (Value == null)
Value = Name;
if (Name == null || Value.length() == 0 || Name.length() == 0)
{
m_errorMessage = "FillMandatory";
return false;
}
String SQL = "";
try
{
Statement stmt = DB.createStatement();
int AD_Client_ID = Integer.parseInt(Env.getContext(ctx, "#AD_Client_ID"));
int AD_Org_ID = Integer.parseInt(Env.getContext(ctx, "#AD_Org_ID"));
int UpdatedBy = Integer.parseInt(Env.getContext(ctx, "#AD_User_ID"));
// === Insert Business Partner ===
if (C_BPartner_ID == 0)
{
C_BPartner_ID = DB.getKeyNextNo(AD_Client_ID, "N", "C_BPartner");
log.debug("save - Insert BPartner " + C_BPartner_ID);
String gr = "NULL";
if (C_GreetingBP_ID > 0)
gr = String.valueOf(C_GreetingBP_ID);
SQL = "INSERT INTO C_BPartner"
+ "(C_BPartner_ID,"
+ "AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,"
+ "Value,Name,Name2,C_Greeting_ID,IsSummary,IsOneTime,IsProspect,IsVendor,IsCustomer,IsEmployee,IsSalesRep,"
+ "AD_Language,C_BP_Group_ID,C_InvoiceSchedule_ID,C_PaymentTerm_ID,"
+ "M_PriceList_ID,M_DiscountSchema_ID,C_Dunning_ID,DocumentCopies,PaymentRule,"
+ "SO_CreditLimit, SO_CreditUsed, EMail) "
//
+ "SELECT " + C_BPartner_ID + ","
+ AD_Client_ID + "," + AD_Org_ID + ",'Y',SysDate," + UpdatedBy + ",SysDate," + UpdatedBy + ","
+ "'" + Value + "','" + Name + "','" + Name2 + "'," + gr + ",'N','N','N','N','Y','N','N',"
+ "AD_Language,C_BP_Group_ID,C_InvoiceSchedule_ID,C_PaymentTerm_ID,"
+ "M_PriceList_ID,M_DiscountSchema_ID,C_Dunning_ID,DocumentCopies,PaymentRule,"
+ "0,0,'" + EMail + "' "
+ "FROM C_BPartner p, AD_ClientInfo c "
+ "WHERE c.AD_Client_ID=" + AD_Client_ID
+ " AND p.C_BPartner_ID(+)=c.C_BPartnerCashTrx_ID";
int i = stmt.executeUpdate(SQL);
if (i != 1)
{
m_errorMessage = "BPartnerNotSaved";
log.error("Partner Insert #=" + i + ", SQL=" + SQL);
return false;
}
}
// === Update Business Partner === (value/greeting/name/name2/email)
else
{
log.debug("save - Update BPartner " + C_BPartner_ID);
String gr = "NULL";
if (C_GreetingBP_ID > 0)
gr = String.valueOf(C_GreetingBP_ID);
SQL = "UPDATE C_BPartner SET "
+ "Value='" + Value + "',"
+ "C_Greeting_ID=" + gr + ","
+ "Name='" + Name + "',"
+ "Name2='" + Name2 + "',"
+ "EMail='" + EMail + "',"
+ "Updated=SysDate, UpdatedBy=" + UpdatedBy
+ " WHERE C_BPartner_ID=" + C_BPartner_ID;
int i = stmt.executeUpdate(SQL);
if (i != 1)
{
m_errorMessage = "BPartnerNotSaved";
log.error ("Partner Update #=" + i + ", SQL=" + SQL);
return false;
}
}
// === Insert New BPartner_Location ===
if (C_BPartner_Location_ID == 0 && C_Location_ID != 0)
{
C_BPartner_Location_ID = DB.getKeyNextNo(AD_Client_ID, "N", "C_BPartner_Location");
log.debug("Insert BPartner Location " + C_BPartner_Location_ID + " (" + C_Location_ID + ")");
SQL = "INSERT INTO C_BPartner_Location"
+ "(C_BPartner_Location_ID, C_BPartner_ID,"
+ "AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,"
+ "Name, C_Location_ID, Phone, Phone2, Fax)"
+ " VALUES "
+ "(" + C_BPartner_Location_ID + "," + C_BPartner_ID + ","
+ AD_Client_ID + "," + AD_Org_ID + ",'Y',SysDate," + UpdatedBy + ",SysDate," + UpdatedBy + ","
+ "'.'," + C_Location_ID + ",'" + Phone + "','" + Phone
+ "','" + Fax + "')";
int i = stmt.executeUpdate(SQL);
if (i != 1)
{
m_errorMessage = "BPartnerNotSaved";
log.error ("Location Insert #=" + i + ", SQL=" + SQL);
return false;
}
}
// === Update BPartner_Location ===
else if (C_BPartner_Location_ID != 0)
{
log.debug("Update BPartner Location " + C_BPartner_Location_ID);
SQL = "UPDATE C_BPartner_Location SET "
+ "C_Location_ID=" + C_Location_ID + ","
+ "Phone='" + Phone + "'," // ?? ok for one contact
+ "Phone2='" + Phone2 + "'," // ??
+ "Fax='" + Fax + "'," // ??
+ "Updated=SysDate, UpdatedBy=" + UpdatedBy
+ " WHERE C_BPartner_Location_ID=" + C_BPartner_Location_ID;
int i = stmt.executeUpdate(SQL);
if (i != 1)
{
m_errorMessage = "BPartnerNotSaved";
log.error ("Location Update #=" + i + ", SQL=" + SQL);
return false;
}
}
// === Insert new BPartner_Contact === only if contact entered
if (C_BPartner_Contact_ID == 0 && Contact.length() != 0)
{
C_BPartner_Contact_ID = DB.getKeyNextNo (AD_Client_ID, "N", "C_BPartner_Contact");
log.debug("Insert BPartner Contact " + C_BPartner_Contact_ID);
String gr = "NULL";
if (C_Greeting_ID > 0)
gr = String.valueOf(C_Greeting_ID);
SQL = "INSERT INTO C_BPartner_Contact"
+ "(C_BPartner_Contact_ID, C_BPartner_ID,"
+ "AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,"
+ "Name, C_Greeting_ID, Title, Phone, Phone2, Fax, EMail, Password)"
+ " VALUES "
+ "(" + C_BPartner_Contact_ID + "," + C_BPartner_ID + ","
+ AD_Client_ID + "," + AD_Org_ID + ",'Y',SysDate," + UpdatedBy + ",SysDate," + UpdatedBy + ","
+ "'" + Contact + "'," + gr + ",'" + Title + "','" + Phone + "','" + Phone2 + "','"
+ Fax + "','" + EMail + "','" + Password +"')";
int i = stmt.executeUpdate(SQL);
if (i != 1)
{
m_errorMessage = "BPartnerNotSaved";
log.error ("Contact Insert #=" + i + ", SQL=" + SQL);
return false;
}
}
// === Update BPartner_Contact ===
else if (C_BPartner_Contact_ID != 0)
{
log.debug("Update BPartner Contact " + C_BPartner_Contact_ID);
// We need to have a Name
if (Contact.length() == 0)
Contact = Name;
String gr = "NULL";
if (C_Greeting_ID > 0)
gr = String.valueOf(C_Greeting_ID);
SQL = "UPDATE C_BPartner_Contact SET "
+ "Name='" + Contact + "',"
+ "C_Greeting_ID=" + gr + ","
+ "Title='" + Title + "',"
+ "Phone='" + Phone + "',"
+ "Phone2='" + Phone2 + "',"
+ "Fax='" + Fax + "',"
+ "EMail='" + EMail + "',"
+ "Password='" + Password + "',"
// location ??
+ "Updated=SysDate, UpdatedBy=" + UpdatedBy
+ " WHERE C_BPartner_Contact_ID=" + C_BPartner_Contact_ID;
int i = stmt.executeUpdate(SQL);
if (i != 1)
{
m_errorMessage = "BPartnerNotSaved";
log.error ("Contact Update #=" + i + ", SQL=" + SQL);
return false;
}
}
}
catch (SQLException e)
{
m_errorMessage = "BPartnerNotSaved";
log.error("save " + SQL, e);
C_BPartner_ID = 0;
}
return m_errorMessage == null;
} // save
public String getErrorMessage()
{
return m_errorMessage;
}
/*************************************************************************/
public int getC_BPartner_Contact_ID()
{
return C_BPartner_Contact_ID;
}
public void setC_BPartner_Contact_ID(int C_BPartner_Contact_ID)
{
this.C_BPartner_Contact_ID = C_BPartner_Contact_ID;
}
public int getC_BPartner_ID()
{
return C_BPartner_ID;
}
public void setC_BPartner_ID(int C_BPartner_ID)
{
this.C_BPartner_ID = C_BPartner_ID;
}
public int getC_BPartner_Location_ID()
{
return C_BPartner_Location_ID;
}
public void setC_BPartner_Location_ID(int C_BPartner_Location_ID)
{
this.C_BPartner_Location_ID = C_BPartner_Location_ID;
}
public int getC_Greeting_ID()
{
return C_Greeting_ID;
}
public void setC_Greeting_ID(int C_Greeting_ID)
{
this.C_Greeting_ID = C_Greeting_ID;
}
public int getC_GreetingBP_ID()
{
return C_GreetingBP_ID;
}
public void setC_GreetingBP_ID(int C_GreetingBP_ID)
{
this.C_GreetingBP_ID = C_GreetingBP_ID;
}
public int getC_Location_ID()
{
return C_Location_ID;
}
public void setC_Location_ID(int C_Location_ID)
{
this.C_Location_ID = C_Location_ID;
}
//
public String getContact()
{
return Contact;
}
public void setContact(String Contact)
{
if (Contact != null)
this.Contact = Contact;
}
public String getEMail()
{
return EMail;
}
public void setEMail(String EMail)
{
if (EMail != null)
this.EMail = EMail;
}
public String getPassword()
{
return Password;
}
public void setPassword(String Password)
{
if (Password != null)
this.Password = Password;
}
public String getFax()
{
return Fax;
}
public void setFax(String Fax)
{
if (Fax != null)
this.Fax = Fax;
}
public String getName()
{
return Name;
}
public void setName(String Name)
{
if (Name != null)
this.Name = Name;
}
public String getName2()
{
return Name2;
}
public void setName2(String Name2)
{
if (Name2 != null)
this.Name2 = Name2;
}
public String getPhone()
{
return Phone;
}
public void setPhone(String Phone)
{
if (Phone != null)
this.Phone = Phone;
}
public String getPhone2()
{
return Phone2;
}
public void setPhone2(String Phone2)
{
if (Phone2 != null)
this.Phone2 = Phone2;
}
public String getTitle()
{
return Title;
}
public void setTitle(String Title)
{
if (Title != null)
this.Title = Title;
}
public String getValue()
{
return Value;
}
public void setValue(String Value)
{
if (Value != null)
this.Value = Value;
}
} // MCustomer