Beispiel #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            DayModel day = sqlDay.GetOneByDate(DateTime.Now.ToString("yyyy - MM - dd"));

            if (day.Id == 0)
            {
                day.SoldProductsCount += order.ProductCount;
                day.TotalMade         += order.Cost;
                day.TotalSpent        += order.Spent;
                day.MostCommonProduct  = "could not determine";
                day.MostCommonCategory = "could not determine";
                sqlDay.SaveDay(day);
            }
            else
            {
                day.SoldProductsCount += order.ProductCount;
                day.TotalMade         += order.Cost;
                day.TotalSpent        += order.Spent;
                day.MostCommonProduct  = "could not determine";
                day.MostCommonCategory = "could not determine";
                sqlDay.Update(day);
            }
        }
Beispiel #2
0
        private async void FinalizeDayBtn_Click(object sender, EventArgs e)
        {
            bool dayExist = false;

            SqlDay sqlDay = new SqlDay();

            DayModel dayChek = sqlDay.GetOneByDate(day.Date.ToString("yyyy - MM - dd"));

            if (dayChek.Id != 0)
            {
                dayExist = true;
            }

            decimal totalEarned  = Convert.ToDecimal(TotalMadeView.Text);
            decimal totalSpend   = Convert.ToDecimal(TotalSpentView.Text);
            int     productsSold = dataGridView1.Rows.Count - 1;


            List <string> categoryCommon = new List <string>();
            List <string> nameCommon     = new List <string>();

            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                string category = dataGridView1.Rows[i].Cells["Category"].Value.ToString();
                categoryCommon.Add(category);
            }
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                string category = dataGridView1.Rows[i].Cells["Name"].Value.ToString();
                nameCommon.Add(category);
            }


            try
            {
                var mostCommonCategory = categoryCommon
                                         .GroupBy(i => i)
                                         .OrderByDescending(grp => grp
                                                            .Count())
                                         .Select(grp => grp.Key)
                                         .First();

                var mostCommonName = nameCommon
                                     .GroupBy(i => i)
                                     .OrderByDescending(grp => grp
                                                        .Count())
                                     .Select(grp => grp.Key)
                                     .First();


                MostSoldCategory.Text = mostCommonCategory;
                MostSoldProduct.Text  = mostCommonName;

                day.MostCommonCategory = mostCommonCategory;
                day.MostCommonProduct  = mostCommonName;
                day.SoldProductsCount  = productsSold;
                day.TotalMade          = totalEarned;
                day.TotalSpent         = totalSpend;


                progressBar1.Maximum = 100;
                progressBar1.Step    = 1;

                var progress = new Progress <int>(v =>
                {
                    progressBar1.Value = v;
                });


                await Task.Run(() => DoWork(progress));

                if (!dayExist)
                {
                    sqlDay.SaveDay(day);
                }
                else
                {
                    DayModel newDay = new DayModel();
                    newDay.Id                 = dayChek.Id;
                    newDay.TotalMade          = dayChek.TotalMade + day.TotalMade;
                    newDay.TotalSpent         = dayChek.TotalSpent + day.TotalSpent;
                    newDay.SoldProductsCount  = dayChek.SoldProductsCount + day.SoldProductsCount;
                    newDay.MostCommonProduct  = dayChek.MostCommonProduct;
                    newDay.MostCommonCategory = dayChek.MostCommonCategory;

                    sqlDay.Update(newDay);
                }
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("Add products!");
            }
        }