Subversion-Projekte lars-tiefland.content-management

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
    // zu verarbeitende Shops einlesen
4
    $shops = file( "shops.txt" );
5
 
6
    // wichtige Konstanten
7
 
8
    define( "FILE_PATH", "/var/www/HQStransfer/" );
9
    define( "ARCHIVE_PATH_BASE", "/var/cronjobs/HQSexport/archiv/" );
10
    define( "ARCHIVE_PATH_EXT", date( "Y" ) . "/" . date( "m" ) );
11
    define( "ARCHIVE_PATH", ARCHIVE_PATH_BASE . ARCHIVE_PATH_EXT . "/" );
12
    define( "TRENNER", "\r\n" );
13
 
14
    //wichtige Variablen
15
 
16
    $zeit = date( "Y-m-d-Gi" );
17
    $handle = date( "Y-m-d G:i:s", time() );
18
 
19
    //Archiv-Verzeichnisse erstellen
20
    if ( !file_exists( ARCHIVE_PATH ) )
21
    {
22
        chdir( ARCHIVE_PATH_BASE );
23
        $p = explode( "/", ARCHIVE_PATH_EXT );
24
        foreach ( $p as $dir )
25
        {
26
            if ( !file_exists( $dir ) )
27
            {
28
                $res_d = mkdir( $dir );
29
                if ( !$res_d )
30
                {
31
                    mail( "tiefland@weban.de",
32
                        "Fehler beim Export der Bestellungen auf www.mediaran.de",
33
                        "Konnte Verzeichnis $dir NICHT erstellen!\n",
34
                        "FROM: info@mediaran.de", "-F info@mediaran.de" );
35
                }
36
                chdir( $dir );
37
            }
38
        }
39
    }
40
    // SQL-Abfragen und Dateien erstellen
41
    $conn = mysql_connect( "localhost", "root", "" );
42
    if ( !$conn )
43
    {
44
        mail( "tiefland@weban.de",
45
            "Fehler beim Export der Bestellungen auf www.mediaran.de",
46
            mysql_error() . "\n", "FROM: info@mediaran.de",
47
            "-F info@mediaran.de" );
48
        exit;
49
    }
50
    $db = mysql_select_db( "mediaran_de" );
51
    if ( !$db )
52
    {
53
        mail( "tiefland@weban.de",
54
            "Fehler beim Export der Bestellungen auf www.mediaran.de",
55
            mysql_error() . "\n", "FROM: info@mediaran.de",
56
            "-F info@mediaran.de" );
57
        exit;
58
    }
59
    foreach ( $shops as $shop )
60
    {
61
        $shop = trim( $shop );
62
        $sql = "
63
			update
64
				artikel_to_Bestellung,
65
				Bestellung,
66
				shops
67
			set
68
				artikel_to_Bestellung.exported_rechnung = '$handle'
69
			WHERE
70
				artikel_to_Bestellung.rechnungStatus = 'schreiben'
71
			and
72
				artikel_to_Bestellung.Bestellung = Bestellung.ID
73
			and
74
				Bestellung.shops_ID = shops.ID
75
			and
76
				shops.Name = '$shop'
77
		";
78
        $res = mysql_query( $sql );
79
        if ( !$res )
80
        {
81
            mail( "tiefland@weban.de",
82
                "Fehler beim Export der Bestellungen auf www.mediaran.de",
83
                mysql_error() . "\n$sql\n", "FROM: info@mediaran.de",
84
                "-F info@mediaran.de" );
85
            exit;
86
        }
87
        /*--
88
        -- Liste der Bestellungen erstellen*/
89
        $sql = "
90
			create temporary table bestellungID engine=heap
91
				select
92
					Bestellung.ID as Bestellung,
93
					shops.Language,
94
					shops.Name
95
				from
96
					artikel_to_Bestellung,
97
					Bestellung,
98
					shops
99
				where
100
					artikel_to_Bestellung.exported_rechnung = '$handle'
101
				and
102
					artikel_to_Bestellung.Bestellung = Bestellung.ID
103
				and
104
					Bestellung.shops_ID = shops.ID
105
				and
106
					shops.Name = '$shop'
107
				group by
108
					Bestellung.ID;
109
		";
110
        $res = mysql_query( $sql );
111
        if ( !$res )
112
        {
113
            mail( "tiefland@weban.de",
114
                "Fehler beim Export der Bestellungen auf www.mediaran.de",
115
                mysql_error() . "\n$sql\n", "FROM: info@mediaran.de",
116
                "-F info@mediaran.de" );
117
            die();
118
        }
119
        /*--
120
        -- Auswählen aller Bestellungen die jetzt exportiert werden sollen*/
121
        $sql_b = "
122
			select
123
				distinct
124
					Bestellung.ID as A001,
125
					LCASE(Logins.E_Mail) as A002,
126
					Bestellung.ID as POSI,
127
					if (
128
						trim(
129
							concat(
130
								Bestellung.liefer_Firma,
131
								Bestellung.liefer_Vorname,
132
								Bestellung.liefer_Name,
133
								Bestellung.liefer_Strasse,
134
								Bestellung.liefer_Str_Nr,
135
								Bestellung.liefer_PLZ,
136
								Bestellung.liefer_Ort,
137
								Bestellung.liefer_Land
138
							)
139
						) = '',
140
						Bemerkung,
141
						concat(
142
							'LIEFERADRESSE:\n',
143
							Bestellung.liefer_Firma, '\n',
144
							Bestellung.liefer_Vorname, '\n',
145
							Bestellung.liefer_Name, '\n',
146
							Bestellung.liefer_Strasse,' ',Bestellung.liefer_Str_Nr, '\n',
147
							Bestellung.liefer_PLZ,' ',Bestellung.liefer_Ort, '\n',
148
							Bestellung.liefer_Land, '\n\n',
149
							repeat('-',50), '\n\n',
150
							Bemerkung
151
						)
152
					) as A003
153
			from
154
				Logins,
155
				Bestellung,
156
				shops,
157
				artikel_to_Bestellung
158
			where
159
				Logins.ID = Bestellung.Login
160
			and
161
				artikel_to_Bestellung.exported_rechnung = '$handle'
162
			and
163
				artikel_to_Bestellung.Bestellung = Bestellung.ID
164
			AND
165
				shops.Name='$shop'
166
			AND
167
				Bestellung.shops_ID=shops.ID
168
		";
169
        $res_b = mysql_query( $sql_b );
170
        if ( !$res_b )
171
        {
172
            mail( "tiefland@weban.de",
173
                "Fehler beim Export der Bestellungen auf www.mediaran.de",
174
                mysql_error() . "\n$sql_b\n", "FROM: info@mediaran.de",
175
                "-F info@mediaran.de" );
176
            die();
177
        }
178
        if ( mysql_num_rows( $res_b ) )
179
        {
180
            //Kundendaten liegen vor
181
            $fName = FILE_PATH . "export_$shop" . ".txt";
182
            $fName_archive = ARCHIVE_PATH . $zeit . " $shop.txt";
183
            $f = fopen( $fName, "a" );
184
            fwrite( $f, TRENNER );
185
            while ( $row_b = mysql_fetch_assoc( $res_b ) )
186
            {
187
                /*--
188
                -- Auswählen aller Kundendaten der Bestellungen die jetzt exportiert werden sollen*/
189
                $sql_l = "
190
					select
191
						distinct
192
					--		Logins.ID as K001,
193
							LCASE(Logins.E_Mail) as K001,
194
							IFNULL(Logins.Anrede,'') as K002,
195
							IF (Logins.Name2 = '', IF(ISNULL(Logins.Vorname), Logins.Name1, concat(Logins.Vorname, ' ', Logins.Name1)), Logins.Name2) as K003,
196
							IF (Logins.Name2 != '', IF(ISNULL(Logins.Vorname), Logins.Name1, concat(Logins.Vorname, ' ', Logins.Name1)), Logins.Name2) as K004,
197
							concat(Logins.Strasse,' ',Logins.Hausnummer) as K005,
198
							concat(Logins.PLZ,' ',Logins.ORT) as K006,
199
							'' as K007,
200
							Logins.Telefon as K008,
201
							'' as K009,
202
							LCASE(Logins.E_Mail) as K010,
203
							Logins.Fax as K011,
204
							'' as K012,
205
							'' as K013,
206
							'' as K014,
207
							'' as K015,
208
							'' as K016,
209
							'' as K017,
210
							'' as K018,
211
							'' as K019,
212
							'' as K020,
213
							'' as K021,
214
							'' as K022,
215
							'' as K023
216
						from
217
							Logins,
218
							Bestellung,
219
							artikel_to_Bestellung
220
						where
221
							artikel_to_Bestellung.exported_rechnung = '$handle'
222
						and
223
							artikel_to_Bestellung.Bestellung = Bestellung.ID
224
						and
225
							Bestellung.Login = Logins.ID
226
						AND
227
							Bestellung.ID=" . $row_b["A001"];
228
                $res_l = mysql_query( $sql_l );
229
                if ( !$res_l )
230
                {
231
                    mail( "tiefland@weban.de",
232
                        "Fehler beim Export der Bestellungen auf www.mediaran.de",
233
                        mysql_error() . "\n$sql_l\n",
234
                        "FROM: info@mediaran.de", "-F info@mediaran.de" );
235
                    die();
236
                }
237
                fwrite( $f, "<KUNDEN>" . TRENNER );
238
                $row_l = mysql_fetch_assoc( $res_l );
239
                foreach ( $row_l as $l_feld => $l_val )
240
                {
241
                    if ( $l_val )
242
                    {
243
                        fwrite( $f, "\t<$l_feld>$l_val" . TRENNER );
244
                    }
245
                }
246
                fwrite( $f, "</KUNDEN>" . TRENNER );
247
                fwrite( $f, "<AUFTRAG>" . TRENNER );
248
                foreach ( $row_b as $b_feld => $b_val )
249
                {
250
                    if ( $b_val )
251
                    {
252
                        if ( $b_feld == "A003" )
253
                        {
254
                            $b_val = str_replace( "\r\n", '\R\N', $b_val );
255
                            $b_val = str_replace( "\r", '\R\N', $b_val );
256
                            $b_val = str_replace( "\n", '\R\N', $b_val );
257
                        }
258
                        fwrite( $f, "\t<$b_feld>$b_val" . TRENNER );
259
                    }
260
                }
261
                /*--
262
                -- Artikeldaten aller Artikel der georderten Bestellungen*/
263
                $sql_d = "SET @pos = 0";
264
                mysql_query( $sql_d );
265
                $sql_a = "
266
					select distinct
267
						artikel_to_Bestellung.Bestellung as P000,
268
						@pos:=@pos+1 as P001,
269
						artikel.kennung as P002,
270
						artikel_to_Bestellung.Menge as P003,
271
						artikel_to_Bestellung.Preis_pro_Stueck as P004,
272
						IF (artikel_to_Bestellung.Bezeichnung!='',substring(artikel_to_Bestellung.Bezeichnung,1,30),substring(artikel.kurzbezeichnung,1,30)) AS P006,
273
						IF (artikel_to_Bestellung.Bezeichnung!='',substring(artikel_to_Bestellung.Bezeichnung,31,30),substring(artikel.kurzbezeichnung,31,30)) AS P007
274
					from
275
						artikel_to_Bestellung,
276
						bestellungID,
277
						artikel
278
					where
279
						artikel_to_Bestellung.Bestellung=bestellungID.Bestellung
280
					AND
281
						artikel_to_Bestellung.Bestellung=" . $row_b["A001"] . "
282
					and
283
						artikel_to_Bestellung.artikel = artikel.ID
284
					and
285
						artikel.Language = bestellungID.Language
286
					and
287
					(
288
						artikel.Father = -3
289
						or
290
							artikel_to_Bestellung.rechnungStatus = 'schreiben'
291
						or
292
							artikel_to_Bestellung.rechnungStatus = 'geschrieben'
293
					)
294
					order by artikel.Father asc
295
				";
296
                $res_a = mysql_query( $sql_a );
297
                if ( !$res_a )
298
                {
299
                    mail( "tiefland@weban.de",
300
                        "Fehler beim Export der Bestellungen auf www.mediaran.de",
301
                        mysql_error() . "\n$sql_a\n",
302
                        "FROM: info@mediaran.de", "-F info@mediaran.de" );
303
                    die();
304
                }
305
                while ( $row_a = mysql_fetch_assoc( $res_a ) )
306
                {
307
                    fwrite( $f, "\t<POSI>" . TRENNER );
308
                    foreach ( $row_a as $a_feld => $a_val )
309
                    {
310
                        if ( $a_val )
311
                        {
312
                            if ( $a_feld == "P006" || $a_feld == "P007" )
313
                            {
314
                                $a_val = str_replace( "\r", '\R', $a_val );
315
                                $a_val = str_replace( "\n", '\N', $a_val );
316
                            }
317
                            fwrite( $f, "\t\t<$a_feld>$a_val" . TRENNER );
318
                        }
319
                    }
320
                    fwrite( $f, "\t</POSI>" . TRENNER );
321
                }
322
                fwrite( $f, "</AUFTRAG>" . TRENNER );
323
            }
324
 
325
            fclose( $f );
326
            // Datei sichern
327
            $res_f = copy( $fName, $fName_archive );
328
            if ( !$res_f )
329
            {
330
                mail( "tiefland@weban.de",
331
                    "Fehler beim Export der Bestellungen auf www.mediaran.de",
332
                    "Konnte Datei $fName NICHT nach $fName_archive kopieren!\n",
333
                    "FROM: info@mediaran.de", "-F info@mediaran.de" );
334
            }
335
 
336
            /*--
337
            -- Aufräumen*/
338
            $sql_d = "drop table bestellungID";
339
            mysql_query( $sql_d );
340
        }
341
    }
342
?>