Iterations

dabba23dabba23 Member Posts: 77
hey newbie on iterations.

I have a table like the one below
From Qty	       Price
0	              0,08
1500	           0,07
2000	           0,06
2500	           0,05


Now I need to find the rigth price for, let's say, the quantity of 2600.

Therefore, for the first quantity of 1499, the price is 0,08 with a total of 119,92.
The second quantity of 500 (1500 - 1999), the price is 0,07 with a total of 35.
The third quantity of 500 (2000 - 2499), the price is 0,06 with a total of 30.
The fourth quantity of 100 (2500 - 2600), the price is 0,05 with a total of 5.

Now how do I write a piece of code that capture this structure :?:

Thanks Ann

Comments

  • krikikriki Member, Moderator Posts: 9,118
    I created a table as follows:
    10:From Quantity:Decimal (this field is the primary key)
    20:Price:Decimal
    100:Used Quantity:Decimal (this field is only used in the temptable)
    110:Total Price:Decimal (this field is only used in the temptable)
    

    And a small function in which I give the quantity and a temptable that will be filled up.
    FindTotalPrice(IdecQuantity : Decimal;VAR OtmpTheTable : TEMPORARY Record "The Table") OdecReturnValue : Decimal
    // FindTotalPrice
    // Finds the total price for a certain quantity
    // PARAMETERS:
    //   IdecQuantity : quantity to divide
    //   OtmpTheTable : temptable that will contain the quantity used in a certain record
    //   RETURN-VALUE : total price of all items
    
    OdecReturnValue := 0;
    
    OtmpTheTable.RESET;
    OtmpTheTable.DELETEALL(FALSE);
    
    LrecTheTable.RESET;
    LrecTheTable.SETCURRENTKEY("From Quantity");
    LrecTheTable.ASCENDING(FALSE);
    LrecTheTable.SETRANGE("From Quantity",0,IdecQuantity);
    IF LrecTheTable.FIND('-') THEN
      REPEAT
        OtmpTheTable := LrecTheTable;
        OtmpTheTable."Used Quantity" := IdecQuantity - LrecTheTable."From Quantity" + 1;
        IF OtmpTheTable."From Quantity" = 0 THEN
          OtmpTheTable."Used Quantity" -= 1;
        
        IdecQuantity -= OtmpTheTable."Used Quantity";
        
        OtmpTheTable."Total Price" := OtmpTheTable."Used Quantity" * OtmpTheTable.Price;
    
        OtmpTheTable.INSERT(FALSE);
        
        OdecReturnValue += OtmpTheTable."Total Price";
      UNTIL LrecTheTable.NEXT = 0;
    

    And this is the result for for a quantity of 2600:
    From Qty	Price	Used Qty	Total Price
    0,000.00	0.08	1499			119.92
    1,500.00	0.07	 500			35.00
    2,000.00	0.06	 500			30.00
    2,500.00	0.05	 101			5.05
    

    And this means that your calculation was wrong [-X

    Controlling :
    Your quantities (1499+500+500+100) and that makes a total of 2599. :(
    My quantities (1499+500+500+101) and that makes a total of 2600. :D

    But your problem is solved :D:D:D
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.