import frappe
from frappe import _

@frappe.whitelist()
def update_tax_article_from_asset(tax_article_name, date_from=None, date_to=None, duration=None):
    """
    Update Tax Article validity fields from Asset
    Called when Asset is saved
    """
    try:
        tax_article = frappe.get_doc('Tax Article', tax_article_name)
        
        if date_from is not None:
            tax_article.date_from = date_from
        
        if date_to is not None:
            tax_article.date_to = date_to
        
        if duration is not None:
            tax_article.duration = int(duration) if duration else None
        
        tax_article.save(ignore_permissions=True)
        frappe.db.commit()
        
        return {
            'success': True,
            'message': f'Tax Article {tax_article_name} updated successfully'
        }
    
    except Exception as e:
        frappe.log_error(f"Error updating Tax Article: {str(e)}", "Update Tax Article Error")
        return {
            'success': False,
            'error': str(e)
        }