cobuild-angular-stack
Version:
Base stack angular sass jade gulp
151 lines (134 loc) • 4.2 kB
JavaScript
(function (module) {
AdminDiscountCodeAddController.$inject = ['$scope', '$state', '$stateParams','$translate', 'toastr', '$uibModalInstance', 'DiscountCode', 'DiscountCategory', 'discountSubCategory', 'discountCategory'];
module.controller('AdminDiscountCodeAddController', AdminDiscountCodeAddController);
function AdminDiscountCodeAddController($scope, $state, $stateParams, $translate, toastr, $uibModalInstance, DiscountCode, DiscountCategory, discountSubCategory, discountCategory) {
$scope.view = {
type: "amount",
amount: 1,
cant: 1,
statusValidity: false,
advanceSale: false,
unique: true,
numberPayments: "1",
costs: 0,
typePay: 'fixed',
discountSubCategoryId: discountSubCategory ? discountSubCategory.id : ''
}
$scope.confirm = false;
$scope.data = {
discountCategoryId : discountCategory ? discountCategory.id : ""
}
$scope.date = {};
$scope.dateSelect = null;
$scope.subcategory = discountSubCategory;
$scope.categories = [];
$scope.isActiveValidity = true;
$scope.category = discountCategory;
$scope.minDate = moment().format('DD-MM-YYYY');
$scope.optionsDate = {
singleDatePicker: true,
applyClass: 'btn-inverse',
minDate: moment(),
locale: {
format : "DD-MM-YYYY",
applyLabel: "Aceptar",
cancelLabel: 'Cancelar',
"daysOfWeek": [
"Do",
"Lu",
"Ma",
"Mi",
"Ju",
"Vi",
"Sa"
],
"monthNames": [
"Enero",
"Febrero",
"Marzo",
"Abril",
"Mayo",
"Junio",
"Julio",
"Agosto",
"Septiembre",
"Octubre",
"Noviembre",
"Diciembre"
],
},
eventHandlers: {
'cancel.daterangepicker': function (ev, picker) {
$scope.date = {};
},
'apply.daterangepicker': function (ev, picker) {
}
}
};
$scope.setConfirm = function() {
if(!$scope.view.discountSubCategoryId) {
toastr.error("Error: debe seleccionar subcategoría");
} else if($scope.view.amount <= 0) {
toastr.error("Error: el monto no puede ser menor o igual a cero");
} else {
$scope.confirm = true;
if($scope.view.statusValidity) {
$scope.dateSelect = moment($(".validity").val(),"DD-MM-YYYY");
}
}
}
$scope.$watch('view.advanceSale', function(newValue, old) {
$scope.view.type = "amount";
$scope.view.statusValidity = false;
$scope.isActiveValidity = newValue;
});
$scope.$watch('view.statusValidity', function(newValue, old) {
if(newValue) {
$scope.dateSelect = moment($(".validity").val(), "DD-MM-YYYY");
}
});
$scope.getCategory = function(start) {
if(!start) $scope.view.discountSubCategoryId = "";
if(!$scope.data.discountCategoryId) return ;
DiscountCategory.findById({id: $scope.data.discountCategoryId, filter: {"include":"discountSubcategories"}}).$promise.then(function(discountCategory) {
$scope.discountSubCategories = discountCategory.discountSubcategories;
}, function(err) {
console.log(err)
}).catch(function(err) {
console.log(err);
});
};
$scope.getDiscountCategories = function() {
DiscountCategory.find({
}).$promise.then(function(discountCategories) {
$scope.categories = discountCategories;
$scope.getCategory(1);
}, function(err) {
console.log(err)
}).catch(function(err) {
console.log(err);
});
};
$scope.create = function() {
if($scope.view.statusValidity) {
$scope.view.validity = $scope.dateSelect;
};
if($scope.view.advanceSale && $scope.view.amount <= $scope.view.costs) {
return toastr.error("El costo no puede ser mayor a la cantidad a pagar");
}
DiscountCode.create($scope.view).$promise.then(function(code) {
$translate('success-save-data').then(function(translation){
toastr.success(translation);
$uibModalInstance.close(code);
});
}, $scope.error).catch($scope.error);
}
$scope.error = function(err) {
toastr.error(err);
}
$scope.close = function() {
$uibModalInstance.dismiss('cancel');
}
$scope.getDiscountCategories();
}
})(angular.module('uniko.admin.discount'));