Subversion-Projekte lars-tiefland.content-management

Revision

Blame | Letzte Änderung | Log anzeigen | RSS feed

<?php

    //$Id: bezahlstatus.php 3993 2011-10-13 08:02:38Z erling $

    /**
     * @author Malte Erling <erling@weban.de>
     * @copyright 2011 Webagentur Niewerth
     * @package Content-management
     * @version $Rev: 3993 $
     * @license propietary
     * @filesource
     * 
     */

    /**
     * 
     * @author Malte Erling <erling@weban.de>
     * @copyright 2011 Webagentur Niewerth
     * @package Content-management
     */

    require_once 'Weban_Smarty.class.php';
    require_once '../Online-Shop/connect2.php';

    setlocale( LC_MONETARY, "de_DE" );
    $cfg = array();

    // In welchem ID-Bereich liegen die Konfig-Vorlagen?
    $start = 280;
    $end = 290;

    $GLOBALS["ui"] = new Weban_Smarty();
    $GLOBALS["ui"]->compile_dir = $_SERVER["DOCUMENT_ROOT"] . "/templates_c/";
    $GLOBALS["ui"]->compile_id = "Warenwirtschaft|bezahlstatus";
    $GLOBALS["ui"]->use_sub_dirs = true;

    $GLOBALS["ui"]->assign( "config_ready", 0 );

    if ( $_GET['shops_ID'] >= 0 )
    {
        $_SESSION['shops_ID'] = $_GET['shops_ID'];
    }

    if ( $_GET['config'] )
    {
        // die übergebene shops_ID ermitteln
        $expl = explode( "###", $_GET['config'] );

        $cfg_ID = $expl[0];
        $shops_ID = $expl[1];
        $cfg_name = $expl[2];

        $GLOBALS["ui"]->assign( "cfg_sel", $_GET['config'] );
        $cfg = getConfig( $cfg_ID );

    }


    // SelectBox für Konfiguration
    $configs = getConfigs();
    $GLOBALS["ui"]->assign( "configs", $configs );


    $bezahlstatus = isset( $user_rechte["Warenwirtschaft"]["bezahlstatus"]["bezahlstatus"] );

    if ( !$bezahlstatus )
    {
        $GLOBALS["ui"]->assign( "meld", "Sie haben nicht das Recht, dieses Tool zu nutzen!" );
        $GLOBALS["ui"]->assign( "error", true );
        $GLOBALS["ui"]->display( "meld.tpl" );
    }
    else
    {
        switch ( $_POST["mode"] )
        {
            default:
                getProtocols();
                break;
            case "save":
                saveBestellungenZuordnung( $_POST['row'] );
                break;
            case "import":
                $error = false;
                if ( $_FILES["datei"] )
                {
                    $tmp_name = $_FILES["datei"]["tmp_name"];
                    $data = csv_import( $tmp_name );
                    if ( !$data )
                    {
                        $error = true;
                        $GLOBALS["ui"]->assign( "meld",
                            "Leider ist beim Import ein Fehler aufgetreten!<br>Die Technik ist bereits informiert und wird sich um das Problem k&uuml;mmern.<br>Bitte versuchen Sie es sp&auml;ter noch einmal." );

                    }
                    else
                    {
                        $GLOBALS["ui"]->assign( "meld", "Die Datei wurde erfolgreich importiert!" );
                    }
                    $GLOBALS["ui"]->assign( "error", $error );
                    $GLOBALS["ui"]->display( "meld.tpl" );
                }
                $GLOBALS["ui"]->assign( "data", $data );
                break;
        }
        $GLOBALS["ui"]->display( "bezahlstatus.tpl" );
    }


    function csv_import( $fName )
    {
        global $site;
        global $cfg;
        $data = array();
        if ( count($cfg) == 0 )
        {
            die( "<p style=\"padding:20px;\">Sie haben keine g&uuml;ltige Konfiguration gew&auml;hlt..</p>" );
        }
        $rows = file( $fName );
        $i = 0; // der Row-Counter
        $felder = array();
        $cnt = count( $rows );
        foreach ( $rows as $row_id => $row )
        {

            if ( $cfg['ignore_lastrows'] && $row_id == $cnt - $cfg['ignore_lastrows'] )
            {
                break;
            }
            if ( $row_id >= $cfg['row_start'] )
            {

                $felder[$i] = explode( $cfg['trenner'], $row );

                $datum = $felder[$i][$cfg['datum']];
                $ueberw_betrag = str_replace( ".", "", $felder[$i][$cfg['haben']] );
                $ueberw_betrag = str_replace( ",", ".", $ueberw_betrag );
                $text = str_replace( "\"", "", $felder[$i][$cfg['text']] );
                if ( $cfg['Name'] )
                {
                    $name = $felder[$i][$cfg['name']];
                }
                $best = getBest( $ueberw_betrag );


                preg_match_all( "/\b([0-9]*)\b/", $text, $matches );
                $zahlen = $matches[0];


                $data[$i]['datum'] = $datum;
                $data[$i]['ueberw_betrag'] = $ueberw_betrag;
                $data[$i]['text'] = $text;
                if ( $name )
                {
                    $data[$i]['name'] = $name;
                }
                // alle gültigen Bestellungen durchlaufen
                $b = 0;
                $hr = 0;
                foreach ( $best as $key => $bestellung )
                {

                    $data[$i]['best'][$b]['rank'] = 1;
                    $data[$i]['best'][$b]['bestID'] = $bestellung['ID'];
                    $data[$i]['best'][$b]['erstellt_am'] = date( "d.m.Y", strtotime($bestellung['erstellt_am']) );
                    $data[$i]['best'][$b]['ueberw_betrag'] = $bestellung['gesamtsumme_buffer'];
                    $data[$i]['best'][$b]['name'] = trim( $bestellung['Anrede'] . " " . $bestellung['Vorname'] .
                        " " . $bestellung['Name1'] . " " . $bestellung['Name2'] . " " . $bestellung['Firma'] );


                    // es wurden Zahlen im Text gefunden, die mit der BestID verglichen werden müssen
                    if ( is_array($zahlen) )
                    {

                        foreach ( $zahlen as $zahl )
                        {
                            // Bei Übereinstimmung wird der Rank hochgesetzt.
                            if ( ($zahl == $bestellung['ID']) )
                            {
                                $data[$i]['best'][$b]['rank']++;

                            }
                        }
                    }
                    if ( $hr < $data[$i]['best'][$b]['rank'] )
                    {
                        $hr = $data[$i]['best'][$b]['rank'];
                        $data[$i]['hr'] = $hr;
                    }

                    $b++;
                }

                $i++;
            }

        }

        return $data;
    }


    //SELECT *  FROM $tabelle AS k WHERE k.datum < date_add(current_date, interval -30 day)"
    function getBest( $ueberw_betrag )
    {
        global $cfg;
        global $shops_ID;
        $best = array();

        $wh = "";
        if ( $shops_ID > 0 )
        {
            $wh = " and B.shops_ID = " . $shops_ID . "";
        }
        $sql = "select
                    B.*,
                    L.Anrede,
                    L.Vorname,
                    L.Name1,
                    L.Name2,
                    L.Firma
                from 
                    Bestellung B

                left join 
                    Logins L
                on 
                    L.ID = B.Login
                    
                where 
                    B.erstellt_am > DATE_ADD( CURDATE( ) , INTERVAL -" . $cfg['days'] .
            " DAY )
                and
                    B.bezahlt != 1
                and 
                    B.gesamtsumme_buffer = " . $ueberw_betrag . "
                
                " . $wh . "
                
                order by
                    B.erstellt_am desc
                 
        ";
        if ( $q = mysql_query($sql) )
        {
            while ( $r = mysql_fetch_assoc($q) )
            {
                $best[] = $r;
            }
        }

        return $best;
    }


    function getAllShops()
    {
        $shops[0] = "Bitte w&auml;hlen";
        $sql = "select ID,Name from shops order by ID";
        if ( $q = mysql_query($sql) )
        {
            while ( $r = mysql_fetch_assoc($q) )
            {
                $shops[$r['ID']] = $r['Name'];
            }
        }
        return $shops;
    }


    function getConfigs()
    {
        global $start;
        global $end;


        $configs = array();
        $configs[-1] = "Bitte w&auml;hlen";
        $sql = "select 
                    * 
                from 
                    web_settings 
                where 
                    ID >= " . $start . " 
                and 
                    ID <= " . $end . " 
                order by 
                    ID";
        if ( $q = mysql_query($sql) )
        {
            while ( $r = mysql_fetch_assoc($q) )
            {
                $name = strtolower( str_replace(" ", "_", $r['Name']) );
                $configs[$r['ID'] . "###" . $r['shops_ID'] . "###" . $name] = $r['Name'];
            }
        }
        return $configs;
    }


    function getConfig( $id )
    {
        global $shops_ID;
        $cfg = "";

        $wh = "";
        if ( $shops_ID > 0 )
        {
            $wh = " and shops_ID = " . $shops_ID . "";
        }

        $sql = "select 
                    Inhalt 
                from 
                    web_settings 
                where 
                    ID = " . $id . " 
                " . $wh . "
        ";

        if ( $q = mysql_query($sql) )
        {
            if ( $r = mysql_fetch_assoc($q) )
            {
                $str = "<?php\n" . $r['Inhalt'] . " \n?>";
                // String in Datei schreiben
                $fname = "/tmp/" . md5( $str ) . ".php";
                if ( file_put_contents($fname, $str) )
                {
                    require_once ( $fname );
                    if ( $cfg )
                    { // Der Array aus der Datei
                        if ( unlink($fname) )
                        { // Datei wieder löschen
                            $GLOBALS["ui"]->assign( "config_ready", 1 );
                        }
                    }
                }
            }
        }
        return $cfg;
    }


    function saveBestellungenZuordnung( $row )
    {
        global $cfg_name;
        $all = 0;
        $save = 0;
        // den Text für das Bemerkungsfeld erstellen
        foreach ( $row as $cnt => $data )
        {
            $all++;
            $expl = explode( "###", $data['bestellung'] );
            $bid = $expl[0];
            $name = $expl[1];
            // ist die Checkbox aktiviert gewesen?
            if ( $data['chk'] == "on" )
            {
                if ( $data['bestellung'] != "none" )
                {
                    $str = utf8_decode( "Überweisung vom " ) . $data['datum'] . utf8_decode( ". Text: " ) .
                        $data['text'] . ". Zugewiesen durch " . $_SERVER['PHP_AUTH_USER'] . " am " .
                        date( "d.m.Y H:i", time() ) . ".\n";

                    $sql = "update
                                Bestellung
                            set
                              Bemerkung = concat_ws('', Bemerkung ,'" . $str .
                        "'),
                              bezahlt = 1
                            where
                                ID = " . $bid . " 
                    ";

                    $q = mysql_query( $sql );
                    $save++;
        
                    // Protokollierung für erfolgreiche Zurodnung
                    if ( isset($bid) and $bid != "none" )
                    {
                        $prot['#0f0'][] = 
                            $data['datum'] . " - " . 
                            "BestNr.: " . $bid . 
                            " - Betrag: " . $data['betrag']." - ". $str;
                    }
                }
            }
            // Protokollierung für nicht erfolgreiche Zurodnung
            if ( $bid == "none" )
            {
                $prot['#fc0'][] = $data['datum'] . " - Betrag: ".$data['betrag']. " - "  . $data['text'] .
                    " - Keine Zuordnung ausgew&auml;hlt.";
            }
            if ( $bid == "" )
            {
                $prot['#f00'][] = $data['datum'] . " - Betrag: ".$data['betrag']. " - " . $data['text'] .
                    " - Keine Zuordnung m&ouml;glich.";
            }

        }
        $html = "<html><head><style>body {font-family:verdana,arial,sans-serif;font-size:0.7em;}table td {vertical-align:top;}</style></head><body>\n";
        $html .= "<h2>Zuordnungen erstellt von " . $_SERVER['PHP_AUTH_USER'] .
            " am " . date( "d.m.Y H:i", time() ) . "</h2>";
        $html .= $save . " Zuordnungen von " . $all;
        $html .= "<table cellpadding=\"2\" cellspacing=\"0\" border=\"1\">\n<br />";

        $i = 1;
        foreach ( $prot as $color => $val )
        {

            if ( is_array($val) )
            {

                foreach ( $val as $key => $v )
                {
                    $html .= "<tr style=\"background-color:" . $color . ";\">";
                    $html .= "<td style=\"text-align:right;\">" . $i . "</td>";
                    $html .= "<td>" . $v . "</td>";
                    
                    $html .= "</tr>\n";
                    $i++;
                }
            }

        }
        $html .= "</table>\n";
        $html .= "<br />" . $save . " Zuordnungen von " . $all;
        $html .= "</body></html>\n";
        $fname = $cfg_name . "_" . $GLOBALS['site'] . "_" . date( "YmdHi", time() ) .
            ".html";
        file_put_contents( "protokoll/" . $fname, $html );
        echo "<br>Es wurden " . $save . " Zuordnungen gespeichert.";

        return true;
    }


    function getProtocols()
    {
        $protocols = "";
        if ( $handle = opendir('protokoll') )
        {
            while ( false !== ($file = readdir($handle)) )
            {
                if ( $file != "." && $file != ".." )
                {
                    if ( stristr($file, $GLOBALS['site']) )
                    {
                        $protocols .= "<a href=\"protokoll/" . $file . "\" target=\"_blank\">" . $file .
                            "</a><br />\n";
                    }

                }
            }
            closedir( $handle );
        }
        $GLOBALS["ui"]->assign( "protocols", $protocols );

    }






?>