private List <RowDetail> GetRowDetails(List <string> headers, List <DixieProduct> products)
        {
            var list    = new List <RowDetail>();
            var product = products[0];

            foreach (var h in headers)
            {
                var detail = new RowDetail();
                detail.Header = h;
                try {
                    detail.Value = product.GetType().GetProperty(h).GetValue(product, null).ToString();
                }
                catch (NullReferenceException) { }

                list.Add(detail);
            }
            return(list);
        }
Beispiel #2
0
        /// <summary>
        /// Get product detail from contract for purchase statistics B
        /// </summary>
        /// <param name="contract"></param>
        /// <returns>IEnumerable</returns>
        private IEnumerable <RowDetail> DistinctProductFromContract(Contract contract)
        {
            var tempList = new List <RowDetail>();

            if (!string.IsNullOrEmpty(contract.ProductName1))
            {
                var row = new RowDetail
                {
                    ArtistCd   = contract.ArtistCd1 == null ? 0 : contract.ArtistCd1.Value,
                    ArtistName = contract.ArtistName1,
                    // ReSharper disable once PossibleInvalidOperationException
                    HoldDate = contract.ContractDate.Value
                };
                var productPrice = (contract.ProductSalesPrice1 ?? 0) + contract.ProductTaxPrice1 ?? 0;
                row.NumberOfContracts++;
                row.TotalPriceOfContracts += productPrice;
                row.ContractPrices.Add(Math.Floor(productPrice / 10000));
                tempList.Add(row);
            }

            if (!string.IsNullOrEmpty(contract.ProductName2))
            {
                var row          = tempList.FirstOrDefault(x => x.ArtistName.Equals(contract.ArtistName2));
                var productPrice = (contract.ProductSalesPrice2 ?? 0) + contract.ProductTaxPrice2 ?? 0;
                if (row == null)
                {
                    row = new RowDetail
                    {
                        ArtistCd   = contract.ArtistCd2 == null ? 0 : contract.ArtistCd2.Value,
                        ArtistName = contract.ArtistName2,
                        // ReSharper disable once PossibleInvalidOperationException
                        HoldDate = contract.ContractDate.Value
                    };
                    row.NumberOfContracts++;
                    row.ContractPrices.Add(Math.Floor(productPrice / 10000));
                    tempList.Add(row);
                }
                else
                {
                    row.ContractPrices[0] += Math.Floor(productPrice / 10000);
                }

                row.TotalPriceOfContracts += productPrice;
            }

            if (!string.IsNullOrEmpty(contract.ProductName3))
            {
                var row          = tempList.FirstOrDefault(x => x.ArtistName.Equals(contract.ArtistName3));
                var productPrice = (contract.ProductSalesPrice3 ?? 0) + contract.ProductTaxPrice3 ?? 0;
                if (row == null)
                {
                    row = new RowDetail
                    {
                        ArtistCd   = contract.ArtistCd3 == null ? 0 : contract.ArtistCd3.Value,
                        ArtistName = contract.ArtistName3,
                        // ReSharper disable once PossibleInvalidOperationException
                        HoldDate = contract.ContractDate.Value
                    };
                    row.NumberOfContracts++;
                    row.ContractPrices.Add(Math.Floor(productPrice / 10000));
                    tempList.Add(row);
                }
                else
                {
                    row.ContractPrices[0] += Math.Floor(productPrice / 10000);
                }

                row.TotalPriceOfContracts += productPrice;
            }

            if (!string.IsNullOrEmpty(contract.ProductName4))
            {
                var row          = tempList.FirstOrDefault(x => x.ArtistName.Equals(contract.ArtistName4));
                var productPrice = (contract.ProductSalesPrice4 ?? 0) + contract.ProductTaxPrice4 ?? 0;
                if (row == null)
                {
                    row = new RowDetail
                    {
                        ArtistCd   = contract.ArtistCd4 == null ? 0 : contract.ArtistCd4.Value,
                        ArtistName = contract.ArtistName4,
                        // ReSharper disable once PossibleInvalidOperationException
                        HoldDate = contract.ContractDate.Value
                    };
                    row.NumberOfContracts++;
                    row.ContractPrices.Add(Math.Floor(productPrice / 10000));
                    tempList.Add(row);
                }
                else
                {
                    row.ContractPrices[0] += Math.Floor(productPrice / 10000);
                }

                row.TotalPriceOfContracts += productPrice;
            }

            if (!string.IsNullOrEmpty(contract.ProductName5))
            {
                var row          = tempList.FirstOrDefault(x => x.ArtistName.Equals(contract.ArtistName5));
                var productPrice = (contract.ProductSalesPrice5 ?? 0) + contract.ProductTaxPrice5 ?? 0;
                if (row == null)
                {
                    row = new RowDetail
                    {
                        ArtistCd   = contract.ArtistCd5 == null ? 0 : contract.ArtistCd5.Value,
                        ArtistName = contract.ArtistName5,
                        // ReSharper disable once PossibleInvalidOperationException
                        HoldDate = contract.ContractDate.Value
                    };
                    row.NumberOfContracts++;
                    row.ContractPrices.Add(Math.Floor(productPrice / 10000));
                    tempList.Add(row);
                }
                else
                {
                    row.ContractPrices[0] += Math.Floor(productPrice / 10000);
                }

                row.TotalPriceOfContracts += productPrice;
            }

            if (!string.IsNullOrEmpty(contract.ProductName6))
            {
                var row          = tempList.FirstOrDefault(x => x.ArtistName.Equals(contract.ArtistName6));
                var productPrice = (contract.ProductSalesPrice6 ?? 0) + contract.ProductTaxPrice6 ?? 0;
                if (row == null)
                {
                    row = new RowDetail
                    {
                        ArtistCd   = contract.ArtistCd6 == null ? 0 : contract.ArtistCd6.Value,
                        ArtistName = contract.ArtistName6,
                        // ReSharper disable once PossibleInvalidOperationException
                        HoldDate = contract.ContractDate.Value
                    };
                    row.NumberOfContracts++;
                    row.ContractPrices.Add(Math.Floor(productPrice / 10000));
                    tempList.Add(row);
                }
                else
                {
                    row.ContractPrices[0] += Math.Floor(productPrice / 10000);
                }

                row.TotalPriceOfContracts += productPrice;
            }

            return(tempList);
        }
Beispiel #3
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);
        }