Miglioramento strutturale statistiche Articoli

This commit is contained in:
Thomas Zilio 2019-07-10 16:31:10 +02:00
parent 5eb283af5c
commit 48de0cfb46
5 changed files with 168 additions and 70 deletions

View File

@ -10,7 +10,7 @@ echo '
'.tr('Periodi temporali').' '.tr('Periodi temporali').'
</h4> </h4>
<div class="box-tools pull-right"> <div class="box-tools pull-right">
<button class="btn btn-warning btn-xs" onclick="add_period()"> <button class="btn btn-warning btn-xs" onclick="add_calendar()">
<i class="fa fa-plus"></i> '.tr('Aggiungi periodo').' <i class="fa fa-plus"></i> '.tr('Aggiungi periodo').'
</button> </button>
<button type="button" class="btn btn-box-tool" data-widget="collapse"> <button type="button" class="btn btn-box-tool" data-widget="collapse">
@ -33,6 +33,7 @@ echo '
<table class="table table-striped table-condensed table-bordered"> <table class="table table-striped table-condensed table-bordered">
<thead> <thead>
<tr> <tr>
<th class="text-center">#</th>
<th>'.tr('Perido').'</th> <th>'.tr('Perido').'</th>
<th>'.tr('Prezzo minimo').'</th> <th>'.tr('Prezzo minimo').'</th>
<th>'.tr('Prezzio medio').'</th> <th>'.tr('Prezzio medio').'</th>
@ -58,6 +59,7 @@ echo '
<table class="table table-striped table-condensed table-bordered"> <table class="table table-striped table-condensed table-bordered">
<thead> <thead>
<tr> <tr>
<th>#</th>
<th>'.tr('Perido').'</th> <th>'.tr('Perido').'</th>
<th>'.tr('Prezzo minimo').'</th> <th>'.tr('Prezzo minimo').'</th>
<th>'.tr('Prezzio medio').'</th> <th>'.tr('Prezzio medio').'</th>
@ -74,81 +76,89 @@ echo '
</div> </div>
</div> </div>
<script src="'.$structure->fileurl('js/stat.js').'"></script>
<script src="'.$structure->fileurl('js/calendar.js').'"></script>
<script src="'.$structure->fileurl('js/prezzo.js').'"></script>
<script> <script>
var calendars = {};
var info = {
url: "'.str_replace('edit.php', '', $structure->fileurl('edit.php')).'",
id_module: globals.id_module,
id_record: globals.id_record,
};
$(document).ready(function() { $(document).ready(function() {
add_period(); add_calendar();
}); });
function change_period(calendar, start, end) { function remove_calendar(button) {
add_prezzo("#prezzi_acquisto", calendar, "uscita", start, end); if (Object.keys(calendars).length > 1){
add_prezzo("#prezzi_vendita", calendar, "entrata", start, end); var name = $(button).parent().find("input").attr("id");
calendars[name].remove();
delete calendars[name];
$("#group-" + name).remove();
} else {
swal({
title: "'.tr("E' presente un solo calendario!").'",
type: "info",
});
}
} }
function add_prezzo(id, calendar, direzione, start, end) { function add_calendar() {
$.ajax({
url: "'.$structure->fileurl('add_prezzo.php').'",
type: "get",
data: {
id_module: globals.id_module,
id_record: globals.id_record,
calendar: calendar,
dir: direzione,
start: start,
end: end,
},
success: function(data){
var row = $(id).find("#row-" + calendar);
if (!row.length) {
$(id).append(data);
} else {
row.after(data);
row.remove();
}
$("#row-" + calendar).effect("highlight", {}, 3000);
}
});
}
function add_period() {
var last = $("#calendars").find("input").last().attr("id"); var last = $("#calendars").find("input").last().attr("id");
var last_id = last ? last.split("-")[1] : 0; var last_id = last ? last.split("-")[1] : 0;
last_id = parseInt(last_id) + 1; last_id = parseInt(last_id) + 1;
var name = "calendar-" + last_id; var name = "calendar-" + last_id;
$("#calendars").append(\'<div class="col-md-4"><input class="form-control" type="text" name="\' + name + \'" id="\' + name + \'"/><br></div>\'); $("#calendars").append(`<div class="col-md-4" id="group-` + name + `">
<div class="input-group">
$("body").on("focus", "#" + name, function() { <span class="input-group-addon before">` + last_id + `</span>
$(this).daterangepicker({ <input class="form-control calendar-input text-center" type="text" name="` + name + `" id="` + name + `"/>
locale: { <span class="input-group-addon after clickable btn btn-danger" onclick="remove_calendar(this)">
customRangeLabel: globals.translations.custom, <i class="fa fa-trash-o"></i>
applyLabel: globals.translations.apply, </span>
cancelLabel: globals.translations.cancel, </div>
fromLabel: globals.translations.from, <br>
toLabel: globals.translations.to, </div>`);
},
startDate: globals.start_date, $("#" + name).daterangepicker({
endDate: globals.end_date, locale: {
applyClass: "btn btn-success btn-sm", customRangeLabel: globals.translations.custom,
cancelClass: "btn btn-danger btn-sm", applyLabel: globals.translations.apply,
linkedCalendars: false cancelLabel: globals.translations.cancel,
}); fromLabel: globals.translations.from,
toLabel: globals.translations.to,
var id = $(this).attr("id").split("-")[1]; },
change_period(id, globals.start_date, globals.end_date); startDate: globals.start_date,
endDate: globals.end_date,
$(this).on("apply.daterangepicker", function(ev, picker) { applyClass: "btn btn-success btn-sm",
var id = $(this).attr("id").split("-")[1]; cancelClass: "btn btn-danger btn-sm",
var start = picker.startDate.format("YYYY-MM-DD"); linkedCalendars: false
var end = picker.endDate.format("YYYY-MM-DD");
console.log(id, start, end);
change_period(id, start, end);
});
}); });
$("#" + name).focus(); $("#" + name).on("apply.daterangepicker", function(ev, picker) {
var name = $(this).attr("id");
var start = picker.startDate.format("YYYY-MM-DD");
var end = picker.endDate.format("YYYY-MM-DD");
calendars[name].update(start, end);
});
// Inizializzazone calendario
var calendar = new Calendar(info, last_id);
calendars[name] = calendar;
var prezzo_acquisto = new Prezzo(calendar, "#prezzi_acquisto", "uscita");
var prezzo_vendita = new Prezzo(calendar, "#prezzi_vendita", "entrata");
calendar.addElement(prezzo_acquisto);
calendar.addElement(prezzo_vendita);
calendar.update(globals.start_date, globals.end_date);
} }
</script>'; </script>';

View File

@ -0,0 +1,25 @@
class Calendar {
constructor(info, id){
this.info = info;
this.id = id;
this.elements = new Array();
}
addElement(object){
this.elements.push(object);
}
update(start, end) {
this.elements.forEach(function (element) {
element.update(start, end);
});
}
remove() {
this.elements.forEach(function (element) {
element.remove();
});
}
}

View File

@ -0,0 +1,51 @@
class Prezzo extends Stat {
constructor(calendar, id, direzione){
super(calendar, id);
this.direzione = direzione;
}
getData(start, end, callback) {
$.ajax({
url: this.calendar.info.url + "/manage_prezzi.php",
type: "get",
data: {
id_module: this.calendar.info.id_module,
id_record: this.calendar.info.id_record,
calendar_id: this.calendar.id,
dir: this.direzione,
start: start,
end: end,
},
success: function(data){
callback(data)
}
});
}
add(start, end) {
var id = this.id;
var calendar_id = this.calendar.id;
this.getData(start, end, function(data) {
var row = $(id).find("#row-" + calendar_id);
if (!row.length) {
$(id).append(data);
} else {
row.after(data);
row.remove();
}
$(id).find("#row-" + calendar_id).effect("highlight", {}, 3000);
});
}
update(start, end) {
this.add(start, end)
}
remove(start, end) {
$(this.id).find("#row-" + this.calendar.id).remove();
}
}

View File

@ -0,0 +1,11 @@
class Stat {
constructor(calendar, id){
this.calendar = calendar;
this.id = id;
}
add(start, end){}
update(start, end){}
remove(){}
}

View File

@ -4,7 +4,7 @@ include_once __DIR__.'/../../core.php';
use Plugins\StatisticheArticoli\Stats; use Plugins\StatisticheArticoli\Stats;
$calendar = filter('calendar'); $calendar_id = filter('calendar_id');
$direzione = filter('dir'); $direzione = filter('dir');
$start = filter('start'); $start = filter('start');
$end = filter('end'); $end = filter('end');
@ -28,12 +28,13 @@ if ($data_min == $data_max) {
} }
echo ' echo '
<tr id="row-'.$calendar.'"> <tr id="row-'.$calendar_id.'">
<td class="text-center">'.$calendar_id.'</td>
<td>'.dateFormat($start).' - '.dateFormat($end).'</td> <td>'.dateFormat($start).' - '.dateFormat($end).'</td>
<td>'.moneyFormat($prezzo_min['prezzo']).'</td> <td class="text-right">'.moneyFormat($prezzo_min['prezzo']).'</td>
<td>'.moneyFormat($prezzo_medio).'</td> <td class="text-right">'.moneyFormat($prezzo_medio).'</td>
<td>'.moneyFormat($prezzo_max['prezzo']).'</td> <td class="text-right">'.moneyFormat($prezzo_max['prezzo']).'</td>
<td>'.moneyFormat($oscillazione).'</td> <td class="text-right">'.moneyFormat($oscillazione).'</td>
<td>'.Translator::numberToLocale($oscillazione_percentuale, '2').' %</td> <td class="text-right">'.Translator::numberToLocale($oscillazione_percentuale, '2').' %</td>
<td>'.$andamento.'</td> <td>'.$andamento.'</td>
</tr>'; </tr>';