<?
if ($auszugid) {
    $auszug = new auszugversand($auszugid);
    $filename = $auszug->auszugname;
    $versand = $auszug->versand;
    $nobalance = $auszug->nobalance;
    $agechange = $auszug->wechsler;
    $u18 = $auszug->u18;
    global $exportpath;
    $outputname = $exportpath."/kontoauszug/".$filename;
    print "Datum: " . $auszug->datum . "<br/>";
    print "Name: " . $filename . "<br/>";
    print "Versand: " . $versand . "<br/>";

    if ($auszug->versand < 2) {
        /** <timestamp>_recievers.csv **/
        if (preg_match('/(^[0-9]*)_/', $filename, $matches)) {
            $receiversfile = $exportpath . '/kontoauszug/' . $matches[1] . '_receivers.csv';
            if (is_file($receiversfile)) {
                print "Loesche $receiversfile<br/>\n";
                unlink($receiversfile);
            }
        }
        if (is_file($outputname)) {
            print "Loesche Datei " . $outputname. "<br/>";
            unlink($outputname);
        }
    } else {
        if (is_dir($outputname)) {
            print "Loesche Verzeichnis " . $outputname. "<br/>";
            rrmdir($outputname);
        }
    }



    /** loesche csv kontrollfile
     */
    $group = $u18?'U18':'O18';
    $group = $agechange?'Wechsler':$group;
    if ($versand == 0) { // Kein Letter
        $type = 'noletter';
    }
    if ($versand == 1) { // Brief
        $type = 'letter';
    }
    if ($versand == 2) { // Email
        $type = 'email';
    }
    $balance = '';
    if ($nobalance) {
        $balance= 'KeinAuszug';
    }
    $logfile = $group . '_' . $type . '.csv';
    if ($balance != '') {
        $logfile = $group . '_' . $type . '_' . $balance . '.csv';
    }
#    print "Loesche $logfile<br/>\n";

    /** Loesche DB-Eintrag **/
    $auszug->delete();
}

function rrmdir($dir) {
    if (is_dir($dir)) {
        $mydir = opendir($dir);
        $objects = array();
        while ($entry = readdir($mydir)) {
            array_push($objects,$entry);
        }
        # $objects = scandir($dir);
        closedir($mydir);
        foreach ($objects as $object) {
            if ($object != "." && $object != "..") {
                if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
            }
        }
        reset($objects);
        rmdir($dir);
    }
} 
?>
