public long Create(InvoiceProformaHeader o) { using (var db = new BillingDbContext()) { db.InvoiceProformaHeaders.Add(o); db.SaveChanges(); return(o.No); } }
public int?SaveBillingBlock(string user, string billingNumber) { List <ContractBlock> listContract = new List <ContractBlock>(); const string FUNCTIONAL_BAPI = "ZBAPI_SALESORDER_CHANGE"; using (var dao = new BillingDbContext()) { var sql1 = from o in dao.InvoiceProformaHeaders.Where(o => o.BillingNo == billingNumber).OrderByDescending(o => o.Version).Include(o => o.Billings) select o; var count = sql1.ToList().Count; if (count > 1) { InvoiceProformaHeader oldHeaderVersion = sql1.ToArray()[0]; var dest = GetRfcDestination(); if (dest != null) { var repo = dest.Repository; var func = repo.CreateFunction(FUNCTIONAL_BAPI); IRfcTable I_REJECT1 = func.GetTable("I_REJECT1"); foreach (var bi in oldHeaderVersion.Billings) { I_REJECT1.Append(); I_REJECT1.SetValue("MANDT", "999"); I_REJECT1.SetValue("VBELN", bi.AUBEL); I_REJECT1.SetValue("POSNR", bi.AUPOS); I_REJECT1.SetValue("ABGRU", bi.VBAK_ABGRU_T); } func.Invoke(dest); } } } return(1); }
public override SAPResponse ExecuteCommand() { try { InvoiceProformaBillingRunsDTO ret = new InvoiceProformaBillingRunsDTO(); System.Diagnostics.Debug.WriteLine("=========> MASUK SUBMIT TO SAP BY HEADER...."); 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; var vrFromDb = InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_VERSION_FROM_DB) is int?(int)InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_VERSION_FROM_DB) : 0; InMemoryCache.Instance.ClearCached(Username + Suffix.QUERIED_VERSION_FROM_DB); var resp = new InvoiceProformaBillingRunsDTO(); const string FUNCTIONAL_BAPI = "ZBAPI_SALESORDER_CHANGE"; var sql2 = from o in dao.InvoiceProformaHeaders.Where(o => o.BillingNo == header.BillingNo && o.Version == vrFromDb && !o.Draft).OrderByDescending(o => o.Version).Include(o => o.Billings) select o; var count1 = sql2.ToList().Count(); var list2 = sql2.ToList(); resp.Items = list2.Select(h => new InvoiceProformaBillingRunDTO { Created = h.Created, Version = h.Version, No = (int)h.No, SoldToParty = h.SoldToParty, Draft = h.Draft }).ToList(); ret = resp; var reasonForRejection = new Dictionary <string, string>(); reasonForRejection["ASSIGNED BY THE SYSTEM (INTERNAL)"] = "00"; reasonForRejection["DELIVERY DATE TOO LATE"] = "01"; reasonForRejection["POOR QUALITY"] = "02"; reasonForRejection["TOO EXPENSIVE"] = "03"; reasonForRejection["COMPETITOR BETTER"] = "04"; reasonForRejection["GUARANTEE"] = "05"; reasonForRejection["UNREASONABLE REQUEST"] = "10"; reasonForRejection["CUST.TO RECEIVE REPLACEMENT"] = "11"; reasonForRejection["BILLING BLOCKED"] = "12"; reasonForRejection["DOUBLE INPUT"] = "13"; reasonForRejection["AVAILABILITY"] = "20"; reasonForRejection["COVERAGE"] = "21"; reasonForRejection["PRICE"] = "22"; reasonForRejection["RELATIONSHIP"] = "23"; reasonForRejection["STANDARDIZATON"] = "24"; reasonForRejection["PRODUCT SPECIFICATION"] = "25"; reasonForRejection["TERM OF PAYMENT"] = "26"; reasonForRejection["OTHERS"] = "27"; reasonForRejection["DON'T PRINT"] = "28"; reasonForRejection["TRANSACTION IS BEING BLOCK"] = "29"; reasonForRejection["DOCUMENT HELD"] = "30"; reasonForRejection["LOST SALES"] = "31"; reasonForRejection["ZPROGRAM DO NOT USE"] = "32"; reasonForRejection["TRANSACTION IS BEING CHECKED"] = "50"; reasonForRejection["EQUIPMENT REPLACEMENT"] = "60"; reasonForRejection["EQUIPMENT REJECTION"] = "61"; reasonForRejection["EXCESS HOURS"] = "63"; reasonForRejection["REVOKE USER APPLICATION"] = "65"; reasonForRejection["RETURNED IT DEVICE"] = "66"; reasonForRejection["REPLACEMENT IT DEVICE"] = "67"; reasonForRejection["REPAIR AND MAINTENANCE"] = "68"; reasonForRejection["UPGRADE IT DEVICE"] = "69"; reasonForRejection["COMPLETION OF CONTRACT"] = "70"; reasonForRejection["REJECT IN PROGRESS"] = "91"; reasonForRejection["PTTU SERVICE REJECTION"] = "99"; reasonForRejection["ADDITIONAL PARTS"] = "C1"; reasonForRejection["REPLACEMENT PARTS"] = "C2"; reasonForRejection["TRANSACTION TO BE DEAL"] = "C3"; reasonForRejection["SSB SERVICE REJECTION"] = "Z1"; reasonForRejection["TECHNICAL REASON"] = "Z2"; reasonForRejection["BILLING BY DEBIT MEMO"] = "Z3"; if (count1 > 0) { InvoiceProformaHeader oldHeaderVersion1 = sql2.ToArray()[0]; System.Diagnostics.Debug.WriteLine(">>>> " + Username); var cred = ParseCredential(Username); var dest = SAPConnectionFactory.Instance.GetRfcDestination(cred); if (dest != null) { var repo = dest.Repository; var stopwatch = new Stopwatch(); stopwatch.Start(); var func = repo.CreateFunction(FUNCTIONAL_BAPI); IRfcTable I_REJECT1 = func.GetTable("I_REJECT1"); foreach (var bi in oldHeaderVersion1.Billings) { I_REJECT1.Append(); I_REJECT1.SetValue("MANDT", "999"); I_REJECT1.SetValue("VBELN", bi.VBAK_VBELN); I_REJECT1.SetValue("POSNR", bi.VBAK_POSNR); try { I_REJECT1.SetValue("ABGRU", reasonForRejection[bi.VBAK_ABGRU_T.ToUpper()]); } catch (Exception ex) { I_REJECT1.SetValue("ABGRU", ""); } // bi.LogMessage="test"; } //dao.SaveChanges(); System.Diagnostics.Debug.WriteLine("<EXECUTING_BAPI NAME = '" + FUNCTIONAL_BAPI + "'>"); func.Invoke(dest); var getDataMessage = func.GetTable("O_BLOCK_STAT"); var list = new OstatusBlock(); var list3 = getDataMessage.Select(e => new OstatusBlock { VBELN = e.GetString("VBELN"), POSNR = e.GetString("POSNR"), FLAG = e.GetString("FLAG"), MESSAGE = e.GetString("MESSAGE"), }); foreach (var item in list3) { if (item.MESSAGE == "") { foreach (var xx in oldHeaderVersion1.Billings) { xx.LogMessage = "Success"; } } else { foreach (var xx in oldHeaderVersion1.Billings) { xx.LogMessage = item.MESSAGE; } } } dao.SaveChanges(); System.Diagnostics.Debug.WriteLine("<EXECUTING_BAPI/>"); stopwatch.Stop(); System.Diagnostics.Debug.WriteLine(">>>> " + stopwatch.Elapsed); } } else { throw new FaultException("Version Final not Found!"); } } return(ret); } catch (Exception ex) { throw ex; } }
protected void CreateOrOverwriteZeroVersion(bool doOverride, BillingDbContext dao) { var header = InMemoryCache.Instance.GetCached(Username + Suffix.REQUEST_HEADER) as RunInvoiceHeaderDTO; var total = InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_PROFORMA_TOTAL_RECS) is int?(int)InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_PROFORMA_TOTAL_RECS) : 0; var cachedVersion = InMemoryCache.Instance.GetCached(Username + Suffix.CACHED_VERSION) is long?(long)InMemoryCache.Instance.GetCached(Username + Suffix.CACHED_VERSION) : 0; if (GetTotalCached(cachedVersion) >= total) { InMemoryCache.Instance.ClearCached(Username + Suffix.QUERIED_FROM_DB); InMemoryCache.Instance.Cache(Username + Suffix.QUERIED_FROM_DB, true); InMemoryCache.Instance.Cache(Username + Suffix.QUERIED_VERSION_FROM_DB, 0); var rawHeader = InMemoryCache.Instance.GetCached(Username + Suffix.QUERIED_PROFORMA_HEADER) as InvoiceProformaHeaderDto; var cachedBills = GetCachedBillings(cachedVersion); if (CountZeroVersion(header, dao) <= 0 && header != null && rawHeader != null) { var newZeroHeaderVersion = new InvoiceProformaHeader { Version = 0, SoldToParty = header.SoldToParty, BillingBlock = header.BillingDocsCriteria, ReasonForRejection = header.ReasonForRejection, ProformaFlag = header.ProformaFlag, BillingNo = header.BillingNo, Created = DateTime.Now, StartDate = header.BillingDateFrom, EndDate = header.BillingDateTo, Draft = true, CITY1 = rawHeader.CITY1, ERDAT = rawHeader.ERDAT, ERNAM = rawHeader.ERNAM, ERZET = rawHeader.ERZET, FKDAT = rawHeader.FKDAT, FPAJAK_NO = rawHeader.FPAJAK_NO, HTOTAL1 = rawHeader.HTOTAL1, HTOTAL2 = rawHeader.HTOTAL2, HTOTAL3 = rawHeader.HTOTAL3, HTOTAL4 = rawHeader.HTOTAL4, HTOTAL5 = rawHeader.HTOTAL5, KUNRG = rawHeader.KUNRG, KURRF = rawHeader.KURRF, NAME1 = rawHeader.NAME1, NAME2 = rawHeader.NAME2, NAME3 = rawHeader.NAME3, NAME4 = rawHeader.NAME4, POST_CODE1 = rawHeader.POST_CODE1, STCEG = rawHeader.STCEG, STREET = rawHeader.STREET, TDLINE = rawHeader.TDLINE, TEXT1 = rawHeader.TEXT1, VBELN = rawHeader.VBELN, VTEXT = rawHeader.VTEXT, WAERK = rawHeader.WAERK, ZTERM = rawHeader.ZTERM, }; // Add new header dao.InvoiceProformaHeaders.Add(newZeroHeaderVersion); dao.SaveChanges(); // Add newly retrieved billings var createZeroSql = createZero.Replace("@no", newZeroHeaderVersion.No + "").Replace("@cachedVersion", cachedVersion + ""); dao.Database.ExecuteSqlCommand(createZeroSql); } else { if (!doOverride) { return; } if (cachedBills.Count <= 0 || rawHeader == null || header == null) { return; } var sql1 = from o in dao.InvoiceProformaHeaders.Where( o => o.BillingBlock == header.BillingDocsCriteria && o.ReasonForRejection == header.ReasonForRejection && o.ProformaFlag == header.ProformaFlag && o.BillingNo == header.BillingNo && o.SoldToParty == header.SoldToParty) select o; InvoiceProformaHeader oldZeroHeaderVersion = sql1.FirstOrDefault(); if (oldZeroHeaderVersion != null) { // Deletes old billings dao.Database.ExecuteSqlCommand("DELETE FROM dbo.InvoiceProformaBilling WHERE InvoiceProformaHeader_No = " + oldZeroHeaderVersion.No); var createZeroSql = createZero.Replace("@no", oldZeroHeaderVersion.No + "").Replace("@cachedVersion", cachedVersion + ""); // Add newly retrieved billings dao.Database.ExecuteSqlCommand(createZeroSql); } } } else { Thread.Sleep(2000); CreateOrOverwriteZeroVersion(doOverride, dao); } }
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); }
public override SAPResponse ExecuteCommand() { try { System.Diagnostics.Debug.WriteLine("=========> MASUK SUBMIT TO SAP BY ID...."); InvoiceProformaBillingRunsDTO ret = new InvoiceProformaBillingRunsDTO(); using (var dao = new BillingDbContext()) { const string FUNCTIONAL_BAPI = "ZBAPI_SALESORDER_CHANGE"; var resp = new InvoiceProformaBillingRunsDTO(); var sql2 = from o in dao.InvoiceProformaHeaders.Where(o => o.No == ID).OrderByDescending(o => o.Version).Include(o => o.Billings) select o; var count1 = sql2.Count(); var list2 = sql2.ToList(); resp.Items = list2.Select(h => new InvoiceProformaBillingRunDTO { Created = h.Created, Version = h.Version, No = (int)h.No, SoldToParty = h.SoldToParty, Draft = h.Draft }).ToList(); ret = resp; var reasonForRejection = new Dictionary <string, string>(); reasonForRejection["ASSIGNED BY THE SYSTEM (INTERNAL)"] = "00"; reasonForRejection["DELIVERY DATE TOO LATE"] = "01"; reasonForRejection["POOR QUALITY"] = "02"; reasonForRejection["TOO EXPENSIVE"] = "03"; reasonForRejection["COMPETITOR BETTER"] = "04"; reasonForRejection["GUARANTEE"] = "05"; reasonForRejection["UNREASONABLE REQUEST"] = "10"; reasonForRejection["CUST.TO RECEIVE REPLACEMENT"] = "11"; reasonForRejection["BILLING BLOCKED"] = "12"; reasonForRejection["DOUBLE INPUT"] = "13"; reasonForRejection["AVAILABILITY"] = "20"; reasonForRejection["COVERAGE"] = "21"; reasonForRejection["PRICE"] = "22"; reasonForRejection["RELATIONSHIP"] = "23"; reasonForRejection["STANDARDIZATON"] = "24"; reasonForRejection["PRODUCT SPECIFICATION"] = "25"; reasonForRejection["TERM OF PAYMENT"] = "26"; reasonForRejection["OTHERS"] = "27"; reasonForRejection["DON'T PRINT"] = "28"; reasonForRejection["TRANSACTION IS BEING BLOCK"] = "29"; reasonForRejection["DOCUMENT HELD"] = "30"; reasonForRejection["LOST SALES"] = "31"; reasonForRejection["ZPROGRAM DO NOT USE"] = "32"; reasonForRejection["TRANSACTION IS BEING CHECKED"] = "50"; reasonForRejection["EQUIPMENT REPLACEMENT"] = "60"; reasonForRejection["EQUIPMENT REJECTION"] = "61"; reasonForRejection["EXCESS HOURS"] = "63"; reasonForRejection["REVOKE USER APPLICATION"] = "65"; reasonForRejection["RETURNED IT DEVICE"] = "66"; reasonForRejection["REPLACEMENT IT DEVICE"] = "67"; reasonForRejection["REPAIR AND MAINTENANCE"] = "68"; reasonForRejection["UPGRADE IT DEVICE"] = "69"; reasonForRejection["COMPLETION OF CONTRACT"] = "70"; reasonForRejection["REJECT IN PROGRESS"] = "91"; reasonForRejection["PTTU SERVICE REJECTION"] = "99"; reasonForRejection["ADDITIONAL PARTS"] = "C1"; reasonForRejection["REPLACEMENT PARTS"] = "C2"; reasonForRejection["TRANSACTION TO BE DEAL"] = "C3"; reasonForRejection["SSB SERVICE REJECTION"] = "Z1"; reasonForRejection["TECHNICAL REASON"] = "Z2"; reasonForRejection["BILLING BY DEBIT MEMO"] = "Z3"; if (count1 > 0) { InvoiceProformaHeader oldHeaderVersion1 = sql2.ToArray()[0]; var cred = ParseCredential(Username); var dest = SAPConnectionFactory.Instance.GetRfcDestination(cred); if (dest != null) { var repo = dest.Repository; var func = repo.CreateFunction(FUNCTIONAL_BAPI); IRfcTable I_REJECT1 = func.GetTable("I_REJECT1"); foreach (var bi in oldHeaderVersion1.Billings) { I_REJECT1.Append(); I_REJECT1.SetValue("MANDT", "999"); I_REJECT1.SetValue("VBELN", bi.VBAK_VBELN); I_REJECT1.SetValue("POSNR", bi.VBAK_POSNR); try { I_REJECT1.SetValue("ABGRU", reasonForRejection[bi.VBAK_ABGRU_T.ToUpper()]); } catch (Exception ex) { I_REJECT1.SetValue("ABGRU", ""); } } func.Invoke(dest); } } } InMemoryCache.Instance.ClearCached(Username + Suffix.BILLINGS_TO_UPDATE); return(ret); } catch (Exception ex) { throw ex; } }