cobuild-angular-stack
Version:
Base stack angular sass jade gulp
60 lines (51 loc) • 1.66 kB
JavaScript
(function (module) {
AdminDiscountPaymentsController.$inject = ['$scope', '$state', '$stateParams','$translate', 'toastr', '$uibModal', 'DiscountCode'];
module.controller('AdminDiscountPaymentsController', AdminDiscountPaymentsController);
function AdminDiscountPaymentsController($scope, $state, $stateParams, $translate, toastr, $uibModal, DiscountCode) {
$scope.view = {
id: $stateParams.discountCodeId,
discountCode: {
discountPayments: []
},
paymentsTotals : 1
}
$scope.add = function() {
var modalInstance = $uibModal.open({
animation: true,
templateUrl: 'partials/admin.discount.payment.add.html',
controller: 'AdminDiscountPaymentAddController',
resolve: {
discount: $scope.view.discountCode
}
});
modalInstance.result.then(function(payment) {
$scope.getDiscounts();
}, $scope.error).catch($scope.error)
}
$scope.getDiscounts = function() {
DiscountCode.findOne({
filter: {
where: {
id: $scope.view.id
},
include: ['discountPayments', 'discountSubCategory']
}
}).$promise.then(function(data) {
$scope.view.discountCode = data;
$scope.view.paymentsTotals = $scope.view.discountCode.discountPayments.length;
$scope.view.totalPayment = _.reduce($scope.view.discountCode.discountPayments, function(total, model) {
return total + model.amount;
}, 0)
}, function(err) {
console.log(err);
}).catch(function(err) {
console.log(err);
});
}
$scope.error = function(err) {
if(err == 'cancel') return false;
toastr.error(err);
}
$scope.getDiscounts();
}
})(angular.module('uniko.admin.discount'));