mirror of
https://github.com/bringout/oca-ocb-sale.git
synced 2026-04-27 04:12:05 +02:00
19.0 vanilla
This commit is contained in:
parent
79f83631d5
commit
73afc09215
6267 changed files with 1534193 additions and 1130106 deletions
|
|
@ -1,7 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, models, _
|
||||
from odoo import _, api, models
|
||||
|
||||
|
||||
class ResCompany(models.Model):
|
||||
|
|
@ -9,59 +8,62 @@ class ResCompany(models.Model):
|
|||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
companies = super(ResCompany, self).create(vals_list)
|
||||
ProductPricelist = self.env['product.pricelist']
|
||||
for new_company in companies:
|
||||
pricelist = ProductPricelist.search([
|
||||
('currency_id', '=', new_company.currency_id.id),
|
||||
('company_id', '=', False)
|
||||
], limit=1)
|
||||
if not pricelist:
|
||||
params = {'currency': new_company.currency_id.name}
|
||||
pricelist = ProductPricelist.create({
|
||||
'name': _("Default %(currency)s pricelist") % params,
|
||||
'currency_id': new_company.currency_id.id,
|
||||
})
|
||||
self.env['ir.property']._set_default(
|
||||
'property_product_pricelist',
|
||||
'res.partner',
|
||||
pricelist,
|
||||
new_company,
|
||||
)
|
||||
companies = super().create(vals_list)
|
||||
companies._activate_or_create_pricelists()
|
||||
return companies
|
||||
|
||||
def write(self, values):
|
||||
# When we modify the currency of the company, we reflect the change on the list0 pricelist, if
|
||||
# that pricelist is not used by another company. Otherwise, we create a new pricelist for the
|
||||
# given currency.
|
||||
ProductPricelist = self.env['product.pricelist']
|
||||
currency_id = values.get('currency_id')
|
||||
main_pricelist = self.env.ref('product.list0', False)
|
||||
if currency_id and main_pricelist:
|
||||
nb_companies = self.search_count([])
|
||||
for company in self:
|
||||
existing_pricelist = ProductPricelist.search(
|
||||
[('company_id', 'in', (False, company.id)),
|
||||
('currency_id', 'in', (currency_id, company.currency_id.id))])
|
||||
if existing_pricelist and any(currency_id == x.currency_id.id for x in existing_pricelist):
|
||||
continue
|
||||
if currency_id == company.currency_id.id:
|
||||
continue
|
||||
currency_match = main_pricelist.currency_id == company.currency_id
|
||||
company_match = (main_pricelist.company_id == company or
|
||||
(main_pricelist.company_id.id is False and nb_companies == 1))
|
||||
if currency_match and company_match:
|
||||
main_pricelist.write({'currency_id': currency_id})
|
||||
else:
|
||||
params = {'currency': self.env['res.currency'].browse(currency_id).name}
|
||||
pricelist = ProductPricelist.create({
|
||||
'name': _("Default %(currency)s pricelist") % params,
|
||||
'currency_id': currency_id,
|
||||
})
|
||||
self.env['ir.property']._set_default(
|
||||
'property_product_pricelist',
|
||||
'res.partner',
|
||||
pricelist,
|
||||
company,
|
||||
)
|
||||
return super(ResCompany, self).write(values)
|
||||
def _activate_or_create_pricelists(self):
|
||||
""" Manage the default pricelists for needed companies. """
|
||||
if self.env.context.get('disable_company_pricelist_creation'):
|
||||
return
|
||||
|
||||
if self.env.user.has_group('product.group_product_pricelist'):
|
||||
companies = self or self.env['res.company'].search([])
|
||||
ProductPricelist = self.env['product.pricelist'].sudo()
|
||||
# Activate existing default pricelists
|
||||
default_pricelists_sudo = ProductPricelist.with_context(active_test=False).search(
|
||||
[('item_ids', '=', False), ('company_id', 'in', companies.ids)]
|
||||
).filtered(lambda pl: pl.currency_id == pl.company_id.currency_id)
|
||||
default_pricelists_sudo.action_unarchive()
|
||||
companies_without_pricelist = companies.filtered(
|
||||
lambda c: c.id not in default_pricelists_sudo.company_id.ids
|
||||
)
|
||||
# Create missing default pricelists
|
||||
ProductPricelist.create([
|
||||
company._get_default_pricelist_vals() for company in companies_without_pricelist
|
||||
])
|
||||
|
||||
def _get_default_pricelist_vals(self):
|
||||
"""Add values to the default pricelist at company creation or activation of the pricelist
|
||||
|
||||
Note: self.ensure_one()
|
||||
|
||||
:rtype: dict
|
||||
"""
|
||||
self.ensure_one()
|
||||
values = {}
|
||||
values.update({
|
||||
'name': _("Default"),
|
||||
'currency_id': self.currency_id.id,
|
||||
'company_id': self.id,
|
||||
'sequence': 10,
|
||||
})
|
||||
return values
|
||||
|
||||
def write(self, vals):
|
||||
"""Delay the automatic creation of pricelists post-company update.
|
||||
|
||||
This makes sure that the pricelist(s) automatically created are created with the right
|
||||
currency.
|
||||
"""
|
||||
if not vals.get('currency_id'):
|
||||
return super().write(vals)
|
||||
|
||||
enabled_pricelists = self.env.user.has_group('product.group_product_pricelist')
|
||||
res = super(
|
||||
ResCompany, self.with_context(disable_company_pricelist_creation=True)
|
||||
).write(vals)
|
||||
if not enabled_pricelists and self.env.user.has_group('product.group_product_pricelist'):
|
||||
self.browse()._activate_or_create_pricelists()
|
||||
|
||||
return res
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue