How to solve this issue in Service Contract Invoice creation

Aravindh_NavisionAravindh_Navision Member Posts: 258
Hi Friends,

Navision Default Scenario in Service Contract creation:

When I create a Service Contract, the Starting Date that I give is 12-04-15. I will give the Invoice Period as "Two Months". I can create Service invoice directly while "Signing Contract" or run the the batch job "Create Contract Invoice". So initially the Service Contract creates partial invoice for 12-04-15 to 30-04-15. Then the next invoice creates for 01-05-15 and 31-05-15.

Customized Scenario:

I have done a customization. I will give "Start Inv. from Start Date" (boolean field as TRUE - a customization field). When I create a Service Contract, the Starting Date that I give is 12-04-15. I will give the Invoice Period as "Two Months". In this case I can create the invoice by running the batch job "Create Contract Invoice" only. Then the first invoice creates for the period 12-04-15 to 11-06-15. Then the next invoice creates for 12-06-15 and 11-08-15. All these modifications are running fine.

When I give the Expiration Date (Header's Details tab) or Contract Expiration Date (Line) say between second invoice period 12-06-15 and 11-08-15 or later, then it is working fine. But when I give the expiration date between the first invoice date period 12-04-15 to 11-06-15, I am getting the error "Service Ledger Entry Entry No xxxxx does not exist." or "The date is invalid."

Since I am unable to attach the txt/fob files, I have given the objects's code herewith.

I am calling the customized function in Codeunit from the batch job report.

Report: 6030 - Create Contract Invoices (batch job report)
Service Contract Header - OnAfterGetRecord()
IF (ServContractHeader."Start Inv. from Start Date") THEN BEGIN
  IF intCount = 1 THEN BEGIN
    cuSignServ.UpdateServiceInvoice(ServContractHeader,'');
    cuSignServ.UpdateServiceLedger(ServContractHeader,'');
  END ELSE BEGIN
    cuSignServ.UpdateServiceInvoice(ServContractHeader,InvoiceNo);
    cuSignServ.UpdateServiceLedger(ServContractHeader,InvoiceNo);
  END;
END;

Codeunit 5944 - SignServContractDoc [Customized Area contains customized code]
SignContract(FromServContractHeader : Record "Service Contract Header")
IF NOT HideDialog THEN
  CLEARALL;
ServContractHeader := FromServContractHeader;
IF ServContractHeader.Status = ServContractHeader.Status::Signed THEN
  EXIT;
IF ServContractHeader.Status = ServContractHeader.Status::Canceled THEN
  ERROR(Text024);
ServContractHeader.TESTFIELD("Serv. Contract Acc. Gr. Code");
ServContractHeader.TESTFIELD("Service Period");
ServContractHeader.CALCFIELDS("Calcd. Annual Amount");

IF ServContractHeader."Annual Amount" <> ServContractHeader."Calcd. Annual Amount" THEN
  ERROR(Text023,ServContractHeader.FIELDCAPTION("Annual Amount"),
         ServContractHeader.FIELDCAPTION("Calcd. Annual Amount"));

IF ServContractHeader."Annual Amount" < 0 THEN
  ERROR(Text019);
ServContractAccGr.GET(ServContractHeader."Serv. Contract Acc. Gr. Code");
ServContractAccGr.TESTFIELD("Non-Prepaid Contract Acc.");
GLAcc.GET(ServContractAccGr."Non-Prepaid Contract Acc.");
GLAcc.TESTFIELD("Direct Posting");

IF (ServContractHeader.Prepaid) THEN BEGIN
  ServContractAccGr.TESTFIELD("Prepaid Contract Acc.");
  GLAcc.GET(ServContractAccGr."Prepaid Contract Acc.");
  GLAcc.TESTFIELD("Direct Posting");
END;

ServContractLine.RESET;
ServContractLine.SETRANGE("Contract Type",ServContractHeader."Contract Type");
ServContractLine.SETRANGE("Contract No.",ServContractHeader."Contract No.");
ServContractLine.SETRANGE("Line Amount",0);
IF ServContractLine.FIND('-') THEN
  ERROR(
    Text004,
    ServContractHeader."Contract No.",
    ServContractLine.FIELDCAPTION("Line Amount"));

//>> EC1.65
ServContractLine.SETRANGE("Line Amount");
ServContractLine.SETFILTER("Service Item No.", '<>%1', '');
ServContractLine.SETRANGE("Shortcut Dimension 2 Code", '');
IF ServContractLine.FINDFIRST THEN
  ServContractLine.TESTFIELD("Shortcut Dimension 2 Code")
ELSE
  ServContractLine.RESET;
//<< EC1.65  

//>>EC1.95
ServContractLine2.SETRANGE("Contract Type",ServContractHeader."Contract Type"::Contract);
ServContractLine2.SETRANGE("Contract No.",ServContractHeader."Contract No.");
ServContractLine2.SETRANGE(ServContractLine2."Invoice Description Code", '');
IF ServContractLine2.FINDFIRST THEN
  ERROR('Invoice Description Code must not be blank'  );
  //ServContractLine2.TESTFIELD("Invoice Description Code");
//<<EC1.95

ServContractHeader.TESTFIELD("Starting Date");
IF ServContractHeader."Invoice Period" <> ServContractHeader."Invoice Period"::None THEN BEGIN
  IF ServContractHeader."Annual Amount" = 0 THEN
    ERROR(Text020);
  ServContractHeader.TESTFIELD("Amount per Period");
END;

ServMgtSetup.GET;
IF ServMgtSetup."Salesperson Mandatory" THEN
  ServContractHeader.TESTFIELD("Salesperson Code");
IF ServContractHeader."Invoice Period" <> ServContractHeader."Invoice Period"::None THEN BEGIN
  IF ServContractHeader.Prepaid THEN BEGIN
    IF CALCDATE(
      '<-CM>',ServContractHeader."Next Invoice Date")
      <> ServContractHeader."Next Invoice Date"
    THEN
      ERROR(Text003,ServContractHeader.FIELDCAPTION("Next Invoice Date"));
  END ELSE BEGIN
    IF
      CALCDATE('<CM>',ServContractHeader."Next Invoice Date")
      <> ServContractHeader."Next Invoice Date"
    THEN
      IF NOT HideDialog THEN
        IF NOT CONFIRM(
          STRSUBSTNO(
            Text005,
            ServContractHeader.FIELDCAPTION("Next Invoice Date")))
        THEN
          EXIT;
  END;
END;

IF ServMgtSetup."Contract Rsp. Time Mandatory" THEN BEGIN
  ServContractLine.RESET;
  ServContractLine.SETRANGE("Contract Type",ServContractHeader."Contract Type");
  ServContractLine.SETRANGE("Contract No.",ServContractHeader."Contract No.");
  ServContractLine.SETRANGE("Response Time (Hours)",0);
  IF ServContractLine.FIND('-') THEN
    ServContractLine.FIELDERROR("Response Time (Hours)");
END;
ServContractMgt.CopyCheckSCDimToTempSCDim(ServContractHeader);

IF NOT HideDialog THEN
  IF NOT CONFIRM(Text010,TRUE,ServContractHeader."Contract No.") THEN
    EXIT;

IF NOT HideDialog THEN BEGIN
  FromServContractLine.RESET;
  FromServContractLine.SETRANGE("Contract Type",FromServContractHeader."Contract Type");
  FromServContractLine.SETRANGE("Contract No.",FromServContractHeader."Contract No.");
  FromServContractLine.SETRANGE("Next Planned Service Date",0D);
  IF FromServContractLine.FIND('-') THEN
    IF NOT
       CONFIRM(
         Text022,
         TRUE,
         FromServContractLine.FIELDCAPTION("Next Planned Service Date"))
    THEN
      EXIT;
END;

Window.OPEN(
  Text012 +
  Text013);

FiledServContractHeader.FileQuotationBeforeSigning(ServContractHeader);

// Customized Area

IF (ServContractHeader."Start Inv. from Start Date") THEN BEGIN
    locServContractLine.RESET;
    locServContractLine.SETRANGE("Contract Type",ServContractHeader."Contract Type");
    locServContractLine.SETRANGE("Contract No.",ServContractHeader."Contract No.");
    IF locServContractLine.FIND('-') THEN BEGIN
      REPEAT
        locServContractLine."New Line" := FALSE;
        locServContractLine.MODIFY;
      UNTIL locServContractLine.NEXT = 0;
    END;

    ServContractHeader."Change Status" := ServContractHeader."Change Status"::Locked;
    ServContractHeader.Status := ServContractHeader.Status::Signed;
    ServContractHeader.MODIFY;
    EXIT;
END;

// Customized Area

Window.UPDATE(1,1);
WPostLine := 0;
InvoicingStartingPeriod := FALSE;
IF ServContractHeader."Invoice Period" <> ServContractHeader."Invoice Period"::None THEN BEGIN
  IF ServContractHeader.Prepaid THEN BEGIN
    IF ServContractHeader."Starting Date" < ServContractHeader."Next Invoice Date" THEN BEGIN
      IF HideDialog THEN
        InvoiceNow := TRUE
      ELSE
        IF CONFIRM(Text015,TRUE,
           ServContractHeader."Starting Date",ServContractHeader."Next Invoice Date" - 1)
        THEN
          InvoiceNow := TRUE;
      InvoiceFrom := ServContractHeader."Starting Date";
      InvoiceTo := ServContractHeader."Next Invoice Date" - 1;
    END;
  END ELSE BEGIN
    GoOut := TRUE;
    TempDate := ServContractHeader."Next Invoice Period Start";
    IF ServContractHeader."Starting Date" < TempDate THEN BEGIN
      TempDate := TempDate -1;
      GoOut := FALSE;
    END;
    IF NOT GoOut THEN BEGIN
      IF HideDialog THEN
        InvoiceNow := TRUE
      ELSE
          IF CONFIRM(
            Text015,TRUE,
            ServContractHeader."Starting Date",TempDate)
          THEN
            InvoiceNow := TRUE;
      InvoiceFrom := ServContractHeader."Starting Date";
      InvoiceTo := TempDate;
      InvoicingStartingPeriod := TRUE;
    END;
  END;
END;

IF InvoiceNow THEN BEGIN
  PostingDate := InvoiceFrom;
  AmountNow := ServContractMgt.CalcContractAmount(ServContractHeader,InvoiceFrom,InvoiceTo);
END;

IF InvoiceNow THEN BEGIN
  ServContractHeader."Last Invoice Date" := ServContractHeader."Starting Date";
  ServContractHeader.VALIDATE("Last Invoice Period End",InvoiceTo);
END;

ServContractLine.RESET;
ServContractLine.SETRANGE("Contract Type",FromServContractHeader."Contract Type");
ServContractLine.SETRANGE("Contract No.",FromServContractHeader."Contract No.");
IF ServContractLine.FIND('-') THEN REPEAT
  ServContractLine."Contract Status" := ServContractLine."Contract Status"::Signed;
  ServContractLine.MODIFY;
  CLEAR(ServLogMgt);
  WPostLine := WPostLine + 1;
  Window.UPDATE(2,WPostLine);
UNTIL ServContractLine.NEXT = 0;

IF InvoicingStartingPeriod AND
  NOT ServContractHeader.Prepaid AND
  InvoiceNow
THEN BEGIN
  ServContractHeader.VALIDATE("Last Invoice Date",InvoiceTo);
  ServContractHeader.MODIFY;
END;

IF InvoiceNow THEN BEGIN
  ServContractMgt.InitCodeUnit;

  ServHeaderNo :=
    ServContractMgt.CreateServHeader(ServContractHeader,PostingDate,FALSE);

  ServHeader.GET(ServHeader."Document Type"::Invoice,ServHeaderNo);
  ServMgtSetup.GET;
  AppliedEntry :=
    ServContractMgt.CreateServiceLedgerEntry(
      ServHeader,ServContractHeader."Contract Type",
      ServContractHeader."Contract No.",InvoiceFrom,
      InvoiceTo,AmountNow,TRUE,FALSE,InvAmountRounded);

  IF ServContractHeader."Contract Lines on Invoice" THEN BEGIN
    ServContractLine.RESET;
    ServContractLine.SETRANGE("Contract Type",ServContractHeader."Contract Type");
    ServContractLine.SETRANGE("Contract No.",ServContractHeader."Contract No.");
    IF ServContractLine.FIND('-') THEN
      REPEAT
        ServContractMgt.CreateDetailedServLine(
          ServHeader,
          ServContractLine,
          ServContractHeader."Contract Type",
          ServContractHeader."Contract No.");
       UNTIL ServContractLine.NEXT = 0;
  END ELSE
    ServContractMgt.CreateHeadingServLine(
      ServHeader,
      ServContractHeader."Contract Type",
      ServContractHeader."Contract No.");
  ServContractMgt.CreateServLine(
    ServHeader,
    ServContractHeader."Contract Type",
    ServContractHeader."Contract No.",
    InvoiceFrom,InvoiceTo,InvAmountRounded,AppliedEntry,TRUE);
  ServContractMgt.FinishServiceInvoice(ServHeader."No."); //EC1.32
  ServContractHeader.MODIFY;
  ServContractMgt.FinishCodeunit;
END;

ContractGainLossEntry.AddEntry(
  2,ServContractHeader."Contract Type",
  ServContractHeader."Contract No.",
  ServContractHeader."Annual Amount",'');

ServContractHeader.Status := ServContractHeader.Status::Signed;
ServContractHeader."Change Status" := ServContractHeader."Change Status"::Locked;
ServContractHeader.MODIFY;

ServContractLine.RESET;
ServContractLine.SETRANGE("Contract Type",ServContractHeader."Contract Type");
ServContractLine.SETRANGE("Contract No.",ServContractHeader."Contract No.");
IF ServContractLine.FIND('-') THEN BEGIN
  REPEAT
    ServContractLine."New Line" := FALSE;
    ServContractLine.MODIFY;
  UNTIL ServContractLine.NEXT = 0;
END;

IF ServMgtSetup."Register Contract Changes" THEN
  ContractChangeLog.LogContractChange(
    ServContractHeader."Contract No.",0,ServContractHeader.FIELDCAPTION(Status),0,
    '',FORMAT(ServContractHeader.Status),'',0);

CLEAR(FromServContractHeader);

Window.CLOSE;

IF NOT HideDialog THEN
  IF ServHeaderNo <> '' THEN
    MESSAGE(Text016,ServHeaderNo);
[/code]

Two new function I have created:
UpdateServiceInvoice(recServConHead : Record "Service Contract Header";cdeInvoiceNo : Code[20])
IF cdeInvoiceNo = '' THEN
  dtDate := recServConHead."Starting Date"
ELSE
  dtDate := recServConHead."Last Invoice Date" +1;

recServCondHead1.GET(recServConHead."Contract Type",recServConHead."Contract No.");

recServContLine.RESET;
recServContLine.SETRANGE(recServContLine."Contract Type",recServContLine."Contract Type"::Contract);
recServContLine.SETRANGE(recServContLine."Contract No.", recServConHead."Contract No.");
IF recServContLine.FINDSET THEN
  REPEAT
    recServLine.RESET;
    recServLine.SETRANGE(recServLine."Document Type",recServLine."Document Type"::Invoice);
    IF ( cdeInvoiceNo <> '' )  THEN
      recServLine.SETRANGE(recServLine."Document No.",cdeInvoiceNo);
    recServLine.SETRANGE(recServLine."Contract No.",recServContLine."Contract No.");
    recServLine.SETRANGE(recServLine."Contract Line No.",recServContLine."Line No.");
    IF recServLine.FINDFIRST THEN BEGIN

     CASE recServConHead."Invoice Period" OF
       recServConHead."Invoice Period"::Month:
         BEGIN
           txtStart := FORMAT(dtDate);
           txtEnd := FORMAT(CALCDATE('<+1M>',dtDate) - 1);
  //         recServLine.Description := txtStart + ' - ' + txtEnd;
         END;
       recServConHead."Invoice Period"::"Two Months":
         BEGIN
           txtStart := FORMAT(dtDate);
           txtEnd := FORMAT(CALCDATE('<+2M>',dtDate) - 1);
 //          recServLine.Description := txtStart + ' - ' + txtEnd;
         END;
       recServConHead."Invoice Period"::Quarter:
         BEGIN
           txtStart := FORMAT(dtDate);
           txtEnd := FORMAT(CALCDATE('<+3M>',dtDate) - 1);
 //          recServLine.Description := txtStart + ' - ' + txtEnd;
         END;
       recServConHead."Invoice Period"::"Half Year":
         BEGIN
           txtStart := FORMAT(dtDate);
           txtEnd := FORMAT(CALCDATE('<+6M>',dtDate) - 1);
 //          recServLine.Description := txtStart + ' - ' + txtEnd;
         END;
       recServConHead."Invoice Period"::Year:
         BEGIN
           txtStart := FORMAT(dtDate);
           txtEnd := FORMAT(CALCDATE('<+1Y>',dtDate) - 1);
 //          recServLine.Description := txtStart + ' - ' + txtEnd;
         END;
     END;
      recServLine."Unit Price" := recServContLine."Line Amount";

      IF recServConHead."Expiration Date" > 0D THEN BEGIN
        IF (recServConHead."Next Invoice Date" = recServConHead."Expiration Date") THEN BEGIN
          recServLine."Unit Price" := ROUND(((recServContLine."Line Amount" / 365) * (recServConHead."Next Invoice Date" -
                                       recServConHead."Last Invoice Date")),0.01,'=');

          recServLine.Description := txtStart + ' - ' + FORMAT(recServConHead."Next Invoice Date");

          CASE recServConHead."Invoice Period" OF
            recServConHead."Invoice Period"::Month:
              recServLine."Unit Price" := recServLine."Unit Price"  * 12;
            recServConHead."Invoice Period"::"Two Months":
              recServLine."Unit Price" := recServLine."Unit Price"  * 6;
            recServConHead."Invoice Period"::Quarter:
              recServLine."Unit Price" := recServLine."Unit Price"  * 4;
            recServConHead."Invoice Period"::"Half Year":
              recServLine."Unit Price" := recServLine."Unit Price"  * 2;
            recServConHead."Invoice Period"::Year:
              recServLine."Unit Price" := recServLine."Unit Price"  * 1;
          END;
        END;
      END;

      IF recServContLine."Contract Expiration Date" > 0D THEN BEGIN
        IF ( recServContLine."Contract Expiration Date" = recServContLine."Invoiced to Date") THEN BEGIN
          recServLine."Unit Price" := ROUND(((recServContLine."Line Amount" / 365)
                                   * ( recServContLine."Contract Expiration Date" -  recServConHead."Last Invoice Date")),0.01,'=');

          recServLine.Description :=  FORMAT(recServConHead."Last Invoice Date"+1) + ' - '
                                      + FORMAT(recServContLine."Contract Expiration Date");

            CASE recServConHead."Invoice Period" OF
              recServConHead."Invoice Period"::Month:
                recServLine."Unit Price" := recServLine."Unit Price"  * 12;
              recServConHead."Invoice Period"::"Two Months":
                recServLine."Unit Price" := recServLine."Unit Price"  * 6;
              recServConHead."Invoice Period"::Quarter:
                recServLine."Unit Price" := recServLine."Unit Price"  * 4;
              recServConHead."Invoice Period"::"Half Year":
                recServLine."Unit Price" := recServLine."Unit Price"  * 2;
              recServConHead."Invoice Period"::Year:
                recServLine."Unit Price" := recServLine."Unit Price"  * 1;
            END;
        END;
      END;
     recServLine.Description := 
     FORMAT(recServConHead."Next Invoice Period Start") + ' - ' + FORMAT(recServConHead."Next Invoice Period End");
      recServLine.VALIDATE(recServLine."Unit Price");
      recServLine.MODIFY;
    END;
  UNTIL recServContLine.NEXT = 0;

  EVALUATE(dtStart,txtStart);
  EVALUATE(dtEnd,txtEnd);

  dtOldStart := dtStart;
  CASE recServConHead."Invoice Period" OF
    recServConHead."Invoice Period"::Month:
      BEGIN
        dtStart := (CALCDATE('<+1M>',dtStart));
        dtEnd := (CALCDATE('<+1M>',dtEnd));
      END;
    recServConHead."Invoice Period"::"Two Months":
      BEGIN
        dtStart := (CALCDATE('<+2M>',dtStart));
        dtEnd := (CALCDATE('<+2M>',dtEnd));
      END;
    recServConHead."Invoice Period"::Quarter:
      BEGIN
        dtStart := (CALCDATE('<+3M>',dtStart));
        dtEnd := (CALCDATE('<+3M>',dtEnd));
      END;
    recServConHead."Invoice Period"::"Half Year":
      BEGIN
        dtStart := (CALCDATE('<+6M>',dtStart));
        dtEnd := (CALCDATE('<+6M>',dtEnd));
      END;
    recServConHead."Invoice Period"::Year:
      BEGIN
        dtStart := (CALCDATE('<+1Y>',dtStart));
        dtEnd := (CALCDATE('<+1Y>',dtEnd));
      END;
  END;

  IF (DATE2DMY(dtDate,1) = 31) AND (DATE2DMY(dtStart,1) = 30) THEN BEGIN
    IF (DATE2DMY(CALCDATE('CM',dtStart),1) = 30) THEN
      dtStart := dtStart + 1;
  END;

  IF (DATE2DMY(dtStart,1) = 1) THEN BEGIN
    CASE recServConHead."Invoice Period" OF
      recServConHead."Invoice Period"::Month:
          dtEnd := CALCDATE('CM',dtStart);
      recServConHead."Invoice Period"::"Two Months":
          dtEnd := (CALCDATE('1M+CM',dtStart));
      recServConHead."Invoice Period"::Quarter:
          dtEnd := (CALCDATE('2M+CM',dtStart));
      recServConHead."Invoice Period"::"Half Year":
          dtEnd := (CALCDATE('5M+CM',dtStart));
      recServConHead."Invoice Period"::Year:
          dtEnd := (CALCDATE('11M+CM',dtStart));
      END;
  END;

  IF (DATE2DMY(dtStart,2) = 2)THEN BEGIN
    IF (DATE2DMY(dtOldStart,1) = 29) OR (DATE2DMY(dtOldStart,1) = 30) OR
       (DATE2DMY(dtOldStart,1) = 31) THEN
    BEGIN
      CLEAR(intCheck);
      IF (DATE2DMY(dtStart,1) = 29) THEN
        intCheck := 1;

      IF (DATE2DMY(dtStart,1) = 28) THEN
      BEGIN
        IF (DATE2DMY(CALCDATE('CM',dtStart),1) = 28) THEN
          intCheck := 1;
      END;

      IF (intCheck = 1) THEN BEGIN
        dtStart := dtStart + 1;
        CASE recServConHead."Invoice Period" OF
          recServConHead."Invoice Period"::Month:
            dtEnd := CALCDATE('CM',dtStart);
          recServConHead."Invoice Period"::"Two Months":
            dtEnd := (CALCDATE('1M+CM',dtStart));
          recServConHead."Invoice Period"::Quarter:
            dtEnd := (CALCDATE('2M+CM',dtStart));
          recServConHead."Invoice Period"::"Half Year":
            dtEnd := (CALCDATE('5M+CM',dtStart));
          recServConHead."Invoice Period"::Year:
            dtEnd := (CALCDATE('11M+CM',dtStart));
          END;
      END;
    END;
  END;

  IF (recServConHead."Expiration Date" = 0D) THEN BEGIN
     recServCondHead1."Last Invoice Date" := dtStart - 1;
     recServCondHead1."Next Invoice Date" := dtEnd;
     recServCondHead1."Next Invoice Period Start" := dtStart;
     recServCondHead1."Next Invoice Period End" := dtEnd;
  END ELSE BEGIN
    IF (recServConHead."Next Invoice Date" = recServConHead."Expiration Date") THEN BEGIN
       recServCondHead1."Last Invoice Date" := recServConHead."Expiration Date";
       recServCondHead1."Next Invoice Date" := 0D;
       recServCondHead1."Next Invoice Period Start" := 0D;
       recServCondHead1."Next Invoice Period End" := 0D;
       recServCondHead1."Amount per Period" := 0;
     END ELSE IF (recServConHead."Next Invoice Date" < recServConHead."Expiration Date") THEN BEGIN
         recServCondHead1."Last Invoice Date" := dtStart - 1;

         IF dtEnd < recServConHead."Expiration Date" THEN BEGIN
           recServCondHead1."Next Invoice Date" := dtEnd;
           recServCondHead1."Next Invoice Period End" := dtEnd;
         END ELSE BEGIN
           recServCondHead1."Next Invoice Date" := recServConHead."Expiration Date";
           recServCondHead1."Next Invoice Period End" := recServConHead."Expiration Date";
         END;
         recServCondHead1."Next Invoice Period Start" := dtStart;
          recServCondHead1."Amount per Period" := recServConHead."Annual Amount";
     END;
   END;

   recServCondHead1.MODIFY;

UpdateServiceLedger(recServConHead : Record "Service Contract Header";cdeInvNo : Code[20])
IF cdeInvNo = '' THEN
  dtDate := recServConHead."Starting Date"
ELSE
  dtDate := recServConHead."Last Invoice Date" +1;

recServContLine.RESET;
recServContLine.SETRANGE(recServContLine."Contract Type",recServContLine."Contract Type"::Contract);
recServContLine.SETRANGE(recServContLine."Contract No.",recServConHead."Contract No.");
IF recServContLine.FINDSET THEN
  REPEAT
    recServLedg.RESET;
    recServLedg.SETRANGE(recServLedg."Service Contract No.",recServContLine."Contract No.");
    IF (cdeInvNo <> '')  THEN
      recServLedg.SETRANGE(recServLedg."Document No.",cdeInvNo);

    recServLedg.SETRANGE(recServLedg."Service Item No. (Serviced)",recServContLine."Service Item No.");
    IF recServLedg.FINDFIRST THEN BEGIN
        CASE recServConHead."Invoice Period" OF
          recServConHead."Invoice Period"::Month:
            BEGIN
              txtStart := FORMAT(dtDate);
              txtEnd := FORMAT(CALCDATE('<+1M>',dtDate) - 1);
              recServLedg.Description := txtStart + ' - ' + txtEnd;
            END;
          recServConHead."Invoice Period"::"Two Months":
            BEGIN
              txtStart := FORMAT(dtDate);
              txtEnd := FORMAT(CALCDATE('<+2M>',dtDate) - 1);
              recServLedg.Description := txtStart + ' - ' + txtEnd;
            END;
          recServConHead."Invoice Period"::Quarter:
            BEGIN
              txtStart := FORMAT(dtDate);
              txtEnd := FORMAT(CALCDATE('<+3M>',dtDate) - 1);
              recServLedg.Description := txtStart + ' - ' + txtEnd;
            END;
          recServConHead."Invoice Period"::"Half Year":
            BEGIN
              txtStart := FORMAT(dtDate);
              txtEnd := FORMAT(CALCDATE('<+6M>',dtDate) - 1);
              recServLedg.Description := txtStart + ' - ' + txtEnd;
            END;
          recServConHead."Invoice Period"::Year:
            BEGIN
              txtStart := FORMAT(dtDate);
              txtEnd := FORMAT(CALCDATE('<+1Y>',dtDate) - 1);
              recServLedg.Description := txtStart + ' - ' + txtEnd;
            END;
         END;

         recServLedg."Amount (LCY)" := -recServContLine."Line Amount";
         recServLedg."Unit Price" := -recServContLine."Line Amount";
         recServLedg.Amount := -recServContLine."Line Amount";

         IF recServConHead."Expiration Date" > 0D THEN BEGIN
           IF (recServConHead."Next Invoice Date" = recServConHead."Expiration Date") THEN BEGIN
             recServLedg.Description := txtStart + ' - ' + FORMAT(recServConHead."Next Invoice Date");
             CLEAR(decAmount);
             decAmount := ROUND(((recServContLine."Line Amount" / 365) * (recServConHead."Next Invoice Date" -
                                  recServConHead."Last Invoice Date")),0.01,'=');

             CASE recServConHead."Invoice Period" OF
               recServConHead."Invoice Period"::Month:
                 decAmount := decAmount * 12;
               recServConHead."Invoice Period"::"Two Months":
                 decAmount := decAmount  * 6;
               recServConHead."Invoice Period"::Quarter:
                 decAmount := decAmount  * 4;
               recServConHead."Invoice Period"::"Half Year":
                 decAmount := decAmount  * 2;
               recServConHead."Invoice Period"::Year:
                 decAmount := decAmount  * 1;
             END;

            recServLedg."Amount (LCY)" := -decAmount;
            recServLedg."Unit Price" := -decAmount;
            recServLedg.Amount := -decAmount;
           END;
         END;


      IF recServContLine."Contract Expiration Date" > 0D THEN BEGIN
        IF ( recServContLine."Contract Expiration Date" = recServContLine."Invoiced to Date") THEN BEGIN
          CLEAR(decAmount);
          decAmount := ROUND(((recServContLine."Line Amount" / 365)
                               * ( recServContLine."Contract Expiration Date" -  recServConHead."Last Invoice Date")),0.01,'=');

          recServLedg.Description :=  FORMAT(recServConHead."Last Invoice Date"+1) + ' - '
                                      +  FORMAT(recServContLine."Contract Expiration Date");

            CASE recServConHead."Invoice Period" OF
              recServConHead."Invoice Period"::Month:
                decAmount := decAmount  * 12;
              recServConHead."Invoice Period"::"Two Months":
                decAmount := decAmount  * 6;
              recServConHead."Invoice Period"::Quarter:
                decAmount := decAmount  * 4;
              recServConHead."Invoice Period"::"Half Year":
                decAmount := decAmount  * 2;
              recServConHead."Invoice Period"::Year:
                decAmount := decAmount  * 1;
            END;

            recServLedg."Amount (LCY)" := -decAmount;
            recServLedg."Unit Price" := -decAmount;
            recServLedg.Amount := -decAmount;

        END;
      END;
     recServLedg.Description :=
     FORMAT(recServConHead."Next Invoice Period Start") + ' - ' + FORMAT(recServConHead."Next Invoice Period End");
    recServLedg.MODIFY;
    END;
  UNTIL recServContLine.NEXT = 0;

Can anyone please help me where I have made mistake?

Thanks in advance,
Aravindh R.
Sign In or Register to comment.