Subversion-Projekte lars-tiefland.content-management

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
    //$Id: bezahlstatus.php 3993 2011-10-13 08:02:38Z erling $
4
 
5
    /**
6
     * @author Malte Erling <erling@weban.de>
7
     * @copyright 2011 Webagentur Niewerth
8
     * @package Content-management
9
     * @version $Rev: 3993 $
10
     * @license propietary
11
     * @filesource
12
     *
13
     */
14
 
15
    /**
16
     *
17
     * @author Malte Erling <erling@weban.de>
18
     * @copyright 2011 Webagentur Niewerth
19
     * @package Content-management
20
     */
21
 
22
    require_once 'Weban_Smarty.class.php';
23
    require_once '../Online-Shop/connect2.php';
24
 
25
    setlocale( LC_MONETARY, "de_DE" );
26
    $cfg = array();
27
 
28
    // In welchem ID-Bereich liegen die Konfig-Vorlagen?
29
    $start = 280;
30
    $end = 290;
31
 
32
    $GLOBALS["ui"] = new Weban_Smarty();
33
    $GLOBALS["ui"]->compile_dir = $_SERVER["DOCUMENT_ROOT"] . "/templates_c/";
34
    $GLOBALS["ui"]->compile_id = "Warenwirtschaft|bezahlstatus";
35
    $GLOBALS["ui"]->use_sub_dirs = true;
36
 
37
    $GLOBALS["ui"]->assign( "config_ready", 0 );
38
 
39
    if ( $_GET['shops_ID'] >= 0 )
40
    {
41
        $_SESSION['shops_ID'] = $_GET['shops_ID'];
42
    }
43
 
44
    if ( $_GET['config'] )
45
    {
46
        // die übergebene shops_ID ermitteln
47
        $expl = explode( "###", $_GET['config'] );
48
 
49
        $cfg_ID = $expl[0];
50
        $shops_ID = $expl[1];
51
        $cfg_name = $expl[2];
52
 
53
        $GLOBALS["ui"]->assign( "cfg_sel", $_GET['config'] );
54
        $cfg = getConfig( $cfg_ID );
55
 
56
    }
57
 
58
 
59
    // SelectBox für Konfiguration
60
    $configs = getConfigs();
61
    $GLOBALS["ui"]->assign( "configs", $configs );
62
 
63
 
64
    $bezahlstatus = isset( $user_rechte["Warenwirtschaft"]["bezahlstatus"]["bezahlstatus"] );
65
 
66
    if ( !$bezahlstatus )
67
    {
68
        $GLOBALS["ui"]->assign( "meld", "Sie haben nicht das Recht, dieses Tool zu nutzen!" );
69
        $GLOBALS["ui"]->assign( "error", true );
70
        $GLOBALS["ui"]->display( "meld.tpl" );
71
    }
72
    else
73
    {
74
        switch ( $_POST["mode"] )
75
        {
76
            default:
77
                getProtocols();
78
                break;
79
            case "save":
80
                saveBestellungenZuordnung( $_POST['row'] );
81
                break;
82
            case "import":
83
                $error = false;
84
                if ( $_FILES["datei"] )
85
                {
86
                    $tmp_name = $_FILES["datei"]["tmp_name"];
87
                    $data = csv_import( $tmp_name );
88
                    if ( !$data )
89
                    {
90
                        $error = true;
91
                        $GLOBALS["ui"]->assign( "meld",
92
                            "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." );
93
 
94
                    }
95
                    else
96
                    {
97
                        $GLOBALS["ui"]->assign( "meld", "Die Datei wurde erfolgreich importiert!" );
98
                    }
99
                    $GLOBALS["ui"]->assign( "error", $error );
100
                    $GLOBALS["ui"]->display( "meld.tpl" );
101
                }
102
                $GLOBALS["ui"]->assign( "data", $data );
103
                break;
104
        }
105
        $GLOBALS["ui"]->display( "bezahlstatus.tpl" );
106
    }
107
 
108
 
109
    function csv_import( $fName )
110
    {
111
        global $site;
112
        global $cfg;
113
        $data = array();
114
        if ( count($cfg) == 0 )
115
        {
116
            die( "<p style=\"padding:20px;\">Sie haben keine g&uuml;ltige Konfiguration gew&auml;hlt..</p>" );
117
        }
118
        $rows = file( $fName );
119
        $i = 0; // der Row-Counter
120
        $felder = array();
121
        $cnt = count( $rows );
122
        foreach ( $rows as $row_id => $row )
123
        {
124
 
125
            if ( $cfg['ignore_lastrows'] && $row_id == $cnt - $cfg['ignore_lastrows'] )
126
            {
127
                break;
128
            }
129
            if ( $row_id >= $cfg['row_start'] )
130
            {
131
 
132
                $felder[$i] = explode( $cfg['trenner'], $row );
133
 
134
                $datum = $felder[$i][$cfg['datum']];
135
                $ueberw_betrag = str_replace( ".", "", $felder[$i][$cfg['haben']] );
136
                $ueberw_betrag = str_replace( ",", ".", $ueberw_betrag );
137
                $text = str_replace( "\"", "", $felder[$i][$cfg['text']] );
138
                if ( $cfg['Name'] )
139
                {
140
                    $name = $felder[$i][$cfg['name']];
141
                }
142
                $best = getBest( $ueberw_betrag );
143
 
144
 
145
                preg_match_all( "/\b([0-9]*)\b/", $text, $matches );
146
                $zahlen = $matches[0];
147
 
148
 
149
                $data[$i]['datum'] = $datum;
150
                $data[$i]['ueberw_betrag'] = $ueberw_betrag;
151
                $data[$i]['text'] = $text;
152
                if ( $name )
153
                {
154
                    $data[$i]['name'] = $name;
155
                }
156
                // alle gültigen Bestellungen durchlaufen
157
                $b = 0;
158
                $hr = 0;
159
                foreach ( $best as $key => $bestellung )
160
                {
161
 
162
                    $data[$i]['best'][$b]['rank'] = 1;
163
                    $data[$i]['best'][$b]['bestID'] = $bestellung['ID'];
164
                    $data[$i]['best'][$b]['erstellt_am'] = date( "d.m.Y", strtotime($bestellung['erstellt_am']) );
165
                    $data[$i]['best'][$b]['ueberw_betrag'] = $bestellung['gesamtsumme_buffer'];
166
                    $data[$i]['best'][$b]['name'] = trim( $bestellung['Anrede'] . " " . $bestellung['Vorname'] .
167
                        " " . $bestellung['Name1'] . " " . $bestellung['Name2'] . " " . $bestellung['Firma'] );
168
 
169
 
170
                    // es wurden Zahlen im Text gefunden, die mit der BestID verglichen werden müssen
171
                    if ( is_array($zahlen) )
172
                    {
173
 
174
                        foreach ( $zahlen as $zahl )
175
                        {
176
                            // Bei Übereinstimmung wird der Rank hochgesetzt.
177
                            if ( ($zahl == $bestellung['ID']) )
178
                            {
179
                                $data[$i]['best'][$b]['rank']++;
180
 
181
                            }
182
                        }
183
                    }
184
                    if ( $hr < $data[$i]['best'][$b]['rank'] )
185
                    {
186
                        $hr = $data[$i]['best'][$b]['rank'];
187
                        $data[$i]['hr'] = $hr;
188
                    }
189
 
190
                    $b++;
191
                }
192
 
193
                $i++;
194
            }
195
 
196
        }
197
 
198
        return $data;
199
    }
200
 
201
 
202
    //SELECT *  FROM $tabelle AS k WHERE k.datum < date_add(current_date, interval -30 day)"
203
    function getBest( $ueberw_betrag )
204
    {
205
        global $cfg;
206
        global $shops_ID;
207
        $best = array();
208
 
209
        $wh = "";
210
        if ( $shops_ID > 0 )
211
        {
212
            $wh = " and B.shops_ID = " . $shops_ID . "";
213
        }
214
        $sql = "select
215
                    B.*,
216
                    L.Anrede,
217
                    L.Vorname,
218
                    L.Name1,
219
                    L.Name2,
220
                    L.Firma
221
                from
222
                    Bestellung B
223
 
224
                left join
225
                    Logins L
226
                on
227
                    L.ID = B.Login
228
 
229
                where
230
                    B.erstellt_am > DATE_ADD( CURDATE( ) , INTERVAL -" . $cfg['days'] .
231
            " DAY )
232
                and
233
                    B.bezahlt != 1
234
                and
235
                    B.gesamtsumme_buffer = " . $ueberw_betrag . "
236
 
237
                " . $wh . "
238
 
239
                order by
240
                    B.erstellt_am desc
241
 
242
        ";
243
        if ( $q = mysql_query($sql) )
244
        {
245
            while ( $r = mysql_fetch_assoc($q) )
246
            {
247
                $best[] = $r;
248
            }
249
        }
250
 
251
        return $best;
252
    }
253
 
254
 
255
    function getAllShops()
256
    {
257
        $shops[0] = "Bitte w&auml;hlen";
258
        $sql = "select ID,Name from shops order by ID";
259
        if ( $q = mysql_query($sql) )
260
        {
261
            while ( $r = mysql_fetch_assoc($q) )
262
            {
263
                $shops[$r['ID']] = $r['Name'];
264
            }
265
        }
266
        return $shops;
267
    }
268
 
269
 
270
    function getConfigs()
271
    {
272
        global $start;
273
        global $end;
274
 
275
 
276
        $configs = array();
277
        $configs[-1] = "Bitte w&auml;hlen";
278
        $sql = "select
279
                    *
280
                from
281
                    web_settings
282
                where
283
                    ID >= " . $start . "
284
                and
285
                    ID <= " . $end . "
286
                order by
287
                    ID";
288
        if ( $q = mysql_query($sql) )
289
        {
290
            while ( $r = mysql_fetch_assoc($q) )
291
            {
292
                $name = strtolower( str_replace(" ", "_", $r['Name']) );
293
                $configs[$r['ID'] . "###" . $r['shops_ID'] . "###" . $name] = $r['Name'];
294
            }
295
        }
296
        return $configs;
297
    }
298
 
299
 
300
    function getConfig( $id )
301
    {
302
        global $shops_ID;
303
        $cfg = "";
304
 
305
        $wh = "";
306
        if ( $shops_ID > 0 )
307
        {
308
            $wh = " and shops_ID = " . $shops_ID . "";
309
        }
310
 
311
        $sql = "select
312
                    Inhalt
313
                from
314
                    web_settings
315
                where
316
                    ID = " . $id . "
317
                " . $wh . "
318
        ";
319
 
320
        if ( $q = mysql_query($sql) )
321
        {
322
            if ( $r = mysql_fetch_assoc($q) )
323
            {
324
                $str = "<?php\n" . $r['Inhalt'] . " \n?>";
325
                // String in Datei schreiben
326
                $fname = "/tmp/" . md5( $str ) . ".php";
327
                if ( file_put_contents($fname, $str) )
328
                {
329
                    require_once ( $fname );
330
                    if ( $cfg )
331
                    { // Der Array aus der Datei
332
                        if ( unlink($fname) )
333
                        { // Datei wieder löschen
334
                            $GLOBALS["ui"]->assign( "config_ready", 1 );
335
                        }
336
                    }
337
                }
338
            }
339
        }
340
        return $cfg;
341
    }
342
 
343
 
344
    function saveBestellungenZuordnung( $row )
345
    {
346
        global $cfg_name;
347
        $all = 0;
348
        $save = 0;
349
        // den Text für das Bemerkungsfeld erstellen
350
        foreach ( $row as $cnt => $data )
351
        {
352
            $all++;
353
            $expl = explode( "###", $data['bestellung'] );
354
            $bid = $expl[0];
355
            $name = $expl[1];
356
            // ist die Checkbox aktiviert gewesen?
357
            if ( $data['chk'] == "on" )
358
            {
359
                if ( $data['bestellung'] != "none" )
360
                {
361
                    $str = utf8_decode( "Überweisung vom " ) . $data['datum'] . utf8_decode( ". Text: " ) .
362
                        $data['text'] . ". Zugewiesen durch " . $_SERVER['PHP_AUTH_USER'] . " am " .
363
                        date( "d.m.Y H:i", time() ) . ".\n";
364
 
365
                    $sql = "update
366
                                Bestellung
367
                            set
368
                              Bemerkung = concat_ws('', Bemerkung ,'" . $str .
369
                        "'),
370
                              bezahlt = 1
371
                            where
372
                                ID = " . $bid . "
373
                    ";
374
 
375
                    $q = mysql_query( $sql );
376
                    $save++;
377
 
378
                    // Protokollierung für erfolgreiche Zurodnung
379
                    if ( isset($bid) and $bid != "none" )
380
                    {
381
                        $prot['#0f0'][] =
382
                            $data['datum'] . " - " .
383
                            "BestNr.: " . $bid .
384
                            " - Betrag: " . $data['betrag']." - ". $str;
385
                    }
386
                }
387
            }
388
            // Protokollierung für nicht erfolgreiche Zurodnung
389
            if ( $bid == "none" )
390
            {
391
                $prot['#fc0'][] = $data['datum'] . " - Betrag: ".$data['betrag']. " - "  . $data['text'] .
392
                    " - Keine Zuordnung ausgew&auml;hlt.";
393
            }
394
            if ( $bid == "" )
395
            {
396
                $prot['#f00'][] = $data['datum'] . " - Betrag: ".$data['betrag']. " - " . $data['text'] .
397
                    " - Keine Zuordnung m&ouml;glich.";
398
            }
399
 
400
        }
401
        $html = "<html><head><style>body {font-family:verdana,arial,sans-serif;font-size:0.7em;}table td {vertical-align:top;}</style></head><body>\n";
402
        $html .= "<h2>Zuordnungen erstellt von " . $_SERVER['PHP_AUTH_USER'] .
403
            " am " . date( "d.m.Y H:i", time() ) . "</h2>";
404
        $html .= $save . " Zuordnungen von " . $all;
405
        $html .= "<table cellpadding=\"2\" cellspacing=\"0\" border=\"1\">\n<br />";
406
 
407
        $i = 1;
408
        foreach ( $prot as $color => $val )
409
        {
410
 
411
            if ( is_array($val) )
412
            {
413
 
414
                foreach ( $val as $key => $v )
415
                {
416
                    $html .= "<tr style=\"background-color:" . $color . ";\">";
417
                    $html .= "<td style=\"text-align:right;\">" . $i . "</td>";
418
                    $html .= "<td>" . $v . "</td>";
419
 
420
                    $html .= "</tr>\n";
421
                    $i++;
422
                }
423
            }
424
 
425
        }
426
        $html .= "</table>\n";
427
        $html .= "<br />" . $save . " Zuordnungen von " . $all;
428
        $html .= "</body></html>\n";
429
        $fname = $cfg_name . "_" . $GLOBALS['site'] . "_" . date( "YmdHi", time() ) .
430
            ".html";
431
        file_put_contents( "protokoll/" . $fname, $html );
432
        echo "<br>Es wurden " . $save . " Zuordnungen gespeichert.";
433
 
434
        return true;
435
    }
436
 
437
 
438
    function getProtocols()
439
    {
440
        $protocols = "";
441
        if ( $handle = opendir('protokoll') )
442
        {
443
            while ( false !== ($file = readdir($handle)) )
444
            {
445
                if ( $file != "." && $file != ".." )
446
                {
447
                    if ( stristr($file, $GLOBALS['site']) )
448
                    {
449
                        $protocols .= "<a href=\"protokoll/" . $file . "\" target=\"_blank\">" . $file .
450
                            "</a><br />\n";
451
                    }
452
 
453
                }
454
            }
455
            closedir( $handle );
456
        }
457
        $GLOBALS["ui"]->assign( "protocols", $protocols );
458
 
459
    }
460
 
461
 
462
 
463
 
464
 
465
 
466
?>