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

751 lines
17 KiB
C#

///////////////////////////////////////////////////////////
// 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
{
/// <summary>
/// Used in PO's, invoices, parts, services to indicate an item is taxable
/// </summary>
[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;
/// <summary>
/// if true, does show in wo select list; if false then is not selectable, but can
/// still bring up history etc
/// default is true
/// </summary>
private bool mActive;
/// <summary>
/// Decimal amount for this tax
/// </summary>
private decimal mTaxA=0m;
/// <summary>
/// decimal amount for this tax
/// </summary>
private decimal mTaxB=0m;
/// <summary>
/// default is false
/// True indicates that where this is applied is tax exempt
/// </summary>
private bool mTaxAExempt=true;
/// <summary>
/// default is false
/// True indicates that where this is applied is tax exempt
/// </summary>
private bool mTaxBExempt=true;
/// <summary>
/// Default is false
/// If true, than the TaxB amount is determined from the total of itemcost + TaxA
/// amount X TaxB
/// </summary>
private bool mTaxOnTax;
#endregion
#region Constructor
/// <summary>
/// Private constructor to prevent direct instantiation
/// </summary>
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
/// <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 Name of item
///
/// </summary>
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();
}
}
}
}
/// <summary>
/// Get /set active status of TaxCode
/// If active = true then taxcode is selectable
/// If active = false then texcode in not selectable
/// </summary>
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();
}
}
}
}
/// <summary>
/// Decimal amount for this tax
/// </summary>
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();
}
}
}
}
/// <summary>
/// decimal amount for this tax
/// </summary>
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();
}
}
}
}
/// <summary>
/// default is false
/// True indicates that where this is applied is tax exempt
/// </summary>
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();
}
}
}
}
/// <summary>
/// default is false
/// True indicates that where this is applied is tax exempt
/// </summary>
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();
}
}
}
}
/// <summary>
/// Default is false
/// If true, than the TaxB amount is determined from the total of itemcost + TaxA
/// amount X TaxB
/// </summary>
public bool TaxOnTax
{
get
{
return mTaxOnTax;
}
set
{
if(bReadOnly || !IsNew)
ThrowSetError();
else
{
if(mTaxOnTax!=value)
{
mTaxOnTax = value;
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.TaxCode")
)
);
}
#endregion
#region System.Object overrides
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "TaxCode" + mID.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(Object obj)
{
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
TaxCode c=(TaxCode)obj;
return mID==c.mID;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return ("TaxCode" + mID).GetHashCode();
}
#endregion
#region Static methods
/// <summary>
/// Get new object
/// </summary>
/// <returns></returns>
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")));
}
/// <summary>
///
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
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")));
}
/// <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("ATAXCODE", "ANAME", Name);
}
#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");
//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();
}
/// <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,"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
// /// <summary>
// /// Criteria for identifying existing object
// /// </summary>
// [Serializable]
// private class Criteria
// {
// public Guid ID;
// public Criteria(Guid _ID)
// {
// ID=_ID;
// }
//
// }
#endregion
}//end TaxCode
#region TaxCodeID Type Converter
#pragma warning disable 1591
/// <summary>
/// Convert tax codes to strings
/// </summary>
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