Пример #1
0
        public IActionResult AddNewOrder(int customerid, DateTime orderdate, decimal amount)
        {
            //construire une nouvelle commande avec les paramètres passés.
            TblOrders order = new TblOrders();

            order.Amount     = amount;
            order.OrderDate  = orderdate;
            order.CustomerId = customerid;

            //ajouter cette nouvelle commande
            context.Add(order);
            context.SaveChanges();

            return(RedirectToAction("CurrentOrders"));
        }
Пример #2
0
        public IActionResult IncreaseAmountForOrder(int orderid, decimal amount)
        {
            //Trouver toutes les commandes
            List <TblOrders> orders = context.TblOrders.ToList();

            //trouver la commande avec l'id = orderid
            var req = from o in orders
                      where o.OrderId == orderid
                      select o;

            TblOrders order = req.FirstOrDefault();

            order.Amount += amount;

            context.Update(order);
            context.SaveChanges();

            return(RedirectToAction("Index"));
        }
Пример #3
0
        public async Task <IActionResult> AddOrder(OrdersDto data)
        {
            if (!ModelState.IsValid || data.TotalCost > data.TotalAmount)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var ClientIdentityId = User.FindFirstValue(ClaimTypes.NameIdentifier);// will give the user's userId

                // var ClientIdentityId = User.Identity.GetUserId();

                var clientId = (await Db.TblClient.FirstOrDefaultAsync(q => q.IdentityId == ClientIdentityId)).PkClientId;

                var orderData = new TblOrders
                {
                    PkOrdersId              = data.OrderId = Guid.NewGuid(),
                    OrderDate               = data.Order_Date = DateTime.Now,
                    TotalAmount             = data.TotalAmount,
                    TotalCost               = data.TotalCost,
                    FkClientsOrdersClientId = data.ClientId = clientId,
                    CraetedAt               = data.CraetedAt = DateTime.Now,
                    CreatedBy               = data.CreatedBy = data.CreatedBy = ClientIdentityId,
                    ModifiedAt              = data.ModifiedAt = DateTime.Now,
                    ModifiedBy              = data.ModifiedBy = data.ModifiedBy = ClientIdentityId,
                    OrderIsOrder            = data.Order_IsOrder = true,
                    IsCancelled             = data.IsCancelled = false,
                    IsDeleted               = data.IsDeleted = false,
                    OnDelivery              = data.OnDelivery = false,
                    OrderIsPaid             = data.Order_IsPaid = false,
                    OrderNo = data.OrderNo = Db.TblOrders.Count() + 1,
                };

                await Db.TblOrders.AddAsync(orderData);


                if (orderData.TblOrderDetails != null)
                {
                    foreach (var item in data.OrderDetails)
                    {
                        var itemData = (await Db.TblItems.Where(i => i.PkItemsId == item.fk_Items_Id).FirstOrDefaultAsync());

                        if (item.Quantity > itemData.Available)
                        {
                            return(BadRequest("please select a valid quantity for item :" + itemData.Name));
                        }
                    }


                    foreach (var item in data.OrderDetails)
                    {
                        var itemData = (await Db.TblItems.Where(i => i.PkItemsId == item.fk_Items_Id).FirstOrDefaultAsync());
                        itemData.BookedUp = itemData.BookedUp + item.Quantity;
                        await Db.SaveChangesAsync();

                        var orderDetailsData = new TblOrderDetails
                        {
                            PkOrderDetailsId = item.OrderDetailId = Guid.NewGuid(),
                            OrderId          = orderData.PkOrdersId,
                            FkItemsId        = item.fk_Items_Id,
                            Quantity         = item.Quantity,
                            CraetedAt        = item.CraetedAt = DateTime.Now,
                            CreatedBy        = item.ModifiedBy = ClientIdentityId,
                            ModifiedAt       = item.ModifiedAt = DateTime.Now,
                            ModifiedBy       = item.ModifiedBy = ClientIdentityId
                        };
                        await Db.TblOrderDetails.AddAsync(orderDetailsData);
                    }
                }
                await Db.SaveChangesAsync();

                return(Ok(new { message = " Your order has been placed successfully :)  With OrderNo : (" + data.OrderNo + ")" }));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Пример #4
0
        public async Task <IActionResult> AddItemToCart(AddItemToCartDto data)
        {
            try
            {
                var ClientIdentityId = User.FindFirstValue(ClaimTypes.NameIdentifier);// will give the user's userId
                // var ClientIdentityId = User.Identity.GetUserId();
                var cartOrderId = (await Db.TblOrders.FirstOrDefaultAsync(c => c.OrderIsOrder == false && c.CreatedBy == ClientIdentityId));

                if (cartOrderId == null)
                {
                    var clientId = (await Db.TblClient.FirstOrDefaultAsync(q => q.IdentityId == ClientIdentityId)).PkClientId;

                    var orderData = new TblOrders
                    {
                        PkOrdersId              = data.OrderId = Guid.NewGuid(),
                        OrderDate               = data.Order_Date = DateTime.Now,
                        TotalAmount             = data.TotalAmount,
                        TotalCost               = data.TotalCost,
                        FkClientsOrdersClientId = data.ClientId = clientId,
                        CraetedAt               = data.CraetedAt = DateTime.Now,
                        CreatedBy               = data.CreatedBy = data.CreatedBy = ClientIdentityId,
                        ModifiedAt              = data.ModifiedAt = DateTime.Now,
                        ModifiedBy              = data.ModifiedBy = data.ModifiedBy = ClientIdentityId,
                        OrderIsOrder            = data.Order_IsOrder = false,
                        IsCancelled             = data.IsCancelled = false,
                        IsDeleted               = data.IsDeleted = false,
                        OnDelivery              = data.OnDelivery = false,
                        OrderIsPaid             = data.Order_IsPaid = false,
                    };

                    await Db.TblOrders.AddAsync(orderData);

                    await Db.SaveChangesAsync();

                    var orderDetailsData = new TblOrderDetails
                    {
                        PkOrderDetailsId = data.OrderDetailId = Guid.NewGuid(),
                        OrderId          = orderData.PkOrdersId,
                        FkItemsId        = data.ItemId,
                        CraetedAt        = data.CraetedAt = DateTime.Now,
                        CreatedBy        = ClientIdentityId,
                        ModifiedAt       = data.ModifiedAt = DateTime.Now,
                        ModifiedBy       = data.ModifiedBy = ClientIdentityId,
                        Quantity         = 1, //??
                    };
                    await Db.TblOrderDetails.AddAsync(orderDetailsData);

                    await Db.SaveChangesAsync();

                    return(Ok(new { message = " Your Order Has Been Placed Successfully :) " }));
                }
                else if (cartOrderId != null)
                {
                    var editeOrder = (await Db.TblOrders.FirstOrDefaultAsync(o => o.PkOrdersId == cartOrderId.PkOrdersId && o.OrderIsOrder != true));
                    editeOrder.OrderDate   = data.Order_Date = DateTime.Now;
                    editeOrder.TotalAmount = data.TotalAmount;
                    editeOrder.TotalCost   = data.TotalCost;
                    editeOrder.ModifiedAt  = data.ModifiedAt = DateTime.Now;
                    editeOrder.ModifiedBy  = data.ModifiedBy = data.ModifiedBy = ClientIdentityId;
                    await Db.SaveChangesAsync();


                    var orderDetailsData = new TblOrderDetails
                    {
                        PkOrderDetailsId = data.OrderDetailId = Guid.NewGuid(),
                        OrderId          = cartOrderId.PkOrdersId,
                        FkItemsId        = data.ItemId,
                        CraetedAt        = data.CraetedAt = DateTime.Now,
                        CreatedBy        = ClientIdentityId,
                        ModifiedAt       = data.ModifiedAt = DateTime.Now,
                        ModifiedBy       = data.ModifiedBy = ClientIdentityId,
                        Quantity         = 1,
                    };
                    await Db.TblOrderDetails.AddAsync(orderDetailsData);

                    await Db.SaveChangesAsync();

                    return(Ok(new { message = " Your Order Has Been Placed Successfully :) " }));
                }
                else
                {
                    return(BadRequest(" Something Went Wrong :( "));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }