Ik heb een klein factuur systeempje, en die kan oa. wat totalen genereren, 1 van de queries is bv. het totaal aantal verkochte producten in een bepaalde maand, van een bepaalde leverancier.
Hiervan willen we dan totaal aantal verkochte producten, totale inkoop, totale verkoop etc.
Beetje gesnoeid in de layout, hopelijk is het leesbaar:
De LOCATE('FV',...) is bedoeld om alleen de bestellingen mee te rekenen waar de Factuur Verzonden is.
De uitkomst hiervan is dus niet correct. De product_aantal_maal_besteld is 1 (moet 3 zijn), de product_inkoop_totaal is slechts 1 van de 3 records - maar de product_verkoop_totaal is wel correct!
I don't get it
Hiervan willen we dan totaal aantal verkochte producten, totale inkoop, totale verkoop etc.
Beetje gesnoeid in de layout, hopelijk is het leesbaar:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| $q = " SELECT A_Producten.ProductID as product_id, A_Producten.Productnaam as product_naam, SUM( IF( LOCATE('FV',A_Bestellingen.Status), A_Bestellingproducten.Productaantal, 0 ) ) as product_aantal_maal_besteld, SUM( IF( LOCATE('FV',A_Bestellingen.Status), A_Bestellingproducten.Productaantal, 0 ) * A_Bestellingproducten.Productverkoop ) as product_verkoop_totaal, SUM( IF( LOCATE('FV',A_Bestellingen.Status), A_Bestellingproducten.Productaantal, 0 ) * IF( A_Producten.Productvaluta = 'perc' , A_Bestellingproducten.Productverkoop * A_Producten.Productinkoop / 100, A_Producten.Productinkoop ) ) as product_inkoop_totaal, A_Crediteuren.Bedrijfsnaam as product_crediteur_bedrijfsnaam, A_Bestellingen.Status as bestelling_status FROM A_Producten LEFT JOIN A_Bestellingproducten ON (A_Bestellingproducten.ProductID = A_Producten.ProductID) LEFT JOIN A_Bestellingen ON (A_Bestellingproducten.BestellingID = A_Bestellingen.BestellingID) LEFT JOIN A_Crediteuren ON (A_Crediteuren.CrediteurID = A_Producten.CrediteurID) WHERE !A_Producten.Deleted AND A_Producten.CrediteurID = ".(int)$crediteurid." AND FROM_UNIXTIME(A_Bestellingen.Datum,'%m') = '". date('m',mktime(0, 0, 0, $date[0], 1, $date[1])) ."' AND FROM_UNIXTIME(A_Bestellingen.Datum,'%Y') = '". date('Y',mktime(0, 0, 0, $date[0], 1, $date[1])) ."' GROUP BY A_Producten.ProductID ORDER BY A_Producten.Productnaam ASC"; |
De LOCATE('FV',...) is bedoeld om alleen de bestellingen mee te rekenen waar de Factuur Verzonden is.
De uitkomst hiervan is dus niet correct. De product_aantal_maal_besteld is 1 (moet 3 zijn), de product_inkoop_totaal is slechts 1 van de 3 records - maar de product_verkoop_totaal is wel correct!
I don't get it
Klaar voor een nieuwe uitdaging.