ej2 / python-quickbooks

A Python library for accessing the Quickbooks API.
MIT License
407 stars 195 forks source link

DepositLine Object needs to be more specific when a LinkedTxn vs DepositLineDetail #329

Closed craftypenguins-chris closed 8 months ago

craftypenguins-chris commented 1 year ago

When updating (and also during creation), the QBO API will fail with an obscure error if you are using linked transactions in a deposit.

QB Validation Exception 2020: Required param missing, need to supply the required value for the API
Required parameter Line.DepositLineDetail.AccountRef is missing in the request

This occurs because the DepositLine Object includes the self.DetailType = "DepositLineDetail" attribute regardless if the line being a LinkedTxn or not.

I haven't dug in far enough to know if this can be a conditional inclusion at object creation or not. I ended up using a delattrib on the object method in my operational codebase:

def remove_unwanted_details(obj):
    if isinstance(obj, list):
        return [remove_unwanted_details(item) for item in obj]
    elif isinstance(obj, dict):
        # Remove 'DetailType' if 'LinkedTxn' key is present in the dict
        if "LinkedTxn" in obj and obj["LinkedTxn"]:
            obj.pop("DetailType", None)
        for key, value in list(obj.items()):
            obj[key] = remove_unwanted_details(value)
    elif hasattr(obj, '__dict__'):
        if isinstance(obj, DepositLine) and getattr(obj, 'LinkedTxn', None):
            # Use delattr to remove the 'DetailType' attribute from Line instances with LinkedTxn
            if hasattr(obj, 'DetailType'):
                delattr(obj, 'DetailType')
        # Recurse into attributes of custom objects
        for attr_name in list(obj.__dict__.keys()):
            attr_value = getattr(obj, attr_name)
            setattr(obj, attr_name, remove_unwanted_details(attr_value))
    return obj
ej2 commented 11 months ago

Could you provide some example code to reproduce the error so I can understand the problem better?