730 lines
16 KiB
C#
730 lines
16 KiB
C#
///////////////////////////////////////////////////////////
|
|
// PartSerial.cs
|
|
// Implementation of Class PartSerial
|
|
// CSLA type: Editable Child
|
|
// Created on: 15-Nov-2004
|
|
// Object design: Joyce
|
|
// Coded: John 15-Nov-20044
|
|
///////////////////////////////////////////////////////////
|
|
|
|
using System;
|
|
using System.Data;
|
|
using CSLA.Data;
|
|
using GZTW.Data;
|
|
using CSLA;
|
|
using System.Threading;
|
|
using CSLA.Security;
|
|
|
|
namespace GZTW.AyaNova.BLL
|
|
{
|
|
/// <summary>
|
|
/// Stores a <see cref="Part"/> serial number record, not accessed directly but rather from it's parent collection <see cref="PartSerials"/>
|
|
/// </summary>
|
|
[Serializable]
|
|
public class PartSerial : BusinessBase
|
|
{
|
|
|
|
#region Attributes
|
|
|
|
private bool bReadOnly;
|
|
private Guid mID;
|
|
private SmartDate mCreated;
|
|
private SmartDate mModified;
|
|
private Guid mCreator;
|
|
private Guid mModifier;
|
|
|
|
private Guid mPartID;
|
|
private Guid mPartWarehouseID;
|
|
private string mSerialNumber=null;
|
|
private SmartDate mDateReceived;
|
|
private SmartDate mDateConsumed;
|
|
private Guid mWorkorderItemPartID=Guid.Empty;
|
|
private Guid mAdjustmentID=Guid.Empty;
|
|
private Guid mPurchaseOrderReceiptItemID=Guid.Empty;
|
|
|
|
private bool mAvailable=true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
/// <summary>
|
|
/// Private constructor to prevent direct instantiation
|
|
/// </summary>
|
|
private PartSerial()
|
|
{
|
|
//Set as child object
|
|
MarkAsChild();
|
|
//Set to read / write initially so that properties
|
|
//can be set
|
|
bReadOnly=false;
|
|
|
|
//New ID
|
|
mID = Guid.NewGuid();
|
|
mDateReceived = new SmartDate(DBUtil.CurrentWorkingDateTime);
|
|
mDateConsumed=new SmartDate();
|
|
|
|
//Pre break serial number required rule
|
|
//to save work in UI
|
|
SerialNumber="";
|
|
|
|
//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>
|
|
/// Part
|
|
/// </summary>
|
|
public Guid PartID
|
|
{
|
|
get
|
|
{
|
|
return mPartID;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mPartID!=value)
|
|
{
|
|
mPartID = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Warehouse
|
|
/// </summary>
|
|
public Guid PartWarehouseID
|
|
{
|
|
get
|
|
{
|
|
return mPartWarehouseID;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mPartWarehouseID!=value)
|
|
{
|
|
mPartWarehouseID = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// SerialNumber
|
|
/// </summary>
|
|
public string SerialNumber
|
|
{
|
|
get
|
|
{
|
|
return mSerialNumber;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mSerialNumber!=value)
|
|
{
|
|
mSerialNumber = value;
|
|
|
|
BrokenRules.Assert("SerialNumberRequired","Error.Object.RequiredFieldEmpty,Common.Label.SerialNumber","SerialNumber",value.Length==0);
|
|
|
|
BrokenRules.Assert("SerialNumberLength",
|
|
"Error.Object.FieldLengthExceeded255,Common.Label.SerialNumber","SerialNumber",value.Length>255);
|
|
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// set by collection if this serial number
|
|
/// is a duplicate of another in the collection
|
|
/// so that broken rule will be set
|
|
///
|
|
/// (assumes no two partID's that are the same will have the same
|
|
/// serial number if serialized part)
|
|
/// </summary>
|
|
internal bool DuplicateSerialNumber
|
|
{
|
|
set
|
|
{
|
|
BrokenRules.Assert("SerialNumberDuplicate","PartSerial.Label.Error.SerialNumberNotUnique",
|
|
"SerialNumber",value);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// DateReceived
|
|
/// </summary>
|
|
public object DateReceived
|
|
{
|
|
get
|
|
{
|
|
return mDateReceived.DBValue;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if (!AyaBizUtils.SmartDateEquals(mDateReceived, value)) //Case 298
|
|
{
|
|
mDateReceived.DBValue = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// DateReceived internal version used
|
|
/// by inventory affecting objects
|
|
/// </summary>
|
|
internal SmartDate sdDateReceived
|
|
{
|
|
get
|
|
{
|
|
return mDateReceived;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mDateReceived != value)
|
|
{
|
|
mDateReceived = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Date consumed
|
|
/// </summary>
|
|
public object DateConsumed
|
|
{
|
|
get
|
|
{
|
|
return mDateConsumed.DBValue;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if (!AyaBizUtils.SmartDateEquals(mDateConsumed, value)) //Case 298
|
|
{
|
|
mDateConsumed.DBValue = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// WorkorderItemPartID record
|
|
/// indicates which workorder item part record this part
|
|
/// was used on
|
|
/// </summary>
|
|
public Guid WorkorderItemPartID
|
|
{
|
|
get
|
|
{
|
|
return mWorkorderItemPartID;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mWorkorderItemPartID!=value)
|
|
{
|
|
mWorkorderItemPartID = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ID of PartInventoryAdjustmentItem object
|
|
/// that caused the adjustment to happen
|
|
/// </summary>
|
|
public Guid AdjustmentID
|
|
{
|
|
get
|
|
{
|
|
return mAdjustmentID;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mAdjustmentID!=value)
|
|
{
|
|
mAdjustmentID = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Purchase order receipt record ID
|
|
/// Indicates which receipt this was entered from
|
|
/// </summary>
|
|
public Guid PurchaseOrderReceiptItemID
|
|
{
|
|
get
|
|
{
|
|
return mPurchaseOrderReceiptItemID;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mPurchaseOrderReceiptItemID!=value)
|
|
{
|
|
mPurchaseOrderReceiptItemID = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
///Available for consumption
|
|
/// </summary>
|
|
public bool Available
|
|
{
|
|
get
|
|
{
|
|
return mAvailable;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mAvailable!=value)
|
|
{
|
|
mAvailable = 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.PartSerial")
|
|
)
|
|
);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region System.object overrides
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override string ToString()
|
|
{
|
|
return "PartSerial" + mID.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public override bool Equals(Object obj)
|
|
{
|
|
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
|
|
PartSerial c=(PartSerial)obj;
|
|
return mID==c.mID;
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override int GetHashCode()
|
|
{
|
|
return ("PartSerial"+mID).GetHashCode();
|
|
}
|
|
#endregion
|
|
|
|
#region Static methods
|
|
|
|
|
|
/// <summary>
|
|
/// Create new PartSerial
|
|
/// </summary>
|
|
/// <returns>PartSerial</returns>
|
|
internal static PartSerial NewItem(RootObjectTypes ObjectType)
|
|
{
|
|
|
|
|
|
if(AyaBizUtils.Right(ObjectType)>(int)SecurityLevelTypes.ReadOnly)
|
|
{
|
|
PartSerial c = new PartSerial();
|
|
return c;
|
|
}
|
|
else
|
|
throw new System.Security.SecurityException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.PartSerial")));
|
|
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetch existing PartSerial
|
|
/// </summary>
|
|
internal static PartSerial GetItem(SafeDataReader dr,RootObjectTypes ObjectType)
|
|
{
|
|
|
|
if(AyaBizUtils.Right(ObjectType)>(int)SecurityLevelTypes.NoAccess)
|
|
{
|
|
PartSerial child = new PartSerial();
|
|
child.Fetch(dr,ObjectType);
|
|
return child;
|
|
}
|
|
else
|
|
throw new System.Security.SecurityException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.PartSerial")));
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Given a part serial record ID returns the serial number
|
|
/// that is stored in that part serial record or an empty
|
|
/// string if not found
|
|
/// </summary>
|
|
/// <param name="PartSerialID">Guid value of partserial record</param>
|
|
/// <returns>String serial number or empty string if not found</returns>
|
|
public static string GetSerialNumberFromPartSerialID(Guid PartSerialID)
|
|
{
|
|
//Case 499
|
|
//return SNHelper.GetSN(PartSerialID);
|
|
return PartSerialFetcher.GetSN(PartSerialID);
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region DAL DATA ACCESS
|
|
|
|
#region Fetch
|
|
/// <summary>
|
|
/// Populate this object from the values in the datareader passed to it
|
|
/// </summary>
|
|
private void Fetch(SafeDataReader dr,RootObjectTypes ObjectType)
|
|
{
|
|
//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");
|
|
|
|
//PartSerial fields
|
|
mPartID=dr.GetGuid("aPartID");
|
|
mPartWarehouseID=dr.GetGuid("aPartWarehouseID");
|
|
mWorkorderItemPartID=dr.GetGuid("aWorkorderItemPartID");
|
|
mAdjustmentID=dr.GetGuid("AADJUSTMENTID");
|
|
mPurchaseOrderReceiptItemID=dr.GetGuid("aPurchaseOrderReceiptItemID");
|
|
|
|
mDateReceived=DBUtil.ToLocal(dr.GetSmartDate("aDateReceived"));
|
|
mDateConsumed=DBUtil.ToLocal(dr.GetSmartDate("aDateConsumed"));
|
|
|
|
//Important: use property not internal field
|
|
//so that initial broken rule is unbroken on fetch
|
|
SerialNumber=dr.GetString("aSerialNumber");
|
|
|
|
mAvailable=dr.GetBoolean("AAVAILABLE");
|
|
|
|
//Get access rights level
|
|
bReadOnly=AyaBizUtils.Right(ObjectType)<(int)SecurityLevelTypes.ReadWrite;
|
|
MarkOld();
|
|
}
|
|
|
|
#endregion fetch
|
|
|
|
#region Update
|
|
/// <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,"aPartSerial");
|
|
|
|
#region Delete
|
|
if(IsDeleted)
|
|
{
|
|
if(!IsNew)
|
|
{
|
|
//Delete object and child objects
|
|
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aPartSerial 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 aPartSerial (aID, aPartID, aPartWarehouseID, aSerialNumber, aDateReceived, " +
|
|
"aDateConsumed, aWorkorderItemPartID, AADJUSTMENTID, AAVAILABLE, " +
|
|
"aPurchaseOrderReceiptItemID, aCreated, aModified, aCreator,aModifier) " +
|
|
"VALUES (@ID, @PartID,@PartWarehouseID,@SerialNumber,@DateReceived, " +
|
|
"@DateConsumed,@WorkorderItemPartID,@AdjustmentID,@Available, " +
|
|
"@PurchaseOrderReceiptItemID,@Created,@Modified,@CurrentUserID,@CurrentUserID)"
|
|
);
|
|
else
|
|
cm=DBUtil.GetCommandFromSQL(
|
|
"UPDATE aPartSerial SET aID=@ID, aPartID=@PartID, aPartWarehouseID=@PartWarehouseID, " +
|
|
"aSerialNumber=@SerialNumber, " +
|
|
"aDateReceived=@DateReceived, aDateConsumed=@DateConsumed, " +
|
|
"aWorkorderItemPartID=@WorkorderItemPartID, " +
|
|
"AADJUSTMENTID=@AdjustmentID, aPurchaseOrderReceiptItemID=@PurchaseOrderReceiptItemID, " +
|
|
"AAVAILABLE=@Available, aModifier=@CurrentUserID, " +
|
|
"aModified=@Modified WHERE aID=@ID"
|
|
);
|
|
|
|
|
|
|
|
//PartSerial fields
|
|
cm.AddInParameter("@PartID",DbType.Guid,mPartID);
|
|
cm.AddInParameter("@PartWarehouseID",DbType.Guid,mPartWarehouseID);
|
|
cm.AddInParameter("@WorkorderItemPartID",DbType.Guid,mWorkorderItemPartID);
|
|
cm.AddInParameter("@AdjustmentID",DbType.Guid,mAdjustmentID);
|
|
cm.AddInParameter("@PurchaseOrderReceiptItemID",DbType.Guid,mPurchaseOrderReceiptItemID);
|
|
cm.AddInParameter("@DateReceived",DbType.DateTime,DBUtil.ToUTC(mDateReceived).DBValue);
|
|
cm.AddInParameter("@DateConsumed",DbType.DateTime,DBUtil.ToUTC(mDateConsumed).DBValue);
|
|
cm.AddInParameter("@SerialNumber",DbType.String,mSerialNumber);
|
|
cm.AddInParameter("@Available",DbType.Boolean, mAvailable);
|
|
|
|
|
|
//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 update
|
|
|
|
#endregion
|
|
|
|
#region Get serial number from id
|
|
//Case 499
|
|
//[Serializable(), System.ComponentModel.Browsable(false)]
|
|
// public class SNHelper
|
|
//{
|
|
// string _SN = "";
|
|
// Guid _ID;
|
|
|
|
// public SNHelper(Guid ID)
|
|
// {
|
|
// _ID=ID;
|
|
|
|
// }
|
|
|
|
// public static string GetSN(Guid ID)
|
|
// { //todo fix for 425
|
|
// SNHelper e=new SNHelper(ID);
|
|
// DataPortal.Update(e);
|
|
// return e._SN;
|
|
// }
|
|
|
|
// public void DataPortal_Update()
|
|
// {
|
|
|
|
// this._SN=DBUtil.ScalarToString(
|
|
// DBUtil.GetScalarFromSQLString(
|
|
// "SELECT aSerialNumber FROM aPartSerial WHERE " +
|
|
// "(aID = @ID)",_ID)
|
|
// );
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
#endregion
|
|
|
|
}//end PartSerial
|
|
|
|
}//end namespace GZTW.AyaNova.BLL |