/************************************************************************/
/* TaxSolve_VA760_2005.c - 						*/
/* Copyright (C) 2005 - Aston Roberts					*/
/* 									*/
/* Compile:   gcc taxsolve_VA760_2005.c -o taxsolve_VA760_2005		*/
/* Run:	      ./taxsolve_VA760_2005  VA_760_2005.dat 			*/
/* 									*/
/* GNU Public License - GPL:						*/
/* This program is free software; you can redistribute it and/or	*/
/* modify it under the terms of the GNU General Public License as	*/
/* published by the Free Software Foundation; either version 2 of the	*/
/* License, or (at your option) any later version.			*/
/* 									*/
/* This program is distributed in the hope that it will be useful,	*/
/* but WITHOUT ANY WARRANTY; without even the implied warranty of	*/
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU	*/
/* General Public License for more details.				*/
/* 									*/
/* You should have received a copy of the GNU General Public License	*/
/* along with this program; if not, write to the Free Software		*/
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA		*/
/* 02111-1307 USA							*/
/* 									*/
/* Aston Roberts 1-2-2004	aston_roberts@yahoo.com			*/
/* Updates by:  Aston Roberts 1-18-2005					*/
/* Updates by:  Aston Roberts 3-17-2006					*/
/************************************************************************/

#include <stdio.h>
#include <time.h>

#include "taxsolve_routines.c"

float thisversion=3.0;

#define SINGLE 		        1
#define MARRIED_FILLING_JOINTLY 2
#define MARRIED_FILLING_SEPARAT 3
#define HEAD_OF_HOUSEHOLD       4
#define WIDOW		        5

float TaxRateFunction( float x, int status )
{
 if (x<3000.0) return x * 0.02; else
 if (x<5000.0) return  60.0 + (x-3000.0) * 0.03; else
 if (x<17000.0) return 120.0 + (x-5000.0) * 0.05; else
 return 720.0 + (x-17000.0) * 0.0575; 
}



/*----------------------------------------------------------------------------*/

int main( int argc, char *argv[] )
{
 int i, j, k;
 char word[1000], outfname[1000];
 int status=0, exemptions=0;
 time_t now;
 float L18b=0.0;

 /* Intercept any command-line arguments. */
 printf("VA-760 2005 - v%3.1f\n", thisversion);
 i = 1;  k=1;
 while (i < argc)
 {
  if (strcmp(argv[i],"-verbose")==0)  verbose = 1;
  else
  if (k==1)
   {
    infile = fopen(argv[i],"r");
    if (infile==0) {printf("ERROR: Parameter file '%s' could not be opened.\n", argv[i]); exit(1);}
    k = 2;
    /* Base name of output file on input file. */
    strcpy(outfname,argv[i]);
    j = strlen(outfname)-1;
    while ((j>=0) && (outfname[j]!='.')) j--;
    if (j<0) strcat(outfname,".out"); else strcpy(&(outfname[j]),".out");
    outfile = fopen(outfname,"w");
    if (outfile==0) {printf("ERROR: Output file '%s' could not be opened.\n", outfname); exit(1);}
    printf("Writing results to file:  %s\n", outfname);
   }
  else {printf("Unknown command-line parameter '%s'\n", argv[i]); exit(1);}
  i = i + 1;
 }

 if (infile==0) {printf("Error: No input file on command line.\n"); exit(1);}

 /* Pre-initialize all lines to zeros. */
 for (i=0; i<MAX_LINES; i++) 
  {
   L[i] = 0.0;
  }

 /* Accept parameters from input file. */
 /* Expect  VA-760 lines, something like:
	Title:  VA-760 1999 Return
	L14		{Wages}
	L15a		{Interest}
	L16		{Dividends}
	L18		{Capital Gains}
	S1		{Property Tax}
	L42		{Witheld tax, from W-2}
*/


 /* Accept Form's "Title" line, and put out with date-stamp for records. */
 read_line( infile, word );
 now = time(0);
 fprintf(outfile,"\n%s		%s\n", word, ctime( &now ));

 /* get_parameter(infile, kind, x, emssg ) */
 get_parameter( infile, 's', word, "Status" );
 get_parameter( infile, 'l', word, "Status ?");
 if (strncasecmp(word,"Single",4)==0) status = SINGLE; else
 if (strncasecmp(word,"Married/Joint",13)==0) status = MARRIED_FILLING_JOINTLY; else
 if (strncasecmp(word,"Married/Sep",11)==0) status = MARRIED_FILLING_SEPARAT; else
 if (strncasecmp(word,"Head_of_House",4)==0) status = HEAD_OF_HOUSEHOLD;
 else
  { 
   printf("Error: unrecognized status '%s'. Must be: Single, Married/joint, Married/sep, Head_of_house, Widow(er)\nExiting.\n", word); 
   exit(0); 
  }
 fprintf(outfile,"Status = %s (%d)\n", word, status);

 get_parameter( infile, 's', word, "Exemptions" );	/* Exemptions, self/spouse. */
 get_parameters( infile, 'i', &exemptions, "Exemptions"); 
 fprintf(outfile,"Exemptions = %d\n", exemptions );

 GetLineF( "L1", &L[1] );	/* Federal Adjusted Gross Income */

 GetLineF( "L2", &L[2] );	/* Additions from attached Schedule ADJ, line 3 */

 L[3] = L[1] + L[2];
 showline(3);

 GetLineF( "L4", &L[4] );	/* Deduction for age on Jan 1, 2006. */

 GetLineF( "L5", &L[5] );	/* Social Security Act, Tier 1 Railroad Retirement Act benef. */

 GetLineF( "L6", &L[6] );	/* State Income Tax refund or overpayment credit */

 GetLineF( "L7", &L[7] );	/* Subtractions from Schedule ADJ, line 7 */

 L[8] = L[4] + L[5] + L[6] + L[7];
 showline(8);

 L[9] = L[3] - L[8]; 
 showline(9);
 
 GetLineF( "L10", &L[10] );	/* Deductions - Std or Itemized minus income taxes */

 L[11] = 800.0 * exemptions;
 showline(11);
 
 GetLineF( "L12", &L[12] );	/* Child and Dependent Care Expenses. */

 L[13] = L[10] + L[11] + L[12];
 showline(13);

 L[14] = L[9] - L[13];
 showline(14);

 L[15] = TaxRateFunction( L[14], status );
 showline(15);

 GetLineF( "L16", &L[16] );	/* Spouse Tax Adjustment. */

 L[17] = L[15] - L[16];
 showline_wmsg( 17, "Net Amount of Tax" );	

 GetLineF( "L18a", &L[18] );	/* Virginia tax withheld for 2005. */
 GetLineF( "L18b", &L18b );	/* Spouse's Virginia tax withheld. */

 GetLineF( "L19", &L[19] );	/* Estimated tax paid for 2005. (form 760ES) */

 GetLineF( "L20", &L[20] );	/* Extension payments (form 760E). */

 GetLineF( "L21", &L[21] );	/* Tax Credit, Low Income Individuals (Sch. ADJ, line 12) */

 GetLineF( "L22", &L[22] );	/* Credit, Tax Paid to other State (Sch. ADJ, line 19 ...) */

 GetLineF( "L23", &L[23] );	/* Other Credits from attached Schedule CR */

 L[24] = L[18] + L18b + L[19] + L[20] + L[21] + L[22] + L[23];
 showline(24);

 if (L[24] < L[17])
  {
   L[25] = L[17] - L[24];
   showline_wmsg( 25, "Tax You Owe" );
  }
 else
  {
   L[26] = L[24] - L[17];
   showline_wmsg( 26, "Your Tax OverPayment" );
  }

 fclose(infile);
 fclose(outfile);
 Display_File( outfname );
 printf("\nResults written to file:  %s\n", outfname);
 return 0;
}
