Standorte

Kombinieren Sie MAP+PLUS mit PRINT+PLUS und geben Sie Standortinformationen auf Ihren Dokumenten aus.

Das nachfolgende Skript ermittelt umliegende Standorte für die Ausgabe in PRINT+PLUS.

namespace Ruthardt.PrintPlus.Skripting
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    using System.Windows.Forms;
    using Ruthardt.CobraBase.Functions.Access.Ado;
    using Ruthardt.CobraFramework.Ado;
    using Ruthardt.PrintPlus.Export.Context;
    using Ruthardt.PrintPlus.Model.Interfaces;
    using Ruthardt.PrintPlus.Model.Configuration;

    public class ExampleScript : IScriptAction
    {
        public void Execute(IPrintContext printContext, ICurrentContext currentContext, IChildContext childContext)
        {
            var currentAdoView = currentContext.AdoView;

            var addressAdoView = currentContext.ParentAdoViewByPrefix["Adresse"];
            string sql = 
                "DECLARE @origin geography " + 
                "SELECT @origin = (SELECT Location FROM Ruthardt_Locations WHERE SUPERID = @AdressId) " + 
                "SELECT TOP(3) Ruthardt_Locations.SuperId, Location.STDistance(@origin) as DISTANCE "+
                "FROM Ruthardt_Locations " +
                "JOIN [#Adressen#] ON Ruthardt_Locations.SuperId = [#Adressen.ID#] "+
                "WHERE Ruthardt_Locations.Location IS NOT NULL " +
                "AND Ruthardt_Locations.TotalScore > 70 " +
                "AND [#Adressen.Kundenart#] = 'Intern' " +
                "AND [#Adressen.Haupt#] = 'H' " +
                "ORDER BY Ruthardt_Locations.Location.STDistance(@origin) ASC";

            var table = printContext.AdoAccess.GetTable(sql, new SqlParameter("@AdressId", addressAdoView.CurrentKey));
            var addressIdsWithDistance = table.Rows.Cast<DataRow>().ToDictionary(row => (int)row["SuperId"], row => (double)row["DISTANCE"]);

            var standortTable = printContext.AdoAccess.GetTable("SELECT * FROM [#Adressen#] WHERE ID IN (" + string.Join(",", addressIdsWithDistance.Keys) + ")");

            var configNode = printContext.Config.Current.Children.First(node => node.Prefix == "Standorte");
            var standorte = new List<IChildContext>();
            foreach(DataRow row in standortTable.Rows)
            {
                var adoView = new AdoView(row, "Adressen", printContext.AdoAccess);

                var distance = addressIdsWithDistance[(int)row["ID"]];
                string stringDistance;
                if (distance == 0)
                {
                    stringDistance = string.Empty;
                }
                else if (distance < 1000)
                {
                    stringDistance = Math.Round(distance / 1000, 1).ToString("n1") + " km";
                }
                else
                {
                    stringDistance = Math.Round(distance / 1000, 0).ToString("n0") + " km";
                }

                adoView.SetValue("Distanz_Standort", stringDistance);
                adoView.SetValue("Distanz_Standort_Zahl", distance);

                standorte.Add(new ChildContext(adoView, currentContext, configNode));
            }

            currentContext.ChildContextByPrefix["Standorte"] = standorte.OrderBy(standort => standort.AdoView.GetDecimalValue("Distanz_Standort_Zahl")).ToList();
        }
    }
}

Last updated

p17 GmbH

Impressum

p17 GmbH | Mühlenmathe 50 | 48599 Gronau