Passed
Push — master ( 8e7a04...f78861 )
by
unknown
11:43 queued 16s
created

myems-admin/app/controllers/settings/hybridpowerstation/hybridpowerstation.controller.js   A

Complexity

Total Complexity 34
Complexity/F 1.26

Size

Lines of Code 205
Function Count 27

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 34
eloc 145
dl 0
loc 205
rs 9.68
c 0
b 0
f 0
mnd 7
bc 7
fnc 27
bpm 0.2592
cpm 1.2592
noi 0
1
'use strict';
2
3
app.controller('HybridPowerStationController', function(
4
    $scope,
5
    $rootScope,
6
    $window,
7
    $translate,
8
    $uibModal,
9
    CostCenterService,
10
    ContactService,
11
    HybridPowerStationService,
12
    toaster,
13
    SweetAlert) {
14
	$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
15
	$scope.getAllCostCenters = function() {
16
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
17
		CostCenterService.getAllCostCenters(headers, function (response) {
18
			if (angular.isDefined(response.status) && response.status === 200) {
19
				$scope.costcenters = response.data;
20
			} else {
21
				$scope.costcenters = [];
22
			}
23
		});
24
	};
25
26
	$scope.getAllContacts = function() {
27
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
28
		ContactService.getAllContacts(headers, function (response) {
29
			if (angular.isDefined(response.status) && response.status === 200) {
30
				$scope.contacts = response.data;
31
			} else {
32
				$scope.contacts = [];
33
			}
34
		});
35
	};
36
37
	$scope.getAllHybridPowerStations = function() {
38
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
39
		HybridPowerStationService.getAllHybridPowerStations(headers, function (response) {
40
			if (angular.isDefined(response.status) && response.status === 200) {
41
				$scope.hybridpowerstations = response.data;
42
			} else {
43
				$scope.hybridpowerstations = [];
44
			}
45
		});
46
	};
47
48
	$scope.addHybridPowerStation = function() {
49
		var modalInstance = $uibModal.open({
50
			templateUrl: 'views/settings/hybridpowerstation/hybridpowerstation.model.html',
51
			controller: 'ModalAddHybridPowerStationCtrl',
52
			windowClass: "animated fadeIn",
53
			resolve: {
54
				params: function() {
55
					return {
56
						costcenters: angular.copy($scope.costcenters),
57
						contacts: angular.copy($scope.contacts),
58
					};
59
				}
60
			}
61
		});
62
		modalInstance.result.then(function(hybridpowerstation) {
63
			hybridpowerstation.cost_center_id = hybridpowerstation.cost_center.id;
64
			hybridpowerstation.contact_id = hybridpowerstation.contact.id;
65
66
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
67
			HybridPowerStationService.addHybridPowerStation(hybridpowerstation, headers, function(response) {
68
				if (angular.isDefined(response.status) && response.status === 201) {
69
					toaster.pop({
70
						type: "success",
71
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
72
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("COMMON.HYBRID_POWER_STATION")}),
73
						showCloseButton: true,
74
					});
75
					$scope.$emit('handleEmitHybridPowerStationChanged');
76
				} else {
77
					toaster.pop({
78
						type: "error",
79
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", { template: $translate.instant("COMMON.HYBRID_POWER_STATION") }),
80
						body: $translate.instant(response.data.description),
81
						showCloseButton: true,
82
					});
83
				}
84
			});
85
		}, function() {
86
87
		});
88
		$rootScope.modalInstance = modalInstance;
89
	};
90
91
	$scope.editHybridPowerStation = function(hybridpowerstation) {
92
		var modalInstance = $uibModal.open({
93
			windowClass: "animated fadeIn",
94
			templateUrl: 'views/settings/hybridpowerstation/hybridpowerstation.model.html',
95
			controller: 'ModalEditHybridPowerStationCtrl',
96
			resolve: {
97
				params: function() {
98
					return {
99
						hybridpowerstation: angular.copy(hybridpowerstation),
100
						costcenters:angular.copy($scope.costcenters),
101
						contacts:angular.copy($scope.contacts),
102
					};
103
				}
104
			}
105
		});
106
107
		modalInstance.result.then(function(modifiedHybridPowerStation) {
108
			modifiedHybridPowerStation.cost_center_id=modifiedHybridPowerStation.cost_center.id;
109
			modifiedHybridPowerStation.contact_id=modifiedHybridPowerStation.contact.id;
110
111
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
112
			HybridPowerStationService.editHybridPowerStation(modifiedHybridPowerStation, headers, function(response) {
113
				if (angular.isDefined(response.status) && response.status === 200) {
114
					toaster.pop({
115
						type: "success",
116
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
117
						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("COMMON.HYBRID_POWER_STATION")}),
118
						showCloseButton: true,
119
					});
120
					$scope.$emit('handleEmitHybridPowerStationChanged');
121
				} else {
122
					toaster.pop({
123
						type: "error",
124
						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("COMMON.HYBRID_POWER_STATION")}),
125
						body: $translate.instant(response.data.description),
126
						showCloseButton: true,
127
					});
128
				}
129
			});
130
		}, function() {
131
			//do nothing;
132
		});
133
		$rootScope.modalInstance = modalInstance;
134
	};
135
136
	$scope.deleteHybridPowerStation=function(hybridpowerstation){
137
		SweetAlert.swal({
138
		        title: $translate.instant("SWEET.TITLE"),
139
		        text: $translate.instant("SWEET.TEXT"),
140
		        type: "warning",
141
		        showCancelButton: true,
142
		        confirmButtonColor: "#DD6B55",
143
		        confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
144
		        cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
145
		        closeOnConfirm: true,
146
		        closeOnCancel: true },
147
		    function (isConfirm) {
148
		        if (isConfirm) {
149
					let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
150
		            HybridPowerStationService.deleteHybridPowerStation(hybridpowerstation, headers, function(response) {
151
		            	if (angular.isDefined(response.status) && response.status === 204) {
152
							toaster.pop({
153
								type: "success",
154
								title: $translate.instant("TOASTER.SUCCESS_TITLE"),
155
								body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("COMMON.HYBRID_POWER_STATION")}),
156
								showCloseButton: true,
157
							});
158
							$scope.$emit('handleEmitHybridPowerStationChanged');
159
						}else {
160
							toaster.pop({
161
								type: "error",
162
								title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("COMMON.HYBRID_POWER_STATION")}),
163
								body: $translate.instant(response.data.description),
164
								showCloseButton: true,
165
							});
166
		            	}
167
		            });
168
		        }
169
		    });
170
	};
171
	$scope.getAllHybridPowerStations();
172
	$scope.getAllCostCenters();
173
	$scope.getAllContacts();
174
	$scope.$on('handleBroadcastHybridPowerStationChanged', function(event) {
175
  		$scope.getAllHybridPowerStations();
176
	});
177
});
178
179
app.controller('ModalAddHybridPowerStationCtrl', function($scope, $uibModalInstance,params) {
180
181
	$scope.operation = "SETTING.ADD_HYBRID_POWER_STATION";
182
	$scope.costcenters=params.costcenters;
183
	$scope.contacts=params.contacts;
184
	$scope.ok = function() {
185
		$uibModalInstance.close($scope.hybridpowerstation);
186
	};
187
188
    $scope.cancel = function() {
189
		$uibModalInstance.dismiss('cancel');
190
	};
191
});
192
193
app.controller('ModalEditHybridPowerStationCtrl', function($scope, $uibModalInstance, params) {
194
	$scope.operation = "SETTING.EDIT_HYBRID_POWER_STATION";
195
	$scope.hybridpowerstation = params.hybridpowerstation;
196
	$scope.costcenters=params.costcenters;
197
	$scope.contacts=params.contacts;
198
	$scope.ok = function() {
199
		$uibModalInstance.close($scope.hybridpowerstation);
200
	};
201
202
	$scope.cancel = function() {
203
		$uibModalInstance.dismiss('cancel');
204
	};
205
});
206