/////////////////////////////////////////////////////////// // Contract.cs // Implementation of Class Contract // CSLA type: Editable Root // Created on: 07-Jun-2004 8:41:19 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 { /// /// Contract /// [Serializable] public class Contract : 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 AssignedDocs mDocs; private string mNotes=""; private string mName=""; private decimal mDiscountParts; private bool mContractRatesOnly; //Child collections private ContractRates mContractRates; //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=""; //case 58 private Guid mRegionID; #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private Contract() { //Set to read / write initially so that properties //can be set bReadOnly=false; //New ID mID = Guid.NewGuid(); Active=true; mContractRates=ContractRates.NewItems(); mDocs=AssignedDocs.NewItems(); //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; } } /// /// Get /set active status of Contract /// If active = true then Contract is selectable for workorders etc /// If active = false then Contract in not selectable, but history can be viewed /// public bool Active { get { return mActive; } set { if(bReadOnly) ThrowSetError(); else { if(mActive!=value) { mActive = value; MarkDirty(); } } } } /// /// Notes about Contract /// public string Notes { get { return mNotes; } set { if(bReadOnly) ThrowSetError(); else { if(mNotes!=value) { mNotes = value; MarkDirty(); } } } } /// /// attributed to this Contract /// public AssignedDocs Docs { get { return mDocs; } } /// /// Original sales Name number /// public string Name { get { return mName; } set { if(bReadOnly) ThrowSetError(); else { if(mName!=value) { mName = value; MarkDirty(); } } } } /// /// Most businesses will enter in a discount that a unit receives that is applied /// to parts to cut down on the entry involved with this, as then you could enter /// in the discount % once, but it would get applied to any part selected for this /// unit without having to enter in the actual price for every single part. (note /// if client has a discount, and unit has a discount - the discount on the unit /// overrides if the unit is selected in the workorder) /// public decimal DiscountParts { get { return mDiscountParts; } set { if(bReadOnly) ThrowSetError(); else { if(mDiscountParts!=value) { mDiscountParts = value; MarkDirty(); } } } } /// /// Collection of rates that are accessible for this contract /// public ContractRates Rates { get { return mContractRates; } } /// /// Default = false /// If true user can select rate from general rateslist rather than the ones /// specifically set for this contract /// public bool ContractRatesOnly { get { return mContractRatesOnly; } set { if(bReadOnly) ThrowSetError(); else { if(mContractRatesOnly!=value) { mContractRatesOnly = 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(); } } } } //CUSTOM FIELDS /// /// Custom1 /// public string Custom1 { get { return mCustom1; } set { if(bReadOnly) ThrowSetError(); else { if(mCustom1!=value) { mCustom1 = value; BrokenRules.Assert("Custom1Length", "Error.Object.FieldLengthExceeded500,Contract.Label.Custom1","Custom1",value.Length>500); MarkDirty(); } } } } /// /// Custom2 /// public string Custom2 { get { return mCustom2; } set { if(bReadOnly) ThrowSetError(); else { if(mCustom2!=value) { mCustom2 = value; BrokenRules.Assert("Custom2Length", "Error.Object.FieldLengthExceeded500,Contract.Label.Custom2","Custom2",value.Length>500); MarkDirty(); } } } } /// /// Custom3 /// public string Custom3 { get { return mCustom3; } set { if(bReadOnly) ThrowSetError(); else { if(mCustom3!=value) { mCustom3 = value; BrokenRules.Assert("Custom3Length", "Error.Object.FieldLengthExceeded500,Contract.Label.Custom3","Custom3",value.Length>500); MarkDirty(); } } } } /// /// Custom4 /// public string Custom4 { get { return mCustom4; } set { if(bReadOnly) ThrowSetError(); else { if(mCustom4!=value) { mCustom4 = value; BrokenRules.Assert("Custom4Length", "Error.Object.FieldLengthExceeded500,Contract.Label.Custom4","Custom4",value.Length>500); MarkDirty(); } } } } /// /// Custom5 /// public string Custom5 { get { return mCustom5; } set { if(bReadOnly) ThrowSetError(); else { if(mCustom5!=value) { mCustom5 = value; BrokenRules.Assert("Custom5Length", "Error.Object.FieldLengthExceeded500,Contract.Label.Custom5","Custom5",value.Length>500); MarkDirty(); } } } } /// /// Custom6 /// public string Custom6 { get { return mCustom6; } set { if(bReadOnly) ThrowSetError(); else { if(mCustom6!=value) { mCustom6 = value; BrokenRules.Assert("Custom6Length", "Error.Object.FieldLengthExceeded500,Contract.Label.Custom6","Custom6",value.Length>500); MarkDirty(); } } } } /// /// Custom7 /// public string Custom7 { get { return mCustom7; } set { if(bReadOnly) ThrowSetError(); else { if(mCustom7!=value) { mCustom7 = value; BrokenRules.Assert("Custom7Length", "Error.Object.FieldLengthExceeded500,Contract.Label.Custom7","Custom7",value.Length>500); MarkDirty(); } } } } /// /// Custom8 /// public string Custom8 { get { return mCustom8; } set { if(bReadOnly) ThrowSetError(); else { if(mCustom8!=value) { mCustom8 = value; BrokenRules.Assert("Custom8Length", "Error.Object.FieldLengthExceeded500,Contract.Label.Custom8","Custom8",value.Length>500); MarkDirty(); } } } } /// /// Custom9 /// public string Custom9 { get { return mCustom9; } set { if(bReadOnly) ThrowSetError(); else { if(mCustom9!=value) { mCustom9 = value; BrokenRules.Assert("Custom9Length", "Error.Object.FieldLengthExceeded500,Contract.Label.Custom9","Custom9",value.Length>500); MarkDirty(); } } } } /// /// Custom0 /// public string Custom0 { get { return mCustom0; } set { if(bReadOnly) ThrowSetError(); else { if(mCustom0!=value) { mCustom0 = value; BrokenRules.Assert("Custom0Length", "Error.Object.FieldLengthExceeded500,Contract.Label.Custom0","Custom0",value.Length>500); MarkDirty(); } } } } /// /// Flag - indicates if current user can open the wiki page for this object /// See method for details /// /// This is cached for the lifetime of this object /// public bool CanWiki//case 73 { get { if (!bCanWiki.HasValue) bCanWiki = WikiPage.ShowWikiLink(RootObjectTypes.Contract, mID); return bCanWiki.Value; } } //cache the result in case the UI calls this repeatedly private bool? bCanWiki = null; /// /// 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.Contract") ) ); } #endregion #region System.object overrides /// /// /// /// public override string ToString() { return "Contract" + mID.ToString(); } /// /// /// /// /// public override bool Equals(Object obj) { if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; Contract c=(Contract)obj; return mID==c.mID; } /// /// /// /// public override int GetHashCode() { return ("Contract"+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.Contract")<(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, aCreator, aModifier, aCreated, aModified, aName, " + " aNotes, aCustom1, aCustom2, aCustom3, aCustom4, aCustom5, " + " aCustom6, aCustom7, aCustom8, aCustom9, aCustom0 FROM " + "aContract WHERE (aID = @ID)" ,ID); if(!dr.Read()) return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for ContractID: " + 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("aNotes")); 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(); } //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.Contract; return sr; } #endregion #region Static methods /// /// Create new Contract /// /// Contract public static Contract NewItem() { Contract c; if(AyaBizUtils.Right("Object.Contract")>(int)SecurityLevelTypes.ReadOnly) { c = new Contract(); return c; } else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"), LocalizedTextTable.GetLocalizedTextDirect("O.Contract"))); } /// /// Fetch existing Contract /// MRU tracked /// /// Contract /// Contract Guid public static Contract GetItem(Guid _ID) { return GetItemMRU(_ID, true); } /// /// Fetch existing Contract /// don't track MRU /// /// Contract /// Contract Guid public static Contract GetItemNoMRU(Guid _ID) { return GetItemMRU(_ID, false); } /// /// Get item, optional MRU /// See /// /// /// /// public static Contract GetItemMRU(Guid _ID, bool TrackMRU) { if (_ID == AyaBizUtils.NewObjectGuid) return NewItem(); if (AyaBizUtils.Right("Object.Contract") > (int)SecurityLevelTypes.NoAccess) return (Contract)DataPortal.Fetch(new Criteria(_ID,TrackMRU)); else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"), LocalizedTextTable.GetLocalizedTextDirect("O.Contract"))); } /// /// Delete Contract /// /// Contract GUID public static void DeleteItem(Guid _ID) { if(AyaBizUtils.Right("Object.Contract")>(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.Contract"))); } /// /// 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("ACONTRACT", "ANAME", Name); } #endregion #region DAL DATA ACCESS #region Fetch /// /// 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 aContract WHERE aID=@ID;",crit.ID); if(!dr.Read()) DBUtil.ThrowFetchError("Contract 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"); //case 58 mRegionID = dr.GetGuid("aRegionID"); //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"); //Contract fields mActive=dr.GetBoolean("AACTIVE"); mNotes=dr.GetString("aNotes"); mName=dr.GetString("aName"); mDiscountParts=dr.GetDecimal("aDiscountParts"); mContractRatesOnly=dr.GetBoolean("aContractRatesOnly"); if(dr!=null) dr.Close(); //Load child collection objects //Rates dr=DBUtil.GetReaderFromSQLString( "SELECT * " + "FROM aContractRate WHERE aContractRate.aContractID=@ID;" ,crit.ID); mContractRates = ContractRates.GetItems(dr); if(dr!=null) dr.Close(); //Docs dr=DBUtil.GetReaderFromSQLString("SELECT * FROM AASSIGNEDDOC WHERE (aRootObjectID=@ID and aRootObjectType=18);",crit.ID); mDocs = AssignedDocs.GetItems(dr, RootObjectTypes.Contract, RootObjectTypes.Contract); if(dr!=null) dr.Close(); } finally { if(dr!=null) dr.Close(); } MarkOld(); if(crit.TrackMRU) if(AyaBizUtils.AllowAutomaticMRUOnUpdate) AyaBizUtils.MRU.Add(RootObjectTypes.Contract, mID); //Get access rights level bReadOnly=AyaBizUtils.Right("Object.Contract")<(int)SecurityLevelTypes.ReadWrite; } #endregion fetch #region Update /// /// Called by DataPortal to delete/add/update data into the database /// 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,"aContract"); #region Delete if(IsDeleted) { if(!IsNew) { //Delete object and child objects DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aContract WHERE aID = @ID;"); cmDelete.AddInParameter("@ID",DbType.Guid,this.mID); DBCommandWrapper cmDeleteContractRates = DBUtil.GetCommandFromSQL("DELETE FROM aContractRate WHERE aContractID = @ID;"); cmDeleteContractRates.AddInParameter("@ID",DbType.Guid,this.mID); using (IDbConnection connection = DBUtil.DB.GetConnection()) { connection.Open(); IDbTransaction transaction = connection.BeginTransaction(); try { DBUtil.DB.ExecuteNonQuery(cmDelete, transaction); DBUtil.DB.ExecuteNonQuery(cmDeleteContractRates, transaction); DBUtil.RemoveKeywords(transaction,RootObjectTypes.Contract,this.mID); DBUtil.RemoveDocs(transaction,RootObjectTypes.Contract,this.mID); // 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 aContract (aID, aDiscountParts, aName, AACTIVE, aNotes, aContractRatesOnly, aCreated,aModified,aCreator,aModifier, aRegionID, " + "aCustom1, aCustom2, aCustom3, aCustom4, aCustom5, aCustom6, aCustom7, aCustom8, aCustom9, aCustom0) " + "VALUES (@ID,@DiscountParts, @Name,@Active,@Notes,@ContractRatesOnly,@Created,@Modified,@CurrentUserID,@CurrentUserID, @RegionID, " + "@Custom1,@Custom2,@Custom3,@Custom4,@Custom5,@Custom6,@Custom7,@Custom8,@Custom9,@Custom0)" ); else cm=DBUtil.GetCommandFromSQL( "UPDATE aContract SET aID=@ID, aDiscountParts=@DiscountParts, " + "aName=@Name, AACTIVE=@Active, aNotes=@Notes, " + "aContractRatesOnly=@ContractRatesOnly, aModifier=@CurrentUserID, " + "aModified=@Modified,aRegionID=@RegionID, aCustom1=@Custom1, " + "aCustom2=@Custom2, aCustom3=@Custom3, " + "aCustom4=@Custom4, aCustom5=@Custom5, aCustom6=@Custom6, " + "aCustom7=@Custom7, aCustom8=@Custom8, " + "aCustom9=@Custom9, aCustom0=@Custom0 WHERE " + "aID=@ID" ); cm.AddInParameter("@ID",DbType.Guid,mID); cm.AddInParameter("@Active",DbType.Boolean, mActive); cm.AddLargeStringInParameter("@Notes", mNotes); cm.AddInParameter("@Name",DbType.String, mName); cm.AddInParameter("@DiscountParts",DbType.Decimal, mDiscountParts); cm.AddInParameter("@ContractRatesOnly",DbType.Boolean, mContractRatesOnly); cm.AddInParameter("@RegionID", DbType.Guid, mRegionID);//case 58 //Standard fields cm.AddInParameter("@CurrentUserID",DbType.Guid, CurrentUserID); 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 mContractRates.Update(this,transaction); //Docs mDocs.Update(transaction); //Process keywords DBUtil.ProcessKeywords(transaction,this.mID,RootObjectTypes.Contract,IsNew,AyaBizUtils.Break(false, mNotes,mName, /*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; if(AyaBizUtils.AllowAutomaticMRUOnUpdate) AyaBizUtils.MRU.Add(RootObjectTypes.Contract, mID); } #endregion } #endregion update #region Delete /// /// Remove a Contract record . /// /// protected override void DataPortal_Delete(object Criteria) { Criteria crit = (Criteria)Criteria; //Delete object and child objects DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aContract WHERE aID = @ID;"); cmDelete.AddInParameter("@ID",DbType.Guid,crit.ID); DBCommandWrapper cmDeleteContractRates = DBUtil.GetCommandFromSQL("DELETE FROM aContractRate WHERE aContractID = @ID;"); cmDeleteContractRates.AddInParameter("@ID",DbType.Guid,crit.ID); using (IDbConnection connection = DBUtil.DB.GetConnection()) { connection.Open(); IDbTransaction transaction = connection.BeginTransaction(); try { DBUtil.DB.ExecuteNonQuery(cmDelete, transaction); DBUtil.DB.ExecuteNonQuery(cmDeleteContractRates, transaction); DBUtil.RemoveKeywords(transaction,RootObjectTypes.Contract,crit.ID); DBUtil.RemoveDocs(transaction,RootObjectTypes.Contract,crit.ID); // Commit the transaction transaction.Commit(); } catch { // Rollback transaction transaction.Rollback(); throw; } finally { connection.Close(); } AyaBizUtils.MRU.Remove(RootObjectTypes.Contract, crit.ID); } } #endregion delete #endregion #region Override IsValid / IsDirty //Override base class version if there are child objects /// /// /// public override bool IsValid { get { return base.IsValid && mContractRates.IsValid && mDocs.IsValid; } } /// /// /// public override bool IsDirty { get { return base.IsDirty || mContractRates.IsDirty || mDocs.IsDirty; } } #endregion #region criteria /// /// Criteria for identifying existing object /// [Serializable] private class Criteria { public Guid ID; public bool TrackMRU; public Criteria(Guid _ID, bool _TrackMRU) { ID=_ID; TrackMRU = _TrackMRU; } } #endregion }//end Contract }//end namespace GZTW.AyaNova.BLL