/////////////////////////////////////////////////////////// // WorkorderQuote.cs // Implementation of Class WorkorderQuote // CSLA type: Editable Child // Created on: 07-Jun-2004 8:41:32 AM // Object design: Joyce // Coded: John July 19 2004 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; using System.Threading; using CSLA.Security; namespace GZTW.AyaNova.BLL { /// /// Quote header component of a object of Quote /// [Serializable] public class WorkorderQuote : BusinessBase { #region Attributes private bool bReadOnly; private Guid mID; private SmartDate mCreated; private SmartDate mModified; private Guid mCreator; private Guid mModifier; private Guid mWorkorderID; private int mQuoteNumber; private WorkorderQuoteStatusTypes mQuoteStatusType; private Guid mPreparedByID; private string mIntroduction=""; private SmartDate mQuoteRequestDate; private SmartDate mValidUntilDate; private SmartDate mDateSubmitted; private SmartDate mDateApproved; //ditto, but with status internal bool mStatusSignificant = false; #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private WorkorderQuote() { //Set to read / write initially so that properties //can be set bReadOnly=false; //Child object MarkAsChild(); //New ID mID = Guid.NewGuid(); mQuoteRequestDate = new SmartDate(DBUtil.CurrentWorkingDateTime); mValidUntilDate = new SmartDate(DBUtil.CurrentWorkingDateTime.AddMonths(1)); mDateSubmitted = new SmartDate(DBUtil.CurrentWorkingDateTime); mDateApproved=new SmartDate(); //defaults to zero which is critical //as a value of zero triggers the stored procedure to //provide an auto-generated ID value mQuoteNumber=0; //Set record history to defaults mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime); mModified=new SmartDate(); mCreator=Guid.Empty; mModifier=Guid.Empty; mQuoteStatusType=WorkorderQuoteStatusTypes.InProgress; mPreparedByID=User.CurrentThreadUserID; } #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; } } /// /// ID of user that is responsible for this quote /// Will take as default the mCreator ID, but user can select from list of /// schedulable and non-schedulable users /// public Guid PreparedByID { get { return mPreparedByID; } set { if(bReadOnly) ThrowSetError(); else { if(mPreparedByID!=value) { mPreparedByID = value; MarkDirty(); } } } } /// /// ID of root object this address belongs to (workorder) /// public Guid WorkorderID { get { return mWorkorderID; } set { if(bReadOnly) ThrowSetError(); else { if(mWorkorderID!=value) { mWorkorderID = value; MarkDirty(); } } } } /// /// When you present your quote to your customer, it helps to add some words /// introducing the quote to the customer, or maybe even reminding them what the /// quote is for and what it will cover. /// Very useful if you have to re-send to the client /// This is in addition to the Workorder object Summary field as that is limited in /// lenght /// public string Introduction { get { return mIntroduction; } set { if(bReadOnly) ThrowSetError(); else { if(mIntroduction!=value) { mIntroduction = value; MarkDirty(); } } } } /// /// Incremental number from sql server /// public int QuoteNumber { get { return mQuoteNumber; } set { if(bReadOnly) ThrowSetError(); else { if(mQuoteNumber!=value) { mQuoteNumber = value; MarkDirty(); } } } } /// ///Date quote requested initially /// public object QuoteRequestDate { get { return mQuoteRequestDate.DBValue; } set { if(bReadOnly) ThrowSetError(); else { if (!AyaBizUtils.SmartDateEquals(mQuoteRequestDate, value)) //Case 298 { mQuoteRequestDate.DBValue = value; MarkDirty(); } } } } /// /// Defaults to one month from mCreated date, but user can edit if needed /// Date quote details are still able to be approved and converted to a Service /// object /// public object ValidUntilDate { get { return mValidUntilDate.DBValue; } set { if(bReadOnly) ThrowSetError(); else { if (!AyaBizUtils.SmartDateEquals(mValidUntilDate, value)) //Case 298 { mValidUntilDate.DBValue = value; MarkDirty(); } } } } /// /// DateSubmitted - date quote submitted to client /// public object DateSubmitted { get { return mDateSubmitted.DBValue; } set { if(bReadOnly) ThrowSetError(); else { if (!AyaBizUtils.SmartDateEquals(mDateSubmitted, value)) //Case 298 { mDateSubmitted.DBValue = value; MarkDirty(); } } } } /// /// Date client approves the quote /// if empty then client has not approved quote /// (this is set when the quote is converted to a workorder if empty) /// public object DateApproved { get { return mDateApproved.DBValue; } set { if(bReadOnly) ThrowSetError(); else { if (!AyaBizUtils.SmartDateEquals(mDateApproved, value)) //Case 298 { mDateApproved.DBValue = value; MarkDirty(); } } } } /// /// Status of the quote /// public WorkorderQuoteStatusTypes QuoteStatus { get { return mQuoteStatusType; } set { if(bReadOnly) ThrowSetError(); else { if(mQuoteStatusType!=value) { //flag status change for event notification mStatusSignificant = true; mQuoteStatusType = value; 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.WorkorderQuote") ) ); } #endregion #region System.Object overrides /// /// /// /// public override string ToString() { return "WorkorderQuote" + mID.ToString(); } /// /// /// /// /// public override bool Equals(Object obj) { if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; WorkorderQuote c=(WorkorderQuote)obj; return mID==c.mID; } /// /// /// /// public override int GetHashCode() { return ("WorkorderQuote" + 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) { //case 1387 if (Workorder.RightsToWorkorder(WorkorderIDFetcher.GetWorkorderByRelative(RootObjectTypes.WorkorderQuote, ID)) < SecurityLevelTypes.ReadOnly) return new SearchResult(); SearchResult sr=new SearchResult(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); SafeDataReader dr = null; try { //Case 88 removed //"SELECT aID, aCreator, aModifier, aCreated, aModified, aIntroduction " + // "FROM aWorkorderQuote WHERE (aID " + // "= @ID)" dr=DBUtil.GetReaderFromSQLString( //Case 88 "SELECT AWORKORDERQUOTE.AID,aClient.aRegionID AS ACLIENTREGION, ACLIENT.ANAME AS ACLIENTNAME, AWORKORDERQUOTE.ACREATOR, AWORKORDERQUOTE.AMODIFIER, " + " AWORKORDERQUOTE.ACREATED, " + "AWORKORDERQUOTE.AMODIFIED, AWORKORDERQUOTE.AQUOTENUMBER, " + " AWORKORDERQUOTE.AINTRODUCTION FROM ACLIENT " + "INNER JOIN AWORKORDER ON ACLIENT.AID = AWORKORDER.ACLIENTID " + "RIGHT OUTER JOIN AWORKORDERQUOTE ON AWORKORDER.AID " + "= AWORKORDERQUOTE.AWORKORDERID " + "WHERE (AWORKORDERQUOTE.AID=@ID)" ,ID); if(!dr.Read()) return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for WorkorderItemLaborID: " + ID.ToString()); if (!AyaBizUtils.InYourRegion(dr.GetGuid("ACLIENTREGION"))) return new SearchResult();//case 58 //Case 88 sr.Description = LocalizedTextTable.GetLocalizedTextDirect("O.WorkorderQuote") + " " + dr.GetInt32("AQUOTENUMBER").ToString() + " " + dr.GetString("ACLIENTNAME"); sb.Append(dr.GetString("aIntroduction")); 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.WorkorderQuote; return sr; } #endregion #region Static methods /// /// New quote /// /// /// internal static WorkorderQuote NewItem(Workorder obj) { //case 1387 no need to check rights here because parent workorder object now checks correct rights by type //if(AyaBizUtils.Right("Object.WorkorderQuote")>(int)SecurityLevelTypes.ReadOnly) //{ WorkorderQuote child = new WorkorderQuote(); child.mWorkorderID=obj.ID; return child; //} //else // throw new System.Security.SecurityException( // string.Format( // LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"), // LocalizedTextTable.GetLocalizedTextDirect("O.WorkorderQuote"))); } /// /// /// /// /// internal static WorkorderQuote GetItem(SafeDataReader dr) { //case 1387 no need to check rights here because parent workorder object now checks correct rights by type //if(AyaBizUtils.Right("Object.WorkorderQuote")>(int)SecurityLevelTypes.NoAccess) //{ WorkorderQuote child = new WorkorderQuote(); child.Fetch(dr); return child; //} //else // throw new System.Security.SecurityException( // string.Format( // LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"), // LocalizedTextTable.GetLocalizedTextDirect("O.WorkorderQuote"))); } /// /// /// /// public static void SetVisibleIDNumber(int newID) { VisibleIDNumber.SetVisibleIDNumber(newID); } #endregion #region DAL DATA ACCESS #region Fetch /// /// /// /// private void Fetch(SafeDataReader dr) { //Standard fields mCreated=DBUtil.ToLocal(dr.GetSmartDate("aCreated")); mModified=DBUtil.ToLocal(dr.GetSmartDate("aModified")); mCreator=dr.GetGuid("aCreator"); mModifier=dr.GetGuid("aModifier"); mID=dr.GetGuid("aID"); //WorkorderQuote fields mDateApproved=DBUtil.ToLocal(dr.GetSmartDate("aDateApproved")); mDateSubmitted=DBUtil.ToLocal(dr.GetSmartDate("aDateSubmitted")); mIntroduction=dr.GetString("aIntroduction"); mPreparedByID=dr.GetGuid("aPreparedByID"); mQuoteNumber=dr.GetInt32("aQuoteNumber"); mQuoteRequestDate=DBUtil.ToLocal(dr.GetSmartDate("aQuoteRequestDate")); mQuoteStatusType=(WorkorderQuoteStatusTypes)dr.GetInt16("aQuoteStatusType"); mValidUntilDate=DBUtil.ToLocal(dr.GetSmartDate("aValidUntilDate")); mWorkorderID=dr.GetGuid("aWorkorderID"); //Get access rights level bReadOnly=AyaBizUtils.Right("Object.WorkorderQuote")<(int)SecurityLevelTypes.ReadWrite; MarkOld(); } #endregion #region Add / Update /// /// Called by DataPortal to delete/add/update data into the database /// internal void Update(Workorder obj,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.CheckSafeToUpdateInsideTransaction(this.mModified.Date, this.mID, "aWorkorderQuote", tr);//case 1960 #region Delete if(IsDeleted) { if(!IsNew) { DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aWorkorderQuote WHERE aID=@ID;"); cmDelete.AddInParameter("@ID",DbType.Guid,this.mID); DBUtil.DB.ExecuteNonQuery(cmDelete, tr); } 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? if(this.mQuoteNumber!=0)//importing? { DBUtil.AllowIdentityInsert();//Tell database to allow inserts into the identity field cm=DBUtil.GetCommandFromSQL( "INSERT INTO aWorkorderQuote (aWorkorderID, aID, aQuoteNumber, " + "aQuoteStatusType, aPreparedByID, aQuoteRequestDate, " + "aIntroduction, aValidUntilDate, aDateSubmitted, " + "aDateApproved, aCreated,aModified, aCreator, " + "aModifier) VALUES (@WorkorderID,@ID,@QuoteNumber, " + "@QuoteStatusType,@PreparedByID,@QuoteRequestDate, " + "@Introduction,@ValidUntilDate,@DateSubmitted, " + "@DateApproved, @Created, @Modified, @CurrentUserID, " + "@CurrentUserID)" ); //add service number parameter cm.AddInParameter("@QuoteNumber",DbType.Int32,this.mQuoteNumber); } else { cm=DBUtil.GetCommandFromSQL( "INSERT INTO aWorkorderQuote (aWorkorderID, aID, aQuoteStatusType, " + "aPreparedByID, aQuoteRequestDate, aIntroduction, " + "aValidUntilDate, aDateSubmitted, aDateApproved, " + "aCreated,aModified, aCreator,aModifier) " + "VALUES (@WorkorderID,@ID,@QuoteStatusType,@PreparedByID, " + "@QuoteRequestDate, @Introduction,@ValidUntilDate, " + "@DateSubmitted,@DateApproved, " + "@Created, @Modified, @CurrentUserID,@CurrentUserID)" ); } else cm=DBUtil.GetCommandFromSQL( "UPDATE aWorkorderQuote SET aWorkorderID=@WorkorderID, " + "aID=@ID, aQuoteStatusType=@QuoteStatusType, " + "aPreparedByID=@PreparedByID, " + "aQuoteRequestDate=@QuoteRequestDate, aIntroduction=@Introduction, " + "aValidUntilDate=@ValidUntilDate, " + "aDateSubmitted=@DateSubmitted, aDateApproved=@DateApproved, " + "aModifier=@CurrentUserID, aModified=@Modified " + "WHERE aID=@ID" ); //WorkorderQuote specific parameters cm.AddInParameter("@ID",DbType.Guid,mID); cm.AddInParameter("@DateApproved",DbType.DateTime,DBUtil.ToUTC(mDateApproved).DBValue); cm.AddInParameter("@DateSubmitted",DbType.DateTime,DBUtil.ToUTC(mDateSubmitted).DBValue); cm.AddInParameter("@Introduction",DbType.String,mIntroduction); cm.AddInParameter("@PreparedByID",DbType.Guid,mPreparedByID); cm.AddInParameter("@QuoteRequestDate",DbType.DateTime,DBUtil.ToUTC(mQuoteRequestDate).DBValue); cm.AddInParameter("@QuoteStatusType",DbType.Int16,(int)mQuoteStatusType); cm.AddInParameter("@ValidUntilDate",DbType.DateTime,DBUtil.ToUTC(mValidUntilDate).DBValue); cm.AddInParameter("@WorkorderID",DbType.Guid,mWorkorderID); //standard parameters 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); //Get new DB generated identity value if(IsNew) this.mQuoteNumber=DBUtil.GetIdentity("aQuoteNumber","aWorkorderQuote",this.mID,tr); //Process keywords DBUtil.ProcessKeywords(tr,this.mID,RootObjectTypes.WorkorderQuote,IsNew,AyaBizUtils.Break(false, mIntroduction)); MarkOld();//db is now synched with object //Successful update so //change modification time to match this.mModified.Date=dtModified; #endregion } #endregion #endregion #region Set VisibleIDNumber #pragma warning disable 1591 /// /// Set the DB generated visible ID number /// to a new user chosen starting point /// [Serializable(), System.ComponentModel.Browsable(false)] public class VisibleIDNumber//DO_NOT_OBFUSCATE { int _newStartID = -1; public VisibleIDNumber(int newStartID) { _newStartID = newStartID; } public static void SetVisibleIDNumber(int newStartID) { DataPortal.Update(new VisibleIDNumber(newStartID)); } public void DataPortal_Update() { //Find the highest existing number object o=DBUtil.GetScalarFromSQLString( "SELECT MAX(aQuoteNumber) FROM aWorkorderQuote" ); if(o==null || o==System.DBNull.Value) o=0; int nHighestExisting=(int)o; //ensure new number is larger than highest existing one if(_newStartID