-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.sql
212 lines (171 loc) · 6.65 KB
/
test.sql
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
set search_path TO inventory, public;
select unit_of_measure_id as item_uom
from unit_of_measure
where name = 'item' \gset
select unit_of_measure_id as yards_uom
from unit_of_measure
where name = 'yards' \gset
\echo Received 100 THING1 @ $10/unit and 25 WIDGET2 @ $10/unit
insert into journal default values returning entry_id \gset
insert into posting (entry_id, account_id, sku, quantity, measure, unit_of_measure_id, unit_cost) values
(:entry_id, 1, 'THING1', -100, 1, :item_uom, 10),
(:entry_id, 2, 'THING1', 100, 1, :item_uom, null),
(:entry_id, 7, 'WIDGET2', -25, 40, :yards_uom, 25),
(:entry_id, 8, 'WIDGET2', 25, 40, :yards_uom, null);
\echo Averae Costs
select account_id, quantity, average_cost
from account_average_cost
join account using (account_id)
order by account_id;
\echo FIFO Cost
select account_id, entry_id, quantity, measure, unit_cost
from account_fifo_cost
order by account_id, entry_id;
\echo Received 100 THING1 @ $15/unit
insert into journal default values returning entry_id \gset
insert into posting (entry_id, account_id, sku, quantity, measure, unit_of_measure_id, unit_cost) values
(:entry_id, 1, 'THING1', -100, 1, :item_uom, 15),
(:entry_id, 2, 'THING1', 100, 1, :item_uom, null);
\echo Averae Costs
select account_id, quantity, average_cost
from account_average_cost
join account using (account_id)
order by account_id;
\echo FIFO Cost
select account_id, entry_id, quantity, measure, unit_cost
from account_fifo_cost
order by account_id, entry_id;
\echo Stocked 50 THING1s
insert into journal default values returning entry_id \gset
insert into posting (entry_id, account_id, sku, quantity, measure, unit_of_measure_id) values
(:entry_id, 2, 'THING1', -50, 1, :item_uom),
(:entry_id, 3, 'THING1', 50, 1, :item_uom);
\echo Averae Costs
select account_id, quantity, average_cost
from account_average_cost
join account using (account_id)
order by account_id;
\echo FIFO Cost
select account_id, entry_id, quantity, measure, unit_cost
from account_fifo_cost
order by account_id, entry_id;
\echo Receive 100 THING1 @ $13/unit
insert into journal default values returning entry_id \gset
insert into posting (entry_id, account_id, sku, quantity, measure, unit_of_measure_id, unit_cost) values
(:entry_id, 1, 'THING1', -100, 1, :item_uom, 13),
(:entry_id, 2, 'THING1', 100, 1, :item_uom, null);
\echo Averae Costs
select account_id, quantity, average_cost
from account_average_cost
join account using (account_id)
order by account_id;
\echo FIFO Cost
select account_id, entry_id, quantity, measure, unit_cost
from account_fifo_cost
order by account_id, entry_id;
\echo Stocked another 50 THING1s
insert into journal default values returning entry_id \gset
insert into posting (entry_id, account_id, sku, quantity, measure, unit_of_measure_id) values
(:entry_id, 2, 'THING1', -50, 1, :item_uom),
(:entry_id, 3, 'THING1', 50, 1, :item_uom);
\echo Averae Costs
select account_id, quantity, average_cost
from account_average_cost
join account using (account_id)
order by account_id;
\echo FIFO Cost
select account_id, entry_id, quantity, measure, unit_cost
from account_fifo_cost
order by account_id, entry_id;
\echo Commited 12 THING1 to fulfilling an order
insert into journal default values returning entry_id \gset
insert into posting (entry_id, account_id, sku, quantity, measure, unit_of_measure_id) values
(:entry_id, 3, 'THING1', -12, 1, :item_uom),
(:entry_id, 4, 'THING1', 12, 1, :item_uom);
\echo Averae Costs
select account_id, quantity, average_cost
from account_average_cost
join account using (account_id)
order by account_id;
\echo FIFO Cost
select account_id, entry_id, quantity, measure, unit_cost
from account_fifo_cost
order by account_id, entry_id;
\echo Picked 2 THING1 into a shipping container
insert into journal default values returning entry_id \gset
insert into posting (entry_id, account_id, sku, quantity, measure, unit_of_measure_id) values
(:entry_id, 4, 'THING1', -2, 1, :item_uom),
(:entry_id, 5, 'THING1', 2, 1, :item_uom);
\echo Averae Costs
select account_id, quantity, average_cost
from account_average_cost
join account using (account_id)
order by account_id;
\echo FIFO Cost
select account_id, entry_id, quantity, measure, unit_cost
from account_fifo_cost
order by account_id, entry_id;
\echo Shipped those 2 widgets
insert into journal default values returning entry_id \gset
insert into posting (entry_id, account_id, sku, quantity, measure, unit_of_measure_id) values
(:entry_id, 5, 'THING1', -2, 1, :item_uom),
(:entry_id, 6, 'THING1', 2, 1, :item_uom);
\echo Averae Costs
select account_id, quantity, average_cost
from account_average_cost
join account using (account_id)
order by account_id;
\echo FIFO Cost
select account_id, entry_id, quantity, measure, unit_cost
from account_fifo_cost
order by account_id, entry_id;
\echo Commit another 12 THING1 to fulfilling another order
\echo This posting is not balance and will error
\set old_error_stop :ON_ERROR_STOP
\set ON_ERROR_STOP 0
insert into journal default values returning entry_id \gset
insert into posting (entry_id, account_id, sku, quantity, measure, unit_of_measure_id) values
(:entry_id, 3, 'THING1', -12, 1, :item_uom),
(:entry_id, 4, 'THING1', 10, 1, :item_uom);
\set ON_ERROR_STOP :old_error_stop
\echo Averae Costs
select account_id, quantity, average_cost
from account_average_cost
join account using (account_id)
order by account_id;
\echo FIFO Cost
select account_id, entry_id, quantity, measure, unit_cost
from account_fifo_cost
order by account_id, entry_id;
\echo Moving WIDGET2 [email protected] from 8 to 9
insert into journal default values returning entry_id \gset
insert into posting (entry_id, account_id, sku, quantity, measure, unit_of_measure_id) values
(:entry_id, 8, 'WIDGET2', -5, 1.5, :yards_uom),
(:entry_id, 9, 'WIDGET2', 5, 1.5, :yards_uom);
\echo Averae Costs
select account_id, quantity, average_cost
from account_average_cost
join account using (account_id)
order by account_id;
\echo FIFO Cost
select account_id, entry_id, quantity, measure, unit_cost
from account_fifo_cost
order by account_id, entry_id;
\set old_error_stop :ON_ERROR_STOP
\set ON_ERROR_STOP 0
\echo Moving WIDGET2 25@40yds from 8 to 10
\echo This will error because we do not have enough
insert into journal default values returning entry_id \gset
insert into posting (entry_id, account_id, sku, quantity, measure, unit_of_measure_id) values
(:entry_id, 8, 'WIDGET2', -25, 40, :yards_uom),
(:entry_id, 10, 'WIDGET2', 25, 40, :yards_uom);
\set ON_ERROR_STOP :old_error_stop
\echo Averae Costs
select account_id, quantity, average_cost
from account_average_cost
join account using (account_id)
order by account_id;
\echo FIFO Cost
select account_id, entry_id, quantity, measure, unit_cost
from account_fifo_cost
order by account_id, entry_id;