public JsonResult ChangeStatus(LoyalityCardViewModel model)
        {
            Response response;
            var      currentUser = GetAuthenticatedUser();

            try
            {
                using (var db = new KiaGalleryContext())
                {
                    var cardList = db.LoyalityCard.Where(x => model.idList.Any(y => y == x.Id)).ToList();
                    foreach (var item in cardList)
                    {
                        if (model.cardStatus == LoyalityCardStatus.SendToBranch)
                        {
                            item.BranchId = model.branchId;
                        }
                        item.CardStatus   = model.cardStatus;
                        item.ModifyUserId = currentUser.Id;
                        item.ModifyDate   = DateTime.Now;
                        item.Ip           = Request.UserHostAddress;

                        var log = new LoyalityCardLog()
                        {
                            LoyalityCardId = item.Id,
                            CardStatus     = model.cardStatus,
                            CreateUserId   = currentUser.Id,
                            CreateDate     = DateTime.Now,
                            Ip             = Request.UserHostAddress
                        };
                        db.LoyalityCardLog.Add(log);
                    }
                    db.SaveChanges();
                }
                response = new Response()
                {
                    status  = 200,
                    message = " وضعیت کارت به " + Enums.GetTitle(model.cardStatus) + " تغییر یافت. "
                };
            }
            catch (Exception ex)
            {
                response = Core.GetExceptionResponse(ex);
            }
            return(Json(response, JsonRequestBehavior.AllowGet));
        }
        public JsonResult Save(LoyalityCardViewModel model)
        {
            Response response;
            var      currentUser = GetAuthenticatedUser();

            try
            {
                using (var db = new KiaGalleryContext())
                {
                    if (model.id > 0)
                    {
                    }
                    else
                    {
                        List <string> codeList = new List <string>();
                        for (int i = 1; i <= model.count; i++)
                        {
                            string code;
                            while (true)
                            {
                                code = RandomString(8);
                                LoyalityCard loyalityCard = db.LoyalityCard.FirstOrDefault(x => x.Code == code);
                                if (loyalityCard == null && codeList.Count(x => x == code) == 0)
                                {
                                    break;
                                }
                            }
                            codeList.Add(code);
                            var entity = new LoyalityCard()
                            {
                                Code         = code,
                                CardType     = model.cardType,
                                CardStatus   = LoyalityCardStatus.Register,
                                CreateUserId = currentUser.Id,
                                ModifyUserId = currentUser.Id,
                                CreateDate   = DateTime.Now,
                                ModifyDate   = DateTime.Now,
                                Ip           = Request.UserHostAddress
                            };
                            var log = new LoyalityCardLog()
                            {
                                LoyalityCard = entity,
                                CardStatus   = LoyalityCardStatus.Register,
                                CreateUserId = currentUser.Id,
                                CreateDate   = DateTime.Now,
                                Ip           = Request.UserHostAddress
                            };
                            db.LoyalityCardLog.Add(log);
                            db.LoyalityCard.Add(entity);
                            var barcode = new Barcode(code, TypeBarcode.Code128C);
                            var bar128  = barcode.Encode(TypeBarcode.Code128C, code, 886, 142);

                            string serverPath = Server.MapPath("~/Upload/LoyalityCard/");
                            if (!Directory.Exists(serverPath))
                            {
                                Directory.CreateDirectory(serverPath);
                            }
                            bar128.Save(serverPath + code + ".jpg");
                        }
                    }
                    db.SaveChanges();
                }
                response = new Response()
                {
                    status  = 200,
                    message = "کارت با موفقیت ایجاد شد."
                };
            }
            catch (Exception ex)
            {
                response = Core.GetExceptionResponse(ex);
            }
            return(Json(response, JsonRequestBehavior.AllowGet));
        }
        public JsonResult ReserveCard(LoyalityCardReserveViewModel model)
        {
            Response response;
            var      currentUser = GetAuthenticatedUser();
            var      birthDate   = DateUtility.GetDateTime(model.persianBirthDate);
            var      mariageDate = DateUtility.GetDateTime(model.persianMariageDate);

            try
            {
                string message = string.Empty;
                using (var db = new KiaGalleryContext())
                {
                    var card     = db.LoyalityCard.Where(x => x.Code == model.code).SingleOrDefault();
                    var customer = db.CustomerLoyality.Where(x => x.PhoneNumber == model.phoneNumber).SingleOrDefault();
                    if (customer == null)
                    {
                        var entity = new CustomerLoyality
                        {
                            LoyalityCardId = card.Id,
                            FirstName      = model.firstName,
                            LastName       = model.lastName,
                            PhoneNumber    = model.phoneNumber,
                            BirthDate      = DateUtility.GetDateTime(model.birthDate),
                            MariageDate    = DateUtility.GetDateTime(model.mariageDate),
                            Date           = DateTime.Now,
                            ModifyUserId   = currentUser.Id,
                            CreateUserId   = currentUser.Id,
                            ModifyDate     = DateTime.Now,
                            CreateDate     = DateTime.Now,
                            Ip             = Request.UserHostAddress,
                        };
                        card.CustomerList.Add(entity);
                    }
                    else
                    {
                        customer.LoyalityCardId = card.Id;
                        customer.MariageDate    = DateUtility.GetDateTime(model.mariageDate);
                        customer.BirthDate      = DateUtility.GetDateTime(model.birthDate);
                        customer.ModifyUserId   = currentUser.Id;
                        customer.ModifyDate     = DateTime.Now;
                        customer.Date           = DateTime.Now;
                    }
                    card.CardStatus = LoyalityCardStatus.Reserved;
                    var log = new LoyalityCardLog()
                    {
                        LoyalityCardId = card.Id,
                        CardStatus     = LoyalityCardStatus.Reserved,
                        CreateUserId   = currentUser.Id,
                        CreateDate     = DateTime.Now,
                        Ip             = Request.UserHostAddress
                    };
                    db.LoyalityCardLog.Add(log);
                    db.SaveChanges();
                    response = new Response()
                    {
                        status  = 200,
                        message = "اطلاعات با موفقیت ثبت شد."
                    };
                }
            }
            catch (Exception ex)
            {
                response = Core.GetExceptionResponse(ex);
            }
            return(Json(response, JsonRequestBehavior.AllowGet));
        }