Ejemplo n.º 1
0
        public RepositoryResult <IEnumerable <SalesViewInterfaceModel> > GetSalesViewsForInterface(CustomParameterModel parameter, int months = 12)
        {
            try
            {
                if (parameter.GroupId.HasValue == false && parameter.MakerId.HasValue == false)
                {
                    return(new RepositoryResult <IEnumerable <SalesViewInterfaceModel> >(HttpStatusCode.BadRequest));
                }
                if (!parameter.Year.HasValue)
                {
                    return(new RepositoryResult <IEnumerable <SalesViewInterfaceModel> >(HttpStatusCode.BadRequest));
                }

                using (DataContext dbContext = DataContext.Create())
                    using (var productRepository = new ProductRepository())
                    {
                        var products   = productRepository.GetProductsForInterface(parameter);
                        var productIds = products.Select(p => p.Id).ToList();

                        ICollection <OfficeModel> office = dbContext.OfficeModels.Where(o => o.Deleted == false).OrderBy(o => o.Code).ToList();

                        string productList = string.Join(",", productIds);
                        string sql         = ServiceResource.SelectSalesViews.Replace("@p2", productList);

                        // 全体の販売実績データ
                        List <SalesInterfaceModel> salesList;
                        DateTime startDate = DateTime.Parse((parameter.Year - 1).ToString() + "/10/1");
                        DateTime endDate   = startDate.AddMonths(months);
                        salesList = dbContext.Database.SqlQuery <SalesInterfaceModel>(sql, startDate, endDate).ToList <SalesInterfaceModel>();

                        // 事業所別
                        List <SalesOfficeInterfaceModel> salesOfficeList;
                        sql             = ServiceResource.SelectOfficesSalesViews.Replace("@p2", productList);
                        salesOfficeList = dbContext.Database.SqlQuery <SalesOfficeInterfaceModel>(sql, startDate.AddYears(-1), endDate).ToList <SalesOfficeInterfaceModel>();

                        // 返却用の準備
                        var resultData = new List <SalesViewInterfaceModel>();
                        foreach (var product in products)
                        {
                            // 在庫予測計算用に1ヶ月多めに取得(貿易のみ)
                            DateTime check = startDate.AddMonths(-1);;
                            SalesViewInterfaceModel addModel = new SalesViewInterfaceModel();
                            addModel.Product     = product;
                            addModel.SalesList   = new List <SalesInterfaceModel>();
                            addModel.OfficeSales = new List <ICollection <SalesOfficeInterfaceModel> >();

                            for (; check <= endDate; check = check.AddMonths(1))
                            {
                                var work = salesList.Where(x => x.product_id == addModel.Product.Id).Where(x => x.detail_date == check).SingleOrDefault();
                                if (work == null)
                                {
                                    var tempModel = new SalesInterfaceModel();
                                    tempModel.product_id  = addModel.Product.Id;
                                    tempModel.detail_date = check;
                                    addModel.SalesList.Add(tempModel);
                                }
                                else
                                {
                                    addModel.SalesList.Add(work);
                                }

                                var workOffice = new List <SalesOfficeInterfaceModel>();
                                foreach (var ofs in office)
                                {
                                    var ofsData = salesOfficeList.Where(x => x.product_id == addModel.Product.Id).Where(x => x.detail_date == check).Where(x => x.office_id == ofs.Id).SingleOrDefault();
                                    if (ofsData == null)
                                    {
                                        var tempModel = new SalesOfficeInterfaceModel();
                                        tempModel.product_id  = product.Id;
                                        tempModel.detail_date = check;
                                        tempModel.office_id   = ofs.Id;
                                        tempModel.office_name = ofs.Name;
                                        workOffice.Add(tempModel);
                                    }
                                    else
                                    {
                                        workOffice.Add(ofsData);
                                    }
                                }
                                addModel.OfficeSales.Add(workOffice);
                            }
                            resultData.Add(addModel);
                        }
                        return(new RepositoryResult <IEnumerable <SalesViewInterfaceModel> >(HttpStatusCode.OK, resultData));
                    }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 2
0
        public RepositoryResult <SalesViewInterfaceModel> GetSalesViewByIdForInterface(int productId, CustomParameterModel parameter, int months = 12)
        {
            try
            {
                if (productId <= 0)
                {
                    return(new RepositoryResult <SalesViewInterfaceModel>(HttpStatusCode.BadRequest));
                }
                if (!parameter.Year.HasValue)
                {
                    return(new RepositoryResult <SalesViewInterfaceModel>(HttpStatusCode.BadRequest));
                }


                using (DataContext dbContext = DataContext.Create())
                    using (var productRepository = new ProductRepository())
                    {
                        var product = productRepository.GetProductByIdForInterface(productId);
                        if (product.Code != HttpStatusCode.OK)
                        {
                            return(new RepositoryResult <SalesViewInterfaceModel>(HttpStatusCode.BadRequest));
                        }

                        // 全体の販売実績データ
                        List <SalesInterfaceModel> salesList;
                        DateTime startDate = DateTime.Parse((parameter.Year - 1).ToString() + "/10/1");
                        DateTime endDate   = startDate.AddMonths(months);
                        salesList = dbContext.Database.SqlQuery <SalesInterfaceModel>(ServiceResource.SelectSalesViews, startDate, endDate, product.resultData.Id).ToList <SalesInterfaceModel>();

                        ICollection <OfficeModel> office = dbContext.OfficeModels.Where(o => o.Deleted == false).OrderBy(o => o.Code).ToList();

                        // 事業所別
                        List <SalesOfficeInterfaceModel> salesOfficeList;
                        salesOfficeList = dbContext.Database.SqlQuery <SalesOfficeInterfaceModel>(ServiceResource.SelectOfficesSalesViews, startDate, endDate, product.resultData.Id).ToList <SalesOfficeInterfaceModel>();

                        // 在庫予測計算用に1ヶ月多めに取得(貿易のみ)
                        DateTime check = startDate.AddMonths(-1);

                        var resultData = new SalesViewInterfaceModel();
                        resultData.Product     = product.resultData;
                        resultData.SalesList   = new List <SalesInterfaceModel>();
                        resultData.OfficeSales = new List <ICollection <SalesOfficeInterfaceModel> >();

                        for (; check <= endDate; check = check.AddMonths(1))
                        {
                            var work = salesList.Where(x => x.product_id == resultData.Product.Id).Where(x => x.detail_date == check).SingleOrDefault();
                            if (work == null)
                            {
                                var tempModel = new SalesInterfaceModel();
                                tempModel.product_id  = resultData.Product.Id;
                                tempModel.detail_date = check;
                                resultData.SalesList.Add(tempModel);
                            }
                            else
                            {
                                resultData.SalesList.Add(work);
                            }

                            var workOffice = new List <SalesOfficeInterfaceModel>();
                            foreach (var value in office)
                            {
                                var ofsData = salesOfficeList.Where(x => x.product_id == resultData.Product.Id).Where(x => x.detail_date == check).Where(x => x.office_id == value.Id).SingleOrDefault();
                                if (ofsData == null)
                                {
                                    var tempModel = new SalesOfficeInterfaceModel();
                                    tempModel.product_id   = resultData.Product.Id;
                                    tempModel.detail_date  = check;
                                    tempModel.office_id    = value.Id;
                                    tempModel.office_name  = value.Name;
                                    tempModel.sales_plan   = 0;
                                    tempModel.sales_actual = 0;
                                    workOffice.Add(tempModel);
                                }
                                else
                                {
                                    workOffice.Add(ofsData);
                                }
                            }
                            resultData.OfficeSales.Add(workOffice);
                        }
                        return(new RepositoryResult <SalesViewInterfaceModel>(HttpStatusCode.OK, resultData));
                    }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }