/// <summary>
        /// 全退处方
        /// </summary>
        /// <param name="costHeadid">结算ID</param>
        /// <param name="operatoreid">操作员ID</param>
        /// <param name="refundPrescriptions">退费处方</param>
        /// <param name="refundInvoiceNO">退费票据号</param>
        public void AllRefund(int costHeadid, int operatoreid, List <Prescription> refundPrescriptions, string refundInvoiceNO)
        {
            /*
             * 1:获取当前退费操作员结账ID
             * 2:修改原结算记录OP_CostHead CostStatus=1
             * 3:新增红冲结算记录 CostStatus = 2,写入对冲OldID,金额为负
             * 4:新增红冲结算明细记录 OP_CostDetail
             * 5:新增红冲结算支付方式记录 OP_CostPayMentInfo
             * 6:修改原费用主表 OP_FeeItemHead ChargeStatus=1
             * 7:新增红冲费用主表记录ChargeStatus=2,写入对冲OldID,金额为负
             * 8:新增红冲费用明细表记录 OP_FeeItemDetail
             * 9:修改退费消息表OP_FeeRefundHead RefundPayFlag = 1
             * 10:减会员积分
             * 11:加虚拟库存
             * 12:修改op_account汇总金额
             */
            int      iAccountType = 0;
            DateTime chargedate   = DateTime.Now;

            //获取当前操作员得到当前结账ID
            int         curAccountId = NewObject <CommonMethod>().GetAccountId(operatoreid, iAccountType);
            OP_CostHead oldCostHead  = NewObject <OP_CostHead>().getmodel(costHeadid) as OP_CostHead;

            if (oldCostHead.CostStatus != 0)
            {
                throw new Exception("该处方已经被退费");
            }

            List <OP_FeeRefundHead> feerefundheadlist = NewObject <OP_FeeRefundHead>().getlist <OP_FeeRefundHead>("invoicenum='" + refundInvoiceNO + "' and flag=0");

            if (feerefundheadlist == null || feerefundheadlist.Count == 0)
            {
                throw new Exception("退费消息已经删除");
            }

            //再次判断退费消息,是不是存在已经删除又修改状态
            List <OP_FeeRefundDetail> feerefundDetailList = NewObject <OP_FeeRefundDetail>().getlist <OP_FeeRefundDetail>(" reheadid=" + feerefundheadlist[0].ReHeadID);

            //返回需要补收的处方记录
            foreach (Prescription refundPresc in refundPrescriptions)
            {
                foreach (OP_FeeRefundDetail refundDetail in feerefundDetailList)
                {
                    if (refundPresc.FeeItemHeadID == refundDetail.FeeItemHeadID && refundPresc.PresDetailID == refundDetail.FeeItemDetailID)
                    {
                        if (refundPresc.Refundamount != refundDetail.RefundAmount)
                        {
                            throw new Exception("退费消息已经修改,请重新获取退费消息");
                        }
                    }
                }
            }

            int refundPosType = Convert.ToInt32(NewObject <CommonMethod>().GetOpConfig(OpConfigConstant.RefundPosType));

            //原结算记录状态修改
            oldCostHead.CostStatus = 1; //状态变为退费状态
            this.BindDb(oldCostHead);
            oldCostHead.save();

            //新结算主表
            OP_CostHead newCostHead = oldCostHead.Clone() as OP_CostHead;

            newCostHead.CostHeadID = 0;
            newCostHead.CostStatus = 2;            //状态为空冲
            newCostHead.AccountID  = curAccountId; //定入新的结算ID
            if (refundPosType == 0)                //pos退现金
            {
                newCostHead.CashFee = (oldCostHead.CashFee + oldCostHead.PosFee) * -1;
                newCostHead.PosFee  = 0;
            }
            else
            {
                newCostHead.CashFee = oldCostHead.CashFee * -1;
                newCostHead.PosFee  = oldCostHead.PosFee * -1;
            }

            newCostHead.TotalFee    = newCostHead.TotalFee * -1;
            newCostHead.PromFee     = newCostHead.PromFee * -1;
            newCostHead.OldID       = oldCostHead.CostHeadID;//写入原costHeadID
            newCostHead.ChargeEmpID = operatoreid;
            newCostHead.CostDate    = chargedate;
            newCostHead.RoundingFee = oldCostHead.RoundingFee * -1;
            this.BindDb(newCostHead);
            newCostHead.save();

            //新结算支付方式表
            List <OP_CostPayMentInfo> oldCostPayList = NewObject <OP_CostPayMentInfo>().getlist <OP_CostPayMentInfo>(" costheadid=" + oldCostHead.CostHeadID);

            if (refundPosType == 0)
            {
                oldCostPayList = oldCostPayList.Where(p => p.PayMentCode != "02").ToList();
                foreach (OP_CostPayMentInfo oldCostPay in oldCostPayList)
                {
                    OP_CostPayMentInfo newCostPay = oldCostPay.Clone() as OP_CostPayMentInfo;
                    newCostPay.ID         = 0;
                    newCostPay.CostHeadID = newCostHead.CostHeadID;
                    newCostPay.AccountID  = curAccountId;

                    //现金
                    if (oldCostPay.PayMentCode != "01")
                    {
                        //退现金另外处理
                        newCostPay.PayMentMoney = oldCostPay.PayMentMoney * -1;
                        this.BindDb(newCostPay);
                        newCostPay.save();
                    }
                }

                if (newCostHead.CashFee != 0)
                {
                    OP_CostPayMentInfo cashCostPay = new OP_CostPayMentInfo();
                    cashCostPay.CostHeadID   = newCostHead.CostHeadID;
                    cashCostPay.AccountID    = curAccountId;
                    cashCostPay.PatListID    = newCostHead.PatListID;
                    cashCostPay.PatName      = newCostHead.PatName;
                    cashCostPay.PatType      = newCostHead.PatTypeID.ToString();
                    cashCostPay.PayMentCode  = "01";
                    cashCostPay.PayMentID    = 1002;
                    cashCostPay.PayMentMoney = newCostHead.CashFee;
                    cashCostPay.PayMentName  = "现金支付";
                    this.BindDb(cashCostPay);
                    cashCostPay.save();
                }
            }
            else if (refundPosType == 1)
            {
                foreach (OP_CostPayMentInfo oldCostPay in oldCostPayList)
                {
                    OP_CostPayMentInfo newCostPay = oldCostPay.Clone() as OP_CostPayMentInfo;
                    newCostPay.ID           = 0;
                    newCostPay.CostHeadID   = newCostHead.CostHeadID;
                    newCostPay.AccountID    = curAccountId;
                    newCostPay.PayMentMoney = oldCostPay.PayMentMoney * -1;
                    this.BindDb(newCostPay);
                    newCostPay.save();
                }
            }

            //费用表插入红冲记录
            List <OP_FeeItemHead> oldFeeItemHeadList = NewObject <OP_FeeItemHead>().getlist <OP_FeeItemHead>(" costheadid=" + oldCostHead.CostHeadID);

            foreach (OP_FeeItemHead oldFeeHead in oldFeeItemHeadList)
            {
                oldFeeHead.ChargeStatus = 1;//退费状态
                this.BindDb(oldFeeHead);
                oldFeeHead.save();

                OP_FeeItemHead newFeeHead = oldFeeHead.Clone() as OP_FeeItemHead;
                newFeeHead.FeeItemHeadID = 0;
                newFeeHead.CostHeadID    = newCostHead.CostHeadID;
                newFeeHead.OldID         = oldFeeHead.FeeItemHeadID; //红冲退记录ID
                newFeeHead.ChargeStatus  = 2;                        //新插入记录为红冲记录
                newFeeHead.ChargeEmpID   = operatoreid;
                newFeeHead.ChargeDate    = chargedate;
                newFeeHead.TotalFee      = oldFeeHead.TotalFee * -1;
                this.BindDb(newFeeHead);
                newFeeHead.save();

                List <OP_FeeItemDetail> oldFeeItemDetailList = NewObject <OP_FeeItemDetail>().getlist <OP_FeeItemDetail>(" feeitemheadid=" + oldFeeHead.FeeItemHeadID);
                foreach (OP_FeeItemDetail oldFeeItemDetail in oldFeeItemDetailList)
                {
                    OP_FeeItemDetail newFeeItemDetail = oldFeeItemDetail.Clone() as OP_FeeItemDetail;
                    newFeeItemDetail.PresDetailID  = 0;
                    newFeeItemDetail.TotalFee      = oldFeeItemDetail.TotalFee * -1;
                    newFeeItemDetail.Amount        = oldFeeItemDetail.Amount * -1;
                    newFeeItemDetail.FeeItemHeadID = newFeeHead.FeeItemHeadID;
                    this.BindDb(newFeeItemDetail);
                    newFeeItemDetail.save();
                }
            }

            //新结算明细表
            List <OP_CostDetail> oldCostDetailList = NewObject <OP_CostDetail>().getlist <OP_CostDetail>("costheadid=" + oldCostHead.CostHeadID);

            foreach (OP_CostDetail oldCostDetail in oldCostDetailList)
            {
                OP_CostDetail newCostDetail = oldCostDetail.Clone() as OP_CostDetail;
                newCostDetail.CostDetailID  = 0;
                newCostDetail.CostHeadID    = newCostHead.CostHeadID;//写入新CostHeadID
                newCostDetail.TotalFee      = oldCostDetail.TotalFee * -1;
                newCostDetail.FeeItemHeadID = NewDao <IOPManageDao>().GetNewFeeItemHeadId(oldCostDetail.FeeItemHeadID);
                this.BindDb(newCostDetail);
                newCostDetail.save();
            }

            //退费消息表置退费状态
            List <OP_FeeRefundHead> refundHeadList = NewObject <OP_FeeRefundHead>().getlist <OP_FeeRefundHead>(" invoiceNum='" + refundInvoiceNO + "' and flag=0");

            if (refundHeadList.Count == 0)
            {
                throw new Exception("找不到退费消息");
            }

            OP_FeeRefundHead refundHead = refundHeadList[0] as OP_FeeRefundHead;

            refundHead.RefundPayFlag = 1;//退费完成状态
            this.BindDb(refundHead);
            refundHead.save();

            //减会员积分
            AddScore(newCostHead.MemberAccountID, newCostHead.TotalFee, newCostHead.CostHeadID.ToString(), operatoreid);

            //药品加虚拟库存
            MinisStorage(refundPrescriptions, true);
            AddAccoutFee(newCostHead, curAccountId, 0, 1);

            //修改医生站处方状态
            RefundDocPrsc(refundPrescriptions);
        }
        public ServiceResponseData SaveRefundMessage()
        {
            try
            {
                DataTable        dtPresc     = requestData.GetData <DataTable>(0);
                DataTable        dtMedical   = requestData.GetData <DataTable>(1);
                int              operatoreid = requestData.GetData <int>(2);
                OP_FeeRefundHead refundHead  = new OP_FeeRefundHead();
                refundHead.RefundDocID   = operatoreid;
                refundHead.RefundDate    = DateTime.Now;
                refundHead.RefundPayFlag = 0;
                refundHead.Flag          = 0;
                refundHead.PatListID     = dtPresc == null || dtPresc.Rows.Count == 0 ? Convert.ToInt32(dtMedical.Rows[0]["patlistid"]):  Convert.ToInt32(dtPresc.Rows[0]["patlistid"]);
                refundHead.PatName       = dtPresc == null || dtPresc.Rows.Count == 0 ? dtMedical.Rows[0]["patname"].ToString(): dtPresc.Rows[0]["patname"].ToString();
                refundHead.InvoiceNum    = dtPresc == null || dtPresc.Rows.Count == 0 ? dtMedical.Rows[0]["invoiceNO"].ToString():dtPresc.Rows[0]["invoiceNO"].ToString();
                this.BindDb(refundHead);
                refundHead.save();

                if (dtPresc != null)
                {
                    for (int i = 0; i < dtPresc.Rows.Count; i++)
                    {
                        if (dtPresc.Rows[i]["ItemName"].ToString().Trim() == "小   计")
                        {
                            continue;
                        }

                        OP_FeeRefundDetail refundDetail = new OP_FeeRefundDetail();
                        refundDetail.ReHeadID      = refundHead.ReHeadID;
                        refundDetail.FeeItemHeadID = Convert.ToInt32(dtPresc.Rows[i]["FeeItemHeadID"]);
                        OP_FeeItemHead opfeeitemhead = NewObject <OP_FeeItemHead>().getmodel(refundDetail.FeeItemHeadID) as OP_FeeItemHead;
                        if (opfeeitemhead.ChargeStatus != 0 && opfeeitemhead.ChargeFlag != 1 && opfeeitemhead.DistributeFlag != Convert.ToInt32(dtPresc.Rows[i]["DistributeFlag"]))
                        {
                            throw new Exception("该处方没有正常的收费记录");
                        }

                        refundDetail.DistributeFlag  = opfeeitemhead.DistributeFlag;//取数据库最新记录状态,防止并发状态
                        refundDetail.RefundFlag      = 0;
                        refundDetail.FeeItemDetailID = Convert.ToInt32(dtPresc.Rows[i]["PresDetailID"]);
                        refundDetail.ItemID          = Convert.ToInt32(dtPresc.Rows[i]["ItemID"]);
                        refundDetail.ItemName        = dtPresc.Rows[i]["ItemName"].ToString();
                        refundDetail.OldAmount       = Convert.ToDecimal(dtPresc.Rows[i]["Amount"]);

                        decimal refundminimun    = Convert.ToDecimal(dtPresc.Rows[i]["RefundMiniNum"]);
                        decimal refundpacknum    = Convert.ToDecimal(dtPresc.Rows[i]["RefundPackNum"]);
                        decimal refundpresamount = Convert.ToDecimal(dtPresc.Rows[i]["refundpresamount"]);
                        decimal unitNO           = Convert.ToDecimal(dtPresc.Rows[i]["UnitNO"]);
                        decimal refundamount     = ((refundpacknum * unitNO) + refundminimun) * refundpresamount;
                        refundDetail.RefundAmount     = refundamount;
                        refundDetail.NewAmount        = refundDetail.OldAmount - refundDetail.RefundAmount;
                        refundDetail.RefundFee        = Convert.ToDecimal(dtPresc.Rows[i]["RefundFee"]);
                        refundDetail.RefundPresAmount = refundpresamount;
                        this.BindDb(refundDetail);
                        refundDetail.save();
                    }
                }

                if (dtMedical != null)
                {
                    //根据组合项目生成明细
                    for (int i = 0; i < dtMedical.Rows.Count; i++)
                    {
                        int            feeItemHeadID = Convert.ToInt32(dtMedical.Rows[i]["FeeItemHeadID"]);
                        OP_FeeItemHead opfeeitemhead = NewObject <OP_FeeItemHead>().getmodel(feeItemHeadID) as OP_FeeItemHead;
                        if (opfeeitemhead.ChargeStatus != 0 && opfeeitemhead.ChargeFlag != 1 && opfeeitemhead.DistributeFlag != Convert.ToInt32(dtMedical.Rows[i]["DistributeFlag"]))
                        {
                            throw new Exception("该处方没有正常的收费记录");
                        }

                        List <OP_FeeItemDetail> list = NewObject <OP_FeeItemDetail>().getlist <OP_FeeItemDetail>(" FeeItemHeadID=" + feeItemHeadID + " and ExamItemID=" + dtMedical.Rows[i]["ExamItemID"] + " ");
                        foreach (OP_FeeItemDetail detail in list)
                        {
                            OP_FeeRefundDetail refundDetail = new OP_FeeRefundDetail();
                            refundDetail.ReHeadID        = refundHead.ReHeadID;
                            refundDetail.FeeItemHeadID   = feeItemHeadID;
                            refundDetail.DistributeFlag  = opfeeitemhead.DistributeFlag;//取数据库最新记录状态,防止并发状态
                            refundDetail.RefundFlag      = 0;
                            refundDetail.FeeItemDetailID = detail.PresDetailID;
                            refundDetail.ItemID          = detail.ItemID;
                            refundDetail.ItemName        = detail.ItemName;
                            refundDetail.OldAmount       = detail.Amount;
                            if (dtMedical.Rows[i]["Sel"] != DBNull.Value &&
                                Convert.ToInt32(dtMedical.Rows[i]["Sel"]) == 1)
                            {
                                refundDetail.RefundAmount = detail.Amount;
                            }
                            else
                            {
                                refundDetail.RefundAmount = 0;
                            }

                            refundDetail.NewAmount        = refundDetail.OldAmount - refundDetail.RefundAmount;
                            refundDetail.RefundFee        = detail.RetailPrice * refundDetail.RefundAmount;
                            refundDetail.RefundPresAmount = detail.PresAmount;
                            this.BindDb(refundDetail);
                            refundDetail.save();
                        }
                    }
                }

                responseData.AddData(true);
                return(responseData);
            }
            catch (Exception err)
            {
                throw new Exception(err.Message);
            }
        }