/************************************************************************/
/* TaxSolve_OH_IT1040_2005.c - 						*/
/* Copyright (C) 2005 - Aston Roberts					*/
/* 									*/
/* Compile:   gcc taxsolve_OH_IT1040_2005.c -o taxsolve_OH_IT1040_2005	*/
/* Run:	      ./taxsolve_OH_IT1040_2005  OH_IT1040_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-2005					*/
/************************************************************************/

#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       1

float TaxRateFunction( float x, int status )
{
 if (x<5000.0) return x * 0.00743; else
 if (x<10000.0) return  37.15 + (x-5000.0) * 0.01486; else
 if (x<15000.0) return 111.45 + (x-10000.0) * 0.02972; else
 if (x<20000.0) return 260.05 + (x-15000.0) * 0.03715; else
 if (x<40000.0) return 445.80 + (x-20000.0) * 0.04457; else
 if (x<80000.0) return 1337.20 + (x-40000.0) * 0.05201; else
 if (x<100000.0) return 3417.60 + (x-80000.0) * 0.05943; else
 if (x<200000.0) return 4606.20 + (x-100000.0) * 0.069; else
 return 11506.20 + (x-200000.0) * 0.075; 
}



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

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

 /* Intercept any command-line arguments. */
 printf("OH IT1040 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  OH IT1040 lines, something like:
	Title:  OH IT1040 1999 Return
	L1		{Wages}
*/


 /* 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.\nExiting.\n", word); 
   exit(0); 
  }
 fprintf(outfile,"Status = %s (%d)\n", word, status);

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

 get_parameter( infile, 's', word, "Exemptions" );	/* Exemptions, self/depend. */
 get_parameters( infile, 'i', &exemptions, "Exemptions"); 

 GetLine( "L11\%", &L[11] );	/* Joint Filing Credit Percentage (%) */
 L[11] = 0.01 * L[11];
 GetLine( "L13", &L[13] );	/* Resident/Nonres/Part-Yr Credits (Sch. C/D) & Nonrefund. Bus. Credits (Sch. E) */
 GetLine( "L15", &L[15] );	/* Manufacturing equipment grant. */
 GetLine( "L17", &L[17] );	/* Interest Penalty , Underpaid Estimated Tax */
 GetLine( "L18", &L[18] );	/* Unpaid Ohio Use Tax */
 GetLine( "L20", &L[20] );	/* Ohio Tax Withheld (box 17 on your W-2) */
 GetLine( "L21", &L[21] );	/* Ohio Estimated Tax, IT 40P Payments for 2005, ... */
 GetLine( "L22", &L[22] );	/* Refundable Business Jobs + Refundable Pass-through Entity */

 /* Additions - add to extent not included in fed adjusted gross income (line 1). */
 GetLine( "L31", &L[31] );	/* Non-Ohio state or local gov't interest, dividends. */
 GetLine( "L32", &L[32] );	/* Pass-through Entity addback. */
 GetLine( "L33", &L[33] );	/* Income from an electing small business trust. */

 /* Other. Check all that apply: */
 get_parameter( infile, 's', word, "34a" );	/* Fed int+div subject to state tax & misc. fed adj. */
 get_parameters( infile, 'l', word, "34a");
 if (strncasecmp(word,"yes",1)==0) L[34] = L[34] + 1.0;

 get_parameter( infile, 's', word, "34b" );	/* Reimbursed college tuit. fees deducted prev yrs. */
 get_parameters( infile, 'l', word, "34b");
 if (strncasecmp(word,"yes",1)==0) L[34] = L[34] + 1.0;

 get_parameter( infile, 's', word, "34c" );	/* Losses, sale of Ohio Public Obligations. */
 get_parameters( infile, 'l', word, "34c");
 if (strncasecmp(word,"yes",1)==0) L[34] = L[34] + 1.0;

 get_parameter( infile, 's', word, "34d" );	/* Nonmedical withdrawals OH Med. Savings Acct. */
 get_parameters( infile, 'l', word, "34d");
 if (strncasecmp(word,"yes",1)==0) L[34] = L[34] + 1.0;

 get_parameter( infile, 's', word, "34e" );	/* Reimb. exp. prev. deducted, if reimb. not in FAGI .*/
 get_parameters( infile, 'l', word, "34e");
 if (strncasecmp(word,"yes",1)==0) L[34] = L[34] + 1.0;

 get_parameter( infile, 's', word, "34f" );	/* Non-edu expenses from College Savings Acct. */
 get_parameters( infile, 'l', word, "34f");
 if (strncasecmp(word,"yes",1)==0) L[34] = L[34] + 1.0;

 get_parameter( infile, 's', word, "34g" );	/* Depreciation adjust, IRC 168(k) + 179. */
 get_parameters( infile, 'l', word, "34g");
 if (strncasecmp(word,"yes",1)==0) L[34] = L[34] + 1.0;

 /* Deductions - see limitations in instructions. */
 GetLine( "L36", &L[36] );	/* Ded. fed int + div exempt from state taxation. */
 GetLine( "L37", &L[37] );	/* Ded. comp. earned in OH by non-residents. */
 GetLine( "L38", &L[38] );	/* Ded. state/municipal income tax overpayments. */
 GetLine( "L39", &L[39] );	/* Ded. disab. & survivor benef. */
 GetLine( "L40", &L[40] );	/* Ded. qualifying soc. sec. + railroad benefits. */ 
 GetLine( "L41", &L[41] );	/* Ded. contrib CollegeAdvantage 529 plan. */ 
 GetLine( "L42", &L[42] );	/* Ded. qualified tuition exp. */
 GetLine( "L43", &L[43] );	/* Ded. Un-subsidized health insurance ... */
 GetLine( "L44", &L[44] );	/* Ded. deposits into Medical Savings Acct ... */
 GetLine( "L45", &L[45] );	/* Ded. losses, electing small business trust. */
 
 /* Other. Check all that apply: */
 get_parameter( infile, 's', word, "46a" );	/* Wage & salary expense not deducted */
 get_parameters( infile, 'l', word, "46a");
 if (strncasecmp(word,"yes",1)==0) L[46] = L[46] + 1.0;
 get_parameter( infile, 's', word, "46b" );	/* Interest income from Ohio Public Obligations */
 get_parameters( infile, 'l', word, "46b");
 if (strncasecmp(word,"yes",1)==0) L[46] = L[46] + 1.0;
 get_parameter( infile, 's', word, "46c" );	/* Refund or reimbursements shown on federal Form 1040, line 21, */
 get_parameters( infile, 'l', word, "46c");
 if (strncasecmp(word,"yes",1)==0) L[46] = L[46] + 1.0;
 get_parameter( infile, 's', word, "46d" );	/* Repayment of income reported in a prior year */
 get_parameters( infile, 'l', word, "46d");
 if (strncasecmp(word,"yes",1)==0) L[46] = L[46] + 1.0;
 get_parameter( infile, 's', word, "46e" );	/* Amount contributed to an Individual Development Account */
 get_parameters( infile, 'l', word, "46e");
 if (strncasecmp(word,"yes",1)==0) L[46] = L[46] + 1.0;
 get_parameter( infile, 's', word, "46f" );	/* Depreciation expense adjustment for IRC sections 168(k) and 179 */
 get_parameters( infile, 'l', word, "46f");
 if (strncasecmp(word,"yes",1)==0) L[46] = L[46] + 1.0;

 GetLine( "L49", &L[49] );	/* Retirement Income Credit (see instructions for credit table) (Limit $200) */
 GetLine( "L50", &L[50] );	/* Senior Citizen Credit (Limit $50 per return) */
 GetLine( "L51", &L[51] );	/* Lump Sum Distribution Credit (you must be 65 years of age or older to claim this credit) */
 GetLine( "L52", &L[52] );	/* Child and Dependent Care Credit (see instructions and worksheet) */
 GetLine( "L53", &L[53] );	/* Lump Sum Retirement Credit */
 GetLine( "L55", &L[55] );	/* Job Training Credit (see instructions & worksheet) (Limit $500 single; $1,000 joint, if both spouses qualify) */
 GetLine( "L56", &L[56] );	/* Ohio Political Contributions Credit (Limit $50 single; $100 joint) */
 GetLine( "L57", &L[57] );	/* Ohio Adoption Credit (Limit $500 per adoption) */

 GetLine( "L59", &L[59] );	/* Portion of line 3 subjected to tax by other states. */
 GetLine( "L62", &L[58] );	/* 2005 income tax paid to other states ... */

#if (0)		/* For non-residents only. */
 GetLineF( "L64", &L[64] );	/* Portion Ohio Adj. Gross Income not earned in Oh. */
 L[65] = L[3];
 showline(65);
 L[66] = (L[64] / L[65]) * L[12];
 L[13] = L[66];
 showline(66);
#endif



 /* Do Calculations. */

 for (j=36; j<=46; j++)
  L[47] = L[47] + L[j];

 L[35] = L[31] + L[32] + L[33] + L[34];

 L[48] = L[35] - L[47];
 L[2] = L[48];

 L[3] = L[1] - L[2];
 L[4] = 1350.0 * exemptions;
 L[5] = L[3] - L[4];
 L[6] = TaxRateFunction( L[5], status );

 for (j=49; j<=57; j++)
  L[58] = L[58] + L[j];
 L[7] = L[58];

 L[8] = NotLessThanZero( L[6] - L[7] );
 L[9] = 20.0 * exemptions;
 L[10] = NotLessThanZero( L[8] - L[9] );
 L[11] = L[11] * L[10];
 if (L[11] > 650.0) L[11] = 650.0;
 L[12] = NotLessThanZero( L[10] - L[11] );

 L[60] = L[3];
 L[61] = (L[59] / L[60]) * L[12];
 L[63] = smallerof( L[61], L[62] );
 L[13] = L[63];

 L[14] = NotLessThanZero( L[12] - L[13] );
 L[16] = NotLessThanZero( L[14] - L[15] );
 L[19] = L[16] + L[17] + L[18];
 L[23] = L[20] + L[21] + L[22];




 /* Output the Results. */

 showline(1);
 showline(2);
 showline(3);
 fprintf(outfile,"Exemptions = %d\n", exemptions );
 for (j=4; j<=18; j++)
  showline(j);
 showline_wmsg(19, "Total Ohio Tax");
 showline(20);
 showline(21);
 showline(22);
 showline_wmsg(23, "Total Payments");
 if (L[23] < L[19])
  {
   L[24] = L[19] - L[23];
   showline_wmsg(24, "AMOUNT YOU OWE");
  }
 else
 if (L[23] > L[19])
  {
   L[25] = L[23] - L[19];
   showline_wmsg(25, "AMOUNT YOU OVERPAID");
  }
 showline(31);
 showline(32);
 showline(33);
 showline(34);
 showline_wmsg(35,"Total additions");
 for (j=36; j<=46; j++)
  showline(j);
 showline_wmsg(47,"Total Deductions");
 showline(48);
 for (j=49; j<=57; j++)
  showline(j);
 showline_wmsg(58,"Total Credits");
 for (j=59; j<=63; j++)
  showline(j);

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