public void DataBind(MainModel model)
 {
     ViewModel = (SalesBusinessAnalysis)model.SalesBA;
     this.DataContext = (SalesBusinessAnalysis)model.SalesBA;
     mainModel = model;
     LoadDiagram(ref chart);
     LoadDiagram2(ref chart2);
     LoadDiagram4(ref chart4);
     if (ViewModel.StructureGrossProfitClientInfo != null && ViewModel.StructureGrossProfitClientInfo.Count > 0)
     {
         UpdateTable();
     };
     UpdateColors();
 }
 public void DataBind(SalesBusinessAnalysis model)
 {
     ViewModel = (SalesBusinessAnalysis)model;
     this.DataContext = (SalesBusinessAnalysis)model;
     UpdateColors();
 }
        public static SalesBusinessAnalysis GetSalesBA(MainModel MainModel)
        {
            //TODO
            // структура компании перевести в проценты
            var model = new SalesBusinessAnalysis();
            model.DynamicsSalesDiagram = new Dictionary<string, decimal>();
            model.Goods1Diagram = new Dictionary<string, decimal>();
            model.Goods2Diagram = new Dictionary<string, decimal>();
            model.Goods3Diagram = new Dictionary<string, decimal>();
            foreach (var mon in MainModel.Sales)
            {
                var tmp = mon.Sales.Select(_ => _.SalesWithoutNDS).Sum();

                model.DynamicsSalesDiagram.Add(MainModel.IsItQuarter || (MainModel.EndDate - MainModel.StartDate).Days > 365 ? string.Format("{0}, {1}", (Month)mon.Date.Month, mon.Date.Year) : ((Month)mon.Date.Month).ToString(), tmp);

                var salGr = (from s in mon.Sales
                             group s by s.GroupCode into g
                             select new
                             {
                                 name = g.FirstOrDefault().GroupName,
                                 money = g.Sum(_ => _.SalesWithoutNDS)
                             }).OrderByDescending(_ => _.money).ToList();

                if (salGr.Count > 0)
                    model.Goods1Diagram.Add(MainModel.IsItQuarter || (MainModel.EndDate - MainModel.StartDate).Days > 365 ? string.Format("{0}, {1}", (Month)mon.Date.Month, mon.Date.Year) : ((Month)mon.Date.Month).ToString(), salGr[0].money);
                if (salGr.Count > 1)
                    model.Goods2Diagram.Add(MainModel.IsItQuarter || (MainModel.EndDate - MainModel.StartDate).Days > 365 ? string.Format("{0}, {1}", (Month)mon.Date.Month, mon.Date.Year) : ((Month)mon.Date.Month).ToString(), salGr[1].money);
                if (salGr.Count > 2)
                    model.Goods3Diagram.Add(MainModel.IsItQuarter || (MainModel.EndDate - MainModel.StartDate).Days > 365 ? string.Format("{0}, {1}", (Month)mon.Date.Month, mon.Date.Year) : ((Month)mon.Date.Month).ToString(), salGr[2].money);

                if (tmp > model.maxCountSaleGoods)
                {
                    model.maxCountSaleGoods = tmp;
                    model.MonthOfMaxCountSaleGoods = MainModel.IsItQuarter || (MainModel.EndDate - MainModel.StartDate).Days > 365 ? string.Format("{0}, {1}", (Month)mon.Date.Month, mon.Date.Year) : ((Month)mon.Date.Month).ToString() + ", " + mon.Date.Year;
                }
            }

            model.SummSaleGoods = MainModel.Sales.SelectMany(_ => _.Sales).Select(_ => _.SalesWithoutNDS).Sum();
            model.maxAverageCountSaleGoods = MainModel.Sales.Count() != 0 ? model.SummSaleGoods / MainModel.Sales.Count() : 0;

            var tmpDecimal = MainModel.GeneralBA.salesFirst.SelectMany(_ => _.Sales).Select(_ => _.SalesWithoutNDS).Sum();
            model.DifSummSaleGoods = model.SummSaleGoods - tmpDecimal;
            if(tmpDecimal != 0)
                model.DifPercentSaleGoods = model.DifSummSaleGoods / tmpDecimal * 100;

            MainModel.allADDSTranz = Accessors.GetAllAddsTranz(MainModel.StartDate, MainModel.EndDate, MainModel.RegGroups ?? new List<RefGroupsEnt>(), new List<string> { });

            var rt = (from tt in MainModel.allADDSTranz
                      where tt.GroupCode.Trim() == "00002"
                      group tt by new { tt.period.Month, tt.period.Year } into g
                      select new
                      {
                          mon = g.FirstOrDefault().period.Month,
                          year = g.FirstOrDefault().period.Year,
                          money = g.Sum(_ => _.Money)
                      }).OrderByDescending(_ => _.year).ThenBy(_ => _.mon).ToList();

            model.DynamicsPaymentDiagram = new Dictionary<string, decimal>();

            rt.ForEach(_ => model.DynamicsPaymentDiagram.Add((
                MainModel.IsItQuarter || (MainModel.EndDate - MainModel.StartDate).Days > 365 ? string.Format("{0}, {1}", (Month)_.mon, _.year) : ((Month)_.mon).ToString()).ToString(), _.money));
            //так и не пригодилось
            //model.MonthOfMaxCountPaymentBuyer = ((Month)rt.OrderByDescending(_ => _.money).First().mon).ToString() + ", " + rt.OrderByDescending(_ => _.money).First().year;
            model.maxAverageCountPaymentBuyer = rt.Count() != 0 ? rt.Sum(_ => _.money) / rt.Count() : 0;
            model.maxCountPaymentBuyer = rt.Count != 0 ? rt.OrderByDescending(_ => _.money).First().money : 0;
            model.SummPaymentBuyer = rt.Sum(_ => _.money);

            MainModel.ADDSTranzPastPeriod = Accessors.GetAllAddsTranz(MainModel.newStTodayDate, MainModel.newEndTodayDate, MainModel.RegGroups ?? new List<RefGroupsEnt>(), new List<string> { });

            var rtPast = (from tt in MainModel.ADDSTranzPastPeriod
                          where tt.GroupCode.Trim() == "00002" // только оплата покупателя, если будет долго, можно отдельный запрос написать все не тащить
                          group tt by new { tt.period.Month, tt.period.Year } into g
                          select new
                          {
                              mon = g.FirstOrDefault().period.Month,
                              year = g.FirstOrDefault().period.Year,
                              money = g.Sum(_ => _.Money)
                          }).OrderByDescending(_ => _.year).ThenBy(_ => _.mon).ToList();

            tmpDecimal = rtPast.Sum(_ => _.money);
            model.DifSummPaymentBuyer = model.SummPaymentBuyer - tmpDecimal;
            model.DifPercentPaymentBuyer = tmpDecimal != 0 ? model.DifSummPaymentBuyer / tmpDecimal * 100 : 0;

            var salGrNow = (from s in MainModel.Sales.SelectMany(_ => _.Sales)
                            group s by s.GroupCode into g
                            select new
                            {
                                name = g.FirstOrDefault().GroupName,
                                money = g.Sum(_ => _.SalesWithoutNDS)
                            }).OrderByDescending(_ => _.money).ToList();

            var salGrPast = (from s in MainModel.GeneralBA.salesFirst.SelectMany(_ => _.Sales)
                             group s by s.GroupCode into g
                             select new
                             {
                                 name = g.FirstOrDefault().GroupName,
                                 money = g.Sum(_ => _.SalesWithoutNDS)
                             }).OrderByDescending(_ => _.money).ToList();

            model.Goods1Info = new FillGoodsModel();
            if (salGrNow.Count > 0)
            {
                model.Goods1Info.Name = salGrNow[0].name;
                model.Goods1Info.Value = salGrNow[0].money;
                model.Goods1Info.pastValue = salGrPast.Where(_ => _.name == salGrNow[0].name).Count() > 0 ? salGrNow[0].money - salGrPast.Where(_ => _.name == salGrNow[0].name).FirstOrDefault().money : 0;
                model.Goods1Info.Percent = salGrPast.Where(_ => _.name == salGrNow[0].name).Count() > 0 ? salGrPast.Count != 0 ? model.Goods1Info.pastValue / salGrPast.Where(_ => _.name == salGrNow[0].name).FirstOrDefault().money * 100 : 0 : 0;
            }
            model.Goods2Info = new FillGoodsModel();
            if (salGrNow.Count > 1)
            {
                model.Goods2Info.Name = salGrNow[1].name;
                model.Goods2Info.Value = salGrNow[1].money;
                model.Goods2Info.pastValue = salGrPast.Where(_ => _.name == salGrNow[1].name).Count() > 0 ? salGrNow[1].money - salGrPast.Where(_ => _.name == salGrNow[1].name).FirstOrDefault().money : 0;
                model.Goods2Info.Percent = salGrPast.Where(_ => _.name == salGrNow[1].name).Count() > 0 ? salGrPast.Count != 0 ? model.Goods2Info.pastValue / salGrPast.Where(_ => _.name == salGrNow[1].name).FirstOrDefault().money * 100 : 0 : 0;
            }

            model.Goods3Info = new FillGoodsModel();

            if (salGrNow.Count > 2)
            {
                model.Goods3Info.Name = salGrNow[2].name;
                model.Goods3Info.Value = salGrNow[2].money;
                model.Goods3Info.pastValue = salGrPast.Where(_ => _.name == salGrNow[2].name).Count() > 0 ? salGrNow[2].money - salGrPast.Where(_ => _.name == salGrNow[2].name).FirstOrDefault().money : 0;
                model.Goods3Info.Percent = salGrPast.Where(_ => _.name == salGrNow[2].name).Count() > 0 ? salGrPast.Count != 0 ? model.Goods3Info.pastValue / salGrPast.Where(_ => _.name == salGrNow[2].name).FirstOrDefault().money * 100 : 0 : 0;
            }

            var salCli = (from s in MainModel.Sales.SelectMany(_ => _.Sales)
                          group s by s.BuyerCode into g
                          select new
                          {
                              name = g.FirstOrDefault().BuyerName,
                              money = g.Sum(_ => _.SalesWithoutNDS)
                          }
                          ).OrderByDescending(_ => _.money).ToList();
            model.StructureGrossProfitClientDiagram = new Dictionary<string, decimal>();
            var generalSumm = salCli.Sum(_ => _.money);
            if(generalSumm != 0)
                foreach(var item in salCli.Take(5))
                {
                    model.StructureGrossProfitClientDiagram.Add(item.name, item.money / generalSumm * 100);
                }
            model.StructureGrossProfitClientDiagram.Add("Прочие", 100 - model.StructureGrossProfitClientDiagram.Sum(_ => _.Value));

            var tc = (from sa in MainModel.GeneralBA.gSalesByClient
                          //join addz in MainModel.ADDSTranz on sa.BuyerCode equals addz.MenCode
                      group sa by sa.BuyerCode into g
                      select new
                      {
                          code = g.FirstOrDefault().BuyerCode,
                          name = g.FirstOrDefault().BuyerName,
                          money = g.Sum(_ => _.SalesWithoutNDS)
                      }).OrderByDescending(_ => _.money);
            var ta = (from addz in MainModel.allADDSTranz
                     // where tc.Select(_ => _.code).ToList().Contains(addz.MenCode)
                      group addz by addz.MenCode into g
                      select new
                      {
                          code = g.FirstOrDefault().MenCode,
                          name = g.FirstOrDefault().MenName,
                          money = g.Sum(_ => _.Money)
                      }).ToList();

            model.StructureGrossProfitClientInfo = new List<FillModel>();
            foreach (var item in tc.Take(5))
                model.StructureGrossProfitClientInfo.Add(new FillModel { Name = item.name, Value = item.money,
                                                                         Value2 = ta.FirstOrDefault(_ => _.code.Trim() == item.code.Trim()) != null ? ta.FirstOrDefault(_ => _.code.Trim() == item.code.Trim()).money : 0
                });

            model.StructureGrossProfitClientInfo.Add(new FillModel
            {
                Name = "Прочие",
                Value = tc.Sum(_ => _.money) - model.StructureGrossProfitClientInfo.Sum(_ => _.Value),
                Value2 = ta.Sum(_ => _.money) - model.StructureGrossProfitClientInfo.Sum(_ => _.Value2)
            });


            return model;

        }