idata 0.1.33 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/full-pg-lawson.sh ADDED
@@ -0,0 +1,707 @@
1
+ #!/bin/bash
2
+
3
+ # SHARED VARIABLES
4
+ # ---------------------------------------------------------------------------------
5
+ # Database to store data tables
6
+ ORGNAME="tiff20140805"
7
+
8
+ # ENV variables used by the validation command
9
+ export HOST="localhost"
10
+ export USERNAME="postgres"
11
+ export PASSWORD="postgres"
12
+ export DATABASE="$ORGNAME"
13
+ export LISTEN=7432
14
+
15
+ # Create temporary output folder
16
+ OUTPUT_DIR="/tmp/$ORGNAME" && test -e $OUTPUT_DIR || mkdir $OUTPUT_DIR
17
+
18
+ # Input files and correspondings names
19
+ FCONTRACTO="ContractMaster.csv"
20
+ FVENDOR="VendorMaster.csv"
21
+ FITEM="ItemMaster.csv"
22
+ FINVOICE="InvoiceHistory.csv"
23
+ FMFR="MfrMaster.csv"
24
+ FPO="PurchaseOrder.csv"
25
+ FUSER="User.csv"
26
+ FLOCATION="Location.csv"
27
+ FITEMCOST="ItemCostCenterAcctExceptions.csv"
28
+ FREQ="ReqHistoryLoad.csv"
29
+ FGL="GLAccount.csv"
30
+ FINVENTORY="Inventory.csv"
31
+ FULPR="UserAndLocationProfile.csv"
32
+ FLPR="LocationProfile.csv"
33
+
34
+ CONTRACTO="contracts"
35
+ VENDOR="vendors"
36
+ ITEM="items"
37
+ INVOICE="invoices"
38
+ MFR="manufacturers"
39
+ PO="purchase_orders"
40
+ USER="users"
41
+ LOCATION="locations"
42
+ ITEMCOST="item_costs"
43
+ REQ="reqs"
44
+ GL="gls"
45
+ INVENTORY="inventory"
46
+ ULPR="user_location_profiles"
47
+ LPR="location_profiles"
48
+
49
+ # Standard UOM for reference
50
+ FUOMSTD="stduom.txt"
51
+ UOMSTD="uomstd"
52
+
53
+ # ---------------------------------------------------------------------------------
54
+ # LOAD
55
+ # ---------------------------------------------------------------------------------
56
+ # Sanitize input files
57
+ #isanitize --strip-newline --remove=65279 "$FCONTRACTO" "$FVENDOR" "$FITEM" "$FINVOICE" "$FMFR" "$FPO" \
58
+ # "$FTMPL" "$FUSER" "$FITEMEXPO" "$FUSEREQ" "$FLOCATION" "$FITEMCOST" "$FREQ" \
59
+ # "$FGL" "$FUSERCOST" "$FINVENTORY" "$FUSERCREATEREQ"
60
+
61
+ # Load
62
+ iload -i "$FCONTRACTO" -t "$CONTRACTO" -f csv
63
+ iload -i "$FVENDOR" -t "$VENDOR" -f csv
64
+ iload -i "$FITEM" -t "$ITEM" -f csv
65
+ #iload -i "$FINVOICE" -t "$INVOICE" -f csv
66
+ iload -i "$FMFR" -t "$MFR" -f csv
67
+ iload -i "$FPO" -t "$PO" -f csv
68
+ iload -i "$FUSER" -t "$USER" -f csv
69
+ iload -i "$FLOCATION" -t "$LOCATION" -f csv
70
+ iload -i "$FREQ" -t "$REQ" -f csv
71
+ iload -i "$FGL" -t "$GL" -f csv
72
+ iload -i "$FINVENTORY" -t "$INVENTORY" -f csv
73
+ iload -i "$FULPR" -t "$ULPR" -f csv
74
+ iload -i "$FLPR" -t "$LPR" -f csv
75
+
76
+ # Load UOM STANDARD for reference
77
+ iload -i "$FUOMSTD" -t "$UOMSTD" -f csv
78
+
79
+ # ---------------------------------------------------------------------------------
80
+ # INDEXING (for speeding up validation queries)
81
+ # ---------------------------------------------------------------------------------
82
+ ipatch -q "drop index if exists items_item_id_index; create index items_item_id_index on $ITEM(item_id)"
83
+ ipatch -q "drop index if exists pos_item_id_index; create index pos_item_id_index on $PO(item_id)"
84
+ ipatch -q "drop index if exists gls_corp_acct_no_index; create index gls_corp_acct_no_index on $GL(corp_acct_no)"
85
+ ipatch -q "drop index if exists gls_corp_acct_name_index; create index gls_corp_acct_name_index on $GL(corp_acct_name)"
86
+ ipatch -q "drop index if exists gls_cc_acct_no_index; create index gls_cc_acct_no_index on $GL(cc_acct_no)"
87
+ ipatch -q "drop index if exists gls_cc_acct_name_index; create index gls_cc_acct_name_index on $GL(cc_acct_name)"
88
+ ipatch -q "drop index if exists locations_name_index; create index locations_name_index on $LOCATION(name)"
89
+
90
+ # ---------------------------------------------------------------------------------
91
+ # Some adjustment
92
+ # ---------------------------------------------------------------------------------
93
+ ipatch -q "
94
+ -- Normalize values
95
+ UPDATE $ITEM set active = '1' where active = 'Y';
96
+ UPDATE items set active = '3' where active = 'N';
97
+ "
98
+
99
+ ipatch -q "
100
+ -- ADD EMAIL COLUMN
101
+ ALTER TABLE $USER ADD COLUMN email VARCHAR;
102
+ UPDATE $USER SET email = LOGIN_ID;
103
+ "
104
+
105
+ ipatch -q "
106
+ -- FIX ISSUE WITH linebreak
107
+ update items set vendor_item_id = (string_to_array(regexp_replace(replace(vendor_item_id, E'\r\n', ' '), '\s+', ' ', 'g'), ' '))[1]
108
+ where vendor_item_id like E'%\r\n%';
109
+ update purchase_orders set vendor_item_id = (string_to_array(regexp_replace(replace(vendor_item_id, E'\r\n', ' '), '\s+', ' ', 'g'), ' '))[1]
110
+ where vendor_item_id like E'%\r\n%';
111
+ update purchase_orders set vendor_item_id = (string_to_array(regexp_replace(replace(vendor_item_id, E'\n', ' '), '\s+', ' ', 'g'), ' '))[1]
112
+ where vendor_item_id like E'%\n%';
113
+ "
114
+
115
+ ipatch -q "
116
+ -- Normalize DATE
117
+ UPDATE $PO SET po_date = to_char(to_date(po_date, 'MM/DD/YYYY'), 'YYYY-MM-DD')
118
+ WHERE po_date IS NOT NULL AND po_date != '';
119
+ UPDATE $CONTRACTO SET contract_start = to_char(to_date(contract_start, 'MM/DD/YYYY'), 'YYYY-MM-DD')
120
+ WHERE contract_start like '%/%/%';
121
+ UPDATE $CONTRACTO SET contract_end = to_char(to_date(contract_end, 'MM/DD/YYYY'), 'YYYY-MM-DD')
122
+ WHERE contract_end like '%/%/%';
123
+
124
+ -- ADD MORE EMPTY FIELD (for validation)
125
+ -- ALTER TABLE $LOCATION ADD COLUMN corp_id varchar;
126
+
127
+ -- FILL EMPTY FIELDS WITH DEFAULT VALUE
128
+ UPDATE $LOCATION SET ship_to_ind = 'N' WHERE ship_to_ind IS NULL OR LENGTH(trim(ship_to_ind)) = 0;
129
+ UPDATE $LOCATION SET bill_to_ind = 'N' WHERE bill_to_ind IS NULL OR LENGTH(trim(bill_to_ind)) = 0;
130
+ UPDATE $LOCATION SET stockless_ind = 'N' WHERE stockless_ind IS NULL OR LENGTH(trim(stockless_ind)) = 0;
131
+ "
132
+
133
+ # Convert A2A2A2 to A2-A2-A2
134
+ # Manual check and comment out the following to speed up validating
135
+ # @todo IEVAL performance is very poor, avoid using it (use IPATCH instead) unless there is no better way
136
+ ipatch -q "
137
+ alter table $GL add column old_fq_acct_no varchar;
138
+ update $GL set old_fq_acct_no = fq_acct_no;
139
+ "
140
+
141
+ ieval -t $GL --eval="
142
+ if item.corp_acct_fmt && item.corp_acct_fmt[/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/]
143
+ item.corp_acct_fmt = item.corp_acct_fmt.unpack('a2a2a2').join('-')
144
+ item.fq_acct_no = item.fq_acct_no.unpack('a2a5a5').join('-')
145
+ end
146
+ "
147
+
148
+ # ---------------------------------------------------------------------------------
149
+ # VALIDATE
150
+ # ---------------------------------------------------------------------------------
151
+ # validate VENDORS
152
+ ivalidate --case-insensitive --pretty -t $VENDOR \
153
+ --log-to=validation_errors \
154
+ --not-null="Purch_Fr_Loc" \
155
+ --match="Purch_Fr_Loc/[a-zA-Z0-9]/" \
156
+ --not-null="vendor_code" \
157
+ --not-null="vendor_name" \
158
+ --unique="vendor_code, Purch_Fr_Loc, vendor_name" \
159
+ --match="vendor_code/[a-zA-Z0-9]/" \
160
+ --match="vendor_name/[a-zA-Z0-9]/" \
161
+ --consistent-by="vendor_code, Purch_Fr_Loc |vendor_name" \
162
+ --consistent-by="vendor_name, Purch_Fr_Loc|vendor_code" \
163
+ --consistent-by="country_code|country_name" \
164
+ --consistent-by="country_name|country_code"
165
+
166
+ # validate MANUFACTURERS
167
+ ivalidate --case-insensitive --pretty -t $MFR \
168
+ --log-to=validation_errors \
169
+ --not-null="mfr_number" \
170
+ --not-null="mfr_name" \
171
+ --not-null="mfr_div" \
172
+ --match="mfr_div/[a-zA-Z0-9]/" \
173
+ --not-null="mfr_name" \
174
+ --unique="mfr_number, mfr_div, mfr_name" \
175
+ --match="mfr_number/[a-zA-Z0-9]/" \
176
+ --match="mfr_name/[a-zA-Z0-9]/" \
177
+ --consistent-by="mfr_name, mfr_div|mfr_number" \
178
+ --consistent-by="mfr_number, mfr_div|mfr_name" \
179
+ --consistent-by="country_code|country_name" \
180
+ --consistent-by="country_name|country_code"
181
+
182
+ # validate GL
183
+ ivalidate --case-insensitive --pretty -t $GL \
184
+ --log-to=validation_errors \
185
+ --not-null=corp_acct_no \
186
+ --match="corp_acct_no/[a-zA-Z0-9]/" \
187
+ --not-null=corp_acct_name \
188
+ --match="corp_acct_name/[a-zA-Z0-9]/" \
189
+ --not-null=corp_acct_fmt \
190
+ --match="corp_acct_fmt/^[A-Z][0-9]-[A-Z][0-9]-[A-Z][0-9]$/" \
191
+ --not-null=cc_acct_no \
192
+ --match="cc_acct_no/[a-zA-Z0-9]/" \
193
+ --not-null=cc_acct_name \
194
+ --match="cc_acct_name/[a-zA-Z0-9]/" \
195
+ --not-null=cc_acct_type \
196
+ --match="cc_acct_type/^(1|2|3|4|5|Asset|Liability|Equity|Income\sStatement|Expense|Income)$/" \
197
+ --not-null=exp_acct_no \
198
+ --match="exp_acct_no/[a-zA-Z0-9]/" \
199
+ --not-null=exp_acct_name \
200
+ --match="exp_acct_name/[a-zA-Z0-9]/" \
201
+ --not-null=fq_acct_no \
202
+ --unique=fq_acct_no \
203
+ --rquery="(fq_acct_no IS NOT NULL AND corp_acct_fmt IS NOT NULL AND fq_acct_no !~ ('^' || regexp_replace(replace(corp_acct_fmt, '-', '}-'), '(?=[abcABC])[A-Z]', '[0-9]{', 'g') || '}$')) -- Invalid fq_acct_no" \
204
+ --consistent-by="corp_acct_no|corp_acct_name" \
205
+ --consistent-by="corp_acct_name|corp_acct_no" \
206
+ --consistent-by="exp_acct_no|corp_acct_no, corp_acct_name, cc_acct_no, cc_acct_name, exp_acct_name" \
207
+ --consistent-by="exp_acct_name|corp_acct_no, corp_acct_name, cc_acct_no, cc_acct_name, exp_acct_no" \
208
+ --consistent-by="cc_acct_no|corp_acct_no, corp_acct_name, cc_acct_name, exp_acct_no, exp_acct_name" \
209
+ --consistent-by="cc_acct_name|corp_acct_no, corp_acct_name, cc_acct_no, exp_acct_no, exp_acct_name" \
210
+ --not-null=exp_acct_type \
211
+ --match="exp_acct_type/^(1|2|3|4|5|Asset|Liability|Equity|Income\sStatement|Expense|Income)$/"
212
+
213
+ # validate LOCATION
214
+ ivalidate --case-insensitive --pretty -t $LOCATION \
215
+ --log-to=validation_errors \
216
+ --not-null="loc_id" \
217
+ --match="loc_id/[a-zA-Z0-9]/" \
218
+ --not-null="name" \
219
+ --match="name/[a-zA-Z0-9]/" \
220
+ --not-null=facility_code \
221
+ --match="facility_code/[a-zA-Z0-9]/" \
222
+ --not-null=facility_desc \
223
+ --match="facility_desc/[a-zA-Z0-9]/" \
224
+ --match="ship_to_ind/^(Y|N|y|n)$/" \
225
+ --match="bill_to_ind/^(Y|N|y|n)$/" \
226
+ --match="stockless_ind/^(Y|N|y|n)$/" \
227
+ --not-null="loc_type" \
228
+ --match="loc_type/^(C|S|LOC_TYPE_SUPPLY|LOC_TYPE_CONSUME)$/" \
229
+ --rquery="(loc_type ~* '^(LOC_TYPE_SUPPLY|S)$' and (corp_acct_no is null or corp_name is null or corp_id is null)) -- either corp id/name or corp_acct_no is null" \
230
+ --not-null="active" \
231
+ --match="active/^(Y|N|1|2|3)$/" \
232
+ --match="corp_acct_no/[a-zA-Z0-9]/" \
233
+ --rquery="((inventory_path_name != '' AND inventory_path_name IS NOT NULL AND lower(inventory_path_name) != 'default') AND (inventory_loc_seq_no IS NULL OR inventory_loc_seq_no = '')) -- [inventory_loc_seq_no] is null" \
234
+ --rquery="((inventory_path_name != '' AND inventory_path_name IS NOT NULL AND lower(inventory_path_name) != 'default') AND (inventory_location_name IS NULL OR inventory_location_name = '')) -- [inventory_location_name] is null" \
235
+ --match="route_no/[a-zA-Z0-9]/" \
236
+ --match="route_name/[a-zA-Z0-9]/" \
237
+ --match="corp_name/[a-zA-Z0-9]/" \
238
+ --consistent-by="corp_name|corp_id" \
239
+ --consistent-by="corp_id|corp_name" \
240
+ --consistent-by="name|facility_code, loc_id" \
241
+ --consistent-by="loc_id|facility_code, name" \
242
+ --cross-reference="inventory_location_name|$LOCATION.name" \
243
+ --cross-reference="corp_id|$GL.corp_acct_no" \
244
+ --cross-reference="corp_name|$GL.corp_acct_name"
245
+
246
+
247
+ # validate CONTRACTS ORIGINAL
248
+ # @note Check unique keyset with item_id included for MSCM only
249
+ ivalidate --case-insensitive --pretty -t $CONTRACTO \
250
+ --log-to=validation_errors \
251
+ --not-null="vendor_code" \
252
+ --match="vendor_code/[a-zA-Z0-9]/" \
253
+ --not-null="mfr_number" \
254
+ --match="mfr_number/[a-zA-Z0-9]/" \
255
+ --not-null="Purch_Fr_Loc" \
256
+ --match="Purch_Fr_Loc/[a-zA-Z0-9]/" \
257
+ --not-null="mfr_div" \
258
+ --match="mfr_div/[a-zA-Z0-9]/" \
259
+ --not-null=contract_number \
260
+ --not-null=contract_start \
261
+ --not-null=contract_end \
262
+ --not-null=vendor_name \
263
+ --not-null=mfr_item_id \
264
+ --not-null=mfr_name \
265
+ --not-null=item_uom \
266
+ --not-null=corp_id \
267
+ --not-null=item_descr \
268
+ --not-null=item_qoe \
269
+ --not-null=contract_price \
270
+ --not-null=contract_gpo_id \
271
+ --match="contract_number/[a-zA-Z0-9]/" \
272
+ --match="contract_gpo_name/[a-zA-Z0-9]/" \
273
+ --match="corp_id/[a-zA-Z0-9]/" \
274
+ --match="corp_name/[a-zA-Z0-9]/" \
275
+ --match="vendor_item_id/[a-zA-Z0-9]/" \
276
+ --match="vendor_name/[a-zA-Z0-9]/" \
277
+ --match="mfr_item_id/[a-zA-Z0-9]/" \
278
+ --match="mfr_name/[a-zA-Z0-9]/" \
279
+ --rquery="(lower(mfr_name) = 'unknown' AND mfr_number IS NULL) -- Unknown mfr_name" \
280
+ --query="to_date(contract_end, 'YYYY-MM-DD') >= to_date(contract_start, 'YYYY-MM-DD') -- [contract_end] comes before [contract_start]" \
281
+ --match="contract_status/^(1|2|3|A|I|Inactive|Active|Y)$/" \
282
+ --match="item_status/^(1|2|3|A|I|Inactive|Active|Y)$/" \
283
+ --consistent-by="corp_id|corp_name" \
284
+ --consistent-by="corp_name|corp_id" \
285
+ --consistent-by="mfr_name|mfr_number" \
286
+ --consistent-by="vendor_code|vendor_name" \
287
+ --consistent-by="vendor_name|vendor_code" \
288
+ --cross-reference="vendor_name|$VENDOR.vendor_name" \
289
+ --cross-reference="mfr_name|$MFR.mfr_name" \
290
+ --cross-reference="corp_id|$GL.corp_acct_no" \
291
+ --cross-reference="corp_name|$GL.corp_acct_name" \
292
+ --cross-reference="mfr_div|$MFR.mfr_div" \
293
+ --cross-reference="Purch_Fr_Loc|$VENDOR.Purch_Fr_Loc" \
294
+ --match="contract_price/^[0-9]+(\.{0,1}[0-9]+|[0-9]*)$/" \
295
+ --match="item_qoe/^[0-9]+(\.{0,1}[0-9]+|[0-9]*)$/" \
296
+ --rquery="(item_uom NOT IN (SELECT code FROM uomstd) AND item_uom !~ '^[a-zA-Z0-9]{1,3}$') -- invalid item_uom" \
297
+ --unique="contract_gpo_name, contract_number, contract_start, contract_end, vendor_name, mfr_item_id, mfr_name, item_uom, corp_id, item_id" \
298
+
299
+ # validate ITEM
300
+ # Accepted:
301
+ # --rquery="mfr_name IN (SELECT mfr_name FROM $ITEM WHERE mfr_name IS NOT NULL GROUP BY mfr_name HAVING count(DISTINCT mfr_number) > 1) -- same mfr_name but with different mfr_number" \
302
+ ivalidate --case-insensitive --pretty -t $ITEM \
303
+ --log-to=validation_errors \
304
+ --not-null="Purch_Fr_Loc" \
305
+ --match="Purch_Fr_Loc/[a-zA-Z0-9]/" \
306
+ --not-null="mfr_div" \
307
+ --match="mfr_div/[a-zA-Z0-9]/" \
308
+ --not-null="item_id" \
309
+ --match="item_id/[a-zA-Z0-9]/" \
310
+ --not-null="item_descr" \
311
+ --match="item_descr/[a-zA-Z0-9]/" \
312
+ --not-null="item_uom" \
313
+ --not-null="default_uom" \
314
+ --not-null="item_price" \
315
+ --not-null="item_qoe" \
316
+ --not-null="corp_id" \
317
+ --not-null="corp_name" \
318
+ --not-null="vendor_code" \
319
+ --not-null="vendor_name" \
320
+ --not-null="mfr_number" \
321
+ --not-null="mfr_name" \
322
+ --not-null="active" \
323
+ --match="corp_id/[a-zA-Z0-9]/" \
324
+ --match="corp_name/[a-zA-Z0-9]/" \
325
+ --match="vendor_code/[a-zA-Z0-9]/" \
326
+ --match="vendor_name/[a-zA-Z0-9]/" \
327
+ --match="mfr_number/[a-zA-Z0-9]/" \
328
+ --match="mfr_name/[a-zA-Z0-9]/" \
329
+ --match="active/^(1|2|3|A|I)$/" \
330
+ --rquery="(lower(mfr_name) = 'unknown' AND mfr_number IS NULL) -- Unknown mfr_name" \
331
+ --unique="item_id, corp_id, vendor_code, item_uom" \
332
+ --cross-reference="vendor_code|$VENDOR.vendor_code" \
333
+ --cross-reference="vendor_name|$VENDOR.vendor_name" \
334
+ --cross-reference="mfr_number|$MFR.mfr_number" \
335
+ --cross-reference="mfr_name|$MFR.mfr_name" \
336
+ --cross-reference="corp_id|$GL.corp_acct_no" \
337
+ --cross-reference="corp_name|$GL.corp_acct_name" \
338
+ --cross-reference="mfr_div|$MFR.mfr_div" \
339
+ --cross-reference="Purch_Fr_Loc|$VENDOR.Purch_Fr_Loc" \
340
+ --consistent-by="corp_id|corp_name" \
341
+ --consistent-by="corp_name|corp_id" \
342
+ --consistent-by="mfr_name,mfr_div|mfr_number" \
343
+ --consistent-by="vendor_code, Purch_Fr_Loc|vendor_name" \
344
+ --consistent-by="vendor_name, Purch_Fr_Loc|vendor_code" \
345
+ --rquery="(item_uom NOT IN (SELECT code FROM uomstd) AND item_uom !~ '^[a-zA-Z0-9]{1,3}$') -- invalid item_uom" \
346
+ --rquery="(default_uom NOT IN (SELECT code FROM uomstd) AND default_uom !~ '^[a-zA-Z0-9]{1,3}$') -- invalid default_uom" \
347
+ --match="item_price/^[0-9]+(\.{0,1}[0-9]+|[0-9]*)$/" \
348
+ --match="item_qoe/^[0-9]+(\.{0,1}[0-9]+|[0-9]*)$/"
349
+
350
+ # validate PO
351
+ ivalidate --case-insensitive --pretty -t $PO \
352
+ --log-to=validation_errors \
353
+ --not-null="Purch_Fr_Loc" \
354
+ --match="Purch_Fr_Loc/[a-zA-Z0-9]/" \
355
+ --not-null="mfr_div" \
356
+ --match="mfr_div/[a-zA-Z0-9]/" \
357
+ --not-null="po_no" \
358
+ --match="po_no/[a-zA-Z0-9]/" \
359
+ --not-null="po_date" \
360
+ --not-null="corp_id" \
361
+ --match="corp_id/[a-zA-Z0-9]/" \
362
+ --not-null="corp_name" \
363
+ --match="corp_name/[a-zA-Z0-9]/" \
364
+ --not-null="cost_center_id" \
365
+ --match="cost_center_id/[a-zA-Z0-9]/" \
366
+ --not-null="cost_center_name" \
367
+ --match="cost_center_name/[a-zA-Z0-9]/" \
368
+ --not-null="po_line_number" \
369
+ --match="po_line_number/^[1-9][0-9]*$/" \
370
+ --not-null="item_id" \
371
+ --match="item_id/[a-zA-Z0-9]/" \
372
+ --not-null="vendor_name" \
373
+ --match="vendor_name/[a-zA-Z0-9]/" \
374
+ --not-null="vendor_code" \
375
+ --match="vendor_code/[a-zA-Z0-9]/" \
376
+ --not-null="mfr_name" \
377
+ --match="mfr_name/[a-zA-Z0-9]/" \
378
+ --not-null="mfr_number" \
379
+ --match="mfr_number/[a-zA-Z0-9]/" \
380
+ --not-null="item_descr" \
381
+ --consistent-by="corp_id|corp_name" \
382
+ --consistent-by="corp_name|corp_id" \
383
+ --consistent-by="vendor_code|vendor_name" \
384
+ --consistent-by="vendor_name|vendor_code" \
385
+ --consistent-by="mfr_name|mfr_number" \
386
+ --unique="po_no, po_line_number" \
387
+ --rquery="(lower(mfr_name) = 'unknown' AND mfr_number IS NULL) -- Unknown mfr_name" \
388
+ --rquery="(item_id not like '%~%' and item_id not in (select item_id from items)) -- [item_id] does not reference [items.item_id]" \
389
+ --cross-reference="vendor_code|$VENDOR.vendor_code" \
390
+ --cross-reference="vendor_name|$VENDOR.vendor_name" \
391
+ --cross-reference="mfr_number|$MFR.mfr_number" \
392
+ --cross-reference="mfr_name|$MFR.mfr_name" \
393
+ --cross-reference="corp_id|$GL.corp_acct_no" \
394
+ --cross-reference="corp_name|$GL.corp_acct_name" \
395
+ --cross-reference="cost_center_id|$GL.cc_acct_no" \
396
+ --cross-reference="cost_center_name|$GL.cc_acct_name" \
397
+ --cross-reference="mfr_div|$MFR.mfr_div" \
398
+ --cross-reference="Purch_Fr_Loc|$VENDOR.Purch_Fr_Loc" \
399
+ --rquery="(purchase_uom NOT IN (SELECT code FROM uomstd) AND purchase_uom !~ '^[a-zA-Z0-9]{1,3}$') -- invalid [purchase_uom]" \
400
+ --rquery="((item_id IS NULL OR item_id !~ '[a-zA-Z0-9]') AND (vendor_item_id IS NULL OR vendor_item_id !~ '[a-zA-Z0-9]')) -- [vendor_item_id] is either null or invalid" \
401
+ --match="purchase_price/^[0-9]+(\.{0,1}[0-9]+|[0-9]*)$/" \
402
+ --match="purchase_qoe/^[0-9]+(\.{0,1}[0-9]+|[0-9]*)$/"
403
+
404
+ # do not check --match="item_descr/[a-zA-Z0-9]/" \
405
+
406
+ # validate Req
407
+ # --consistent-by="corp_id|corp_name" \
408
+ # --consistent-by="corp_name|corp_id" \
409
+ ivalidate --case-insensitive --pretty -t $REQ \
410
+ --log-to=validation_errors \
411
+ --not-null="req_no" \
412
+ --match="req_no/[a-zA-Z0-9]/" \
413
+ --not-null="req_date" \
414
+ --match="req_date/[a-zA-Z0-9]/" \
415
+ --not-null="corp_id" \
416
+ --match="corp_id/[a-zA-Z0-9]/" \
417
+ --not-null="corp_name" \
418
+ --match="corp_name/[a-zA-Z0-9]/" \
419
+ --not-null="costcenter_id" \
420
+ --match="costcenter_id/[a-zA-Z0-9]/" \
421
+ --not-null="req_line_number" \
422
+ --match="req_line_number/^[1-9][0-9]*$/" \
423
+ --not-null="item_id" \
424
+ --match="item_id/[a-zA-Z0-9]/" \
425
+ --not-null="vendor_name" \
426
+ --match="vendor_name/[a-zA-Z0-9]/" \
427
+ --not-null="vendor_code" \
428
+ --match="vendor_code/[a-zA-Z0-9]/" \
429
+ --rquery="(item_id not like '%~%' and item_id not in (select item_id from items)) -- item_id does not reference items.item_id" \
430
+ --cross-reference="corp_id|$GL.corp_acct_no" \
431
+ --cross-reference="corp_name|$GL.corp_acct_name" \
432
+ --cross-reference="vendor_name|$VENDOR.vendor_name" \
433
+ --cross-reference="vendor_code|$VENDOR.vendor_code" \
434
+ --cross-reference="mfr_name|$MFR.mfr_name" \
435
+ --cross-reference="costcenter_id|$GL.cc_acct_no" \
436
+ --cross-reference="costcenter_name|$GL.cc_acct_name" \
437
+ --cross-reference="Purch_Fr_Loc|$VENDOR.Purch_Fr_Loc" \
438
+ --unique="req_no, req_line_number" \
439
+
440
+ # validate USERS
441
+ ivalidate --case-insensitive --pretty -t $USER \
442
+ --log-to=validation_errors \
443
+ --not-null="email" \
444
+ --unique="email" \
445
+ --match="lower(email)/[a-z0-9][a-z0-9_\.]+@[a-z0-9][a-z0-9_\.\-]+\.[a-z0-9_\.\-]+/"
446
+
447
+
448
+ # validate INVENTORY
449
+ ivalidate --case-insensitive --pretty -t $INVENTORY \
450
+ --log-to=validation_errors \
451
+ --not-null="item_id" \
452
+ --match="item_id/[a-zA-Z0-9]/" \
453
+ --not-null="loc_id" \
454
+ --match="loc_id/[a-zA-Z0-9]/" \
455
+ --not-null="vendor_code" \
456
+ --match="vendor_code/[a-zA-Z0-9]/" \
457
+ --not-null="corp_id" \
458
+ --match="corp_id/[a-zA-Z0-9]/" \
459
+ --match="location_name/[a-zA-Z0-9]/" \
460
+ --not-null="item_id" \
461
+ --match="inventory_status/^(Active|Pending Inactive|Inactive|1|2|3)$/" \
462
+ --cross-reference="item_id|$ITEM.item_id" \
463
+ --cross-reference="location_name|$LOCATION.name" \
464
+ --cross-reference="vendor_code|$VENDOR.vendor_code" \
465
+ --cross-reference="vendor_name|$VENDOR.vendor_name" \
466
+ --cross-reference="corp_id|$GL.corp_acct_no" \
467
+ --cross-reference="corp_name|$GL.corp_acct_name"
468
+ --cross-reference="Purch_Fr_Loc|$VENDOR.Purch_Fr_Loc" \
469
+
470
+ ivalidate --case-insensitive --pretty -t $ULPR \
471
+ --log-to=validation_errors \
472
+ --match="Default_Indicator/^(Y|N|y|n|Y|N)$/" \
473
+ --not-null="email" \
474
+ --cross-reference="email|$USER.email" \
475
+ --cross-reference="loc_id|$LOCATION.loc_id" \
476
+ --cross-reference="loc_name|$LOCATION.name" \
477
+ --cross-reference="corp_no|$GL.corp_acct_no" \
478
+ --cross-reference="corp_name|$GL.corp_acct_name" \
479
+
480
+ ivalidate --case-insensitive --pretty -t $LPR \
481
+ --log-to=validation_errors \
482
+ --not-null="Default_Inventory_Location_Name" \
483
+ --cross-reference="Default_Inventory_Location_Name|$LOCATION.Inventory_Location_Name" \
484
+ --match="loc_type/^(C|LOC_TYPE_CONSUME)$/" \
485
+ --match="active/^(1|2|3|A|I|Y|N)$/" \
486
+ --cross-reference="item_id|$ITEM.item_id" \
487
+ --cross-reference="corp_acct_no|$GL.corp_acct_no" \
488
+ --cross-reference="corp_name|$GL.corp_acct_name" \
489
+ --cross-reference="cc_acct_no|$GL.cc_acct_no" \
490
+ --cross-reference="cc_acct_name|$GL.cc_acct_name" \
491
+ --cross-reference="loc_id|$LOCATION.loc_id" \
492
+ --cross-reference="Location_Name|$LOCATION.name"
493
+
494
+ ####################################################
495
+ # Create report file for every table (extract 1000 records for every error)
496
+ # These file will then be used for the Validation Report
497
+ ####################################################
498
+
499
+ iexport -t $CONTRACTO \
500
+ -o "$OUTPUT_DIR/$CONTRACTO.csv" -f csv --no-quote-empty --quotes --headers \
501
+ --query="select * from (select ROW_NUMBER() OVER (PARTITION BY error) AS group_index, *
502
+ FROM ( select unnest(string_to_array(validation_errors, ' || ')) as error, * from
503
+ $CONTRACTO order by id ) as main) as tmp
504
+ where group_index <= 1000" \
505
+ --exclude="id, validation_errors, group_index"
506
+
507
+ iexport -t $VENDOR \
508
+ -o "$OUTPUT_DIR/$VENDOR.csv" -f csv --no-quote-empty --quotes --headers \
509
+ --query="select * from (select ROW_NUMBER() OVER (PARTITION BY error) AS group_index, *
510
+ FROM ( select unnest(string_to_array(validation_errors, ' || ')) as error, * from
511
+ $VENDOR order by id ) as main) as tmp
512
+ where group_index <= 1000" \
513
+ --exclude="id, validation_errors, group_index"
514
+
515
+ iexport -t $MFR \
516
+ -o "$OUTPUT_DIR/$MFR.csv" -f csv --no-quote-empty --quotes --headers \
517
+ --query="select * from (select ROW_NUMBER() OVER (PARTITION BY error) AS group_index, *
518
+ FROM ( select unnest(string_to_array(validation_errors, ' || ')) as error, * from
519
+ $MFR order by id ) as main) as tmp
520
+ where group_index <= 1000" \
521
+ --exclude="id, validation_errors, group_index"
522
+
523
+ iexport -t $GL \
524
+ -o "$OUTPUT_DIR/$GL.csv" -f csv --no-quote-empty --quotes --headers \
525
+ --query="select * from (select ROW_NUMBER() OVER (PARTITION BY error) AS group_index, *
526
+ FROM ( select unnest(string_to_array(validation_errors, ' || ')) as error, * from
527
+ $GL order by id ) as main) as tmp
528
+ where group_index <= 1000" \
529
+ --exclude="id, validation_errors, group_index"
530
+
531
+ iexport -t $PO \
532
+ -o "$OUTPUT_DIR/$PO.csv" -f csv --no-quote-empty --quotes --headers \
533
+ --query="select * from (select ROW_NUMBER() OVER (PARTITION BY error) AS group_index, *
534
+ FROM ( select unnest(string_to_array(validation_errors, ' || ')) as error, * from
535
+ $PO order by id ) as main) as tmp
536
+ where group_index <= 1000" \
537
+ --exclude="id, validation_errors, group_index"
538
+
539
+ iexport -t $INVENTORY \
540
+ -o "$OUTPUT_DIR/$INVENTORY.csv" -f csv --no-quote-empty --quotes --headers \
541
+ --query="select * from (select ROW_NUMBER() OVER (PARTITION BY error) AS group_index, *
542
+ FROM ( select unnest(string_to_array(validation_errors, ' || ')) as error, * from
543
+ $INVENTORY order by id ) as main) as tmp
544
+ where group_index <= 1000" \
545
+ --exclude="id, validation_errors, group_index"
546
+
547
+ iexport -t $REQ \
548
+ -o "$OUTPUT_DIR/$REQ.csv" -f csv --no-quote-empty --quotes --headers \
549
+ --query="select * from (select ROW_NUMBER() OVER (PARTITION BY error) AS group_index, *
550
+ FROM ( select unnest(string_to_array(validation_errors, ' || ')) as error, * from
551
+ $REQ order by id ) as main) as tmp
552
+ where group_index <= 1000" \
553
+ --exclude="id, validation_errors, group_index"
554
+
555
+ iexport -t $ITEM \
556
+ -o "$OUTPUT_DIR/$ITEM.csv" -f csv --no-quote-empty --quotes --headers \
557
+ --query="select * from (select ROW_NUMBER() OVER (PARTITION BY error) AS group_index, *
558
+ FROM ( select unnest(string_to_array(validation_errors, ' || ')) as error, * from
559
+ $ITEM order by id ) as main) as tmp
560
+ where group_index <= 1000" \
561
+ --exclude="id, validation_errors, group_index"
562
+
563
+ iexport -t $USER \
564
+ -o "$OUTPUT_DIR/$USER.csv" -f csv --no-quote-empty --quotes --headers \
565
+ --query="select * from (select ROW_NUMBER() OVER (PARTITION BY error) AS group_index, *
566
+ FROM ( select unnest(string_to_array(validation_errors, ' || ')) as error, * from
567
+ $USER order by id ) as main) as tmp
568
+ where group_index <= 1000" \
569
+ --exclude="id, validation_errors, group_index"
570
+
571
+ iexport -t $LOCATION \
572
+ -o "$OUTPUT_DIR/$LOCATION.csv" -f csv --no-quote-empty --quotes --headers \
573
+ --query="select * from (select ROW_NUMBER() OVER (PARTITION BY error) AS group_index, *
574
+ FROM ( select unnest(string_to_array(validation_errors, ' || ')) as error, * from
575
+ $LOCATION order by id ) as main) as tmp
576
+ where group_index <= 1000" \
577
+ --exclude="id, validation_errors, group_index"
578
+
579
+ iexport -t $ULPR \
580
+ -o "$OUTPUT_DIR/$ULPR.csv" -f csv --no-quote-empty --quotes --headers \
581
+ --query="select * from (select ROW_NUMBER() OVER (PARTITION BY error) AS group_index, *
582
+ FROM ( select unnest(string_to_array(validation_errors, ' || ')) as error, * from
583
+ $ULPR order by id ) as main) as tmp
584
+ where group_index <= 1000" \
585
+ --exclude="id, validation_errors, group_index"
586
+
587
+ iexport -t $LPR \
588
+ -o "$OUTPUT_DIR/$LPR.csv" -f csv --no-quote-empty --quotes --headers \
589
+ --query="select * from (select ROW_NUMBER() OVER (PARTITION BY error) AS group_index, *
590
+ FROM ( select unnest(string_to_array(validation_errors, ' || ')) as error, * from
591
+ $LPR order by id ) as main) as tmp
592
+ where group_index <= 1000" \
593
+ --exclude="id, validation_errors, group_index"
594
+
595
+
596
+ # Use SQL to compute the summary, write the outputs to summary.csv
597
+ # --------------------------------------------------------------
598
+ iexport --output="$OUTPUT_DIR/summary.csv" -f csv --no-quote-empty --quotes --headers \
599
+ --query="(select 'ContractMaster' as input_file, unnest(string_to_array(validation_errors, ' || ')) as error, count(*), round((count(*) * 100)::numeric / (select count(*) from $CONTRACTO), 2)::varchar || '%' as percentage from $CONTRACTO group by error order by error) union
600
+ (select 'VendorMaster' as input_file, unnest(string_to_array(validation_errors, ' || ')) as error, count(*), round((count(*) * 100)::numeric / (select count(*) from $VENDOR), 2)::varchar || '%' as percentage from $VENDOR group by error order by error) union
601
+ (select 'ItemMaster' as input_file, unnest(string_to_array(validation_errors, ' || ')) as error, count(*), round((count(*) * 100)::numeric / (select count(*) from $ITEM), 2)::varchar || '%' as percentage from $ITEM group by error order by error) union
602
+ (select 'ReqHistoryLoad' as input_file, unnest(string_to_array(validation_errors, ' || ')) as error, count(*), round((count(*) * 100)::numeric / (select count(*) from $REQ), 2)::varchar || '%' as percentage from $REQ group by error order by error) union
603
+ (select 'MfrMaster' as input_file, unnest(string_to_array(validation_errors, ' || ')) as error, count(*), round((count(*) * 100)::numeric / (select count(*) from $MFR), 2)::varchar || '%' as percentage from $MFR group by error order by error) union
604
+ (select 'PurchaseOrder' as input_file, unnest(string_to_array(validation_errors, ' || ')) as error, count(*), round((count(*) * 100)::numeric / (select count(*) from $PO), 2)::varchar || '%' as percentage from $PO group by error order by error) union
605
+ (select 'Inventory' as input_file, unnest(string_to_array(validation_errors, ' || ')) as error, count(*), round((count(*) * 100)::numeric / (select count(*) from $INVENTORY), 2)::varchar || '%' as percentage from $INVENTORY group by error order by error) union
606
+ (select 'User' as input_file, unnest(string_to_array(validation_errors, ' || ')) as error, count(*), round((count(*) * 100)::numeric / (select count(*) from $USER), 2)::varchar || '%' as percentage from $USER group by error order by error) union
607
+ (select 'Location' as input_file, unnest(string_to_array(validation_errors, ' || ')) as error, count(*), round((count(*) * 100)::numeric / (select count(*) from $LOCATION), 2)::varchar || '%' as percentage from $LOCATION group by error order by error) union
608
+ (select 'GLAccount' as input_file, unnest(string_to_array(validation_errors, ' || ')) as error, count(*), round((count(*) * 100)::numeric / (select count(*) from $GL), 2)::varchar || '%' as percentage from $GL group by error order by error) union
609
+ (select 'UserLocationProfile' as input_file, unnest(string_to_array(validation_errors, ' || ')) as error, count(*), round((count(*) * 100)::numeric / (select count(*) from $ULPR), 2)::varchar || '%' as percentage from $ULPR group by error order by error) union
610
+ (select 'LocationProfile' as input_file, unnest(string_to_array(validation_errors, ' || ')) as error, count(*), round((count(*) * 100)::numeric / (select count(*) from $LPR), 2)::varchar || '%' as percentage from $LPR group by error order by error)"
611
+
612
+ # Merge summary.xls and report files into one single file with several tabs
613
+ # --input="ItemCostCenterAcctExceptions:$OUTPUT_DIR/$ITEMCOST.csv" \
614
+ imerge --output=$OUTPUT_DIR/$ORGNAME.xls \
615
+ --input="Summary:$OUTPUT_DIR/summary.csv" \
616
+ --input="ContractMaster:$OUTPUT_DIR/$CONTRACTO.csv" \
617
+ --input="ItemMaster:$OUTPUT_DIR/$ITEM.csv" \
618
+ --input="MfrMaster:$OUTPUT_DIR/$MFR.csv" \
619
+ --input="VendorMaster:$OUTPUT_DIR/$VENDOR.csv" \
620
+ --input="PurchaseOrder:$OUTPUT_DIR/$PO.csv" \
621
+ --input="User:$OUTPUT_DIR/$USER.csv" \
622
+ --input="Location:$OUTPUT_DIR/$LOCATION.csv" \
623
+ --input="ReqHistoryLoad:$OUTPUT_DIR/$REQ.csv" \
624
+ --input="GLAccount:$OUTPUT_DIR/$GL.csv" \
625
+ --input="Inventory:$OUTPUT_DIR/$INVENTORY.csv" \
626
+ --input="$ULPR:$OUTPUT_DIR/$ULPR.csv" \
627
+ --input="$LPR:$OUTPUT_DIR/$LPR.csv"
628
+
629
+ ####################################################
630
+ # EXPORT FOR UPLOADING
631
+ ####################################################
632
+
633
+ ipatch -q "
634
+ DELETE FROM purchase_orders where validation_errors ilike '%[po_no, po_line_number] is not unique%';
635
+ DELETE FROM purchase_orders where validation_errors ilike '%[item_id] does not reference [items.item_id]%';
636
+
637
+ DELETE FROM users where validation_errors ilike '%email is null%';
638
+ DELETE FROM user_location_profiles where validation_errors ilike '%email is null%';
639
+ "
640
+
641
+
642
+
643
+ iexport -t $ITEMCOST \
644
+ -o "$OUTPUT_DIR/$ITEMCOST.csv" -f csv --no-quote-empty --no-quotes --headers --delim=$'\t' \
645
+ --exclude="id, validation_errors"
646
+
647
+ iexport -t $CONTRACTO \
648
+ -o "$OUTPUT_DIR/$CONTRACTO.csv" -f csv --no-quote-empty --no-quotes --headers --delim=$'\t' \
649
+ --exclude="id, validation_errors"
650
+
651
+ iexport -t $VENDOR \
652
+ -o "$OUTPUT_DIR/$VENDOR.csv" -f csv --no-quote-empty --quotes --headers --delim=$'\t' \
653
+ --exclude="id, validation_errors"
654
+
655
+ iexport -t $MFR \
656
+ -o "$OUTPUT_DIR/$MFR.csv" -f csv --no-quote-empty --quotes --headers --delim=$'\t' \
657
+ --exclude="id, validation_errors"
658
+
659
+ iexport -t $GL \
660
+ -o "$OUTPUT_DIR/$GL.csv" -f csv --no-quote-empty --quotes --headers --delim=$'\t' \
661
+ --exclude="id, validation_errors"
662
+
663
+ iexport -t $PO \
664
+ -o "$OUTPUT_DIR/$PO.csv" -f csv --no-quote-empty --no-quotes --headers --delim=$'\t' \
665
+ --exclude="id, validation_errors"
666
+
667
+ iexport -t $INVENTORY \
668
+ -o "$OUTPUT_DIR/$INVENTORY.csv" -f csv --no-quote-empty --quotes --headers --delim=$'\t' \
669
+ --exclude="id, validation_errors"
670
+
671
+ iexport -t $REQ \
672
+ -o "$OUTPUT_DIR/$REQ.csv" -f csv --no-quote-empty --quotes --headers --delim=$'\t' \
673
+ --exclude="id, validation_errors"
674
+
675
+ iexport -t $ITEM \
676
+ -o "$OUTPUT_DIR/$ITEM.csv" -f csv --no-quote-empty --no-quotes --headers --delim=$'\t' \
677
+ --query="select item_id, item_descr,vendor_name,vendor_code,vendor_item_id,mfr_name,mfr_number,mfr_item_id,corp_id,corp_name, active, array_to_string(array_agg(item_uom), ',') item_uom, array_to_string(array_agg(item_qoe),',') item_qoe,array_to_string(array_agg(item_price),',') item_price
678
+ from
679
+ (
680
+ select * from items order by item_id, item_descr,vendor_name,vendor_code,vendor_item_id,mfr_name,mfr_number,mfr_item_id,corp_id,corp_name, active, item_qoe::float desc
681
+ ) abc
682
+ group by item_id, item_descr,vendor_name,vendor_code,vendor_item_id,mfr_name,mfr_number,mfr_item_id,corp_id,corp_name, active
683
+ " \
684
+ --exclude="id, validation_errors, group_index"
685
+
686
+
687
+ ipatch -q "
688
+ update users set phone = regexp_replace(phone, '[^0123456789]', '', 'g');
689
+ update users set phone = '1234567890' where phone is null or length(phone) < 10;
690
+ update users set first_name = username where length(first_name) < 2;
691
+ update users set last_name = username where length(last_name) < 2;
692
+ "
693
+ iexport -t $USER \
694
+ -o "$OUTPUT_DIR/$USER.csv" -f csv --no-quote-empty --no-quotes --no-headers --delim=',' \
695
+ --query="select first_name, last_name, phone, 0 as tmp1, -1 as tmp2, -1 as tmp3, -1 as tmp4, -1 as tmp5, email, '12345678' as passwd, 'Analyst' as tmp6 from users WHERE email IS NOT NULL AND length(email) > 0"
696
+
697
+ iexport -t $LOCATION \
698
+ -o "$OUTPUT_DIR/$LOCATION.csv" -f csv --no-quote-empty --quotes --headers --delim=$'\t' \
699
+ --exclude="id, validation_errors"
700
+
701
+ iexport -t $LPR \
702
+ -o "$OUTPUT_DIR/$LPR.csv" -f csv --no-quote-empty --quotes --headers --delim=$'\t' \
703
+ --exclude="id, validation_errors"
704
+
705
+ iexport -t $ULPR \
706
+ -o "$OUTPUT_DIR/$ULPR.csv" -f csv --no-quote-empty --quotes --headers --delim=$'\t' \
707
+ --exclude="id, validation_errors"