Files
ayanova7/source/bizobjects/AyaLib/GZTW.AyaNova.BLL/Vendor.cs
2018-06-29 19:47:36 +00:00

1745 lines
45 KiB
C#

///////////////////////////////////////////////////////////
// Vendor.cs
// Implementation of Class Vendor
// CSLA type: Editable Root
// Created on: 07-Jun-2004 8:41:45 AM
// Object design: Joyce
// Coded: John 29-July-2004
///////////////////////////////////////////////////////////
using System;
using System.Data;
using CSLA.Data;
using GZTW.Data;
using CSLA;
using System.Threading;
using CSLA.Security;
namespace GZTW.AyaNova.BLL
{
/// <summary>
/// Supplier of various goods and services depending on type selected.
/// </summary>
[Serializable]
public class Vendor : BusinessBase {
#region Attributes
private bool bReadOnly;
private Guid mID;
private SmartDate mCreated;
private SmartDate mModified;
private bool mActive;
private Guid mCreator;
private Guid mModifier;
private string mName=null;
private string mNotes="";
private string mContactNotes = "";
private AssignedDocs mDocs;
private Address mGoToAddress;
private Address mMailToAddress;
//private Contacts mContacts;
private string mWebAddress="";
private string mAccountNumber="";
private VendorTypes mVendorType;
//Custom fields
private string mCustom1="";
private string mCustom2="";
private string mCustom3="";
private string mCustom4="";
private string mCustom5="";
private string mCustom6="";
private string mCustom7="";
private string mCustom8="";
private string mCustom9="";
private string mCustom0="";
private string mContact = "";
private string mPhone1 = "";
private string mPhone2 = "";
private string mPhone3 = "";
private string mPhone4 = "";
private string mPhone5 = "";
private string mEmail = "";
#endregion
#region Constructor
/// <summary>
/// Private constructor to prevent direct instantiation
/// </summary>
private Vendor()
{
//Set to read / write initially so that properties
//can be set
bReadOnly=false;
//New ID
mID = Guid.NewGuid();
//pre-break the rule
Name="";
Active=true;
mVendorType=VendorTypes.Wholesaler;
//Set record history to defaults
mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime);
mModified=new SmartDate();
mCreator=Guid.Empty;
mModifier=Guid.Empty;
}
#endregion
#region Business properties
/// <summary>
/// <see cref="VendorTypes"/> type of supplier
/// </summary>
public VendorTypes VendorType
{
get
{
return mVendorType;
}
set
{
if(mVendorType!=value)
{
mVendorType = value;
MarkDirty();
}
}
}
/// <summary>
/// Localized text of Vendor type
/// </summary>
public string VendorTypeString
{
get
{
switch(mVendorType)
{
case VendorTypes.Manufacturer:
return LocalizedTextTable.GetLocalizedTextDirect("Vendor.Label.VendorType.Manufacturer");
case VendorTypes.Shipper:
return LocalizedTextTable.GetLocalizedTextDirect("Vendor.Label.VendorType.Shipper");
case VendorTypes.SubContractor:
return LocalizedTextTable.GetLocalizedTextDirect("Vendor.Label.VendorType.SubContractor");
case VendorTypes.ThirdPartyRepair:
return LocalizedTextTable.GetLocalizedTextDirect("Vendor.Label.VendorType.ThirdPartyRepair");
case VendorTypes.Wholesaler:
return LocalizedTextTable.GetLocalizedTextDirect("Vendor.Label.VendorType.Wholesaler");
default:
return "";
}
}
}
/// <summary>
/// Optional identifier
/// </summary>
public string AccountNumber
{
get
{
return mAccountNumber;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mAccountNumber!=value)
{
mAccountNumber = value;
BrokenRules.Assert("AccountNumberLength",
"Error.Object.FieldLengthExceeded255,Vendor.Label.AccountNumber","AccountNumber",value.Length>255);
MarkDirty();
}
}
}
}
/// <summary>
/// Get internal id number Read only property because it's set internally, not
/// externally
/// </summary>
public Guid ID
{
get
{
return mID;
}
}
/// <summary>
/// Get created date
///
///
/// </summary>
public string Created
{
get
{
return mCreated.ToString();
}
}
/// <summary>
/// Get modified date
///
///
/// </summary>
public string Modified
{
get
{
return mModified.ToString();
}
}
/// <summary>
/// Get user record ID of person who created this record
///
///
/// </summary>
public Guid Creator
{
get
{
return mCreator;
}
}
/// <summary>
/// Get user ID of person who modified this record
///
///
/// </summary>
public Guid Modifier
{
get
{
return mModifier;
}
}
/// <summary>
/// Set/get Vendor name
/// </summary>
public string Name
{
get
{
return mName;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mName!=value)
{
mName = value;
BrokenRules.Assert("NameRequired","Error.Object.RequiredFieldEmpty,Vendor.Label.Name","Name",value.Length==0);
BrokenRules.Assert("NameLength",
"Error.Object.FieldLengthExceeded255,Vendor.Label.Name","Name",value.Length>255);
MarkDirty();
}
}
}
}
/// <summary>
/// Get /set active status of Vendor
/// If active = true then Vendor is selectable for workorders etc
/// If active = false then Vendor in not selectable, but history can be viewed
/// </summary>
public bool Active
{
get
{
return mActive;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mActive!=value)
{
mActive = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Get service address for this Vendor
/// </summary>
public Address GoToAddress
{
get
{
return mGoToAddress;
}
}
/// <summary>
/// Get mailing address object for this Vendor
/// Returns GoToAddress object if MailToAddress is empty
/// </summary>
public Address MailToAddress
{
get
{
return mMailToAddress;
}
}
/// <summary>
/// web address of Vendor if applicable
/// can be null
/// ties into hyerlink to web browser
/// </summary>
public string WebAddress
{
get
{
return mWebAddress;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mWebAddress!=value)
{
mWebAddress = value;
BrokenRules.Assert("WebAddressLength",
"Error.Object.FieldLengthExceeded255,Common.Label.WebAddress","WebAddress",value.Length>255);
MarkDirty();
}
}
}
}
/// <summary>
/// Corresponds to Vendors.generalnotes in AyaNova v1
/// </summary>
public string Notes
{
get
{
return mNotes;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mNotes!=value)
{
mNotes = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Collection of <see cref="AssignedDoc"/> objects assigned to this Vendor
/// </summary>
public AssignedDocs Docs
{
get
{
return mDocs;
}
}
///// <summary>
///// Get Contacts collection for this object
///// </summary>
//public Contacts Contacts
//{
// get
// {
// return mContacts;
// }
//}
#region Contact fields
/// <summary>
/// Set/get Vendor Contact
/// </summary>
public string Contact
{
get
{
return mContact;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mContact != value)
{
mContact = value;
BrokenRules.Assert("ContactLength",
"Error.Object.FieldLengthExceeded500,Vendor.Label.Contact",
"Contact", value.Length > 500);
MarkDirty();
}
}
}
}
/// <summary>
/// Set/get Vendor Phone1
/// </summary>
public string Phone1
{
get
{
return mPhone1;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mPhone1 != value)
{
mPhone1 = value;
BrokenRules.Assert("Phone1Length",
"Error.Object.FieldLengthExceeded255,Vendor.Label.Phone1",
"Phone1", value.Length > 255);
MarkDirty();
}
}
}
}
/// <summary>
/// Set/get Vendor Phone2
/// </summary>
public string Phone2
{
get
{
return mPhone2;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mPhone2 != value)
{
mPhone2 = value;
BrokenRules.Assert("Phone2Length",
"Error.Object.FieldLengthExceeded255,Vendor.Label.Phone2",
"Phone2", value.Length > 255);
MarkDirty();
}
}
}
}
/// <summary>
/// Set/get Vendor Phone3
/// </summary>
public string Phone3
{
get
{
return mPhone3;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mPhone3 != value)
{
mPhone3 = value;
BrokenRules.Assert("Phone3Length",
"Error.Object.FieldLengthExceeded255,Vendor.Label.Phone3",
"Phone3", value.Length > 255);
MarkDirty();
}
}
}
}
/// <summary>
/// Set/get Vendor Phone4
/// </summary>
public string Phone4
{
get
{
return mPhone4;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mPhone4 != value)
{
mPhone4 = value;
BrokenRules.Assert("Phone4Length",
"Error.Object.FieldLengthExceeded255,Vendor.Label.Phone4",
"Phone4", value.Length > 255);
MarkDirty();
}
}
}
}
/// <summary>
/// Set/get Vendor Phone5
/// </summary>
public string Phone5
{
get
{
return mPhone5;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mPhone5 != value)
{
mPhone5 = value;
BrokenRules.Assert("Phone5Length",
"Error.Object.FieldLengthExceeded255,Vendor.Label.Phone5",
"Phone5", value.Length > 255);
MarkDirty();
}
}
}
}
/// <summary>
/// Set/get Vendor Email
/// </summary>
public string Email
{
get
{
return mEmail;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mEmail != value)
{
mEmail = value;
BrokenRules.Assert("EmailLength",
"Error.Object.FieldLengthExceeded255,Vendor.Label.Email",
"Email", value.Length > 255);
MarkDirty();
}
}
}
}
/// <summary>
/// Contact Notes / Other contacts
/// </summary>
public string ContactNotes
{
get
{
return mContactNotes;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mContactNotes != value)
{
mContactNotes = value;
MarkDirty();
}
}
}
}
#endregion contact fields
//CUSTOM FIELDS
/// <summary>
/// Custom1
/// </summary>
public string Custom1
{
get
{
return mCustom1;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom1!=value)
{
mCustom1 = value;
BrokenRules.Assert("Custom1Length",
"Error.Object.FieldLengthExceeded500,Vendor.Label.Custom1","Custom1",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom2
/// </summary>
public string Custom2
{
get
{
return mCustom2;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom2!=value)
{
mCustom2 = value;
BrokenRules.Assert("Custom2Length",
"Error.Object.FieldLengthExceeded500,Vendor.Label.Custom2","Custom2",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom3
/// </summary>
public string Custom3
{
get
{
return mCustom3;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom3!=value)
{
mCustom3 = value;
BrokenRules.Assert("Custom3Length",
"Error.Object.FieldLengthExceeded500,Vendor.Label.Custom3","Custom3",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom4
/// </summary>
public string Custom4
{
get
{
return mCustom4;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom4!=value)
{
mCustom4 = value;
BrokenRules.Assert("Custom4Length",
"Error.Object.FieldLengthExceeded500,Vendor.Label.Custom4","Custom4",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom5
/// </summary>
public string Custom5
{
get
{
return mCustom5;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom5!=value)
{
mCustom5 = value;
BrokenRules.Assert("Custom5Length",
"Error.Object.FieldLengthExceeded500,Vendor.Label.Custom5","Custom5",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom6
/// </summary>
public string Custom6
{
get
{
return mCustom6;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom6!=value)
{
mCustom6 = value;
BrokenRules.Assert("Custom6Length",
"Error.Object.FieldLengthExceeded500,Vendor.Label.Custom6","Custom6",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom7
/// </summary>
public string Custom7
{
get
{
return mCustom7;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom7!=value)
{
mCustom7 = value;
BrokenRules.Assert("Custom7Length",
"Error.Object.FieldLengthExceeded500,Vendor.Label.Custom7","Custom7",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom8
/// </summary>
public string Custom8
{
get
{
return mCustom8;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom8!=value)
{
mCustom8 = value;
BrokenRules.Assert("Custom8Length",
"Error.Object.FieldLengthExceeded500,Vendor.Label.Custom8","Custom8",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom9
/// </summary>
public string Custom9
{
get
{
return mCustom9;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom9!=value)
{
mCustom9 = value;
BrokenRules.Assert("Custom9Length",
"Error.Object.FieldLengthExceeded500,Vendor.Label.Custom9","Custom9",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom0
/// </summary>
public string Custom0
{
get
{
return mCustom0;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom0!=value)
{
mCustom0 = value;
BrokenRules.Assert("Custom0Length",
"Error.Object.FieldLengthExceeded500,Vendor.Label.Custom0","Custom0",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Get all phone numbers and email address as one string
/// </summary>
/// <returns></returns>
public string GetPrimaryContactDefaultContactInfo()
{
System.Text.StringBuilder b = new System.Text.StringBuilder();
b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("Vendor.Label.Phone1") + ": ", Phone1, "\r\n"));
b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("Vendor.Label.Phone2") + ": ", Phone2, "\r\n"));
b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("Vendor.Label.Phone3") + ": ", Phone3, "\r\n"));
b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("Vendor.Label.Phone4") + ": ", Phone4, "\r\n"));
b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("Vendor.Label.Phone5") + ": ", Phone5, "\r\n"));
b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("Vendor.Label.Email") + ": ", Email, "\r\n"));
return b.ToString();
}
/// <summary>
/// Flag - indicates if current user can open the wiki page for this object
/// See <see cref="WikiPage.ShowWikiLink(RootObjectTypes, Guid)"/> method for details
///
/// This is cached for the lifetime of this object
/// </summary>
public bool CanWiki//case 73
{
get
{
if (!bCanWiki.HasValue)
bCanWiki = WikiPage.ShowWikiLink(RootObjectTypes.Vendor, mID);
return bCanWiki.Value;
}
}
//cache the result in case the UI calls this repeatedly
private bool? bCanWiki = null;
/// <summary>
/// Throw an error when a read only user
/// tries to set a property
/// (this should normally never be called unless someone is using the developer api since the UI
/// should prevent it from happening initially)
/// </summary>
private void ThrowSetError()
{
throw new System.Security.SecurityException
(
string.Format
(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToChange"),
LocalizedTextTable.GetLocalizedTextDirect("O.Vendor")
)
);
}
#endregion
#region System.object overrides
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "Vendor" + mID.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(Object obj)
{
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
Vendor c=(Vendor)obj;
return mID==c.mID;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return ("Vendor"+mID).GetHashCode();
}
#endregion
#region Searching
/// <summary>
/// Returns a search result object based on search terms
/// for the ID specified
/// </summary>
/// <param name="ID"></param>
/// <param name="searchTerms"></param>
/// <returns></returns>
public static SearchResult GetSearchResult(Guid ID, string[]searchTerms)
{
if(AyaBizUtils.Right("Object.Vendor")<(int)SecurityLevelTypes.ReadOnly)
return new SearchResult();
SearchResult sr=new SearchResult();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
SafeDataReader dr = null;
try
{
dr=DBUtil.GetReaderFromSQLString(
"SELECT aCreated, aModified, aCreator, aModifier, aName, " +
"aContactNotes,aContact,aPhone1,aPhone2,aPhone3,aPhone4,aPhone5,aEmail, " +
" aWebAddress, aNotes, AACCOUNTNUMBER, aCustom1, aCustom2, " +
" aCustom3, aCustom4, aCustom5, aCustom6, aCustom7, aCustom8, " +
" aCustom9, aCustom0 FROM aVendor WHERE (aID " +
"= @ID)"
,ID);
if(!dr.Read())
return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for VendorID: " + ID.ToString());
sr.Description=dr.GetString("aName");
sb.Append(sr.Description);
sb.Append(" ");
sb.Append(dr.GetString("aWebAddress"));
sb.Append(" ");
sb.Append(dr.GetString("AACCOUNTNUMBER"));
sb.Append(" ");
sb.Append(dr.GetString("aNotes"));
sb.Append(" ");
sb.Append(dr.GetString("aContactNotes"));
sb.Append(" ");
sb.Append(dr.GetString("aContact"));
sb.Append(" ");
sb.Append(dr.GetString("aPhone1"));
sb.Append(" ");
sb.Append(dr.GetString("aPhone2"));
sb.Append(" ");
sb.Append(dr.GetString("aPhone3"));
sb.Append(" ");
sb.Append(dr.GetString("aPhone4"));
sb.Append(" ");
sb.Append(dr.GetString("aPhone5"));
sb.Append(" ");
sb.Append(dr.GetString("aEmail"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom0"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom1"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom2"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom3"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom4"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom5"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom6"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom7"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom8"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom9"));
sr.Created=DBUtil.ToLocal(dr.GetSmartDate("aCreated"));
sr.Modified=DBUtil.ToLocal(dr.GetSmartDate("aModified"));
sr.Creator=dr.GetGuid("aCreator");
sr.Modifier=dr.GetGuid("aModifier");
}
finally
{
if(dr!=null) dr.Close();
}
//Added 02-Aug-2006 was not returning address search results :(
#region Address block
try
{
dr = DBUtil.GetReaderFromSQLString(
"SELECT adeliveryaddress, acity, astateprov, acountrycode, acountry, apostal " +
"from aaddress " +
"where " +
"arootobjectid=@ID "
, ID);
while (dr.Read())
{
sb.Append(" ");
sb.Append(dr.GetString("adeliveryaddress"));
sb.Append(" ");
sb.Append(dr.GetString("acity"));
sb.Append(" ");
sb.Append(dr.GetString("astateprov"));
sb.Append(" ");
sb.Append(dr.GetString("acountrycode"));
sb.Append(" ");
sb.Append(dr.GetString("acountry"));
sb.Append(" ");
sb.Append(dr.GetString("apostal"));
}
}
finally
{
if (dr != null) dr.Close();
}
#endregion
//Formulate results
ExtractAndRank er = new ExtractAndRank();
er.Process(sb.ToString().Trim(),searchTerms);
sr.Extract=er.Extract;
sr.Rank=er.Ranking;
sr.AncestorRootObjectID=ID;
sr.AncestorRootObjectType=RootObjectTypes.Vendor;
return sr;
}
#endregion
#region Static methods
/// <summary>
/// Create new Vendor
/// </summary>
/// <returns>Vendor</returns>
public static Vendor NewItem()
{
Vendor c;
if(AyaBizUtils.Right("Object.Vendor")>(int)SecurityLevelTypes.ReadOnly)
{
c = new Vendor();
c.mGoToAddress=Address.NewItem();
c.mGoToAddress.AddressType=AddressTypes.Physical;
c.mGoToAddress.RootObjectID=c.mID;
c.mGoToAddress.RootObjectType=RootObjectTypes.Vendor;
c.mMailToAddress=Address.NewItem();
c.mMailToAddress.AddressType=AddressTypes.Postal;
c.mMailToAddress.RootObjectID=c.mID;
c.mMailToAddress.RootObjectType=RootObjectTypes.Vendor;
//c.mContacts=Contacts.NewItems();
c.mDocs=AssignedDocs.NewItems();
return c;
}
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"),
LocalizedTextTable.GetLocalizedTextDirect("O.Vendor")));
}
/// <summary>
/// Fetch Vendor by ID
/// </summary>
/// <param name="_ID"></param>
/// <returns></returns>
public static Vendor GetItem(Guid _ID)
{
return GetItemMRU(_ID, true);
}
/// <summary>
/// Fetch Vendor by ID and do not track in <see cref="UserMRU"/> system
/// </summary>
/// <param name="_ID"></param>
/// <returns></returns>
public static Vendor GetItemNoMRU(Guid _ID)
{
return GetItemMRU(_ID, false);
}
/// <summary>
/// Fetch vendor, optionally track in MRU system
/// See <see cref="UserMRU"/>
/// </summary>
/// <param name="_ID"></param>
/// <param name="TrackMRU"></param>
/// <returns></returns>
public static Vendor GetItemMRU(Guid _ID, bool TrackMRU)
{
if (_ID == AyaBizUtils.NewObjectGuid)
return NewItem();
if (AyaBizUtils.Right("Object.Vendor") > (int)SecurityLevelTypes.NoAccess)
return (Vendor)DataPortal.Fetch(new Criteria(_ID, TrackMRU));
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"),
LocalizedTextTable.GetLocalizedTextDirect("O.Vendor")));
}
/// <summary>
/// Delete Vendor
/// </summary>
/// <param name="_ID">Vendor GUID</param>
public static void DeleteItem(Guid _ID)
{
if(AyaBizUtils.Right("Object.Vendor")>(int)SecurityLevelTypes.ReadWrite)
DataPortal.Delete(new Criteria(_ID,true));
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToDelete"),
LocalizedTextTable.GetLocalizedTextDirect("O.Vendor")));
}
/// <summary>
/// Check for the existance of a Vendor
/// in the database by ID OR by Name
///
///
/// </summary>
/// <param name="ID">Guid of Vendor</param>
/// <param name="Name">Name of Vendor</param>
public static bool Exists(Guid ID, string Name)
{
//Case 505
return VendorExistanceChecker.VendorExists(ID, Name);
}
/// <summary>
/// Retrieve internal ID from name.
///
/// </summary>
/// <param name="Name">Text value</param>
/// <returns>Guid ID value or Guid.Empty if no match</returns>
public static Guid GetIDFromName(string Name)
{
return GuidFetcher.GetItem("AVENDOR", "ANAME", Name);
}
/// <summary>
/// Retrieve internal ID from AccountNumber.
///
/// </summary>
/// <param AccountNumber="AccountNumber">Text value</param>
/// <returns>Guid ID value or Guid.Empty if no match</returns>
public static Guid GetIDFromAccountNumber(string AccountNumber)
{
return GuidFetcher.GetItem("AVENDOR", "AACCOUNTNUMBER", AccountNumber);
}
#endregion
#region DAL DATA ACCESS
#region Fetch
///
/// <param name="Criteria"></param>
protected override void DataPortal_Fetch(object Criteria)
{
//set to false to load items initially
bReadOnly=false;
Criteria crit = (Criteria)Criteria;
SafeDataReader dr = null;
try
{
dr=DBUtil.GetReaderFromSQLString("SELECT * FROM aVendor WHERE aID=@ID;",crit.ID);
if(!dr.Read())
DBUtil.ThrowFetchError("Vendor ID: " + crit.ID.ToString());
//Standard fields
mID=dr.GetGuid("aID");
mCreated=DBUtil.ToLocal(dr.GetSmartDate("aCreated"));
mModified=DBUtil.ToLocal(dr.GetSmartDate("aModified"));
mCreator=dr.GetGuid("aCreator");
mModifier=dr.GetGuid("aModifier");
//Custom fields
mCustom1=dr.GetString("aCustom1");
mCustom2=dr.GetString("aCustom2");
mCustom3=dr.GetString("aCustom3");
mCustom4=dr.GetString("aCustom4");
mCustom5=dr.GetString("aCustom5");
mCustom6=dr.GetString("aCustom6");
mCustom7=dr.GetString("aCustom7");
mCustom8=dr.GetString("aCustom8");
mCustom9=dr.GetString("aCustom9");
mCustom0=dr.GetString("aCustom0");
//Vendor fields
Active=dr.GetBoolean("AACTIVE");
//Important: use property not internal field
//so that initial broken rule is unbroken on fetch
Name=dr.GetString("aName");
WebAddress=dr.GetString("aWebAddress");
Notes=dr.GetString("aNotes");
ContactNotes = dr.GetString("aContactNotes");
mContact = dr.GetString("aContact");
mPhone1 = dr.GetString("aPhone1");
mPhone2 = dr.GetString("aPhone2");
mPhone3 = dr.GetString("aPhone3");
mPhone4 = dr.GetString("aPhone4");
mPhone5 = dr.GetString("aPhone5");
mEmail = dr.GetString("aEmail");
mAccountNumber=dr.GetString("AACCOUNTNUMBER");
mVendorType=(VendorTypes)dr.GetInt16("aVendorType");
if(dr!=null) dr.Close();
//Children:
//GoToAddress
dr=DBUtil.GetReaderFromSQLString("SELECT * " +
"FROM AADDRESS WHERE aRootObjectID=@ID AND " +
"aRootObjectType=4 AND AADDRESSTYPE=2",crit.ID);
if(dr.Read())
mGoToAddress=Address.GetItem(dr);
if(dr!=null) dr.Close();
//MailToAddress
dr=DBUtil.GetReaderFromSQLString("SELECT * " +
"FROM AADDRESS WHERE aRootObjectID=@ID AND " +
"aRootObjectType=4 AND AADDRESSTYPE=1",crit.ID);
if(dr.Read())
mMailToAddress=Address.GetItem(dr);
if(dr!=null) dr.Close();
////Contacts
//dr=DBUtil.GetReaderFromSQLString("SELECT * " +
// "FROM aContact WHERE aRootObjectID=@ID AND " +
// "aRootObjectType=4",crit.ID);
//mContacts=Contacts.GetItems(dr, RootObjectTypes.Vendor);
//if(dr!=null) dr.Close();
//Docs
dr=DBUtil.GetReaderFromSQLString("SELECT * FROM AASSIGNEDDOC WHERE (aRootObjectID=@ID and aRootObjectType=4);",crit.ID);
mDocs = AssignedDocs.GetItems(dr, RootObjectTypes.Vendor, RootObjectTypes.Vendor);
if(dr!=null) dr.Close();
}
finally
{
if(dr!=null) dr.Close();
}
MarkOld();
if(crit.TrackMRU)
if(AyaBizUtils.AllowAutomaticMRUOnUpdate) AyaBizUtils.MRU.Add(RootObjectTypes.Vendor, crit.ID);
//Get access rights level
bReadOnly=AyaBizUtils.Right("Object.Vendor")<(int)SecurityLevelTypes.ReadWrite;
}
#endregion
#region Update
/// <summary>
/// Called by DataPortal to delete/add/update data into the database
/// </summary>
protected override void DataPortal_Update()
{
// If not a new record, check if record was modified
//by another user since original retrieval:
if(!IsNew)
DBUtil.CheckSafeToUpdate(this.mModified.Date,this.mID,"aVendor");
#region Delete
if(IsDeleted)
{
if(!IsNew)
{
//Delete object and child objects
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aVendor WHERE aID = @ID;");
cmDelete.AddInParameter("@ID",DbType.Guid,this.mID);
//Delete all address record types with matching root object ID
DBCommandWrapper cmDeleteAddress = DBUtil.GetCommandFromSQL("DELETE FROM AADDRESS WHERE aRootObjectID = @ID;");
cmDeleteAddress.AddInParameter("@ID",DbType.Guid,this.mID);
using (IDbConnection connection = DBUtil.DB.GetConnection())
{
connection.Open();
IDbTransaction transaction = connection.BeginTransaction();
try
{
//Contacts.DeleteItems(this.mID,transaction);
DBUtil.RemoveKeywords(transaction,RootObjectTypes.Vendor,this.mID);
DBUtil.RemoveDocs(transaction,RootObjectTypes.Vendor,this.mID);
DBUtil.DB.ExecuteNonQuery(cmDeleteAddress, transaction);
DBUtil.DB.ExecuteNonQuery(cmDelete, transaction);
// Commit the transaction
transaction.Commit();
}
catch
{
// Rollback transaction
transaction.Rollback();
throw;
}
finally
{
connection.Close();
}
}
//-----------------------------
}
MarkNew();
return;
}
#endregion
#region Add / Update
//get modification time temporarily, if update succeeds then
//set to this time
System.DateTime dtModified = DBUtil.CurrentWorkingDateTime;
DBCommandWrapper cm = null;
if(IsNew)//Add or update?
cm=DBUtil.GetCommandFromSQL(
"INSERT INTO aVendor (aID, AACTIVE, aName, aWebAddress, aVendorType, aNotes, aContactNotes, aContact, aPhone1, aPhone2, aPhone3, aPhone4, aPhone5, aEmail, " +
"AACCOUNTNUMBER, aCreated, aModified,aCreator,aModifier, " +
"aCustom1, aCustom2, aCustom3, aCustom4, aCustom5, aCustom6, aCustom7, aCustom8, aCustom9, aCustom0) " +
"VALUES (@ID,@Active,@Name,@WebAddress,@VendorType,@Notes, @ContactNotes, @Contact, @Phone1, @Phone2, @Phone3, @Phone4, @Phone5, @Email, " +
"@AccountNumber,@Created,@Modified,@CurrentUserID,@CurrentUserID, " +
"@Custom1,@Custom2,@Custom3,@Custom4,@Custom5,@Custom6,@Custom7,@Custom8,@Custom9,@Custom0)"
);
else
cm=DBUtil.GetCommandFromSQL(
"UPDATE aVendor SET aID=@ID, AACTIVE=@Active, aName=@Name, " +
"aWebAddress=@WebAddress, aVendorType=@VendorType, " +
"aNotes=@Notes, AACCOUNTNUMBER=@AccountNumber, " +
"aModifier=@CurrentUserID, aModified=@Modified, " +
"aCustom1=@Custom1, aCustom2=@Custom2, aCustom3=@Custom3, " +
"aCustom4=@Custom4, aCustom5=@Custom5, " +
"aCustom6=@Custom6, aCustom7=@Custom7, aCustom8=@Custom8, " +
"aCustom9=@Custom9, aCustom0=@Custom0, " +
"aContactNotes=@ContactNotes, " +
"aContact=@Contact, aPhone1=@Phone1, aPhone2=@Phone2, aPhone3=@Phone3, aPhone4=@Phone4, aPhone5=@Phone5, aEmail=@Email " +
"WHERE aID=@ID"
);
//Vendor specific
cm.AddInParameter("@ID",DbType.Guid,mID);
cm.AddInParameter("@Active",DbType.Boolean, mActive);
cm.AddLargeStringInParameter("@Notes", mNotes);
cm.AddInParameter("@Name",DbType.String, mName);
cm.AddInParameter("@WebAddress",DbType.String, mWebAddress);
cm.AddInParameter("@AccountNumber",DbType.String, mAccountNumber);
cm.AddInParameter("@VendorType",DbType.Int16, (int)mVendorType);
cm.AddLargeStringInParameter("@ContactNotes", mContactNotes);
cm.AddInParameter("@Contact", DbType.String, mContact);
cm.AddInParameter("@Phone1", DbType.String, mPhone1);
cm.AddInParameter("@Phone2", DbType.String, mPhone2);
cm.AddInParameter("@Phone3", DbType.String, mPhone3);
cm.AddInParameter("@Phone4", DbType.String, mPhone4);
cm.AddInParameter("@Phone5", DbType.String, mPhone5);
cm.AddInParameter("@Email", DbType.String, mEmail);
//Standard fields
//Change for case 432 - Vendor is the only contact linked object
//that has not null specified for creator, modifier fields for some odd reason
//causing not logged in db schema update to fail when xferring contact data
if (CurrentUserID != Guid.Empty)
cm.AddInParameter("@CurrentUserID", DbType.Guid, CurrentUserID);
else
cm.AddInParameter("@CurrentUserID", DbType.Guid, User.AdministratorID);
cm.AddInParameter("@Created",DbType.DateTime, DBUtil.ToUTC(mCreated).DBValue);
cm.AddInParameter("@Modified",DbType.DateTime, DBUtil.ToUTC(dtModified));
//Custom fields
cm.AddLargeStringInParameter("@Custom1", mCustom1);
cm.AddLargeStringInParameter("@Custom2", mCustom2);
cm.AddLargeStringInParameter("@Custom3", mCustom3);
cm.AddLargeStringInParameter("@Custom4", mCustom4);
cm.AddLargeStringInParameter("@Custom5", mCustom5);
cm.AddLargeStringInParameter("@Custom6", mCustom6);
cm.AddLargeStringInParameter("@Custom7", mCustom7);
cm.AddLargeStringInParameter("@Custom8", mCustom8);
cm.AddLargeStringInParameter("@Custom9", mCustom9);
cm.AddLargeStringInParameter("@Custom0", mCustom0);
using (IDbConnection connection = DBUtil.DB.GetConnection())
{
connection.Open();
IDbTransaction transaction = connection.BeginTransaction();
try
{
DBUtil.DB.ExecuteNonQuery(cm, transaction);
//Update child objects
//GoToAddress
mGoToAddress.Update(RootObjectTypes.Vendor,mID,AddressTypes.Physical,transaction);
//MailToAddress
mMailToAddress.Update(RootObjectTypes.Vendor,mID,AddressTypes.Postal,transaction);
//Contacts
//mContacts.Update(mID,RootObjectTypes.Vendor,transaction);
//Docs
mDocs.Update(transaction);
//Process keywords
DBUtil.ProcessKeywords(transaction,this.mID,RootObjectTypes.Vendor,IsNew,AyaBizUtils.Break(false,
mAccountNumber,mGoToAddress.FullAddress,mMailToAddress.FullAddress,
mName,mNotes,mContactNotes,mWebAddress,
mContact,mPhone1,mPhone2,mPhone3,mPhone4,mPhone5,mEmail,
/*Custom fields*/
mCustom1,mCustom2,mCustom3,mCustom4,mCustom5,mCustom6,mCustom7,mCustom8,mCustom9,mCustom0));
MarkOld();//db is now synched with object
// Commit the transaction
transaction.Commit();
}
catch
{
// Rollback transaction
transaction.Rollback();
throw;
}
finally
{
connection.Close();
}
//Successful update so
//change modification time to match
this.mModified.Date=dtModified;
}
#endregion
if(AyaBizUtils.AllowAutomaticMRUOnUpdate) AyaBizUtils.MRU.Add(RootObjectTypes.Vendor, mID);
}
#endregion
#region Delete
/// <summary>
/// Remove a Vendor record .
/// </summary>
/// <param name="Criteria"></param>
protected override void DataPortal_Delete(object Criteria)
{
Criteria crit = (Criteria)Criteria;
//Delete object and child objects
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aVendor WHERE aID = @ID;");
cmDelete.AddInParameter("@ID",DbType.Guid,crit.ID);
//Delete all address record types with matching root object ID
DBCommandWrapper cmDeleteAddress = DBUtil.GetCommandFromSQL("DELETE FROM AADDRESS WHERE aRootObjectID = @ID;");
cmDeleteAddress.AddInParameter("@ID",DbType.Guid,crit.ID);
using (IDbConnection connection = DBUtil.DB.GetConnection())
{
connection.Open();
IDbTransaction transaction = connection.BeginTransaction();
try
{
//Contacts.DeleteItems(crit.ID,transaction);
DBUtil.RemoveKeywords(transaction,RootObjectTypes.Vendor,crit.ID);
DBUtil.RemoveDocs(transaction,RootObjectTypes.Vendor,crit.ID);
DBUtil.DB.ExecuteNonQuery(cmDeleteAddress, transaction);
DBUtil.DB.ExecuteNonQuery(cmDelete, transaction);
// Commit the transaction
transaction.Commit();
}
catch
{
// Rollback transaction
transaction.Rollback();
throw;
}
finally
{
connection.Close();
}
AyaBizUtils.MRU.Remove(RootObjectTypes.Vendor, crit.ID);
}
}
#endregion delete
#endregion
#region Override IsValid / IsDirty
//Override base class version if there are child objects
/// <summary>
///
/// </summary>
public override bool IsValid
{
get
{
return base.IsValid &&
mGoToAddress.IsValid && mMailToAddress.IsValid && mDocs.IsValid;
}
}
/// <summary>
///
/// </summary>
public override bool IsDirty
{
get
{
return base.IsDirty ||
mGoToAddress.IsDirty || mMailToAddress.IsDirty || mDocs.IsDirty;
}
}
#endregion
#region criteria
/// <summary>
/// Criteria for identifying existing object
/// </summary>
[Serializable]
private class Criteria
{
public Guid ID;
public bool TrackMRU;
public Criteria(Guid _ID, bool _TrackMRU)
{
ID = _ID;
TrackMRU = _TrackMRU;
}
}
#endregion
#region Exists
//Case 505
//[Serializable(), System.ComponentModel.Browsable(false)]
// public class ExistsHelper
//{
// bool _Exists = false;
// Guid _ID;
// string _Name;
// public ExistsHelper(Guid ID, string Name)
// {
// _ID=ID;
// _Name=Name;
// }
// public static bool GetExists(Guid ID, string Name)
// { //todo fix for 425
// ExistsHelper e=new ExistsHelper(ID,Name);
// DataPortal.Update(e);
// return e._Exists;
// }
// public void DataPortal_Update()
// {
// if(_Name!=null && _Name!="")
// {
// DBCommandWrapper dbCommandWrapper = DBUtil.DB.GetSqlStringCommandWrapper(
// "SELECT aID FROM aVendor WHERE " +
// "(aName = @ANAME)"
// );
// dbCommandWrapper.AddInParameter("@ANAME",DbType.String,_Name);
// if(DBUtil.ToGuid(DBUtil.DB.ExecuteScalar(dbCommandWrapper))==Guid.Empty)
// this._Exists=false;
// else
// this._Exists=true;
// }
// else
// {
// if(DBUtil.ToGuid(DBUtil.GetScalarFromSQLString(
// "SELECT aID FROM aVendor WHERE " +
// "(aID = @ID)",_ID
// ))==Guid.Empty)
// this._Exists=false;
// else
// this._Exists=true;
// }
// }
//}
#endregion
}//end Vendor
}//end namespace GZTW.AyaNova.BLL