Ejemplo n.º 1
0
        //todo Change

        private void AddNewGradingFactorValues()
        {
            Nullable <float>    totalValue      = null;
            Nullable <Guid>     GradingResultId = new Guid(ViewState["vsGradingResultId"].ToString());
            GradingResultStatus GenralReqiurmentStatus;

            GenralReqiurmentStatus = GradingResultStatus.Undertermined;
            List <GradingResultDetailBLL> list = new List <GradingResultDetailBLL>();

            #region Checking GradingFactors
            foreach (GridViewRow row in this.gvGradingFactorsNew.Rows)
            {
                GradingResultDetailBLL objResult = new GradingResultDetailBLL();
                Guid   GradingFactorId;
                string possibleValue;
                string dataType;
                string value;
                string FailPoint = string.Empty;
                int    isMax     = -1;
                // get the Id and the value.
                Label lblId = new Label();

                try
                {
                    lblId           = row.FindControl("lblId") as Label;
                    GradingFactorId = new Guid(lblId.Text);
                }
                catch (InvalidCastException)
                {
                    this.lblMsg.Text = "An error has occured please try again.If the error persits contact the administrator.";
                    return;
                }

                //Get Possible Values
                try
                {
                    Label lblPossibleTypes = new Label();
                    lblPossibleTypes = (Label)row.FindControl("lblPossibleTypes");
                    possibleValue    = lblPossibleTypes.Text;
                }
                catch
                {
                    this.lblMsg.Text = "An error has occured please try again.If the error persits contact the administrator.";
                    return;
                }
                // get DataType
                try
                {
                    Label lblDataType = new Label();
                    lblDataType = (Label)row.FindControl("lblDataType");
                    dataType    = lblDataType.Text;
                }
                catch
                {
                    this.lblMsg.Text = "An error has occured please try again.If the error persits contact the administrator.";
                    return;
                }

                // get Value.
                try
                {
                    TextBox txtGradingFactorValue = new TextBox();
                    txtGradingFactorValue = (TextBox)row.FindControl("txtGradingFactorValue");
                    value = txtGradingFactorValue.Text;
                    if (value == "" || value == String.Empty)
                    {
                        row.FindControl("lblEmpty").Visible = true;
                    }
                }
                catch
                {
                    this.lblMsg.Text = "An error has occured please try again.If the error persits contact the administrator.";
                    return;
                }
                // if it is a Genral req.

                try
                {
                    Label lblFailPoint = new Label();
                    lblFailPoint = (Label)row.FindControl("lblFailPoint");
                    FailPoint    = lblFailPoint.Text;
                }
                catch
                {
                }

                try
                {
                    Label lblisMax = new Label();
                    lblisMax = (Label)row.FindControl("lblisMax");
                    if (string.IsNullOrEmpty(lblisMax.Text) != true)
                    {
                        try
                        {
                            isMax = GradingFactorBLL.ParseTextGradingResultStatus(lblisMax.Text);
                        }
                        catch
                        {
                            this.lblMsg.Text = "Invalid Grading Result Comparision.Please try again.";
                            return;
                        }
                    }
                }
                catch
                {
                }
                //IsInTotalValue
                bool IsInTotalValue = false;
                try
                {
                    Label lblisTotalValue = new Label();
                    lblisTotalValue = (Label)row.FindControl("lblisTotalValue");
                    if (string.IsNullOrEmpty(lblisTotalValue.Text) != true)
                    {
                        try
                        {
                            if (lblisTotalValue.Text == "True")
                            {
                                IsInTotalValue = true;
                            }
                        }
                        catch
                        {
                            IsInTotalValue = false;
                        }
                    }
                }
                catch
                {
                }
                if (IsInTotalValue == true)
                {
                    if (totalValue == null)
                    {
                        totalValue = 0;
                    }
                    totalValue += float.Parse(value);
                }



                //Check Data Type.
                if (DataValidationBLL.isDataValidForDataType(value, dataType) == false)
                {
                    Label lblmessage;
                    lblmessage = row.FindControl("lblEmpty") as Label;
                    if (lblmessage != null)
                    {
                        lblmessage.Text      = "ERROR in Data Type";
                        lblmessage.Visible   = true;
                        lblmessage.ForeColor = System.Drawing.Color.Red;
                    }
                    return;
                }
                // check in possible Values
                if (DataValidationBLL.isExists(value, possibleValue) == false)
                {
                    Label lblmessage;
                    lblmessage = row.FindControl("lblEmpty") as Label;
                    if (lblmessage != null)
                    {
                        lblmessage.Text      = "Data Should Be one of the following: " + possibleValue;
                        lblmessage.Visible   = true;
                        lblmessage.ForeColor = System.Drawing.Color.Red;
                    }
                    return;
                }

                objResult.GradingFactorId = GradingFactorId;
                objResult.RecivedValue    = value;
                objResult.Status          = GradingResultDetailStatus.Active;

                list.Add(objResult);
                Label lblmessage1;
                lblmessage1 = row.FindControl("lblEmpty") as Label;
                if (lblmessage1 != null)
                {
                    lblmessage1.Text    = "";
                    lblmessage1.Visible = false;
                }

                // Check if is a General requirment.
                GradingFactorBLL objGenralReqiurment = new GradingFactorBLL();
                if (GradingResultStatus.GeneralRequiementfail == GenralReqiurmentStatus)
                {
                    GenralReqiurmentStatus = GradingResultStatus.GeneralRequiementfail;
                }
                else
                {
                    GenralReqiurmentStatus = objGenralReqiurment.GetGradingResultStatus(value, (FailPointComparsion)(isMax), FailPoint, dataType);
                }
            }

            #endregion



            //Get Commodity Grade

            Guid CommodityGradeId = Guid.Empty;
            if (this.chkRecivedGrade.Checked == true)
            {
                try
                {
                    CommodityGradeId = new Guid(this.cboCommodityGrade.SelectedValue.ToString());
                }
                catch
                {
                    this.lblMsg.Text = "Please Select Commodity Grade.";
                    return;
                }
            }

            #region Checking Total Value
            //Check Grading is OK
            bool ValidGrade = false;
            CommodityGradeFactorValueBLL objGradeFactorValueBLL = new CommodityGradeFactorValueBLL();
            objGradeFactorValueBLL = objGradeFactorValueBLL.GetActiveValueByCommodoityGradeId(CommodityGradeId);
            GradingResultDetailBLL objGradingResultDetail = new GradingResultDetailBLL();
            if (totalValue != null)
            {
                string err = "";
                ValidGrade = objGradingResultDetail.PreInsertHasValidGradingResult((float)totalValue, CommodityGradeId, out err);
                if (ValidGrade == false)
                {
                    this.lblMsg.Text      = err + "-total Value=" + totalValue.ToString();;
                    this.lblMsg.ForeColor = System.Drawing.Color.Red;
                    return;
                }
            }

            #endregion



            GradingResultBLL obj = new GradingResultBLL();
            try
            {
                obj.GradingId = new Guid(ViewState["vsGradingResultId"].ToString());
            }
            catch
            {
                this.lblMsg.Text = "Please select Grading Code";
            }
            obj.CommodityGradeId = CommodityGradeId;
            obj.IsSupervisor     = this.isSupervisor.Checked;
            //obj.Status = (GradingResultStatus)Convert.ToInt32(this.cboStatus.SelectedValue);
            obj.Status = GenralReqiurmentStatus;
            obj.Remark = this.txtRemark.Text;
            try
            {
                obj.GradeRecivedTimeStamp = Convert.ToDateTime(this.txtDateRecived.Text + " " + this.txtTimeRecived.Text);
            }
            catch
            {
                this.lblMsg.Text = "Please check grade recieved date and try again";
                return;
            }

            obj.CreatedBy      = UserBLL.GetCurrentUser();
            obj.ProductionYear = int.Parse(this.cboProductionYear.SelectedValue.ToString());
            string strTrackinNo = String.Empty;
            try
            {
                if (ViewState["vsTrackingNo"] != null)
                {
                    // Update WF
                    strTrackinNo = ViewState["vsTrackingNo"].ToString();
                }
                bool isUpdated = false;
                if (strTrackinNo != String.Empty)
                {
                    isUpdated = obj.UpdateGradingResult(list, GradingResultId, strTrackinNo);
                    Response.Redirect("ListInbox.aspx");
                }
                else
                {
                    isUpdated = obj.UpdateGradingResult(list, GradingResultId);
                }
                if (isUpdated == true)
                {
                    Session["GRID"] = obj.GradingId;
                    LoadData();
                    this.lblMsg.Text = "Data updated Successfully";
                }
                else
                {
                    this.lblMsg.Text = "Data can not be added please check the form and try again.If the error persists contact the administrator.";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }