-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomerController.js
181 lines (154 loc) · 4.59 KB
/
customerController.js
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
customerApp.controller('customerController', function ($scope) {
var dateTypes = [
['birth','Birth Date'],
['init','First Purchase'],
['last','Most Recent Purchase']
];
var oPhoneTypeModel = new PhoneTypeModel();
// Make columns.
function makeColumns(){
$scope.columns = [
{
title: 'Number',
content: function(row) { return row.customerNumber; },
visible: true
},
{
title: 'Name',
content: function(row) { return row.customerName.formatted; },
visible: true,
sort: [
['customerName.surname','by surname A-Z'],
['-customerName.surname','by surname Z-A'],
['customerName.firstname','by first name A-Z'],
['-customerName.firstname','by first name Z-A']
]
},
{
title: 'Age',
content: function(row) {
now = (new Date()).getTime();
then = row.dates.birth.getTime();
oneYear = 1000*60*60*24*365.25;
return Math.floor((now-then)/oneYear);
},
visible: true,
sort: [
['-dates.birth','youngest to oldest'],
['dates.birth','oldest to youngest']
]
},
{
title: 'Address',
content: function(row) { return row.address; },
visible: false
}
];
for (var i in dateTypes) {
dateType = dateTypes[i];
slug = dateType[0];
title = dateType[1];
rowDateFunction = function(slug) { return function(row) {
o = row.dates[slug];
return o ? o.toDateString() : null;
}};
sortArray = function(slug) { return [
['-dates.'+slug,'most recent'],
['dates.'+slug,'least recent']
]};
$scope.columns.push({
title: title,
content: rowDateFunction(slug),
visible: false,
sort: sortArray(slug)
});
}
function makeColumnsFor1MDetail(code) {
for (var fieldSlug in code) {
field = code[fieldSlug];
$scope.columns.push({
title: field.data ? (field.data.title ? field.data.title : 'NO TITLE') : 'NO DATA',
content: field.content ? field.content : function() { return 'NO CONTENT'; },
visible: false
});
}
}
makeColumnsFor1MDetail(oPhoneTypeModel);
}
makeColumns();
$scope.init = function() {
$scope.data = [];
$scope.nameCount = 10;
$scope.namePattern = 'USA';
$scope.setSort('customerName.surname');
};
$scope.setSort = function(predicate) {
$scope.sortPredicate = predicate;
}
$scope.setNamePattern = function(slug) {
$scope.namePattern = slug;
}
o = new nameModel();
$scope.namePatternOptions = o.getPatternOptions();
$scope.makeCustomers = function() {
lAddressModel = new addressModel();
// Date constants.
dateGenerator = new RandomDateGenerator();
oMinBirthDate = new DateMath();
oMinBirthDate.addYears(-75);
oMaxBirthDate = new DateMath();
oMaxBirthDate.addYears(-18);
rank = Date.now();
for (i = 0; i < $scope.nameCount; i++) {
newCustomer = {
customerNumber: rank % 1000000,
address: lAddressModel.makeAddress(rank)
};
// Name
model = new nameModel();
model.setPatternSlug($scope.namePattern);
model.setRank(rank);
model.setGender((rank % $scope.nameCount) / $scope.nameCount);
newCustomer.customerName = model.makeName();
// Dates
newCustomer.dates = {};
// Date of birth: Make a broad range of age between 18 and 75.
dateGenerator.setMinimum(oMinBirthDate.getInternal());
dateGenerator.setMaximum(oMaxBirthDate.getInternal());
dateGenerator.setRandomFunction(function() {
return 1-Math.exp(Math.random()-1);
});
newCustomer.dates.birth = dateGenerator.makeDate();
// Date of first purchase: Some time between 18th birthday and now.
o18thBirthday = new DateMath();
o18thBirthday.setInternal(newCustomer.dates.birth);
o18thBirthday.addYears(18);
dateGenerator.setMinimum(o18thBirthday.getInternal());
dateGenerator.setMaximumNow();
newCustomer.dates.init = dateGenerator.makeDate();
// Date of last purchase: Some time between first purchase and now.
dateGenerator.setMinimum(newCustomer.dates.init);
dateGenerator.setMaximumNow();
newCustomer.dates.last = dateGenerator.makeDate();
// Phone numbers.
//TODO Add random factor.
oPhone = {};
for (var sType in oPhoneTypeModel) {
oType = oPhoneTypeModel[sType];
oPhoneNumberModel = new PhoneNumberModel();
oPhoneNumberModel.setRank(rank);
oPhoneNumberModel.setAreaCode('985');
if (Math.random() < oType.data.rate) {
oPhone[oType.data.slug] = oPhoneNumberModel.makePhoneNumber();
rank++;
}
}
newCustomer.phone = oPhone;
// Push customer and increment.
console.log('newCustomer',newCustomer);
$scope.data.push(newCustomer);
rank++;
}
};
$scope.init();
});