caisse-bliss/assets/js/parts/main.js
2018-04-20 12:30:58 +02:00

180 lines
6.5 KiB
JavaScript
Executable File

console.log('hello console for main.js');
var stuff = ['initialstuff'];
angular
.module('caisse', [])
.controller('CaisseCtrl', ['$scope', '$http', function ($scope, $http, stuff) {
$scope.productsFromDB = []; // loaded products
$scope.categories = []; // product categories
$scope.sellingComment = "un gens"; // comment about the client or the current selling
$scope.initLoadDone = false; // becames true after first init of product loading
$scope.recentSellings = [];
$scope.lesParams = {};
$scope.paidAmount = 0;
$scope.expressSelling = true;
$scope.pausedSelling = [];
$scope.activeItemsSold = []; // list of products ID to sell
$scope.activeSelling = []; // list of products to sell
$scope.activeFestival = { // an event where selling take place
id : null,
name : "le festival",
dateCreation : new Date(),
chiffreAffaire: 0,
clientsCount : 0,
commentaire : ""
};
/**
* 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);
};
$scope.regenActiveSellingIds = function () {
$scope.activeItemsSold = [];
$scope.paidAmount = 0;
for (let obj in $scope.activeSelling) {
$scope.activeItemsSold.push(obj.id);
}
$scope.paidAmount += $scope.sumOfList($scope.activeSelling);
};
$scope.stuff = stuff;
$scope.setActiveSelling = function (selling) {
$scope.activeSelling = selling;
};
$scope.pauseSelling = function (selling) {
$scope.pausedSelling.push(selling);
};
/**
* add to current sell list
* @param product
*/
$scope.addProduct = function (product) {
product.stockCount--;
$scope.activeSelling.push(product);
$scope.activeItemsSold.push(product.id);
$scope.regenActiveSellingIds();
};
/**
* remove from current sell list
* @param product
*/
$scope.removeProduct = function (product, index) {
product.stockCount++;
$scope.activeSelling.splice($index, 1);
$scope.regenActiveSellingIds()
};
$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);
};
$scope.clearSellingComment = function () {
$scope.sellingComment = '';
document.querySelector('#sellingCommentInput').focus();
};
$scope.clearCurrentSelling = function () {
$scope.paidAmount = 0;
// $scope.sellingComment = "";
$scope.activeSelling = [];
};
// 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;
// $scope.recentSellings = rep.data.history;
// 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;
$scope.activeFestival.chiffreAffaire = rep.data.lastFestival.chiffreAffaire;
$scope.activeFestival.fondDeCaisseAvant = rep.data.lastFestival.fondDeCaisseAvant;
$scope.activeFestival.fondDeCaisseApres = rep.data.lastFestival.fondDeCaisseApres;
$scope.activeFestival.clientsCount = rep.data.lastFestival.clientsCount;
//done
$scope.initLoadDone = true;
}, (err) => {
console.log(err);
$scope.initLoadDone = true;
});
};
$scope.expressSell = function (product) {
$scope.addProduct(product);
$scope.sendForm();
};
$scope.recentId = 0;
$scope.logger = function (stuff) {
console.log('logger', stuff);
}
$scope.sendForm = function () {
console.log('$scope.sellingComment', this.sellingComment);
let lesParams = {
paidByClient : this.paidAmount,
sellingComment: this.sellingComment,
activeSelling : this.activeSelling,
activeFestival: this.activeFestival
};
$scope.recentSellings.push({
id : this.recentId++,
amount : this.CurrentSellingTotal(),
paidAmount: this.paidAmount,
products :
angular
.copy(this.activeSelling)
});
console.log('$scope.recentSellings', this.recentSellings);
$scope.lesParams = lesParams;
$http({
method : 'POST',
url : 'add-selling',
headers: {
'Content-Type': 'application/json'
},
data : lesParams // pass in data as strings
}).then(function (rep) {
$scope.clearCurrentSelling();
// if successful, bind success message to message
$scope.successMessage = rep.data.message;
$scope.activeFestival.chiffreAffaire = rep.data.newChiffreAffaire;
$scope.activeFestival.clientsCount = rep.data.clientsCount;
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);
})
;
};
$scope.init = (function () {
$scope.fetchProductsFromDB();
})();
}]);