Accounts Payable Customization

<< Click to Display Table of Contents >>

Navigation:  Zetadocs SDK Guide > Creating a Per Tenant Extension >

Accounts Payable Customization

Zetadocs Accounts Payable Automation allows you to customize the how the purchase invoice is created in Business Central to satisfy specific business requirements. You can specify your own business validation logic or custom purchase invoice line types using a Per Tenant Extension.

 

Business Logic validation Example

The following code sample illustrates how to use the OnValidateReviewLine event to define specific business requirements or data consolidation, that runs during validation of the fields:

 

 

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnValidateReviewLine', '', true, true)]

    local procedure OnValidateReviewLine(var OnValidateReviewLineHandler: Codeunit "Zdd OnValidatePreviewLine")

    var

        reviewLine: Record "Zetadocs ADE Lines";

    begin

        OnValidateReviewLineHandler.GetReviewLine(reviewLine);

 

        case reviewLine.ReviewLineType of

            reviewLine.ReviewLineType::"Fixed Asset":

                begin

                    if (reviewLine.Description = 'fail') then begin

                        Error('Because I should fail')

                    end;

 

                    if (reviewLine.Description = 'override') then begin

                        reviewLine.Description := 'My override';

                    end;

                end;

        end;

 

        OnValidateReviewLineHandler.SetReviewLine(reviewLine);

    end;

 

 

 

Custom Line types Example

Zetadocs supports the use of custom lines types on Business Central Purchase Invoices. The following code illustrates how to create new line types for Zetadocs and Business central.

enumextension 50680 "Zd Review Line Type extension" extends "Zetadocs Review Line Type"

{

    value(50680; CustomTypeToItem)

    {

        Caption = 'Per Tenant Extension type to Item';

    }

    value(50681; CustomTypeToCustomePurchaseInvoiceType)

    {

        Caption = Per Tenant Extension type To Custom Purchase type';

    }

}

 

enumextension 50681 "Zd Purchase Line Type extension" extends "Purchase Line Type"

{

    value(50680; CustomPurchaseInvoice)

    {

        Caption = 'Per Tenant Ext Purchase type';

    }

}

 

 

Once created, new line types can be used with the OnAddDefaultsToReviewLine, OnLookupReviewLine and OnEnumConversion events to adapt the Zetadocs logic to suit your requirements.

 

 

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnAddDefaultsToReviewLine', '', true, true)]

    local procedure OnAddDefaultsToReviewLine(var onAddDefaultsToReviewLineHandler: Codeunit "Zdd OnAddDefaultsToReviewLine")

    var

        reviewLine: Record "Zetadocs ADE Lines" temporary;

    begin

        onAddDefaultsToReviewLineHandler.GetReviewLine(reviewLine);

        case reviewLine.ReviewLineType of

            reviewLine.ReviewLineType::CustomTypeToItem,

            reviewLine.ReviewLineType::CustomTypeToCustomePurchaseInvoiceType:

                begin

                    if (reviewLine.Description = '') then begin

                        reviewLine.Description := 'custom description from the Per Tenant Extension';

                    end;

                    if (reviewLine.ProductCode <> '') then begin

                        if reviewLine."Gen. Prod. Posting Group" = '' then reviewLine."Gen. Prod. Posting Group" := 'RETAIL';

                        if reviewLine."VAT Prod. Posting Group" = '' then reviewLine."VAT Prod. Posting Group" := 'VAT20';

                    end;

                end;

        end;

        onAddDefaultsToReviewLineHandler.SetReviewLine(reviewLine);

    end;

 

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnLookupReviewLine', '', true, true)]

    local procedure OnLookupReviewLine(var onLookupReviewLineHandler: Codeunit "Zdd OnLookupReviewLine")

    var

        reviewLine: Record "Zetadocs ADE Lines";

        item: Record Item;

        onLookupText: Text;

        fieldNameLookedUp: enum "Zetadocs Review Line Column";

    begin

        onLookupReviewLineHandler.GetLookupContext(reviewLine, onLookupText, fieldNameLookedUp);

 

        if (fieldNameLookedUp = fieldNameLookedUp::ProductCode) then begin

            case reviewLine.ReviewLineType of

                reviewLine.ReviewLineType::CustomTypeToItem:

                    begin

                        Message('Lookup from Per Tenant Extension');

                        case reviewLine.ReviewDocumentType of

                            reviewLine.ReviewDocumentType::"Credit Memo",

                            reviewLine.ReviewDocumentType::"Return Order":

                                begin

                                    item.SetRange(Blocked, FALSE);

                                end;

                            else begin

                                item.SetRange(Blocked, FALSE);

                                item.SetRange("Purchasing Blocked", FALSE);

                            end;

                        end;

 

                        if (Page.RunModal(0, item) = Action::LookupOK) then begin

                            onLookupReviewLineHandler.SetLookupValue(item."No.");

                        end;

                    end;

            end;

        end;

    end;

 

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnEnumConversion', '', true, true)]

    local procedure OnEnumConversion(var OnEnumConversionHandler: Codeunit "Zdd OnEnumConversion")

    var

    begin

        OnEnumConversionHandler.AddOrUpdatePurchaseLineMapping("Zetadocs Review Line Type"::CustomTypeToItem, "Purchase Line Type"::Item);

        OnEnumConversionHandler.AddOrUpdatePurchaseLineMapping("Zetadocs Review Line Type"::CustomTypeToCustomePurchaseInvoiceType, "Purchase Line Type"::CustomPurchaseInvoice);

    end;

 

 

Customizing The Created Purchase Invoice
The OnBeforeCreatePurchaseHeaderOnBeforeCreatePurchaseLineOnAfterCreatePurchaseHeaderOnAfterCreatePurchaseLineOnAfterCreatePurchaseRecord events can be used to validate and alter the purchase record created by the AP Automation process.

 
The following example shows validating and failing the invoice creation based on the Zetadocs ADE Review Header during the preview stage to prevent the user creating the Invoice against some custom business logic.

 

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnBeforeCreatePurchaseHeader', '', true, true)]

    local procedure OnBeforeCreatePurchaseHeader(var onBeforeCreatePurchaseHdrHandler: Codeunit "Zdd OnBeforeCreatePurchaseHdr")

    var

        reviewHeader: Record "Zetadocs ADE Review Header";

        ZdSystemSettings: Record "Zetadocs System Settings";

        isPreview: Boolean;

    begin

        onBeforeCreatePurchaseHdrHandler.GetZdReviewHeader(reviewHeader);

        onBeforeCreatePurchaseHdrHandler.GetZdIsPreview(isPreview);

 

        if (isPreview) then begin         

            if (reviewHeader.SubTotal > 10000) then begin

                Error('Not allowed to create invoices over $10,000.');

            end;

        end;

    end;

 

The following example shows altering the created Purchase Invoice.

 

   [EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnAfterCreatePurchaseHeader', '', true, true)]

    local procedure OnAfterCreatePurchaseHeader(var onAfterCreatePurchaseHdrHandler: Codeunit "Zdd OnAfterCreatePurchaseHdr")

    var

        purchaseHeader: Record "Purchase Header";

    begin

        onAfterCreatePurchaseHdrHandler.GetPurchaseHeader(purchaseHeader);

 

        purchaseHeader."Posting Description" := 'Invoice: ' + purchaseHeader."No." + ' Posted on:' + Today().ToText();

       

        purchaseHeader.Modify();

        onAfterCreatePurchaseHdrHandler.SetPurchaseHeader(purchaseHeader);

    end;

 

The following example shows altering the final Purchase Invoice based after retrieving all of the created lines.

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnAfterCreatePurchaseRec', '', true, true)]

   local procedure OnAfterCreatePurchaseRec(var onAfterCreatePurchaseRecHandler: Codeunit "Zdd OnAfterCreatePurchaseRec")

    var

        purchaseHeader: Record "Purchase Header";

        purchaseLine: Record "Purchase Line";

        reviewLines: Record "Zetadocs ADE Lines";

    begin

        onAfterCreatePurchaseRecHandler.GetPurchaseHeader(purchaseHeader);

        onAfterCreatePurchaseRecHandler.GetZdReviewLines(reviewLines);

 

        reviewLines.Reset();

        if (reviewLines.Find('-')) then begin

            repeat

                if (reviewLines.Description.Contains('Table')) then begin

                    onAfterCreatePurchaseRecHandler.GetCreatedPurchaseLine(reviewLines, purchaseLine);

 

                    purchaseLine."Description 2" := CopyStr(purchaseLine."Description 2" + '. Check all legs.', 1, MaxStrLen(purchaseLine."Description 2"));

                    purchaseLine.Modify();

                end;

            until reviewLines.Next() = 0;

        end;

    end;

 

 

Using Custom Fields from a Custom Layout

 

The following examples shows using some of the custom fields read from an invoice using a custom layout.
The OnBeforeReviewDisplay event can be used to access the custom fields and validate/alter the Zetadocs Review records before the review page is displayed.
The OnBeforeCreatePurchaseHeader, OnBeforeCreatePurchaseLine, OnAfterCreatePurchaseHeader, OnAfterCreatePurchaseLine events can be used to access and use the custom fields from a custom layout. These can then be used validate and alter the purchase record created by the AP Automation process.

 

The following example shows fetching some custom fields that have been read using the custom layout and then setting added fields on the Zetadocs Review Header and Lines to display on the review page.

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnBeforeReviewDisplay', '', true, true)]

    local procedure OnBeforeReviewDisplay(var OnBeforeReviewDisplayHandler: Codeunit "Zetadocs OnBeforeReviewDisplay")

    var

        reviewLine: Record "Zetadocs ADE Lines";

        reviewHeader: Record "Zetadocs ADE Review Header";

        zdInterface: Codeunit "Zetadocs Interface";

        customFields: JsonArray;

        customFieldValue: Text;

    begin

        OnBeforeReviewDisplayHandler.GetReviewPageData(reviewHeader, reviewLine);

 

        OnBeforeReviewDisplayHandler.GetCustomFieldsForReviewHeader(customFields);

 

        zdInterface.GetApAutomationCustomField(customFields, 'BankAccount', customFieldValue);

        reviewHeader."BankAccount" := CopyStr(customFieldValue, 1, MaxStrLen(reviewHeader."BankAccount"));

 

        zdInterface.GetApAutomationCustomField(customFields, 'vat no', customFieldValue);

        reviewHeader."VAT No" := CopyStr(customFieldValue, 1, MaxStrLen(reviewHeader."VAT No"));

 

         if (reviewLine.FindSet()) then begin

            repeat

                Clear(customFields);

                OnBeforeReviewDisplayHandler.GetCustomFieldsForReviewLine(reviewLine.HeaderNo, reviewLine.No, customFields);

 

                zdInterface.GetApAutomationCustomField(customFields, 'DelUnit', customFieldValue);

                reviewLine."DelUnit" := CopyStr(customFieldValue, 1, MaxStrLen(reviewLine."DelUnit"));

 

                zdInterface.GetApAutomationCustomField(customFields, 'vatpercentage', customFieldValue);

                reviewLine.VATPercentage := CopyStr(customFieldValue, 1, MaxStrLen(reviewLine.VATPercentage));

 

                reviewLine.Modify();

            until reviewLine.Next() = 0;

        end;

 

 

        OnBeforeReviewDisplayHandler.SetReviewPageData(reviewHeader, reviewLine);

    end;

 

 

 

The following example shows using one of the custom fields for some extra validation when creating the Purchase Invoice from the review screen, and then raising an error to block the creation if the validation isn't met.

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Zetadocs Customize", 'OnBeforeCreatePurchaseHeader', '', true, true)]

    local procedure OnBeforeCreatePurchaseHeader(var onBeforeCreatePurchaseHdrHandler: Codeunit "Zdd OnBeforeCreatePurchaseHdr")

    var

        zdInterface: Codeunit "Zetadocs Interface";

        isPreview: Boolean;

        customFields: JsonArray;

        customFieldValue: Text;

    begin

        onBeforeCreatePurchaseHdrHandler.GetZdIsPreview(isPreview);

 

        onBeforeCreatePurchaseHdrHandler.GetCustomFieldsForReviewHeader(customFields);

 

        if (isPreview) then begin

            zdInterface.GetApAutomationCustomField(customFields, 'vat no', customFieldValue);

            if (customFieldValue = '') then begin

                Error('OnBeforeCreatePurchaseHeader fail preview invoice creation - VAT No was empty on recognised invoice header');

            end;

        end;

    end;