public ActionResult <SellingOrderDto> CreateSellingOrder([FromBody] SellingOrderDto order)
        {
            order.CustomerId = order.Customer.Id;
            order.Customer   = null;
            foreach (SellingTransactionDto tran in order.SellingTransactions)
            {
                tran.ProductDetailId = tran.ProductDetail.Id;
                tran.ProductDetail   = null;
            }

            var orderDto = _orderService.CreateSellingOrder(order);

            if (orderDto == null)
            {
                List <string> errorMessage = new List <string>();
                errorMessage.Add("Đã phát sinh lỗi, vui lòng thử lại");
                return(BadRequest(new ResponseDto(errorMessage, 500, orderDto)));
            }

            List <string> successMessage = new List <string>();

            successMessage.Add("Thêm thông tin thành công");
            var responseDto = new ResponseDto(successMessage, 200, orderDto);

            return(Ok(responseDto));
        }
        public SellingOrderDto CreateSellingOrder(SellingOrderDto orderDto)
        {
            var order = _mapper.Map <SellingOrder>(orderDto);

            foreach (SellingTransaction transaction in order.SellingTransactions)
            {
                // Trừ số lượng của từng chi tiết sản phẩm
                var productDetail = _productDetailRepo.GetById(transaction.ProductDetailId);
                productDetail.Quantity = productDetail.Quantity - transaction.Quantity;
                _productDetailRepo.Update(productDetail);

                // Trừ số lượng của sản phẩm
                var product = _productRepo.GetById(productDetail.ProductId);
                product.Quantity = product.Quantity - transaction.Quantity;
                _productRepo.Update(product);
            }



            int res = _orderRepo.Create(order);

            if (res <= 0)
            {
                return(null);
            }
            int insertedId       = _orderRepo.GetLatestSellingOrderId();
            var orderInserted    = this.GetSellingOrder(insertedId);
            var orderDtoInserted = _mapper.Map <SellingOrderDto>(orderInserted);

            return(orderDtoInserted);
        }
        public IActionResult Edit(SellingOrderDto order)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    sellingOrderService.UpdateOrder(order);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!sellingOrderService.OrderExists(order.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                var orders = sellingOrderService.GetAll();
                return(Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "_ViewAll", orders.ToList()) }));
            }

            ViewBag.Customers = new SelectList(customerService.GetAll(), "Id", "Fullname");
            ViewBag.Employees = new SelectList(employeeService.GetAll(), "Id", "Fullname");

            return(Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "Edit", order) }));
        }
示例#4
0
        public void UpdateOrder(SellingOrderDto orderDto)
        {
            var oldOrder = sellingOrderRepository.GetBy(orderDto.Id);

            oldOrder.SellingTransactions = sellingTransactionRepository.GetTransactions(orderDto.Id).ToList();

            /*Remove old quantity*/
            List <Product> productList = new List <Product>();

            foreach (var transaction in oldOrder.SellingTransactions)
            {
                var product = productRepository.GetBy(transaction.ProductId);
                product.Quantity += transaction.Quantity;
                productList.Add(product);
            }
            productRepository.UpdateQuantity(productList);

            /*Remove old transaction*/
            sellingTransactionRepository.RemoveRange(oldOrder.SellingTransactions);

            /*Add new quantity*/
            List <Product> newProductList = new List <Product>();

            foreach (var transaction in orderDto.SellingTransactions)
            {
                transaction.SellingOrderId = orderDto.Id;
                var product = productRepository.GetBy(transaction.ProductId);
                product.Quantity -= transaction.Quantity;
                newProductList.Add(product);
            }
            productRepository.UpdateQuantity(newProductList);

            /*Add new transaction*/
            sellingOrderRepository.Update(orderDto.MappingOrder());
        }
        public SellingOrderDto UpdateSellingOrder(SellingOrderDto orderDto)
        {
            var order = _mapper.Map <SellingOrder>(orderDto);
            int res   = _orderRepo.Update(order);

            if (res <= 0)
            {
                return(null);
            }
            return(orderDto);
        }
示例#6
0
 //Map from Dto to Entity
 public static SellingOrder MappingOrder(this SellingOrderDto sellingOrderDto)
 {
     return(new SellingOrder
     {
         Id = sellingOrderDto.Id,
         CustomerId = sellingOrderDto.CustomerId,
         EmployeeId = sellingOrderDto.EmployeeId,
         CreatedDate = (System.DateTime)sellingOrderDto.CreatedDate,
         Status = sellingOrderDto.Status,
         TotalPrice = sellingOrderDto.TotalPrice,
         NameCode = sellingOrderDto.NameCode,
         SellingTransactions = sellingOrderDto.SellingTransactions.MappingTransactions().ToList(),
     });
 }
        public IActionResult Add(SellingOrderDto order)
        {
            if (ModelState.IsValid)
            {
                order.Customer = customerService.GetCustomer(order.CustomerId);
                order.Employee = employeeService.GetEmployee(order.EmployeeId);

                sellingOrderService.CreateOrder(order);
            }

            ViewBag.Customers = new SelectList(customerService.GetAll(), "Id", "Fullname");
            ViewBag.Employees = new SelectList(employeeService.GetAll(), "Id", "Fullname");

            return(Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "Add", order) }));
        }
示例#8
0
        public void CreateOrder(SellingOrderDto orderDto)
        {
            orderDto.CreatedDate = System.DateTime.UtcNow;

            List <Product> productList = new List <Product>();

            foreach (var transaction in orderDto.SellingTransactions)
            {
                var product = productRepository.GetBy(transaction.ProductId);
                product.Quantity -= transaction.Quantity;
                productList.Add(product);
            }

            sellingOrderRepository.Add(orderDto.MappingOrder());
            productRepository.UpdateQuantity(productList);
        }
        public ActionResult <SellingOrderDto> UpdateSellingOrder([FromBody] SellingOrderDto order)
        {
            var orderDto = _orderService.UpdateSellingOrder(order);

            if (orderDto == null)
            {
                List <string> errorMessage = new List <string>();
                errorMessage.Add("Đã phát sinh lỗi, vui lòng thử lại");
                return(BadRequest(new ResponseDto(errorMessage, 500, orderDto)));
            }
            List <string> successMessage = new List <string>();

            successMessage.Add("Sửa thông tin thành công");
            var responseDto = new ResponseDto(successMessage, 200, orderDto);

            return(Ok(responseDto));
        }
示例#10
0
 public void DeleteOrder(SellingOrderDto orderDto)
 {
     sellingOrderRepository.Delete(orderDto.MappingOrder());
 }