Exemplo n.º 1
0
        protected void OrderTable_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int       index   = Convert.ToInt32(e.CommandArgument); //get row index
            DataTable orders  = (DataTable)OrderTable.DataSource;   //get data
            int       orderId = int.Parse(orders.Rows[index]["ID"].ToString());
            BlOrder   order   = new BlOrder(BlOrder.GetOrderById(orderId));
            int       status  = order.GetOrderStatus();

            if (e.CommandName == "updateArrivalTime" && status == 3)
            {
                if (!(Session["user"] is Deliver))
                {
                    Response.Redirect("HomePage.aspx");
                    return;
                }
                // calculate the best way time
                Deliver deliver = (Deliver)Session["user"];
                //get the new orders of the deliver
                List <BlOrder> Orders = deliver.GetOrdersListByTime();
                //set the road lists
                List <BlShop>             shops = new List <BlShop>();
                List <BlCustomersAddress> customersAddresses = new List <BlCustomersAddress>();
                //full road lists
                foreach (BlOrder Order in Orders)
                {
                    shops.Add(BlShop.GetShopById(Order.ShopId));
                    Point customerAddress = Order.Location != null ? new Point(Order.Location) : new Point(BlUser.UserById(Order.CustomerId).Location);
                    customersAddresses.Add(new BlCustomersAddress(customerAddress, Order.NumOfFloor, ""));
                }
                //calculate minutes
                double   minutes     = deliver.GetDistanceToCustomerHome(shops, customersAddresses);
                DateTime exitTime    = DateTime.Now;
                DateTime arrivalTime = exitTime.AddMinutes(5 + minutes);                                                  //add 5 minute of insure
                bool     success     = order.UpdateArrivalTime(arrivalTime) && BlOrder.UpdateStatus(status + 1, orderId); //update arrive time and order status
                if (!success)
                {
                    Response.Redirect("ViewOrders.aspx"); //go back to  page when there is a error
                    MSG.Visible = true;
                    MSG.Text    = "fail to start order "; //massage error
                }
                else
                {
                    //Button myButton = null;
                    //GridViewRow row = OrderTable.Rows[index];
                    //myButton = (Button)row.Cells[row.Cells.Count - 1].Controls[0];
                    //myButton.Text = "Order started";
                    //myButton.CommandName = "started";
                    MSG.Visible = true;
                    MSG.Text    = "success to start order!!!"; //massage error
                    UpOrders((BlUser)Session["user"], "");     //update table
                    UpdateButtonsText(3);                      //3=delivery
                }
            }
            else if (e.CommandName == "updateReadyTime" && status == 1)
            {
                DateTime readyTime = DateTime.Now;
                //get the order object
                //get the shop object
                BlShop shop = BlShop.GetShopById(order.ShopId);
                //get the Id of the closest  delivery
                string matchDeliveryId = Deliver.GetMatchesDeliveryId(shop.Location);;
                if (matchDeliveryId == "")
                {
                    List <int> orderWait = null;
                    if (Application["updateReadyTime"] == null)
                    {
                        orderWait = new List <int> {
                            orderId
                        };
                    }
                    else
                    {
                        orderWait = (List <int>)Application["updateReadyTime"];
                        orderWait.Add(orderId);
                    }
                    Application["updateReadyTime"] = orderWait;
                    //update button
                    Button      myButton = null;
                    GridViewRow row      = OrderTable.Rows[index];
                    myButton             = (Button)(row.Cells[4].Controls[0]);
                    myButton.Text        = "Product Ready";
                    myButton.CommandName = "Ready";
                    UpOrders((BlUser)Session["user"], "");//update table
                    MSG.Text = " update ready time!";
                }
                else
                {
                    bool success = UpdateReadytime(order, readyTime, matchDeliveryId);
                    if (!success)
                    {
                        ErMSG.Text = "fail to update ready time";//massage error
                    }
                    else
                    {
                        //update button
                        Button      myButton = null;
                        GridViewRow row      = OrderTable.Rows[index];
                        myButton             = (Button)(row.Cells[4].Controls[0]);
                        myButton.Text        = "Product Ready";
                        myButton.CommandName = "Ready";
                        UpOrders((BlUser)Session["user"], "");//update table
                        BlUser userToSendMailTo         = BlUser.UserById(order.DeliveryId);
                        List <BLOrderDetailsDB> Details = BLOrderDetailsDB.DetailsListOfOrder(orderId);
                        string productsString           = BLOrderDetailsDB.GetProductString(Details);
                        bool   isEmailSent = Register.sendEmail(userToSendMailTo.Email, " Fly pack you have a new order to deliver",
                                                                $"Hi,{userToSendMailTo} please accept your order as soon as you can.Here a summery of the order's products <br/> {productsString} <br/> Have a nice day,The Fly Pack Team");
                        if (!isEmailSent)
                        {
                            //take care if email dont send
                        }
                        MSG.Text = " update ready time!";
                    }
                }
            }
            else if (e.CommandName == "finish")
            {
                if (status == 4 && BlOrder.UpdateStatus(status + 1, orderId))//update order status
                {
                    BlUser customer = BlUser.UserById(order.CustomerId);
                    NewOrOld.SelectedIndex = 1;//to see that the orders turn to old one
                    BlUser CurrentUser = (BlUser)Session["user"];
                    NewOrOld.SelectedIndex = 0;
                    UpOrders(CurrentUser, "");//update table
                    UpdateButtonsText(CurrentUser.Type);
                    if (Application["updateReadyTime"] != null)
                    {
                        List <int> orderWait   = (List <int>)Application["updateReadyTime"];
                        int        waitOrderId = orderWait[0];
                        BlOrder    waitOrder   = new BlOrder(BlOrder.GetOrderById(waitOrderId));
                        bool       success     = UpdateReadytime(waitOrder, DateTime.Now, CurrentUser.UserId);
                        if (success)
                        {
                            orderWait.Remove(0);
                            BlUser userToSendMailTo         = BlUser.UserById(order.DeliveryId);
                            List <BLOrderDetailsDB> Details = BLOrderDetailsDB.DetailsListOfOrder(orderId);
                            string productsString           = BLOrderDetailsDB.GetProductString(Details);
                            bool   isEmailSent1             = Register.sendEmail(userToSendMailTo.Email, " Fly pack you have a new order to deliver",
                                                                                 $"Hi,{userToSendMailTo} please accept your order as soon as you can.Here a summery of the order's products <br/> {productsString} <br/> Have a nice day,The Fly Pack Team");
                            if (!isEmailSent1)
                            {
                                //take care if email dont send
                            }
                        }
                    }
                    //send email to customer
                    bool isEmailSent = Register.sendEmail(customer.Email, " Fly pack your order arrived!!!",
                                                          $"Hi,{customer} the drone arrive to your home please take your order.Have a nice day,The Fly Pack Team");
                    if (!isEmailSent)
                    {
                        //take care if email don't send
                    }
                    MSG.Text = "success to update status";
                }
                else
                {
                    ErMSG.Text = "fail to update status";//massage error
                }
            }
            else if (e.CommandName == "ShowOrderDetails")
            {
                List <BLOrderDetail> orderDetails = BLOrderDetail.GetOrderDetailsByOrderId(orderId);
                Update(orderDetails);
                OrderDetailsPanel.Visible = true;
            }

            if (e.CommandName == "not ready to start")
            {
                MSG.Text = "wait to shop manager to confirm order ";//massage
            }
        }