invoice discount via webservice

vsnasevsnase Member Posts: 23
edited 2011-08-22 in NAV Three Tier
Hi all,

I have published pages 42 and 43 as webservices, I can read and create new orders. Now I want to enter also an invoice discount similar to F9 in classic client. Therefore I published also pages 160 and 402. My code in .NET runs without errors but no data is saved into NAV.

Did anyone manage to enter an invoice-discount into NAV via webservices? How?

Thanks for any help


Volker


I posted this also on dynamicsuser.net

Comments

  • yukonyukon Member Posts: 361
    Hi vsnase,

    You can give invoice discount using with webservice. But you need to do some customization. If you are using R2 no need change a lot. 2009 SP1(SG) need to change field to global variable. 2009 W1 no need to change. I don't know this problem :D

    Remarks,

    You cannot be give both discount. It means, if you give Inv. Discount Amount, total Excl. Gst cannot work. You can give only one.


    Best Regards,
    Yukon
    Make Simple & Easy
  • vsnasevsnase Member Posts: 23
    Hi Yukon,

    I checked W1 but there is no difference to our version of pages 160 and 402.

    Our version is R2.

    If I understood well, then You managed to add a invoice-discount to an order. You mentioned to change some fields to global variable. Could You explain a little bit? Or if explanation is to much You could send me Your changed version of page 160 or 402.

    It is terrible if You have code running without errors but it is not doing what You expect - and if You know someone did it before.

    Volker
  • yukonyukon Member Posts: 361
    Hi vsnase,

    Please try with this one. You will be see what is changes.
    OBJECT Page 50007 Sales Statistics Ws
    {
      OBJECT-PROPERTIES
      {
        Date=03/05/11;
        Time=[ 7:47:42 PM];
        Modified=Yes;
        Version List=NAVW16.00,NAVAP4.00.01;
      }
      PROPERTIES
      {
        CaptionML=ENU=Sales Statistics;
        InsertAllowed=No;
        DeleteAllowed=No;
        LinksAllowed=No;
        SourceTable=Table36;
        PageType=Document;
        OnOpenPage=BEGIN
                     IF ISSERVICETIER AND NOT GUIALLOWED THEN BEGIN
                       CalcInvDiscForHeader;
                       COMMIT;
                     END;
    
                     SalesSetup.GET;
                     AllowInvDisc :=
                       NOT (SalesSetup."Calc. Inv. Discount" AND CustInvDiscRecExists("Invoice Disc. Code"));
                     AllowVATDifference :=
                       SalesSetup."Allow VAT Difference" AND
                       NOT ("Document Type" IN ["Document Type"::Quote,"Document Type"::"Blanket Order"]);
                     CurrPage.EDITABLE :=
                       AllowVATDifference OR AllowInvDisc;
                     SetVATSpecification;
                   END;
    
        OnAfterGetRecord=VAR
                           SalesLine@1000 : Record 37;
                           TempSalesLine@1001 : TEMPORARY Record 37;
                         BEGIN
                           CurrPage.CAPTION(STRSUBSTNO(Text000,"Document Type"));
                           IF PrevNo = "No." THEN
                             EXIT;
                           PrevNo := "No.";
                           FILTERGROUP(2);
                           SETRANGE("No.",PrevNo);
                           FILTERGROUP(0);
                           CLEAR(SalesLine);
                           CLEAR(TotalSalesLine);
                           CLEAR(TotalSalesLineLCY);
                           CLEAR(SalesPost);
    
                           SalesPost.GetSalesLines(Rec,TempSalesLine,0);
                           CLEAR(SalesPost);
                           SalesPost.SumSalesLinesTemp(
                             Rec,TempSalesLine,0,TotalSalesLine,TotalSalesLineLCY,
                             VATAmount,VATAmountText,ProfitLCY,ProfitPct,TotalAdjCostLCY);
    
                           {===Caption Class===}
                           g_recLanguage.GET("Language Code");
                           g_txtAmtCapt := g_cuApplicationMgt.CaptionClassTranslate(g_recLanguage."Windows Language ID",GetCaptionClass(Text002,FALSE));
                           g_txtTotAmtCapt := g_cuApplicationMgt.CaptionClassTranslate(g_recLanguage."Windows Language ID",GetCaptionClass(Text001,FALSE));
                           g_txtTotAmtInclCapt := g_cuApplicationMgt.CaptionClassTranslate(g_recLanguage."Windows Language ID",GetCaptionClass(Text001,TRUE));
    
                           AdjProfitLCY := TotalSalesLineLCY.Amount - TotalAdjCostLCY;
                           IF TotalSalesLineLCY.Amount <> 0 THEN
                             AdjProfitPct := ROUND(AdjProfitLCY / TotalSalesLineLCY.Amount * 100,0.1);
    
                           IF "Prices Including VAT" THEN BEGIN
                             TotalAmount2 := TotalSalesLine.Amount;
                             TotalAmount1 := TotalAmount2 + VATAmount;
                             TotalSalesLine."Line Amount" := TotalAmount1 + TotalSalesLine."Inv. Discount Amount";
                           END ELSE BEGIN
                             TotalAmount1 := TotalSalesLine.Amount;
                             TotalAmount2 := TotalSalesLine."Amount Including VAT";
                           END;
    
                           IF Cust.GET("Bill-to Customer No.") THEN
                             Cust.CALCFIELDS("Balance (LCY)")
                           ELSE
                             CLEAR(Cust);
    
                           g_decCustBalLCY:= Cust."Balance (LCY)";
                           g_decCustCreditLmtLCY := Cust."Credit Limit (LCY)";
    
                           IF Cust."Credit Limit (LCY)" = 0 THEN
                             CreditLimitLCYExpendedPct := 0
                           ELSE
                             IF Cust."Balance (LCY)" / Cust."Credit Limit (LCY)" < 0 THEN
                               CreditLimitLCYExpendedPct := 0
                             ELSE
                               IF Cust."Balance (LCY)" / Cust."Credit Limit (LCY)" > 1 THEN
                                 CreditLimitLCYExpendedPct := 10000
                               ELSE
                                 CreditLimitLCYExpendedPct := ROUND(Cust."Balance (LCY)" / Cust."Credit Limit (LCY)" * 10000,1);
    
                           SalesLine.CalcVATAmountLines(0,Rec,TempSalesLine,TempVATAmountLine);
                           TempVATAmountLine.MODIFYALL(Modified,FALSE);
                           SetVATSpecification;
                           fn_wsUpdateToSalesLine();
                         END;
    
        OnQueryClosePage=BEGIN
                           GetVATSpecification;
                           IF TempVATAmountLine.GetAnyLineModified THEN
                             UpdateVATOnSalesLines;
                           EXIT(TRUE);
                         END;
    
      }
      CONTROLS
      {
        { 1900000001;0;Container;
                    ContainerType=ContentArea }
    
        { 1   ;1   ;Group     ;
                    CaptionML=ENU=General }
    
        { 1000000000;2;Field  ;
                    SourceExpr="Document Type";
                    Visible=FALSE }
    
        { 1000000001;2;Field  ;
                    SourceExpr="No.";
                    Visible=FALSE }
    
        { 129 ;2   ;Field     ;
                    Name=<Control129>;
                    CaptionML=ENU=Amount;
                    SourceExpr=TotalSalesLine."Line Amount";
                    AutoFormatType=1;
                    AutoFormatExpr="Currency Code";
                    CaptionClass=GetCaptionClass(Text002,FALSE);
                    Editable=FALSE }
    
        { 1000000020;2;Field  ;
                    Name=Amount Capt;
                    SourceExpr=g_txtAmtCapt;
                    Visible=FALSE }
    
        { 93  ;2   ;Field     ;
                    CaptionML=ENU=Inv. Discount Amount;
                    DecimalPlaces=0:5;
                    SourceExpr=TotalSalesLine."Inv. Discount Amount";
                    AutoFormatType=1;
                    AutoFormatExpr="Currency Code";
                    OnValidate=BEGIN
                                 UpdateInvDiscAmount;
                               END;
                                }
    
        { 80  ;2   ;Field     ;
                    CaptionML=ENU=Total;
                    SourceExpr=TotalAmount1;
                    AutoFormatType=1;
                    AutoFormatExpr="Currency Code";
                    CaptionClass=GetCaptionClass(Text001,FALSE);
                    OnValidate=BEGIN
                                 IF ISSERVICETIER AND GUIALLOWED THEN
                                   UpdateTotalAmount;
                               END;
                                }
    
        { 1000000017;2;Field  ;
                    Name=Total Amount Capt;
                    CaptionML=ENU=g_txtTotAmtCapt;
                    SourceExpr=g_txtTotAmtCapt;
                    Visible=FALSE }
    
        { 75  ;2   ;Field     ;
                    CaptionML=ENU=GST Amount;
                    SourceExpr=VATAmount;
                    AutoFormatType=1;
                    AutoFormatExpr="Currency Code";
                    CaptionClass=FORMAT (VATAmountText) }
    
        { 1000000018;2;Field  ;
                    Name=VAT Amount Text;
                    SourceExpr=FORMAT (VATAmountText);
                    Visible=FALSE }
    
        { 76  ;2   ;Field     ;
                    CaptionML=ENU=Total Incl. GST;
                    SourceExpr=TotalAmount2;
                    AutoFormatType=1;
                    AutoFormatExpr="Currency Code";
                    CaptionClass=GetCaptionClass(Text001,TRUE) }
    
        { 1000000019;2;Field  ;
                    Name=Total Incl. GST Capt;
                    SourceExpr=g_txtTotAmtInclCapt;
                    Visible=FALSE }
    
        { 2   ;2   ;Field     ;
                    CaptionML=ENU=Sales (LCY);
                    SourceExpr=g_decAmtLCY;
                    AutoFormatType=1 }
    
        { 1000000002;2;Field  ;
                    Name=<ProfitLCY>;
                    CaptionML=ENU=Original Profit (LCY);
                    SourceExpr=ProfitLCY;
                    AutoFormatType=1 }
    
        { 1000000003;2;Field  ;
                    Name=<AdjProfitLCY>;
                    CaptionML=ENU=Adjusted Profit (LCY);
                    SourceExpr=AdjProfitLCY;
                    AutoFormatType=1 }
    
        { 1000000004;2;Field  ;
                    Name=<ProfitPct>;
                    CaptionML=ENU=Original Profit %;
                    DecimalPlaces=1:1;
                    SourceExpr=ProfitPct }
    
        { 1000000005;2;Field  ;
                    Name=<AdjProfitPct>;
                    CaptionML=ENU=Adjusted Profit %;
                    DecimalPlaces=1:1;
                    SourceExpr=AdjProfitPct }
    
        { 1000000006;2;Field  ;
                    Name=<Control95>;
                    CaptionML=ENU=Quantity;
                    DecimalPlaces=0:5;
                    SourceExpr=TotalSalesLine.Quantity;
                    Editable=FALSE }
    
        { 1000000007;2;Field  ;
                    Name=<Control73>;
                    CaptionML=ENU=Parcels;
                    DecimalPlaces=0:5;
                    SourceExpr=TotalSalesLine."Units per Parcel";
                    Editable=FALSE }
    
        { 1000000008;2;Field  ;
                    Name=<Control91>;
                    CaptionML=ENU=Net Weight;
                    DecimalPlaces=0:5;
                    SourceExpr=g_decNetWeight }
    
        { 1000000009;2;Field  ;
                    Name=<Control82>;
                    CaptionML=ENU=Gross Weight;
                    DecimalPlaces=0:5;
                    SourceExpr=g_decGrossWeight }
    
        { 1000000010;2;Field  ;
                    Name=<Control71>;
                    CaptionML=ENU=Volume;
                    DecimalPlaces=0:5;
                    SourceExpr=g_decUnitVolume }
    
        { 1000000011;2;Field  ;
                    Name=<Control78>;
                    CaptionML=ENU=Original Cost (LCY);
                    SourceExpr=g_decUnitCostLCY;
                    AutoFormatType=1 }
    
        { 1000000012;2;Field  ;
                    Name=<TotalAdjCostLCY>;
                    CaptionML=ENU=Adjusted Cost (LCY);
                    SourceExpr=TotalAdjCostLCY;
                    AutoFormatType=1 }
    
        { 1000000013;2;Field  ;
                    Name=<Control7>;
                    CaptionML=ENU=Cost Adjmt. Amount (LCY);
                    SourceExpr=TotalAdjCostLCY - g_decUnitCostLCY;
                    AutoFormatType=1;
                    Editable=FALSE;
                    OnLookup=BEGIN
                               LookupAdjmtValueEntries(0);
                             END;
                              }
    
        { 5   ;1   ;Part      ;
                    Name=SubForm;
                    PagePartID=Page576 }
    
        { 1903289601;1;Group  ;
                    CaptionML=ENU=Customer }
    
        { 1000000015;2;Field  ;
                    Name=<Control67>;
                    CaptionML=ENU=Balance (LCY);
                    SourceExpr=g_decCustBalLCY;
                    AutoFormatType=1 }
    
        { 1000000014;2;Field  ;
                    Name=<Control68>;
                    CaptionML=ENU=Credit Limit (LCY);
                    SourceExpr=g_decCustCreditLmtLCY;
                    AutoFormatType=1 }
    
        { 1000000016;2;Field  ;
                    Name=<CreditLimitLCYExpendedPct>;
                    ExtendedDatatype=Ratio;
                    CaptionML=ENU=Expended % of Credit Limit (LCY);
                    ToolTipML=ENU=Expended % of Credit Limit (LCY);
                    SourceExpr=CreditLimitLCYExpendedPct }
    
      }
      CODE
      {
        VAR
          Text000@1000 : TextConst 'ENU=Sales %1 Statistics';
          Text001@1001 : TextConst 'ENU=Total';
          Text002@1002 : TextConst 'ENU=Amount';
          Text003@1003 : TextConst 'ENU=%1 must not be 0.';
          Text004@1004 : TextConst 'ENU=%1 must not be greater than %2.';
          Text005@1005 : TextConst 'ENU=You cannot change the invoice discount because there is a %1 record for %2 %3.';
          TotalSalesLine@1006 : Record 37;
          TotalSalesLineLCY@1007 : Record 37;
          Cust@1008 : Record 18;
          TempVATAmountLine@1009 : TEMPORARY Record 290;
          SalesSetup@1010 : Record 311;
          SalesPost@1011 : Codeunit 80;
          TotalAmount1@1012 : Decimal;
          TotalAmount2@1013 : Decimal;
          VATAmount@1014 : Decimal;
          VATAmountText@1015 : Text[30];
          ProfitLCY@1016 : Decimal;
          ProfitPct@1017 : Decimal;
          AdjProfitLCY@1024 : Decimal;
          AdjProfitPct@1023 : Decimal;
          TotalAdjCostLCY@1022 : Decimal;
          CreditLimitLCYExpendedPct@1018 : Decimal;
          PrevNo@1019 : Code[20];
          AllowInvDisc@1020 : Boolean;
          AllowVATDifference@1021 : Boolean;
          g_cuApplicationMgt@1000000012 : Codeunit 1;
          g_recLanguage@1000000011 : Record 8;
          g_decLineAmt@1000000001 : Decimal;
          g_decInvDiscAmt@1000000002 : Decimal;
          g_decAmtLCY@1000000003 : Decimal;
          g_decNetWeight@1000000004 : Decimal;
          g_decGrossWeight@1000000005 : Decimal;
          g_decUnitVolume@1000000006 : Decimal;
          g_decUnitCostLCY@1000000007 : Decimal;
          g_decCustBalLCY@1000000008 : Decimal;
          g_decCustCreditLmtLCY@1000000009 : Decimal;
          g_txtAmtCapt@1000000014 : Text[30];
          g_txtTotAmtCapt@1000000010 : Text[30];
          g_txtTotAmtInclCapt@1000000013 : Text[30];
    
        LOCAL PROCEDURE UpdateHeaderInfo@5();
        VAR
          CurrExchRate@1000 : Record 330;
          UseDate@1001 : Date;
        BEGIN
          TotalSalesLine."Inv. Discount Amount" := TempVATAmountLine.GetTotalInvDiscAmount;
          TotalAmount1 :=
            TotalSalesLine."Line Amount" - TotalSalesLine."Inv. Discount Amount";
          VATAmount := TempVATAmountLine.GetTotalVATAmount;
          IF "Prices Including VAT" THEN BEGIN
            TotalAmount1 := TempVATAmountLine.GetTotalAmountInclVAT;
            TotalAmount2 := TotalAmount1 - VATAmount;
            TotalSalesLine."Line Amount" := TotalAmount1 + TotalSalesLine."Inv. Discount Amount";
          END ELSE
            TotalAmount2 := TotalAmount1 + VATAmount;
    
          IF "Prices Including VAT" THEN
            TotalSalesLineLCY.Amount := TotalAmount2
          ELSE
            TotalSalesLineLCY.Amount := TotalAmount1;
          IF "Currency Code" <> '' THEN BEGIN
            IF ("Document Type" IN ["Document Type"::"Blanket Order","Document Type"::Quote]) AND
               ("Posting Date" = 0D)
            THEN
              UseDate := WORKDATE
            ELSE
              UseDate := "Posting Date";
    
            TotalSalesLineLCY.Amount :=
              CurrExchRate.ExchangeAmtFCYToLCY(
                UseDate,"Currency Code",TotalSalesLineLCY.Amount,"Currency Factor");
          END;
          ProfitLCY := TotalSalesLineLCY.Amount - TotalSalesLineLCY."Unit Cost (LCY)";
          IF TotalSalesLineLCY.Amount = 0 THEN
            ProfitPct := 0
          ELSE
            ProfitPct := ROUND(100 * ProfitLCY / TotalSalesLineLCY.Amount,0.01);
    
          AdjProfitLCY := TotalSalesLineLCY.Amount - TotalAdjCostLCY;
          IF TotalSalesLineLCY.Amount = 0 THEN
            AdjProfitPct := 0
          ELSE
            AdjProfitPct := ROUND(100 * AdjProfitLCY / TotalSalesLineLCY.Amount,0.01);
        END;
    
        LOCAL PROCEDURE GetVATSpecification@21();
        BEGIN
          CurrPage.SubForm.FORM.GetTempVATAmountLine(TempVATAmountLine);
          IF TempVATAmountLine.GetAnyLineModified THEN
            UpdateHeaderInfo;
        END;
    
        LOCAL PROCEDURE SetVATSpecification@11();
        BEGIN
          CurrPage.SubForm.FORM.SetTempVATAmountLine(TempVATAmountLine);
          CurrPage.SubForm.FORM.InitGlobals(
            "Currency Code",AllowVATDifference,AllowVATDifference,
            "Prices Including VAT",AllowInvDisc,"VAT Base Discount %",1);
        END;
    
        LOCAL PROCEDURE UpdateTotalAmount@16();
        VAR
          SaveTotalAmount@1000 : Decimal;
        BEGIN
          CheckAllowInvDisc;
          IF "Prices Including VAT" THEN BEGIN
            SaveTotalAmount := TotalAmount1;
            UpdateInvDiscAmount;
            TotalAmount1 := SaveTotalAmount;
          END;
          WITH TotalSalesLine DO
            "Inv. Discount Amount" := "Line Amount" - TotalAmount1;
          UpdateInvDiscAmount;
        END;
    
        LOCAL PROCEDURE UpdateInvDiscAmount@3();
        VAR
          InvDiscBaseAmount@1000 : Decimal;
        BEGIN
          CheckAllowInvDisc;
          InvDiscBaseAmount := TempVATAmountLine.GetTotalInvDiscBaseAmount(FALSE,"Currency Code");
          IF InvDiscBaseAmount = 0 THEN
            ERROR(Text003,TempVATAmountLine.FIELDCAPTION("Inv. Disc. Base Amount"));
    
          IF TotalSalesLine."Inv. Discount Amount" / InvDiscBaseAmount > 1 THEN
            ERROR(
              Text004,
              TotalSalesLine.FIELDCAPTION("Inv. Discount Amount"),
              TempVATAmountLine.FIELDCAPTION("Inv. Disc. Base Amount"));
    
          TempVATAmountLine.SetInvoiceDiscountAmount(
            TotalSalesLine."Inv. Discount Amount","Currency Code","Prices Including VAT","VAT Base Discount %");
    
          CurrPage.SubForm.FORM.SetTempVATAmountLine(TempVATAmountLine);
          UpdateHeaderInfo;
    
          "Invoice Discount Calculation" := "Invoice Discount Calculation"::Amount;
          "Invoice Discount Value" := TotalSalesLine."Inv. Discount Amount";
          MODIFY;
          UpdateVATOnSalesLines;
          fn_wsUpdateToSalesLine();
        END;
    
        LOCAL PROCEDURE GetCaptionClass@2(FieldCaption@1000 : Text[100];ReverseCaption@1001 : Boolean) : Text[80];
        BEGIN
          IF "Prices Including VAT" XOR ReverseCaption THEN
            EXIT('2,1,' + FieldCaption)
          ELSE
            EXIT('2,0,' + FieldCaption);
        END;
    
        LOCAL PROCEDURE UpdateVATOnSalesLines@1();
        VAR
          SalesLine@1000 : Record 37;
        BEGIN
          GetVATSpecification;
          IF TempVATAmountLine.GetAnyLineModified THEN BEGIN
            SalesLine.UpdateVATOnLines(0,Rec,SalesLine,TempVATAmountLine);
            SalesLine.UpdateVATOnLines(1,Rec,SalesLine,TempVATAmountLine);
          END;
          PrevNo := '';
        END;
    
        LOCAL PROCEDURE CustInvDiscRecExists@4(InvDiscCode@1000 : Code[20]) : Boolean;
        VAR
          CustInvDisc@1001 : Record 19;
        BEGIN
          CustInvDisc.SETRANGE(Code,InvDiscCode);
          EXIT(CustInvDisc.FIND('-'));
        END;
    
        LOCAL PROCEDURE CheckAllowInvDisc@8();
        VAR
          CustInvDisc@1000 : Record 19;
        BEGIN
          IF NOT AllowInvDisc THEN
            ERROR(
              Text005,
              CustInvDisc.TABLECAPTION,FIELDCAPTION("Invoice Disc. Code"),"Invoice Disc. Code");
        END;
    
        LOCAL PROCEDURE fn_wsUpdateToSalesLine@1000000002();
        BEGIN
          g_decLineAmt := TotalSalesLine."Line Amount";
          g_decInvDiscAmt := TotalSalesLine."Inv. Discount Amount";
          g_decAmtLCY := TotalSalesLineLCY.Amount;
          g_decNetWeight := TotalSalesLine."Net Weight";
          g_decGrossWeight := TotalSalesLine."Gross Weight";
          g_decUnitVolume := TotalSalesLine."Unit Volume";
          g_decUnitCostLCY := TotalSalesLineLCY."Unit Cost (LCY)";
        END;
    
        BEGIN
        END.
      }
    }
    

    Remarks! It is 2009 SP1 SG Version.

    Hope this help to you.


    Regards,
    Yukon
    Make Simple & Easy
  • vsnasevsnase Member Posts: 23
    Hi Yukon,

    Many Thanks!! It works, but there is one thing I do not understand:
    OnOpenPage=BEGIN
                     IF ISSERVICETIER AND NOT GUIALLOWED THEN BEGIN
                       CalcInvDiscForHeader;
                       COMMIT;
                     END;
    

    What do I need this for? In our case it works only without above. Until now I did not find any wrong result in NAV.


    Volker
  • yukonyukon Member Posts: 361
    Hi Volker,
                     IF ISSERVICETIER AND NOT GUIALLOWED THEN BEGIN
                       CalcInvDiscForHeader;
                       COMMIT;
                     END;
    

    If you open this page from WS, "CalcinvDiscForHeader' func: will work. If you open from RTC it will be skip. Because of this function already has "Page Action". Look at your Pg-41\Page Action\Statistics.

    Best Regards,
    Yukon
    Make Simple & Easy
  • vsnasevsnase Member Posts: 23
    Hi Yukon,

    in may case it is just the other way round. When connecting via WS I get an error saying that Documenttype=Quote, No=. But record has Documenttype=Invoice and No=R2011_4.

    As I mentioned without above code-fragment it works fin via WS and as mentioned until now I did not find any errors or wrong results in NAV. Can it be, because Your code is for SP1 and we are using R2? Or didn't I find the wrong result in NAV until now?

    Volker
  • smarkrichsmarkrich Member Posts: 1
    My only intention is to maximize my working business capital. I have also heard about invoice factoring. Are invoice factoring and invoice Discounting the same?
    Mark Rich is working for Business Finance as a writer.

    They provide you techniques on how to maintain your business working capital.
Sign In or Register to comment.