/***************************************
*                                      *
*          各種ファイルの読込          *
*                                      *
*                                      *
***************************************/

#include <iostream>
#include "snaps.h"
#include "kiso.h"
#include "kisosu.h"
#include "file_open.h"
#include "econ.h"
#include "cntl.h"

void read_file()
{
	#define SHIHARAIOKURE 2
	
	double buffer[DATA_MAX];
	double previous_buffer0;
	int nendo;
	int data_number;
	int counter;
	int sotai_nendo;
	
	using namespace std;
	
	// 福祉費の設定
	for( nendo = FUKUSHI_NENDO + 1 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		Fukushi[nendo - SHONENDO] = Fukushi[FUKUSHI_NENDO - SHONENDO];
	}
	for( nendo = 2015 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		Fukushi[nendo - SHONENDO] *= ( Hiho_Kokunen[nendo - SHONENDO] / Hiho_Kokunen[2014 - SHONENDO] );
	}
	
	// 独自給付ファイルの読込
	// 初期化
	previous_buffer0 = -1000.;
	
	while( ( read_data( buffer , fp_dokuzi , &data_number ) != EOF ) && ( buffer[0] > previous_buffer0 ) )
	{
		nendo = (int)(buffer[0]);
		if( nendo >= SHONENDO && nendo <= SAISHUNENDO )
		{
			Fuka_Ninzu[nendo - SHONENDO] = buffer[1];
			Fuka_Nendomatu[nendo - SHONENDO][NEW] = buffer[2];
			Fuka_Nendomatu[nendo - SHONENDO][OLD_ROREI] = buffer[3];
			Fuka_Nendomatu[nendo - SHONENDO][OLD_TURO] = buffer[4];
			Kafu_Nendomatu[nendo - SHONENDO][NEW] = buffer[5];
			Kafu_Nendomatu[nendo - SHONENDO][OLD_NOUFU] = buffer[6];
			Kafu_Nendomatu[nendo - SHONENDO][OLD_MENJO] = buffer[7];
			Ichijikin_Nendomatu[nendo - SHONENDO][NOUFU] = buffer[8];
			Ichijikin_Nendomatu[nendo - SHONENDO][FUKA] = buffer[9];
		}
		previous_buffer0 = buffer[0];
	}
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		Fuka_Nendomatu[nendo - SHONENDO][SUM]
		 = Fuka_Nendomatu[nendo - SHONENDO][NEW]
		 + Fuka_Nendomatu[nendo - SHONENDO][OLD_ROREI]
		 + Fuka_Nendomatu[nendo - SHONENDO][OLD_TURO];
		
		Kafu_Nendomatu[nendo - SHONENDO][SUM]
		 = Kafu_Nendomatu[nendo - SHONENDO][NEW]
		 + Kafu_Nendomatu[nendo - SHONENDO][OLD_NOUFU]
		 + Kafu_Nendomatu[nendo - SHONENDO][OLD_MENJO];
		
		Ichijikin_Nendomatu[nendo - SHONENDO][SUM]
		 = Ichijikin_Nendomatu[nendo - SHONENDO][NOUFU]
		 + Ichijikin_Nendomatu[nendo - SHONENDO][FUKA];
	}
	fclose(fp_dokuzi);
	
	
	// 住宅融資債権（ＲＦＯ納付金を含む）の読込
	// 初期化
	previous_buffer0 = -1000.;
	
	while( ( read_data( buffer , fp_yuushi , &data_number ) != EOF ) && ( buffer[0] > previous_buffer0 ) )
	{
		nendo = (int)(buffer[0]);
		if( nendo >= SHONENDO && nendo <= SAISHUNENDO )
		{
			Yuushi_Saiken[nendo - SHONENDO] = buffer[1] * 1000000.;
		}
		previous_buffer0 = buffer[0];
	}
	fclose( fp_yuushi );
	
	
	// 保険料ファイルの読込
	// 初期化
	previous_buffer0 = -1000.;
	
	while( ( read_data( buffer , fp_hoken , &data_number ) != EOF ) && ( buffer[0] > previous_buffer0 ) )
	{
		nendo = (int)(buffer[0]);
		if( nendo >= SHONENDO )
		{
			Hokenryou_m[nendo - SHONENDO][0] = buffer[1];
			Hokenryou_m[nendo - SHONENDO][1] = buffer[2];
			Fuka_Hokenryou_m[nendo - SHONENDO] = buffer[3];
		}
		previous_buffer0 = buffer[0];
		
		if( Hokenryou_m[nendo - SHONENDO - 1][0] < Hokenryou_m[nendo - SHONENDO][0] )
		{
			h_nendo = nendo;
		}
	}
	fclose( fp_hoken );
	
	// 年度間値を算出
	for( nendo = SHONENDO + 1 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		
		Ichijikin[sotai_nendo][NOUFU]
		 = Ichijikin_Nendomatu[sotai_nendo - 1][NOUFU] * ( SHIHARAIOKURE + 6 ) / 12.
		 + Ichijikin_Nendomatu[sotai_nendo][NOUFU] * ( 6 - SHIHARAIOKURE ) / 12.;
		
		Ichijikin[sotai_nendo][FUKA]
		 = Ichijikin_Nendomatu[sotai_nendo - 1][FUKA] * ( SHIHARAIOKURE + 6 ) / 12.
		 + Ichijikin_Nendomatu[sotai_nendo][FUKA] * ( 6 - SHIHARAIOKURE ) / 12.;
		
		Ichijikin[sotai_nendo][SUM] = Ichijikin[sotai_nendo][NOUFU] + Ichijikin[sotai_nendo][FUKA];
		
		for( counter = SUM ; counter <= OLD_MENJO ; counter++ )
		{
			if( nendo == 2013 )
			{
				Kafu[sotai_nendo][counter]
				 = Kafu_Nendomatu[sotai_nendo - 1][counter]
				 * ( SHIHARAIOKURE + ( kaiteiritu[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] + 1.0 / 100 ) * 6 ) / 12.
				 + Kafu_Nendomatu[sotai_nendo][counter] * ( 6 - SHIHARAIOKURE ) / 12.;
			}
			else
			{
				Kafu[sotai_nendo][counter]
				 = Kafu_Nendomatu[sotai_nendo - 1][counter]
				 * ( SHIHARAIOKURE + kaiteiritu[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] * 6 ) / 12.
				 + Kafu_Nendomatu[sotai_nendo][counter] * ( 6 - SHIHARAIOKURE ) / 12.;
			}
		}
		for( counter = SUM ; counter <= OLD_TURO ; counter++ )
		{
			Fuka[sotai_nendo][counter]
			 = Fuka_Nendomatu[sotai_nendo - 1][counter] * ( SHIHARAIOKURE + 6 ) / 12.
			 + Fuka_Nendomatu[sotai_nendo][counter] * ( 6 - SHIHARAIOKURE ) / 12.;
		}
	}
}
