Переглянути джерело

Agregar edad en ficha de clientes

SEBAS 2 днів тому
батько
коміт
46e86427a8
2 змінених файлів з 13 додано та 0 видалено
  1. 12 0
      partner_extra_data.py
  2. 1 0
      partner_extra_data_view.xml

+ 12 - 0
partner_extra_data.py

@@ -20,6 +20,7 @@
 ##############################################################################
 
 from openerp import models, fields, tools, api, _
+from datetime import date, datetime
 
 class res_partner(models.Model):
     _name = 'res.partner'
@@ -53,7 +54,18 @@ class res_partner(models.Model):
     trab_street = fields.Char('Dirección Laboral')
     trab_city = fields.Char('Ciudad donde trabaja')
     ubicacion = fields.Char('Ubicación Google Map')
+    edad = fields.Integer('Edad', compute='_compute_edad', store=True)
 
+    @api.depends('fecha_nac')
+    def _compute_edad(self):
+        """ Calcula la edad basada en la fecha de nacimiento """
+        today = date.today()
+        for record in self:
+            if record.fecha_nac:
+                birth_date = fields.Date.from_string(record.fecha_nac)
+                record.edad = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
+            else:
+                record.edad = 0
 
     _sql_constraints = [
         ('ruc_uniq', 'unique (ruc)', 'El número de R.U.C./C.I. debe ser único!')

+ 1 - 0
partner_extra_data_view.xml

@@ -10,6 +10,7 @@
                     <field name="ruc" attrs="{'invisible':[('customer','=',False),('supplier','=',False)]}"/>
                     <field name="sexo" attrs="{'invisible':[('is_company','=',True)]}"/>
                     <field name="fecha_nac" attrs="{'invisible':[('is_company','=',True)]}" options="{'datepicker':{'yearRange': 'c-60:c+0'}}"/>
+                    <field name="edad" attrs="{'invisible':[('is_company','=',True)],'required':[('is_company','=',False)]}"/>
                     <field name="ubicacion" widget="url"/>
                 </field>
                 <button name="open_parent" position="replace">