diff --git a/invoice_az/api.py b/invoice_az/api.py index f5c3229..ed845a7 100644 --- a/invoice_az/api.py +++ b/invoice_az/api.py @@ -1745,11 +1745,14 @@ 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 @@ -1760,6 +1763,7 @@ def match_similar_items(): 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 = {} @@ -1767,6 +1771,7 @@ def match_similar_items(): 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 = [] @@ -1778,19 +1783,25 @@ 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) @@ -1801,6 +1812,7 @@ 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 @@ -1818,7 +1830,7 @@ def match_similar_items(): # Log high matches (more than 0.8) if name_score > 0.8: - print("test") + 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: @@ -1827,6 +1839,7 @@ def match_similar_items(): 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 @@ -1837,10 +1850,12 @@ 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, @@ -1854,14 +1869,17 @@ 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: - print("test") + 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, @@ -1871,6 +1889,7 @@ 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."