Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

I cannot get values from the form of one2many.

By
Cpueyo
on 3/13/13, 3:15 PM 2,870 views

Hello!

I have a two relations of one2many nested. I compute results in the last form, and try to get this values form the second form, for compute it, and the next is get from the first view form for compute.

I try in create and write method, but I can not call them until I'm on the main form. It's confuse i know. I will try to explain with the code.

I try too with a button to update, but i don't know how to recover fields.

Main Form & Second Form - Python File

import pdb
import time
from datetime import datetime
from osv import fields, osv

class account_analytic_account(osv.Model):
    _inherit = 'account.analytic.account'
    _columns = {
        'journal_plan_id': fields.one2many('project.costes.plan', 'contract_id', 'Costes planificados en contrato'),
    }

class project_costes_plan(osv.Model):
    _name = 'project.costes.plan'
    _description = 'Tabla de costes planificados por contrato'
    _columns = {
        'contract_id': fields.many2one('account.analytic.account', 'Costes planificados en contrato', select=True, ondelete='cascade'),
        'project_id': fields.one2many('project.costes.plan.project', 'project_id', 'Costes planificados en proyecto'),
#        'tasks_id': fields.one2many('project.costes.plan.tasks', 'tasks_id', 'Costes planificados por tareas'),
        'concepto': fields.char('Breve descripcion'),
        'version': fields.char('Version'),
        'estado': fields.boolean('Activa'),
        'plan_coste': fields.float('Total planificado', digits=(12,2), required=True),
        'plan_hours': fields.float('Total horas planificadas', digits=(12,2), required=True),
    }
    _defaults = {
        'version': '1.0',
        'estado': False,
        'plan_coste': 0.0,
        'plan_hours': 0.0,
    }

    def actualizar_costes(self, cr, uid, ids, context = None):
        osv.logging.log(100, "[Alca] actualizar_costes:project_costes_plan:project_costes "+str(ids))
        pdb.set_trace()
        return False

project_costes_plan()

Main Form & Second Form - XML File

<openerp>
    <data>
        <record id="view_account_analytic_account_costes_form" model="ir.ui.view">
            <field name="name">analytic.analytic.account.form</field>
            <field name="model">account.analytic.account</field>
            <field name="inherit_id" ref="analytic.view_account_analytic_account_form" />
            <field name="arch" type="xml">
                <notebook position="inside">
                    <page string="Costes Planificados" version="7.0">
                        <field name="journal_plan_id" context="{'form_view_ref':'view_project_costes_plan_form', 'tree_view_ref':'view_project_costes_plan_tree','search_view_ref':'view_project_costes_plan_search'}" />
                    </page>
                </notebook>
            </field>
        </record>

        <record id="view_project_costes_plan_form" model="ir.ui.view">
            <field name="name">project.costes.plan.form</field>
            <field name="model">project.costes.plan</field>
            <field name="arch" type="xml">
                <form string="Costes Planificados" version="7.0">
                    <sheet>
                        <h1>
                            <field name="concepto" placeholder="Descripcion breve"/>
                        </h1>
                        <group colspan="2">
                            <group col="2">
                                <field name="version" />
                                <field name="estado" />
                            </group>
                            <group col="2">
                                <field name="plan_coste" class="soloLectura" />
                                <field name="plan_hours" class="soloLectura" />
                                <button name="actualizar_costes" string="Actualizar Costes" type="object" class="oe_highlight"/>
                            </group>
                        </group>
                        <notebook>
                            <page string="Proyecto">
                                <!-- Formulario -->
                                <field name="project_id" context="{'form_view_ref': 'view_project_costes_plan_project_form','tree_view_ref':'view_project_costes_plan_project_tree'}" />
                            </page>
                            <!--<page string="Tareas">-->
                                <!-- Arbol con Formulario -->
                                <!--<field name="tasks_id" context="{'form_view_ref': 'view_project_costes_plan_task_form','tree_view_ref':'view_project_costes_plan_task_tree'}" />-->
                            <!--</page>-->
                        </notebook>
                    </sheet>
                </form>
            </field>
        </record>

        <record id="view_project_costes_plan_tree" model="ir.ui.view">
            <field name="name">project.costes.plan.tree</field>
            <field name="model">project.costes.plan</field>
            <field name="arch" type="xml">
                <tree string="Costes Planificados" version="7.0" colors="blue:estado == True;red:estado == False" class="xnTree">
                    <!--<field name="contract_id" />-->
                    <!--<field name="project_id" />-->
                    <!--<field name="tasks_id" />-->
                    <field name="concepto" />
                    <field name="version" />
                    <field name="estado" invisible="1" />
                    <field name="plan_coste" />
                    <field name="plan_hours" />
                </tree>
            </field>
        </record>

        <record id="view_project_costes_plan_search" model="ir.ui.view">
            <field name="name">project.costes.plan.search</field>
            <field name="model">project.costes.plan</field>
            <field name="arch" type="xml">
                <search string="Buscar planificaciones">
                    <field name="version"/>
                </search>
            </field>
        </record>

    </data>
</openerp>

Third Form - Python File

import pdb
import time
from datetime import datetime
from osv import fields, osv

class project_costes_plan_project(osv.Model):

    # Metodos definidos anteriormente para los valores por defecto
    def _get_uom_id(self, cr, uid, ids, context=None):
        result = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'product', 'product_uom_hour')
        return result and result[1] or False

    _name = 'project.costes.plan.project'
    _description = 'Tabla de costes planificados en proyecto'
    _columns = {
        'project_id': fields.many2one('project.costes.plan', 'Costes planificados en proyecto', select=True, ondelete='cascade'),

        'perfil_id': fields.many2one('project.profile', 'Perfil'),
        'product_id': fields.many2one('product.product', 'Producto'),

        'tipo': fields.selection( [('trabajo','Trabajo'),('gasto','Gasto')] ,'Tipo', required=True),

        'fecha_inicio': fields.date('Inicio previsto', required=True),
        'fecha_final': fields.date('Final previsto', required=True),

        'cantidad': fields.float('Cantidad',digits=(12,2), required=True),
        'uom_id': fields.many2one('product.uom', 'Unidad de medida', required=True),
        'concepto': fields.char('Breve descripcion'),

        'coste': fields.float('Coste Estandar', digits=(12,2), required=True),

        'plan_coste': fields.float('Total planificado', digits=(12,2), required=True, help="Total planificado por perfil/producto"),
        'plan_hours': fields.float('Total horas planificadas', digits=(12,2), required=True, help="Total de horas planificadas por perfil"),
    }
    _defaults = {
        'tipo': 'trabajo',
        'fecha_inicio': lambda *a: time.strftime('%Y-%m-%d'),
        'cantidad': 1,
        'uom_id': _get_uom_id,
    }

    # Metodos privados
    def _get_default_uom_type(self, cr, uid, tipo, context = None):
        osv.logging.log(100, "[Alca] _get_default_uom_type:project_costes_plan_project:project_costes "+str(tipo))
        v = {}

        if tipo == 'trabajo':
            result = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'product', 'product_uom_hour')
        elif tipo == 'gasto':
            result = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'product', 'product_uom_unit')
        else:
            result = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'product', 'product_uom_hour')

        if result and result[1]:
            v['uom_id'] = result[1]

        if v:
            return {'value': v}
        else:
            return False

    def _get_price_perfil(self, cr, uid, perfil, producto, context = None):
        osv.logging.log(100, "[Alca] _get_price_perfil:project_costes_plan_project:project_costes "+str(perfil)+" - "+str(producto))
        v = 0.0

        if perfil:
            result = self.pool.get("project.profile").browse(cr, uid, perfil, context);
            if result['tipo_perfil']:
                if result['tipo_perfil'] == "interno":
                    if result['coste']:
                        v = result['coste']
                elif result['tipo_perfil'] == "externo":
                    v = self._get_price_product(cr, uid, producto, context)

        return v

    def _get_price_product(self, cr, uid, producto, context = None):
        osv.logging.log(100, "[Alca] _get_price_product:project_costes_plan_project:project_costes "+str(producto))
        v = 0.0

        if producto:
            result = self.pool.get("product.product").browse(cr, uid, producto, context);
            if result['list_price']:
                v = result['list_price']

        return v

    def _total_coste(self, cr, uid, values, context = None):
        osv.logging.log(100, "[Alca] _total_coste:project_costes_plan_project:project_costes "+str(values))
        v = 0.0
        if values['coste'] and values['cantidad']:
            v = values['coste'] * values['cantidad']
        return v

    def _total_horas(self, cr, uid, values, context = None):
        osv.logging.log(100, "[Alca] _total_horas:project_costes_plan_project:project_costes "+str(values))
        v = 0.0
        if values['tipo'] and values['tipo'] == "trabajo":
            result = self.pool.get('product.uom').browse(cr, uid, values['uom_id'], context)
            if result and result.name == "Hour(s)":
                if values['cantidad']:
                    v = values['cantidad']
        return v

    # Metodos publicos, accesibles desde XML
    def changes_tipo (self, cr, uid, ids, tipo, perfil_id, product_id, cantidad, coste, uom_id, context = "None"):
        osv.logging.log(100, "[Alca] changes_tipo:project_costes_plan_project:project_costes "+str(ids))
        v = {}
        v = self._get_default_uom_type(cr, uid, tipo, context)
        self.set_calculo_total(cr, uid, ids, tipo, perfil_id, product_id, cantidad, coste, uom_id, context)
        if v:
            return v
        else:
            return False

    def set_calculo_total(self, cr, uid, ids, tipo, perfil_id, product_id, cantidad, coste, uom_id, metodo = 'N', context = None):
        osv.logging.log(100, "[Alca] set_calculo_total:project_costes_plan_project:project_costes "+str(ids))
        osv.logging.log(100, "[Alca] set_calculo_total: tipo="+str(tipo)+" perfil_id="+str(perfil_id)+" product_id="+str(product_id)+" cantidad="+str(cantidad)+" coste="+str(coste)+"")
        v = {}

        if tipo == "gasto":
            if metodo == "P":
                v['coste'] = self._get_price_product(cr, uid, product_id, context)
                coste = v['coste']
                # Si es un gasto, no necesita el perfil
                if perfil_id:
                    v['perfil_id'] = ""
            # Vamos a calcular
            v['plan_coste'] = self._total_coste(cr, uid, {'coste': coste, 'cantidad': cantidad}, context)
            v['plan_hours'] = self._total_horas(cr, uid, {'cantidad': cantidad, 'tipo': tipo, 'uom_id': uom_id}, context)
        elif tipo == "trabajo":
            if metodo == "P":
                # Si es un trabajo y tiene perfil
                if perfil_id:
                    # Calculamos el coste
                    v['coste'] = self._get_price_perfil(cr, uid, perfil_id, product_id, context)
                    result = self.pool.get("project.profile").browse(cr, uid, perfil_id, context)
                    # Y el perfil es de tipo Interno, borramos el producto, si es de tipo externo, No
                    if result['tipo_perfil']:
                        if result['tipo_perfil'] == "interno":
                            v['product_id'] = ""
                else:
                    # Si es un trabajo, pero no tiene perfil, tal vez tenga producto, con lo cual, no calculamos el precio
                    v['coste'] = 0.0
            # Vamos a calcular
            v['plan_coste'] = self._total_coste(cr, uid, {'coste': coste, 'cantidad': cantidad}, context)
            v['plan_hours'] = self._total_horas(cr, uid, {'cantidad': cantidad, 'tipo': tipo, 'uom_id': uom_id}, context)

        if v:
            return {'value': v}
        else:
            return False

project_costes_plan_project(

Third Form - XML File

<openerp>
    <data>

        <record id="view_project_costes_plan_project_form" model="ir.ui.view">
            <field name="name">project.costes.plan.project.form</field>
            <field name="model">project.costes.plan.project</field>
            <field name="arch" type="xml">
                <form string="Costes Planificados Proyecto" version="7.0">
                    <sheet>
                        <div class="xnComponente">
                            <group>
                                <field name="concepto" class="xnTopLabel" placeholder="Descripcion..."/>
                                <field name="tipo" class="xnTopLabel" on_change="changes_tipo(tipo, perfil_id, product_id, cantidad, coste, uom_id)" />
                            </group>

                            <group attrs="{'invisible':[('tipo','not in',['trabajo','gasto'])]}">
                                <group col="7">
                                    <label for="perfil_id" class="xnTopLabel" />
                                    <label for="product_id" class="xnTopLabel" />
                                    <label for="fecha_inicio" class="xnTopLabel" />
                                    <label for="fecha_final" class="xnTopLabel" />
                                    <label for="cantidad" class="xnTopLabel" />
                                    <label for="uom_id" class="xnTopLabel" />
                                    <label for="coste" class="xnTopLabel" />
                                </group>
                                <newline />
                                <group col="7">
                                    <field name="perfil_id" class="xnTopLabel" nolabel="1" on_change="set_calculo_total(tipo, perfil_id, product_id, cantidad, coste, uom_id, 'P')" />
                                    <field name="product_id" class="xnTopLabel" nolabel="1" on_change="set_calculo_total(tipo, perfil_id, product_id, cantidad, coste, uom_id, 'P')" />
                                    <field name="fecha_inicio" class="xnTopLabel" nolabel="1" />
                                    <field name="fecha_final" class="xnTopLabel" nolabel="1" />
                                    <field name="cantidad" class="xnTopLabel" nolabel="1" on_change="set_calculo_total(tipo, perfil_id, product_id, cantidad, coste, uom_id)" />
                                    <field name="uom_id" class="xnTopLabel" nolabel="1" on_change="set_calculo_total(tipo, perfil_id, product_id, cantidad, coste, uom_id)" />
                                    <field name="coste" class="xnTopLabel" nolabel="1" on_change="set_calculo_total(tipo, perfil_id, product_id, cantidad, coste, uom_id)" />
                                </group>
                            </group>

                            <group attrs="{'invisible':[('tipo','not in',['trabajo','gasto'])]}">
                                <!--<field name="project_id" invisible="1" />-->
                                <field name="plan_coste" class="soloLectura"/>
                                <field name="plan_hours" class="soloLectura"/>
                            </group>
                        </div>
                    </sheet>
                </form>
            </field>
        </record>

        <record id="view_project_costes_plan_project_tree" model="ir.ui.view">
            <field name="name">project.costes.plan.project.tree</field>
            <field name="model">project.costes.plan.project</field>
            <field name="arch" type="xml">
                <tree string="Costes Planificados" version="7.0">
                    <field name="tipo"/>
                    <field name="perfil_id"/>
                    <field name="product_id"/>
                    <field name="fecha_inicio" invisible="1"/>
                    <field name="fecha_final" invisible="1"/>
                    <field name="cantidad"/>
                    <field name="uom_id"/>
                    <field name="coste"/>
                    <field name="plan_coste" invisible="1"/>
                    <field name="plan_hours" invisible="1"/>
                </tree>
            </field>
        </record>

    </data>
</openerp>

That is what I have to do to pass the values ​​of the third form the second and first? What am I doing wrong? Thanks!

0
Cpueyo
On 3/22/13, 6:17 AM

Hi!

I make it in the first form, with the methods create and write ^_^ And i make too, a little button in second form for update it, and work too.

The files are here: bazaar.launchpad.net/~cpueyo/nerviamodulesopenerp/trunk/files/head:/project_costes/

Thanks!

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 3/13/13, 3:15 PM
Seen: 2870 times
Last updated: 3/16/15, 8:10 AM