static void TestRemove() { using var context = new DbContextFactory().CreateDbContext(); var unitOfWork = CreateUnitOfWork(context); var productService = new ProductService(unitOfWork); var articleService = new ArticleService(unitOfWork); var customerService = new CustomerService(unitOfWork); var orderService = new OrderService(unitOfWork); var orderLineService = new OrderLineService(unitOfWork); var customer = customerService.Create(new Customer { FirstName = "Test", LastName = "Test", Email = "*****@*****.**" }); var product = productService.Create(new Product { Name = "Test", Description = "Test", Manufacturer = "Test", Publisher = "Test", RentalExpiresAfterDays = 10 }); var article = articleService.Create(new Article { ProductId = product.Id, Status = ArticleStatus.Normal }); var order = orderService.Create(customer.Id); var orderLine = orderLineService.Rent(order.Id, article.Id); var deleteResult = customerService.Remove(product.Id); }
/*Removes an item from the order if allowed.*/ protected void gvOrderDetails_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("CancelOrderLine")) { try { //get the orderline GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer); int index = gvr.RowIndex; //int index = Convert.ToInt32(e.CommandArgument.ToString()); String orderLineID = gvOrderDetails.Rows[index].Cells[1].Text; OrderLine orderLine = new OrderLineService().getByID(orderLineID); //Throws NoRecordException if (orderLine.orderLineType.id == 1) //only allow cancelling of rent-lines (type 1) { if ((orderLine.startdate - DateTime.Today.Date) >= TimeSpan.FromDays(2)) //only allow cancelling if there is at least 2 days between today and the startdate { if (new OrderModel().deleteItemFromOrder(orderLine)) { //succesfully removed - refresh the details gridview displayOrderDetails(orderLine.order.order_id.ToString()); } else { //something went wrong string script = "alert(\"An error occured while trying to removing this item from your order.\");"; ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true); } } else if ((orderLine.startdate - DateTime.Today.Date) < TimeSpan.FromDays(0)) { //can't remove this copy anymore - too late (already shipped) string script = "alert(\"Can't cancel an item that has already shipped!\");"; ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true); } else { //can't remove this copy anymore - too late (shipping soon) string script = "alert(\"This item will ship within 2 days and can no longer be cancelled.\");"; ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true); } } else { //can't remove a BUY copy string script = "alert(\"Only rent-copies can be cancelled.\");"; ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true); } } catch (NoRecordException) { } } }
protected void btnPay_Click(object sender, EventArgs e) { Customer user = (Customer)Session["user"]; if (user != null) { try { //get the order String orderID = lblStatus.Text; Order order = new OrderService().getByID(orderID); //Throws NoRecordException new OrderModel().payOrder(order); //Throws NoRecordException || DALException //get the orderLines List <OrderLine> orderLines = new OrderLineService().getByOrder(order); //Throws NoRecordException //check if all orderLines can be given a dvdCopy Boolean allInStock = hasAllInStock(orderLines); //send the user an order confirmation String currency = "€"; if (CookieUtil.CookieExists("currency")) { if (CookieUtil.GetCookieValue("currency").Equals("usd")) { currency = "$"; } } new EmailModel().sendOrderConfirmationEmail(user, order, orderLines, allInStock, currency); Response.Redirect("~/ThankYou.aspx"); } catch (NoRecordException) { } } else { lblStatus.Text = "Please log in to access this page"; } }
/*Displays the details of the selected order.*/ private void displayOrderDetails(String orderID) { try { String currency = "€"; if (Request.QueryString["currency"] == null) { if (CookieUtil.CookieExists("currency")) { if (CookieUtil.GetCookieValue("currency").Equals("usd")) { currency = "$"; } } } else if (Request.QueryString["currency"].Equals("usd")) { currency = "$"; } //show details pnlOrderDetails.Visible = true; //get the order info OrderService orderService = new OrderService(); Order selectedOrder = orderService.getByID(orderID); //Throws NoRecordException lblOrderStatus.Text = "(" + selectedOrder.orderstatus.name + ")"; //1 = new, 2 = paid, 3 = shipped lblOrderID.Text = selectedOrder.order_id.ToString(); //hide pay button if the order has already been paid if (selectedOrder.orderstatus.id != 1) { lblPay.Visible = false; btnPay.Visible = false; } else { lblPay.Visible = true; btnPay.Visible = true; } //get all articles in the order and display them Order order = orderService.getByID(orderID); //Throws NoRecordException List <OrderLine> orderLines = new OrderLineService().getByOrder(order); //Throws NoRecordException Boolean hasRentItems = false; foreach (OrderLine item in orderLines) { if (item.orderLineType.id == 1) { hasRentItems = true; } } DataTable orderTable = new DataTable(); orderTable.Columns.Add("Item number"); orderTable.Columns.Add("Name"); orderTable.Columns.Add("Type"); orderTable.Columns.Add("Price"); if (hasRentItems) { orderTable.Columns.Add("Start date"); orderTable.Columns.Add("End date"); } foreach (OrderLine item in orderLines) { DataRow orderRow = orderTable.NewRow(); orderRow[0] = item.orderline_id; orderRow[1] = item.dvdInfo.name; orderRow[2] = item.orderLineType.name; orderRow[3] = currency + " " + setPriceInRightCurrency(item.dvdInfo.buy_price, currency); if (item.orderLineType.name.Equals("Verhuur")) { orderRow[4] = item.startdate.ToString("dd/MM/yyyy"); orderRow[5] = item.enddate.ToString("dd/MM/yyyy"); } orderTable.Rows.Add(orderRow); } gvOrderDetails.DataSource = orderTable; gvOrderDetails.DataBind(); //total cost OrderModel orderModel = new OrderModel(); lblTotalCost.Text = currency + " " + setPriceInRightCurrency((float)orderModel.getOrderCost(order), currency); //user has already paid, check status of copies in cart if (selectedOrder.orderstatus.id > 1) { Boolean allInStock = hasAllInStock(orderLines); updateOrderStatusDetails(allInStock); } } catch (NoRecordException) { //order does not exist, reload the current page to update the gridview and close this order's panel Response.Redirect("Orders.aspx"); } }
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Customer user = (Customer)Session["user"]; OrderService orderService = new OrderService(); if (user != null) { String orderID = Request.QueryString["order"]; try { Order order = orderService.getByID(orderID); //Throws NoRecordException if (order.customer.customer_id == user.customer_id) { String currency = "€"; if (Request.QueryString["currency"] == null) { //Check if the user has set the currencypreference if (CookieUtil.CookieExists("currency")) { if (CookieUtil.GetCookieValue("currency").Equals("usd")) { currency = "$"; } } } else { switch (Request.QueryString["currency"]) { case "usd": currency = "$"; break; } } //all good lblStatus.Text = orderID; OrderModel helper = new OrderModel(); lblCost.Text = currency + " " + Math.Round(helper.getOrderCost(order), 2).ToString(); List <OrderLine> orderLines = new OrderLineService().getByOrder(order); //Throws NoRecordException Boolean hasRentItems = false; foreach (OrderLine item in orderLines) { if (item.orderLineType.id == 1) { hasRentItems = true; } } DataTable orderTable = new DataTable(); orderTable.Columns.Add("Item number"); orderTable.Columns.Add("Name"); orderTable.Columns.Add("Type"); orderTable.Columns.Add("Price"); if (hasRentItems) { orderTable.Columns.Add("Start date"); orderTable.Columns.Add("End date"); } foreach (OrderLine item in orderLines) { DataRow orderRow = orderTable.NewRow(); orderRow[0] = item.orderline_id; orderRow[1] = item.dvdInfo.name; orderRow[2] = item.orderLineType.name; if (item.orderLineType.id == 1) { double cost = item.dvdInfo.rent_price * (item.enddate - item.startdate).Days; orderRow[3] = currency + " " + Math.Round(cost, 2); orderRow[4] = item.startdate.ToString("dd/MM/yyyy"); orderRow[5] = item.enddate.ToString("dd/MM/yyyy"); } else { double cost = item.dvdInfo.buy_price; orderRow[3] = currency + " " + Math.Round(cost, 2); } orderTable.Rows.Add(orderRow); } //set gridview gvOrderDetails.DataSource = orderTable; gvOrderDetails.DataBind(); } else { //this order does not belong to the logged in user, access denied. lblStatus.Text = "Access denied"; } } catch (NoRecordException) { } } else { //not logged in, access denied lblStatus.Text = "Please log in to access this page"; } } }
private void setStatistics() { Customer user = (Customer)Session["user"]; if (user != null) { OrderModel orderModel = new OrderModel(); RentModel rentModel = new RentModel(); lblOrderTotal.Text = "You have purchased " + orderModel.getNumberOfOrderLinesForCustomer(user) + " items (" + orderModel.getItemsBoughtByCustomer(user).Count + " unique) so far."; lblActiveRentCopies.Text = "You are currently renting " + rentModel.getNumberOfActiveRentOrdersCopiesForCustomer(user) + " items:"; if (rentModel.getNumberOfActiveRentOrdersCopiesForCustomer(user) > 0) { List <OrderLine> orderLines = new OrderLineService().getActiveRentOrderLinesByCustomer(user); String currency = "€"; if (Request.QueryString["currency"] == null) { if (CookieUtil.CookieExists("currency")) { if (CookieUtil.GetCookieValue("currency").Equals("usd")) { currency = "$"; } } } else if (Request.QueryString["currency"].Equals("usd")) { currency = "$"; } Boolean hasRentItems = false; foreach (OrderLine item in orderLines) { if (item.orderLineType.id == 1) { hasRentItems = true; } } DataTable orderTable = new DataTable(); orderTable.Columns.Add("Item number"); orderTable.Columns.Add("Name"); orderTable.Columns.Add("Type"); orderTable.Columns.Add("Price"); if (hasRentItems) { orderTable.Columns.Add("Start date"); orderTable.Columns.Add("End date"); } foreach (OrderLine item in orderLines) { DataRow orderRow = orderTable.NewRow(); orderRow[0] = item.orderline_id; orderRow[1] = item.dvdInfo.name; orderRow[2] = item.orderLineType.name; orderRow[3] = currency + " " + setPriceInRightCurrency(item.dvdInfo.buy_price, currency); orderRow[4] = item.startdate.ToString("dd/MM/yyyy"); orderRow[5] = item.enddate.ToString("dd/MM/yyyy"); orderTable.Rows.Add(orderRow); } gvActiveRent.DataSource = orderTable; gvActiveRent.DataBind(); } } }