public async Task <CompanyDto> UpdateCompany(long id, CompanyDto dto) { var entity = await _companyManager.Find(id); if (entity == null) { return(null); } var newEntity = _mapper.Map(dto, entity); entity = await _companyManager.Update(newEntity); return(_mapper.Map <CompanyDto>(entity)); }
public async Task <AgingReportResultDto> GetAgingReport(long companyId, DateTime dateTo, int daysPerPeriod, int numberOfPeriods, bool includeAllCustomers = true) { var company = await _companyManager.Find(companyId); if (company == null) { return(null); } var allCustomers = await _customerManager.FindByCompanyId(companyId, dateTo); var customers = allCustomers.Where(x => x.Activities.IsActive(dateTo)).ToList(); var invoices = await _reportManager.GetAgingInvoices(companyId, dateTo, daysPerPeriod, numberOfPeriods); //invoices = invoices.Where(x => x.Customer?.Activities.IsActive(dateTo)).ToList(); //check invoices for Inactive customers if (includeAllCustomers) { //Добавить всех недостающих Customers var exceptedCustomers = customers.Where(x => !invoices.Any(y => y.CustomerId == x.Id)).ToList(); var expectedInvoices = exceptedCustomers.Select(x => new InvoiceEntity() { Customer = x, CustomerId = x.Id, Company = company, CompanyId = company.Id }); invoices.AddRange(expectedInvoices); invoices.OrderBy(x => x.CustomerAccountNumber); } #region CREATE HEADERS var _col = new List <AgingReportColsDto>() { }; for (int i = -1; i < numberOfPeriods; i++) { var from = (i < 0 ? -1 : 1) + i * daysPerPeriod; var to = (i + 1) * daysPerPeriod; _col.Add(new AgingReportColsDto() { From = from, To = to, Name = $"{from}-{to}" }); } _col.Add(new AgingReportColsDto() { To = 10000, From = 1 + numberOfPeriods * daysPerPeriod, Name = $"{1 + numberOfPeriods * daysPerPeriod}+" }); _col.Add(new AgingReportColsDto() { Name = "Total Late" }); _col.Add(new AgingReportColsDto() { Name = "Total" }); #endregion var report = new Dictionary <long, AgingReportRowDto>(); foreach (var invoice in invoices) { var recordKey = invoice.CustomerId ?? 0; var invoiceAmount = invoice.Subtotal * (1 + invoice.TaxRate / 100); //get total amount var diffPay = invoiceAmount - (invoice.Payments?.Sum(x => x.Amount) ?? 0); if (diffPay > 0 || includeAllCustomers) { if (!report.ContainsKey(recordKey)) { report.Add(recordKey, new AgingReportRowDto() { Customer = _mapper.Map <CustomerDto>(invoice.Customer), CustomerName = invoice.Customer.Name, AccountNo = invoice.Customer.No, Data = new Dictionary <string, decimal>() }); } var summary = report[recordKey].Data; foreach (var c in _col) { if (!summary.ContainsKey(c.Name)) { summary.Add(c.Name, 0); } } if (diffPay > 0) { var diffDays = (dateTo - invoice.DueDate).Days; foreach (var col in _col) { if (diffDays <= 0) { summary[col.Name] += diffPay; break; } else if (diffDays >= col.From && diffDays <= col.To) { summary[col.Name] += diffPay; break; } } } } else { Console.WriteLine($"{invoice.CustomerId} Pay more that should {diffPay}"); } } foreach (var d in report) { d.Value.Data["Total"] = d.Value.Data.Sum(x => x.Value); } #region BALANCE var balanceTotal = new Dictionary <string, AgingReportBalanceDto>(); foreach (var c in _col) { if (c.Name.Equals("Total")) { balanceTotal.Add("Total", new AgingReportBalanceDto { Count = balanceTotal.Values.Sum(x => x.Count), Sum = report.Values.Sum(x => x.Data["Total"]) }); } else if (c.Name.Equals("Total Late")) { } else { balanceTotal.Add(c.Name, new AgingReportBalanceDto { Count = report.Values.Count(x => x.Data[c.Name] != 0), Sum = report.Values.Sum(x => x.Data[c.Name]) }); } } balanceTotal.Add("Total Late", new AgingReportBalanceDto { Count = balanceTotal["Total"].Count - balanceTotal["-31-0"].Count, Sum = balanceTotal["Total"].Sum - balanceTotal["-31-0"].Sum }); #endregion #region DEBT - DOUBLE INVOICE var doubleDebt = new Dictionary <string, decimal>(); foreach (var data in report.Values) { var debt = data.Data.Where(x => !x.Key.Equals("Total") && x.Value > 0).ToList(); if (debt.Count > 1) { var keyName = string.Join(',', debt.Select(x => x.Key)); var debtCount = debt.Count(x => x.Value > 0); if (!doubleDebt.ContainsKey(keyName)) { doubleDebt.Add(keyName, 0); } doubleDebt[keyName] += 1; } } #endregion #region CUSTOMER TYPES var customerTypes = customers.GroupBy(x => x.Type == null ? "No types" : x.Type.Name).ToDictionary(x => x.Key, x => x.Count()); #endregion #region TOTAL CUSTOMERS var balanceCustomer = report.Count(x => x.Value.Data["Total"] != 0); var customerTotal = new Dictionary <string, int>(); customerTotal.Add("Total Customers", customers.Count); customerTotal.Add("Balance", balanceCustomer); customerTotal.Add("No Balance", customers.Count - balanceCustomer); #endregion return(new AgingReportResultDto() { CompanyId = companyId, CompanyName = company.Name, Date = dateTo, NumberOfPeriods = numberOfPeriods, Cols = _col, Rows = report.Select(x => x.Value).ToList(), CustomerTotal = customerTotal, CustomerTypes = customerTypes, BalanceTotal = balanceTotal, DoubleDebt = doubleDebt }); }