public void DataBind(MainModel model)
 {
     ViewModel = (PurchaseBusinessAnalysis)model.PurchaseBA;
     this.DataContext = (PurchaseBusinessAnalysis)model.PurchaseBA;
     mainModel = model;
     LoadDiagram2(ref chart2);
     LoadDiagram3(ref chart3);
     if (ViewModel.ClientDiagramInfo != null)
     {
         UpdateTable();
     }
     UpdateColors();
 }
 public void DataBind(PurchaseBusinessAnalysis model)
 {
     ViewModel = (PurchaseBusinessAnalysis)model;
     this.DataContext = (PurchaseBusinessAnalysis)model;
     LoadDiagram1(ref chart1);
 }
예제 #3
0
        public static PurchaseBusinessAnalysis GetPurchaseBA(MainModel MainModel)
        {
            var model = new PurchaseBusinessAnalysis();

            model.allPurchase = MainModel.Sales.SelectMany(_ => _.Sales).Where(_ => _.CountPur != decimal.Zero).Select(_ => _.CostPrise).Sum();
            MainModel.CostsBA.CostsDiagram.Add("Закуп", model.allPurchase);
            var pastPur = MainModel.GeneralBA.salesFirst.SelectMany(_ => _.Sales).Where(_ => _.CountPur != decimal.Zero).Select(_ => _.CostPrise).Sum();
            model.difPastPeriod = pastPur != 0 ? (model.allPurchase - pastPur) / pastPur * 100 : 0;

            model.maxPurchaseByMonth = decimal.Zero;
            var counter = 0;
            model.PurchaseDiagram = new Dictionary<string, decimal>();
            model.SalesDiagram = new Dictionary<string, decimal>();
            model.PaymentDiagram = new Dictionary<string, decimal>();
            foreach (var item in MainModel.Sales)
            {
                var dt = string.Empty;
                var tmp = item.Sales.Where(_ => _.CountPur != decimal.Zero).Select(_ => _.CostPrise).Sum();
                if (tmp > model.maxPurchaseByMonth)
                    model.maxPurchaseByMonth = tmp;


                dt = MainModel.IsItQuarter || (MainModel.EndDate - MainModel.StartDate).Days > 365 ? string.Format("{0}, {1}", (Month)item.Date.Month, item.Date.Year) : ((Month)item.Date.Month).ToString();
                model.PurchaseDiagram.Add(dt, item.Sales.Sum(_ => _.CostPrise));
                model.SalesDiagram.Add(dt, item.Sales.Sum(_ => _.SalesWithoutNDS));
                if(MainModel.ADDSTranzPastPeriod.Count > counter)
                    model.PaymentDiagram.Add(dt, MainModel.ADDSTranzPastPeriod[counter].Money);

                counter++;
            }
            model.averagePurchaseByMonth = MainModel.Sales.Count() != 0 ? model.allPurchase / MainModel.Sales.Count() : 0;

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

            model.PurchaseByGoodsDiagram = new Dictionary<string, decimal>();
            model.PurchaseByGoodsDiagram.Add("Прочее", tmpPurGroup.Skip(3).Sum(_ => _.money));
            if(tmpPurGroup.Count > 2)
             model.PurchaseByGoodsDiagram.Add(tmpPurGroup[2].name, tmpPurGroup[2].money);
            if (tmpPurGroup.Count > 1)
                model.PurchaseByGoodsDiagram.Add(tmpPurGroup[1].name, tmpPurGroup[1].money);
            if (tmpPurGroup.Count > 0)
                model.PurchaseByGoodsDiagram.Add(tmpPurGroup[0].name, tmpPurGroup[0].money);

            model.salesByGoodsDiagram = new Dictionary<string, decimal>();
            var ti = MainModel.GeneralBA.gSales.OrderByDescending(_ => _.SalesWithoutNDS).ToArray();
            model.salesByGoodsDiagram.Add("Прочее", ti.Skip(3).Sum(_ => _.SalesWithoutNDS));
            var ttv = 3;
            if (ti.Count() < 3) ttv = ti.Count();
            for (var i = ttv-1; i >= 0; i --)
                model.salesByGoodsDiagram.Add(ti[i].GroupName, ti[i].SalesWithoutNDS);

            model.SalesvsPurchase = model.allPurchase != 0 ? MainModel.GeneralBA.Sales / model.allPurchase * 100 : 0;
            model.difSalesvsPurchasePastPeriod = pastPur != 0 ? model.SalesvsPurchase - 
                (MainModel.GeneralBA.salesFirst.SelectMany(_ => _.Sales).Sum(_ => _.SalesWithoutNDS) / pastPur * 100) : 0;

            model.PaymentvsPurchase = model.allPurchase != 0 ? MainModel.ADDSTranz.Where(_ => _.GroupCode.Trim() == "00001").Sum(_ => _.Money) / model.allPurchase * 100 : 0;
            model.difPaymentvsPurchasePastPeriod = pastPur != 0 ? MainModel.ADDSTranzPastPeriod.Sum(_ => _.Money) / pastPur : 0;

            var pusa = Accessors.getPurMan(MainModel.StartDate, MainModel.EndDate).OrderByDescending(_ => _.CostPrise).ToList();

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

            for (var i = 0; i < 5; i++)
                model.PurchaseByClientDiagram.Add(pusa[i].SalerName, pusa[i].CostPrise);

            model.PurchaseByClientDiagram.Add("Прочие", pusa.Skip(5).Sum(_ => _.CostPrise));

            var adz = (from az in MainModel.allADDSTranz
                       group az by az.MenCode into g
                       select new
                       {
                           name = g.FirstOrDefault().MenName,
                           code = g.FirstOrDefault().MenCode,
                           money = g.Sum(_ => _.Money)
                       }).OrderByDescending(_ => _.money).ToList();


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

            for (var i = 0; i < 5; i++)
            {
                if (adz.FirstOrDefault(_ => _.code == pusa[i].SalerCode) != null)
                {
                    model.PaymentByClientDiagram.Add(adz.FirstOrDefault(_ => _.code == pusa[i].SalerCode).name, adz.FirstOrDefault(_ => _.code == pusa[i].SalerCode).money);
                }
                else
                {
                    model.PaymentByClientDiagram.Add(pusa[i].SalerName, 0);
                }
            }

            model.PaymentByClientDiagram.Add("Прочие", adz.Sum(_ => _.money) - model.PaymentByClientDiagram.Sum(_ => _.Value));

            var sstmp = model.PurchaseByClientDiagram.Sum(_ => _.Value);
            model.ClientDiagramInfo = new List<FillModel>();
            if(sstmp != 0)
                foreach (var item in model.PurchaseByClientDiagram)
                    model.ClientDiagramInfo.Add(new FillModel() { Name = item.Key, Share = item.Value / sstmp * 100 });


            return model;
        }