fixed a bunch of bugs

This commit is contained in:
Ali 2025-05-26 16:03:16 +04:00
parent 56b24bc644
commit 57fc4ce046
3 changed files with 117 additions and 90 deletions

View File

@ -3524,120 +3524,147 @@ def load_units_from_invoices(date_from, date_to, max_count=200, offset=0):
# Обработчик для обновления статусов сопоставленных элементов
@frappe.whitelist()
def update_mapped_statuses(doc, method=None):
"""Update mapped statuses in E-Taxes DocTypes when settings are saved"""
try:
# 1. Handle E-Taxes Unit mappings
if hasattr(doc, 'unit_mappings'):
# Create a dictionary of current mappings
# Создаем словарь текущих соответствий
current_unit_mappings = {}
for mapping in doc.unit_mappings:
if mapping.etaxes_unit_name:
current_unit_mappings[mapping.etaxes_unit_name] = mapping.erp_unit
# Проверяем что erp_unit не None и не пустая строка
erp_unit = getattr(mapping, 'erp_unit', None)
current_unit_mappings[mapping.etaxes_unit_name] = erp_unit if erp_unit else None
# Update statuses for all mapped units
# Обновляем статусы для всех единиц в mappings
for etaxes_unit_name, erp_unit in current_unit_mappings.items():
try:
if frappe.db.exists('E-Taxes Unit', etaxes_unit_name):
if erp_unit: # If erp_unit has a value, set as Mapped
if erp_unit: # Если есть соответствие
frappe.db.set_value('E-Taxes Unit', etaxes_unit_name, {
'status': 'Mapped',
'mapped_unit': erp_unit
})
else: # If erp_unit is empty/None, set status back to New
}, update_modified=False)
else: # Если соответствие пустое
frappe.db.set_value('E-Taxes Unit', etaxes_unit_name, {
'status': 'New',
'mapped_unit': None
})
}, update_modified=False)
except Exception as e:
frappe.log_error(f"Error updating E-Taxes Unit {etaxes_unit_name}: {str(e)}", "Status Update Error")
# Find units that were previously mapped but are no longer in the mappings
# Сбрасываем статус для единиц, которые были удалены из mappings
try:
mapped_units = frappe.get_all('E-Taxes Unit',
filters={'status': 'Mapped'},
fields=['name', 'mapped_unit'])
for unit in mapped_units:
if unit.name not in current_unit_mappings:
# This unit was mapped before but is no longer in mappings
frappe.db.set_value('E-Taxes Unit', unit.name, {
'status': 'New',
'mapped_unit': None
})
}, update_modified=False)
except Exception as e:
frappe.log_error(f"Error resetting unmapped units: {str(e)}", "Status Update Error")
# 2. Handle E-Taxes Item mappings
if hasattr(doc, 'item_mappings'):
# Create a dictionary of current mappings
# Создаем словарь текущих соответствий
current_item_mappings = {}
for mapping in doc.item_mappings:
if mapping.etaxes_item_name:
current_item_mappings[mapping.etaxes_item_name] = mapping.erp_item
# Проверяем что erp_item не None и не пустая строка
erp_item = getattr(mapping, 'erp_item', None)
current_item_mappings[mapping.etaxes_item_name] = erp_item if erp_item else None
# Update statuses for all mapped items
# Обновляем статусы для всех товаров в mappings
for etaxes_item_name, erp_item in current_item_mappings.items():
try:
if frappe.db.exists('E-Taxes Item', etaxes_item_name):
if erp_item: # If erp_item has a value, set as Mapped
if erp_item: # Если есть соответствие
frappe.db.set_value('E-Taxes Item', etaxes_item_name, {
'status': 'Mapped',
'mapped_item': erp_item
})
else: # If erp_item is empty/None, set status back to New
}, update_modified=False)
else: # Если соответствие пустое
frappe.db.set_value('E-Taxes Item', etaxes_item_name, {
'status': 'New',
'mapped_item': None
})
}, update_modified=False)
except Exception as e:
frappe.log_error(f"Error updating E-Taxes Item {etaxes_item_name}: {str(e)}", "Status Update Error")
# Find items that were previously mapped but are no longer in the mappings
# Сбрасываем статус для товаров, которые были удалены из mappings
try:
mapped_items = frappe.get_all('E-Taxes Item',
filters={'status': 'Mapped'},
fields=['name', 'mapped_item'])
for item in mapped_items:
if item.name not in current_item_mappings:
# This item was mapped before but is no longer in mappings
frappe.db.set_value('E-Taxes Item', item.name, {
'status': 'New',
'mapped_item': None
})
}, update_modified=False)
except Exception as e:
frappe.log_error(f"Error resetting unmapped items: {str(e)}", "Status Update Error")
# 3. Handle E-Taxes Parties mappings
if hasattr(doc, 'party_mappings'):
# Create lookup for current party mappings
# Создаем словарь текущих соответствий
current_party_mappings = {}
for mapping in doc.party_mappings:
if mapping.etaxes_party_name:
current_party_mappings[mapping.etaxes_party_name] = mapping.erp_party
# Проверяем что erp_party не None и не пустая строка
erp_party = getattr(mapping, 'erp_party', None)
current_party_mappings[mapping.etaxes_party_name] = erp_party if erp_party else None
# Get all parties with their actual docname
# Получаем все контрагенты с их фактическими именами документов
try:
all_parties = frappe.get_all('E-Taxes Parties',
fields=['name', 'etaxes_party_name', 'status', 'mapped_party'])
# Create a lookup from etaxes_party_name to actual docname
# Создаем lookup от etaxes_party_name к фактическому имени документа
party_name_to_docname = {}
for party in all_parties:
party_name_to_docname[party.etaxes_party_name] = party.name
# Update statuses for all parties in current mappings
# Обновляем статусы для всех контрагентов в mappings
for etaxes_party_name, erp_party in current_party_mappings.items():
try:
if etaxes_party_name in party_name_to_docname:
docname = party_name_to_docname[etaxes_party_name]
if erp_party: # If mapping exists
if erp_party: # Если есть соответствие
frappe.db.set_value('E-Taxes Parties', docname, {
'status': 'Mapped',
'mapped_party': erp_party
})
else: # If mapping was cleared
}, update_modified=False)
else: # Если соответствие пустое
frappe.db.set_value('E-Taxes Parties', docname, {
'status': 'New',
'mapped_party': None
})
}, update_modified=False)
except Exception as e:
frappe.log_error(f"Error updating E-Taxes Party {etaxes_party_name}: {str(e)}", "Status Update Error")
# Reset status for parties that were mapped but are no longer in mappings
# Сбрасываем статус для контрагентов, которые были удалены из mappings
for party in all_parties:
try:
if party.status == 'Mapped' and party.etaxes_party_name not in current_party_mappings:
frappe.db.set_value('E-Taxes Parties', party.name, {
'status': 'New',
'mapped_party': None
})
}, update_modified=False)
except Exception as e:
frappe.log_error(f"Error resetting unmapped party {party.name}: {str(e)}", "Status Update Error")
# Commit changes
except Exception as e:
frappe.log_error(f"Error processing parties mappings: {str(e)}", "Status Update Error")
# Явно коммитим изменения
frappe.db.commit()
except Exception as e:

View File

@ -15,7 +15,7 @@ frappe.ui.form.on('E-Taxes Settings', {
after_save: function(frm) {
// Обновляем индикатор после сохранения
frm.page.set_indicator(__('Mappings updated'), 'green');
// frm.page.set_indicator(__('Mappings updated'), 'green');
// Показываем уведомление об успешном обновлении
// frappe.show_alert({
@ -24,9 +24,9 @@ frappe.ui.form.on('E-Taxes Settings', {
// }, 5);
// Снимаем индикатор через 2 секунды
setTimeout(function() {
frm.page.set_indicator();
}, 2000);
// setTimeout(function() {
// frm.page.set_indicator();
// }, 2000);
},

View File

@ -58,7 +58,7 @@
"fieldname": "similarity_threshold",
"fieldtype": "Percent",
"label": "Similarity Threshold (%)",
"default": 80,
"default": 95,
"description": "Minimum similarity percentage for automatic mapping"
},
{