microsoft / ALAppExtensions

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

Refactor function "CopyPurchDocLine" like "CopySalesDocLine" #14048

Closed fridrichovsky closed 3 years ago

fridrichovsky commented 3 years ago

Can you check marked part in code for refactor. Function "CopyPurchDocLine" is similar to "CopySalesDocLine". Can you refactor it? If you refactor it we can use events similar to sales in purchase.

CopyPurchDocLine(var ToPurchHeader: Record "Purchase Header"; var ToPurchLine: Record "Purchase Line"; var FromPurchHeader: Record "Purchase Header"; var FromPurchLine: Record "Purchase Line"; var NextLineNo: Integer; var LinesNotCopied: Integer; RecalculateAmount: Boolean; FromPurchDocType: Enum "Purchase Document Type From"; var CopyPostedDeferral: Boolean; DocLineNo: Integer) Result: Boolean
    var
        RoundingLineInserted: Boolean;
        CopyThisLine: Boolean;
        CheckVATBusGroup: Boolean;
        IsHandled: Boolean;
    begin
        CopyThisLine := true;
        IsHandled := false;
        OnBeforeCopyPurchLine(
          ToPurchHeader, FromPurchHeader, FromPurchLine, RecalculateLines, CopyThisLine, ToPurchLine, MoveNegLines,
          RoundingLineInserted, Result, IsHandled);
        if IsHandled then
            exit(Result);
        if not CopyThisLine then begin
            LinesNotCopied := LinesNotCopied + 1;
            exit(false);
        end;

        CheckPurchRounding(FromPurchLine, RoundingLineInserted);

        if ((ToPurchHeader."Language Code" <> FromPurchHeader."Language Code") or RecalculateLines) and
           (FromPurchLine."Attached to Line No." <> 0) or
           FromPurchLine."Prepayment Line" or RoundingLineInserted
        then
            exit(false);

        if IsEntityBlocked(Database::"Purchase Line", ToPurchHeader.IsCreditDocType(), FromPurchLine.Type.AsInteger(), FromPurchLine."No.") then begin
            LinesNotCopied := LinesNotCopied + 1;
            exit(false);
        end;

        if RecalculateLines and not FromPurchLine."System-Created Entry" then begin
            ToPurchLine.Init();
            OnAfterInitToPurchLine(ToPurchLine);
        end else begin
            ToPurchLine := FromPurchLine;
            ToPurchLine."Returns Deferral Start Date" := 0D;
            if ToPurchHeader."Document Type" in [ToPurchHeader."Document Type"::Quote, ToPurchHeader."Document Type"::"Blanket Order"] then
                ToPurchLine."Deferral Code" := '';
            if MoveNegLines and (ToPurchLine.Type <> ToPurchLine.Type::" ") then begin
                ToPurchLine.Amount := -ToPurchLine.Amount;
                ToPurchLine."Amount Including VAT" := -ToPurchLine."Amount Including VAT";
            end
        end;

        CheckVATBusGroup := (not RecalculateLines) and (ToPurchLine."No." <> '');
        OnCopyPurchLineOnBeforeCheckVATBusGroup(ToPurchLine, CheckVATBusGroup);
        if CheckVATBusGroup then
            ToPurchLine.TestField("VAT Bus. Posting Group", ToPurchHeader."VAT Bus. Posting Group");

        NextLineNo := NextLineNo + 10000;
        OnCopyPurchDocLineOnAfterSetNextLineNo(ToPurchLine, FromPurchLine, NextLineNo);
        ToPurchLine."Document Type" := ToPurchHeader."Document Type";
        ToPurchLine."Document No." := ToPurchHeader."No.";
        ToPurchLine."Line No." := NextLineNo;
        ToPurchLine."Copied From Posted Doc." := FromPurchLine."Copied From Posted Doc.";
        ToPurchLine.Validate("Currency Code", FromPurchHeader."Currency Code");
        ValidatePurchLineDiscountFields(FromPurchHeader, ToPurchHeader, ToPurchLine);

        UpdatePurchLine(
          ToPurchHeader, ToPurchLine, FromPurchHeader, FromPurchLine,
          CopyThisLine, RecalculateAmount, FromPurchDocType, CopyPostedDeferral);
        //----------------------------------------CopyPurchDocLine:BEGIN
        //ToPurchLine.CheckLocationOnWMS;
        //
        //if ExactCostRevMandatory and
        //   (FromPurchLine.Type = FromPurchLine.Type::Item) and
        //   (FromPurchLine."Appl.-to Item Entry" <> 0) and
        //   not MoveNegLines
        //then begin
        //    if RecalculateAmount then begin
        //        ToPurchLine.Validate("Direct Unit Cost", FromPurchLine."Direct Unit Cost");
        //        ToPurchLine.Validate("Line Discount %", FromPurchLine."Line Discount %");
        //        ToPurchLine.Validate(
        //          "Line Discount Amount",
        //          Round(FromPurchLine."Line Discount Amount", Currency."Amount Rounding Precision"));
        //        ToPurchLine.Validate(
        //          "Inv. Discount Amount",
        //          Round(FromPurchLine."Inv. Discount Amount", Currency."Amount Rounding Precision"));
        //    end;
        //    ToPurchLine.Validate("Appl.-to Item Entry", FromPurchLine."Appl.-to Item Entry");
        //    if not CreateToHeader then
        //        if ToPurchLine."Expected Receipt Date" = 0D then begin
        //            if ToPurchHeader."Expected Receipt Date" <> 0D then
        //                ToPurchLine."Expected Receipt Date" := ToPurchHeader."Expected Receipt Date"
        //            else
        //                ToPurchLine."Expected Receipt Date" := WorkDate;
        //        end;
        //end;
        // EXAMPLE FROM FUNCTION CopySalesDocLine
        IsHandled := false;
        OnCopySalesDocLineOnBeforeCheckLocationOnWMS(ToSalesHeader, ToSalesLine, FromSalesLine, IsHandled);
        if not IsHandled then
            ToSalesLine.CheckLocationOnWMS;

        RecalculateAndApplySalesLine(ToSalesHeader, ToSalesLine, FromSalesLine, RecalculateAmount);

        //----------------------------------------CopyPurchDocLine:end

        OnCopyPurchLineOnBeforeValidateQuantity(ToPurchLine, RecalculateLines);

        if MoveNegLines and (ToPurchLine.Type <> ToPurchLine.Type::" ") then begin
            ToPurchLine.Validate(Quantity, -FromPurchLine.Quantity);
            OnCopyPurchLineOnAfterValidateQuantityMoveNegLines(ToPurchLine, FromPurchLine);
            ToPurchLine."Appl.-to Item Entry" := FromPurchLine."Appl.-to Item Entry"
        end;

        CopyPurchLineExtText(ToPurchHeader, ToPurchLine, FromPurchHeader, FromPurchLine, DocLineNo, NextLineNo);

        if FromPurchLine."Job No." <> '' then
            CopyPurchaseJobFields(ToPurchLine, FromPurchLine);

        if not RecalculateLines then begin
            ToPurchLine."Dimension Set ID" := FromPurchLine."Dimension Set ID";
            ToPurchLine."Shortcut Dimension 1 Code" := FromPurchLine."Shortcut Dimension 1 Code";
            ToPurchLine."Shortcut Dimension 2 Code" := FromPurchLine."Shortcut Dimension 2 Code";
            OnCopyPurchLineOnAfterSetDimensions(ToPurchLine, FromPurchLine);
        end;

        IsHandled := false;
        OnCopyPurchDocLineOnBeforeCopyThisLine(ToPurchLine, FromPurchLine, MoveNegLines, FromPurchDocType, LinesNotCopied, CopyThisLine, Result, IsHandled, ToPurchHeader);
        if IsHandled then
            exit(Result);

        if CopyThisLine then begin
            OnBeforeInsertToPurchLine(
                ToPurchLine, FromPurchLine, FromPurchDocType.AsInteger(), RecalculateLines, ToPurchHeader, DocLineNo, NextLineNo);
            ToPurchLine.Insert();
            OnAfterInsertToPurchLine(ToPurchLine, FromPurchLine, RecalculateLines, DocLineNo, FromPurchDocType, ToPurchHeader);
        end else
            LinesNotCopied := LinesNotCopied + 1;

        exit(CopyThisLine);
    end;
JesperSchulz commented 3 years 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.