From 374d401692b2476cb210c97fc1e5fe183acafecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Thu, 24 Aug 2023 23:00:16 +0200 Subject: [PATCH] New vue for reading in + some new repositories --- WinFormDi.BLI/IReadingIn.cs | 10 ++ WinFormDi.BLR/ReadingIn.cs | 131 +++++++++++++++ WinFormDi.BLR/WinFormDiApp.BLR.csproj | 4 + WinFormDi/ContainerConfig.cs | 1 + WinFormDi/MainWindow.Designer.cs | 13 ++ WinFormDi/MainWindow.cs | 13 +- WinFormDi/WinFormDi.csproj | 6 + WinFormDi/frmReadPayments.Designer.cs | 219 ++++++++++++++++++++++++++ WinFormDi/frmReadPayments.cs | 46 ++++++ WinFormDi/frmReadPayments.resx | 123 +++++++++++++++ 10 files changed, 565 insertions(+), 1 deletion(-) create mode 100644 WinFormDi.BLI/IReadingIn.cs create mode 100644 WinFormDi.BLR/ReadingIn.cs create mode 100644 WinFormDi/frmReadPayments.Designer.cs create mode 100644 WinFormDi/frmReadPayments.cs create mode 100644 WinFormDi/frmReadPayments.resx diff --git a/WinFormDi.BLI/IReadingIn.cs b/WinFormDi.BLI/IReadingIn.cs new file mode 100644 index 0000000..039a305 --- /dev/null +++ b/WinFormDi.BLI/IReadingIn.cs @@ -0,0 +1,10 @@ +using WinFormDiApp.BL.Models; + +namespace WinFormDiApp.BLI +{ + public interface IReadingIn + { + bool ReadAndSaveInvoices(string fullFileName); + IEnumerable readXLS(string FilePath); + } +} \ No newline at end of file diff --git a/WinFormDi.BLR/ReadingIn.cs b/WinFormDi.BLR/ReadingIn.cs new file mode 100644 index 0000000..ba05346 --- /dev/null +++ b/WinFormDi.BLR/ReadingIn.cs @@ -0,0 +1,131 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using WinFormDiApp.BL.Helpers; +using WinFormDiApp.BLI; +using OfficeOpenXml; +using System.Runtime.InteropServices; +using WinFormDiApp.BL.Models; +//using Excel = Microsoft.Office.Interop.Excel; + + +namespace WinFormDiApp.BLR; + +public class ReadingIn : IReadingIn +{ + private readonly IConfiguration _configuration; + private readonly ILogger _logger; + private readonly IAccountRecordRepository _accountRecordRepository; + + public ReadingIn( + IConfiguration configuration, + ILogger logger, + IAccountRecordRepository accountRecordRepository) + { + _configuration = configuration; + _logger = logger; + _accountRecordRepository = accountRecordRepository; + } + + // @"C:\dev\MyYearlyCountings\TransactionsTest.xls" + + public IEnumerable readXLS(string FilePath) + { + List records = new List(); + AccountRecord? record = null; + FileInfo existingFile = new FileInfo(FilePath); + using (ExcelPackage package = new ExcelPackage(existingFile)) + { + //get the first worksheet in the workbook + ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; + int colCount = worksheet.Dimension.End.Column; //get Column Count + int rowCount = worksheet.Dimension.End.Row; //get row count + bool prt = false; + for (int row = 1; row <= rowCount; row++) + { + if (prt) + { + //Console.WriteLine(); + _logger.LogInformation(""); + records.Add(record); + } + prt = false; + for (int col = 1; col <= colCount; col++) + { + if (worksheet.Cells[row, col].Value == null) + { + // Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + "null"); + } + else + { + if (col == 1 && worksheet.Cells[row, col].Value.ToString().IsDate()) + { + prt = true; + record = new AccountRecord(); + } + //Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim()); + if (prt) + { + _logger.LogInformation($"{worksheet.Cells[row, col].Value.ToString().Trim()}/t"); + switch (col) + { + case 1: + { + record.BetalDatum = DateTime.Parse( worksheet.Cells[row, col].Value.ToString().Trim()); + break; + } + case 3: + { + record.Mottagare = worksheet.Cells[row, col].Value.ToString().Trim(); + break; + } + case 5: + { + record.Konto = worksheet.Cells[row, col].Value.ToString().Trim(); + break; + } + case 7: + { + record.Belopp = double.Parse(worksheet.Cells[row, col].Value.ToString().Trim()); + break; + } + case 9: + { + record.Avisering = worksheet.Cells[row, col].Value.ToString().Trim(); + break; + } + + } + } + } + } + } + } + return records; + } + public bool ReadAndSaveInvoices(string fullFileName) + { + var result = true; + var restab = readXLS(fullFileName); + if (restab != null) + { + try + { + + restab.ToList().ForEach(x => + { + _accountRecordRepository.AddAccountRecord(x); + }); + + // restab.ToList().ForEach(x => { _accountRecordRepository.AddAccountRecord(x); }); + } + catch (Exception ex) + { + _logger.LogError($"MassUppdatering misslyckat: {ex.Message}"); + result = false; + } + } + + return result; + } + +} diff --git a/WinFormDi.BLR/WinFormDiApp.BLR.csproj b/WinFormDi.BLR/WinFormDiApp.BLR.csproj index 25851ce..f30997f 100644 --- a/WinFormDi.BLR/WinFormDiApp.BLR.csproj +++ b/WinFormDi.BLR/WinFormDiApp.BLR.csproj @@ -11,5 +11,9 @@ + + + + diff --git a/WinFormDi/ContainerConfig.cs b/WinFormDi/ContainerConfig.cs index 9a116d1..65b56a4 100644 --- a/WinFormDi/ContainerConfig.cs +++ b/WinFormDi/ContainerConfig.cs @@ -40,6 +40,7 @@ namespace WinFormDiApp .AddTransient() .AddTransient() .AddTransient() + .AddTransient() .AddTransient(); }); diff --git a/WinFormDi/MainWindow.Designer.cs b/WinFormDi/MainWindow.Designer.cs index 30c87d5..8f873e0 100644 --- a/WinFormDi/MainWindow.Designer.cs +++ b/WinFormDi/MainWindow.Designer.cs @@ -31,6 +31,7 @@ helloText = new Label(); goodbyeText = new Label(); btnCheckPayments = new Button(); + btnLoadPayments = new Button(); SuspendLayout(); // // helloText @@ -63,11 +64,22 @@ btnCheckPayments.UseVisualStyleBackColor = true; btnCheckPayments.Click += btnCheckPayments_Click; // + // btnLoadPayments + // + btnLoadPayments.Location = new Point(73, 75); + btnLoadPayments.Name = "btnLoadPayments"; + btnLoadPayments.Size = new Size(156, 23); + btnLoadPayments.TabIndex = 3; + btnLoadPayments.Text = "Ladda betalningar"; + btnLoadPayments.UseVisualStyleBackColor = true; + btnLoadPayments.Click += btnLoadPayments_Click; + // // MainWindow // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(btnLoadPayments); Controls.Add(btnCheckPayments); Controls.Add(goodbyeText); Controls.Add(helloText); @@ -84,5 +96,6 @@ private Label helloText; private Label goodbyeText; private Button btnCheckPayments; + private Button btnLoadPayments; } } \ No newline at end of file diff --git a/WinFormDi/MainWindow.cs b/WinFormDi/MainWindow.cs index 3700827..7c2f621 100644 --- a/WinFormDi/MainWindow.cs +++ b/WinFormDi/MainWindow.cs @@ -15,12 +15,18 @@ namespace WinFormDiApp { private readonly IMessages _messages; private readonly frmPayments _payments; + private readonly frmReadPayments _readPayments; - public MainWindow(IMessages messages, frmPayments payments) + public MainWindow( + IMessages messages, + frmPayments payments, + frmReadPayments readPayments + ) { InitializeComponent(); _messages = messages; _payments = payments; + _readPayments = readPayments; } private void MainWindow_Load(object sender, EventArgs e) @@ -38,5 +44,10 @@ namespace WinFormDiApp { _payments.ShowDialog(); } + + private void btnLoadPayments_Click(object sender, EventArgs e) + { + _readPayments.ShowDialog(); + } } } diff --git a/WinFormDi/WinFormDi.csproj b/WinFormDi/WinFormDi.csproj index 7ce132a..1670fdd 100644 --- a/WinFormDi/WinFormDi.csproj +++ b/WinFormDi/WinFormDi.csproj @@ -27,6 +27,12 @@ + + + Form + + + Always diff --git a/WinFormDi/frmReadPayments.Designer.cs b/WinFormDi/frmReadPayments.Designer.cs new file mode 100644 index 0000000..0728f06 --- /dev/null +++ b/WinFormDi/frmReadPayments.Designer.cs @@ -0,0 +1,219 @@ +namespace WinFormDiApp +{ + partial class frmReadPayments + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + lvPayments = new ListView(); + ch1_Id = new ColumnHeader(); + ch2_Mottagare = new ColumnHeader(); + ch3_Konto = new ColumnHeader(); + ch4_Belopp = new ColumnHeader(); + ch5_Förfallodag = new ColumnHeader(); + ch6_Avisering = new ColumnHeader(); + btnClose = new Button(); + listView1 = new ListView(); + columnHeader1 = new ColumnHeader(); + columnHeader2 = new ColumnHeader(); + columnHeader3 = new ColumnHeader(); + columnHeader4 = new ColumnHeader(); + columnHeader5 = new ColumnHeader(); + columnHeader6 = new ColumnHeader(); + btnChooseFile = new Button(); + ofChooseFile = new OpenFileDialog(); + lblTransFileName = new Label(); + btnStartRead = new Button(); + SuspendLayout(); + // + // lvPayments + // + lvPayments.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + lvPayments.BackColor = Color.FromArgb(192, 255, 255); + lvPayments.Columns.AddRange(new ColumnHeader[] { ch1_Id, ch2_Mottagare, ch3_Konto, ch4_Belopp, ch5_Förfallodag, ch6_Avisering }); + lvPayments.Location = new Point(28, 55); + lvPayments.Name = "lvPayments"; + lvPayments.Size = new Size(805, 286); + lvPayments.TabIndex = 0; + lvPayments.UseCompatibleStateImageBehavior = false; + lvPayments.View = View.Details; + // + // ch1_Id + // + ch1_Id.Text = "Id"; + // + // ch2_Mottagare + // + ch2_Mottagare.Text = "Mottagare"; + ch2_Mottagare.Width = 120; + // + // ch3_Konto + // + ch3_Konto.Text = "Konto"; + ch3_Konto.Width = 100; + // + // ch4_Belopp + // + ch4_Belopp.Text = "Belopp"; + ch4_Belopp.Width = 100; + // + // ch5_Förfallodag + // + ch5_Förfallodag.Text = "Förfallodag"; + ch5_Förfallodag.Width = 100; + // + // ch6_Avisering + // + ch6_Avisering.Text = "Avisering"; + ch6_Avisering.Width = 100; + // + // btnClose + // + btnClose.Location = new Point(758, 418); + btnClose.Name = "btnClose"; + btnClose.Size = new Size(75, 23); + btnClose.TabIndex = 1; + btnClose.Text = "Stäng"; + btnClose.UseVisualStyleBackColor = true; + btnClose.Click += btnClose_Click; + // + // listView1 + // + listView1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + listView1.BackColor = Color.FromArgb(192, 255, 255); + listView1.Columns.AddRange(new ColumnHeader[] { columnHeader1, columnHeader2, columnHeader3, columnHeader4, columnHeader5, columnHeader6 }); + listView1.Location = new Point(28, 107); + listView1.Name = "listView1"; + listView1.Size = new Size(805, 286); + listView1.TabIndex = 2; + listView1.UseCompatibleStateImageBehavior = false; + listView1.View = View.Details; + // + // columnHeader1 + // + columnHeader1.Text = "Id"; + // + // columnHeader2 + // + columnHeader2.Text = "Mottagare"; + columnHeader2.Width = 120; + // + // columnHeader3 + // + columnHeader3.Text = "Konto"; + columnHeader3.Width = 100; + // + // columnHeader4 + // + columnHeader4.Text = "Belopp"; + columnHeader4.Width = 100; + // + // columnHeader5 + // + columnHeader5.Text = "Förfallodag"; + columnHeader5.Width = 100; + // + // columnHeader6 + // + columnHeader6.Text = "Avisering"; + columnHeader6.Width = 100; + // + // btnChooseFile + // + btnChooseFile.Location = new Point(30, 28); + btnChooseFile.Name = "btnChooseFile"; + btnChooseFile.Size = new Size(75, 23); + btnChooseFile.TabIndex = 3; + btnChooseFile.Text = "Välj infil"; + btnChooseFile.UseVisualStyleBackColor = true; + btnChooseFile.Click += btnChooseFile_Click; + // + // ofChooseFile + // + ofChooseFile.InitialDirectory = "DownLoads"; + // + // lblTransFileName + // + lblTransFileName.AutoSize = true; + lblTransFileName.Font = new Font("Segoe UI", 12F, FontStyle.Bold, GraphicsUnit.Point); + lblTransFileName.Location = new Point(125, 27); + lblTransFileName.Name = "lblTransFileName"; + lblTransFileName.Size = new Size(57, 21); + lblTransFileName.TabIndex = 4; + lblTransFileName.Text = "label1"; + // + // btnStartRead + // + btnStartRead.BackColor = Color.Red; + btnStartRead.Enabled = false; + btnStartRead.ForeColor = Color.FromArgb(255, 255, 128); + btnStartRead.Location = new Point(726, 21); + btnStartRead.Name = "btnStartRead"; + btnStartRead.Size = new Size(107, 36); + btnStartRead.TabIndex = 5; + btnStartRead.Text = "Starta Inläsning"; + btnStartRead.UseVisualStyleBackColor = false; + // + // frmReadPayments + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(856, 453); + Controls.Add(btnStartRead); + Controls.Add(lblTransFileName); + Controls.Add(btnChooseFile); + Controls.Add(listView1); + Controls.Add(btnClose); + Name = "frmReadPayments"; + Text = "frmReadPayments"; + Load += frmReadPayments_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private ListView lvPayments; + private ColumnHeader ch1_Id; + private ColumnHeader ch2_Mottagare; + private ColumnHeader ch3_Konto; + private ColumnHeader ch4_Belopp; + private ColumnHeader ch6_Avisering; + private Button btnClose; + private ColumnHeader ch5_Förfallodag; + private ListView listView1; + private ColumnHeader columnHeader1; + private ColumnHeader columnHeader2; + private ColumnHeader columnHeader3; + private ColumnHeader columnHeader4; + private ColumnHeader columnHeader5; + private ColumnHeader columnHeader6; + private Button btnChooseFile; + private OpenFileDialog ofChooseFile; + private Label lblTransFileName; + private Button btnStartRead; + } +} \ No newline at end of file diff --git a/WinFormDi/frmReadPayments.cs b/WinFormDi/frmReadPayments.cs new file mode 100644 index 0000000..0bbf6b4 --- /dev/null +++ b/WinFormDi/frmReadPayments.cs @@ -0,0 +1,46 @@ +using WinFormDiApp.BLI; + +namespace WinFormDiApp +{ + public partial class frmReadPayments : Form + { + private readonly IAccountRecordRepository _accountRecordRepository; + + public frmReadPayments(IAccountRecordRepository accountRecordRepository) + { + InitializeComponent(); + _accountRecordRepository = accountRecordRepository; + } + + + private void btnClose_Click(object sender, EventArgs e) + { + this.Close(); + } + + private void frmReadPayments_Load(object sender, EventArgs e) + { + var payments = _accountRecordRepository.GetAllAccounts(); + foreach (var account in payments) + { + var lvitem = lvPayments.Items.Add(account.Id.ToString()); + lvitem.SubItems.Add(account.Mottagare); + lvitem.SubItems.Add(account.Konto); + lvitem.SubItems.Add(account.Belopp.ToString()); + lvitem.SubItems.Add(account.BetalDatum.ToShortDateString()); + lvitem.SubItems.Add(account.Avisering); + } + } + + private void btnChooseFile_Click(object sender, EventArgs e) + { + ofChooseFile.Title = "Välj nerladdad excel-fil (Transaktioner)"; + ofChooseFile.InitialDirectory = "C:\\"; + if (ofChooseFile.ShowDialog() == DialogResult.OK) + { + lblTransFileName.Text = ofChooseFile.FileName; + btnStartRead.Enabled = true; + } + } + } +} diff --git a/WinFormDi/frmReadPayments.resx b/WinFormDi/frmReadPayments.resx new file mode 100644 index 0000000..bb88643 --- /dev/null +++ b/WinFormDi/frmReadPayments.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file