public ActionResult BillingPopUp(int Id) { AuleaseEntities db = new AuleaseEntities(); //Checking to make sure the component is in the new database or old database Component singleComp = db.Components.Where(n => n.Id == Id).Single(); // The condition checks if any billing charges exist in the new database // If no charges exist then fetch info from old db. if (singleComp.Leases.OrderByDescending(n => n.EndDate).First().Charges.Where(n => n.Type == singleComp.Type).Count() < 1 || singleComp.Leases.OrderByDescending(n => n.EndDate).First().Charges.Where(n => n.Type == singleComp.Type).Single().Tax == null) { var oldGroup = singleComp.Leases.OrderByDescending(n => n.EndDate).First().SystemGroup; LeasingDatabaseMVC.OldAuleaseEntities olddb = new LeasingDatabaseMVC.OldAuleaseEntities(); if (olddb.Billings.Where(n => n.SerialNumber == singleComp.SerialNumber).Count() == 0) { return(View()); } LeasingDatabaseMVC.Billing oldSingleComp = olddb.Billings.Where(n => n.SerialNumber == singleComp.SerialNumber).OrderByDescending(n => n.EndBillDate).First(); ViewBag.Component1 = singleComp.Type.Name; ViewBag.MonthlyCharge1 = oldSingleComp.MonthlyCharge; ViewBag.ComponentCost1 = oldSingleComp.ComponentCost; ViewBag.InsuranceCost1 = oldSingleComp.InsuranceCost; ViewBag.WarrantyCost1 = oldSingleComp.WarrantyCost; ViewBag.ShippingCost1 = 0M; ViewBag.LeasingRate1 = oldSingleComp.LeasingRate; ViewBag.ChargedRate1 = oldSingleComp.ChargedRate; ViewBag.InsuranceRate1 = oldSingleComp.Insurance; ViewBag.Tax1 = oldSingleComp.Tax; ViewBag.IGFRate1 = oldSingleComp.igfRate; return(View()); } var group = db.Components.Where(n => n.Id == Id).Select(n => n.Leases.FirstOrDefault().SystemGroup).Single(); List <Component> comps = group.Leases.Select(n => n.Component).Distinct().ToList(); Component comp = comps.Where(n => n.Type.Name != "Monitor").SingleOrDefault(); List <Component> monitors = comps.Where(n => n.Type.Name == "Monitor").ToList(); int count = comps.Count; if (comp != null) { ViewBag.Component1 = comp.Type.Name; ViewBag.MonthlyCharge1 = Billing.CalculateMonthlyCharge(comp.Leases.OrderByDescending(n => n.EndDate).First().Id); ViewBag.ComponentCost1 = Billing.GetComponentCost(comp.Id); ViewBag.InsuranceCost1 = Billing.GetInsuranceCost(comp.Id); ViewBag.WarrantyCost1 = Billing.GetWarrantyCost(comp.Id); ViewBag.ShippingCost1 = Billing.GetShippingCost(comp.Id); ViewBag.LeasingRate1 = Billing.CalculateLeasingRate(comp.Id); ViewBag.ChargedRate1 = Billing.CalculateChargedRate(comp.Id); ViewBag.InsuranceRate1 = Billing.CalculateSecondaryChargeRate(comp.Id, "Insurance"); ViewBag.WarrantyRate1 = Billing.CalculateSecondaryChargeRate(comp.Id, "Warranty"); ViewBag.ShippingRate1 = Billing.CalculateSecondaryChargeRate(comp.Id, "Shipping"); ViewBag.IGFInsuranceRate1 = Billing.GetIGFInsuranceRate(comp.Id); ViewBag.Tax1 = Billing.CalculateTax(comp.Id); ViewBag.IGFRate1 = Billing.GetIGFRate(comp.Id); ViewBag.Term = comp.Leases.OrderByDescending(n => n.EndDate).FirstOrDefault().Overhead.Term; if (count > 1) { Component mon1 = monitors.OrderBy(n => n.Id).First(); ViewBag.Component2 = mon1.Type.Name; ViewBag.MonthlyCharge2 = Billing.CalculateMonthlyCharge(mon1.Leases.OrderByDescending(n => n.EndDate).First().Id); ViewBag.ComponentCost2 = Billing.GetComponentCost(mon1.Id); ViewBag.InsuranceCost2 = Billing.GetInsuranceCost(mon1.Id); ViewBag.WarrantyCost2 = Billing.GetWarrantyCost(mon1.Id); ViewBag.ShippingCost2 = Billing.GetShippingCost(mon1.Id); ViewBag.LeasingRate2 = Billing.CalculateLeasingRate(mon1.Id); ViewBag.ChargedRate2 = Billing.CalculateChargedRate(mon1.Id); ViewBag.InsuranceRate2 = Billing.CalculateSecondaryChargeRate(mon1.Id, "Insurance"); ViewBag.WarrantyRate2 = Billing.CalculateSecondaryChargeRate(mon1.Id, "Warranty"); ViewBag.ShippingRate2 = Billing.CalculateSecondaryChargeRate(mon1.Id, "Shipping"); ViewBag.IGFInsuranceRate2 = Billing.GetIGFInsuranceRate(mon1.Id); ViewBag.Tax2 = Billing.CalculateTax(mon1.Id); ViewBag.IGFRate2 = Billing.GetIGFRate(mon1.Id); } if (count > 2) { Component mon2 = monitors.OrderBy(n => n.Id).Skip(1).First(); ViewBag.Component3 = mon2.Type.Name; ViewBag.MonthlyCharge3 = Billing.CalculateMonthlyCharge(mon2.Leases.OrderByDescending(n => n.EndDate).First().Id); ViewBag.ComponentCost3 = Billing.GetComponentCost(mon2.Id); ViewBag.InsuranceCost3 = Billing.GetInsuranceCost(mon2.Id); ViewBag.WarrantyCost3 = Billing.GetWarrantyCost(mon2.Id); ViewBag.ShippingCost3 = Billing.GetShippingCost(mon2.Id); ViewBag.LeasingRate3 = Billing.CalculateLeasingRate(mon2.Id); ViewBag.ChargedRate3 = Billing.CalculateChargedRate(mon2.Id); ViewBag.InsuranceRate3 = Billing.CalculateSecondaryChargeRate(mon2.Id, "Insurance"); ViewBag.WarrantyRate3 = Billing.CalculateSecondaryChargeRate(mon2.Id, "Warranty"); ViewBag.ShippingRate3 = Billing.CalculateSecondaryChargeRate(mon2.Id, "Shipping"); ViewBag.IGFInsuranceRate3 = Billing.GetIGFInsuranceRate(mon2.Id); ViewBag.Tax3 = Billing.CalculateTax(mon2.Id); ViewBag.IGFRate3 = Billing.GetIGFRate(mon2.Id); } } else { if (count > 0) { Component mon1 = monitors.OrderBy(n => n.Id).First(); ViewBag.Component1 = mon1.Type.Name; ViewBag.MonthlyCharge1 = Billing.CalculateMonthlyCharge(mon1.Leases.OrderByDescending(n => n.EndDate).First().Id); ViewBag.ComponentCost1 = Billing.GetComponentCost(mon1.Id); ViewBag.InsuranceCost1 = Billing.GetInsuranceCost(mon1.Id); ViewBag.WarrantyCost1 = Billing.GetWarrantyCost(mon1.Id); ViewBag.ShippingCost1 = Billing.GetShippingCost(mon1.Id); ViewBag.LeasingRate1 = Billing.CalculateLeasingRate(mon1.Id); ViewBag.ChargedRate1 = Billing.CalculateChargedRate(mon1.Id); ViewBag.InsuranceRate1 = Billing.CalculateSecondaryChargeRate(mon1.Id, "Insurance"); ViewBag.WarrantyRate1 = Billing.CalculateSecondaryChargeRate(mon1.Id, "Warranty"); ViewBag.ShippingRate1 = Billing.CalculateSecondaryChargeRate(mon1.Id, "Shipping"); ViewBag.IGFInsuranceRate1 = Billing.GetIGFInsuranceRate(mon1.Id); ViewBag.Tax1 = Billing.CalculateTax(mon1.Id); ViewBag.IGFRate1 = Billing.GetIGFRate(mon1.Id); } if (count > 1) { Component mon2 = monitors.OrderBy(n => n.Id).Skip(1).First(); ViewBag.Component2 = mon2.Type.Name; ViewBag.MonthlyCharge2 = Billing.CalculateMonthlyCharge(mon2.Leases.OrderByDescending(n => n.EndDate).First().Id); ViewBag.ComponentCost2 = Billing.GetComponentCost(mon2.Id); ViewBag.InsuranceCost2 = Billing.GetInsuranceCost(mon2.Id); ViewBag.WarrantyCost2 = Billing.GetWarrantyCost(mon2.Id); ViewBag.ShippingCost2 = Billing.GetShippingCost(mon2.Id); ViewBag.LeasingRate2 = Billing.CalculateLeasingRate(mon2.Id); ViewBag.ChargedRate2 = Billing.CalculateChargedRate(mon2.Id); ViewBag.InsuranceRate2 = Billing.CalculateSecondaryChargeRate(mon2.Id, "Insurance"); ViewBag.WarrantyRate2 = Billing.CalculateSecondaryChargeRate(mon2.Id, "Warranty"); ViewBag.ShippingRate2 = Billing.CalculateSecondaryChargeRate(mon2.Id, "Shipping"); ViewBag.IGFInsuranceRate2 = Billing.GetIGFInsuranceRate(mon2.Id); ViewBag.Tax2 = Billing.CalculateTax(mon2.Id); ViewBag.IGFRate2 = Billing.GetIGFRate(mon2.Id); } } return(View()); }