public void DataBind(MainModel model)
 {
     ViewModel = (ProfitBusinessAnalysis)model.ProfitBA;
     mainModel = model;
     this.DataContext = (ProfitBusinessAnalysis)ViewModel;
     LoadDiagram(ref chart);
     LoadDiagram2(ref chart2);
     LoadDiagram3(ref chart3);
     LoadDiagram4(ref chart4);
     if (ViewModel.StructureGrossProfitGoodsInfo != null && ViewModel.StructureGrossProfitGoodsInfo.Count > 0)
     {
         UpdateTable1();
     }
     if (ViewModel.StructureGrossProfitClientInfo != null && ViewModel.StructureGrossProfitClientInfo.Count > 0)
     {
         UpdateTable2();
     }
 }
 public void DataBind(ProfitBusinessAnalysis model)
 {
     ViewModel = (ProfitBusinessAnalysis)model;
     this.DataContext = (ProfitBusinessAnalysis)model;
     UpdateColors();
 }
        public static ProfitBusinessAnalysis GetProfitBA(MainModel MainModel)
        {
            var model = new ProfitBusinessAnalysis();

            //пока валовая равна чистой т.к. у нас нет расходов
            model.GrossProfit = MainModel.GeneralBA.GrossProfit;
            model.GrossProfitAnFirst = MainModel.GeneralBA.GrossProfitAnFirst;
            model.NetProfit = MainModel.GeneralBA.NetProfit;
            model.NetProfitAnFirst = MainModel.GeneralBA.NetProfitAnFirst;

            var mM = MainModel.Sales.Sum(_ => _.Sales.Sum(s => s.SalesWithoutNDS));// выручка - цена продажи
            if (mM != 0) 
                model.NetProfitability = model.NetProfit / mM * 100;
            model.AverageNetProfitByMonth = model.NetProfit / dif;
            if (mM != 0)
                model.GrossProfitability = model.GrossProfit / mM * 100;
            model.AverageGrossProfitByMonth = model.GrossProfit / dif;

            model.SavedProfit = MainModel.BusinessResults.AccumulatedProfitAndLossStart + MainModel.BusinessResults.AccumulatedProfitAndLossEnd;

            //Динамика валовой и чистой прибыли и рентабильность тут же
            model.GrossProfitDiagram = new Dictionary<string, decimal>();
            model.NetProfitDiagram = new Dictionary<string, decimal>();
            model.GrossProfitabilityDiagram = new Dictionary<string, decimal>();
            model.NetProfitabilityDiagram = new Dictionary<string, decimal>();
            var counter = 0;//счетчик месяцев
            foreach (var mon in MainModel.Sales)
            {
                var gp = mon.Sales.Sum(_ => _.SalesWithoutNDS - _.CostPrise);
                   model.GrossProfitDiagram.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(), gp);
                model.NetProfitDiagram.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(),
                    gp - MainModel.ReportProfitAndLoss.Costs.ToArray()[counter]
                    );

                var tj =  mon.Sales.Sum(s => s.SalesWithoutNDS);// выручка - цена продажи
                 
                model.GrossProfitabilityDiagram.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(), tj != 0 ? gp / tj * 100 : 0);

                model.NetProfitabilityDiagram.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(),
                    tj != 0 ? (gp - MainModel.ReportProfitAndLoss.Costs[counter]) / tj * 100 : 0);
                counter++;
            }

            //структура валовой прибыли по товарам
            var StrBestGoods = MainModel.GeneralBA.gSales
                        .Select(_ => new { gName = _.GroupName, gGrow = _.SalesWithoutNDS - _.CostPrise, gPrice = _.SalesWithoutNDS })
                        .OrderByDescending(_ => _.gGrow)
                        .Take(3);
            model.StructureGrossProfitGoodsDiagram = new Dictionary<string, decimal>();
            model.StructureGrossProfitGoodsInfo = new List<FillModel>();
            foreach (var g in StrBestGoods)
            {
                var ty = MainModel.GeneralBA.gSales.Where(_ => _.GroupName == g.gName).Sum(_ => _.SalesWithoutNDS - _.CostPrise);
                model.StructureGrossProfitGoodsDiagram.Add(g.gName, g.gGrow);
                model.StructureGrossProfitGoodsInfo.Add(new FillModel
                {
                    Name = g.gName,
                    Share = ty / MainModel.GeneralBA.gSales.Sum(_ => _.SalesWithoutNDS - _.CostPrise) * 100 ,
                    Value = MainModel.GeneralBA.gSales.Sum(_ => _.SalesWithoutNDS) != 0 ? ty
                        / MainModel.GeneralBA.gSales.Sum(_ => _.SalesWithoutNDS) * 100 : 0
                });
            }

            model.StructureGrossProfitGoodsInfo.OrderByDescending(_ => _.Share);


            //структура валовой прибыли по клиентам
            var StrBestClient = MainModel.GeneralBA.gSalesByClient
                        .Select(_ => new { gName = _.BuyerName, gGrow = _.SalesWithoutNDS - _.CostPrise, gPrice = _.SalesWithoutNDS })
                        .OrderByDescending(_ => _.gGrow)
                        .Take(5);
            model.StructureGrossProfitClientDiagram = new Dictionary<string, decimal>();
            model.StructureGrossProfitClientInfo = new List<FillModel>();
            foreach (var g in StrBestClient)
            {
                var tu = MainModel.GeneralBA.gSalesByClient.Where(_ => _.BuyerName == g.gName).Sum(_ => _.SalesWithoutNDS - _.CostPrise);
                model.StructureGrossProfitClientDiagram.Add(g.gName, g.gGrow / MainModel.GeneralBA.gSalesByClient.Sum(_ => _.SalesWithoutNDS - _.CostPrise)  
                     * 100);
                model.StructureGrossProfitClientInfo.Add(new FillModel
                {
                    Name = g.gName,
                    Share = tu / MainModel.GeneralBA.gSales.Sum(_ => _.SalesWithoutNDS - _.CostPrise) * 100,
                    Value = tu / MainModel.GeneralBA.gSalesByClient.Sum(_ => _.SalesWithoutNDS) * 100
                });
            }
            model.StructureGrossProfitClientDiagram.Add("Прочее", 100 - model.StructureGrossProfitClientDiagram.Sum(_ => _.Value));
            var tmOth =
                MainModel.GeneralBA.gSalesByClient
                            .Select(_ => new { gName = _.BuyerName, gGrow = _.SalesWithoutNDS - _.CostPrise, gPrice = _.SalesWithoutNDS })
                            .OrderByDescending(_ => _.gGrow).Skip(5);
            model.StructureGrossProfitClientInfo.Add(new FillModel
            {
                Name = "Прочие",
                Share = 100 - model.StructureGrossProfitClientInfo.Sum(_ => _.Share),
                Value = MainModel.GeneralBA.gSalesByClient.Sum(_ => _.SalesWithoutNDS) != 0 ? tmOth.Sum(_ => _.gGrow) / MainModel.GeneralBA.gSalesByClient.Sum(_ => _.SalesWithoutNDS) * 100 : 0
            });

            return model;
        }