Exemplo n.º 1
0
        //public static List<T> CastList<T>(List<object> list)
        //{
        //    List<T> retList=new List<T>();
        //    foreach (var obj in list)
        //    {
        //        if (obj is T Tvalue)
        //        {
        //            retList.Add(Tvalue);
        //        }
        //    }
        //    return retList;
        //}
        public void GetBestWayLists(bool isAllSameShop, List <BlShop> shops, List <BlCustomersAddress> customersAddresses, List <BlOrder> orders, Deliver deliver)
        {
            //set vars
            List <BlCustomersAddress> copyCustomersAddresses  = new List <BlCustomersAddress>(customersAddresses);
            List <BlOrder>            copyOrders              = new List <BlOrder>(orders);
            List <BlShop>             orderShops              = new List <BlShop>();
            List <BlCustomersAddress> orderCustomersAddresses = new List <BlCustomersAddress>();
            int           minIndex = 0;
            List <int>    IndexsOrderByDistance;
            List <double> readyTimes  = new List <double>();
            List <double> arriveTimes = new List <double>();
            int           lateTimes   = 0;
            List <BlShop> copyShops   = new List <BlShop>(shops);
            Point         locationNow = deliver.Location;

            for (int i = 0; i < shops.Count; i++)
            {
                //set a list of the index of the shop order by distance ( the first is the closest)
                IndexsOrderByDistance = isAllSameShop ? locationNow.MinimumDistanceCustomerList(copyShops, copyCustomersAddresses, 0) : locationNow.MinimumDistanceList(copyShops, copyCustomersAddresses, 0);

                // for orderShopIndex to select the shortest distance shop that the deliver deliver the pack on time
                foreach (var index in IndexsOrderByDistance)
                {
                    if (shops.Count == orderShops.Count)
                    {
                        break;
                    }
                    //to add shop and customer
                    int realIndex = index - lateTimes;
                    orderShops.Add(copyShops[realIndex]);
                    orderCustomersAddresses.Add(copyCustomersAddresses[realIndex]);
                    //orderShops[i] = copyShops[index];
                    //orderCustomersAddresses[i] = copyCustomersAddresses[index];
                    //calculate Times
                    DateTime arriveTimeCustomer = DateTime.Now.AddMinutes(locationNow.ArriveTimeCustomer(copyCustomersAddresses, copyShops, realIndex));
                    DateTime arriveTimeShop     = DateTime.Now.AddMinutes(locationNow.ArriveTimeShop(copyCustomersAddresses, copyShops, realIndex));
                    DateTime arriveTime         = copyOrders[realIndex].ArriveTime;
                    DateTime readyTime          = copyOrders[realIndex].ReadyTime;
                    if (!IsLate(arriveTimeCustomer, arriveTime, readyTime, arriveTimeShop, readyTimes, arriveTimes))
                    {
                        break;//out the loop (the shop and the match customer address already added
                    }
                    else
                    {
                        //remove because index not good
                        copyShops.RemoveAt(realIndex);
                        copyOrders.RemoveAt(realIndex);
                        copyCustomersAddresses.RemoveAt(realIndex);
                        //update late times
                        lateTimes++;
                    }
                }
                //update locationNow
                locationNow = new Point(orderCustomersAddresses[i].Location);
            }
            if (lateTimes == shops.Count)
            {
                //if all late the best way is the shortest way
                List <BlShop>             bestWayShops        = new List <BlShop>();
                List <BlCustomersAddress> bestWayCustomers    = new List <BlCustomersAddress>();
                List <BlShop>             cShops              = new List <BlShop>(shops);
                List <BlCustomersAddress> cCustomersAddresses = new List <BlCustomersAddress>(customersAddresses);
                for (int i = 0; i < shops.Count; i++)
                {
                    int bestIndex = 0;
                    if (i != shops.Count - 1)
                    {
                        IndexsOrderByDistance = locationNow.MinimumDistanceList(cShops, cCustomersAddresses, 0);
                        bestIndex             = GetBestShopIndex(readyTimes, arriveTimes, IndexsOrderByDistance);//get the best index by all the parameters
                    }
                    bestWayShops.Add(cShops[bestIndex]);
                    bestWayCustomers.Add(cCustomersAddresses[bestIndex]);
                    cShops.RemoveAt(bestIndex);
                    cCustomersAddresses.RemoveAt(bestIndex);
                }
                //update the Page vars
                Shops     = Json.Encode(bestWayShops);
                Customers = Json.Encode(bestWayCustomers);
                UpdateWayString(bestWayShops, bestWayCustomers);
                //update the global vars
                double minutes = deliver.GetDistanceToCustomerHome(shops, customersAddresses);
                GlobalVariable.Distance = minutes;
            }
            else
            {
                //update the Page vars
                Shops     = Json.Encode(orderShops);
                Customers = Json.Encode(orderCustomersAddresses);
                UpdateWayString(orderShops, orderCustomersAddresses);
                //update the global vars
                double minutes = deliver.GetDistanceToCustomerHome(shops, customersAddresses);
                GlobalVariable.Distance = minutes;
            }
        }
Exemplo n.º 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    OrderStartedSucces.InnerHtml = Request.QueryString.Get("text");
                }
                catch
                {
                    OrderStartedSucces.InnerHtml = "";
                }
            }
            //Session["user"] = new Deliver("shlakot1");
            //BlOrderUser user = (BlOrderUser)Session["user"]; /*(BLUser)Session["user"];*/
            //GlobalVariable.UnVerifyEmail.Add(user.UserId, "111111");
            BlOrderUser user = /*(BlOrderUser)Session["user"];*/ (BlOrderUser)Session["user"];

            //to check if user verify email!!!!!!!!!!!!!!!!!!!!!!!!!!
            //try
            //{
            //    Dictionary<string, string> VerifyEmail = ((Dictionary<string, string>) Application["UnVerifyEmail"]);
            //    if (VerifyEmail!=null)
            //    {
            //        string verifyCode = VerifyEmail[user.UserId];
            //        Response.Redirect("VerifyEmail.aspx");
            //        return;
            //    }
            //    else
            //    {
            //        errorMSG.Text = "VerifyEmail null";
            //    }

            //}
            //catch
            //{

            //}


            if (!(user is Deliver))
            {
                return;
            }
            //get the new orders of the deliver
            List <BlOrder> orders = user.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, user.GetName()));
            }

            if (orders.Count == 0)
            {
                errorMSG.Text = "there is no orders"; return;
            }
            else
            {
                errorMSG.Text = "";
            }
            if (orders.Count > 1)//to change to orders.Count>1!!!!!!!!!!!!!####$$$$$$$$########@@@@@@@*********#########&&&&&&&&&&$$$$$$$$$$^^^^^^^^^^%%%%%%%%%**********
            {
                //List<BlShop> sameShops = BlShop.isHaveSameShop(shops);
                //bool isHaveSameShop = sameShops.Count > 0;
                //if (isHaveSameShop)
                //{

                //}
                bool isAllSameShop = BlShop.isAllSameShop(shops);
                //calculate shorter way to deliver
                GetBestWayLists(isAllSameShop, shops, customersAddresses, orders, (Deliver)user);
            }
            else
            {
                //update the Page vars
                Shops     = Json.Encode(shops);
                Customers = Json.Encode(customersAddresses);
                UpdateWayString(shops, customersAddresses);
                //update the global vars
                Deliver deliver = (Deliver)user;
                double  minutes = deliver.GetDistanceToCustomerHome(shops, customersAddresses);
                GlobalVariable.Distance = minutes;
            }
        }
Exemplo n.º 3
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
            }
        }