Exemplo n.º 1
0
        public static double?EvaluateCost(int id, double weight, double height, double length, double width, int number = 1)
        {
            double?cost = -1;

            using (ModelDBContainer db = new ModelDBContainer())
            {
                try
                {
                    string           formula = db.Tariffs.Find(id).Formula;
                    NumberFormatInfo format  = new NumberFormatInfo();
                    format.NumberDecimalSeparator = ".";
                    formula = formula.Replace("weight", weight.ToString(format));
                    formula = formula.Replace("height", height.ToString(format));
                    formula = formula.Replace("length", length.ToString(format));
                    formula = formula.Replace("width", width.ToString(format));
                    formula = formula.Replace("number", number.ToString());
                    var options = ScriptOptions.Default.WithImports("System.Math");
                    cost = CSharpScript.EvaluateAsync <double>(formula, options).Result;
                }
                catch (Exception)
                {
                    cost = null;
                }
            }
            return(cost);
        }
Exemplo n.º 2
0
 private static void LoadSource()
 {
     using (ModelDBContainer db = new ModelDBContainer())
     {
         cities = new Dictionary <int, string>();
         foreach (City c in db.Cities)
         {
             cities.Add(c.Id, c.NameCity);
             citiesFullNames.Add(c.Id, Helper.GetCityString(c));
         }
         centres = new Dictionary <int, string>();
         foreach (DistributionCentre c in db.DistributionCentres)
         {
             centres.Add(c.Id, Helper.GetCentreString(c));
         }
         tariffs = new Dictionary <int, string>();
         foreach (Tariff c in db.Tariffs)
         {
             tariffs.Add(c.Id, c.Name);
         }
         descriptions = Properties.Settings.Default.Descriptions.Cast <string>().ToList();
     }
 }
Exemplo n.º 3
0
        public static string ParseXML(XDocument d)
        {
            LoadSource();
            countMistakes = 0;
            countSuccess  = 0;
            curCount      = 1;
            Mistakes      = "";

            foreach (XElement add in d.Root.Elements())
            {
                var from = add.Element("from");
                var to   = add.Element("to");

                surnameFrom       = from.Attribute("surname")?.Value;
                nameFrom          = from.Attribute("name")?.Value;
                midnameFrom       = from.Attribute("midname")?.Value;
                companyFrom       = from.Attribute("company")?.Value;
                phoneFrom         = from.Attribute("phone")?.Value;
                mailFrom          = from.Attribute("mail")?.Value;
                indexFrom         = from.Attribute("index")?.Value;
                addressFrom       = from.Attribute("address")?.Value;
                cityFrom          = from.Attribute("city")?.Value;
                informingSMSFrom  = Convert.ToBoolean(from.Attribute("informingSMS") == null ? "false" : from.Attribute("informingSMS").Value);
                informingMailFrom = Convert.ToBoolean(from.Attribute("informingMail") == null ? "false" : from.Attribute("informingMail").Value);

                surnameTo       = to.Attribute("surname")?.Value;
                nameTo          = to.Attribute("name")?.Value;
                midnameTo       = to.Attribute("midname")?.Value;
                companyTo       = to.Attribute("company")?.Value;
                phoneTo         = to.Attribute("phone")?.Value;
                mailTo          = to.Attribute("mail")?.Value;
                indexTo         = to.Attribute("index")?.Value;
                addressTo       = to.Attribute("address")?.Value;
                cityTo          = to.Attribute("city")?.Value;
                informingSMSTo  = Convert.ToBoolean(to.Attribute("informingSMS") == null ? "false" : to.Attribute("informingSMS").Value);
                informingMailTo = Convert.ToBoolean(to.Attribute("informingMail") == null ? "false" : to.Attribute("informingMail").Value);

                var temp = from.Attribute("idCentre");
                idCentreFrom = temp == null ? null : (int?)Convert.ToInt32(temp.Value);
                temp         = to.Attribute("idCentre");
                idCentreTo   = temp == null ? null : (int?)Convert.ToInt32(temp.Value);

                weightDouble = add.Attribute("weight") == null ? (double?)null : Convert.ToDouble(add.Attribute("weight").Value, CultureInfo.InvariantCulture);
                lengthDouble = add.Attribute("length") == null ? (double?)null : Convert.ToDouble(add.Attribute("length").Value, CultureInfo.InvariantCulture);
                widthtDouble = add.Attribute("width") == null ? (double?)null : Convert.ToDouble(add.Attribute("width").Value, CultureInfo.InvariantCulture);
                heightDouble = add.Attribute("height") == null ? (double?)null : Convert.ToDouble(add.Attribute("height").Value, CultureInfo.InvariantCulture);
                temp         = add.Attribute("number");
                numberInt    = temp == null ? null : (int?)Convert.ToInt32(temp.Value);
                temp         = add.Attribute("value");
                valueDouble  = temp == null ? null : (int?)Convert.ToDouble(temp.Value, CultureInfo.InvariantCulture);
                description  = add.Attribute("description")?.Value;
                tariff       = add.Attribute("tariff")?.Value;

                if (!CheckFromTo(ref surnameFrom, ref nameFrom, ref midnameFrom, ref companyFrom, ref phoneFrom, ref mailFrom, ref indexFrom,
                                 ref addressFrom, ref idCentreFrom, out idCityFrom, cityFrom, informingSMSFrom, informingMailFrom))
                {
                    countMistakes++;
                    Mistakes += curCount + ", ";
                    curCount++;
                    continue;
                }
                if (!CheckFromTo(ref surnameTo, ref nameTo, ref midnameTo, ref companyTo, ref phoneTo, ref mailTo, ref indexTo,
                                 ref addressTo, ref idCentreTo, out idCityTo, cityTo, informingSMSTo, informingMailTo))
                {
                    countMistakes++;
                    Mistakes += curCount + ", ";
                    curCount++;
                    continue;
                }
                if (!CheckPackage())
                {
                    countMistakes++;
                    Mistakes += curCount + ", ";
                    curCount++;
                    continue;
                }

                using (ModelDBContainer db = new ModelDBContainer())
                {
                    Person From = new Person
                    {
                        Name          = nameFrom,
                        MiddleName    = midnameFrom,
                        Surname       = surnameFrom,
                        Company       = companyFrom,
                        Phone         = phoneFrom,
                        Mail          = mailFrom,
                        Index         = indexFrom,
                        CityId        = idCityFrom,
                        Address       = addressFrom,
                        CentreId      = idCentreFrom,
                        InformingSMS  = informingSMSFrom,
                        InformingMail = informingMailFrom
                    };
                    Person To = new Person
                    {
                        Name          = nameTo,
                        MiddleName    = midnameTo,
                        Surname       = surnameTo,
                        Company       = companyTo,
                        Phone         = phoneTo,
                        Mail          = mailTo,
                        Index         = indexTo,
                        CityId        = idCityTo,
                        Address       = addressTo,
                        CentreId      = idCentreTo,
                        InformingSMS  = informingSMSTo,
                        InformingMail = informingMailTo
                    };
                    db.Persons.Add(From); db.Persons.Add(To);
                    db.SaveChanges();
                    Package package = new Package
                    {
                        PersonIdFrom     = From.Id,
                        PersonIdTo       = To.Id,
                        Weight           = weightDouble,
                        Width            = widthtDouble,
                        Length           = lengthDouble,
                        Height           = heightDouble,
                        DeclaredValue    = valueDouble,
                        Cost             = costDouble,
                        TariffId         = idTariff,
                        Description      = description,
                        NumberOfPackages = numberInt == null ? 1 : (int)numberInt
                    };
                    db.Packages.Add(package);
                    db.SaveChanges();
                    db.Records.Add(new Record {
                        PackageId = package.Id, DateAndTime = DateTime.Now, Status = RegisterStatus
                    });
                    db.SaveChanges();
                }
                countSuccess++;
                curCount++;
            }
            string message = "Всего записей об отправлениях: " + (curCount - 1) +
                             ". Успешно загружено: " + countSuccess + ". Не  загружено по причине ошибки: " + countMistakes + ".";

            if (Mistakes.Length > 0)
            {
                message += " Порядковые номера записей с ошибками: " + Mistakes.Substring(0, Mistakes.Length - 2) + ".";
            }
            return(message);
        }