cobuild-angular-stack
Version:
Base stack angular sass jade gulp
117 lines (102 loc) • 5.18 kB
JavaScript
(function (module) {
'use strict';
module.controller('CashOutController', CashOutController);
CashOutController.$inject = ['$rootScope', '$scope', '$timeout', '$state', 'CoupleAccount', '$http', '$stateParams', '$uibModal', 'toastr', 'Upload', 'Auth', 'uiGmapGoogleMapApi', '$analytics', '$translate', 'lodash'];
function CashOutController($rootScope, $scope, $timeout, $state, CoupleAccount, $http, $stateParams, $uibModal, toastr, Upload, Auth, GoogleMapApi, $analytics, $translate, lodash) {
$scope.account = CoupleAccount.getCurrent();
$scope.disabled = true;
$scope.btnDisabled = true;
$scope.cashout = {};
$scope.account
.$promise
.then(function (account) {
return CoupleAccount.cashOutInformation({id: account.id}).$promise;
})
.then(function (cashOutInformation) {
$scope.account.cashOutInformationList = cashOutInformation;
});
$scope.validCashout = 0;
$scope.$watch(function () {
return $scope.account.cashOutInformationList ? JSON.stringify($scope.account.cashOutInformationList) : null;
}, function () {
var account = $scope.account;
if (account && account.cashOutInformationList) {
var cashOutInformation = account.cashOutInformationList;
var validCashout = $scope.validCashout = lodash.reduce(cashOutInformation, function (valid, cashout) {
return cashout.isCancelled ? valid : valid + 1;
}, 0);
console.log(!account.payInformationData);
$scope.disabled = !account.payInformationData.isPremium ? (account.isDisabled ? validCashout >= 3 : validCashout >= 2) : false;
$scope.btnDisabled = false;
}
});
this.send = function () {
var account = $scope.account;
if ((!$scope.disabled && $scope.form_message.$invalid) || (!account.payInformationData.isPremium && $scope.disabled && $scope.validCashout >= 3)) {
$scope.form_message.$setPristine();
$scope.form_message.$setUntouched();
return;
}
if (!account.payInformationData.isPremium && !$scope.account.isDisabled && $scope.validCashout === 2) {
return $uibModal.open({
animation: true,
templateUrl: 'partials/cashout.error-dialog.html',
controller: ['$uibModalInstance', '$scope', function ($uibModalInstance, $scope) {
$scope.ok = function () {
$uibModalInstance.close();
};
}]
});
}
$scope.btnDisabled = true;
var modalInstance = $uibModal.open({
animation: true,
templateUrl: 'partials/cashout.confirm-dialog.html',
controller: ['$uibModalInstance', '$scope', function ($uibModalInstance, $scope) {
$scope.ok = function () {
$uibModalInstance.close();
};
$scope.cancel = function () {
$uibModalInstance.dismiss();
};
}]
});
modalInstance.result.then(function () {
CoupleAccount.cashOutInformation.create({id: $scope.account.id}, $scope.cashout)
.$promise
.then(function (data) {
$scope.cashout = {};
$scope.form_message.$setPristine();
$scope.form_message.$setUntouched();
$analytics.eventTrack('Account Payment Date', {});
$scope.account.cashOutInformationList.push(data);
$rootScope.$broadcast('cashoutUpdate');
$translate('request-success').then(function (translation) {
toastr.success(translation);
});
})
.catch(function (response) {
$scope.btnDisabled = false;
if (response.status === 422) {
lodash.forEach(response.data.error.details.codes, function (code, key) {
switch (key) {
case 'requiredAmount':
$translate('no-fund').then(function (translation) {
toastr.error(translation);
});
break;
}
});
}
});
});
};
$scope.account.$promise
.then(function () {
$scope.disabled = false;
})
.catch(function (err) {
console.error(err);
});
}
})(angular.module('uniko.cashout'));