Пример #1
0
        public IHttpActionResult Purchase(PurchaseOrder order)
        {
            if (order != null)
            {
                Supplier supplier = db.Suppliers.Find(order.SupplierId);
                if (supplier != null)
                {
                    order.Supplier             = supplier;
                    order.SupplierName         = supplier.Name;
                    order.SupplierEmailAddress = supplier.EmailAddress;
                    order.SupplierCreditToken  = supplier.CreditToken;

                    foreach (PurchaseOrderItem orderItem in order.OrderItems)
                    {
                        Product product = db.Products.GetAll()
                                          .Where(prod => !prod.Discontinued)
                                          .FirstOrDefault(prod => prod.Code == orderItem.ProductCode);

                        if (product != null)
                        {
                            product.Stocks += orderItem.Quantity;

                            orderItem.Product            = product;
                            orderItem.ProductId          = product.Id;
                            orderItem.UnitPrice          = product.UnitPrice;
                            orderItem.ProductDisplayName = product.DisplayName;
                            orderItem.StocksAvailable    = product.Stocks - orderItem.Quantity;

                            orderItem.CategoryId = product.CategoryId;
                        }
                        else
                        {
                            ModelState.AddModelError(key: "ProductCode", errorMessage: orderItem.ProductCode + " not found or has been discontinued.");
                        }
                    }

                    if (ModelState.IsValid)
                    {
                        db.PurchaseOrders.Add(order);
                        db.Commit();

                        return(Created(location: "purchaseorders/id/" + order.Id, content: new { id = order.Id }));
                    }
                }
                else
                {
                    ModelState.AddModelError(key: "SupplierId", errorMessage: order.SupplierId + " not found.");
                }

                return(BadRequest(ModelState));
            }

            return(Conflict());
        }
Пример #2
0
        public IHttpActionResult PostProduct(Product product)
        {
            if (product != null)
            {
                if (string.IsNullOrWhiteSpace(product.Code))
                {
                    ModelState.AddModelError(key: "ProductCode", errorMessage: "Product must have a code.");
                }

                if (ModelState.IsValid)
                {
                    db.Products.Add(product);
                    db.Commit();

                    return(Created("products/code/" + product.Code, new { code = product.Code }));
                }

                return(BadRequest(ModelState));
            }
            return(Conflict());
        }
Пример #3
0
        public IHttpActionResult Order(SalesOrder order)
        {
            if (order != null)
            {
                Customer customer = db.Customers.Find(order.CustomerId);
                if (customer != null)
                {
                    order.Customer             = customer;
                    order.CustomerName         = customer.Name;
                    order.CustomerMobile       = customer.Mobile;
                    order.CustomerEmailAddress = customer.EmailAddress;

                    foreach (SalesOrderItem orderItem in order.OrderItems)
                    {
                        Product product = db.Products.GetAll()
                                          .Where(prod => !prod.Discontinued)
                                          .FirstOrDefault(prod => prod.Code == orderItem.ProductCode);

                        if (product != null)
                        {
                            if (orderItem.Quantity < product.Stocks)
                            {
                                product.Stocks -= orderItem.Quantity;

                                orderItem.Product            = product;
                                orderItem.ProductId          = product.Id;
                                orderItem.UnitPrice          = product.UnitPrice;
                                orderItem.ProductDisplayName = product.DisplayName;
                                orderItem.StocksRemaining    = product.Stocks + orderItem.Quantity;

                                orderItem.CategoryId = product.CategoryId;
                            }
                            else
                            {
                                ModelState.AddModelError(key: "Quantity", errorMessage: orderItem.ProductCode + " insufficient stocks.");
                            }
                        }
                        else
                        {
                            ModelState.AddModelError(key: "ProductCode", errorMessage: orderItem.ProductCode + " not found or has been discontinued.");
                        }
                    }

                    if (ModelState.IsValid)
                    {
                        db.SalesOrders.Add(order);
                        db.Commit();

                        return(Created(location: "orders/id/" + order.Id, content: new { Id = order.Id }));
                    }
                }
                else
                {
                    ModelState.AddModelError(key: "CustomerId", errorMessage: order.CustomerId + " not found.");
                }

                BadRequest(ModelState);
            }

            return(Conflict());
        }