/////////////////////////////////////////////////////////// // TaxCode.cs // Implementation of Class TaxCode // CSLA type: Editable Child // Created on: 07-Jun-2004 8:41:37 AM // Object design: Joyce // Coded: John July 8 2004 // Re-coded: as editable child by John Nov 11 2004 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; using System.Threading; using CSLA.Security; using System.ComponentModel; using System.Globalization; using System.Collections; namespace GZTW.AyaNova.BLL { /// /// Used in PO's, invoices, parts, services to indicate an item is taxable /// [Serializable] public class TaxCode : BusinessBase { #region Attributes private bool bReadOnly; private Guid mID; private string mName=null; private SmartDate mCreated; private SmartDate mModified; private Guid mCreator; private Guid mModifier; /// /// if true, does show in wo select list; if false then is not selectable, but can /// still bring up history etc /// default is true /// private bool mActive; /// /// Decimal amount for this tax /// private decimal mTaxA=0m; /// /// decimal amount for this tax /// private decimal mTaxB=0m; /// /// default is false /// True indicates that where this is applied is tax exempt /// private bool mTaxAExempt=true; /// /// default is false /// True indicates that where this is applied is tax exempt /// private bool mTaxBExempt=true; /// /// Default is false /// If true, than the TaxB amount is determined from the total of itemcost + TaxA /// amount X TaxB /// private bool mTaxOnTax; #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private TaxCode() { MarkAsChild(); //Set to read / write initially so that properties //can be set bReadOnly=false; //New ID mID = Guid.NewGuid(); //pre-break the rule Name=""; //Set record history to defaults mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime); mModified=new SmartDate(); mCreator=Guid.Empty; mModifier=Guid.Empty; } #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 Name of item /// /// public string Name { get { return mName; } set { if(bReadOnly) ThrowSetError(); else { if(mName!=value) { mName = value; BrokenRules.Assert("NameRequired","Error.Object.RequiredFieldEmpty,TaxCode.Label.Name","Name",value.Length==0); BrokenRules.Assert("NameLength", "Error.Object.FieldLengthExceeded255,TaxCode.Label.Name","Name",value.Length>255); MarkDirty(); } } } } /// /// Get /set active status of TaxCode /// If active = true then taxcode is selectable /// If active = false then texcode in not selectable /// public bool Active { get { return mActive; } set { if(bReadOnly) ThrowSetError(); else { if(mActive!=value) { mActive = value; BrokenRules.Assert( "DefaultMustBeActive", "TaxCode.Error.Default", "Active", value==false && AyaBizUtils.GlobalSettings.TaxCodeIsADefault(this.mID)); MarkDirty(); } } } } /// /// Decimal amount for this tax /// public decimal TaxA { get { return mTaxA; } set { if(bReadOnly || !IsNew) ThrowSetError(); else { if(mTaxA!=value) { mTaxA = value; //Changed: Added 20-March-2006 when removed from code behind tax codes form mTaxAExempt=(mTaxA==0); MarkDirty(); } } } } /// /// decimal amount for this tax /// public decimal TaxB { get { return mTaxB; } set { if(bReadOnly || !IsNew) ThrowSetError(); else { if(mTaxB!=value) { mTaxB = value; //Changed: Added 20-March-2006 when removed from code behind tax codes form mTaxBExempt=(mTaxB==0); MarkDirty(); } } } } /// /// default is false /// True indicates that where this is applied is tax exempt /// public bool TaxAExempt { get { return mTaxAExempt; } set { if(bReadOnly || !IsNew) ThrowSetError(); else { if(mTaxAExempt!=value) { mTaxAExempt = value; //Changed: Added 20-March-2006 when removed from code behind tax codes form if(mTaxAExempt) mTaxA=0m; MarkDirty(); } } } } /// /// default is false /// True indicates that where this is applied is tax exempt /// public bool TaxBExempt { get { return mTaxBExempt; } set { if(bReadOnly || !IsNew) ThrowSetError(); else { if(mTaxBExempt!=value) { mTaxBExempt = value; //Changed: Added 20-March-2006 when removed from code behind tax codes form if(mTaxBExempt) mTaxB=0m; MarkDirty(); } } } } /// /// Default is false /// If true, than the TaxB amount is determined from the total of itemcost + TaxA /// amount X TaxB /// public bool TaxOnTax { get { return mTaxOnTax; } set { if(bReadOnly || !IsNew) ThrowSetError(); else { if(mTaxOnTax!=value) { mTaxOnTax = 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.TaxCode") ) ); } #endregion #region System.Object overrides /// /// /// /// public override string ToString() { return "TaxCode" + mID.ToString(); } /// /// /// /// /// public override bool Equals(Object obj) { if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; TaxCode c=(TaxCode)obj; return mID==c.mID; } /// /// /// /// public override int GetHashCode() { return ("TaxCode" + mID).GetHashCode(); } #endregion #region Static methods /// /// Get new object /// /// internal static TaxCode NewItem() { if(AyaBizUtils.Right("Object.TaxCode")>(int)SecurityLevelTypes.ReadOnly) return new TaxCode(); else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"), LocalizedTextTable.GetLocalizedTextDirect("O.TaxCode"))); } /// /// /// /// /// internal static TaxCode GetItem(SafeDataReader dr) { if(AyaBizUtils.Right("Object.TaxCode")>(int)SecurityLevelTypes.NoAccess) { TaxCode child = new TaxCode(); child.Fetch(dr); return child; } else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"), LocalizedTextTable.GetLocalizedTextDirect("O.TaxCode"))); } /// /// 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("ATAXCODE", "ANAME", Name); } #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"); //TaxCode fields mActive=dr.GetBoolean("AACTIVE"); //Important: use property not internal field //so that initial broken rule is unbroken on fetch Name=dr.GetString("aName"); mTaxA=dr.GetDecimal("aTaxA"); mTaxB=dr.GetDecimal("aTaxB"); mTaxAExempt=dr.GetBoolean("aTaxAExempt"); mTaxBExempt=dr.GetBoolean("aTaxBExempt"); mTaxOnTax=dr.GetBoolean("aTaxOnTax"); //Get access rights level bReadOnly=AyaBizUtils.Right("Object.TaxCode")<(int)SecurityLevelTypes.ReadWrite; MarkOld(); } /// /// /// /// 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,"aTaxCode"); #region Delete if(IsDeleted) { if(!IsNew) { //Changed: 17-Nov-2006 case 150 //taxcode should never be deleteable once saved to db throw new System.ApplicationException ( string.Format ( LocalizedTextTable.GetLocalizedTextDirect("Error.Object.NotDeleteable"), LocalizedTextTable.GetLocalizedTextDirect("O.TaxCode") ) ); //if(AyaBizUtils.GlobalSettings.TaxCodeIsADefault(this.mID)) //{ // throw new System.ApplicationException(LocalizedTextTable.GetLocalizedTextDirect("TaxCode.Error.Default")); //} ////Delete object and child objects //DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aTaxCode 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? cm=DBUtil.GetCommandFromSQL( "INSERT INTO aTaxCode (aID, AACTIVE, aName, aTaxA, aTaxB, " + "aTaxAExempt, aTaxBExempt, aTaxOnTax, aCreated,aModified,aCreator, " + "aModifier) VALUES (@ID,@Active,@Name,@TaxA,@TaxB, " + "@TaxAExempt,@TaxBExempt,@TaxOnTax,@Created,@Modified,@CurrentUserID, " + "@CurrentUserID)" ); else cm=DBUtil.GetCommandFromSQL( "UPDATE aTaxCode SET AACTIVE=@Active, aName=@Name, " + " aModifier=@CurrentUserID, aModified=@Modified " + "WHERE aID=@ID" ); //CHANGE: 14-march-2006 added Active field to update sql above //TaxCode fields cm.AddInParameter("@Name",DbType.String,mName); cm.AddInParameter("@Active",DbType.Boolean,mActive); cm.AddInParameter("@TaxA",DbType.Decimal,mTaxA); cm.AddInParameter("@TaxB",DbType.Decimal,mTaxB); cm.AddInParameter("@TaxAExempt",DbType.Boolean,mTaxAExempt); cm.AddInParameter("@TaxBExempt",DbType.Boolean,mTaxBExempt); cm.AddInParameter("@TaxOnTax",DbType.Boolean,mTaxOnTax); //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); MarkOld();//db is now synched with object //Successful update so //change modification time to match this.mModified.Date=dtModified; #endregion } #endregion #region Override IsValid / IsDirty //Override base class version if there are child objects /* public override bool IsValid { get { return base.IsValid && ChildItem.IsValid; } } public override bool IsDirty { get { return base.IsDirty || ChildItem.IsDirty; } } */ #endregion #region criteria // /// // /// Criteria for identifying existing object // /// // [Serializable] // private class Criteria // { // public Guid ID; // public Criteria(Guid _ID) // { // ID=_ID; // } // // } #endregion }//end TaxCode #region TaxCodeID Type Converter #pragma warning disable 1591 /// /// Convert tax codes to strings /// public class TaxCodeIDConverter:StringConverter { //NVCHANGED private GenericNVList mList; public TaxCodeIDConverter() { mList=GenericNVList.GetList("aTaxCode","aID","aName",true,false,false); } public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { ArrayList ar = new ArrayList(mList.Count+1); ar.Add(""); foreach(DictionaryEntry d in mList.BindableList) { ar.Add(d.Value.ToString()); } return new StandardValuesCollection(ar); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value is Guid) { string s=""; if((Guid)value==Guid.Empty) return s; try { s=mList[value.ToString()]; } catch(Exception ex) { return ex.Message; } return s; } // if (destinationType==typeof(Guid)) // { // Guid g= new Guid(mList[(string)value); // return g;// (Guid)mList[value]; // // } // // return base.ConvertTo (context, culture, value, destinationType); } //convert from whatever kind of object to this converters type public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if(value==null) return Guid.Empty; if(value is String) { if(value.ToString()=="") return Guid.Empty; return new Guid(mList.get_Key(value.ToString())); } return base.ConvertFrom (context, culture, value); } } #pragma warning restore 1591 #endregion }//end namespace GZTW.AyaNova.BLL