Passed
Push — master ( 98975f...e9b166 )
by
unknown
09:13
created

myems-admin/app/controllers/settings/equipment/equipment.controller.js   C

Complexity

Total Complexity 56
Complexity/F 1.37

Size

Lines of Code 333
Function Count 41

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 56
eloc 235
dl 0
loc 333
rs 5.5199
c 0
b 0
f 0
mnd 15
bc 15
fnc 41
bpm 0.3658
cpm 1.3657
noi 0

How to fix   Complexity   

Complexity

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