forked from ledgersmb/LedgerSMB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
42-goods.pg
560 lines (467 loc) · 24.2 KB
/
42-goods.pg
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
BEGIN;
-- Load the TAP functions.
CREATE EXTENSION pgtap;
SET client_min_messages TO warning;
-- Plan the tests.
SELECT plan(58);
-- TABLE OF CONTENTS
-- 1. VALIDATE REQUIRED TABLES
-- 2. VALIDATE REQUIRED FUNCTIONS
-- 3. VERIFY 'INVENTORY_ADJUST__APPROVE'
-- a. verify adjustment report
-- b. verify on-hand numbers (TODO)
-- c. verify 'invoice' lines (TODO)
-- 4. VERIFY 'INVENTORY_ADJUST__LIST'
-- 5. VERIFY 'ASSEMBLY__STOCK'
-- a. stocking lacks inputs
-- b. sufficient input stock
-- c. repeated stocking
-- d. updated on-hand numbers
-- 6. VERIFY 'INVENTORY_ACTIVITY'
-- a. Bought parts
-- b. Sold parts
-- c. Stocking assemblies
-- Add data
\i xt/data/42-pg/Base.sql
--
-- Validate required tables
--
SELECT has_table('ar');
SELECT has_table('ap');
SELECT has_table('gl');
SELECT has_table('account');
SELECT has_table('mfg_lot');
SELECT has_table('mfg_lot_item');
SELECT has_table('invoice');
SELECT has_table('parts');
--
-- Validate required functions
--
SELECT has_function('part__get_by_id',ARRAY['integer']);
SELECT has_function('mfg_lot__commit',ARRAY['integer']);
SELECT has_function('assembly__stock',ARRAY['integer','numeric']);
SELECT has_function('goods__search',
ARRAY['text','text','text','integer','text','text',
'text','text','text','text','date','date']);
SELECT has_function('partsgroup__search',ARRAY['text']);
SELECT has_function('inventory__activity',ARRAY['date','date','text','text']);
SELECT has_function('inventory__search_part',ARRAY['integer','text','date']);
SELECT has_function('inventory_adjust__save_line',
ARRAY['integer','integer','numeric',
'numeric','numeric']);
SELECT has_function('inventory_adjust__save_info',ARRAY['date','text']);
SELECT has_function('inventory_adjust__approve',ARRAY['integer']);
SELECT has_function('inventory_adjust__delete',ARRAY['integer']);
SELECT has_function('inventory_adjust__list',ARRAY['date','date','boolean']);
SELECT has_function('inventory_adjust__get',ARRAY['integer']);
SELECT has_function('inventory_adjust__get_lines',ARRAY['integer']);
SELECT has_function('warehouse__list','{}'::text[]);
SELECT has_function('goods__history',
ARRAY['date','date','text','text','text','boolean',
'boolean','boolean','boolean','boolean',
'boolean']);
-- Set specific data
INSERT INTO account_heading (id, accno, description)
VALUES (-1000, '-1000', 'Test heading');
INSERT INTO account (id, accno, category, description, heading)
VALUES (-1011, 'TEST0001', 'A', 'GOODS test ar account', -1000),
(-1012, 'TEST0002', 'L', 'GOODS test ap account', -1000),
(-1101, 'TEST1001', 'A', 'GOODS test series 1 Inventory', -1000),
(-1102, 'TEST1002', 'E', 'GOODS test series 1 GOODS', -1000),
(-1103, 'TEST1003', 'E', 'GOODS test series 1 returns', -1000),
(-1104, 'TEST1004', 'I', 'GOODS test series 1 income', -1000),
(-2101, 'TEST2001', 'A', 'GOODS test series 2 Inventory', -1000),
(-2102, 'TEST2002', 'E', 'GOODS test series 1 GOODS', -1000),
(-2103, 'TEST2003', 'E', 'GOODS test series 1 returns', -1000),
(-2104, 'TEST2004', 'I', 'GOODS test series 1 income', -1000),
(-3101, 'TEST3002', 'A', 'GOODS test series 2 Inventory', -1000),
(-3102, 'TEST3001', 'E', 'GOODS test series 1 GOODS', -1000),
(-3103, 'TEST3003', 'E', 'GOODS test series 1 returns', -1000),
(-3104, 'TEST3004', 'I', 'GOODS test series 1 income', -1000),
(-4101, 'TEST4001', 'A', 'GOODS test series 2 Inventory', -1000),
(-4102, 'TEST4002', 'E', 'GOODS test series 1 GOODS', -1000),
(-4103, 'TEST4003', 'E', 'GOODS test series 1 returns', -1000),
(-4104, 'TEST4004', 'I', 'GOODS test series 1 income', -1000);
INSERT INTO parts
(id, partnumber, description, inventory_accno_id, expense_accno_id,
income_accno_id, returns_accno_id, assembly, lastcost)
VALUES
(-1, 'TS1', 'GOODS Test Series 1', -1101, -1102, -1104, -1103, false, 1.5),
(-2, 'TS2', 'GOODS Test Series 2', -2101, -2102, -2104, -2103, false, 2),
(-3, 'TS3', 'GOODS Test Series 3', -3101, -3102, -3104, -3103, false, 3),
(-4, 'TS4', 'GOODS Test Series 4', -4101, -4102, -4104, -4103, false, 4),
(-5, 'TS5', 'GOODS Test Series 5', -2101, -2102, -2104, -2103, false, 5),
(-6, 'AS1', 'GOODS Test Series 6', -3101, -3102, -3104, -3103, true, 6),
(-7, 'AS1a', 'GOODS Test Series 6',-2101, -2102, -2104, -2103, false, 7),
(-8, 'AS1b', 'GOODS Test Series 6',-2101, -2102, -2104, -2103, false, 8),
(-9, 'AS2', 'GOODS Test Series 6', -3101, -3102, -3104, -3103, true, 9),
(-10, 'AS2a', 'GOODS Test Series 6', -3101, -3102, -3104, -3103, true, 10);
INSERT INTO assembly (id, parts_id, qty)
VALUES (-6, -7, 3),
(-6, -8, 5),
(-9, -10, 7);
INSERT INTO entity (id, name, country_id, entity_class)
VALUES (-1000, 'Test act', 232, 1);
INSERT INTO entity_credit_account
(id, entity_class, meta_number, entity_id, curr, ar_ap_account_id)
VALUES (-1000, 1, 'cogs test1', -1000, 'XTS', -1103),
(-2000, 2, 'cogs test2', -1000, 'XTS', -1103);
UPDATE parts
SET onhand = 90
WHERE id = -1;
UPDATE parts
SET onhand = 80
WHERE id = -2;
--
-- Verify 'inventory_adjust__approve'
--
INSERT INTO ap (id, invoice, invnumber, transdate, entity_credit_account, approved)
VALUES (-1102, true, 'test1002', now() - '10 days'::interval, -1000, 't');
INSERT INTO invoice (id, trans_id, parts_id, qty, allocated, sellprice)
VALUES (-1102, -1102, -1, -150, 50, 0.5);
INSERT INTO ap (id, invoice, invnumber, transdate, entity_credit_account, approved)
VALUES (-1103, true, 'test1003', now() - '10 days'::interval, -1000, 't');
INSERT INTO invoice (id, trans_id, parts_id, qty, allocated, sellprice)
VALUES (-1103, -1103, -2, -150, 75, 9);
INSERT INTO inventory_report (transdate) VALUES (now()::date);
-- LedgerSMB::Inventory::Adjust_Line defines variance as:
-- counted - expected
SELECT inventory_adjust__save_line(currval('inventory_report_id_seq')::int,
-1, 100, 90, 10);
SELECT inventory_adjust__save_line(currval('inventory_report_id_seq')::int,
-2, 75, 80, -5);
SELECT inventory_adjust__approve(currval('inventory_report_id_seq')::int);
PREPARE test AS SELECT * FROM inventory_report_line
WHERE adjust_id = currval('inventory_report_id_seq');
SELECT results_eq('test', $$VALUES(currval('inventory_report_id_seq')::int,
-1::int, 100::numeric, 90::numeric,
10::numeric),
(currval('inventory_report_id_seq')::int,
-2::int, 75::numeric, 80::numeric,
-5::numeric)$$,
'inventory adjustment, save adjustment lines');
DEALLOCATE test;
PREPARE test AS SELECT transdate, trans_id
FROM inventory_report
WHERE id = currval('inventory_report_id_seq');
SELECT results_eq('test', $$VALUES(now()::date, currval('id')::int)$$,
'inventory adjustment, adjustment transactions');
DEALLOCATE test;
SELECT is(
ARRAY(SELECT count(*)
FROM acc_trans
WHERE trans_id = currval('id')),
ARRAY[4]::bigint[],
'There are 4 lines in acc_trans (correcting the inventory account)'
);
SELECT is(
(SELECT ARRAY[count(*), sum(amount_bc)]
FROM acc_trans
WHERE trans_id = currval('id') AND chart_id = -1101),
ARRAY[1, -5]::numeric[],
'There is 1 line on "series 1" inventory'
);
SELECT is(
(SELECT ARRAY[count(*), sum(amount_bc)]
FROM acc_trans
WHERE trans_id = currval('id') AND chart_id = -1102),
ARRAY[1, 5]::numeric[],
'There is 1 line on "series 1" COGS'
);
SELECT is(
(SELECT ARRAY[count(*), sum(amount_bc)]
FROM acc_trans
WHERE trans_id = currval('id') AND chart_id = -2101),
ARRAY[1, 45]::numeric[],
'There is 1 line on "series 2" inventory'
);
SELECT is(
(SELECT ARRAY[count(*), sum(amount_bc)]
FROM acc_trans
WHERE trans_id = currval('id') AND chart_id = -2102),
ARRAY[1, -45]::numeric[],
'There is 1 line on "series 2" COGS'
);
PREPARE test AS SELECT onhand FROM parts
WHERE id IN (-1, -2) ORDER BY id DESC;
SELECT results_eq('test', ARRAY[100::numeric, 75::numeric],
'inventory adjustment, approval updates on-hand numbers');
DEALLOCATE test;
--
-- Verify 'inventory_adjust__list'
--
-- Above, we created a report with adjustments both ways.
-- Create one which only goes one way (leaving out the ar or ap transaction)
INSERT INTO inventory_report (transdate) VALUES (now()::date);
SELECT inventory_adjust__save_line(currval('inventory_report_id_seq')::int,
-1, 100, 90, 10);
SELECT inventory_adjust__approve(currval('inventory_report_id_seq')::int);
PREPARE test AS SELECT count(*)::int
FROM inventory_adjust__list(now()::date,now()::date, null);
SELECT results_eq('test', ARRAY[2::int],
'inventory adjustment listing, with dates');
DEALLOCATE test;
PREPARE test AS SELECT count(*)::int
FROM inventory_adjust__list(now()::date,now()::date, true);
SELECT results_eq('test', ARRAY[2::int],
'inventory adjustment listing, with dates and approval');
DEALLOCATE test;
--
-- Verify 'assembly_stock'
--
PREPARE test AS SELECT assembly__stock(-6, 1);
SELECT throws_matching('test','Not enough parts in stock',
'stock assembly without stock');
DEALLOCATE test;
INSERT INTO ap (id, invoice, invnumber, transdate,
entity_credit_account, approved)
VALUES (-7101, true, 'test1001', now() - '17 days'::interval,
-1000, 't');
INSERT INTO invoice (id, trans_id, parts_id, qty, allocated, sellprice)
VALUES (-7101, -7101, -7, -100, 0, -3);
INSERT INTO invoice (id, trans_id, parts_id, qty, allocated, sellprice)
VALUES (-7102, -7101, -8, -100, 0, -1);
UPDATE parts
SET onhand = 100
WHERE id IN (-7, -8);
PREPARE test AS SELECT assembly__stock(-6, 1);
SELECT results_eq('test', ARRAY[1::numeric],
'stock assembly with stock');
SELECT results_eq('test', ARRAY[1::numeric],
'stock assembly repeated stocking');
DEALLOCATE test;
PREPARE test AS SELECT onhand FROM parts
WHERE id IN (-6, -7, -8) ORDER BY id DESC;
SELECT results_eq('test', ARRAY[2, 94, 90]::numeric[],
'stocking assemblies updates on hand numbers');
DEALLOCATE test;
--
-- Verify 'inventory__activity'
--
--- First test: bought parts appear on the inventory report
---- Simulate bought parts: insert 'ap' and 'invoice' lines
INSERT INTO ap (id, invoice, invnumber, transdate,
entity_credit_account, approved)
VALUES (-1101, true, 'test1001', now() - '10 days'::interval,
-1000, 't');
INSERT INTO invoice (id, trans_id, parts_id, qty, allocated, sellprice)
VALUES (-1101, -1101, -3, -100, 0, 3);
-- test interval no bounds
PREPARE test AS SELECT *
FROM inventory__activity(NULL, NULL, 'TS3', NULL);
SELECT results_eq('test', $$VALUES(-3::int, 'GOODS Test Series 3', 'TS3',
0::numeric, 0::numeric, 100::numeric,
300::numeric,
0::numeric, 0::numeric,
0::numeric)$$,
'ap inventory activity TS3, no interval');
DEALLOCATE test;
-- test interval lower bound
PREPARE test AS SELECT *
FROM inventory__activity((now() - '11 days'::interval)::date,
NULL, 'TS3', NULL);
SELECT results_eq('test', $$VALUES(-3, 'GOODS Test Series 3', 'TS3',
0::numeric, 0::numeric, 100::numeric,
300::numeric,
0::numeric, 0::numeric,
0::numeric)$$,
'ap inventory activity TS3, lower bound interval below');
DEALLOCATE test;
PREPARE test AS SELECT (count(*) = 0)
FROM inventory__activity((now() - '9 days'::interval)::date,
NULL, 'TS3', NULL);
SELECT results_eq('test', ARRAY[true],
'ap inventory activity TS3, lower bound interval above');
DEALLOCATE test;
-- test interval upper bound
PREPARE test AS SELECT *
FROM inventory__activity(NULL,
(now() - '9 days'::interval)::date,
'TS3', NULL);
SELECT results_eq('test', $$VALUES(-3, 'GOODS Test Series 3', 'TS3',
0::numeric, 0::numeric, 100::numeric,
300::numeric,
0::numeric, 0::numeric,
0::numeric)$$,
'ap inventory activity TS3, upper bound interval above');
DEALLOCATE test;
PREPARE test AS SELECT (count(*) = 0)
FROM inventory__activity(NULL,
(now() - '11 days'::interval)::date,
'TS3', NULL);
SELECT results_eq('test', ARRAY[true],
'ap inventory activity TS3, upper bound interval below');
DEALLOCATE test;
-- test interval upper and lower bound
PREPARE test AS SELECT *
FROM inventory__activity((now() - '11 days'::interval)::date,
(now() - '9 days'::interval)::date,
'TS3', NULL);
SELECT results_eq('test', $$VALUES(-3, 'GOODS Test Series 3', 'TS3',
0::numeric, 0::numeric, 100::numeric,
300::numeric,
0::numeric, 0::numeric,
0::numeric)$$,
'ap inventory activity TS3, interval enclosing');
DEALLOCATE test;
PREPARE test AS SELECT *
FROM inventory__activity((now() - '10 days'::interval)::date,
(now() - '10 days'::interval)::date,
'TS3', NULL);
SELECT results_eq('test', $$VALUES(-3, 'GOODS Test Series 3', 'TS3',
0::numeric, 0::numeric, 100::numeric,
300::numeric,
0::numeric, 0::numeric,
0::numeric)$$,
'ap inventory activity TS3, interval single day');
DEALLOCATE test;
PREPARE test AS SELECT (count(*) = 0)
FROM inventory__activity((now() - '11 days'::interval)::date,
(now() - '11 days'::interval)::date,
'TS3', NULL);
SELECT results_eq('test', ARRAY[true],
'ap inventory activity TS3, interval upper bound below');
DEALLOCATE test;
PREPARE test AS SELECT (count(*) = 0)
FROM inventory__activity((now() - '9 days'::interval)::date,
(now() - '9 days'::interval)::date,
'TS3', NULL);
SELECT results_eq('test', ARRAY[true],
'ap inventory activity TS3, interval lower bound above');
DEALLOCATE test;
--- Second test: sold parts appear on the inventory report
---- Simulate sold parts: insert 'ar' and 'invoice' lines
INSERT INTO ar (id, invoice, invnumber, transdate,
entity_credit_account, approved)
VALUES (-1201, true, 'test1001', now() - '10 days'::interval,
-2000, 't');
INSERT INTO invoice (id, trans_id, parts_id, qty, allocated, sellprice)
VALUES (-1201, -1201, -4, 100, 0, -3);
-- test interval no bounds
PREPARE test AS SELECT *
FROM inventory__activity(NULL, NULL, 'TS4', NULL);
SELECT results_eq('test', $$VALUES(-4::int, 'GOODS Test Series 4', 'TS4',
100::numeric, -300::numeric,
0::numeric, 0::numeric,
0::numeric, 0::numeric,
0::numeric)$$,
'ar inventory activity TS4, no interval');
DEALLOCATE test;
-- test interval lower bound
PREPARE test AS SELECT *
FROM inventory__activity((now() - '11 days'::interval)::date,
NULL, 'TS4', NULL);
SELECT results_eq('test', $$VALUES(-4, 'GOODS Test Series 4', 'TS4',
100::numeric, -300::numeric,
0::numeric, 0::numeric,
0::numeric, 0::numeric,
0::numeric)$$,
'ar inventory activity TS4, lower bound interval below');
DEALLOCATE test;
PREPARE test AS SELECT (count(*) = 0)
FROM inventory__activity((now() - '9 days'::interval)::date,
NULL, 'TS4', NULL);
SELECT results_eq('test', ARRAY[true],
'ar inventory activity TS4, lower bound interval above');
DEALLOCATE test;
-- test interval upper bound
PREPARE test AS SELECT *
FROM inventory__activity(NULL,
(now() - '9 days'::interval)::date,
'TS4', NULL);
SELECT results_eq('test', $$VALUES(-4, 'GOODS Test Series 4', 'TS4',
100::numeric, -300::numeric,
0::numeric, 0::numeric,
0::numeric, 0::numeric,
0::numeric)$$,
'ar inventory activity TS4, upper bound interval above');
DEALLOCATE test;
PREPARE test AS SELECT (count(*) = 0)
FROM inventory__activity(NULL,
(now() - '11 days'::interval)::date,
'TS4', NULL);
SELECT results_eq('test', ARRAY[true],
'ar inventory activity TS4, upper bound interval below');
DEALLOCATE test;
-- test interval upper and lower bound
PREPARE test AS SELECT *
FROM inventory__activity((now() - '11 days'::interval)::date,
(now() - '9 days'::interval)::date,
'TS4', NULL);
SELECT results_eq('test', $$VALUES(-4, 'GOODS Test Series 4', 'TS4',
100::numeric, -300::numeric,
0::numeric, 0::numeric,
0::numeric, 0::numeric,
0::numeric)$$,
'ar inventory activity TS2, interval enclosing');
DEALLOCATE test;
PREPARE test AS SELECT *
FROM inventory__activity((now() - '10 days'::interval)::date,
(now() - '10 days'::interval)::date,
'TS4', NULL);
SELECT results_eq('test', $$VALUES(-4, 'GOODS Test Series 4', 'TS4',
100::numeric, -300::numeric,
0::numeric, 0::numeric,
0::numeric, 0::numeric,
0::numeric)$$,
'ar inventory activity TS4, interval single day');
DEALLOCATE test;
PREPARE test AS SELECT (count(*) = 0)
FROM inventory__activity((now() - '11 days'::interval)::date,
(now() - '11 days'::interval)::date,
'TS4', NULL);
SELECT results_eq('test', ARRAY[true],
'ar inventory activity TS4, interval upper bound below');
DEALLOCATE test;
PREPARE test AS SELECT (count(*) = 0)
FROM inventory__activity((now() - '9 days'::interval)::date,
(now() - '9 days'::interval)::date,
'TS4', NULL);
SELECT results_eq('test', ARRAY[true],
'ar inventory activity TS4, interval lower bound above');
DEALLOCATE test;
--- Third test: stocked items should be on the inventory report
INSERT INTO ap (id, invoice, invnumber, transdate,
entity_credit_account, approved)
VALUES (-8101, true, 'test1001', now() - '17 days'::interval,
-1000, 't');
INSERT INTO invoice (id, trans_id, parts_id, qty, allocated, sellprice)
VALUES (-8101, -8101, -10, -100, 0, -10);
SELECT assembly__stock(-9, 3);
PREPARE test AS SELECT *
FROM inventory__activity(now()::date, now()::date,
NULL, NULL)
WHERE partnumber in ('AS2', 'AS2a')
ORDER BY partnumber;
SELECT results_eq('test', $$VALUES(-9, 'GOODS Test Series 6', 'AS2',
0::numeric, 0::numeric,
0::numeric, 0::numeric,
0::numeric, 3::numeric,
0::numeric),
(-10, 'GOODS Test Series 6', 'AS2a',
0::numeric, 0::numeric,
0::numeric, 0::numeric,
21::numeric, 0::numeric,
0::numeric)$$,
'assembly inventory activity AS2, assembled part');
DEALLOCATE test;
--- Fourth test: adjustments should be on the activity report
-- Re-use data from 'inventory_adjust__approve' test
PREPARE test AS SELECT *
FROM inventory__activity(now()::date, now()::date,
NULL, NULL)
WHERE partnumber IN ('TS1', 'TS2')
ORDER BY partnumber;
SELECT results_eq('test', $$VALUES(-1, 'GOODS Test Series 1', 'TS1',
0::numeric, 0::numeric,
0::numeric, 0::numeric,
0::numeric, 0::numeric,
20::numeric),
(-2, 'GOODS Test Series 2', 'TS2',
0::numeric, 0::numeric,
0::numeric, 0::numeric,
0::numeric, 0::numeric,
-5::numeric)$$,
'adjustment inventory activity TS1, TS2');
DEALLOCATE test;
ROLLBACK;