Skip to Content
Menu
This question has been flagged
1 Reply
1919 Views

I am trying to add the cash rounding line via API and wonder whether it is possible. I have created the invoice and define the cash rounding method but it will not add the cash rounding line as invoice_line is added later. I wonder whether there is a method to trigger the cash rounding like compute_tax. Below is the code I use, for testing purpose i have include adding invoice_line as part of the code.

@app.route("/create_invoice")

def create_invoice():

    date_invoice = request.args.get("date_invoice")

    invoice_id = models.execute_kw(db, uid, password, 'account.invoice', 'create', [{

    'date_invoice':date_invoice,

    'type': 'out_invoice',

    'partner_id': 92,

    'account_id': 38,

    'cash_rounding_id': 1,

    }])

    product_id = request.args.get("product_id")

    name = request.args.get("name")

    quantity = request.args.get("quantity")

    price_unit = request.args.get("price_unit")

    account_id = request.args.get("account_id")

    invoice_line_id = models.execute_kw(db, uid, password, 'account.invoice.line', 'create', [{

    'invoice_id': invoice_id,

    'product_id': product_id,

    'name': name,

    'quantity': quantity,

    'price_unit': price_unit,

    'account_id': account_id,

    'invoice_line_tax_ids':  [ (4,6) ],

    }])

    invoice_tax_id = models.execute_kw(db, uid, password, 'account.invoice', 'compute_taxes', [(invoice_id)])

    return jsonify(invoice_id)

Avatar
Discard
Author Best Answer

I did manually in the end.

    rounding_line_id = models.execute_kw(db, uid, password,'account.invoice.line', 'search',[[['invoice_id', '=', invoice_id], ['is_rounding_line', '=', True]]])
if rounding_line_id:
models.execute_kw(db, uid, password, 'account.invoice.line', 'unlink', [rounding_line_id]) models.execute_kw(db, uid, password, 'account.invoice', 'compute_taxes', [(invoice_id)])
total = models.execute_kw(db, uid, password,'account.invoice', 'search_read', [[['id', '=', invoice_id]]], {'fields': ['amount_total']})
total_value = total[0].get('amount_total')
normalized_value = total_value/0.05
sign = math.copysign(1.0, normalized_value)
epsilon_magnitude = math.log(abs(normalized_value), 2)
epsilon = 2**(epsilon_magnitude-53)
normalized_value += sign*epsilon
rounded_value = math.floor(abs(normalized_value)) * sign * 0.05
rounding_difference = rounded_value - total_value
if rounding_difference:
models.execute_kw(db, uid, password, 'account.invoice.line', 'create', [{
'invoice_id': invoice_id,
'name': 'Rounding',
'quantity': 1,
'price_unit': rounding_difference,
'account_id': 126,
'is_rounding_line': True,
'sequence': 9999
}])
return jsonify(invoice_line_id)
Avatar
Discard
Related Posts Replies Views Activity
1
Nov 23
3152
1
Mar 23
4831
2
Dec 19
14460
7
Oct 19
9235
1
Jul 19
2702