Commit all views
This commit is contained in:
@@ -643,6 +643,56 @@ def ensure_party_is_supplier(party, auto_enable=True):
|
||||
print(f" Please manually add SUPPLIER category to '{party.rec_name}' in Tryton UI")
|
||||
return party, False
|
||||
|
||||
def ensure_party_has_category(party, category_name, auto_enable=True):
|
||||
"""Ensure a party has a given category by name, optionally creating the category and adding it."""
|
||||
if not party:
|
||||
return party, False
|
||||
|
||||
Category = Model.get('party.category')
|
||||
|
||||
# Find or create the category
|
||||
categories = Category.find([('name', '=', category_name)])
|
||||
if not categories:
|
||||
all_cats = Category.find([])
|
||||
categories = [c for c in all_cats if c.name.upper() == category_name.upper()]
|
||||
|
||||
if not categories:
|
||||
if not auto_enable:
|
||||
print(f" ⚠ Category '{category_name}' not found in the system")
|
||||
return party, False
|
||||
# Create the category
|
||||
try:
|
||||
new_cat = Category()
|
||||
new_cat.name = category_name
|
||||
new_cat.save()
|
||||
categories = [new_cat]
|
||||
print(f" ✓ Category '{category_name}' created")
|
||||
except Exception as e:
|
||||
print(f" ✗ Could not create category '{category_name}': {e}")
|
||||
return party, False
|
||||
|
||||
category = categories[0]
|
||||
|
||||
# Check if party already has the category
|
||||
if hasattr(party, 'categories') and party.categories:
|
||||
for cat in party.categories:
|
||||
if cat.id == category.id:
|
||||
return party, True
|
||||
|
||||
if not auto_enable:
|
||||
print(f" ⚠ Party '{party.rec_name}' does not have '{category_name}' category")
|
||||
return party, False
|
||||
|
||||
try:
|
||||
party.categories.append(category)
|
||||
party.save()
|
||||
print(f" ✓ '{category_name}' category added to party '{party.rec_name}'")
|
||||
return party, True
|
||||
except Exception as e:
|
||||
print(f" ✗ Could not add '{category_name}' category to '{party.rec_name}': {e}")
|
||||
return party, False
|
||||
|
||||
|
||||
#===================================== Client Functions =====================================
|
||||
|
||||
def find_client_category():
|
||||
|
||||
Reference in New Issue
Block a user