diff --git a/jey_erp/custom/create_asset_categories.py b/jey_erp/custom/create_asset_categories.py index da8218c..b6853ab 100644 --- a/jey_erp/custom/create_asset_categories.py +++ b/jey_erp/custom/create_asset_categories.py @@ -1,12 +1,20 @@ import frappe def create_asset_categories(): - """Create default Asset Categories with accounts""" + """ + Создает категории активов с настройками счетов + """ + + # Проверка существования таблицы Company + if not frappe.db.table_exists("Company"): + frappe.log_error("Company table not found", "Asset Categories Patch") + return # Получаем первую компанию company = frappe.get_all("Company", limit=1) if not company: - frappe.throw("No company found in the system") + frappe.log_error("No company found in the system", "Asset Categories Patch") + return company_name = company[0].name @@ -24,42 +32,63 @@ def create_asset_categories(): "Sənaye, tikinti, nəqliyyat, rabitə, ticarət-məişət xidməti və digər xüsusi təyinatlı torpaq sahələri" ] - # Находим нужные счета по account_name (только название, без кода) - accumulated_depreciation = get_account_by_name( - "Tikililər üzrə yığılmış amortizasiya", - company_name - ) - depreciation_expense = get_account_by_name( - "Amortizasiya xərci", - company_name - ) - fixed_asset = get_account_by_name( - "Digər uzunmüddətli aktivlər", - company_name - ) + # Находим нужные счета + try: + accumulated_depreciation = get_account_by_name( + "Tikililər üzrə yığılmış amortizasiya", + company_name + ) + depreciation_expense = get_account_by_name( + "Amortizasiya xərci", + company_name + ) + fixed_asset = get_account_by_name( + "Digər uzunmüddətli aktivlər", + company_name + ) + except Exception as e: + frappe.log_error(f"Required accounts not found: {str(e)}", "Asset Categories Patch") + return # Создаем категории активов + created_count = 0 + skipped_count = 0 + for category_name in asset_categories: - if not frappe.db.exists("Asset Category", category_name): - asset_category = frappe.new_doc("Asset Category") - asset_category.asset_category_name = category_name - asset_category.enable_cwip_accounting = 0 - - # Добавляем accounts - asset_category.append("accounts", { - "company_name": company_name, - "fixed_asset_account": fixed_asset, - "accumulated_depreciation_account": accumulated_depreciation, - "depreciation_expense_account": depreciation_expense, - "capital_work_in_progress_account": "" - }) - - asset_category.insert(ignore_permissions=True) - frappe.db.commit() - - print(f"Created Asset Category: {category_name}") - else: - print(f"Asset Category already exists: {category_name}") + try: + if not frappe.db.exists("Asset Category", category_name): + asset_category = frappe.new_doc("Asset Category") + asset_category.asset_category_name = category_name + asset_category.enable_cwip_accounting = 0 + + # Добавляем accounts + asset_category.append("accounts", { + "company_name": company_name, + "fixed_asset_account": fixed_asset, + "accumulated_depreciation_account": accumulated_depreciation, + "depreciation_expense_account": depreciation_expense, + "capital_work_in_progress_account": "" + }) + + asset_category.insert(ignore_permissions=True) + created_count += 1 + + frappe.msgprint(f"Created Asset Category: {category_name}") + else: + skipped_count += 1 + except Exception as e: + frappe.log_error( + f"Failed to create Asset Category '{category_name}': {str(e)}", + "Asset Categories Patch" + ) + + # Коммитим все изменения разом + frappe.db.commit() + + # Выводим итоговую информацию + message = f"Asset Categories Patch completed. Created: {created_count}, Skipped: {skipped_count}" + print(message) + frappe.msgprint(message) def get_account_by_name(account_name, company): @@ -74,25 +103,6 @@ def get_account_by_name(account_name, company): ) if not account: - frappe.throw(f"Account not found: {account_name} for company {company}") + raise ValueError(f"Account not found: {account_name} for company {company}") return account - - -def prevent_delete_system_asset_categories(doc, method): - """Prevent deletion of system Asset Categories""" - system_categories = [ - "Binalar, tikililər və qurğular", - "Maşınlar və avadanlıqlar", - "Yüksək texnologiyalar məhsulu olan hesablama texnikası", - "Nəqliyyat vasitələri", - "Digər əsas vəsaitlər", - "Düzən torpaqlar", - "Dağ torpaqlar", - "Orta və yüksək dağ torpaqlar", - "Şəhərin mövcud hüdudundan kənarda olan kənd təsərrüfatı təyinatlı torpaqlar", - "Sənaye, tikinti, nəqliyyat, rabitə, ticarət-məişət xidməti və digər xüsusi təyinatlı torpaq sahələri" - ] - - if doc.name in system_categories: - frappe.throw(f"System category '{doc.name}' cannot be deleted") \ No newline at end of file diff --git a/jey_erp/hooks.py b/jey_erp/hooks.py index 687e23a..47009e5 100644 --- a/jey_erp/hooks.py +++ b/jey_erp/hooks.py @@ -63,8 +63,8 @@ def after_migrate_combined(): show_item_tax_template_in_sales_invoice() from jey_erp.custom.vat_calculations import patch_sales_documents patch_sales_documents() - # from jey_erp.custom.create_asset_categories import create_asset_categories - # create_asset_categories() + from jey_erp.custom.create_asset_categories import create_asset_categories + create_asset_categories() fixtures = [ diff --git a/jey_erp/patches.txt b/jey_erp/patches.txt index ad6ef03..f15c3a9 100644 --- a/jey_erp/patches.txt +++ b/jey_erp/patches.txt @@ -3,5 +3,4 @@ # Read docs to understand patches: https://frappeframework.com/docs/v14/user/en/database-migrations [post_model_sync] -# Patches added in this section will be executed after doctypes are migrated -jey_erp.patches.v1_0.create_asset_categories \ No newline at end of file +# Patches added in this section will be executed after doctypes are migrated \ No newline at end of file diff --git a/jey_erp/patches/v1_0/create_asset_categories.py b/jey_erp/patches/v1_0/create_asset_categories.py deleted file mode 100644 index b73e23d..0000000 --- a/jey_erp/patches/v1_0/create_asset_categories.py +++ /dev/null @@ -1,110 +0,0 @@ -import frappe - - -def execute(): - """ - Создает категории активов с настройками счетов - Этот патч выполняется только один раз - """ - - # Проверка существования таблицы Company - if not frappe.db.table_exists("Company"): - frappe.log_error("Company table not found", "Asset Categories Patch") - return - - # Получаем первую компанию - company = frappe.get_all("Company", limit=1) - if not company: - frappe.log_error("No company found in the system", "Asset Categories Patch") - return - - company_name = company[0].name - - # Список категорий активов - asset_categories = [ - "Binalar, tikililər və qurğular", - "Maşınlar və avadanlıqlar", - "Yüksək texnologiyalar məhsulu olan hesablama texnikası", - "Nəqliyyat vasitələri", - "Digər əsas vəsaitlər", - "Düzən torpaqlar", - "Dağ torpaqlar", - "Orta və yüksək dağ torpaqlar", - "Şəhərin mövcud hüdudundan kənarda olan kənd təsərrüfatı təyinatlı torpaqlar", - "Sənaye, tikinti, nəqliyyat, rabitə, ticarət-məişət xidməti və digər xüsusi təyinatlı torpaq sahələri" - ] - - # Находим нужные счета - try: - accumulated_depreciation = get_account_by_name( - "Tikililər üzrə yığılmış amortizasiya", - company_name - ) - depreciation_expense = get_account_by_name( - "Amortizasiya xərci", - company_name - ) - fixed_asset = get_account_by_name( - "Digər uzunmüddətli aktivlər", - company_name - ) - except Exception as e: - frappe.log_error(f"Required accounts not found: {str(e)}", "Asset Categories Patch") - return - - # Создаем категории активов - created_count = 0 - skipped_count = 0 - - for category_name in asset_categories: - try: - if not frappe.db.exists("Asset Category", category_name): - asset_category = frappe.new_doc("Asset Category") - asset_category.asset_category_name = category_name - asset_category.enable_cwip_accounting = 0 - - # Добавляем accounts - asset_category.append("accounts", { - "company_name": company_name, - "fixed_asset_account": fixed_asset, - "accumulated_depreciation_account": accumulated_depreciation, - "depreciation_expense_account": depreciation_expense, - "capital_work_in_progress_account": "" - }) - - asset_category.insert(ignore_permissions=True) - created_count += 1 - - frappe.msgprint(f"Created Asset Category: {category_name}") - else: - skipped_count += 1 - except Exception as e: - frappe.log_error( - f"Failed to create Asset Category '{category_name}': {str(e)}", - "Asset Categories Patch" - ) - - # Коммитим все изменения разом - frappe.db.commit() - - # Выводим итоговую информацию - message = f"Asset Categories Patch completed. Created: {created_count}, Skipped: {skipped_count}" - print(message) - frappe.msgprint(message) - - -def get_account_by_name(account_name, company): - """Get account by account_name field""" - account = frappe.db.get_value( - "Account", - { - "account_name": account_name, - "company": company - }, - "name" - ) - - if not account: - raise ValueError(f"Account not found: {account_name} for company {company}") - - return account