Passed
Push — master ( 283033...66ce56 )
by Guangyu
07:51 queued 11s
created

settings/shopfloor/shopfloor.controller.js (1 issue)

1
'use strict';
2
3
app.controller('ShopfloorController', function (
4
    $scope,
5
    $window,
6
    $translate,
7
    $uibModal,
8
    CostCenterService,
9
    ContactService,
10
    ShopfloorService,
11
    toaster,
12
    SweetAlert) {
13
	$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
14
	$scope.getAllCostCenters = function () {
15
		CostCenterService.getAllCostCenters(function (response) {
16
			if (angular.isDefined(response.status) && response.status === 200) {
17
				$scope.costcenters = response.data;
18
			} else {
19
				$scope.costcenters = [];
20
			}
21
		});
22
	};
23
24
	$scope.getAllContacts = function () {
25
		ContactService.getAllContacts(function (response) {
26
			if (angular.isDefined(response.status) && response.status === 200) {
27
				$scope.contacts = response.data;
28
			} else {
29
				$scope.contacts = [];
30
			}
31
		});
32
	};
33
34
	$scope.getAllShopfloors = function () {
35
		ShopfloorService.getAllShopfloors(function (response) {
36
			if (angular.isDefined(response.status) && response.status === 200) {
37
				$scope.shopfloors = response.data;
38
			} else {
39
				$scope.shopfloors = [];
40
			}
41
		});
42
	};
43
44
	$scope.addShopfloor = function () {
45
		var modalInstance = $uibModal.open({
46
			templateUrl: 'views/settings/shopfloor/shopfloor.model.html',
47
			controller: 'ModalAddShopfloorCtrl',
48
			windowClass: "animated fadeIn",
49
			resolve: {
50
				params: function () {
51
					return {
52
						shopfloors: angular.copy($scope.shopfloors),
53
						costcenters: angular.copy($scope.costcenters),
54
						contacts: angular.copy($scope.contacts),
55
					};
56
				}
57
			}
58
		});
59
		modalInstance.result.then(function (shopfloor) {
60
			shopfloor.cost_center_id = shopfloor.cost_center.id;
61
			shopfloor.contact_id = shopfloor.contact.id;
62
			if (angular.isDefined(shopfloor.is_input_counted) == false) {
63
				shopfloor.is_input_counted = false;
64
			}
65
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
66
			ShopfloorService.addShopfloor(shopfloor, headers, function (response) {
67
				if (angular.isDefined(response.status) && response.status === 201) {
68
					toaster.pop({
69
						type: "success",
70
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
71
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", { template: $translate.instant("COMMON.SHOPFLOOR") }),
72
						showCloseButton: true,
73
					});
74
					$scope.$emit('handleEmitShopfloorChanged');
75
				} else {
76
					toaster.pop({
77
						type: "error",
78
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", { template: $translate.instant("COMMON.SHOPFLOOR") }),
79
						body: $translate.instant(response.data.description),
80
						showCloseButton: true,
81
					});
82
				}
83
			});
84
		}, function () {
85
86
		});
87
	};
88
89
	$scope.editShopfloor = function(shopfloor) {
90
		var modalInstance = $uibModal.open({
91
			windowClass: "animated fadeIn",
92
			templateUrl: 'views/settings/shopfloor/shopfloor.model.html',
93
			controller: 'ModalEditShopfloorCtrl',
94
			resolve: {
95
				params: function () {
96
					return {
97
						shopfloor: angular.copy(shopfloor),
98
						costcenters: angular.copy($scope.costcenters),
99
						contacts: angular.copy($scope.contacts)
100
					};
101
				}
102
			}
103
		});
104
105
		modalInstance.result.then(function(modifiedShopfloor) {
106
			console.log(modifiedShopfloor);
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
107
			modifiedShopfloor.cost_center_id = modifiedShopfloor.cost_center.id;
108
			modifiedShopfloor.contact_id = modifiedShopfloor.contact.id;
109
			if (angular.isDefined(shopfloor.is_input_counted) == false) {
110
				shopfloor.is_input_counted = false;
111
			}
112
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
113
			ShopfloorService.editShopfloor(modifiedShopfloor, headers, function (response) {
114
				if (angular.isDefined(response.status) && response.status === 200) {
115
					toaster.pop({
116
						type: "success",
117
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
118
						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", { template: $translate.instant("COMMON.SHOPFLOOR") }),
119
						showCloseButton: true,
120
					});
121
					$scope.$emit('handleEmitShopfloorChanged');
122
				} else {
123
					toaster.pop({
124
						type: "error",
125
						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", { template: $translate.instant("COMMON.SHOPFLOOR") }),
126
						body: $translate.instant(response.data.description),
127
						showCloseButton: true,
128
					});
129
				}
130
			});
131
		}, function () {
132
			//do nothing;
133
		});
134
	};
135
136
	$scope.deleteShopfloor = function (shopfloor) {
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
		},
148
			function (isConfirm) {
149
				if (isConfirm) {
150
					let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
151
					ShopfloorService.deleteShopfloor(shopfloor, headers, function (response) {
152
						if (angular.isDefined(response.status) && response.status === 204) {
153
							toaster.pop({
154
								type: "success",
155
								title: $translate.instant("TOASTER.SUCCESS_TITLE"),
156
								body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", { template: $translate.instant("COMMON.SHOPFLOOR") }),
157
								showCloseButton: true,
158
							});
159
							$scope.$emit('handleEmitShopfloorChanged');
160
						} else {
161
							toaster.pop({
162
								type: "error",
163
								title: $translate.instant("TOASTER.ERROR_DELETE_BODY", { template: $translate.instant("COMMON.SHOPFLOOR") }),
164
								body: $translate.instant(response.data.description),
165
								showCloseButton: true,
166
							});
167
						}
168
					});
169
				}
170
			});
171
	};
172
	$scope.getAllShopfloors();
173
	$scope.getAllCostCenters();
174
	$scope.getAllContacts();
175
	$scope.$on('handleBroadcastShopfloorChanged', function (event) {
176
		$scope.getAllShopfloors();
177
	});
178
});
179
180
app.controller('ModalAddShopfloorCtrl', function ($scope, $uibModalInstance, params) {
181
182
	$scope.operation = "SHOPFLOOR.ADD_SHOPFLOOR";
183
	$scope.costcenters = params.costcenters;
184
	$scope.contacts = params.contacts;
185
	$scope.ok = function () {
186
		$uibModalInstance.close($scope.shopfloor);
187
	};
188
189
	$scope.cancel = function () {
190
		$uibModalInstance.dismiss('cancel');
191
	};
192
});
193
194
app.controller('ModalEditShopfloorCtrl', function ($scope, $uibModalInstance, params) {
195
	$scope.operation = "SHOPFLOOR.EDIT_SHOPFLOOR";
196
	$scope.shopfloor = params.shopfloor;
197
	$scope.costcenters = params.costcenters;
198
	$scope.contacts = params.contacts;
199
	
200
	$scope.ok = function () {
201
		$uibModalInstance.close($scope.shopfloor);
202
	};
203
204
	$scope.cancel = function () {
205
		$uibModalInstance.dismiss('cancel');
206
	};
207
});
208