
Code example - finance.c
/*
** FINANCE
**
** HTTP Wrapper for the Financial Tools
**
** Confidential Property of Tod Sambar
** (c) Copyright Tod Sambar 1996-1997
** All rights reserved.
**
**
** Public Functions:
**
** finance_init
**
**
** History:
** Chg# Date Description Resp
** ---- ------- ------------------------------------------------------- ----
** 6SEP96 Created sambar
*/
#include
#include
#include
#include
#include
#include
/*
** FINANCE_INIT
**
** Initialize the Financial Tools calculators.
**
** Parameters:
** sactx Sambar Server context
**
** Returns:
** SA_SUCCEED | SA_FAIL
*/
SA_RETCODE SA_PUBLIC
finance_init(sactx)
SA_CTX *sactx;
{
if (sa_cmd_init(sactx, "amorcalc", SA_AUTHORIZATION_ALL,
"Amortization schedule calculation.",
(SA_RPCFUNC)finance_amorcalc) != SA_SUCCEED)
{
sa_log(sactx, "Unable to initialize Finance RPCs");
return (SA_FAIL);
}
if (sa_cmd_init(sactx, "mortcalc", SA_AUTHORIZATION_ALL,
"Mortgage calculation.",
(SA_RPCFUNC)finance_mortcalc) != SA_SUCCEED)
{
sa_log(sactx, "Unable to initialize Finance RPCs");
return (SA_FAIL);
}
sa_log(sactx, "Finance Library Initialized");
return (SA_SUCCEED);
}
/*
** FINANCE_AMORCALC
**
** Amortization schedule calculator.
**
** Parameters:
** sactx Sambar Server context
** saconn Sambar Server connection
** saparams RPC Parameters
** infop Error parameters
**
** Returns:
** SA_SUCCEED | SA_FAIL
*/
SA_RETCODE SA_PUBLIC
finance_amorcalc(sactx, saconn, saparams, infop)
SA_CTX *sactx;
SA_CONN *saconn;
SA_PARAMS *saparams;
SA_INT *infop;
{
SA_INT i;
SA_INT months;
SA_INT start;
SA_INT year;
SA_INT datalen;
double price;
double rate;
double payment;
double tmp1;
double tmp2;
double at;
double ay;
SA_CHAR *data;
SA_CHAR buffer[256];
/* Get the principle amount */
if ((sa_param(sactx, saparams, "price", &data, &datalen) != SA_SUCCEED) ||
(datalen == 0))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
price = atof(data);
/* Get the number of months in the loan */
if ((sa_param(sactx, saparams, "months", &data, &datalen) != SA_SUCCEED)
|| (datalen == 0))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
months = atoi(data);
/* Get the interest rate */
if ((sa_param(sactx, saparams, "rate", &data, &datalen) != SA_SUCCEED)
|| (datalen == 0))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
rate = atof(data);
/* Get the starting month */
if ((sa_param(sactx, saparams, "month", &data, &datalen) != SA_SUCCEED) ||
(datalen == 0))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
start = atoi(data);
/* Get the starting year */
if ((sa_param(sactx, saparams, "year", &data, &datalen) != SA_SUCCEED)
|| (datalen == 0))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
year = atoi(data);
if ((price <= 0) || (months <= 0) || (rate <= 0) || (start <= 0) ||
(year <= 0))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
if (sa_send_macro(saconn, "FINANCE_AMORCALC") != SA_SUCCEED)
return (SA_FAIL);
start--;
year--;
tmp1 = rate / 1200.0;
tmp2 = 1.0 - (1.0 / pow((float)(tmp1+1), (float)months));
payment = price * tmp1 / tmp2;
at = ay = 0.0;
for (i = 0; i < months; i++)
{
if (start >= 12)
{
year++;
at += ay;
sprintf(buffer,
"Total Interest for paid during %d: $%11.2f",
year, ay);
if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
ay = 0.0;
start = 0;
}
ay = ay + (price * tmp1);
price = price - (payment - (price * tmp1));
if (price > 0.0)
start++;
else
break;
}
if (start > 0)
{
at += ay;
sprintf(buffer,
"Total Interest paid during %d: $%11.2f",
year, ay);
if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
}
sprintf(buffer,
"Total Interest paid during loan: $%11.2f",
at);
if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
if (sa_send_macro(saconn, "FINANCE_FOOTER") != SA_SUCCEED)
return (SA_FAIL);
return (SA_SUCCEED);
}
/*
** FINANCE_MORTCALC
**
** Mortgage calculator.
**
** Parameters:
** sactx Sambar Server context
** saconn Sambar Server connection
** saparams RPC Parameters
** infop Error parameters
**
** Returns:
** SA_SUCCEED | SA_FAIL
*/
SA_RETCODE SA_PUBLIC
finance_mortcalc(sactx, saconn, saparams, infop)
SA_CTX *sactx;
SA_CONN *saconn;
SA_PARAMS *saparams;
SA_INT *infop;
{
SA_INT years;
SA_INT datalen;
double price;
double down;
double rate;
double tmp1;
double mort;
SA_CHAR *data;
SA_CHAR buffer[256];
/* Get the sale price of the house */
if ((sa_param(sactx, saparams, "price", &data, &datalen) != SA_SUCCEED)
|| (datalen == 0))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
price = atof(data);
/* Get the down payment amount */
if ((sa_param(sactx, saparams, "down", &data, &datalen) != SA_SUCCEED)
|| (datalen == 0))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
down = atof(data);
/* Get the number of years to pay off the mortgage */
if ((sa_param(sactx, saparams, "years", &data, &datalen) != SA_SUCCEED)
|| (datalen == 0))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
years = atoi(data);
/* Get the interest rate */
if ((sa_param(sactx, saparams, "rate", &data, &datalen) != SA_SUCCEED)
|| (datalen == 0))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
rate = atof(data);
if ((price <= 0) || (down <= 0) || (years <= 0) || (rate <= 0) ||
(down > price))
{
*infop = SA_E_INVALIDDATA;
return (SA_FAIL);
}
tmp1 = rate / (12 * 100);
mort = (price - down) * (tmp1 / (1 - pow(1 + tmp1, - (years * 12))));
if (sa_send_macro(saconn, "FINANCE_MORTCALC") != SA_SUCCEED)
return (SA_FAIL);
sprintf(buffer, "$%11.2f\n", mort);
if (sa_conn_send(saconn, buffer, SA_NULLTERM) != SA_SUCCEED)
return (SA_FAIL);
if (sa_send_macro(saconn, "FINANCE_FOOTER") != SA_SUCCEED)
return (SA_FAIL);
return (SA_SUCCEED);
}