from QBClass.QBClasses import CustomerQuery import pprint import json import xmltodict # cust = CustomerQuery(MaxReturned = 90, OwnerID = 0, IncludeRetElement = ['ListID', 'Name', 'FullName', 'EditSequence', 'CompanyName', 'ShipAddress','ShipToAddress', 'CreditLimit', 'PriceLevelRef', 'AdditionalNotesRet', 'Notes', 'DataExtRet']) cust = CustomerQuery(OwnerID = 0, IncludeRetElement = ['ListID', 'Name', 'FullName', 'EditSequence', 'CompanyName', 'ShipAddress','ShipToAddress', 'CreditLimit', 'PriceLevelRef', 'AdditionalNotesRet', 'Notes', 'DataExtRet']) # pprint.pprint(cust.all(), sort_dicts=False) # print(cust.response_string) print(cust.all()) print(type(cust.all())) # pprint.pprint(cust.filter('DataExtRet').all(), sort_dicts=False) beeer = json.loads(json.dumps(cust.all())) pprint.pprint( beeer , sort_dicts=False) if not isinstance(cust.all(), list): customers = [cust.all()] else: customers = cust.all() notes_create = [] notes_update = [] for customer in customers: DER_note = {} if 'DataExtRet' in customer: # notes = {customer['ListID']:[]} if not isinstance(customer['DataExtRet'], list): customer['DataExtRet'] = [customer['DataExtRet']] for DER in customer['DataExtRet']: Der_name = DER['DataExtName'] Der_value = DER['DataExtValue'] DER_note[Der_name] = Der_value print(f'{DER_note = }') note_json = json.dumps(DER_note, ensure_ascii=False) print(note_json) is_new = True noteid_counter = 0 if DER_note and 'AdditionalNotesRet' in customer: print(f"{customer['AdditionalNotesRet'] = }") # print(f"dernote and additional note ret {customer['FullName']}") if not isinstance(customer['AdditionalNotesRet'], list): customer['AdditionalNotesRet'] = [customer['AdditionalNotesRet']] print(f"{customer['FullName'] = } {customer['AdditionalNotesRet'] = }") for cur_note in customer['AdditionalNotesRet']: cur_noteid = cur_note['NoteID'] if float(noteid_counter)<=float(cur_noteid): noteid_counter = str(int(float(cur_noteid)+1)) cur_date = cur_note['Date'] cur_note_json = cur_note['Note'] try: cur_note = json.loads(cur_note_json) except Exception as e: continue print(f'{cur_noteid = } {cur_note_json = } {len(cur_note) = } {len(DER_note) = }') if len(cur_note)!=len(DER_note): #then update notes # note_update = note_json print('len(cur_note)!=len(DER_note)') notes_create.append({customer['ListID']:{'ListID':customer['ListID'], 'EditSequence':customer['EditSequence'], 'AdditionalNotesMod':{'NoteID':int(float(cur_noteid)), 'Note':note_json}}}) is_new=False break if json.dumps(cur_note)!=note_json: print('cur_note!=note_json') # print(cur_note) # print(json.dumps(cur_note)) # print(note_json) notes_create.append({customer['ListID']:{'ListID':customer['ListID'], 'EditSequence':customer['EditSequence'], 'AdditionalNotesMod':{'NoteID':int(float(cur_noteid)), 'Note':note_json}}}) is_new = False break is_new=False if is_new: print('is_new') notes_create.append({customer['ListID']:{'ListID':customer['ListID'], 'EditSequence':customer['EditSequence'], 'AdditionalNotesMod':{'NoteID':int(float(noteid_counter)), 'Note':note_json}}}) elif DER_note and 'AdditionalNotesRet' not in customer: print("DER_note and 'AdditionalNotesRet' not in customer") notes_create.append({customer['ListID']:{'ListID':customer['ListID'], 'EditSequence':customer['EditSequence'], 'Notes':note_json}}) # if is_new: print(f'{notes_create=} {len(notes_create) = }') if notes_create: customer_mods = [] total_notes_create = len(notes_create) max_transaction_len = len(notes_create) repeat = int(total_notes_create/max_transaction_len) leftover = total_notes_create%max_transaction_len cannot_update = [] for x in range(repeat): xml_body="" customer_mods = [] print(f'repeat ke : {x=} {x*max_transaction_len=} {x*max_transaction_len+max_transaction_len=}') for idx, notecreate in enumerate(notes_create[x*max_transaction_len:x*max_transaction_len+max_transaction_len]): # print(list(notecreate.keys())[0]) print(f'{idx = }') xml = xmltodict.unparse(notecreate, pretty=True) # print(xml) # print(xml.split('\n')[2:-1]) joined_xml = "".join(xml.split('\n')[2:-1]) # print(f'{joined_xml = }') # pprint.pprint(joined_xml, sort_dicts=False) # print(f'{joined_xml = }') single_customermod = f'\n {joined_xml}\n' customer_mods.append(single_customermod) # print(f'\n {joined_xml}\n<\CustomerMod>') # for cur_item in cur_note: # print(f'{customer_mods = }') xml_body = '\n'.join(customer_mods) xml_body = "" + xml_body + "" # print(f'{xml_body = }') xml_header = """ """ xml_footer = """ """ qbxml = xml_header+xml_body+xml_footer # print(f'{qbxml = }') print(qbxml) try: cust.connect_to_quickbooks(qbxml) except Exception as e: cannot_update.append(xml_body) # print(cust.response_string) print(f'{cannot_update = }, {len(cannot_update) = }') no_notes=[] for x in cust.all(): if 'Notes' not in x: no_notes.append(x['FullName']) print(f'{no_notes = }, {len(no_notes) = }')