UNPKG

cobuild-angular-stack

Version:

Base stack angular sass jade gulp

100 lines (89 loc) 2.65 kB
(function (module) { AdminDiscountPaymentAddController.$inject = ['$scope', '$state', '$stateParams', '$uibModalInstance','$translate', 'toastr', 'DiscountCode', 'discount']; module.controller('AdminDiscountPaymentAddController', AdminDiscountPaymentAddController); function AdminDiscountPaymentAddController($scope, $state, $stateParams, $uibModalInstance, $translate, toastr, DiscountCode, discount) { $scope.view = { discount: discount, date: { startDate: moment().format('DD-MM-YYYY') }, amount: 0 } $scope.date = {}; $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.view.date = {}; }, 'apply.daterangepicker': function (ev, picker) { } } }; $scope.create = function() { DiscountCode.createPayment({ discountId: $scope.view.discount.id, amount: $scope.view.amount, paymentDate: $scope.date.startDate ? $scope.date.startDate.format("DD-MM-YYYY") : moment().format("DD-MM-YYYY") }).$promise.then(function(data) { $translate('success-save-data').then(function(translation){ toastr.success(translation); $uibModalInstance.close(data); }); }).catch(function(err) { console.log(err); }); } $scope.getPayment = function() { var amount = 0; amount = $scope.view.discount.amount / $scope.view.discount.numberPayments; var numberPayments = $scope.view.discount.discountPayments.length + 1; var balance = 0; if($scope.view.discount.numberPayments == numberPayments) { balance = _.reduce($scope.view.discount.discountPayments, function(total, row) { return total + row.amount; }, 0); amount = $scope.view.discount.amount - balance; } $scope.view.amount = parseFloat(amount.toFixed(2)); } $scope.error = function(err) { if(err == 'cancel') return false; toastr.error(JSON.stringify(err)); } $scope.close = function() { $uibModalInstance.dismiss('cancel'); } $scope.getPayment(); } })(angular.module('uniko.admin.discount'));