microsoft / ALAppExtensions

Repository for collaboration on Microsoft AL application add-on and localization extensions for Microsoft Dynamics 365 Business Central.
MIT License
748 stars 600 forks source link

[Event Request] Codeunit 1001 "Job Post-Line" - procedure PostInvoiceContractLine #26772

Closed mrabus closed 5 days ago

mrabus commented 6 days ago

Describe the request

For my customization, I require an additional check to skip PostJobOnSalesLine function in the PostInvoiceContractLine procedure. To achieve this, I propose to add the following event and code change.

    procedure PostInvoiceContractLine(SalesHeader: Record "Sales Header"; SalesLine: Record "Sales Line")
    var
        Job: Record Job;
        JobTask: Record "Job Task";
        JobPlanningLine: Record "Job Planning Line";
        JobPlanningLineInvoice: Record "Job Planning Line Invoice";
        DummyJobLedgEntryNo: Integer;
        JobLineChecked: Boolean;
        IsHandled: Boolean;
    begin
        IsHandled := false;
        OnBeforePostInvoiceContractLine(SalesHeader, SalesLine, IsHandled);
        if IsHandled then
            exit;

        JobPlanningLine.SetCurrentKey("Job Contract Entry No.");
        JobPlanningLine.SetRange("Job Contract Entry No.", SalesLine."Job Contract Entry No.");
        OnPostInvoiceContractLineOnBeforeJobPlanningLineFindFirst(SalesHeader, SalesLine, JobPlanningLine);
        JobPlanningLine.FindFirst();
        Job.Get(JobPlanningLine."Job No.");

        CheckCurrency(Job, SalesHeader, JobPlanningLine);

        IsHandled := false;
        OnPostInvoiceContractLineOnBeforeCheckBillToCustomer(SalesHeader, SalesLine, JobPlanningLine, IsHandled);
        if not IsHandled then
            if Job."Task Billing Method" = Job."Task Billing Method"::"One customer" then
                SalesHeader.TestField("Bill-to Customer No.", Job."Bill-to Customer No.")
            else begin
                JobTask.Get(JobPlanningLine."Job No.", JobPlanningLine."Job Task No.");
                SalesHeader.TestField("Bill-to Customer No.", JobTask."Bill-to Customer No.");
            end;

        OnPostInvoiceContractLineBeforeCheckJobLine(SalesHeader, SalesLine, JobPlanningLine, JobLineChecked);
        if not JobLineChecked then begin
            JobPlanningLine.CalcFields("Qty. Transferred to Invoice");
            if JobPlanningLine.Type <> JobPlanningLine.Type::Text then
                JobPlanningLine.TestField("Qty. Transferred to Invoice");
        end;

        ValidateRelationship(SalesHeader, SalesLine, JobPlanningLine);

        case SalesHeader."Document Type" of
            SalesHeader."Document Type"::Invoice:
                if JobPlanningLineInvoice.Get(JobPlanningLine."Job No.", JobPlanningLine."Job Task No.", JobPlanningLine."Line No.",
                     JobPlanningLineInvoice."Document Type"::Invoice, SalesHeader."No.", SalesLine."Line No.")
                then begin
                    JobPlanningLineInvoice.Delete(true);
                    JobPlanningLineInvoice."Document Type" := JobPlanningLineInvoice."Document Type"::"Posted Invoice";
                    JobPlanningLineInvoice."Document No." := SalesLine."Document No.";
                    JobPlanningLineInvoice.Insert(true);

                    JobPlanningLineInvoice."Invoiced Date" := SalesHeader."Posting Date";
                    JobPlanningLineInvoice."Invoiced Amount (LCY)" :=
                      CalcLineAmountLCY(JobPlanningLine, JobPlanningLineInvoice."Quantity Transferred");
                    JobPlanningLineInvoice."Invoiced Cost Amount (LCY)" :=
                      JobPlanningLineInvoice."Quantity Transferred" * JobPlanningLine."Unit Cost (LCY)";
                    JobPlanningLineInvoice.Modify();
                end;
            SalesHeader."Document Type"::"Credit Memo":
                if JobPlanningLineInvoice.Get(JobPlanningLine."Job No.", JobPlanningLine."Job Task No.", JobPlanningLine."Line No.",
                     JobPlanningLineInvoice."Document Type"::"Credit Memo", SalesHeader."No.", SalesLine."Line No.")
                then begin
                    JobPlanningLineInvoice.Delete(true);
                    JobPlanningLineInvoice."Document Type" := JobPlanningLineInvoice."Document Type"::"Posted Credit Memo";
                    JobPlanningLineInvoice."Document No." := SalesLine."Document No.";
                    JobPlanningLineInvoice.Insert(true);

                    JobPlanningLineInvoice."Invoiced Date" := SalesHeader."Posting Date";
                    JobPlanningLineInvoice."Invoiced Amount (LCY)" :=
                      CalcLineAmountLCY(JobPlanningLine, JobPlanningLineInvoice."Quantity Transferred");
                    JobPlanningLineInvoice."Invoiced Cost Amount (LCY)" :=
                      JobPlanningLineInvoice."Quantity Transferred" * JobPlanningLine."Unit Cost (LCY)";
                    JobPlanningLineInvoice.Modify();
                end;
        end;

        OnBeforeJobPlanningLineUpdateQtyToInvoice(SalesHeader, SalesLine, JobPlanningLine, JobPlanningLineInvoice, DummyJobLedgEntryNo);

        JobPlanningLine.UpdateQtyToInvoice();
        JobPlanningLine.Modify();

        OnAfterJobPlanningLineModify(JobPlanningLine);

        //NEW CODE 
        //if JobPlanningLine.Type <> JobPlanningLine.Type::Text then
        if JobPlanningLine.Type <> JobPlanningLine.Type::Text then begin
           IsHandled := false;
           OnBeforePostJobOnSalesLine(JobPlanningLine, SalesHeader, SalesLine, "Job Journal Line Entry Type"::Sale, IsHandled);
           if not IsHandled then
        //NEW 
               PostJobOnSalesLine(JobPlanningLine, SalesHeader, SalesLine, "Job Journal Line Entry Type"::Sale);
        //NEW CODE
       end;
        //NEW CODE

        OnAfterPostInvoiceContractLine(SalesHeader, SalesLine);
    end;

     //NEW CODE 
  [IntegrationEvent(false, false)]
  local procedure OnBeforePostJobOnSalesLine(JobPlanningLineInvoice: Record "Job Planning Line Invoice"; SalesHeader: Record "Sales Header"; "SalesLine: Record "Sales Line"; JobJournalLineEntryType: Enum "Job Journal Line Entry Type"; var IsHandled: Boolean)
  begin
  end;

Additional context

Please add additional option to skip PostJobOnSalesLine in codeunit "Job Post-Line" Internal work item: AB#539931

JesperSchulz commented 5 days ago

Thanks for reporting this. We agree, and we’ll publish a fix asap, either in an update for the current version or in the next major release. Please do not reply to this, as we do not monitor closed issues. If you have follow-up questions or requests, please create a new issue where you reference this one.

Build ID: 21487.