//
        // GET: /Transfer/
        public ActionResult Index()
        {
            Transfer srchTransfer = new Transfer { TransferCode = "", TransferDate = (DateTime) SqlDateTime.MinValue, FromBranch = 0, ToBranch = 0 };
            List<Transfer> transferList = TransferManager.SearchTransfer(srchTransfer, "dini");
            List<TransferModel> transferModelList = Utility.ConvetrToList<TransferModel, Transfer>(transferList);

            return View();
        }
 public static bool UpdateTransfer(Transfer transfer, string executedBy)
 {
     return TransferDAO.Update(transfer, executedBy);
 }
 public static List<Transfer> SearchTransfer(Transfer transfer, string executedBy)
 {
     return TransferDAO.Search(transfer, executedBy);
 }
 public static bool AddTransfer(Transfer transfer, string executedBy)
 {
     return TransferDAO.Insert(transfer, executedBy);
 }
        public static bool Update(Transfer transfer, string executedBy)
        {
            bool rslt = false;

            try
            {
                Database db = DatabaseFactory.CreateDatabase(Constants.DBConnection);
                DbCommand cmd = db.GetStoredProcCommand(Constants.SP_Transfer_Update);

                connection = db.CreateConnection();
                connection.Open();
                transaction = connection.BeginTransaction();

                db.AddInParameter(cmd, "TransferID", DbType.Int32, transfer.TransferID);
                db.AddInParameter(cmd, "TransferDate", DbType.DateTime, transfer.TransferDate);
                db.AddInParameter(cmd, "FromBranch", DbType.Int32, transfer.FromBranch);
                db.AddInParameter(cmd, "ToBranch", DbType.Int32, transfer.ToBranch);
                db.AddInParameter(cmd, "IsReceived", DbType.Boolean, transfer.IsReceived);
                 db.AddInParameter(cmd, "RecievedBy", DbType.Int32, transfer.RecievedBy);
                db.AddInParameter(cmd, "IsApproved", DbType.Boolean, transfer.IsApproved);
                db.AddInParameter(cmd, "ApprovedBy", DbType.Int32, transfer.ApprovedBy);
                db.AddInParameter(cmd, "UpdatedBY", DbType.Int32, transfer.UpdatedBY);
                db.AddInParameter(cmd, "LastUpdatedDate", DbType.Int32, transfer.UpdatedDate);

                db.ExecuteNonQuery(cmd, transaction);

                bool dtlUpdated;
                foreach (TransferDetail trDtl in transfer.TransferItems)
                {
                    dtlUpdated = UpdateDetail(trDtl, transaction, executedBy);
                    if (dtlUpdated == false)
                    {
                        throw new Exception("Failed to update transfer detail/s");
                    }
                }

                transaction.Commit();
                rslt = true;
            }

            catch (Exception ex)
            {
                transaction.Rollback();
                rslt = false;
                throw ex;
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                {
                    connection.Close();
                }
            }

            return rslt;
        }
        public static List<Transfer> Search(Transfer srchTransfer, string executedBy)
        {
            List<Transfer> transferList = null;

            try
            {
                Database db = DatabaseFactory.CreateDatabase(Constants.DBConnection);
                DbCommand cmd = db.GetStoredProcCommand(Constants.SP_Transfer_Search);

                db.AddInParameter(cmd, "TransferCode", DbType.String, srchTransfer.TransferCode);
                db.AddInParameter(cmd, "TransferDate", DbType.DateTime, srchTransfer.TransferDate);
                db.AddInParameter(cmd, "FromBranch", DbType.Int32, srchTransfer.FromBranch);
                db.AddInParameter(cmd, "ToBranch", DbType.Int32, srchTransfer.ToBranch);

                DataSet ds = db.ExecuteDataSet(cmd);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null)
                {
                    transferList = Utility.DataTableToCollection<Transfer>(ds.Tables[0]);
                }

            }

            catch (Exception ex)
            {
                transferList = null;
            }

            return transferList;
        }
        public static bool Insert(Transfer transfer, string executedBy)
        {
            bool rslt = false;

            try
            {
                Database db = DatabaseFactory.CreateDatabase(Constants.DBConnection);
                DbCommand cmd = db.GetStoredProcCommand(Constants.SP_Transfer_Insert);

                connection = db.CreateConnection();
                    connection.Open();
                    transaction = connection.BeginTransaction();

                db.AddInParameter(cmd, "TransferDate", DbType.DateTime, transfer.TransferDate);
              db.AddInParameter(cmd, "FromBranch", DbType.Int32, transfer.FromBranch);
                db.AddInParameter(cmd, "ToBranch", DbType.Int32, transfer.ToBranch);
                //db.AddInParameter(cmd, "TransferDate", DbType.DateTime, transfer.IsReceived);
                 db.AddInParameter(cmd, "IsApproved", DbType.Boolean, transfer.IsApproved);
                db.AddInParameter(cmd, "ApprovedBy", DbType.DateTime, transfer.ApprovedBy);
                db.AddInParameter(cmd, "CreatedBy", DbType.Int32, transfer.CreatedBy);

                db.AddOutParameter(cmd, "NewID", DbType.Int32, 4);

                db.ExecuteNonQuery(cmd, transaction);

                int newID = 0;
                int.TryParse(db.GetParameterValue(cmd, "NewID").ToString(), out newID);

                if (newID > 0)
                {
                    bool dtlInserted;
                    foreach (TransferDetail trDtl in transfer.TransferItems)
                    {
                        dtlInserted = InsertDetail(trDtl, transaction, executedBy);
                        if (dtlInserted == false)
                       {
                           throw new Exception("Failed to insert transfer detail/s");
                       }
                    }

                    transaction.Commit();
                    rslt = true;
                }
               }

            catch (Exception ex)
            {
                transaction.Rollback();
                rslt = false;
                throw ex;
            }
            finally
            {
                if(connection.State == ConnectionState.Open)
                {
                    connection.Close();
                 }
            }

            return rslt;
        }