from odoo import api, fields, models class CommContractLine(models.Model): _name = 'comm.contractline' _description = 'Услуга договора аренды' _order = 'period,product_name' name = fields.Char(string='Название услуги', compute='_compute_name', store=True) product_id = fields.Many2one('product.product', string='Услуга') product_name = fields.Char( related='product_id.name', store=True, readonly=True, ) counter_id = fields.Many2one('comm.counter', string='Счётчик') price = fields.Float(string='Цена', digits='Product Price') tax_id = fields.Many2one('account.tax', string='НДС') period = fields.Selection( [ ('month', 'Месяц'), ('quarter', 'Квартал'), ('year', 'Год'), ], string='Период', default='month', ) contract_id = fields.Many2one('comm.contract', string='Номер договора') @api.depends('product_id', 'product_id.name') def _compute_name(self): for rec in self: rec.name = rec.product_id.name or '' @api.onchange('product_id') def _onchange_product_id(self): for rec in self: rec.tax_id = rec.product_id.taxes_id[:1] rec.price = rec.product_id.lst_price def write(self, values): if self.counter_id and self.contract_id: self.counter_id.contract_id = self.contract_id return super().write(values) @api.model_create_multi def create(self, vals_list): records = super().create(vals_list) for record in records: if record.contract_id and record.counter_id: record.counter_id.contract_id = record.contract_id return records @api.model def connect_counters(self): for contract in self.env['comm.contract'].search([]): for line in contract.line_ids: if line.counter_id: line.counter_id.contract_id = contract