<?php
include("kurs/datenbankanbindung.php"); // f&uuml;gt die Datenbankanbindung ein: Sys:\php\includes\kurs\datenbankanbindung.php
$user_admin=$_COOKIE["user_admin"];
$uid=$_GET["uid"];
$hist=$_GET["hist"];
#header('Content-Type: text/plain; charset=windows-1252'); 

#$saaid = "1234|4567|1478|2584|99988";
#$saaid=substr($saaid, 0, -1); // Letztes Zeichen (das |) entfernen
#$saaidx = preg_split("/\|/", $saaid);
#$uid='schwaral';


$db = dbconnect();
if(!isset($hist)){
$queryb = $db->query ("SELECT count(distinct qid) Anz
                          FROM stan_printqueue_antrag a, stan_antrag b, stud c
                         WHERE (a.saaid=b.saaid or a.uid=b.uid)
                           AND b.uid=c.uid
                           AND b.uid='$uid'
                           AND druckstatus ='N'");
$row_cur_uid_anz = $queryb->fetch_array();
}else{
$queryb = $db->query ("SELECT count(distinct qid) Anz
                          FROM stan_printqueue_hist a, stan_antrag b, stud c
                         WHERE (a.saaid=b.saaid or a.uid=b.uid)
                           AND b.uid=c.uid
                           AND b.uid='$uid'
                           AND druckstatus ='N'");
$row_cur_uid_anz = $queryb->fetch_array();
}



if($row_cur_uid_anz['Anz'] > 0){

  define('FPDF_FONTPATH','font/');
  require('prints/as_utf_class.php');
  require_once("prints/table_def_kehl.inc");
  
  $pdf= new utfFPDF();
  
  
  
  
  $pagecount = $pdf->setSourceFile("anschreiben_kehl/blanko_kehl.pdf");
  #$pagecount = $pdf->setSourceFile("anschreiben_kehl/zuweisung_student.pdf");
  
  $tplidx = $pdf->ImportPage(1);
  
  
  # Briefe an den Student werden nur einmal gedruckt. Dort sind alle Praxisstellen darauf
  $lnzaehler1 = 0;
  $lnzaehler2 = 0;
  $lnzaehler3 = 0;
  
  
  $db = dbconnect();
  if(!isset($hist)){
  $querya = "SELECT a.saaid, b.uid, a.zuweisung, vorname, nachname,a.qid
              FROM stan_printqueue_antrag a, stan_antrag b, stud c
             WHERE a.saaid=b.saaid
               AND b.uid=c.uid
               AND b.uid='$uid'
               AND druckstatus ='N'
               AND a.zuweisung != 'SOZ'
             ORDER BY uid,sort_order";
  $resulta = $db->query ($querya)
     or die ("Cannot execute query1a");
  }else{

  $querya = "SELECT a.saaid, b.uid, a.zuweisung, vorname, nachname,a.qid
              FROM stan_printqueue_hist a, stan_antrag b, stud c
             WHERE a.saaid=b.saaid
               AND b.uid=c.uid
               AND b.uid='$uid'
               AND druckstatus ='N'
               AND a.zuweisung != 'SOZ'
             ORDER BY uid,sort_order";
  $resulta = $db->query ($querya)
     or die ("Cannot execute query2");
  }   

  
  while ($row_cur_uid = $resulta->fetch_array())
  {

  $resultakt = $db->query("SELECT saaid, aendid, status, zuweisung, uid
                              FROM stan_antrag
                             WHERE saaid ='$row_cur_uid[saaid]'
                          ");
  $row_akt = $resultakt->fetch_array();  // Prüfen ob schon Änderungsanträge vorhanden
  
  
  
  $res_alt = $db->query("SELECT saaid, aendid, status, zuweisung
                            FROM stan_antrag
                           WHERE saaid<'$row_cur_uid[saaid]'
                             AND aendid='$row_akt[aendid]'
                             AND zuweisung!='NA'
                           ORDER BY saaid DESC
                           LIMIT 1");
  $row_alt = $res_alt->fetch_array();
  

  
    if($lnzaehler1 < 1 AND $row_cur_uid['zuweisung'] == 'EZS'){
      $pdf->addPage();
      $pdf->useTemplate($tplidx,0,0,0);
      $pdf->SetAutoPageBreak(true, 10);
      
      require("anschreiben_kehl/zuweisung_erstantrag_student.php");
      $lnzaehler1 =1;
    }
  
    if($row_cur_uid['zuweisung'] == 'EZP'){
     $pdf->addPage();
      $pdf->useTemplate($tplidx,0,0,0);
      $pdf->SetAutoPageBreak(true, 10);
      
      require("anschreiben_kehl/zuweisung_erstantrag_praxisstelle.php");
    }
    
    if($lnzaehler2 < 1 AND $row_cur_uid['zuweisung'] == 'AES'){
      $pdf->addPage();
      $pdf->useTemplate($tplidx,0,0,0);
      $pdf->SetAutoPageBreak(true, 10);
      
      require("anschreiben_kehl/zuweisung_aenderung_student.php");
      $lnzaehler2 =1;
    }
  
  
    if($row_cur_uid['zuweisung'] == 'AEP'){
      $pdf->addPage();
      $pdf->useTemplate($tplidx,0,0,0);
      $pdf->SetAutoPageBreak(true, 10);
      
      require("anschreiben_kehl/zuweisung_aenderung_praxisstelle.php");
    }
  

  
      $datum = date("Y-m-d H:i:s",time());
    
      if(!isset($hist)){
         $sql_queue = $db->query("UPDATE stan_printqueue_antrag 
                                    SET druckstatus='G'
                                      , datum='$datum'
                                      , dokument ='drucke_mailversand/zuweisung_$uid.pdf'
                                      , versandart='B'
                                  WHERE qid='$row_cur_uid[qid]'");
      }
       
  
  }

 # Standortzuweisung
  if(!isset($hist)){
  $querya = "SELECT a.block, c.UID, a.zuweisung, vorname, nachname,a.qid
              FROM stan_printqueue_antrag a,  stud c
             WHERE a.uid=c.uid
               AND c.uid='$uid'
               AND druckstatus ='N'
               AND a.zuweisung = 'SOZ'
             ORDER BY uid,sort_order";
  $resulta = $db->query ($querya)
     or die ("Cannot execute query3");
  }else{

  $querya = "SELECT a.block, c.UID, a.zuweisung, vorname, nachname,a.qid
              FROM stan_printqueue_hist a, stud c
             WHERE a.uid=c.uid
               AND c.uid='$uid'
               AND druckstatus ='N'
               AND a.zuweisung = 'SOZ'
             ORDER BY uid,sort_order";
  $resulta = $db->query ($querya)
     or die ("Cannot execute query4");

  }     
   
   
   while ($row_cur_uid2 = $resulta->fetch_array())
   {
   
     if($lnzaehler3 < 1 AND $row_cur_uid2['zuweisung'] == 'SOZ'){
       $pdf->addPage();
       $pdf->useTemplate($tplidx,0,0,0);
       $pdf->SetAutoPageBreak(true, 10);
       require("anschreiben_kehl/zuweisung_ag_standort.php");
       $lnzaehler3 = 1;
    }
  
       $datum = date("Y-m-d H:i:s",time());
     
       if(!isset($hist)){
         $sql_queue = $db->query("UPDATE stan_printqueue_antrag 
                                    SET druckstatus='G'
                                      , datum='$datum'
                                      , dokument ='drucke_mailversand/zuweisung_$uid.pdf'
                                      , versandart='B'
                                  WHERE qid='$row_cur_uid2[qid]'");
       }
        
   
   }
 
     if(isset($hist)){
       $sql_queue = $db->query("DELETE FROM stan_printqueue_hist WHERE admin='$user_admin'");
     }
 
 
 #exit;
 
 # Für Historie auf Platte schreiben
 if(!isset($hist)){
   $pdf->Output("drucke_mailversand/zuweisung_".$uid.".pdf","F");
 }
 $pdf->Output("zuweisung_$uid.pdf","i");
 
 $pdf->closeParsers();
} //Endif($row_cur_uid_anz[Anz] > 0){
?>