All of the business logic for handling the requirements is defined in a GiftManagement codeunit, as follows:
codeunit 50101 "GiftManagement_PKT"{ procedure AddGifts(var SalesHeader: Record "Sales Header") var SalesLine: record "Sales Line"; Handled: Boolean; begin SalesLine.SetRange("Document Type", SalesHeader."Document Type"); SalesLine.SetRange("Document No.", SalesHeader."No."); SalesLine.SetRange(Type, SalesLine.Type::Item); //We exclude the generated gifts lines in order to avoid loops SalesLine.SetFilter("Line Discount %", '<>100'); if SalesLine.FindSet() then repeat //Integration event raised OnBeforeFreeGiftSalesLineAdded(SalesLine, Handled); AddFreeGiftSalesLine(SalesLine, Handled); //Integration Event raised ...