# -*- encoding: utf-8 -*- from openerp import models, fields, api, tools from openerp.exceptions import ValidationError class CarService(models.Model): _name = 'car.service' name=fields.Char('Nombre', compute='_get_vehicle_name', store=True) model=fields.Char('Modelo') year=fields.Integer('Año') color=fields.Char('Color') number=fields.Char('Número de chapa') logo = fields.Binary('Logo File') partner_id = fields.Many2one( 'res.partner', string='Cliente', ondelete='restrict' ) description = fields.Text('Description', translate=True) policy = fields.Boolean() policy_number =fields.Char('Número de chapa') agent_id = fields.Many2one( 'res.partner', string='Agente', ondelete='restrict' ) secure_id = fields.Many2one( 'res.partner', string='Aseguradora', ondelete='restrict' ) odometer = fields.Float('Odometro') car_value = fields.Float('Valor del vehiculo') transmission = fields.Selection([('manual','Manual'), ('automatic','Automatico')], string="Tipo de transmision") fuel_type = fields.Selection([('gasoline','Gasolina'), ('diesel','Diesel'), ('electric','Electrico'), ('hybrid','Hibrido')], string="Tipo de combustible") seats = fields.Integer('Nro. de asientos') doors = fields.Integer('Nro. de puertas') car_brand_id = fields.Many2one( 'car.brand', string='Marca' ) @api.one @api.constrains('number') def _controla_chapa(self): part_obj = self.env['car.service'].search([('number','=',self.number)]) if len(part_obj) > 1: raise ValidationError('La chapa del vehículo debe ser único') @api.one @api.depends('car_brand_id','model','number') def _get_vehicle_name(self): self.name = self.car_brand_id.name + ' / ' + self.model + ' / ' + self.number class CarBrand(models.Model): _name = 'car.brand' name = fields.Char('Brand Name', required=True) description = fields.Text('Description', translate=True) logo = fields.Binary('Logo File') car_ids = fields.One2many( 'car.service', 'car_brand_id', ) car_count = fields.Integer( string='Numero de autos', compute='_get_car_count', ) @api.one @api.depends('car_ids') def _get_car_count(self): self.car_count = len(self.car_ids)