Exemplo n.º 1
0
        public async Task <IActionResult> QuickAddProduct(QuickAddProductViewModel productAdd)
        {
            int orderId = productAdd.OrderId;
            var product = new Products
            {
                ProductName  = productAdd.ProductName,
                CategoryId   = productAdd.CategoryId,
                CreateAt     = DateTime.Now,
                Description  = "",
                Discontinued = true,
                DisplayIndex = false,
                Discount     = 0,
                Stock        = 0,
                UnitPrice    = 0,
                Summary      = "",
                Guarantee    = 0
            };
            await _context.Products.AddAsync(product);

            await _context.SaveChangesAsync();

            if (productAdd.OrderId > 0)
            {
                var orderDetail = new OrderImportGoodsDetails
                {
                    OrderId   = productAdd.OrderId,
                    ProductId = product.ProductId,
                    Quantity  = productAdd.Quantity,
                    UnitPrice = productAdd.UnitPrice
                };
                _context.OrderImportGoodsDetails.Add(orderDetail);
                await _context.SaveChangesAsync();
            }
            else
            {
                OrdersImportGoods orders = new OrdersImportGoods
                {
                    OrderDate  = DateTime.Now,
                    SupplierId = productAdd.SupplierId,
                    UserId     = productAdd.UserId,
                    TotalPrice = 0
                };
                orders.OrderImportGoodsDetails.Add(new OrderImportGoodsDetails
                {
                    ProductId = product.ProductId,
                    Quantity  = productAdd.Quantity,
                    UnitPrice = productAdd.UnitPrice
                });
                _context.OrdersImportGoods.Add(orders);
                await _context.SaveChangesAsync();

                orderId = orders.OrderId;
            }
            return(Ok(new Response
            {
                Status = 201,
                Module = new { orderId, product.ProductId }
            }));
        }
        public async Task <IActionResult> CreateOrdersImportGoods([FromBody] OrderImportFirstViewModel order)
        {
            if (!ModelState.IsValid)
            {
                return(Ok(new Response
                {
                    IsError = true,
                    Status = 400,
                    Message = "Sai dữ liệu đầu vào"
                }));
            }
            OrdersImportGoods orders = new OrdersImportGoods
            {
                OrderDate  = DateTime.Now,
                SupplierId = order.SupplierId,
                UserId     = order.UserId,
                TotalPrice = 0
            };

            orders.OrderImportGoodsDetails.Add(new OrderImportGoodsDetails
            {
                ProductId = order.ProductId,
                Quantity  = 0,
                UnitPrice = 0
            });

            _context.OrdersImportGoods.Add(orders);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(Ok(new Response
                {
                    IsError = true,
                    Status = 409,
                    Message = "không thể lưu dữ liệu"
                }));
            }

            return(Ok(new Response
            {
                Status = 201,
                Module = orders.OrderId
            }));
        }