Skip to content

Commit

Permalink
statistiche su tutte le prenotazioni, non solo quelle consegnate. closes
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Sep 30, 2023
1 parent c460f6d commit 70cf8e2
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 23 deletions.
67 changes: 47 additions & 20 deletions code/app/Http/Controllers/StatisticsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ private function formatCSV($data)
return $ret;
}

private function createBookingQuery($query, $start, $end, $target, $supplier)
private function createBookingQuery($query, $type, $start, $end, $target, $supplier)
{
$query->where('delivery', '!=', '0000-00-00')->where('delivery', '>=', $start)->where('delivery', '<=', $end);
if ($type == 'all') {
$query->where('bookings.updated_at', '>=', $start)->where('bookings.updated_at', '<=', $end);
}
else {
$query->where('bookings.delivery', '!=', '0000-00-00')->where('bookings.delivery', '>=', $start)->where('bookings.delivery', '<=', $end);
}

if ($supplier) {
$query->whereHas('order', function ($query) use ($supplier) {
Expand All @@ -100,12 +105,22 @@ private function createBookingQuery($query, $start, $end, $target, $supplier)
return $query;
}

private function getSummary($start, $end, $target)
private function getSummary($start, $end, $type, $target)
{
$data = [];
$data_for_suppliers = BookedProduct::selectRaw('supplier_id, SUM(final_price) as price')->whereHas('booking', function($query) use ($start, $end, $target) {
$this->createBookingQuery($query, $start, $end, $target, null);
})->join('bookings', 'booked_products.booking_id', '=', 'bookings.id')->join('orders', 'bookings.order_id', '=', 'orders.id')->groupBy('supplier_id')->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');

if ($type == 'all') {
$data_for_suppliers_query->selectRaw('orders.supplier_id, SUM(price) as price')->join('products', 'booked_products.product_id', '=', 'products.id');
}
else {
$data_for_suppliers_query->selectRaw('orders.supplier_id, SUM(final_price) as price');
}

$data_for_suppliers = $data_for_suppliers_query->get();

foreach($data_for_suppliers as $dfs) {
$name = $dfs->supplier_id;
Expand All @@ -120,7 +135,7 @@ private function getSummary($start, $end, $target)
$data[$name]->value += $dfs->price;
}

$data_for_user = $this->createBookingQuery(Booking::query(), $start, $end, $target, null)->whereHas('user', function($query) {
$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();

Expand All @@ -132,8 +147,16 @@ private function getSummary($start, $end, $target)
}

$categories = [];
$data_for_categories = BookedProduct::selectRaw('product_id, SUM(final_price) as price, category_id')->whereHas('booking', function($query) use ($start, $end, $target) {
$this->createBookingQuery($query, $start, $end, $target, null);

if ($type == 'all') {
$price_column = 'price';
}
else {
$price_column = 'final_price';
}

$data_for_categories = BookedProduct::selectRaw('product_id, SUM(' . $price_column . ') as price, category_id')->whereHas('booking', function($query) use ($type, $start, $end, $target) {
$this->createBookingQuery($query, $type, $start, $end, $target, null);
})->join('products', 'booked_products.product_id', '=', 'products.id')->groupBy('product_id', 'category_id')->get();

foreach($data_for_categories as $dfc) {
Expand All @@ -153,17 +176,12 @@ private function getSummary($start, $end, $target)
return [$data, $categories];
}

private function getSupplier($start, $end, $target, $supplier)
private function getSupplier($start, $end, $type, $target, $supplier)
{
$data = [];
$categories = [];

/*
TODO: ottimizzare usando gli stessi criteri di getSummary() anziché
iterare tutte le prenotazioni ed i prodotti
*/

$bookings = $this->createBookingQuery(Booking::query(), $start, $end, $target, $supplier)->with('order', 'products')->get();
$bookings = $this->createBookingQuery(Booking::query(), $type, $start, $end, $target, $supplier)->with(['order', 'products'])->get();

foreach ($bookings as $booking) {
foreach ($booking->products as $product) {
Expand All @@ -185,8 +203,16 @@ private function getSupplier($start, $end, $target, $supplier)
}

$data[$name]->users[$booking->user_id] = true;
$data[$name]->value += $product->final_price;
$categories[$product->product->category_id]->value += $product->final_price;

if ($type == 'all') {
$price = $product->product->price;
}
else {
$price = $product->final_price;
}

$data[$name]->value += $price;
$categories[$product->product->category_id]->value += $price;
}
}

Expand All @@ -202,17 +228,18 @@ public function show(Request $request, $id)
$start = decodeDate($request->input('startdate'));
$end = decodeDate($request->input('enddate'));
$target = fromInlineId($request->input('target'));
$type = $request->input('type') ?: 'shipped';
$csv_headers = [];

switch ($id) {
case 'summary':
list($data, $categories) = $this->getSummary($start, $end, $target);
list($data, $categories) = $this->getSummary($start, $end, $type, $target);
$csv_headers = [_i('Fornitore'), _i('Valore Ordini'), _i('Utenti Coinvolti')];
break;

case 'supplier':
$supplier = $request->input('supplier');
list($data, $categories) = $this->getSupplier($start, $end, $target, $supplier);
list($data, $categories) = $this->getSupplier($start, $end, $type, $target, $supplier);
$csv_headers = [_i('Prodotto'), _i('Valore Ordini'), _i('Utenti Coinvolti')];
break;
}
Expand Down
2 changes: 1 addition & 1 deletion code/public/js/gasdotto.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion code/public/js/gasdotto.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion code/public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"/js/gasdotto.js": "/js/gasdotto.js?id=5a4234c5e8604efe351a8301bdd8739c",
"/js/gasdotto.js": "/js/gasdotto.js?id=79d77a475d774ebdfee7955becb757a3",
"/css/gasdotto.css": "/css/gasdotto.css?id=03185e0a3bee7058398f38f8838c191b"
}
2 changes: 2 additions & 0 deletions code/resources/assets/js/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Statistics {
startdate: $('#stats-summary-form input[name=startdate]').val(),
enddate: $('#stats-summary-form input[name=enddate]').val(),
target: $('#stats-summary-form input[name=target]').val(),
type: $('#stats-summary-form select[name=type]').val(),
format: 'json',
}, (data) => {
this.doGraphs('generic', data);
Expand All @@ -93,6 +94,7 @@ class Statistics {
startdate: $('#stats-supplier-form input[name=startdate]').val(),
enddate: $('#stats-supplier-form input[name=enddate]').val(),
target: $('#stats-supplier-form input[name=target]').val(),
type: $('#stats-summary-form select[name=type]').val(),
format: 'json',
}, (data) => {
this.doGraphs('products', data);
Expand Down
14 changes: 14 additions & 0 deletions code/resources/views/commons/statspage.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
@php
$shipped_count = App\Booking::where('status', 'shipped')->count();
if ($shipped_count == 0) {
$default_type = 'all';
}
else {
$default_type = 'shipped';
}
@endphp

<div class="card mb-2">
<div class="card-header">
<h3>{{ _i('Statistiche Generali') }}</h3>
Expand All @@ -7,6 +19,7 @@
<div class="col-12 col-lg-6">
<form id="stats-summary-form" class="form-horizontal">
@include('commons.genericdaterange')
<x-larastrap::select name="type" :label="_i('Tipo')" :options="['all' => _i('Tutto'), 'shipped' => _i('Consegnato')]" :value="$default_type" />
<input type="hidden" name="target" value="{{ inlineId($target) }}">

<div class="form-group">
Expand Down Expand Up @@ -52,6 +65,7 @@
<form id="stats-supplier-form" class="form-horizontal">
<x-larastrap::selectobj name="supplier" :label="_i('Fornitore')" :options="$currentgas->suppliers" />
@include('commons.genericdaterange')
<x-larastrap::select name="type" :label="_i('Tipo')" :options="['all' => _i('Tutto'), 'shipped' => _i('Consegnato')]" :value="$default_type" />
<input type="hidden" name="target" value="{{ inlineId($target) }}">

<div class="form-group">
Expand Down

0 comments on commit 70cf8e2

Please sign in to comment.