123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- import pandas as pd
- import json
- import requests
- # import requests
- from requests_oauthlib import OAuth2Session
- from oauthlib.oauth2 import BackendApplicationClient
- from datetime import datetime
- domain = 'https://mappsacc.mazdaeur.com'
- # domain = 'https://mapps.mazdaeur.com' # Production Server
- webservice = domain + '/dogma-restapi-dms/api'
- module = '/vehicles/workshop/order-report'
- auth_url = domain + '/oauth/authorize'
- token_url = domain + '/oauth/token'
- redirect_uri = 'https://localhost/'
- client_id = 'E7FC943B-B73F-F48E-B71A-419EA4CD4AC7'
- client_secret = '^bH=rk@c58zrr^Apc#9fzy$c'
- username = 'mmd88888.cdk'
- password = 'MazdaCX30' # 'Der neue MX-30'
- dealer_number = '88888/MMD' # '11197/MMD' # Example '10030/MMD'
- # token = 'MDDq3CUd9ix0iSqR'
- base_dir = '/home/robert/projekte/python/mazda/'
- def date_format(d: datetime):
- date_str = d.isoformat(sep='T')
- if len(date_str) == 19:
- return date_str + '.000Z'
- return date_str[:-3] + 'Z'
- def convert_csv(csv_file, json_file, year, month):
- date_min = datetime(year, month, 1, 0, 0, 0)
- date_max = datetime(year, month + 1, 1, 0, 0, 0)
- date_cols = ['invoiceDate', 'orderDate', 'orderCompletionDate', 'vehicleIntakeDate', 'nextMotDueDate']
- df = pd.read_csv(csv_file, encoding='latin-1', decimal=',', sep=';', parse_dates=date_cols)
- # print(df[['currency','documentType','invoiceCategory','invoiceDate','invoiceNumber']].drop_duplicates().info())
- invoices = df[['currency', 'documentType', 'invoiceCategory', 'invoiceDate', 'invoiceNumber']].drop_duplicates().to_dict('records')
- invoice_items = df[['invoiceNumber', 'orderLineNumber', 'orderNumber', 'amount', 'discount', 'portion', 'unitPrice']].groupby('invoiceNumber')
- for invoice in invoices:
- invoice['invoiceDate'] = date_format(invoice['invoiceDate'])
- items = invoice_items.get_group(invoice['invoiceNumber'])
- items.pop('invoiceNumber')
- invoice['invoiceItems'] = items.to_dict('records')
- orders = df[['orderNumber', 'orderDate', 'orderCompletionDate', 'vehicleIntakeDate']].drop_duplicates().to_dict('records')
- orders_vehicle = df[['orderNumber', 'licensePlate', 'nextMotDueDate', 'odometer', 'odometerUnit', 'vin']].drop_duplicates().groupby('orderNumber')
- orders_items = df[[
- 'orderNumber', 'lineNumber', 'orderItemType',
- 'category', 'descriptionOperation', 'hours', 'operationCode', 'standardHours',
- 'descriptionOther', 'type',
- 'descriptionPart', 'isDamageCausal', 'manufacturer', 'partNumber', 'quantity', 'serialNumber', 'unit',
- 'company', 'descriptionPurchase', 'invoiceCode', 'invoiceDate', 'invoiceNumber'
- ]].drop_duplicates().groupby('orderNumber')
- for order in orders:
- order['vehicle'] = orders_vehicle.get_group(order['orderNumber']).to_dict('records')[0]
- order['vehicle']['nextMotDueDate'] = date_format(order['vehicle']['nextMotDueDate'])
- order['orderDate'] = date_format(order['orderDate'])
- order['orderCompletionDate'] = date_format(order['orderCompletionDate'])
- order['vehicleIntakeDate'] = date_format(order['vehicleIntakeDate'])
- items = orders_items.get_group(order['orderNumber']).to_dict('records')
- order['items'] = []
- for item in items:
- if item['orderItemType'] == 'operation':
- order['items'].append({
- 'lineNumber': item['lineNumber'],
- 'operation': {
- 'category': item['category'],
- 'description': item['descriptionOperation'],
- 'hours': item['hours'],
- 'operationCode': item['operationCode'],
- 'standardHours': item['standardHours']
- }
- })
- elif item['orderItemType'] == 'part':
- order['items'].append({
- 'lineNumber': item['lineNumber'],
- 'part': {
- 'description': item['descriptionPart'],
- 'isDamageCausal': item['isDamageCausal'],
- 'manufacturer': item['manufacturer'],
- 'partNumber': item['partNumber'],
- 'quantity': item['quantity'],
- 'serialNumber': item['serialNumber'],
- 'unit': item['unit']
- }
- })
- elif item['orderItemType'] == 'other':
- order['items'].append({
- 'lineNumber': item['lineNumber'],
- 'other': {
- 'description': item['descriptionOther'],
- 'type': item['type']
- }
- })
- else:
- order['items'].append({
- 'lineNumber': item['lineNumber'],
- 'purchaseInvoice': {
- 'company': item['company'],
- 'description': item['descriptionPurchase'],
- 'invoiceCode': item['invoiceCode'],
- 'invoiceDate': date_format(item['invoiceDate']),
- 'invoiceNumber': item['invoiceNumber']
- }
- })
- res = {
- 'creationDate': date_format(datetime.now()),
- 'invoices': invoices,
- 'orders': orders,
- 'timeRangeBegin': date_min,
- 'timeRangeEnd': date_max
- }
- json.dump(res, open(json_file, 'w'), indent=2)
- return res
- def upload(data):
- headers = {
- 'accept': 'application/vnd.mazdaeur.dms.v4+json',
- 'x-mme-organisation': dealer_number,
- 'X-mazda-org': dealer_number,
- 'Content-Type': 'application/json',
- # 'Authorization': 'Bearer ' + token
- }
- # client = BackendApplicationClient(client_id=client_id)
- oauth = OAuth2Session(client_id, redirect_uri=redirect_uri)
- authorization_url, state = oauth.authorization_url(auth_url)
- print('Please go here and authorize: ' + authorization_url)
- redirect_response = input('Paste the full redirect URL here:')
- token = oauth.fetch_token(token_url, client_secret=client_secret, authorization_response=redirect_response)
- # print(token)
- r = oauth.post(webservice + module, json.dumps(data), headers=headers)
- with open(base_dir + 'post_error.log', 'w') as fwh:
- fwh.write(r.text)
- def main():
- data = convert_csv(base_dir + 'Workshop_Order_Report.csv', base_dir + 'mazda_export.json', 2021, 6)
- # data = json.load(open(base_dir + 'mazda_export.json', 'r'))
- upload(data)
- if __name__ == '__main__':
- main()
|