/* DEC	**DepreciateSumOfYearsTable(depr,remval, book, salv, life, partial);
 *
 * ARGUMENT
 *	DEC	**depr,	**remval, *book, *salv,	*partial;
 *	int	life;
 *
 * DESCRIPTION
 *	Calculate several years' depreciation and remaining depreciable
 *   value according to	sum-of-the-years-digits	depreciation.
 *   The item has book value
 *   book, salvage value salv, and lifetime life (which	may include a partial
 *   period).  At year#	i, the depreciation and	remaining value	are
 *   computed and stored in depr[i] and	remval[i].  The	first year is always
 *   the partial year.	The results are	calculated for the entire lifetime.
 *
 * SIDE	EFFECTS
 *	depr and remval	are changed.
 *
 * RETURNS
 *	depr if	successful, otherwise GM_NULLARR.
 *
 * POSSIBLE ERRORS
 *	GM_NULLPOINTER
 *	GM_ARGVAL
 *
 * AUTHOR
 *  Jared Levy
 *   Copyright (C) 1988-1990 Greenleaf Software	Inc.  All rights reserved.
 *
 * MODIFICATIONS
 *
 *
 */

#include <stdio.h>
#include "gm.h"
#include "gmsystem.h"

DEC	**DepreciateSumOfYearsTable(depr, remval, book,	salv, life, partial)
DEC	**depr,	**remval, *book, *salv,	*partial;
int	life;
{
	int	i, plife;
	DEC	drbv0, *rbv0=&drbv0, dladj, *ladj=&dladj;
	DEC	ddlife,	*dlife=&ddlife,	dsoyd, *soyd=&dsoyd;
	DEC	dtemp, *temp=&dtemp, dtemp2, *temp2=&dtemp2;

	_MacStart(GM_DPSOYTBL);

	if (!depr||!remval||!book||!salv||!partial)  {
		_MacErr(GM_NULLPOINTER);
		_MacRet(GM_NULLARR);
	}
	if (_MacBad(book)||_MacBad(salv)||_MacBad(partial))  {
		_MacErr(GM_INIT);
		_MacRet(GM_NULLARR);
	}

	plife=life+(_MacIsDecZ(partial)?0:1);

	for (i=0;i<=plife;i++)
		if(!depr[i]||!remval[i])  {
			_MacErr(GM_NULLPOINTER);
			_MacRet(GM_NULLARR);
		}

/* check life>=1, 0<=partial<=1	*/
	if (life<1||_MacIsDecN(partial)||CompareDecimal(partial,&decOne)==1
		||_MacIsDecN(book)||_MacIsDecN(salv))  {
		_MacErr(GM_ARGVAL);
		_MacRet(GM_NULLARR);
	}

/* calculate initial remaining book value & store zeroes indices */
	_MacDZero(depr[0]);
	(void) _SubDec80Bit(rbv0,book,salv);
	(void) _ScaleDec80Bit(rbv0, rbv0, 2);
	_MacDCopy(remval[0], rbv0);

/* calculate sum of years digits soyd */
	(void) ConvLongToDecimal(soyd,
		(long) (life) *	(long) (life+1)	/ 2L);

	if (_MacIsDecZ(partial))  {
		(void) ConvLongToDecimal(ladj, (long) life);
		i=0;
	}
	else  {
		(void) ConvLongToDecimal(dlife,	(long) life);
/* calculate first partial year's depreciation */
		(void) _MulDec80Bit(temp, dlife, partial);
		(void) _MulDec80Bit(temp, temp,	rbv0);
		(void) _DivRndDec80Bit(depr[1],	temp, soyd, 2);
/* subtract first depreciation from rbv	*/
		(void) _SubDec80Bit(rbv0, rbv0,	depr[1]);
		_MacDCopy(remval[1],rbv0);
		i=1;

/* calculate sum of years digits soyd */
		(void) _SubDec80Bit(ladj, dlife, partial);
		(void) _TruncateDec80Bit(temp, ladj, 0);
		(void) _SubDec80Bit(temp2, ladj, temp);
		(void) _AddDec80Bit(temp2, temp2, temp2);
		(void) _AddDec80Bit(temp2, temp2, temp);
		(void) _AddDec80Bit(temp, temp,	&decOne);
		(void) _MulDec80Bit(temp, temp,	temp2);
		(void) _MulDec80Bit(soyd, temp,	&decPoint5);

	}

/* handle remaining years */
	(void) _DivDec80Bit(temp, rbv0,	soyd);
	while (i<plife)	 {
		i++;
		_MulDec80Bit(depr[i], ladj, temp);
		_ScaleDec80Bit(depr[i],	depr[i], 2);
		_SubDec80Bit(ladj, ladj, &decOne);
		(void) _SubDec80Bit(remval[i], remval[i-1], depr[i]);
	}

/* add balance to last depreciation period */
	_MacDCopy(depr[plife],remval[plife-1]);
	_MacDZero(remval[plife]);

	_MacRet(depr);
}