Subversion-Projekte lars-tiefland.content-management

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?
2
    // Dateiname:					$URL: http://server3/svn/content-management/trunk/Warenwirtschaft/includes/statistik/system.php $
3
    // Revision:					$Rev: 3307 $
4
    // letzter Bearbeiter:			$Author: tiefland $
5
    // letztes Bearbeitungsdatum:	$Date: 2011-02-23 12:33:32 +0100 (Mi, 23. Feb 2011) $
6
 
7
    function select_tpl( $formTagName, $options, $optionValueKey, $optionLabelKey,
8
        $preSelectValue = false, $required = false, $onChange = null )
9
    {
10
        if ( $required == false )
11
        {
12
            array_unshift( $options, array( $optionValueKey => false, $optionLabelKey =>
13
                "----------- alle -----------" ) );
14
        }
15
 
16
        $ret = "<select name=\"" . $formTagName . "\" onChange=\"$onChange\">";
17
        foreach ( $options as $option )
18
        {
19
            $ret .= "<option value=\"" . $option[$optionValueKey] . "\"";
20
            if ( $option[$optionValueKey] == $preSelectValue )
21
            {
22
                $ret .= " selected";
23
            }
24
            $ret .= ">" . $option[$optionLabelKey] . "</option>";
25
        }
26
        $ret .= "</select>";
27
        return $ret;
28
    }
29
 
30
    function select( $formTagName, $options, $optionValueKey, $optionLabelKey,
31
        $preSelectValue = false, $required = false, $onChange = null )
32
    {
33
        echo select_tpl( $formTagName, $options, $optionValueKey, $optionLabelKey,
34
            $preSelectValue, $required, $onChange );
35
    }
36
 
37
    function getShops( $shop_id = null, $filter = false, $start = 0, $ende =
38
        0, $start_v = 0, $ende_v = 0 )
39
    {
40
        $buffer = array();
41
        $query = "
42
            SELECT
43
                *
44
            FROM
45
                shops
46
        ";
47
        $query .= ( !empty( $shop_id ) ) ? " WHERE ID=$shop_id" : "";
48
        $result = mysql_query( $query ) or die( mysql_errno() . ": " .
49
            mysql_error() . "<hr>$query<hr>" );
50
        while ( $zeile = mysql_fetch_assoc( $result ) )
51
        {
52
            $buffer[] = $zeile;
53
        }
54
        if ( $filter )
55
        {
56
            //Shops filtern
57
            foreach ( $buffer as $id => $shop )
58
            {
59
                if ( get_umsatz( $start, $ende, $shop["ID"], 0 ) ||
60
                    get_umsatz( $start_v, $ende_v, $shop["ID"], 0 ) )
61
                {
62
                    $shops[] = $shop;
63
                }
64
            }
65
        }
66
        else
67
        {
68
            $shops = $buffer;
69
        }
70
        unset( $buffer );
71
        return $shops;
72
    }
73
 
74
    function getBestellarten()
75
    {
76
        global $user_rechte;
77
        $bestellarten = false;
78
        if ( isset( $user_rechte["Warenwirtschaft"]["bestellung"]["bestellart"] ) )
79
        {
80
            $sql = "SELECT * FROM bestellart";
81
            $query = mysql_query( $sql );
82
            if ( !$query )
83
            {
84
                return false;
85
            }
86
            while ( $db = mysql_fetch_assoc( $query ) )
87
            {
88
                $bestellarten[] = $db;
89
            }
90
        }
91
        return $bestellarten;
92
    }
93
    function getDates()
94
    {
95
        $buffer[] = array( "datum" => strftime( "%G-%m" ), "datumText" =>
96
            strftime( "%B %G" ), );
97
        $buffer[] = array( "datum" => strftime( "%Y-%m", strtotime( "-1 month",
98
            time() ) ), "datumText" => strftime( "%B %Y", strtotime( "-1 month",
99
            time() ) ), );
100
        for ( $i = 2; $i <= 5; $i++ )
101
        {
102
            $buffer[] = array( "datum" => strftime( "%G-%m", strtotime( "-$i months" ) ),
103
                "datumText" => strftime( "%B %G", strtotime( "-$i months" ) ), );
104
        }
105
        if ( strftime( "%w" ) != 1 )
106
        {
107
            $start = strtotime( "last monday" );
108
        }
109
        else
110
        {
111
            $start = time();
112
        }
113
        $buffer[] = array( "datum" => strftime( "%G-%m-%dW", $start ),
114
            "datumText" => strftime( "Woche %V %G", $start ), );
115
        $buffer[] = array( "datum" => strftime( "%G-%m-%dW", strtotime( "-1 week",
116
            $start ) ), "datumText" => strftime( "Woche %V %G", strtotime( "-1 week",
117
            $start ) ), );
118
        for ( $i = 2; $i <= 5; $i++ )
119
        {
120
            $buffer[] = array( "datum" => strftime( "%G-%m-%dW", strtotime( "-$i weeks",
121
                $start ) ), "datumText" => strftime( "Woche %V %G",
122
                strtotime( "-$i weeks", $start ) ), );
123
        }
124
        return $buffer;
125
    }
126
 
127
    function getWeeks( $year = 2008, $maxReverse = 6 )
128
    {
129
        $paramYear = $year;
130
        $scalar = 0;
131
        $weekNow = date( "W", time() );
132
        $monthNow = date( "n", time() );
133
        $dateArray = array();
134
        $monthArray = array();
135
 
136
        $countedWeek = $weekNow;
137
        $countedMonth = $monthNow;
138
 
139
 
140
        for ( $i = $weekNow; $i > ( $weekNow - $maxReverse ); $i-- )
141
        {
142
            $dateArray[$scalar]['week'] = $countedWeek;
143
            $dateArray[$scalar]['year'] = $year;
144
            $dateArray[$scalar]['date_string'] = "kw," . $countedWeek . "," .
145
                $year;
146
            $dateArray[$scalar]['human_string'] = "$countedWeek/$year";
147
 
148
            if ( $countedWeek == 1 )
149
            {
150
                $year -= 1;
151
                $countedWeek = 1;
152
            }
153
            else
154
            {
155
                $countedWeek--;
156
            }
157
 
158
            $scalar++;
159
        }
160
 
161
        $year = $paramYear;
162
 
163
        for ( $i = $monthNow; $i > ( $monthNow - $maxReverse ); $i-- )
164
        {
165
            $dateArray[$scalar]['month'] = $countedMonth;
166
            $dateArray[$scalar]['year'] = $paramYear;
167
            $dateArray[$scalar]['date_string'] = "mon," . $countedMonth .
168
                "," . $year;
169
            $dateArray[$scalar]['human_string'] = date( "M", mktime( 0, 0, 0,
170
                $countedMonth, 1, $year ) ) . " $year";
171
 
172
            if ( $countedMonth == 1 )
173
            {
174
                $year -= 1;
175
                $countedMonth = 12;
176
            }
177
            else
178
            {
179
                $countedMonth--;
180
            }
181
 
182
            $scalar++;
183
        }
184
        return $dateArray;
185
 
186
    }
187
 
188
    /* ################################################################################ */
189
    /*  Select-Abfrage an die Datenbank senden.											*/
190
    /*  Methode um Select-Abfragen zentral an die Datenbank zu senden					*/
191
    /*  NUR Select-Abfragen !															*/
192
    /*  Nur sinnvoll in Verbindung mit while-Schleifen.									*/
193
    /*	Aufrufbeispiel: while($zeile=mysqlSelect("select ....")) {...}					*/
194
    /* ################################################################################ */
195
    function mysqlSelect( $query )
196
    {
197
        if ( !preg_match( "/^select/i", trim( $query ) ) )
198
            // Abbrechen falls query nicht mit "select beginnt"
199
 
200
        {
201
            return false;
202
        }
203
 
204
 
205
        static $status; // Statuswerte der jeweiligen queries speichern
206
        $hash = md5( $query );
207
        if ( !$status[$hash] )
208
        {
209
            $status[$hash] = array( "result" => "", "numRows" => 0,
210
                "endOfResult" => false );
211
        }
212
 
213
        $result = &$status[$hash]["result"];
214
        $numRows = &$status[$hash]["numRows"];
215
        $endOfResult = &$status[$hash]["endOfResult"];
216
 
217
        if ( ( $numRows == 0 ) && $endOfResult == false )
218
            // Beginn des Aufrufes: EoR = false $numRows=0
219
 
220
        {
221
            $result = mysql_query( $query ) or die( mysql_errno() . ": " .
222
                mysql_error() . "<hr>" . pre( $query, true ) . "<hr>" );
223
            $numRows = mysql_num_rows( $result );
224
            $endOfResult = false;
225
        }
226
 
227
        if ( $numRows > 0 ) // Mittendrinn: numRows > 0
228
 
229
        {
230
            if ( $numRows == 1 )
231
            {
232
                $endOfResult = true;
233
            } // letzter durchlauf mit Daten
234
            $numRows--; //
235
            return mysql_fetch_assoc( $result );
236
        }
237
 
238
        if ( ( $numRows == 0 ) && $endOfResult == true )
239
            // letzter durchlauf
240
 
241
        {
242
            $endOfResult = false; // Werte zurücksetzen
243
            return false; // Ende zurückliefern
244
        }
245
 
246
        return false;
247
    }
248
 
249
    /**
250
     *
251
     *	@param  string	Datum Filter
252
     *	@param	int		ID des Shops; Default: alle Shops
253
     *	@param	int		Debug-Modus; Default: 0
254
     *
255
     *	@return	int		Anzahl der Bestellungen
256
     */
257
    function get_anz_bestellungen( $start, $ende, $shop_id = null, $debug =
258
        0, $bestellart = 0 )
259
    {
260
        $b_ges = 0;
261
        $start = date( "Y-m-d", $start );
262
        $ende = date( "Y-m-d", $ende );
263
        $sql = "
264
			SELECT
265
				COUNT(*) AS anz
266
			FROM
267
				Bestellung b
268
			WHERE
269
				b.erstellt_am >='$start' AND b.erstellt_am <'$ende'
270
		";
271
        $sql .= ( !empty( $shop_id ) ) ? " AND b.shops_ID=$shop_id" : "";
272
        $sql .= ( !empty( $bestellart ) ) ? " AND b.bestellart_id=$bestellart" :
273
            "";
274
        $res = mysql_query( $sql );
275
        if ( $debug )
276
        {
277
            echo "$sql==>$b_ges<br>";
278
        }
279
        $row = mysql_fetch_assoc( $res );
280
        if ( $row["anz"] )
281
        {
282
            $b_ges = $row["anz"];
283
        }
284
        else
285
        {
286
            //Bestellungen aus Hilfstabelle lesen
287
            $sql_ba = "
288
			SELECT
289
				SUM(best_anzahl) AS anz
290
			FROM
291
				bestellung_archiv ba
292
			WHERE
293
				ba.datum >='$start' AND ba.datum <'$ende'
294
            ";
295
            $sql_ba .= ( !empty( $shop_id ) ) ? " AND ba.shops_id=$shop_id" :
296
                "";
297
            $sql_ba .= ( !empty( $bestellart ) ) ? " AND ba.bestellart_id=$bestellart" :
298
                "";
299
            $res_ba = mysql_query( $sql_ba );
300
            echo mysql_error();
301
            $row = mysql_fetch_assoc( $res_ba );
302
            if ( $row["anz"] )
303
            {
304
                $b_ges = $row["anz"];
305
            }
306
            if ( $debug )
307
            {
308
                echo "$sql_ba==>$b_ges<br>";
309
            }
310
        }
311
        return $b_ges;
312
    }
313
 
314
    /**
315
     *
316
     *	@param  string	Datum Filter
317
     *	@param	int		ID des Shops; Default: alle Shops
318
     *	@param	int		Debug-Modus; Default: 0
319
     *
320
     *	@return	int		Anzahl der Bestellungen
321
     */
322
    function get_pos_artikel( $start, $ende, $shop_id = null, $debug = 0, $bestellart =
323
 
324
    {
325
        $start = date( "Y-m-d", $start );
326
        $ende = date( "Y-m-d", $ende );
327
        $sql = "
328
			SELECT
329
				COUNT(*) AS anz
330
			FROM
331
				artikel_to_Bestellung a,
332
				Bestellung b
333
			WHERE
334
				b.ID=a.Bestellung
335
			AND
336
				b.erstellt_am >='$start' AND b.erstellt_am <'$ende'
337
		";
338
        $sql .= ( !empty( $shop_id ) ) ? " AND b.shops_ID=$shop_id" : "";
339
        $sql .= ( !empty( $bestellart ) ) ? " AND b.bestellart_id=$bestellart" :
340
            "";
341
        $res = mysql_query( $sql );
342
        $row = mysql_fetch_assoc( $res );
343
        $b_ges = $row["anz"];
344
        if ( $debug )
345
        {
346
            echo mysql_error() . "<br>";
347
            echo "$sql==>$b_ges<br>";
348
        }
349
        return $b_ges;
350
    }
351
 
352
    /**
353
     *
354
     *	@param  string	Datum Filter
355
     *	@param	int		ID des Shops; Default: alle Shops
356
     *	@param	int		Debug-Modus; Default: 0
357
     *
358
     *	@return	int		Anzahl der Bestellungen
359
     */
360
    function get_anz_artikel( $start, $ende, $shop_id = null, $debug = 0, $bestellart =
361
 
362
    {
363
        $start = date( "Y-m-d", $start );
364
        $ende = date( "Y-m-d", $ende );
365
        $sql = "
366
			SELECT
367
				SUM(Menge) AS anz
368
			FROM
369
				artikel_to_Bestellung a,
370
				Bestellung b
371
			WHERE
372
				b.ID=a.Bestellung
373
			AND
374
				b.erstellt_am >='$start' AND b.erstellt_am <'$ende'
375
		";
376
        $sql .= ( !empty( $shop_id ) ) ? " AND b.shops_ID=$shop_id" : "";
377
        $sql .= ( !empty( $bestellart ) ) ? " AND b.bestellart_id=$bestellart" :
378
            "";
379
        $res = mysql_query( $sql );
380
        $row = mysql_fetch_assoc( $res );
381
        $b_ges = $row["anz"];
382
        if ( $debug )
383
        {
384
            echo mysql_error() . "<br>";
385
            echo "$sql==>$b_ges<br>";
386
        }
387
        return $b_ges;
388
    }
389
 
390
    /**
391
     *
392
     *	@param  string	Datum Filter
393
     *	@param	int		ID des Shops; Default: alle Shops
394
     *	@param	int		Debug-Modus; Default: 0
395
     *
396
     *	@return	int		Umsatz
397
     */
398
    function get_umsatz( $start, $ende, $shop_id = null, $debug = 0, $bestellart =
399
 
400
    {
401
        $start = date( "Y-m-d", $start );
402
        $ende = date( "Y-m-d", $ende );
403
        $sql = "
404
			SELECT
405
				sum(Preis_pro_Stueck * Menge) as Umsatz
406
			FROM
407
				artikel_to_Bestellung a,
408
				Bestellung b
409
			WHERE
410
				b.ID=a.Bestellung
411
			AND
412
				b.erstellt_am >='$start' AND b.erstellt_am <'$ende'
413
		";
414
        $sql .= ( !empty( $shop_id ) ) ? " AND b.shops_ID=$shop_id" : "";
415
        $sql .= ( !empty( $bestellart ) ) ? " AND b.bestellart_id=$bestellart" :
416
            "";
417
        $res = mysql_query( $sql );
418
        $row = mysql_fetch_assoc( $res );
419
        $umsatz = $row["Umsatz"];
420
        if ( $debug )
421
        {
422
            echo "$sql==>$umsatz<br>";
423
        }
424
        if ( !$row["Umsatz"] )
425
        {
426
            //Umsatz aus Hilfstabelle lesen
427
            $sql_ba = "
428
			SELECT
429
				SUM(umsatz) as Umsatz
430
			FROM
431
				bestellung_archiv ba
432
			WHERE
433
				ba.datum >='$start' AND ba.datum <'$ende'
434
            ";
435
            $sql_ba .= ( !empty( $shop_id ) ) ? " AND ba.shops_id=$shop_id" :
436
                "";
437
            $sql .= ( !empty( $bestellart ) ) ? " AND ba.bestellart_id=$bestellart" :
438
                "";
439
            $res = mysql_query( $sql_ba );
440
            if ( $res )
441
            {
442
                if ( mysql_num_rows( $res ) )
443
                {
444
                    $row = mysql_fetch_assoc( $res );
445
                    $umsatz = $row["Umsatz"];
446
                }
447
            }
448
            if ( $debug )
449
            {
450
                echo "$sql_ba==>$umsatz<br>";
451
            }
452
        }
453
        return $umsatz;
454
    }
455
?>