cobuild-angular-stack
Version:
Base stack angular sass jade gulp
86 lines (80 loc) • 2.39 kB
JavaScript
(function(module) {
AdminDiscountController.$inject = ['$scope', '$state', '$stateParams','$translate', 'toastr', 'DiscountCode', 'DiscountSubCategory'];
module.controller('AdminDiscountController', AdminDiscountController);
function AdminDiscountController($scope, $state, $stateParams, $translate, toastr, DiscountCode, DiscountSubCategory) {
$scope.discountCode = {
codes: []
};
$scope.loader = false;
$scope.category = {};
$scope.role = localStorage.role
$scope.getCodes = function() {
$scope.loader = true;
DiscountCode.findById({
id: $stateParams.discountCodeId,
filter: {
include: {
relation: 'codes',
scope:{
include:{
relation: 'coupleAccount'
}
}
}
}
}).$promise.then(function(discountCode) {
$scope.discountCode = discountCode;
$scope.loader = false;
DiscountSubCategory.findById({
id: discountCode.discountSubCategoryId,
filter: {
include:['discountCategory']
}
}).$promise.then(function(subcategory) {
var category = subcategory.discountCategory;
$scope.discountCode.discountSubCategory = subcategory;
$scope.category = category;
}, function(err) {
console.log(err);
toastr.error(JSON.stringify(err));
}).catch(function(err) {
console.log(err);
toastr.error(JSON.stringify(err));
});
}, function(err) {
$scope.loader = false;
console.log(err);
toastr.error(JSON.stringify(err));
}).catch(function(err) {
toastr.error(JSON.stringify(err));
});
}
$scope.getState = function(status) {
switch(status) {
case "DISABLED":
return "Inactivo";
case "PENDING":
return "Pendiente";
case "APPROVED":
return "Activo";
case "REDEEMED":
return "Redimido";
}
}
$scope.download = function() {
var csv = "code,state \n";
$scope.discountCode.codes.forEach(function(data) {
csv += data.code + "," + $scope.getState(data.status) + "\n";
});
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_blank';
hiddenElement.download = 'people.csv';
hiddenElement.click();
}
$scope.formatDate = function(date) {
return moment(date).format("DD-MM-YYYY")
}
$scope.getCodes();
}
})(angular.module('uniko.admin.discount'));