<?php

include("kurs/datenbankanbindung.php"); // f&uuml;gt die Datenbankanbindung ein: Sys:\php\includes\kurs\datenbankanbindung.php

$user_admin=$_COOKIE["user_admin"];

$jahrgang=$_COOKIE["jahrgang"];

$hs=$_COOKIE["ck_hs"];



define('FPDF_FONTPATH','font/');

require('prints/as_utf_class.php');

require_once("prints/table_def.inc");



$pdf= new utfFPDF();



$pagecount = $pdf->setSourceFile("anschreiben/blanko_ludwigsburg.pdf");

#$pagecount = $pdf->setSourceFile("anschreiben/aenderung_praxisstelle.pdf");



$tplidx = $pdf->ImportPage(1);



$db = dbconnect();

$query_uid = "SELECT distinct c.uid

                FROM stan_printqueue_antrag a, stan_antrag b, stud c

               WHERE a.saaid=b.saaid

                 AND b.uid=c.uid

                 AND druckstatus ='N'

                 AND c.hs = '$hs'

               ORDER BY c.uid";

$result_uid = $db->query ($query_uid)

   or die ("Cannot execute query2");





while ($row_akt_stud = $result_uid->fetch_array())

{



$uid = $row_akt_stud[uid];



$db = dbconnect();



$queryb = $db->query ("SELECT count(a.saaid) Anz

                          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'");

$row_cur_uid_anz = $queryb->fetch_array();



#echo"$uid --> $row_cur_uid_anz[Anz]<br>";



if($row_cur_uid_anz[Anz] > 0){







# Briefe an den Student werden nur einmal gedruckt. Dort sind alle Praxisstellen darauf

$lnzaehler1 = 0;

$lnzaehler2 = 0;





$db = dbconnect();



$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'

           ORDER BY uid,sort_order";

$resulta = $db->query ($querya)

   or die ("Cannot execute query1");





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/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/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/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/zuweisung_aenderung_praxisstelle.php");

  }



    $datum = date("Y-m-d H:i:s",time());

    

    $sql_queue = $db->query("UPDATE stan_printqueue_antrag SET druckstatus='G', datum='$datum' WHERE qid='$row_cur_uid[qid]'");

#    echo "UPDATE stan_printqueue_antrag SET druckstatus='G', datum='$datum' WHERE qid='$row_cur_uid[qid]'<br>";

    

}

} //Endif($row_cur_uid_anz[Anz] > 0){

}





$pdf->Output("zuweisung_ges.pdf","i");

$pdf->closeParsers();



?>