/// <summary>
 /// 退款预检查.必须有退款调整单且状态为待审核&必须由退款调整单单据且状态为审核通过。
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="soBaseInfo"></param>
 /// <param name="refundBalanceInfo"></param>
 private void VerifyForRefund(RefundBalanceInfo entity, out SOInfo soInfo, out RefundBalanceInfo refundBalanceInfo)
 {
     refundBalanceInfo = GetRefundBalanceBySysNo(entity.SysNo.Value);
     if (refundBalanceInfo == null)
     {
         throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "RefundBalanceNotExists"));
     }
     if (refundBalanceInfo.Status != RefundBalanceStatus.WaitingRefund)
     {
         throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "Refund_RefundBalanceStatusValid"));
     }
     if (refundBalanceInfo.CashAmt == null)
     {
         throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "CashAmtRequired"));
     }
     if (refundBalanceInfo.CashAmt.Value >= 0)
     {
         SOIncomeRefundInfo incomeBankInfo = ExternalDomainBroker.GetSOIncomeRefundInfo(entity.SysNo.Value, RefundOrderType.RO_Balance);
         if (incomeBankInfo == null)
         {
             throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "Refund_SOIncomeRefundInfoValid"));
         }
         else if (incomeBankInfo.Status != ECCentral.BizEntity.Invoice.RefundStatus.Audit)
         {
             throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "Refund_SOIncomeStatusValid"));
         }
     }
     soInfo = ExternalDomainBroker.GetSOInfo(entity.OriginalSOSysNo.Value);
     if (soInfo == null)
     {
         throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "SONotExists"));
     }
 }
        /// <summary>
        /// 根据退款调整单系统编号获取退款单信息
        /// </summary>
        /// <param name="sysNo"></param>
        /// <returns></returns>
        public virtual RefundBalanceInfo LoadRefundBalanceBySysNo(int sysNo)
        {
            SOIncomeRefundInfo soIncomRefundInfo = new SOIncomeRefundInfo();

            RefundBalanceInfo refundBalanceInfo = GetRefundBalanceBySysNo(sysNo);

            soIncomRefundInfo = ExternalDomainBroker.GetSOIncomeRefundInfo(sysNo, RefundOrderType.RO_Balance);
            refundBalanceInfo.IncomeBankInfo = soIncomRefundInfo;
            refundBalanceInfo.CustomerID     = ExternalDomainBroker.GetCustomerBasicInfo(refundBalanceInfo.CustomerSysNo.Value).CustomerID;
            return(refundBalanceInfo);
        }
        /// <summary>
        /// 根据退款单系统编号获取新退款调整单基本信息
        /// </summary>
        /// <param name="OrgRefundSysNo"></param>
        /// <returns></returns>
        public virtual RefundBalanceInfo LoadNewRefundBalanceByRefundSysNo(int refundSysNo)
        {
            RefundBalanceInfo refundBalanceInfo = refundBalanceDA.LoadNewRefundBalanceByRefundSysNo(refundSysNo);

            if (refundBalanceInfo == null)
            {
                throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "Void_RefundBalanceInfo"));
            }
            refundBalanceInfo.IncomeBankInfo = ExternalDomainBroker.GetSOIncomeRefundInfo(refundBalanceInfo.OriginalRefundSysNo.Value, RefundOrderType.RO);
            refundBalanceInfo.CustomerID     = ExternalDomainBroker.GetCustomerBasicInfo(refundBalanceInfo.CustomerSysNo.Value).CustomerID;
            return(refundBalanceInfo);
        }
        /// <summary>
        /// 作废退款调整单
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Abandon(int sysNo)
        {
            RefundBalanceInfo refundBalanceInfo = GetRefundBalanceBySysNo(sysNo);

            if (refundBalanceInfo == null)
            {
                throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "RefundBalanceNotExists"));
            }
            if (refundBalanceInfo.Status != RefundBalanceStatus.WaitingRefund)
            {
                throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "Void_RefundBalanceStatusValid"));
            }
            RefundBalanceInfo newEntity = new RefundBalanceInfo()
            {
                SysNo  = refundBalanceInfo.SysNo,
                Status = RefundBalanceStatus.Abandon
            };

            SOIncomeRefundInfo incomeBankInfo = ExternalDomainBroker.GetSOIncomeRefundInfo(refundBalanceInfo.SysNo.Value, RefundOrderType.RO_Balance);

            TransactionScopeFactory.TransactionAction(() =>
            {
                if (incomeBankInfo != null)
                {
                    if (incomeBankInfo.Status != RefundStatus.Origin)
                    {
                        throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "Void_SOIncomeStatusValid"));
                    }
                    ExternalDomainBroker.AbandonSOIncomeRefundForROBalance(incomeBankInfo.SysNo.Value);

                    //20130808 Chester Added: RO_Balance取消审核 - 待办事项:
                    EventPublisher.Publish <RMAROBalanceCancelAuditMessage>(new RMAROBalanceCancelAuditMessage()
                    {
                        SOIncomeRefundSysNo = incomeBankInfo.SysNo.Value,
                        CurrentUserSysNo    = ServiceContext.Current.UserSysNo
                    });
                }

                refundBalanceDA.UpdateRefundBalance(newEntity);

                //20130808 Chester Added 完成RMA退款调整单待审核 - 待办事项:
                EventPublisher.Publish <RMACompleteRefundBalanceWaitingForAuditMessage>(new RMACompleteRefundBalanceWaitingForAuditMessage()
                {
                    RefundBalanceSysNo = refundBalanceInfo.SysNo.Value,
                    RefundSysNo        = refundBalanceInfo.OriginalRefundSysNo.Value,
                    CurrentUserSysNo   = ServiceContext.Current.UserSysNo
                });
            });
        }
        /// <summary>
        /// 提交审核(审核银行信息)
        /// </summary>
        /// <param name="entity"></param>
        public virtual RefundBalanceInfo SubmitAudit(RefundBalanceInfo entity)
        {
            RefundBalanceInfo refundBalanceInfo = GetRefundBalanceBySysNo(entity.SysNo.Value);

            if (refundBalanceInfo == null)
            {
                throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "RefundBalanceNotExists"));
            }
            refundBalanceInfo.PointAmt = refundBalanceInfo.PointAmt ?? 0;

            if (refundBalanceInfo.Status != RefundBalanceStatus.WaitingRefund)
            {
                throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "Audit_RefundBalanceWaitingRefundValid"));
            }
            if (refundBalanceInfo.CashAmt == null)
            {
                throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "CashAmtRequired"));
            }
            if (refundBalanceInfo.CashAmt.Value < 0)
            {
                throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "Audit_CashAmtValid"));
            }
            SOIncomeRefundInfo oldIncomeBankInfo = ExternalDomainBroker.GetSOIncomeRefundInfo(entity.SysNo.Value, RefundOrderType.RO_Balance);
            SOIncomeRefundInfo newIncomeBankInfo = new SOIncomeRefundInfo()
            {
                RefundReason   = 9, // RefundReason.RefundBalance
                BankName       = entity.IncomeBankInfo.BankName,
                BranchBankName = entity.IncomeBankInfo.BranchBankName,
                CardNumber     = entity.IncomeBankInfo.CardNumber,
                CardOwnerName  = entity.IncomeBankInfo.CardOwnerName,
                PostAddress    = entity.IncomeBankInfo.PostAddress,
                PostCode       = entity.IncomeBankInfo.PostCode,
                ReceiverName   = entity.IncomeBankInfo.ReceiverName,
                Note           = entity.IncomeBankInfo.Note,
                CompanyCode    = refundBalanceInfo.CompanyCode
            };

            TransactionScopeFactory.TransactionAction(() =>
            {
                if (oldIncomeBankInfo == null)
                {
                    newIncomeBankInfo.RefundPayType = refundBalanceInfo.RefundPayType;
                    newIncomeBankInfo.SOSysNo       = refundBalanceInfo.OriginalSOSysNo;
                    newIncomeBankInfo.OrderType     = RefundOrderType.RO_Balance;
                    newIncomeBankInfo.OrderSysNo    = refundBalanceInfo.SysNo;
                    newIncomeBankInfo.HaveAutoRMA   = false;

                    if (newIncomeBankInfo.RefundPayType == RefundPayType.CashRefund)
                    {
                        newIncomeBankInfo.Status = ECCentral.BizEntity.Invoice.RefundStatus.Audit;
                    }
                    else
                    {
                        newIncomeBankInfo.Status = ECCentral.BizEntity.Invoice.RefundStatus.Origin;
                    }

                    if (newIncomeBankInfo.RefundPayType == RefundPayType.TransferPointRefund)
                    {
                        newIncomeBankInfo.RefundCashAmt = 0;
                        newIncomeBankInfo.RefundPoint   = Convert.ToInt32(Decimal.Round((refundBalanceInfo.CashAmt ?? 0M)
                                                                                        * pointExchangeRate, 0));
                        if (refundBalanceInfo.PointAmt > 0)//如果有积分累加到bankInfo的PointAmt字段上
                        {
                            newIncomeBankInfo.RefundPoint += refundBalanceInfo.PointAmt;
                        }
                    }
                    else
                    {
                        newIncomeBankInfo.RefundCashAmt = refundBalanceInfo.CashAmt;
                        newIncomeBankInfo.RefundPoint   = refundBalanceInfo.PointAmt;
                    }
                    newIncomeBankInfo.RefundGiftCard = refundBalanceInfo.GiftCardAmt;

                    ExternalDomainBroker.CreateSOIncomeRefundInfo(newIncomeBankInfo);
                }
                else if (oldIncomeBankInfo.Status == RefundStatus.Origin ||
                         (oldIncomeBankInfo.Status != RefundStatus.Origin && oldIncomeBankInfo.RefundPayType == RefundPayType.CashRefund))
                {
                    newIncomeBankInfo.SysNo          = oldIncomeBankInfo.SysNo;
                    newIncomeBankInfo.OrderType      = oldIncomeBankInfo.OrderType;
                    newIncomeBankInfo.RefundPayType  = oldIncomeBankInfo.RefundPayType;
                    newIncomeBankInfo.RefundReason   = oldIncomeBankInfo.RefundReason;
                    newIncomeBankInfo.HaveAutoRMA    = oldIncomeBankInfo.HaveAutoRMA;
                    newIncomeBankInfo.RefundCashAmt  = oldIncomeBankInfo.RefundCashAmt;
                    newIncomeBankInfo.RefundGiftCard = oldIncomeBankInfo.RefundGiftCard;
                    newIncomeBankInfo.RefundPoint    = oldIncomeBankInfo.RefundPoint;
                    newIncomeBankInfo.ToleranceAmt   = oldIncomeBankInfo.ToleranceAmt;
                    newIncomeBankInfo.Status         = oldIncomeBankInfo.Status;

                    ExternalDomainBroker.UpdateSOIncomeRefundInfo(newIncomeBankInfo);
                }
                else if (oldIncomeBankInfo.Status == RefundStatus.Abandon)
                {
                    throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "Audit_RefundBalanceAbandonStatusValid"));
                }
                else
                {
                    throw new BizException(ResouceManager.GetMessageString("RMA.RefundBalance", "Audit_RefundBalanceStatusValid"));
                }
                entity.IncomeBankInfo = newIncomeBankInfo;

                //完成RMA退款调整单待审核 - 待办事项:
                EventPublisher.Publish <RMACompleteRefundBalanceWaitingForAuditMessage>(new RMACompleteRefundBalanceWaitingForAuditMessage()
                {
                    RefundBalanceSysNo = entity.SysNo.Value,
                    RefundSysNo        = entity.OriginalRefundSysNo.Value,
                    CurrentUserSysNo   = ServiceContext.Current.UserSysNo
                });
            });
            return(entity);
        }