From 8b5f87619cfa41d1ad62177be3b3cd49e751e5c0 Mon Sep 17 00:00:00 2001 From: bcomsugi Date: Tue, 11 Mar 2025 06:50:39 +0700 Subject: [PATCH] add change to notes and minor change in server.py --- QBClass/server.py | 10 +++- change_to_notes.py | 137 +++++++++++++++++++++++++++++++++++++++++++++ exim.py | 5 +- 3 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 change_to_notes.py diff --git a/QBClass/server.py b/QBClass/server.py index c120b54..f678594 100644 --- a/QBClass/server.py +++ b/QBClass/server.py @@ -95,8 +95,10 @@ class baseQBQuery: ticket = sessionManager.BeginSession("", 2) # Send query and receive response - self.response_string = sessionManager.ProcessRequest(ticket, self.QBXML) - + if not qbxml_query: + self.response_string = sessionManager.ProcessRequest(ticket, self.QBXML) + else: + self.response_string = sessionManager.ProcessRequest(ticket, qbxml_query) # Disconnect from Quickbooks sessionManager.EndSession(ticket) # Close the company file sessionManager.CloseConnection() # Close the connection @@ -107,7 +109,9 @@ class baseQBQuery: self.response_string = xml.toprettyxml() # print(f'{self.response_string = }') - self.statusOk = self.isDataOK() + if not qbxml_query: + self.statusOk = self.isDataOK() + return self.statusOk return self.response_string diff --git a/change_to_notes.py b/change_to_notes.py new file mode 100644 index 0000000..a4ba555 --- /dev/null +++ b/change_to_notes.py @@ -0,0 +1,137 @@ +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) = }') \ No newline at end of file diff --git a/exim.py b/exim.py index fc1e562..8a3bea8 100644 --- a/exim.py +++ b/exim.py @@ -570,7 +570,8 @@ if __name__=='__main__': print(fromtxndate) readydata = main(fromtxndate, totxndate, 2) - # print(readydata) + # pprint(readydata, sort_dicts=False) - IA = InvoiceAdd(*readydata, debug=False) + IA = InvoiceAdd(*readydata, debug=True) + print(IA.create_QBXML()) # IA.all()