#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "set.h"
#include "glva.h"

void shus_init(void);    
void init_premium(void);
void shus_premium(void); 
void shus_fukkjn(void);  

void shus_excel(void); 


void shus_econ_out(void); 
void shus_nin_out(void);  
void shus_shushiout(int); 
void shus_cutout(void);  

void shus_summary(void); 
void shus_Tokutyoout(void);  
void shus_testout(void); 

void shus_fullout(int);  

void shus_calc0(void);	
int  shus_calc8(void);
void shus_calc9(void);

void shus_smodel(void); 

void shus(void){
	shus_init();
	init_premium();
	shus_smodel();
	if(0) shus_excel();
	
	shus_premium();
	
	shus_fukkjn();
	
	shus_econ_out();
	shus_nin_out();  
	
	shus_calc0();       
	shus_shushiout(0);  
	shus_fullout(0);    
	
	if(Fpset==8) shus_calc8();
	
	if(Fpset==9) shus_calc9();
	
	shus_shushiout(1);     
	shus_fullout(1); 
	
	if(Fpset!=9) shus_cutout();
	
	if(Flg_Kmakuro==1 | Flg_Kmakuro==3) shus_Tokutyoout();
	
	shus_testout();
	
	/*	shus_smodel(); */
	if(Cntlset==1 && (Saimu==0 || Saimu==1 || Saimu==2)) shus_summary();
}



void shus_init(void){
	int jj, k, x, dx;
	int ii, is, ie, usd;
	
	if(Seidn!=0){
		is=ie=0;
	} else {
		is=1;
		ie=4;
	}
	for(ii=is; ii<=ie; ii++){
		if(Seidn!=8){
			usd=ii2usd(ii);
		} else {
			printf("Seidn==8で基礎年金拠出金を読み込もうとしていますSeidn=5\n");
			exit(1);
		}
		
		for(k=Ks; k<=Ke; k++){	
			for(x=XA-1; x<=XB; x++){
				if(x<67){ dx = 66; } else { dx = x; }	
				
				E3dxb[ii][0][k-STTY][dx] += D3bxtp[ii][0][0][1][k-STTY][x];
				E3dxb[ii][1][k-STTY][dx] +=( D3bxtp[ii][0][0][3][k-STTY][x] + D3bxtp[ii][0][0][4][k-STTY][x]
				                            +D3bxtp[ii][0][0][6][k-STTY][x] + D3bxtp[ii][0][0][7][k-STTY][x]);
				E3dxb[ii][2][k-STTY][dx] += D3bxtp[ii][0][0][5][k-STTY][x];
				E3dxb[ii][3][k-STTY][dx] += Kfpbxtp[ii][0][0][1][k-STTY][x];
				E3dxb[ii][4][k-STTY][dx] += Kfpbxtp[ii][0][0][2][k-STTY][x];
				E3dxb[ii][5][k-STTY][dx] += Kfpbxtp[ii][0][0][7][k-STTY][x];
				
				E3dxb[ii][6][k-STTY][dx] += Kyosdx[0][usd][0][k-STTY][x];
				E3dxb[ii][7][k-STTY][dx] += Kfkyosdx[0][usd][0][k-STTY][x];
				E3dxb[ii][8][k-STTY][dx] += Kyosdx[1][usd][0][k-STTY][x];
				E3dxb[ii][9][k-STTY][dx] += Kfkyosdx[1][usd][0][k-STTY][x];
				E3dxb[ii][10][k-STTY][dx]+= Kyosdx[0][usd][1][k-STTY][x];
				E3dxb[ii][11][k-STTY][dx]+= Kfkyosdx[0][usd][1][k-STTY][x];
				E3dxb[ii][12][k-STTY][dx]+= Kyosdx[1][usd][1][k-STTY][x];
				E3dxb[ii][13][k-STTY][dx]+= Kfkyosdx[1][usd][1][k-STTY][x];
			}
		}
	}


	
	if(Cntlset==1){
		for(ii=1; ii<=4; ii++){
		for(jj=0; jj<=13; jj++){
		for(k=Ks; k<=Ke; k++){
			for(x=66; x<=XB; x++){
				E3dxb[0][jj][k-STTY][x] += E3dxb[ii][jj][k-STTY][x];
			}
		}
		}
		}
	}
}

void shus_excel(void){  
	FILE *ofp_excel;
	char flname[30];
	int ii, i, k, x;
	
	if(Cntlset==1){
		sprintf(flname, "./tem_excel-%s.csv", Ecfile);
		if((ofp_excel=fopen(flname,"w"))==NULL){
			printf("エクセル用簡易収支ファイルのオープンに失敗しました\n");
			exit(1);
		}
		
		for(ii=1; ii<=4; ii++){
		for(i=0; i<=4; i++){
		for(k=Ks; k<=Ke; k++){
			fprintf(ofp_excel, "%d,%3d,%d,", ii, k, i);
			for(x=66; x<=115; x++){
				if(i!=4){
					fprintf(ofp_excel, "%18.14e,", E3dxb[ii][i][k-STTY][x]/1.0E8);
				} else {
					fprintf(ofp_excel, "%18.14e,",(E3dxb[ii][4][k-STTY][x]+E3dxb[ii][5][k-STTY][x])/1.0E8);
				}
			}
			fprintf(ofp_excel, "\n");
		}
		}
		}
		
		fclose(ofp_excel);
	}
}

void init_premium(void){
	int ii, k;
	double dtemp1, dtemp2;
	
	if(Ks<10 || Ks<STTY){
		printf("【警告】保険料率が正しくセットできていません\n");
	}
	
	dtemp1 = 0.1358;
	dtemp2 = 0.1496;
	for(k=4; k<=ENDY; k++){
	    dtemp1 += 0.00354;
	    dtemp2 += 0.00248;
		if(k>=STTY){
			Prema[1][k-STTY] = MIN(0.183, dtemp1);
			Prema[5][k-STTY] = MIN(0.183, dtemp2);
		}
	}
	dtemp1 = 0.15154;
	dtemp2 = 0.1223;
	for(k=10; k<=ENDY; k++){
		dtemp1 += 0.00354;
		dtemp2 += 0.00354;
		if(k>=STTY){
			Prema[2][k-STTY] = Prema[3][k-STTY] = MIN(0.183, dtemp1);
			Prema[4][k-STTY] = MIN(0.183, dtemp2);
		}
	}
	
	for(ii=1; ii<=5; ii++){	 
		if(ii==1 || ii==5 ){ 
			dtemp1=6.0;
			dtemp2=6.0;
		} else if(ii==2 || ii==3){  
			dtemp1=5.0;
			dtemp2=7.0;
		} else if(ii==4){
			dtemp1= 1.0;
			dtemp2=11.0;
		}
		for(k=Ks+1; k<=ENDY; k++){
			Premb[ii][k-STTY] = (dtemp1 * Prema[ii][k-1-STTY] + dtemp2 * Prema[ii][k-STTY] ) / 12.;
		}
	}
}


void shus_premium(void){	
	int k, s;
	int is, ie, ii, se;
	double prem[3];	
	double dtemp;
	
	if(Seidn!=0){
		is=ie=0;
	} else {
		is=1;
		ie=4;
	}

	
	
	/*****************************************************************
	             保険料収入計算(旧rd_premium.f)
	 *****************************************************************/
	for(ii=is; ii<=ie; ii++){
		for(k=Ks+1; k<=Ke; k++){
			if(Seidn==1 || ii==1){	
				prem[1]  = An[ii][0][k-STTY] * Premb[1][k-STTY];
				prem[2]  = An[ii][3][k-STTY] * (Premb[5][k-STTY]-Premb[1][k-STTY]);
				
				Cc[ii][1][k-STTY] = prem[1] + prem[2];
				Cc[ii][2][k-STTY] = prem[2];
				
				if(Flg_Part>0 && (k==Part_Yr1 || k==Part_Yr2)){
					if(k==Part_Yr1) {
					dtemp=Cbm_Pt2;
					}
					else  dtemp=Cbm_Pt2;
					if(dtemp<=6.0){
						dtemp=Prema[1][k-STTY];
					}else{
						dtemp=((dtemp-6.0)*Prema[1][k-1-STTY]+6.0*Prema[1][k-STTY])/dtemp;
					}
					Cc[ii][22][k-STTY] = Anpart[0][k-STTY] * dtemp;
					Cc[ii][1][k-STTY] += Cc[ii][22][k-STTY];
					for(s=0; s<=3; s++){
						A[ii][s][k-STTY]    += Apart[s][k-STTY];
						Aiku[ii][s][k-STTY] += Aikupart[s][k-STTY];
						A60[ii][s][k-STTY]  += A60part[s][k-STTY];
						A65[ii][s][k-STTY]  += A65part[s][k-STTY];
						A70[ii][s][k-STTY]  += A70part[s][k-STTY];
						
						An[ii][s][k-STTY]    += Anpart[s][k-STTY];
						Aniku[ii][s][k-STTY] += Anikupart[s][k-STTY];
					}
				}
			} else {  
				Cc[ii][1][k-STTY] = An[ii][0][k-STTY] * Premb[ii2psd(ii)][k-STTY];
			}
			
			Cc[ii][23][k-STTY] = A[ii][0][k-STTY] + Aiku[ii][0][k-STTY]; 
			Cc[ii][24][k-STTY] = An[ii][0][k-STTY]   ;
			Cc[ii][25][k-STTY] = Aniku[ii][0][k-STTY]; 
		}
	}


		
	if(Cntlset==1) {
		for(ii=1; ii<=4; ii++){
			Dc[ii][1]  = An[ii][0][Kyoskijun-STTY]  / 2. * Prema[ii][Kyoskijun-STTY];
			Dc[ii][23] = Cc[ii][23][Kyoskijun-STTY] / 2.;
			Dc[ii][24] = Cc[ii][24][Kyoskijun-STTY] / 2.;
			Dc[ii][25] = Cc[ii][25][Kyoskijun-STTY] / 2.;
			
			for(k=Ks+1; k<=Ke; k++){
				if(k>Kyoskijun || ii==1){
					Cc[0][1][k-STTY] += Cc[ii][1][k-STTY];
					if(ii==1) Cc[0][2][k-STTY] += Cc[ii][2][k-STTY];
					if(Flg_Part>0 && (k==Part_Yr1 || k==Part_Yr1+1 | k==Part_Yr2) && ii==1)
					  Cc[0][22][k-STTY] += Cc[ii][22][k-STTY];
					Cc[0][23][k-STTY] += Cc[ii][23][k-STTY];
					Cc[0][24][k-STTY] += Cc[ii][24][k-STTY];
					Cc[0][25][k-STTY] += Cc[ii][25][k-STTY];
				} else if(k==Kyoskijun){
					Cc[0][1][k-STTY]  += Dc[ii][1];
					Cc[0][23][k-STTY] += Dc[ii][23];
					Cc[0][24][k-STTY] += Dc[ii][24];
					Cc[0][25][k-STTY] += Dc[ii][25];
				}
			}
		}
	}

}



void shus_fukkjn(void){
	int k, ii, is, ie, psd;
	
	if(Seidn!=0){
		is=ie=0;
	} else {
		is=1;
		ie=4;
	}
	
	for(ii=is; ii<=ie; ii++){
		psd=ii2psd(ii);
		if(Kijun==12) Cc[1][20][12-STTY] = 1440617.0E+8;
		if(Kijun==13) Cc[1][20][13-STTY] = 1496815.0E+8;
		if(Kijun==14) Cc[1][20][14-STTY] = 1637632.0E+8;
		if(Kijun==15) Cc[1][20][15-STTY] = 1545895.0E+8;
		if(Kijun==15) Cc[2][20][15-STTY] = 71552.0E+8;
		if(Kijun==15) Cc[3][20][15-STTY] = 195697.0E+8;
		if(Kijun==15) Cc[4][20][15-STTY] = 20652.0E+8;
		if(Kijun==16) Cc[1][20][16-STTY] = 1596869.0E+8;
		if(Kijun==16) Cc[2][20][16-STTY] = 71145.0E+8; 
		if(Kijun==16) Cc[3][20][16-STTY] = 200478.0E+8;
		if(Kijun==16) Cc[4][20][16-STTY] = 20562.0E+8;
		if(Kijun==17) Cc[1][20][17-STTY] = 1682577.0E+8;
		if(Kijun==17) Cc[2][20][17-STTY] = 72687.0E+8; 
		if(Kijun==17) Cc[3][20][17-STTY] = 213577.0E+8;
		if(Kijun==17) Cc[4][20][17-STTY] = 22219.0E+8;
		
		
		for(k=Ks+1; k<=Ke; k++){
			if(Seidn<=4){
				Cc[ii][ 9][k-STTY] = 0;

				Cc[ii][10][k-STTY] = Nofu[psd][k-STTY];
				
				if(Seidn==1 || ii==1){
					if(k==12){
						Cc[ii][17][k-STTY] = 796.31340E+8;  
					} else if(k==13){
						Cc[ii][17][k-STTY] = 896.74026E+8; 
					} else if(k==14){
						Cc[ii][17][k-STTY] = 996.02674E+8;  
					} else if (k==15){
						Cc[ii][17][k-STTY] = 1114.49628E+8;
					}
					  else if (k==16){
						Cc[ii][17][k-STTY] = 1068.59931E+8;  
					}
					  else if (k==17){
					  	Cc[ii][17][k-STTY] = 1150.76818E+8; 
					}
					else if  (k==18) {
					  	Cc[ii][17][k-STTY] = 1512E+8; 
					}
					else {
							Cc[ii][17][k-STTY] = 1700E+8;

					}						
				} else if(Seidn==2 || ii==2){
					if(k==15){
						Cc[ii][17][k-STTY] =  48.96181E+8;
					}  else if (k>=16){
						Cc[ii][17][k-STTY] =  37.76072E+8;
					}
				} else if(Seidn==3 || ii==3){
					if(k==15){
						Cc[ii][17][k-STTY] =  60.16865E+8;
					}  else if (k>=16){
						Cc[ii][17][k-STTY] =  63.54911E+8;
					}
				} else if(Seidn==4 || ii==4){
					if(k==15){
						Cc[ii][17][k-STTY] =  17.55707E+8; 
					} else if (k>=16){
						Cc[ii][17][k-STTY] =  21.88732E+8;
					}
				}

				if(k>=20 && Flg_Jimu==2){
					Cc[ii][17][k-STTY]=Cc[ii][17][19-STTY]/Ap[ii][0][19-STTY]*Ap[ii][0][k-STTY];
				}
				
				Cc[ii][18][k-STTY] = 0;
			}
		}
	}


	/*** 一元化時の全厚年計算 ***/
	if(Cntlset==1){
		for(ii=1; ii<=4; ii++){
			if(ii==1) Dc[ii][10] = Cc[ii][10][Kyoskijun-STTY] / 2.;
			Dc[ii][17] = Cc[ii][17][Kyoskijun-STTY] / 2.;
			Dc[ii][30] = Tumazumi[ii][Kyoskijun-STTY];
			
			for(k=Ks+1; k<=Ke; k++){
				if(k>Kyoskijun || ii==1){
					Cc[0][9][k-STTY] +=Cc[ii][9][k-STTY];
					Cc[0][10][k-STTY]+=Cc[ii][10][k-STTY];
					Cc[0][17][k-STTY]+=Cc[ii][17][k-STTY];
					if(ii==1) Cc[0][20][k-STTY] = Cc[ii][20][k-STTY];
					/* 妻積分配金の設定 */
					Cc[ii][30][k-STTY] = Tumazumi[ii][k-STTY];
					Cc[0][30][k-STTY] += Tumazumi[ii][k-STTY];
				} else if(k==Kyoskijun){
					Cc[0][17][k-STTY] += Dc[ii][17];
					Cc[ii][30][k-STTY] = Dc[ii][30];
					Cc[0][30][k-STTY] += Dc[ii][30];
				}
			}
		}
	}
}

