コード例 #1
0
        public IHttpActionResult PutCustomer_Order(int id, Customer_Order customer_Order)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customer_Order.Customer_Order_ID)
            {
                return(BadRequest());
            }

            db.Entry(customer_Order).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!Customer_OrderExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #2
0
        /// <inheritdoc />
        public ServiceResponse ConvertToHTML(string json, int caseId, Guid orderId)
        {
            ServiceResponse response = new ServiceResponse();
            var             order    = JsonConvert.DeserializeObject <ExternalOrderFormServiceResponseDto>(json);

            if (!ValidateRequest(order))
            {
                response.IsSuccess = false;
                response.Message   = "Invalid Param";
                return(response);
            }
            int  supplierid   = Convert.ToInt32(order.responses.FirstOrDefault().hidden.supplierid);
            Guid userid       = Guid.Parse(order.responses.FirstOrDefault().hidden.userid);
            int  departmentid = Convert.ToInt32(order.responses.FirstOrDefault().hidden.departmentid);
            int  customerid   = Convert.ToInt32(order.responses.FirstOrDefault().hidden.customerid);
            int  channel      = Convert.ToInt32(order.responses.FirstOrDefault().hidden.channel);

            Supplier       supplier      = _supplierService.Find(supplierid);
            Customer       member        = _customerService.Find(customerid);
            Department     department    = member.Departments.FirstOrDefault(d => d.Id == departmentid);
            AspNetUser     user          = _accountService.GetUser(userid);
            Customer_Order customerOrder = _customerOrderService.GetNewOrders(orderId);

            string html = "<html><body>{0}</body></html>";
            string body = GetHtmlBody(user, supplier, member, department);
            string rows = ConvertLineToHtml(customerOrder.OrderLines.ToList(), caseId.ToString(), user.LanguageCode);

            body = String.Format(body, rows);
            html = String.Format(html, body);

            response.IsSuccess = true;
            response.Message   = html;
            return(response);
        }
        public object cancelCusOrder(Customer_Order OrderUpdate)
        {
            db.Configuration.ProxyCreationEnabled = false;

            Customer_Order objectOrder = new Customer_Order();
            dynamic        toReturn    = new ExpandoObject();
            var            id          = OrderUpdate.CustomerOrderID;

            try
            {
                objectOrder = db.Customer_Order.Where(x => x.CustomerOrderID == id).FirstOrDefault();
                if (objectOrder != null && objectOrder.CustomerOrderStatusID == 1)
                {
                    objectOrder.CustomerOrderStatusID = 4;
                    db.SaveChanges();

                    toReturn.Message = "The customer order has successfully been cancelled.";
                }
                else
                {
                    toReturn.Message = "Only 'Placed' customer orders can be cancelled.";
                }
            }

            catch (Exception)
            {
                toReturn.Message = "Cancellation not successful.";
            }


            return(toReturn);
        }
コード例 #4
0
        public string Edit(string customer_id, int?id)
        {
            try
            {
                Customer_Order Data_Amdani = Data.Customer_Order.Single(Z => Z.Id == id && Z.Purches_id == customer_id && Z.Order_Status == 1);

                if (Data_Amdani.Unit_name == "PACKAGE")
                {
                    return("Sorry !! You cant get More then 1 Package");
                }


                else
                {
                    Data_Amdani.Unit_number++;
                    Data.Entry(Data_Amdani).State = EntityState.Modified;
                    Data.SaveChanges();
                    return(Data_Amdani.Product_Name + " is successfully Incrimented.");
                }
            }

            catch
            {
                return("Missing Data");
            }
        }
コード例 #5
0
ファイル: OfferClass.cs プロジェクト: pori468/HatBazar
        public int Add_Package_ToCart(int Package_id, string customerid)
        {
            try
            {
                Package PP = Data.Package.Single(x => x.Package_Id == Package_id);

                Customer_Order CO = new Customer_Order();
                CO.Product_Name = PP.Package_Name;
                CO.Unit_name    = "PACKAGE";
                CO.Unit_number  = 1;
                CO.Price        = PP.Price;
                CO.Purches_id   = customerid;
                CO.Product_id   = PP.Package_Id;
                CO.Order_Status = 1;
                Data.Customer_Order.Add(CO);
                Data.SaveChanges();

                return(Data.Customer_Order.Count(x => x.Purches_id == customerid && x.Order_Status == 1));
            }


            catch
            {
                throw;
            }
        }
        public object collectCusOrder(Customer_Order OrderUpdate)
        {
            db.Configuration.ProxyCreationEnabled = false;

            Customer_Order objectOrder   = new Customer_Order();
            Payment        objectP       = new Payment();
            List <Payment> objectPayment = db.Payments.Include(x => x.Customer_Order).ToList();
            dynamic        toReturn      = new ExpandoObject();
            var            id            = OrderUpdate.CustomerOrderID;

            try
            {
                objectOrder = db.Customer_Order.Where(x => x.CustomerOrderID == id).FirstOrDefault();
                objectP     = db.Payments.Where(x => x.CustomerOrderID == id).FirstOrDefault();
                List <Product_Order_Line> product_Orders = db.Product_Order_Line.Include(x => x.Customer_Order).Include(x => x.Product).Where(x => x.CustomerOrderID == objectOrder.CustomerOrderID).ToList();

                if (objectP != null)
                {
                    List <dynamic> products = new List <dynamic>();
                    foreach (var prod in product_Orders)
                    {
                        Container_Product container_Product = db.Container_Product.Where(x => x.ContainerID == objectOrder.ContainerID && x.ProductID == prod.ProductID).FirstOrDefault();
                        if (container_Product.CPQuantity < prod.PLQuantity)
                        {
                            toReturn.Message = "Oops. Not enough stock.";
                        }
                        else
                        {
                            container_Product.CPQuantity = container_Product.CPQuantity - prod.PLQuantity;
                            db.SaveChanges();
                        }
                    }
                    if (objectOrder.CustomerOrderStatusID == 2)
                    {
                        objectOrder.CustomerOrderStatusID = 3;
                        db.SaveChanges();

                        toReturn.Message = "The customer order has successfully been collected.";
                    }
                    else
                    {
                        toReturn.Message = "Only 'Fulfilled' customer orders can be collected.";
                    }
                }
                else
                {
                    toReturn.Message = "Order has not been paid for yet.";
                }
            }
            catch (Exception)
            {
                toReturn.Message = "Cancellation not successful.";
            }


            return(toReturn);
        }
コード例 #7
0
        public IHttpActionResult GetCustomer_Order(int id)
        {
            Customer_Order customer_Order = db.Customer_Order.Find(id);

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

            return(Ok(customer_Order));
        }
コード例 #8
0
        public IHttpActionResult PostCustomer_Order(Customer_Order customer_Order)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.Customer_Order.Add(customer_Order);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = customer_Order.Customer_Order_ID }, customer_Order));
        }
        public object removeCustomerOrderProduct(int productID, int customerorderID, int quantity)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            try
            {
                Product product = db.Products.Where(x => x.ProductID == productID).FirstOrDefault();
                if (product != null)
                {
                    Customer_Order customerorder = db.Customer_Order.Where(x => x.CustomerOrderID == customerorderID).FirstOrDefault();
                    if (customerorder != null)
                    {
                        Product_Backlog backlog_Product = db.Product_Backlog.Where(x => x.ContainerID == customerorder.ContainerID && x.ProductID == product.ProductID).FirstOrDefault();
                        if (backlog_Product != null)
                        {
                            backlog_Product.QuantityToOrder = (backlog_Product.QuantityToOrder - quantity);
                            db.SaveChanges();

                            Product_Order_Line product_Order = db.Product_Order_Line.Where(x => x.ProductID == product.ProductID && x.CustomerOrderID == customerorder.CustomerOrderID).FirstOrDefault();
                            if (product_Order != null)
                            {
                                db.Product_Order_Line.Remove(product_Order);
                                db.SaveChanges();

                                toReturn.Product_Order_Line = product_Order;
                            }
                        }
                        else
                        {
                            toReturn.Error = "Container Not Found";
                        }
                    }
                    else
                    {
                        toReturn.Error = "Order Not Found";
                    }
                }
                else
                {
                    toReturn.Error = "Product Not Found";
                }
            }
            catch
            {
                toReturn.Error = "Product Removal Unsuccessful";
            }

            return(toReturn);
        }
コード例 #10
0
        public IHttpActionResult DeleteCustomer_Order(int id)
        {
            Customer_Order customer_Order = db.Customer_Order.Find(id);

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

            db.Customer_Order.Remove(customer_Order);
            db.SaveChanges();

            return(Ok(customer_Order));
        }
        public object makeOrderPayment(int customerorderID, float payAmount, int paymentTypeID)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            try
            {
                Customer_Order customerorder = db.Customer_Order.Where(x => x.CustomerOrderID == customerorderID).FirstOrDefault();
                if (customerorder != null)
                {
                    if (customerorder.CustomerOrderStatusID != null)
                    {
                        Payment_Type paymentType = db.Payment_Type.Where(x => x.PaymentTypeID == paymentTypeID).FirstOrDefault();
                        if (paymentType != null)
                        {
                            Payment payment = new Payment();
                            payment.CustomerOrderID = customerorder.CustomerOrderID;
                            payment.Customer_Order  = customerorder;
                            payment.PayAmount       = payAmount;
                            payment.PayDate         = DateTime.Now;
                            payment.PaymentTypeID   = paymentTypeID;
                            db.Payments.Add(payment);
                            db.SaveChanges();

                            toReturn.Payment = db.Payments.ToList().LastOrDefault();
                        }
                        else
                        {
                            toReturn.Error = "Payment Type Not Found";
                        }
                    }
                    else
                    {
                        toReturn.Error = "Order isn't elgible for payment.";
                    }
                }
                else
                {
                    toReturn.Error = "Order Not Found";
                }
            }
            catch
            {
                toReturn.Error = "Payment Add Unsuccessful";
            }

            return(toReturn);
        }
コード例 #12
0
        public string Addtocart(int?id, string Customerid)
        {
            if (id != null)
            {
                try
                {
                    Product_Information product_Information = new Product_Information();
                    product_Information = Data.Product_Information.Single(x => x.Product_id == id);
                    bool Check = Data.Customer_Order.Any(x => x.Product_id == product_Information.Product_id && x.Purches_id == Customerid && x.Order_Status == 1);
                    if (Check == true)
                    {
                        Customer_Order Data_Amdani = Data.Customer_Order.Single(x => x.Product_id == product_Information.Product_id && x.Purches_id == Customerid && x.Order_Status == 1);

                        Data_Amdani.Unit_number++;
                        Data.Entry(Data_Amdani).State = EntityState.Modified;
                    }
                    else
                    {
                        Customer_Order CO = new Customer_Order();
                        CO.Product_Name = product_Information.Product_name;
                        CO.Unit_name    = product_Information.Unit;
                        CO.Unit_number  = 1;
                        CO.Price        = product_Information.Price_per_unit;
                        CO.Product_id   = product_Information.Product_id;
                        CO.Purches_id   = Customerid;
                        CO.Order_Status = 1;

                        Data.Customer_Order.Add(CO);
                    }

                    Data.SaveChanges();


                    return(product_Information.Product_name + "" + " Added Successfully.");
                }


                catch
                {
                    throw;
                }
            }
            else
            {
                return("Product could not be Added.");
            }
        }
コード例 #13
0
ファイル: Details.cshtml.cs プロジェクト: Vekz/ZTPSBD
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Customer_Order = await _context.Customer_Order
                             .Include(c => c.customer)
                             .Include(c => c.order).FirstOrDefaultAsync(m => m.Customer_id_customer == id);

            if (Customer_Order == null)
            {
                return(NotFound());
            }
            return(Page());
        }
コード例 #14
0
ファイル: Delete.cshtml.cs プロジェクト: Vekz/ZTPSBD
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Customer_Order = await _context.Customer_Order.FindAsync(id);

            if (Customer_Order != null)
            {
                _context.Customer_Order.Remove(Customer_Order);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
コード例 #15
0
ファイル: Edit.cshtml.cs プロジェクト: Vekz/ZTPSBD
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Customer_Order = await _context.Customer_Order
                             .Include(c => c.customer)
                             .Include(c => c.order).FirstOrDefaultAsync(m => m.Customer_id_customer == id);

            if (Customer_Order == null)
            {
                return(NotFound());
            }
            ViewData["Customer_id_customer"] = new SelectList(_context.Customer, "id_customer", "id_customer");
            ViewData["Order_id_order"]       = new SelectList(_context.Order, "id_order", "id_order");
            return(Page());
        }
        public object getAllCustomerOrders()
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            try
            {
                List <Customer_Order> customerorders = db.Customer_Order.ToList();
                List <dynamic>        CustomerOrders = new List <dynamic>();

                foreach (Customer_Order customerorder in customerorders)
                {
                    Customer_Order customerorder1 = db.Customer_Order.Where(x => x.CustomerOrderID == customerorder.CustomerOrderID).FirstOrDefault();
                    ///User user = sale1.User;
                    DateTime date = Convert.ToDateTime(customerorder1.CusOrdDate);
                    if (customerorder1 != null)
                    {
                        List <Product_Order_Line> product_Order_Line = db.Product_Order_Line.Where(x => x.CustomerOrderID == customerorder1.CustomerOrderID).ToList();

                        if (product_Order_Line != null)
                        {
                            dynamic searchedCustomerOrder = new ExpandoObject();
                            searchedCustomerOrder.CustomerOrderID = customerorder.CustomerID;
                            // searchedSale.UserName = user.UserName;
                            searchedCustomerOrder.CusOrdDate = date.ToString("yyyy-MM-dd");
                            CustomerOrders.Add(searchedCustomerOrder);
                        }
                    }
                }

                toReturn.CustomerOrders = CustomerOrders;
            }
            catch
            {
                toReturn.Error = "Search Interrupted. Retry";
            }

            return(toReturn);
        }
コード例 #17
0
        private List <email_attachment> GetEmailAttachment(Customer_Order customerOrder)
        {
            if (customerOrder.CustomerOrderAttachments != null && customerOrder.CustomerOrderAttachments.ToList().Count > 0)
            {
                List <email_attachment> emailAttachmentList = new List <email_attachment>();

                foreach (var item in customerOrder.CustomerOrderAttachments.ToList())
                {
                    var destination = ConfigurationManager.AppSettings["TempDir"].ToString();
                    if (!Directory.Exists(destination))
                    {
                        Directory.CreateDirectory(destination);
                    }

                    string fileUrl = _fileStore.Download(item.FileURL, destination);

                    if (!string.IsNullOrEmpty(fileUrl) && File.Exists(fileUrl))
                    {
                        byte[] filedata = File.ReadAllBytes(fileUrl);
                        var    base64   = Convert.ToBase64String(filedata);

                        emailAttachmentList.Add(new email_attachment
                        {
                            content = base64,
                            name    = item.FileName,
                            type    = item.MimeType
                        });

                        File.Delete(fileUrl);
                    }
                }

                return(emailAttachmentList);
            }

            return(null);
        }
コード例 #18
0
        public string Delete(string customer_id, int?id)
        {
            try
            {
                Customer_Order Data_Amdani = Data.Customer_Order.Single(Z => Z.Id == id && Z.Purches_id == customer_id && Z.Order_Status == 1);
                if (Data_Amdani.Unit_number > 1)
                {
                    Data_Amdani.Unit_number--;
                    Data.Entry(Data_Amdani).State = EntityState.Modified;
                }

                else
                {
                    Data.Customer_Order.Remove(Data_Amdani);
                }
                Data.SaveChanges();
                return(Data.Customer_Order.Count(x => x.Purches_id == customer_id && x.Order_Status == 1).ToString());
            }

            catch
            {
                throw;
            }
        }
        public object initiatePlaceOrder(int customerID, dynamic session)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            toReturn.products      = new ExpandoObject();
            toReturn.CustomerOrder = new Sale();
            toReturn.Customer      = new ExpandoObject();
            toReturn.VAT           = new ExpandoObject();


            try
            {
                //get customer details
                Customer customer = new Customer();
                customer          = db.Customers.Where(x => x.CustomerID == customerID).FirstOrDefault();
                toReturn.Customer = customer;

                Container con = new Container();
                //get container of current user
                string sessionID = session.token;
                var    user      = db.Users.Where(x => x.SessionID == sessionID).FirstOrDefault();

                if (user.ContainerID == null)
                {
                    return(toReturn.Error = ("Curent Container Not Found"));
                }
                con = db.Containers.Where(x => x.ContainerID == user.ContainerID).FirstOrDefault();
                if (con == null)
                {
                    return(toReturn.Error = ("Curent Container Not Found"));
                }
                //get products in container
                List <Container_Product> conProd = db.Container_Product.Include(x => x.Product).Where(x => x.CPQuantity < 1 && x.ContainerID == con.ContainerID).ToList();



                //get todays date
                DateTime CustomerOrderDate = DateTime.Now;

                //get payment types
                toReturn.Customer = db.Payment_Type.ToList();



                if (conProd != null)
                {
                    Customer_Order prevOrder   = db.Customer_Order.ToList().LastOrDefault();
                    int            prevOrderNo = Convert.ToInt32(prevOrder.CusOrdNumber);
                    int            OrderNo     = prevOrderNo + 1;

                    //Get List Of products with current price
                    List <Product> productsList = db.Products.ToList();
                    List <dynamic> products     = new List <dynamic>();
                    foreach (var prod in conProd)
                    {
                        Price price = db.Prices.Include(x => x.Product).Where(x => x.PriceStartDate <= DateTime.Now && x.PriceEndDate >= DateTime.Now && x.ProductID == prod.ProductID).ToList().LastOrDefault();
                        if (price != null)
                        {
                            double  Price          = (double)price.UPriceR;
                            dynamic productDetails = new ExpandoObject();
                            productDetails.ProductCategoryID = prod.Product.ProductCategoryID;
                            productDetails.ProductID         = prod.Product.ProductID;
                            productDetails.ProdBarcode       = prod.Product.ProdBarcode;
                            productDetails.ProdDescription   = prod.Product.ProdDesciption;
                            productDetails.Prodname          = prod.Product.ProdName;
                            productDetails.CPQuantity        = prod.CPQuantity;
                            productDetails.Quantity          = 0;
                            productDetails.Price             = Math.Round(Price, 2);
                            productDetails.Subtotal          = 0.0;

                            products.Add(productDetails);
                        }
                    }
                    toReturn.products = products;

                    //get VAT
                    toReturn.VAT = db.VATs.Where(x => x.VATStartDate <= DateTime.Now).ToList().LastOrDefault();

                    //set up sale
                    Customer_Order newCustomerOrder = new Customer_Order();
                    newCustomerOrder.CusOrdDate            = CustomerOrderDate;
                    newCustomerOrder.CusOrdNumber          = Convert.ToString(OrderNo);
                    newCustomerOrder.UserID                = user.UserID;
                    newCustomerOrder.User                  = user;
                    newCustomerOrder.CustomerID            = customer.CustomerID;
                    newCustomerOrder.CustomerOrderStatusID = 3;
                    newCustomerOrder.Customer              = customer;
                    newCustomerOrder.Container             = con;
                    newCustomerOrder.ContainerID           = con.ContainerID;
                    db.Customer_Order.Add(newCustomerOrder);
                    db.SaveChanges();

                    //getsale
                    toReturn.CustomerOrder = db.Customer_Order.ToList().LastOrDefault();
                }
                else
                {
                    return(toReturn.Message = "All products seem to be in stock.");
                }
            }
            catch
            {
                toReturn.Error = "Please Reload Page to Initiate Order";
            }

            return(toReturn);
        }
        public object addCustomerOrderProduct(int productID, int customerorderID, int quantity)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            try
            {
                Product product = db.Products.Where(x => x.ProductID == productID).FirstOrDefault();
                if (product != null)
                {
                    Customer_Order customerorder = db.Customer_Order.Where(x => x.CustomerOrderID == customerorderID).FirstOrDefault();
                    if (customerorder != null)
                    {
                        Product_Backlog backlog_Product = db.Product_Backlog.Where(x => x.ContainerID == customerorder.ContainerID && x.ProductID == product.ProductID).FirstOrDefault();
                        if (backlog_Product != null)
                        {
                            backlog_Product.QuantityToOrder = (backlog_Product.QuantityToOrder + quantity);
                            db.SaveChanges();

                            Product_Order_Line product_Order_Line = db.Product_Order_Line.Where(x => x.ProductID == product.ProductID && x.CustomerOrderID == customerorder.CustomerOrderID).FirstOrDefault();
                            if (product_Order_Line == null)
                            {
                                Product_Order_Line newProduct_Order_Line = new Product_Order_Line();
                                newProduct_Order_Line.ProductID       = product.ProductID;
                                newProduct_Order_Line.Product         = product;
                                newProduct_Order_Line.CustomerOrderID = customerorder.CustomerOrderID;
                                newProduct_Order_Line.Customer_Order  = customerorder;
                                newProduct_Order_Line.PLQuantity      = quantity;
                                db.Product_Order_Line.Add(newProduct_Order_Line);
                                db.SaveChanges();

                                toReturn.Product_Order_Line = db.Product_Order_Line.ToList().LastOrDefault();
                            }
                            else
                            {
                                product_Order_Line.PLQuantity = product_Order_Line.PLQuantity + quantity;
                                db.SaveChanges();

                                toReturn.Product_Order_Line = product_Order_Line;
                            }
                        }
                        else
                        {
                            toReturn.Error = "Container Not Found";
                        }
                    }
                    else
                    {
                        toReturn.Error = "Order Not Found";
                    }
                }
                else
                {
                    toReturn.Error = "Product Not Found";
                }
            }
            catch
            {
                toReturn.Error = "Product Add Unsuccessful";
            }

            return(toReturn);
        }
        public object cancelCustomerOrder(int customerorderID)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            try
            {
                //get sale
                Customer_Order newCustomerOrder = db.Customer_Order.Where(x => x.CustomerOrderID == customerorderID).FirstOrDefault();
                if (newCustomerOrder != null)
                {
                    //get container
                    Container container = db.Containers.Where(x => x.ContainerID == newCustomerOrder.ContainerID).FirstOrDefault();



                    //get list of products in Sale
                    List <Product_Order_Line> product_Order_line = newCustomerOrder.Product_Order_Line.ToList();

                    if (container != null)
                    {
                        if (product_Order_line != null)
                        {
                            foreach (var prod in product_Order_line)
                            {
                                Product product = db.Products.Where(x => x.ProductID == prod.ProductID).FirstOrDefault();
                                if (product != null)
                                {
                                    Product_Backlog backlog_Product = db.Product_Backlog.Where(x => x.ContainerID == newCustomerOrder.ContainerID && x.ProductID == product.ProductID).FirstOrDefault();
                                    if (backlog_Product != null)
                                    {
                                        backlog_Product.QuantityToOrder = (backlog_Product.QuantityToOrder + prod.PLQuantity);
                                        db.SaveChanges();

                                        Product_Order_Line product_Order_Line = db.Product_Order_Line.Where(x => x.ProductID == product.ProductID && x.CustomerOrderID == newCustomerOrder.CustomerOrderID).FirstOrDefault();
                                        if (product_Order_Line != null)
                                        {
                                            db.Product_Order_Line.Remove(product_Order_Line);
                                            db.SaveChanges();
                                        }
                                    }
                                }
                                else
                                {
                                    toReturn.Error = "Product Not Found";
                                }
                            }

                            toReturn.Message = "Order Cancelled";
                        }
                    }
                    else
                    {
                        toReturn.Error = "Container Not Found";
                    }
                }


                else
                {
                    toReturn.Error = "Cancel Failed: Order Not Found";
                }
            }
            catch
            {
                toReturn.Error = "Customer Order Cancellation Unsuccessfully Completed";
            }

            return(toReturn);
        }
        public dynamic placeOrder(Customer_Order order)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            toReturn.newOrder = new ExpandoObject();
            string newOrderNo = "";

            try
            {
                //Get Product Order Line Details from order
                List <Product_Order_Line> productList = order.Product_Order_Line.ToList();

                if (order != null && productList != null)
                {
                    Customer              customer     = db.Customers.Where(x => x.CustomerID == order.CustomerID).FirstOrDefault();
                    User                  user         = db.Users.Where(x => x.UserID == order.UserID).FirstOrDefault();
                    Container             con          = db.Containers.Where(x => x.ContainerID == order.ContainerID).FirstOrDefault();
                    Customer_Order_Status order_Status = db.Customer_Order_Status.Where(x => x.CODescription == "Placed").FirstOrDefault();

                    //save customer order details
                    Customer_Order customerOrder = new Customer_Order();
                    customerOrder.Customer = customer;
                    customerOrder.Customer_Order_Status = order_Status;
                    customerOrder.User         = user;
                    customerOrder.Container    = con;
                    customerOrder.CusOrdNumber = order.CusOrdNumber;
                    customerOrder.CusOrdDate   = DateTime.Now;


                    db.Customer_Order.Add(customerOrder);
                    db.SaveChanges();

                    //Get The Saved Order details form the db
                    Customer_Order placedOrder = db.Customer_Order.ToList().LastOrDefault();

                    if (placedOrder != null)
                    {
                        //Add the Product_Order_Line Records for each product
                        foreach (var prod in productList)
                        {
                            Product            product   = db.Products.Where(x => x.ProductID == prod.ProductID).FirstOrDefault();
                            Product_Order_Line orderProd = new Product_Order_Line();
                            orderProd.Customer_Order = placedOrder;
                            orderProd.Product        = product;
                            orderProd.PLQuantity     = prod.PLQuantity;

                            db.Product_Order_Line.Add(orderProd);
                            db.SaveChanges();
                        }

                        //Get the placed Orders Order Number
                        newOrderNo = placedOrder.CusOrdNumber;
                    }

                    else
                    {
                        toReturn.Message = "Something went wrong adding the products!";
                    }

                    //Set the return Objects
                    toReturn.newOrder = searchByOrderNo(newOrderNo);
                    toReturn.Message  = "Success! Order was placed successfully and email confirmation sent.";
                }
                else
                {
                    toReturn.Message = " Null Parameters Received";
                }
            }

            catch (Exception error)
            {
                toReturn.Message = error.Message;
            }

            return(toReturn);
        }
        public object DeleteContainer(int id)
        {
            db.Configuration.ProxyCreationEnabled = false;

            Container objectContainer = new Container();
            dynamic   toReturn        = new ExpandoObject();

            try
            {
                objectContainer = db.Containers.Find(id);

                if (objectContainer == null)
                {
                    toReturn.Message = "Record Not Found";
                }
                else
                {
                    User user                = db.Users.Where(x => x.ContainerID == id).FirstOrDefault();
                    Container_Product con    = db.Container_Product.Where(x => x.ContainerID == id).FirstOrDefault();
                    Sale            sale     = db.Sales.Where(x => x.ContainerID == id).FirstOrDefault();
                    Customer_Order  order    = db.Customer_Order.Where(x => x.ContainerID == id).FirstOrDefault();
                    Supplier_Order  suporder = db.Supplier_Order.Where(x => x.ContainerID == id).FirstOrDefault();
                    Product_Backlog prod     = db.Product_Backlog.Where(x => x.ContainerID == id).FirstOrDefault();
                    if (user == null && con == null && sale == null && order == null && suporder == null && prod == null)
                    {
                        List <Manager> managers = db.Managers.ToList();
                        if (managers.Count != 0)
                        {
                            foreach (Manager man in managers)
                            {
                                List <Container> containers = man.Containers.ToList();
                                foreach (Container container in containers)
                                {
                                    if (container.ContainerID == id)
                                    {
                                        objectContainer.InActive = true;
                                        db.SaveChanges();
                                        toReturn.Message = "Delete Restricted But Container Set to Inactive";
                                        return(toReturn);
                                    }
                                }
                            }
                        }
                        db.Containers.Remove(objectContainer);
                        db.SaveChanges();
                        toReturn.Message = "Delete Successful";
                    }
                    else
                    {
                        objectContainer.InActive = true;
                        db.SaveChanges();
                        toReturn.Message = "Delete Restricted But Container Set to Inactive";
                        return(toReturn);
                    }
                }
            }
            catch
            {
                toReturn.Message = "Delete Unsuccesful";
            }

            return(toReturn);
        }
        public object initiatePlaceOrder(int customerID, dynamic session)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            toReturn.customer  = new ExpandoObject();
            toReturn.orderInfo = new ExpandoObject();
            toReturn.products  = new ExpandoObject();


            try
            {
                Container con = new Container();
                //get container of current user
                string sessionID = session.token;
                var    user      = db.Users.Where(x => x.SessionID == sessionID).FirstOrDefault();

                if (user.ContainerID == null)
                {
                    return(toReturn.Error = ("Curent Container Not Found"));
                }
                con = db.Containers.Where(x => x.ContainerID == user.ContainerID).FirstOrDefault();
                if (con == null)
                {
                    return(toReturn.Error = ("Curent Container Not Found"));
                }
                //get products in container
                List <Container_Product> conProd = db.Container_Product.Include(x => x.Product).Where(x => x.CPQuantity < 1 && x.ContainerID == con.ContainerID).ToList();

                //get customer details
                Customer customer = new Customer();
                customer          = db.Customers.Where(x => x.CustomerID == customerID).FirstOrDefault();
                toReturn.customer = customer;


                if (customer != null)
                {
                    //Get Order No
                    Customer_Order prevOrder   = db.Customer_Order.ToList().LastOrDefault();
                    Customer_Order prevID      = db.Customer_Order.ToList().LastOrDefault();
                    int            prevOrderNo = Convert.ToInt32(prevOrder.CusOrdNumber);
                    int            OrderNo     = prevOrderNo + 1;

                    //Get Todays date
                    var orderDate = DateTime.Now.ToString("yyyy-MM-dd");

                    //Set Order No And Order Date In Dynamic Object
                    dynamic orderInfo = new ExpandoObject();
                    orderInfo.OrderNo   = OrderNo;
                    orderInfo.OrderDate = orderDate;
                    orderInfo.VatPerc   = db.VATs.Where(x => x.VATStartDate <= DateTime.Now).ToList().LastOrDefault();

                    toReturn.orderInfo = orderInfo;

                    //Get List Of products with current price
                    List <Product> productsList = db.Products.ToList();
                    List <dynamic> products     = new List <dynamic>();
                    foreach (var prod in conProd)
                    {
                        Price price = db.Prices.Include(x => x.Product).Where(x => x.PriceStartDate <= DateTime.Now && x.PriceEndDate >= DateTime.Now && x.ProductID == prod.ProductID).FirstOrDefault();
                        if (price != null)
                        {
                            double  Price          = (double)price.UPriceR;
                            dynamic productDetails = new ExpandoObject();
                            productDetails.ProductCategoryID = prod.Product.ProductCategoryID;
                            productDetails.ProductID         = prod.ProductID;
                            productDetails.ProdDescription   = prod.Product.ProdDesciption;
                            productDetails.Prodname          = prod.Product.ProdName;
                            productDetails.Quantity          = 0;
                            productDetails.Price             = Math.Round(Price, 2);
                            productDetails.Subtotal          = 0.0;

                            products.Add(productDetails);
                        }
                    }
                    toReturn.products = products;

                    toReturn.VAT = db.VATs.Where(x => x.VATStartDate <= DateTime.Now).ToList().LastOrDefault();
                    Customer_Order_Status order_Status = db.Customer_Order_Status.Where(x => x.CODescription == "Placed").FirstOrDefault();

                    //set up sale
                    Customer_Order customerOrder = new Customer_Order();
                    customerOrder.Customer = customer;
                    customerOrder.Customer_Order_Status = order_Status;
                    customerOrder.UserID       = user.UserID;
                    customerOrder.User         = user;
                    customerOrder.Container    = con;
                    customerOrder.ContainerID  = con.ContainerID;
                    customerOrder.CusOrdNumber = Convert.ToString(OrderNo);
                    customerOrder.CusOrdDate   = DateTime.Now;
                    db.Customer_Order.Add(customerOrder);
                    db.SaveChanges();

                    toReturn.CustomerOrder    = db.Customer_Order.ToList().LastOrDefault();
                    orderInfo.CustomerOrderID = customerOrder.CustomerOrderID;
                }
                else
                {
                    toReturn.Message = "No products were found. All products seem to be in stock.";
                }
            }

            catch (Exception error)
            {
                toReturn.Message = error.Message;
            }

            return(toReturn);
        }
        public object getCustomerOrder(int id)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn    = new ExpandoObject();
            double  TotalIncVat = 0.0;

            toReturn.calculatedValues      = new ExpandoObject();
            toReturn.customerorderProducts = new List <dynamic>();
            toReturn.customerorderDate     = new ExpandoObject();
            DateTime customerorderDate = DateTime.Now;

            try
            {
                Customer_Order searchedCustomerOrder = db.Customer_Order.Include(x => x.Product_Order_Line).Include(x => x.Customer).Include(x => x.User).Where(x => x.CustomerOrderID == id).FirstOrDefault();
                if (searchedCustomerOrder != null)
                {
                    //get list of Products in sale from db
                    List <Product_Order_Line> product_Order_line = db.Product_Order_Line.Include(x => x.Customer_Order).Include(x => x.Product).Where(x => x.CustomerOrderID == searchedCustomerOrder.CustomerOrderID).ToList();


                    List <dynamic> products = new List <dynamic>();
                    foreach (var prod in product_Order_line)
                    {
                        //Get Price For Each Product
                        Price price = db.Prices.Include(x => x.Product).Where(x => x.PriceStartDate <= searchedCustomerOrder.CusOrdDate && x.PriceEndDate >= searchedCustomerOrder.CusOrdDate && x.ProductID == prod.ProductID).FirstOrDefault();

                        if (price != null)
                        {
                            //Calculate Product Subtotal
                            double unitPrice = (double)price.UPriceR;
                            double quantity  = (double)prod.PLQuantity;
                            double subtotal  = unitPrice * quantity;

                            //Create Object And Populate With Product Related Details
                            dynamic productDetails = new ExpandoObject();
                            productDetails.ProductID       = prod.ProductID;
                            productDetails.Prodname        = prod.Product.ProdName;
                            productDetails.ProdDescription = prod.Product.ProdDesciption;
                            productDetails.Quantity        = prod.PLQuantity.ToString();
                            productDetails.Price           = price.UPriceR.ToString();
                            productDetails.Subtotal        = subtotal.ToString("#.##");

                            TotalIncVat = TotalIncVat + subtotal;

                            products.Add(productDetails);
                        }
                        //set list of products to return object
                        toReturn.saleProducts = products;


                        var vatOnDate = db.VATs.Where(x => x.VATStartDate <= searchedCustomerOrder.CusOrdDate).FirstOrDefault();
                        if (vatOnDate != null)
                        {
                            ///Calculate Sale Amounts
                            double vatPerc     = (double)vatOnDate.VATPerc;
                            double vat         = (vatPerc / (vatPerc + 100)) * TotalIncVat;
                            double TotalExcVat = TotalIncVat - vat;


                            //set and Populate With Calculated Details
                            dynamic calculations = new ExpandoObject();
                            calculations.TotalIncVat  = TotalIncVat.ToString("#.##");
                            calculations.TotalExcVat  = TotalExcVat.ToString("#.##");
                            calculations.Vat          = vat.ToString("#.##");
                            toReturn.calculatedValues = calculations;

                            //set Sale date to return object
                            customerorderDate          = Convert.ToDateTime(searchedCustomerOrder.CusOrdDate);
                            toReturn.customerorderDate = customerorderDate;
                        }
                        else
                        {
                            toReturn.Message = "Something Went Wrong Price is null";
                        }
                    }
                }
            }

            catch
            {
                toReturn.Message = "Order Search Inturrupted, Retry";
            }

            return(toReturn);
        }
        public object searchByOrderNo(string orderNo)
        {
            db.Configuration.ProxyCreationEnabled = false;
            dynamic toReturn = new ExpandoObject();

            toReturn.orderDetails     = new ExpandoObject();
            toReturn.customerDetails  = new ExpandoObject();
            toReturn.calculatedValues = new ExpandoObject();
            toReturn.orderProducts    = new List <dynamic>();
            double   TotalIncVat = 0.0;
            DateTime orderDate   = DateTime.Now;


            try
            {
                //Get Customer Order Details From Db
                Customer_Order order = db.Customer_Order.Include(x => x.Customer).Include(x => x.Customer_Order_Status).Where(x => x.CusOrdNumber == orderNo).FirstOrDefault();

                if (order != null)
                {
                    //Get List Of Products In Customer Order From Db
                    List <Product_Order_Line> orderProduct = db.Product_Order_Line.Include(x => x.Customer_Order).Include(x => x.Product).Where(x => x.Customer_Order.CusOrdNumber == orderNo).ToList();


                    List <dynamic> products = new List <dynamic>();
                    foreach (var prod in orderProduct)
                    {
                        //Get Price For Each Product
                        Price price = db.Prices.Include(x => x.Product).Where(x => x.PriceStartDate <= order.CusOrdDate && x.PriceEndDate >= order.CusOrdDate && x.ProductID == prod.ProductID).FirstOrDefault();

                        if (price != null)
                        {
                            //Calculate Product Subtotal
                            double unitPrice = (double)price.UPriceR;
                            double quantity  = (double)prod.PLQuantity;
                            double subtotal  = unitPrice * quantity;

                            //Create Object And Populate With Product Related Details
                            dynamic productDetails = new ExpandoObject();
                            productDetails.ProductID       = prod.ProductID;
                            productDetails.Prodname        = prod.Product.ProdName;
                            productDetails.ProdDescription = prod.Product.ProdDesciption;
                            productDetails.Quantity        = prod.PLQuantity.ToString();
                            productDetails.Price           = price.UPriceR.ToString();
                            productDetails.Subtotal        = subtotal.ToString("#.##");

                            TotalIncVat = TotalIncVat + subtotal;

                            products.Add(productDetails);
                        }
                        //set list of products to return object
                        toReturn.orderProducts = products;

                        var vatOnDate = db.VATs.Where(x => x.VATStartDate <= order.CusOrdDate).FirstOrDefault();
                        if (vatOnDate != null)
                        {
                            ///Calculate Order Amounts
                            double vatPerc     = (double)vatOnDate.VATPerc;
                            double vat         = (vatPerc / (vatPerc + 100)) * TotalIncVat;
                            double TotalExcVat = TotalIncVat - vat;
                            orderDate = Convert.ToDateTime(order.CusOrdDate);

                            //Create objects to store the seperated details
                            dynamic cusOrder     = new ExpandoObject();
                            dynamic cusdetails   = new ExpandoObject();
                            dynamic calculations = new ExpandoObject();

                            //Populate With Customer Order Details
                            cusOrder.CustomerOrderID = order.CustomerOrderID;
                            cusOrder.OrderNo         = order.CusOrdNumber;
                            cusOrder.CusOrdStatus    = order.Customer_Order_Status.CODescription;
                            cusOrder.OrderDate       = orderDate.ToString("yyyy-MM-dd");

                            //Populate With  Customer Details
                            cusdetails.CustomerID = order.CustomerID;
                            cusdetails.CusName    = order.Customer.CusName;
                            cusdetails.CusSurname = order.Customer.CusSurname;
                            cusdetails.CusCell    = order.Customer.CusCell;
                            cusdetails.CusEmail   = order.Customer.CusEmail;

                            //Populate With Calculated Details
                            calculations.TotalIncVat = TotalIncVat.ToString("#.##");
                            calculations.TotalExcVat = TotalExcVat.ToString("#.##");
                            calculations.Vat         = vat.ToString("#.##");


                            //set objects to return
                            toReturn.orderDetails     = cusOrder;
                            toReturn.customerDetails  = cusdetails;
                            toReturn.calculatedValues = calculations;
                            toReturn.orderProducts    = products;
                        }


                        else
                        {
                            toReturn.Message = "Something Went Wrong Price is null";
                        }
                    }
                }
                else
                {
                    toReturn.Message = "Order(s) Not Found";
                }
            }
            catch (Exception error)
            {
                toReturn.Message = "Something Went Wrong: " + error.Message;
            }


            return(toReturn);
        }