/// <summary>
        /// Deletes the ca payment.
        /// </summary>
        /// <param name="refId">The reference identifier.</param>
        /// <returns>
        /// CAPaymentResponse.
        /// </returns>
        public BUPlanReceiptResponse DeleteBUPlanReceipt(string refId)
        {
            var buPlanReceiptResponse = new BUPlanReceiptResponse {
                Acknowledge = AcknowledgeType.Success
            };

            using (var scope = new TransactionScope())
            {
                var buPlanReceiptEntityForDelete = BUPlanReceiptDao.GetBUPlanReceiptEntitybyRefId(refId);

                #region Update account balance
                // Cập nhật giá trị vào account balance trước khi xóa
                buPlanReceiptResponse.Message = UpdateAccountBalance(buPlanReceiptEntityForDelete);
                if (buPlanReceiptResponse.Message != null)
                {
                    buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                    scope.Dispose();
                    return(buPlanReceiptResponse);
                }

                #endregion

                buPlanReceiptResponse.Message = BUPlanReceiptDao.DeleteBUPlanReceipt(buPlanReceiptEntityForDelete);
                if (buPlanReceiptResponse.Message != null)
                {
                    buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                    scope.Dispose();
                    return(buPlanReceiptResponse);
                }

                //Xóa bảng GeneralLedger
                buPlanReceiptResponse.Message = GeneralLedgerDao.DeleteGeneralLedger(buPlanReceiptEntityForDelete.RefId);
                if (buPlanReceiptResponse.Message != null)
                {
                    buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                    scope.Dispose();
                    return(buPlanReceiptResponse);
                }

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

            return(buPlanReceiptResponse);
        }
        /// <summary>
        /// Updates the ca payment.
        /// </summary>
        /// <param name="buPlanReceiptEntity">The bu plan receipt entity.</param>
        /// <returns>
        /// CAPaymentResponse.
        /// </returns>
        public BUPlanReceiptResponse UpdateBUPlanReceipt(BUPlanReceiptEntity buPlanReceiptEntity)
        {
            var buPlanReceiptResponse = new BUPlanReceiptResponse {
                Acknowledge = AcknowledgeType.Success
            };
            int?cashWithDrawTypeId;

            try
            {
                if (buPlanReceiptEntity != null && !buPlanReceiptEntity.Validate())
                {
                    foreach (var error in buPlanReceiptEntity.ValidationErrors)
                    {
                        buPlanReceiptResponse.Message += error + Environment.NewLine;
                    }
                    buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                    return(buPlanReceiptResponse);
                }

                using (var scope = new TransactionScope())
                {
                    if (buPlanReceiptEntity != null)
                    {
                        if (buPlanReceiptEntity.RefType == (int)BuCA.Enum.RefType.BUPlanCancel)
                        {
                            cashWithDrawTypeId = 17;
                        }
                        else
                        {
                            cashWithDrawTypeId = null;
                        }

                        var buPlanReceiptForExisting = BUPlanReceiptDao.GetBUPlanReceipt(buPlanReceiptEntity.RefNo.Trim(), buPlanReceiptEntity.PostedDate);
                        if (buPlanReceiptForExisting != null && buPlanReceiptForExisting.PostedDate.Year == buPlanReceiptEntity.PostedDate.Year)
                        {
                            if (buPlanReceiptForExisting.RefId != buPlanReceiptEntity.RefId)
                            {
                                buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                                buPlanReceiptResponse.Message     = @"Số chứng từ '" + buPlanReceiptEntity.RefNo.Trim() + @"' đã tồn tại!";
                                return(buPlanReceiptResponse);
                            }
                        }

                        buPlanReceiptResponse.Message = BUPlanReceiptDao.UpdateBUPlanReceipt(buPlanReceiptEntity);
                        if (buPlanReceiptResponse.Message != null)
                        {
                            buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                            scope.Dispose();
                            return(buPlanReceiptResponse);
                        }

                        #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(buPlanReceiptEntity);
                        if (buPlanReceiptResponse.Message != null)
                        {
                            buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                            scope.Dispose();
                            return(buPlanReceiptResponse);
                        }

                        #endregion

                        // Xóa bảng BUPlanReceiptDetail
                        buPlanReceiptResponse.Message = BUPlanReceiptDetailDao.DeleteBUPlanReceiptDetail(buPlanReceiptEntity.RefId);
                        if (buPlanReceiptResponse.Message != null)
                        {
                            buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                            scope.Dispose();
                            return(buPlanReceiptResponse);
                        }

                        // Xóa bảng GeneralLedger
                        buPlanReceiptResponse.Message = GeneralLedgerDao.DeleteGeneralLedger(buPlanReceiptEntity.RefId);
                        if (buPlanReceiptResponse.Message != null)
                        {
                            buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                            scope.Dispose();
                            return(buPlanReceiptResponse);
                        }

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

                        foreach (var buPlanReceiptDetail in buPlanReceiptEntity.BuPlanReceiptDetails)
                        {
                            buPlanReceiptDetail.RefId       = buPlanReceiptEntity.RefId;
                            buPlanReceiptDetail.RefDetailId = Guid.NewGuid().ToString();

                            if (!buPlanReceiptDetail.Validate())
                            {
                                foreach (string error in buPlanReceiptDetail.ValidationErrors)
                                {
                                    buPlanReceiptResponse.Message += error + Environment.NewLine;
                                }
                                buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                                return(buPlanReceiptResponse);
                            }
                            buPlanReceiptResponse.Message = BUPlanReceiptDetailDao.InsertBUPlanReceiptDetail(buPlanReceiptDetail);
                            if (!string.IsNullOrEmpty(buPlanReceiptResponse.Message))
                            {
                                buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                                return(buPlanReceiptResponse);
                            }

                            #region Insert into AccountBalance

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

                            #endregion

                            #region Insert into GeneralLedger
                            if (buPlanReceiptDetail.DebitAccount != null)
                            {
                                var generalLedgerEntity = new GeneralLedgerEntity
                                {
                                    RefType               = buPlanReceiptEntity.RefType,
                                    RefNo                 = buPlanReceiptEntity.RefNo,
                                    ProjectId             = buPlanReceiptDetail.ProjectId,
                                    BudgetSourceId        = buPlanReceiptDetail.BudgetSourceId,
                                    Description           = buPlanReceiptDetail.Description,
                                    RefDetailId           = buPlanReceiptDetail.RefDetailId,
                                    ExchangeRate          = buPlanReceiptEntity.ExchangeRate,
                                    BudgetSubKindItemCode = buPlanReceiptDetail.BudgetSubKindItemCode,
                                    CurrencyCode          = buPlanReceiptEntity.CurrencyCode,
                                    BudgetKindItemCode    = buPlanReceiptDetail.BudgetKindItemCode,
                                    RefId                 = buPlanReceiptEntity.RefId,
                                    PostedDate            = buPlanReceiptEntity.PostedDate,
                                    BudgetItemCode        = buPlanReceiptDetail.BudgetItemCode,
                                    ListItemId            = buPlanReceiptDetail.ListItemId,
                                    BudgetSubItemCode     = buPlanReceiptDetail.BudgetSubItemCode,
                                    BudgetDetailItemCode  = buPlanReceiptDetail.BudgetDetailItemCode,
                                    AccountNumber         = buPlanReceiptDetail.DebitAccount,
                                    DebitAmount           = buPlanReceiptDetail.DebitAccount == null ? 0 : buPlanReceiptDetail.Amount,
                                    DebitAmountOC         = buPlanReceiptDetail.DebitAccount == null ? 0 : buPlanReceiptDetail.AmountOC,
                                    CreditAmount          = 0,
                                    CreditAmountOC        = 0,
                                    FundStructureId       = buPlanReceiptDetail.FundStructureId,
                                    GeneralLedgerId       = Guid.NewGuid().ToString(),
                                    JournalMemo           = buPlanReceiptEntity.JournalMemo,
                                    RefDate               = buPlanReceiptEntity.RefDate,
                                    SortOrder             = buPlanReceiptDetail.SortOrder,
                                    MethodDistributeId    = buPlanReceiptDetail.MethodDistributeId,
                                    CashWithDrawTypeId    = buPlanReceiptEntity.RefType == 51 ? 20 : (buPlanReceiptEntity.RefType == 52 ? 27 : 17),//cashWithDrawTypeId,
                                    BudgetChapterCode     = buPlanReceiptDetail.BudgetChapterCode,
                                    ContractId            = buPlanReceiptDetail.ContractId,
                                    CapitalPlanId         = buPlanReceiptDetail.CapitalPlanId
                                };

                                buPlanReceiptResponse.Message = GeneralLedgerDao.InsertGeneralLedger(generalLedgerEntity);

                                if (!string.IsNullOrEmpty(buPlanReceiptResponse.Message))
                                {
                                    buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                                    return(buPlanReceiptResponse);
                                }
                            }
                            #endregion

                            #region Insert OriginalGeneralLedger
                            var originalGeneralLedgerEntity = new OriginalGeneralLedgerEntity
                            {
                                OriginalGeneralLedgerId = Guid.NewGuid().ToString(),
                                RefType               = buPlanReceiptEntity.RefType,
                                RefId                 = buPlanReceiptEntity.RefId,
                                RefDetailId           = buPlanReceiptDetail.RefDetailId,
                                RefDate               = buPlanReceiptEntity.RefDate,
                                RefNo                 = buPlanReceiptEntity.RefNo,
                                Amount                = buPlanReceiptDetail.Amount,
                                AmountOC              = buPlanReceiptDetail.AmountOC,
                                BankId                = buPlanReceiptDetail.BankId,
                                BudgetChapterCode     = buPlanReceiptDetail.BudgetChapterCode,
                                BudgetDetailItemCode  = buPlanReceiptDetail.BudgetDetailItemCode,
                                BudgetItemCode        = buPlanReceiptDetail.BudgetItemCode,
                                BudgetKindItemCode    = buPlanReceiptDetail.BudgetKindItemCode,
                                BudgetSourceId        = buPlanReceiptDetail.BudgetSourceId,
                                BudgetSubItemCode     = buPlanReceiptDetail.BudgetSubItemCode,
                                BudgetSubKindItemCode = buPlanReceiptDetail.BudgetSubKindItemCode,
                                DebitAccount          = buPlanReceiptDetail.DebitAccount,
                                Description           = buPlanReceiptDetail.Description,
                                FundStructureId       = buPlanReceiptDetail.FundStructureId,
                                JournalMemo           = buPlanReceiptEntity.JournalMemo,
                                ProjectId             = buPlanReceiptDetail.ProjectId,
                                ToBankId              = buPlanReceiptDetail.BankId,
                                SortOrder             = buPlanReceiptDetail.SortOrder,
                                PostedDate            = buPlanReceiptEntity.PostedDate,
                                CurrencyCode          = buPlanReceiptEntity.CurrencyCode,
                                ExchangeRate          = buPlanReceiptEntity.ExchangeRate,
                                CashWithDrawTypeId    = buPlanReceiptEntity.RefType == 51 ? 20 : (buPlanReceiptEntity.RefType == 52 ? 27 : 17),//cashWithDrawTypeId,
                                ContractId            = buPlanReceiptDetail.ContractId,
                            };
                            buPlanReceiptResponse.Message = OriginalGeneralLedgerDao.InsertOriginalGeneralLedger(originalGeneralLedgerEntity);
                            if (!string.IsNullOrEmpty(buPlanReceiptResponse.Message))
                            {
                                buPlanReceiptResponse.Acknowledge = AcknowledgeType.Failure;
                                return(buPlanReceiptResponse);
                            }

                            #endregion
                        }

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

                        buPlanReceiptResponse.RefId = buPlanReceiptEntity.RefId;
                    }
                    scope.Complete();
                }

                return(buPlanReceiptResponse);
            }
            catch (Exception ex)
            {
                buPlanReceiptResponse.Message = ex.Message;
                return(buPlanReceiptResponse);
            }
        }