122 lines
4.0 KiB
C#
122 lines
4.0 KiB
C#
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
|
|
{
|
|
IEnumerable<AccountRecord>? foundRecs = null;
|
|
private class AccCount
|
|
{
|
|
public AccountRecord? AcRec { get; set; }
|
|
public int NoOfRecs { get; set; }
|
|
public double SearchedSum { get; set; }
|
|
}
|
|
private readonly IAccountRecordRepository _accountRecordRepository;
|
|
private readonly frmPayments _showPayments;
|
|
|
|
public frmSearchData(IAccountRecordRepository accountRecordRepository, frmPayments showPayments)
|
|
{
|
|
InitializeComponent();
|
|
_accountRecordRepository = accountRecordRepository;
|
|
_showPayments = showPayments;
|
|
}
|
|
|
|
private void btnClose_Click(object sender, EventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
|
|
private void btnStartSearch_Click(object sender, EventArgs e)
|
|
{
|
|
double SearchedSum = 0;
|
|
flpPanel1.Controls.Clear();
|
|
foundRecs = _accountRecordRepository.GetAllAccBetweenDates(dtpFrom.Value, dtpTo.Value);
|
|
if (foundRecs.Any())
|
|
{
|
|
var aggregates = aggegateAccounts(foundRecs);
|
|
foreach (var account in aggregates)
|
|
{
|
|
var ucCustV = createAndFillUCcv(account);
|
|
SearchedSum += Convert.ToDouble(ucCustV.Amount);
|
|
flpPanel1.Controls.Add(ucCustV);
|
|
}
|
|
txtSoekSumma.Text=SearchedSum.ToString();
|
|
}
|
|
else
|
|
{
|
|
flpPanel1.Controls.Clear();
|
|
MessageBox.Show("Din sökning gav ingen träff");
|
|
}
|
|
}
|
|
|
|
private ucCustomerValue createAndFillUCcv(AccCount account)
|
|
{
|
|
var result = new ucCustomerValue(this);
|
|
result.Amount = String.Format("{0:0.00}", account.SearchedSum).adjustRight();
|
|
result.Receiver = account.AcRec.Mottagare;
|
|
result.Number = Convert.ToString(account.NoOfRecs);
|
|
result.Tag = account.AcRec.Mottagare;
|
|
return result;
|
|
}
|
|
|
|
private IEnumerable<AccCount> aggegateAccounts(IEnumerable<AccountRecord> result)
|
|
{
|
|
List<AccCount> Localresult = new List<AccCount>();
|
|
string oldMott = string.Empty;
|
|
AccCount wrkRec = null;
|
|
foreach (var accountRecord in result)
|
|
{
|
|
if (accountRecord.Mottagare == oldMott)
|
|
{
|
|
wrkRec.SearchedSum += 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.AcRec = accountRecord;
|
|
acc.SearchedSum = accountRecord.Belopp;
|
|
return acc;
|
|
}
|
|
|
|
public void ShowDetails(string mott)
|
|
{
|
|
IEnumerable<AccountRecord> detailRecs = foundRecs.Where(fr => fr.Mottagare == mott);
|
|
_showPayments.EnableDetailShow = true;
|
|
_showPayments.CustomPayments = detailRecs;
|
|
_showPayments.ShowDialog();
|
|
}
|
|
}
|
|
}
|