public JsonResult GetExpiredCustomer(CustomerLoyalitySearchViewModel model)
        {
            var      currentUser = GetAuthenticatedUser();
            Response response;
            int      dataCount;

            try
            {
                using (var db = new KiaGalleryContext())
                {
                    var query = db.CustomerFactor.Where(x => x.PurchaseType == PurchaseType.Return && DbFunctions.AddDays(x.Date, 30) < x.ReturnDate);
                    if (model.branchId != null)
                    {
                        query = query.Where(x => x.BranchId == model.branchId);
                    }
                    dataCount = query.Count();
                    query     = query.OrderByDescending(x => x.Id).Skip(model.page * model.count).Take(model.count);
                    var list = query.Select(x => new CustomerLoyalityViewModel
                    {
                        id           = x.Id,
                        fullName     = x.CustomerLoyality.FirstName + " " + x.CustomerLoyality.LastName,
                        phoneNumber  = x.CustomerLoyality.PhoneNumber,
                        branchId     = x.BranchId,
                        branchName   = x.Branch.Name,
                        date         = x.Date,
                        returnDate   = x.ReturnDate,
                        factorPrice  = x.FactorPrice,
                        factorNumber = x.FactorNumber,
                        purchaseType = x.PurchaseType
                    }).ToList();
                    list.ForEach(x =>
                    {
                        x.persianDate         = DateUtility.GetPersianDate(x.date);
                        x.persianReturnDate   = DateUtility.GetPersianDate(x.returnDate);
                        x.separateFactorPrice = Core.ToSeparator(x.factorPrice);
                        x.purchaseTypeTitle   = Enums.GetTitle(x.purchaseType);
                    });

                    response = new Response()
                    {
                        status = 200,
                        data   = new
                        {
                            list = list,

                            pageCount = Math.Ceiling((double)dataCount / model.count),
                            count     = dataCount,
                            page      = model.page + 1
                        }
                    };
                }
            }
            catch (Exception ex)
            {
                response = Core.GetExceptionResponse(ex);
            }
            return(Json(response, JsonRequestBehavior.AllowGet));
        }
        public JsonResult Search(CustomerLoyalitySearchViewModel model)
        {
            var      currentUser = GetAuthenticatedUser();
            Response response;
            int      dataCount;

            try
            {
                var customerCount = 0;
                using (var db = new KiaGalleryContext())
                {
                    var query = db.CustomerLoyality.Select(x => x);
                    if (User.IsInRole("admin"))
                    {
                        customerCount = query.Count();
                    }
                    if (model.goal)
                    {
                        query = query.Where(x => x.CustomerFactorList.Where(y => y.PurchaseType == PurchaseType.Buy).Sum(y => y.FactorPrice) - ((long?)x.CustomerFactorList.Where(y => y.PurchaseType == PurchaseType.Return).Sum(y => y.FactorPrice) ?? 0) >= model.priceGoal || x.CustomerFactorList.Where(y => y.PurchaseType == PurchaseType.Buy).Count() >= model.countGoal);
                    }
                    if (currentUser.BranchType == BranchType.Solicitorship)
                    {
                        query = query.Where(x => x.CustomerFactorList.Where(y => y.PurchaseType == PurchaseType.Buy).Any(y => y.BranchId == currentUser.BranchId));
                    }
                    if (model.factorPrice != null && model.factorPrice > 0)
                    {
                        query = query.Where(x => x.CustomerFactorList.Where(y => y.PurchaseType == PurchaseType.Buy).Sum(y => y.FactorPrice) - ((long?)x.CustomerFactorList.Where(y => y.PurchaseType == PurchaseType.Return).Sum(y => y.FactorPrice) ?? 0) >= model.factorPrice);
                    }
                    if (model.factorCount != null && model.factorCount > 0)
                    {
                        query = query.Where(x => x.CustomerFactorList.Where(y => y.PurchaseType == PurchaseType.Buy).Count() >= model.factorCount);
                    }
                    ;


                    dataCount = query.Count();
                    query     = query.OrderByDescending(x => x.CustomerFactorList.Where(y => y.PurchaseType == PurchaseType.Buy).Sum(y => y.FactorPrice) - ((long?)x.CustomerFactorList.Where(y => y.PurchaseType == PurchaseType.Return).Sum(y => y.FactorPrice) ?? 0)).Skip(model.page * model.count).Take(model.count);

                    var list = query.Select(x => new CustomerLoyalitySearchViewModel
                    {
                        id                = x.Id,
                        fullName          = x.FirstName + " " + x.LastName,
                        phoneNumber       = x.PhoneNumber,
                        returnFactorPrice = x.CustomerFactorList.Where(y => y.PurchaseType == PurchaseType.Return).Sum(y => y.FactorPrice),
                        factorPrice       = x.CustomerFactorList.Where(y => y.PurchaseType == PurchaseType.Buy).Sum(y => y.FactorPrice),
                        factorCount       = x.CustomerFactorList.Where(y => y.PurchaseType == PurchaseType.Buy).Count(),
                        branchName        = x.CustomerFactorList.Select(y => y.Branch.Name).FirstOrDefault(),
                    }).ToList();
                    //list.ForEach(x =>
                    //{
                    //    x.separateFactorPrice = Core.ToSeparator(x.factorPrice);
                    //});

                    response = new Response()
                    {
                        status = 200,
                        data   = new
                        {
                            list          = list,
                            customerCount = Core.ToSeparator(customerCount),
                            //factorPrice = list.Sum(x => x.factorPrice),
                            pageCount = Math.Ceiling((double)dataCount / model.count),
                            count     = dataCount,
                            page      = model.page + 1
                        }
                    };
                }
            }
            catch (Exception ex)
            {
                response = Core.GetExceptionResponse(ex);
            }
            return(Json(response, JsonRequestBehavior.AllowGet));
        }