test commit
This commit is contained in:
parent
41aff209e8
commit
ea621fa182
|
|
@ -1745,14 +1745,11 @@ def match_similar_items():
|
|||
# Get active settings
|
||||
settings = get_active_settings()
|
||||
if not settings:
|
||||
frappe.log_error("No active E-Taxes settings found", "E-Taxes Item Matching")
|
||||
return {
|
||||
'success': False,
|
||||
'message': 'No active E-Taxes settings found'
|
||||
}
|
||||
|
||||
frappe.log_error(f"Using settings: {settings.name}, similarity_threshold: {settings.similarity_threshold}", "E-Taxes Item Matching")
|
||||
|
||||
# Get similarity threshold from settings
|
||||
similarity_threshold = float(settings.similarity_threshold) / 100.0 if settings.similarity_threshold else 0.8
|
||||
consider_azeri = settings.consider_azeri_chars
|
||||
|
|
@ -1762,17 +1759,11 @@ def match_similar_items():
|
|||
filters={'status': 'New'},
|
||||
fields=['name', 'etaxes_item_name', 'etaxes_item_code'])
|
||||
|
||||
# Log items
|
||||
frappe.log_error(f"Found {len(etaxes_items)} E-Taxes items with 'New' status", "E-Taxes Item Matching")
|
||||
|
||||
# Get existing mappings
|
||||
existing_mappings = {}
|
||||
for mapping in settings.item_mappings:
|
||||
existing_mappings[mapping.etaxes_item_name] = mapping.erp_item
|
||||
|
||||
# Log existing mappings
|
||||
frappe.log_error(f"Found {len(existing_mappings)} existing mappings", "E-Taxes Item Matching")
|
||||
|
||||
# Get items with empty mapping (exist in table but erp_item is None or empty)
|
||||
items_with_empty_mapping = []
|
||||
for etaxes_id, erp_item in existing_mappings.items():
|
||||
|
|
@ -1783,25 +1774,17 @@ def match_similar_items():
|
|||
items_with_empty_mapping.append(item)
|
||||
break
|
||||
|
||||
frappe.log_error(f"Found {len(items_with_empty_mapping)} items with empty mappings", "E-Taxes Item Matching")
|
||||
|
||||
# Filter only those that are not in mappings
|
||||
unmapped_items = []
|
||||
for item in etaxes_items:
|
||||
if item.name not in existing_mappings:
|
||||
unmapped_items.append(item)
|
||||
|
||||
frappe.log_error(f"Found {len(unmapped_items)} completely unmapped items", "E-Taxes Item Matching")
|
||||
|
||||
# Combine two lists: items without mappings and items with empty mappings
|
||||
items_to_process = unmapped_items + items_with_empty_mapping
|
||||
|
||||
# Log final list of items to process
|
||||
frappe.log_error(f"Total items to process: {len(items_to_process)}", "E-Taxes Item Matching")
|
||||
|
||||
# Get all items from system
|
||||
system_items = frappe.get_all('Item', fields=['name', 'item_name', 'item_code'])
|
||||
frappe.log_error(f"Found {len(system_items)} system items for matching", "E-Taxes Item Matching")
|
||||
|
||||
matched_count = 0
|
||||
total_processed = len(items_to_process)
|
||||
|
|
@ -1811,9 +1794,6 @@ def match_similar_items():
|
|||
|
||||
# Process all items
|
||||
for etaxes_item in items_to_process:
|
||||
# Log item being processed
|
||||
frappe.log_error(f"Processing item: {etaxes_item.etaxes_item_name} (ID: {etaxes_item.name})", "E-Taxes Item Matching")
|
||||
|
||||
# Find similar item
|
||||
best_match = None
|
||||
best_score = 0
|
||||
|
|
@ -1828,19 +1808,12 @@ def match_similar_items():
|
|||
# Calculate similarity coefficient
|
||||
name_score = SequenceMatcher(None, etaxes_name, item_name).ratio()
|
||||
|
||||
# Log high matches (more than 0.8)
|
||||
if name_score > 0.8:
|
||||
frappe.log_error(f"High similarity match found: '{etaxes_item.etaxes_item_name}' vs '{item.item_name}' - Score: {name_score:.3f}", "E-Taxes Item Matching")
|
||||
|
||||
# Check if this is a better match
|
||||
if name_score > best_score and name_score >= similarity_threshold:
|
||||
best_score = name_score
|
||||
best_match = item
|
||||
|
||||
if best_match:
|
||||
# Log found match
|
||||
frappe.log_error(f"Best match found for '{etaxes_item.etaxes_item_name}': '{best_match.item_name}' with score {best_score:.3f}", "E-Taxes Item Matching")
|
||||
|
||||
# IMPORTANT: check if there is already a record for this item in mappings
|
||||
existing_row = None
|
||||
for idx, mapping in enumerate(doc.item_mappings):
|
||||
|
|
@ -1850,12 +1823,10 @@ def match_similar_items():
|
|||
|
||||
if existing_row is not None:
|
||||
# If row already exists, update its erp_item value
|
||||
frappe.log_error(f"Updating existing mapping row {existing_row} for item {etaxes_item.name}", "E-Taxes Item Matching")
|
||||
doc.item_mappings[existing_row].erp_item = best_match.name
|
||||
doc.item_mappings[existing_row].mapping_type = 'Automatic'
|
||||
else:
|
||||
# If no row, add a new one
|
||||
frappe.log_error(f"Adding new mapping for item {etaxes_item.name}", "E-Taxes Item Matching")
|
||||
doc.append('item_mappings', {
|
||||
'etaxes_item_name': etaxes_item.name,
|
||||
'erp_item': best_match.name,
|
||||
|
|
@ -1869,17 +1840,10 @@ def match_similar_items():
|
|||
item_doc.status = 'Mapped'
|
||||
item_doc.mapped_item = best_match.name
|
||||
item_doc.save()
|
||||
frappe.log_error(f"Updated E-Taxes Item {etaxes_item.name} status to 'Mapped'", "E-Taxes Item Matching")
|
||||
else:
|
||||
frappe.log_error(f"No suitable match found for '{etaxes_item.etaxes_item_name}' (threshold: {similarity_threshold})", "E-Taxes Item Matching")
|
||||
|
||||
# Save settings only if matches were found
|
||||
if matched_count > 0:
|
||||
doc.save()
|
||||
frappe.log_error(f"Saved {matched_count} new mappings to settings document", "E-Taxes Item Matching")
|
||||
|
||||
# Log final result
|
||||
frappe.log_error(f"Matching completed: {matched_count} matched out of {total_processed} items processed", "E-Taxes Item Matching")
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
|
|
@ -1889,12 +1853,12 @@ def match_similar_items():
|
|||
}
|
||||
|
||||
except Exception as e:
|
||||
frappe.log_error(f"Error in match_similar_items: {str(e)}", "E-Taxes Item Matching Error")
|
||||
return {
|
||||
'success': False,
|
||||
'message': "An unknown error occurred, please try again in a few minutes."
|
||||
}
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
def match_similar_parties():
|
||||
"""Matching parties by similar names"""
|
||||
|
|
@ -1923,26 +1887,17 @@ def match_similar_parties():
|
|||
fields=['name', 'etaxes_party_name', 'etaxes_tax_id',
|
||||
'etaxes_party_type', 'erp_party_type'])
|
||||
|
||||
# Log parties
|
||||
# frappe.log_error(f"E-Taxes parties with status New: {etaxes_parties}", "Match Parties Log")
|
||||
|
||||
# Get existing mappings
|
||||
existing_mappings = {}
|
||||
for mapping in settings.party_mappings:
|
||||
existing_mappings[mapping.etaxes_party_name] = mapping.erp_party
|
||||
|
||||
# Log existing mappings
|
||||
# frappe.log_error(f"Existing mappings: {existing_mappings}", "Match Parties Log")
|
||||
|
||||
# Filter only those that are not in mappings or have empty mappings
|
||||
parties_to_process = []
|
||||
for party in etaxes_parties:
|
||||
if party.etaxes_party_name not in existing_mappings or not existing_mappings[party.etaxes_party_name]:
|
||||
parties_to_process.append(party)
|
||||
|
||||
# Log final list of parties to process
|
||||
# frappe.log_error(f"Parties to process: {parties_to_process}", "Match Parties Log")
|
||||
|
||||
matched_count = 0
|
||||
total_processed = len(parties_to_process)
|
||||
|
||||
|
|
@ -1954,9 +1909,6 @@ def match_similar_parties():
|
|||
# Determine ERP party type based on etaxes_party_type
|
||||
erp_party_type = etaxes_party.erp_party_type
|
||||
|
||||
# Log party being processed
|
||||
# frappe.log_error(f"Processing party: {etaxes_party.etaxes_party_name} (Type: {erp_party_type})", "Match Parties Log")
|
||||
|
||||
# Get all parties of corresponding type from system
|
||||
system_parties = frappe.get_all(erp_party_type,
|
||||
fields=['name',
|
||||
|
|
@ -1979,19 +1931,12 @@ def match_similar_parties():
|
|||
# Calculate similarity coefficient
|
||||
name_score = SequenceMatcher(None, etaxes_name, party_name_normalized).ratio()
|
||||
|
||||
# # Log high matches (more than 0.8)
|
||||
# if name_score > 0.8:
|
||||
# frappe.log_error(f"High score: '{etaxes_party.etaxes_party_name}' vs '{party_name}' = {name_score}", "Match Parties Log")
|
||||
|
||||
# Check if this is a better match
|
||||
if name_score > best_score and name_score >= similarity_threshold:
|
||||
best_score = name_score
|
||||
best_match = party
|
||||
|
||||
if best_match:
|
||||
# Log found match
|
||||
# frappe.log_error(f"Match found: '{etaxes_party.etaxes_party_name}' matches with '{best_match.name}' (score: {best_score})", "Match Parties Log")
|
||||
|
||||
# IMPORTANT: check if there is already a record for this party in mappings
|
||||
existing_row = None
|
||||
for idx, mapping in enumerate(doc.party_mappings):
|
||||
|
|
@ -2023,18 +1968,12 @@ def match_similar_parties():
|
|||
party_doc.mapped_party = best_match.name
|
||||
party_doc.save()
|
||||
except Exception as e:
|
||||
return
|
||||
else:
|
||||
# For parties that have no match, DO NOT change status
|
||||
return
|
||||
pass
|
||||
|
||||
# Save settings after adding all mappings
|
||||
if matched_count > 0:
|
||||
doc.save()
|
||||
|
||||
# Log final result
|
||||
# frappe.log_error(f"Final result: matched {matched_count} out of {total_processed} parties", "Match Parties Log")
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'matched_count': matched_count,
|
||||
|
|
@ -2043,7 +1982,6 @@ def match_similar_parties():
|
|||
}
|
||||
|
||||
except Exception as e:
|
||||
# frappe.log_error(frappe.get_traceback(), f"Error in match_similar_parties: {str(e)}")
|
||||
return {
|
||||
'success': False,
|
||||
'message': "An unknown error occurred, please try again in a few minutes."
|
||||
|
|
|
|||
Loading…
Reference in New Issue