Пример #1
0
 public static void UpdateFlatrateShipping(long profileid, decimal?[] shipping_cost, int?[] shipping_destination, long subdomainid)
 {
     using (var repository = new TradelrRepository())
     {
         repository.DeleteShippingRules(profileid, subdomainid);
         for (int i = 0; i < shipping_destination.Length; i++)
         {
             var dest = shipping_destination[i];
             var cost = shipping_cost[i];
             if (cost.HasValue)
             {
                 var rule = new shippingRule()
                 {
                     cost          = cost.Value,
                     secondaryCost = cost.Value,
                     country       = dest,
                     matchvalue    = 0,
                     name          = ShippingProfileType.FLATRATE.ToDescriptionString(),
                     profileid     = profileid,
                     ruletype      = (byte)RuleType.PRICE,
                     state         = ""
                 };
                 repository.AddShippingRule(rule);
             }
         }
     }
 }
Пример #2
0
 public static ShippingRule ToModel(this shippingRule value)
 {
     return(new ShippingRule()
     {
         cost = value.cost.ToString("n2"),
         id = value.name, // use name so we can use it to identify shipping method
         name = value.name
     });
 }
Пример #3
0
        public bool ExistDifferentRuleType(shippingRule rule)
        {
            var rowcount =
                db.shippingRules.Count(x => x.country == rule.country && x.state == rule.state && x.profileid == rule.profileid && x.ruletype != rule.ruletype);

            if (rowcount == 0)
            {
                return(false);
            }
            return(true);
        }
Пример #4
0
        public bool ExistShippingRule(shippingRule rule)
        {
            var existing =
                db.shippingRules.SingleOrDefault(x => x.ruletype == rule.ruletype &&
                                                 x.name == rule.name &&
                                                 x.matchvalue == rule.matchvalue &&
                                                 x.country == rule.country &&
                                                 x.state == rule.state &&
                                                 x.profileid == rule.profileid);

            if (existing == null)
            {
                return(false);
            }
            return(true);
        }
Пример #5
0
        public long AddShippingRule(shippingRule rule)
        {
            var existing =
                db.shippingRules.SingleOrDefault(x => x.profileid == rule.profileid && x.ruletype == rule.ruletype && x.matchvalue == rule.matchvalue &&
                                                 x.cost == rule.cost && x.secondaryCost == rule.secondaryCost && x.country == rule.country &&
                                                 x.state == rule.state && x.etsy_templateentryid == rule.etsy_templateentryid);

            if (existing == null)
            {
                db.shippingRules.InsertOnSubmit(rule);
                db.SubmitChanges();
                return(rule.id);
            }
            else
            {
                return(existing.id);
            }
        }
Пример #6
0
        public static ShippingRuleViewModel ToModel(this shippingRule rule, Currency currency, bool ismetric, bool showSymbols = true)
        {
            var viewData = new ShippingRuleViewModel()
            {
                id        = rule.id.ToString(),
                cost      = rule.cost.ToString("n" + currency.decimalCount),
                countryid = rule.country,
                state     = rule.state.ToStateName(rule.country.ToString()),
                name      = rule.name,
                ruleType  = (RuleType)rule.ruletype,
                isMetric  = ismetric,
                currency  = currency.symbol
            };

            viewData.country = rule.country.HasValue ?
                               Country.GetCountry(rule.country.Value).name : "Everywhere Else";
            viewData.matchValue = rule.matchvalue.ToMatchValueDisplay(viewData.ruleType, currency, ismetric, showSymbols);

            return(viewData);
        }
Пример #7
0
        public ActionResult Create(long profileid, decimal cost, int?country, decimal matchvalue, bool metric, string name, string rule_type,
                                   string states_other, string states_us, string states_canadian)
        {
            var rule = new shippingRule
            {
                country       = country,
                cost          = cost,
                secondaryCost = cost,
                matchvalue    = matchvalue,
                name          = name,
                profileid     = profileid
            };

            switch (country)
            {
            case 185:     // USA
                rule.state = states_us;
                break;

            case 32:     // Canada
                rule.state = states_canadian;
                break;

            default:
                rule.state = states_other;
                break;
            }

            switch (rule_type)
            {
            case "weight":
                if (!metric)
                {
                    rule.matchvalue = matchvalue.ConvertWeight(true);
                }
                else
                {
                    rule.matchvalue = matchvalue;
                }
                rule.ruletype = (byte)RuleType.WEIGHT;
                break;

            case "price":
                rule.matchvalue = matchvalue;
                rule.ruletype   = (byte)RuleType.PRICE;
                break;

            default:
                return(Json("Invalid rule".ToJsonFail()));
            }

            try
            {
                // check if entry already exist
                if (repository.ExistShippingRule(rule))
                {
                    return(Json("Rule already exist".ToJsonFail()));
                }

                // check that the rule we're adding is the same for the current destination
                if (repository.ExistDifferentRuleType(rule))
                {
                    return(Json("Different rule types for the same destination are not allowed".ToJsonFail()));
                }

                repository.AddShippingRule(rule);
            }
            catch (Exception ex)
            {
                return(SendJsonErrorResponse(ex));
            }

            var usr      = repository.GetUserById(sessionid.Value, subdomainid.Value);
            var settings = (UserSettings)usr.settings;
            var currency = usr.organisation1.MASTERsubdomain.currency.ToCurrency();

            var viewdata = rule.ToModel(currency, settings.HasFlag(UserSettings.METRIC_VIEW));

            return(Json(viewdata.ToJsonOKData()));
        }