From 611bd0d3ba25f60884d76c4714a865c18d8f5e3c Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Fri, 6 Oct 2023 15:28:11 +0200 Subject: [PATCH] aggiustamento in calcolo statistiche --- .../Http/Controllers/StatisticsController.php | 63 ++++++++++++++++--- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/code/app/Http/Controllers/StatisticsController.php b/code/app/Http/Controllers/StatisticsController.php index 23b3a6e3..5259bfd9 100644 --- a/code/app/Http/Controllers/StatisticsController.php +++ b/code/app/Http/Controllers/StatisticsController.php @@ -7,6 +7,7 @@ use App\Http\Controllers\Controller; use App\User; +use App\Order; use App\Supplier; use App\Category; use App\Booking; @@ -105,22 +106,54 @@ private function createBookingQuery($query, $type, $start, $end, $target, $suppl return $query; } - private function getSummary($start, $end, $type, $target) + /* + Laddove le statistiche sul valore consegnato possono essere rapidamente + estrapolate usando i valori consolidati sul DB (nell'attributo + final_price di BookedProduct), per il prenotato รจ necessario rifare + tutti i calcoli daccapo. Per ottenere i prezzi storicizzati al momento + della creazione dell'ordine, o per contemplare le differenze di prezzo + delle varianti + */ + private function basicSummaryForAll($start, $end, $target) { $data = []; + $bookings = $this->createBookingQuery(Booking::query(), 'all', $start, $end, $target, null)->angryload()->get(); - $data_for_suppliers_query = BookedProduct::whereHas('booking', function($query) use ($type, $start, $end, $target) { - $this->createBookingQuery($query, $type, $start, $end, $target, null); - })->join('bookings', 'booked_products.booking_id', '=', 'bookings.id')->join('orders', 'bookings.order_id', '=', 'orders.id')->groupBy('orders.supplier_id'); + $orders = []; + foreach($bookings as $booking) { + if (isset($orders[$booking->order_id]) == false) { + $orders[$booking->order_id] = []; + } - if ($type == 'all') { - $data_for_suppliers_query->selectRaw('orders.supplier_id, SUM(products.price * booked_products.quantity) as price')->join('products', 'booked_products.product_id', '=', 'products.id'); + $orders[$booking->order_id][] = $booking; } - else { - $data_for_suppliers_query->selectRaw('orders.supplier_id, SUM(final_price) as price'); + + foreach($orders as $id => $bookings) { + $order = Order::find($id); + $summary = $order->reduxData(['bookings' => $bookings]); + + $name = $order->supplier_id; + if (isset($data[$name]) == false) { + $data[$name] = (object) [ + 'users' => 0, + 'value' => 0, + 'name' => $order->supplier->printableName(), + ]; + } + + $data[$name]->value += $summary->price; } - $data_for_suppliers = $data_for_suppliers_query->get(); + return $data; + } + + private function basicSummaryForShipped($start, $end, $type, $target) + { + $data = []; + + $data_for_suppliers = BookedProduct::selectRaw('orders.supplier_id, SUM(final_price) as price')->whereHas('booking', function($query) use ($type, $start, $end, $target) { + $this->createBookingQuery($query, $type, $start, $end, $target, null); + })->join('bookings', 'booked_products.booking_id', '=', 'bookings.id')->join('orders', 'bookings.order_id', '=', 'orders.id')->groupBy('orders.supplier_id')->get(); foreach($data_for_suppliers as $dfs) { $name = $dfs->supplier_id; @@ -135,6 +168,18 @@ private function getSummary($start, $end, $type, $target) $data[$name]->value += $dfs->price; } + return $data; + } + + private function getSummary($start, $end, $type, $target) + { + if ($type == 'all') { + $data = $this->basicSummaryForAll($start, $end, $target); + } + else { + $data = $this->basicSummaryForShipped($start, $end, $type, $target); + } + $data_for_user = $this->createBookingQuery(Booking::query(), $type, $start, $end, $target, null)->whereHas('user', function($query) { $query->whereNull('parent_id'); })->selectRaw('supplier_id, COUNT(DISTINCT(bookings.user_id)) as total')->join('orders', 'bookings.order_id', '=', 'orders.id')->groupBy('supplier_id')->get();