-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpProject.json
More file actions
245 lines (245 loc) · 12 KB
/
Copy pathCpProject.json
File metadata and controls
245 lines (245 loc) · 12 KB
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
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": 3
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Testing_LIR:\n",
"\n",
" \n",
" #instantiate a dictionary containing all invoices in queue + invoice ID's needed to dig further into API\n",
"\n",
" def __init__(self):\n",
" #credentials \n",
" self.payload={}\n",
" self.headers = {\n",
" 'accept': 'application/json',\n",
" 'Authorization': 'Bearer ##{token obtained via token request}##\n",
" }\n",
"\n",
" #use credentials to grab all LIR invoices with:\n",
" #response = requests.request(\"GET\", lir_url, headers=headers)\n",
"\n",
" #example dictionary return until I get credentials figured out\n",
" self.invoices_dict = [{'id': 176931,\n",
" 'processID': '182309_AP_PAY',\n",
" 'activityID': '9015226_182309_AP_PAY_LINE_ITEM_RESOLUTION_SCREEN',\n",
" 'activityDefinitionId': 'LINE_ITEM_RESOLUTION_SCREEN',\n",
" 'activityName': 'Line Item Resolution',\n",
" 'activityState': 'open.running',\n",
" 'processState': 'open.running',\n",
" 'resourceID': 'Resttestuser',\n",
" 'activityStartedDate': '2021-02-10T15:15:40.000-0600',\n",
" 'lockedBy': None,\n",
" '_links': {'self': {'href': 'https://test.apiclient.com/CHPAPO/v2/lineitemresolution/176931'},\n",
" 'lineitemresolution': {'href': 'https://test.apiclient.com/CHPAPO/v2/lineitemresolution/176931'},\n",
" 'terminate': {'href': 'https://test.apiclient.com/CHPAPO/v2/lineitemresolution/176931/terminate'},\n",
" 'job': {'href': 'https://test.apiclient.com/CHPAPO/v2/lineitemresolution/176931/job'}}},\n",
" {'id': 176960,\n",
" 'processID': '182338_AP_PAY',\n",
" 'activityID': '8979894_182338_AP_PAY_LINE_ITEM_RESOLUTION_SCREEN',\n",
" 'activityDefinitionId': 'LINE_ITEM_RESOLUTION_SCREEN',\n",
" 'activityName': 'Line Item Resolution',\n",
" 'activityState': 'open.running',\n",
" 'processState': 'open.running',\n",
" 'resourceID': 'Resttestuser',\n",
" 'activityStartedDate': '2021-01-26T12:23:04.000-0600',\n",
" 'lockedBy': None,\n",
" '_links': {'self': {'href': 'https://test.apiclient.com/CHPAPO/v2/lineitemresolution/176960'},\n",
" 'lineitemresolution': {'href': 'https://test.apiclient.com/CHPAPO/v2/lineitemresolution/176960'},\n",
" 'terminate': {'href': 'https://test.apiclient.com/CHPAPO/v2/lineitemresolution/176960/terminate'},\n",
" 'job': {'href': 'https://test.apiclient.com/CHPAPO/v2/lineitemresolution/176960/job'}}}]\n",
" \n",
" #returns the number of invoices to instantiate for entire program\n",
" self.number_invoices = len(self.invoices_dict)\n",
"\n",
" \n",
"\n",
"class Invoice():\n",
"\n",
" #loop through an instantiate invoice 'number_invoices' times\n",
" def __init__(self, queue_position):\n",
" self.queue_position = queue_position\n",
" self.document_number = int()\n",
" self.document_amount = float()\n",
" self.approved_amount = float()\n",
" self.freight_amount = float()\n",
" self.document_date = str()\n",
" self.sku_ids = []\n",
" #document ID needed to dig further into API\n",
" self.document_id = testing_lir.invoices_dict[self.queue_position]['id']\n",
"\n",
" #general invoice information - might be of use later\n",
" def get_job_info(self):\n",
" job_url = testing_lir.invoices_dict[self.queue_position]['_links']['job']['href']\n",
" r = requests.request(\"GET\", job_url, headers=headers, data=payload)\n",
" job = json.loads(r.text)\n",
"\n",
" self.document_number = job['documentNumber']\n",
" self.document_amount = job['documentAmount']\n",
" self.approved_amount = job['approvedAmount']\n",
" self.freight_amount = job['freightAmount']\n",
" self.document_date = job['documentDate']\n",
"\n",
" def extract_sku_matrix(skus_info, feature):\n",
" return [sku[feature] for sku in skus_info]\n",
"\n",
" def matrix_to_df(data, cols=['documentDetailId', 'quantity', 'unitPrice', 'adjustedQuantity', 'adjustedUnitPrice']):\n",
" return pd.DataFrame(data = data, columns = cols)\n",
"\n",
"\n",
" def get_api_sku_info(self):\n",
" #get information entered by CenterViews for each SKU on a given invoice as well as links to PO/receiving data supplied by ChemPoint needed for calculations\n",
" api_sku_infos_url = f'https://test.apiclient.com/CHPAPO/v2/jobs/{self.document_id}/documentdetails'\n",
" sku_infos_request = requests.request(\"GET\", api_sku_infos_url, headers=headers, data=payload)\n",
" sku_infos_response = json.loads(sku_infos_request.text)\n",
" skus_info = sku_infos_response['_embedded']['documentdetails']\n",
" print(skus_info)\n",
" #number of SKUs on invoice\n",
" self.number_skus = len(skus_info)\n",
" sku_ids = Test.extract_sku_matrix(skus_info, 'documentDetailId')\n",
" sku_qtys = Test.extract_sku_matrix(skus_info, 'quantity')\n",
" sku_unit_prices = Test.extract_sku_matrix(skus_info, 'unitPrice')\n",
" sku_adjusted_qty = Test.extract_sku_matrix(skus_info, 'adjustedQuantity')\n",
" sku_adjusted_prices = Test.extract_sku_matrix(skus_info, 'adjustedUnitPrice')\n",
" #populating df object with data below. \n",
" arr = np.transpose(np.array([sku_ids, sku_qtys, sku_unit_prices, sku_adjusted_qty, sku_adjusted_prices]))\n",
" self.sku_table = Invoice.matrix_to_df(data = arr)\n",
" self.sku_table['documentDetailId'] = self.sku_table['documentDetailId'].astype('int32')\n",
"\n",
" \n",
"\n",
" def get_po_details(self):\n",
" #array containing each SKU's information\n",
" po_df = pd.DataFrame(columns = ['po_detail_id', 'sku_number', 'sku_description', 'po_price'])\n",
" for sku_id in self.sku_table.documentDetailId:\n",
" sku_po_url = f'https://test.apiclient.com/CHPAPO/v2/documentdetails/{int(sku_id)}/podetail'\n",
" po_sku_request = requests.request(\"GET\", sku_po_url, headers=headers, data=payload)\n",
" po_sku_response = json.loads(po_sku_request.text)\n",
" po_detail_id, sku_number, sku_description, po_price = po_sku_response['poDetailId'], po_sku_response['itemNumber'], po_sku_response['itemDescription'], po_sku_response['unitPrice']\n",
" d = {}\n",
" d['po_detail_id'] = po_detail_id\n",
" d['sku_number'] = sku_number\n",
" d['sku_description'] = sku_description\n",
" d['po_price'] = po_price\n",
" po_df = po_df.append(d, ignore_index=True)\n",
" # join resulting df to self.pandas df\n",
" #There are no keys (that I can tell on the API) which link PO detail to SKU. Ask John why\n",
"\n",
" self.sku_table = self.sku_table.join(po_df)\n",
"\n",
" \n",
"\n",
" def get_receiving_details(self):\n",
" #array containing each SKU's information\n",
" rec_df = pd.DataFrame(columns = ['detail_id', 'rec_qty'])\n",
" for sku_id in self.sku_table.documentDetailId:\n",
" sku_receiving_url = f'https://test.apiclient.com/CHPAPO/v2/documentdetails/{int(sku_id)}/receivingdetail'\n",
" sku_rec_request = requests.request(\"GET\", sku_receiving_url, headers=headers, data=payload)\n",
" sku_rec_response = json.loads(sku_rec_request.text)\n",
" detail_id, rec_qty = sku_rec_response['receivingDetailId'], sku_rec_response['receivingQuantity']\n",
" d = {}\n",
" d['po_detail_id'] = detail_id\n",
" d['rec_qty'] = rec_qty\n",
" rec_df = rec_df.append(d, ignore_index=True)\n",
" # join resulting df to self.pandas df\n",
" self.sku_table = self.sku_table.join(rec_df, rsuffix='r', lsuffix='l')\n",
"\n",
" \n",
"\n",
" def calculate_cost(self):\n",
" self.sku_table['adjustedQuantity'] = self.sku_table['rec_qty']\n",
" self.sku_table['extended_sku_cost'] = self.sku_table['quantity'] * self.sku_table['unitPrice']\n",
" self.sku_table['adjustedUnitPrice'] = self.sku_table['extended_sku_cost'] / self.sku_table['adjustedQuantity']\n",
" self.sku_table['adjustedQuantity'] = self.sku_table['rec_qty']\n",
" self.sku_table['extended_sku_cost'] = self.sku_table['quantity'] * self.sku_table['unitPrice']\n",
" self.sku_table['adjustedUnitPrice'] = self.sku_table['extended_sku_cost'] / self.sku_table['adjustedQuantity']\n",
" self.sku_table['ext_po_cost']= self.sku_table['po_price'] * self.sku_table['rec_qty']\n",
" self.sku_table['percent_difference'] = abs(self.sku_table['ext_po_cost'] - self.sku_table['extended_sku_cost']) / (self.sku_table['extended_sku_cost'])\n",
"\n",
" \n",
" def send_vals(self):\n",
" #need to figure out how to actually send values with the correct syntax. \n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"testing_lir = Testing_LIR()\n",
"invoice = Invoice(0)\n",
"invoice.get_job_info() #works\n",
"invoice.get_api_sku_info() #works, legend\n",
"invoice.get_po_details() #works\n",
"invoice.get_receiving_details() #works\n",
"invoice.calculate_cost()\n",
"test_df = invoice.sku_table\n",
"test_df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"url = https://test.apiclient.com/CHPAPO/v2/documentdetails/202722\n",
"\n",
" \n",
"\n",
"payload = json.dumps({\n",
"\n",
" \"path\": \"/adjustedUnitPrice\",\n",
"\n",
" \"op\": \"add\",\n",
"\n",
" \"value\": 3\n",
"\n",
"})\n",
"\n",
"headers = {\n",
"\n",
" 'acc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\n",
"\n",
" 'Content-Type': 'application/json'\n",
"\n",
"}\n",
"\n",
" \n",
"\n",
"response = requests.request(\"PATCH\", url, headers=headers, data=payload)\n",
"\n",
" \n",
"\n",
"response.text"
]
}
]
}