1
|
|
|
'use strict'; |
2
|
|
|
|
3
|
|
|
app.controller('OfflineMeterController', function($scope, $common, $translate, $uibModal, OfflineMeterService, CategoryService, EnergyItemService, CostCenterService, toaster, SweetAlert) { |
4
|
|
|
$scope.getAllCostCenters = function() { |
5
|
|
|
CostCenterService.getAllCostCenters(function(error, data) { |
6
|
|
|
if (!error) { |
7
|
|
|
$scope.costcenters = data; |
8
|
|
|
} else { |
9
|
|
|
$scope.costcenters = []; |
10
|
|
|
} |
11
|
|
|
}); |
12
|
|
|
}; |
13
|
|
|
|
14
|
|
|
$scope.getAllCategories = function() { |
15
|
|
|
CategoryService.getAllCategories(function(error, data) { |
16
|
|
|
if (!error) { |
17
|
|
|
$scope.categories = data; |
18
|
|
|
} else { |
19
|
|
|
$scope.categories = []; |
20
|
|
|
} |
21
|
|
|
}); |
22
|
|
|
}; |
23
|
|
|
|
24
|
|
|
$scope.getAllEnergyItems = function() { |
25
|
|
|
EnergyItemService.getAllEnergyItems(function(error, data) { |
26
|
|
|
if (!error) { |
27
|
|
|
$scope.energyitems = data; |
28
|
|
|
} else { |
29
|
|
|
$scope.energyitems = []; |
30
|
|
|
} |
31
|
|
|
}); |
32
|
|
|
}; |
33
|
|
|
|
34
|
|
|
$scope.getAllOfflineMeters = function() { |
35
|
|
|
OfflineMeterService.getAllOfflineMeters(function(error, data) { |
36
|
|
|
if (!error) { |
37
|
|
|
$scope.offlinemeters = data; |
38
|
|
|
} else { |
39
|
|
|
$scope.offlinemeters = []; |
40
|
|
|
} |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
}; |
44
|
|
|
|
45
|
|
|
$scope.addOfflineMeter = function() { |
46
|
|
|
var modalInstance = $uibModal.open({ |
47
|
|
|
templateUrl: 'views/settings/meter/offlinemeter.model.html', |
48
|
|
|
controller: 'ModalAddOfflineMeterCtrl', |
49
|
|
|
windowClass: "animated fadeIn", |
50
|
|
|
resolve: { |
51
|
|
|
params: function() { |
52
|
|
|
return { |
53
|
|
|
offlinemeters: angular.copy($scope.offlinemeters), |
54
|
|
|
categories: angular.copy($scope.categories), |
55
|
|
|
energyitems: angular.copy($scope.energyitems), |
56
|
|
|
costcenters: angular.copy($scope.costcenters) |
57
|
|
|
}; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
}); |
61
|
|
|
modalInstance.result.then(function(offlinemeter) { |
62
|
|
|
offlinemeter.energy_category_id = offlinemeter.energy_category.id; |
63
|
|
|
if(angular.isDefined(offlinemeter.energy_item)) { |
64
|
|
|
offlinemeter.energy_item_id = offlinemeter.energy_item.id; |
65
|
|
|
} else { |
66
|
|
|
offlinemeter.energy_item_id = undefined; |
67
|
|
|
} |
68
|
|
|
offlinemeter.cost_center_id = offlinemeter.cost_center.id; |
69
|
|
|
OfflineMeterService.addOfflineMeter(offlinemeter, function(error, status) { |
70
|
|
|
if (angular.isDefined(status) && status == 201) { |
71
|
|
|
var templateName = "SETTING.OFFLINE_METER"; |
72
|
|
|
templateName = $translate.instant(templateName); |
73
|
|
|
|
74
|
|
|
var popType = 'TOASTER.SUCCESS'; |
75
|
|
|
var popTitle = $common.toaster.success_title; |
76
|
|
|
var popBody = $common.toaster.success_add_body; |
77
|
|
|
|
78
|
|
|
popType = $translate.instant(popType); |
79
|
|
|
popTitle = $translate.instant(popTitle); |
80
|
|
|
popBody = $translate.instant(popBody,{template: templateName}); |
81
|
|
|
|
82
|
|
|
toaster.pop({ |
83
|
|
|
type: popType, |
84
|
|
|
title: popTitle, |
85
|
|
|
body: popBody, |
86
|
|
|
showCloseButton: true, |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
$scope.getAllOfflineMeters(); |
90
|
|
|
$scope.$emit('handleEmitOfflineMeterChanged'); |
91
|
|
|
} else { |
92
|
|
|
var templateName = "SETTING.OFFLINE_METER"; |
93
|
|
|
templateName = $translate.instant(templateName); |
94
|
|
|
|
95
|
|
|
var popType = 'TOASTER.ERROR'; |
96
|
|
|
var popTitle = $common.toaster.error_title; |
97
|
|
|
var popBody = $common.toaster.error_add_body; |
98
|
|
|
|
99
|
|
|
popType = $translate.instant(popType); |
100
|
|
|
popTitle = $translate.instant(popTitle); |
101
|
|
|
popBody = $translate.instant(popBody,{template: templateName}); |
102
|
|
|
|
103
|
|
|
toaster.pop({ |
104
|
|
|
type: popType, |
105
|
|
|
title: popTitle, |
106
|
|
|
body: popBody, |
107
|
|
|
showCloseButton: true, |
108
|
|
|
}); |
109
|
|
|
} |
110
|
|
|
}); |
111
|
|
|
}, function() { |
112
|
|
|
|
113
|
|
|
}); |
114
|
|
|
}; |
115
|
|
|
|
116
|
|
|
$scope.editOfflineMeter = function(offlinemeter) { |
117
|
|
|
var modalInstance = $uibModal.open({ |
118
|
|
|
windowClass: "animated fadeIn", |
119
|
|
|
templateUrl: 'views/settings/meter/offlinemeter.model.html', |
120
|
|
|
controller: 'ModalEditOfflineMeterCtrl', |
121
|
|
|
resolve: { |
122
|
|
|
params: function() { |
123
|
|
|
return { |
124
|
|
|
offlinemeter: angular.copy(offlinemeter), |
125
|
|
|
offlinemeters: angular.copy($scope.offlinemeters), |
126
|
|
|
categories: angular.copy($scope.categories), |
127
|
|
|
energyitems: angular.copy($scope.energyitems), |
128
|
|
|
costcenters: angular.copy($scope.costcenters) |
129
|
|
|
}; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
}); |
133
|
|
|
|
134
|
|
|
modalInstance.result.then(function(modifiedOfflineMeter) { |
135
|
|
|
modifiedOfflineMeter.energy_category_id = modifiedOfflineMeter.energy_category.id; |
136
|
|
|
if (modifiedOfflineMeter.energy_item != null && modifiedOfflineMeter.energy_item.id != null ) { |
137
|
|
|
modifiedOfflineMeter.energy_item_id = modifiedOfflineMeter.energy_item.id; |
138
|
|
|
} else { |
139
|
|
|
modifiedOfflineMeter.energy_item_id = undefined; |
140
|
|
|
} |
141
|
|
|
modifiedOfflineMeter.cost_center_id = modifiedOfflineMeter.cost_center.id; |
142
|
|
|
OfflineMeterService.editOfflineMeter(modifiedOfflineMeter, function(error, status) { |
143
|
|
|
if (angular.isDefined(status) && status == 200) { |
144
|
|
|
var templateName = "SETTING.OFFLINE_METER"; |
145
|
|
|
templateName = $translate.instant(templateName); |
146
|
|
|
|
147
|
|
|
var popType = 'TOASTER.SUCCESS'; |
148
|
|
|
var popTitle = $common.toaster.success_title; |
149
|
|
|
var popBody = $common.toaster.success_update_body; |
150
|
|
|
|
151
|
|
|
popType = $translate.instant(popType); |
152
|
|
|
popTitle = $translate.instant(popTitle); |
153
|
|
|
popBody = $translate.instant(popBody,{template: templateName}); |
154
|
|
|
|
155
|
|
|
toaster.pop({ |
156
|
|
|
type: popType, |
157
|
|
|
title: popTitle, |
158
|
|
|
body: popBody, |
159
|
|
|
showCloseButton: true, |
160
|
|
|
}); |
161
|
|
|
$scope.getAllOfflineMeters(); |
162
|
|
|
$scope.$emit('handleEmitOfflineMeterChanged'); |
163
|
|
|
} else { |
164
|
|
|
var templateName = "SETTING.OFFLINE_METER"; |
165
|
|
|
templateName = $translate.instant(templateName); |
166
|
|
|
|
167
|
|
|
var popType = 'TOASTER.ERROR'; |
168
|
|
|
var popTitle = $common.toaster.error_title; |
169
|
|
|
var popBody = $common.toaster.error_update_body; |
170
|
|
|
|
171
|
|
|
popType = $translate.instant(popType); |
172
|
|
|
popTitle = $translate.instant(popTitle); |
173
|
|
|
popBody = $translate.instant(popBody,{template: templateName}); |
174
|
|
|
|
175
|
|
|
toaster.pop({ |
176
|
|
|
type: popType, |
177
|
|
|
title: popTitle, |
178
|
|
|
body: popBody, |
179
|
|
|
showCloseButton: true, |
180
|
|
|
}); |
181
|
|
|
} |
182
|
|
|
}); |
183
|
|
|
}, function() { |
184
|
|
|
//do nothing; |
185
|
|
|
}); |
186
|
|
|
}; |
187
|
|
|
|
188
|
|
|
$scope.deleteOfflineMeter = function(offlinemeter) { |
189
|
|
|
SweetAlert.swal({ |
190
|
|
|
title: $translate.instant($common.sweet.title), |
191
|
|
|
text: $translate.instant($common.sweet.text), |
192
|
|
|
type: "warning", |
193
|
|
|
showCancelButton: true, |
194
|
|
|
confirmButtonColor: "#DD6B55", |
195
|
|
|
confirmButtonText: $translate.instant($common.sweet.confirmButtonText), |
196
|
|
|
cancelButtonText: $translate.instant($common.sweet.cancelButtonText), |
197
|
|
|
closeOnConfirm: true, |
198
|
|
|
closeOnCancel: true |
199
|
|
|
}, |
200
|
|
|
function(isConfirm) { |
201
|
|
|
if (isConfirm) { |
202
|
|
|
OfflineMeterService.deleteOfflineMeter(offlinemeter, function(error, status) { |
203
|
|
|
if (angular.isDefined(status) && status == 204) { |
204
|
|
|
var templateName = "SETTING.OFFLINE_METER"; |
205
|
|
|
templateName = $translate.instant(templateName); |
206
|
|
|
|
207
|
|
|
var popType = 'TOASTER.SUCCESS'; |
208
|
|
|
var popTitle = $common.toaster.success_title; |
209
|
|
|
var popBody = $common.toaster.success_delete_body; |
210
|
|
|
|
211
|
|
|
popType = $translate.instant(popType); |
212
|
|
|
popTitle = $translate.instant(popTitle); |
213
|
|
|
popBody = $translate.instant(popBody, {template: templateName}); |
214
|
|
|
|
215
|
|
|
toaster.pop({ |
216
|
|
|
type: popType, |
217
|
|
|
title: popTitle, |
218
|
|
|
body: popBody, |
219
|
|
|
showCloseButton: true, |
220
|
|
|
}); |
221
|
|
|
$scope.getAllOfflineMeters(); |
222
|
|
|
$scope.$emit('handleEmitOfflineMeterChanged'); |
223
|
|
|
} else if (angular.isDefined(status) && status == 400) { |
224
|
|
|
var popType = 'TOASTER.ERROR'; |
225
|
|
|
var popTitle = error.title; |
226
|
|
|
var popBody = error.description; |
227
|
|
|
|
228
|
|
|
popType = $translate.instant(popType); |
229
|
|
|
popTitle = $translate.instant(popTitle); |
230
|
|
|
popBody = $translate.instant(popBody); |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
toaster.pop({ |
234
|
|
|
type: popType, |
235
|
|
|
title: popTitle, |
236
|
|
|
body: popBody, |
237
|
|
|
showCloseButton: true, |
238
|
|
|
}); |
239
|
|
|
} else { |
240
|
|
|
var templateName = "SETTING.OFFLINE_METER"; |
241
|
|
|
templateName = $translate.instant(templateName); |
242
|
|
|
|
243
|
|
|
var popType = 'TOASTER.ERROR'; |
244
|
|
|
var popTitle = $common.toaster.error_title; |
245
|
|
|
var popBody = $common.toaster.error_delete_body; |
246
|
|
|
|
247
|
|
|
popType = $translate.instant(popType); |
248
|
|
|
popTitle = $translate.instant(popTitle); |
249
|
|
|
popBody = $translate.instant(popBody, {template: templateName}); |
250
|
|
|
|
251
|
|
|
toaster.pop({ |
252
|
|
|
type: popType, |
253
|
|
|
title: popTitle, |
254
|
|
|
body: popBody, |
255
|
|
|
showCloseButton: true, |
256
|
|
|
}); |
257
|
|
|
} |
258
|
|
|
}); |
259
|
|
|
} |
260
|
|
|
}); |
261
|
|
|
}; |
262
|
|
|
|
263
|
|
|
$scope.getAllOfflineMeters(); |
264
|
|
|
$scope.getAllCategories(); |
265
|
|
|
$scope.getAllEnergyItems(); |
266
|
|
|
$scope.getAllCostCenters(); |
267
|
|
|
}); |
268
|
|
|
|
269
|
|
|
app.controller('ModalAddOfflineMeterCtrl', function($scope, $uibModalInstance, params) { |
270
|
|
|
|
271
|
|
|
$scope.operation = "SETTING.ADD_OFFLINE_METER"; |
272
|
|
|
$scope.categories = params.categories; |
273
|
|
|
$scope.energyitems = params.energyitems; |
274
|
|
|
$scope.costcenters = params.costcenters; |
275
|
|
|
$scope.offlinemeter = { |
276
|
|
|
is_counted: false |
277
|
|
|
}; |
278
|
|
|
$scope.ok = function() { |
279
|
|
|
$uibModalInstance.close($scope.offlinemeter); |
280
|
|
|
}; |
281
|
|
|
|
282
|
|
|
$scope.cancel = function() { |
283
|
|
|
$uibModalInstance.dismiss('cancel'); |
284
|
|
|
}; |
285
|
|
|
}); |
286
|
|
|
|
287
|
|
|
app.controller('ModalEditOfflineMeterCtrl', function($scope, $uibModalInstance, params) { |
288
|
|
|
$scope.operation = "SETTING.EDIT_OFFLINE_METER"; |
289
|
|
|
$scope.offlinemeter = params.offlinemeter; |
290
|
|
|
$scope.offlinemeters = params.offlinemeters; |
291
|
|
|
$scope.categories = params.categories; |
292
|
|
|
$scope.energyitems = params.energyitems; |
293
|
|
|
$scope.costcenters = params.costcenters; |
294
|
|
|
|
295
|
|
|
$scope.ok = function() { |
296
|
|
|
$uibModalInstance.close($scope.offlinemeter); |
297
|
|
|
}; |
298
|
|
|
|
299
|
|
|
$scope.cancel = function() { |
300
|
|
|
$uibModalInstance.dismiss('cancel'); |
301
|
|
|
}; |
302
|
|
|
}); |
303
|
|
|
|