Exemplo n.º 1
0
        public static void Cancel(int id, JewelleryDBEntities db)
        {
            var order = db.Order.Single(q => q.Id == id);

            if (order.OrderState != OrderStates.WAITING.ToString())
            {
                throw new BusinessException("Bu sipariş iptal edilemez. Sipariş: {0}", order.Id);
            }

            using (var ts = db.Database.BeginTransaction())
            {
                try
                {
                    var orderHistory = OrderMapper.MapToHistory(order);

                    orderHistory.UpdateTime = DateTime.Now;

                    OrderHistoryBL.Create(orderHistory, db);

                    order.OrderState = OrderStates.CANCELLED.ToString();

                    db.SaveChanges();

                    ts.Commit();
                }
                catch (Exception)
                {
                    ts.Rollback();
                    throw;
                }
            }
        }
Exemplo n.º 2
0
        public static void ChangeState(int id, int?responsibleAccountId, JewelleryDBEntities db)
        {
            var order = db.Order.Single(q => q.Id == id);

            if (order.OrderState == OrderStates.READY.ToString() || order.OrderState == OrderStates.CANCELLED.ToString())
            {
                throw new BusinessException("Bu sipariş zaten hazırlanmıştır. Sipariş: {0}", order.Id);
            }

            var orderState = (OrderStates)Enum.Parse(typeof(OrderStates), order.OrderState);
            var nexState   = orderState;

            AccountDTO responsibleAccount = null;

            switch (orderState)
            {
            case OrderStates.WAITING:
            case OrderStates.BOILER:
            case OrderStates.STONECUTTER:
            case OrderStates.POLISHER:
            {
                if (responsibleAccountId != null)
                {
                    responsibleAccount = AccountBL.TryGet(responsibleAccountId.Value, db);
                    if (responsibleAccount == null)
                    {
                        throw new BusinessException("Yönlendirilecek hesap bilgisi bulunamadı.");
                    }
                }
            }
            break;
            }

            switch (orderState)
            {
            case OrderStates.WAITING:
            {
                if (responsibleAccount.Role != Roles.Boiler)
                {
                    throw new BusinessException("Sipariş bu hesaba atanamaz.");
                }

                nexState = OrderStates.BOILER;
            }
            break;

            case OrderStates.BOILER:
            {
                if (responsibleAccount == null)
                {
                    nexState = OrderStates.STONECUTTER;
                }
                else
                {
                    if (responsibleAccount.Role != Roles.StoneCutter)
                    {
                        throw new BusinessException("Sipariş bu hesaba atanamaz.");
                    }
                }
            }
            break;

            case OrderStates.STONECUTTER:
            {
                if (responsibleAccount == null)
                {
                    nexState = OrderStates.POLISHER;
                }
                else
                {
                    if (responsibleAccount.Role != Roles.StoneCutter)
                    {
                        throw new BusinessException("Sipariş bu hesaba atanamaz.");
                    }
                }
            }
            break;

            case OrderStates.POLISHER:
            {
                if (responsibleAccount == null)
                {
                    nexState = OrderStates.READY;
                }
                else
                {
                    if (responsibleAccount.Role != Roles.Polisher)
                    {
                        throw new BusinessException("Sipariş bu hesaba atanamaz.");
                    }
                }
            }
            break;
            }

            using (var ts = db.Database.BeginTransaction())
            {
                try
                {
                    var orderHistory = OrderMapper.MapToHistory(order);

                    orderHistory.UpdateTime = DateTime.Now;

                    OrderHistoryBL.Create(orderHistory, db);

                    order.ResponsibleAccountId = responsibleAccountId;
                    order.OrderState           = nexState.ToString();
                    order.UpdateTime           = DateTime.Now;

                    db.SaveChanges();

                    ts.Commit();
                }
                catch (Exception)
                {
                    ts.Rollback();
                    throw;
                }
            }
        }