Hello Silvio,
I created a function for this (
Tested and Updated 28.03.2009 ).
With that it is possible to calculate the price, even with using days are
not Linear.
In my application, I used it for the calculation of fluctuate prices of articles.
Another exsample how to
keep the price between two given points ,
You can find in my answer to Amando at the bottom. It is very easy and uses the same logic.
Just put some values and tell me Your results.
There are some control-Alerts You can use.
Silvio, maybe You can change the title of the topic in :
How to calculate fluctuate prices ?
If somebody needs this calulation, it is better to find.
Test-calculation (
interpolating ) between two given values :
7 Days = 50 Euro and 14 Days = 100 Euro
-----------------------------------------------
7 Days = 50,00 Euro
8 Days = 57,14 Euro
9 Days = 64,29 Euro
10 Days = 71,42 Euro
11 Days = 78,57 Euro
12 Days = 85,71 Euro
13 Days = 92,86 Euro
14 Days = 100,00 Euro
FUNCTION PRICE_TEST(oWnd)
LOCAL oDlg, oGET
// A array DAYS and PRICE
// ----------------------------
PRIVATE aPRICE[6][2]
aPRICE[1] := { 1, 10 }
aPRICE[2] := { 7, 50 } // 12 Days = 50 + ( ( ( 100 - 50 ) / 7 ) * 5 ) = 85.71
aPRICE[3] := { 14, 100 }
aPRICE[4] := { 21, 130 } // 25 Days = 130 + ( ( ( 200 - 130 ) / 7 ) * 4 ) = 170
aPRICE[5] := { 28, 200 }
aPRICE[6] := { 35, 200 } // 30 Days = 200 + ( ( 200 / ( 35 - 28 ) * 2 ) = 257,14 => End
// calculate the price
// ----------------------
nDAYS := 12
DEFINE DIALOG oDlg TITLE "Price Test"
@ 1, 2 GET oGet VAR nDAYS OF oDlg SIZE 30, 15 PICTURE "99"
@ 2, 2 BUTTON "Price-Test" size 50, 25 OF oDlg ;
ACTION ( oGet:Refresh(), ;
nPRICE := GETPRICE1( nDAYS, aPRICE ), ;
MsgAlert( nPrice ) )
ACTIVATE DIALOG oDlg CENTERED
RETURN ( NIL )
// ------------------------------------------
FUNCTION GETPRICE1( nDAYS, aPRICE )
nPRICE := 0
// Testing days of last Array-Element for valid input
// ------------------------------------------------------------
IF nDAYS = 0
MsgAlert( "No Days-value defined !","Error" )
RETURN( NIL )
ENDIF
IF nDAYS > aPRICE[LEN(aPRICE)][1]
MsgAlert( "To many Days !","Error" )
RETURN( NIL )
ENDIF
I := 1
FOR I := 1 to LEN( aPRICE ) // 6
IF I + 1 < LEN( aPRICE ) // < 6
// Price is defined for nDAYS ( no calculation )
// ---------------------------------------------------
IF nDAYS = aPRICE[I][1]
nPRICE := aPRICE[I][2]
EXIT
ENDIF
// 1 7 14 21 28 35
// 6 13 20 27 34
// 1-6, 7-13, 14-20, 21-27, 28-34, 35
IF nDAYS >= aPRICE[I][1] .and. nDAYS <= aPRICE[I+1][1]-1
// nPARTPRICE := ( 100 - 50 ) / ( 14 - 7 ) = 7,14
// aPRICE[3] := { 14, 100 }
// ----
// nPARTPRICE := ( 200 - 130 ) / ( 28 - 21 ) = 10
// aPRICE[5] := { 28, 200 }
// ---------------------------------------------------------
nPARTPRICE := ( aPRICE[I+1][2]- aPRICE[I][2] )/( aPRICE[I+1][1]- aPRICE[I][1] )
// nPRICE := 50 + ( 7,14 * ( 12 - 7 ) ) = 85,71
// aPRICE[2] := { 7, 50 }
// 12 Days = 50 + ( ( ( 100 - 50 ) / 7 ) * 5 ) = 85.71
// ------
// nPRICE := 130 + ( 10 * ( 25 - 21 ) ) = 170
// aPRICE[4] := { 21, 130 }
// 25 Days = 130 + ( ( ( 200 - 130 ) / 7 ) * 4 ) = 170
// -------------------------------------------------------------
nPRICE := aPRICE[I][2] + ( nPARTPRICE * ( nDAYS - aPRICE[I][1] ) )
// msgalert( aPRICE[I][2], "1")
// msgalert( nPARTPRICE, "2")
// msgalert( aPRICE[I-1][1], "3")
EXIT
ENDIF
ENDIF
// Special calculation needed for last Array-Element
// Last Array-Element only for End of days ( no extra price )
// -------------------------------------------------------------------
IF I = LEN( aPRICE ) // 6
IF nDAYS >= aPRICE[I-1][1] // Array-Element 5
// aPRICE[5] := { 28, 200 }
// aPRICE[6] := { 35, 200 } // End
// nPARTPRICE := 200 / ( 35 - 28 ) = 28,57
// ----------------------------------------------------------
nPARTPRICE := aPRICE[I][2] / ( aPRICE[I][1] - aPRICE[I-1][1] )
// 30 Days = 200 + ( ( 200 / ( 35 - 28 ) * 2 ) = 257,14
// -------------------------------------------------------------------
nPRICE := aPRICE[I][2] + ( nPARTPRICE * ( nDAYS - aPRICE[I-1][1] ) )
// msgalert( aPRICE[I][2], "1") // 200
// msgalert( nPARTPRICE, "2") // 28,57
// msgalert( aPRICE[I-1][1], "3") // 28
// msgalert( aPRICE[I][1], "4") // 35
EXIT
ENDIF
ENDIF
NEXT
RETURN ( nPRICE )
Regards
Uwe
