﻿<?xml version="1.0" encoding="utf-8"?>
<AlvaoApplication xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ModelVersion="1">
  <Applications>
    <Application id="40">
      <Name>Ticket Costs Calculation</Name>
      <Description>The Command adds the value on the selected ticket from the custom items SW Costs and HW Costs.</Description>
      <Scripts>
        <Script id="84">
          <Name>Settings</Name>
          <Code>public static class Settings 
{
    public const string CommandName = "Calculate the total cost of SW and HW";
    public const int CommandPosition = 1;
    public const string CommandIcon = "calculator_20_regular";
  
    public const int StateId = 94; // The ID of the status in which the ticket is to be displayed.
    public const int ServiceId = 37; // The ID of the service in which the ticket should be displayed.
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
        <Script id="85">
          <Name>CostsCalculation</Name>
          <Code>using System;
using Alvao.API.Common.Model.CustomApps;
using Alvao.API.Common.Model.Database;
using Alvao.API.SD;
using Alvao.Apps.API;
using Alvao.Context;
using Dapper;

public class CostsCalculation : IEntityCommand 
{
    public string Id {get; set;}
    public Entity Entity {get; set;}

    public CostsCalculation()
    {
        Id = "TicketCostsCalculation";
        Entity = Entity.Request;
    }

    public EntityCommandShowResult Show(int entityId, int personId)
    {   
        int position = Settings.CommandPosition; 
        string icon =  Settings.CommandIcon; 
        string name =  Settings.CommandName; 

        // Retrieve the required requirements data from the database.
        tHdTicket ticket = Ticket.GetById(entityId);
        if(ticket == null)
            return new EntityCommandShowResult (false, name, icon, position);

        // In the Show method, for performance reasons, it is more convenient to work with service ID, reseller, and status.
        int sectionId = ticket.liHdTicketHdSectionId; // Id of the service in which the ticket is made.
        int solverId = ticket.liHdTicketSolverPersonId ?? 0; // Ticket solver id.
        int stateId = ticket.TicketStateId; // Ticket status id.

        // Check whether the ticket is in the required service and status and the solver is the user to whom the ticket should be displayed.
        bool show = sectionId == Settings.ServiceId &amp;&amp; stateId == Settings.StateId &amp;&amp; solverId == personId;

        return new EntityCommandShowResult (show, name, icon, position);
    }

    public CommandResult Run(int entityId, int personId)
    {
        MessageType messageType = MessageType.None; // MessageType.None, MessageType.Info, MessageType.Warning, MessageType.Error
        string messageText = string.Empty; //message that is displayed to the user after the command is executed
        string navigateToUrl = string.Empty; //url that opens after executing the command

        // Check whether the conditions for its execution / display have not changed between display and execution of the command (can be done anywhere in this Run method).
        var verifyRights = Show(entityId, personId);
        if (!verifyRights.Show)
        {
            messageType = MessageType.Error;
            messageText = "The command cannot be executed because the conditions for its display are not met.";
            return new CommandResult(messageType, messageText, navigateToUrl);
        }

        using (var scope = AlvaoContext.GetConnectionScope())
        {
            scope.BeginTransaction();
            try
            {
                // Read values from custom fields "SW Costs" and "HW Costs".
                string strCostSW = Alvao.API.Common.Database.ReadColumn(entityId, "tHdTicketCust", "costSW");
                int costSW = string.IsNullOrEmpty(strCostSW) ? 0 : int.Parse(strCostSW);
                string strCostHW = Alvao.API.Common.Database.ReadColumn(entityId, "tHdTicketCust", "costHW");
                int costHW = string.IsNullOrEmpty(strCostHW) ? 0 : int.Parse(strCostHW);
                int costTotal = costSW + costHW;

                // Write the sum of costSW and costHW in your custom field Total costs.
                Alvao.API.Common.Database.WriteColumn(entityId, "tHdTicketCust", "costTotal", costTotal.ToString());

                //example of using custom SQL query within running transaction
                //scope.Connection.ExecuteAsync(@"UPDATE table SET column = @value WHERE id = @id", new { id = id, value = value }, scope.LegacyTransaction);

                scope.CommitTransaction();

                return new CommandResult(messageType, messageText, navigateToUrl);
            }
            catch (Exception)
            {
                messageType = MessageType.Error;
                messageText = "Error when saving the value.";
                scope.RollbackTransaction();
                return new CommandResult(messageType, messageText, navigateToUrl);
            }
        }
    }
}</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>