logo

Reserving registration numbers using a script

This article describes how to use a script to reserve a registration number in a process.

For the script to work correctly, you need to:

  • configure a category in Administration - Document Management - Records Classification Scheme (fig. 1). In the Allow to reserve the number field select Yes. After that, save and publish the records classification scheme;
  • configure a document registration variant. You can find information about registration variants here.

 

Fig. 1. Administration – Document Management - Records Classification Scheme

For the script to work correctly:

  • add the namespace: using EleWise.ELMA.Documents.Managers
  • user the class: ReservedRegNumberManager

Script text:

// EQL query for searching the required document type
string eqlParam = "ContractType = " + context.ContractType.Id + " AND Templates = " + context.Template.Id;
var Param = EntityManager<TypicalContractParameters>.Instance.Find (eqlParam).FirstOrDefault ().DocumentType.DocumentTypeUID;
if (string.IsNullOrWhiteSpace (context.ReservedNumber)) {
    // find the registration variant
    var varReg = RegistrationVariantManager.Instance.FindAll ().Where (x => x.DocumentType.TypeUid.ToString () == Param).FirstOrDefault ();
    if (varReg != null) {
        //reserve a registration number by variant
        var reservReg = ReservedRegNumberManager.Instance.ReservRegNumberByVariant (varReg);
        reservReg.Save ();
        context.ReservedNumber = reservReg.RegNumber;
        //generate the full registration number according to the records classification scheme
        var prefix = context.IsFrame ? "PA_" : "SA_";
        var operatorCode = context.ContractingPartyLeg != null ? context.ContractingPartyLeg.Name + "_" : string.Empty;
        var objectID = context.ObjectInf != null ? context.ObjectInf.Name + "-" : string.Empty;
        var reservedNumber = !string.IsNullOrEmpty (context.ReservedNumber) ? DateTime.Now.Year.ToString () + context.ReservedNumber : string.Empty;
        context.RegistrNumber = string.Format ("{0}{1}{2}", operatorCode, objectID, reservedNumber);
        if (!string.IsNullOrEmpty (context.ReservedNumber) && (string.IsNullOrEmpty (context.ContractNumber) || context.ContractNumber.Contains (context.ReservedNumber))) {
            context.ContractNumber = context.RegistrNumber;
        }
    }
}