/// <summary>
        /// Deletes the ba deposit.
        /// </summary>
        /// <param name="refId">The reference identifier.</param>
        /// <returns></returns>
        public SUTransferResponse DeleteSUTransfer(string refId)
        {
            var response = new SUTransferResponse {
                Acknowledge = AcknowledgeType.Success
            };

            try
            {
                var suTransferEntity = SUTransferDao.GetSUTransfer(refId);

                if (suTransferEntity == null)
                {
                    response.Acknowledge = AcknowledgeType.Failure;
                    response.Message     = "Dữ liệu cần xóa không tồn tại!";
                    return(response);
                }
                using (var scope = new TransactionScope())
                {
                    #region Update account balance
                    // Cập nhật giá trị vào account balance trước khi xóa
                    response.Message = UpdateAccountBalance(suTransferEntity);
                    if (response.Message != null)
                    {
                        response.Acknowledge = AcknowledgeType.Failure;
                        scope.Dispose();
                        return(response);
                    }
                    #endregion

                    //Xóa bảng SUTransfer
                    response.Message = SUTransferDao.DeleteSUTransfer(suTransferEntity);
                    if (!string.IsNullOrEmpty(response.Message))
                    {
                        response.Acknowledge = AcknowledgeType.Failure;
                        return(response);
                    }

                    //Xóa bảng OriginalGeneralLedgerDao
                    response.Message = OriginalGeneralLedgerDao.DeleteOriginalGeneralLedger(suTransferEntity.RefId);
                    if (response.Message != null)
                    {
                        response.Acknowledge = AcknowledgeType.Failure;
                        scope.Dispose();
                        return(response);
                    }

                    //Xóa bảng SupplyLedger
                    response.Message = SupplyLedgerDao.DeleteSupplyLedgerByRefId(suTransferEntity.RefId, suTransferEntity.RefType);
                    if (response.Message != null)
                    {
                        response.Acknowledge = AcknowledgeType.Failure;
                        scope.Dispose();
                        return(response);
                    }

                    scope.Complete();
                }
                response.RefId = suTransferEntity.RefId;
                return(response);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return(response);
            }
        }
        /// <summary>
        /// Updates the ba deposit.
        /// </summary>
        /// <param name="suTransferEntity">The su transfer entity.</param>
        /// <returns></returns>
        public SUTransferResponse UpdateSUTransfer(SUTransferEntity suTransferEntity)
        {
            var response = new SUTransferResponse {
                Acknowledge = AcknowledgeType.Success
            };

            try
            {
                if (!suTransferEntity.Validate())
                {
                    foreach (var error in suTransferEntity.ValidationErrors)
                    {
                        response.Message += error + Environment.NewLine;
                    }
                    response.Acknowledge = AcknowledgeType.Failure;
                    return(response);
                }
                using (var scope = new TransactionScope())
                {
                    var suTransfer = SUTransferDao.GetSUTransfer(suTransferEntity.RefNo.Trim(), suTransferEntity.PostedDate);
                    if (suTransfer != null && suTransfer.PostedDate.Year == suTransferEntity.PostedDate.Year)
                    {
                        if (suTransfer.RefId != suTransferEntity.RefId)
                        {
                            response.Acknowledge = AcknowledgeType.Failure;
                            response.Message     = @"Số chứng từ " + suTransferEntity.RefNo + @" đã tồn tại !";
                            return(response);
                        }
                    }

                    response.Message = SUTransferDao.UpdateSUTransfer(suTransferEntity);
                    if (!string.IsNullOrEmpty(response.Message))
                    {
                        response.Acknowledge = AcknowledgeType.Failure;
                        return(response);
                    }

                    #region Update account balance
                    //Trừ đi số tiền của chứng từ cũ trước khi cộng thêm số tiền mới

                    UpdateAccountBalance(suTransferEntity);
                    if (response.Message != null)
                    {
                        response.Acknowledge = AcknowledgeType.Failure;
                        scope.Dispose();
                        return(response);
                    }

                    #endregion

                    #region Delete detail and insert detail

                    // Xóa bảng SUTransferDetail
                    response.Message = SUTransferDetailDao.DeleteSUTransferDetailByRefId(suTransferEntity.RefId);
                    if (!string.IsNullOrEmpty(response.Message))
                    {
                        response.Acknowledge = AcknowledgeType.Failure;
                        return(response);
                    }

                    // Xóa bảng OriginalGeneralLedger
                    response.Message = OriginalGeneralLedgerDao.DeleteOriginalGeneralLedger(suTransferEntity.RefId);
                    if (response.Message != null)
                    {
                        response.Acknowledge = AcknowledgeType.Failure;
                        scope.Dispose();
                        return(response);
                    }

                    // Xóa bảng SupplyLedger
                    response.Message = SupplyLedgerDao.DeleteSupplyLedgerByRefId(suTransferEntity.RefId, suTransferEntity.RefType);
                    if (response.Message != null)
                    {
                        response.Acknowledge = AcknowledgeType.Failure;
                        scope.Dispose();
                        return(response);
                    }

                    if (suTransferEntity.SUTransferDetails != null)
                    {
                        foreach (var suTransferDetail in suTransferEntity.SUTransferDetails)
                        {
                            if (suTransferEntity.RefType == (int)BuCA.Enum.RefType.SUTransfer)
                            {
                                AutoMapper(GetUnitsInDepartment(suTransferDetail.InventoryItemId, suTransferDetail.FromDepartmentId, suTransferDetail.Quantity, suTransferDetail.Description), response);
                                if (response.Acknowledge == AcknowledgeType.Failure)
                                {
                                    return(response);
                                }
                            }

                            suTransferDetail.RefDetailId = Guid.NewGuid().ToString();
                            suTransferDetail.RefId       = suTransferEntity.RefId;
                            response.Message             = SUTransferDetailDao.InsertSUTransferDetail(suTransferDetail);
                            if (!string.IsNullOrEmpty(response.Message))
                            {
                                response.Acknowledge = AcknowledgeType.Failure;
                                return(response);
                            }

                            #region Insert into AccountBalance

                            // Cộng thêm số tiền mới sau khi sửa chứng từ
                            InsertAccountBalance(suTransferEntity, suTransferDetail);
                            if (response.Message != null)
                            {
                                response.Acknowledge = AcknowledgeType.Failure;
                                scope.Dispose();
                                return(response);
                            }

                            #endregion

                            #region Insert OriginalGeneralLedger
                            var originalGeneralLedgerEntity = new OriginalGeneralLedgerEntity
                            {
                                OriginalGeneralLedgerId = Guid.NewGuid().ToString(),
                                RefType           = suTransferEntity.RefType,
                                RefId             = suTransferEntity.RefId,
                                RefDetailId       = suTransferDetail.RefDetailId,
                                RefDate           = suTransferEntity.RefDate,
                                RefNo             = suTransferEntity.RefNo,
                                Amount            = suTransferDetail.Amount,
                                BudgetChapterCode = suTransferDetail.BudgetChapterCode,
                                CreditAccount     = suTransferDetail.CreditAccount,
                                DebitAccount      = suTransferDetail.DebitAccount,
                                Description       = suTransferDetail.Description,
                                JournalMemo       = suTransferEntity.JournalMemo,
                                SortOrder         = suTransferDetail.SortOrder,
                                PostedDate        = suTransferEntity.PostedDate,

                                // Không có Currency trong db : mặc định VNĐ và 1
                                CurrencyCode = "VND",
                                ExchangeRate = 1,
                            };
                            response.Message = OriginalGeneralLedgerDao.InsertOriginalGeneralLedger(originalGeneralLedgerEntity);
                            if (!string.IsNullOrEmpty(response.Message))
                            {
                                response.Acknowledge = AcknowledgeType.Failure;
                                return(response);
                            }

                            #endregion

                            #region Insert SupplyLedger
                            if (suTransferDetail.InventoryItemId != null)
                            {
                                var supplyLedgerEntity = new SupplyLedgerEntity
                                {
                                    SupplyLedgerId    = Guid.NewGuid().ToString(),
                                    RefId             = suTransferEntity.RefId,
                                    RefType           = suTransferEntity.RefType,
                                    RefNo             = suTransferEntity.RefNo,
                                    RefDate           = suTransferEntity.RefDate,
                                    PostedDate        = suTransferEntity.PostedDate,
                                    DepartmentId      = suTransferDetail.FromDepartmentId,
                                    InventoryItemId   = suTransferDetail.InventoryItemId,
                                    Unit              = suTransferDetail.Unit,
                                    UnitPrice         = suTransferDetail.UnitPrice,
                                    IncrementQuantity = 0,
                                    DecrementQuantity = suTransferDetail.Quantity,
                                    IncrementAmount   = 0,
                                    DecrementAmount   = suTransferDetail.Amount,
                                    JournalMemo       = suTransferEntity.JournalMemo,
                                    Description       = suTransferDetail.Description,
                                    AccountNumber     = suTransferDetail.DebitAccount,
                                    RefDetailId       = suTransferDetail.RefDetailId
                                };
                                response.Message = SupplyLedgerDao.InsertSupplyLedger(supplyLedgerEntity);
                                if (!string.IsNullOrEmpty(response.Message))
                                {
                                    response.Acknowledge = AcknowledgeType.Failure;
                                    return(response);
                                }

                                //insert lan 2
                                supplyLedgerEntity.SupplyLedgerId    = Guid.NewGuid().ToString();
                                supplyLedgerEntity.AccountNumber     = suTransferDetail.CreditAccount;
                                supplyLedgerEntity.DepartmentId      = suTransferDetail.ToDepartmentId;
                                supplyLedgerEntity.IncrementQuantity = suTransferDetail.Quantity;
                                supplyLedgerEntity.DecrementQuantity = 0;
                                supplyLedgerEntity.IncrementAmount   = suTransferDetail.Amount;
                                supplyLedgerEntity.DecrementAmount   = 0;

                                response.Message = SupplyLedgerDao.InsertSupplyLedger(supplyLedgerEntity);
                                if (!string.IsNullOrEmpty(response.Message))
                                {
                                    response.Acknowledge = AcknowledgeType.Failure;
                                    return(response);
                                }
                            }
                            #endregion
                        }
                    }

                    #endregion

                    scope.Complete();
                }
                response.RefId = suTransferEntity.RefId;
                return(response);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return(response);
            }
        }