add something to massage some of the data

This commit is contained in:
Casey 2026-01-09 14:53:06 -06:00
parent 2f1c975e0a
commit 89bdbcfdcb
4 changed files with 121 additions and 9 deletions

View file

@ -383,10 +383,13 @@ def update_address_fields():
'custom_estimate_sent_status': 0,
'custom_job_status': 0,
'custom_payment_received_status': 0,
'address_linked_to_customer': 0,
'total_field_updates': 0,
'addresses_updated': 0,
'quotations_updated': 0,
'sales_orders_updated': 0
'sales_orders_updated': 0,
'customers_updated': 0,
'contacts_updated': 0
}
onsite_meta = frappe.get_meta("On-Site Meeting")
@ -401,7 +404,7 @@ def update_address_fields():
# Print a three-line, refreshing progress block without adding new lines each loop
progress_line = f"📊 Progress: [{bar}] {progress_percentage:3d}% ({index}/{total_doctypes})"
counters_line = f" Fields updated: {field_counters['total_field_updates']} | DocTypes updated: {field_counters['addresses_updated'] + field_counters['quotations_updated'] + field_counters['sales_orders_updated']}"
counters_line = f" Fields updated: {field_counters['total_field_updates']} | DocTypes updated: {field_counters['addresses_updated'] + field_counters['quotations_updated'] + field_counters['sales_orders_updated'] + field_counters['customers_updated']}"
detail_line = f" Processing: {doc['name'][:40]}..."
if index == 1:
@ -498,6 +501,26 @@ def update_address_fields():
elif sales_invoices and sales_invoices[0]:
payment_received = "In Progress"
customer_name = getattr(address_doc, 'custom_customer_to_bill', None)
links = address_doc.get("links", [])
customer_links = [link for link in links if link.link_doctype == "Customer"]
needs_link_update = False
if customer_name and frappe.db.exists("Customer", customer_name):
customer_doc = frappe.get_doc("Customer", customer_name)
# Check if address needs link update
if not customer_links:
needs_link_update = True
if not address_doc.name in [link.address_name for link in customer_doc.get("custom_select_address", [])]:
customer_doc.append("custom_select_address", {
"address_name": address_doc.name
})
customer_doc.save(ignore_permissions=True)
field_counters['customers_updated'] += 1
field_counters['total_field_updates'] += 1
if getattr(address_doc, 'custom_onsite_meeting_scheduled', None) != onsite_meeting:
updates['custom_onsite_meeting_scheduled'] = onsite_meeting
field_counters['custom_onsite_meeting_scheduled'] += 1
@ -518,6 +541,21 @@ def update_address_fields():
if updates:
frappe.db.set_value("Address", doc['name'], updates)
field_counters['addresses_updated'] += 1
# Handle address links after db.set_value to avoid timestamp mismatch
if needs_link_update:
# Reload the document to get the latest version
address_doc = frappe.get_doc("Address", doc['name'])
address_doc.append("links", {
"link_doctype": "Customer",
"link_name": customer_name
})
address_doc.save(ignore_permissions=True)
field_counters['address_linked_to_customer'] += 1
field_counters['total_field_updates'] += 1
# Commit every 100 records to avoid long transactions
if index % 100 == 0:
@ -530,6 +568,7 @@ def update_address_fields():
print(f" • Addresses updated: {field_counters['addresses_updated']:,}")
print(f" • Quotations updated: {field_counters['quotations_updated']:,}")
print(f" • Sales Orders updated: {field_counters['sales_orders_updated']:,}")
print(f" • Customers updated: {field_counters['customers_updated']:,}")
print(f" • Total field updates: {field_counters['total_field_updates']:,}")
print(f"\n📝 Field-specific updates:")
print(f" • Full Address: {field_counters['full_address']:,}")