private static void DoApprove(ISession session, int appId, ApproveStatus status, string appNote)
        {
            if (_approvingItems.Contains(appId))
            {
                throw new Exception("该单据正在处理签核结果,请不要重复提交");
            }
            lock (_lockObj)
            {
                if (_approvingItems.Contains(appId))
                {
                    throw new Exception("该单据正在处理签核结果,请不要重复提交");
                }
                _approvingItems.Add(appId);

                try
                {
                    IApprovable        appOrder = null;
                    OrderApproveResult ar       = OrderApproveResult.Retrieve(session, appId);
                    if (ar == null)
                    {
                        throw new Exception("Invalidate approve result " + appId.ToString());
                    }
                    OrderApproveItem ai = OrderApproveItem.Retrieve(session, ar.ApproveID);
                    if (ai == null)
                    {
                        throw new Exception(string.Format("Invalidate approve item {0}", ar.ApproveID));
                    }
                    if (ar.HasFinished)
                    {
                        throw new Exception(string.Format("该单据已经签核完成,请不要重复提交 {0}", ar.ApproveID));
                    }

                    ar.ActiveItem    = false;
                    ar.HasFinished   = true;
                    ar.ApproveResult = status == ApproveStatus.Approve;
                    ar.ApproveTime   = DateTime.Now;
                    ar.ApproveNote   = appNote;
                    ar.Update(session, "ActiveItem", "HasFinished", "ApproveResult", "ApproveTime", "ApproveNote");

                    #region 驳回处理
                    if (status == ApproveStatus.Reject)
                    {
                        appOrder = ERPUtil.GetApproveItem(session, ai.OrderTypeCode, ai.OrderNumber);
                        if (appOrder == null)
                        {
                            throw new Exception(string.Format("Approve item for {{{0}, {1}}} not found", ai.OrderTypeCode, ai.OrderNumber));
                        }

                        //删除掉后面还没有签核的记录
                        session.CreateEntityQuery <OrderApproveResult>()
                        .Where(Exp.Eq("ApproveID", ai.ApproveID))
                        .And(Exp.Eq("HasFinished", false))
                        .Delete();
                        //更新签核主记录状态
                        ai.Status      = ApproveStatus.Reject;
                        ai.ApproveTime = DateTime.Now;
                        ai.Update(session, "Status", "ApproveTime");
                        //单据的签核状态
                        appOrder.ApproveResult = ApproveStatus.Reject;
                        appOrder.ApproveTime   = DateTime.Now;
                        appOrder.ApproveUser   = ar.ApproveUser;
                        appOrder.ApproveNote   = appNote;
                        appOrder.Update(session, "ApproveResult", "ApproveTime", "ApproveUser", "ApproveNote");
                        //执行回调方法
                        appOrder.OnApprove(session);
                        return;
                    }
                    #endregion

                    #region 签核通过处理
                    IList <OrderApproveResult> unfinished = session.CreateEntityQuery <OrderApproveResult>()
                                                            .Where(Exp.Eq("ApproveID", ar.ApproveID))
                                                            .And(Exp.Eq("HasFinished", false))
                                                            .OrderBy("ApproveResultID")
                                                            .SetLastOffset(1)
                                                            .List <OrderApproveResult>();
                    OrderApproveResult next = null;
                    if (unfinished != null && unfinished.Count > 0)
                    {
                        next = unfinished[0];
                    }

                    if (next != null)
                    {
                        //单据签核过程还没有结束
                        next.ActiveItem = true;
                        next.Update(session, "ActiveItem");
                    }
                    else
                    {
                        //单据已经完成签核
                        appOrder = ERPUtil.GetApproveItem(session, ai.OrderTypeCode, ai.OrderNumber);
                        if (appOrder == null)
                        {
                            throw new Exception(string.Format("Approve item for {{{0}, {1}}} not found", ai.OrderTypeCode, ai.OrderNumber));
                        }

                        //更新签核主记录状态
                        ai.Status      = ApproveStatus.Approve;
                        ai.ApproveTime = DateTime.Now;
                        ai.Update(session, "Status", "ApproveTime");
                        //被签核单据的状态
                        appOrder.ApproveResult = ApproveStatus.Approve;
                        appOrder.ApproveTime   = DateTime.Now;
                        appOrder.ApproveUser   = ar.ApproveUser;
                        appOrder.ApproveNote   = appNote;
                        appOrder.Update(session, "ApproveResult", "ApproveTime", "ApproveUser", "ApproveNote");
                        //执行回调方法
                        appOrder.OnApprove(session);
                        session.PostCommit += appOrder.PostApprove;
                    }
                    #endregion
                }
                catch
                {
                    throw;
                }
                finally
                {
                    _approvingItems.Remove(appId);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 送签单据
        /// </summary>
        /// <param name="session"></param>
        /// <param name="order"></param>
        public static void ApproveThis(ISession session, IApprovable order)
        {
            log.DebugFormat("submit order {0} for approve", order.OrderNumber);
            OrderTypeDef typeDef = OrderTypeDef.Retrieve(session, order.OrderTypeCode);

            log.DebugFormat("OrderTypeDef {0} retrieved", order.OrderTypeCode);
            //不需要签核
            if (!typeDef.NeedApprove)
            {
                log.DebugFormat("order type {0} don't need approval", order.OrderTypeCode);
                order.ApproveResult = ApproveStatus.Approve;
                order.Update(session, "ApproveResult");
                order.OnApprove(session);
                session.PostCommit += order.PostApprove;
                return;
            }

            //检查该单据是否已经有未完成的签核项,避免重复生成
            int count = session.CreateEntityQuery <OrderApproveItem>()
                        .Where(Exp.Eq("OrderTypeCode", order.OrderTypeCode)
                               & Exp.Eq("OrderNumber", order.OrderNumber)
                               & Exp.Eq("Status", ApproveStatus.UnApprove))
                        .Count();

            if (count > 0)
            {
                log.DebugFormat("order {0} has {1} unfinished approve items, maybe it's already in approving state", order.OrderNumber, count);
                throw new Exception(typeDef.TypeText + order.OrderNumber + "已经在签核中");
            }

            IList <OrderApproveDef> appDefines = session.CreateEntityQuery <OrderApproveDef>()
                                                 .Where(Exp.Eq("OrderTypeCode", order.OrderTypeCode))
                                                 .OrderBy("StepIndex")
                                                 .List <OrderApproveDef>();

            if (appDefines == null || appDefines.Count <= 0)
            {
                log.DebugFormat("no OrderApproveDef exists for order type {0}", order.OrderTypeCode);
                throw new Exception(string.Format("Approve defines error for order type {0}", order.OrderTypeCode));
            }
            else
            {
                log.DebugFormat("{0} OrderApproveDef retrieved", appDefines.Count);
            }

            //生成签核项
            OrderApproveItem appItem = new OrderApproveItem();

            appItem.OrderTypeCode = order.OrderTypeCode;
            appItem.OrderNumber   = order.OrderNumber;
            appItem.Status        = ApproveStatus.UnApprove;
            appItem.ApproveTime   = new DateTime(1900, 1, 1);
            appItem.SubmitUser    = Magic.Security.SecuritySession.CurrentUser.UserId;
            appItem.SubmitTime    = DateTime.Now;
            appItem.CreateUser    = order.CreateUser;
            appItem.CreateTime    = order.CreateTime;
            appItem.Create(session);
            log.DebugFormat("OrderApproveItem {0} created", appItem.ApproveID);

            for (int i = 0; i < appDefines.Count; i++)
            {
                OrderApproveResult appResult = new OrderApproveResult();
                appResult.ApproveID   = appItem.ApproveID;
                appResult.StepIndex   = appDefines[i].StepIndex;
                appResult.ApproveUser = appDefines[i].UserID;
                appResult.HasFinished = false;
                if (i == 0)
                {
                    appResult.ActiveItem = true;
                }
                else
                {
                    appResult.ActiveItem = false;
                }
                appResult.ApproveResult = false;
                appResult.ApproveTime   = new DateTime(1900, 1, 1);
                appResult.ApproveNote   = " ";
                appResult.Create(session);
                log.DebugFormat("OrderApproveResult {0} created", appResult.ApproveResultID);
            }
        }