How to programatically create a paid invoice from sale order [duplicate] - odoo

I am trying to make a single action on a sales order in Odoo v15 CE that creates an invoice for the sales order and immediately posts it and registers a payment for it. The way I'm doing so is through a wizard method that looks like this:
def create_invoices(self):
sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', []))
for order in sale_orders:
order._create_invoices()
for inv in order.invoice_ids:
# make invoice:
inv.action_post()
# register payment
# ????
Currently, it creates the invoice and posts it. I would like to add something like inv.register_payment() to the final line in order to register the payment.
I've fount the action_register_payment method of the account.move model and also looked into using the account.payment.register wizard but neither worked. I've also found this question, which is trying to do something similar, but through an XML-RPC call (from what I can tell).
Can anyone please explain how to do this? Thanks!

You have to post the invoices using action_post then register payment by creating a payment like in
https://github.com/odoo/odoo/blob/15.0/addons/account/wizard/account_payment_register.py#L496
account.payment.register is a wizard, that's why you need to create values for it, otherwise you can action_register_payment which is a window action that will trigger the wizard and then it's for the user to register the payment (which is the standard behavior of Odoo)

You can create an account.payment.register record and pass the model and invoice ids in context then call action_create_payments to create the payment with the default values.
Example:
payment_register.with_context(
{'active_model': 'account.move',
'active_ids': inv.ids}
).create({}).action_create_payments()

Related

Odoo How Can I update my value from another module's value

I have a question...
As you know in odoo we have sales and purchase apps. I am building a module which will be
provide update product prices in the sales module from the vendor price in the purchase app.
I created a wizard and linked to purchase app.
Okay here is my question...
I want to do this simply.
product.supplierinfo.["price"] = product.product_template["list_price"]
I tried:
for rec self.env["product.supplierinfo"]:
self.env[product.product_template["list_price"]] = rec.["price"]
But as you know it's not working ^^
Any advice for this?
i think you mean to do like this
new_price= self.env[product.product_template].write({
"list_price" : rec.price,
})

Odoo - Add Outstanding payment to invoice through API

Using Odoo 15 and python xmlrpc for API
here is my problem: I have a created payment for which I store the id. Then, when i create the invoice that corresponds, I want to be able to add this specific outstanding credit of the customer through API just like this "Ajouter" (Add) button does. ("crédits en circulation" is outstanding credits):
invoice screenshot
How would I do with python and xml-rpc tu simulate the use of this button for a specific payment (with its id)?
Note: For our sales workflow we have to create payments straight away and invoices later so I can't create the payment when creating the invoice
When you click on Add button, Odoo will call the js_assign_outstanding_line function.
def js_assign_outstanding_line(self, line_id):
''' Called by the 'payment' widget to reconcile a suggested journal item to the present
invoice.
:param line_id: The id of the line to reconcile with the current invoice.
'''
self.ensure_one()
lines = self.env['account.move.line'].browse(line_id)
lines += self.line_ids.filtered(lambda line: line.account_id == lines[0].account_id and not line.reconciled)
return lines.reconcile()
The Add button depends on the value of the computed invoice_outstanding_credits_debits_widget field. To reproduce the same logic using XML-RPC, you need to get the payment credit line id and call the js_assign_outstanding_line function like following:
models.execute_kw(db, uid, password, 'account.move', 'js_assign_outstanding_line', [move_id, line_id])
It will add the payment and return the result of reconcile function which will raise the following error (XML-RPC code):
TypeError: cannot marshal <class \'odoo.api.account.partial.reconcile\'> objects\n'>

Create an invoice from a sales order using XML-RPC (Python)

I'm following the external APIs documentation : https://www.odoo.com/documentation/13.0/webservices/odoo.html
to implement our companies requirements. I'm required to create a sales order and automatically create an invoice after that. The sales order part is done but I cant seem to be able to attach the Invoice to the Sales order
I've tried linking it via the 'invoice_ids' field but the documentation does not mention how to provide a many2many field in it. here is the code:
many2manyInvoice = [(4, invoice_id)]
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
#Admin user Id
uid = common.authenticate(db, username, password, {})
models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))
models.execute_kw(db, uid, password, 'sale.order', 'write', [[sales_order_id], {'invoice_ids':many2manyInvoice}])
The response returned is 200 , but nothing is happening on the sales order level. I think its the way that I defined the field that might be incorrect.
Can someone help with this issue ? Thanks in advance
Nothing is happening on the sales order level because you are not creating a sales record, writing to it without doing anything. Not sure if this would work in your specific case but here is what I would do.
Use the Pro-forma invoice https://www.odoo.com/documentation/user/13.0/sales/invoicing/proforma.html
Then when a sales record is created run the "Send pro-forma invoice" method using the web api. This takes care of the db linking, as it can get very complicated.

How to relate fields in Odoo 11 CE

I added a custom field in "account.payment" model, I also added it in "account.move.line" model (aka: Journal Item).
The value of the custom field is entered from account.payment and since the journal items are generated from a payment creation I want to do the following:
If account.move.line.id is created by account.payment.id
let account.move.line.custom_field = account.payment.custom_field
I appreciate your help
You have to find in account.move.line that represent the relation(foreign_key) with account.payment, let says payment_id, this field allows you to access to account.payment model, and for your case you just need to do:
field_in_account_move_line = payment_id.field_in_account_payment
I hope this answer can be helpful for you.

Can't confirm sale orders after applying record rule

In odoo 9, I have added a record rule on the model mrp.production as:
['|', ('user_id', '=', user.id), ('user_id', '=', False)]
This will show users only the MOs that belongs to them. Now when I am trying to confirm the sale order which will then create a MO for the lines in that sale order I am getting an access error as:
The requested operation cannot be completed due to security restrictions. Please contact your system administrator.
(Document type: mrp.production, Operation: read)
Diagnosing more I found that it is causing due to the missing_ids. Take a look at this.
Before that I have used the same solution in openerp 7 and it is still working perfect without any access error while confirming SO.
From which user you are trying ? i think you are trying with admin login, and you write security rule that only user of that record can access that. I think that is the problem.
Finally I found the reason of this issue.
In opernep/addons/mrp/procurement.py there is line
production_obj.create(cr, SUPERUSER_ID, vals, context=dict(context, force_company=procurement.company_id.id))
which is using SUPERUSER_ID to create the production order from the procurement.
I don't know why they changed it to use SUPERUSER_ID. May be to enable non mrp users or external users to create mrp orders even if they have not rights. BTW I have not such requirement and I have solved my problem by replacing this SUPERUSER_ID with uid.