From 9215e73fa229d64167de076ef0a288dd8d89a2cc Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Fri, 11 Oct 2019 11:30:40 +0000 Subject: [PATCH] [IMP] * : replace with_context(force_company=c) by with_company(c) --- addons/account/models/partner.py | 7 ++-- addons/hr/models/res_users.py | 2 +- addons/hr_expense/models/hr_expense.py | 6 ++-- .../l10n_ar/models/account_fiscal_position.py | 4 +-- addons/mrp/models/mrp_production.py | 4 +-- addons/mrp/models/mrp_unbuild.py | 2 +- addons/mrp/models/stock_rule.py | 7 ++-- addons/mrp/models/stock_warehouse.py | 2 +- addons/mrp/report/mrp_report_bom_structure.py | 4 +-- .../mrp_subcontracting/models/stock_move.py | 2 +- .../models/stock_picking.py | 6 ++-- .../wizard/stock_picking_return.py | 2 +- addons/payment/models/payment_acquirer.py | 2 +- addons/point_of_sale/models/pos_order.py | 17 ++++++---- addons/point_of_sale/models/pos_session.py | 2 +- addons/product/models/product.py | 7 ++-- addons/product/models/product_pricelist.py | 2 +- addons/product/models/product_template.py | 9 ++--- addons/product/models/res_partner.py | 6 ++-- .../product_margin/models/product_product.py | 7 +--- addons/purchase/models/purchase.py | 4 +-- .../models/stock.py | 2 +- addons/purchase_stock/models/stock_rule.py | 12 +++---- addons/repair/models/repair.py | 24 +++++++------- addons/sale/models/payment.py | 14 ++++---- addons/sale/models/sale.py | 4 +-- addons/sale_purchase/models/sale_order.py | 3 +- addons/sale_timesheet/models/sale_order.py | 7 ++-- addons/stock/models/product.py | 19 +++++------ addons/stock/models/res_company.py | 2 +- addons/stock/models/stock_inventory.py | 2 +- addons/stock/models/stock_move.py | 2 +- addons/stock/models/stock_quant.py | 4 +-- addons/stock/models/stock_rule.py | 12 ++----- addons/stock/models/stock_warehouse.py | 2 +- addons/stock/tests/test_stock_flow.py | 6 ++-- addons/stock/wizard/product_replenish.py | 2 +- addons/stock_account/models/product.py | 9 +++-- addons/stock_account/models/stock_move.py | 33 +++++++++---------- .../models/stock_landed_cost.py | 2 +- addons/website_sale/models/sale_order.py | 3 +- addons/website_sale/models/website.py | 4 +-- .../tests/test_website_sale_pricelist.py | 6 ++-- 43 files changed, 129 insertions(+), 149 deletions(-) diff --git a/addons/account/models/partner.py b/addons/account/models/partner.py index 220dda29c8f..4b6d8cd6ed8 100644 --- a/addons/account/models/partner.py +++ b/addons/account/models/partner.py @@ -119,11 +119,10 @@ class AccountFiscalPosition(models.Model): def _get_fpos_by_region(self, country_id=False, state_id=False, zipcode=False, vat_required=False): if not country_id: return False - company_id = self.env.context.get('force_company', self.env.company.id) base_domain = [ ('auto_apply', '=', True), ('vat_required', '=', vat_required), - ('company_id', '=', company_id), + ('company_id', '=', self.env.company.id), ] null_state_dom = state_domain = [('state_ids', '=', False)] null_zip_dom = zip_domain = [('zip_from', '=', False), ('zip_to', '=', False)] @@ -226,7 +225,7 @@ class ResPartner(models.Model): _name = 'res.partner' _inherit = 'res.partner' - @api.depends_context('force_company') + @api.depends_context('company') def _credit_debit_get(self): tables, where_clause, where_params = self.env['account.move.line'].with_context(state='posted', company_id=self.env.company.id)._query_get() where_params = [tuple(self.ids)] + where_params @@ -425,7 +424,7 @@ class ResPartner(models.Model): invoice_warn = fields.Selection(WARNING_MESSAGE, 'Invoice', help=WARNING_HELP, default="no-message") invoice_warn_msg = fields.Text('Message for Invoice') # Computed fields to order the partners as suppliers/customers according to the - # amount of their generated incoming/outgoing account moves + # amount of their generated incoming/outgoing account moves supplier_rank = fields.Integer(default=0) customer_rank = fields.Integer(default=0) diff --git a/addons/hr/models/res_users.py b/addons/hr/models/res_users.py index d2379aa720c..6448676a7fd 100644 --- a/addons/hr/models/res_users.py +++ b/addons/hr/models/res_users.py @@ -192,7 +192,7 @@ class User(models.Model): return super(User, self).action_get() @api.depends('employee_ids') - @api.depends_context('force_company') + @api.depends_context('company') def _compute_company_employee(self): for user in self: user.employee_id = self.env['hr.employee'].search([('id', 'in', user.employee_ids.ids), ('company_id', '=', self.env.company.id)], limit=1) diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py index 6080fee5e31..a535a8840c0 100644 --- a/addons/hr_expense/models/hr_expense.py +++ b/addons/hr_expense/models/hr_expense.py @@ -322,12 +322,12 @@ class HrExpense(models.Model): if self.account_id: account = self.account_id elif self.product_id: - account = self.product_id.product_tmpl_id.with_context(force_company=self.company_id.id)._get_product_accounts()['expense'] + account = self.product_id.product_tmpl_id.with_company(self.company_id)._get_product_accounts()['expense'] if not account: raise UserError( _("No Expense account found for the product %s (or for its category), please configure one.") % (self.product_id.name)) else: - account = self.env['ir.property'].with_context(force_company=self.company_id.id).get('property_account_expense_categ_id', 'product.category') + account = self.env['ir.property'].with_company(self.company_id).get('property_account_expense_categ_id', 'product.category') if not account: raise UserError(_('Please configure Default Expense account for Product expense: `property_account_expense_categ_id`.')) return account @@ -342,7 +342,7 @@ class HrExpense(models.Model): else: if not self.employee_id.address_home_id: raise UserError(_("No Home Address found for the employee %s, please configure one.") % (self.employee_id.name)) - partner = self.employee_id.address_home_id.with_context(force_company=self.company_id.id) + partner = self.employee_id.address_home_id.with_company(self.company_id) account_dest = partner.property_account_payable_id.id or partner.parent_id.property_account_payable_id.id return account_dest diff --git a/addons/l10n_ar/models/account_fiscal_position.py b/addons/l10n_ar/models/account_fiscal_position.py index c445f453864..d2032de8f7b 100644 --- a/addons/l10n_ar/models/account_fiscal_position.py +++ b/addons/l10n_ar/models/account_fiscal_position.py @@ -13,13 +13,13 @@ class AccountFiscalPosition(models.Model): def get_fiscal_position(self, partner_id, delivery_id=None): """ Take into account the partner afip responsibility in order to auto-detect the fiscal position """ - company = self.env['res.company'].browse(self._context.get('force_company', self.env.company.id)) + company = self.env.company if company.country_id == self.env.ref('base.ar'): domain = [ ('auto_apply', '=', True), ('l10n_ar_afip_responsibility_type_ids', '=', self.env['res.partner'].browse( partner_id).l10n_ar_afip_responsibility_type_id.id), - ('company_id', '=', company.id) + ('company_id', '=', company.id), ] return self.search(domain, limit=1).id return super().get_fiscal_position(partner_id, delivery_id=delivery_id) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index 868c917f2ec..1acbe32a3dc 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -538,7 +538,7 @@ class MrpProduction(models.Model): 'date': self.date_planned_start, 'date_expected': self.date_planned_finished, 'picking_type_id': self.picking_type_id.id, - 'location_id': self.product_id.with_context(force_company=self.company_id.id).property_stock_production.id, + 'location_id': self.product_id.with_company(self.company_id).property_stock_production.id, 'location_dest_id': self.location_dest_id.id, 'company_id': self.company_id.id, 'production_id': self.id, @@ -594,7 +594,7 @@ class MrpProduction(models.Model): 'product_uom_qty': quantity, 'product_uom': bom_line.product_uom_id.id, 'location_id': source_location.id, - 'location_dest_id': self.product_id.with_context(force_company=self.company_id.id).property_stock_production.id, + 'location_dest_id': self.product_id.with_company(self.company_id).property_stock_production.id, 'raw_material_production_id': self.id, 'company_id': self.company_id.id, 'operation_id': bom_line.operation_id.id or alt_op, diff --git a/addons/mrp/models/mrp_unbuild.py b/addons/mrp/models/mrp_unbuild.py index b767e71cf1b..a49249fb7ca 100644 --- a/addons/mrp/models/mrp_unbuild.py +++ b/addons/mrp/models/mrp_unbuild.py @@ -229,7 +229,7 @@ class MrpUnbuild(models.Model): def _generate_move_from_bom_line(self, product, product_uom, quantity, bom_line_id=False, byproduct_id=False): location_id = bom_line_id and product.property_stock_production or self.location_id - location_dest_id = bom_line_id and self.location_dest_id or product.with_context(force_company=self.company_id.id).property_stock_production + location_dest_id = bom_line_id and self.location_dest_id or product.with_company(self.company_id).property_stock_production warehouse = location_dest_id.get_warehouse() return self.env['stock.move'].create({ 'name': self.name, diff --git a/addons/mrp/models/stock_rule.py b/addons/mrp/models/stock_rule.py index ef3f1e9e797..a003063d9bd 100644 --- a/addons/mrp/models/stock_rule.py +++ b/addons/mrp/models/stock_rule.py @@ -44,7 +44,7 @@ class StockRule(models.Model): for company_id, productions_values in productions_values_by_company.items(): # create the MO as SUPERUSER because the current user may not have the rights to do it (mto product launched by a sale for example) - productions = self.env['mrp.production'].sudo().with_context(force_company=company_id).create(productions_values) + productions = self.env['mrp.production'].sudo().with_company(company_id).create(productions_values) self.env['stock.move'].sudo().create(productions._get_moves_raw_values()) productions.action_confirm() @@ -69,9 +69,8 @@ class StockRule(models.Model): def _get_matching_bom(self, product_id, company_id, values): if values.get('bom_id', False): return values['bom_id'] - return self.env['mrp.bom'].with_context( - company_id=company_id.id, force_company=company_id.id - )._bom_find(product=product_id, picking_type=self.picking_type_id, bom_type='normal') # TDE FIXME: context bullshit + return self.env['mrp.bom']._bom_find( + product=product_id, picking_type=self.picking_type_id, bom_type='normal', company_id=company_id.id) def _prepare_mo_vals(self, product_id, product_qty, product_uom, location_id, name, origin, company_id, values, bom): date_deadline = fields.Datetime.to_string(self._get_date_planned(product_id, company_id, values)) diff --git a/addons/mrp/models/stock_warehouse.py b/addons/mrp/models/stock_warehouse.py index 543db91f65b..eb0e79c69ac 100644 --- a/addons/mrp/models/stock_warehouse.py +++ b/addons/mrp/models/stock_warehouse.py @@ -61,7 +61,7 @@ class StockWarehouse(models.Model): @api.model def _get_production_location(self): - location = self.env['stock.location'].with_context(force_company=self.company_id.id).search([('usage', '=', 'production'), ('company_id', '=', self.company_id.id)], limit=1) + location = self.env['stock.location'].search([('usage', '=', 'production'), ('company_id', '=', self.company_id.id)], limit=1) if not location: raise UserError(_('Can\'t find any production location.')) return location diff --git a/addons/mrp/report/mrp_report_bom_structure.py b/addons/mrp/report/mrp_report_bom_structure.py index 0d341420d1a..8a743e94dc0 100644 --- a/addons/mrp/report/mrp_report_bom_structure.py +++ b/addons/mrp/report/mrp_report_bom_structure.py @@ -115,7 +115,7 @@ class ReportBomStructure(models.AbstractModel): 'currency': company.currency_id, 'product': product, 'code': bom and bom.display_name or '', - 'price': product.uom_id._compute_price(product.with_context(force_company=company.id).standard_price, bom.product_uom_id) * bom_quantity, + 'price': product.uom_id._compute_price(product.with_company(company).standard_price, bom.product_uom_id) * bom_quantity, 'total': sum([op['total'] for op in operations]), 'level': level or 0, 'operations': operations, @@ -136,7 +136,7 @@ class ReportBomStructure(models.AbstractModel): if line._skip_bom_line(product): continue company = bom.company_id or self.env.company - price = line.product_id.uom_id._compute_price(line.product_id.with_context(force_company=company.id).standard_price, line.product_uom_id) * line_quantity + price = line.product_id.uom_id._compute_price(line.product_id.with_company(company).standard_price, line.product_uom_id) * line_quantity if line.child_bom_id: factor = line.product_uom_id._compute_quantity(line_quantity, line.child_bom_id.product_uom_id) / line.child_bom_id.product_qty sub_total = self._get_price(line.child_bom_id, factor, line.product_id) diff --git a/addons/mrp_subcontracting/models/stock_move.py b/addons/mrp_subcontracting/models/stock_move.py index 3871329238c..21884eb269e 100644 --- a/addons/mrp_subcontracting/models/stock_move.py +++ b/addons/mrp_subcontracting/models/stock_move.py @@ -109,7 +109,7 @@ class StockMove(models.Model): subcontract_details_per_picking[move.picking_id].append((move, bom)) move.write({ 'is_subcontract': True, - 'location_id': move.picking_id.partner_id.with_context(force_company=move.company_id.id).property_stock_subcontractor.id + 'location_id': move.picking_id.partner_id.with_company(move.company_id).property_stock_subcontractor.id }) for picking, subcontract_details in subcontract_details_per_picking.items(): picking._subcontracted_produce(subcontract_details) diff --git a/addons/mrp_subcontracting/models/stock_picking.py b/addons/mrp_subcontracting/models/stock_picking.py index 2a5541bf59d..e3040792084 100644 --- a/addons/mrp_subcontracting/models/stock_picking.py +++ b/addons/mrp_subcontracting/models/stock_picking.py @@ -125,8 +125,8 @@ class StockPicking(models.Model): 'product_id': product.id, 'product_uom_id': subcontract_move.product_uom.id, 'bom_id': bom.id, - 'location_src_id': subcontract_move.picking_id.partner_id.with_context(force_company=subcontract_move.company_id.id).property_stock_subcontractor.id, - 'location_dest_id': subcontract_move.picking_id.partner_id.with_context(force_company=subcontract_move.company_id.id).property_stock_subcontractor.id, + 'location_src_id': subcontract_move.picking_id.partner_id.with_company(subcontract_move.company_id).property_stock_subcontractor.id, + 'location_dest_id': subcontract_move.picking_id.partner_id.with_company(subcontract_move.company_id).property_stock_subcontractor.id, 'product_qty': subcontract_move.product_uom_qty, 'picking_type_id': warehouse.subcontracting_type_id.id } @@ -135,7 +135,7 @@ class StockPicking(models.Model): def _subcontracted_produce(self, subcontract_details): self.ensure_one() for move, bom in subcontract_details: - mo = self.env['mrp.production'].with_context(force_company=move.company_id.id).create(self._prepare_subcontract_mo_vals(move, bom)) + mo = self.env['mrp.production'].with_company(move.company_id).create(self._prepare_subcontract_mo_vals(move, bom)) self.env['stock.move'].create(mo._get_moves_raw_values()) mo.action_confirm() diff --git a/addons/mrp_subcontracting/wizard/stock_picking_return.py b/addons/mrp_subcontracting/wizard/stock_picking_return.py index 94162fe308d..c3ec170f811 100644 --- a/addons/mrp_subcontracting/wizard/stock_picking_return.py +++ b/addons/mrp_subcontracting/wizard/stock_picking_return.py @@ -12,7 +12,7 @@ class ReturnPicking(models.TransientModel): res = super(ReturnPicking, self)._onchange_picking_id() if not any(self.product_return_moves.filtered(lambda r: r.quantity > 0).move_id.mapped('is_subcontract')): return res - subcontract_location = self.picking_id.partner_id.with_context(force_company=self.picking_id.company_id.id).property_stock_subcontractor + subcontract_location = self.picking_id.partner_id.with_company(self.picking_id.company_id).property_stock_subcontractor self.location_id = subcontract_location.id domain_location = OR([ ['|', ('id', '=', self.original_location_id.id), ('return_location', '=', True)], diff --git a/addons/payment/models/payment_acquirer.py b/addons/payment/models/payment_acquirer.py index 0ba02689ede..e095a4d7111 100644 --- a/addons/payment/models/payment_acquirer.py +++ b/addons/payment/models/payment_acquirer.py @@ -761,7 +761,7 @@ class PaymentTransaction(models.Model): trans.payment_id = payment for company in payments: - payments[company].with_context(force_company=company, company_id=company).post() + payments[company].with_company(company).with_context(company_id=company).post() def _set_transaction_cancel(self): '''Move the transaction's payment to the cancel state(e.g. Paypal).''' diff --git a/addons/point_of_sale/models/pos_order.py b/addons/point_of_sale/models/pos_order.py index f1be8b77032..013bc199c42 100644 --- a/addons/point_of_sale/models/pos_order.py +++ b/addons/point_of_sale/models/pos_order.py @@ -104,13 +104,12 @@ class PosOrder(models.Model): def _process_order(self, order, draft, existing_order): """Create or update an pos.order from a given dictionary. - :param pos_order: dictionary representing the order. - :type pos_order: dict. - :param draft: Indicate that the pos_order is not validated yet. - :type draft: bool. + :param dict order: dictionary representing the order. + :param bool draft: Indicate that the pos_order is not validated yet. :param existing_order: order to be updated or False. :type existing_order: pos.order. - :returns number pos_order id + :returns: id of created/updated pos.order + :rtype: int """ order = order['data'] pos_session = self.env['pos.session'].browse(order['pos_session_id']) @@ -126,6 +125,8 @@ class PosOrder(models.Model): order['user_id'] = pos_order.user_id.id pos_order.write(self._order_fields(order)) + pos_order = pos_order.with_company(pos_order.company_id) + self = self.with_company(pos_order.company_id) self._process_payment_lines(order, pos_order, pos_session, draft) if not draft: @@ -140,7 +141,8 @@ class PosOrder(models.Model): pos_order._create_order_picking() if pos_order.to_invoice and pos_order.state == 'paid': pos_order.action_pos_order_invoice() - pos_order.account_move.sudo().with_context(force_company=self.env.user.company_id.id).post() + pos_order.account_move.sudo().post() + return pos_order.id @@ -389,7 +391,8 @@ class PosOrder(models.Model): 'invoice_line_ids': [(0, None, order._prepare_invoice_line(line)) for line in order.lines], } new_move = moves.sudo()\ - .with_context(default_type=move_vals['type'], force_company=order.company_id.id)\ + .with_company(order.company_id)\ + .with_context(default_type=move_vals['type'])\ .create(move_vals) message = _("This invoice has been created from the point of sale session: %s") % (order.id, order.name) new_move.message_post(body=message) diff --git a/addons/point_of_sale/models/pos_session.py b/addons/point_of_sale/models/pos_session.py index c5cfe503200..64d50ef236b 100644 --- a/addons/point_of_sale/models/pos_session.py +++ b/addons/point_of_sale/models/pos_session.py @@ -578,7 +578,7 @@ class PosSession(models.Model): """ def get_income_account(order_line): product = order_line.product_id - income_account = product.with_context(force_company=order_line.company_id.id).property_account_income_id or product.categ_id.with_context(force_company=order_line.company_id.id).property_account_income_categ_id + income_account = product.with_company(order_line.company_id).property_account_income_id or product.categ_id.with_company(order_line.company_id).property_account_income_categ_id if not income_account: raise UserError(_('Please define income account for this product: "%s" (id:%d).') % (product.name, product.id)) diff --git a/addons/product/models/product.py b/addons/product/models/product.py index c13fa083c0f..96a0c7fbc82 100644 --- a/addons/product/models/product.py +++ b/addons/product/models/product.py @@ -579,8 +579,7 @@ class ProductProduct(models.Model): res = self.env['product.supplierinfo'] sellers = self._prepare_sellers(params) - if self.env.context.get('force_company'): - sellers = sellers.filtered(lambda s: not s.company_id or s.company_id.id == self.env.context['force_company']) + sellers = sellers.filtered(lambda s: not s.company_id or s.company_id.id == self.env.company.id) for seller in sellers: # Set quantity in UoM of seller quantity_uom_seller = quantity @@ -601,7 +600,7 @@ class ProductProduct(models.Model): res |= seller return res.sorted('price')[:1] - def price_compute(self, price_type, uom=False, currency=False, company=False): + def price_compute(self, price_type, uom=False, currency=False, company=None): # TDE FIXME: delegate to template or not ? fields are reencoded here ... # compatibility about context keys used a bit everywhere in the code if not uom and self._context.get('uom'): @@ -614,7 +613,7 @@ class ProductProduct(models.Model): # standard_price field can only be seen by users in base.group_user # Thus, in order to compute the sale price from the cost for users not in this group # We fetch the standard price as the superuser - products = self.with_context(force_company=company and company.id or self._context.get('force_company', self.env.company.id)).sudo() + products = self.with_company(company or self.env.company).sudo() prices = dict.fromkeys(self.ids, 0.0) for product in products: diff --git a/addons/product/models/product_pricelist.py b/addons/product/models/product_pricelist.py index dbcea6a714c..959fd87b993 100644 --- a/addons/product/models/product_pricelist.py +++ b/addons/product/models/product_pricelist.py @@ -332,7 +332,7 @@ class Pricelist(models.Model): # as we will do a search() later (real case for website public user). Partner = self.env['res.partner'].with_context(active_test=False) - Property = self.env['ir.property'].with_context(force_company=company_id or self.env.company.id) + Property = self.env['ir.property'].with_company(company_id) Pricelist = self.env['product.pricelist'] pl_domain = self._get_partner_pricelist_multi_search_domain_hook() diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index 093bd25f5ae..01316babeca 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -481,7 +481,7 @@ class ProductTemplate(models.Model): }, } - def price_compute(self, price_type, uom=False, currency=False, company=False): + def price_compute(self, price_type, uom=False, currency=False, company=None): # TDE FIXME: delegate to template or not ? fields are reencoded here ... # compatibility about context keys used a bit everywhere in the code if not uom and self._context.get('uom'): @@ -494,12 +494,9 @@ class ProductTemplate(models.Model): # standard_price field can only be seen by users in base.group_user # Thus, in order to compute the sale price from the cost for users not in this group # We fetch the standard price as the superuser - templates = self.with_context(force_company=company and company.id or self._context.get('force_company', self.env.company.id)).sudo() + templates = self.with_company(company).sudo() if not company: - if self._context.get('force_company'): - company = self.env['res.company'].browse(self._context['force_company']) - else: - company = self.env.company + company = self.env.company date = self.env.context.get('date') or fields.Date.today() prices = dict.fromkeys(self.ids, 0.0) diff --git a/addons/product/models/res_partner.py b/addons/product/models/res_partner.py index 4e9be575a77..786923ef5a4 100644 --- a/addons/product/models/res_partner.py +++ b/addons/product/models/res_partner.py @@ -15,9 +15,9 @@ class Partner(models.Model): help="This pricelist will be used, instead of the default one, for sales to the current partner") @api.depends('country_id') - @api.depends_context('force_company') + @api.depends_context('company') def _compute_product_pricelist(self): - company = self.env.context.get('force_company', False) + company = self.env.company.id res = self.env['product.pricelist']._get_partner_pricelist_multi(self.ids, company_id=company) for p in self: p.property_product_pricelist = res.get(p.id) @@ -33,7 +33,7 @@ class Partner(models.Model): # update at each change country, and so erase old pricelist if partner.property_product_pricelist or (actual and default_for_country and default_for_country.id != actual.id): # keep the company of the current user before sudo - self.env['ir.property'].with_context(force_company=self._context.get('force_company', self.env.company.id)).sudo().set_multi( + self.env['ir.property'].sudo().set_multi( 'property_product_pricelist', partner._name, {partner.id: partner.property_product_pricelist or default_for_country.id}, diff --git a/addons/product_margin/models/product_product.py b/addons/product_margin/models/product_product.py index 252dd42727e..3f5c607c987 100644 --- a/addons/product_margin/models/product_product.py +++ b/addons/product_margin/models/product_product.py @@ -100,10 +100,7 @@ class ProductProduct(models.Model): elif invoice_state == 'draft_open_paid': states = ('posted', 'draft') invoice_payment_states = ('not_paid', 'paid') - if "force_company" in self.env.context: - company_id = self.env.context['force_company'] - else: - company_id = self.env.company.id + company_id = self.env.company.id #Cost price is calculated afterwards as it is a property self.env['account.move.line'].flush(['price_unit', 'quantity', 'balance', 'product_id', 'display_type']) @@ -141,8 +138,6 @@ class ProductProduct(models.Model): res[val.id]['turnover'] = result[2] and -result[2] or 0.0 res[val.id]['sale_expected'] = result[3] and result[3] or 0.0 res[val.id]['sales_gap'] = res[val.id]['sale_expected'] - res[val.id]['turnover'] - ctx = self.env.context.copy() - ctx['force_company'] = company_id invoice_types = ('in_invoice', 'out_refund') self.env.cr.execute(sqlstr, (val.id, states, invoice_payment_states, invoice_types, date_from, date_to, company_id)) result = self.env.cr.fetchall()[0] diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index dac7ac1ece4..01d58569c11 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -200,7 +200,7 @@ class PurchaseOrder(models.Model): def onchange_partner_id(self): # Ensures all properties and fiscal positions # are taken with the company of the order - # if not defined, force_company doesn't change anything. + # if not defined, with_company doesn't change anything. self = self.with_context(force_company=self.company_id.id) if not self.partner_id: self.fiscal_position_id = False @@ -518,7 +518,7 @@ class PurchaseOrderLine(models.Model): def _compute_tax_id(self): for line in self: - fpos = line.order_id.fiscal_position_id or line.order_id.partner_id.with_context(force_company=line.company_id.id).property_account_position_id + fpos = line.order_id.fiscal_position_id or line.order_id.partner_id.with_company(line.company_id).property_account_position_id # If company_id is set, always filter taxes by the company taxes = line.product_id.supplier_taxes_id.filtered(lambda r: not line.company_id or r.company_id == line.company_id) line.taxes_id = fpos.map_tax(taxes, line.product_id, line.order_id.partner_id) if fpos else taxes diff --git a/addons/purchase_requisition_stock/models/stock.py b/addons/purchase_requisition_stock/models/stock.py index 54c9eae7049..6d2a20d72ad 100644 --- a/addons/purchase_requisition_stock/models/stock.py +++ b/addons/purchase_requisition_stock/models/stock.py @@ -21,7 +21,7 @@ class StockRule(models.Model): else: other_procurements.append((procurement, rule)) for company_id, requisitions_values in requisitions_values_by_company.items(): - self.env['purchase.requisition'].sudo().with_context(force_company=company_id).create(requisitions_values) + self.env['purchase.requisition'].sudo().with_company(company_id).create(requisitions_values) return super(StockRule, self)._run_buy(other_procurements) def _prepare_purchase_order(self, company_id, origins, values): diff --git a/addons/purchase_stock/models/stock_rule.py b/addons/purchase_stock/models/stock_rule.py index ea9d574195e..1bd553e9c52 100644 --- a/addons/purchase_stock/models/stock_rule.py +++ b/addons/purchase_stock/models/stock_rule.py @@ -76,7 +76,7 @@ class StockRule(models.Model): vals = rules[0]._prepare_purchase_order(company_id, origins, [p.values for p in procurements]) # The company_id is the same for all procurements since # _make_po_get_domain add the company in the domain. - po = self.env['purchase.order'].with_context(force_company=company_id.id).sudo().create(vals) + po = self.env['purchase.order'].with_company(company_id).sudo().create(vals) else: # If a purchase order is found, adapt its `origin` field. if po.origin: @@ -177,7 +177,7 @@ class StockRule(models.Model): def _update_purchase_order_line(self, product_id, product_qty, product_uom, company_id, values, line): partner = values['supplier'].name procurement_uom_po_qty = product_uom._compute_quantity(product_qty, product_id.uom_po_id) - seller = product_id.with_context(force_company=company_id.id)._select_seller( + seller = product_id.with_company(company_id)._select_seller( partner_id=partner, quantity=line.product_qty + procurement_uom_po_qty, date=line.order_id.date_order and line.order_id.date_order.date(), @@ -204,7 +204,7 @@ class StockRule(models.Model): procurement_uom_po_qty = product_uom._compute_quantity(product_qty, product_id.uom_po_id) # _select_seller is used if the supplier have different price depending # the quantities ordered. - seller = product_id.with_context(force_company=company_id.id)._select_seller( + seller = product_id.with_company(company_id)._select_seller( partner_id=partner, quantity=procurement_uom_po_qty, date=po.date_order and po.date_order.date(), @@ -266,7 +266,7 @@ class StockRule(models.Model): partner = values['supplier'].name purchase_date = schedule_date - relativedelta(days=int(values['supplier'].delay)) - fpos = self.env['account.fiscal.position'].with_context(force_company=company_id.id).get_fiscal_position(partner.id) + fpos = self.env['account.fiscal.position'].with_company(company_id).get_fiscal_position(partner.id) gpo = self.group_propagation_option group = (gpo == 'fixed' and self.group_id.id) or \ @@ -277,10 +277,10 @@ class StockRule(models.Model): 'user_id': False, 'picking_type_id': self.picking_type_id.id, 'company_id': company_id.id, - 'currency_id': partner.with_context(force_company=company_id.id).property_purchase_currency_id.id or company_id.currency_id.id, + 'currency_id': partner.with_company(company_id).property_purchase_currency_id.id or company_id.currency_id.id, 'dest_address_id': values.get('partner_id', False), 'origin': ', '.join(origins), - 'payment_term_id': partner.with_context(force_company=company_id.id).property_supplier_payment_term_id.id, + 'payment_term_id': partner.with_company(company_id).property_supplier_payment_term_id.id, 'date_order': purchase_date, 'fiscal_position_id': fpos, 'group_id': group diff --git a/addons/repair/models/repair.py b/addons/repair/models/repair.py index 2ce89ee3b5a..e6589b47af9 100644 --- a/addons/repair/models/repair.py +++ b/addons/repair/models/repair.py @@ -295,18 +295,18 @@ class Repair(models.Model): and not repair.invoice_id and repair.invoice_method != 'none') for repair in repairs: + repair = repair.with_company(repair.company_id) partner_invoice = repair.partner_invoice_id or repair.partner_id if not partner_invoice: raise UserError(_('You have to select an invoice address in the repair form.')) narration = repair.quotation_notes currency = repair.pricelist_id.currency_id - # Fallback on the user company as the 'company_id' is not required. - company = repair.company_id or self.env.user.company_id + company = repair.env.company - journal = self.env['account.move'].with_context(default_company_id=company.id, type='out_invoice')._get_default_journal() + journal = repair.env['account.move'].with_context(type='out_invoice')._get_default_journal() if not journal: - raise UserError(_('Please define an accounting sales journal for the company %s (%s).') % (self.company_id.name, self.company_id.id)) + raise UserError(_('Please define an accounting sales journal for the company %s (%s).') % (company.name, company.id)) if (partner_invoice.id, currency.id) not in grouped_invoices_vals: grouped_invoices_vals[(partner_invoice.id, currency.id, company.id)] = [] @@ -341,7 +341,7 @@ class Repair(models.Model): else: name = operation.name - account = operation.product_id.with_context(force_company=company.id).product_tmpl_id._get_product_accounts()['income'] + account = operation.product_id.product_tmpl_id._get_product_accounts()['income'] if not account: raise UserError(_('No account defined for product "%s".') % operation.product_id.name) @@ -364,7 +364,7 @@ class Repair(models.Model): }) else: amount_currency = -(operation.product_uom_qty * operation.price_unit) - balance = currency._convert(amount_currency, self.company_id.currency_id, self.company_id, fields.Date.today()) + balance = currency._convert(amount_currency, company.currency_id, company, fields.Date.today()) invoice_line_vals.update({ 'amount_currency': amount_currency, 'debit': balance > 0.0 and balance or 0.0, @@ -383,7 +383,7 @@ class Repair(models.Model): if not fee.product_id: raise UserError(_('No product defined on fees.')) - account = fee.product_id.product_tmpl_id.with_context(force_company=company.id)._get_product_accounts()['income'] + account = fee.product_id.product_tmpl_id._get_product_accounts()['income'] if not account: raise UserError(_('No account defined for product "%s".') % fee.product_id.name) @@ -406,7 +406,7 @@ class Repair(models.Model): }) else: amount_currency = -(fee.product_uom_qty * fee.price_unit) - balance = currency._convert(amount_currency, self.company_id.currency_id, self.company_id, + balance = currency._convert(amount_currency, company.currency_id, company, fields.Date.today()) invoice_line_vals.update({ 'amount_currency': amount_currency, @@ -423,7 +423,9 @@ class Repair(models.Model): invoices_vals_list_per_company[company_id].append(invoice) for company_id, invoices_vals_list in invoices_vals_list_per_company.items(): - self.env['account.move'].with_context(force_company=company_id, default_company_id=company_id, default_type='out_invoice').create(invoices_vals_list) + # VFE TODO remove the default_company_id ctxt key ? + # Account fallbacks on self.env.company, which is correct with with_company + self.env['account.move'].with_company(company_id).with_context(default_company_id=company_id, default_type='out_invoice').create(invoices_vals_list) repairs.write({'invoiced': True}) repairs.mapped('operations').filtered(lambda op: op.type == 'add').write({'invoiced': True}) @@ -503,7 +505,7 @@ class Repair(models.Model): 'location_id': operation.location_id.id, 'location_dest_id': operation.location_dest_id.id, 'move_line_ids': [(0, 0, {'product_id': operation.product_id.id, - 'lot_id': operation.lot_id.id, + 'lot_id': operation.lot_id.id, 'product_uom_qty': 0, # bypass reservation here 'product_uom_id': operation.product_uom.id, 'qty_done': operation.product_uom_qty, @@ -528,7 +530,7 @@ class Repair(models.Model): 'location_id': repair.location_id.id, 'location_dest_id': repair.location_id.id, 'move_line_ids': [(0, 0, {'product_id': repair.product_id.id, - 'lot_id': repair.lot_id.id, + 'lot_id': repair.lot_id.id, 'product_uom_qty': 0, # bypass reservation here 'product_uom_id': repair.product_uom.id or repair.product_id.uom_id.id, 'qty_done': repair.product_qty, diff --git a/addons/sale/models/payment.py b/addons/sale/models/payment.py index dc991441c2a..efa10be09bd 100644 --- a/addons/sale/models/payment.py +++ b/addons/sale/models/payment.py @@ -94,11 +94,10 @@ class PaymentTransaction(models.Model): default_template = self.env['ir.config_parameter'].sudo().get_param('sale.default_email_template') if default_template: for trans in self.filtered(lambda t: t.sale_order_ids): - ctx_company = {'company_id': trans.acquirer_id.company_id.id, - 'force_company': trans.acquirer_id.company_id.id, - 'mark_invoice_as_sent': True, - } - trans = trans.with_context(ctx_company) + trans = trans.with_company(trans.acquirer_id.company_id).with_context( + mark_invoice_as_sent=True, + company_id=trans.acquirer_id.company_id.id, + ) for invoice in trans.invoice_ids: invoice.message_post_with_template(int(default_template), email_layout_xmlid="mail.mail_notification_paynow") return res @@ -106,9 +105,8 @@ class PaymentTransaction(models.Model): def _invoice_sale_orders(self): if self.env['ir.config_parameter'].sudo().get_param('sale.automatic_invoice'): for trans in self.filtered(lambda t: t.sale_order_ids): - ctx_company = {'company_id': trans.acquirer_id.company_id.id, - 'force_company': trans.acquirer_id.company_id.id} - trans = trans.with_context(**ctx_company) + trans = trans.with_company(trans.acquirer_id.company_id)\ + .with_context(company_id=trans.acquirer_id.company_id.id) trans.sale_order_ids._force_lines_to_invoice_policy_order() invoices = trans.sale_order_ids._create_invoices() trans.invoice_ids = [(6, 0, invoices.ids)] diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py index 73753fac631..08c4f2f30f1 100644 --- a/addons/sale/models/sale.py +++ b/addons/sale/models/sale.py @@ -412,7 +412,7 @@ class SaleOrder(models.Model): if 'date_order' in vals: seq_date = fields.Datetime.context_timestamp(self, fields.Datetime.to_datetime(vals['date_order'])) if 'company_id' in vals: - vals['name'] = self.env['ir.sequence'].with_context(force_company=vals['company_id']).next_by_code( + vals['name'] = self.env['ir.sequence'].with_company(vals['company_id']).next_by_code( 'sale.order', sequence_date=seq_date) or _('New') else: vals['name'] = self.env['ir.sequence'].next_by_code('sale.order', sequence_date=seq_date) or _('New') @@ -492,7 +492,7 @@ class SaleOrder(models.Model): a clean extension chain). """ self.ensure_one() - journal = self.env['account.move'].with_context(force_company=self.company_id.id, default_type='out_invoice')._get_default_journal() + journal = self.env['account.move'].with_company(self.company_id).with_context(default_type='out_invoice')._get_default_journal() if not journal: raise UserError(_('Please define an accounting sales journal for the company %s (%s).') % (self.company_id.name, self.company_id.id)) diff --git a/addons/sale_purchase/models/sale_order.py b/addons/sale_purchase/models/sale_order.py index 74356834d64..d2244ad66f2 100644 --- a/addons/sale_purchase/models/sale_order.py +++ b/addons/sale_purchase/models/sale_order.py @@ -175,6 +175,7 @@ class SaleOrderLine(models.Model): :rtype: dict """ self.ensure_one() + self = self.with_context(force_company=self.company_id.id) partner_supplier = supplierinfo.name fiscal_position_id = self.env['account.fiscal.position'].sudo().get_fiscal_position(partner_supplier.id) date_order = self._purchase_get_date_order(supplierinfo) @@ -255,7 +256,7 @@ class SaleOrderLine(models.Model): supplier_po_map = {} sale_line_purchase_map = {} for line in self: - line = line.with_context(force_company=line.company_id.id) + line = line.with_company(line.company_id) # determine vendor of the order (take the first matching company and product) # VFE fixme why isn't the _select_seller function used ??? suppliers = line.product_id.seller_ids.filtered(lambda vendor: (not vendor.company_id or vendor.company_id == line.company_id) and (not vendor.product_id or vendor.product_id == line.product_id)) diff --git a/addons/sale_timesheet/models/sale_order.py b/addons/sale_timesheet/models/sale_order.py index fc871c98d39..b110cd082c4 100644 --- a/addons/sale_timesheet/models/sale_order.py +++ b/addons/sale_timesheet/models/sale_order.py @@ -83,9 +83,8 @@ class SaleOrder(models.Model): def _action_confirm(self): """ On SO confirmation, some lines should generate a task or a project. """ result = super(SaleOrder, self)._action_confirm() - self.mapped('order_line').sudo().with_context( - force_company=self.company_id.id, - )._timesheet_service_generation() + self.mapped('order_line').sudo() \ + .with_company(self.company_id)._timesheet_service_generation() return result def action_view_task(self): @@ -332,7 +331,7 @@ class SaleOrderLine(models.Model): # search the global project of current SO lines, in which create their task map_sol_project = {} if so_line_task_global_project: - map_sol_project = {sol.id: sol.product_id.with_context(force_company=sol.company_id.id).project_id for sol in so_line_task_global_project} + map_sol_project = {sol.id: sol.product_id.with_company(sol.company_id).project_id for sol in so_line_task_global_project} def _can_create_project(sol): if not sol.project_id: diff --git a/addons/stock/models/product.py b/addons/stock/models/product.py index 4f1b5b63fe4..67e215c90f7 100644 --- a/addons/stock/models/product.py +++ b/addons/stock/models/product.py @@ -94,7 +94,7 @@ class Product(models.Model): @api.depends('stock_move_ids.product_qty', 'stock_move_ids.state') @api.depends_context( 'lot_id', 'owner_id', 'package_id', 'from_date', 'to_date', - 'company_owned', 'force_company', + 'company_owned', ) def _compute_quantities(self): products = self.filtered(lambda p: p.type != 'service') @@ -226,7 +226,7 @@ class Product(models.Model): ''' Parses the context and returns a list of location_ids based on it. It will return all stock locations when no parameters are given - Possible parameters are shop, warehouse, location, force_company, compute_child + Possible parameters are shop, warehouse, location, compute_child ''' Warehouse = self.env['stock.warehouse'] @@ -251,7 +251,7 @@ class Product(models.Model): ('location_dest_id.company_id', '=', company_id), ] ) - def _search_ids(model, values, force_company_id): + def _search_ids(model, values): ids = set() domain = [] for item in values: @@ -260,8 +260,6 @@ class Product(models.Model): else: domain = expression.OR([[('name', 'ilike', item)], domain]) if domain: - if force_company_id: - domain = expression.AND([[('company_id', '=', force_company_id)], domain]) ids |= set(self.env[model].search(domain).ids) return ids @@ -274,22 +272,21 @@ class Product(models.Model): warehouse = self.env.context.get('warehouse') if warehouse and not isinstance(warehouse, list): warehouse = [warehouse] - force_company = self.env.context.get('force_company', False) # filter by location and/or warehouse if warehouse: - w_ids = set(Warehouse.browse(_search_ids('stock.warehouse', warehouse, force_company)).mapped('view_location_id').ids) + w_ids = set(Warehouse.browse(_search_ids('stock.warehouse', warehouse)).mapped('view_location_id').ids) if location: - l_ids = _search_ids('stock.location', location, force_company) + l_ids = _search_ids('stock.location', location) location_ids = w_ids & l_ids else: location_ids = w_ids else: if location: - location_ids = _search_ids('stock.location', location, force_company) + location_ids = _search_ids('stock.location', location) else: location_ids = set(Warehouse.search([]).mapped('view_location_id').ids) - return self._get_domain_locations_new(location_ids, company_id=self.env.context.get('force_company', False), compute_child=self.env.context.get('compute_child', True)) + return self._get_domain_locations_new(location_ids, compute_child=self.env.context.get('compute_child', True)) def _get_domain_locations_new(self, location_ids, company_id=False, compute_child=True): operator = compute_child and 'child_of' or 'in' @@ -619,7 +616,7 @@ class ProductTemplate(models.Model): 'product_variant_ids.stock_move_ids.product_qty', 'product_variant_ids.stock_move_ids.state', ) - @api.depends_context('company_owned', 'force_company') + @api.depends_context('company_owned', 'company') def _compute_quantities(self): res = self._compute_quantities_dict() for template in self: diff --git a/addons/stock/models/res_company.py b/addons/stock/models/res_company.py index a219958b545..6f97e54fbcb 100644 --- a/addons/stock/models/res_company.py +++ b/addons/stock/models/res_company.py @@ -40,7 +40,7 @@ class Company(models.Model): company.write({'internal_transit_location_id': location.id}) - company.partner_id.with_context(force_company=company.id).write({ + company.partner_id.with_company(company).write({ 'property_stock_customer': location.id, 'property_stock_supplier': location.id, }) diff --git a/addons/stock/models/stock_inventory.py b/addons/stock/models/stock_inventory.py index affb45bccdc..5a701b87056 100644 --- a/addons/stock/models/stock_inventory.py +++ b/addons/stock/models/stock_inventory.py @@ -472,7 +472,7 @@ class InventoryLine(models.Model): } def _get_virtual_location(self): - return self.product_id.with_context(force_company=self.company_id.id).property_stock_inventory + return self.product_id.with_company(self.company_id).property_stock_inventory def _generate_moves(self): vals_list = [] diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py index ee407987709..4a95587f4da 100644 --- a/addons/stock/models/stock_move.py +++ b/addons/stock/models/stock_move.py @@ -667,7 +667,7 @@ class StockMove(models.Model): domain = [('location_src_id', '=', move.location_dest_id.id), ('action', 'in', ('push', 'pull_push'))] # first priority goes to the preferred routes defined on the move itself (e.g. coming from a SO line) warehouse_id = move.warehouse_id or move.picking_id.picking_type_id.warehouse_id - if not self.env.context.get('force_company', False) and move.location_dest_id.company_id == self.env.user.company_id: + if move.location_dest_id.company_id == self.env.company: rules = self.env['procurement.group']._search_rule(move.route_ids, move.product_id, warehouse_id, domain) else: rules = self.sudo().env['procurement.group']._search_rule(move.route_ids, move.product_id, warehouse_id, domain) diff --git a/addons/stock/models/stock_quant.py b/addons/stock/models/stock_quant.py index 6bd89d968f4..6f13cb8b6ad 100644 --- a/addons/stock/models/stock_quant.py +++ b/addons/stock/models/stock_quant.py @@ -113,9 +113,9 @@ class StockQuant(models.Model): if diff_float_compared == 0: continue elif diff_float_compared > 0: - move_vals = quant._get_inventory_move_values(diff, quant.product_id.with_context(force_company=quant.company_id.id or self.env.company.id).property_stock_inventory, quant.location_id) + move_vals = quant._get_inventory_move_values(diff, quant.product_id.with_company(quant.company_id).property_stock_inventory, quant.location_id) else: - move_vals = quant._get_inventory_move_values(-diff, quant.location_id, quant.product_id.with_context(force_company=quant.company_id.id or self.env.company.id).property_stock_inventory, out=True) + move_vals = quant._get_inventory_move_values(-diff, quant.location_id, quant.product_id.with_company(quant.company_id).property_stock_inventory, out=True) move = quant.env['stock.move'].with_context(inventory_mode=False).create(move_vals) move._action_done() diff --git a/addons/stock/models/stock_rule.py b/addons/stock/models/stock_rule.py index 4ce280e4764..b8add883f27 100644 --- a/addons/stock/models/stock_rule.py +++ b/addons/stock/models/stock_rule.py @@ -233,7 +233,7 @@ class StockRule(models.Model): for company_id, moves_values in moves_values_by_company.items(): # create the move as SUPERUSER because the current user may not have the rights to do it (mto product launched by a sale for example) - moves = self.env['stock.move'].sudo().with_context(force_company=company_id).create(moves_values) + moves = self.env['stock.move'].sudo().with_company(company_id).create(moves_values) # Since action_confirm launch following procurement_group we should activate it. moves._action_confirm() return True @@ -508,19 +508,13 @@ class ProcurementGroup(models.Model): return domain @api.model - def _procure_orderpoint_confirm(self, use_new_cursor=False, company_id=False): + def _procure_orderpoint_confirm(self, use_new_cursor=False, company_id=None): """ Create procurements based on orderpoints. :param bool use_new_cursor: if set, use a dedicated cursor and auto-commit after processing 1000 orderpoints. This is appropriate for batch jobs only. """ - if company_id and self.env.company.id != company_id: - # To ensure that the company_id is taken into account for - # all the processes triggered by this method - # i.e. If a PO is generated by the run of the procurements the - # sequence to use is the one for the specified company not the - # one of the user's company - self = self.with_context(company_id=company_id, force_company=company_id) + self = self.with_company(company_id) OrderPoint = self.env['stock.warehouse.orderpoint'] domain = self._get_orderpoint_domain(company_id=company_id) orderpoints_noprefetch = OrderPoint.with_context(prefetch_fields=False).search(domain, diff --git a/addons/stock/models/stock_warehouse.py b/addons/stock/models/stock_warehouse.py index 6fbdef3beb0..63292df54ab 100644 --- a/addons/stock/models/stock_warehouse.py +++ b/addons/stock/models/stock_warehouse.py @@ -257,7 +257,7 @@ class Warehouse(models.Model): ResCompany = self.env['res.company'] if company_id: transit_loc = ResCompany.browse(company_id).internal_transit_location_id.id - self.env['res.partner'].browse(partner_id).with_context(force_company=company_id).write({'property_stock_customer': transit_loc, 'property_stock_supplier': transit_loc}) + self.env['res.partner'].browse(partner_id).with_company(company_id).write({'property_stock_customer': transit_loc, 'property_stock_supplier': transit_loc}) else: transit_loc = self.env.company.internal_transit_location_id.id self.env['res.partner'].browse(partner_id).write({'property_stock_customer': transit_loc, 'property_stock_supplier': transit_loc}) diff --git a/addons/stock/tests/test_stock_flow.py b/addons/stock/tests/test_stock_flow.py index 39df9e26a5b..425b9bb580e 100644 --- a/addons/stock/tests/test_stock_flow.py +++ b/addons/stock/tests/test_stock_flow.py @@ -1851,9 +1851,9 @@ class TestStockFlow(TestStockCommon): def test_transit_multi_companies_ultimate(self): """ Ensure that inter company rules set the correct company on picking and their moves. This test validate a picking with make_to_order moves. - Moves are created in batch with a force_company. This test should create - moves for company_2 and company_3 at the same time. Ensure they are not - create in the same batch. + Moves are created in batch with a company-focused environment. This test + should create moves for company_2 and company_3 at the same time. + Ensure they are not create in the same batch. """ grp_multi_loc = self.env.ref('stock.group_stock_multi_locations') grp_multi_routes = self.env.ref('stock.group_adv_location') diff --git a/addons/stock/wizard/product_replenish.py b/addons/stock/wizard/product_replenish.py index 96362497165..735020e07b2 100644 --- a/addons/stock/wizard/product_replenish.py +++ b/addons/stock/wizard/product_replenish.py @@ -76,7 +76,7 @@ class ProductReplenish(models.TransientModel): def _prepare_run_values(self): replenishment = self.env['procurement.group'].create({ - 'partner_id': self.product_id.with_context(force_company=self.company_id.id).responsible_id.partner_id.id, + 'partner_id': self.product_id.with_company(self.company_id).responsible_id.partner_id.id, }) values = { diff --git a/addons/stock_account/models/product.py b/addons/stock_account/models/product.py index e1076cfb504..8a35c7c63ee 100644 --- a/addons/stock_account/models/product.py +++ b/addons/stock_account/models/product.py @@ -97,10 +97,10 @@ class ProductProduct(models.Model): stock_valuation_layer_ids = fields.One2many('stock.valuation.layer', 'product_id') @api.depends('stock_valuation_layer_ids') - @api.depends_context('to_date', 'force_company') + @api.depends_context('to_date', 'company') def _compute_value_svl(self): """Compute `value_svl` and `quantity_svl`.""" - company_id = self.env.context.get('force_company', self.env.company.id) + company_id = self.env.company.id domain = [ ('product_id', 'in', self.ids), ('company_id', '=', company_id), @@ -242,7 +242,7 @@ class ProductProduct(models.Model): account_moves.post() # Actually update the standard price. - self.with_context(force_company=company_id.id).sudo().write({'standard_price': new_price}) + self.with_company(company_id).sudo().write({'standard_price': new_price}) def _run_fifo(self, quantity, company): self.ensure_one() @@ -279,7 +279,7 @@ class ProductProduct(models.Model): # Update the standard price with the price of the last used candidate, if any. if new_standard_price and self.cost_method == 'fifo': - self.sudo().with_context(force_company=company.id).standard_price = new_standard_price + self.sudo().with_company(company.id).standard_price = new_standard_price # If there's still quantity to value but we're out of candidates, we fall in the # negative stock use case. We chose to value the out move at the price of the @@ -724,4 +724,3 @@ class ProductCategory(models.Model): account_moves = self.env['account.move'].create(move_vals_list) account_moves.post() return res - diff --git a/addons/stock_account/models/stock_move.py b/addons/stock_account/models/stock_move.py index 7e05dd901c3..16f9fafcef4 100644 --- a/addons/stock_account/models/stock_move.py +++ b/addons/stock_account/models/stock_move.py @@ -148,7 +148,7 @@ class StockMove(models.Model): """ svl_vals_list = [] for move in self: - move = move.with_context(force_company=move.company_id.id) + move = move.with_company(move.company_id) valued_move_lines = move._get_in_move_lines() valued_quantity = 0 for valued_move_line in valued_move_lines: @@ -171,7 +171,7 @@ class StockMove(models.Model): """ svl_vals_list = [] for move in self: - move = move.with_context(force_company=move.company_id.id) + move = move.with_company(move.company_id) valued_move_lines = move._get_out_move_lines() valued_quantity = 0 for valued_move_line in valued_move_lines: @@ -193,7 +193,7 @@ class StockMove(models.Model): """ svl_vals_list = [] for move in self: - move = move.with_context(force_company=move.company_id.id) + move = move.with_company(move.company_id) valued_move_lines = move.move_line_ids valued_quantity = 0 for valued_move_line in valued_move_lines: @@ -303,8 +303,8 @@ class StockMove(models.Model): tmpl_dict = defaultdict(lambda: 0.0) # adapt standard price on incomming moves if the product cost_method is 'average' std_price_update = {} - for move in self.filtered(lambda move: move._is_in() and move.with_context(force_company=move.company_id.id).product_id.cost_method == 'average'): - product_tot_qty_available = move.product_id.with_context(force_company=move.company_id.id).quantity_svl + tmpl_dict[move.product_id.id] + for move in self.filtered(lambda move: move._is_in() and move.with_company(move.company_id).product_id.cost_method == 'average'): + product_tot_qty_available = move.product_id.with_company(move.company_id).quantity_svl + tmpl_dict[move.product_id.id] rounding = move.product_id.uom_id.rounding valued_move_lines = move._get_in_move_lines() @@ -320,19 +320,19 @@ class StockMove(models.Model): new_std_price = move._get_price_unit() else: # Get the standard price - amount_unit = std_price_update.get((move.company_id.id, move.product_id.id)) or move.product_id.with_context(force_company=move.company_id.id).standard_price + amount_unit = std_price_update.get((move.company_id.id, move.product_id.id)) or move.product_id.with_company(move.company_id).standard_price new_std_price = ((amount_unit * product_tot_qty_available) + (move._get_price_unit() * qty)) / (product_tot_qty_available + qty) tmpl_dict[move.product_id.id] += qty_done # Write the standard price, as SUPERUSER_ID because a warehouse manager may not have the right to write on products - move.product_id.with_context(force_company=move.company_id.id).sudo().write({'standard_price': new_std_price}) + move.product_id.with_company(move.company_id.id).sudo().write({'standard_price': new_std_price}) std_price_update[move.company_id.id, move.product_id.id] = new_std_price def _get_accounting_data_for_valuation(self): """ Return the accounts and journal to use to post Journal Entries for the real-time valuation of the quant. """ self.ensure_one() - self = self.with_context(force_company=self.company_id.id) + self = self.with_company(self.company_id) accounts_data = self.product_id.product_tmpl_id.get_product_accounts() if self.location_id.valuation_out_account_id: @@ -473,34 +473,34 @@ class StockMove(models.Model): if self._is_in(): journal_id, acc_src, acc_dest, acc_valuation = self._get_accounting_data_for_valuation() if location_from and location_from.usage == 'customer': # goods returned from customer - self.with_context(force_company=company_to.id)._create_account_move_line(acc_dest, acc_valuation, journal_id, qty, description, svl_id, cost) + self.with_company(company_to)._create_account_move_line(acc_dest, acc_valuation, journal_id, qty, description, svl_id, cost) else: - self.with_context(force_company=company_to.id)._create_account_move_line(acc_src, acc_valuation, journal_id, qty, description, svl_id, cost) + self.with_company(company_to)._create_account_move_line(acc_src, acc_valuation, journal_id, qty, description, svl_id, cost) # Create Journal Entry for products leaving the company if self._is_out(): cost = -1 * cost journal_id, acc_src, acc_dest, acc_valuation = self._get_accounting_data_for_valuation() if location_to and location_to.usage == 'supplier': # goods returned to supplier - self.with_context(force_company=company_from.id)._create_account_move_line(acc_valuation, acc_src, journal_id, qty, description, svl_id, cost) + self.with_company(company_from)._create_account_move_line(acc_valuation, acc_src, journal_id, qty, description, svl_id, cost) else: - self.with_context(force_company=company_from.id)._create_account_move_line(acc_valuation, acc_dest, journal_id, qty, description, svl_id, cost) + self.with_company(company_from)._create_account_move_line(acc_valuation, acc_dest, journal_id, qty, description, svl_id, cost) if self.company_id.anglo_saxon_accounting: # Creates an account entry from stock_input to stock_output on a dropship move. https://github.com/odoo/odoo/issues/12687 journal_id, acc_src, acc_dest, acc_valuation = self._get_accounting_data_for_valuation() if self._is_dropshipped(): if cost > 0: - self.with_context(force_company=self.company_id.id)._create_account_move_line(acc_src, acc_valuation, journal_id, qty, description, svl_id, cost) + self.with_company(self.company_id)._create_account_move_line(acc_src, acc_valuation, journal_id, qty, description, svl_id, cost) else: cost = -1 * cost - self.with_context(force_company=self.company_id.id)._create_account_move_line(acc_valuation, acc_dest, journal_id, qty, description, svl_id, cost) + self.with_company(self.company_id)._create_account_move_line(acc_valuation, acc_dest, journal_id, qty, description, svl_id, cost) elif self._is_dropshipped_returned(): if cost > 0: - self.with_context(force_company=self.company_id.id)._create_account_move_line(acc_valuation, acc_src, journal_id, qty, description, svl_id, cost) + self.with_company(self.company_id)._create_account_move_line(acc_valuation, acc_src, journal_id, qty, description, svl_id, cost) else: cost = -1 * cost - self.with_context(force_company=self.company_id.id)._create_account_move_line(acc_dest, acc_valuation, journal_id, qty, description, svl_id, cost) + self.with_company(self.company_id)._create_account_move_line(acc_dest, acc_valuation, journal_id, qty, description, svl_id, cost) if self.company_id.anglo_saxon_accounting: #eventually reconcile together the invoice and valuation accounting entries on the stock interim accounts @@ -511,4 +511,3 @@ class StockMove(models.Model): to the way they mix stock moves with invoices. """ return self.env['account.move'] - diff --git a/addons/stock_landed_costs/models/stock_landed_cost.py b/addons/stock_landed_costs/models/stock_landed_cost.py index 0a3e4368835..44c6063efe6 100644 --- a/addons/stock_landed_costs/models/stock_landed_cost.py +++ b/addons/stock_landed_costs/models/stock_landed_cost.py @@ -144,7 +144,7 @@ class LandedCost(models.Model): # Update the AVCO product = line.move_id.product_id if product.cost_method == 'average' and not float_is_zero(product.quantity_svl, precision_rounding=product.uom_id.rounding): - product.with_context(force_company=self.company_id.id).sudo().standard_price += cost_to_add / product.quantity_svl + product.with_company(self.company_id).sudo().standard_price += cost_to_add / product.quantity_svl # `remaining_qty` is negative if the move is out and delivered proudcts that were not # in stock. qty_out = 0 diff --git a/addons/website_sale/models/sale_order.py b/addons/website_sale/models/sale_order.py index 58162810309..84bc296ac69 100644 --- a/addons/website_sale/models/sale_order.py +++ b/addons/website_sale/models/sale_order.py @@ -100,9 +100,8 @@ class SaleOrder(models.Model): 'quantity': qty, 'date': order.date_order, 'pricelist': order.pricelist_id.id, - 'force_company': order.company_id.id, }) - product = self.env['product.product'].with_context(product_context).browse(product_id) + product = self.env['product.product'].with_context(product_context).with_company(order.company_id.id).browse(product_id) discount = 0 if order.pricelist_id.discount_policy == 'without_discount': diff --git a/addons/website_sale/models/website.py b/addons/website_sale/models/website.py index dea48782d02..89c7dcdcd08 100644 --- a/addons/website_sale/models/website.py +++ b/addons/website_sale/models/website.py @@ -274,7 +274,7 @@ class Website(models.Model): # TODO cache partner_id session pricelist = self.env['product.pricelist'].browse(pricelist_id).sudo() so_data = self._prepare_sale_order_values(partner, pricelist) - sale_order = self.env['sale.order'].with_context(force_company=request.website.company_id.id).sudo().create(so_data) + sale_order = self.env['sale.order'].with_company(request.website.company_id.id).sudo().create(so_data) # set fiscal position if request.website.partner_id.id != partner.id: @@ -283,7 +283,7 @@ class Website(models.Model): country_code = request.session['geoip'].get('country_code') if country_code: country_id = request.env['res.country'].search([('code', '=', country_code)], limit=1).id - fp_id = request.env['account.fiscal.position'].sudo().with_context(force_company=request.website.company_id.id)._get_fpos_by_region(country_id) + fp_id = request.env['account.fiscal.position'].sudo().with_company(request.website.company_id.id)._get_fpos_by_region(country_id) sale_order.fiscal_position_id = fp_id else: # if no geolocation, use the public user fp diff --git a/addons/website_sale/tests/test_website_sale_pricelist.py b/addons/website_sale/tests/test_website_sale_pricelist.py index 547559037f1..36b742a6782 100644 --- a/addons/website_sale/tests/test_website_sale_pricelist.py +++ b/addons/website_sale/tests/test_website_sale_pricelist.py @@ -433,8 +433,8 @@ class TestWebsitePriceListMultiCompany(TransactionCaseWithUserDemo): self.demo_user.partner_id.property_product_pricelist = self.c2_pl # Ensure everything was done correctly - self.assertEqual(self.demo_user.partner_id.with_context(force_company=self.company1.id).property_product_pricelist, self.c1_pl) - self.assertEqual(self.demo_user.partner_id.with_context(force_company=self.company2.id).property_product_pricelist, self.c2_pl) + self.assertEqual(self.demo_user.partner_id.with_company(self.company1.id).property_product_pricelist, self.c1_pl) + self.assertEqual(self.demo_user.partner_id.with_company(self.company2.id).property_product_pricelist, self.c2_pl) irp1 = self.env['ir.property'].search([ ('name', '=', 'property_product_pricelist'), ('company_id', '=', self.company1.id), @@ -471,7 +471,7 @@ class TestWebsitePriceListMultiCompany(TransactionCaseWithUserDemo): # First check: It should return ir.property,4 as company_id is # website.company_id and not env.user.company_id company_id = self.website.company_id.id - partner = self.demo_user.partner_id.with_context(force_company=company_id) + partner = self.demo_user.partner_id.with_company(company_id) demo_pl = partner.property_product_pricelist self.assertEqual(demo_pl, self.c2_pl)