Odoo - Filter child records in parent class - odoo-8

I am using Odoo 10-e. I am working on a sample module.
I have 5 models (customer, product, order, order details, movement)
Customer :
class Customer(models.Model):
_name ="amgl.customer"
…… all other fields related to customer
product_ids = fields.Many2many('amgl.products', string='Products')
Products :
class Products(models.Model):
_name ="amgl.products"
…… all other fields related to product
customer_ids = fields.Many2many('amgl.customer', string='Customers')
Order :
class Order(models.Model):
_name ="amgl.order"
order_line = fields.One2many('amgl.order_line', 'order_id', string='Order Lines')
…… all other fields related to Order
Order Details :
class OrderLine(models.Model):
_name = 'amgl.order_line'
order_id = fields.Many2one('amgl.order', string='Orders')
products = fields.Many2one('amgl.products', string="Products")
…… all other fields related to order details
Movement
class Movement(models.Model):
_name = 'amgl.metal_movement'
customer = fields.Many2one("amgl.customer", string="Customer", required=True)
order_lines = fields.One2many('amgl.order_line','metal_movement_id')
…… all other fields related to movement
What i am trying to do is, i am creating a movement form in which user will select a customer from customer drop down first then user will add order_lines and in that he will be able to add products i want to filter products which are associated with above selected customer . How can i do that ? I have been trying since last month.
Movement View
<odoo>
<data>
<record id="action_metal_movement" model="ir.actions.act_window">
<field name="name">Metal Movement Request</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">metal.movement</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click to create
</p><p>
<!-- More details about what a user can do with this object will be OK -->
</p>
</field>
</record>
<record id="form_metal_movement" model="ir.ui.view">
<field name="name">Metal Movement Request </field>
<field name="model">metal.movement</field>
<field name="arch" type="xml">
<form string="Metal Movement">
<sheet>
<group>
<group string="Metal Movement Request">
<field name="date_create" string="Date Created"/>
<field name="reference"/>
<field name="metal_movement_type"/>
<field name="first_approve"/>
<field name="second_approve" domain="[('id', '!=', first_approve)]"/>
<field name="customer"/>
<field name="sepcial_instruction" widget="html"/>
</group>
<group string="Metal Movement From">
<group colspan="6">
<field name="custodian"/>
<field name="mmf_name"/>
<field name="mmf_account_number"/>
<field name="mmf_account_type"/>
</group>
<group string="Metal Movement To" colspan="6">
<field name="mmt_name"/>
<field name="mmt_address" attrs="{'invisible':[('metal_movement_type', '=', 'IT')]}"/>
<field name="mmt_account_number" />
<field name="mmt_company" attrs="{'invisible':[('metal_movement_type','not in',('AC','IPPU','IT'))]}"/>
<field name="pickup_date" string="Pick up Datetime" attrs="{'invisible':[('metal_movement_type','not in',('AC','IPPU'))]}"/>
</group>
</group>
<group string="Metals To Be Moved" colspan="12">
<field name="order_lines">
<tree editable="bottom">
<field name="quantity" string="Quantity"/>
<field name="products" domain="[('customer_ids','in', parent.customer)]" string="Product Name"/>
<field name="weight" string="Weight"/>
<field name="total_weight" string="Total Weight"/>
</tree>
</field>
</group>
</group>
</sheet>
</form>
</field>
</record>
</data>
</odoo>

Thank you for updation of question.
First add customer in product field context.
<field name="order_lines">
<tree editable="bottom">
<field name="quantity" string="Quantity"/>
<field name="products" context="{'customer_id':parent.customer}" string="Product Name"/>
<field name="weight" string="Weight"/>
<field name="total_weight" string="Total Weight"/>
</tree>
</field>
Then in product's model write it's name_get method like this.
#api.multi
def name_get(self):
if self.env.context('customer_id',False):
customer = self.env['amgl.customer'].browse(self.env.context('customer_id',False))
for product in customer.product_ids:
res.append((product.id,product.name))
else:
res=super(product_product,self).name_get()
return res
Thats it.

Related

AssertionError: Element odoo has extra content: record, line 4

I'm trying to add multi field to timesheet module to work as a tasks manage app i did inheritance to account.analytic.line the problem is in the view i got that error "AssertionError: Element odoo has extra content: record, line 4"
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="todo_timesheet_line_tree" model="ir.ui.view">
<field name="inherit_id" ref="hr_timesheet.hr_timesheet_line_tree"/>
<field name="model">account.analytic.line</field>
<field name="arch" type="xml"/>
<field name="task_id" position="after">
<field name="priority"/>
</field>
<field name="company_id" groups="base.group_multi_company" position="before">
<field name="gov_department"/>
<field name="priority"/>
<field name="date_deadline"/>
</field>
</record>
</odoo>
from odoo import models, fields, api
class TodoTask(models.Model):
_inherit = 'account.analytic.line'
startdate=fields.Date('start Date', required=True, index=True, default=fields.Date.context_today)
date_deadline=fields.Date('Deadline', required=True, index=True, default=fields.Date.context_today)
priority = fields.Selection(selection=[('1', 'فوري'), ('2','هام جدا' ),('3','اقل اهمية')])
is_done = fields.Boolean ('Done?')
gov_department = fields.Many2one('govauthority','الجهة الحكومية')
class govauthority(models.Model):
govname = fields.Char('GOV_Department', size=25, required=True)
The error is in this line
<field name="arch" type="xml" />
You closed the tag while you really should not. Within this tag the actual XML body should be.
<record id="todo_timesheet_line_tree" model="ir.ui.view">
<field name="inherit_id" ref="hr_timesheet.hr_timesheet_line_tree" />
<field name="model">account.analytic.line</field>
<field name="arch" type="xml">
<field name="task_id" position="after">
<field name="priority" />
</field>
<field name="company_id" groups="base.group_multi_company" position="before">
<field name="gov_department" />
<field name="priority" />
<field name="date_deadline" />
</field>
</field>
</record>

Tree view in odoo 11

I want to show the fields that i mentioned in the tree string when there are no records in my tree view instead of showing "Click to add a new Sample record."
<record model="ir.ui.view" id="sample_tree_view">
<field name="name">Sample</field>
<field name="model">sample.test</field>
<field name="arch" type="xml"
<tree string="Sample" default_order="name">
<field name="code" />
<field name="name" />
<field name="status" />
</tree>
</field>
</record>
<record model="ir.actions.act_window" id="action_sample">
<field name="name">Sample</field>
<field name="res_model">sample.test</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click here to create a new sample Record.
</p>
</field>
</record>
Thanks in advance ..!
Please remove help from your defined action & check.

Hide lines in odoo treeview

I want to hide equipments that are not in an inventory. I've tried with
<record id="view_inventory_equipment_tree" model="ir.ui.view">
<field name="name">maintenance.equipment.tree</field>
<field name="model">maintenance.equipment</field>
<field name="domain">[('inventory_id','=',False)]</field>
<field name="arch" type="xml">
<tree string="Lot/Serial Number Inventory">
<field name="name"/>
<field name="serial_no"/>
<field name="category_id"/>
<field name="inventory_id"/>
<field name="cost"/>
</tree>
</field>
</record>
But that did not work, also there is no luck with
<field name="domain_force">[('inventory_id','=',False)]</field>
My model:
class MaintenanceEquipment(models.Model):
_name = 'maintenance.equipment'
_description = 'Equipment'
name = fields.Char('Equipment Name', required=True, translate=True)
inventory_id = fields.Many2one('maintenance.equipment.inventory', string='Equipment Inventory', track_visibility='onchange')
Put domain in action window record. Domain not work in directly with any view like tree or form view.

The states of a model for the Workflows in Odoo

I am trying to implement Odoo publishing workflows for my custom model 'product_images.product_image'.
My models look like this:
# product_images/models/models.py
# -*- coding: utf-8 -*-
from odoo import models, fields, api, tools
class PublishingStatus(models.Model):
_name = 'product_images.publishing_status'
_description = 'Publishing status'
name = fields.Char(string="Name")
slug = fields.Char(string="Slug")
class ProductImage(models.Model):
_name = 'product_images.product_image'
_description = 'Product image'
name = fields.Char(string="Alternative text")
product_id = fields.Many2one('product.product', string='Product', ondelete='set null', index=True)
original_image = fields.Binary(string='Original image')
#api.model
def _get_default_state(self):
return self.env['product_images.publishing_status'].search([['slug', '=', 'draft']])
#api.model
def _get_all_states(self, groups, domain, order):
state_ids = self.env['product_images.publishing_status'].search([])
return state_ids
state_id = fields.Many2one(
'product_images.publishing_status',
string='Publishing status',
default=_get_default_state,
group_expand='_get_all_states',
)
#api.multi
def action_set_to_draft(self):
self.state_id = self.env['product_images.publishing_status'].search([['slug', '=', 'draft']])
#api.multi
def action_request_for_approval(self):
self.state_id = self.env['product_images.publishing_status'].search([['slug', '=', 'pending']])
#api.multi
def action_approve(self):
self.state_id = self.env['product_images.publishing_status'].search([['slug', '=', 'approved']])
#api.multi
def action_reject(self):
self.state_id = self.env['product_images.publishing_status'].search([['slug', '=', 'rejected']])
Then I have some data records for the publishing statuses:
<!-- product_images/data/data.xml -->
<odoo>
<data>
<!-- explicit list view definition -->
<record model="product_images.publishing_status" id="product_images.publishing_status_draft">
<field name="name">Draft</field>
<field name="slug">draft</field>
</record>
<record model="product_images.publishing_status" id="product_images.publishing_status_pending">
<field name="name">Pending</field>
<field name="slug">pending</field>
</record>
<record model="product_images.publishing_status" id="product_images.publishing_status_approved">
<field name="name">Approved</field>
<field name="slug">approved</field>
</record>
<record model="product_images.publishing_status" id="product_images.publishing_status_rejected">
<field name="name">Rejected</field>
<field name="slug">rejected</field>
</record>
</data>
</odoo>
I also have some records to create a workflow which allows to switch between the publishing statuses:
<odoo>
<data>
<record model="workflow" id="product_images.wkf_image_publishing">
<field name="name">Product Image Publishing Workflow</field>
<field name="osv">product_images.product_image</field>
<field name="on_create">True</field>
</record>
<record model="workflow.activity" id="product_images.wkf_activity_draft">
<field name="name">Draft</field>
<field name="wkf_id" ref="product_images.wkf_image_publishing" />
<field name="flow_start" eval="True" />
<field name="kind">function</field>
<field name="action">action_set_to_draft()</field>
</record>
<record model="workflow.activity" id="product_images.wkf_activity_pending">
<field name="name">Pending</field>
<field name="wkf_id" ref="product_images.wkf_image_publishing" />
<field name="kind">function</field>
<field name="action">action_request_for_approval()</field>
</record>
<record model="workflow.activity" id="product_images.wkf_activity_approved">
<field name="name">Approved</field>
<field name="wkf_id" ref="product_images.wkf_image_publishing" />
<field name="flow_stop" eval="True" />
<field name="kind">function</field>
<field name="action">action_approve()</field>
</record>
<record model="workflow.activity" id="product_images.wkf_activity_rejected">
<field name="name">Rejected</field>
<field name="wkf_id" ref="product_images.wkf_image_publishing" />
<field name="flow_stop" eval="True" />
<field name="kind">function</field>
<field name="action">action_reject()</field>
</record>
<record model="workflow.transition" id="product_images.wkf_transition_draft_to_pending">
<field name="act_from" ref="product_images.wkf_activity_draft" />
<field name="act_to" ref="product_images.wkf_activity_pending" />
<field name="condition">name != "" and original_image != ""</field>
<field name="signal">pending</field>
</record>
<record model="workflow.transition" id="product_images.wkf_transition_pending_to_draft">
<field name="act_from" ref="product_images.wkf_activity_pending" />
<field name="act_to" ref="product_images.wkf_activity_draft" />
<field name="signal">draft</field>
</record>
<record model="workflow.transition" id="product_images.wkf_transition_pending_to_approved">
<field name="act_from" ref="product_images.wkf_activity_pending" />
<field name="act_to" ref="product_images.wkf_activity_approved" />
<field name="signal">approve</field>
</record>
<record model="workflow.transition" id="product_images.wkf_transition_pending_to_rejected">
<field name="act_from" ref="product_images.wkf_activity_pending" />
<field name="act_to" ref="product_images.wkf_activity_rejected" />
<field name="signal">reject</field>
</record>
</data>
</odoo>
And now the tickiest part! I need a form with the buttons to switch between workflow states and a status bar showing the currently active status. This is what I tried:
<record model="ir.ui.view" id="product_images.form">
<field name="name">Product Image</field>
<field name="model">product_images.product_image</field>
<field name="arch" type="xml">
<form>
<header>
<!--
<button name="draft"
type="workflow"
string="Set to draft"
attrs="{'invisible': [('state_id.slug','not in',['pending'])]}"
/>
<button name="pending"
type="workflow"
string="Request for approval"
attrs="{'invisible': [('state_id.slug','not in',['draft'])]}"
/>
<button name="approve"
type="workflow"
string="Approve"
attrs="{'invisible': [('state_id.slug','not in',['pending'])]}"
class="oe_highlight"
/>
<button name="reject"
type="workflow"
string="Reject"
attrs="{'invisible': [('state_id.slug','not in',['pending'])]}"
class="oe_highlight"
/>
-->
<field name="state_id" widget="statusbar" />
</header>
<sheet>
<group>
<field name="product_id" />
<field name="name" string="Alternative text" />
<field name="original_image" widget="image" class="oe_avatar" />
<field name="state_id" />
</group>
</sheet>
</form>
</field>
</record>
The problems I got:
The status bar is shown, but the current publishing status is not activated.
If I uncomment the buttons, they throw an error about invalid domain:
Uncaught Error: Unknown field state_id.slug in domain
[["state_id.slug","not in",["pending"]]]
What am I missing?
In domain attribute, we can not use parent field in left hand side. In your case, we need to add related field.
For example:
class ProductImage(models.Model):
_name = 'product_images.product_image'
slug = fields.Char(related='state_id.slug', string='Slug', store=True)
Put slug field after state_id in your view file
<field name="slug" invisible="1"/>
Now uncomment <button> codes.
Afterwards, restart Odoo server and upgrade your custom module.

How add new field on tree view (inherit on inherited)

I'm trying to replace a field in a tree view. Help me please.
This is script on base odoo enteprice
<record model="ir.ui.view" id="view_task_form2_inherited">
<field name="name">project.task.form.inherited</field>
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_task_form2" />
<field name="arch" type="xml">
<field name="project_id" position="attributes">
<attribute name="on_change">onchange_project(project_id)</attribute>
</field>
<field name="tag_ids" position="after">
<field name="analytic_account_id" invisible="1"/>
<field name="progress" widget="progressbar"
groups="project.group_time_work_estimation_tasks"/>
</field>
<xpath expr="//notebook/page[#name='description_page']" position="after">
<page string="Timesheets" groups="project.group_tasks_work_on_tasks,project.group_time_work_estimation_tasks">
<field name="timesheet_ids" groups="project.group_tasks_work_on_tasks" context="{'default_account_id' : analytic_account_id, 'default_is_timesheet' : 1}">
<tree editable="top" string="Timesheet Activities">
<field name="date"/>
<field name="user_id" required="1"/>
<field name="name"/>
<field name="account_id"/>
<field name="unit_amount" string="Duration" sum="Total time" widget="float_time"/>
<field name="is_timesheet" invisible="1"/>
</tree>
</field>
<group>
<group class="oe_subtotal_footer oe_right" name="project_hours" groups="project.group_time_work_estimation_tasks">
<field name="effective_hours" widget="float_time" groups="project.group_time_work_estimation_tasks"/>
<field name="remaining_hours" widget="float_time" class="oe_subtotal_footer_separator" groups="project.group_time_work_estimation_tasks"/>
</group>
</group>
</page>
</xpath>
</field>
</record>
This is my custom script on _view.xml:
<record id="project_task_view_form" model="ir.ui.view">
<field name="name">project.task.view.form</field>
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_task_form2"/>
<field name="arch" type="xml">
<xpath expr="/notebook/page[#name='description_page']" position="replace">
<page string="Timesheets" groups="project.group_tasks_work_on_tasks,project.group_time_work_estimation_tasks">
<field name="timesheet_ids" groups="project.group_tasks_work_on_tasks" context="{'default_account_id' : analytic_account_id, 'default_is_timesheet' : 1}">
<tree editable="top" string="Timesheet Activities">
<field name="date"/>
<field name="user_id" required="1"/>
<field name="name"/>
<field name="unit_amount" string="Duration" sum="Total time" widget="float_time"/>
<field name="is_timesheet" invisible="1"/>
<field name="invoiceable_analytic_line"/>
</tree>
</field>
</page>
</xpath>
</field>
</record>
I want add new field "invoiceable_analytic_line" inside "timesheet_ids", but it doesn't work.
anybody help me?
Thanks.
View
The procedure is correct but just that there is an error with the expr attribute inside xpath.
Instead:
<xpath expr="/notebook/page[#name='description_page']" position="replace">,you have to write:
<xpath expr="//sheet/notebook/page[1]" position="replace"> when on odoo 10<xpath expr="/sheet/notebook/page[#string='Description']" position="replace">Because description_page doesn't existSo copy whole <page string="Description">..</page> tag from /project/project_view.xml and paste it inside your xpath tag by modify the string. After that insert your field where you want
Model
class Add_in_timesheet(models.Model)
_inherit = 'project.task
invoiceable_analytic_line = fields.Char(string=u"Invoiceable")

Resources