<?php
	
// A CONFIGURER : Année de sortie de la Fonction Groupage
$FG_YEAR='2015';
// A CONFIGURER : Version de la Fonction Groupage et de la Classification des GHM
$FG_VERSION='fg1311g';
// A CONFIGURER : Version PRECEDENTE de la Fonction Groupage et de la Classification des GHM
$FG_PGM='fg11f11g.exe';

$workDir = $mgt_path.'/'.$FG_YEAR;

$csvColumns[$FG_YEAR] = "annee_sortie numeric(4),
					no_rss bigint DEFAULT 0,
					ghm_fg1_code character(6),
					ghs_fg1_code numeric(4),
					erreur character varying(10000) DEFAULT ''::character varying,
					commentaire character varying(10000) DEFAULT ''::character varying,
					nb_exh_fg1 numeric(5, 0) DEFAULT 0,
					nb_exb_fg1 numeric(5, 1) DEFAULT 0,
					nb_rea_fg1 numeric(5, 0) DEFAULT 0,
					nb_rep_fg1 numeric(5, 0) DEFAULT 0,
					nb_src_fg1 numeric(5, 0) DEFAULT 0,
					nb_stf_rea_fg1 numeric(5, 0) DEFAULT 0,
					nb_stf_fg1 numeric(5, 0) DEFAULT 0,
					nb_nn1_fg1 numeric(5, 0) DEFAULT 0,
					nb_nn2_fg1 numeric(5, 0) DEFAULT 0,
					nb_nn3_fg1 numeric(5, 0) DEFAULT 0,
					nb_ant_fg1 numeric(5, 0) DEFAULT 0,
					nb_rap_fg1 numeric(5, 0) DEFAULT 0,
					ghs_minore_code character(1)";

//group_file_2015();

// Récupère et groupe le fichier de groupage depuis le site www.ctisante.com.
function group_file_2015() {
	echo "\r\n" . date("d/m/Y H:i:s") . "\t" . "groupage fichier version groupeur 2015";
	
	global $tmp_RSSoutfile;
	global $mgt_path;
	global $ngExt;
	global $workDir;
	global $FG_YEAR;
	global $FG_VERSION;
	global $FG_PGM;
	
	$absoluteWorkDir = $workDir;
	$dirFG12 = Environnement::getFiness();
	$tmp = explode('/'.$dirFG12.'/',$tmp_RSSoutfile);
	$fileFG12 = $tmp[1];
	
	
	//partie copiée de PMSI4CTI_simulationV1311X partie if ($operation == OP_GROUP_FILE) {} en enlevant get_file_from_web($fileFG12);

    $rss_array = array();
    $rss_dp_array = array();
    $rss_dr_array = array();
    $rss_da_array = array();

    $diag_p_array = array();
    $diag_a_array = array();

    $actes_array = array();

    $diag_handle = fopen($workDir . "/pgm/diags.csv", "r");

    $inputR = 0;
    while (($data = fgetcsv($diag_handle, 1000, ";")) !== FALSE) {
        $inputR++;

        if ($inputR > 1 && trim($data[2]) != "") {
            $from_code = substr(trim($data[0]) . "         ", 0, 8);
            $to_code = substr(trim($data[2]) . "         ", 0, 8);
            $diag_a_array[$from_code] = $to_code;
        }
        if ($inputR > 1 && trim($data[3]) != "") {
            $from_code = substr(trim($data[0]) . "         ", 0, 8);
            $to_code = substr(trim($data[3]) . "         ", 0, 8);
            $diag_p_array[$from_code] = $to_code;
        }
    }
    fclose($diag_handle);

    $acte_handle = fopen($workDir . "/pgm/actes.csv", "r");

    $inputR = 0;
    while (($data = fgetcsv($acte_handle, 1000, ";")) !== FALSE) {
        $inputR++;

        if ($inputR > 1 && trim($data[3]) != "") {
            $from_code = substr(trim($data[0]) . "         ", 0, 10);
            if (trim($data[3]) != "*SUP") {
                $to_code = substr(trim($data[3]) . "         ", 0, 10);
            } else {
                $to_code = "*SUP";
            }
            $actes_array[$from_code] = $to_code;
        }
    }
    fclose($acte_handle);

    $typegrp = "2";

    $debugFG12 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".debug";
    $debugFG12_handle = fopen($workDir . "/files/$dirFG12/$debugFG12", "w");
    // préparation
    $tempFG12 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".IN";
    $tempFG12_handle = fopen($workDir . "/files/$dirFG12/$tempFG12", "w");

    $umFG12 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".FICUM";
    $umFG12_handle = fopen($workDir . "/files/$dirFG12/$umFG12", "w");

    $fg12_handle = @fopen($workDir . "/files/$dirFG12/$fileFG12", "r");
    if ($fg12_handle) {
        $no_ligne = 0;
        while (!feof($fg12_handle)) {
            $buffer = fgets($fg12_handle, 102400);

            $no_ligne++;

            if (trim($buffer) != "" && $no_ligne < 100000000) {

                if (substr($buffer, 0, 9) == "TYPEETS=2" || substr($buffer, 0, 9) == "TYPEETS=3") {
                    $typegrp = "1";
                } else {

                    if (substr($buffer, 0, 10) == "UNITEMEDIC") {
                        $buffer = substr($buffer, 10);
                        $buffer = str_replace("0101" . $FG_YEAR . "000", "0101" . ($FG_YEAR - 1) . "000", $buffer);
                        fwrite($umFG12_handle, "$buffer");
                    } else {

                        $no_rss = substr($buffer, 27, 20);
                        $cmd_code = substr($buffer, 2, 2);
                        $rss_array[$no_rss] = array();
                        $rss_array[$no_rss][0] = substr($no_rss, 0, 4);
                        $rss_array[$no_rss][1] = substr($no_rss, 4, 16);
                        $rss_array[$no_rss][2] = substr($buffer, 2, 6);
                        //$rss_array[$no_rss][3] = substr($buffer , 6 , 4);
                        $rss_array[$no_rss][3] = "0000";
                        $rss_array[$no_rss][4] = " ";
                        $rss_array[$no_rss][5] = " ";
                        $rss_array[$no_rss][6] = " ";
                        $rss_array[$no_rss][7] = " ";
                        $rss_array[$no_rss][8] = " ";
                        $rss_array[$no_rss][9] = " ";
                        $rss_array[$no_rss][11] = 0;
                        $rss_array[$no_rss][12] = 0;
                        $rss_array[$no_rss][13] = 0;
                        $rss_array[$no_rss][14] = 0;
                        $rss_array[$no_rss][15] = 0;
                        $rss_array[$no_rss][16] = 0;
                        $rss_array[$no_rss][17] = 0;
                        $rss_array[$no_rss][18] = 0;
                        $rss_array[$no_rss][19] = 0;
                        $rss_array[$no_rss][20] = 0;

                        $rss_dp_array[$no_rss] = "";
                        $rss_dr_array[$no_rss] = "";
                        $rss_da_array[$no_rss] = "";

                        $rss_array[$no_rss][4] = "";
                        $rss_array[$no_rss][5] = "ZZZZZZ";
                        $rss_array[$no_rss][6] = 9999;
                        $rss_array[$no_rss][7] = "";

                        $ghm_fg12_5 = substr($buffer, 2, 5);

                        $cmd = substr($buffer, 2, 2);

                        $buffer = substr($buffer, 15);

                        if (substr($buffer, 9, 3) == "015") {
                            $buffer = substr($buffer, 0, 108) . "        " . substr($buffer, 108, 39) . "               " . "       " . substr($buffer, 150);
                        }

                        // Remplacement mode entree / provenance
                        $mode_entree = substr($buffer, 85, 1);
                        $provenance = substr($buffer, 86, 1);
                        if ($mode_entree == "8" & $provenance != " ") {
                            $buffer = substr_replace($buffer, " ", 86, 1);
                        }

                        // Remplacement mode sortie / destination
                        $mode_sortie = substr($buffer, 95, 1);
                        $destination = substr($buffer, 96, 1);
                        if ($mode_sortie == "8" & $destination != " ") {
                            $buffer = substr_replace($buffer, " ", 96, 1);
                        }

                        // Remplacement "Nb de séances"
                        if ($modeTraitement) {
                            $date_entree = substr($buffer, 77, 8);
                            $date_sortie = substr($buffer, 87, 8);
                            $nb_seances = (int) substr($buffer, 116, 2);
                            if (($date_entree == $date_sortie) && ($cmd == "28") && ($nb_seances == 0)) {
                                $buffer = substr_replace($buffer, '01', 116, 2);
                            }
                        }

                        if (substr($buffer, 125, 1) == "O") {
                            $buffer = substr_replace($buffer, "0107" . ($FG_YEAR - 1), 108, 8);
                        }
                        if (substr($buffer, 125, 3) == "Z35") {
                            $buffer = substr_replace($buffer, "0107" . ($FG_YEAR - 1), 108, 8);
                        }
                        if (substr($buffer, 177, 3) == "Z35") {
                            $buffer = substr_replace($buffer, "0107" . ($FG_YEAR - 1), 108, 8);
                        }
                        if (substr($buffer, 185, 3) == "Z35") {
                            $buffer = substr_replace($buffer, "0107" . ($FG_YEAR - 1), 108, 8);
                        }
                        if (substr($buffer, 193, 3) == "Z35") {
                            $buffer = substr_replace($buffer, "0107" . ($FG_YEAR - 1), 108, 8);
                        }

                        // Remplacement confirmation codage
                        $confirmation = substr($buffer, 144, 1);
                        if ($confirmation == "1") {
                            //$buffer = substr_replace($buffer, "2", 144, 1);
                        }
                        if ($ghm_fg12_5 == "03C26") {
                            //$buffer = substr_replace($buffer,"1", 144, 1);
                        }

                        // Remplacement version
                        $version = substr($buffer, 9, 3);
                        if ($version == "016") {
                            $buffer = substr_replace($buffer, "017", 9, 3);
							echo 'in';
                        }

                        // Age gestationnel
                        $age = substr($buffer, 106, 2);
                        if ($age + 0 == 0 && $cmd_code == "14" && substr($buffer, 102, 4) != "0000") {
                            $buffer = substr_replace($buffer, "38", 106, 2);
                        }
                        if ($age + 0 == 0 && $cmd_code == "15" && substr($buffer, 102, 4) != "0000") {
                            $buffer = substr_replace($buffer, "38", 106, 2);
                        }

                        // Remplacement diagnostic principal
                        $diag_p_in = substr($buffer, 125, 8);
                        if (trim($diag_p_in) != "****") {
                            $diag_p = "";
                            $diag_p = @$diag_p_array[$diag_p_in];

                            if (trim($diag_p) != "MAT") {
                                if (strlen($diag_p) != 8) {
                                    $diag_p = @$diag_a_array[$diag_p_in];
                                }
                                if (strlen($diag_p) == 8) {
                                    $buffer = substr_replace($buffer, $diag_p, 125, 8);
                                    $rss_array[$no_rss][9] = $rss_array[$no_rss][9] . "RDP " . trim($diag_p_in) . " remplacé par " . trim($diag_p) . ", ";
                                }
                            } else {
                                $diags_a_l = substr($buffer, 118, 2) + 0;
                                if ($diags_a_l > 0) {
                                    $echange = false;
                                    for ($i = 0; $i < $diags_a_l; $i++) {
                                        $diag_a_in = substr($buffer, 177 + ($i * 8), 8);
                                        if (substr($diag_a_in, 0, 3) == "O68" || substr($diag_a_in, 0, 3) == "O69") {
                                            $buffer = substr_replace($buffer, $diag_a_in, 125, 8);
                                            $buffer = substr_replace($buffer, $diag_p_in, 177 + ($i * 8), 8);
                                            $rss_array[$no_rss][9] = $rss_array[$no_rss][9] . "Echange DP/DA " . trim($diag_p_in) . "-" . trim($diag_a_in) . ", ";
                                            $echange = true;
                                            break;
                                        }
                                    }
                                }

                                if ($echange == false) {
                                    if ($diags_a_l > 0) {
                                        $echange = false;
                                        for ($i = 0; $i < $diags_a_l; $i++) {
                                            $diag_a_in = substr($buffer, 177 + ($i * 8), 8);
                                            if (substr($diag_a_in, 0, 2) == "O6") {
                                                $buffer = substr_replace($buffer, $diag_a_in, 125, 8);
                                                $buffer = substr_replace($buffer, $diag_p_in, 177 + ($i * 8), 8);
                                                $rss_array[$no_rss][9] = $rss_array[$no_rss][9] . "DP/DA " . trim($diag_p_in) . "-" . trim($diag_a_in) . " échangés, ";
                                                $echange = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            $diag_p = trim(substr($buffer, 125, 8));
                            if ($diag_p != "") {
                                if (trim($rss_dp_array[$no_rss]) == "") {
                                    $rss_dp_array[$no_rss] = $diag_p . ",";
                                } else {
                                    if (strpos($rss_dp_array[$no_rss], $diag_p . ",") === false) {
                                        $rss_dp_array[$no_rss] = $rss_dp_array[$no_rss] . $diag_p . ",";
                                    }
                                }
                            }

                            // Remplacement diagnostic relie
                            $diag_r_in = substr($buffer, 133, 8);
                            $diag_r = "";
                            $diag_r = @$diag_p_array[$diag_r_in];
                            if (strlen($diag_r) != 8 && trim($diag_r) != "MAT") {
                                $diag_r = @$diag_a_array[$diag_r_in];
                            }
                            if (strlen($diag_r) == 8) {
                                $buffer = substr_replace($buffer, $diag_r, 133, 8);

                                $rss_array[$no_rss][9] = $rss_array[$no_rss][9] . "DR " . trim($diag_r_in) . " remplacé par " . trim($diag_r) . ", ";
                            }
                            $diag_r = trim(substr($buffer, 133, 8));
                            if ($diag_r != "") {
                                if ($rss_dr_array[$no_rss] == "") {
                                    $rss_dr_array[$no_rss] = $diag_r . ",";
                                } else {
                                    if (strpos($rss_dr_array[$no_rss], $diag_r . ",") === false) {
                                        $rss_dr_array[$no_rss] = $rss_dr_array[$no_rss] . $diag_r . ",";
                                    }
                                }
                            }

                            // Remplacement diagnostics associés
                            $diags_a_l = substr($buffer, 118, 2) + 0;
                            if ($diags_a_l > 0) {

                                for ($i = 0; $i < $diags_a_l; $i++) {
                                    $diag_a_in = substr($buffer, 177 + ($i * 8), 8);

                                    $diag_a = "";
                                    $diag_a = @$diag_a_array[$diag_a_in];
                                    if (strlen($diag_a) == 8) {
                                        $buffer = substr_replace($buffer, $diag_a, 177 + ($i * 8), 8);

                                        $rss_array[$no_rss][9] = $rss_array[$no_rss][9] . "DA " . trim($diag_a_in) . " remplacé par " . trim($diag_a) . ", ";
                                    }
                                    $diag_a = trim(substr($buffer, 177 + ($i * 8), 8));
                                    if ($diag_a != "") {
                                        if ($rss_da_array[$no_rss] == "") {
                                            $rss_da_array[$no_rss] = $diag_a . ",";
                                        } else {
                                            if (strpos($rss_da_array[$no_rss], $diag_a . ",") === false) {
                                                $rss_da_array[$no_rss] = $rss_da_array[$no_rss] . $diag_a . ",";
                                            }
                                        }
                                    }
                                }
                            }

                            // Remplacement actes
                            $diags_a_l = substr($buffer, 118, 2) + 0;
                            $diags_n_l = substr($buffer, 120, 2) + 0;
                            $actes_l = substr($buffer, 122, 3) + 0;
                            $pos_actes = 177 + ($diags_n_l * 8) + ($diags_a_l * 8);
                            
							
							// par rapport au passage a la V17, la taille des actes ccam a été rallongée de 3 caractères (10 maintenant)
							// implémentation de ce bout de code pour traiter en 2015 les fichiers en V16 (2014)
                        /*    if ($actes_l > 0) {
								$actes_traites = "";
								$actes = substr($buffer, $pos_actes , 26 * $actes_l);
								$arr_actes = str_split($actes, 26);
								foreach ($arr_actes as $actes_a_traiter){
									$actes_traites .= substr($actes_a_traiter, 0, 15 ) . "-00" . substr($actes_a_traiter, 15, 26);
									//SPECIAL RECHERCHE ACTES A DECOMMENTER SI BESOIN
									// $actes_traites_2 = substr($actes_a_traiter, 8, 7 ) . "-00";
									//  $file_debug .= PHP_EOL . $no_rss . ";" . $actes_traites_2; 
									//SPECIAL RECHERCHE ACTES
								}
								//SPECIAL RECHERCHE ACTES A DECOMMENTER SI BESOIN
								//FILE_PUT_CONTENTS("d:/cti/temp/actes_debug.txt", $file_debug, FILE_APPEND );
								//$file_debug = "";
								//FIN SPECIAL RECHERCHE ACTES
								$buffer = substr_replace($buffer, $actes_traites, $pos_actes, strlen($actes)  );
							}	*/
							// Fin implementation CCAM 10 caractères
	 						
							if ($actes_l > 0) {
								$nb_actes_supprimes = 0;
								$actes_traites = "";
								$actes = substr($buffer, $pos_actes , 29 * $actes_l);
								$arr_actes = str_split($actes, 29);
								foreach ($arr_actes as $actes_a_traiter){
									$actes_a_remplacer = substr($actes_a_traiter, 8, 10 );
									if (strlen($actes_a_remplacer) > 0){
										// si actes present dans tableaux actes.csv faire le remplacement
										if (array_key_exists($actes_a_remplacer, $actes_array) ){
											if ($actes_array[$actes_a_remplacer] != "*SUP"){ 
												$actes_a_traiter = preg_replace ('/' . $actes_a_remplacer . '/' , $actes_array[$actes_a_remplacer] ,$actes_a_traiter);
												$rss_array[$no_rss][9] = $rss_array[$no_rss][9] . "$actes_a_remplacer remplacé par $actes_array[$actes_a_remplacer], ";
											}
											else {
												$actes_a_traiter = "";
												$rss_array[$no_rss][9] = $rss_array[$no_rss][9] . "$actes_a_remplacer supprimé, ";
												$nb_actes_supprimes = $nb_actes_supprimes + 1;
											}
										}
										$actes_traites .= $actes_a_traiter;
									}
								}
								$buffer = substr_replace($buffer, $actes_traites, $pos_actes, strlen($actes)  );
								
								$nb_actes = $actes_l - $nb_actes_supprimes;
								if ($nb_actes < 100){
									$nb_actes = "0" . "$nb_actes";
									if ($nb_actes < 10){
										$nb_actes = "0" . "$nb_actes";
									}
								}
								$buffer = substr_replace($buffer, "$nb_actes" , 122 , 3  );
                            } 
                            fwrite($tempFG12_handle, ltrim("$buffer"));
                        }
                    }
                }
            }
        }
        fclose($fg12_handle);
    }
    fclose($tempFG12_handle);
    fclose($umFG12_handle);

    $logFG13 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".IN.log";
    $errFG13 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".IN.err";
    // Groupage
	$shell_cmd = "$absoluteWorkDir/pgm/fg/bin/" . $FG_PGM . " $absoluteWorkDir/files/$dirFG12/$tempFG12 $absoluteWorkDir/pgm/fg/TABLES/ $typegrp $absoluteWorkDir/files/$dirFG12/$umFG12 $absoluteWorkDir/files/$dirFG12/";
    fwrite($debugFG12_handle, "$shell_cmd");
    shell_exec($shell_cmd);

    fclose($debugFG12_handle);

    $errFG13 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".IN.err";
    $errFG13_handle = @fopen($workDir . "/files/$dirFG12/$errFG13", "r");
    while (($data = fgetcsv($errFG13_handle, 1000, ",")) !== FALSE) {

        $no_rss = $data[0];
        $rss_array[$no_rss][8] = $data[4];
    }
    fclose($errFG13_handle);

    $rdsFG13 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".IN.rds";
    $rdsFG13_handle = @fopen($workDir . "/files/$dirFG12/$rdsFG13", "r");
    if ($rdsFG13_handle) {
        while (!feof($rdsFG13_handle)) {
            $buffer = fgets($rdsFG13_handle, 10240);

            $no_ligne++;
            if (trim($buffer) != "" && $no_ligne < 10000000) {

                $no_rss = substr($buffer, 0, 20); // N° RSS

                $rss_array[$no_rss][4] = get_ghm($rss_array[$no_rss][2]);

                $rss_array[$no_rss][5] = substr($buffer, 37, 6); // GHM
                $rss_array[$no_rss][6] = substr($buffer, 93, 4); // GHS

                $rss_array[$no_rss][7] = get_ghm($rss_array[$no_rss][5]);

                $rss_array[$no_rss][8] = get_err($rss_array[$no_rss][8]);

                // Supplements
                $rss_array[$no_rss][11] = substr($buffer, 97, 4) + 0; // nb_exh_fg1 : nombre de journées au dela de la borne haute
                $rss_array[$no_rss][12] = (substr($buffer, 102, 5) + 0); // nb_exb_fg1 : nombre de journées en dessous de la borne basse
                $rss_array[$no_rss][13] = substr($buffer, 108, 3) + 0; // nb_rea_fg1 : nombre de suppléments de réanimation
                $rss_array[$no_rss][14] = substr($buffer, 111, 3) + 0; // nb_rep_fg1 : nombre de suppléments de réanimation pédiatrique
                $rss_array[$no_rss][15] = substr($buffer, 114, 3) + 0; // nb_src_fg1 : nombre de suppléments surveillance continue
                $rss_array[$no_rss][16] = substr($buffer, 117, 3) + 0; // nb_stf_rea_fg1 : nombre de suppléments soins intensifs provenant de la réanimation
                $rss_array[$no_rss][17] = substr($buffer, 120, 3) + 0; // nb_stf_fg1 : nombre total de suppléments soins intensifs, y compris les suppléments provenant de la réanimation
                $rss_array[$no_rss][18] = substr($buffer, 166, 3) + 0; // nb_nn1_fg1 : nombre de journées de néonatologie type 2A
                $rss_array[$no_rss][19] = substr($buffer, 169, 3) + 0; // nb_nn2_fg1 : nombre de journées de néonatologie type 2B
                $rss_array[$no_rss][20] = substr($buffer, 172, 3) + 0; // nb_nn3_fg1 : nombre de journées de néonatologie type 3
                $rss_array[$no_rss][21] = substr($buffer, 194, 3) + 0; // nb_ant_fg1 : Nombre de suppléments antepartum
                $rss_array[$no_rss][22] = substr($buffer, 197, 3) + 0; // nb_rap_fg1 : Nombre d?actes pour le supplément radiothérapie pédiatrique
				$rss_array[$no_rss][23] = substr($buffer, 200, 1) + 0; // GHS Minoré
            }
        }
    }
    fclose($rdsFG13_handle);

    // detail par RSS
    $rssFG13 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".csv";
    $rssFG13_handle = fopen($workDir . "/files/$dirFG12/$rssFG13", 'w');

    foreach ($rss_array as $rss) {
		fwrite($rssFG13_handle, implode(';',$rss).PHP_EOL);
    }

    fclose($rssFG13_handle);

    // fichier à renvoyer
    $fileFG13 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . "." . strtoupper($FG_VERSION);

    @unlink($workDir . "/files/$dirFG12/$fileFG13");

    $fileFG13_handle = fopen($workDir . "/files/$dirFG12/$fileFG13", 'w');

    foreach ($rss_array as $rss) {
        if ($rss[0] != "") {
            $torss = array();
            $torss[0] = $rss[0];
            $torss[1] = $rss[1];
            $torss[2] = $rss[5];
            $torss[3] = trim($rss[6]);
            if ($torss[3] == "D09") {
                $torss[3] = "9999";
            }
            if ($torss[3] == "D19") {
                $torss[3] = "9999";
            }
            if (substr($torss[3], 0, 1) == "D") {
                $torss[3] = "9999";
            }
            $torss[4] = $rss[8];
            $torss[5] = $rss[9];
            $torss[11] = $rss[11];
            $torss[12] = $rss[12];
            $torss[13] = $rss[13];
            $torss[14] = $rss[14];
            $torss[15] = $rss[15];
            $torss[16] = $rss[16];
            $torss[17] = $rss[17];
            $torss[18] = $rss[18];
            $torss[19] = $rss[19];
            $torss[20] = $rss[20];
            $torss[21] = $rss[21];
            $torss[22] = $rss[22];
			$torss[23] = $rss[23];
			
            fputcsv($fileFG13_handle, $torss, ";");
        }
    }

    fclose($fileFG13_handle);

    // Log cti
    $logFG13 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".IN.log";
    $errFG13 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".IN.err";

    // préparation
    $tempFG12 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".IN";
    $fg12_handle = @fopen($workDir . "/files/$dirFG12/$tempFG12", "r");
    if ($fg12_handle) {
        while (!feof($fg12_handle)) {
            $buffer = fgets($fg12_handle, 10240);

            $no_ligne++;
            if (trim($buffer) != "" && $no_ligne < 100000000) {

                $no_rss = substr($buffer, 12, 20);

                $rss_dp_array[$no_rss] = "";
                $rss_dr_array[$no_rss] = "";
                $rss_da_array[$no_rss] = "";
                $rss_ac_array[$no_rss] = "";

                $diag_p = trim(substr($buffer, 125, 8));
                if ($diag_p != "") {
                    if ($rss_dp_array[$no_rss] == "") {
                        $rss_dp_array[$no_rss] = $diag_p . "-";
                    } else {
                        if (strpos($rss_dp_array[$no_rss], $diag_p . "-") === false) {
                            $rss_dp_array[$no_rss] = $rss_dp_array[$no_rss] . $diag_p . "-";
                        }
                    }
                }

                $diag_r = trim(substr($buffer, 133, 8));
                if ($diag_r != "") {
                    if ($rss_dr_array[$no_rss] == "") {
                        $rss_dr_array[$no_rss] = $diag_r . "-";
                    } else {
                        if (strpos($rss_dr_array[$no_rss], $diag_r . "-") === false) {
                            $rss_dr_array[$no_rss] = $rss_dr_array[$no_rss] . $diag_r . "-";
                        }
                    }
                }

                // Remplacement diagnostics associés
                $diags_a_l = substr($buffer, 118, 2) + 0;
                if ($diags_a_l > 0) {
                    for ($i = 0; $i < $diags_a_l; $i++) {
                        $diag_a = trim(substr($buffer, 177 + ($i * 8), 8));
                        if ($diag_a != "") {
                            if ($rss_da_array[$no_rss] == "") {
                                $rss_da_array[$no_rss] = $diag_a . "-";
                            } else {
                                if (strpos($rss_da_array[$no_rss], $diag_a . "-") === false) {
                                    $rss_da_array[$no_rss] = $rss_da_array[$no_rss] . $diag_a . "-";
                                }
                            }
                        }
                    }
                }

                $diags_a_l = substr($buffer, 118, 2) + 0;
                $diags_n_l = substr($buffer, 120, 2) + 0;
                $actes_l = substr($buffer, 122, 3) + 0;
                $pos_actes = 177 + ($diags_a_l * 8) + ($diags_n_l * 8);
                if ($actes_l > 0) {
                    $rss_ac_array = array();
                    for ($i = 0; $i < $actes_l; $i++) {
                        $acte = substr($buffer, 177 + ($diags_a_l * 8) + ($diags_n_l * 8) + ($i * 28) + 8, 7);
                        if ($rss_ac_array[$no_rss] == "") {
                            $rss_ac_array[$no_rss] = $acte . "-";
                        } else {
                            if (strpos($rss_ac_array[$no_rss], $acte . "-") === false) {
                                $rss_ac_array[$no_rss] = $rss_ac_array[$no_rss] . $acte . "-";
                            }
                        }
                    }
                }
            }
        }
        fclose($fg12_handle);
    }

    $ctilogFG13 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".CTILOG";
    $ctilogFG13_handle = fopen($workDir . "/files/$dirFG12/$ctilogFG13", "w");

    fwrite($ctilogFG13_handle, "Sommaire\r\n");
    fwrite($ctilogFG13_handle, @file_get_contents($workDir . "/files/$dirFG12/$logFG13"));
    fwrite($ctilogFG13_handle, "\r\nDétail\r\n");

    $errFG13 = substr($fileFG12, 0, strlen($fileFG12) - strlen($ngExt)) . ".IN.err";
    $errFG13_handle = @fopen($workDir . "/files/$dirFG12/$errFG13", "r");
    while (($data = fgetcsv($errFG13_handle, 1000, ",")) !== FALSE) {
        $no_rss = $data[0];
        $um = $data[1];
        $code_err = $data[4];
        $text_err = get_err($code_err);

        $dp = "Aucun DP";
        $dr = "Aucun DR";
        $da = "Aucun DA";
        $ac = "Aucun Acte";
        if ($rss_dp_array[$no_rss] != "") {
            $dp = "DP=" . substr($rss_dp_array[$no_rss], 0, strlen($rss_dp_array[$no_rss]) - 1);
        }
        if ($rss_dr_array[$no_rss] != "") {
            $dr = "DR=" . substr($rss_dr_array[$no_rss], 0, strlen($rss_dr_array[$no_rss]) - 1);
        }
        $da = "";
        if ($rss_da_array[$no_rss] != "") {
            $da = "DA=" . substr($rss_da_array[$no_rss], 0, strlen($rss_da_array[$no_rss]) - 1);
        }
        $ac = "";
        if ($rss_ac_array[$no_rss] != "") {
            $ac = "actes=" . substr($rss_ac_array[$no_rss], 0, strlen($rss_ac_array[$no_rss]) - 1);
        }
        fwrite($ctilogFG13_handle, "$no_rss; $um; $code_err; $text_err; $dp; $dr; $da; $ac\r\n");
    }
    fclose($errFG13_handle);
    fclose($ctilogFG13_handle);

    $return_code = "OK";
    $return_message = "Regroupement terminé";

}

?>
