public Mansion Get(long id) { return(_mansionRepository.Get(id)); }
public void Generate(long mansionId) { int month = DateTime.UtcNow.Month == 1 ? 12 : DateTime.UtcNow.Month - 1; int year = DateTime.UtcNow.Month == 1 ? DateTime.UtcNow.Year - 1 : DateTime.Now.Year; var mansion = _mansionRepository.Get(mansionId); var apartments = mansion.Apartments; if (!apartments.Any()) { throw new Exception("No apartments on this mansion!"); } var users = mansion.Apartments.Select(x => x.User); var numberOfPersonPerMansion = this.GetNumberOfPersonsPerMansion(apartments); var mansionBills = mansion.Bills.Where(x => x.CreationDate.Value.Month == DateTime.UtcNow.Month && x.CreationDate.Value.Year == DateTime.UtcNow.Year ); CheckElectricityBill(mansionBills); var waterBill = this.GetWaterBill(mansionBills); var billsWithoutWater = mansionBills.Where(x => x.Provider.Type != ProviderType.Water); var totalWaterSent = this.GetTotalWaterSent(users, month, year); var lostWater = waterBill.Units - totalWaterSent; var otherConsumption = mansion.Consumptions.Where(x => x.Date.Value.Month == month && x.Date.Value.Year == year); var linesToExport = new List <Line>(); foreach (var apartment in apartments) { var line = new Line(); line.ApartmentNo = apartment.Number; line.IndividualQuota = apartment.IndividualQuota; line.MembersCount = apartment.MembersCount; var items = new List <Item>(); items.AddRange(this.GetSplitedBills(billsWithoutWater, numberOfPersonPerMansion, apartment)); items.Add(this.GetSplitedWaterBill(waterBill, lostWater, numberOfPersonPerMansion, apartment, month, year)); if (otherConsumption.Any()) { items.AddRange(this.GetSplitedConsumptions(otherConsumption, apartment, numberOfPersonPerMansion)); } line.Items = items; linesToExport.Add(line); } var csv = this.GenerateCsv(linesToExport.OrderBy(x => x.ApartmentNo)); var existingCsvFromDb = _generatedBillRepository.GetAll().FirstOrDefault(x => x.Date.Year == year && x.Date.Month == month && x.MansionId == mansion.UniqueId); if (existingCsvFromDb != null) { existingCsvFromDb.CSV = csv; _generatedBillRepository.Update(existingCsvFromDb); } else { var date = new DateTime(year, month, 1); _generatedBillRepository.Insert(new GeneratedBill { CSV = csv, Date = date, MansionId = mansion.UniqueId }); } }