Sequencing in odoo 10 - sequence

I am trying to create a sequence in odoo here is my xml file
<record model="ir.sequence" id="connection_sequence_id">
<field name="name">New Connections Sequence</field>
<field name="code">ftth.connection</field>
<field name="prefix">FTTH</field>
<field name="padding">6</field>
<field name="company_id" eval="False" />
</record>
I have created a field for it in my .py file
my_seq = fields.Char(string="sequence", default=lambda self: _('New'))
and overiddeen the create function by using the following code
#api.model
def create(self, vals):
if vals:
vals['my_seq'] = self.env['ir.sequence'].next_by_code('ftth.connection') or _('New')
result = super(NewConnections, self).create(vals)
return result
but my code is not generating a sequence instead it gives ftth.connection.(sequence number).what might be wrong with my code.please assist

use super to customize your create function, this an example:
#api.model
def create(self, vals):
if vals:
seq = self.env['ir.sequence']
vals['my_seq'] = seq.next_by_code('ftth.connection') or _('New')
return super(ObjectName, self).create(values)

Related

How do I set a value on a selection via a button

I need to create a button which will define a ticket the value of the state variable to cloturé.
And when a ticket is cloturé, it will on read only.
How can i do it ?
The ticket's model is helpdesk.ticket
view.xml :
<?xml version='1.0' encoding='UTF-8'?>
<odoo>
<record id="helpdesk_ticket_view_form_inherit_header_modifie" model="ir.ui.view">
<field name="name">helpdesk.ticket.modifie.header</field>
<field name="model">helpdesk.ticket</field>
<field name="inherit_id" ref="helpdesk_fsm.helpdesk_ticket_view_form" />
<field name="arch" type="xml">
<xpath expr="//button[#name='action_generate_fsm_task']" position="attributes" >
<attribute name="string">Planifier tache</attribute>
</xpath>
</field>
</record>
</odoo>
ticket.py :
# -*- coding: utf-8 -*-
from odoo import models, fields, api
class ticket_inherit(models.Model):
_inherit = "helpdesk.ticket"
state = fields.Selection(['test','annulé','cloturé'],'selection')
#What i want to do
def cloture_le_ticket(self):
state = 'cloturé'
You can just use write().
state = fields.Selection(string="State", selection=[
('test', 'Test),
('annulé', 'Annulé'),
('cloturé', 'Cloturé')
])
def cloture_le_ticket(self):
self.write({
'state': 'cloturé'
})
To force the ticket to be read-only:
def write(self, vals):
for record in self:
if record.state == 'cloturé':
raise exceptions.UserError("Sorry but the ticket is read only")
return super(HelpdeskTicket, self).write(vals)

ir sequence increment issue in odoo

Hi guys Help me with this.
the check field doesnt increment.it stays blank
from odoo import api, fields, models, _
class check_print(models.Model):
_inherit = 'account.payment'
check = fields.Char("Check Number",readonly=True)
#api.model
def create(self,vals):
if vals.get('payment_method_code') == ('check_printing'):
vals['check'] = self.env['ir.sequence'].next_by_code('seq.ch.code')
res = super(check_print, self).create(vals)
return res
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<record id="seq_check_id" model="ir.sequence">
<field name="name">check</field>
<field name="code">seq.ch.code</field>
<field name="prefix">CH</field>
<field name="padding">10</field>
<field name="company_id" eval="False"/>
</record>
</data>
</odoo>
the check field is empty everytime i save the record
Seems that this line is not being evaluated to True:
if vals.get('payment_method_code') == ('check_printing'):
Also check your indentation of this lines that need to be outside the if block
res = super(check_print, self).create(vals)
return res

How to add on_change in default field odoo 8

i want to add onchange function in module manufacturing (mrp.production)
in view.xml
<record model="ir.ui.view" id="partner_instructur_form_view">
<field name="name">mrp.production.form.instructur</field>
<field name="model">mrp.production</field>
<field name="inherit_id" ref="mrp.mrp_production_form_view" />
<field name="arch" type="xml">
<xpath expr="//field[#name='location_dest_id']" position="after">
<field name="product_qty" on_change="onchange_hitung_kuota(product_qty, prod_qtys)"/>
<field name="prod_qtys" on_change="onchange_hitung_kuota(product_qty, prod_qtys)"/>
<field name="progres_persen" widget="progressbar"/>
</xpath>
</field>
</record>
in python
class ala_mrp_prod(osv.osv):
_inherit = 'mrp.production'
def onchange_hitung_kuota(self, cr, uid, ids, prod_qtys, product_qty):
kurangi = product_qty - prod_qtys
res = {
'value':{ 'progres_persen': (kurangi * 100) / product_qty}
}
return res
_columns = {
'prod_qtys':fields.integer('Jumlah Total'),
'progres_persen': fields.float('Progres'),
}
_defaults = {
'prod_qtys': 1,
}
ala_mrp_prod()
why product quantity show 2?
so if i input product_quantity 3 , jumlah total 5 progres 40%?
please help
You passed parameters in the wrong order.
product_qty should be the first in onchange_hitung_kuota() method:
def onchange_hitung_kuota(self, cr, uid, ids, product_qty, prod_qtys)
In odoo new api you do not need to modify the xml file. what you have to do is
class mrp_order(models.Model)
_inherit = 'mrp.production'
#api.onchange('product_qty', 'prod_qtys')
def onchange_hitung_kuota(self):
kurangi = product_qty - prod_qtys
self.progres_persen = (kurangi * 100) / self.product_qty
hope this helps!
I think you inverted the two parameters inside the onchange_hitung_kuota function

How to store default entered value in custom field of quantity on Hand.?

Need to store default value on qty in M.sqr and square meter like quantity on hand showing default entered value.
when i click on update button of quantity on Hand from product inventory page
then it should show me a previous entered value.
class stock_change_product_qty(osv.osv):
_inherit = 'stock.change.product.qty'
_columns = {
'new_quantity' : fields.float('Qty in Boxes'),
'squ_meter': fields.related('product_id','squ_meter', type='float', relation='product.product', string='Square Meter'),
'qty_char': fields.float('Qty in M.sqr', compute='_compute_qty_char'),
}
#api.depends('new_quantity', 'squ_meter')
def _compute_qty_char(self):
for record in self:
record.qty_char = record.new_quantity * record.squ_meter
view.xml
<field name="name">update_product_quantity inherit</field>
<field name="model">stock.change.product.qty</field>
<field name="inherit_id" ref="stock.view_change_product_quantity"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='new_quantity']" position="attributes">
<attribute name="string">Qty in Boxes</attribute>
</xpath>
<xpath expr="//field[#name='new_quantity']" position="replace">
<field name="new_quantity"/>
<field name="squ_meter"/>
<field name="qty_char"/>
</xpath>
</field>
</record>
A solution is to use a function as default value for your field:
odoo V8
from openerp import api, fields, models
class stock_change_product_qty(models.Model):
_inherit = 'stock.change.product.qty'
new_quantity = fields.Float(string='Qty in Boxes', default=1.0),
squ_meter = fields.Float(related='product_id.squ_meter', string='Square Meter', default=1.0),
qty_char = fields.Float('Qty in M.sqr', compute='_compute_qty_char'),
}
#api.one
#api.depends('new_quantity', 'squ_meter')
def _compute_qty_char(self):
for record in self:
record.qty_char = record.new_quantity * record.squ_meter
odoo V7
def _your_function(self, cr, uid, context=None):
your code here
...
...
...
return field_value
_defaults = {
'your_field' : _your_function,
}
#api.onchange
This decorator will trigger the call to the decorated function if any of the fields specified in the decorator is changed in the form:
#api.onchange('fieldx')
def do_stuff(self):
if self.fieldx == x:
self.fieldy = 'toto'
In previous sample self corresponds to the record currently edited on the form. When in on_change context all work is done in the cache. So you can alter RecordSet inside your function without being worried about altering database. That’s the main difference with #api.depends
At function return, differences between the cache and the RecordSet will be returned to the form.

Odoo Wizard writes to model but getting values from models doesn`t doesnt populate the wizard fields

I have a wizard below that writes to the database but doesnt read the values written to the database and display in the view.
I using the get_opinion to read the opinions and the get_notes to read the notes , but when i click on the buttons they just disappear with no error , where am i going wrong
Q2 , in this code which is part of the code below why am i getting model_name none
if context is None: context = {}
model_name=context.get('active_model')
print model_name #gives NONE why ?
Q3 How can i list all fields in context?
The module code is as below
class opinion(models.TransientModel):
_name='opinion'
opinion_emission = fields.Text(string='OPINION EMISSION')
notes = fields.Text(string='Additional Notes')
defaults={
'opinion_emission': lambda self : self.get_opinion(self),
'notes': lambda self : self.get_notes(self),
}
def save_it(self, cr, uid,ids , context=None):
# context = dict(self._context or {} )
if context is None: context = {}
active_id = context.get('active_id',False)
print 'active_id' , active_id
if active_id :
# op = self.env['opinion'].browse(active_id)
info = self.browse(cr,uid,ids)
self.pool.get('opinion').write(cr,uid,context['active_id'],{'opinion_emission': info[0].opinion_emission,'notes': info[0].notes})
return {
'type': 'ir.actions.act_window_close',
}
#functions that get the info stored in db
#api.one
def get_opinion(self):
ids=self._ids
cr = self._cr
uid = self._uid
context = self._context
if context is None: context = {}
active_id = context.get('active_id',False)
print 'active_id' , active_id
if active_id :
return self.env['opinion'].browse(cr, uid, context['active_id'], context).opinion_emission
#api.one
def get_notes(self,records=None):
ids=self._ids
cr = self._cr
uid = self._uid
context = self._context
if context is None: context = {}
model_name=context.get('active_model')
print model_name #gives NONE why ?
active_id = context.get('active_id',False)
print 'active_id' , active_id
if active_id :
print 'output',self.env['opinion'].browse(cr, uid, context['active_id'], context)[0].notes
return self.env['opinion'].browse(cr, uid, context['active_id'], context)[0].notes
The xml view code is :
<openerp>
<data>
<record id="view_opinion_wizard" model="ir.ui.view">
<field name="name">opinion_wizard.form</field>
<field name="model">opinion</field>
<field name="type">form</field>
<field name="arch" type="xml">
<!-- create a normal form view, with the fields you've created on your python file -->
<form string="OPINION" version="8.0">
<button icon="gtk-ok" name="get_notes" string='SET VAL' type="object" />
<group >
<separator string="Please insert OPINION" colspan="2"/>
<field name="opinion_emission" string="OPINION EMISSION "/>
<field name="notes" string="NOTES"/>
<newline/>
</group>
<div style="text-align:right">
<button icon="gtk-cancel" special="cancel" string="Cancel"/>
<button icon="gtk-ok" name="save_it" string="SAVE INFO" type="object" />
<button icon="gtk-ok" name="get_notes" string="GET NOTES" type="object" />
<button icon="gtk-ok" name="get_opinion" string="GET OPINION" type="object" />
</div>
</form>
</field>
</record>
<!-- your action window refers to the view_id you've just created -->
<record id="action_opinion" model="ir.actions.act_window">
<field name="name">OPINION</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">opinion</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_opinion_wizard"/>
<field name="target">new</field>
</record>
<menuitem name="OPINION" id="menu_opinion" action="action_opinion" parent="timeorder_ratecard_rnd_menu"/>
</data>
This is new API code try this code:
class opinion(models.TransientModel):
_name = 'opinion'
opinion_emission = fields.Text(default=lambda self: self.get_opinion(self), string='OPINION EMISSION')
notes = fields.Text(default=lambda self: self.get_notes(self), string='Additional Notes')
#api.multi
def save_it(self):
active_id = self.env.context.get('active_id', False)
print 'active_id', active_id
if active_id:
op = self.env['opinion'].browse(active_id)
op.write({'opinion_emission': self.opinion_emission, 'notes': self.notes})
return {'type': 'ir.actions.act_window_close', }
#functions that get the info stored in db
#api.one
def get_opinion(self):
active_id = self.env.context.get('active_id', False)
print 'active_id', active_id
if active_id:
return self.env['opinion'].browse(active_id).opinion_emission
#api.one
def get_notes(self, records=None):
model_name = self.env.context.get('active_model')
print model_name # gives NONE why ?
active_id = self.env.context.get('active_id', False)
print 'active_id', active_id
if active_id:
print 'output', self.env['opinion'].browse(active_id).notes
return self.env['opinion'].browse(active_id).notes