XeroAPI / xero-python-oauth2-app

python app for demonstrating the xero-python SDK
MIT License
19 stars 31 forks source link

Examples for quote submission #44

Closed tofusoul closed 2 years ago

tofusoul commented 2 years ago

There's no examples of submitting quotes.

Would be good to know how best to deal with wrapping the data for submission etc.

The example in the SDK docs doesn't marry too well with how the requests are done in the example apps. There's seems to be more abstraction etc in the example apps.

majority of the examples of interacting with quotes are just comments.

Would really help to have these created in order to understand the SDK.

RettBehrens commented 2 years ago

Hey @tofusoul thanks for calling this out. I've added a create example for quotes and it's on the team's backlog to continue filling in more methods.

@app.route("/accounting_quotes_create")
@xero_token_required
def accounting_quotes_create():
    code = get_code_snippet("QUOTES","CREATE")
    xero_tenant_id = get_xero_tenant_id()
    accounting_api = AccountingApi(api_client)

    # READ CONTACT
    try:
        read_contacts = accounting_api.get_contacts(
            xero_tenant_id
        )
        contact_id = getvalue(read_contacts, "contacts.0.contact_id", "")
    except AccountingBadRequestException as exception:
        output = "Error: " + exception.reason
        json = jsonify(exception.error_data)

    # READ ACCOUNT
    where = "Type==\"SALES\"&&Status==\"ACTIVE\""
    try:
        read_accounts = accounting_api.get_accounts(
            xero_tenant_id, where=where
        )
        account_id = getvalue(read_accounts, "accounts.0.account_id", "")
    except AccountingBadRequestException as exception:
        output = "Error: " + exception.reason
        json = jsonify(exception.error_data)

    #[QUOTES:CREATE]
    xero_tenant_id = get_xero_tenant_id()
    accounting_api = AccountingApi(api_client)

    contact = Contact(
        contact_id=contact_id
    )

    line_item = LineItem(
        account_id=account_id,
        description= "Consulting",
    )

    quote = Quote(
        line_items=[line_item],
        contact=contact,
        date= dateutil.parser.parse("2022-08-29T00:00:00Z"),
    )

    quotes = Quotes(quotes=[quote])

    try:
        created_quotes = accounting_api.create_quotes(
            xero_tenant_id, quotes=quotes
        )
    except AccountingBadRequestException as exception:
        output = "Error: " + exception.reason
        json = jsonify(exception.error_data)
    else:
        output = "New quote created with ID: '{}'.".format(
            getvalue(created_quotes, "quotes.0.quote_id", "")
        )
        json = serialize_model(created_quotes)
    #[/QUOTES:CREATE]

    return render_template(
        "output.html", title="Quotes", code=code, output=output, json=json, len = 0, set="accounting", endpoint="quotes", action="create"
    )
tofusoul commented 2 years ago

thanks @RettBehrens will give this a go!