I want to create a new company when I create a new account, I have written a code for that, I used res.users but can't get to the core of the problem.
I have created a new model for managing company and made many2one field with res.users, but still my desired thing is not being done.
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Akuntansi
- Inventaris
- PoS
- Project
- MRP
Pertanyaan ini telah diberikan tanda
I have used this code
from odoo import models, fields, api, _
class ResUsers(models.Model):
_inherit = 'res.users'
has_logged_in = fields.Boolean(string='Has Logged In', default=False)
company_id = fields.Many2one('my.company', string='Company')
@api.model
def _login(self, db, login, password):
# Call the original login method
res = super(ResUsers, self)._login(db, login, password)
# Check if the user has logged in before
user = self.env['res.users'].sudo().search([('login', '=', login)])
if user and not user.has_logged_in:
# Generate a unique company name using a sequence
company_name = 'Company %s' % self.env['ir.sequence'].next_by_code('my.company.sequence')
# Create a new Company
company_data = {
'name': company_name,
}
new_company = self.env['my.company'].sudo().create(company_data)
# Update the user's has logged_in and company_id fields
user.write({'has_logged_in': True, 'company_id': new_company.id})
return res
Hi,
You can use the following code for creating a new company upon the creation of an account.
class Account(models.Model):
_inherit = 'account.account'
@api.model
def create(self, vals):
res = super().create(vals)
company = self.env['res.company'].sudo().create({
'name': 'New Company'
})Replace your name with your desired name of the company
Hope it helps
Menikmati diskusi? Jangan hanya membaca, ikuti!
Buat akun sekarang untuk menikmati fitur eksklufi dan agar terlibat dengan komunitas kami!
DaftarPost Terkait | Replies | Tampilan | Aktivitas | |
---|---|---|---|---|
|
0
Sep 24
|
1479 | ||
|
2
Agu 25
|
1312 | ||
|
1
Mar 25
|
1794 | ||
|
0
Nov 24
|
1477 | ||
|
1
Sep 24
|
4536 |