Пример #1
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"));
        }
        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"));
        }
Пример #3
0
        public ActionResult SetSubpurchaseChecked(Guid subpurchaseID, string phone)
        {
            if (!SystemUtils.Authorization.IsAdmin)
            {
                return(RedirectToAction("NotAdminUser", "Authentication"));
            }

            var dataModel = new DataModel();

            var subpurchase = dataModel.SubPurchases
                              .SingleOrDefault(s => s.id == subpurchaseID);

            if (subpurchase == null)
            {
                ViewBag.ErrorMessage = "Посредник не найден по текущему ID.";
                return(PartialView("~/Views/Controls/FormSubmitSaveFailed.cshtml"));
            }

            subpurchase.not_checked = false;

            var subpurchasePhones = dataModel.SubPurchasePhones
                                    .Where(sp => sp.SubPurchaseId == subpurchaseID)
                                    .Select(sp => sp.phone);

            if (subpurchasePhones.Any())
            {
                var phonesList = new List <string>();
                foreach (var _phone in subpurchasePhones)
                {
                    var tempList = subpurchase.GetPhoneFormatsList(_phone);
                    phonesList.AddRange(
                        tempList.Where(p => !phonesList.Contains(p) && !subpurchasePhones.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);
                }
            }

            dataModel.SubmitChanges();

            //--- remove advertisments
            var subpurchaseAdvertisments = dataModel.Advertisments
                                           .GroupJoin(
                dataModel.AdvertismentPhones,
                a => a.Id,
                ap => ap.AdvertismentId,
                (a, ap) => new { Advertisment = a, Phones = ap }
                )
                                           .Where(x => x.Phones.Any(p => p.phone == phone))
                                           .Select(x => x.Advertisment);

            foreach (var advertisment in subpurchaseAdvertisments)
            {
                advertisment.SubPurchase_Id          = subpurchaseID;
                advertisment.subpurchaseAdvertisment = true;
            }

            dataModel.SubmitChanges();

            return(PartialView("~/Views/Controls/FormSubmitSaveSuccess.cshtml"));
        }