public override SAPResponse ExecuteQuery() { var resp = new InvoiceProformaItemsDTO(); var isFromDB = InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_FROM_DB) is bool && (bool)InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_FROM_DB); #region "QUERY FROM DB" if (isFromDB) { System.Diagnostics.Debug.WriteLine("<QUERY_TOTAL_RECORDS FROM = 'DB'>"); var rawHead = InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_PROFORMA_HEADER) as InvoiceProformaHeaderDto; InMemoryCache.Instance.ClearCached(Username + Suffix.CACHED_VERSION); using (var dao = new BillingDbContext()) { var sql0 = from o in dao.InvoiceProformaHeaders.Where(o => o.No == rawHead.No) select o; var count = sql0.Count(); if (count > 0) { var cachedVersion = DateTime.Now.Ticks; InMemoryCache.Instance.Cache(Username + Suffix.CACHED_VERSION, cachedVersion); // Load all by selected version var sql2 = from o in dao.InvoiceProformaHeaders.Where(o => o.No == rawHead.No).Include(o => o.Billings) select o; var verHead = sql2.AsParallel().FirstOrDefault(); if (verHead != null && verHead.Version == 0) { var list1 = verHead.Billings; InMemoryCache.Instance.Cache(Username + Suffix.QUERIED_PROFORMA_TOTAL_RECS, list1.Count); var list2 = SAPHandlerHelper.Instance.FromListOfBillingModels2DTOs(list1, cachedVersion); resp.Items = list2.ToList(); } else if (verHead != null) { // Load all version 0 System.Diagnostics.Debug.WriteLine("<ROWS_QUERY VERSION = '0'/>"); var sql1 = from o in dao.InvoiceProformaHeaders.Where(o => o.BillingNo == verHead.BillingNo && o.BillingBlock == verHead.BillingBlock && o.ProformaFlag == verHead.ProformaFlag && o.ReasonForRejection == verHead.ReasonForRejection && o.Version == 0).Include(o => o.Billings) select o; var zeroHeader = sql1.FirstOrDefault(); if (zeroHeader == null) { return(null); } var list1 = zeroHeader.Billings; System.Diagnostics.Debug.WriteLine("<ROWS_QUERY COMBINING'/>"); var list2 = verHead.Billings; foreach (var upb in list2) { InvoiceProformaBilling upb1 = upb; var bil = from o in list1.Where(o => o.VBAK_VBELN == upb1.VBAK_VBELN && o.VBAK_POSNR == upb1.VBAK_POSNR) select o; var dest = bil.FirstOrDefault(); if (dest == null) { list1.Add(upb); } else { SAPHandlerHelper.Instance.CopyBillingValues(upb, dest); } } var list3 = SAPHandlerHelper.Instance.FromListOfBillingModels2DTOs(list1, cachedVersion); //var list4 = list3 as InvoiceProformaItemDTO[] ?? list3.ToArray(); InMemoryCache.Instance.Cache(Username + Suffix.QUERIED_PROFORMA_TOTAL_RECS, list3.Count()); resp.Items = list3; } else { throw new FaultException("Selected header is null!"); } } else { throw new FaultException("Selected header is null!"); } } } #endregion #region "QUERY FROM SAP" else { InMemoryCache.Instance.ClearCached(Username + Suffix.CACHED_VERSION); System.Diagnostics.Debug.WriteLine("<QUERY_TOTAL_RECORDS FROM = 'SAP'>"); var cred = ParseCredential(Username); var dest = SAPConnectionFactory.Instance.GetRfcDestination(cred); if (dest != null) { var cachedVersion = DateTime.Now.Ticks; InMemoryCache.Instance.Cache(Username + Suffix.CACHED_VERSION, cachedVersion); var totalBills = InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_PROFORMA_TOTAL_RECS) is int ?(int)InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_PROFORMA_TOTAL_RECS) : 0; var sessionId = WaitForSessionId(); var repo = dest.Repository; var func = repo.CreateFunction("ZBAPI_PROFORMA_PAGING"); func.SetValue("SESSIONID", sessionId); func.SetValue("STARTROW", 0); func.SetValue("ENDROW", totalBills); func.Invoke(dest); var proformaTbl = func.GetTable("PROFORMA"); var ret = proformaTbl.Select(e => new InvoiceProformaItemDTO { VBELN = e.GetString("VBELN"), POSNR = e.GetString("POSNR"), AUBEL = e.GetString("AUBEL"), AUPOS = e.GetString("AUPOS"), MATKL = e.GetString("MATKL"), ITEM_TYPE = e.GetString("ITEM_TYPE"), CATEGORY = e.GetString("CATEGORY"), SUBCATEGORY = e.GetString("SUBCATEGORY"), WGBEZ = e.GetString("WGBEZ"), MATNR = e.GetString("MATNR"), ARKTX = e.GetString("ARKTX"), FKIMG = e.GetString("FKIMG"), VRKME = e.GetString("VRKME"), TOTAL1 = e.GetString("TOTAL1"), TOTAL2 = e.GetString("TOTAL2"), TOTAL3 = e.GetString("TOTAL3"), TOTAL4 = e.GetString("TOTAL4"), TOTAL5 = e.GetString("TOTAL5"), VBAK_VBELN = e.GetString("VBAK_VBELN"), VBAK_POSNR = e.GetString("VBAK_POSNR"), VBAK_ZMENG = e.GetString("VBAK_ZMENG"), VBAK_ZIEME = e.GetString("VBAK_ZIEME"), VBAK_KONDM = e.GetString("VBAK_KONDM"), VBAK_VTEXT1 = e.GetString("VBAK_VTEXT1"), VBAK_BSTKD_E = e.GetString("VBAK_BSTKD_E"), VBAK_EQUNR = e.GetString("VBAK_EQUNR"), VBAK_TYPBZ = e.GetString("VBAK_TYPBZ"), VBAK_MAPAR = e.GetString("VBAK_MAPAR"), VBAK_SERGE = e.GetString("VBAK_SERGE"), VBAK_LOCATION = e.GetString("VBAK_LOCATION"), VBAK_PERNR1 = e.GetString("VBAK_PERNR1"), VBAK_PERNR2 = e.GetString("VBAK_PERNR2"), VBAK_EMPNAME = e.GetString("VBAK_EMPNAME"), VBAK_PERSAREA = e.GetString("VBAK_PERSAREA"), VBAK_PERSAREA_T = e.GetString("VBAK_PERSAREA_T"), VBAK_SUBAREA = e.GetString("VBAK_SUBAREA"), VBAK_SUBAREA_T = e.GetString("VBAK_SUBAREA_T"), VBAK_ORGUNIT = e.GetString("VBAK_ORGUNIT"), VBAK_ORGUNIT_T = e.GetString("VBAK_ORGUNIT_T"), VBAK_OUCODE = e.GetString("VBAK_OUCODE"), VBAK_KOSTL = e.GetString("VBAK_KOSTL"), VBAK_WAERK = e.GetString("VBAK_WAERK"), VBAK_NETWR = e.GetString("VBAK_NETWR"), VBAK_BEDAT = e.GetString("VBAK_BEDAT"), VBAK_ENDAT = e.GetString("VBAK_ENDAT"), VBAK_ABGRU_T = e.GetString("VBAK_ABGRU_T"), VBAK_PS_PSP_PNR = e.GetString("VBAK_PS_PSP_PNR"), VBAK_PSTYV = e.GetString("VBAK_PSTYV"), VBAK_KONDA = e.GetString("VBAK_KONDA"), VBAK_VTEXT2 = e.GetString("VBAK_VTEXT2"), VBAK_BSTKD = e.GetString("VBAK_BSTKD"), VBAK_BSTDK = e.GetString("VBAK_BSTDK"), VBAK_NAME = e.GetString("VBAK_NAME"), VBAK_APPROVAL = e.GetString("VBAK_APPROVAL"), VBAK_NOTE = e.GetString("VBAK_NOTE"), VBAK_ADDRESS = e.GetString("VBAK_ADDRESS"), VBAK_FAKSK_T = e.GetString("VBAK_FAKSK_T"), VBAK_EQ_YEAR = e.GetString("VBAK_EQ_YEAR"), LogMessage = "", //NOTO CachedVersion = cachedVersion, NO = e.GetString("VBAK_VBELN") + "#" + e.GetString("VBAK_POSNR"), }); Task.Run(() => PersistsToCache(ret)); resp.Items = ret.ToList(); // InMemoryCache.Instance.ClearCached(Username + Suffix.QUERIED_SAP_SESSIONID); } else { System.Diagnostics.Debug.WriteLine("</QUERY_TOTAL_RECORDS>"); throw new FaultException("RfcDestination is null!"); } } #endregion return(resp); }
public void CopyBillingValues(InvoiceProformaBilling src, InvoiceProformaBilling dest) { dest.VBELN = src.VBELN; dest.POSNR = src.POSNR; dest.AUBEL = src.AUBEL; dest.AUPOS = src.AUPOS; dest.MATKL = src.MATKL; dest.ITEM_TYPE = src.ITEM_TYPE; dest.CATEGORY = src.CATEGORY; dest.SUBCATEGORY = src.SUBCATEGORY; dest.WGBEZ = src.WGBEZ; dest.MATNR = src.MATNR; dest.ARKTX = src.ARKTX; dest.FKIMG = src.FKIMG; dest.VRKME = src.VRKME; dest.TOTAL1 = src.TOTAL1; dest.TOTAL2 = src.TOTAL2; dest.TOTAL3 = src.TOTAL3; dest.TOTAL4 = src.TOTAL4; dest.TOTAL5 = src.TOTAL5; dest.VBAK_VBELN = src.VBAK_VBELN; dest.VBAK_POSNR = src.VBAK_POSNR; dest.VBAK_ZMENG = src.VBAK_ZMENG; dest.VBAK_ZIEME = src.VBAK_ZIEME; dest.VBAK_KONDM = src.VBAK_KONDM; dest.VBAK_VTEXT1 = src.VBAK_VTEXT1; dest.VBAK_BSTKD_E = src.VBAK_BSTKD_E; dest.VBAK_EQUNR = src.VBAK_EQUNR; dest.VBAK_TYPBZ = src.VBAK_TYPBZ; dest.VBAK_MAPAR = src.VBAK_MAPAR; dest.VBAK_SERGE = src.VBAK_SERGE; dest.VBAK_LOCATION = src.VBAK_LOCATION; dest.VBAK_PERNR1 = src.VBAK_PERNR1; dest.VBAK_PERNR2 = src.VBAK_PERNR2; dest.VBAK_EMPNAME = src.VBAK_EMPNAME; dest.VBAK_PERSAREA = src.VBAK_PERSAREA; dest.VBAK_PERSAREA_T = src.VBAK_PERSAREA_T; dest.VBAK_SUBAREA = src.VBAK_SUBAREA; dest.VBAK_SUBAREA_T = src.VBAK_SUBAREA_T; dest.VBAK_ORGUNIT = src.VBAK_ORGUNIT; dest.VBAK_ORGUNIT_T = src.VBAK_ORGUNIT_T; dest.VBAK_OUCODE = src.VBAK_OUCODE; dest.VBAK_KOSTL = src.VBAK_KOSTL; dest.VBAK_WAERK = src.VBAK_WAERK; dest.VBAK_NETWR = src.VBAK_NETWR; dest.VBAK_BEDAT = src.VBAK_BEDAT; dest.VBAK_ENDAT = src.VBAK_ENDAT; dest.VBAK_ABGRU_T = src.VBAK_ABGRU_T; dest.VBAK_PS_PSP_PNR = src.VBAK_PS_PSP_PNR; dest.VBAK_PSTYV = src.VBAK_PSTYV; dest.VBAK_KONDA = src.VBAK_KONDA; dest.VBAK_VTEXT2 = src.VBAK_VTEXT2; dest.VBAK_BSTKD = src.VBAK_BSTKD; dest.VBAK_BSTDK = src.VBAK_BSTDK; dest.VBAK_NAME = src.VBAK_NAME; dest.VBAK_APPROVAL = src.VBAK_APPROVAL; dest.VBAK_NOTE = src.VBAK_NOTE; dest.VBAK_ADDRESS = src.VBAK_ADDRESS; dest.VBAK_FAKSK_T = src.VBAK_FAKSK_T; dest.VBAK_EQ_YEAR = src.VBAK_EQ_YEAR; dest.LogMessage = src.LogMessage; //NOTO dest.Remarks = src.Remarks; }
public InvoiceProformaBilling FromBillingDTO2Model(InvoiceProformaItemDTO dto) { var nbi = new InvoiceProformaBilling { VBELN = dto.VBELN, POSNR = dto.POSNR, AUBEL = dto.AUBEL, AUPOS = dto.AUPOS, MATKL = dto.MATKL, ITEM_TYPE = dto.ITEM_TYPE, CATEGORY = dto.CATEGORY, SUBCATEGORY = dto.SUBCATEGORY, WGBEZ = dto.WGBEZ, MATNR = dto.MATNR, ARKTX = dto.ARKTX, FKIMG = dto.FKIMG, VRKME = dto.VRKME, TOTAL1 = dto.TOTAL1, TOTAL2 = dto.TOTAL2, TOTAL3 = dto.TOTAL3, TOTAL4 = dto.TOTAL4, TOTAL5 = dto.TOTAL5, VBAK_VBELN = dto.VBAK_VBELN, VBAK_POSNR = dto.VBAK_POSNR, VBAK_ZMENG = dto.VBAK_ZMENG, VBAK_ZIEME = dto.VBAK_ZIEME, VBAK_KONDM = dto.VBAK_KONDM, VBAK_VTEXT1 = dto.VBAK_VTEXT1, VBAK_BSTKD_E = dto.VBAK_BSTKD_E, VBAK_EQUNR = dto.VBAK_EQUNR, VBAK_TYPBZ = dto.VBAK_TYPBZ, VBAK_MAPAR = dto.VBAK_MAPAR, VBAK_SERGE = dto.VBAK_SERGE, VBAK_LOCATION = dto.VBAK_LOCATION, VBAK_PERNR1 = dto.VBAK_PERNR1, VBAK_PERNR2 = dto.VBAK_PERNR2, VBAK_EMPNAME = dto.VBAK_EMPNAME, VBAK_PERSAREA = dto.VBAK_PERSAREA, VBAK_PERSAREA_T = dto.VBAK_PERSAREA_T, VBAK_SUBAREA = dto.VBAK_SUBAREA, VBAK_SUBAREA_T = dto.VBAK_SUBAREA_T, VBAK_ORGUNIT = dto.VBAK_ORGUNIT, VBAK_ORGUNIT_T = dto.VBAK_ORGUNIT_T, VBAK_OUCODE = dto.VBAK_OUCODE, VBAK_KOSTL = dto.VBAK_KOSTL, VBAK_WAERK = dto.VBAK_WAERK, VBAK_NETWR = dto.VBAK_NETWR, VBAK_BEDAT = dto.VBAK_BEDAT, VBAK_ENDAT = dto.VBAK_ENDAT, VBAK_ABGRU_T = dto.VBAK_ABGRU_T, VBAK_PS_PSP_PNR = dto.VBAK_PS_PSP_PNR, VBAK_PSTYV = dto.VBAK_PSTYV, VBAK_KONDA = dto.VBAK_KONDA, VBAK_VTEXT2 = dto.VBAK_VTEXT2, VBAK_BSTKD = dto.VBAK_BSTKD, VBAK_BSTDK = dto.VBAK_BSTDK, VBAK_NAME = dto.VBAK_NAME, VBAK_APPROVAL = dto.VBAK_APPROVAL, VBAK_NOTE = dto.VBAK_NOTE, VBAK_ADDRESS = dto.VBAK_ADDRESS, VBAK_FAKSK_T = dto.VBAK_FAKSK_T, VBAK_EQ_YEAR = dto.VBAK_EQ_YEAR, LogMessage = dto.LogMessage, //NOTO Remarks = dto.Remarks }; return(nbi); }
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); }