private void InsertIntoLedgerdetail(TblLedgerMainDetail row, ccnewEntities entity) { var query = (from e in entity.TblLedgerDetails where e.TblLedgerMainDetail == row.Iserial select e).ToList(); foreach (var variable in query) { entity.DeleteObject(variable); } if (row.OffsetEntityAccount != null) { var newrow = new TblLedgerDetail(); newrow.InjectFrom(row); newrow.DrOrCr = !row.DrOrCr; newrow.TblJournalAccountType = row.OffsetAccountType; newrow.EntityAccount = row.OffsetEntityAccount; newrow.GlAccount = row.OffsetGlAccount; newrow.EntityKey = null; newrow.EntityRef = null; newrow.OffsetAccountType = row.TblJournalAccountType; newrow.OffsetEntityAccount = row.EntityAccount; newrow.OffsetGlAccount = row.GlAccount; newrow.TblLedgerMainDetail = row.Iserial; newrow.Iserial = 0; entity.TblLedgerDetails.AddObject(newrow); } var newRow2 = new TblLedgerDetail(); newRow2.InjectFrom(row); newRow2.EntityKey = null; newRow2.EntityRef = null; newRow2.TblLedgerMainDetail = row.Iserial; newRow2.Iserial = 0; entity.TblLedgerDetails.AddObject(newRow2); entity.SaveChanges(); }
private void ImportLedgerMainDetails(List <TblLedgerMainDetail> list, string company) { using (var entity = new ccnewEntities(GetSqlConnectionString(company))) { foreach (var row in list) { if (row.TblCurrency1 != null && !string.IsNullOrWhiteSpace(row.TblCurrency1.Ename)) { var currency = entity.TblCurrencyTests.FirstOrDefault(x => x.Ename.ToLower() == row.TblCurrency1.Ename.ToLower()); if (currency != null) { row.TblCurrency = currency.Iserial; row.TblCurrency1 = null; if (row.ExchangeRate == 0 || row.ExchangeRate == null) { row.ExchangeRate = currency.ExchangeRate; } else { row.ExchangeRate = row.ExchangeRate; } } } if (row.TblJournalAccountType1 != null && !string.IsNullOrWhiteSpace(row.TblJournalAccountType1.Ename)) { try { var journalAccountType = entity.TblJournalAccountTypes.SingleOrDefault(x => x.Ename.ToLower() == row.TblJournalAccountType1.Ename.ToLower() || x.Code.ToLower() == row.TblJournalAccountType1.Ename.ToLower()); if (journalAccountType != null) { row.TblJournalAccountType = journalAccountType.Iserial; row.TblJournalAccountType1 = null; } else { throw new Exception("Journal Account Type Not Found :" + row.TblJournalAccountType1.Ename); } } catch (Exception) { throw new Exception("Something Wrong With Journal Account Type :" + row.TblJournalAccountType1.Ename); } } if (row.TblJournalAccountType2 != null && !string.IsNullOrWhiteSpace(row.TblJournalAccountType2.Code)) { try { var journalAccountType = entity.Entities.SingleOrDefault(x => x.scope == 0 && x.Code.ToLower() == row.TblJournalAccountType2.Code.ToLower() && x.TblJournalAccountType == row.TblJournalAccountType); if (journalAccountType != null) { row.EntityAccount = journalAccountType.Iserial; row.TblJournalAccountType2 = null; } else { throw new Exception("Entity Not Found Code:" + row.TblJournalAccountType2.Code + " With Type : " + row.TblJournalAccountType2.Code.ToLower()); } } catch (Exception) { throw new Exception("Something Wrong With Entity :" + row.TblJournalAccountType2.Code + " With Type : " + row.TblJournalAccountType2.Code.ToLower()); } } if (row.TblMethodOfPayment1 != null && !string.IsNullOrWhiteSpace(row.TblMethodOfPayment1.Code)) { try { var journalAccountType = entity.TblAccounts.SingleOrDefault(x => x.Code.ToLower() == row.TblMethodOfPayment1.Code.ToLower()); if (journalAccountType != null) { row.GlAccount = journalAccountType.Iserial; row.TblMethodOfPayment1 = null; } else { throw new Exception("Account Not Found Code:" + row.TblMethodOfPayment1.Code.ToLower()); } } catch (Exception) { throw new Exception("Something Wrong With Account Code :" + row.TblMethodOfPayment1.Code.ToLower()); } } foreach (var variable in row.TblLedgerDetailCostCenters.Where(x => x.TblCostCenter1.Code != null && x.TblCostCenter1.Code != "")) { variable.Amount = (double)row.Amount; try { var journalAccountType = entity.TblCostCenterTypes.SingleOrDefault(x => x.Ename.ToLower() == variable.TblCostCenterType1.Ename.ToLower()); if (journalAccountType != null) { variable.TblCostCenterType = journalAccountType.Iserial; variable.TblCostCenterType1 = null; } else { throw new Exception("Cost Center Type Not Found Code:" + variable.TblCostCenterType1.Ename.ToLower()); } } catch { throw new Exception("something Wrong With Center Type Code:" + variable.TblCostCenterType1.Ename.ToLower()); } try { var costCenter = entity.TblCostCenters.SingleOrDefault(x => x.TblCostCenterType == variable.TblCostCenterType && x.Code.ToLower() == variable.TblCostCenter1.Code.ToLower()); if (costCenter != null) { variable.TblCostCenter = costCenter.Iserial; variable.TblCostCenter1 = null; } else { throw new Exception("Cost Center Not Found Code:" + variable.TblCostCenter1.Code.ToLower()); } } catch { throw new Exception("Something Wrong with Cost Center Code:" + variable.TblCostCenter1.Code.ToLower()); } } var tblLedgerHeader = entity.TblLedgerHeaders.FirstOrDefault(x => x.Iserial == row.TblLedgerHeader); if (tblLedgerHeader != null) { var ledgerheader = tblLedgerHeader.TblJournal; var journal = entity.TblJournals.Include("TblSequence1").Include("TblSequence").FirstOrDefault(x => x.Iserial == ledgerheader); int temp = 0; row.Code = HandelSequence(row.Code, journal, "TblLedgerMainDetail", company, list.IndexOf(row), 0, 0, out temp); } var newrow = new TblLedgerDetail(); newrow.InjectFrom(row); row.TblLedgerDetails = new EntityCollection <TblLedgerDetail> { newrow }; entity.TblLedgerMainDetails.AddObject(row); } entity.SaveChanges(); } }