public string SaveLedger(LedgerModel model) { tblLedgerMaster tb = new tblLedgerMaster(); int CurrentYear = DateTime.Today.Year; int PreviousYear = DateTime.Today.Year - 1; int NextYear = DateTime.Today.Year + 1; string PreYear = PreviousYear.ToString(); string NexYear = NextYear.ToString(); string CurYear = CurrentYear.ToString(); string FinYear = null; if (DateTime.Today.Month > 3) { FinYear = CurYear + "-" + NexYear; } else { FinYear = PreYear + "-" + CurYear; } var duplicate = _entities.tblLedgerMasters.Where(o => o.LedgerName == model.LedgerName && o.tblLedgerGroup.LedgerGroupId == model.LedgerGroupId && o.OPBalance == model.OpeningBalance && o.Percentage == model.Percentage && o.Transfer_Type == model.TransferType).SingleOrDefault(); if (duplicate == null) { try { if (model.Id != 0) { tb = _entities.tblLedgerMasters.Where(x => x.LedgerMasterId == model.Id).SingleOrDefault(); tb.LedgerName = model.LedgerName; tb.LedgerGroup = model.LedgerGroupId; tb.Date = model.Date; tb.OPBalance = model.OpeningBalance; tb.Percentage = model.Percentage; tb.RecordId = model.Record_Id; tb.Transfer_Type = model.TransferType; _entities.SaveChanges(); int id = tb.LedgerMasterId; if (model.TransferType == "Credit") { Voucher_Entry_Credit tb1 = new Voucher_Entry_Credit(); if (_entities.Voucher_Entry_Credit.Where(x => x.voucher_ledger_accout_id == id) != null) { tb1 = _entities.Voucher_Entry_Credit.Where(x => x.voucher_ledger_accout_id == id && x.voucher_type == "OB").SingleOrDefault(); tb1.voucher_no = tb.RecordId.ToString(); tb1.Voucher_date = model.Date; tb1.record_no = tb.RecordId.ToString(); tb1.record_date = model.Date; tb1.voucher_sno = 1; tb1.voucher_tb = "To"; tb1.voucher_cr_amount = model.OpeningBalance; tb1.voucher_year = FinYear.Trim(); tb1.create_date = DateTime.Now; tb1.from_form_name = "Ledger_master"; tb1.from_form_id = id; tb1.account_type = "OB"; tb1.userid = WebSecurity.CurrentUserId; _entities.Voucher_Entry_Credit.Add(tb1); _entities.SaveChanges(); } else { tb1.voucher_no = tb.RecordId.ToString(); tb1.Voucher_date = model.Date; tb1.record_no = tb.RecordId.ToString(); tb1.record_date = model.Date; tb1.voucher_sno = 1; tb1.voucher_tb = "To"; tb1.voucher_ledger_accout_id = id; tb1.voucher_cr_amount = model.OpeningBalance; tb1.voucher_type = "OB"; tb1.voucher_year = FinYear.Trim(); tb1.create_date = DateTime.Now; tb1.from_form_name = "Ledger_master"; tb1.from_form_id = id; tb1.account_type = "OB"; tb1.userid = WebSecurity.CurrentUserId; _entities.Voucher_Entry_Credit.Add(tb1); _entities.SaveChanges(); } } else if (model.TransferType == "Debit") { Voucher_Entry_Debit tb2 = new Voucher_Entry_Debit(); if (_entities.Voucher_Entry_Debit.Where(x => x.voucher_ledger_accout_id == id) != null) { tb2 = _entities.Voucher_Entry_Debit.Where(x => x.voucher_ledger_accout_id == id && x.voucher_type == "OB").SingleOrDefault(); tb2.voucher_no = tb.RecordId.ToString(); tb2.voucher_date = model.Date; tb2.record_no = tb.RecordId.ToString(); tb2.record_date = model.Date; tb2.voucher_sno = 1; tb2.voucher_tb = "To"; tb2.voucher_dbt_amount = model.OpeningBalance; tb2.voucher_year = DateTime.Now.ToFinancialYear();//FinYear.Trim(); tb2.create_date = DateTime.Now; tb2.from_form_name = "Ledger_master"; tb2.from_form_id = model.Id; tb2.account_type = "OB"; tb2.userid = WebSecurity.CurrentUserId; _entities.Voucher_Entry_Debit.Add(tb2); _entities.SaveChanges(); } else { tb2.voucher_no = tb.RecordId.ToString(); tb2.voucher_date = model.Date; tb2.record_no = tb.RecordId.ToString(); tb2.record_date = model.Date; tb2.voucher_sno = 1; tb2.voucher_tb = "To"; tb2.voucher_ledger_accout_id = id; tb2.voucher_dbt_amount = model.OpeningBalance; tb2.voucher_type = "OB"; tb2.voucher_year = DateTime.Now.ToFinancialYear();//FinYear.Trim(); tb2.create_date = DateTime.Now; tb2.from_form_name = "Ledger_master"; tb2.from_form_id = model.Id; tb2.account_type = "OB"; tb2.userid = WebSecurity.CurrentUserId; _entities.Voucher_Entry_Debit.Add(tb2); _entities.SaveChanges(); } } return("Record Updated Successfully..."); } else { tb.LedgerName = model.LedgerName; tb.LedgerGroup = model.LedgerGroupId; tb.Date = model.Date; tb.OPBalance = model.OpeningBalance; if (_entities.tblLedgerMasters.Select(p => p.RecordId).Count() > 0) { tb.RecordId = _entities.tblLedgerMasters.Select(p => p.RecordId).Max() + 1; } else { tb.RecordId = 1; } tb.Percentage = model.Percentage; tb.Transfer_Type = model.TransferType; _entities.tblLedgerMasters.Add(tb); _entities.SaveChanges(); int id = tb.LedgerMasterId; if (model.TransferType == "Credit") { Voucher_Entry_Credit tb1 = new Voucher_Entry_Credit(); tb1.voucher_no = tb.RecordId.ToString(); tb1.Voucher_date = model.Date; tb1.record_no = tb.RecordId.ToString(); tb1.record_date = model.Date; tb1.voucher_sno = 1; tb1.voucher_tb = "To"; tb1.voucher_ledger_accout_id = id; tb1.voucher_cr_amount = model.OpeningBalance; tb1.voucher_type = "OB"; tb1.voucher_year = FinYear.Trim(); tb1.create_date = DateTime.Now; tb1.from_form_name = "Ledger_master"; tb1.from_form_id = id; tb1.account_type = "OB"; tb1.userid = WebSecurity.CurrentUserId; _entities.Voucher_Entry_Credit.Add(tb1); _entities.SaveChanges(); } else if (model.TransferType == "Debit") { Voucher_Entry_Debit tb2 = new Voucher_Entry_Debit(); tb2.voucher_no = tb.RecordId.ToString(); tb2.voucher_date = model.Date; tb2.record_no = tb.RecordId.ToString(); tb2.record_date = model.Date; tb2.voucher_sno = 1; tb2.voucher_tb = "To"; tb2.voucher_ledger_accout_id = tb.LedgerMasterId; tb2.voucher_dbt_amount = model.OpeningBalance; tb2.voucher_type = "OB"; tb2.voucher_year = DateTime.Now.ToFinancialYear();//FinYear.Trim(); tb2.create_date = DateTime.Now; tb2.from_form_name = "Ledger_master"; tb2.from_form_id = model.Id; tb2.account_type = "OB"; tb2.userid = WebSecurity.CurrentUserId; _entities.Voucher_Entry_Debit.Add(tb2); _entities.SaveChanges(); } return("Record Saved Successfully..."); } } catch (Exception ex) { return(ex.Message); } } else { return(model.LedgerName + " Already Exits"); } }
public ActionResult JournalBill(Journal model) { try { int CurrentYear = DateTime.Today.Year; int PreviousYear = DateTime.Today.Year - 1; int NextYear = DateTime.Today.Year + 1; string PreYear = PreviousYear.ToString(); string NexYear = NextYear.ToString(); string CurYear = CurrentYear.ToString(); string FinYear = null; if (DateTime.Today.Month > 3) { FinYear = CurYear + "-" + NexYear; } else { FinYear = PreYear + "-" + CurYear; } for (int i = 0; i < model.ByAccount.Length; i++) { Voucher_Entry_Debit tb1 = new Voucher_Entry_Debit(); tb1.voucher_no = model.VoucherNo; tb1.voucher_date = model.Date; tb1.record_no = model.RecordNo.ToString(); tb1.record_date = model.Date; tb1.voucher_sno = i + 1; tb1.voucher_tb = "By"; tb1.voucher_type = "Journal"; tb1.voucher_year = FinYear; tb1.from_form_name = "Journal"; tb1.userid = WebSecurity.CurrentUserId; tb1.account_type = "Journal"; tb1.from_form_id = 0; tb1.check_no = model.ChequeNo[i]; tb1.check_date = model.ChequeDate[i]; string accountname = model.ByAccount[i]; var idl = (from n in db.tblLedgerMasters.Where(x => x.LedgerName == accountname) select n.LedgerMasterId).SingleOrDefault(); tb1.voucher_ledger_accout_id = Convert.ToInt32(idl); tb1.voucher_dbt_amount = model.DrAmount[i]; tb1.voucher_narration = model.DrNarration[i]; tb1.create_date = DateTime.Now; db.Voucher_Entry_Debit.Add(tb1); db.SaveChanges(); } for (int j = 0; j < model.LedgerAccId.Length; j++) { Voucher_Entry_Credit tb = new Voucher_Entry_Credit(); tb.voucher_no = model.VoucherNo; tb.Voucher_date = model.Date; tb.record_no = model.RecordNo.ToString(); tb.record_date = model.Date; tb.voucher_sno = j + 1; tb.voucher_tb = "To"; tb.voucher_type = "Journal"; tb.voucher_year = FinYear; tb.from_form_name = "Journal"; tb.userid = WebSecurity.CurrentUserId; tb.account_type = "Journal"; tb.from_form_id = 0; string name = model.LedgerAccId[j]; var id = (from n in db.tblLedgerMasters.Where(x => x.LedgerName == name) select n.LedgerMasterId).SingleOrDefault(); tb.voucher_ledger_accout_id = Convert.ToInt32(id); tb.voucher_cr_amount = model.CreditAmount[j]; tb.voucher_narration = model.CrNarration[j]; tb.create_date = DateTime.Now; db.Voucher_Entry_Credit.Add(tb); db.SaveChanges(); } TempData["Perror"] = "Inserted Successfully !"; return(View("Journal")); } catch (Exception e) { TempData["Perror"] = "Please Enter the Details Correctlly !"; return(RedirectToAction("Journal")); //return View(e); } }