Files
admin-php-module/web/controllers/dashboard.js
miroman-afk 85fd6af04f v.2.15
Order history
2022-03-25 00:03:43 +03:00

782 lines
22 KiB
JavaScript

// code style: https://github.com/johnpapa/angular-styleguide
(function () {
'use strict';
angular
.module('app')
.filter('curr', function () { //maybe we should take it to the filter folder
return function (input) {
if (typeof input == 'string') {
input = parseFloat(input.replace(/,/g, '.'));
}
var outputValue = input;
if (typeof outputValue !== 'undefined') {
return outputValue.toFixed(2);
} else {
return 0.00;
}
};
})
.controller('DashboardCtrl', DashboardCtrl);
DashboardCtrl.inject = ['$scope', 'smartRequest', '$interval', '$location'];
function DashboardCtrl($scope, smartRequest, $interval, $location) {
$(document).ready(function() {
if (moment(moment().format('YYYY-MM-DD')).isAfter('2021-12-25')) {
if (moment(moment().format('YYYY-MM-DD')).isBefore('2022-01-10')) {
smartRequest.get('v1/topdishesnewyear', function (data) {
$scope.year_count = data.count;
$scope.top_dishes = data.dishes;
$scope.top_shifts = data.top_shift;
$scope.in_file = data.in_file
if ($scope.in_file == 1) {
var delayMs = 1000; // delay in millisecondsget-more-staff
setTimeout(function(){
$('#get-more-newyear').modal('show');
}, delayMs);
if ($( window ).width() < 800) {
$('#get-more-newyear').on('show.bs.modal', function () {
$('.my-modal').css('z-index', 1060);
$('.my-modal-dialog').css('margin-right',$( window ).width()-$( window ).width());
$('.my-modal-dialog').css('margin-left',$( window ).width()-$( window ).width());
$('.my-modal-dialog').css('margin-top',$( window ).height()-$( window ).height());
$('.my-modal-content').css('height',$( window ).height()*0.95);
$('.my-modal-content').css('width',$( window ).width());
$('.myCarousel').css('height', $( window ).height()*0.94);
$('.myCarousel').css('width', 'auto');
$('.myCarousel').css('object-fit', 'cover');
});
} else {
$('#get-more-newyear').on('show.bs.modal', function () {
$('.my-modal').css('z-index', 1060);
$('.my-modal-dialog').css('margin-right',$( window ).width()/4);
$('.my-modal-dialog').css('margin-left',$( window ).width()/4);
$('.my-modal-dialog').css('margin-top',$( window ).height()/8);
$('.my-modal-content').css('height',$( window ).height()*0.7);
$('.my-modal-content').css('width',$( window ).width()*0.5);
$('.myCarousel').css('height', $( window ).height()*0.7);
$('.myCarousel').css('width', 'auto');
$('.myCarousel').css('object-fit', 'cover');
});
}
}
});
}
}
});
$scope.nextImage = function () {
$('.carousel').carousel('next');
};
$scope.previousImage = function () {
$('.carousel').carousel('prev');
};
$scope.orders_closed = {
percent: 0,
total: 0,
sum: 0
};
$scope.orders_waited = {
percent: 0,
total: 0,
sum: 0
};
$scope.orders_deleted = {
percent: 0,
total: 0,
sum: 0
};
$scope.orders_returned = {
percent: 0,
total: 0,
sum: 0
};
$scope.middle = 0;
$scope.total = 0;
$scope.guests = 0;
$scope.namedGuests = 0;
$scope.profit = 0;
$scope.deleted = 0;
$scope.deleted_sum = 0;
$scope.discounts = 0;
$scope.discounts_sum = 0;
$scope.personals = [];
$scope.dishes = [];
$scope.need_update = false;
$scope.shiftInfo = {};
$scope.folders = {
data: [],
labels: [],
options: {
legend: {
display: true
},
tooltips: {
mode: 'nearest',
displayColors: false
}
}
};
$scope.printers = {
data: [],
labels: [],
options: {
legend: {
display: true
},
tooltips: {
mode: 'nearest',
displayColors: false
}
}
};
$scope.menus = {
data: [],
labels: [],
options: {
legend: {
display: true
},
tooltips: {
mode: 'nearest',
displayColors: false
}
}
};
$scope.payments = {
data: [[]],
labels: [],
options: {
legend: {
display: true
},
tooltips: {
mode: 'nearest',
displayColors: false
}
}
};
$scope.update_time = '';
$scope.colors = [
{
backgroundColor: '#97bbcd',
borderColor: '#97bbcd',
hoverBackgroundColor: '#97bbcd',
hoverBorderColor: '#97bbcd'
},
{
backgroundColor: '#F7464A',
borderColor: '#F7464A',
hoverBackgroundColor: '#F7464A',
hoverBorderColor: '#F7464A'
},
{
backgroundColor: '#46BFBD',
borderColor: '#46BFBD',
hoverBackgroundColor: '#46BFBD',
hoverBorderColor: '#46BFBD'
}
];
//for widget mediana guests
$scope.medianGuests = {
series: ['Количество'],
data: [[]],
labels: [],
options: {
tooltips: {
mode: 'nearest',
displayColors: false,
callbacks: {
title: function (tooltipItems, data) {
var time = tooltipItems[0].xLabel;
var nextTime = moment(time).add(30, 'm');
return time.format('HH:mm') + ' - ' + nextTime.format('HH:mm');
}
}
},
scales: {
xAxes: [{
type: 'time',
bounds: 'ticks',
time: {
unit: 'hour',
unitStepSize: 0.5,
displayFormats: {
'hour': 'HH:mm'
},
}
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
},
}
};
//for widget mediana finance
$scope.medianFinance = {
data: [[], [], []],
labels: [],
series: ['Выручка', 'Себестоимость', 'Прибыль'],
options: {
legend: {
display: true
},
scales: {
xAxes: [{
type: 'time',
bounds: 'ticks',
time: {
unit: 'hour',
unitStepSize: 0.5,
displayFormats: {
'hour': 'HH:mm'
}
}
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
},
tooltips: {
callbacks: {
title: function (tooltipItems, data) {
var time = tooltipItems[0].xLabel;
var nextTime = moment(time).add(30, 'm');
return time.format('HH:mm') + ' - ' + nextTime.format('HH:mm');
}
}
}
}
};
$scope.NOT_CLOSED = 'не закрыт';
$scope.checkUpdate = function () {
smartRequest.get('dashboard/ping', function (data) {
$scope.need_update = data.data.update;
});
};
$scope.updateData = function () {
smartRequest.get('dashboard/online/updatetime', function (data) {
$scope.update_time = data.updated;
});
smartRequest.get('dashboard/online/updatetime', function (data) {
$scope.update_time = data.updated;
});
smartRequest.get('dashboard/online/info', function (data) {
$scope.shiftInfo = data;
});
smartRequest.get('dashboard/online/payments', function (data) {
$scope.payments.labels = data.payments.map(payment => payment.name);
$scope.payments.data = data.payments.map(payment => payment.value);
});
smartRequest.get('dashboard/online/orders', function (data) {
var total = data.wait_count + data.closed_count + data.deleted_count + data.returned_count;
$scope.orders_closed.percent = $scope.calcPercentageForGroupOrders(total, data.closed_count);
$scope.orders_closed.total = data.closed_count;
$scope.orders_closed.sum = data.closed_sum;
$scope.orders_waited.percent = $scope.calcPercentageForGroupOrders(total, data.wait_count);
$scope.orders_waited.total = data.wait_count;
$scope.orders_waited.sum = data.wait_sum;
$scope.orders_deleted.percent = $scope.calcPercentageForGroupOrders(total, data.deleted_count);
$scope.orders_deleted.total = data.deleted_count;
$scope.orders_deleted.sum = data.deleted_sum;
$scope.orders_returned.percent = $scope.calcPercentageForGroupOrders(total, data.returned_count);
$scope.orders_returned.total = data.returned_count;
$scope.orders_returned.sum = data.returned_sum;
});
smartRequest.get('dashboard/online/middle', function (data) {
$scope.middle = data.middle_check;
});
smartRequest.get('dashboard/online/total', function (data) {
$scope.total = data.total;
});
smartRequest.get('dashboard/online/staff', function (data) {
$scope.personals = data.staff;
});
smartRequest.get('v1/topdishes', function (data) {
$scope.dishes = data.dishes;
$scope.terminalId = data.terminal;
if ($scope.terminalId > 0) {
smartRequest.post('settings/update/force', {
task: 'online',
terminal: $scope.terminalId,
}, function(data) {
});
}
});
smartRequest.get('dashboard/online/folders', function (data) {
$scope.folders.labels = data.folders.map(folder => folder.name);
$scope.folders.data = data.folders.map(folder => Math.roundClearNG(folder.count));
});
smartRequest.get('dashboard/online/printers', function (data) {
$scope.printers.labels = [];
$scope.printers.data = [];
for (var i = 0; i < data.printers.length; i++) {
$scope.printers.labels.push(data.printers[i].name);
$scope.printers.data.push(Math.roundClearNG(data.printers[i].count, -2));
}
});
smartRequest.get('dashboard/online/tables', function (data) {
$scope.tables = data.tables;
});
smartRequest.get('dashboard/online/menu', function (data) {
$scope.menus.labels = [];
$scope.menus.data = [];
for (var i = 0; i < data.menus.length; i++) {
$scope.menus.labels.push(data.menus[i].menu_name);
$scope.menus.data.push(Math.roundClearNG(data.menus[i].count, -2));
}
});
smartRequest.get('dashboard/online/profit', function (data) {
$scope.profit = data.profit;
});
smartRequest.get('dashboard/online/guests', function (data) {
$scope.guests = data.guestsCount;
$scope.namedGuests = data.namedGuests;
$scope.sumNamedGuests = data.totalSum;
});
smartRequest.get('dashboard/online/deleted', function (data) {
$scope.deleted = Math.roundNG(data.count, -2);
$scope.deleted_sum = Math.roundNG(data.sum, -2);
});
smartRequest.get('dashboard/online/discount', function (data) {
$scope.discounts = Math.roundNG(data.count, -2);
$scope.discounts_sum = Math.roundNG(data.sum, -2);
$scope.tot_disc_sum = Math.roundNG(data.total_sum, -2);
});
smartRequest.get('dashboard/online/guests/median', function (data) {
$scope.medianGuests.labels = [];
$scope.medianGuests.data = [[]];
var tempData = [];
var tempTime = [];
for (var i = 0; i < data.data.length; i++) {
var newDate = $scope.getMomentDate(data.data[i].time);
tempData.push(parseInt(data.data[i].count));
tempTime.push(newDate);
var indexTime = $scope.getIntervals(tempTime[i], $scope.medianGuests.labels);
if (indexTime == -1) {
$scope.medianGuests.labels.push(tempTime[i]);
$scope.medianGuests.data[0].push(tempData[i]);
} else {
$scope.medianGuests.data[0][indexTime] += tempData[i];
}
}
});
smartRequest.get('dashboard/online/finance/median', function (data) {
$scope.medianFinance.labels = [];
$scope.medianFinance.data = [[], [], []];
var tempSum = [];
var tempTotalCost = [];
var tempProffit = [];
var tempTime = [];
for (var i = 0; i < data.data.length; i++) {
var newDate = $scope.getMomentDate(data.data[i].time);
tempSum.push(data.data[i].sum);
tempTotalCost.push(data.data[i].totalCost);
var proffit = parseFloat((tempSum[i] - tempTotalCost[i]).toFixed(2));
tempProffit.push(proffit);
tempTime.push(newDate);
var indexTime = $scope.getIntervals(tempTime[i], $scope.medianFinance.labels);
$scope.fillMedianFinance(indexTime, tempTime[i], tempSum[i], tempTotalCost[i], tempProffit[i]);
}
for (var i = 0; i < $scope.medianFinance.data.length; i++) {
for (var j = 0; j < $scope.medianFinance.data[i].length; j++) {
var data = $scope.medianFinance.data[i][j];
data = Math.round(data * 100) / 100;
$scope.medianFinance.data[i][j] = data;
}
}
});
$scope.checkUpdate();
};
$scope.update = function () {
smartRequest.get('dashboard/online/updatetime', function (data) {
$scope.update_time = data.updated;
});
smartRequest.get('dashboard/online/updatetime', function (data) {
$scope.update_time = data.updated;
});
smartRequest.get('dashboard/online/info', function (data) {
$scope.shiftInfo = data;
});
smartRequest.get('dashboard/online/payments', function (data) {
$scope.payments.labels = data.payments.map(payment => payment.name);
$scope.payments.data = data.payments.map(payment => payment.value);
});
smartRequest.get('dashboard/online/orders', function (data) {
var total = data.wait_count + data.closed_count + data.deleted_count + data.returned_count;
$scope.orders_closed.percent = $scope.calcPercentageForGroupOrders(total, data.closed_count);
$scope.orders_closed.total = data.closed_count;
$scope.orders_closed.sum = data.closed_sum;
$scope.orders_waited.percent = $scope.calcPercentageForGroupOrders(total, data.wait_count);
$scope.orders_waited.total = data.wait_count;
$scope.orders_waited.sum = data.wait_sum;
$scope.orders_deleted.percent = $scope.calcPercentageForGroupOrders(total, data.deleted_count);
$scope.orders_deleted.total = data.deleted_count;
$scope.orders_deleted.sum = data.deleted_sum;
$scope.orders_returned.percent = $scope.calcPercentageForGroupOrders(total, data.returned_count);
$scope.orders_returned.total = data.returned_count;
$scope.orders_returned.sum = data.returned_sum;
});
smartRequest.get('dashboard/online/middle', function (data) {
$scope.middle = data.middle_check;
});
smartRequest.get('dashboard/online/total', function (data) {
$scope.total = data.total;
});
smartRequest.get('dashboard/online/staff', function (data) {
$scope.personals = data.staff;
});
smartRequest.get('v1/topdishes', function (data) {
$scope.dishes = data.dishes;
});
smartRequest.get('dashboard/online/folders', function (data) {
$scope.folders.labels = data.folders.map(folder => folder.name);
$scope.folders.data = data.folders.map(folder => Math.roundClearNG(folder.count));
});
smartRequest.get('dashboard/online/printers', function (data) {
$scope.printers.labels = [];
$scope.printers.data = [];
for (var i = 0; i < data.printers.length; i++) {
$scope.printers.labels.push(data.printers[i].name);
$scope.printers.data.push(Math.roundClearNG(data.printers[i].count, -2));
}
});
smartRequest.get('dashboard/online/tables', function (data) {
$scope.tables = data.tables;
});
smartRequest.get('dashboard/online/menu', function (data) {
$scope.menus.labels = [];
$scope.menus.data = [];
for (var i = 0; i < data.menus.length; i++) {
$scope.menus.labels.push(data.menus[i].menu_name);
$scope.menus.data.push(Math.roundClearNG(data.menus[i].count, -2));
}
});
smartRequest.get('dashboard/online/profit', function (data) {
$scope.profit = data.profit;
});
smartRequest.get('dashboard/online/guests', function (data) {
$scope.guests = data.guestsCount;
$scope.namedGuests = data.namedGuests;
$scope.sumNamedGuests = data.totalSum;
});
smartRequest.get('dashboard/online/deleted', function (data) {
$scope.deleted = Math.roundNG(data.count, -2);
$scope.deleted_sum = Math.roundNG(data.sum, -2);
});
smartRequest.get('dashboard/online/discount', function (data) {
$scope.discounts = Math.roundNG(data.count, -2);
$scope.discounts_sum = Math.roundNG(data.sum, -2);
$scope.tot_disc_sum = Math.roundNG(data.total_sum, -2);
});
smartRequest.get('dashboard/online/guests/median', function (data) {
$scope.medianGuests.labels = [];
$scope.medianGuests.data = [[]];
var tempData = [];
var tempTime = [];
for (var i = 0; i < data.data.length; i++) {
var newDate = $scope.getMomentDate(data.data[i].time);
tempData.push(parseInt(data.data[i].count));
tempTime.push(newDate);
var indexTime = $scope.getIntervals(tempTime[i], $scope.medianGuests.labels);
if (indexTime == -1) {
$scope.medianGuests.labels.push(tempTime[i]);
$scope.medianGuests.data[0].push(tempData[i]);
} else {
$scope.medianGuests.data[0][indexTime] += tempData[i];
}
}
});
smartRequest.get('dashboard/online/finance/median', function (data) {
$scope.medianFinance.labels = [];
$scope.medianFinance.data = [[], [], []];
var tempSum = [];
var tempTotalCost = [];
var tempProffit = [];
var tempTime = [];
for (var i = 0; i < data.data.length; i++) {
var newDate = $scope.getMomentDate(data.data[i].time);
tempSum.push(data.data[i].sum);
tempTotalCost.push(data.data[i].totalCost);
var proffit = parseFloat((tempSum[i] - tempTotalCost[i]).toFixed(2));
tempProffit.push(proffit);
tempTime.push(newDate);
var indexTime = $scope.getIntervals(tempTime[i], $scope.medianFinance.labels);
$scope.fillMedianFinance(indexTime, tempTime[i], tempSum[i], tempTotalCost[i], tempProffit[i]);
}
for (var i = 0; i < $scope.medianFinance.data.length; i++) {
for (var j = 0; j < $scope.medianFinance.data[i].length; j++) {
var data = $scope.medianFinance.data[i][j];
data = Math.round(data * 100) / 100;
$scope.medianFinance.data[i][j] = data;
}
}
});
$scope.checkUpdate();
};
$scope.update();
$scope.ping = function () {
let stop = $interval(function () {
if($location.url() == '/dashboard') {
smartRequest.get('dashboard/ping', function (data) {
$scope.need_update = data.data.update;
if (!$scope.need_update) {
$scope.update();
$interval.cancel(stop);
}
});
}
}, 10000);
};
$scope.ping();
$scope.all_widgets = [];
$scope.add = function () {
smartRequest.get('settings/widgets/all', function (data) {
$scope.all_widgets = data.widgets;
});
$('#add-widget').modal();
};
$scope.calcPercentageForGroupOrders = function (total, countOrdersGroup) {
return total > 0 ? Math.round(countOrdersGroup * 100 / total) : 0;
};
$scope.addWidget = function () {
$('#add-widget').modal('toggle');
smartRequest.post('settings/widgets/enable', {
code: $scope.add_widget_code
}, function (data) {
$scope.update();
});
};
$scope.deleteWidget = function (code) {
smartRequest.post('settings/widgets/disable', {
code: code
}, function (data) {
$scope.update();
});
};
$scope.getMomentDate = function (date_str) {
var date = moment(date_str, 'DD.MM.YYYY HH:mm:ss');
var remainder = 30 - (date.minute() % 30);
return moment(date).add(remainder, 'minutes').set('second', 0);
};
$scope.getIntervals = function (momentTime, array) {
for (var i = 0; i < array.length; i++) {
var time = array[i];
if (time.get('hour') == momentTime.get('hour') && time.get('minute') == momentTime.get('minute')) {
return i;
}
}
return -1;
};
$scope.fillMedianFinance = function (index, time, sum, totalCost, proffit) {
if (index == -1) {
$scope.pushToMedianFinance(time, sum, totalCost, proffit);
} else {
$scope.incMedianFinance(index, sum, totalCost, proffit);
}
};
$scope.pushToMedianFinance = function (time, sum, totalCost, proffit) {
$scope.medianFinance.labels.push(time);
$scope.medianFinance.data[0].push(sum);
$scope.medianFinance.data[1].push(totalCost);
$scope.medianFinance.data[2].push(proffit);
};
$scope.incMedianFinance = function (index, sum, totalCost, proffit) {
$scope.medianFinance.data[0][index] += sum;
$scope.medianFinance.data[1][index] += totalCost;
$scope.medianFinance.data[2][index] += proffit;
};
$scope.getMore = function (code) {
smartRequest.get('dashboard/more/' + code, function (data) {
$scope.moreData = data;
$('#get-more-' + code).modal();
});
};
$scope.getItems = function (modal, order) {
$('#get-more-' + modal).modal('toggle');
smartRequest.get('dashboard/' + modal + '/items?order=' + order.number, function (data) {
$scope.order = data;
$('#items-' + modal).modal('toggle');
});
};
$scope.returnModal = function (name) {
$('#items-' + name).on('hidden.bs.modal', function (event) {
$('#get-more-' + name).modal('show');
});
};
$scope.getTotalItems = function (order) {
$('#get-more-total').modal('toggle');
smartRequest.get('dashboard/total/items?order=' + order.number, function (data) {
$scope.order = data;
smartRequest.get('v1/orderhistory?order_id=' + order.number, function (actions) {
$scope.actions = actions;
});
$('#items-total').modal('toggle');
});
};
$scope.getStaffMore = function (staff) {
smartRequest.get('dashboard/more/staff?code=' + staff.code, function (data) {
$scope.staff = data;
$('#get-more-staff').modal('toggle');
});
};
$scope.getTableMore = function (table) {
smartRequest.get('dashboard/more/table?place=' + table.place_name + '&table=' + table.table_name, function (data) {
$scope.table_ord = data;
$('#get-more-table').modal('toggle');
});
};
$scope.getClosedDate = function (closeTime) {
if (closeTime == '30.12.1899')
return $scope.NOT_CLOSED;
return closeTime;
};
$scope.getBackClass = function (percentProffit) {
if (percentProffit <= 25) {
return 'green-600';
} else if (percentProffit <= 35) {
return 'green';
} else if (percentProffit <= 50) {
return '';
} else if (percentProffit <= 100) {
return 'red';
}
};
var declOfNum = function (titles, number) {
number = Math.abs(number);
var cases = [2, 0, 1, 1, 1, 2];
return function (number) {
return titles[(number % 100 > 4 && number % 100 < 20) ? 2 : cases[(number % 10 < 5) ? number % 10 : 5]];
};
};
$scope.GetCountNamedGuests = declOfNum(['именованный гость', 'именованных гостя', 'именованных гостей']);
$scope.GetCountDeletedPositions = declOfNum(['позиций', 'позиции', 'позиций']);
$scope.GetCountTotalOrders = declOfNum(['заказ', 'заказа', 'заказов']);
}
})();