Esempio n. 1
0
        private void ParseLastFile()
        {
            try {
                List <Payer> payers = new List <Payer>();

                MissedMessages.Clear();  //clear missed list for new read

                DateTime latestDateTime = new DateTime(1, 1, 1);
                Payer    latestPayer    = null;

                foreach (string line in lastFileLines)
                {
                    try
                    {
                        string msgString = "";
                        if (CopiedFromWaWeb)
                        {
                            ////new parse with copy from whatsapp web
                            //handle lines of format: "[16:57, 8.1.2020] XYZ: Shampoo 2,50€"

                            //1. get latest date
                            if (line.StartsWith("["))
                            {
                                try
                                {
                                    string   dateTimeString = line.Split(']').First().Trim('[');
                                    DateTime newDateTime    = DateTime.ParseExact(dateTimeString, "HH:mm, d.M.yyyy", null);
                                    latestDateTime = newDateTime;
                                }
                                catch (Exception ex) { }
                            }

                            // --> if date doesnt fit into filter continue
                            string messageMonthString = latestDateTime.Month.ToString();
                            if (messageMonthString.Count() < 2)
                            {
                                messageMonthString = '0' + messageMonthString;
                            }
                            if (messageMonthString != MonthString)
                            {
                                continue;
                            }
                            if (latestDateTime.Year.ToString() != YearString)
                            {
                                continue;
                            }

                            //2. get latest payer
                            if (line.StartsWith("["))
                            {
                                string payerName = line.Split(':')[1].Split(']').Last().Trim(' ');
                                if (payers.Where(x => x.Name == payerName).Count() == 0)    //if payer not exists add payer
                                {
                                    payers.Add(new Payer(payerName));
                                }

                                //add payment to payer
                                latestPayer = payers.Where(x => x.Name == payerName).First();
                            }

                            //3. get payment
                            if (!line.Contains("€"))
                            {
                                MissedMessages.Add(line);
                                continue;
                            }

                            msgString = line.Split(':').Last();
                        }
                        else
                        {
                            //old parse with export function that is now disabled in germany
                            //handles lines of format: "27.08.19, 19:43 - XYZ: Restaurant 22€"

                            //filter month
                            if (line.Split(new char[] { '.' })[1] != MonthString)
                            {
                                continue;
                            }

                            //filter year
                            if (!YearString.EndsWith(line.Split(new char[] { '.' })[2].Substring(0, 2)))
                            {
                                string[] test = line.Split(new char[] { '.' });
                                continue;
                            }

                            try
                            {
                                string   dateTimeString = line.Substring(0, 15);
                                DateTime newDateTime    = DateTime.ParseExact(dateTimeString, "dd.MM.yy, HH:mm", null);
                                latestDateTime = newDateTime;
                            }
                            catch (Exception ex) { }

                            //filter contains payment
                            if (!line.Contains("€"))
                            {
                                MissedMessages.Add(line);
                                continue;
                            }

                            //get payer
                            string payerName    = null;
                            string msgAndSender = line.Substring(18);
                            payerName = msgAndSender.Split(new char[] { ':' })[0];

                            //get amount and subject
                            msgString = msgAndSender.Split(new char[] { ':' })[1];

                            if (payers.Where(x => x.Name == payerName).Count() == 0)    //if payer not exists add payer
                            {
                                payers.Add(new Payer(payerName));
                            }

                            //add payment to payer
                            latestPayer = payers.Where(x => x.Name == payerName).First();
                        }

                        float  amount       = 0;
                        string subject      = "";
                        string amountString = null;
                        foreach (string msgPart in msgString.Split(new char[] { ' ' }))
                        {
                            if (!msgPart.Contains("€"))
                            {
                                subject += msgPart + " ";
                                continue;
                            }

                            amountString = msgPart.Trim(new char[] { ' ', '€' });
                        }
                        if (amountString != null && Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator == "." && amountString.Contains(","))   //replace comma with . if english style decimal separators
                        {
                            amountString = amountString.Replace(',', '.');
                        }
                        subject = subject.Trim(new char[] { ' ' });
                        if (!string.IsNullOrEmpty(amountString) && !float.TryParse(amountString, out amount))
                        {
                            MissedMessages.Add(line);
                        }

                        //add payment to payer
                        if (latestPayer != null)
                        {
                            latestPayer.AddPayment(amount, subject, latestDateTime.ToString());
                        }
                        else
                        {
                            MissedMessages.Add(line);
                        }
                    }
                    catch (Exception)
                    {
                        System.Windows.Forms.MessageBox.Show("Error on line: " + line);
                        MissedMessages.Add(line);
                        PropChanged(nameof(MissedMessages));
                    }
                }

                if (payers.Count > 0)
                {
                    Payers.Clear();
                    Sum = 0;
                }

                txtBlckOutput.Text = "";
                foreach (Payer payer in payers)
                {
                    Sum += payer.SumPayed;
                }
                float sumPerPayer = Sum / payers.Count;
                foreach (Payer payer in payers)
                {
                    payer.CompensationPayment = sumPerPayer - payer.SumPayed;

                    Payers.Add(payer);
                }
            }

            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Fehler beim öffnen der Datei: " + ex.ToString(), "Falscho", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }