/////////////////////////////////////////////////////////// // 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 { /// /// 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. /// [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 /// /// Private constructor to prevent direct instantiation /// 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 /// /// Get internal id number Read only property because it's set internally, not /// externally /// public Guid ID { get { return mID; } } /// /// Get created date /// /// /// public string Created { get { return mCreated.ToString(); } } /// /// Get modified date /// /// /// public string Modified { get { return mModified.ToString(); } } /// /// Get user record ID of person who created this record /// /// /// public Guid Creator { get { return mCreator; } } /// /// Get user ID of person who modified this record /// /// /// public Guid Modifier { get { return mModifier; } } /// /// Set/get client group name /// 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(); } } } } /// /// 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 /// 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(); } } } } } /// /// Human readable description of warehouse i.e. "Norm's van" or "5th Street warehouse" /// public string Description { get { return mDescription; } set { if(bReadOnly) ThrowSetError(); else { if(mDescription!=value) { mDescription = value; MarkDirty(); } } } } /// /// Limit to specific region or available to all regions using Region.DefaultRegionID /// 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(); } } } } /// /// 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) /// private void ThrowSetError() { throw new System.Security.SecurityException ( string.Format ( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToChange"), LocalizedTextTable.GetLocalizedTextDirect("O.PartWarehouse") ) ); } #endregion #region System.Object overrides /// /// /// /// public override string ToString() { return "PartWarehouse" + mID.ToString(); } /// /// /// /// /// public override bool Equals(Object obj) { if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; PartWarehouse c=(PartWarehouse)obj; return mID==c.mID; } /// /// /// /// public override int GetHashCode() { return ("PartWarehouse"+mID).GetHashCode(); } #endregion #region Searching /// /// Returns a search result object based on search terms /// for the ID specified /// /// /// /// 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 /// /// 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 /// public static Guid DefaultWarehouseID { get { return new Guid("{FECD26B2-26A4-471B-B68B-54B642029943}"); } } /// /// Create new PartWarehouse /// 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"))); } /// /// Fetch existing PartWarehouse /// /// 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"))); } /// /// Retrieve internal ID from name. /// /// /// Text value /// Guid ID value or Guid.Empty if no match public static Guid GetIDFromName(string Name) { return GuidFetcher.GetItem("APARTWAREHOUSE", "ANAME", Name); } //case 2082 helper /// /// Provides a list of regionally accessible, Active, warehouses for user or region specified /// /// If provided and not Guid.empty, fetches the selected region for the user id provided, if not then the RegionId paramter is used /// If UserId==Guid.empty uses this value as the region to compare to /// /// Guid list of accessible warehouses public static System.Collections.Generic.List 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 ret = new System.Collections.Generic.List(); 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 /// /// Populate this object from the values in the datareader passed to it /// /// 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; } /// /// /// /// 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