Beispiel #1
0
        public async Task <IActionResult> CreateOrder([FromBody] CreateOrderResource createOrderResource)
        {
            var userId   = getUserId();
            var order    = new Order(userId);
            var newOrder = await _orderService.CreateOrder(order);

            var basketItems = createOrderResource.BasketItems;

            foreach (BasketItem basketItem in basketItems)
            {
                var item = await _itemService.GetItemById(basketItem.ItemId);

                if (item == null)
                {
                    return(NotFound());
                }

                await _orderItemService.CreateOrderItem(new OrderItem(order.Id, item.Id, basketItem.Quantity, item.Price));

                // update item quantity
                var updatedQuantity = item.Quantity - basketItem.Quantity;
                await _itemService.UpdateQuantity(basketItem.ItemId, updatedQuantity);
            }

            return(Ok(newOrder));
        }
Beispiel #2
0
        public ActionResult CreateOrderItem([FromBody] OrderItemModel model)
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            _service.CreateOrderItem(userId, model);
            return(Ok());
        }
Beispiel #3
0
        public async Task CreateOrder(Order order)
        {
            if (order.Delivery == true)
            {
                order.Status = GlobalConstants.Sent;
            }
            else
            {
                order.Status = GlobalConstants.Open;
            }
            orderRepository.AddAsync(order).Wait();

            await orderRepository.SaveChangesAsync();

            await orderItemService.CreateOrderItem(order);
        }
        public ActionResult Checkout(string CustomerName, string CustomePhone, string CustomerAddress, string CustomerNote)
        {
            Order order = new Order();

            order.CustomerName    = CustomerName;
            order.CustomerPhone   = CustomePhone;
            order.CustomerAddress = CustomerAddress;
            order.Note            = CustomerNote;

            ListProductCartModel listCart = new ListProductCartModel();

            listCart = (ListProductCartModel)Session["Cart"];
            #region input data vào session
            listCart.CustomePhone    = CustomePhone;
            listCart.CustomerAddress = CustomerAddress;
            listCart.CustomerName    = CustomerName;
            listCart.CustomerNote    = CustomerNote;
            #endregion
            if (listCart != null)
            {
                order.OrderTotal = listCart.TotalPayment;
                if (listCart.GiftCode != "")
                {
                    order.TotalPayment = listCart.TotalAfterDiscount;
                }
                else
                {
                    order.TotalPayment = listCart.TotalPayment;
                }
                order.Status = 1;
                //order.Discount = listCart.GiftCodePercent;
                order.Temp_1 = listCart.GiftCode;

                order.Deleted     = false;
                order.DateCreated = DateTime.Now;

                //add Order to DB
                _OrderService.CreateOrder(order);

                #region set orderItem
                foreach (var item in listCart.listProduct)
                {
                    OrderItem Oitem = new OrderItem();
                    Oitem.Discount  = 0;
                    Oitem.OrderId   = order.Id;
                    Oitem.Price     = item.Price;
                    Oitem.ProductId = item.ProductId;
                    Oitem.Quantity  = item.Quantity;


                    //Oitem.Temp_1 = "";
                    //Oitem.Temp_2 = "";
                    //Oitem.Temp_3 = "";
                    //add to order Item
                    _OrderItemService.CreateOrderItem(Oitem);
                }
                #endregion
                Session["Cart"] = listCart;
                return(RedirectToAction("ConfirmCheckOut", "Cart"));
            }
            return(RedirectToAction("Index", "Home"));
        }
        public async Task <ActionResult <Order> > PostOrder([FromBody] PostOrderDTO order)
        {
            var orderToAdd = new Order
            {
                SellDate     = DateTime.Now,
                ShallDisplay = true,
                OrderState   = OrderState.New,
                InvoiceId    = null,
                OrderValue   = 0.0M,
                Username     = order.Invoice.Username
            };

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

            foreach (var item in order.PostOrders)
            {
                var product = await productService.GetByIdAsync(item.ProductId);

                products.Add(product);
                orderToAdd.OrderValue += (product.NettoPrice + decimal.Divide(product.VAT, 100) * product.NettoPrice) * item.QuantityOfProducts;
            }

            var placedOrder = await orderService.CreateOrder(orderToAdd);

            var orderItems = new List <OrderItem>();

            foreach (var product in products)
            {
                var orderItem = new OrderItem
                {
                    OrderId   = placedOrder.OrderId,
                    ProductId = product.ProductId
                };
                foreach (var item in order.PostOrders)
                {
                    if (item.ProductId == product.ProductId)
                    {
                        orderItem.Amount       = item.QuantityOfProducts;
                        orderItem.NettoPrice   = product.NettoPrice * item.QuantityOfProducts;
                        orderItem.BruttoPrice += (product.NettoPrice + decimal.Divide(product.VAT, 100) * product.NettoPrice) * item.QuantityOfProducts;
                    }
                }
                await orderItemService.CreateOrderItem(orderItem);

                orderItems.Add(orderItem);
            }

            EdiParser ediParser        = new EdiParser();
            var       mappedOrderItems = mapper.Map <List <OrderItem>, List <GetOrderItemForXml> >(orderItems);
            var       invoice          = await CreateInvoice(placedOrder.OrderId, order.Invoice);

            var invoiceXml = new XmlInvoice(invoice.InvoiceId, invoice.IssueDate, invoice.DeliveryDate,
                                            SynthPolInfo.supplierName, SynthPolInfo.supplierStreet, SynthPolInfo.supplierCity, SynthPolInfo.supplierZipCode, SynthPolInfo.supplierNIP,
                                            invoice.Name, invoice.Street, invoice.ZipCode, invoice.City, invoice.NIP, mappedOrderItems);
            YAXSerializer serializer      = new YAXSerializer(typeof(XmlInvoice));
            string        xmlString       = serializer.Serialize(invoiceXml);
            var           invoiceEdiToAdd = new InvoiceEdi
            {
                Username  = invoice.Username,
                InvoiceId = invoice.InvoiceId,
                EdiString = ediParser.Save(invoice.InvoiceId.ToString(), invoice.IssueDate.ToString(), invoice.DeliveryDate.ToString(),
                                           SynthPolInfo.supplierName, SynthPolInfo.supplierStreet, SynthPolInfo.supplierZipCode, SynthPolInfo.supplierCity,
                                           SynthPolInfo.supplierNIP, invoice.Name, invoice.Street, invoice.ZipCode, invoice.City, invoice.NIP, orderItems),
                XmlString = xmlString
            };

            var invoiceEdi = await invoiceEdiService.CreateInvoiceEdi(invoiceEdiToAdd);

            var newOrder = placedOrder;

            newOrder.InvoiceId = invoice.InvoiceId;
            await orderService.UpdateOrder(placedOrder, newOrder);

            var newInvoice = invoice;

            newInvoice.InvoiceEdiId = invoiceEdi.InvoiceEdiId;
            await invoiceService.UpdateInvoice(invoice, newInvoice);

            return(Ok());
        }