Looping Issue

vipinkuruvillavipinkuruvilla Member Posts: 143
Hai All,

Iam creating a payroll system for my company using Navision 4.0
For calculating Bonus Iam using 2 For Loops , for getting the start month and start year.
But the looping is not properly working. I have written my code in OnPush of a button

The following is my code :


StructureDetails.SETRANGE(StructureDetails.Code,Code);
IF StructureDetails.FIND('-') THEN
REPEAT
IF StructureDetails."Tax/Charge Code" ='BONUS' THEN
BEGIN
getMonth:=0;
recBonus.FIND('-');
recBonus.RESET;
RecEmployee.SETRANGE(RecEmployee."No.","Employee No");
IF RecEmployee.FIND('-') THEN
DateOfJoining:=RecEmployee."Data of Joining";
MonthDOJ := DATE2DMY(DateOfJoining, 2);
DOJYear := DATE2DMY(DateOfJoining, 3);
IF recBonus.FIND('-') THEN
strMonth:= recBonus."Start Date";
NoMonth:= DATE2DMY(strMonth, 2);
noYear:= DATE2DMY(strMonth, 3);
strEndMonth:= recBonus."End Date";
NoEndMonth:= DATE2DMY(strEndMonth, 2);
NoEndYear:= DATE2DMY(strEndMonth, 3);
IF (MonthDOJ <= NoMonth) OR (DOJYear <= noYear) THEN
recBonus.SETRANGE(recBonus.Active,TRUE);
IF recBonus.FIND('-') THEN
recBonuspayhead.SETRANGE(recBonuspayhead."Bonus Code",recBonus."Bonus Code");
IF recBonuspayhead.FIND('-') THEN
REPEAT
StrOrderLineDetails.SETRANGE(StrOrderLineDetails."Structure Code",Code);
StrOrderLineDetails.SETRANGE("Tax/Charge Code",recBonuspayhead.PayHead);
IF StrOrderLineDetails.FIND('-') THEN BEGIN
REPEAT
FOR j:= NoMonth TO NoEndMonth DO BEGIN
FOR y:= noYear TO NoEndYear DO BEGIN

BonusAmount:=BonusAmount+StrOrderLineDetails."Actual Amount";
END;
UNTIL StrOrderLineDetails.NEXT=0;
END;
UNTIL recBonuspayhead.NEXT=0;
END;
UNTIL StructureDetails.NEXT=0;
Actualamount:= (BonusAmount*8.33)/100;
IF (Actualamount > 3500) THEN
BEGIN
BonusResult:= 3500 * (StrOrderLineDetails."Actual Amount"/StrOrderLineDetails."Actual Calculation Value");
END ELSE BEGIN
BonusResult:= 3500;
END;
MESSAGE('%1',BonusResult);

Can anyone go through my above code and let me know my mistake??

Any help is appreciated.....

Comments

  • AlbertvhAlbertvh Member Posts: 516
    Hi

    Ithink you have your Begin and End in the wrong places.
    StructureDetails.SETRANGE(StructureDetails.Code,Code); 
    IF StructureDetails.FIND('-') THEN 
      REPEAT
        IF StructureDetails."Tax/Charge Code" ='BONUS' THEN BEGIN 
          getMonth:=0;
          recBonus.FIND('-');
          recBonus.RESET;
          RecEmployee.SETRANGE(RecEmployee."No.","Employee No");
          IF RecEmployee.FIND('-') THEN
            DateOfJoining:=RecEmployee."Data of Joining";
          MonthDOJ := DATE2DMY(DateOfJoining, 2);
          DOJYear := DATE2DMY(DateOfJoining, 3);
          IF recBonus.FIND('-') THEN
            strMonth:= recBonus."Start Date";
          NoMonth:= DATE2DMY(strMonth, 2);
          noYear:= DATE2DMY(strMonth, 3);
          strEndMonth:= recBonus."End Date";
          NoEndMonth:= DATE2DMY(strEndMonth, 2);
          NoEndYear:= DATE2DMY(strEndMonth, 3);
          IF (MonthDOJ <= NoMonth) OR (DOJYear <= noYear) THEN
            recBonus.SETRANGE(recBonus.Active,TRUE);
          IF recBonus.FIND('-') THEN
            recBonuspayhead.SETRANGE(recBonuspayhead."Bonus Code",recBonus."Bonus Code");
          IF recBonuspayhead.FIND('-') THEN
            REPEAT
              StrOrderLineDetails.SETRANGE(StrOrderLineDetails."Structure Code",Code);
              StrOrderLineDetails.SETRANGE("Tax/Charge Code",recBonuspayhead.PayHead);
              IF StrOrderLineDetails.FIND('-') THEN BEGIN
                REPEAT
                  FOR j:= NoMonth TO NoEndMonth DO BEGIN
                    FOR y:= noYear TO NoEndYear DO BEGIN
                      BonusAmount:=BonusAmount+StrOrderLineDetails."Actual Amount";
                    END;
                  END;
                UNTIL StrOrderLineDetails.NEXT=0;
                UNTIL recBonuspayhead.NEXT=0;
              END;
          Actualamount:= (BonusAmount*8.33)/100;
          IF (Actualamount > 3500) THEN
          BEGIN
            BonusResult:= 3500 * (StrOrderLineDetails."Actual Amount"/StrOrderLineDetails."Actual Calculation Value");
          END ELSE BEGIN
            BonusResult:= 3500;
        END;
      UNTIL StructureDetails.NEXT=0;
      MESSAGE('%1',BonusResult);
    

    Hope this helps

    Albert
  • krzychub83krzychub83 Member Posts: 120
    You want to count Bonus for
    2005 JAN, FEB, MAR
    2006 JAN, FEB, MAR
    2007 JAN, FEB, MAR
    This code does it... 9 months only, even if project goes from 01.2005 to 03.2007.
                  FOR j:= NoMonth TO NoEndMonth DO BEGIN
                    FOR y:= noYear TO NoEndYear DO BEGIN
                      BonusAmount:=BonusAmount+StrOrderLineDetails."Actual Amount";
                    END;
                  END;
    

    Maybe You want something like this:
                IF noYear = NoEndYear THEN
                BEGIN
                  FOR j:= NoMonth TO NoEndMonth DO
                    BonusAmount:=BonusAmount+StrOrderLineDetails."Actual Amount";
                END 
                ELSE
                BEGIN
                  FOR y:= noYear TO NoEndYear DO
                  BEGIN
                    IF y=noYear THEN
                    FOR j:= NoMonth TO 12 DO
                      BonusAmount:=BonusAmount+StrOrderLineDetails."Actual Amount"
                    ELSE
                    IF y=NoEndYear THEN
                      FOR j:= 1 TO NoEndMonth DO
                        BonusAmount:=BonusAmount+StrOrderLineDetails."Actual Amount"
                    ELSE
                    FOR j:= 1 TO 12 DO
                      BonusAmount:=BonusAmount+StrOrderLineDetails."Actual Amount";
                  END;
                 END;
    

    I made it on break at work, so there can be some mistakes... I just wanted to show You what in my opinion is "strange".
  • vipinkuruvillavipinkuruvilla Member Posts: 143
                  FOR j:= NoMonth TO NoEndMonth DO BEGIN
                    FOR y:= noYear TO NoEndYear DO BEGIN
                      BonusAmount:=BonusAmount+StrOrderLineDetails."Actual Amount";
                    END;
                  END;
    

    Iam not getting any values in Y. Iam not sure why this is happening. Iam getting value in j but no value in y. Can anyone tell me why this is so?
  • krzychub83krzychub83 Member Posts: 120
    1. Set a trap befor that line
    (FOR j:= NoMonth TO NoEndMonth DO BEGIN)
    and tell me what values do You have in:
    - NoMonth
    - NoEndMonth
    - noYear
    - NoEndYear
    2. What is the point of using strEndMonth? In this code I don't see a reason why it should be declare.
  • AlbertvhAlbertvh Member Posts: 516
    Hi
    It could also be that noyear and noendyear as the same value

    Just a question
    You are always finding the first bonus record regardless if it is for the right employee or not.

    BTW my code should read
          IF recBonuspayhead.FIND('-') THEN 
            REPEAT 
              StrOrderLineDetails.SETRANGE(StrOrderLineDetails."Structure Code",Code); 
              StrOrderLineDetails.SETRANGE("Tax/Charge Code",recBonuspayhead.PayHead); 
              IF StrOrderLineDetails.FIND('-') THEN BEGIN 
                REPEAT 
                  FOR j:= NoMonth TO NoEndMonth DO BEGIN 
                    FOR y:= noYear TO NoEndYear DO BEGIN 
                      BonusAmount:=BonusAmount+StrOrderLineDetails."Actual Amount"; 
                    END; 
                  END; 
                UNTIL StrOrderLineDetails.NEXT=0; 
              END; 
          UNTIL recBonuspayhead.NEXT=0; 
          Actualamount:= (BonusAmount*8.33)/100;
    
  • krzychub83krzychub83 Member Posts: 120
    It could also be that noyear and noendyear as the same value

    Even if they are the same, this code should run once for evry month in NoMonth..NoEndMonth.
  • krzychub83krzychub83 Member Posts: 120
    This Code is wrong:
    FOR j:= NoMonth TO NoEndMonth DO BEGIN 
                    FOR y:= noYear TO NoEndYear DO BEGIN 
                      BonusAmount:=BonusAmount+StrOrderLineDetails."Actual Amount"; 
                    END; 
                  END;
    

    Example:
    StartDate 10\25\2006
    EndDate 1\20\2008
    The project is running for few months, but this code is:
    FOR j:=10 TO 1 DO BEGIN  // Is not going to loop 
      FOR y:=2006 TO 2008 DO BEGIN 
    
  • knightknight Member Posts: 45
    In that case, before the For loop he would need this:
    IF NoEndMonth < NoMonth THEN
    NoEndMonth += 12;
    FOR j:=10 TO 1 DO BEGIN  // Is not going to loop
      FOR y:=2006 TO 2008 DO BEGIN
    
  • krzychub83krzychub83 Member Posts: 120
    yes knight
    , but still he is loosing some months from "middle" year. I just don't belive tha he wants to do it. In my example it is 8 months from 2007 year.

    PS: You code should be in my first example code
    (Posted: Wed Jun 25, 2008 12:29 pm):
    IF noYear = NoEndYear THEN 
                BEGIN 
                    IF NoEndMonth < NoMonth THEN NoEndMonth += 12;
                    FOR j:= NoMonth TO NoEndMonth DO 
                    BonusAmount:=BonusAmount+StrOrderLineDetails."Actual Amount"; 
                END
    
  • knightknight Member Posts: 45
    well talk about teamwork.. the combination of our code creates the solution? :P
Sign In or Register to comment.