using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WinFormDi.UserControls; using WinFormDiApp.BL.Helpers; using WinFormDiApp.BL.Models; using WinFormDiApp.BLI; namespace WinFormDiApp { public partial class frmSearchData : Form { private class AccCount : AccountRecord { public int NoOfRecs { get; set; } } private readonly IAccountRecordRepository _accountRecordRepository; public frmSearchData(IAccountRecordRepository accountRecordRepository) { InitializeComponent(); _accountRecordRepository = accountRecordRepository; } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void btnStartSearch_Click(object sender, EventArgs e) { flpPanel1.Controls.Clear(); var result = _accountRecordRepository.GetAllAccBetweenDates(dtpFrom.Value, dtpTo.Value); var aggregates = aggegateAccounts(result); foreach (var account in aggregates) { var ucCustV = createAndFillUCcv(account); flpPanel1.Controls.Add(ucCustV); } } private ucCustomerValue createAndFillUCcv(AccCount account) { var result = new ucCustomerValue(); result.Amount = String.Format("{0:0.00}", account.Belopp).adjustRight(); result.Receiver = account.Mottagare; result.Number = Convert.ToString(account.NoOfRecs); return result; } private IEnumerable aggegateAccounts(IEnumerable result) { List Localresult = new List(); string oldMott = string.Empty; AccCount wrkRec = null; foreach (var accountRecord in result) { if (accountRecord.Mottagare == oldMott) { wrkRec.Belopp += accountRecord.Belopp; wrkRec.NoOfRecs++; } else { oldMott = accountRecord.Mottagare; if (wrkRec != null) { Localresult.Add(wrkRec); wrkRec = AddAccountRecord(accountRecord); } else { wrkRec = AddAccountRecord(accountRecord); } } } Localresult.Add(wrkRec); return Localresult; } private AccCount AddAccountRecord(AccountRecord accountRecord) { AccCount acc = new AccCount(); acc.NoOfRecs = 1; acc.BetalDatum = accountRecord.BetalDatum; acc.Stored = accountRecord.Stored; acc.Mottagare = accountRecord.Mottagare; acc.Konto = accountRecord.Konto; acc.Avisering = accountRecord.Avisering; acc.Belopp = accountRecord.Belopp; acc.Id = accountRecord.Id; return acc; } } }