public IResult Create(float res)
        {
            var result = new NumberResult();

            result.CountResult = res;
            return(result);
        }
 private async Task AgeValidator(ITurnContext context, NumberResult <int> result)
 {
     if (0 > result.Value || result.Value > 122)
     {
         result.Status = PromptStatus.NotRecognized;
         await context.SendActivity("Your age should be between 0 and 122.");
     }
 }
Example #3
0
 private Task ValidateAge(ITurnContext context, NumberResult <int> result)
 {
     if (result.Value < 13)
     {
         result.Status = PromptStatus.TooSmall;
     }
     else if (result.Value > 90)
     {
         result.Status = PromptStatus.TooBig;
     }
     return(Task.CompletedTask);
 }
Example #4
0
        public void ShouldReturnString1ForNumber1()
        {
            //arrange
            IFizzBuzz       fizzBuzz       = new FizzBuzz();
            IFizzBuzzNumber inputNumber    = new FizzBuzzNumber(1);
            IFizzBuzzNumber expectedNumber = new FizzBuzzNumber(1);
            IFizzBuzzResult expectedResult = new NumberResult(expectedNumber);
            //act
            IFizzBuzzResult actualResultNumber = fizzBuzz.Calculate(inputNumber);

            //assert
            actualResultNumber.Equals(expectedResult).Should().BeTrue();
        }
        public static Task MoneyValidator(ITurnContext context, NumberResult <int> toValidate)
        {
            if (toValidate.Value < 0)
            {
                toValidate.Status = PromptStatus.TooSmall;
            }
            else
            {
                toValidate.Status = PromptStatus.Recognized;
            }

            return(Task.CompletedTask);
        }
Example #6
0
        public override SAPResponse ExecuteQuery()
        {
            var isFromDB = InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_FROM_DB) is bool && (bool)InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_FROM_DB);

            if (isFromDB)
            {
                System.Diagnostics.Debug.WriteLine("<QUERY_TOTAL_RECORDS FROM = 'DB'>");
                var loadedHead = InMemoryCache.Instance.GetCached(Username + Suffix.REQUEST_HEADER) as RunInvoiceHeaderDTO;
                if (loadedHead != null)
                {
                    System.Diagnostics.Debug.WriteLine("MASUK....");
                    using (var dao = new BillingDbContext())
                    {
                        var sql1 =
                            from o in dao.InvoiceProformaHeaders.Where(o => o.BillingNo == loadedHead.BillingNo &&
                                                                       o.BillingBlock == loadedHead.BillingDocsCriteria &&
                                                                       o.ProformaFlag == loadedHead.ProformaFlag &&
                                                                       o.ReasonForRejection ==
                                                                       loadedHead.ReasonForRejection &&
                                                                       o.Version == 0).Include(o => o.Billings)
                            select o;
                        var zeroHeader = sql1.AsParallel().FirstOrDefault();
                        if (zeroHeader != null)
                        {
                            var total = zeroHeader.Billings.Count;
                            var resp  = new NumberResult {
                                Value = total
                            };
                            return(resp);
                        }
                        throw new FaultException("Header not found in Database!");
                    }
                }
                throw new FaultException("Header not found in Session!");
            }
            System.Diagnostics.Debug.WriteLine("<QUERY_TOTAL_RECORDS FROM = 'SAP'>");

            InMemoryCache.Instance.ClearCached(Username + Suffix.QUERIED_SAP_SESSIONID);
            var cred = ParseCredential(Username);
            var dest = SAPConnectionFactory.Instance.GetRfcDestination(cred);

            if (dest != null)
            {
                var repo = dest.Repository;

                if (_billingBlock == false && _reasonForRejection == false)
                {
                    _proformaFlag = true;
                }

                System.Diagnostics.Debug.WriteLine("<REQUEST_FOR_SESSION_ID>");
                var func1 = repo.CreateFunction("ZBAPI_PRINT_BILLING");
                func1.SetValue("BILL_NO", _billingNo);
                func1.SetValue("PROFORMA_FLAG", _proformaFlag ? "X" : " ");
                func1.SetValue("ITEM_FLAG", "X");
                func1.SetValue("BILLING_BLOCK", _billingBlock ? "X" : " ");
                func1.SetValue("REASON_FOR_REJECTION", _reasonForRejection ? "X" : " ");
                func1.SetValue("PAGING", "X");
                func1.SetValue("MAXROWS", 1);
                func1.Invoke(dest);

                var sessionId = func1.GetInt("SESSIONID");
                System.Diagnostics.Debug.WriteLine("<SESSION ID = '" + sessionId + "'/>");

                InMemoryCache.Instance.Cache(Username + Suffix.QUERIED_SAP_SESSIONID, sessionId);

                dest = SAPConnectionFactory.Instance.GetRfcDestination(cred);
                repo = dest.Repository;

                var func0 = repo.CreateFunction("ZBAPI_PROFORMA_CHECK");
                func0.SetValue("SESSIONID", sessionId);

                func0.Invoke(dest);
                var totalBills = func0.GetInt("ROWS");

                System.Diagnostics.Debug.WriteLine("<ROWS_QUERY TOTAL = '" + totalBills + "'/>");

                InMemoryCache.Instance.ClearCached(Username + Suffix.QUERIED_PROFORMA_TOTAL_RECS);
                InMemoryCache.Instance.Cache(Username + Suffix.QUERIED_PROFORMA_TOTAL_RECS, totalBills);

                System.Diagnostics.Debug.WriteLine("</QUERY_TOTAL_RECORDS>");

                var resp = new NumberResult {
                    Value = totalBills
                };
                return(resp);
            }

            System.Diagnostics.Debug.WriteLine("</QUERY_TOTAL_RECORDS>");
            throw new FaultException("RfcDestination is null!");
        }
Example #7
0
        public void BuildNumberResults(DateTime?startDate, DateTime?endDate, DateTime drawDate, bool appendOnly, bool resetCountersOnRealign)
        {
            if (!appendOnly)
            {
                _numberResults = new List <NumberResult>();
                _pbResults     = new List <NumberResult>();
            }

            DateTime drawingStartDate = startDate ?? DateTime.MinValue;
            DateTime drawingEndDate   = endDate ?? DateTime.MaxValue;
            bool     resetWb          = false;
            bool     resetPb          = false;

            if (resetCountersOnRealign)
            {
                if (drawDate == DateTime.Parse("11/5/1997"))
                {
                    //Reset Powerball
                    _pbResults = new List <NumberResult>();
                    resetPb    = true;
                }
                else if (drawDate == DateTime.Parse("10/9/2002"))
                {
                    //Reset WhiteBalls
                    _numberResults = new List <NumberResult>();
                    resetWb        = true;
                }
                else if (drawDate == DateTime.Parse("8/28/2005"))
                {
                    //Reset WhiteBalls
                    _numberResults = new List <NumberResult>();
                    resetWb        = true;
                }
                else if (drawDate == DateTime.Parse("1/07/2009"))
                {
                    //Reset WhiteBalls
                    //Reset Powerball
                    _numberResults = new List <NumberResult>();
                    _pbResults     = new List <NumberResult>();
                    resetPb        = true;
                    resetWb        = true;
                }
                else if (drawDate == DateTime.Parse("1/15/2012"))
                {
                    //Reset Powerball
                    _pbResults = new List <NumberResult>();
                    resetPb    = true;
                }
                else if (drawDate == DateTime.Parse("10/07/2015"))
                {
                    //Reset WhiteBalls
                    //Reset Powerball
                    _numberResults = new List <NumberResult>();
                    _pbResults     = new List <NumberResult>();
                    resetPb        = true;
                    resetWb        = true;
                }
            }
            //if (appendOnly)
            //{
            //    drawingStartDate = drawingEndDate;
            //}

            if (!appendOnly || resetWb)
            {
                for (short numberCounter = 0; numberCounter < 69; numberCounter++)
                {
                    NumberResult numberResult = new NumberResult();
                    numberResult.Number    = numberCounter + 1;
                    numberResult.Frequency = 0;
                    _numberResults.Add(numberResult);
                }
            }
            if (!appendOnly || resetPb)
            {
                for (short numberCounter = 0; numberCounter < 45; numberCounter++)
                {
                    NumberResult pbResult = new NumberResult();
                    pbResult.Number    = numberCounter + 1;
                    pbResult.Frequency = 0;
                    _pbResults.Add(pbResult);
                }
            }

            TimeSpan ts;

            foreach (Drawing drawing in _drawings.Where(d => d.DrawDate <= drawingEndDate && d.DrawDate >= drawingStartDate))
            {
                ts = drawingEndDate - _drawings[0].DrawDate;
                for (int wbCounter = 0; wbCounter < 5; wbCounter++)
                {
                    int wbIndex = drawing.WhiteBall[wbCounter] - 1;
                    _numberResults[wbIndex].Frequency += 1;
                    //if (!appendOnly)
                    //{
                    //    _numberResults[wbIndex].Points = _numberResults[wbIndex].Frequency + CalculateWbPoints(_numberResults[wbIndex].Number, drawing.DrawDate);
                    //}
                }



                _pbResults[drawing.PowerBall - 1].Frequency         += 1;
                _pbResults[drawing.PowerBall - 1].DrawingsAvailable += 1;
                //if (!appendOnly)
                //{
                //    _pbResults[drawing.PowerBall - 1].Points = _pbResults[drawing.PowerBall - 1].Frequency + CalculateWbPoints(_pbResults[drawing.PowerBall - 1].Number, drawing.DrawDate);
                //}

                #region Adjustments

                double adj = 0;

                //Increment number of drawings per number
                for (int n = 0; n < 69; n++)
                {
                    //Recalculate points

                    if (appendOnly)
                    {
                        _numberResults[n].Points = _numberResults[n].ProbabilityAverage +
                                                   CalculateWbPoints(_numberResults[n].Number, drawingEndDate);
                        if (n < 45)
                        {
                            _pbResults[n].Points = _pbResults[n].ProbabilityAverage +
                                                   CalculatePbPoints(_pbResults[n].Number, drawingEndDate);
                        }
                    }

                    //White ball
                    if (_numberResults[n].Number <= 49)
                    {
                        _numberResults[n].DrawingsAvailable += 1;
                    }

                    else if (_numberResults[n].Number <= 53 && drawing.DrawDate >= DateTime.Parse("10/9/2002"))
                    {
                        _numberResults[n].DrawingsAvailable += 1;
                    }
                    else if (_numberResults[n].Number <= 55 && drawing.DrawDate >= DateTime.Parse("8/28/2005"))
                    {
                        _numberResults[n].DrawingsAvailable += 1;
                    }
                    else if (_numberResults[n].Number <= 59 && drawing.DrawDate >= DateTime.Parse("1/07/2009"))
                    {
                        _numberResults[n].DrawingsAvailable += 1;
                    }
                    else if (_numberResults[n].Number <= 69 && drawing.DrawDate >= DateTime.Parse("10/07/2015"))
                    {
                        _numberResults[n].DrawingsAvailable += 1;
                    }

                    int maxWb = GetMaxWhiteBall(drawing.DrawDate);
                    if (maxWb >= _numberResults[n].Number)
                    {
                        _numberResults[n].Probability += (double)5 / maxWb;
                    }

                    //Powerball
                    if (n < 45)
                    {
                        if (_pbResults[n].Number <= 26)
                        {
                            _pbResults[n].DrawingsAvailable++;
                        }
                        else if (_pbResults[n].Number <= 45 && drawing.DrawDate <= DateTime.Parse("11/5/1997"))
                        {
                            _pbResults[n].DrawingsAvailable++;
                        }
                        else if (_pbResults[n].Number <= 42 && drawing.DrawDate < DateTime.Parse("1/1/2009"))
                        {
                            _pbResults[n].DrawingsAvailable++;
                        }
                        else if (_pbResults[n].Number <= 39 && drawing.DrawDate < DateTime.Parse("1/15/2012"))
                        {
                            _pbResults[n].DrawingsAvailable++;
                        }
                        else if (_pbResults[n].Number <= 35 && drawing.DrawDate < DateTime.Parse("10/7/2015"))
                        {
                            _pbResults[n].DrawingsAvailable++;
                        }

                        int maxPb = GetMaxPowerBall(drawing.DrawDate);
                        _pbResults[n].Probability += (double)1 / maxPb;
                    }
                }

                #endregion
            }

            if (!appendOnly)
            {
                for (int n = 0; n < 69; n++)
                {
                    //Recalculate points
                    _numberResults[n].Points = _numberResults[n].ProbabilityAverage +
                                               CalculateWbPoints(_numberResults[n].Number, drawingEndDate);
                    if (n < 45)
                    {
                        _pbResults[n].Points = _pbResults[n].ProbabilityAverage +
                                               CalculatePbPoints(_pbResults[n].Number, drawingEndDate);
                    }
                }
            }
        }
Example #8
0
        public void ToStringTest()
        {
            var result = new NumberResult(10.2);

            Assert.Equal("10.2", result.ToString());
        }
Example #9
0
        public void IResultTest()
        {
            var result = new NumberResult(10.2) as IResult;

            Assert.Equal(10.2, result.Result);
        }
        public override SAPResponse ExecuteCommand()
        {
            var resp          = new NumberResult();
            var cachedVersion = (long)InMemoryCache.Instance.GetCached(Username + Suffix.CACHED_VERSION);
            var toUpdateList  = InMemoryCache.Instance.GetCached(Username + Suffix.BILLINGS_TO_UPDATE) as List <BillingToUpdate>;

            #region "CREATING NEW OR UPDATES HEADER"
            if (toUpdateList != null && toUpdateList.Count > 0)
            {
                System.Diagnostics.Debug.WriteLine("=========> CREATE OR UPDATES THE OLD OR NEW VERSION ON DATA LOADED FROM SAP OR DB....");

                var rawHeader = InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_PROFORMA_HEADER) as InvoiceProformaHeaderDto;
                var header    = InMemoryCache.Instance.GetCached(Username + Suffix.REQUEST_HEADER) as RunInvoiceHeaderDTO;
                if (rawHeader != null && header != null)
                {
                    #region "CREATE OR UPDATES DATA LOADED FROM DB"
                    var isFromDB = InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_FROM_DB) is bool && (bool)InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_FROM_DB);
                    if (isFromDB)
                    {
                        #region "UPDATING 0 VERSION"
                        var vrFromDb = InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_VERSION_FROM_DB) is int?(int)InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_VERSION_FROM_DB) : 0;
                        if (vrFromDb == 0)
                        {
                            System.Diagnostics.Debug.WriteLine("IS FROM DB = " + true + " - VER LOADED = " + vrFromDb);
                            using (var dao = new BillingDbContext())
                            {
                                System.Diagnostics.Debug.WriteLine("LOADED VER FROM DB = " + vrFromDb);
                                var loadedVersion = vrFromDb;
                                // Query the version which loaded
                                var sql1 = from o in dao.InvoiceProformaHeaders.Where(o => o.BillingNo == header.BillingNo &&
                                                                                      o.ReasonForRejection ==
                                                                                      header.ReasonForRejection &&
                                                                                      o.BillingBlock ==
                                                                                      header.BillingDocsCriteria &&
                                                                                      o.Version == loadedVersion)
                                           select o;
                                var loadedHeaderVersion = sql1.FirstOrDefault();

                                // Query the latest version from DB
                                var sql2 = from o in dao.InvoiceProformaHeaders.Where(o => o.BillingNo == header.BillingNo &&
                                                                                      o.ReasonForRejection ==
                                                                                      header.ReasonForRejection &&
                                                                                      o.BillingBlock ==
                                                                                      header.BillingDocsCriteria &&
                                                                                      o.Version > 0).OrderBy(o => o.Version)
                                           select o;

                                var latestHeaderVersion = 0;
                                var count = sql2.Count();
                                if (count > 0)
                                {
                                    latestHeaderVersion = sql2.ToArray()[count - 1].Version;
                                }

                                var newHeaderVersion = new InvoiceProformaHeader
                                {
                                    BillingNo          = header.BillingNo,
                                    SoldToParty        = header.SoldToParty,
                                    StartDate          = header.BillingDateFrom,
                                    EndDate            = header.BillingDateTo,
                                    BillingBlock       = header.BillingDocsCriteria,
                                    ReasonForRejection = header.ReasonForRejection,
                                    ProformaFlag       = header.ProformaFlag,
                                    Created            = DateTime.Now,
                                    Version            = latestHeaderVersion + 1,
                                    Draft = true
                                };
                                ClassCopier.Instance.Copy(rawHeader, newHeaderVersion);
                                ClassCopier.Instance.Copy(loadedHeaderVersion, newHeaderVersion);
                                // Add newly updated billings to newly version
                                foreach (var b2u in toUpdateList)
                                {
                                    var u                = b2u;
                                    var VBELN            = u.No.Split('#')[0];
                                    var POSNR            = u.No.Split('#')[1];
                                    var getCachedBillSql = from o in dao.InvoiceProformaBillings.Where(o => o.VBAK_VBELN == VBELN && o.VBAK_POSNR == POSNR && o.CachedVersion == 0).OrderByDescending(o => o.No) select o;
                                    var sbil             = getCachedBillSql.FirstOrDefault();
                                    if (sbil == null)
                                    {
                                        continue;
                                    }
                                    // Check if content changed
                                    if (sbil.VBAK_ABGRU_T != b2u.Content)
                                    {
                                        var upbi = new InvoiceProformaBilling();
                                        SAPHandlerHelper.Instance.CopyBillingValues(sbil, upbi);
                                        upbi.VBAK_ABGRU_T = b2u.Content;
                                        upbi.Remarks      = b2u.Remarks;
                                        if (b2u.Content == Properties.Settings.Default.Unblock)
                                        {
                                            upbi.VBAK_ABGRU_T = String.Empty;
                                        }
                                        upbi.CachedVersion = cachedVersion;
                                        newHeaderVersion.Billings.Add(upbi);
                                    }
                                }
                                // Clear updated billings from cached
                                InMemoryCache.Instance.ClearCached(Username + Suffix.BILLINGS_TO_UPDATE);
                                // Persists new header version and all related billings to DB
                                dao.InvoiceProformaHeaders.Add(newHeaderVersion);
                                dao.SaveChanges();
                                resp.Value = int.Parse(newHeaderVersion.No.ToString(CultureInfo.InvariantCulture));
                            }
                        }
                        #endregion
                        #region "UPDATING ABOVE 0 VERSION"
                        else
                        {
                            using (var dao = new BillingDbContext())
                            {
                                System.Diagnostics.Debug.WriteLine("LOADED VER FROM DB = " + vrFromDb);
                                var loadedVersion = vrFromDb;
                                // Query the version which loaded
                                var sql1 = from o in dao.InvoiceProformaHeaders.Where(o => o.BillingNo == header.BillingNo &&
                                                                                      o.ReasonForRejection ==
                                                                                      header.ReasonForRejection &&
                                                                                      o.BillingBlock ==
                                                                                      header.BillingDocsCriteria &&
                                                                                      o.Version == loadedVersion)
                                           .Include(o => o.Billings)
                                           select o;
                                var loadedHeaderVersion = sql1.FirstOrDefault();

                                // Query the latest version from DB
                                var sql2 = from o in dao.InvoiceProformaHeaders.Where(o => o.BillingNo == header.BillingNo &&
                                                                                      o.ReasonForRejection ==
                                                                                      header.ReasonForRejection &&
                                                                                      o.BillingBlock ==
                                                                                      header.BillingDocsCriteria &&
                                                                                      o.Version > 0)
                                           select o;
                                var count = sql2.Count();
                                var latestHeaderVersion = sql2.ToArray()[count - 1];

                                System.Diagnostics.Debug.WriteLine("=========> CREATE NEW HEADER VERSION FOR UPDATED BILLINGS");

                                // Check if the cached version of old header version's billing similar to current
                                // cached version
                                System.Diagnostics.Debug.WriteLine("=========> CREATE NEW HEADER VERSION FOR UPDATED BILLINGS");
                                // If queried billing cache version != current cachedVersion, create new header
                                var newHeaderVersion = new InvoiceProformaHeader
                                {
                                    BillingNo          = header.BillingNo,
                                    SoldToParty        = header.SoldToParty,
                                    StartDate          = header.BillingDateFrom,
                                    EndDate            = header.BillingDateTo,
                                    BillingBlock       = header.BillingDocsCriteria,
                                    ReasonForRejection = header.ReasonForRejection,
                                    ProformaFlag       = header.ProformaFlag,
                                    Created            = DateTime.Now,
                                    Version            = latestHeaderVersion.Version + 1,
                                    Draft = true
                                };
                                ClassCopier.Instance.Copy(rawHeader, newHeaderVersion);
                                ClassCopier.Instance.Copy(loadedHeaderVersion, newHeaderVersion);

                                // Add newly updated billings to newly version
                                foreach (var b2u in toUpdateList)
                                {
                                    var u                = b2u;
                                    var VBELN            = u.No.Split('#')[0];
                                    var POSNR            = u.No.Split('#')[1];
                                    var getCachedBillSql = from o in dao.InvoiceProformaBillings.Where(o => o.VBAK_VBELN == VBELN && o.VBAK_POSNR == POSNR && o.CachedVersion == 0).OrderByDescending(o => o.No) select o;
                                    var sbil             = getCachedBillSql.FirstOrDefault();
                                    if (sbil == null)
                                    {
                                        continue;
                                    }
                                    // Check if content changed
                                    if (sbil.VBAK_ABGRU_T != b2u.Content)
                                    {
                                        var upbi = new InvoiceProformaBilling();
                                        SAPHandlerHelper.Instance.CopyBillingValues(sbil, upbi);
                                        upbi.VBAK_ABGRU_T = b2u.Content;
                                        upbi.Remarks      = b2u.Remarks;
                                        if (b2u.Content == Properties.Settings.Default.Unblock)
                                        {
                                            upbi.VBAK_ABGRU_T = String.Empty;
                                        }
                                        upbi.CachedVersion = cachedVersion;
                                        newHeaderVersion.Billings.Add(upbi);
                                    }
                                }

                                // Add updated billings from previous version to current version
                                foreach (var bi in loadedHeaderVersion.Billings)
                                {
                                    InvoiceProformaBilling bi1 = bi;
                                    var chk =
                                        from o in
                                        newHeaderVersion.Billings.Where(
                                            o =>
                                            o.VBAK_VBELN == bi1.VBAK_VBELN &&
                                            o.VBAK_POSNR == bi1.VBAK_POSNR)
                                        select o;
                                    if (chk.Count() == 0)
                                    {
                                        var nbi = new InvoiceProformaBilling();
                                        SAPHandlerHelper.Instance.CopyBillingValues(bi, nbi);
                                        nbi.CachedVersion = cachedVersion;
                                        newHeaderVersion.Billings.Add(nbi);
                                    }
                                }

                                // Clear updated billings from cached
                                InMemoryCache.Instance.ClearCached(Username + Suffix.BILLINGS_TO_UPDATE);
                                // Persists new header version and all related billings to DB
                                dao.InvoiceProformaHeaders.Add(newHeaderVersion);
                                dao.SaveChanges();
                                resp.Value = int.Parse(newHeaderVersion.No.ToString(CultureInfo.InvariantCulture));
                            }
                        }
                        #endregion
                        System.Diagnostics.Debug.WriteLine("=========> FROM DB");
                    }
                    #endregion
                    #region "CREATE OR UPDATES DATA LOADED FROM SAP"
                    else
                    {
                        using (var dao = new BillingDbContext())
                        {
                            System.Diagnostics.Debug.WriteLine("=========> FROM SAP");
                            // Query any version above 0
                            var sql1 = from o in dao.InvoiceProformaHeaders.Where(o => o.BillingNo == header.BillingNo &&
                                                                                  o.ReasonForRejection ==
                                                                                  header.ReasonForRejection &&
                                                                                  o.BillingBlock ==
                                                                                  header.BillingDocsCriteria &&
                                                                                  o.Version > 0)
                                       select o;

                            var count = sql1.Count();
                            if (count > 0)
                            {
                                // If there is an older version which is above 0 get latest header
                                System.Diagnostics.Debug.WriteLine("=========> OLD HEADER EXISTS");
                                var oldHeaderVersion = sql1.ToArray()[count - 1];
                                #region "WORKING ON HEADER THAT HAS UPDATED BILLINGS"
                                var sample = 0;
                                // Check if the cached version of old header version's billing similar to current
                                // cached version
                                #region "CREATE NEW HEADER VERSION"
                                if (sample == 0)
                                {
                                    System.Diagnostics.Debug.WriteLine("=========> CREATE NEW HEADER VERSION FOR UPDATED BILLINGS");
                                    // If queried billing cache version != current cachedVersion, create new header
                                    var newHeaderVersion = new InvoiceProformaHeader
                                    {
                                        BillingNo          = header.BillingNo,
                                        SoldToParty        = header.SoldToParty,
                                        StartDate          = header.BillingDateFrom,
                                        EndDate            = header.BillingDateTo,
                                        BillingBlock       = header.BillingDocsCriteria,
                                        ReasonForRejection = header.ReasonForRejection,
                                        ProformaFlag       = header.ProformaFlag,
                                        Created            = DateTime.Now,
                                        Version            = oldHeaderVersion.Version + 1,
                                        Draft = true
                                    };
                                    ClassCopier.Instance.Copy(rawHeader, newHeaderVersion);
                                    ClassCopier.Instance.Copy(oldHeaderVersion, newHeaderVersion);

                                    // Add newly updated billings to newly version
                                    foreach (var b2u in toUpdateList)
                                    {
                                        var u                = b2u;
                                        var VBELN            = u.No.Split('#')[0];
                                        var POSNR            = u.No.Split('#')[1];
                                        var getCachedBillSql = from o in dao.InvoiceProformaBillings.Where(o => o.VBAK_VBELN == VBELN && o.VBAK_POSNR == POSNR && o.CachedVersion == 0) select o;
                                        var sbil             = getCachedBillSql.FirstOrDefault();
                                        if (sbil == null)
                                        {
                                            continue;
                                        }
                                        // Check if content changed
                                        if (sbil.VBAK_ABGRU_T != b2u.Content)
                                        {
                                            var upbi = new InvoiceProformaBilling();
                                            SAPHandlerHelper.Instance.CopyBillingValues(sbil, upbi);
                                            upbi.VBAK_ABGRU_T = b2u.Content;
                                            upbi.Remarks      = b2u.Remarks;
                                            if (b2u.Content == Properties.Settings.Default.Unblock)
                                            {
                                                upbi.VBAK_ABGRU_T = String.Empty;
                                            }
                                            upbi.CachedVersion = cachedVersion;
                                            newHeaderVersion.Billings.Add(upbi);
                                        }
                                    }

                                    // Add updated billings from previous version to current version
                                    foreach (var bi in oldHeaderVersion.Billings)
                                    {
                                        InvoiceProformaBilling bi1 = bi;
                                        var chk =
                                            from o in
                                            newHeaderVersion.Billings.Where(
                                                o =>
                                                o.VBAK_VBELN == bi1.VBAK_VBELN &&
                                                o.VBAK_POSNR == bi1.VBAK_POSNR)
                                            select o;
                                        if (chk.ToList().Count == 0)
                                        {
                                            var nbi = new InvoiceProformaBilling();
                                            SAPHandlerHelper.Instance.CopyBillingValues(bi, nbi);
                                            nbi.CachedVersion = cachedVersion;
                                            newHeaderVersion.Billings.Add(nbi);
                                        }
                                    }

                                    // Clear update billings list from cache
                                    InMemoryCache.Instance.ClearCached(Username + Suffix.BILLINGS_TO_UPDATE);
                                    // Persists new header version and all related billings to DB
                                    dao.InvoiceProformaHeaders.Add(newHeaderVersion);
                                    dao.SaveChanges();
                                    resp.Value = int.Parse(newHeaderVersion.No.ToString(CultureInfo.InvariantCulture));
                                }
                                #endregion
                                #region "UPDATE HEADER DATA"
                                else
                                {
                                    // If old header sample billing cached version equals to current cached version
                                    // add newly updated billings to old header version as add items
                                    System.Diagnostics.Debug.WriteLine("=========> UPDATE OLD HEADER WITH UPDATED BILLINGS");
                                    foreach (var b2u in toUpdateList)
                                    {
                                        var u                = b2u;
                                        var VBELN            = u.No.Split('#')[0];
                                        var POSNR            = u.No.Split('#')[1];
                                        var getCachedBillSql = from o in dao.InvoiceProformaBillings.Where(o => o.VBAK_VBELN == VBELN && o.VBAK_POSNR == POSNR && o.CachedVersion == 0) select o;
                                        var sbil             = getCachedBillSql.FirstOrDefault();
                                        if (sbil == null)
                                        {
                                            continue;
                                        }
                                        // Check if content changed
                                        if (sbil.VBAK_ABGRU_T != b2u.Content)
                                        {
                                            var upbi = new InvoiceProformaBilling();
                                            SAPHandlerHelper.Instance.CopyBillingValues(sbil, upbi);
                                            dao.Entry(upbi).State = EntityState.Added;
                                            upbi.CachedVersion    = cachedVersion;
                                            upbi.VBAK_ABGRU_T     = b2u.Content;
                                            upbi.Remarks          = b2u.Remarks;
                                            if (b2u.Content == Properties.Settings.Default.Unblock)
                                            {
                                                upbi.VBAK_ABGRU_T = String.Empty;
                                            }
                                            oldHeaderVersion.Billings.Add(upbi);
                                        }
                                    }
                                    // Clear update billings list from cache
                                    InMemoryCache.Instance.ClearCached(Username + Suffix.BILLINGS_TO_UPDATE);
                                    // Persists old header version and all newly added updated billings to DB
                                    dao.Entry(oldHeaderVersion).State = EntityState.Modified;
                                    dao.SaveChanges();
                                    resp.Value = int.Parse(oldHeaderVersion.No.ToString(CultureInfo.InvariantCulture));
                                }
                                #endregion
                            }
                            #endregion
                            #region "WORKING ON DATA THAT HAS NO VERSION ABOVE 0 IN DB"
                            else
                            {
                                System.Diagnostics.Debug.WriteLine("=========> NO OLD HEADER ABOVE 0 EXISTS");
                                System.Diagnostics.Debug.WriteLine("=========> CREATE 1st HEADER VERSION WITH UPDATED BILLINGS");
                                // If there is no version above 0
                                var newHeaderVersion = new InvoiceProformaHeader
                                {
                                    BillingNo          = header.BillingNo,
                                    SoldToParty        = header.SoldToParty,
                                    StartDate          = header.BillingDateFrom,
                                    EndDate            = header.BillingDateTo,
                                    BillingBlock       = header.BillingDocsCriteria,
                                    ReasonForRejection = header.ReasonForRejection,
                                    ProformaFlag       = header.ProformaFlag,
                                    Created            = DateTime.Now,
                                    Version            = 1,
                                    Draft = true
                                };
                                ClassCopier.Instance.Copy(rawHeader, newHeaderVersion);
                                dao.InvoiceProformaHeaders.Add(newHeaderVersion);

                                dao.SaveChanges();
                                // Add newly updated billings to newly version
                                foreach (var b2u in toUpdateList)
                                {
                                    var u = b2u;
                                    var getCachedBillSql = from o in dao.CachedBillings.Where(o => o.NO == u.No) select o;
                                    var sbil             = getCachedBillSql.FirstOrDefault();
                                    if (sbil == null)
                                    {
                                        continue;
                                    }
                                    // Check if content changed
                                    if (sbil.VBAK_ABGRU_T != b2u.Content)
                                    {
                                        var upbi = SAPHandlerHelper.Instance.FromBillingDTO2Model(sbil);
                                        upbi.CachedVersion = cachedVersion;
                                        upbi.VBAK_ABGRU_T  = b2u.Content;
                                        upbi.Remarks       = b2u.Remarks;
                                        if (b2u.Content == Properties.Settings.Default.Unblock)
                                        {
                                            upbi.VBAK_ABGRU_T = String.Empty;
                                        }
                                        newHeaderVersion.Billings.Add(upbi);
                                    }
                                }
                                dao.SaveChanges();

                                var isSaved = InMemoryCache.Instance.GetCached(Username + Suffix.PERFORMED_INITIAL_SAVE) is bool && (bool)InMemoryCache.Instance.GetCached(Username + Suffix.PERFORMED_INITIAL_SAVE);
                                if (isSaved == false)
                                {
                                    System.Diagnostics.Debug.WriteLine("<CREATE_ZERO_VERSION CALL = 'FROM SAVE BILLINGS UPDATES' />");
                                    CreateOrOverwriteZeroVersion(false, dao);
                                    InMemoryCache.Instance.ClearCached(Username + Suffix.PERFORMED_INITIAL_SAVE);
                                }

                                // Clear update billings list from cache
                                InMemoryCache.Instance.ClearCached(Username + Suffix.BILLINGS_TO_UPDATE);
                                // Persists new header version and all related billings to DB
                                resp.Value = int.Parse(newHeaderVersion.No.ToString(CultureInfo.InvariantCulture));
                            }
                            #endregion
                        }
                    }

                    #endregion
                }

                InMemoryCache.Instance.ClearCached(Username + Suffix.BILLINGS_TO_UPDATE);
            }
            #endregion
            #region "CREATING OR UPDATING HEADER AS FINAL HEADER"
            else
            {
                System.Diagnostics.Debug.WriteLine("=========> CREATE FINAL VERSION....");
                using (var dao = new BillingDbContext())
                {
                    var rawHeader = InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_PROFORMA_HEADER) as InvoiceProformaHeaderDto;
                    var header    = InMemoryCache.Instance.GetCached(Username + Suffix.REQUEST_HEADER) as RunInvoiceHeaderDTO;
                    if (rawHeader != null && header != null)
                    {
                        var vrFromDb = InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_VERSION_FROM_DB) is int?(int)InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_VERSION_FROM_DB) : 0;

                        // Query any version above 0
                        var sql1 = from o in dao.InvoiceProformaHeaders.Where(o => o.BillingNo == header.BillingNo &&
                                                                              o.ReasonForRejection ==
                                                                              header.ReasonForRejection &&
                                                                              o.BillingBlock ==
                                                                              header.BillingDocsCriteria &&
                                                                              o.Version == vrFromDb &&
                                                                              o.Version > 0)
                                   .Include(o => o.Billings)
                                   select o;
                        var count = sql1.Count();
                        #region "UPDATES THE LATEST HEADER TO FINAL"
                        if (count > 0)
                        {
                            // If there is an older version which is above 0, get latest header
                            System.Diagnostics.Debug.WriteLine("=========> OLD HEADER EXISTS");
                            System.Diagnostics.Debug.WriteLine("=========> UPDATES OLD HEADER AS FINAL");
                            var oldHeaderVersion = sql1.ToArray()[count - 1];
                            if (oldHeaderVersion != null)
                            {
                                oldHeaderVersion.Draft            = false;
                                dao.Entry(oldHeaderVersion).State = EntityState.Modified;
                                dao.SaveChanges();
                                resp.Value = int.Parse(oldHeaderVersion.No.ToString(CultureInfo.InvariantCulture));
                            }
                        }
                        #endregion
                        #region "CREATE A NEW FIRST VERSION AS FINAL"
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("=========> NO OLD HEADER EXISTS");
                            System.Diagnostics.Debug.WriteLine("=========> CREATE 1st HEADER VERSION AS FINAL");
                            // If there is no version above 0
                            var sql2 = from o in dao.InvoiceProformaHeaders.Where(o => o.BillingNo == header.BillingNo &&
                                                                                  o.ReasonForRejection ==
                                                                                  header.ReasonForRejection &&
                                                                                  o.BillingBlock ==
                                                                                  header.BillingDocsCriteria
                                                                                  ).OrderByDescending(o => o.Version) select o;
                            var latestHeaderVersion = sql2.ToArray()[0].Version;

                            var finalHeaderVersion = new InvoiceProformaHeader
                            {
                                BillingNo          = header.BillingNo,
                                SoldToParty        = header.SoldToParty,
                                StartDate          = header.BillingDateFrom,
                                EndDate            = header.BillingDateTo,
                                BillingBlock       = header.BillingDocsCriteria,
                                ReasonForRejection = header.ReasonForRejection,
                                ProformaFlag       = header.ProformaFlag,
                                Created            = DateTime.Now,
                                Version            = latestHeaderVersion + 1,
                                Draft = false
                            };
                            ClassCopier.Instance.Copy(rawHeader, finalHeaderVersion);
                            InMemoryCache.Instance.ClearCached(Username + Suffix.BILLINGS_TO_UPDATE);
                            // Persists new header version and all related billings to DB
                            dao.InvoiceProformaHeaders.Add(finalHeaderVersion);
                            dao.SaveChanges();

                            InMemoryCache.Instance.Cache(Username + Suffix.QUERIED_VERSION_FROM_DB, latestHeaderVersion + 1);
                            resp.Value = int.Parse(finalHeaderVersion.No.ToString(CultureInfo.InvariantCulture));
                        }
                        #endregion
                    }
                }
            }
            #endregion
            return(resp);
        }