Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.6 kB
0
Indexable
Never
import json


def invoiceConversion(invoice_dict):

    if invoice_dict['tax-amount'] == "":
      tax_amount = 0.000
    else:
      tax_amount = round(float(invoice_dict['tax-amount']),3)
    
    svb_vchr_line = [{
        "VOUCHER_LINE_NUM": invoice_line['line-num'],
        "DESCR": invoice_line['description'],
        "MERCHANDISE_AMT": round(float(invoice_line['total']), 3),
        "UNIT_PRICE": round(float(invoice_line['price']), 3),
        "CUSTOM_DATE_C1": invoice_line['prepaid-start-date'][0:10] if invoice_line['prepaid-start-date']  else "",
        "CUSTOM_DATE_C2": invoice_line['prepaid-end-date'][0:10] if invoice_line['prepaid-end-date'] else "",
        **({
            "BUSINESS_UNIT_GL": invoice_line['account']['segment-1'],
            "ACCOUNT": invoice_line['account']['segment-2'],
            "PRODUCT": invoice_line['account']['segment-5'],
            "DEPTID": invoice_line['account']['segment-3'],
            "PROJECT_ID": invoice_line['account']['segment-4'],
        } if 'account' in invoice_line else {
        "BUSINESS_UNIT_GL": invoice_line['account-allocations'][1]['account-allocation'][1]['account']['segment-1'],
        "ACCOUNT": invoice_line['account-allocations'][1]['account-allocation'][1]['account']['segment-2'],
        "PRODUCT": invoice_line['account-allocations'][1]['account-allocation'][1]['account']['segment-5'],
        "DEPTID": invoice_line['account-allocations'][1]['account-allocation'][1]['account']['segment-3'],
        "PROJECT_ID": invoice_line['account-allocations'][1]['account-allocation'][1]['account']['segment-4'],
        }),
        "FIELDVAL3_0": invoice_line['project-activity-id'][0:15] if invoice_line['project-activity-id'] else "",
        "SVB_ACCTG_TOTAL": invoice_line['accounting-total'],
        "SVB_ACCTG_CURRENCY": invoice_line['accounting-total-currency']['code'] if invoice_line['accounting-total-currency'] else "",
        "SVB_DATE_1_0": invoice_line['custom-fields']['service-period-start-date'][1:10] if invoice_line['custom-fields']['service-period-start-date'] else "",
        "SVB_DATE_2_0": invoice_line['custom-fields']['service-period-end-date'][1:10] if invoice_line['custom-fields']['service-period-end-date'] else "",
        "USER_VCHR_DATE": invoice_line['tax-supply-date'],
        "CUSTOM_C100_B1": invoice_line['tax-code']['code'] if 'tax-code' in invoice_line else "",
        "CUSTOM_C100_B2": invoice_line['tax-rate'],
        "CUSTOM_C100_B3": invoice_line['tax-amount'],
        "CUSTOM_C100_B4": invoice_line['tax-description'],
        "CUSTOM_DATE_B": invoice_line['in-service-date'][0:10] if invoice_line['in-service-date'] else "",
        "CUSTOM_C1_B": "Y" if invoice_line['prepaid--fixed-asset'] != '' else "N",
        **({"SVB_VCHR_DIST":  { 
            "DISTRIB_LINE_NUM": account_allocation['id'],
            "BUSINESS_UNIT_GL_0": account_allocation['account']['segment-1'],
            "ACCOUNT_0": account_allocation['account']['segment-2'],
            "DEPTID_0": account_allocation['account']['segment-3'],
            "MERCHANDISE_AMT_0": account_allocation['amount'],
            "PO_ID_1": invoice_line['order-header-num'],
            "LINE_NBR": invoice_line['order-line-num'],
            "PRODUCT_0": account_allocation['account']['segment-5'],
            "AFFILIATE_0": account_allocation['account']['segment-6'],
            "PROJECT_ID_0": account_allocation['account']['segment-4']
        } for account_allocation in invoice_line['account-allocations']['account-allocation'] 
        } if 'account-allocation' in invoice_line['account-allocations'] else {})
         } for invoice_line in invoice_dict['invoice-lines']
        ]

    result = {
        "BUSINESS_UNIT": invoice_dict['ap-business-unit']['name'] if invoice_dict['ap-business-unit'] else "",
        "VOUCHER_ID": "C"+str(invoice_dict['id']),
        "VENDOR_ID": invoice_dict['supplier']['number'],
        "VCHR_BLD_KEY_C1": 1,
        "VCHR_BLD_KEY_C2": 1,
        "VCHR_BLD_KEY_N2": 1,
        "INVOICE_ID": invoice_dict['invoice-number'],
        "INVOICE_DT": invoice_dict['invoice-date'][0:10],
        "ADDRESS_SEQ_NUM": invoice_dict['supplier']['primary-contact']['id'],
        "REMIT_ADDR_SEQ_NUM": invoice_dict['remit-to-address']['remit-to-code'] if 'remit-to-address' in invoice_dict else "",
        "OPRID": invoice_dict['created-by']['login'][0:10],
        "GROSS_AMT": round(float(invoice_dict['shipping-amount']), 3)+round(float(invoice_dict['handling-amount']), 3)+tax_amount+round(float(invoice_dict['misc-amount']), 3),
        "SALETX_AMT": tax_amount,
        "FREIGHT_AMT": round(float(invoice_dict['shipping-amount']),3)+round(float(invoice_dict['handling-amount']),3),
        "MISC_AMT": round(float(invoice_dict['misc-amount']),3),
        "PYMNT_TERMS_CD": invoice_dict['payment-term']['code'].replace(" ", "") if 'payment-term' in invoice_dict else "",
        "ENTERED_DT": invoice_dict['created-at'][0:10],
        "TXN_CURRENCY_CD": invoice_dict['currency']['code'],
        "DESCR254_MIXED": invoice_dict['fas-comments'],
        "INV_RECPT_DT": invoice_dict['invoice-received-date'][0:10] if invoice_dict['invoice-received-date'] else "",
        "LC_ID": invoice_dict['fas-location']['name'] if invoice_dict['fas-location'] else "",
        "SVB_SOURCE": "COUPA",
        "SVB_SRC_UNIQUE_ID": invoice_dict['id'],
        "VCHR_PROCESS_FLG": "N",
        "FIELDVAL5": invoice_dict['supplier-note'],
       "SVB_VCHR_LINE": svb_vchr_line   
    }
    return result


def getBusinessAndInvoiceId(response):
    result = {}
    errorInConversion = {}
    json_load = json.loads(response, strict=False)
    for i in json_load:
        try:
            payload = invoiceConversion(i)
            if i['ap-business-unit']:
                result[i['id']] = {"business_id": i['ap-business-unit']['name'], "payload": payload}
            else:
                errorInConversion[i['id']] = "Business Unit Not Found"
        except Exception as err:
            print(err.__str__, dir(err))
            errorInConversion[i['id']] = f"Exception Error: {str(err)}"
    return result, errorInConversion


def getError(response):
    result = {"error": ""}
    error = json.loads(response)['CIFault']['IBResponse']
    try:
        for i in error:
            if i['type'] == 'Message':
                result['message'] = error['DefaultMessage']
    except Exception as err:
        result['error'] = str(err)
    return result