Subversion-Projekte lars-tiefland.content-management

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
    /**
4
     * @package   Content-management
5
     * @author    Lars Tiefland <tiefland@weban.de>
6
     * @copyright 2010 Webagentur Niewerth
7
     * @license   propietary http://www.weban.de
8
     * @version   $Rev: 3931 $
9
     * @filesource
10
     *
11
     */
12
 
13
    /**
14
     *
15
     * @package   Content-management
16
     * @author    Lars Tiefland <tiefland@weban.de>
17
     * @copyright 2010 Webagentur Niewerth
18
     */
19
 
20
    // SVN: $Id: rhenus_export.php 3931 2011-09-16 08:17:57Z tiefland $
21
 
22
    require_once 'Weban_Smarty.class.php';
23
    require_once '../Online-Shop/connect2.php';
24
    require_once 'File/Find.php';
25
    require_once 'module/functions.common.php';
26
 
27
    $GLOBALS["ui"] = new Weban_Smarty();
28
    $GLOBALS["ui"]->compile_dir = $_SERVER["DOCUMENT_ROOT"] . "/templates_c/";
29
    $GLOBALS["ui"]->compile_id = "Warenwirtschaft|export";
30
    $GLOBALS["ui"]->use_sub_dirs = true;
31
    $GLOBALS["ui"]->assign( "title", "Auftragsdaten nach Rhenus exportieren" );
32
    if ( isset( $user_rechte['Warenwirtschaft']['export']['rhenus'] ) )
33
    {
34
        if ( $_GET['mode'] == 'new' )
35
        {
36
            rhenus_export();
37
        } elseif ( $_GET['file'] )
38
        {
39
            $file = $_GET['file'];
40
            if ( file_exists( $file ) )
41
            {
42
                //header( 'Content-Type: text/plain' );
43
                header( 'Content-Disposition: attachment; filename="' .
44
                    basename( $file ) . '"' );
45
                readfile( $file );
46
            }
47
        }
48
        else
49
        {
50
            if ( !file_exists( "archiv/$site" ) )
51
            {
52
                mkdir( "archiv/$site" );
53
            }
54
            if ( !file_exists( "archiv/$site" ) )
55
            {
56
                mkdir( "archiv/$site" );
57
            }
58
            $ff = new File_Find();
59
            $files = $ff->search( "/export_Bestellungen_rhenus_[0-9a-zA-Z]{0,3}_{0,1}[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}.csv$/i",
60
                "archiv/$site/", "perl" );
61
            rsort( $files );
62
            foreach ( $files as $f_id => $file )
63
            {
64
                $files_arr[$f_id]["name"] = $file;
65
                $date = date( "d.m.Y H:i:s", filemtime( $file ) );
66
                $files_arr[$f_id]["desc"] = $date;
67
            }
68
            $GLOBALS["ui"]->assign( "files", $files_arr );
69
            $GLOBALS["ui"]->assign( "anbieter", "rhenus" );
70
            $GLOBALS["ui"]->display( "export.tpl" );
71
        }
72
    }
73
    else
74
    {
75
        $GLOBALS["ui"]->assign( "meld",
76
            "Sie haben nicht das Recht dieses Tool zu nutzen!" );
77
        $GLOBALS["ui"]->assign( "error", true );
78
        $GLOBALS["ui"]->display( "meld.tpl" );
79
    }
80
 
81
 
82
    function rhenus_export()
83
    {
84
        global $site, $web_rechte;
85
        $date = date( 'Y-m-d_H-i-s' );
86
        $sql = "SELECT DISTINCT
87
                Bestellung,
88
                artikel,
89
                short_line_1 AS rufname,
90
                ab.Preis_pro_Stueck,
91
                a.Eigenschaft_1
92
            FROM
93
                artikel_to_Bestellung ab
94
            JOIN
95
                artikel AS a
96
            ON
97
                a.ID=ab.artikel
98
            WHERE
99
                ab.status='freigegeben'
100
            AND
101
                a.Father = -3
102
        ";
103
        $res_b = mysql_query( $sql );
104
        if ( !$res_b )
105
        {
106
            die( mysql_error() );
107
        }
108
        while ( $row_b = mysql_fetch_assoc( $res_b ) )
109
        {
110
            $b_id = $row_b["Bestellung"];
111
            $paket = !( $row_b["Eigenschaft_1"] );
112
            //$paket = false;
113
            $a_id = $row_b["artikel"];
114
            $inkasso = "";
115
            $zahlungsart = ucfirst( $row_b["rufname"] );
116
            if ( $zahlungsart == "Nachnahme" )
117
            {
118
                // Inkassobetrag ermitteln
119
                $inkasso = getInkasso( $b_id );
120
            }
121
            $entsorgung = getEntsorgung( $b_id );
122
            $sql_a = "SELECT
123
                    ab.ID,
124
                    ab.artikel,
125
                    ab.Menge,
126
                    ab.Bezeichnung,
127
                    ab.Preis_pro_Stueck,
128
                    a.kurzbezeichnung,
129
                    a.short_line_2,
130
                    a.gewicht,
131
                    a.Father
132
                FROM
133
                    artikel_to_Bestellung ab
134
                JOIN
135
                    artikel AS a
136
                ON
137
                    a.ID=ab.artikel
138
                WHERE
139
                    a.Father!=-3
140
                AND
141
                    ab.Bestellung=$b_id
142
            ";
143
            $res_a = mysql_query( $sql_a );
144
            if ( !$res_a )
145
            {
146
                die( mysql_error() );
147
            }
148
            $p_anz_total = mysql_num_rows( $res_a );
149
            $sql_a = "SELECT
150
                    ab.ID,
151
                    ab.artikel,
152
                    ab.Menge,
153
                    ab.Bezeichnung,
154
                    ab.Preis_pro_Stueck,
155
                    a.kurzbezeichnung,
156
                    a.short_line_2,
157
                    a.gewicht,
158
                    a.Father
159
                FROM
160
                    artikel_to_Bestellung ab
161
                JOIN
162
                    artikel AS a
163
                ON
164
                    a.ID=ab.artikel
165
                WHERE
166
                    ab.status='reserviert'
167
                AND
168
                    a.Father!=-3
169
                AND
170
                    ab.Bestellung=$b_id
171
            ";
172
            $res_a = mysql_query( $sql_a );
173
            if ( !$res_a )
174
            {
175
                die( mysql_error() );
176
            }
177
            $p_anz = mysql_num_rows( $res_a );
178
            //echo $p_anz;
179
            if ( $p_anz )
180
            {
181
                $sql = "SELECT
182
                        b.*,
183
                        l.E_Mail,
184
                        l.Vorname,
185
                        l.Name1,
186
                        l.Name2 as Firma,
187
                        l.Strasse,
188
                        l.Hausnummer,
189
                        l.PLZ,
190
                        l.Ort,
191
                        l.Land,
192
                        l.Telefon,
193
                        s.short
194
                    FROM
195
                        Bestellung b,
196
                        Logins l,
197
                        shops s
198
                    WHERE
199
                        l.ID=b.Login
200
                    AND
201
                        b.id= $b_id
202
                    AND
203
                        s.ID=b.shops_ID
204
                ";
205
                $res = mysql_query( $sql );
206
                if ( !$res )
207
                {
208
                    die( mysql_error() );
209
                }
210
                while ( $row = mysql_fetch_assoc( $res ) )
211
                {
212
                    $inv_artikel = array( 13024, 999997 );
213
                    if ( in_array( $row_a["artikel"], $inv_artikel ) )
214
                    {
215
                        continue;
216
                    }
217
                    $b_id = $row["ID"];
218
                    $s_short = $row["short"];
219
                    $l_id = $row["Login"];
220
                    $i = 1;
221
                    if ( $row['liefer_Land'] )
222
                    {
223
                        $land = $row['liefer_Land'];
224
                        $anrede = $row['liefer_Andrede'];
225
                        $strasse = $row['liefer_Strasse'];
226
                        $hausnummer = $row['liefer_Str_Nr'];
227
                        $name = $row['liefer_Name'];
228
                        $vorname = $row['liefer_Vorname'];
229
                        $ort = $row['liefer_Ort'];
230
                        $plz = $row['liefer_PLZ'];
231
                    }
232
                    else
233
                    {
234
                        $land = $row['Land'];
235
                        $anrede = $row['Anrede'];
236
                        $strasse = $row['Strasse'];
237
                        $hausnummer = $row['Hausnummer'];
238
                        $name = $row['Name1'];
239
                        $vorname = $row['Vorname'];
240
                        $ort = $row['Ort'];
241
                        $plz = $row['PLZ'];
242
                    }
243
                    $bemerkung = preg_replace( "/[\"'\r\n]*/", "", $row["Bemerkung_extern"] );
244
                    $bemerkung = str_replace( ";", ",", $bemerkung );
245
                    $bemerkung = substr( $bemerkung, 0, 70 );
246
                    $dat = datetime2human( $row["erstellt_am"] );
247
                    $datum = $dat["Datum"];
248
                    $land = getISOCode( $land );
249
                    while ( $row_a = mysql_fetch_assoc( $res_a ) )
250
                    {
251
                        $ab_id = $row_a["ID"];
252
                        $f_row = array();
253
                        if ( $row_a["Father"] == 131 )
254
                        {
255
                            $rhenus_a_id = $row_a["short_line_2"];
256
                        }
257
                        else
258
                        {
259
                            $rhenus_a_id = $row_a["artikel"];
260
                        }
261
                        $a_name = ( $row_a["Bezeichnung"] ) ? $row_a["Bezeichnung"] :
262
                            $row_a["kurzbezeichnung"];
263
                        $f_row[] = $b_id;
264
                        $f_row[] = $l_id;
265
                        $f_row[] = $anrede;
266
                        $f_row[] = $name;
267
                        $f_row[] = $vorname;
268
                        $f_row[] = $strasse;
269
                        $f_row[] = $hausnummer;
270
                        $f_row[] = $plz;
271
                        $f_row[] = $ort;
272
                        $f_row[] = $land;
273
                        $f_row[] = $row["Telefon"];
274
                        $f_row[] = "";
275
                        $f_row[] = "";
276
                        $f_row[] = $row["E_Mail"];
277
                        $f_row[] = ( $row_a["Father"] == 131 ) ? 3 : 1;
278
                        $f_row[] = $datum;
279
                        $f_row[] = $inkasso;
280
                        $f_row[] = $zahlungsart;
281
                        $f_row[] = "Ganztags";
282
                        $f_row[] = $bemerkung;
283
                        $f_row[] = $i;
284
                        $f_row[] = $rhenus_a_id;
285
                        $f_row[] = $a_name;
286
                        $f_row[] = 0;
287
                        $f_row[] = $row_a["Menge"];
288
                        $f_row[] = $entsorgung;
289
                        $f_row[] = 1;
290
                        $f_row[] = "";
291
                        $f_row[] = round( $row_a["gewicht"], 0 );
292
                        $sql_u = "
293
                            UPDATE
294
                                artikel_to_Bestellung
295
                            SET
296
                                status='erledigt',
297
                                exported='" . time() . "'
298
                            WHERE
299
                                ID=$ab_id
300
                        ";
301
                        $res_u = mysql_query( $sql_u );
302
                        if ( $i == $p_anz_total )
303
                        {
304
                            $sql_u = "
305
                                UPDATE
306
                                    artikel_to_Bestellung
307
                                SET
308
                                    status = 'versand',
309
                                    exported='" . time() . "'
310
                                WHERE
311
                                    Bestellung = $b_id
312
                                AND
313
                                    artikel = $a_id
314
                            ";
315
                            $res_u = mysql_query( $sql_u );
316
                        }
317
                        else
318
                        {
319
                            $sql_u = "
320
                                UPDATE
321
                                    artikel_to_Bestellung
322
                                SET
323
                                    exported='" . time() . "'
324
                                WHERE
325
                                    Bestellung = $b_id
326
                                AND
327
                                    artikel = $a_id
328
                            ";
329
                            $res_u = mysql_query( $sql_u );
330
                        }
331
                        $i++;
332
                        $f_cont[$s_short][] = implode( ";", $f_row );
333
                        if ( $paket )
334
                        {
335
                            $f_row[20] = $i;
336
                            $f_row[21] = "";
337
                            switch ( true )
338
                            {
339
                                case $row_a["gewicht"] >= 0 && $row_a["gewicht"] <
340
                                        6:
341
                                    $f_row[22] = "P1";
342
                                    break;
343
                                case $row_a["gewicht"] >= 6 && $row_a["gewicht"] <
344
                                        11:
345
                                    $f_row[22] = "P2";
346
                                    break;
347
                                case $row_a["gewicht"] >= 11 && $row_a["gewicht"] <
348
                                        17:
349
                                    $f_row[22] = "P3";
350
                                    break;
351
                                case $row_a["gewicht"] >= 17 && $row_a["gewicht"] <
352
                                        21:
353
                                    $f_row[22] = "P4";
354
                                    break;
355
                                case $row_a["gewicht"] >= 21 && $row_a["gewicht"] <
356
                                        33:
357
                                    $f_row[22] = "P5";
358
                                    break;
359
                                case $row_a["gewicht"] >= 33 && $row_a["gewicht"] <
360
                                        35:
361
                                    $f_row[22] = "P6";
362
                                    break;
363
                                case $row_a["gewicht"] >= 35 && $row_a["gewicht"] <
364
                                        41:
365
                                    $f_row[22] = "P7";
366
                                    break;
367
                                case $row_a["gewicht"] >= 41 && $row_a["gewicht"] <
368
                                        43:
369
                                    $f_row[22] = "P8";
370
                                    break;
371
                                case $row_a["gewicht"] >= 43 && $row_a["gewicht"] <
372
                                        45:
373
                                    $f_row[22] = "P9";
374
                                    break;
375
                                case $row_a["gewicht"] >= 45 && $row_a["gewicht"] <
376
                                        47:
377
                                    $f_row[22] = "Q1";
378
                                    break;
379
                                case $row_a["gewicht"] >= 47 && $row_a["gewicht"] <
380
                                        49:
381
                                    $f_row[22] = "Q2";
382
                                    break;
383
                                case $row_a["gewicht"] >= 49 && $row_a["gewicht"] <
384
                                        51:
385
                                    $f_row[22] = "Q3";
386
                                    break;
387
                                case $row_a["gewicht"] >= 51 && $row_a["gewicht"] <
388
                                        56:
389
                                    $f_row[22] = "Q4";
390
                                    break;
391
                                case $row_a["gewicht"] >= 56 && $row_a["gewicht"] <
392
                                        61:
393
                                    $f_row[22] = "Q5";
394
                                    break;
395
                                case $row_a["gewicht"] >= 61 && $row_a["gewicht"] <
396
                                        66:
397
                                    $f_row[22] = "Q6";
398
                                    break;
399
                                case $row_a["gewicht"] >= 66 && $row_a["gewicht"] <
400
                                        71:
401
                                    $f_row[22] = "Q7";
402
                                    break;
403
                            }
404
                            $i++;
405
                            $f_cont[$s_short][] = implode( ";", $f_row );
406
                        }
407
                    }
408
                }
409
            }
410
            else
411
            {
412
                $sql_u = "
413
                    UPDATE
414
                        artikel_to_Bestellung
415
                    SET
416
                        status = 'versand',
417
                        exported='" . time() . "'
418
                    WHERE
419
                        Bestellung = $b_id
420
                    AND
421
                        artikel = $a_id
422
                ";
423
                $res_u = mysql_query( $sql_u );
424
            }
425
        }
426
        foreach ( $f_cont as $shop => $s_cont )
427
        {
428
            $f_name = "export_Bestellungen_rhenus_" . $shop . "_" . $date .
429
                ".csv";
430
            $cont = implode( "\r\n", $s_cont );
431
            if ( $cont )
432
            {
433
                //$f = fopen( "archiv/$site/$f_name", 'w' );
434
                if ( $header )
435
                {
436
                    $cont = $header . "\r\n" . $cont;
437
                }
438
                /*fwrite( $f, $cont );
439
                fclose( $f );*/
440
                file_put_contents( "archiv/$site/$f_name", $cont );
441
            }
442
        }
443
        header( 'Location: export.php' );
444
    }
445
 
446
    function getInkasso( $b_id )
447
    {
448
        $sql = "SELECT
449
                SUM(ab.Menge * ab.Preis_pro_Stueck) AS inkasso
450
            FROM
451
                artikel_to_Bestellung ab,
452
                artikel a
453
            WHERE
454
                ab.Bestellung=$b_id
455
            AND
456
                ab.artikel=a.ID
457
            AND
458
            (
459
                (ab.status='reserviert')
460
                OR
461
                (ab.exported='0000-00-00 00:00:00' AND a.Father=-3)
462
            )
463
        ";
464
        $res = mysql_query( $sql );
465
        echo mysql_error();
466
        $row = mysql_fetch_assoc( $res );
467
        $inkasso = number_format( $row["inkasso"], 2, ',', '' );
468
        return $inkasso;
469
    }
470
 
471
    function getEntsorgung( $b_id )
472
    {
473
        $sql = "SELECT
474
                COUNT(*) AS entsorgung
475
            FROM
476
                artikel_to_Bestellung AS ab
477
            WHERE
478
                ab.artikel=999997
479
            AND
480
                ab.Bestellung=$b_id
481
        ";
482
        $res = mysql_query( $sql );
483
        echo mysql_error();
484
        $row = mysql_fetch_assoc( $res );
485
        $entsorgung = $row["entsorung"];
486
        return $entsorgung;
487
    }
488
?>