public void EditRows() { var gridModel = new LeasingDatabase.Models.Grid.OrdersJqGridModel(); var db = new AuleaseEntities(); var e = gridModel.OrdersGrid.GetEditData(); // Edit Row if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.EditRow) { int Term = Convert.ToInt32(e.RowData["Term"]); string Type = e.RowData["RateType"].ToString(); decimal Rate = Convert.ToDecimal(e.RowData["Rate"]); VendorRate lastRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == Type).OrderByDescending(n => n.BeginDate).First(); VendorRate newRate = new VendorRate(); newRate.Type = lastRate.Type; newRate.Term = lastRate.Term; newRate.BeginDate = DateTime.Now; newRate.Rate = Rate; db.VendorRates.Add(newRate); db.SaveChanges(); } if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.AddRow) { int Term = Convert.ToInt32(e.RowData["Term"]); string Type = e.RowData["RateType"].ToString(); decimal Rate = Convert.ToDecimal(e.RowData["Rate"]); VendorRate newRate = new VendorRate(); newRate.Type = db.Types.Where(n => n.Name == Type).Single(); newRate.Term = Term; newRate.BeginDate = DateTime.Now; newRate.Rate = Rate; db.VendorRates.Add(newRate); db.SaveChanges(); } if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.DeleteRow) { // Not gonna allow deleting of rates throw new NotImplementedException(); } }
public static void FillLeasingInfo(int SystemGroupID, string BegBillDate, string EndBillDate, string CPUCost, string MonitorCost, string Monitor2Cost, string InsuranceCost, string WarrantyCost, string ShippingCost) { AuleaseEntities db = new AuleaseEntities(); SystemGroup system = db.SystemGroups.Where(n => n.Id == SystemGroupID).Single(); //Remove existing charges before adding new ones foreach (var lease in system.Leases) { List <Charge> charges = lease.Charges.ToList(); foreach (var charge in charges) { lease.Charges.Remove(charge); } foreach (var charge in db.Charges.Where(n => n.Leases.FirstOrDefault().Id == lease.Id)) { db.Entry(charge).State = EntityState.Deleted; } } db.SaveChanges(); //Add new charges int compCount = system.Leases.Count; List <Component> MainComps = system.Components.Where(n => n.Type.Name != "Monitor").ToList(); List <Component> Monitors = system.Components.Where(n => n.Type.Name == "Monitor").ToList(); foreach (var comp in MainComps) { Lease currentLease = comp.CurrentLease; currentLease.BeginDate = Convert.ToDateTime(BegBillDate); currentLease.EndDate = Convert.ToDateTime(EndBillDate); int Term = comp.Leases.FirstOrDefault().Overhead.Term + 1; // Add 1 for vendor rates VendorRate VendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == comp.Type.Name).OrderByDescending(n => n.BeginDate).FirstOrDefault(); Charge MainCharge = new Charge { Price = Convert.ToDecimal(CPUCost), VendorRate = VendorRate, Type = comp.Type, Tax = db.Taxes.OrderByDescending(n => n.Timestamp).First() }; currentLease.Charges.Add(MainCharge); if (!String.IsNullOrWhiteSpace(InsuranceCost)) { VendorRate InsuranceVendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == "Insurance").OrderByDescending(n => n.BeginDate).FirstOrDefault(); Charge InsuranceCharge = new Charge { Price = Convert.ToDecimal(InsuranceCost), VendorRate = InsuranceVendorRate, Type = db.TypesInsurance }; currentLease.Charges.Add(InsuranceCharge); } if (!String.IsNullOrWhiteSpace(WarrantyCost)) { VendorRate WarrantyVendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == "Insurance").OrderByDescending(n => n.BeginDate).FirstOrDefault(); Charge WarrantyCharge = new Charge { Price = Convert.ToDecimal(WarrantyCost) / compCount, VendorRate = WarrantyVendorRate, Type = db.TypesWarranty }; currentLease.Charges.Add(WarrantyCharge); } if (!String.IsNullOrWhiteSpace(ShippingCost)) { VendorRate ShippingVendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == "Insurance").OrderByDescending(n => n.BeginDate).FirstOrDefault(); Charge ShippingCharge = new Charge { Price = Convert.ToDecimal(ShippingCost) / compCount, VendorRate = ShippingVendorRate, Type = db.TypesShipping }; currentLease.Charges.Add(ShippingCharge); } } int i = 0; foreach (var comp in Monitors) { Lease currentLease = comp.CurrentLease; currentLease.BeginDate = Convert.ToDateTime(BegBillDate); currentLease.EndDate = Convert.ToDateTime(EndBillDate); int Term = comp.Leases.FirstOrDefault().Overhead.Term + 1; // Add 1 for vendor rates VendorRate VendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == comp.Type.Name).OrderByDescending(n => n.BeginDate).FirstOrDefault(); Charge MainCharge; if (i == 0) { MainCharge = new Charge { Price = Convert.ToDecimal(MonitorCost), VendorRate = VendorRate, Type = comp.Type, Tax = db.Taxes.OrderByDescending(n => n.Timestamp).First() }; } else { MainCharge = new Charge { Price = Convert.ToDecimal(Monitor2Cost), VendorRate = VendorRate, Type = comp.Type, Tax = db.Taxes.OrderByDescending(n => n.Timestamp).First() }; } i++; currentLease.Charges.Add(MainCharge); if (!String.IsNullOrWhiteSpace(InsuranceCost) && MainComps.Count == 0) { VendorRate InsuranceVendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == "Insurance").OrderByDescending(n => n.BeginDate).FirstOrDefault(); Charge InsuranceCharge = new Charge { Price = Convert.ToDecimal(InsuranceCost), VendorRate = InsuranceVendorRate, Type = db.TypesInsurance }; currentLease.Charges.Add(InsuranceCharge); } if (!String.IsNullOrWhiteSpace(WarrantyCost)) { VendorRate WarrantyVendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == "Insurance").OrderByDescending(n => n.BeginDate).FirstOrDefault(); Charge WarrantyCharge = new Charge { Price = Convert.ToDecimal(WarrantyCost) / compCount, VendorRate = WarrantyVendorRate, Type = db.TypesWarranty }; currentLease.Charges.Add(WarrantyCharge); } if (!String.IsNullOrWhiteSpace(ShippingCost)) { VendorRate ShippingVendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == "Insurance").OrderByDescending(n => n.BeginDate).FirstOrDefault(); Charge ShippingCharge = new Charge { Price = Convert.ToDecimal(ShippingCost) / compCount, VendorRate = ShippingVendorRate, Type = db.TypesShipping }; currentLease.Charges.Add(ShippingCharge); } } db.SaveChanges(); }