Ejemplo n.º 1
0
        /// <summary>
        /// Loading purchase statistics A and B data to DataTable
        /// </summary>
        /// <param name="filtering">EventCd and Type</param>
        /// <returns>IndexViewModel</returns>
        public IndexViewModel Load(IndexViewModel filtering)
        {
            var result = new IndexViewModel();

            var purchaseStatistics = _context.PurchaseStatistics
                                     .Where(x => (filtering.EventCd == 0 || x.EventCd == filtering.EventCd) &&
                                            x.Type == filtering.Type);

            result.TotalRowsAfterFiltering = purchaseStatistics.Count();
            purchaseStatistics             = Filtering(purchaseStatistics, filtering);
            result.PurchaseStatistics      = purchaseStatistics
                                             .Select(x => new
            {
                x.Cd,
                x.InsertUser.FirstName,
                x.InsertUser.LastName,
                x.InsertDate,
                x.Event.StartDate,
                x.Event.EndDate,
                x.Event.Name
            })
                                             .AsEnumerable()
                                             .Select(x => new PerchaseStatisticsRow
            {
                Cd   = x.Cd,
                Time = x.StartDate.HasValue && x.EndDate.HasValue
                                    ? $"{x.StartDate.Value.Date.Year}年{CommonExtension.GetWeekOfMonth(x.StartDate.Value, x.EndDate.Value, false)}"
                                    : "",
                EventName    = x.Name,
                OutputPerson = string.Concat(x.FirstName, x.LastName),
                InsertDate   = x.InsertDate.Value.ToString(Constants.ExactDateTimeFormat)
            }).ToList();

            if (result.PurchaseStatistics.Any())
            {
                for (var i = 0; i < result.PurchaseStatistics.Count; i++)
                {
                    result.PurchaseStatistics[i].No = filtering.Start + i + 1;
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get purchase statistics A & B data to create
        /// </summary>
        /// <param name="model"></param>
        /// <returns>PurchaseStatisticsDetailModel</returns>
        public PurchaseStatisticsDetailModel GetListOfContract(SearchFormModel model)
        {
            var evt = _context.MstEvents.Find(model.EventCd);

            if (evt == null || !evt.StartDate.HasValue || !evt.EndDate.HasValue)
            {
                return(null);
            }

            var result = new PurchaseStatisticsDetailModel
            {
                PurchaseType = model.Type,
                PeriodTime   = evt.StartDate.HasValue && evt.EndDate.HasValue
                                ? $"{CommonExtension.GetWeekOfMonth(evt.StartDate.Value, evt.EndDate.Value)}【{evt.Name}】"
                                : "",
                EventCd      = evt.Cd,
                EventName    = evt.Name,
                EventAddress = evt.Address,
                EventPlace   = evt.Place,
                StartDate    = evt.StartDate.Value,
                EndDate      = evt.EndDate.Value
            };

            var contracts = _context.Contracts
                            .Include(x => x.Media)
                            .Where(x => x.EventCd == evt.Cd && !x.IsDeleted && x.IsCompleted);

            if (contracts.Any())
            {
                var rowsDetail = new List <RowDetail>();
                var dates      = new List <DateTime>();
                if (model.Type == (int)Constants.PurchaseType.A)
                {
                    foreach (var contract in contracts)
                    {
                        dates.Add(contract.ContractDate.Value);
                        var row = rowsDetail.FirstOrDefault(x => x.MediaCd == contract.MediaCd &&
                                                            // ReSharper disable once PossibleInvalidOperationException
                                                            x.HoldDate.Date.CompareTo(contract.ContractDate.Value.Date) == 0);
                        if (row == null)
                        {
                            row = new RowDetail
                            {
                                // ReSharper disable once PossibleInvalidOperationException
                                HoldDate  = contract.ContractDate.Value,
                                MediaCd   = contract.Media.Cd,
                                MediaName = contract.Media.Name,
                                MediaCode = contract.Media.Code
                            };

                            row.NumberOfAttractingCustomers = _context.Surveys
                                                              .Count(x => x.MediaCd == row.MediaCd && row.HoldDate.Date == x.InsertDate.Value.Date);
                            rowsDetail.Add(row);
                        }

                        row.NumberOfContracts++;
                        row.TotalPriceOfContracts += contract.TotalPrice ?? 0;
                        row.ContractPrices.Add(Math.Floor((contract.TotalPrice ?? 0) / 10000));
                    }
                }
                else
                {
                    foreach (var contract in contracts)
                    {
                        var tempRowDetail = DistinctProductFromContract(contract);
                        foreach (var temp in tempRowDetail)
                        {
                            var artist = _context.MstArtists.FirstOrDefault(x => temp.ArtistCd != 0 && x.Cd == temp.ArtistCd);
                            if (artist != null && !artist.Name.Equals(temp.ArtistName))
                            {
                                temp.ArtistName = artist.Name;
                            }

                            var cell = rowsDetail.FirstOrDefault(x => x.ArtistCd == temp.ArtistCd && x.ArtistName.Equals(temp.ArtistName) && x.HoldDate.Date.CompareTo(temp.HoldDate.Date) == 0);
                            if (cell == null)
                            {
                                rowsDetail.Add(temp);
                            }
                            else
                            {
                                cell.NumberOfContracts++;
                                cell.TotalPriceOfContracts += temp.TotalPriceOfContracts;
                                cell.ContractPrices.AddRange(temp.ContractPrices);
                            }
                        }
                    }
                }

                if (rowsDetail.Any())
                {
                    result.RowDetails = rowsDetail;

                    if (model.Type == (int)Constants.PurchaseType.A)
                    {
                        result.MediasTotal = rowsDetail.GroupBy(x => new
                        {
                            x.MediaCd,
                            x.MediaCode,
                            x.MediaName
                        })
                                             .Select(x => new Total
                        {
                            MediaCd   = x.Key.MediaCd,
                            MediaName = x.Key.MediaName,
                            MediaCode = x.Key.MediaCode,
                            TotalAttractingCustomers = x.Sum(y => y.NumberOfAttractingCustomers),
                            TotalContracts           = x.Sum(y => y.NumberOfContracts),
                            SubTotal = x.Sum(y => y.TotalPriceOfContracts),
                            MaxCell  = x.Max(y => y.ContractPrices.Count),
                        }).ToList();

                        // add number of attracting customer for the dayafowithout a contract
                        for (var date = evt.StartDate.Value; date <= evt.EndDate; date = date.AddDays(1))
                        {
                            foreach (var media in result.MediasTotal)
                            {
                                var isExisted = rowsDetail.FirstOrDefault(x => x.HoldDate.Date == date.Date &&
                                                                          x.MediaCd == media.MediaCd) != null;
                                if (!isExisted)
                                {
                                    var row = new RowDetail
                                    {
                                        // ReSharper disable once PossibleInvalidOperationException
                                        HoldDate  = date,
                                        MediaCd   = media.MediaCd,
                                        MediaName = media.MediaName,
                                        MediaCode = media.MediaCode
                                    };

                                    row.NumberOfAttractingCustomers = _context.Surveys.Count(x => x.EventCd == evt.Cd &&
                                                                                             x.MediaCd == row.MediaCd && row.HoldDate.Date == x.InsertDate.Value.Date);

                                    // add to total attracting customer
                                    media.TotalAttractingCustomers += row.NumberOfAttractingCustomers;

                                    rowsDetail.Add(row);
                                }
                            }
                        }
                    }
                    else
                    {
                        result.ArtistsTotal = rowsDetail.GroupBy(x => new
                        {
                            x.ArtistCd,
                            x.ArtistName
                        })
                                              .Select(x => new Total
                        {
                            ArtistCd       = x.Key.ArtistCd,
                            ArtistName     = x.Key.ArtistName,
                            TotalContracts = x.Sum(y => y.NumberOfContracts),
                            SubTotal       = x.Sum(y => y.TotalPriceOfContracts),
                            MaxCell        = x.Max(y => y.ContractPrices.Count)
                        }).ToList();
                    }

                    result.DateTotal = rowsDetail.GroupBy(x => x.HoldDate)
                                       .Select(x => new Total
                    {
                        Date = x.Key,
                        TotalAttractingCustomers = x.Sum(y => y.NumberOfAttractingCustomers),
                        TotalContracts           = x.Sum(y => y.NumberOfContracts),
                        SubTotal = x.Sum(y => y.TotalPriceOfContracts)
                    }).ToList();
                }
            }

            return(result);
        }