<?php
/*=======================================================================
 *
 * import_UTA.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/

include_once ("../sysadmin/import.php");
// include_once ("../include/html.inc.php");

// Execution-Time for script
set_time_limit(30000);


// $hq_id = "1";
$importFile = "../temp/6305.UTA";
$fire = true;

$f_numOfRows = 0;
$f_checksum  = 0;

$currentTime = getDateTime("0");

// *******************
// * Import UTA data *
// *******************

// Check for existence of new UTA file
$importFileDoesExist = true;
if (false) :
   $importFileDoesExist = false;
endif;


// Converts STR to DATE
function cStr2Date ($str, $mode = "") {
	if ($mode == "1") :
	   // Default: "d.m.Y" => "Y-m-d"
     $str = substr($str,6,4) . "-" . substr($str,3,2) . "-" . substr($str,0,2);
	elseif ($mode == "2") :
	   // ...
	else :
	   // Default: "dmY" => "Y-m-d"
     $str = substr($str,4,4) . "-" . substr($str,2,2) . "-" . substr($str,0,2);
  endif;
  return $str;
}

// Converts STR to FLOAT (with ".") [e.g.: 000011122 => 111.22 with 2 decimals]
function cStr2Float ($str, $decimals = "2") {
	if (is_numeric($str)) :
     $numberPrefix = substr($str, 0, strlen($str) - $decimals);
     $numberSuffix = substr($str, strlen($str) - $decimals, $decimals);
     while (substr($numberPrefix,0,1) == "0" && strlen($numberPrefix) > 0) {
     	     $numberPrefix = substr($numberPrefix,1);
     }
     $str = $numberPrefix . "." . $numberSuffix;
  endif;
  return $str;
}


if ($importFileDoesExist) :

   echo "************************************************************<br>";
   echo "IMPORTDATEI: " . $importFile . "<br>";
   echo "************************************************************<br>";
   echo "<br><br><br>";   

   // Read file to import
   $data = importDataset($importFile);
   $dataLen = count($data);

   echo "Anzahl Einträge: " . $dataLen . "<br><br>";

   $f_31_sum = 0;  // Sum of all values of field $f_31 to be compared with the checksum
   $numOfNotEmptyRows = 0;

   // Loop all rows
   for ($j = 0; $j < $dataLen; $j++) :

      $r = trim($data[$j]);   // Get row
      if ($r != "") :

         echo "<br><br>";
         if ($j < $dataLen - 1) : echo $j . ". "; endif;


         $f_dataset_type = substr($r,0,2);   // Type of the data set

         if ($f_dataset_type == "10") :
            $f_02 = substr($r,2,13);    // Absender-ID
            $f_03 = substr($r,15,13);   // Empfänger-ID
            $f_04 = cStr2Date(substr($r,28,10),1);   // Datum
            $f_05 = substr($r,38,1);    // Leerzeichen
            $f_06 = substr($r,39,8);    // Uhrzeit
            $f_07 = substr($r,47,13);   // Abrechnungsnr.
            $f_08 = substr($r,60,6);    // Übertragungsnr.
            $f_09 = substr($r,66,6);    // DTF-Format

         elseif ($f_dataset_type == "20") :
            $f_02 = substr($r,2,13);    // Abrechnungsnr.
            $f_03 = cStr2Date(substr($r,15,8));    // Datum "Für Lieferungen und Leistungen bis"
            $f_04 = substr($r,23,13);   // Rechnunsempfänger-Kundennr.
            $f_05 = cStr2Date(substr($r,36,8));    // Abrechnungsdatum
            $f_06 = substr($r,44,13);   // Kundennr.
            $f_07 = substr($r,57,19);   // Kartennr. (vollständig)
            $f_08 = cStr2Date(substr($r,76,8));    // Belegdatum [TTMMJJJJ]
            $f_09 = substr($r,84,13);   // Akzeptanzstellennr.
            $f_10 = substr($r,97,25);   // Standort der Akzeptanzstelle
            $f_11 = substr($r,122,3);   // Lieferland
            $f_12 = substr($r,125,13);  // Belegnr. des Lieferanten
            $f_13 = substr($r,138,8);   // Kilometer-Stand
            $f_14 = substr($r,146,5);   // Fakturierwarenart
            $f_15 = substr($r,151,1);   // Vorzeichen-Statusfeld für alle Beträge und Mengen (0 = pos., 1 = neg.)
            $f_16 = cStr2Float(substr($r,152,10));  // Menge
            $f_17 = substr($r,162,1);   // SB / BT (1 = Selbstbedienung / 0 = Bedienung)
            $f_18 = cStr2Float(substr($r,163,5));   // USt.-Prozentsatz
            $f_19 = substr($r,168,3);   // Lieferlandwährung (ISO)
            $f_20 = cStr2Float(substr($r,171,11));  // Einzelpreis-Brutto in Lieferlandwährung  (= $f_21 * (($f_18 + 100) / 100))
            $f_21 = cStr2Float(substr($r,182,11));  // Einzelpreis-Netto in Lieferlandwährung
            $f_22 = cStr2Float(substr($r,193,11));  // Betrag Service in Lieferlandwährung (Brutto)
            $f_23 = cStr2Float(substr($r,204,11));  // Betrag Nachlass in Lieferlandwährung (Brutto)
            $f_24 = cStr2Float(substr($r,215,11));  // Gesamtbetrag-Netto in Lieferlandwährung (= ($f_16 * $f_48) + ($f_22 / (($f_18 + 100) / 100)))
            $f_25 = cStr2Float(substr($r,226,11));  // Gesamtbetrag-Brutto in Lieferlandwährung (= ($f_16 * $f_47) + $f_22)
            $f_26 = substr($r,237,3);   // Darstellwährung (ISO)
            $f_27 = cStr2Float(substr($r,240,11));  // Betrag Service in Darstellwährung
            $f_28 = cStr2Float(substr($r,251,11));  // Betrag Nachlass in Darstellwährung
            $f_29 = cStr2Float(substr($r,262,11));  // Gesamtbetrag-Netto in Darstellwährung
            $f_30 = cStr2Float(substr($r,273,11));  // Gesamtbetrag-USt. in Darstellwährung
            $f_31 = cStr2Float(substr($r,284,11));  // Gesamtbetrag-Brutto in Darstellwährung (= $f_29 + $f_30)#
            $f_31_sum += $f_31;
            $f_32 = substr($r,295,16);  // Prägetext (i.d.R. KFZ-Kennzeichen)
            $f_33 = substr($r,311,20);  // Kunden-Kostenstelle (für ein Fahrzeug gewählte Kostenstelle)
            $f_34 = substr($r,331,8);   // Kartentyp
            $f_35 = substr($r,339,1);   // %
            $f_36 = substr($r,340,3);   // %
            $f_37 = substr($r,343,1);   // KZ Autobahn (0 = liegt nicht an der Autobahn, 1 = liegt an der Autobahn)
            $f_38 = substr($r,344,18);  // Info-Feld (für später)
            $f_39 = substr($r,362,16);  // KFZ-Kennzeichen (evtl. = $f_32)
            $f_40 = substr($r,378,1);   // %
            $f_41 = substr($r,379,1);   // Buchungsart (B = Buchung, G = Gutschrift, S = Storno)
            $f_42 = substr($r,380,1);   // Buchungsgrund (- = normale Buchung, Q = Kulanz, B = Betrug, K = Kondition)
            $f_43 = substr($r,381,1);   // %
            $f_44 = substr($r,382,1);   // Informationsbeleg (0 = nein, 1 = ja)
            $f_45 = substr($r,383,1);   // Belegherkunft (D = Digitaler Beleg, normaler Dateneingang, M = Manueller Beleg über Belegerfassung ohne EXTERNEN Papierbeleg, A = Analoger Beleg mit EXT. Papierbeleg, K = Konditionsbeleg)
            $f_46 = substr($r,384,14);  // %
            
            // Further fields according to version DTF 6301
            $f_47 = cStr2Float(substr($r,398,13),4);  // Einzelpreis-Brutto in Lieferlandwährung mit vier Nachkommastellen
            $f_48 = cStr2Float(substr($r,411,13),4);  // Einzelpreis-Netto in Lieferlandwährung mit vier Nachkommastellen
            $f_49 = cStr2Date(substr($r,424,8));   // Rechnungsdatum
            $f_50 = substr($r,432,5);   // USt.-Prozentsatz informativ
            $f_51 = cStr2Float(substr($r,437,11));  // Betrag Nachlass in Lieferlandwährung USt. informativ
            $f_52 = cStr2Float(substr($r,448,11));  // Betrag Service in Lieferlandwährung USt. informativ
            $f_53 = cStr2Float(substr($r,459,11));  // Gesamtbetrag in Lieferlandwährung USt. informativ
            $f_54 = cStr2Date(substr($r,470,8));   // Fälligkeitsdatum
            $f_55 = substr($r,478,3);   // Zahlungsziel in Tagen
            $f_56 = substr($r,481,8);   // Zahlungsart
            $f_57 = substr($r,489,20);  // TC-Rechnungsnr. gegenüber dem Nutzer
            $f_58 = substr($r,509,8);   // TC-Rechnungsdatum
            $f_59 = substr($r,517,7);   // Zahlungszielgruppe
            $f_60 = substr($r,524,20);  // Kunden-Kostenstelle 2
            $f_61 = substr($r,544,6);   // Lieferzeit
            
            // Further fields according to version DTF 6303
            $f_62 = substr($r,550,20);  // Fremdkartennr.
            $f_63 = substr($r,570,20);  // OnBoardUnit-ID
            $f_64 = substr($r,590,16);  // KFZ-Kennzeichen komprimiert (wie $f_32 bzw. $f_39, nur ohne Leerzeichen und Bindestriche)
            $f_65 = substr($r,606,8);   // Kartenkategorie
            $f_66 = substr($r,614,13);  // Rechnungsnr. pro Lieferland
            $f_67 = substr($r,627,40);  // Autobahnauffahrt
            $f_68 = substr($r,667,40);  // Autobahnausfahrt
            $f_69 = substr($r,707,1);   // Rabattschlüssel der französischen Autobahngesellschaften
            $f_70 = substr($r,708,13);  // Nr. Belastungsanzeige
            $f_71 = substr($r,721,20);  // Betreuungsstelle
            $f_72 = substr($r,741,20);  // %
            $f_73 = substr($r,761,20);  // Akzeptanz-Medium
            
            // Further fields according to version DTF 6304
            $f_74 = substr($r,781,8);   // Steuerkategorie
            $f_75 = substr($r,789,11);  // UTA Beleg-ID
            $f_76 = substr($r,800,11);  // UTA Normbeleg-ID
            $f_77 = cStr2Date(substr($r,811,8));   // Gebühren-Bescheinigung von
            $f_78 = cStr2Date(substr($r,819,8));   // Gebühren-Bescheinigung bis
            $f_79 = substr($r,827,20);  // Herausgeber- / Nutzer-Nr.
            $f_80 = substr($r,847,8);   // Maßeinheit
            $f_81 = substr($r,855,3);   // Land der Akzeptanzstelle
            $f_82 = substr($r,858,7);   // PLZ der Akzeptanzstelle
            
            // Further fields according to version DTF 6305
            $f_83 = substr($r,865,3);   // steuerliches Leistungsland
            $f_84 = substr($r,868,5);   // steuerliches Produktgruppe
            $f_85 = substr($r,873,1);   // Leistungsort verlagerbar (0 = nein, 1 = ja)
            
            // $f_86 = substr($r,,);    // 
            // $f_87 = substr($r,,);    // 
            // $f_88 = substr($r,,);    // 
            // $f_89 = substr($r,,);    // 
            // $f_90 = substr($r,,);    // 

         elseif ($f_dataset_type == "90") :
            $f_numOfRows = substr($r,2,13);    // Anzahl Sätze inklusive Header und Trailer
            $f_checksum  = cStr2Float(substr($r,15,18));   // Checksumme über Feld "Gesamt Brutto in Darstellwährung" [Satzart 20] unter Berücksichtigung des Vorzeichens (Statusfeld Vorzeichen!!!)

         endif;

         // Generate output for visual check
         if ($f_dataset_type == "10" || $f_dataset_type == "20") :
            echo $f_01 . " - " . $f_02 . " - " . $f_03 . " - " . $f_04 . " - " . $f_05 . " - " . $f_06 . " - " . $f_07 . " - " . $f_08 . " - " . $f_09 . " - " . $f_10 . " - " . "<br>";
            echo $f_11 . " - " . $f_12 . " - " . $f_13 . " - " . $f_14 . " - " . $f_15 . " - " . $f_16 . " - " . $f_17 . " - " . $f_18 . " - " . $f_19 . " - " . $f_20 . " - " . "<br>";
            echo $f_21 . " - " . $f_22 . " - " . $f_23 . " - " . $f_24 . " - " . $f_25 . " - " . $f_26 . " - " . $f_27 . " - " . $f_28 . " - " . $f_29 . " - " . $f_30 . " - " . "<br>";
            echo $f_31 . " - " . $f_32 . " - " . $f_33 . " - " . $f_34 . " - " . $f_35 . " - " . $f_36 . " - " . $f_37 . " - " . $f_38 . " - " . $f_39 . " - " . $f_40 . " - " . "<br>";
            echo $f_41 . " - " . $f_42 . " - " . $f_43 . " - " . $f_44 . " - " . $f_45 . " - " . $f_46 . " - " . $f_47 . " - " . $f_48 . " - " . $f_49 . " - " . $f_50 . " - " . "<br>";
            echo $f_51 . " - " . $f_52 . " - " . $f_53 . " - " . $f_54 . " - " . $f_55 . " - " . $f_56 . " - " . $f_57 . " - " . $f_58 . " - " . $f_59 . " - " . $f_60 . " - " . "<br>";
            echo $f_61 . " - " . $f_62 . " - " . $f_63 . " - " . $f_64 . " - " . $f_65 . " - " . $f_66 . " - " . $f_67 . " - " . $f_68 . " - " . $f_69 . " - " . $f_70 . " - " . "<br>";
            echo $f_71 . " - " . $f_72 . " - " . $f_73 . " - " . $f_74 . " - " . $f_75 . " - " . $f_76 . " - " . $f_77 . " - " . $f_78 . " - " . $f_79 . " - " . $f_80 . " - " . "<br>";
            echo $f_81 . " - " . $f_82 . " - " . $f_83 . " - " . $f_84 . " - " . $f_85 . " - " . $f_86 . " - " . $f_87 . " - " . $f_88 . " - " . $f_89 . " - " . $f_90 . " - " . "<br>";
            echo "<br>";
         endif;
         
         
         // *** START DATABASE ACCESS ***
         if ($fire) :
            if ($f_dataset_type == "10") :
               insertStmt("phoenix_log.uta_header", array("utah_dataset_type", $f_dataset_type, "utah_consigner_id", $f_02, "utah_receiver_id", $f_03, "utah_date", $f_04, "utah_time", $f_06, "utah_account_no", $f_07, "utah_transfer_no", $f_08, "utah_dtf_format", $f_09, "utah_createtimestamp", $currentTime));
         
            elseif ($f_dataset_type == "20") :
               insertStmt("phoenix_log.uta", array("uta_01", $f_dataset_type, "uta_02", $f_02, "uta_03", $f_03, "uta_04", $f_04, "uta_05", $f_05, "uta_06", $f_06, "uta_07", $f_07, "uta_08", $f_08, "uta_09", $f_09, "uta_10", $f_10,
                                                   "uta_11", $f_11, "uta_12", $f_12, "uta_13", $f_13, "uta_14", $f_14, "uta_15", $f_15, "uta_16", $f_16, "uta_17", $f_17, "uta_18", $f_18, "uta_19", $f_19, "uta_20", $f_20,
                                                   "uta_21", $f_21, "uta_22", $f_22, "uta_23", $f_23, "uta_24", $f_24, "uta_25", $f_25, "uta_26", $f_26, "uta_27", $f_27, "uta_28", $f_28, "uta_29", $f_29, "uta_30", $f_30,
                                                   "uta_31", $f_31, "uta_32", $f_32, "uta_33", $f_33, "uta_34", $f_34, "uta_35", $f_35, "uta_36", $f_36, "uta_37", $f_37, "uta_38", $f_38, "uta_39", $f_39, "uta_40", $f_40,
                                                   "uta_41", $f_41, "uta_42", $f_42, "uta_43", $f_43, "uta_44", $f_44, "uta_45", $f_45, "uta_46", $f_46, "uta_47", $f_47, "uta_48", $f_48, "uta_49", $f_49, "uta_50", $f_50,
                                                   "uta_51", $f_51, "uta_52", $f_52, "uta_53", $f_53, "uta_54", $f_54, "uta_55", $f_55, "uta_56", $f_56, "uta_57", $f_57, "uta_58", $f_58, "uta_59", $f_59, "uta_60", $f_60,
                                                   "uta_61", $f_61, "uta_62", $f_62, "uta_63", $f_63, "uta_64", $f_64, "uta_65", $f_65, "uta_66", $f_66, "uta_67", $f_67, "uta_68", $f_68, "uta_69", $f_69, "uta_70", $f_70,
                                                   "uta_71", $f_71, "uta_72", $f_72, "uta_73", $f_73, "uta_74", $f_74, "uta_75", $f_75, "uta_76", $f_76, "uta_77", $f_77, "uta_78", $f_78, "uta_79", $f_79, "uta_80", $f_80,
                                                   "uta_81", $f_81, "uta_82", $f_82, "uta_83", $f_83, "uta_84", $f_84, "uta_85", $f_85, "uta_createtimestamp", $currentTime));
            endif;
         endif;
      
         $numOfNotEmptyRows++;
      endif;
   endfor;
   
   settype($f_checksum, 'string');
   settype($f_31_sum, 'string');
   
   if (!is_numeric($f_numOfRows) || $f_numOfRows != $numOfNotEmptyRows) :
      echo "Number of iterated rows (" . $numOfNotEmptyRows . ") do not match with tailer information (" . $f_numOfRows . ")!";
      echo "<br>";
   else : 
      echo "Number of iterated rows matches with tailer information (" . $f_numOfRows . "). OKAY!";
      echo "<br>";
   endif;
   if (!is_numeric($f_checksum)) :
      echo "The delivered checksum in tailer information is not a numeric value!";
      echo "<br>";
   else :
      if ($f_checksum != $f_31_sum) :
         echo "The delivered checksum " . $f_checksum . " in tailer information does not equal the brutto sum of " . $f_31_sum;
         echo "<br>";
      else :
         echo "The delivered checksum " . $f_checksum . " in tailer information equal the brutto sum! OKAY!";
         echo "<br>";
      endif;
   endif;

else :
   echo "Importfile doeas not exist!";
endif;

?>