odoo

hide button using groups – odoo


<xpath expr="//button[@name='%(hr_holidays.act_hr_employee_holiday_request)d']" position="attributes">
<attribute name="groups">base.group_no_one</attribute>
</xpath>
Advertisements

How to add new button box – odoo


class Employee(models.Model):
    _inherit = "hr.employee"


    @api.multi
    def view_onboard(self):
        onboard_ids = []
        action = self.env.ref('feag_hr.open_view_onboarding_action').read()[0]

        onboard = self.env['employee.onboard'].search([('name', '=', self.id)])
        for data in onboard:
            onboard_ids.append(data.id)

        action['domain'] = [('id', 'in', onboard_ids)]
        return action
<record id="cf_hr_onboard_view_employee_form" model="ir.ui.view">
        <field name="name">hr.employee.form</field>
        <field name="model">hr.employee</field>
        <field name="inherit_id" ref="hr.view_employee_form"/>
        <field name="arch" type="xml">
            <xpath expr="//div[@name='button_box']" position="inside">
                <button name="view_onboard" type="object" string="Onboarding" groups="hr.group_hr_user"
                                    class="oe_stat_button" icon="fa-file-text">
                </button>
            </xpath>
        </field>
    </record>

hide edit and create button for users – odoo


        <!-- hiding create and edit button for users -->
        <record id="fair_hide_view_picking_form" model="ir.ui.view">
        <field name="name">stock.picking.form</field>
        <field name="model">stock.picking</field>
        <field name="inherit_id" ref="stock.view_picking_form"/>
        <field name="groups_id" eval="[(4,ref('fair_construction.group_fair_user'))]"/>
        <field name="arch" type="xml">
            <xpath expr="/form" position="attributes">
                <attribute name="create">false</attribute>
                <attribute name="edit">false</attribute>
            </xpath>
        </field>
        </record> 

pdf Print from python function – odoo


Python File

class AMCPaintBooth(models.Model):
    _name = 'amc.paint.booth'    
    @api.multi
    def action_amc_jobcard_pb_print(self):
        context = {
                'lang': 'en_US', 
                'active_ids': [self.id], 
                'tz': False, 
                'uid': 1
                }
        return {
            'context': context,
            'data': None,
            'type': 'ir.actions.report.xml',
            'report_name': 'safe_amc.safe_report_amc_jobcard_pb',
            'report_type': 'qweb-pdf',
            'report_file': 'safe_amc.safe_report_amc_jobcard_pb',
            'name': 'AMC',
        }

xml File

<header>
   <button name="action_amc_jobcard_pb_print" string="Print AMC Job Card" icon="fa-bars" type="object" class="oe_highlight"/>
</header>

Get users ids from the group – odoo


@api.model
    def create(self, vals):
        result = super(HrExpenseAdvances,self).create(vals)        
        group_hr_expense_manager_id = self.env['ir.model.data'].xmlid_to_res_id('hr_expense.group_hr_expense_manager')
        hr_expense_managers = self.get_users_from_group(group_hr_expense_manager_id)
        if hr_expense_managers:
            result.message_subscribe_users(user_ids=hr_expense_managers)
        return result
    
    #passing group id using self.env['ir.model.data'].xmlid_to_res_id('hr_expense.group_hr_expense_manager')
    @api.multi
    def get_users_from_group(self,group_id):
        users_ids = []
        sql_query = """select uid from res_groups_users_rel where gid = %s"""                
        params = (group_id,)
        self.env.cr.execute(sql_query, params)
        results = self.env.cr.fetchall()
        for users_id in results:
            users_ids.append(users_id[0])
        return users_ids

How to track a field in messages in Odoo


.python file
For tracking a field we have to specify it in the field definition with parameter track_visibility set to ‘always’ or ‘onchange’ like the following:

from odoo import models,fields,api,_,SUPERUSER_ID

class SaleOrder(models.Models):
    _name = "sale.order"
    _inherit = ['mail.thread', 'ir.needaction_mixin']
    state = fields.Selection([
        ('draft', 'Draft'),
        ('submitted', 'Submitted'),
        ('paid', 'Paid'),
        ],default='draft',string='Status', copy=False, index=True,         readonly=True, store=True,track_visibility='always',

    @api.multi
    def action_submit(self):        
        self.state = 'submitted'
        body =_("Expense Advance Submitted")
        subject = _("Expense Advance")
        self.sudo().message_post(body=body,subject=subject, message_type="notification", subtype="mail.mt_comment", author_id=self.partner.id)

you can use author_id=SUPERUSER_ID

.xml file after sheet you can place this.

 
<div class="oe_chatter">
   <field name="message_follower_ids" widget="mail_followers" groups="base.group_user"/>
   <field name="message_ids" widget="mail_thread"/>
</div>

Create journal entry using code – odoo


   #Posting journal entries
    @api.multi
    def action_done(self):
        for rec in self:
            debit = credit = rec.currency_id.compute(rec.paid_amount, rec.currency_id)           
            if rec.state == 'draft':
                raise UserError(_("Only a Submitted payment can be posted. Trying to post a payment in state %s.") % rec.state)

            sequence_code = 'hr.advance.sequence'
            rec.name = self.env['ir.sequence'].with_context(ir_sequence_date=rec.payment_date).next_by_code(sequence_code)
            
            move = {
                'name': '/',
                'journal_id': rec.journal_id.id,
                'date': rec.payment_date,

                'line_ids': [(0, 0, {
                        'name': rec.name or '/',
                        'debit': debit,
                        'account_id': rec.advance_account.id,
                        'partner_id': rec.employee_id.user_id.partner_id.id,
                    }), (0, 0, {
                        'name': rec.name or '/',
                        'credit': credit,
                        'account_id': rec.journal_id.default_credit_account_id.id,
                        'partner_id': rec.employee_id.user_id.partner_id.id,
                    })]
            }
            move_id = self.env['account.move'].create(move)
            move_id.post()    
            return rec.write({'state': 'paid', 'move_id': move_id.id})