How do I set a value on a selection via a button - odoo-14

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)

Related

Create custom excel report from wizard in odoo 15

I am trying to generate an excel report from wizard using report_xlsx module but i am getting below error.
Traceback (most recent call last):
File "/home/kabeer/Projects/Odoo15ce/odoo/custom_addons/report_xlsx/controllers/main.py", line 76, in report_download
response = self.report_routes(
File "/home/kabeer/Projects/Odoo15ce/odoo/http.py", line 535, in response_wrap
response = f(*args, **kw)
File "/home/kabeer/Projects/Odoo15ce/odoo/custom_addons/report_xlsx/controllers/main.py", line 32, in report_routes
data.update(json.loads(data.pop("options")))
ValueError: dictionary update sequence element #0 has length 10; 2 is required
Here is what i did.
*.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="open_purchase_order_print_menu" model="ir.actions.report">
<field name="name">Open PO</field>
<field name="model">open.po</field>
<field name="report_type">xlsx</field>
<field name="report_name">transight.open_po</field>
<field name="report_file">transight.open_po</field>
<field name="print_report_name">'Purchase Order'</field>
<!-- <field name="binding_model_id" ref="purchase.model_purchase_order"/> -->
<field name="binding_type">report</field>
</record>
<record id="open_po_view_form" model="ir.ui.view">
<field name="name">open.po.form</field>
<field name="model">open.po</field>
<field name="arch" type="xml">
<form string="Open PO">
<group>
<field name="mode" required="1"/>
</group>
<group col="4">
<field name="purchase_order_id"
widget="many2many_tags"
attrs="{'invisible':[('mode','=','partnumber')]}"/>
<field name="partnumber" attrs="{'invisible':[('mode','=','po')]}"/>
</group>
<footer>
<button string="Print" name="action_print" type="object" class="btn-primary"/>
<button string="Cancel" class="btn-secondary" special="cancel"/>
</footer>
</form>
</field>
</record>
<record id="action_open_po" model="ir.actions.act_window">
<field name="name">Open PO</field>
<field name="res_model">open.po</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
<menuitem id="menu_open_po_action" name="Open PO" parent="mrp.menu_mrp_configuration" action="action_open_po" sequence="4"/>
</odoo>
*.py
from odoo import fields,models,api,_
class OpenPOXlsx(models.AbstractModel):
_name = 'report.transight.open_po'
_inherit = 'report.report_xlsx.abstract'
def generate_xlsx_report(self, workbook, data, partners):
print('data',data)
for obj in partners:
report_name = 'Excel Report test'
# One sheet by partner
sheet = workbook.add_worksheet(report_name[:31])
bold = workbook.add_format({'bold': True})
sheet.write(0, 0, 'Hi Am here', bold)
class OpenPO(models.TransientModel):
_name = 'open.po'
_description = 'OpenPO'
purchase_order_id = fields.Many2many('purchase.order',string='Purchase Order')
partnumber = fields.Char(string="Partnumber")
mode = fields.Selection([('partnumber','Part Number'),('po','Purchase Order')],string="Filter by")
def action_print(self):
if self.mode == 'po':
query = """my query %s """
self.env.cr.execute(query,(tuple(self.purchase_order_id.ids),))
datas = self.env.cr.dictfetchall()
print('data---------',datas)
# return self.env.ref('account_batch_payment.action_print_batch_payment').report_action(self, config=False)
return self.env.ref('transight.open_purchase_order_print_menu').report_action(self,data=datas)
The dictfetchall() method returns a list of dicts and report_action is expecting data (datas) to be a dict.
In previous Odoo versions, we pass record data using form key, for example:
datas = {
'ids': active_ids,
'model': 'hr.contribution.register',
'form': self.read()[0]
}
To fix this issue, add the fetched values to datas:
values = self.env.cr.dictfetchall()
datas['values'] = values
Then use values key in generate_xlsx_report method
Make sure that datas is a dictionary before calling report_action

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

Odoo. Tree/form display field data

I have some problem with tree/form view in Odoo.
My model have such classes: https://yadi.sk/d/sCLVo3gHtbVEu
class URLList(models.Model):
_name = 'webvisitorcalc.url_list'
url = fields.Char(string="URL", required=True)
url_parametes = fields.Char(string="URL parameters") #пераметры URLб всё что идёт после ?
target_session_id = fields.One2many('webvisitorcalc.session_visitor', 'target_url_ids', string='Target URL')
site_trip_prevouse_id = fields.One2many('webvisitorcalc.site_trip', 'url_prevouse_ids', string='Prevouse URL')
site_trip_current_id = fields.One2many('webvisitorcalc.site_trip', 'url_current_ids', string='Current URL')
remote_sites_id = fields.One2many('webvisitorcalc.remote_sites', 'site_url_ids', string='Remote site page with URL')
remote_sites_target_url_id = fields.One2many('webvisitorcalc.remote_sites', 'target_url_ids', string='URL on remote site page')
#api.multi
def url_exist(self, cr, SUPERUSER_ID, urlForCheck):
_logger.error("Check URL exist in DB ")
result = False
if (self.search_count(cr, SUPERUSER_ID, [('url', '=', urlForCheck)])>0):
result = True
return result
class SiteTrip(models.Model):
_name = 'webvisitorcalc.site_trip'
session_ids = fields.Many2one('webvisitorcalc.session_visitor', string='Session ID', index=True)
url_prevouse_ids = fields.Many2one('webvisitorcalc.url_list', string='Prevouse URL', index=True)
url_current_ids = fields.Many2one('webvisitorcalc.url_list', string='Current URL', index=True)
Template for this model: https://yadi.sk/d/Ob0o65PutbVFA
<record model="ir.actions.act_window" id="site_trip_list_action">
<field name="name">Site trip</field>
<field name="res_model">webvisitorcalc.site_trip</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">Create the first record for site trip
</p>
</field>
</record>
<record model="ir.actions.act_window" id="url_list_list_action">
<field name="name">URL list</field>
<field name="res_model">webvisitorcalc.url_list</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">Create the first url
</p>
</field>
</record>
<record model="ir.ui.view" id="site_trip_tree_view">
<field name="name">site_trip.tree</field>
<field name="model">webvisitorcalc.site_trip</field>
<field name="arch" type="xml">
<tree string="URL list tree">
<field name="session_ids"/>
<field name="url_prevouse_ids" string="PrevURL">
</field>
<!--<field name="url_prevouse_ids"/>-->
<field name="url_current_ids"/>
</tree>
</field>
</record>
<record model="ir.ui.view" id="url_list_tree_view">
<field name="name">url_list.tree</field>
<field name="model">webvisitorcalc.url_list</field>
<field name="arch" type="xml">
<tree string="URL list tree">
<field name="url"/>
<field name="url_parametes"/>
</tree>
</field>
</record>
<menuitem id="site_trip_menu" name="Site trip" parent="webvisitorcalc_menu"
action="site_trip_list_action"/>
<menuitem id="url_list_menu" name="URL list" parent="webvisitorcalc_menu"
action="url_list_list_action"/>
Screenshots are here:
Tree view for class SiteTrip
http://i.stack.imgur.com/FjRDK.png
Form view for class SiteTrip
http://i.stack.imgur.com/uDbOp.png
Tree view for class URLList
http://i.stack.imgur.com/tXzqL.png
Form view for class URLList
http://i.stack.imgur.com/oVnqg.png
As you see URLList displayed fine. For class SiteTrip present problem. Field is displaying not data from URLList. This is field stored element such webvisitorcalc.url_list.ID (array?). How I can display real data in this field (for example URL: http://some-site.com/page.html)?
URL in URLList must be uniq. SiteTrip must have stored only ID of URLList record.
UPD:
class RemoteSites(models.Model):
_name = 'webvisitorcalc.remote_sites'
site_id = advert_company_id = fields.One2many('webvisitorcalc.site_list', 'remote_sites_ids', string='Site')
site_url_ids = fields.Many2one('webvisitorcalc.url_list', string='URL page ')
target_url_ids = fields.Many2one('webvisitorcalc.url_list', string='URL target page')
You obviously have no name field on your webvisitorcalc.url_list model. Odoo needs this to use it as name in webclient wherever you use this model as e.g. many2one field or in the breadcrumb navigation.
So either you define a name field or you set _name on your class with another field identifier.
You can also (re-)define the method display_name on your model (enough examples in Odoo code) where you can do more cool stuff with the record display name :-)

Access inherited field of a model from a view

I'm working on OpenERP7, trying to access a related field located in a parent model of said related model. If someone at this point understand something, you're way smarter than I am, so i will just put the example of what i'm trying to achieve :
My model :
class trench(osv.osv):
_name = 'trench'
_inherit = 'common'
_columns = {
'trench_lines': fields.one2many('trench.line', 'trench_id', 'Trench Lines'),
'trench_depth': fields.one2many('trench.depth', 'trench_id', 'Trench Depth'),
}
trench()
class trench_common(osv.osv):
_name = 'trench.common'
def compute_vals(self, cr, uid, ids, field_name, arg, context):
...
def on_change_values(self, cr, uid, ids, context=None):
...
_columns = {
'trench_id': fields.many2one('trench', 'Trench', ondelete='cascade', required=True),
'length' : fields.function(compute_vals, type='float', string="Length", method=True, store=True),
}
trench_common()
class trench_line(trench_common):
_name = 'trench.line'
_inherit = 'trench.common'
trench_line()
class trench_dig_common(trench_common):
_name = 'trench.dig.common'
_inherit = 'trench.common'
_columns = {
'length' : fields.float('Length', digits=(6,3)),
'height' : fields.float('Height', digits=(6,3)),
'total_m3' : fields.float('Total m3', digits=(6,3)),
'observation' : fields.text('Observation'),
}
trench_dig_common()
class trench_depth(trench_dig_common):
_name = 'trench.depth'
_inherit = 'trench.common'
trench_depth()
My view :
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="trench_form" model="ir.ui.view">
<field name="name">trench.form</field>
<field name="model">trench</field>
<field name="inherit_id" ref="common_form" />
<field name="arch" type="xml">
<group string="Worksite" position="after">
<separator string="Progress" />
<field name="trench_lines">
<tree editable="bottom">
<field name="length"/>
</tree>
</field>
<separator string="Cumulate Length" />
<field name="total_length"/>
<separator string="Trench Particularity" />
<notebook>
<page string="Surdepth">
<field name="trench_depth">
<tree editable="bottom">
<field name="height"/>
</tree>
</field>
</page>
</notebook>
</group>
</field>
</record>
</data>
</openerp>
My error :
except_orm: ('View error', u"Can't find field 'height' in the following view parts composing the view of object model 'qhse.trench':\n * trench.form\n\nEither you wrongly customized this view, or some modules bringing those views are not compatible with your current data model")
2015-05-21 07:56:28,631 13918 ERROR ahak_production openerp.tools.convert: Parse error in trench_view.xml:4:
So, i'm figuring i can't access a field of a model with so many layers, but is there a way to achieve this, and if so, how? I'm trying to make something DRY, but always end up duplicating code with OpenERP.
Thanks for reading.
You need to define your last class as below.
class trench_depth(trench_dig_common):
_name = 'trench.depth'
_inherit = 'trench.dig.common'
trench_depth()
Then after you can access all fields which are available inside "trehch.dig.common" model.

OpenERP - Show child_ids invoices of a partner

I'd like to show invoices adressed to child partner in the parent partner form view.
I've already a inherited res_partner model as follow :
class res_partner(osv.osv):
_inherit = 'res.partner'
_columns = {
'invoice_ids': fields.one2many('account.invoice', 'partner_id', 'Invoices'),
}
And a view displaying invoices as follow :
<?xml version="1.0"?>
<openerp>
<data>
<!-- Partners inherited form -->
<record id="view_history_partner_info_form" model="ir.ui.view">
<field name="name">res.partner.cap_history.form.inherit</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="model">res.partner</field>
<field name="arch" type="xml">
<page string="Accounting" position="after" version="7.0">
<page string="History" name="cap_history_tab">
<group name="grp_invoice_history" string="Invoices History">
<field name="invoice_ids" colspan="4" nolabel="1">
<tree string="Partner Invoices" create="false" delete="false">
<field name="number" readonly="True"/>
<field name="origin" readonly="True"/>
<field name="name" string="Reference" readonly="True"/>
<field name="date_invoice" readonly="True"/>
<field name="x_category" readonly="True"/>
<field name="state" readonly="True"/>
<field name="payment_term" readonly="True"/>
<field name="amount_total" readonly="True"/>
</tree>
</field>
</group>
</page>
</page>
</field>
</record>
With this code I can see invoices that are directly adressed to a company or a person on their respective form view.
But if an invoice is adressed to person, and none is adressed to the parent company, when I am on the company form view, I won't see the invoice adressed to the child contact.
Is there a way to make visible the contact's invoice in the parent partner form view ?
Thank you for your help !
Cheers
One way to do this is to add a new field, all_invoice_ids, as a function field, and then have the function return both the contents of invoice_ids, plus the contents of any child's invoice_ids.
Something like this (untested):
'all_invoice_ids': fields.function(
_get_invoice_ids,
type='one2many',
obj='account.invoice',
method=True,
string='Invoices',
),
and _get_invoice_ids (which should be defined before columns) like this (also untested):
def _get_invoice_ids(self, cr, uid, ids, field_name, arg, context=None):
res = {}
if isinstance(ids, (int, long)):
ids = [ids] # in case an id was passed in directly
for main_partner in self.browse(cr, uid, ids, context=context):
main_invoices = main_partner.invoice_ids or [] # in case it was False
invoices = [inv.id for inv in main_invoices]
for child_partner in main_partner.child_ids:
child_invoices = child_partner.invoice_ids or []
invoices.extend([inv.id for inv in child_invoices])
# at this point we should have all the invoice ids
# use a set to get rid of duplicates
invoices = list(set(invoices))
# and store in res to be returned
res[main_partner.id] = invoices
return res