﻿<?xml version="1.0"?>
<AlvaoApplication xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ModelVersion="1">
  <Applications>
    <Application id="77">
      <Name>Create Analysis</Name>
      <Description>When creating a new ticket in the Program Modifications service, a new linked ticket is created in the Program modifications/Designs and analyses service for the new created ticket.</Description>
      <Scripts>
        <Script id="162">
          <Name>CreateAnalysis</Name>
          <Code>using System;
using System.Data;
using Microsoft.Data.SqlClient;
using Alvao.Apps.API;
using Alvao.API.SD;
using Alvao.API.SD.Model;
using Alvao.API.Common;
using Dapper;

namespace Alvao.Helpdesk
{
    class CreateAnalysis : ITicketAutoAction
    {
        public string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public CreateAnalysis()
        {
            name = Settings.ActionName;
        }

        public void OnTicketChanged(SqlConnection con, SqlTransaction trans, int ticketId, int personId, string properties)
        {
            throw new NotImplementedException();
        }

        public void OnTicketCreated(SqlConnection con, SqlTransaction trans, int ticketId, int personId)
        {
            // Get values.
            bool createAnalysis;
            try
            {
                createAnalysis = con.QueryFirstOrDefault&lt;bool&gt;(@"SELECT createAnalysis from tHdTicketCust where liHdTicketId = @ticketId", 
                    new { ticketId }, trans);
            }
            catch (SqlException)
            {
                return;
            }            

            int? analysisServiceId = con.QueryFirstOrDefault&lt;int?&gt;(@"SELECT iHdSectionId from tHdSection where sHdSection = @sectionName", 
                new { sectionName = Settings.AnalysisServiceName }, trans);

            string section = API.Common.Database.ReadColumn(ticketId, "TicketForeignKeyInfo", "SectionName");
            string name = API.Common.Database.ReadColumn(ticketId, "tHdTicket", "sHdTicket");

            // Check values.
            if (section == Settings.ServiceName &amp;&amp; createAnalysis)
            {
                // Create a new ticket for analysis.

                var newTicketModel = new NewTicketModel();
                newTicketModel.Ticket.sHdTicket = name + " - analysis";
                newTicketModel.Ticket.liHdTicketUserPersonId = personId;
                newTicketModel.LoadRequesterData = true;
                newTicketModel.Ticket.liHdTicketHdSectionId = analysisServiceId.Value;
                var ticket = Ticket.Create(newTicketModel);
                int newTicketId = ticket.iHdTicketId;

                // Creat a link.
                Relation.Create(ticketId, newTicketId, 2, Person.GetSystem().iPersonId);
            }
        }
    }
}</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
        <Script id="163">
          <Name>Settings</Name>
          <Code>public static class Settings 
{
    public const string ActionName = "Create project ticket";
    public const string ServiceName = "Program modifications"; // The name of the service in which the new ticket is being created.
    public const string AnalysisServiceName = "Program modifications/Designs and analyzes"; // The name of the service to which the ticket for the created ticket is to be created.
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>