Passed
Push — master ( 844492...7c39ee )
by
unknown
09:45 queued 11s
created

myems-admin/app/controllers/settings/photovoltaicpowerstation/photovoltaicpowerstation.controller.js   D

Complexity

Total Complexity 59
Complexity/F 1.31

Size

Lines of Code 354
Function Count 45

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 59
eloc 252
dl 0
loc 354
rs 4.08
c 0
b 0
f 0
mnd 14
bc 14
fnc 45
bpm 0.3111
cpm 1.3111
noi 0

How to fix   Complexity   

Complexity

Complex classes like myems-admin/app/controllers/settings/photovoltaicpowerstation/photovoltaicpowerstation.controller.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
'use strict';
2
3
app.controller('PhotovoltaicPowerStationController', function(
4
    $scope,
5
    $rootScope,
6
    $window,
7
    $translate,
8
    $uibModal,
9
    ContactService,
10
    CostCenterService,
11
    SVGService,
12
    PhotovoltaicPowerStationService,
13
    toaster,
14
    SweetAlert) {
15
	$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
16
	$scope.exportdata = '';
17
	$scope.importdata = '';
18
19
	$scope.getAllContacts = function() {
20
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token};
21
		ContactService.getAllContacts(headers, function (response) {
22
			if (angular.isDefined(response.status) && response.status === 200) {
23
				$scope.contacts = response.data;
24
			} else {
25
				$scope.contacts = [];
26
			}
27
		});
28
	};
29
30
	$scope.getAllCostCenters = function() {
31
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
32
		CostCenterService.getAllCostCenters(headers, function (response) {
33
			if (angular.isDefined(response.status) && response.status === 200) {
34
				$scope.costcenters = response.data;
35
			} else {
36
				$scope.costcenters = [];
37
			}
38
		});
39
	};
40
41
	$scope.getAllSVGs = function() {
42
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token, "Quickmode": 'true'  };
43
		SVGService.getAllSVGs(headers, function (response) {
44
			if (angular.isDefined(response.status) && response.status === 200) {
45
				$scope.svgs = response.data;
46
			} else {
47
				$scope.svgs = [];
48
			}
49
		});
50
	};
51
52
	$scope.getAllPhotovoltaicPowerStations = function() {
53
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
54
		PhotovoltaicPowerStationService.getAllPhotovoltaicPowerStations(headers, function (response) {
55
			if (angular.isDefined(response.status) && response.status === 200) {
56
				$scope.photovoltaicpowerstations = response.data;
57
			} else {
58
				$scope.photovoltaicpowerstations = [];
59
			}
60
		});
61
	};
62
63
        let searchDebounceTimer = null;
64
        $scope.searchPhotovoltaicPowerStations = function() {
65
                let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
66
                const rawKeyword = $scope.searchKeyword || "";
67
                const trimmedKeyword = rawKeyword.trim();
68
                if (searchDebounceTimer) {
69
                    clearTimeout(searchDebounceTimer);
70
                }               
71
                searchDebounceTimer = setTimeout(() => {
72
                    if (!trimmedKeyword) {
73
                        $scope.getAllPhotovoltaicPowerStations();
74
                        return;
75
                    }
76
                PhotovoltaicPowerStationService.searchPhotovoltaicPowerStations(trimmedKeyword, headers, function (response) {
77
                        if (angular.isDefined(response.status) && response.status === 200) {
78
                                $scope.photovoltaicpowerstations = response.data;
79
                        } else {
80
                                $scope.photovoltaicpowerstations = [];
81
                        }       
82
                });
83
                }, 300);
84
        };
85
86
	$scope.getAllPhaseOfLifecycles = function() {
87
		$scope.phaseoflifecycles = [
88
			{"code":"1use", "name": $translate.instant("PHOTOVOLTAIC_POWER_STATION.PHASE_1USE")},
89
			{"code":"2commissioning", "name": $translate.instant("PHOTOVOLTAIC_POWER_STATION.PHASE_2COMMISSIONING")},
90
			{"code":"3installation", "name": $translate.instant("PHOTOVOLTAIC_POWER_STATION.PHASE_3INSTALLATION")}
91
		];
92
	};
93
	$scope.addPhotovoltaicPowerStation = function() {
94
		var modalInstance = $uibModal.open({
95
			templateUrl: 'views/settings/photovoltaicpowerstation/photovoltaicpowerstation.model.html',
96
			controller: 'ModalAddPhotovoltaicPowerStationCtrl',
97
			windowClass: "animated fadeIn",
98
			resolve: {
99
				params: function() {
100
					return {
101
						costcenters: angular.copy($scope.costcenters),
102
						contacts: angular.copy($scope.contacts),
103
						svgs: angular.copy($scope.svgs),
104
						phaseoflifecycles: angular.copy($scope.phaseoflifecycles)
105
					};
106
				}
107
			}
108
		});
109
		modalInstance.result.then(function(photovoltaicpowerstation) {
110
			photovoltaicpowerstation.cost_center_id = photovoltaicpowerstation.cost_center.id;
111
			photovoltaicpowerstation.contact_id = photovoltaicpowerstation.contact.id;
112
			photovoltaicpowerstation.svg_id = photovoltaicpowerstation.svg.id;
113
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
114
			PhotovoltaicPowerStationService.addPhotovoltaicPowerStation(photovoltaicpowerstation, headers, function(response) {
115
				if (angular.isDefined(response.status) && response.status === 201) {
116
					toaster.pop({
117
						type: "success",
118
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
119
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("COMMON.PHOTOVOLTAIC_POWER_STATION")}),
120
						showCloseButton: true,
121
					});
122
					$scope.$emit('handleEmitPhotovoltaicPowerStationChanged');
123
				} else {
124
					toaster.pop({
125
						type: "error",
126
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", { template: $translate.instant("COMMON.PHOTOVOLTAIC_POWER_STATION") }),
127
						body: $translate.instant(response.data.description),
128
						showCloseButton: true,
129
					});
130
				}
131
			});
132
		}, function() {
133
134
		});
135
		$rootScope.modalInstance = modalInstance;
136
	};
137
138
	$scope.editPhotovoltaicPowerStation = function(photovoltaicpowerstation) {
139
		var modalInstance = $uibModal.open({
140
			windowClass: "animated fadeIn",
141
			templateUrl: 'views/settings/photovoltaicpowerstation/photovoltaicpowerstation.model.html',
142
			controller: 'ModalEditPhotovoltaicPowerStationCtrl',
143
			resolve: {
144
				params: function() {
145
					return {
146
						photovoltaicpowerstation: angular.copy(photovoltaicpowerstation),
147
						costcenters:angular.copy($scope.costcenters),
148
						contacts:angular.copy($scope.contacts),
149
						svgs:angular.copy($scope.svgs),
150
						phaseoflifecycles: angular.copy($scope.phaseoflifecycles)
151
					};
152
				}
153
			}
154
		});
155
156
		modalInstance.result.then(function(modifiedPhotovoltaicPowerStation) {
157
			modifiedPhotovoltaicPowerStation.cost_center_id=modifiedPhotovoltaicPowerStation.cost_center.id;
158
			modifiedPhotovoltaicPowerStation.contact_id=modifiedPhotovoltaicPowerStation.contact.id;
159
			modifiedPhotovoltaicPowerStation.svg_id = modifiedPhotovoltaicPowerStation.svg.id;
160
161
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
162
			PhotovoltaicPowerStationService.editPhotovoltaicPowerStation(modifiedPhotovoltaicPowerStation, headers, function(response) {
163
				if (angular.isDefined(response.status) && response.status === 200) {
164
					toaster.pop({
165
						type: "success",
166
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
167
						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("COMMON.PHOTOVOLTAIC_POWER_STATION")}),
168
						showCloseButton: true,
169
					});
170
					$scope.$emit('handleEmitPhotovoltaicPowerStationChanged');
171
				} else {
172
					toaster.pop({
173
						type: "error",
174
						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("COMMON.PHOTOVOLTAIC_POWER_STATION")}),
175
						body: $translate.instant(response.data.description),
176
						showCloseButton: true,
177
					});
178
				}
179
			});
180
		}, function() {
181
			//do nothing;
182
		});
183
		$rootScope.modalInstance = modalInstance;
184
	};
185
186
	$scope.deletePhotovoltaicPowerStation=function(photovoltaicpowerstation){
187
		SweetAlert.swal({
188
      title: $translate.instant("SWEET.TITLE"),
189
      text: $translate.instant("SWEET.TEXT"),
190
      type: "warning",
191
      showCancelButton: true,
192
      confirmButtonColor: "#DD6B55",
193
      confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
194
      cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
195
      closeOnConfirm: true,
196
      closeOnCancel: true
197
    },
198
    function (isConfirm) {
199
      if (isConfirm) {
200
				let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
201
	            PhotovoltaicPowerStationService.deletePhotovoltaicPowerStation(photovoltaicpowerstation, headers, function(response) {
202
          if (angular.isDefined(response.status) && response.status === 204) {
203
						toaster.pop({
204
							type: "success",
205
							title: $translate.instant("TOASTER.SUCCESS_TITLE"),
206
							body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("COMMON.PHOTOVOLTAIC_POWER_STATION")}),
207
							showCloseButton: true,
208
						});
209
						$scope.$emit('handleEmitPhotovoltaicPowerStationChanged');
210
					}else {
211
						toaster.pop({
212
							type: "error",
213
							title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("COMMON.PHOTOVOLTAIC_POWER_STATION")}),
214
							body: $translate.instant(response.data.description),
215
							showCloseButton: true,
216
						});
217
          }
218
        });
219
      }
220
    });
221
	};
222
223
	$scope.exportPhotovoltaicPowerStation = function(photovoltaicpowerstation) {
224
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
225
		PhotovoltaicPowerStationService.exportPhotovoltaicPowerStation(photovoltaicpowerstation, headers, function(response) {
226
			if (angular.isDefined(response.status) && response.status === 200) {
227
				$scope.exportdata = JSON.stringify(response.data);
228
				var modalInstance = $uibModal.open({
229
					windowClass: "animated fadeIn",
230
					templateUrl: 'views/common/export.html',
231
					controller: 'ModalExportCtrl',
232
					resolve: {
233
						params: function() {
234
							return {
235
								exportdata: angular.copy($scope.exportdata)
236
							};
237
						}
238
					}
239
				});
240
				modalInstance.result.then(function() {
241
					//do nothing;
242
				}, function() {
243
					//do nothing;
244
				});
245
				$rootScope.modalInstance = modalInstance;
246
			} else {
247
				$scope.exportdata = null;
248
			}
249
		});
250
	};
251
252
	$scope.clonePhotovoltaicPowerStation = function(photovoltaicpowerstation){
253
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
254
		PhotovoltaicPowerStationService.clonePhotovoltaicPowerStation(photovoltaicpowerstation, headers, function(response) {
255
			if (angular.isDefined(response.status) && response.status === 201) {
256
				toaster.pop({
257
					type: "success",
258
					title: $translate.instant("TOASTER.SUCCESS_TITLE"),
259
					body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("COMMON.PHOTOVOLTAIC_POWER_STATION")}),
260
					showCloseButton: true,
261
				});
262
				$scope.$emit('handleEmitPhotovoltaicPowerStationChanged');
263
			}else {
264
				toaster.pop({
265
					type: "error",
266
					title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("COMMON.PHOTOVOLTAIC_POWER_STATION")}),
267
					body: $translate.instant(response.data.description),
268
					showCloseButton: true,
269
				});
270
			}
271
		});
272
	};
273
274
	$scope.importPhotovoltaicPowerStation = function() {
275
		var modalInstance = $uibModal.open({
276
			templateUrl: 'views/common/import.html',
277
			controller: 'ModalImportCtrl',
278
			windowClass: "animated fadeIn",
279
			resolve: {
280
				params: function() {
281
					return {
282
					};
283
				}
284
			}
285
		});
286
		modalInstance.result.then(function(importdata) {
287
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
288
			PhotovoltaicPowerStationService.importPhotovoltaicPowerStation(importdata, headers, function(response) {
289
				if (angular.isDefined(response.status) && response.status === 201) {
290
					toaster.pop({
291
						type: "success",
292
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
293
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("COMMON.PHOTOVOLTAIC_POWER_STATION")}),
294
						showCloseButton: true,
295
					});
296
					$scope.$emit('handleEmitPhotovoltaicPowerStationChanged');
297
				} else {
298
					toaster.pop({
299
						type: "error",
300
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", { template: $translate.instant("COMMON.PHOTOVOLTAIC_POWER_STATION") }),
301
						body: $translate.instant(response.data.description),
302
						showCloseButton: true,
303
					});
304
				}
305
			});
306
		}, function() {
307
308
		});
309
		$rootScope.modalInstance = modalInstance;
310
	};
311
312
	$scope.getAllPhotovoltaicPowerStations();
313
	$scope.getAllContacts();
314
	$scope.getAllCostCenters();
315
	$scope.getAllSVGs();
316
	$scope.getAllPhaseOfLifecycles();
317
	$scope.$on('handleBroadcastPhotovoltaicPowerStationChanged', function(event) {
318
  		$scope.getAllPhotovoltaicPowerStations();
319
	});
320
});
321
322
app.controller('ModalAddPhotovoltaicPowerStationCtrl', function($scope, $uibModalInstance,params) {
323
	$scope.operation = "SETTING.ADD_PHOTOVOLTAIC_POWER_STATION";
324
	$scope.costcenters=params.costcenters;
325
	$scope.contacts=params.contacts;
326
	$scope.svgs=params.svgs;
327
	$scope.phaseoflifecycles=params.phaseoflifecycles;
328
	$scope.photovoltaicpowerstation = {
329
		is_cost_data_displayed: false
330
	};
331
	$scope.ok = function() {
332
		$uibModalInstance.close($scope.photovoltaicpowerstation);
333
	};
334
335
    $scope.cancel = function() {
336
		$uibModalInstance.dismiss('cancel');
337
	};
338
});
339
340
app.controller('ModalEditPhotovoltaicPowerStationCtrl', function($scope, $uibModalInstance, params) {
341
	$scope.operation = "SETTING.EDIT_PHOTOVOLTAIC_POWER_STATION";
342
	$scope.photovoltaicpowerstation = params.photovoltaicpowerstation;
343
	$scope.costcenters=params.costcenters;
344
	$scope.contacts=params.contacts;
345
	$scope.svgs=params.svgs;
346
	$scope.phaseoflifecycles=params.phaseoflifecycles;
347
	$scope.ok = function() {
348
		$uibModalInstance.close($scope.photovoltaicpowerstation);
349
	};
350
351
	$scope.cancel = function() {
352
		$uibModalInstance.dismiss('cancel');
353
	};
354
});
355