Beispiel #1
0
        public HttpResponseMessage EditWasteItem(SpoilViewModel spoilVm)
        {
            var response = new ApiResultViewModel <dynamic>();

            if (spoilVm == null || Guid.Empty == spoilVm.SpoilId || spoilVm.Quantity <= 0)
            {
                response.errorStatus  = true;
                response.errorMessage = "Invalid request. Please confirm and try again.";
            }

            try
            {
                using (var uow = _wasteSvc.UnitOfWork)
                {
                    var spoil = _wasteSvc.GetWastedById(spoilVm.SpoilId);

                    var product = _prodSvc.GetProductById(spoil.Product_Id.Value);
                    if (product == null)
                    {
                        response.errorStatus  = true;
                        response.errorMessage = "Product was not found.";
                        return(Request.CreateResponse(response));
                    }
                    var oldQty = spoil.Quantity;

                    if (product.Quantity < 0 || product.Quantity + (oldQty - spoilVm.Quantity) < 0)
                    {
                        response.errorStatus  = true;
                        response.errorMessage = "Cannot report waste for a negative product.";
                        return(Request.CreateResponse(response));
                    }

                    var membershipId   = IposConfig.UseMembership ? (Guid?)IposMembershipService.GetUserId(User.Identity.Name) : null;
                    var identityUserId = User.Identity.GetUserId <int>();
                    spoil.Quantity    = spoilVm.Quantity;
                    product.Quantity += (oldQty - spoilVm.Quantity);

                    _prodSvc.Update(product);
                    _wasteSvc.Update(spoil);

                    var eventDescription = String.Format("Waste {0} item was edited.", product.Name);

                    _auditSvc.LogEvent(eventDescription, AuditType.EDIT_WASTE, membershipId, identityUserId);
                    uow.SaveChanges();
                    response.message = "Waste item has been updated";
                    response.result  = new { productName = product.Name, spoil.Quantity, spoil.SpoilId, EntryDate = spoil.EntryDate.Value.ToString("dd/MM/yyyy") };
                }
            }
            catch (Exception e)
            {
                _logger.Log(e);
#if DEBUG
                response.errorMessage = e.Message;
                response.errorStatus  = true;
#else
                response.errorMessage = "An error occurred while working, Please try again or contact support.";
                response.errorStatus  = true;
#endif
                return(Request.CreateResponse(response));
            }
            return(Request.CreateResponse(response));
        }
Beispiel #2
0
        public HttpResponseMessage CreateWasteStock(SpoilViewModel spoilVM)
        {
            var response = new ApiResultViewModel <dynamic>();

            if (spoilVM == null || spoilVM.Id == 0)
            {
                response.errorStatus  = true;
                response.errorMessage = "Invalid request.";
                return(Request.CreateResponse(response));
            }

            try
            {
                using (var uow = _spoilSvc.UnitOfWork)
                {
                    uow.BeginTransaction();
                    var product = _prodSvc.GetProductById(spoilVM.Id);
                    if (product == null)
                    {
                        response.errorStatus  = true;
                        response.errorMessage = "Product was not found.";
                        return(Request.CreateResponse(response));
                    }

                    if (product.Quantity < 0 || product.Quantity - spoilVM.Quantity < 0)
                    {
                        response.errorStatus  = true;
                        response.errorMessage = "Cannot report waste for a negative product.";
                        return(Request.CreateResponse(response));
                    }
                    var newSpoil     = new Spoil();
                    var membershipId = IposConfig.UseMembership ? (Guid?)IposMembershipService.GetUserId(User.Identity.Name) : null;

                    var identityUserId = User.Identity.GetUserId <int>();
                    newSpoil.Title        = product.Name;
                    newSpoil.Description  = spoilVM.Description;
                    newSpoil.Quantity     = spoilVM.Quantity;
                    newSpoil.Product_Id   = product.ProductId;
                    newSpoil.CreatedBy_Id = identityUserId;
                    newSpoil.User_Id      = membershipId;

                    _spoilSvc.NewWaste(newSpoil);

                    if (newSpoil.HasErrors)
                    {
                        response.errorStatus  = newSpoil.HasErrors;
                        response.errorMessage = newSpoil.ValidationErrors.FirstOrDefault() != null?
                                                newSpoil.ValidationErrors.FirstOrDefault().ErrorMessage : String.Empty;
                    }
                    else
                    {
                        product.Quantity -= newSpoil.Quantity;
                        _prodSvc.Update(product);
                        var eventDescription = String.Format("{0} quantity of {1} was entered as a waste.", newSpoil.Quantity, product.Name);
                        _auditSvc.LogEvent(eventDescription, AuditType.NEW_WASTE, membershipId, identityUserId);
                        uow.Commit();
                        response.result  = new { product.Quantity, Id = product.ProductId };
                        response.message = "Waste has now been reported.";
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Log(ex);

#if DEBUG
                response.errorMessage = ex.Message;
                response.errorStatus  = true;
#else
                response.errorMessage = "Error occured, please contact admin.";
                response.errorStatus  = true;
#endif
            }
            return(Request.CreateResponse(response));
        }