public BOMMasterTag CheckIsManualBasedOnValue(string ActualStartDateTime, string ActualEndDateTime, BOMMasterTag objBOMMasterTag1)
        {
            DateTime      ActualStartDate = Convert.ToDateTime(ActualStartDateTime);
            DateTime      ActualEndDate   = Convert.ToDateTime(ActualEndDateTime);
            BOMMasterTag  objBOMMasterTag = new BOMMasterTag();
            AFValue       strtVal;
            AFValue       endVal;
            PIPoint       strtTag;
            string        exceptionMsg = "";
            List <string> lstString    = new List <string>();

            try
            {
                PIServerDetails   piServerDetails = new PIServerDetails();
                PISystems         myPISystems     = new PISystems();
                PISystem          mypiSystem      = myPISystems[piServerDetails.PIServerName];
                PIServer          myPiServer      = PIServer.FindPIServer(mypiSystem, piServerDetails.PIServerName);
                NetworkCredential Credentials     = new NetworkCredential(piServerDetails.UserName, piServerDetails.Password);

                ActualStartDate = ActualStartDate.AddMinutes(-330);
                AFTime sAFTime = new AFTime(ActualStartDate);
                ActualEndDate = ActualEndDate.AddMinutes(-330);
                AFTime eAFTime = new AFTime(ActualEndDate);
                mypiSystem.Connect(Credentials);
                IDictionary <AFSummaryTypes, AFValue> sm   = null;
                IDictionary <AFSummaryTypes, AFValue> smin = null;
                List <string> lstComponent = new List <string>();

                double resultValue = 0;
                strtTag = PIPoint.FindPIPoint(myPiServer, objBOMMasterTag1.PITag);
                strtVal = strtTag.RecordedValue(sAFTime, AFRetrievalMode.AtOrAfter);
                endVal  = strtTag.RecordedValue(eAFTime, AFRetrievalMode.AtOrAfter);

                AFTimeRange graphTimeReange = new AFTimeRange(sAFTime, eAFTime);

                sm   = strtTag.Summary(graphTimeReange, AFSummaryTypes.Maximum, 0, 0);
                smin = strtTag.Summary(graphTimeReange, AFSummaryTypes.Minimum, 0, 0);
                AFValue mx            = sm[AFSummaryTypes.Maximum];
                AFValue mn            = smin[AFSummaryTypes.Minimum];
                string  strtValString = strtVal.Value.ToString();
                string  endValString  = endVal.Value.ToString();

                if (endValString.Equals("No Data") || mx.IsGood == false || strtValString.Equals("No Data") || mx.IsGood == false)
                {
                    resultValue = 00000;
                    objBOMMasterTag1.isManual        = true;
                    objBOMMasterTag1.isManualWithTag = true;
                    lstString.Add(objBOMMasterTag1.component);
                }
                else
                {
                    if (Convert.ToDouble(endVal.Value) < Convert.ToDouble(mx.Value))
                    {
                        try
                        {
                            resultValue = Convert.ToDouble(mx.Value) - Convert.ToDouble(strtVal.Value) + Convert.ToDouble(endVal.Value) - Convert.ToDouble(mn.Value);
                        }
                        catch (Exception e)
                        {
                            objBOMMasterTag1.isManual        = true;
                            objBOMMasterTag1.isManualWithTag = true;
                            exceptionMsg = "Bad value from PI Tag";
                            lstString.Add(objBOMMasterTag1.component);
                        }
                    }
                    else if (Convert.ToDouble(endVal.Value) == Convert.ToDouble(strtVal.Value))
                    {
                        resultValue = 00000;
                        objBOMMasterTag1.isManual        = true;
                        objBOMMasterTag1.isManualWithTag = true;
                        lstString.Add(objBOMMasterTag1.component);
                    }
                    else
                    {
                        resultValue = Convert.ToDouble(endVal.Value) - Convert.ToDouble(strtVal.Value);
                    }
                    // objBOMMasterTag1.quantity = (float)resultValue;
                }

                return(objBOMMasterTag1);
            }
            catch (Exception e)
            {
                return(null);
            }
        }
        public JsonResult CalculateBill(string ActualStartDateTime, string ActualEndDateTime, string SubRefinery, string isSave_status, string PONO, int IDOCNumber)
        {
            var sessionObj = Session["SessionBO"] as UserModel;

            if (sessionObj != null)
            {
                BOMMasterTag objBOMMasterTag = new BOMMasterTag();
                AFLocaleIndependentFormatProvider myTimeZone = new AFLocaleIndependentFormatProvider();

                DateTime ActualStartDate = Convert.ToDateTime(ActualStartDateTime);
                DateTime ActualEndDate   = Convert.ToDateTime(ActualEndDateTime);

                AFValue strtVal;
                AFValue endVal;
                PIPoint strtTag;
                string  exceptionMsg = "";

                try
                {
                    List <BOMMasterTag> lstBOMMasterTag = new List <BOMMasterTag>();
                    if (isSave_status == "1")
                    {
                        lstBOMMasterTag = objBOMMasterTag.getIsSavedList(PONO, IDOCNumber);
                        List <string> lslComponent = GetIsManualWithTag(ActualStartDateTime, ActualEndDateTime, SubRefinery, isSave_status, PONO, IDOCNumber);
                        foreach (BOMMasterTag objBOMMasterTag1 in lstBOMMasterTag)
                        {
                            foreach (String component in lslComponent)
                            {
                                if (objBOMMasterTag1.component == component)
                                {
                                    objBOMMasterTag1.isManual        = true;
                                    objBOMMasterTag1.isManualWithTag = true;
                                }
                            }
                        }
                        //List<string> lstComponent1 = new List<string>();
                        //List<string> lstComponent2 = new List<string>();

                        //lstBOMMasterTag = objBOMMasterTag.getIsSavedList(PONO, IDOCNumber);
                        //List<string> lslComponent = GetIsManualWithTag(ActualStartDateTime, ActualEndDateTime, SubRefinery, isSave_status, PONO, IDOCNumber);

                        //foreach (BOMMasterTag objBOMMasterTag1 in lstBOMMasterTag)
                        //{

                        //    foreach (String component in lslComponent) {
                        //     if (objBOMMasterTag1.component == component)
                        //    {
                        //        objBOMMasterTag1.isManual = true;
                        //        objBOMMasterTag1.isManualWithTag = true;
                        //    }
                        //    }
                        //    lstComponent1.Add(objBOMMasterTag1.component);
                        //    lstComponent2.Add(objBOMMasterTag1.BOMCategory);
                        //}


                        //string duplicate = "";
                        //List<string> NonDuplicateList = new List<string>();
                        //foreach (string s in lstComponent2)
                        //{
                        //    if (!NonDuplicateList.Contains(s))
                        //        NonDuplicateList.Add(s);
                        //    else
                        //        duplicate = s;

                        //}

                        //foreach (BOMMasterTag objBOMMasterTag1 in lstBOMMasterTag)
                        //{
                        //    if (duplicate != "")
                        //    {
                        //        if (objBOMMasterTag1.BOMCategory == "Raw-Material" || objBOMMasterTag1.BOMCategory == "Product")
                        //        {
                        //            if (duplicate == objBOMMasterTag1.BOMCategory)
                        //            {
                        //               // lstBOMMasterTag.Remove(objBOMMasterTag1);
                        //                objBOMMasterTag1.BOMCategory = "Product";
                        //            }
                        //        }
                        //    }
                        //}
                    }
                    else
                    {
                        PIServerDetails   piServerDetails = new PIServerDetails();
                        PISystems         myPISystems     = new PISystems();
                        PISystem          mypiSystem      = myPISystems[piServerDetails.PIServerName];
                        PIServer          myPiServer      = PIServer.FindPIServer(mypiSystem, piServerDetails.PIServerName);
                        NetworkCredential Credentials     = new NetworkCredential(piServerDetails.UserName, piServerDetails.Password);

                        ActualStartDate = ActualStartDate.AddMinutes(-330);
                        AFTime sAFTime = new AFTime(ActualStartDate);
                        ActualEndDate = ActualEndDate.AddMinutes(-330);
                        AFTime eAFTime = new AFTime(ActualEndDate);
                        mypiSystem.Connect(Credentials);

                        //Tag Mapping with BOMMaster.
                        List <BOMMasterTag> lstBOMMasterTag1 = objBOMMasterTag.getMasterBOM(SubRefinery, PONO, IDOCNumber);

                        IDictionary <AFSummaryTypes, AFValue> sm   = null;
                        IDictionary <AFSummaryTypes, AFValue> smin = null;
                        List <string> lstComponent = new List <string>();

                        foreach (BOMMasterTag objBOMMasterTag1 in lstBOMMasterTag1)
                        {
                            lstComponent.Add(objBOMMasterTag1.component);
                        }

                        //string duplicate = "";
                        //List<string> NonDuplicateList = new List<string>();
                        //foreach (string s in lstComponent)
                        //{
                        //    if (!NonDuplicateList.Contains(s))
                        //        NonDuplicateList.Add(s);
                        //    else
                        //        duplicate = s;

                        //}
                        foreach (BOMMasterTag objBOMMasterTag1 in lstBOMMasterTag1)
                        {
                            if (objBOMMasterTag1.isManual || objBOMMasterTag1.PITag == "")
                            {
                                //objBOMMasterTag1.quantity = 00;
                                objBOMMasterTag1.isManual = true;

                                if (objBOMMasterTag1.component == "")
                                {
                                }
                            }
                            else
                            {
                                double resultValue = 0;
                                strtTag = PIPoint.FindPIPoint(myPiServer, objBOMMasterTag1.PITag);
                                strtVal = strtTag.RecordedValue(sAFTime, AFRetrievalMode.AtOrAfter);
                                endVal  = strtTag.RecordedValue(eAFTime, AFRetrievalMode.AtOrAfter);

                                AFTimeRange graphTimeReange = new AFTimeRange(sAFTime, eAFTime);

                                sm   = strtTag.Summary(graphTimeReange, AFSummaryTypes.Maximum, 0, 0);
                                smin = strtTag.Summary(graphTimeReange, AFSummaryTypes.Minimum, 0, 0);
                                AFValue mx           = sm[AFSummaryTypes.Maximum];
                                AFValue mn           = smin[AFSummaryTypes.Minimum];
                                Boolean isWrongValue = false;

                                if (endVal.Value.ToString().Equals("No Data") || mx.IsGood == false || strtVal.Value.ToString().Equals("No Data") || mx.IsGood == false)
                                {
                                    resultValue  = 0.0;
                                    isWrongValue = true;
                                }
                                else
                                {
                                    if (Convert.ToDouble(endVal.Value) < Convert.ToDouble(mx.Value))
                                    {
                                        resultValue = Convert.ToDouble(mx.Value) - Convert.ToDouble(strtVal.Value) + Convert.ToDouble(endVal.Value) - Convert.ToDouble(mn.Value);
                                    }
                                    else if (Convert.ToDouble(endVal.Value) == Convert.ToDouble(strtVal.Value))
                                    {
                                        resultValue  = 0;
                                        isWrongValue = true;
                                    }
                                    else
                                    {
                                        resultValue = Convert.ToDouble(endVal.Value) - Convert.ToDouble(strtVal.Value);
                                    }
                                }

                                //if (objBOMMasterTag1.component == "power")
                                //{
                                //    BOMDetails bomDetails = new BOMDetails();
                                //    List<MasterRefineryModel> lstMasterRefinery = bomDetails.getPlantWiseSubRefineries(SubRefinery);

                                //    double upTimeHours = 0;
                                //    double totalWeightIntoUptime = 0;
                                //    double weightIntoUptime = 0;
                                //    double weightIntoUptime1 = 0;
                                //    double effectiveWeight = 0;

                                //    foreach (MasterRefineryModel RefineryDetails in lstMasterRefinery)
                                //    {
                                //        Boolean flag = bomDetails.InsertForUpTime(ActualStartDate, ActualEndDate, RefineryDetails.SubRefineryCode, RefineryDetails.SubRefineryName);
                                //        if (flag)
                                //        {

                                //            strtTag = PIPoint.FindPIPoint(myPiServer, RefineryDetails.UpTimeTag);
                                //            strtVal = strtTag.RecordedValue(sAFTime, AFRetrievalMode.AtOrAfter);
                                //            endVal = strtTag.RecordedValue(eAFTime, AFRetrievalMode.AtOrAfter);

                                //            upTimeHours = Convert.ToDouble(endVal.Value);
                                //            weightIntoUptime = (upTimeHours * RefineryDetails.UpTimePercentage);
                                //            totalWeightIntoUptime = totalWeightIntoUptime + weightIntoUptime;
                                //            if (objBOMMasterTag1.SubRefinery == RefineryDetails.SubRefineryCode)
                                //            {
                                //                weightIntoUptime1 = weightIntoUptime;
                                //            }
                                //        }
                                //    }
                                //    effectiveWeight = weightIntoUptime1 / totalWeightIntoUptime;
                                //    resultValue = resultValue * effectiveWeight;
                                //}

                                if (resultValue <= 0 || isWrongValue)
                                {
                                    objBOMMasterTag1.isManual        = true;
                                    objBOMMasterTag1.isManualWithTag = true;
                                }
                                objBOMMasterTag1.quantity = (float)resultValue;
                            }
                            lstBOMMasterTag.Add(objBOMMasterTag1);
                            //if (duplicate != "")
                            //{
                            //    if (objBOMMasterTag1.BOMCategory == "Raw-Material" || objBOMMasterTag1.BOMCategory == "Product")
                            //    {
                            //        if (duplicate == objBOMMasterTag1.component)
                            //        {
                            //            lstBOMMasterTag.Remove(objBOMMasterTag1);
                            //        }
                            //    }
                            //}
                        }
                    }

                    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                    string     json = javaScriptSerializer.Serialize(lstBOMMasterTag);
                    JsonResult js   = Json(new { value = json }, JsonRequestBehavior.AllowGet);
                    return(js);
                }
                catch (Exception e)
                {
                    return(Json(new { value = exceptionMsg }, JsonRequestBehavior.AllowGet));
                }
            }
            else
            {
                return(Json(new { status = false }, JsonRequestBehavior.AllowGet));
            }
        }