// GET: Demands/Create public ActionResult Create(int?id) { // ViewBag.YearBox = MyExtensions.MakeYrRq(1, 1, DateTime.Today.Year); //Depending on where this is called from, set the Citizen int CitID = id ?? 0; ViewBag.CitName = (CitID > 0) ? db.Citizens.Find(CitID).Name : ""; DemandInitLoad dil = new DemandInitLoad { CitizenID = CitID }; ViewBag.PeriodID = new SelectList(db.PeriodSLs, "PeriodID", "PeriodSL1"); return(View(dil)); }
public ActionResult Create([Bind(Include = "DemandID,CitizenID,PeriodID, HouseNo, SubLedgerID, Amt, Remarks")] DemandInitLoad demand, FormCollection fm) { if (ModelState.IsValid) { Demand dmdID; //If the Demand record doesnt exist create it. demand.HouseNo = demand.HouseNo.Trim().ToUpper(); //it is alphanumeric. To Upper avoids duplications due to lowercase if (db.DemandDetails.Any(dde => dde.Demand.CitizenID == demand.CitizenID && dde.Demand.HouseNo == demand.HouseNo && dde.SubLedgerID == demand.SubLedgerID)) { throw new EAshowstopper("You are attempting to enter a duplicate record. Save aborted."); } using (var dbt = db.Database.BeginTransaction()) { try { if (db.Demands.Any(d => d.CitizenID == demand.CitizenID && d.HouseNo == demand.HouseNo)) { dmdID = db.Demands.FirstOrDefault(d => d.CitizenID == demand.CitizenID && d.HouseNo == demand.HouseNo); //If remarks are updated save it, else leave existing remarks if (demand.Remarks?.Length > 0) { dmdID.Remarks = demand.Remarks; db.Entry(dmdID).State = EntityState.Modified; } } else { dmdID = new Demand { CitizenID = demand.CitizenID, HouseNo = demand.HouseNo, Remarks = demand.Remarks }; dmdID.StopDate = DateTime.Parse("31-Mar-3999"); dmdID.CreatedOn = DateTime.Today; db.Demands.Add(dmdID); } db.SaveChanges(); //Add the Demand Details Record var dd = db.DemandDetails.Add(new DemandDetail { DemandID = dmdID.DemandID, SubLedgerID = (int)demand.SubLedgerID }); //Add the Demand Period record. Our PK will scream if duplicate. since this is a new record initialise the SysAmt too var demP = db.DemandPeriods.Add(new DemandPeriod { DDID = dd.DDID, Amount = demand.Amt, SysAmt = demand.Amt, PeriodID = GetCurrentPeriod() }); db.DemandYears.Add(new DemandYear { DDID = dd.DDID, DemandYear1 = (System.DateTime.Today.Month > 3) ? System.DateTime.Today.Year : System.DateTime.Today.Year - 1, PeriodID = GetCurrentPeriod() }); foreach (var item in fm) { if (item.ToString().Length > 5 && item.ToString().Substring(0, 5) == "data-") { int iLedDetID = int.Parse(item.ToString().Substring(item.ToString().LastIndexOf('-') + 1)); //Prevent overposting attack by checking that only valid LedgerDetails for the given subledger are saved if (db.LedgerDetails.Any(l => l.LedgerDetailID == iLedDetID && l.SubLedgerID == demand.SubLedgerID)) { db.DemandLedgerDetails.Add(new DemandLedgerDetail { DDID = dd.DDID, LedgerDetailID = iLedDetID, DemandLedgerDetail1 = fm[item.ToString()] }); } } } db.SaveChanges(); dbt.Commit(); return(RedirectToAction("Create", new { id = dmdID.CitizenID })); } catch (Exception) { dbt.Rollback(); } } } return(RedirectToAction("Create")); }