public JsonResult NotifySubpurchaseAdvertisment(int advertismentID)
        {
            var dataModel = new DataModel();

            var advertismentPhones = dataModel.AdvertismentPhones
                                     .Where(ap => ap.AdvertismentId == advertismentID);

            bool isExists = true;

            foreach (var advertismentPhone in advertismentPhones)
            {
                if (!dataModel.SubPurchasePhones
                    .Any(s => s.phone == advertismentPhone.phone))
                {
                    isExists = false;
                    break;
                }
            }

            if (!isExists)
            {
                Guid subpurchaseID = Guid.NewGuid();
                var  subpurchase   = new SubPurchase()
                {
                    id          = subpurchaseID,
                    createDate  = SystemUtils.Utils.Date.GetUkranianDateTimeNow(),
                    modifyDate  = SystemUtils.Utils.Date.GetUkranianDateTimeNow(),
                    not_checked = true
                };
                dataModel.SubPurchases.InsertOnSubmit(subpurchase);

                foreach (var advertismentPhone in advertismentPhones)
                {
                    var subpurchasePhone = new SubPurchasePhone()
                    {
                        Id            = Guid.NewGuid(),
                        createDate    = SystemUtils.Utils.Date.GetUkranianDateTimeNow(),
                        phone         = advertismentPhone.phone,
                        SubPurchaseId = subpurchaseID
                    };
                    dataModel.SubPurchasePhones.InsertOnSubmit(subpurchasePhone);
                }

                dataModel.SubmitChanges();
            }
            else
            {
                if (SystemUtils.Authorization.IsAdmin)
                {
                    var advertisment = dataModel.Advertisments
                                       .SingleOrDefault(a => a.Id == advertismentID);
                    if (advertisment != null)
                    {
                        advertisment.not_show_advertisment = true;
                        dataModel.SubmitChanges();
                    }
                    return(Json("Already exists in db"));
                }
            }

            return(Json("success"));
        }
Пример #2
0
        public ActionResult AddSubpurchaseAndPhones(SiteMVC.ViewModels.SubPurchase subpurchase)
        {
            if (!SystemUtils.Authorization.IsAdmin)
            {
                return(RedirectToAction("NotAdminUser", "Authentication"));
            }

            var dataModel = new DataModel();

            Guid subpurchaseID = Guid.NewGuid();
            var  dbSubpurchase = new SubPurchase()
            {
                id          = subpurchaseID,
                createDate  = SystemUtils.Utils.Date.GetUkranianDateTimeNow(),
                modifyDate  = SystemUtils.Utils.Date.GetUkranianDateTimeNow(),
                name        = subpurchase.FirstName,
                surname     = subpurchase.LastName,
                phone       = null,
                not_checked = false
            };

            dataModel.SubPurchases.InsertOnSubmit(dbSubpurchase);

            if (subpurchase.Phones.Any())
            {
                var phonesList = new List <string>();
                foreach (var phone in subpurchase.Phones)
                {
                    var tempList = dbSubpurchase.GetPhoneFormatsList(phone);
                    phonesList.AddRange(
                        tempList.Where(p => !phonesList.Contains(p))
                        );
                }

                foreach (var phone in phonesList)
                {
                    var dbSubpurchasePhone = new SubPurchasePhone()
                    {
                        Id            = Guid.NewGuid(),
                        phone         = phone,
                        createDate    = SystemUtils.Utils.Date.GetUkranianDateTimeNow(),
                        SubPurchaseId = subpurchaseID
                    };
                    dataModel.SubPurchasePhones.InsertOnSubmit(dbSubpurchasePhone);
                }
            }
            else
            {
                ViewBag.ErrorMessage = "Не найдено ни одного телефона для добавления.";
                return(PartialView("~/Views/Controls/FormSubmitSaveFailed.cshtml"));
            }

            dataModel.SubmitChanges();

            foreach (var subpurchasePhone in dbSubpurchase.SubPurchasePhones)
            {
                var advertismentIDs = dataModel.AdvertismentPhones
                                      .Where(ap => ap.phone == subpurchasePhone.phone)
                                      .Select(ap => ap.AdvertismentId);
                foreach (var advertismentID in advertismentIDs)
                {
                    var advertisment = dataModel.Advertisments
                                       .SingleOrDefault(a => a.Id == advertismentID);
                    if (advertisment != null)
                    {
                        advertisment.not_show_advertisment = true;
                        dataModel.SubmitChanges();
                    }
                }
            }
            return(PartialView("~/Views/Controls/FormSubmitSaveSuccess.cshtml"));
        }