private void _checkQtyRows()
        {
            foreach (GridViewRow row in GridViewRequest.Rows)
            {
                Label   rowNum = row.FindControl("NumLabel") as Label;
                TextBox tb     = row.FindControl("tbQuantity") as TextBox;

                string text = tb.Text;

                int rowNumInt = Convert.ToInt32(rowNum.Text);
                List <MakeNewRequestModel> rowModels = _getModelsFromSession();
                try
                {
                    MakeNewRequestModel model = rowModels[rowNumInt - 1];
                    int qty = 0;
                    if (int.TryParse(text, out qty) == false)
                    { // Couldn't convert
                        qty = model.Quantity;
                    }

                    model.Quantity = qty;

                    Session[SESSION_MODELS] = rowModels;

                    //_refreshGrid(rowModels);
                } catch (Exception)
                {
                }
            }
        }
        private MakeNewRequestModel _updateViewModelCategory(MakeNewRequestModel model, string itemCode)
        {
            List <Stock_Inventory> stocks = Session[SESSION_STOCKS] as List <Stock_Inventory>;

            model.DictDescriptions = stocks.Where(w => w.cat_id == model.CurrentCategory).ToDictionary(k => k.item_code, v => v.item_description);

            if (itemCode == null)
            {
                var stList = stocks.Where(w => w.cat_id == model.CurrentCategory);
                if (stList.Count() > 0)
                {
                    Stock_Inventory st = stocks.Where(w => w.cat_id == model.CurrentCategory).First();
                    model.CurrentItem = st.item_code;
                }
                else
                {
                    model.CurrentItem = null;
                }
            }
            else
            {
                model.CurrentItem = itemCode;
            }

            return(_updateViewModelItem(model));
        }
        protected void tbQuantity_TextChanged(object sender, EventArgs e)
        {
            TextBox     tb  = sender as TextBox;
            GridViewRow gvr = tb.Parent.Parent as GridViewRow;
            Label       num = gvr.FindControl("NumLabel") as Label;

            string text = tb.Text;

            int numInt = Convert.ToInt32(num.Text);
            List <MakeNewRequestModel> models = _getModelsFromSession();
            MakeNewRequestModel        model  = models[numInt - 1];

            int qty = 0;

            if (int.TryParse(text, out qty) == false)
            { // Couldn't convert
                qty = model.Quantity;
            }

            model.Quantity = qty;

            Session[SESSION_MODELS] = models;

            _refreshGrid(models);
        }
        protected void GridViewRequest_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label        num        = (e.Row.FindControl("NumLabel") as Label);
                int          numInt     = Convert.ToInt32(num.Text);
                DropDownList ddlCats    = (e.Row.FindControl("ddlCategories") as DropDownList);
                ListBox      lbDescs    = (e.Row.FindControl("lbDescriptions") as ListBox);
                ListBox      lbPrevApp  = (e.Row.FindControl("lbPrevApproved") as ListBox);
                TextBox      tbQuantity = (e.Row.FindControl("tbQuantity") as TextBox);

                List <MakeNewRequestModel> models = _getModelsFromSession();
                MakeNewRequestModel        model  = models[numInt - 1];

                ddlCats.DataSource     = model.DictCats;
                ddlCats.SelectedValue  = model.CurrentCategory.ToString();
                ddlCats.DataValueField = "Key";
                ddlCats.DataTextField  = "Value";
                ddlCats.DataBind();

                lbDescs.DataSource     = model.DictDescriptions;
                lbDescs.SelectedValue  = model.CurrentItem;
                lbDescs.DataValueField = "Key";
                lbDescs.DataTextField  = "Value";
                lbDescs.DataBind();

                lbPrevApp.DataSource = model.Approved;
                lbPrevApp.DataBind();

                tbQuantity.Text = model.Quantity.ToString();
            }
        }
        private MakeNewRequestModel _updateViewModelItem(MakeNewRequestModel model)
        {
            List <Stock_Inventory> stocks   = Session[SESSION_STOCKS] as List <Stock_Inventory>;
            RequestModelCollection requests = Session[SESSION_APPROVED_REQS] == null ? null : Session[SESSION_APPROVED_REQS] as RequestModelCollection;

            if (model.CurrentItem == null)
            {
                model.UnitOfMeasure = "";
                model.Approved      = new List <string>();

                return(model); // EARLY RETURN
            }

            model.UnitOfMeasure = stocks.Where(w => w.item_code == model.CurrentItem).First().unit_of_measure;

            if (requests == null)
            {
                model.Approved = new List <string>();
            }
            else
            {
                model.Approved = requests
                                 .SelectMany(s =>
                {
                    string date = s.Date.ToShortDateString();
                    IEnumerable <string> its = s.Items
                                               .Where(w => w.Key.ItemCode == model.CurrentItem)
                                               .Select(ss =>
                    {
                        string result = string
                                        .Format("({0}) {1} - {2} ({3})",
                                                ss.Key.Category.cat_name,
                                                ss.Key.Description,
                                                ss.Value,
                                                date
                                                );
                        //string res = "";
                        //res += ss.Key.Category.cat_name;
                        //res += " " + ss.Key.Description;
                        //res += " (" + date + ")";
                        return(result);
                    });

                    return(its);
                }
                                             ).ToList();
            }

            return(model);
        }
        protected void btnNewRow_Click(object sender, EventArgs e)
        {
            _checkQtyRows();

            List <MakeNewRequestModel> models   = _getModelsFromSession();
            MakeNewRequestModel        newModel = _makeNewModel(models.Count);

            models.Add(newModel);

            if (models.Count == 10)
            {
                btnNewRow.Enabled = false;
            }

            Session[SESSION_MODELS] = models;

            _refreshGrid(models);
        }
        protected void lbDescriptions_SelectedIndexChanged(object sender, EventArgs e)
        {
            _checkQtyRows();

            ListBox     lb  = sender as ListBox;
            GridViewRow gvr = lb.Parent.Parent as GridViewRow;
            Label       num = gvr.FindControl("NumLabel") as Label;

            string selected = lb.SelectedValue;

            int numInt = Convert.ToInt32(num.Text);
            List <MakeNewRequestModel> models = _getModelsFromSession();
            MakeNewRequestModel        model  = models[numInt - 1];

            model.CurrentItem = selected;

            model = _updateViewModelItem(model);

            Session[SESSION_MODELS] = models;

            _refreshGrid(models);
        }
        protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            _checkQtyRows();

            DropDownList ddl = sender as DropDownList;
            GridViewRow  gvr = ddl.Parent.Parent as GridViewRow;
            Label        num = gvr.FindControl("NumLabel") as Label;

            string selected = ddl.SelectedValue;

            int numInt = Convert.ToInt32(num.Text);
            List <MakeNewRequestModel> models = _getModelsFromSession();
            MakeNewRequestModel        model  = models[numInt - 1];

            model.CurrentCategory = Convert.ToInt32(selected);

            model = _updateViewModelCategory(model, null);

            Session[SESSION_MODELS] = models;

            _refreshGrid(models);
        }
        private MakeNewRequestModel _makeNewModel(int count, int catId, string itemCode, int qty)
        {
            List <Category>        cats     = Session[SESSION_CATEGORIES] as List <Category>;
            List <Stock_Inventory> stocks   = Session[SESSION_STOCKS] as List <Stock_Inventory>;
            RequestModelCollection requests = Session[SESSION_APPROVED_REQS] == null ? null : Session[SESSION_APPROVED_REQS] as RequestModelCollection;

            MakeNewRequestModel model = new MakeNewRequestModel();

            model.Num      = count + 1;
            model.Quantity = qty;
            model.DictCats = cats.ToDictionary(k => k.cat_id.ToString(), v => v.cat_name);

            if (catId == 0)
            {
                model.CurrentCategory = cats.First().cat_id;
            }
            else
            {
                model.CurrentCategory = catId;
            }

            return(_updateViewModelCategory(model, itemCode));
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { // New entry into page
                // Check that user is not a d head or dlg head or whatever
                UserModel userModel = new UserModel(User.Identity.Name);
                if (userModel.isDelegateHead() || userModel.isDeptHead() || userModel.isStoreManager())
                {
                    Response.Redirect("~/Views/notauthorized.aspx");
                }

                int requestId = 0;
                // Get any request string
                string requestToEdit = Request.QueryString["edit"];
                int.TryParse(requestToEdit, out requestId); // 0 if fails

                //if (requestId > 0)
                //{
                //    lblPageTitle.Text = "Update Request (Id " + requestId + ")";
                //    isEditing = true;
                //}

                Session[SESSION_IS_EDITING] = false;

                List <MakeNewRequestModel> models = new List <MakeNewRequestModel>();
                using (SSISEntities context = new SSISEntities())
                {
                    List <Category> cats = context.Categories.Where(w => w.deleted != "Y").ToList();
                    Session[SESSION_CATEGORIES] = cats;
                    List <Stock_Inventory> stocks = context.Stock_Inventory.Where(w => w.deleted != "Y").ToList();
                    Session[SESSION_STOCKS] = stocks;
                    RequestModelCollection requests;

                    /*
                     * if (!User.Identity.IsAuthenticated)
                     * {
                     *  Response.Redirect("~/login.aspx?return=Views/StoreClerk/MakeNewRequest.aspx");
                     * }*/

                    UserModel currentUser = new UserModel(User.Identity.Name);
                    //UserModel currentUser = new UserModel("Sally");
                    try
                    {
                        string deptCode = currentUser.Department.dept_code;
                        requests = FacadeFactory.getRequestService(context).getAllApprovedRequests()
                                   .fromDepartment(deptCode);
                    }
                    catch (ItemNotFoundException)
                    {
                        requests = null;
                    }

                    Session[SESSION_USER_MODEL]    = currentUser;
                    Session[SESSION_APPROVED_REQS] = requests;

                    if (requestId == 0)
                    { // Making a new request
                        MakeNewRequestModel model = _makeNewModel(0);
                        models.Add(model);

                        panelCannotChange.Visible = false;
                        panelNormalBtns.Visible   = true;
                        btnCancelRequest.Visible  = false;
                    }
                    else
                    {
                        Request found = context.Requests.Find(requestId);
                        // Set to cannot update first
                        panelCannotChange.Visible = true;
                        panelNormalBtns.Visible   = false;
                        string reason = "";
                        if (found == null)
                        {
                            reason = "That request could not be found.";
                        }
                        else if (found.username != currentUser.Username)
                        {
                            reason = "You did not make this request.";
                        }
                        else if (found.current_status != RequestStatus.PENDING && found.current_status != RequestStatus.UPDATED)
                        {
                            // status is neither pending nor updated
                            switch (found.current_status)
                            {
                            case RequestStatus.CANCELLED:
                                reason = "The request was cancelled.";
                                break;

                            case RequestStatus.REJECTED:
                                reason = "The request was already rejected.";
                                break;

                            default:     // Approved or others
                                reason = "The request was already approved.";
                                break;
                            }
                        }
                        else
                        {
                            // Request can be updated
                            panelCannotChange.Visible = false;
                            panelNormalBtns.Visible   = true;
                            btnCancelRequest.Visible  = true;

                            RequestModel rModel = FacadeFactory.getRequestService(context).findRequestById(requestId);

                            tbReason.Text = rModel.Reason;

                            int numIter = 0;
                            foreach (var item in rModel.Items)
                            {
                                if (item.Value == 0)
                                {
                                    continue;
                                }
                                MakeNewRequestModel model = _makeNewModel(numIter, item.Key.Category.cat_id, item.Key.ItemCode, item.Value);
                                models.Add(model);
                                numIter++;
                            }

                            lblPageTitle.Text = "Update Request (Id " + requestId + ")";

                            //isEditing = true;
                            Session[SESSION_IS_EDITING]  = true;
                            Session[SESSION_REQ_EDIT_ID] = requestId;
                            btnSubmit.Text = "Save Changes";
                        }

                        lblCannotChangeInfo.Text = reason;
                    }
                }

                Session[SESSION_MODELS] = models;
                _refreshGrid(models);
            } // end of isPostBack
        }