@@ -94,13 +94,18 @@ protected function collectData(DateTime $to, ?DateTime $from = null): array
9494 $ args ['to ' ] = $ to ->format ('Y-m-d 23:59:59 ' );
9595 }
9696
97- // === 1️⃣ Statistiche generali (solo tabella `order`) ===
97+ // === 1️⃣ Statistiche generali ===
9898 $ qOrders = "
9999 SELECT
100100 COUNT(o.id) AS total_sales,
101+ COUNT(DISTINCT o.user_id) AS unique_customers,
102+ SUM(o.admin_sub_total) AS net_income,
101103 SUM(o.admin_total_incl_tax) AS total_income,
102- SUM(o.discount_amount) AS total_discounts,
103- SUM(o.tax_amount) AS total_tax,
104+ SUM(o.admin_shipping_amount) AS total_shipping,
105+ SUM(o.admin_discount_amount) AS total_discounts,
106+ SUM(o.admin_tax_amount) AS total_tax,
107+ AVG(o.admin_discount_amount) AS avg_discount,
108+ AVG(o.admin_tax_amount) AS avg_tax,
104109 MAX(o.admin_currency_code) AS admin_currency_code
105110 FROM `order` o
106111 $ where
@@ -122,7 +127,7 @@ protected function collectData(DateTime $to, ?DateTime $from = null): array
122127 $ stmt2 ->execute ($ args );
123128 $ items = $ stmt2 ->fetch (\PDO ::FETCH_ASSOC ) ?: [];
124129
125- // === 3️⃣ Prodotti più venduti (TOP 10 ) ===
130+ // === 3️⃣ Prodotti più venduti (TOP 5 ) ===
126131 $ qTop = "
127132 SELECT
128133 CONCAT(oi.product_class, ':', oi.product_id) AS product_key,
@@ -146,9 +151,9 @@ protected function collectData(DateTime $to, ?DateTime $from = null): array
146151 $ label = method_exists ($ product , 'getSku ' )
147152 ? ($ product ->getSku () ?: $ product ->getName ())
148153 : $ product ->getName ();
149- $ stock = ($ product instanceof PhysicalProductInterface) ?
150- $ product ->getProductStock ()->getCurrentQuantity () :
151- $ this ->getUtils ()->translate ('unlimited ' , locale: $ this ->getCurrentLocale ());
154+ $ stock = ($ product instanceof PhysicalProductInterface)
155+ ? $ product ->getProductStock ()->getCurrentQuantity ()
156+ : $ this ->getUtils ()->translate ('unlimited ' , locale: $ this ->getCurrentLocale ());
152157 } catch (\Exception $ e ) {
153158 $ label = 'n/a ' ;
154159 $ stock = 'n/a ' ;
@@ -181,22 +186,33 @@ protected function collectData(DateTime $to, ?DateTime $from = null): array
181186 $ currency = $ orders ['admin_currency_code ' ] ?? 'EUR ' ;
182187
183188 $ total_sales = (int ) ($ orders ['total_sales ' ] ?? 0 );
189+ $ total_products = (int ) ($ items ['total_products ' ] ?? 0 );
190+ $ unique_customers = (int ) ($ orders ['unique_customers ' ] ?? 0 );
184191 $ total_income = (float ) ($ orders ['total_income ' ] ?? 0 );
192+ $ net_income = (float ) ($ orders ['net_income ' ] ?? 0 );
185193 $ total_tax = (float ) ($ orders ['total_tax ' ] ?? 0 );
186194 $ total_discount = (float ) ($ orders ['total_discounts ' ] ?? 0 );
187- $ total_products = (int ) ($ items [ ' total_products ' ] ?? 0 );
195+ $ total_shipping = (float ) ($ orders [ ' total_shipping ' ] ?? 0 );
188196
189197 $ aov = $ total_sales > 0 ? $ total_income / $ total_sales : 0 ;
198+ $ avg_products = $ total_sales > 0 ? $ total_products / $ total_sales : 0 ;
199+ $ days = $ from ? max (1 , $ to ->diff ($ from )->days ) : 1 ;
200+ $ orders_per_day = $ days > 0 ? $ total_sales / $ days : 0 ;
190201
191202 return [
192- 'total_sales ' => $ total_sales ,
193- 'total_income ' => $ utils ->formatPrice ($ total_income , $ currency ),
194- 'total_tax ' => $ utils ->formatPrice ($ total_tax , $ currency ),
195- 'total_discount ' => $ utils ->formatPrice ($ total_discount , $ currency ),
196- 'total_products ' => $ total_products ,
197- 'average_order ' => $ utils ->formatPrice ($ aov , $ currency ),
198- 'most_sold ' => $ most_sold ,
199- 'top_payment ' => $ payment ['payment_method ' ] ?? 'n/a ' ,
203+ 'total_sales ' => $ total_sales ,
204+ 'unique_customers ' => $ unique_customers ,
205+ 'total_income ' => $ utils ->formatPrice ($ total_income , $ currency ),
206+ 'net_income ' => $ utils ->formatPrice ($ net_income , $ currency ),
207+ 'total_tax ' => $ utils ->formatPrice ($ total_tax , $ currency ),
208+ 'total_discount ' => $ utils ->formatPrice ($ total_discount , $ currency ),
209+ 'total_shipping ' => $ utils ->formatPrice ($ total_shipping , $ currency ),
210+ 'total_products ' => $ total_products ,
211+ 'average_order ' => $ utils ->formatPrice ($ aov , $ currency ),
212+ 'avg_products ' => round ($ avg_products , 2 ),
213+ 'orders_per_day ' => round ($ orders_per_day , 2 ),
214+ 'most_sold ' => $ most_sold ,
215+ 'top_payment ' => $ payment ['payment_method ' ] ?? 'n/a ' ,
200216 ];
201217 }
202218}
0 commit comments