﻿<?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="55">
      <Name>Order After Approval</Name>
      <Description>When the ticket is approved in the Purchase service by all approvers, the ticket transit to the Order status.</Description>
      <Scripts>
        <Script id="128">
          <Name>OrderAfterApproval</Name>
          <Code>using System;
using System.Data;
using Microsoft.Data.SqlClient;
using Alvao.Global;
using Alvao.API.Common;
using Alvao.Apps.API;
using System.Collections.Generic; 
using Alvao.API.Common.Model.Database;
using System.Linq;

public class OrderAfterApproval : ITicketApprovalAutoAction 
{ 
    public string Name { get =&gt; Settings.ActionName; set =&gt; Name = value; } 

    public void OnApproved(SqlConnection con, SqlTransaction trans, int ticketId, int approvalItemId) 
    { 
        string sectionName = Alvao.API.Common.Database.ReadColumn(ticketId, "TicketForeignKeyInfo", "SectionName"); 
        string stateName = Alvao.API.SD.TicketState.GetCurrentStateByTicketId(ticketId)._TicketState; 

        // Approval information (approver, comment, etc.)
        tHdTicketApprovalItem currentApprovalItem = Alvao.API.SD.Approval.GetApprovalItemById(approvalItemId); 

        if(sectionName == Settings.ServiceName &amp;&amp; stateName == Settings.State)
        {
            // Returns a list of current approval approvers, including their decisions.
            IEnumerable&lt;tHdTicketApprovalItem&gt; approvalItems = Alvao.API.SD.Approval.GetCurrentApprovalItems(ticketId); 

            // If everyone approved.
            if(approvalItems.Where(s =&gt; s.liHdTicketApprovalItemHdTicketApprovalItemResultId == (int)tHdTicketApprovalItem.ApprovalResult.Accepted).Count() == approvalItems.Count()) 
            { 
                Alvao.API.Common.Model.Database.TicketState targetState = Alvao.API.SD.TicketState.GetByName(Settings.TargetState, ticketId); 

                if(targetState == null) 
                    return; 

                int systemPersonId = Alvao.API.Common.Person.GetSystem().iPersonId; 

                // Status change.
                Alvao.API.SD.Ticket.ChangeState(ticketId, targetState.id, systemPersonId, null); 
            } 
        } 
    }

    public void OnRejected(SqlConnection con, SqlTransaction trans, int ticketId, int approvalItemId)  
    {
        throw new NotImplementedException();
    }

    public void OnApproverAdded(SqlConnection con, SqlTransaction trans, int ticketId, IEnumerable&lt;int&gt; approvalItemIds)
    {
        throw new NotImplementedException();
    }

    public void OnApproverCanceled(SqlConnection con, SqlTransaction trans, int ticketId, IEnumerable&lt;int&gt; approvalItemIds)
    {
        throw new NotImplementedException();
    }
} </Code>
          <IsLibCode>false</IsLibCode>
        </Script>
        <Script id="129">
          <Name>Settings</Name>
          <Code>public class Settings 
{
    public static string ActionName = "OrderAfterApproval";
    public static string ServiceName = "Purchase"; // The name of the service where the approval is taking place
    public static string State = "Purchase approval"; // The name of the status where the approval is taking place.
    public static string TargetState = "Order"; // The name of the status to enter after approval.
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>