Passed
Push — master ( e9b166...efda51 )
by
unknown
09:12
created

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

Complexity

Total Complexity 57
Complexity/F 1.36

Size

Lines of Code 330
Function Count 42

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 57
eloc 232
dl 0
loc 330
rs 5.04
c 0
b 0
f 0
mnd 15
bc 15
fnc 42
bpm 0.357
cpm 1.3571
noi 0

How to fix   Complexity   

Complexity

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