using System;
using System.Data;
using Ease.Core.DataAccess;
using HRM.BO;


namespace HRM.DA
{
    public class AssetInventoryDA
    {
        #region Insert

        internal static void Insert(TransactionContext tc, AssetInventory oAssetInventory)
        {
            string sSQL = SQLParser.MakeSQL(
                "INSERT INTO AssetInventory(AssetInventoryID, AssetID, AllocatedUserID, AllocationDate," +
                " AllocationRemarks, AllocatorUserID, IsEmpConfirmed, IsEmpRetained, RetainRemarks, RetainUserID, RetainDate,Amount, CreatedBy, CreationDate)" +
                " VALUES(%n, %n, %n, %d, %s, %n,%b,%b,%s,%n,%d,%n, %n,%d)",
                oAssetInventory.ID, oAssetInventory.AssetID,
                DataReader.GetNullValue(oAssetInventory.ReceiveEmployeeID, 0),
                oAssetInventory.AssignedDate, oAssetInventory.AssignedRemarks,
                DataReader.GetNullValue(oAssetInventory.AssignUserID, 0),
                oAssetInventory.IsEmpConfirmed, oAssetInventory.IsEmpRetained, oAssetInventory.RetainRemarks,
                DataReader.GetNullValue(oAssetInventory.RetainUserID, 0), oAssetInventory.RetainDate,
                oAssetInventory.Amount,
                DataReader.GetNullValue(oAssetInventory.CreatedBy),
                DataReader.GetNullValue(oAssetInventory.CreatedDate));
            tc.ExecuteNonQuery(sSQL);
        }

        #endregion

        #region Get

        internal static IDataReader Get(TransactionContext tc, int id)
        {
            return tc.ExecuteReader("SELECT * FROM AssetInventory WHERE AssetInventoryID=%n", id);
        }

        internal static IDataReader Get(TransactionContext tc)
        {
            return tc.ExecuteReader("SELECT * FROM AssetInventory");
        }

        internal static IDataReader GetAllAsset(TransactionContext tc, int assignedUserID, int assignUserID)
        {
            return tc.ExecuteReader("SELECT * FROM AssetInventory WHERE AllocatedUserID=%n And AllocatorUserID=%n",
                assignedUserID, assignUserID);
        }

        internal static IDataReader GetAllAssets(TransactionContext tc, int assignedUserID)
        {
            return tc.ExecuteReader("SELECT * FROM AssetInventory WHERE AllocatedUserID=%n", assignedUserID);
        }


        internal static IDataReader GetAllAsset(TransactionContext tc, int assignedUserID)
        {
            return tc.ExecuteReader("SELECT * FROM AssetInventory WHERE AllocatedUserID=%n ", assignedUserID);
        }

        internal static IDataReader GetReceivedItems(TransactionContext tc, int receiveEmpoyeeID)
        {
            return tc.ExecuteReader(
                "SELECT * FROM AssetInventory WHERE AllocatedUserID=%n And IsEmpRetained=0 And IsEmpConfirmed=1",
                receiveEmpoyeeID);
        }

        internal static IDataReader GetAssets(TransactionContext tc, int assignedUserID, bool isEmpConfirmed,
            int assignUserID)
        {
            string sSQL =
                SQLParser.MakeSQL(
                    "SELECT * FROM AssetInventory WHERE AllocatedUserID=%n AND IsEmpConfirmed=%b And AllocatorUserID=%n",
                    assignedUserID, isEmpConfirmed, assignUserID);
            return tc.ExecuteReader(sSQL);
        }

        internal static IDataReader GetReturnedItems(TransactionContext tc, int receiveEmpid)
        {
            return tc.ExecuteReader("SELECT * FROM AssetInventory WHERE AllocatedUserID=%n And IsEmpRetained=1",
                receiveEmpid);
        }

        internal static DataSet GetAssetsClearance(TransactionContext tc, int receiveEmpid)
        {
            string sSQL = SQLParser.MakeSQL(
                @"SELECT e.NAME AllocatorName,e.EMAILADDRESS AllocatorEmail,a.NAME, ai.* FROM AssetInventory ai,employee e,ASSET a 
            WHERE ai.ASSETID=a.ASSETID AND ai.ALLOCATORUSERID=e.EMPLOYEEID AND ai.ISEMPCONFIRMED=1 AND ai.ISEMPRETAINED<>1 AND a.DEFAULTITEM=1 and AllocatedUserID=%n",
                receiveEmpid);
            return tc.ExecuteDataSet(sSQL);
        }

        internal static IDataReader GetEmpAssignedNotReceiveConfirm(TransactionContext tc, int employeeID)
        {
            return tc.ExecuteReader("SELECT * FROM AssetInventory WHERE AllocatedUserID=%n And IsEmpConfirmed=0",
                employeeID);
        }

        #endregion

        #region Update

        internal static void Update(TransactionContext tc, AssetInventory oAssetInventory)
        {
            string sSQL = SQLParser.MakeSQL(
                "UPDATE AssetInventory SET AssetID=%n,AllocatedUserID=%n,AllocationDate=%d," +
                " AllocationREMARKS=%s,AllocatorUserID=%n,IsEmpConfirmed=%b,EmpConfirmationDate=%d," +
                " EmpConfirmationRemarks=%s,RetainUserID=%n,RetainDate=%d,RetainRemarks=%s,IsEmpRetained=%b," +
                "Amount=%n, ModifiedBy=%n,ModificationDate=%d WHERE AssetInventoryID=%n",
                oAssetInventory.AssetID, oAssetInventory.ReceiveEmployeeID,
                oAssetInventory.AssignedDate, oAssetInventory.AssignedRemarks,
                oAssetInventory.AssignUserID,
                DataReader.GetNullValue(oAssetInventory.IsEmpConfirmed),
                DataReader.GetNullValue(oAssetInventory.empConfirmDate),
                DataReader.GetNullValue(oAssetInventory.EmpConfirmRemarks),
                DataReader.GetNullValue(oAssetInventory.RetainUserID),
                DataReader.GetNullValue(oAssetInventory.RetainDate),
                DataReader.GetNullValue(oAssetInventory.RetainRemarks),
                DataReader.GetNullValue(oAssetInventory.IsEmpRetained),
                oAssetInventory.Amount,
                DataReader.GetNullValue(oAssetInventory.ModifiedBy),
                DataReader.GetNullValue(oAssetInventory.ModifiedDate),
                oAssetInventory.ID);
            tc.ExecuteNonQuery(sSQL);
        }

        internal static void UpdateEmpConfirm(TransactionContext tc, AssetInventory oAssetInventory)
        {
            string sSQL = SQLParser.MakeSQL(@"UPDATE AssetInventory SET IsEmpConfirmed=%b,
                                            EmpConfirmDate=%d,EmpConfirmRemarks=%s,
                                            [ModifiedBy]=%n,[ModificationDate]=%d 
                                            where AssetInventoryID=%n",
                oAssetInventory.IsEmpConfirmed, oAssetInventory.empConfirmDate,
                oAssetInventory.EmpConfirmRemarks,
                DataReader.GetNullValue(oAssetInventory.ModifiedBy),
                DataReader.GetNullValue(oAssetInventory.ModifiedDate),
                oAssetInventory.ID);
            tc.ExecuteNonQuery(sSQL);
        }

        internal static void UpdateRetainAsset(TransactionContext tc, AssetInventory oAssetInventory)
        {
            string sSQL = SQLParser.MakeSQL(@"UPDATE AssetInventory SET RetainUserID=%n,
                 RetainDate=%d,RetainRemarks=%s,IsEmpRetained=%b,Amount=%n,ModifiedBy=%n,ModificationDate=%d
                  where AssetInventoryID=%n",
                oAssetInventory.RetainUserID,
                oAssetInventory.RetainDate,
                oAssetInventory.RetainRemarks, oAssetInventory.IsEmpRetained, oAssetInventory.Amount,
                DataReader.GetNullValue(oAssetInventory.ModifiedBy),
                DataReader.GetNullValue(oAssetInventory.ModifiedDate),
                oAssetInventory.ID);
            tc.ExecuteNonQuery(sSQL);
        }

        #endregion

        #region Delete

        internal static void Delete(TransactionContext tc, int id)
        {
            tc.ExecuteNonQuery("DELETE FROM AssetInventory WHERE AssetInventoryID=%n", id);
        }

        internal static void Delete(TransactionContext tc, int AssignedUserID, int AssignUserID)
        {
            tc.ExecuteNonQuery("DELETE FROM AssetInventory WHERE  AllocatedUSERID=%n And AllocatorUserID=%n ",
                AssignedUserID, AssignUserID);
        }

        #endregion
    }
}