File: TradingData.m

package info (click to toggle)
gnustep-dl2 0.12.0-9%2Bnmu1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,232 kB
  • sloc: objc: 61,721; makefile: 82; sh: 22
file content (312 lines) | stat: -rw-r--r-- 8,441 bytes parent folder | download | duplicates (5)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "TradingData.h"

#define PDEC2(x) \
[NSDecimalNumber decimalNumberWithMantissa:x \
exponent:-2 \
isNegative:NO]

@implementation TradingData
- (id) init
{

  self = [super init];

  if (self)
  {
    ec = [[EOEditingContext alloc] init];

    customerDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
					 entityName:@"Customer"];
    customerGroupDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
					 entityName:@"CustomerGroup"];
    productGroupDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
					 entityName:@"ProductGroup"];
    productDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
					 entityName:@"Product"];
    suppliersDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
					 entityName:@"Supplier"];
    priceListDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
					 entityName:@"PriceList"];
    priceListPosDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
					 entityName:@"PriceListPos"];
    orderDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
					 entityName:@"Order"];
    orderPosDS = [[EODatabaseDataSource alloc] initWithEditingContext:ec
					 entityName:@"OrderPos"];
  }

  return self;
}

- (void) dealloc
{
  [ec release];
  [customerDS release];
  [customerGroupDS release];
  [productGroupDS release];
  [productDS release];
  [suppliersDS release];
  [priceListDS release];
  [priceListPosDS release];
  [orderDS release];
  [orderPosDS release];
}

- (void) fillTables 
{
  id fsfSup, mysqlSup, postgreSup, publicSup, acmeSup;
  id implGrp, softGrp;
  id folkGrp;
  id rake, shovel, pitchfork, mysql, postgresql, sqlite, gnustep, gcc;
  id salePriceList;
  id customer;
  id order;
 
  implGrp = [self addProductGroup:@"Implements of destruction"];
  softGrp = [self addProductGroup:@"Software"];

  fsfSup = [self addSupplierNamed:@"The Free Software Foundation"];
  gnustep = [self addProduct:@"GNUstep"
	 price:PDEC2(2595)
	 supplier:fsfSup
	 group:softGrp]; 
  gcc = [self addProduct:@"gcc"
	 price:PDEC2(4995)
	 supplier:fsfSup
	 group:softGrp]; 
  mysqlSup = [self addSupplierNamed:@"MySQL AB"];
  mysql = [self addProduct:@"MySQL"
	 price:PDEC2(3000)
	 supplier:fsfSup
	 group:softGrp]; 


  postgreSup = [self addSupplierNamed:@"The PostgreSQL Global Development Group"];
  postgresql = [self addProduct:@"PostgreSQL"
	 price:PDEC2(3000)
	 supplier:postgreSup
	 group:softGrp]; 


  publicSup = [self addSupplierNamed:@"sqlite.org"];
  sqlite = [self addProduct:@"SQLite"
	 price:PDEC2(1500)
	 supplier:publicSup
	 group:softGrp]; 


  acmeSup = [self addSupplierNamed:@"ACME"];
  shovel = [self addProduct:@"Shovel"
	price:PDEC2(1400)
	 supplier:acmeSup
	 group:implGrp]; 

  rake = [self addProduct:@"Rake"
	 price:PDEC2(1295)
	 supplier:acmeSup
	 group:implGrp]; 
  pitchfork = [self addProduct:@"Pitchfork"
	 price:PDEC2(1800)
	 supplier:acmeSup
	 group:implGrp]; 

  folkGrp = [self addCustomerGroup:@"Angry Townfolk"];

  [self customer:[self addCustomer:@"Angry Townsperson1" group:folkGrp]
		order:rake
		quantity:3];
  [self customer:[self addCustomer:@"Angry Townsperson2" group:folkGrp]
		order:shovel];
  
  [self customer:[self addCustomer:@"Angry Townsperson3" group:folkGrp]
		order:rake
		quantity:2];
  
  customer = [self addCustomer:@"Test Customer1"];
  order = [self createOrderForCustomer:customer];

  [self order:order product:mysql quantity:5];
  [self order:order product:postgresql quantity:1];
  [self order:order product:rake quantity:2];

  [self customer:[self addCustomer:@"Test Customer2"]
		order:postgresql];
  [self customer:[self addCustomer:@"Test Customer3"]
		order:sqlite];


  salePriceList = [self addPriceList:@"Farm Supply Sale"
				forGroup:folkGrp];
 
  [self addProduct:pitchfork
	price:PDEC2(1675)
	toPriceList:salePriceList];
  [self addProduct:rake
	price:PDEC2(800)
	toPriceList:salePriceList];
  [self addProduct:shovel
	price:PDEC2(995)
	toPriceList:salePriceList];

  [self saveChanges];
}

- (id) addPriceList:(NSString *)name
	forGroup:(id)grp
{
  id record = [priceListDS createObject];
  [record takeValue:name forKey:@"name"]; 
  [record addObject:grp toBothSidesOfRelationshipWithKey:@"customerGroup"]; 
  return record;
}

- (id) addProduct:(id)product
	price:(NSDecimalNumber *)price
	toPriceList:(id)priceList
{
  id record = [priceListPosDS createObject];

  [record addObject:product toBothSidesOfRelationshipWithKey:@"product"];
  [record takeValue:price forKey:@"price"];
  [record addObject:priceList toBothSidesOfRelationshipWithKey:@"priceList"];
  [priceListPosDS insertObject:record];
  return record;
}

- (id) addProductGroup:(NSString *)productGroupName
{
  id record = [productGroupDS createObject];
  
  [record takeValue:productGroupName forKey:@"name"];

  return record;     
}

- (id) addSupplierNamed:(NSString *)name
{
  id record = [suppliersDS createObject];

  [record takeValue:name forKey:@"name"];

  [suppliersDS insertObject:record];

  return record;
}

- (id) addProduct:(NSString *)productName
	price:(NSDecimalNumber *)price
	supplier:(id)supplier
	group:(id)productGroup
{
  id record = [productDS createObject];
 
  [record takeValue:productName forKey:@"name"];
  [record takeValue:price forKey:@"price"];
  [record addObject:supplier toBothSidesOfRelationshipWithKey:@"supplier"];
  [record addObject:productGroup toBothSidesOfRelationshipWithKey:@"productGroup"];

  [productDS insertObject:record];
  return record;
}
- (id) addCustomerGroup:(NSString *)name
{
  id record = [customerGroupDS createObject];

  [record takeValue:name forKey:@"name"];

  [customerGroupDS insertObject:record];
  return record;
}

- (id) addCustomer:(NSString *)name
{
  return [self addCustomer:name group:nil];
}

- (id) addCustomer:(NSString *)name
	group:(id)group
{
  id record = [customerDS createObject];

  [record takeValue:name forKey:@"name"];
  [record addObject:group toBothSidesOfRelationshipWithKey:@"customerGroup"]; 

  [customerDS insertObject:record];
  return record;
}

- (id) createOrderForCustomer:(id)customer
{
  id order = [orderDS createObject];
  [order takeValue:[NSDate date] forKey:@"date"];
  [order addObject:customer toBothSidesOfRelationshipWithKey:@"customer"];
  [orderDS insertObject:order];
  return order;
}

- (id) order:(id)order
	product:(id)product
	quantity:(int)quantity
{
  id orderPos = [orderPosDS createObject];
  NSNumber *price = [product valueForKey:@"price"]; 
  NSNumber *qNum = [NSNumber numberWithInt:quantity]; 
  /* this should really not be in the database but in some business logic */
  NSNumber *value = [NSNumber numberWithDouble:((double)quantity) * [price doubleValue]]; 
  [orderPos addObject:product toBothSidesOfRelationshipWithKey:@"product"];
  [orderPos addObject:order toBothSidesOfRelationshipWithKey:@"order"];
  /* FIXME customer group pricing */
  [orderPos takeValue:price forKey:@"price"];
  [orderPos takeValue:qNum forKey:@"quantity"];
  [orderPos takeValue:value forKey:@"value"];
 
  /* FIXME this should be a relationship to a pos table */
  [orderPos takeValue:[NSNumber numberWithInt:1] forKey:@"posnr"];
  return orderPos;
}

- (id) customer:(id)customer
	 order:(id)product
{
   return [self customer:customer order:product quantity:1]; 
}

- (id) customer:(id)customer
	order:(id)product
	quantity:(int)quantity
{
  return [self customer:customer order:product quantity:quantity posNr:1]; 
}

- (id) customer:(id)customer
	order:(id)product
	quantity:(int)quantity
	posNr:(int)posNr
{
  id order = [orderDS createObject];
  id orderPos = [orderPosDS createObject];
  NSNumber *price = [product valueForKey:@"price"]; 
  NSNumber *qNum = [NSNumber numberWithInt:quantity]; 
  NSNumber *value = [NSNumber numberWithDouble:((double)quantity) * [price doubleValue]]; 

  [order takeValue:[NSDate date] forKey:@"date"];
  [order addObject:customer toBothSidesOfRelationshipWithKey:@"customer"];

  [orderPos addObject:order toBothSidesOfRelationshipWithKey:@"order"];
  /* FIXME customer group pricing */
  [orderPos takeValue:price forKey:@"price"];
  [orderPos takeValue:qNum forKey:@"quantity"];
  [orderPos takeValue:value forKey:@"value"];
  /* fixme this should be a relationship to a pos table */
  [orderPos takeValue:[NSNumber numberWithInt:posNr] forKey:@"posnr"];
  [orderPos addObject:product toBothSidesOfRelationshipWithKey:@"product"];

  return order;
}

- (void) saveChanges
{
  [ec saveChanges];
}

@end