731 lines
17 KiB
C#
731 lines
17 KiB
C#
///////////////////////////////////////////////////////////
|
|
// PartInventoryAdjustment.cs
|
|
// Implementation of Class PartInventoryAdjustment
|
|
// CSLA type: Editable Root
|
|
// Created on: 15-Nov-2004 8:41:45 AM
|
|
// Object design: Joyce
|
|
// Coded: John 15-Nov-2004
|
|
///////////////////////////////////////////////////////////
|
|
|
|
using System;
|
|
using System.Data;
|
|
using CSLA.Data;
|
|
using GZTW.Data;
|
|
using CSLA;
|
|
using System.Threading;
|
|
using CSLA.Security;
|
|
|
|
|
|
namespace GZTW.AyaNova.BLL
|
|
{
|
|
/// <summary>
|
|
/// Manual inventory adjustment
|
|
/// </summary>
|
|
[Serializable]
|
|
public class PartInventoryAdjustment : BusinessBase
|
|
{
|
|
|
|
#region Attributes
|
|
|
|
private bool bReadOnly;
|
|
private Guid mID;
|
|
private SmartDate mCreated;
|
|
private SmartDate mModified;
|
|
private Guid mCreator;
|
|
private Guid mModifier;
|
|
|
|
private SmartDate mDateAdjusted;
|
|
private int mAdjustmentNumber;
|
|
private string mReasonForAdjustment="";
|
|
private PartInventoryAdjustmentItems mItems;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
/// <summary>
|
|
/// Private constructor to prevent direct instantiation
|
|
/// </summary>
|
|
private PartInventoryAdjustment()
|
|
{
|
|
|
|
|
|
|
|
//Set to read / write initially so that properties
|
|
//can be set
|
|
bReadOnly=false;
|
|
|
|
//New ID
|
|
mID = Guid.NewGuid();
|
|
mDateAdjusted = new SmartDate(DBUtil.CurrentWorkingDateTime);
|
|
mItems=PartInventoryAdjustmentItems.NewItems();
|
|
|
|
//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 record ID of person who modified this record
|
|
///
|
|
///
|
|
/// </summary>
|
|
public Guid Modifier
|
|
{
|
|
get
|
|
{
|
|
return mModifier;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// collection of <see cref="PartInventoryAdjustmentItems"/> containing <see cref="PartInventoryAdjustmentItem"/> objects
|
|
/// </summary>
|
|
public PartInventoryAdjustmentItems Items
|
|
{
|
|
get
|
|
{
|
|
return mItems;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Effective date of adjustment (may not be entry date)
|
|
/// </summary>
|
|
public object DateAdjusted
|
|
{
|
|
get
|
|
{
|
|
return mDateAdjusted.DBValue;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if (!AyaBizUtils.SmartDateEquals(mDateAdjusted, value)) //Case 298
|
|
{
|
|
mDateAdjusted.DBValue = value;
|
|
BrokenRules.Assert("DateAdjustedRequired",
|
|
"Error.Object.RequiredFieldEmpty,PartInventoryAdjustment.Label.DateAdjusted",
|
|
"DateAdjusted",mDateAdjusted.IsEmpty);
|
|
MarkDirty();
|
|
foreach(PartInventoryAdjustmentItem i in mItems)
|
|
{
|
|
i.mDateAdjusted=mDateAdjusted;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public SmartDate sdDateAdjusted
|
|
{
|
|
get{return mDateAdjusted;}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read only DB set unique identity number
|
|
/// </summary>
|
|
public int AdjustmentNumber
|
|
{
|
|
get
|
|
{
|
|
return mAdjustmentNumber;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Reason for adjustment
|
|
/// </summary>
|
|
public string ReasonForAdjustment
|
|
{
|
|
get
|
|
{
|
|
return mReasonForAdjustment;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mReasonForAdjustment!=value)
|
|
{
|
|
mReasonForAdjustment = 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.PartInventoryAdjustment")
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// called by UI on change of collection of items
|
|
/// </summary>
|
|
public void CheckForDuplicateSerialNumbers()
|
|
{
|
|
|
|
foreach(PartInventoryAdjustmentItem p in mItems)
|
|
{
|
|
p.SerialNumbers.CheckForDuplicateSerialNumbers();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#region System.object overrides
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override string ToString()
|
|
{
|
|
return "PartInventoryAdjustment" + mID.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public override bool Equals(Object obj)
|
|
{
|
|
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
|
|
PartInventoryAdjustment c=(PartInventoryAdjustment)obj;
|
|
return mID==c.mID;
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override int GetHashCode()
|
|
{
|
|
return ("PartInventoryAdjustment"+mID).GetHashCode();
|
|
}
|
|
#endregion
|
|
|
|
#region Searching
|
|
|
|
/// <summary>
|
|
/// Returns a search result object based on search terms
|
|
/// for the ID specified
|
|
/// </summary>
|
|
/// <param name="ID"></param>
|
|
/// <param name="searchTerms"></param>
|
|
/// <returns></returns>
|
|
public static SearchResult GetSearchResult(Guid ID, string[]searchTerms)
|
|
{
|
|
|
|
|
|
if (AyaBizUtils.Right("Object.PartInventoryAdjustment") < (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 aCreated, aModified, aCreator, aModifier, aReasonForAdjustment FROM aPartInventoryAdjustment " +
|
|
"WHERE (aID = @ID)"
|
|
|
|
, ID);
|
|
|
|
if (!dr.Read())
|
|
return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for PartID: " + ID.ToString());
|
|
|
|
|
|
|
|
sr.Description = LocalizedTextTable.GetLocalizedTextDirect("O.PartInventoryAdjustment");
|
|
sb.Append(sr.Description);
|
|
sb.Append(" ");
|
|
sb.Append(dr.GetString("aReasonForAdjustment"));
|
|
sb.Append(" ");
|
|
|
|
|
|
|
|
|
|
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.PartInventoryAdjustment;
|
|
|
|
return sr;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region Static methods
|
|
/// <summary>
|
|
/// Create new PartInventoryAdjustment
|
|
/// </summary>
|
|
/// <returns>PartInventoryAdjustment</returns>
|
|
public static PartInventoryAdjustment NewItem()
|
|
{
|
|
PartInventoryAdjustment c;
|
|
|
|
if(AyaBizUtils.Right("Object.PartInventoryAdjustment")>(int)SecurityLevelTypes.ReadOnly)
|
|
{
|
|
c = new PartInventoryAdjustment();
|
|
return c;
|
|
}
|
|
else
|
|
throw new System.Security.SecurityException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.PartInventoryAdjustment")));
|
|
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetch existing PartInventoryAdjustment
|
|
/// </summary>
|
|
/// <returns>PartInventoryAdjustment</returns>
|
|
/// <param name="_ID">PartInventoryAdjustment Guid</param>
|
|
public static PartInventoryAdjustment GetItem(Guid _ID)
|
|
{
|
|
if (_ID == AyaBizUtils.NewObjectGuid)
|
|
return NewItem();
|
|
|
|
if(AyaBizUtils.Right("Object.PartInventoryAdjustment")>(int)SecurityLevelTypes.NoAccess)
|
|
return (PartInventoryAdjustment)DataPortal.Fetch(new Criteria(_ID));
|
|
else
|
|
throw new System.Security.SecurityException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.PartInventoryAdjustment")));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="newID"></param>
|
|
public static void SetVisibleIDNumber(int newID)
|
|
{
|
|
VisibleIDNumber.SetVisibleIDNumber(newID);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region DAL DATA ACCESS
|
|
|
|
#region Fetch
|
|
///
|
|
/// <param name="Criteria"></param>
|
|
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 aPartInventoryAdjustment WHERE aID=@ID;",crit.ID);
|
|
if(!dr.Read())
|
|
DBUtil.ThrowFetchError("PartInventoryAdjustment 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");
|
|
|
|
|
|
//PartInventoryAdjustment fields
|
|
mDateAdjusted=DBUtil.ToLocal(dr.GetSmartDate("aDateAdjusted"));
|
|
mAdjustmentNumber=dr.GetInt32("AADJUSTMENTNUMBER");
|
|
mReasonForAdjustment=dr.GetString("aReasonForAdjustment");
|
|
if(dr!=null) dr.Close();
|
|
|
|
|
|
/*
|
|
* Load child collection objects
|
|
*/
|
|
|
|
//Items
|
|
|
|
dr=DBUtil.GetReaderFromSQLString(
|
|
"SELECT * FROM aPartInventoryAdjustmentItem " +
|
|
"WHERE aPartInventoryAdjustmentItem.aPartInventoryAdjustmentID=@ID "
|
|
,crit.ID);
|
|
mItems=PartInventoryAdjustmentItems.GetItems(dr);
|
|
if(dr!=null) dr.Close();
|
|
}
|
|
finally
|
|
{
|
|
if(dr!=null) dr.Close();
|
|
}
|
|
MarkOld();
|
|
|
|
|
|
|
|
|
|
//Get access rights level
|
|
bReadOnly=AyaBizUtils.Right("Object.PartInventoryAdjustment")<(int)SecurityLevelTypes.ReadWrite;
|
|
|
|
}
|
|
#endregion fetch
|
|
|
|
#region Update
|
|
|
|
/// <summary>
|
|
/// Called by DataPortal to delete/add/update data into the database
|
|
/// </summary>
|
|
protected override void DataPortal_Update()
|
|
{
|
|
//PartInventoryAdjustment adjustment is unchangeable add only:
|
|
if(!IsNew)
|
|
throw new System.ApplicationException
|
|
(
|
|
string.Format
|
|
(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Object.NotChangeable"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.PartInventoryAdjustment")
|
|
)
|
|
);
|
|
|
|
if(IsDeleted)
|
|
throw new System.ApplicationException
|
|
(
|
|
string.Format
|
|
(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Object.NotDeleteable"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.PartInventoryAdjustment")
|
|
)
|
|
);
|
|
|
|
#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 aPartInventoryAdjustment (aID, aDateAdjusted, aReasonForAdjustment, aCreated,aModified,aCreator,aModifier) " +
|
|
"VALUES (@ID,@DateAdjusted,@ReasonForAdjustment,@Created,@Modified,@CurrentUserID,@CurrentUserID)"
|
|
);
|
|
else
|
|
cm=DBUtil.GetCommandFromSQL(
|
|
"UPDATE aPartInventoryAdjustment SET aID=@ID, aDateAdjusted=@DateAdjusted, " +
|
|
"aReasonForAdjustment=@ReasonForAdjustment, " +
|
|
"aModifier=@CurrentUserID, aModified=@Modified " +
|
|
"WHERE aID=@ID"
|
|
);
|
|
|
|
|
|
|
|
cm.AddInParameter("@ID",DbType.Guid, this.mID);
|
|
cm.AddInParameter("@DateAdjusted",DbType.DateTime,DBUtil.ToUTC(mDateAdjusted).DBValue);
|
|
cm.AddInParameter("@ReasonForAdjustment",DbType.String,mReasonForAdjustment);
|
|
|
|
//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));
|
|
|
|
|
|
using (IDbConnection connection = DBUtil.DB.GetConnection())
|
|
{
|
|
connection.Open();
|
|
|
|
//All inventory affecting methods use a high isolation level transaction:
|
|
IDbTransaction transaction = connection.BeginTransaction(System.Data.IsolationLevel.Serializable);
|
|
bool bGetIdentity=IsNew;
|
|
try
|
|
{
|
|
|
|
DBUtil.DB.ExecuteNonQuery(cm, transaction);
|
|
|
|
//Update child objects
|
|
//Items
|
|
mItems.Update(mID,transaction);
|
|
|
|
//Process keywords
|
|
DBUtil.ProcessKeywords(transaction,this.mID,RootObjectTypes.PartInventoryAdjustment,IsNew,
|
|
AyaBizUtils.Break(false,
|
|
this.mReasonForAdjustment));
|
|
|
|
MarkOld();//db is now synched with object
|
|
|
|
// Commit the transaction
|
|
transaction.Commit();
|
|
|
|
}
|
|
catch
|
|
{
|
|
// Rollback transaction
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
connection.Close();
|
|
}
|
|
|
|
//Get new DB generated identity value
|
|
if(bGetIdentity)
|
|
this.mAdjustmentNumber=DBUtil.GetIdentity("AADJUSTMENTNUMBER","aPartInventoryAdjustment",this.mID,null);
|
|
|
|
//Successful update so
|
|
//change modification time to match
|
|
this.mModified.Date=dtModified;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
#endregion Update
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#region Override IsValid / IsDirty
|
|
//Override base class version if there are child objects
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public override bool IsValid
|
|
{
|
|
get
|
|
{
|
|
return base.IsValid && mItems.IsValid && mItems.GrandChildIsValid();
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public override bool IsDirty
|
|
{
|
|
get
|
|
{
|
|
return base.IsDirty || mItems.IsDirty || mItems.GrandChildIsDirty() ;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region criteria
|
|
/// <summary>
|
|
/// Criteria for identifying existing object
|
|
/// </summary>
|
|
[Serializable]
|
|
private class Criteria
|
|
{
|
|
public Guid ID;
|
|
public Criteria(Guid _ID)
|
|
{
|
|
ID=_ID;
|
|
}
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region Set VisibleIDNumber
|
|
#pragma warning disable 1591
|
|
|
|
/// <summary>
|
|
/// Set the DB generated visible ID number
|
|
/// to a new user chosen starting point
|
|
/// </summary>
|
|
[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(AADJUSTMENTNUMBER) FROM aPartInventoryAdjustment"
|
|
);
|
|
if(o==null || o==System.DBNull.Value) o=0;
|
|
int nHighestExisting=(int)o;
|
|
|
|
|
|
//ensure new number is larger than highest existing one
|
|
if(_newStartID<nHighestExisting)
|
|
throw new ApplicationException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.AutoIncrement.NumberTooLow"),
|
|
(nHighestExisting+1).ToString()));
|
|
|
|
|
|
|
|
|
|
switch(DBUtil.DB.DBType)
|
|
{
|
|
case DataBaseType.FireBird:
|
|
{
|
|
string sql="SET Generator GEN_AADJUSTMENTNUMBER TO " + _newStartID.ToString();
|
|
DBUtil.DB.ExecuteNonQuery(CommandType.Text,sql);
|
|
}
|
|
break;
|
|
|
|
case DataBaseType.MSSQL:
|
|
{
|
|
string sql="DBCC CHECKIDENT (aPartInventoryAdjustment, RESEED, " + _newStartID.ToString()+")";
|
|
DBUtil.DB.ExecuteNonQuery(CommandType.Text,sql);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
throw new ApplicationException("UNKNOWN DB TYPE IN START SEED UPDATE");
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
#pragma warning restore 1591
|
|
#endregion set visibleIDnumber
|
|
|
|
|
|
}//end PartInventoryAdjustment
|
|
|
|
}//end namespace GZTW.AyaNova.BLL |