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

654 lines
18 KiB
C#

///////////////////////////////////////////////////////////
// PartWarehouse.cs
// Implementation of Class PartWarehouse
// CSLA type: Editable Child
// Created on: 07-Jun-2004 8:41:29 AM
// Object design: Joyce
// Coded: John 04-Nov-2004
///////////////////////////////////////////////////////////
using System;
using System.Data;
using CSLA.Data;
using GZTW.Data;
using CSLA;
using System.Threading;
using CSLA.Security;
using System.ComponentModel;
namespace GZTW.AyaNova.BLL {
/// <summary>
/// PartWarehouse class is used to group parts for inventory purposes.
/// Often service company's will have more than one warehouse or consider a service van as a warehouse
/// AyaNova allows any number of warehouses for separate inventory tracking.
/// </summary>
[Serializable]
public class PartWarehouse : BusinessBase {
#region Attributes
private bool bReadOnly;
private Guid mID;
private string mName=null;
private SmartDate mCreated;
private SmartDate mModified;
private bool mActive;
private Guid mCreator;
private Guid mModifier;
private string mDescription="";
//case 58
private Guid mRegionID;
#endregion
#region Constructor
/// <summary>
/// Private constructor to prevent direct instantiation
/// </summary>
private PartWarehouse()
{
MarkAsChild();
//Set to read / write initially so that properties
//can be set
bReadOnly=false;
//New ID
mID = Guid.NewGuid();
//prebreak
Name="";
Active=true;
//Set record history to defaults
mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime);
mModified=new SmartDate();
mCreator=Guid.Empty;
mModifier=Guid.Empty;
//Built-in "Default" region
mRegionID = Region.DefaultRegionID;//case 58
}
#endregion
#region Business properties
/// <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 client group name
/// </summary>
public string Name
{
get
{
return mName;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mName!=value)
{
mName = value;
BrokenRules.Assert("NameRequired","Error.Object.RequiredFieldEmpty,PartWarehouse.Label.Name","Name",value.Length==0);
BrokenRules.Assert("NameLength",
"Error.Object.FieldLengthExceeded255,PartWarehouse.Label.Name","Name",value.Length>255);
MarkDirty();
}
}
}
}
/// <summary>
/// Get /set active status of client group
/// If active = true then PartWarehouse is selectable for workorders etc
/// If active = false then PartWarehouse in not selectable, but history can be
/// viewed
/// </summary>
public bool Active
{
get
{
return mActive;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mActive!=value)
{
//Changed 13-June-2006 Default warehouse must always remain active
//Decided to do this rather than throwing an exception
//If user trying to set default warehouse from active to inactive
//it ignores the change
if (mActive == true && this.ID == DefaultWarehouseID && value == false)
return;
else
{
mActive = value;
MarkDirty();
}
}
}
}
}
/// <summary>
/// Human readable description of warehouse i.e. "Norm's van" or "5th Street warehouse"
/// </summary>
public string Description
{
get
{
return mDescription;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mDescription!=value)
{
mDescription = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Limit to specific region or available to all regions using Region.DefaultRegionID
/// </summary>
public Guid RegionID
{
get
{
return mRegionID;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mRegionID != value)
{
mRegionID = value;
BrokenRules.Assert("RegionIDRequired",
"Error.Object.RequiredFieldEmpty,O.Region",
"RegionID", value == Guid.Empty);
MarkDirty();
}
}
}
}
/// <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.PartWarehouse")
)
);
}
#endregion
#region System.Object overrides
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "PartWarehouse" + mID.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(Object obj)
{
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
PartWarehouse c=(PartWarehouse)obj;
return mID==c.mID;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return ("PartWarehouse"+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.PartWarehouse")<(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 aRegionID, aCreated, aModified, aCreator, aModifier, aName, " +
"aDescription FROM aPartWarehouse WHERE (aID = @ID)"
,ID);
if(!dr.Read())
return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for PartWarehouseID: " + ID.ToString());
if (!AyaBizUtils.InYourRegion(dr.GetGuid("aRegionID"))) return new SearchResult();//case 58
sr.Description=dr.GetString("aName");
sb.Append(sr.Description);
sb.Append(" ");
sb.Append(dr.GetString("aDescription"));
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();
}
//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.PartWarehouse;
return sr;
}
#endregion
#region Static methods
/// <summary>
/// The one and only set identification number for the default built in warehouse in AyaNova
/// All other warehouses added later will have unique ID's
/// </summary>
public static Guid DefaultWarehouseID
{
get
{
return new Guid("{FECD26B2-26A4-471B-B68B-54B642029943}");
}
}
/// <summary>
/// Create new PartWarehouse
/// </summary>
internal static PartWarehouse NewItem()
{
if(AyaBizUtils.Right("Object.PartWarehouse")>(int)SecurityLevelTypes.ReadOnly)
{
PartWarehouse c = new PartWarehouse();
return c;
}
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"),
LocalizedTextTable.GetLocalizedTextDirect("O.PartWarehouse")));
}
/// <summary>
/// Fetch existing PartWarehouse
/// </summary>
/// <param name="dr"></param>
internal static PartWarehouse GetItem(SafeDataReader dr)
{
if(AyaBizUtils.Right("Object.PartWarehouse")>(int)SecurityLevelTypes.NoAccess)
{
PartWarehouse child = new PartWarehouse();
child.Fetch(dr);
return child;
}
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"),
LocalizedTextTable.GetLocalizedTextDirect("O.PartWarehouse")));
}
/// <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("APARTWAREHOUSE", "ANAME", Name);
}
//case 2082 helper
/// <summary>
/// Provides a list of regionally accessible, Active, warehouses for user or region specified
/// </summary>
/// <param name="UserId">If provided and not Guid.empty, fetches the selected region for the user id provided, if not then the RegionId paramter is used</param>
/// <param name="RegionId">If UserId==Guid.empty uses this value as the region to compare to</param>
///
/// <returns>Guid list of accessible warehouses</returns>
public static System.Collections.Generic.List<Guid> UserValidWarehouseIdList(Guid UserId, Guid RegionId)
{
if (UserId == Guid.Empty && RegionId == Guid.Empty)
throw new System.ArgumentOutOfRangeException("UserId or RegionId must be specified");
System.Collections.Generic.List<Guid> ret = new System.Collections.Generic.List<Guid>();
PartWarehousePickList pl = PartWarehousePickList.GetList(false);
Guid checkRegionId = RegionId;
if (UserId != Guid.Empty)
{
checkRegionId = UserRegionIDFetcher.UserRegion(UserId);
}
foreach (PartWarehousePickList.PartWarehousePickListInfo i in pl)
{
if ((checkRegionId == Region.DefaultRegionID ||//If the check region is the default region it works everywhere
i.RegionID == checkRegionId ||//if the check region is the warehouse region it's good
i.RegionID == Region.DefaultRegionID) && //if the warenouse region is the default region it works everywhere
i.Active)
{
ret.Add(i.ID);
}
}
return ret;
}
#endregion
#region DAL DATA ACCESS
/// <summary>
/// Populate this object from the values in the datareader passed to it
/// </summary>
/// <param name="dr"></param>
private void Fetch(SafeDataReader dr)
{
//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");
//PartWarehouse fields
Active=dr.GetBoolean("AACTIVE");
//Important: use property not internal field
//so that initial broken rule is unbroken on fetch
Name=dr.GetString("aName");
Description=dr.GetString("aDescription");
//case 58
mRegionID = dr.GetGuid("aRegionID");
MarkOld();
//Get access rights level
bReadOnly=AyaBizUtils.Right("Object.PartWarehouse")<(int)SecurityLevelTypes.ReadWrite;
}
/// <summary>
///
/// </summary>
/// <param name="tr"></param>
internal void Update(IDbTransaction tr)
{
//No need to update if there is nothing changed
if(!this.IsDirty) return;
// 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,"aPartWarehouse");
#region Delete
if(IsDeleted)
{
throw new System.ApplicationException
(
string.Format
(
LocalizedTextTable.GetLocalizedTextDirect("Error.Object.NotDeleteable"),
LocalizedTextTable.GetLocalizedTextDirect("O.Warehouse")
)
);
}
#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 aPartWarehouse (aID, AACTIVE, aName, aDescription, " +
"aCreated,aModified,aCreator,aModifier, aRegionID) VALUES (@ID,@Active, " +
"@Name,@Description,@Created,@Modified,@CurrentUserID,@CurrentUserID, @RegionID)"
);
else
cm=DBUtil.GetCommandFromSQL(
"UPDATE aPartWarehouse SET aID=@ID, AACTIVE=@Active, " +
"aName=@Name, aDescription=@Description, aModifier=@CurrentUserID, " +
"aModified=@Modified, aRegionID=@RegionID WHERE aID=@ID"
);
//PartWarehouse fields
cm.AddInParameter("@Active",DbType.Boolean,mActive);
cm.AddLargeStringInParameter("@Description",mDescription);
cm.AddInParameter("@Name",DbType.String,mName);
cm.AddInParameter("@RegionID", DbType.Guid, mRegionID);//case 58
//Standard fields
cm.AddInParameter("@ID",DbType.Guid,this.mID);
cm.AddInParameter("@CurrentUserID",DbType.Guid, CurrentUserID);
cm.AddInParameter("@Created",DbType.DateTime, DBUtil.ToUTC(mCreated.Date));
cm.AddInParameter("@Modified",DbType.DateTime, DBUtil.ToUTC(dtModified));
DBUtil.DB.ExecuteNonQuery(cm, tr);
//Process keywords
DBUtil.ProcessKeywords(tr,this.mID,RootObjectTypes.PartWarehouse,IsNew,AyaBizUtils.Break(false,
mName,mDescription
));
#region DEPRECATED create empty inventory records
//case 476 if(IsNew)
//{
// DBCommandWrapper cmInvent = DBUtil.GetCommandFromSQL(
// "INSERT INTO aPartByWarehouseInventory ( " +
// "aID, AACTIVE, aPartID, aPartWarehouseID, " +
// "aQuantityOnHand, aQuantityOnOrder, " +
// "aQtyOnOrderCommitted, aMinStockLevel, " +
// "aCreated,aModified,aCreator,aModifier) VALUES " +
// "(@ID,@Active,@PartID,@PartWarehouseID, " +
// "0,0,0,0, " +
// "@Created,@Modified,@CurrentUserID,@CurrentUserID)"
// );
// //Iterate through all warehouses and create a PartByWarehouseInventory
// //record for each one
// PartPickList ppl=PartPickList.GetAllParts();
// foreach(PartPickList.PartPickListInfo partinfo in ppl)
// {
// cmInvent.Command.Parameters.Clear();
// cmInvent.AddInParameter("@ID",DbType.Guid,Guid.NewGuid());
// cmInvent.AddInParameter("@Active",DbType.Boolean, true);
// cmInvent.AddInParameter("@PartID",DbType.Guid,partinfo.ID);
// cmInvent.AddInParameter("@PartWarehouseID",DbType.Guid,this.mID);
// //Standard fields
// cmInvent.AddInParameter("@CurrentUserID",DbType.Guid, CurrentUserID);
// cmInvent.AddInParameter("@Created",DbType.DateTime, DBUtil.ToUTC(mCreated).DBValue);
// cmInvent.AddInParameter("@Modified",DbType.DateTime, DBUtil.ToUTC(dtModified));
// DBUtil.DB.ExecuteNonQuery(cmInvent, tr);
// }
//}
#endregion
MarkOld();//db is now synched with object
//Successful update so
//change modification time to match
this.mModified.Date=dtModified;
#endregion
}
#endregion
}//end PartWarehouse
}//end namespace GZTW.AyaNova.BLL