public StockReceived updateQuantityById(StockReceived stockReceived)
        {
            StockReceived stockReceivedExisting = (from sr in foodForAllContext.StockReceived where sr.Id == stockReceived.Id select sr).FirstOrDefault();

            stockReceivedExisting.Quantity = stockReceivedExisting.Quantity + stockReceived.Quantity;

            foodForAllContext.SaveChanges();

            Stock stock = (from s in foodForAllContext.Stock where s.Id == stockReceived.IdStock select s).FirstOrDefault();

            if (stock != null)
            {
                int quantity = stock.Quantity - stockReceived.Quantity;
                quantity = quantity < 0 ? 0 : quantity;

                stock.Quantity    = quantity;
                stock.IsAvailable = quantity == 0 ? false : true;
                stock.UpdateDate  = DateTime.Now;

                foodForAllContext.SaveChanges();

                if (!stock.IsAvailable)
                {
                    StockAvailable stockAvailable = (from sa in foodForAllContext.StockAvailable where sa.IdStock == stock.Id select sa).FirstOrDefault();
                    foodForAllContext.StockAvailable.Remove(stockAvailable);

                    foodForAllContext.SaveChanges();
                }
            }

            return(stockReceivedExisting);
        }
Example #2
0
        public StockAvailable create(StockAvailable stockAvailable)
        {
            stockAvailable.DateOfAdmission = DateTime.Now;
            foodForAllContext.StockAvailable.Add(stockAvailable);
            foodForAllContext.SaveChanges();

            return(stockAvailable);
        }
Example #3
0
        public StockAvailable destroyByIdStock(int idStock)
        {
            StockAvailable stockAvailable = (from sa in foodForAllContext.StockAvailable where sa.IdStock == idStock select sa).FirstOrDefault();

            foodForAllContext.StockAvailable.Remove(stockAvailable);
            foodForAllContext.SaveChanges();

            stockAvailable = (from sa in foodForAllContext.StockAvailable where sa.IdStock == idStock select sa).FirstOrDefault();

            return(stockAvailable);
        }
Example #4
0
        public ValidationResponse CheckSkuStockLevel(Sku sku, int requiredQuantity, Commerce.Models.Shops.Shop shop)
        {
            if (sku == null)
            {
                throw new ArgumentNullException("sku");
            }
            if (shop == null)
            {
                throw new ArgumentNullException("shop");
            }

            var validationResponse  = new ValidationResponse();
            var outOfStockThreshold = AppSettingsHelper.GetSafeValue(shop.Name + ".OutOfStockThreshold", 5);

            var _cache =
                (Dictionary <string, Tuple <DateTime, ValidationResponse> >)
                HttpContext.Current.Session["RCStockLookupCache"];

            if (_cache == null)
            {
                _cache = new Dictionary <string, Tuple <DateTime, ValidationResponse> >();
            }

            // 10 minute stock cache
            if (_cache.ContainsKey(sku.Code))
            {
                var tup = _cache[sku.Code];
                if (tup.Item1.AddMinutes(10) > DateTime.Now)
                {
                    return(tup.Item2);
                }
            }

            try
            {
                var attributes =
                    _skuProductsService.Skus.GetAttributesForSku(sku)
                    .Select(t => new { t.Name, t.Value })
                    .Distinct()
                    .ToDictionary(t => t.Name, t => t.Value);
                var productGroup = attributes.GetValueOrNull("Product Group");

                //TODO:STOCKLEVEL
                if (productGroup == "Rugs" || productGroup == "Cushions" || productGroup == "Breakfast" ||
                    productGroup == "Bedding" || productGroup == "Paint")
                {
                    return(new ValidationResponse());
                }

                var service = new services();
                service.Url = ConfigurationManager.AppSettings["WebtexBaseUrl"] + "/redant/services.asmx";
                //var mertexCode = attributes.GetValueOrNull("Design Code");
                var mertexCode = sku.Code;
                if (mertexCode != null)
                {
                    mertexCode += "/C"; // TODO: revisit to not hard code for samples
                    var customerNumber = GetCustomerNumberForSite(shop);

                    StockAvailable pd = null;

                    // There maybe instances where Mertex is not available for a live stock check,
                    // therefore we need to wrap the stock lookup in a try...catch. If a SoapException is thrown,
                    // we catch it and set the stock level to configured threshold.
                    try
                    {
                        pd = service.B2CStockLookup(customerNumber, mertexCode);
                    }
                    catch (SoapException ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                        pd = new StockAvailable
                        {
                            ProductCode      = mertexCode,
                            Item_Level_Stock = outOfStockThreshold.ToString()
                        };
                    }
                    if (pd != null)
                    {
                        int stocklevel;
                        var mertexStockLevel = pd.Item_Level_Stock;
                        int.TryParse(mertexStockLevel, out stocklevel);

                        if (stocklevel < requiredQuantity)
                        {
                            var errorMsg =
                                string.Format(
                                    "The item {0} – {1} is temporarily out of stock.",
                                    mertexCode, sku.Name);
                            validationResponse.Errors.Add(new StockError {
                                Message = errorMsg
                            });
                        }
                    }
                    else
                    {
                        var errorMsg =
                            string.Format(
                                "The item {0} – {1} is temporarily out of stock.",
                                mertexCode, sku.Name);
                        validationResponse.Errors.Add(new StockError {
                            Message = errorMsg
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                validationResponse.Errors.Add(new StockError {
                    Message = "There was an error checking stock levels."
                });
            }
            if (!_cache.ContainsKey(sku.Code))
            {
                _cache.Add(sku.Code, new Tuple <DateTime, ValidationResponse>(DateTime.Now, validationResponse));
            }
            HttpContext.Current.Session["RCStockLookupCache"] = _cache;
            return(validationResponse);
        }
Example #5
0
        public StockAvailable findByIdStock(int idStock)
        {
            StockAvailable stockAvailable = (from sa in foodForAllContext.StockAvailable where sa.IdStock == idStock select sa).FirstOrDefault();

            return(stockAvailable);
        }
        public IActionResult create([FromHeader(Name = "Authorization")] string token, [FromBody] StockAvailable stockAvailable)
        {
            try
            {
                if (string.IsNullOrEmpty(token))
                {
                    return(Ok(new
                    {
                        message = "El Token es requerido.",
                        statusCode = HttpStatusCode.NoContent
                    }));
                }
                else
                {
                    string host          = Request.HttpContext.Connection.RemoteIpAddress.ToString();
                    Token  tokenExisting = tokenService.findByToken(token, host);

                    if (tokenExisting != null)
                    {
                        if (string.IsNullOrEmpty(stockAvailable.IdStock.ToString()))
                        {
                            return(Ok(new
                            {
                                message = "El Id del Stock es requerido.",
                                statusCode = HttpStatusCode.NoContent
                            }));
                        }
                        else
                        {
                            stockAvailable = stockAvailableService.create(stockAvailable);

                            if (stockAvailable.Id != 0)
                            {
                                EventLog eventLog = new EventLog();

                                eventLog.IdUser         = tokenExisting.IdUser;
                                eventLog.IdEventLogType = 1;
                                eventLog.Host           = Request.HttpContext.Connection.RemoteIpAddress.ToString();
                                eventLog.HttpMethod     = ControllerContext.HttpContext.Request.Method;
                                eventLog.Controller     = ControllerContext.ActionDescriptor.ControllerName;
                                eventLog.Method         = ControllerContext.ActionDescriptor.ActionName;

                                eventLogService.create(eventLog);

                                stockAvailableHubContext.Clients.All.SendAsync("create", stockAvailable);

                                return(Ok(new
                                {
                                    message = "Stock Disponible Agregado.",
                                    statusCode = HttpStatusCode.Created
                                }));
                            }
                            else
                            {
                                return(Ok(new
                                {
                                    message = "El Stock Disponible no se pudo agregar, intentalo nuevamente.",
                                    statusCode = HttpStatusCode.NotFound
                                }));
                            }
                        }
                    }
                    else
                    {
                        return(Ok(new
                        {
                            message = "Token no permitido.",
                            statusCode = HttpStatusCode.Forbidden
                        }));
                    }
                }
            }
            catch (Exception exception)
            {
                EventLog eventLog = new EventLog();

                eventLog.IdEventLogType = 2;
                eventLog.Host           = Request.HttpContext.Connection.RemoteIpAddress.ToString();
                eventLog.HttpMethod     = ControllerContext.HttpContext.Request.Method;
                eventLog.Controller     = ControllerContext.ActionDescriptor.ControllerName;
                eventLog.Method         = ControllerContext.ActionDescriptor.ActionName;
                eventLog.Message        = exception.InnerException != null ? exception.InnerException.Message : exception.Message;

                eventLogService.create(eventLog);

                return(Ok(new
                {
                    message = "Upps!!, tenemos un problema, intentalo nuevamente.",
                    statusCode = HttpStatusCode.InternalServerError
                }));
            }
        }
        public IActionResult destroyByIdStock([FromHeader(Name = "Authorization")] string token, int idStock)
        {
            try
            {
                if (string.IsNullOrEmpty(token))
                {
                    return(Ok(new
                    {
                        message = "El Token es requerido.",
                        statusCode = HttpStatusCode.NoContent
                    }));
                }
                else
                {
                    string host          = Request.HttpContext.Connection.RemoteIpAddress.ToString();
                    Token  tokenExisting = tokenService.findByToken(token, host);

                    if (tokenExisting != null)
                    {
                        StockAvailable stockAvailableExisting = stockAvailableService.findByIdStock(idStock);

                        if (stockAvailableExisting != null)
                        {
                            StockAvailable stockAvailableDestroyed = stockAvailableService.destroyByIdStock(idStock);

                            if (stockAvailableDestroyed == null)
                            {
                                EventLog eventLog = new EventLog();

                                eventLog.IdUser         = tokenExisting.IdUser;
                                eventLog.IdEventLogType = 1;
                                eventLog.Host           = Request.HttpContext.Connection.RemoteIpAddress.ToString();
                                eventLog.HttpMethod     = ControllerContext.HttpContext.Request.Method;
                                eventLog.Controller     = ControllerContext.ActionDescriptor.ControllerName;
                                eventLog.Method         = ControllerContext.ActionDescriptor.ActionName;

                                eventLogService.create(eventLog);

                                return(Ok(new
                                {
                                    message = "Stock Disponible eliminado.",
                                    statusCode = HttpStatusCode.OK
                                }));
                            }
                            else
                            {
                                return(Ok(new
                                {
                                    message = "El Stock Disponible no se pudo eliminar, intentalo nuevamente.",
                                    statusCode = HttpStatusCode.NotFound
                                }));
                            }
                        }
                        else
                        {
                            return(Ok(new
                            {
                                message = "El comentario no existe.",
                                statusCode = HttpStatusCode.NotFound
                            }));
                        }
                    }
                    else
                    {
                        return(Ok(new
                        {
                            message = "Token no permitido.",
                            statusCode = HttpStatusCode.Forbidden
                        }));
                    }
                }
            }
            catch (Exception exception)
            {
                EventLog eventLog = new EventLog();

                eventLog.IdEventLogType = 2;
                eventLog.Host           = Request.HttpContext.Connection.RemoteIpAddress.ToString();
                eventLog.HttpMethod     = ControllerContext.HttpContext.Request.Method;
                eventLog.Controller     = ControllerContext.ActionDescriptor.ControllerName;
                eventLog.Method         = ControllerContext.ActionDescriptor.ActionName;
                eventLog.Message        = exception.InnerException != null ? exception.InnerException.Message : exception.Message;

                eventLogService.create(eventLog);

                return(Ok(new
                {
                    message = "Upps!!, tenemos un problema, intentalo nuevamente.",
                    statusCode = HttpStatusCode.InternalServerError
                }));
            }
        }