-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvbfa_mdl.py
318 lines (294 loc) · 11.7 KB
/
vbfa_mdl.py
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
import pandas as pd
import os
import networkx
import datetime
from frozendict import frozendict
from copy import deepcopy
import uuid
class Shared:
vbeln = {}
timestamp_column = "event_timestamp"
activity_column = "event_activity"
dir = r"../sap_extraction"
tcodes = {}
associated_events = {}
enable_filling_events = False
unmapped = set()
timestamps = {}
event_map = {}
bkpf = {}
vbap = {}
ekpo = {}
lips = {}
def read_vbap():
vbap = pd.read_csv(os.path.join(Shared.dir, "vbap.tsv"), sep="\t", dtype={"VBELN": str, "MATNR": str})
vbap = vbap.dropna(subset=["VBELN"])
vbap = vbap.dropna(subset=["MATNR"])
vbap = vbap.to_dict("r")
for el in vbap:
vbeln = el["VBELN"]
matnr = el["MATNR"]
if not vbeln in Shared.vbap:
Shared.vbap[vbeln] = set()
Shared.vbap[vbeln].add(matnr)
def read_ekpo():
ekpo = pd.read_csv(os.path.join(Shared.dir, "ekpo.tsv"), sep="\t", dtype={"EBELN": str, "MATNR": str})
ekpo = ekpo.dropna(subset=["EBELN"])
ekpo = ekpo.dropna(subset=["MATNR"])
ekpo = ekpo.to_dict("r")
for ev in ekpo:
ebeln = ev["EBELN"]
matnr = ev["MATNR"]
if ebeln not in Shared.ekpo:
Shared.ekpo[ebeln] = set()
Shared.ekpo[ebeln].add(matnr)
def read_lips():
lips = pd.read_csv(os.path.join(Shared.dir, "lips.tsv"), sep="\t", dtype={"VBELN": str, "MATNR": str})
lips = lips.dropna(subset=["VBELN"])
lips = lips.dropna(subset=["MATNR"])
lips = lips.to_dict("r")
for ev in lips:
vbeln = ev["VBELN"]
matnr = ev["MATNR"]
if vbeln not in Shared.lips:
Shared.lips[vbeln] = set()
Shared.lips[vbeln].add(matnr)
def read_ekko():
ekko = pd.read_csv(os.path.join(Shared.dir, "ekko.tsv"), sep="\t", dtype={"EBELN": str})
ekko = ekko[["EBELN", "BEDAT", "ERNAM"]]
ekko = ekko.rename(columns={"ERNAM": "event_resource", "BEDAT": Shared.timestamp_column})
ekko[Shared.timestamp_column] = pd.to_datetime(ekko[Shared.timestamp_column], format="%d.%m.%Y")
ekko[Shared.activity_column] = "ME21N"
ekko = ekko.dropna(subset=[Shared.activity_column])
stream = ekko.to_dict("r")
events = set()
for ev in stream:
ebeln = ev["EBELN"]
if ebeln in Shared.ekpo:
basic_event = {Shared.timestamp_column: ev[Shared.timestamp_column],
Shared.activity_column: ev[Shared.activity_column], "event_id": str(uuid.uuid4()),
"event_objid": ebeln, "event_objtype": "EINKBELEG"}
event = deepcopy(basic_event)
event["EINKBELEG"] = ebeln
events.add(frozendict(event))
for el in Shared.ekpo[ebeln]:
event = deepcopy(basic_event)
event["MATERIAL"] = el
events.add(frozendict(event))
return events
def read_eban():
eban = pd.read_csv(os.path.join(Shared.dir, "eban.tsv"), sep="\t", dtype={"BANFN": str, "MATNR": str})
eban["ERDAT"] = pd.to_datetime(eban["ERDAT"], format="%d.%m.%Y")
eban = eban.rename(columns={"ERDAT": Shared.timestamp_column})
eban = eban.to_dict("r")
# EINKBELEG
events = set()
for ev in eban:
basic_event = {Shared.timestamp_column: ev[Shared.timestamp_column], Shared.activity_column: "ME51N",
"event_id": str(uuid.uuid4()), "event_objid": ev["BANFN"], "event_objtype": "EINKBELEG"}
ev0 = deepcopy(basic_event)
ev0["EINKBELEG"] = ev["BANFN"]
ev1 = deepcopy(basic_event)
ev1["MATERIAL"] = ev["MATNR"]
events.add(frozendict(ev0))
events.add(frozendict(ev1))
return events
def extract_bkpf():
bkpf = pd.read_csv(os.path.join(Shared.dir, "bkpf.tsv"), sep="\t", dtype={"BELNR": str, "AWKEY": str})
bkpf = bkpf[["BELNR", "CPUDT", "CPUTM", "USNAM", "TCODE", "AWKEY"]]
bkpf = bkpf.rename(columns={"USNAM": "event_resource", "TCODE": Shared.activity_column})
bkpf[Shared.timestamp_column] = bkpf["CPUDT"] + " " + bkpf["CPUTM"]
bkpf[Shared.timestamp_column] = pd.to_datetime(bkpf[Shared.timestamp_column], format="%d.%m.%Y %H:%M:%S")
stream = bkpf.to_dict("r")
for ev in stream:
awkey = ev["AWKEY"]
if awkey not in Shared.bkpf:
Shared.bkpf[awkey] = set()
Shared.bkpf[awkey].add(frozendict(
{Shared.timestamp_column: ev[Shared.timestamp_column], Shared.activity_column: ev[Shared.activity_column],
"event_id": str(uuid.uuid4()), "event_objtype": "BELNR", "event_objid": ev["BELNR"]}))
def read_activities():
tstct = pd.read_csv(os.path.join(Shared.dir, "TSTCT.tsv"), sep="\t")
tstct = tstct[tstct["SPRSL"] == "E"]
tstct = tstct[["TCODE", "TTEXT"]]
stream = tstct.to_dict("r")
for row in stream:
Shared.tcodes[row["TCODE"]] = row["TTEXT"]
def read_vbak():
vbak = pd.read_csv(os.path.join(Shared.dir, "vbak.tsv"), sep="\t", dtype={"VBELN": str, "VBTYP": str})
vbak[Shared.timestamp_column] = vbak["ERDAT"] + " " + vbak["ERZET"]
vbak[Shared.timestamp_column] = pd.to_datetime(vbak[Shared.timestamp_column], format="%d.%m.%Y %H:%M:%S")
# vbak = vbak[["VBELN", Shared.timestamp_column]]
vbak = vbak.to_dict("r")
Shared.vbeln = {ev["VBELN"]: frozendict({"event_" + x: y for x, y in ev.items()}) for ev in vbak}
Shared.timestamps = {ev["VBELN"]: ev[Shared.timestamp_column] for ev in vbak}
# print(Shared.vbeln)
def get_class_from_type(typ):
dct = {}
dct["C"] = "VERKBELEG"
dct["J"] = "HANDL_UNIT"
dct["Q"] = "WMS_TRANSFER"
dct["R"] = "CHARGE"
dct["M"] = "BELNR"
dct["L"] = "DEBIT_MEMO_REQ"
dct["P"] = "DEBIT_MEMO_DOC"
dct["U"] = "INVOICE_PRO_FORMA"
dct["H"] = "RETURNS_DOC"
dct["A"] = "VERKBELEG" # inquiry
dct["T"] = "RETURNS_DELIVERY"
dct["D"] = "ITEM_PROPOSAL"
dct["V"] = "INFOSATZ" # purchase order
dct["N"] = "INVOICE_CANCEL"
dct["E"] = "EINKBELEG" # scheduling agreement
dct["O"] = "CREDIT_MEMO_DOC"
dct["K"] = "CREDIT_MEMO_REQ"
dct["B"] = "VERKBELEG" # quotation
dct["G"] = "VERKBELEG" # contract
dct["W"] = "INDIP_REQ" # indipendent requisition
dct["I"] = "ORD_WO_CHARGE" # order without charge
dct["X"] = "HANDL_UNIT" # handling unit
if typ in dct:
return dct[typ]
Shared.unmapped.add(typ)
return "C_" + str(typ)
if __name__ == "__main__":
read_lips()
read_ekpo()
ekko_events = read_ekko()
read_vbap()
eban_events = read_eban()
print(eban_events)
#eban_events = set()
extract_bkpf()
read_activities()
read_vbak()
G = networkx.DiGraph()
nodes = {}
timestamp = {}
path = os.path.join(Shared.dir, "VBFA.tsv")
vbfa = pd.read_csv(path, sep="\t", dtype={"VBELN": str, "VBELV": str})
# df = df.sample(n=100)
vbfa["event_timestamp"] = vbfa["ERDAT"] + " " + vbfa["ERZET"]
vbfa["event_timestamp"] = pd.to_datetime(vbfa["event_timestamp"], format="%d.%m.%Y %H:%M:%S")
stream = vbfa.to_dict("r")
for ev in stream:
id1 = ev["VBELV"]
id2 = ev["VBELN"]
typ1 = ev["VBTYP_V"]
typ2 = ev["VBTYP_N"]
if not id1 in G.nodes:
nodes[id1] = typ1
G.add_node(id1)
if not id2 in G.nodes:
nodes[id2] = typ2
G.add_node(id2)
G.add_edge(id1, id2)
timestamp[id2] = ev["event_timestamp"]
events = set()
id = 0
for node in G.nodes:
id = id + 1
typ = nodes[node]
cl = get_class_from_type(typ)
timest = timestamp[node] if node in timestamp else (
Shared.timestamps[node] if node in Shared.timestamps else None)
if timest is not None:
basic_event = {Shared.activity_column: typ, Shared.timestamp_column: timest, "event_id": str(id),
"event_objtype": cl, "event_objid": node}
event = deepcopy(basic_event)
event[cl] = node
Shared.event_map[node] = cl
events.add(frozendict(event))
for other_node in G.predecessors(node):
event = deepcopy(basic_event)
otyp = nodes[other_node]
ocl = get_class_from_type(otyp)
event[ocl] = other_node
events.add(frozendict(event))
if node in Shared.vbap:
for matnr in Shared.vbap[node]:
event = deepcopy(basic_event)
event["MATERIAL"] = matnr
events.add(frozendict(event))
if node in Shared.lips:
for matnr in Shared.lips[node]:
event = deepcopy(basic_event)
event["MATERIAL"] = matnr
events.add(frozendict(event))
events_to_add = set()
for awkey in Shared.bkpf:
for ev in Shared.bkpf[awkey]:
print(ev)
events_to_add.add(ev)
"""for ev in events:
if ev["event_objid"] in Shared.bkpf:
for nev0 in Shared.bkpf[ev["event_objid"]]:
nev1 = dict(nev0)
nev1["BELNR"] = nev1["event_objid"]
events_to_add.add(frozendict(nev1))
nev2 = dict(nev0)
nev2[ev["event_objtype"]] = ev["event_objid"]
events_to_add.add(frozendict(nev2))"""
for ev in events_to_add:
events.add(ev)
for ev in eban_events:
events.add(ev)
for ev in ekko_events:
events.add(ev)
events = [dict(x) for x in events]
"""for ev in events:
if ev["event_objid"] in Shared.vbeln:
ev.update(Shared.vbeln[ev["event_objid"]])"""
df = pd.DataFrame(events)
df.type = "exploded"
# unique_values = set(df[Shared.activity_column].unique())
# activities = {x: x for x in unique_values}
activities = {}
activities["C"] = "Create Order"
activities["J"] = "Create Delivery"
activities["Q"] = "WMS Transfer Order"
activities["R"] = "Goods Movement"
activities["M"] = "Create Invoice"
activities["L"] = "Create Debit Memo Request"
activities["P"] = "Create Debit Memo"
activities["U"] = "Create Pro Forma Invoice"
activities["H"] = "Create Returns Document"
"""
dct["A"] = "VERKBELEG" # inquiry
dct["T"] = "RETURNS_DELIVERY"
dct["D"] = "ITEM_PROPOSAL"
dct["V"] = "INFOSATZ" # purchase order
dct["N"] = "INVOICE_CANCEL"
dct["E"] = "EINKBELEG" # scheduling agreement
dct["O"] = "CREDIT_MEMO_DOC"
dct["K"] = "CREDIT_MEMO_REQ"
dct["B"] = "VERKBELEG" # quotation
dct["G"] = "VERKBELEG" # contract
dct["W"] = "INDIP_REQ" # indipendent requisition
dct["I"] = "ORD_WO_CHARGE" # order without charge
dct["X"] = "HANDL_UNIT" # handling unit
"""
activities["A"] = "Create Inquiry"
activities["T"] = "Returns Delivery"
activities["D"] = "Item Proposal"
activities["V"] = "Create Purchase Order"
activities["N"] = "Invoice Cancellation"
activities["E"] = "Scheduling Agreement"
activities["O"] = "Create Credit Memo"
activities["K"] = "Create Credit Memo Request"
activities["B"] = "Create Quotation"
activities["G"] = "Create Contract"
activities["W"] = "Indipendent Requisition"
activities["I"] = "Create Order without Charge"
activities["X"] = "Handling Unit"
activities.update(Shared.tcodes)
df[Shared.activity_column] = df[Shared.activity_column].map(activities)
df = df.dropna(subset=[Shared.activity_column])
df = df[[x for x in df.columns if "named:" not in x]]
allowed_columns = [x for x in df.columns if not x.startswith("C_") and not x.startswith("event_")]
df = df.dropna(subset=allowed_columns, how="all")
df = df.sort_values(Shared.timestamp_column)
print(df)
df.type = "exploded"
from pm4pymdl.objects.mdl.exporter import exporter as mdl_exporter
mdl_exporter.apply(df, "sap.mdl")