public Promotion GetPromotionFromProductGroup(ProductGroup prodGroup, string promotionName)
 {
     var promo = prodGroup.Promotions.FirstOrDefault(m => m.PromotionName == promotionName && !m.IsDeleted);
     return promo;
 }
 public void SaveProductPromotion(int customerFk, CampaignSetupModel model, CampaignSetupModel oldModel)
 {
     using (var dbcontext = new SemplestModel.Semplest())
     {
         var queryProdGrp = from c in dbcontext.ProductGroups
                            where
                                c.CustomerFK == customerFk &&
                                c.ProductGroupName == model.ProductGroup.ProductGroupName
                            select c;
         if (!queryProdGrp.Any())
         {
             //create new productgroup and promotion
             var prodgroup = new ProductGroup
             {
                 ProductGroupName = model.ProductGroup.ProductGroupName,
                 IsActive = true,
                 CustomerFK = customerFk,
                 StartDate =
                     Convert.ToDateTime(model.ProductGroup.StartDate,
                                        new CultureInfo("en-Us")),
                 EndDate =
                     String.IsNullOrEmpty(model.ProductGroup.EndDate)
                         ? (DateTime?)null
                         : Convert.ToDateTime(model.ProductGroup.EndDate)
             };
             var promo = CreatePromotionFromModel(model,
                                                     dbcontext.Configurations.First().
                                                         CustomerDefaultPerCampaignFlatFeeAmount);
             dbcontext.ProductGroups.Add(prodgroup);
             dbcontext.Promotions.Add(promo);
             SavePromotionAdEngineSelected(promo,model, dbcontext);
             dbcontext.SaveChanges();
             _savedCampaign = true;
         }
         else
         {//productgroupexists
             var updateProdGrp = queryProdGrp.Single();
             var updatePromotion = GetPromotionFromProductGroup(updateProdGrp,
                                                                model.ProductGroup.ProductPromotionName);
             // if this is null means promotion name changed so create a new promotion
             if (updatePromotion == null)
             {
                 // create new promotion
                 updatePromotion = CreatePromotionFromModel(model,
                                                            dbcontext.Configurations.First().
                                                                CustomerDefaultPerCampaignFlatFeeAmount);
                 updatePromotion.ProductGroupFK = updateProdGrp.ProductGroupPK;
                 dbcontext.Promotions.Add(updatePromotion);
                 SavePromotionAdEngineSelected(updatePromotion, model, dbcontext);
             }
             else
             {
                 // update promotion
                 UpdatePromotionFromModel(updatePromotion, model, dbcontext.Configurations.First().CustomerDefaultPerCampaignFlatFeeAmount);
                 if (updatePromotion.IsLaunched)
                 {
                     var sw = new ServiceClientWrapper();
                     var adEngines = new List<string>();
                     adEngines.AddRange(
                         updatePromotion.PromotionAdEngineSelecteds.Select(
                             pades => pades.AdvertisingEngine.AdvertisingEngine1));
                     if (model.ProductGroup.Budget != oldModel.ProductGroup.Budget)
                         sw.scheduleUpdateBudget(updatePromotion.PromotionPK, model.ProductGroup.Budget,
                                                 adEngines);
                     if (Convert.ToDateTime(model.ProductGroup.StartDate) != Convert.ToDateTime(oldModel.ProductGroup.StartDate))
                         sw.scheduleChangePromotionStartDate(updatePromotion.PromotionPK,
                                                             updatePromotion.PromotionStartDate, adEngines);
                 }
                 SavePromotionAdEngineSelected(updatePromotion, model, dbcontext);
             }
         }
         dbcontext.SaveChanges();
         // we need to set this because the _dbcontext is  and campaign is updated so reflect changes we need to create new context
         _savedCampaign = true;
     }
 }
        public string SavePromotionDetails(SmartWordSetupModel model, SmartWordSetupModel oldModel, int customerFk)
        {
            var rString = new System.Text.StringBuilder();
            using (var dbcontext = new SemplestModel.Semplest())
            {
                var queryProdGrp = (from c in dbcontext.ProductGroups
                                    where
                                        c.CustomerFK == customerFk &&
                                        c.ProductGroupName == model.ProductGroup.ProductGroupName
                                    select c).SingleOrDefault();
                Promotion promo;
                if (queryProdGrp == null)
                {
                    //create new productgroup and promotion
                    var prodgroup = new ProductGroup
                                        {
                                            ProductGroupName = model.ProductGroup.ProductGroupName,
                                            IsActive = true,
                                            CustomerFK = customerFk,
                                            StartDate =
                                                Convert.ToDateTime(model.ProductGroup.StartDate,
                                                                   new CultureInfo("en-Us")),
                                            EndDate =
                                                String.IsNullOrEmpty(model.ProductGroup.EndDate)
                                                    ? (DateTime?) null
                                                    : Convert.ToDateTime(model.ProductGroup.EndDate)
                                        };

                    if (model.LandingUrl != null)
                        model.LandingUrl = model.LandingUrl.Trim();
                    promo = CreatePromotionFromModel(model);
                    dbcontext.ProductGroups.Add(prodgroup);
                    dbcontext.Promotions.Add(promo);
                    dbcontext.SaveChanges();
                }
                else
                {
                    var updateProdGrp = queryProdGrp;
                    promo = GetPromotionFromProductGroup(updateProdGrp,
                                                         model.ProductGroup.ProductPromotionName);
                    // if this is null means promotion name changed so create a new promotion
                    if (promo == null)
                    {
                        // create new promotion
                        promo = CreatePromotionFromModel(model);
                        promo.ProductGroupFK = updateProdGrp.ProductGroupPK;
                        dbcontext.Promotions.Add(promo);
                    }
                    else
                    {
                        promo.LandingPageURL = model.LandingUrl.Trim();
                        promo.PromotionName = model.ProductGroup.ProductPromotionName;
                        promo.PromotionDescription = model.ProductGroup.Words;
                    }
                }


                GeoTargetTableType gt;
                int addressCode = -1;
                var st = dbcontext.AddressTypes.SingleOrDefault(pt => pt.AddressType1 == model.PromotionAddressType);
                if (st != null)
                    addressCode = st.AddressTypePK;
                AddGeoTargetingToPromotion(promo, model, oldModel, out gt);
      

                var parameter = new SqlParameter("PromotionPK", promo.PromotionPK) {SqlDbType = SqlDbType.Int};
                var parameter2 = new SqlParameter("LandingUrl", model.LandingUrl.Trim())
                                     {SqlDbType = SqlDbType.NVarChar};
                var parameter3 = new SqlParameter("DisplayUrl", string.Empty) {SqlDbType = SqlDbType.NVarChar};
                var parameter4 = new SqlParameter("AddressTypeFK", addressCode) {SqlDbType = SqlDbType.Int};
                var parameter5 = new SqlParameter("GeoTVP", gt)
                                     {SqlDbType = SqlDbType.Structured, TypeName = "GeoTargetTableType"};
                var parameter6 = new SqlParameter("AdTVP", null)
                                     {SqlDbType = SqlDbType.Structured, TypeName = "PromoAdTableType"};
                var parameters = new object[] {parameter, parameter2, parameter3, parameter4, parameter5, parameter6};
                var results =
                    ((IObjectContextAdapter) dbcontext).ObjectContext.ExecuteStoreQuery<RVal>(
                        "exec UpdateGeoTargetingPromoAds @PromotionPK, @LandingUrl, @DisplayUrl, @AddressTypeFK, @GeoTVP, @AdTVP",
                        parameters);
                foreach (var r in results)
                {
                    rString.Append(r.UID);
                    rString.Append("=");
                    rString.Append(r.PKEY);
                    rString.Append(",");
                }















                dbcontext.SaveChanges();
            }
            return string.IsNullOrEmpty(rString.ToString())
                       ? String.Empty
                       : rString.ToString().Substring(0, rString.ToString().Length - 1);
        }
 private Promotion GetPromotionFromProductGroup(ProductGroup prodGroup, string promotionName)
 {
     var promo = prodGroup.Promotions.SingleOrDefault(m => m.PromotionName == promotionName && !m.IsDeleted);
     return promo;
 }
        public void SaveProductGroupAndCampaign(int userid, CampaignSetupModel model)
        {
            using (var dbcontext = new SemplestModel.Semplest())
            {
                // get the customerfk from userid
                var queryCustFk = from c in dbcontext.Users where c.UserPK == userid select c.CustomerFK;
                var i = queryCustFk.FirstOrDefault();
                if (i != null)
                {
                    var custfk = (int)i;

                    // check if the ProductGroupName already exists
                    var queryProdGrp = from c in dbcontext.ProductGroups
                                       where c.CustomerFK == custfk && c.ProductGroupName == model.ProductGroup.ProductGroupName
                                       select c;
                    if (queryProdGrp.Any())
                    {
                        // product grp already exists so update the product group 
                        var updateProdGrp = queryProdGrp.FirstOrDefault();
                        if (updateProdGrp != null)
                        {
                            updateProdGrp.ProductGroupName = model.ProductGroup.ProductGroupName;
                            updateProdGrp.StartDate = Convert.ToDateTime(model.ProductGroup.StartDate, new CultureInfo("en-Us"));
                            updateProdGrp.EndDate = String.IsNullOrEmpty(model.ProductGroup.EndDate) ? (DateTime?)null : Convert.ToDateTime(model.ProductGroup.EndDate);

                            // get promotion and update it
                            var updatePromotion = GetPromotionFromProductGroup(updateProdGrp, model.ProductGroup.ProductPromotionName);
                            // if this is null means promotion name changed so create a new promotion
                            if (updatePromotion == null)
                            {
                                // create new promotion
                                updatePromotion = CreatePromotionFromModel(model, dbcontext.Configurations.First().CustomerDefaultPerCampaignFlatFeeAmount);
                                updatePromotion.ProductGroupFK = updateProdGrp.ProductGroupPK;

                                // add geotargeting to promotion
                                AddGeoTargetingToPromotion(updatePromotion, model, custfk);
                                // promotion ads
                                AddPromotionAdsToPromotion(updatePromotion, model, custfk);

                                dbcontext.Promotions.Add(updatePromotion);
                            }
                            else
                            {
                                // update promotion
                                UpdatePromotionFromModel(updatePromotion, model, dbcontext, custfk);
                            }
                        }

                        dbcontext.SaveChanges();

                        // we need to set this because the _dbcontext is  and campaign is updated so reflect changes we need to create new context
                        _savedCampaign = true;
                    }
                    else
                    {
                        // create product group
                        var prodgroup = new ProductGroup
                        {
                            ProductGroupName = model.ProductGroup.ProductGroupName,
                            IsActive = true,
                            CustomerFK = custfk,
                            StartDate = Convert.ToDateTime(model.ProductGroup.StartDate, new CultureInfo("en-Us")),
                            EndDate = String.IsNullOrEmpty(model.ProductGroup.EndDate) ? (DateTime?)null : Convert.ToDateTime(model.ProductGroup.EndDate)
                        };

                        // create promotion
                        var promo = CreatePromotionFromModel(model, dbcontext.Configurations.First().CustomerDefaultPerCampaignFlatFeeAmount);

                        // add advertising engines that are selected
                        SavePromotionAdEngineSelected(promo, model, dbcontext);

                        // add geotargeting to promotion
                        AddGeoTargetingToPromotion(promo, model, custfk);

                        // save site links
                        AddSiteLinksToPromotion(promo, model, custfk);

                        // promotion ads
                        AddPromotionAdsToPromotion(promo, model, custfk);

                        // add product group
                        dbcontext.ProductGroups.Add(prodgroup);
                        // add promotion
                        dbcontext.Promotions.Add(promo);
                        dbcontext.SaveChanges();

                        // save negative keywords
                        SaveNegativeKeywords(promo, model, dbcontext);

                        // we need to set this because the _dbcontext is  and campaign is updated so reflect changes we need to create new context
                        _savedCampaign = true;
                    }
                }
            }
        }