<?php
    require_once("../sepa_xml_creator/SepaXmlCreator.php");
    include("../class/dB.class.php");
    
    session_start();

    if ( ($_SESSION['auth'] != true) || (($_SESSION['authId'] != 1) && ($_SESSION['authId'] != 2)) )
    {
      $_SESSION['auth'] = false;
      header("Location: ../login.php");
    }

    $mydb= new DB_MySQL('zeltlager','zeltlager','Z3ltlager3363','localhost');
    
    //JAhreszahl
    $mydb->query("SELECT * FROM VerwaltungLagerplatz ORDER BY jahr DESC limit 1");
    $row = $mydb->fetchRow();
    $year = $row['jahr'];
    
    //Lagerbeiträge
    $mydb->query("SELECT * FROM AnmeldungBankdaten AS b LEFT JOIN AnmeldungAdressen AS a  ON b.uid = a.uid  LEFT JOIN AnmeldungStatus AS s ON b.uid = s.uid where berechneter_lagerbeitrag > 0 and status_beitrag = 0 and s.bestaetigt = 1");
    $creator = new SepaXmlCreator(); 
    $creator->setAccountValues('EV GEMEINDEJUGEND MEMMINGEN', 'DE53731500000810200550', 'BYLADEM1MLM');
    $creator->setGlaeubigerId("DE50ZZZ00001016793");
    $creator->setAusfuehrungOffset(2);
    $createfile=0;
    
    while ($row = $mydb->fetchRow())
    {
        $buchung = new SepaBuchung();
        $buchung->setBetrag($row['berechneter_lagerbeitrag']);
        $buchung->setEnd2End('NOTPROVIDED');
        $buchung->setBic($row['bic']);
        $buchung->setName($row['kontoinhaber']);
        $buchung->setIban($row['iban']);
        $buchung->setVerwendungszweck('Evang. Gemeindejugend Memmingen Lagerbeitrag ' . $year.' '. $row['nachname'].', '. $row['vorname'] );
        $buchung->setMandat($row['mandatsreferenz_beitrag'], date("Y-m-d",  strtotime($row['datum'])), false);
        $creator->addBuchung($buchung);
        $createfile=1;
    }
     
    if ($createfile == 1)
    { 
      $sepaxml = $creator->generateBasislastschriftXml();
  	 file_put_contents('../sepa_listen/Sepalastschrift-Lagerbeitrag-'. $year.'.xml', $sepaxml);
    }
  	//$creator->validateBasislastschriftXml('Sepalastschrift-Lagerbeitrag-'. $year.'.xml');
    //$creator->printXmlErrors();
    
     //Spende
    $mydb->query("SELECT * FROM AnmeldungBankdaten AS b LEFT JOIN AnmeldungAdressen AS a  ON b.uid = a.uid  LEFT JOIN AnmeldungStatus AS s ON b.uid = s.uid WHERE spende > 0 and status_spende = 0 and s.bestaetigt = 1");
    $creator = new SepaXmlCreator(); 
    $creator->setAccountValues('EV GEMEINDEJUGEND MEMMINGEN', 'DE53731500000810200550', 'BYLADEM1MLM');
    $creator->setGlaeubigerId("DE50ZZZ00001016793");
    $creator->setAusfuehrungOffset(2);
    $createfile=0;
    
    while ($row = $mydb->fetchRow())
    {
        $buchung = new SepaBuchung();
        $buchung->setBetrag($row['spende']);
        $buchung->setEnd2End('NOTPROVIDED');
        $buchung->setBic($row['bic']);
        $buchung->setName($row['kontoinhaber']);
        $buchung->setIban($row['iban']);
        $buchung->setVerwendungszweck('Evang. Gemeindejugend Memmingen Spende ' . $year.' '. $row['nachname'].', '. $row['vorname'] );
        $buchung->setMandat($row['mandatsreferenz_spende'], date("Y-m-d",  strtotime($row['datum'])), false);
        $creator->addBuchung($buchung);
        $createfile=1;
     }
    
    if ($createfile == 1)
    { 
     $sepaxml = $creator->generateBasislastschriftXml();
  	 file_put_contents('../sepa_listen/Sepalastschrift-Spende-'. $year.'.xml', $sepaxml);
    }
    
     //Aufkleber
    $mydb->query("SELECT * FROM AnmeldungBankdaten AS b LEFT JOIN AnmeldungAdressen AS a  ON b.uid = a.uid  LEFT JOIN AnmeldungStatus AS s ON b.uid = s.uid WHERE aufkleber > 0 and s.bestaetigt = 1");
    $creator = new SepaXmlCreator(); 
    $creator->setAccountValues('EV GEMEINDEJUGEND MEMMINGEN', 'DE53731500000810200550', 'BYLADEM1MLM');
    $creator->setGlaeubigerId("DE50ZZZ00001016793");
    $creator->setAusfuehrungOffset(2);
    $createfile=0;
    
    while ($row = $mydb->fetchRow())
    {
        $buchung = new SepaBuchung();
        $buchung->setBetrag(5);
        $buchung->setEnd2End('NOTPROVIDED');
        $buchung->setBic($row['bic']);
        $buchung->setName($row['kontoinhaber']);
        $buchung->setIban($row['iban']);
        $buchung->setVerwendungszweck('Evang. Gemeindejugend Memmingen Aufkleber ' . $year.' '. $row['nachname'].', '. $row['vorname'] );
        $buchung->setMandat($row['mandatsreferenz_aufkleber'], date("Y-m-d",  strtotime($row['datum'])), false);
        $creator->addBuchung($buchung);
        $createfile=1;
     }
     
    if ($createfile == 1)
    {
      $sepaxml = $creator->generateBasislastschriftXml();
  	 file_put_contents('../sepa_listen/Sepalastschrift-Aufkleber-'. $year.'.xml', $sepaxml);
  	}
    
    //prepay
    $mydb->query("SELECT * FROM AnmeldungBankdaten AS b LEFT JOIN AnmeldungAdressen AS a  ON b.uid = a.uid  LEFT JOIN AnmeldungStatus AS s ON b.uid = s.uid WHERE kaufladen > 0 and status_prepay = 0 and s.bestaetigt = 1");
    $creator = new SepaXmlCreator(); 
    $creator->setAccountValues('EV GEMEINDEJUGEND MEMMINGEN', 'DE53731500000810200550', 'BYLADEM1MLM');
    $creator->setGlaeubigerId("DE50ZZZ00001016793");
    $creator->setAusfuehrungOffset(2);
    $createfile=0;
    
    while ($row = $mydb->fetchRow())
    {
        $buchung = new SepaBuchung();
        $buchung->setBetrag($row['kaufladen']);
        $buchung->setEnd2End('NOTPROVIDED');
        $buchung->setBic($row['bic']);
        $buchung->setName($row['kontoinhaber']);
        $buchung->setIban($row['iban']);
        $buchung->setVerwendungszweck('Evang. Gemeindejugend Memmingen PrePay ' . $year.' '. $row['nachname'].', '. $row['vorname'] );
        $buchung->setMandat($row['mandatsreferenz_prepay'], date("Y-m-d",  strtotime($row['datum'])), false);
        $creator->addBuchung($buchung);
        $createfile=1;
     }
     
    if ($createfile == 1)
    {
      $sepaxml = $creator->generateBasislastschriftXml();
  	 file_put_contents('../sepa_listen/Sepalastschrift-PrePay-'. $year.'.xml', $sepaxml);
  	}
    
    
     //Jugendpullies
    $mydb->query("SELECT * FROM AnmeldungBankdaten AS b LEFT JOIN AnmeldungAdressen AS a  ON b.uid = a.uid  LEFT JOIN AnmeldungStatus AS s ON b.uid = s.uid WHERE jugendpullover > 0 and s.bestaetigt = 1");
    $creator = new SepaXmlCreator(); 
    $creator->setAccountValues('EV GEMEINDEJUGEND MEMMINGEN', 'DE53731500000810200550', 'BYLADEM1MLM');
    $creator->setGlaeubigerId("DE50ZZZ00001016793");
    $creator->setAusfuehrungOffset(2);
    $createfile=0;
    
    while ($row = $mydb->fetchRow())
    {
        $buchung = new SepaBuchung();
        $buchung->setBetrag($row['jugendpullover']);
        $buchung->setEnd2End('NOTPROVIDED');
        $buchung->setBic($row['bic']);
        $buchung->setName($row['kontoinhaber']);
        $buchung->setIban($row['iban']);
        $buchung->setVerwendungszweck('Evang. Gemeindejugend Memmingen Jugendpullover ' . $year.' '. $row['nachname'].', '. $row['vorname'] );
        $buchung->setMandat($row['mandatsreferenz_jugendpullover'], date("Y-m-d",  strtotime($row['datum'])), false);
        $creator->addBuchung($buchung);
        $createfile=1;
     }
     
    if ($createfile == 1)
    {
      $sepaxml = $creator->generateBasislastschriftXml();
  	 file_put_contents('../sepa_listen/Sepalastschrift-Jugendpullover-'. $year.'.xml', $sepaxml);
  	}
    
    $mydb->disconnect();
    
    header('Location: ../index.php?pageId=11');

?>
