protected void GetPurchaseRequisitionsApprovalListByWarehouse(string warehouseId)
        {
            PurchaseRequisitionBLL purchaseRequisition = new PurchaseRequisitionBLL();

            try
            {
                DataTable dt = purchaseRequisition.GetPurchaseRequisitionsApprovalListByWarehouse(warehouseId);

                purchaseRequisitionApprovalListGridView.DataSource = dt;
                purchaseRequisitionApprovalListGridView.DataBind();

                if (purchaseRequisitionApprovalListGridView.Rows.Count > 0)
                {
                    purchaseRequisitionApprovalListGridView.UseAccessibleHeader = true;
                    purchaseRequisitionApprovalListGridView.HeaderRow.TableSection = TableRowSection.TableHeader;
                }
                else
                {
                    msgbox.Visible = true; msgTitleLabel.Text = "Data Not Found!!!"; msgDetailLabel.Text = "";
                }
            }
            catch (Exception ex)
            {
                msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = ex.Message;
            }
            finally
            {
                purchaseRequisition = null;
            }
        }
        protected void GetPurchaseRequisitionById(string purchaseRequisitionId)
        {
            PurchaseRequisitionBLL purchaseRequisition = new PurchaseRequisitionBLL();

            try
            {
                DataTable dt = purchaseRequisition.GetPurchaseRequisitionById(purchaseRequisitionId);

                if (dt.Rows.Count > 0)
                {
                    requisitionDateLabel.Text = dt.Rows[0]["RequisitionDate"].ToString();
                    warehouseIdLabel.Text = dt.Rows[0]["WarehouseId"].ToString();
                    warehouseNameLabel.Text = dt.Rows[0]["WarehouseName"].ToString();
                    narrationLabel.Text = dt.Rows[0]["Narration"].ToString();
                    statusLabel.Text = dt.Rows[0]["Status"].ToString();
                    lblCreatedBy.Text = dt.Rows[0]["CreatedBy"].ToString();
                    lblCreatedDate.Text = dt.Rows[0]["CreatedDate"].ToString();
                }
                else
                {
                    msgbox.Visible = true; msgTitleLabel.Text = "Data Not Found!!!"; msgDetailLabel.Text = "";
                }
            }
            catch (Exception ex)
            {
                msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = ex.Message;
            }
            finally
            {
                purchaseRequisition = null;
            }
        }
        protected void GetPurchaseRequisitionProductListById(string purchaseRequisitionId)
        {
            PurchaseRequisitionBLL purchaseRequisition = new PurchaseRequisitionBLL();

            try
            {
                DataTable dt = purchaseRequisition.GetPurchaseRequisitionProductListById(purchaseRequisitionId);

                if (dt.Rows.Count > 0)
                {
                    purchaseRequisitionProductListGridView.DataSource = dt;
                    purchaseRequisitionProductListGridView.DataBind();

                    if (purchaseRequisitionProductListGridView.Rows.Count > 0)
                    {
                        approveButton.Enabled = true;
                        purchaseRequisitionProductListGridView.UseAccessibleHeader = true;
                        purchaseRequisitionProductListGridView.HeaderRow.TableSection = TableRowSection.TableHeader;

                        TextBox approveQuantityTextBox;
                        TextBox unitPriceTextBox;
                        DropDownList vendorDropDownList;

                        VendorBLL vendor = new VendorBLL();
                        DataTable dtVendor=new DataTable();

                        for (int i = 0; i < purchaseRequisitionProductListGridView.Rows.Count; i++)
                        {
                            
                            approveQuantityTextBox = (TextBox)purchaseRequisitionProductListGridView.Rows[i].FindControl("approveQuantityTextBox");
                            //approveQuantityTextBox.Text = purchaseRequisitionProductListGridView.Rows[i].Cells[3].Text.ToString();
                            approveQuantityTextBox.Text = dt.Rows[i][3].ToString();
                            unitPriceTextBox = (TextBox)purchaseRequisitionProductListGridView.Rows[i].FindControl("unitPriceTextBox");
                            unitPriceTextBox.Text = dt.Rows[i]["UnitPrice"].ToString();
                            dtVendor = vendor.GetProductVendorsByProductId(purchaseRequisitionProductListGridView.Rows[i].Cells[0].Text.ToString());
                            vendorDropDownList = (DropDownList)purchaseRequisitionProductListGridView.Rows[i].FindControl("vendorDropDownList");
                            vendorDropDownList.DataSource = dtVendor;
                            vendorDropDownList.DataValueField = "VendorId";
                            vendorDropDownList.DataTextField = "VendorName";
                            vendorDropDownList.DataBind();
                            vendorDropDownList.Items.Insert(0, "");
                            vendorDropDownList.SelectedIndex = 0;
                            vendorDropDownList.Items.Insert(dtVendor.Rows.Count+1, "Select Others.");
                        }
                    }
                    else
                    {
                        approveButton.Enabled = false;
                    }
                }
                else
                {
                    msgbox.Visible = true; msgTitleLabel.Text = "Data Not Found!!!"; msgDetailLabel.Text = "";
                }
            }
            catch (Exception ex)
            {
                msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = ex.Message;
            }
            finally
            {
                purchaseRequisition = null;
            }
        }
        protected void approveButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (purchaseRequisitionProductListGridView.Rows.Count > 0)
                {
                    TextBox approveQuantityTextBox;
                    
                    decimal num; bool isApproveQuantityNum;
                    DropDownList vendorDropDownList;
                    DropDownList statusDropDownList;
                    TextBox narrationTextBox;
                    TextBox unitPriceTextBox;
                    DataTable dt = new DataTable();
                    DataRow dr = null;
                    dt.Columns.Add(new DataColumn("ProductId"));
                   
                    dt.Columns.Add(new DataColumn("ApprovedQuantity"));
                    dt.Columns.Add(new DataColumn("unitPriceTextBox"));
                    dt.Columns.Add(new DataColumn("VendorId"));
                    dt.Columns.Add(new DataColumn("Status"));
                    dt.Columns.Add(new DataColumn("Narration"));

                    for (int i = 0; i < purchaseRequisitionProductListGridView.Rows.Count; i++)
                    {
                        approveQuantityTextBox = (TextBox)purchaseRequisitionProductListGridView.Rows[i].FindControl("approveQuantityTextBox");
                        
                        isApproveQuantityNum = decimal.TryParse(approveQuantityTextBox.Text.Trim(), out num);
                        vendorDropDownList = (DropDownList)purchaseRequisitionProductListGridView.Rows[i].FindControl("vendorDropDownList");
                        statusDropDownList = (DropDownList)purchaseRequisitionProductListGridView.Rows[i].FindControl("statusDropDownList");
                        narrationTextBox = (TextBox)purchaseRequisitionProductListGridView.Rows[i].FindControl("narrationTextBox");
                        unitPriceTextBox = (TextBox)purchaseRequisitionProductListGridView.Rows[i].FindControl("unitPriceTextBox");

                        if (statusDropDownList.SelectedValue == "")
                        {
                            msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = "Product ID [" + purchaseRequisitionProductListGridView.Rows[i].Cells[0].Text.ToString() + "] has no status selected.";
                            return;
                        }
                        else if (statusDropDownList.SelectedValue == "A")
                        {
                            if (string.IsNullOrEmpty(approveQuantityTextBox.Text.Trim()) || !isApproveQuantityNum)
                            {
                                msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = "Product ID [" + purchaseRequisitionProductListGridView.Rows[i].Cells[0].Text.ToString() + "] has no valid approved quantity.";
                                return;
                            }
                            else if (vendorDropDownList.SelectedValue == "")
                            {
                                msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = "Product ID [" + purchaseRequisitionProductListGridView.Rows[i].Cells[0].Text.ToString() + "] has no vendor selected.";
                                return;
                            }
                        }

                        dr = dt.NewRow();
                        dr["ProductId"] = purchaseRequisitionProductListGridView.Rows[i].Cells[0].Text.ToString();
                        
                        dr["ApprovedQuantity"] = approveQuantityTextBox.Text.Trim();
                        dr["unitPriceTextBox"] = unitPriceTextBox.Text.Trim();
                        dr["VendorId"] = vendorDropDownList.SelectedValue.Trim();
                        dr["Status"] = statusDropDownList.SelectedValue.Trim();
                        dr["Narration"] = narrationTextBox.Text.Trim();
                        dt.Rows.Add(dr);
                    }

                    if (dt.Rows.Count == purchaseRequisitionProductListGridView.Rows.Count)
                    {
                        PurchaseRequisitionBLL purchaseRequisition = new PurchaseRequisitionBLL();
                        string msg = purchaseRequisition.ApprovePurchaseRequisitionAndCreatePurchaseOrder(dt, idLabel.Text.Trim(), warehouseIdLabel.Text.Trim());


                        string message = "<span class='actionTopic'></span> <span class='actionTopic'>" + msg + "</span>.";
                        MyAlertBox("var callbackOk = function () { MyOverlayStart(); window.location = \"/UI/PurchaseRequisition/ApprovalRequisitionList.aspx\"; }; SuccessAlert(\"" + "Process Succeed" + "\", \"" + message + "\", callbackOk);");

                       // MyAlertBox("alert(\"" + msg + " \"); window.location=\"/UI/PurchaseRequisition/ApprovalRequisitionList.aspx\"");
                    }
                    else
                    {
                        msgbox.Visible = true; msgTitleLabel.Text = "Collected Data Count Mismatch!!!"; msgDetailLabel.Text = "";
                    }
                }
            }
            catch (Exception ex)
            {
                msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = ex.Message;
            }
        }
        protected void saveButton_Click(object sender, EventArgs e)
        {
            string msg = string.Empty;

            List<PurchaseRequisitionBLL> purchaseRequisitions = new List<PurchaseRequisitionBLL>();
            PurchaseRequisitionBLL purchaseRequisition;
            decimal quantity = 0;
            TextBox requisitionQuantityTextBox;
            TextBox requiredDateTextBox;
            TextBox productNarrationTextBox;
            int i = 0;

            try
            {
                if (warehouseDropDownList.SelectedValue == "")
                {
                    msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = "Warehouse Name field is required.";
                }
                else
                {
                    for (i = 0; i < selectedProductListGridView.Rows.Count; i++)
                    {
                        requisitionQuantityTextBox = (TextBox)selectedProductListGridView.Rows[i].FindControl("requisitionQuantityTextBox");
                        requiredDateTextBox = (TextBox)selectedProductListGridView.Rows[i].FindControl("requiredDateTextBox");
                        productNarrationTextBox = (TextBox)selectedProductListGridView.Rows[i].FindControl("productNarrationTextBox");

                        if (!string.IsNullOrEmpty(requisitionQuantityTextBox.Text.Trim()) && decimal.TryParse(requisitionQuantityTextBox.Text.Trim(), out quantity))
                        {
                            purchaseRequisition = new PurchaseRequisitionBLL();

                            purchaseRequisition.ProductId = selectedProductListGridView.Rows[i].Cells[0].Text.ToString();
                            purchaseRequisition.RequisitionQuantity = quantity.ToString();
                            purchaseRequisition.RequiredDate = LumexLibraryManager.ParseAppDate(requiredDateTextBox.Text.ToString());
                            purchaseRequisition.ProductNarration = productNarrationTextBox.Text.ToString();

                            purchaseRequisitions.Add(purchaseRequisition);
                        }
                        else
                        {
                            msg = "Product ID [" + selectedProductListGridView.Rows[i].Cells[0].Text.ToString() + "] has no valid quantity.";
                            msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = msg;
                            return;
                        }
                    }

                    if (purchaseRequisitions.Count == selectedProductListGridView.Rows.Count)
                    {
                        purchaseRequisition = new PurchaseRequisitionBLL();
                        string purchaseRequisitionId = purchaseRequisition.SavePurchaseRequisition(purchaseRequisitions, warehouseDropDownList.SelectedValue.Trim(), narrationTextBox.Text.Trim());


                        string message = "Purchase Requisition <span class='actionTopic'>Created</span> Successfully with Purchase Requisition ID: <span class='actionTopic'>" + purchaseRequisitionId + "</span>.";
                        MyAlertBox("var callbackOk = function () { MyOverlayStart(); window.location = \"/UI/PurchaseRequisition/RequisitionList.aspx\"; }; SuccessAlert(\"" + "Process Succeed" + "\", \"" + message + "\", callbackOk);");


                    }
                    else
                    {

                    }
                }
            }
            catch (Exception ex)
            {
                msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = ex.Message;
            }
            finally
            {
                purchaseRequisition = null;
            }
        }
        protected void requisitionListButton_Click(object sender, EventArgs e)
        {
            PurchaseRequisitionBLL purchaseRequisition = new PurchaseRequisitionBLL();

            try
            {
                if (warehouseDropDownList.SelectedValue == "")
                {
                    msgbox.Visible = true; msgTitleLabel.Text = "Validation!!!"; msgDetailLabel.Text = "Warehouse Name field is required.";
                }
                else if (fromDateTextBox.Text.Trim() == "" || LumexLibraryManager.ParseAppDate(fromDateTextBox.Text.Trim()) == "False")
                {
                    msgbox.Visible = true; msgTitleLabel.Text = "Validation!!!"; msgDetailLabel.Text = "Date From field is required.";
                }
                else if (toDateTextBox.Text.Trim() == "" || LumexLibraryManager.ParseAppDate(toDateTextBox.Text.Trim()) == "False")
                {
                    msgbox.Visible = true; msgTitleLabel.Text = "Validation!!!"; msgDetailLabel.Text = "Date To field is required.";
                }
                else
                {
                    string warehouseId = warehouseDropDownList.SelectedValue.Trim();
                    string fromDate = LumexLibraryManager.ParseAppDate(fromDateTextBox.Text.Trim());
                    string toDate = LumexLibraryManager.ParseAppDate(toDateTextBox.Text.Trim());
                    string status = statusDropDownList.SelectedValue.Trim();

                    DataTable dt = purchaseRequisition.GetPurchaseRequisitionsListByWarehouseDateRangeAndStatus(warehouseId, fromDate, toDate, status);

                    purchaseRequisitionListGridView.DataSource = dt;
                    purchaseRequisitionListGridView.DataBind();

                    if (purchaseRequisitionListGridView.Rows.Count > 0)
                    {
                        purchaseRequisitionListGridView.UseAccessibleHeader = true;
                        purchaseRequisitionListGridView.HeaderRow.TableSection = TableRowSection.TableHeader;
                    }
                    else
                    {
                        msgbox.Visible = true; msgTitleLabel.Text = "Data Not Found!!!"; msgDetailLabel.Text = "";
                    }
                }
            }
            catch (Exception ex)
            {
                msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = ex.Message;
            }
            finally
            {
                purchaseRequisition = null;
            }
        }