Programm_Wirl/FormImport.cs
2025-11-30 11:44:05 +01:00

435 lines
15 KiB
C#

using DatenDB;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Deckungsbeitrag
{
public partial class FormImport : Form
{
public Umsatz umsatz = new Umsatz();
public Kunde kunde = new Kunde();
public Kosten kosten;
string filename;
string row;
string[] rows;
int[] fails = new int[0];
int index = 0;
int fehler = 0; int itemimported = 0; int umsatzsaved = 0; int kostensaved = 0; int itemtoimport = 0; int itemnotimported = 0;
int kontonr = 0;
int[] variabel = ConfigurationManager.AppSettings["Variabel"].Split(',').Select(int.Parse).ToArray();
int[] fixlohn = ConfigurationManager.AppSettings["FixLohn"].Split(',').Select(int.Parse).ToArray();
int[] fixmiet = ConfigurationManager.AppSettings["FixMiete"].Split(',').Select(int.Parse).ToArray();
Thread t;
public FormImport()
{
InitializeComponent();
toolStripOpen.Visible = false;
dTPUmsatz.Enabled = false;
dTPUmsatz.Value = DateTime.Now.AddMonths(-1);
buttonImport.Enabled = false;
buttonImport.BackColor = Color.Gray;
checkBoxExtra.Visible = checkBoxExtra.Enabled = false;
}
private void toolStripOpen_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "CommaSeparatedFiles|*.txt;*.csv";
dialog.InitialDirectory = "C:";
if (dialog.ShowDialog() == DialogResult.OK)
{
filename = dialog.FileName;
DatenLesen();
}
if (this.listViewImport.Items.Count > 0)
{
buttonImport.Enabled = true;
buttonImport.BackColor = Color.PaleGreen;
}
}
private void DatenLesen()
{
StreamReader sr = new StreamReader(filename, Encoding.GetEncoding("iso-8859-1"));
row = string.Empty;
rows = new string[0];
int idx = 0;
while (!sr.EndOfStream)
{
Array.Resize<string>(ref rows, ++idx);
row = sr.ReadLine();
rows[idx - 1] = row;
}
sr.Close();
for (int i = 0; i < rows.Length; i++)
{
string[] rowData = rows[i].Split(';');
if(i == 0)
{
ColumnHeader ch = new ColumnHeader();
foreach (string s in rowData)
{
ch = new ColumnHeader();
if (s.Contains("Wert"))
{
if (s.Contains("Wert01")) ch.Name = ch.Text = "Jän " + dTPUmsatz.Value.Year.ToString();
if (s.Contains("Wert02")) ch.Name = ch.Text = "Feb " + dTPUmsatz.Value.Year.ToString();
if (s.Contains("Wert03")) ch.Name = ch.Text = "Mär " + dTPUmsatz.Value.Year.ToString();
if (s.Contains("Wert04")) ch.Name = ch.Text = "Apr " + dTPUmsatz.Value.Year.ToString();
if (s.Contains("Wert05")) ch.Name = ch.Text = "Mai " + dTPUmsatz.Value.Year.ToString();
if (s.Contains("Wert06")) ch.Name = ch.Text = "Jun " + dTPUmsatz.Value.Year.ToString();
if (s.Contains("Wert07")) ch.Name = ch.Text = "Jul " + dTPUmsatz.Value.Year.ToString();
if (s.Contains("Wert08")) ch.Name = ch.Text = "Aug " + dTPUmsatz.Value.Year.ToString();
if (s.Contains("Wert09")) ch.Name = ch.Text = "Sep " + dTPUmsatz.Value.Year.ToString();
if (s.Contains("Wert10")) ch.Name = ch.Text = "Okt " + dTPUmsatz.Value.Year.ToString();
if (s.Contains("Wert11")) ch.Name = ch.Text = "Nov " + dTPUmsatz.Value.Year.ToString();
if (s.Contains("Wert12")) ch.Name = ch.Text = "Dez " + dTPUmsatz.Value.Year.ToString();
}
else ch.Name = ch.Text = s.Trim();
this.listViewImport.Columns.Add(ch);
}//UMSCHREIBEN DER COLUMNHEADER VON WERT AUF MONATE
}
else
{
ListViewItem item = new ListViewItem();
item.UseItemStyleForSubItems = false;
item.Text = rowData[0];
for (int j = 1; j < rowData.Length; j++)
{
item.SubItems.Add(rowData[j]);
}
this.listViewImport.Items.Add(item);
} //BEFÜLLEN DER COLUMNS DER LISTVIEW
}
this.listViewImport.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
ListView.ColumnHeaderCollection cc = listViewImport.Columns;
for (int i = 0; i < cc.Count; i++)
{
int colWidth = TextRenderer.MeasureText(cc[i].Text, listViewImport.Font).Width + 10;
if (colWidth > cc[i].Width)
{
cc[i].Width = colWidth;
}
} //COLUMNS BREITE CONTENT ODER HEADER SIZE
this.listViewImport.AllowColumnReorder = true;
if (!rBKundenDaten.Checked)
{
this.listViewImport.CheckBoxes = true;
}//CHECKBOXES BEI KOSTENLISTE UND UMSATZLISTE AKTIVIERT NUR BEI FEHLERHAFT NICHT IMPORTIERTEN VERWENDET
}//AUSGEWÄHLTE LISTE WIRD IN DIE LISTVIEW GELESEN.
private void buttonImport_Click(object sender, EventArgs e)
{
//t = new Thread(new ThreadStart(Splash));
//t.Start();
this.Cursor = Cursors.WaitCursor;
if (rBKundenDaten.Checked)
{
fehler = 0;
int kundensaved = 0;
string vorkunde = null;
int items = 0;
List<Kunde> kndvorimp = new List<Kunde>();
kndvorimp = Kunde.GetList(true, string.Empty);
StringCollection kndimport = new StringCollection();
foreach(ListViewItem item in this.listViewImport.Items)
{
++items;
if (item.Text != "0000001")
{
if (int.TryParse(item.Text, out int tmp))
{
if (vorkunde == item.Text)
{
++fehler;
vorkunde = item.Text;
}
else
{
if (!item.SubItems[5].Text.Contains("Preisgruppe"))
{
if (kunde.CheckKndNr(item.Text) == 0)
{
this.kunde.TourID = null;
this.kunde.KundeNummer = vorkunde = item.Text;
this.kunde.KundeGruppe = string.IsNullOrEmpty(item.SubItems[3].Text) ? string.Empty : item.SubItems[3].Text;
this.kunde.PLZ = int.TryParse(item.SubItems[4].Text, out int tmp2) ? tmp2 : 00;
this.kunde.KundeName = string.IsNullOrEmpty(item.SubItems[5].Text) ? string.Empty : item.SubItems[5].Text;
this.kunde.KundeName2 = string.IsNullOrEmpty(item.SubItems[6].Text) ? string.Empty : item.SubItems[6].Text;
this.kunde.Strasse = string.IsNullOrEmpty(item.SubItems[8].Text) ? string.Empty : item.SubItems[8].Text;
this.kunde.Land = string.IsNullOrEmpty(item.SubItems[9].Text) ? string.Empty : item.SubItems[9].Text;
this.kunde.Ort = string.IsNullOrEmpty(item.SubItems[10].Text) ? string.Empty : item.SubItems[10].Text;
this.kunde.Bettenanzahl = int.TryParse(item.SubItems[45].Text, out int tmp3) ? tmp3 : 00;
this.kunde.Waescheart = string.IsNullOrEmpty(item.SubItems[46].Text) ? string.Empty : item.SubItems[46].Text;
this.kunde.Service = string.IsNullOrEmpty(item.SubItems[47].Text) ? string.Empty : item.SubItems[47].Text;
this.kunde.Region = string.IsNullOrEmpty(item.SubItems[48].Text) ? string.Empty : item.SubItems[48].Text;
this.kunde.Suchtext = string.IsNullOrEmpty(item.SubItems[55].Text) ? string.Empty : item.SubItems[55].Text;
this.kunde.Aktiv = item.SubItems[56].Text == "1" ? true : false;
this.kunde.KundeID = null;
if (this.kunde.Save() == 1) ++kundensaved;
else
{
Array.Resize<int>(ref fails, ++index);
fails[index - 1] = items;
}
}
else
{
this.kunde.TourID = null;
this.kunde.KundeNummer = vorkunde = item.Text;
this.kunde.KundeGruppe = string.IsNullOrEmpty(item.SubItems[3].Text) ? string.Empty : item.SubItems[3].Text;
this.kunde.PLZ = int.TryParse(item.SubItems[4].Text, out int tmp2) ? tmp2 : 00;
this.kunde.KundeName = string.IsNullOrEmpty(item.SubItems[5].Text) ? string.Empty : item.SubItems[5].Text;
this.kunde.KundeName2 = string.IsNullOrEmpty(item.SubItems[6].Text) ? string.Empty : item.SubItems[6].Text;
this.kunde.Strasse = string.IsNullOrEmpty(item.SubItems[8].Text) ? string.Empty : item.SubItems[8].Text;
this.kunde.Land = string.IsNullOrEmpty(item.SubItems[9].Text) ? string.Empty : item.SubItems[9].Text;
this.kunde.Ort = string.IsNullOrEmpty(item.SubItems[10].Text) ? string.Empty : item.SubItems[10].Text;
this.kunde.Bettenanzahl = int.TryParse(item.SubItems[45].Text, out int tmp3) ? tmp3 : 00;
this.kunde.Waescheart = string.IsNullOrEmpty(item.SubItems[46].Text) ? string.Empty : item.SubItems[46].Text;
this.kunde.Service = string.IsNullOrEmpty(item.SubItems[47].Text) ? string.Empty : item.SubItems[47].Text;
this.kunde.Region = string.IsNullOrEmpty(item.SubItems[48].Text) ? string.Empty : item.SubItems[48].Text;
this.kunde.Suchtext = string.IsNullOrEmpty(item.SubItems[55].Text) ? string.Empty : item.SubItems[55].Text;
this.kunde.KundeID = Kunde.GetKundeID(item.Text, null);
this.kunde.Aktiv = item.SubItems[56].Text == "1" ? true : false;
if (this.kunde.Save() == 1) ++kundensaved;
else
{
Array.Resize<int>(ref fails, ++index);
fails[index - 1] = items;
}
//kndimport.Add(this.kunde.KundeNummer);
Kunde kun = kndvorimp.SingleOrDefault(x => x.KundeNummer == this.kunde.KundeNummer);
kndvorimp.Remove(kun);
}
}
else ++fehler;
}
}
else
{
MessageBox.Show($"Zeile {items} nicht importiert! KundenNr. ist keine Zahl.");
++fehler;
}
}
else ++fehler;
}
if (checkBoxMandat.Checked)
{
foreach (Kunde knd in kndvorimp)
{
knd.Aktiv = false;
knd.Save();
}
}
//t.Abort();
if (items == kundensaved + fehler)
{
MessageBox.Show("Import Fertig!");
}
else
{
string show;
string[] shows = new string[0];
int idx = 0;
foreach (int i in fails)
{
Array.Resize<string>(ref shows, ++idx);
show = i.ToString();
shows[idx - 1] = show;
}
string mssgbx = string.Join(Environment.NewLine, shows);
MessageBox.Show("Import nicht vollständig!" + Environment.NewLine + $"Folgende Datensätze wurden nicht importiert: {mssgbx}");
}
}//IMPORT VON KUNDENDATENLISTE AUS SOCOM
if (rBKostenListe.Checked)
{
itemimported = 0;
kostensaved = 0;
foreach (ListViewItem item in this.listViewImport.Items)
{
kosten = new Kosten();
if (int.TryParse(item.SubItems[1].Text, out kontonr))
{
this.kosten.KontoNr = kontonr;
if (Array.Exists<int>(variabel, x => x == kontonr)) this.kosten.Kategorie = KostenKat.Variabel;
else
{
if (Array.Exists<int>(fixlohn, x => x == kontonr)) this.kosten.Kategorie = KostenKat.FixLohn;
else
{
if (Array.Exists<int>(fixmiet, x => x == kontonr)) this.kosten.Kategorie = KostenKat.FixMiete;
else this.kosten.Kategorie = KostenKat.Rest;
}
}
this.kosten.Bezeichnung = item.SubItems[2].Text;
foreach (ColumnHeader ch in this.listViewImport.Columns)
{
if (DateTime.TryParse(ch.Text, out DateTime datum))
{
if (datum.Year != 0001)
{
this.kosten.Datum = datum;
this.kosten.KostenID = kosten.GetKostenID(kosten.KontoNr, datum);
if (double.Parse(item.SubItems[ch.Index].Text, System.Globalization.NumberStyles.Any) != 0)
{
++itemimported;
this.kosten.Betrag = double.Parse(item.SubItems[ch.Index].Text, System.Globalization.NumberStyles.Any);
if (kosten.Save() == 1) ++kostensaved;
}
}
}
}
}
}
//t.Abort();
if (kostensaved == itemimported)
{
MessageBox.Show("Import Fertig! Alle Zeilen importiert.");
}
else MessageBox.Show("Import Fertig!");
}//IMPORT VON KOSTENLISTE VON STEUERBERATER
if (rBUmsatzListe.Checked)
{
itemnotimported = 0;
itemimported = 0;
itemtoimport = 0;
string monat = string.Empty;
foreach (ListViewItem item in this.listViewImport.Items)
{
++itemtoimport;
this.umsatz.UmsatzID = null;
if (kunde.CheckKndNr(item.SubItems[0].Text) != 0)
{
this.umsatz.KundeID = kunde.CheckKndNr(item.SubItems[0].Text);
foreach (ColumnHeader ch in this.listViewImport.Columns)
{
if (DateTime.TryParse(ch.Text, out DateTime datum))
{
this.umsatz.Datum = datum;
this.umsatz.Betrag = double.Parse(item.SubItems[ch.Index].Text);
this.umsatz.UmsatzID = umsatz.GetUmsatzID(umsatz.KundeID, datum);
if (umsatz.Save() == 1) ++itemimported;
else
{
item.Checked = true;
++itemnotimported;
}
}
}
}
else
{
item.Checked = true;
++itemnotimported;
}
}
//t.Abort();
if (itemimported / 12 + itemnotimported == itemtoimport)
{
MessageBox.Show($"Import Fertig! {itemnotimported} Zeilen wurden nicht importiert.");
foreach(ListViewItem item in this.listViewImport.Items)
{
if(item.Checked == false) this.listViewImport.Items.Remove(item);
}
}
else MessageBox.Show($"Irgendwas ist falsch gelaufen... {itemtoimport} != {itemimported / 12}+{itemnotimported}!!");
}//IMPORT VON UMSATZLISTE AUS SOCOM
if (checkBoxExtra.Checked)
{
foreach(ListViewItem item in this.listViewImport.Items)
{
++itemimported;
if (kunde.CheckKndNr(item.Text) != 0)
{
this.umsatz.UmsatzID = null;
this.umsatz.KundeID = kunde.CheckKndNr(item.Text);
this.umsatz.Datum = DateTime.Parse(item.SubItems[1].Text);
this.umsatz.Betrag = double.Parse(item.SubItems[2].Text);
if (umsatz.Save() == 1) ++umsatzsaved;
}
else ++fehler;
}
if (umsatzsaved + fehler == itemimported)
{
MessageBox.Show("Import Fertig!");
//ConfigurationManager.AppSettings["LetzterImport"].Replace(ConfigurationManager.AppSettings["LetzterImport"], DateTime.Today.ToString());
}
}//COLUMNHEADER BEARBEITBAR PROGRAMMIEREN UM FLEXIBEL IMPORTIEREN ZU KÖNNEN??
this.Cursor = Cursors.Default;
}//LISTVIEW WIRD IN DATENBANK IMPORTIERT.
private void rB_CheckedChanged(object sender, EventArgs e)
{
dTPUmsatz.Enabled = false;
if (sender.GetType() == typeof(CheckBox))
{
if(MessageBox.Show("Möchtest du wirklich eine Sonderliste importieren?", "Frage", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Alle Dateien|*.*|TXT Dateien|*.txt|CSV Dateien|*.csv";
dialog.InitialDirectory = "C:";
if (dialog.ShowDialog() == DialogResult.OK)
{
filename = dialog.FileName;
}
DatenLesen();
}
else
{
CheckBox cb = (CheckBox)sender;
cb.Checked = false;
}
} //ABFRAGE BEI SONDERLISTE CHECKBOX OB DAS WIRKLICH GEWÜNSCHT WIRD
else
{
RadioButton rb = (RadioButton)sender;
if (rb.Parent == groupBoxImportdaten)
{
if (rBUmsatzListe.Checked) dTPUmsatz.Enabled = true;
toolStripOpen.Visible = true;
}
} //BEI RADIOBUTTON AUSGEWÄHLT WIRD OPEN FELD SICHTBAR
}
private void tSBAbbrechen_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void Splash()
{
//Open a splash screen form
FormLaden frm = new FormLaden();
Application.Run(frm);
}
}
}