with your help, Dale (DrD), khapster, Eddie, I'm debugging this menu based on Drawmenu and menu handler routine.
Please help me to debug.
i.e. when user press "Quit" soft key the program return to previous Print screen: any advise?
Code:
// Initialize procedures used in project
EVENTHANDLER();
PUTMENU();
GETMENU();
Leasing_advpmt();
Leasing_yield();
Leasing_residual();
Leasing_res_yield();
// Initialize variables used globally in project
mSEL;
eTYP;
kP;
m;
m1;
EXPORT Leasing()
BEGIN
// Initialize local variables used within this procedure
LOCAL mx,my,mTXT,EXITPGM;
// Set the menu text (this is softcoded and determines if a menu item is valid)
mTXT := {"AdvPmt","Yield","Resid","ResYld","","Quit"};
// MAIN CODE - Loop until ESC is pressed, then exit the program
EXITPGM := 0;
WHILE EXITPGM == 0 DO
// Clear the screen and draw the menu
RECT_P();
PUTMENU(mTXT);
// Flush the mouse buffer
WHILE MOUSE(1) ≥ 0 DO END;
// Loop until we have a keyboard or mouse event
REPEAT
EVENTHANDLER();
UNTIL eTYP <> "";
CASE
// If the event type was a keyboard action then process it
IF eTYP == "K" THEN
// If the ESC key was pressed set the program to end
IF kP == 4 THEN
EXITPGM := 1;
ELSE
// ESC was not pressed do what you want with the keypress, we are just going to do a TEXTOUT
END;
END;
// If the event type was mouse action then process it
IF eTYP == "M" THEN
// Convert mouse coordinates to decimal
mx := B→R(m1(1));
my := B→R(m1(2));
// Determine if mouse coordinates are inside of a valid menu item otherwise return a zero
GETMENU(mx,my,mTXT);
// If a valid menu item was selected, do something, we are just going to do a TEXTOUT
IF mSEL > 0 THEN
CASE
// If menu item 1 was selected display a choose box for the user to select from
IF mSEL == 1 THEN
Leasing_advpmt();
END;
IF mSEL == 2 THEN
Leasing_yield();
END;
IF mSEL == 3 THEN
Leasing_residual();
END;
IF mSEL == 4 THEN
Leasing_res_yield();
END;
IF mSEL == 6 THEN
EXITPGM := 1;
END;
DEFAULT
// This is the default action for the menu selection and will only run if no case statement is satisfied
END;
ELSE
// Mouse not in menu, do something if needed
END;
END;
END;
END;
END;
// -------------------------------------------------------------------
// Detect keyboard or mouse input (keyboard has priority)
// -------------------------------------------------------------------
EVENTHANDLER()
BEGIN
eTYP := "";
kP := GETKEY;
IF kP <> -1 THEN
eTYP := "K";
ELSE
m := MOUSE;
m1 := m(1);
IF SIZE(m1) > 0 THEN
eTYP := "M";
END;
END;
END;
// ----------------------------------------------
// Draw the menu using the list passed in
// ----------------------------------------------
PUTMENU(mTXT)
BEGIN
DRAWMENU(mTXT(1),mTXT(2),mTXT(3),mTXT(4),mTXT(5),mTXT(6));
END;
// ------------------------------------------------------------------------------------------
// Get the number of the menu item selected (1-6) by checking mouse position
// Menu items with empty/blank text will return a zero
// ------------------------------------------------------------------------------------------
GETMENU(mx,my,mTXT)
BEGIN
mSEL := 0;
IF my≥220 AND my≤239 THEN
CASE
IF mx≥0 AND mx≤51 AND mTXT(1)>"" THEN
mSEL := 1;
END;
IF mx≥53 AND mx≤104 AND mTXT(2)>"" THEN
mSEL := 2;
END;
IF mx≥106 AND mx≤157 AND mTXT(3)>"" THEN
mSEL := 3;
END;
IF mx≥159 AND mx≤210AND mTXT(4)>"" THEN
mSEL := 4;
END;
IF mx≥212 AND mx≤263 AND mTXT(5)>"" THEN
mSEL := 5;
END;
IF mx≥265 AND mx≤319 AND mTXT(6)>"" THEN
mSEL := 6;
END;
END;
END;
END;
// ------------------------------------------------------------------------------------------
// LEASING APPLICATION CODE
// ------------------------------------------------------------------------------------------
Leasing_advpmt()
// solve for monthly payment
// by Salvo Micciché 2015 - Credits: kharpster
BEGIN
LOCAL r, n, fpmt, val;
INPUT ({r, n, fpmt, val},"Leasing: advance payments", {"yield%", "n lease", "n pmt advance", "value"},
{"Yield (rate)%", "Duration of lease", "Num of advance payments", "Value of Leasing"}, {0,0, 0, 0},{0,12, 3,0});
LOCAL npmt, pv;
npmt:= n - fpmt;
pv:= Finance.CalcPV(npmt, r, -1, 0, 12, 12, 1);
PRINT;
PRINT(
"Leasing with advanced payments
Value of lease: " + EVAL(val) + "
Duration of lease: " + EVAL(n) + "
Advanced payments n.: " + EVAL(fpmt) + "
Periodic payments n.: " + EVAL(npmt) + "
Yield (rate): " + EVAL(r) + "%
Monthly payment: " + EVAL(val/(pv+fpmt))
);
WAIT;
RETURN val/(pv+fpmt);
END;
Leasing_yield()
// solve for yield
BEGIN
LOCAL n, fpmt, val, pmt, yield, npmt, pv;
INPUT ({n, fpmt, val, pmt},"Leasing: calc yield", {"n lease", "n pmt advance", "value", "payment"},
{"Duration of lease", "Num of advance payments", "Value of Leasing", "Monthly payment"}, {0,0, 0, 0},{12,3, 0, 0});
npmt:= n - fpmt;
pv:= fpmt*pmt-val;
yield:= Finance.CalcIPYR(npmt, pv, pmt, 0, 12, 12, 1);
PRINT;
PRINT ("Leasing: solving for Yield
Value of lease (loan): " + EVAL(val) + "
Duration of lease: " + EVAL(n) + "
Advanced payments n.: " + EVAL(fpmt) + "
Periodic payments n.: " + EVAL(npmt) + "
Monthly Payment: " + EVAL(pmt) + "
Monthly yield: " + EVAL(yield/12) + "%
Yearly yield: " + EVAL(yield) + "%"
);
WAIT;
RETURN yield;
END;
Leasing_residual()
// Leasing with residual value, solve for monthly payment
BEGIN
LOCAL n, r, val, resval, fpmt, att, pv;
LOCAL npmt, pv2;
INPUT ({n, fpmt, r, val, resval},"Leasing with residual", {"n lease", "n adv pmt", "yield%", "value", "res value"},
{"Duration of lease", "Num advanced payments", "Yield (rate)%", "Value of Leasing (loan)", "Residual value"},
{0,0, 0, 0, 0},{12, 3, 0, 0, 0});
pv:= Finance.CalcPV(n, r,0, resval, 12, 12, 1);
att:= pv + val;
npmt:= n - fpmt;
pv2:= Finance.CalcPV(npmt, r, -1, 0, 12, 12, 1);
pv2:= pv2 + fpmt;
PRINT;
PRINT ("Leasing with residual
Value of lease (loan): " + EVAL(val) + "
Residual value: " + EVAL(resval) + "
Duration of lease: " + EVAL(n) + "
Advanced payments n.: " + EVAL(fpmt) + "
Periodic payments n.: " + EVAL(npmt) + "
Yield (rate): " + EVAL(r) + "%
Monthly payment: " + EVAL(att/pv2)
);
WAIT;
RETURN att/pv2;
END;
Leasing_res_yield()
// Leasing with residual value, solve for yield (rate)
BEGIN
LOCAL n, fpmt, val, pmt, resval, j, irr;
LOCAL flows:={}, yield, npmt, netamt;
INPUT ({n, fpmt, val, resval, pmt},"Leasing: calc yield", {"n lease", "n pmt advance", "value", "res val", "payment"},
{"Duration of lease", "Num of advance payments", "Value of Leasing", "REsidual value", "Monthly payment"},
{0,0, 0, 0, 0},{36,2, 0, 0, 0});
npmt:= n - fpmt;
netamt:= -val+pmt*fpmt; // net amount (-value + payment*advanced num pmt)
flows:= append(flows, netamt); // create list, append net amount
FOR j FROM 1 TO npmt DO
flows:= append (flows, pmt); // append payment, (num pmt - advanced pmt) times
END; // for
FOR j FROM 1 TO (fpmt-1) DO
flows:= append (flows, 0); // append 0, (mum advanced pmt minus 1)
END; // for
flows:= append(flows, resval); // append residual value
L1:= flows;
irr:= Solve.SOLVE(ΣLIST(MAKELIST((L1(I)/X^(I-1)),I,1,SIZE(L1))),X)-1;
// Solve.SOLVE() could not always work, but give only a secure value, solve() works better, butr gives more values
PRINT;
PRINT ("Leasing with advanced payments and residual value: yield
Solved for IRR
Value of lease (loan): " + EVAL(val) + "
Residual value: " + EVAL(resval) + "
Duration of lease: " + EVAL(n) + "
Advanced payments n.: " + EVAL(fpmt) + "
Periodic payments n.: " + EVAL(npmt) + "
Monthly Payment: " + EVAL(pmt) + "
Net amount of cash advanced: " + EVAL(netamt) + "
Monthly yield: " + EVAL(irr*100) + "%
Yearly yield: " + EVAL(12*irr*100) + "%"
);
WAIT;
RETURN 12*irr; // yearly yield (IRR) in decimal
END;