Example #1
0
        } // End Delete

        /*
         *
         *
         *  Id = Id,
         *      Description = Description,
         *      Price = Price,
         *      Status = Status,
         */
        public Product Create(Product product, User user)
        {
            RoleService              rService                 = new RoleService(_context);
            StockService             stockService             = new StockService(_context);
            StockMovementService     stockMovementService     = new StockMovementService(_context);
            AuditLogService          auditLogService          = new AuditLogService(_context);
            StockMovementTypeService stockMovementTypeService = new StockMovementTypeService(_context);


            Role role = new Role();

            role = rService.GetById(user.Role.Id);
            string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(product);

            StockMovementType stockMovementType = stockMovementTypeService.FindByName("In");
            AuditLog          auditLog          = new AuditLog();

            auditLog.createdBy    = user.Username;
            auditLog.CreationDate = DateTime.Now;
            auditLog.Description  = "Auditing Product Creation: " + product.Description;
            auditLog.RawParams    = jsonString;
            auditLog.EventType    = "Product Creation";
            auditLog.UserAudited  = user.Username;



            if (role.RoleName == "Admin")
            {
                // Validation
                if (string.IsNullOrWhiteSpace(product.Description))
                {
                    //if (float.IsNaN(product.Price))
                    //{
                    //    product.Price = float.Parse("0.00");
                    //}
                    throw new System.ApplicationException("The product description can not be empty");
                }

                product.Status = true;

                _context.Products.Add(product);
                _context.SaveChanges();


                // Create Stock
                Stock stock = new Stock();
                stock.Description = product.Description;
                stock.createdBy   = user.Username;
                stock.product     = product;
                // We are creating a new product in here, not handling stock movements, but We will create this record to match the product creation
                // for it's own stock register.
                // The stock quantity needs to be handled by StockMovementService
                stock.Quantity = 0;
                stock.Status   = true;

                _context.Stocks.Add(stock);
                _context.SaveChanges();
                // Create the auditLog

                _context.AuditLogs.Add(auditLog);
                _context.SaveChanges();

                return(product);
            }
            else
            {
                Product errorProduct = new Product();
                errorProduct.Description = "Only Admin users can update a product!";
                errorProduct.Status      = false;
                errorProduct.Id          = -1;
                errorProduct.Price       = 0;
                return(errorProduct);
            }
        } // End Create
Example #2
0
        } // End Create

        public Product Update(Product product, User user)
        {
            RoleService rService = new RoleService(_context);
            Role        role     = new Role();

            role = rService.GetById(user.Role.Id);

            Product p = this.FindByOrdinalAsNoTracking(product.Id);

            StockService             stockService             = new StockService(_context);
            StockMovementService     stockMovementService     = new StockMovementService(_context);
            AuditLogService          auditLogService          = new AuditLogService(_context);
            StockMovementTypeService stockMovementTypeService = new StockMovementTypeService(_context);

            Product errorResponse = new Product();



            if (role.RoleName == "Admin")
            {
                // Validation
                if (string.IsNullOrWhiteSpace(product.Description))
                {
                    //throw new System.ApplicationException("The product description can not be empty");
                    //Product errorResponse = new Product();
                    errorResponse.Description = "The product description can not be empty";
                    errorResponse.Id          = -1;
                    return(errorResponse);
                }

                if (product.Price.HasValue)
                {
                    p.Price = product.Price;
                }
                if (product.Status.HasValue)
                {
                    p.Status = product.Status;
                }
                if (product.Description != p.Description)
                {
                    p.Description = product.Description;
                }
                if (product.Likes != p.Likes)
                {
                    p.Likes = product.Likes;
                }
                p.Modified = DateTime.Now;



                _context.Products.Update(p);
                _context.SaveChanges();

                string   jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(product);
                AuditLog auditLog   = new AuditLog();
                auditLog.createdBy    = user.Username;
                auditLog.CreationDate = DateTime.Now;
                auditLog.Description  = "Auditing Product Update: " + product.Description;
                auditLog.RawParams    = jsonString;
                auditLog.EventType    = "Product Update";
                auditLog.UserAudited  = user.Username;

                _context.AuditLogs.Add(auditLog);
                _context.SaveChanges();

                return(p);
            }
            else
            {
                // throw new System.ApplicationException("You need admin rights to add a new product");
                // Product error

                errorResponse.Description = "Only Administrator users can update products";
                errorResponse.Id          = -1;
                return(errorResponse);
            }
        } // End Update
Example #3
0
        } // End Delete

        //public Order Create(Order order, IEnumerable<OrderDetail> details, User user)

        public Order Create(Order order, User user)
        {
            RoleService              rService                 = new RoleService(_context);
            StockService             stockService             = new StockService(_context);
            StockMovementService     stockMovementService     = new StockMovementService(_context);
            AuditLogService          auditLogService          = new AuditLogService(_context);
            StockMovementTypeService stockMovementTypeService = new StockMovementTypeService(_context);
            OrderDetailService       orderDetailService       = new OrderDetailService(_context);
            ProductService           productService           = new ProductService(_context);

            Role role = new Role();

            role = rService.GetById(user.Role.Id);
            string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(order);

            StockMovementType stockMovementType = stockMovementTypeService.FindByName("Out");
            AuditLog          auditLog          = new AuditLog();

            auditLog.createdBy    = user.Username;
            auditLog.CreationDate = DateTime.Now;
            auditLog.Description  = "Auditing Purchase Order Creation: " + order.Description;
            auditLog.RawParams    = jsonString;
            auditLog.EventType    = "Purchase Order Creation";
            auditLog.UserAudited  = user.Username;


            AuditLogPurchase auditLogPurchase = new AuditLogPurchase();

            auditLogPurchase.createdBy    = user.Username;
            auditLogPurchase.PurchaseDate = DateTime.Now;
            auditLogPurchase.Description  = "Auditing Purchase Order header Creation: " + order.Description;
            auditLogPurchase.RawParams    = jsonString;
            auditLogPurchase.EventType    = "Auditing Purchase Order,header,Creation";
            auditLogPurchase.UserAudited  = user.Username;
            auditLogPurchase.Quantity     = 0;
            auditLogPurchase.product      = null;
            auditLogPurchase.UserAudited  = user.Username;
            auditLogPurchase.Customer     = user;



            Order errorProduct = new Order();


            // Only users with customer or admin role can create an order, everyone else is just an internal user
            if (role.RoleName.ToLower() == "customer" || role.RoleName.ToLower() == "admin")
            {
                // Validation
                //if (string.IsNullOrWhiteSpace(product.Description))
                //{
                //    //if (float.IsNaN(product.Price))
                //    //{
                //    //    product.Price = float.Parse("0.00");
                //    //}
                //    throw new System.ApplicationException("The product description can not be empty");
                //}

                //product.Status = true;

                //_context.Products.Add(product);
                //_context.SaveChanges();


                //// Create Stock
                //Stock stock = new Stock();
                //stock.Description = product.Description;
                //stock.createdBy = user.Username;
                //stock.product = product;
                //// We are creating a new product in here, not handling stock movements, but We will create this record to match the product creation
                //// for it's own stock register.
                //// The stock quantity needs to be handled by StockMovementService
                //stock.Quantity = 0;
                //stock.Status = true;

                //_context.Stocks.Add(stock);
                //_context.SaveChanges();
                //// Create the auditLog

                //_context.AuditLogs.Add(auditLog);
                //_context.SaveChanges();

                //return product;

                // First save the order header

                order.createdBy = user.Username;
                order.Created   = DateTime.Now;
                order.Status    = true;

                foreach (OrderDetail d in order.Details)
                {
                    d.createdBy = user.Username;
                    d.Created   = DateTime.Now;

                    // Check if there is stock available for the purchase
                    Stock currentStock = stockService.FindByOrdinal(d.product.Id);
                    if (currentStock.Quantity <= 0 || currentStock.Quantity < d.Quantity)
                    {
                        errorProduct.Description = "Error: Product: " + d.product.Description + " has not stock available";
                        errorProduct.Status      = false;
                        errorProduct.Id          = -1;

                        return(errorProduct);
                    }
                    // Update stock
                    currentStock.Quantity = currentStock.Quantity - d.Quantity;

                    // Get the product by Id in the order request
                    Product pr = productService.FindByOrdinal(d.product.Id);
                    if (pr != null)
                    {
                        d.product.Description = pr.Description;
                        d.product.Likes       = pr.Likes;
                        d.product.Price       = pr.Price;
                        d.product.Status      = pr.Status;
                    }
                    // TODO: Add transactions and rollback handler scenarios (THIS IS FOR THE PRO VERSION!!!) XD
                    // R@ul Berrios
                    // Why we need this, because we don't know when something was fail, in that case we need to handle the rolling back
                    // scenarios, restore previous stock values to their original state.


                    _context.Stocks.Update(currentStock);
                    _context.SaveChanges();

                    // Update current stock

                    // Add stock movement
                    StockMovement stockMovement = new StockMovement();
                    stockMovement.product           = d.product;
                    stockMovement.Modified          = DateTime.Now;
                    stockMovement.Quantity          = d.Quantity;
                    stockMovement.StockMovementType = stockMovementType;
                    stockMovement.DocumentID        = order.OrderNumber;

                    _context.StockMovements.Add(stockMovement);
                    _context.SaveChanges();


                    // Add Log Purchase Record
                    AuditLogPurchase auditLogPurchaseItem = new AuditLogPurchase();
                    auditLogPurchaseItem.createdBy    = user.Username;
                    auditLogPurchaseItem.PurchaseDate = DateTime.Now;
                    auditLogPurchaseItem.Description  = "Auditing Purchase Order,Detail,Creation: " + d.product.Description;
                    //auditLogPurchaseItem.RawParams = jsonString;
                    auditLogPurchaseItem.EventType   = "Purchase Order Detail Creation";
                    auditLogPurchaseItem.UserAudited = user.Username;
                    auditLogPurchaseItem.Quantity    = d.Quantity;
                    auditLogPurchaseItem.product     = d.product;
                    auditLogPurchaseItem.UserAudited = user.Username;
                    auditLogPurchaseItem.Customer    = user;

                    _context.AuditLogPurchases.Add(auditLogPurchaseItem);
                    _context.SaveChanges();
                    // add Detail
                    _context.OrderDetails.Add(d);
                    _context.SaveChanges();



                    //order.OrderDetails.Add(d);
                }

                //_context.Orders.Add(order);
                //_context.SaveChanges();

                // Now Save the order details
                //foreach (OrderDetail d in details)
                //{
                //    d.createdBy = user.Username;
                //    d.Created = DateTime.Now;
                //    order.OrderDetails.Add(d);
                //}


                _context.Orders.Add(order);
                _context.SaveChanges();

                return(order);
            }
            else
            {
                errorProduct.Description = "Only customers or administrators users can create an order!";
                errorProduct.Status      = false;
                errorProduct.Id          = -1;

                return(errorProduct);
            }
        } // End Create