Ejemplo n.º 1
0
        void RefreshView()
        {
            Table.RemoveAllViews();

            TextViews = new Dictionary <string, TextView>();

            // TODO: remove Stopwatchers
            var generateViews = new Stopwatch();

            generateViews.Start();

            //var financeDatas = MainDatabase.GetItems<FinanceData>().Where(i => i.Pharmacy == Pharmacy.UUID).ToList();
            var dict_key = string.Empty;

            // 1. Рисуем таблицу
            int monthesCount = 9;

            DateTimeOffset[] months = new DateTimeOffset[monthesCount];
            int quartersCount       = monthesCount / 3;

            int[] quarters = new int[quartersCount];

            DateTimeOffset baseDate = DateTimeOffset.Now.AddMonths(2);

            int            highMonth = ((baseDate.Month + 2) / 3 * 3);
            DateTimeOffset highDate  = baseDate.AddMonths(highMonth - baseDate.Month);

            for (int m = 0, q = 1; m < monthesCount; m++, q++)
            {
                months[m] = highDate.AddMonths(m - monthesCount + 1);

                if ((q % 3) == 0)
                {
                    quarters[(q / 3) - 1] = (months[m].Month + 2) / 3;
                }
            }

            var header = (LinearLayout)LayoutInflater.Inflate(Resource.Layout.FinanceTableHeader, Table, false);

            for (int col = 0; col < (monthesCount + quartersCount); col++)
            {
                var hView = header.GetChildAt(col + 1);
                if (hView is TextView)
                {
                    if ((col + 1) % 4 == 0)
                    {
                        (hView as TextView).Text = months[col - (col / 4) - 1].ToString(string.Format(@"{0} кв.{1}yyyy", quarters[col / 4], System.Environment.NewLine));
                    }
                    else
                    {
                        (hView as TextView).Text = months[col - (col / 4)].ToString(string.Format(@"MMMM{0}yyyy", System.Environment.NewLine));
                    }
                }
            }

            Content.AddView(header, 0);
            //Table.AddView(header);

            var infoTypes = Enum.GetValues(typeof(FinanceInfoType)).Cast <FinanceInfoType>();
            var drugSKUs  = MainDatabase.GetDrugSKUs();

            foreach (var type in infoTypes)
            {
                View subheader = LayoutInflater.Inflate(Resource.Layout.FinanceTableSubHeader, Table, false);
                subheader.FindViewById <TextView>(Resource.Id.ftshTypeName).Text = GetTypeName(type);               //type.ToString();
                Table.AddView(subheader);
                foreach (var SKU in drugSKUs)
                {
                    var row = (LinearLayout)LayoutInflater.Inflate(Resource.Layout.FinanceTableItem, Table, false);
                    row.FindViewById <TextView>(Resource.Id.ftiDrugSKUTV).Text = SKU.name;
                    for (int col = 0; col < (monthesCount + quartersCount); col++)
                    {
                        var view = row.GetChildAt(col + 1);
                        if (view is TextView)
                        {
                            (view as TextView).Text = string.Empty;

                            if ((col + 1) % 4 == 0)
                            {
                                string datePart = months[col - (col / 4) - 1].ToString(string.Format(@"4{0}yyyy", quarters[col / 4]));
                                dict_key = string.Format("{0}-{1}-{2}", SKU.uuid, type, datePart);
                            }
                            else
                            {
                                string datePart = months[col - (col / 4)].ToString(PeriodFormatForKey);
                                dict_key = string.Format("{0}-{1}-{2}", SKU.uuid, type, datePart);
                            }

                            TextViews.Add(dict_key, (view as TextView));
                        }
                    }

                    Table.AddView(row);
                }
            }

            generateViews.Stop();

            Console.WriteLine(
                "FinanceData: RefreshView={0}-{1}",
                generateViews.ElapsedMilliseconds,
                TextViews.Count);

            var setValues = new Stopwatch();

            setValues.Start();

            var key = string.Empty;
            // 2. Данные
            TextView txt;

            // 2.1 Данные с визита
            // 2.1.1 Вставляем данные за месяц
            var sdMonths = MainDatabase.GetPharmacyDatas <SaleDataByMonth>(Pharmacy.UUID);

            foreach (var data in sdMonths)
            {
                string datePart = string.Format("{0}{1}", data.Month.ToString("D2"), data.Year);
                // Sale
                key = string.Format("{0}-{1}-{2}", data.DrugSKU, FinanceInfoType.fitSale, datePart);
                if (TextViews.ContainsKey(key))
                {
                    txt      = TextViews[key];
                    txt.Text = data.Sale.ToString();
                    txt.SetTextColor(Android.Graphics.Color.DarkOrange);
                }
            }
            // 2.1.2 Вставляем данные за квартал
            var sdQuarters = MainDatabase.GetPharmacyDatas <SaleDataByQuarter>(Pharmacy.UUID);

            foreach (var data in sdQuarters)
            {
                string datePart = string.Format("4{0}{1}", data.Quarter, data.Year);
                // Sale
                key = string.Format("{0}-{1}-{2}", data.DrugSKU, FinanceInfoType.fitSale, datePart);
                if (TextViews.ContainsKey(key))
                {
                    txt      = TextViews[key];
                    txt.Text = data.Sale.ToString();
                    txt.SetTextColor(Android.Graphics.Color.DarkOrange);
                }
            }

            // 2.2 Данные от сетей введенные
            // 2.2.1 Вставляем данные за месяц
            var fdMonths = MainDatabase.GetPharmacyDatas <FinanceDataByMonth>(Pharmacy.UUID);

            foreach (var data in fdMonths)
            {
                string datePart = string.Format("{0}{1}", data.Month.ToString("D2"), data.Year);
                // Sale
                key = string.Format("{0}-{1}-{2}", data.DrugSKU, FinanceInfoType.fitSale, datePart);
                if (TextViews.ContainsKey(key))
                {
                    txt      = TextViews[key];
                    txt.Text = data.Sale.ToString();
                    txt.SetTextColor(Android.Graphics.Color.Blue);
                }
                //TextViewsBySKU[key].Text = financeData.Sale.ToString();
                // Purchase
                key = string.Format("{0}-{1}-{2}", data.DrugSKU, FinanceInfoType.fitPurchase, datePart);
                if (TextViews.ContainsKey(key))
                {
                    txt      = TextViews[key];
                    txt.Text = data.Purchase.ToString();
                    txt.SetTextColor(Android.Graphics.Color.Blue);
                }
                //TextViewsBySKU[key].Text = financeData.Purchase.ToString();

                // Remain
                key = string.Format("{0}-{1}-{2}", data.DrugSKU, FinanceInfoType.fitRemain, datePart);
                if (TextViews.ContainsKey(key))
                {
                    txt      = TextViews[key];
                    txt.Text = data.Remain.ToString();
                    txt.SetTextColor(Android.Graphics.Color.Blue);
                }
                //TextViewsBySKU[key].Text = financeData.Remain.ToString();
            }

            // 2.2.2 Вставляем данные за квартал
            var fdQuarters = MainDatabase.GetPharmacyDatas <FinanceDataByQuarter>(Pharmacy.UUID);

            foreach (var data in fdQuarters)
            {
                string datePart = string.Format("4{0}{1}", data.Quarter, data.Year);
                // Sale
                key = string.Format("{0}-{1}-{2}", data.DrugSKU, FinanceInfoType.fitSale, datePart);
                if (TextViews.ContainsKey(key))
                {
                    txt      = TextViews[key];
                    txt.Text = data.Sale.ToString();
                    txt.SetTextColor(Android.Graphics.Color.Blue);
                }
                //TextViewsBySKU[key].Text = financeData.Sale.ToString();
                // Purchas
                key = string.Format("{0}-{1}-{2}", data.DrugSKU, FinanceInfoType.fitPurchase, datePart);
                if (TextViews.ContainsKey(key))
                {
                    txt      = TextViews[key];
                    txt.Text = data.Purchase.ToString();
                    txt.SetTextColor(Android.Graphics.Color.Blue);
                }
                //TextViewsBySKU[key].Text = financeData.Purchase.ToString();

                // Remain
                key = string.Format("{0}-{1}-{2}", data.DrugSKU, FinanceInfoType.fitRemain, datePart);
                if (TextViews.ContainsKey(key))
                {
                    txt      = TextViews[key];
                    txt.Text = data.Remain.ToString();
                    txt.SetTextColor(Android.Graphics.Color.Blue);
                }
                //TextViewsBySKU[key].Text = financeData.Remain.ToString();
            }

            setValues.Stop();
            Console.WriteLine(
                "FinanceData: SetValues={0}-{1}-{2}-{3}-{4}",
                setValues.ElapsedMilliseconds,
                sdMonths.Count, sdQuarters.Count, fdMonths.Count, fdQuarters.Count);

            //RefreshViewBySKU(financeDatas, dates, infoTypes, drugSKUs);
        }
Ejemplo n.º 2
0
        void GenerateData_Click(object sender, EventArgs e)
        {
            var rnd = new Random();

            var step        = 7;
            var visitsCount = 10;
            var baseDate    = DateTimeOffset.Now.AddDays(-1 * visitsCount * step);

            var promotions  = MainDatabase.GetItems <Promotion>();
            var drugSKUs    = MainDatabase.GetDrugSKUs();
            var pharmacies  = MainDatabase.GetItems <Pharmacy>();
            var transaction = MainDatabase.BeginTransaction();

            MainDatabase.DeleteAttendancies();
            MainDatabase.DeleteDistributions();
            MainDatabase.DeleteFinanceData();
            MainDatabase.DeleteSaleData();

            MainDatabase.DeleteItems <CoterieData>();
            MainDatabase.DeleteItems <PresentationData>();
            MainDatabase.DeleteItems <Employee>();
            MainDatabase.DeleteItems <PromotionData>();
            MainDatabase.DeleteItems <CompetitorData>();
            MainDatabase.DeleteItems <MessageData>();
            MainDatabase.DeleteItems <ResumeData>();
            MainDatabase.DeleteItems <PhotoData>();

            transaction.Commit();

            transaction = MainDatabase.BeginTransaction();

            //int count = 0;
            foreach (var pharmacy in pharmacies)
            {
                //count++;
                // Визиты
                if (true)
                {
                    for (int i = 0; i < visitsCount; i++)
                    {
                        var attendance = MainDatabase.Create <Attendance>();
                        attendance.Pharmacy = pharmacy.UUID;
                        attendance.When     = baseDate.AddDays(i * step);

                        foreach (var SKU in drugSKUs)
                        {
                            var distribution = MainDatabase.CreateData <DistributionData>(attendance.UUID);
                            distribution.DrugSKU     = SKU.uuid;
                            distribution.IsExistence = rnd.NextDouble() > 0.5;
                            distribution.Count       = rnd.Next(0, 10);
                            distribution.Price       = (int)(rnd.NextDouble() * 200);
                            distribution.IsPresence  = rnd.NextDouble() > 0.5;
                            distribution.HasPOS      = rnd.NextDouble() > 0.5;
                            distribution.Order       = @"Order " + SKU.uuid;
                            distribution.Comment     = @"Comment " + SKU.uuid;
                        }
                    }
                }

                // Продажи
                var financesCount = 5;
                baseDate = DateTimeOffset.Now.AddMonths(-3);
                for (int i = 0; i < financesCount; i++)
                {
                    foreach (var SKU in drugSKUs)
                    {
                        var financeData = MainDatabase.Create <FinanceData>();
                        financeData.Pharmacy = pharmacy.UUID;
                        financeData.Period   = baseDate.AddMonths(i);
                        financeData.DrugSKU  = SKU.uuid;
                        financeData.Sale     = rnd.NextDouble() > 0.3 ? null : (float?)(rnd.NextDouble() * 20);
                        financeData.Purchase = rnd.NextDouble() > 0.4 ? null : (float?)(rnd.NextDouble() * 20);
                        financeData.Remain   = rnd.NextDouble() > 0.5 ? null : (float?)(rnd.NextDouble() * 20);
                    }
                }

                // Содержание визите = Презентаци
                int employeesCount = 3;
                //int presentationsCount = 5;
                var attendanceLast = MainDatabase.GetAttendaces(pharmacy.UUID).OrderByDescending(i => i.When).FirstOrDefault();
                var positions      = MainDatabase.GetItems <Position>();
                var workTypes      = MainDatabase.GetItems <WorkType>();
                var brands         = MainDatabase.GetItems <DrugBrand>();
                var employees      = MainDatabase.GetEmployees(pharmacy.UUID);

                if (employees.Count == 0)
                {
                    for (int emp = 0; emp < employeesCount; emp++)
                    {
                        var employee = MainDatabase.CreateEmployee(pharmacy.UUID);
                        employee.SetSex(Sex.Male);
                        employee.Name = (rnd.NextDouble() > 0.5 ? @"Иванов" : @"Петров") + @" "
                                        + (rnd.NextDouble() > 0.5 ? @"Иван" : @"Петр") + @" "
                                        + (rnd.NextDouble() > 0.5 ? @"Иванович" : @"Петрович");
                        employee.Position  = positions[rnd.Next(positions.Count)].uuid;
                        employee.BirthDate = DateTimeOffset.Now.AddYears(-rnd.Next(25, 40)).AddDays((rnd.NextDouble() - 0.5) * 365);
                        employees.Add(employee);
                    }
                }

                foreach (var employee in employees)
                {
                    foreach (var brand in brands)
                    {
                        if (rnd.NextDouble() > 0.5)
                        {
                            continue;
                        }

                        for (int wt = 0; wt < workTypes.Count; wt++)
                        {
                            if (rnd.NextDouble() > 0.5)
                            {
                                continue;
                            }

                            var presentationData = MainDatabase.CreateData <PresentationData>(attendanceLast.UUID);
                            presentationData.Employee = employee.UUID;
                            presentationData.Brand    = brand.uuid;
                            presentationData.WorkType = workTypes[wt].uuid;
                        }
                    }
                }

                // Содержание визите = Фарм-кружок
                foreach (var employee in employees)
                {
                    if (rnd.NextDouble() > 0.5)
                    {
                        continue;
                    }

                    foreach (var brand in brands)
                    {
                        if (rnd.NextDouble() > 0.5)
                        {
                            continue;
                        }

                        var coterieData = MainDatabase.CreateData <CoterieData>(attendanceLast.UUID);
                        coterieData.Employee = employee.UUID;
                        coterieData.Brand    = brand.uuid;
                    }
                }

                // Акция
                var promotionData = MainDatabase.CreateData <PromotionData>(attendanceLast.UUID);
                promotionData.Promotion = promotions[rnd.Next(promotions.Count - 1)].uuid;
                promotionData.Text      = @"promotionData";

                // Активность конкурентов
                var competitorData = MainDatabase.CreateData <CompetitorData>(attendanceLast.UUID);
                competitorData.Text = @"competitorData";

                // Сообщения от аптеки
                var messageTypes = MainDatabase.GetItems <MessageType>();
                foreach (var item in messageTypes)
                {
                    if (rnd.NextDouble() > 0.5)
                    {
                        continue;
                    }

                    var messageData = MainDatabase.CreateData <MessageData>(attendanceLast.UUID);
                    messageData.Type = item.uuid;
                    messageData.Text = @"messageData " + item.uuid;
                }

                // Резюме визита;
                var resumeData = MainDatabase.CreateData <ResumeData>(attendanceLast.UUID);
                resumeData.Text = @"resumeData";
            }

            transaction.Commit();

            RefreshView();
        }
Ejemplo n.º 3
0
        void RefreshView()
        {
            Stopwatch s1 = new Stopwatch();

            s1.Start();
            //var attendances = MainDatabase.GetEmployees(pharmacy.UUID).Where(> Datetime.Min).Sort();
            var attendances = MainDatabase.GetItems <Attendance>().Where(i => i.Pharmacy == Pharmacy.UUID).OrderBy(i => i.When).ToList();
            var dict_key    = string.Empty;
            // 1. Рисуем таблицу
            int    dateStep   = 7;
            int    datesCount = 20;
            string format     = @"ddMMyy";

            DateTimeOffset[] dates = new DateTimeOffset[datesCount];
            var header             = (LinearLayout)LayoutInflater.Inflate(Resource.Layout.HistoryTableHeader, Table, false);

            for (int d = 0; d < datesCount; d++)
            {
                if (d < attendances.Count)
                {
                    dates[d] = attendances[d].When;
                }
                else
                {
                    dates[d] = d == 0 ? DateTimeOffset.Now : dates[d - 1].AddDays(dateStep);
                }
                var hView = header.GetChildAt(d + 1);
                if (hView is TextView)
                {
                    (hView as TextView).Text = dates[d].ToString(
                        string.Format("dd{0}MMM{1}yyyy", System.Environment.NewLine, System.Environment.NewLine)
                        );
                }
            }

            Content.AddView(header, 0);
            //Table.AddView(header);

            var infoTypes = Enum.GetValues(typeof(DistributionInfoType)).Cast <DistributionInfoType>();
            var drugSKUs  = MainDatabase.GetDrugSKUs();

            foreach (var type in infoTypes)
            {
                View subheader = LayoutInflater.Inflate(Resource.Layout.HistoryTableSubHeader, Table, false);
                subheader.FindViewById <TextView>(Resource.Id.htshTypeName).Text = GetTypeName(type);               //type.ToString();
                Table.AddView(subheader);
                foreach (var SKU in drugSKUs)
                {
                    var row = (LinearLayout)LayoutInflater.Inflate(Resource.Layout.HistoryTableItem, Table, false);
                    row.FindViewById <TextView>(Resource.Id.htiDrugSKUTV).Text = SKU.name;
                    for (int v = 1; v <= datesCount; v++)
                    {
                        dict_key = string.Format("{0}-{1}-{2}", SKU.uuid, type, dates[v - 1].ToString(format));
                        var view = row.GetChildAt(v);
                        if (view is TextView)
                        {
                            (view as TextView).Text = string.Empty;
                            if (!TextViews.ContainsKey(dict_key))
                            {
                                TextViews.Add(dict_key, (view as TextView));
                                //HockeyApp.MetricsManager.TrackEvent(
                                //"HistoryActivity.RefreshView",
                                //	new Dictionary<string, string> { { "dict_key", dict_key }, { "date", dates[v - 1].LocalDateTime.ToLongDateString() } },
                                //	new Dictionary<string, double> { { "v", v } }
                                //);
                            }
                            else
                            {
                                HockeyApp.MetricsManager.TrackEvent(
                                    "HistoryActivity.RefreshView.DuplicateKey",
                                    new Dictionary <string, string> {
                                    { "dict_key", dict_key },
                                    { "android_id", Helper.AndroidId },
                                    { "agent_uuid", MainDatabase.AgentUUID },
                                },
                                    new Dictionary <string, double> {
                                    { "v", v }
                                }
                                    );
                            }
                        }
                    }
                    Table.AddView(row);
                }
            }

            // 2. Вставляем данные
            foreach (var attendance in attendances)
            {
                var distributions = MainDatabase.GetItems <DistributionData>().Where(i => i.Attendance == attendance.UUID);
                foreach (var distribution in distributions)
                {
                    // IsExistence

                    dict_key = string.Format("{0}-{1}-{2}", distribution.DrugSKU, DistributionInfoType.ditIsExistence, attendance.When.ToString(format));
                    if (!TextViews.ContainsKey(dict_key))
                    {
                        TextViews[dict_key].Text = distribution.IsExistence ? @"+" : @"-";
                    }

                    // Count
                    dict_key = string.Format("{0}-{1}-{2}", distribution.DrugSKU, DistributionInfoType.ditCount, attendance.When.ToString(format));
                    if (!TextViews.ContainsKey(dict_key))
                    {
                        TextViews[dict_key].Text = distribution.Count.ToString();
                    }

                    // Price
                    dict_key = string.Format("{0}-{1}-{2}", distribution.DrugSKU, DistributionInfoType.ditPrice, attendance.When.ToString(format));
                    if (!TextViews.ContainsKey(dict_key))
                    {
                        TextViews[dict_key].Text = distribution.Price.ToString();
                    }

                    // IsPresence
                    dict_key = string.Format("{0}-{1}-{2}", distribution.DrugSKU, DistributionInfoType.ditIsPresence, attendance.When.ToString(format));
                    if (!TextViews.ContainsKey(dict_key))
                    {
                        TextViews[dict_key].Text = distribution.IsPresence ? @"+" : @"-";
                    }

                    // HasPOS
                    dict_key = string.Format("{0}-{1}-{2}", distribution.DrugSKU, DistributionInfoType.ditHasPOS, attendance.When.ToString(format));
                    if (!TextViews.ContainsKey(dict_key))
                    {
                        TextViews[dict_key].Text = distribution.HasPOS ? @"+" : @"-";
                    }
                }
            }

            s1.Stop();

            Console.WriteLine(
                "{0}-{1}",
                s1.ElapsedMilliseconds,
                TextViews.Count);
        }