Subversion-Projekte lars-tiefland.content-management

Revision

Zur aktuellen Revision | Blame | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

<?php
    //$Id: rechnungsbuch.php 3931 2011-09-16 08:17:57Z tiefland $

    /**
     * @author Patirck Lautsch <lautsch@weban.de>
     * @copyright 2010 Webagentur Niewerth
     * @package Content-management
     * @version $Rev: 3931 $
     * @license propietary
     * @filesource
     * 
     */

    /**
     * 
     * @author Patirck Lautsch <lautsch@weban.de>
     * @copyright 2010 Webagentur Niewerth
     * @package Content-management
     */
    // deutsche Umgebung
    setlocale( LC_ALL, "de_DE" );
    ////////////////////

    // Connect-Datei einbinden
    define( "modul_name", "Warenwirtschaft" );
    define( "tool_name", "bestellung" );
    define( "F_NAME", basename( $_SERVER["PHP_SELF"] ) );
    require_once  "../Online-Shop/connect2.php";
    require_once "module/functions.common.php";
    //////////////////////////

    // Start der Session
    session_start();
    ////////////////////

    // Smarty Initalisierung
    require_once  "Weban_Smarty.class.php";
    require_once "includes/rechnung/rechnungsbuch.function.php";
    $GLOBALS["ui"] = new Weban_Smarty();
    $GLOBALS["ui"]->conifg_dir = SMARTY_DIR;
    $GLOBALS["ui"]->template_dir = "templates/";
    $GLOBALS["ui"]->compile_dir = $_SERVER["DOCUMENT_ROOT"] . "/templates_c";
    $GLOBALS["ui"]->compile_id = "Warenwirtschaft|best_manu";
    $GLOBALS["ui"]->use_sub_dirs = true;
    $GLOBALS["ui"]->compile_check = true;
    $GLOBALS["ui"]->assign( "eingabefelder", $eingabefelder );
    $GLOBALS["ui"]->assign( "webs", $webs );
    $GLOBALS["ui"]->assign("site", $site);
    ////////////////////////

    // ME: Muss der Bezahlstatus gespeichert werden?
    if($_POST['action'] == "save_bezahlstatus") {
        save_bezahlstatus($_POST['status']);
    }

    // Hole alle Ersteller und übergebe diese an Smarty
    $sql = '
        SELECT
            erstellt_von
        FROM
            rechnung
        GROUP BY
            erstellt_von
    ;';

    $result = mysql_query( $sql ) or die( mysql_errno() . ': ' . mysql_error
        () . '<hr />' . $sql );

    for ( $i = 0; $row = mysql_fetch_assoc( $result ); $i++ )
    {
        $ersteller2[$i] = $row['erstellt_von'];
    }

    $GLOBALS["ui"]->assign( "ersteller", $ersteller2 );
    ///////////////////////////////////////////////////

    // Hole alle Shops und übergebe diese an Smarty
    $sql = '
        SELECT
            ID AS id,
            Name AS name
        FROM
            shops
    ;';
    $result = mysql_query( $sql ) or die( mysql_errno() . ': ' . mysql_error
        () . '<hr />' . $sql );

    $shops["id"][] = "";
    $shops["name"][] = "--- alle ---";
    while ( $row = mysql_fetch_assoc( $result ) )
    {
        $shops["id"][] = $row['id'];
        $shops["name"][] = $row["name"];
    }

    $GLOBALS["ui"]->assign( "shops", $shops );
    ///////////////////////////////////////////////

    // Zeitraum
    $zeitraum = array( 0 => strftime( "%B %Y" ), 1 => strftime( "%B %Y",
        strtotime( "-1 month" ) ), 2 => strftime( "%B %Y", strtotime( "-2 months" ) ),
        3 => strftime( "%B %Y", strtotime( "-3 months" ) ), 4 => strftime( "%B %Y",
        strtotime( "-4 months" ) ), 5 => strftime( "%B %Y", strtotime( "-5 months" ) ),
        6 => strftime( "%B %Y", strtotime( "-6 months" ) ), 7 => strftime( "%B %Y",
        strtotime( "-7 months" ) ), 8 => strftime( "%B %Y", strtotime( "-8 months" ) ),
        9 => strftime( "%B %Y", strtotime( "-9 months" ) ), 10 => strftime( "%B %Y",
        strtotime( "-10 months" ) ), 11 => strftime( "%B %Y", strtotime( "-11 months" ) ),
        12 => strftime( "%B %Y", strtotime( "-12 months" ) ), 13 => strftime
        ( "%B %Y", strtotime( "-13 months" ) ), 14 => "--- alle ---", );

    $GLOBALS["ui"]->assign( "zeitraum", $zeitraum );
    ///////////

    // Übergebe ausgewählte Formulardaten an Smarty zur Vorauswal
    $GLOBALS["ui"]->assign( "cbErsteller", $_POST['cbErsteller'] );
    $GLOBALS["ui"]->assign( "cbShop", $_POST['cbShop'] );
    $GLOBALS["ui"]->assign( "cbBezahlstatus", $_POST['cbBezahlstatus'] );
    $GLOBALS["ui"]->assign( "cbKdNr", $_POST['cbKdNr'] );
    $GLOBALS["ui"]->assign( "cbKunde", $_POST['cbKunde'] );
    $GLOBALS["ui"]->assign( "cbFirma", $_POST['cbFirma'] );
    $GLOBALS["ui"]->assign( "cbMwSt7", $_POST['cbMwSt7'] );
    $GLOBALS["ui"]->assign( "cbMwSt19", $_POST['cbMwSt19'] );
    $GLOBALS["ui"]->assign( "cbNettobetrag", $_POST['cbNettobetrag'] );
    $GLOBALS["ui"]->assign( "cbMwStBefreiung", $_POST['cbMwStBefreiung'] );
    $GLOBALS["ui"]->assign( "cbUStIdNr", $_POST['cbUStIdNr'] );
    $GLOBALS["ui"]->assign( "cbBestellnummer", $_POST['cbBestellnummer'] );
    $GLOBALS["ui"]->assign( "va_zeitraum", $_POST['zeitraum'] );
    
    if($_POST['txtFaellig']) {
        $txtFaellig = strtotime( $_POST['txtFaellig'] ) + 86399;
        $GLOBALS["ui"]->assign( "txtFaellig", strtotime( $_POST['txtFaellig'] ) + 86399 );    
    }else{
        //$GLOBALS["ui"]->assign( "txtFaellig", 0);
    }
    if(is_array( $_POST['msZahlart'] )) {
        $GLOBALS["ui"]->assign( "msZahlart", $_POST['msZahlart'] );
    }
    
    /////////////////////////////////////////////////////////////
    // ME: alle Zahlungsarten holen, wenn es die Tabelle dazu gibt.
    if(table_exists('zahlungsarten',$webs["datenbank"])==true) {
        $zahlungsarten = getZahlungsarten();
        $GLOBALS["ui"]->assign("zahlungsarten",$zahlungsarten);
    }


    // WHERE-Klausel zusammenbauen für die Rechnungsausgabe
    $post = $_POST;

    if ( !empty( $post['ersteller'] ) )
    {
        $where_klausel .= " AND erstellt_von = '" . $post['ersteller'] .
            "' ";
    }

    if ( !empty( $post['shops_id'] ) )
    {
        $where_klausel .= " AND shops_id = '" . $post['shops_id'] . "' ";
    }

    if ( !empty( $post['kd_nr'] ) )
    {
        $where_klausel .= " AND kundennr = '" . $post['kd_nr'] . "' ";
    }

    $zselect="";
    $zfrom="";
    $zorder_by="";
    if ( isset($web_rechte["Warenwirtschaft"]["rechnungsbuch"]["zahlungsziel"]) ) {
        if ( !empty( $post['txtFaellig'] ) )
        {
            $where_klausel .= " AND zahlungsziel != '' ";
        }
        if(table_exists('zahlungsarten',$webs['datenbank'])) {
            $zselect = " ,z.* ";
            $zfrom = " left join zahlungsarten z on r.zahlungsart_id = z.id";
            $zorder_by = ", z.name ";
        }
        if(is_array( $_POST['msZahlart'] )) {
            foreach($_POST['msZahlart'] as $key => $val) {
                if($val>0) {
                    $wh[] = " r.zahlungsart_id = ".$val."";    
                }
            }
            if(is_array($wh)) {
                $wh_impl = implode(" or ", $wh);
                $where_klausel .= " AND ( ".$wh_impl." )";
                $zorder_by = ", z.name ";
            }
        }
    }

    
    //if ( isset( $post['zeitraum'] ) )

    {
        // ### ZEITFILTER HERSTELLEN ###
        $filter_zeit1 = 'erstellt_am';
        switch ( $post['zeitraum'] )
        {
            case 14:
                break;
            case 0: // lfd. Monat
            default:
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(NOW(),\"%Y%m\")";
                $zeit_filter2 = 'NOW()';
                break;
            case 1: // letzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 1 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 1 MONTH';
                break;
            case 2: // vorletzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 2 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 2 MONTH';
                break;
            case 3: // vorletzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 3 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 3 MONTH';
                break;
            case 4: // vorletzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 4 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 4 MONTH';
                break;
            case 5: // vorletzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 5 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 5 MONTH';
                break;
            case 6: // vorletzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 6 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 6 MONTH';
                break;
            case 7: // vorletzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 7 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 7 MONTH';
                break;
            case 8: // vorletzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 8 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 8 MONTH';
                break;
            case 9: // vorletzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 9 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 9 MONTH';
                break;
            case 10: // vorletzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 10 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 10 MONTH';
                break;
            case 11: // vorletzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 11 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 11 MONTH';
                break;
            case 12: // vorletzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 12 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 12 MONTH';
                break;
            case 13: // vorletzter Monat
                $time_filter = " and date_format(" . $filter_zeit1 . ",\"%Y%m\") = date_format(date_sub(NOW(), INTERVAL 13 MONTH),\"%Y%m\")";
                $zeit_filter2 = 'NOW() - INTERVAL 13 MONTH';
                break;
        }
        $where_klausel .= $time_filter;
    }
    /////////////////////////////////////////////////////
    
    
    // Hole alle Rechnungen für die Rechnungsausgabe
    $sql = '
        SELECT
            r.*
            '.$zselect.'
        FROM
            rechnung r
            '.$zfrom.'
        WHERE
            1 = 1
            ' . $where_klausel . '
        ORDER BY
            rechnungsnummer DESC '.$zorder_by.'
    ;';
    
    $result = mysql_query( $sql ) or die( mysql_errno() . ': ' . mysql_error
        () . '<hr />' . $sql );

    $temp = explode( "redaktion.", $_SERVER['SERVER_NAME'] );
    $domain = $temp[1];

    for ( $i = 0; $row = mysql_fetch_assoc( $result ); $i++ )
    {
        if(strlen( $row['zahlungsziel'])>0)  {
            $zahlungsziel = unserialize($row['zahlungsziel']);
            
            if($txtFaellig) {
                if($zahlungsziel['ziel']>$txtFaellig) {
                    continue;
                }
            }
            $rec[$i]['zahlungsziel'] = $zahlungsziel['ziel'];    
            $rec[$i]['now'] = time();
        }
        
        if($row['zahlungsart_id']>0 and $row['name']) {
            $rec[$i]['zahlungsart'] = $row['name'];    
        }else{
            $rec[$i]['zahlungsart'] = "n/a";        
        }
        $rec[$i]['rechnungsnummer'] = $row['rechnungsnummer'];
        $rec[$i]['rechnungsnummerLupe'] = 'http://' . $domain .
            '/images/dokumente/R' . $rec[$i]['rechnungsnummer'] . '.pdf';
        $rec[$i]['rechnungsnummerLink'] = "http://redaktion." . $domain .
            "/Warenwirtschaft/bestellungen.php?Bestellung=" . $row['bestellung'];
        $rec[$i]['erstellt_am'] = $row['erstellt_am'];
        $rec[$i]['bruttobetrag'] = number_format( $row['bruttobetrag'], 2,
            ',', '.' );
        $rec[$i]['erstellt_von'] = $row['erstellt_von'];
        if ( $row['shops_id'] )
        {
            $rec[$i]['shop'] = getShop( $row['shops_id'] );
        }
        else
        {
            $rec[$i]["shop"] = $row["shop"];
        }
        $rec[$i]['kundennr'] = $row['kundennr'];
        $rec[$i]['kunde'] = $row['kunde'];
        $rec[$i]['firma'] = $row['firma'];
        $rec[$i]['mwst_7'] = number_format( $row['mwst_7'], 2, ',', '.' );
        $rec[$i]['mwst_19'] = number_format( $row['mwst_19'], 2, ',', '.' );
        $rec[$i]['nettobetrag'] = number_format( $row['nettobetrag'], 2, ',',
            '.' );
        $rec[$i]['mwst_befreiung'] = $row['mwst_befreiung'];
        $rec[$i]['ustid_nr'] = $row['ustid_nr'];
        $rec[$i]['bestellung'] = $row['bestellung'];

        // Zählen für die Summenfunktionen
        $nettobetragGesamt += $row['nettobetrag'];
        $bruttobetragGesamt += $row['bruttobetrag'];
        $mwst_7Gesamt += $row['mwst_7'];
        $mwst_19Gesamt += $row['mwst_19'];

        // Bezahlstatus Sonderfall
        // ME: History des Bezahlstatus aufbauen und an smarty übergeben
        $rec[$i]['bezahlstatus_history'] = getRechnungHistory($row);
        
        if ( $row['storniert'] )
        {
            $rec[$i]['bezahlstatus'] = 'storniert';
        }
        else
        {
            if ( $row['bezahlt_am'] > 0 )
            {
                $rec[$i]['bezahlstatus'] = 'bezahlt';
            }
            else
            {
                if ( $row['3_mahnung_nr'] > 0 )
                {
                    $rec[$i]['bezahlstatus'] = '3. Mahnung';
                } elseif ( $row['2_mahnung_nr'] > 0 )
                {
                    $rec[$i]['bezahlstatus'] = '2. Mahnung';
                } elseif ( $row['1_mahnung_nr'] > 0 )
                {
                    $rec[$i]['bezahlstatus'] = '1. Mahnung';
                }
                else
                {
                    $rec[$i]['bezahlstatus'] = 'offen';
                }
            }
        }
        //////////////////////////
    }

    // Gesamtsumme übergeben
    $nettobetragGesamt = number_format( $nettobetragGesamt, 2, ',', '.' );
    $bruttobetragGesamt = number_format( $bruttobetragGesamt, 2, ',', '.' );
    $mwst_7Gesamt = number_format( $mwst_7Gesamt, 2, ',', '.' );
    $mwst_19Gesamt = number_format( $mwst_19Gesamt, 2, ',', '.' );


    if ( $bruttobetragGesamt > 0 || $mwst_7Gesamt > 0 || $mwst_19Gesamt > 0 ||
        $nettobetragGesamt > 0 )
    {

        $rec[$i]['rechnungsnummer'] = "<b>Gesamt:</b>";
        $rec[$i]['erstellt_am'] = "&nbsp;";
        $rec[$i]['bruttobetrag'] = "<b>" . $bruttobetragGesamt . "</b>";
        $rec[$i]['erstellt_von'] = "&nbsp;";
        $rec[$i]['shop'] = "&nbsp;";
        $rec[$i]['kundennr'] = "&nbsp;";
        $rec[$i]['kunde'] = "&nbsp;";
        $rec[$i]['firma'] = "&nbsp;";
        $rec[$i]['mwst_7'] = "<b>" . $mwst_7Gesamt . "</b>";
        $rec[$i]['mwst_19'] = "<b>" . $mwst_19Gesamt . "</b>";
        $rec[$i]['nettobetrag'] = "<b>" . $nettobetragGesamt . "</b>";
        $rec[$i]['mwst_befreiung'] = "&nbsp;";
        $rec[$i]['ustid_nr'] = "&nbsp;";
        $rec[$i]['bestellung'] = "&nbsp;";
    }

    $GLOBALS["ui"]->assign( "rec", $rec );
    ////////////////////////////////////////////////

    // Datev-Export Recht
    $GLOBALS["ui"]->assign( "datev_web_recht", $web_rechte['Warenwirtschaft']['rechnungsbuch']['datev_export'] );
    $GLOBALS["ui"]->assign( "datev_user_recht", $user_rechte['Warenwirtschaft']['rechnungsbuch']['datev_export'] );
    /////////////////////

    // Datev CSV-Export
    if ( empty( $_POST['zeitraum'] ) )
    {
        $_POST['zeitraum'] = 0;
    }

    $aktMonatJahr = date( "Y-m-d H:i:s", strtotime( "-" . $_POST['zeitraum'] .
        " months" ) );
    $aktMonatJahr = urlencode( $aktMonatJahr );
    $GLOBALS["ui"]->assign( "aktMonatJahr", $aktMonatJahr );
    ////////////////////

    // Templateausgabe
    $GLOBALS["ui"]->display( "rechnungsbuch/rechnungsbuch.tpl" );
    //////////////////

     
    function getRechnungHistory($row) {
        $str = "";
        if($row['1_mahnung_nr']!="0000-00-00 00:00:00") {
            $date = datetime2human( $row['1_mahnung_nr'] );
            $str .= "1. Mahnung am ". $date['Datum']." ".$date['Zeit'] ."<br />";
        }
        if($row['2_mahnung_nr']!="0000-00-00 00:00:00") {
            $date = datetime2human( $row['2_mahnung_nr'] );
            $str .= "2. Mahnung am ". $date['Datum']." ".$date['Zeit'] ."<br />";
        }
        if($row['3_mahnung_nr']!="0000-00-00 00:00:00") {
            $date = datetime2human( $row['3_mahnung_nr'] );
            $str .= "3. Mahnung am ". $date['Datum']." ".$date['Zeit'] ."<br />";
        }
        if($row['bezahlt_am']=="0000-00-00 00:00:00") {
            $str .= "Rechnung noch nicht bezahlt!<br />";
        } else {
            $date = datetime2human( $row['bezahlt_am'] );
            $str .= "Rechnung bezahlt am ". $date['Datum']." ".$date['Zeit'] ."<br />";
        }
        if($row['storniert'] == 1) {
            $str = "storniert";
        }
        return $str;
    }



    function save_bezahlstatus($status) {
        if(is_array($status)) {
            foreach($status as $rid => $newStatus) {
                switch($newStatus) {
                    case "bezahlt":
                        $sql = "update 
                                    rechnung
                                set
                                    bezahlt_am = NOW(),
                                    storniert = 0
                                where 
                                    rechnungsnummer = '".$rid."'
                        ";
                    break;
                    
                    case "storniert":
                        $sql = "update
                                    rechnung
                                set
                                    storniert = 1
                                where
                                    rechnungsnummer = '".$rid."'          
                        ";
                    break;
                    
                    default:
                }
                if($sql) {
                    if($q=mysql_query($sql)){
                        
                    }
                }
            }
        }
    }






















?>