public ActionResult AddEditBalance(OpenBalanceVm openBalanceVm) { #region Check Rights bool hasRights; string accountId = ""; string tbName, pkName; int libId; libId = openBalanceVm.LibItemId; tbName = openBalanceVm.TbName; pkName = openBalanceVm.PkName; //Get AccountId accountId = AccountingChartHelper.GetAccountIdByPkAndTbName(libId, tbName, pkName); if (string.IsNullOrEmpty(accountId)) { hasRights = AdminHelper.CheckUserAction(ScreenEnum.OpenBalance, ActionEnum.Add); } else { hasRights = AdminHelper.CheckUserAction(ScreenEnum.OpenBalance, ActionEnum.Edit); } if (!hasRights) { return(RedirectToAction("UnAuthorized", "Home", new { area = "" })); } #endregion string isSaved = AccountingHelper.AddEditOpenBalance(openBalanceVm); return(Json(new { IsSaved = isSaved })); }
//public static OpenBalanceVm GetOpenBalanceForCash() //{ // OpenBalanceVm balanceVm = new OpenBalanceVm(); // balanceVm.TbName = "Currency"; // balanceVm.LibItemId = 0; // balanceVm.CreateDate = DateTime.Now; // balanceVm.IsCreditAgent = false; // AccountingEntities db = new AccountingEntities(); // //Get Currency List // var currList = db.CurrencyAccs.ToList(); // OpenBalanceDetailVm openBalanceDet; // foreach (var item in currList) // { // string accountId = AccountingChartHelper.GetAccountIdByPkAndTbName(item.CurrencyId, "Currency", "CurrencyId"); // //Get Transaction details for open balance // var accTanDet = db.AccTransactionDetails.Include("AccTransaction") // .Where(x => x.AccountId == accountId && x.AccTransaction.TransactionName == "open balance").ToList(); // openBalanceDet = new OpenBalanceDetailVm(); // openBalanceDet.CurrencyId = item.CurrencyId; // openBalanceDet.CurrencySign = item.CurrencySign; // openBalanceDet.CurrencyAccountId = item.AccountId; // //check if has open balance for this currency // var openAccTran = accTanDet.Where(x => x.CurrencyId == item.CurrencyId).FirstOrDefault(); // if (openAccTran != null) // { // openBalanceDet.TransDetailId = openAccTran.TransDetailId; // openBalanceDet.CreditAmount = openAccTran.CreditAmount; // openBalanceDet.DebitAmount = openAccTran.DebitAmount; // } // balanceVm.OpenBalanceDetails.Add(openBalanceDet); // } // return balanceVm; //} public static string AddEditOpenBalance(OpenBalanceVm openBalanceVm) { string isSaved = "true"; string accountId = ""; string tbName, pkName; int libId; bool isCreditAgent = false; libId = openBalanceVm.LibItemId; tbName = openBalanceVm.TbName; pkName = openBalanceVm.PkName; if (openBalanceVm.IsCreditAgent != null) { isCreditAgent = openBalanceVm.IsCreditAgent.Value; } //Get AccountId accountId = AccountingChartHelper.GetAccountIdByPkAndTbName(libId, tbName, pkName); if (string.IsNullOrEmpty(accountId)) { switch (tbName) { case "Agent": byte agentType; if (isCreditAgent == false) { agentType = 1; //debit note A/R } else { agentType = 2; //Credit note A/P } accountId = AccountingChartHelper.AddAgentToChart(libId, agentType); break; case "Carrier": accountId = AccountingChartHelper.AddCarrierToChart(libId); break; case "Contractor": accountId = AccountingChartHelper.AddContractorToChart(libId); break; case "Shipper": accountId = AccountingChartHelper.AddShipperToChart(libId); break; case "Consignee": accountId = AccountingChartHelper.AddConsigneeToChart(libId); break; case "Currency": accountId = AccountingChartHelper.AddCashToChart(libId); break; case "BankAccount": accountId = AccountingChartHelper.AddBankAccountToChart(0, libId); break; case "AccountingChart": accountId = AccountingChartHelper.AddBankAccountToChart(0, libId); break; } AccountingChartHelper.AddAccountIdToObj(accountId, tbName, libId, pkName); } openBalanceVm.AccountId = accountId; int transId = openBalanceVm.TransId; AccTransaction accTran; AccountingEntities db = new AccountingEntities(); if (transId != 0) { accTran = db.AccTransactions.Include("AccTransactionDetails").Where(x => x.TransId == transId).FirstOrDefault(); //delete all tran details and add it later foreach (var item in accTran.AccTransactionDetails.ToList()) { db.AccTransactionDetails.Remove(item); } } else { accTran = new AccTransaction() { CreateBy = AdminHelper.GetCurrentUserId(), CreateDate = DateTime.Now, TransactionName = "open balance" }; } Mapper.CreateMap <OpenBalanceDetailVm, AccTransactionDetail>(); AccTransactionDetail accTranDetail; foreach (var item in openBalanceVm.OpenBalanceDetails.Where(x => x.DebitAmount != null || x.CreditAmount != null)) { accTranDetail = new AccTransactionDetail(); item.AccountId = accountId; Mapper.Map(item, accTranDetail); accTran.AccTransactionDetails.Add(accTranDetail); } if (transId == 0) { db.AccTransactions.Add(accTran); } try { db.SaveChanges(); } catch (DbEntityValidationException e) { isSaved = "false " + e.Message; } catch (Exception e) { isSaved = "false " + e.Message; } return(isSaved); }
public static OpenBalanceVm GetOpenBalanceInfo(string tbName, int libId, string pkName) { OpenBalanceVm balanceVm = new OpenBalanceVm(); balanceVm.TbName = tbName; balanceVm.LibItemId = libId; balanceVm.CreateDate = DateTime.Now; balanceVm.IsCreditAgent = false; string accountId = ""; accountId = AccountingChartHelper.GetAccountIdByPkAndTbName(libId, tbName, pkName); AccountingEntities db = new AccountingEntities(); //Get Transaction details for open balance var accTanDet = db.AccTransactionDetails.Include("AccTransaction") .Where(x => x.AccountId == accountId && x.AccTransaction.TransactionName == "open balance").ToList(); if (accTanDet.Count > 0) { balanceVm.TransId = accTanDet.FirstOrDefault().TransId; } int?bankAccCurrId = null; //For bank Acc will get only the currency for this bank if (tbName == "BankAccount") { bankAccCurrId = db.BankAccounts.Where(x => x.BankAccId == libId).FirstOrDefault().CurrencyId; } //Get Currency List // Dictionary<int, string> currList = ListCommonHelper.GetCurrencyList(); List <CurrencyAcc> currList; if (bankAccCurrId == null) { currList = db.CurrencyAccs.ToList(); } else { currList = db.CurrencyAccs.Where(x => x.CurrencyId == bankAccCurrId).ToList(); } OpenBalanceDetailVm openBalanceDet; foreach (var item in currList) { openBalanceDet = new OpenBalanceDetailVm(); openBalanceDet.CurrencyId = item.CurrencyId; openBalanceDet.CurrencySign = item.CurrencySign; openBalanceDet.CurrencyAccountId = item.AccountId; openBalanceDet.LibItemId = libId; //check if has open balance for this currency var openAccTran = accTanDet.Where(x => x.CurrencyId == item.CurrencyId).FirstOrDefault(); if (openAccTran != null) { openBalanceDet.TransDetailId = openAccTran.TransDetailId; openBalanceDet.CreditAmount = openAccTran.CreditAmount; openBalanceDet.DebitAmount = openAccTran.DebitAmount; openBalanceDet.TransId = openAccTran.TransId; } balanceVm.OpenBalanceDetails.Add(openBalanceDet); } return(balanceVm); }