# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# Generated by the Odoo plugin for Dia !
from openerp import api, fields, models
from odoo.exceptions import ValidationError, UserError

class persona(models.Model):
    """Modelo utilizado para el regsitro de nuevas personas"""
    _name = 'persona'
    _rec_name = 'cedula'
    _order = 'apellidos'

    cedula = fields.Char(string="Cedula", size=8, help="Ingrese la cedula")
    nombres = fields.Char(string="Nombres", help="Ingrese primer y segundo nombre")
    apellidos = fields.Char(string="Apellidos", help="Ingrese primer y segundo apellido")
    direccion = fields.Text(string="Direccion", help="Ingrese la direccion")
    sexo = fields.Selection([('M','Masculino'),('F','Femenino')], string="Sexo", help="Ingrese su sexo")
    fecha_nacimiento = fields.Date(string="Fecha de nacimiento", help="Ingrese su fecha de nacimiento")
    ingreso_mensual = fields.Float(string="Ingreso mensual", help="Especifique su ingreso mensual")
    mensaje = fields.Html(string="Mensaje", help="Ingresa un mensaje")
    active = fields.Boolean(string="Activo", help="Activar o desactivar el registro")
    foto = fields.Binary(string="Fotografia")

    state_id = fields.Many2one('res.country.state', string="Estado")
    municipality_id = fields.Many2one('res.country.state.municipality', string="Municipio")
    parish_id = fields.Many2one('res.country.state.municipality.parish', string="Parroquia")
    telefono_ids = fields.One2many('persona.telefono', 'persona_id', size=11, string="Telefonos")
    deporte_ids = fields.Many2many('persona.deporte', string="Deportes")


    @api.multi
    def desactivar(self):
        for record in self:
            self.active = False

    @api.onchange('state_id')
    def _onchange_state_id(self):
        self.municipality_id = ""

    @api.onchange('municipality_id')
    def _onchange_municipality_id(self):
        self.parish_id = ""

    @api.constrains('fecha_nacimiento')
    def _check_fecha_nacimiento(self):
        if self.fecha_nacimiento > fields.Datetime.now():
            raise ValidationError("La fecha de nacimiento (%s) no puede ser mayor a la fecha actual (%s)" % (self.fecha_nacimiento,fields.Datetime.now()))


class PersonaTelefono(models.Model):
    _name = "persona.telefono"

    persona_id = fields.Many2one('persona')
    telefono = fields.Char('Telefono', size=11)

class PersonaDeporte(models.Model):
    _name = "persona.deporte"

    nombre = fields.Char('Nombre')
    descripcion = fields.Text('Descripcion')