caisse-bliss/assets/js/parts/main.js

192 lines
6.9 KiB
JavaScript
Raw Normal View History

2018-05-08 13:24:02 +02:00
var $ = require('jquery');
// JS is equivalent to the normal "bootstrap" package
// no need to set this to a variable, just require it
require('bootstrap-sass');
// or you can include specific pieces
// require('bootstrap-sass/javascripts/bootstrap/tooltip');
// require('bootstrap-sass/javascripts/bootstrap/popover');
$(document).ready(function () {
$('[data-toggle="popover"]').popover();
});
2018-04-04 16:25:25 +02:00
console.log('hello console for main.js');
2018-04-05 14:21:32 +02:00
var stuff = ['initialstuff'];
angular
.module('caisse', [])
.controller('CaisseCtrl', ['$scope', '$http', function ($scope, $http, stuff) {
$scope.productsFromDB = []; // loaded products
$scope.categories = []; // product categories
2018-04-19 16:26:48 +02:00
$scope.sellingComment = "un gens"; // comment about the client or the current selling
$scope.initLoadDone = false; // becames true after first init of product loading
2018-04-05 16:40:40 +02:00
$scope.recentSellings = [];
2018-04-17 12:10:21 +02:00
$scope.lesParams = {};
2018-04-17 15:08:30 +02:00
$scope.paidAmount = 0;
2018-04-20 09:40:17 +02:00
$scope.expressSelling = true;
2018-04-05 14:21:32 +02:00
$scope.pausedSelling = [];
2018-04-17 15:08:30 +02:00
$scope.activeItemsSold = []; // list of products ID to sell
$scope.activeSelling = []; // list of products to sell
$scope.activeFestival = { // an event where selling take place
2018-04-19 16:26:48 +02:00
id : null,
name : "le festival",
dateCreation : new Date(),
chiffreAffaire: 0,
2018-04-19 17:38:43 +02:00
clientsCount : 0,
2018-04-19 16:26:48 +02:00
commentaire : ""
2018-04-05 16:40:40 +02:00
};
2018-04-05 17:01:36 +02:00
/**
* get the sum of products prices
* @param list
* @returns {number}
*/
$scope.sumOfList = function (list) {
let counter = 0;
for (let i = 0; i < list.length; i++) {
counter += list[i].price;
}
return counter;
};
/**
* sum of current selling list prices
* @returns {number}
* @constructor
*/
$scope.CurrentSellingTotal = function () {
return $scope.sumOfList($scope.activeSelling);
};
2018-04-05 14:21:32 +02:00
2018-04-17 15:08:30 +02:00
$scope.regenActiveSellingIds = function () {
$scope.activeItemsSold = [];
2018-04-19 16:26:48 +02:00
$scope.paidAmount = 0;
2018-04-17 15:08:30 +02:00
for (let obj in $scope.activeSelling) {
$scope.activeItemsSold.push(obj.id);
}
2018-04-19 16:26:48 +02:00
$scope.paidAmount += $scope.sumOfList($scope.activeSelling);
2018-04-17 15:08:30 +02:00
};
2018-04-05 14:21:32 +02:00
$scope.stuff = stuff;
$scope.setActiveSelling = function (selling) {
$scope.activeSelling = selling;
};
$scope.pauseSelling = function (selling) {
$scope.pausedSelling.push(selling);
};
2018-04-20 09:40:17 +02:00
/**
* add to current sell list
* @param product
*/
2018-04-05 16:40:40 +02:00
$scope.addProduct = function (product) {
2018-04-20 09:40:17 +02:00
product.stockCount--;
2018-04-05 16:40:40 +02:00
$scope.activeSelling.push(product);
2018-04-17 15:08:30 +02:00
$scope.activeItemsSold.push(product.id);
2018-04-19 16:26:48 +02:00
$scope.regenActiveSellingIds();
2018-04-05 14:21:32 +02:00
};
2018-04-20 09:40:17 +02:00
/**
* remove from current sell list
* @param product
*/
$scope.removeProduct = function (product, index) {
product.stockCount++;
$scope.activeSelling.splice($index, 1);
$scope.regenActiveSellingIds()
};
2018-04-05 17:01:36 +02:00
$scope.pauseSelling = function () {
$scope.pausedSelling.push(angular.copy($scope.activeSelling));
$scope.activeSelling = [];
};
$scope.setBackPausedSelling = function (sellingList, index) {
$scope.activeSelling = angular.copy(sellingList);
$scope.pausedSelling.splice(index, 1);
2018-04-05 14:21:32 +02:00
};
2018-04-20 09:40:17 +02:00
$scope.clearSellingComment = function () {
$scope.sellingComment = '';
2018-04-20 12:15:53 +02:00
document.querySelector('#sellingCommentInput').focus();
};
$scope.clearCurrentSelling = function () {
2018-04-06 16:39:41 +02:00
$scope.paidAmount = 0;
2018-04-20 12:15:53 +02:00
// $scope.sellingComment = "";
$scope.activeSelling = [];
};
2018-04-17 15:08:30 +02:00
// http related calls
$scope.fetchProductsFromDB = function () {
console.log('fetch products...');
$http.get('get-my-products').then((rep) => {
console.log('ok', rep);
$scope.categories = rep.data.categories;
$scope.productsFromDB = rep.data.categories;
2018-04-20 12:15:53 +02:00
// $scope.recentSellings = rep.data.history;
2018-04-17 16:15:24 +02:00
// festoche
$scope.activeFestival.id = rep.data.lastFestival.id;
$scope.activeFestival.name = rep.data.lastFestival.name;
$scope.activeFestival.dateCreation = rep.data.lastFestival.dateCreation;
$scope.activeFestival.commentaire = rep.data.lastFestival.commentaire;
2018-04-19 16:26:48 +02:00
$scope.activeFestival.chiffreAffaire = rep.data.lastFestival.chiffreAffaire;
2018-04-20 12:15:53 +02:00
$scope.activeFestival.fondDeCaisseAvant = rep.data.lastFestival.fondDeCaisseAvant;
$scope.activeFestival.fondDeCaisseApres = rep.data.lastFestival.fondDeCaisseApres;
2018-04-19 17:38:43 +02:00
$scope.activeFestival.clientsCount = rep.data.lastFestival.clientsCount;
2018-04-17 16:15:24 +02:00
//done
2018-04-17 15:08:30 +02:00
$scope.initLoadDone = true;
}, (err) => {
console.log(err);
$scope.initLoadDone = true;
});
};
2018-04-20 09:23:44 +02:00
$scope.expressSell = function (product) {
$scope.addProduct(product);
$scope.sendForm();
};
2018-04-20 12:15:53 +02:00
$scope.recentId = 0;
2018-04-20 12:30:58 +02:00
$scope.logger = function (stuff) {
console.log('logger', stuff);
}
2018-04-05 14:21:32 +02:00
$scope.sendForm = function () {
2018-04-20 12:15:53 +02:00
console.log('$scope.sellingComment', this.sellingComment);
2018-04-05 16:40:40 +02:00
let lesParams = {
2018-04-20 12:15:53 +02:00
paidByClient : this.paidAmount,
sellingComment: this.sellingComment,
activeSelling : this.activeSelling,
activeFestival: this.activeFestival
2018-04-05 16:40:40 +02:00
};
2018-04-20 12:15:53 +02:00
$scope.recentSellings.push({
id : this.recentId++,
amount : this.CurrentSellingTotal(),
paidAmount: this.paidAmount,
products :
angular
.copy(this.activeSelling)
});
console.log('$scope.recentSellings', this.recentSellings);
2018-04-17 12:10:21 +02:00
$scope.lesParams = lesParams;
2018-04-05 14:21:32 +02:00
$http({
2018-04-06 16:39:41 +02:00
method : 'POST',
url : 'add-selling',
headers: {
'Content-Type': 'application/json'
},
data : lesParams // pass in data as strings
2018-04-05 14:21:32 +02:00
}).then(function (rep) {
2018-04-17 12:10:21 +02:00
$scope.clearCurrentSelling();
2018-04-19 17:38:43 +02:00
// if successful, bind success message to message
$scope.successMessage = rep.data.message;
$scope.activeFestival.chiffreAffaire = rep.data.newChiffreAffaire;
$scope.activeFestival.clientsCount = rep.data.clientsCount;
2018-04-05 14:21:32 +02:00
console.log(rep);
if (!rep.success) {
// if not successful, bind errors to error variables
$scope.errors = rep.errors;
}
}, function (rep) {
console.log('nope! ', rep.data);
})
;
};
2018-04-05 17:01:36 +02:00
$scope.init = (function () {
$scope.fetchProductsFromDB();
})();
2018-04-05 14:21:32 +02:00
}]);