How to use a script to setup all Aircraft Types as placeholders for unknown Registrations

This feature allows for all Aircraft Types to be setup as aircraft registrations allowing users to create and provide pricing for quotes and handling orders when an aircraft registration has not been provided. This feature is also useful for FBO's creating scheduled flights for which aircraft registrations are not available in advance.

Although this process can be done manually on an adhoc basis via the 'Add Registration' screen, a script allows this to be done quickly and easily for all aircraft types once and for all.

Once created, each aircraft type will have a generic registration assigned to it for order creation using the aircraft type. For example, the A380F type aircraft will have the registration ?A380F? created with the Operator and Debtor setup an 'Unknown Operator'.

To begin, go to Administration > Api add-ins > Script editor (NOTE: The user conducting this process must have access rights to the Role UnrestrictedApiAccess)

The 'script editor' screen may contain some text, select and delete the text so that the field is blank

Copy and paste the below script into the field


using FboOne.Client;
using FboOne.ServiceModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FboOne.Scripts.Quotes
{
  /// <summary>
  /// PPP-7994 Allow Quote creation based on A/C Types 
  /// </summary>
  public class RegenerateAicraftTypePlaceholderRegistrations
  {
    public void Main()
    {
      var types = AircraftTypeRepository.Get().OrderBy(t => t.ShortName).ToList();
      var registrations = AircraftRegistrationRepository.Get();
      var contacts = ContactRepository.Get();
      var aircraftContacts = AircraftContactRepository.Get();
      var fbos = FboLocationRepository.Get();
      var aircraftAdministrationIds = fbos.Select(f => f.AircraftAdministrationId).Distinct().ToList();
      var operatorAircraftContactType = AircraftContactTypeRepository.Get().Single(t => t.EnumValue == (int)AircraftContactTypeEnum.Operator);

      var unknownOperator = contacts.SingleOrDefault(c => c.ShortName == "ZZZZ");
      if (unknownOperator == null)
      {
        unknownOperator = new Contact
        {
          IsOperator = true,
          IsDebtor = true,
          ShortName = "ZZZZ",
          OrganizationName = "Unknown"
        }.Save();
      }
      else
      {
        unknownOperator.IsDebtor = true;
        unknownOperator.IsOperator = true;
        unknownOperator.IsActive = true;
        unknownOperator.Save();
      }

      foreach (var type in types)
      {
        // dashes and spaces not allowed in registration code
        // B747-ER  -->  ?B747ER?
        var placeHolderCode = "?" + type.ShortName.Replace(" ", "").Replace("-", "").ToUpper() + "?";
        var registration = registrations.SingleOrDefault(r => r.RegistrationCode.ToUpper() == placeHolderCode);
        if (registration == null)
        {
          //Log.WriteLine("Creating " + placeHolderCode);

          registration = new AircraftRegistration
          {
            RegistrationCode = placeHolderCode,
            AircraftTypeId = type.Id,
            Remarks = "For creating quotes for aircraft type " + type.ShortName
          }.Save();
          registrations.Add(registration);

          foreach (var aircraftAdministrationId in aircraftAdministrationIds)
          {
            new AircraftContact
            {
              AircraftRegistrationId = registration.Id,
              ContactId = unknownOperator.Id,
              AircraftContactTypeId = operatorAircraftContactType.Id,
              AircraftAdministrationId = aircraftAdministrationId,
            }.Save();
          }
        }
      }

      //Log.WriteLine("Done");
    }
  }
}


Finally click 'Run'



Whilst running, the screen will show as 'Processing'



Once complete, search an aircraft type at random, e.g. A380F - the search box should now display the aircraft type but also the newly created (script inserted) Aircraft Registration ?A380F? (Operator and Debtor Unknown).


This should be the case for all aircraft type once the script has been run



Related pages: