public void UpdateOrderMode(long orderToStoreId, string sOrderId, string sStatus, string sMode, string sModeCharge, DateTime sPromiseTime)
        {
            var orderToStore = new OrderToStore
            {
                OrderToStoreId  = orderToStoreId,
                OrderAtoId      = sOrderId,
                LastStatus      = sStatus,
                OrderMode       = sMode,
                OrderModeCharge = sModeCharge,
                PromiseTime     = sPromiseTime
            };

            DbEntities.OrderToStore.Attach(orderToStore);

            var entry = DbEntities.Entry(orderToStore);

            entry.Property(e => e.OrderAtoId).IsModified      = true;
            entry.Property(e => e.LastStatus).IsModified      = true;
            entry.Property(e => e.OrderMode).IsModified       = true;
            entry.Property(e => e.PromiseTime).IsModified     = true;
            entry.Property(e => e.OrderModeCharge).IsModified = true;

            DbEntities.SaveChanges();
            SaveLogOrderToStore(orderToStore, "Se consulta el histórico de la orden", sStatus, DateTime.Now, true);
        }
        public void SaveOrderToStore(OrderModelDto model)
        {
            var now = DateTime.Now;

            var orderToStore = new OrderToStore
            {
                LastStatus       = OrderStatus.NEW_READY_TO_SEND,
                AddressId        = model.AddressInfo.AddressId ?? SettingsData.Constants.Entities.NULL_ID_INT,
                LastUpdate       = now,
                ClientPhoneId    = model.PhoneId,
                ClientId         = model.ClientId ?? SettingsData.Constants.Entities.NULL_ID_INT,
                FranchiseId      = model.FranchiseId,
                FranchiseStoreId = (int)(model.Store.IdKey ?? SettingsData.Constants.Entities.NULL_ID_INT),
                PosOrderId       = model.PosOrder.Id ?? SettingsData.Constants.Entities.NULL_ID_INT,
                StartDatetime    = now,
                UserInsId        = model.UserId,
                PaymentId        = model.OrderDetails.Payment.Id,
                PosOrderStatus   = model.OrderDetails.PosOrderStatus,
                ExtraNotes       = model.OrderDetails.ExtraNotes,
                InputType        = SettingsData.Constants.StoreOrder.INPUT_TYPE_DELIVERY
            };

            DbEntities.OrderToStore.Add(orderToStore);
            SaveLogOrderToStore(orderToStore, "Se almacenó la información del pedido. Listo para el envío a la sucursal " + model.Store.Value, orderToStore.LastStatus, now);
            DbEntities.SaveChanges();
            model.OrderToStoreId = orderToStore.OrderToStoreId;
            model.Status         = orderToStore.LastStatus;
        }
        public void UpdateOrderStatus(long orderToStoreId, string sStatus, DateTime sPromiseTime)
        {
            var orderToStore = new OrderToStore
            {
                OrderToStoreId      = orderToStoreId,
                LastStatus          = sStatus,
                PromiseTime         = sPromiseTime,
                FailedStatusCounter = 0
            };


            DbEntities.OrderToStore.Attach(orderToStore);

            var entry = DbEntities.Entry(orderToStore);

            entry.Property(e => e.LastStatus).IsModified          = true;
            entry.Property(e => e.PromiseTime).IsModified         = true;
            entry.Property(e => e.FailedStatusCounter).IsModified = true;

            if (SettingsData.Constants.TrackConst.OrderStatusEnd.Contains(orderToStore.LastStatus))
            {
                orderToStore.EndDatetime = DateTime.Now;
                entry.Property(e => e.EndDatetime).IsModified = true;
            }

            DbEntities.SaveChanges();
            SaveLogOrderToStore(orderToStore, "Se consulta el histórico de la orden", sStatus, DateTime.Now, true);
        }
        public void UpdateOrderStatusFailedRetrieve(long orderToStoreId, int failedStatusCounter)
        {
            var orderToStore = new OrderToStore
            {
                OrderToStoreId      = orderToStoreId,
                FailedStatusCounter = failedStatusCounter + 1
            };

            DbEntities.OrderToStore.Attach(orderToStore);
            var entry = DbEntities.Entry(orderToStore);

            entry.Property(e => e.FailedStatusCounter).IsModified = true;
            DbEntities.SaveChanges();
        }
        public OrderToStoreLog SaveLogOrderToStore(OrderToStore orderToStore, string comments, string status, DateTime timestamp, bool bHasToSave = false)
        {
            var orderToStoreLog = new OrderToStoreLog
            {
                Comments     = comments,
                OrderToStore = orderToStore,
                Status       = status,
                Timestamp    = timestamp
            };

            DbEntities.OrderToStoreLog.Add(orderToStoreLog);
            if (bHasToSave)
            {
                DbEntities.SaveChanges();
            }

            return(orderToStoreLog);
        }
        public void SetCancelOrderToStore(long orderToStoreId)
        {
            var now          = DateTime.Now;
            var orderToStore = new OrderToStore
            {
                OrderToStoreId   = orderToStoreId,
                IsCanceled       = true,
                DateTimeCanceled = now,
                LastStatus       = string.Empty
            };

            DbEntities.OrderToStore.Attach(orderToStore);
            DbEntities.Entry(orderToStore).Property(e => e.IsCanceled).IsModified       = true;
            DbEntities.Entry(orderToStore).Property(e => e.DateTimeCanceled).IsModified = true;
            DbEntities.SaveChanges();
            ((IObjectContextAdapter)DbEntities).ObjectContext.Detach(orderToStore);

            SaveLogOrderToStore(orderToStoreId, "Cancelado por el cliente", SettingsData.Constants.TrackConst.CANCELED, now, true);
        }
        public OrderToStoreLog SaveLogOrderToStore(long orderId, string comments, string status, DateTime timestamp, bool bHasToSave = false)
        {
            var orderToStoreLog = new OrderToStoreLog
            {
                Comments       = comments,
                OrderToStoreId = orderId,
                Status         = status,
                Timestamp      = timestamp
            };

            DbEntities.OrderToStoreLog.Add(orderToStoreLog);

            var orderToStore = new OrderToStore
            {
                OrderToStoreId = orderId,
                LastStatus     = status
            };

            DbEntities.OrderToStore.Attach(orderToStore);

            var entry = DbEntities.Entry(orderToStore);

            entry.Property(e => e.LastStatus).IsModified = true;

            if (SettingsData.Constants.TrackConst.OrderStatusEnd.Any(e => e == status))
            {
                orderToStore.EndDatetime = timestamp;
                entry.Property(e => e.EndDatetime).IsModified = true;
            }

            if (bHasToSave)
            {
                DbEntities.SaveChanges();
            }

            return(orderToStoreLog);
        }