-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathable-cart.js
More file actions
249 lines (239 loc) · 6.91 KB
/
able-cart.js
File metadata and controls
249 lines (239 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
(function() {
'use strict';
angular.module('able')
.directive('cartElement', cartDirective)
.controller('promotedController', promotedController)
function promotedController($scope, $rootScope) {
$scope.promoted.putOne = putOne
$scope.promoted.takeOne = takeOne
$scope.promoted.quantity = 0
function putOne() {
if ($scope.promoted.quantity < $scope.promoted.object.max) {
$scope.promoted.quantity += 1
$rootScope.updateCart()
}
}
function takeOne() {
if ($scope.promoted.quantity > 0) {
$scope.promoted.quantity -= 1
$rootScope.updateCart()
}
}
}
function cartDirective() {
var directive = {
restrict: 'A',
controller: cartController,
controllerAs: 'cart',
bindToController: true
}
return directive
}
function cartController($scope, $rootScope, config, auth, $http, $localStorage, $state) {
var vm = this;
vm.send = send;
vm.quantity
vm.freight
vm.products
vm.estimated_time
vm.total
vm.discount
vm.voucher
function updateCart() {
if ($rootScope.offer && $rootScope.offer.object) {
var promoteds = $rootScope.offer.object.promoteds
var quantity = 0
var products = 0
for (var key in promoteds) {
quantity += promoteds[key].quantity
products += promoteds[key].object.price * promoteds[key].quantity
}
$rootScope.products_value = products
$rootScope.updateFreight()
var freight = $rootScope.freight
var total = products + freight
//apply selected voucher
var initial_total = total
if ($rootScope.voucher && $rootScope.voucher.object) {
var voucher = $rootScope.voucher
if (!voucher.object.burns.burnt) {
if (voucher.object.value_available > 0) {
total -= voucher.object.value_available
} else if (voucher.object.rate > 0) {
products -= products * voucher.object.rate
total = products + freight
}
if (total < 0) {
total = 0
}
if (initial_total != total) {
vm.voucher = voucher.path
}
}
}
vm.quantity = +(quantity).toFixed(2)
vm.freight = +(freight).toFixed(2)
vm.products = +(products).toFixed(2)
vm.total = +(total).toFixed(2)
vm.discount = +(initial_total - total).toFixed(2)
vm.estimated = $rootScope.estimated
}
}
$rootScope.updateCart = updateCart
$scope.$watch(function w(scope) {
return ($rootScope.place)
}, function c(n, o) {
vm.place = $rootScope.place
vm.complement = $rootScope.complement
});
$scope.$watch(function w(scope) {
return ($rootScope.estimated)
}, function c(n, o) {
updateCart()
});
$scope.$watch(function w(scope) {
return ($rootScope.voucher)
}, function c(n, o) {
updateCart()
});
$scope.$watch(function w(scope) {
return ($rootScope.plastic)
}, function c(n, o) {
vm.plastic = $rootScope.plastic
});
// if there is no active plastic, load all and pick the first.
if (!$rootScope.plastic) {
var req_config = {
headers: {
'Authorization': auth.token
}
};
$http.get(config.api + '/users/' + auth.id + '/plastics', req_config).then(function successCallback(response) {
var plastics = response.data.resources;
if (plastics && plastics[0]) {
$localStorage.plastic = plastics[0]
$rootScope.plastic = plastics[0]
vm.plastic = plastics[0]
}
}, function errorCallback(response) {
if (response.status == 401) {
auth.signout()
return
}
})
}
//if vouchers are not loaded, get all.
function getVouchers(force) {
if (!$rootScope.vouchers || force) {
var req_config = {
headers: {
'Authorization': auth.token
}
}
$http.get(config.api + '/users/' + auth.id + '/vouchers', req_config).then(function successCallback(response) {
$rootScope.vouchers = response.data.resources
$rootScope.voucher = $localStorage.voucher
}, function errorCallback(response) {
if (response.status == 401) {
auth.signout()
return
}
})
}
}
function send() {
vm.sending = true
var payload = {}
payload.items_selected = []
var promoteds = $rootScope.offer.object.promoteds
for (var key in promoteds) {
if (promoteds[key].quantity > 0) {
payload.items_selected.push({
promoted: promoteds[key].path,
quantity: promoteds[key].quantity,
})
}
}
payload.latitude = $rootScope.latitude
payload.longitude = $rootScope.longitude
payload.total_freight = vm.freight
payload.total_items = vm.products
payload.total_quantity = vm.quantity
payload.total_value = vm.total
payload.plastic = $rootScope.plastic.path
payload.place = JSON.stringify($rootScope.place)
payload.offer = $rootScope.offer.path
payload.voucher = vm.voucher
var req_config = {
headers: {
'Authorization': auth.token
}
};
$http.post(config.api + '/users/' + auth.id + '/orders', payload, req_config).then(function successCallback(response) {
console.log(response)
empty()
delete $rootScope.voucher
vm.voucher = ''
$rootScope.voucher = ''
$rootScope.last = response.data;
$rootScope.updateOfferStocks($rootScope.offer.path)
vm.sending = false
$state.go('storePage.confirmationPage');
}, function errorCallback(response) {
console.log(response)
vm.sending = false
if (response.status == 401 || response.status == 404) {
if (navigator && navigator.notification) {
navigator.notification.alert('Você precisa se logar novamente.', false, 'Able', 'Ok')
} else {
window.alert('Você precisa se logar novamente.')
}
auth.signout()
return
}
if (response.status == 403) {
switch (response.data.errors[0].reference) {
case 'offer':
delete $rootScope.offer
delete $localStorage.offer
if (navigator && navigator.notification) {
navigator.notification.alert(response.data.errors[0].error, refreshPage, 'Able', 'Ok')
} else {
window.alert(response.data.errors[0].error)
refreshPage()
}
break
case 'quantity':
if (navigator && navigator.notification) {
navigator.notification.alert(response.data.errors[0].error, null, 'Able', 'Ok')
} else {
window.alert(response.data.errors[0].error)
}
$rootScope.updateOfferStocks($rootScope.offer.path)
break
default:
if (navigator && navigator.notification) {
navigator.notification.alert(response.data.errors[0].error, null, 'Able', 'Ok')
} else {
window.alert(response.data.errors[0].error)
}
return
}
return
}
if (navigator && navigator.notification) {
navigator.notification.alert('Verifique sua conexão.', false, 'Able', 'Ok')
} else {
window.alert('Verifique sua conexão.')
}
})
} // end sendCart
function empty() {
var promoteds = $rootScope.offer.object.promoteds
for (var key in promoteds) {
promoteds[key].quantity = 0
}
updateCart()
}
} //end controller
})();