Dalam menjalankan aplikasi berbasis web tentu kita akan melihat tombol-tombol untuk menjalankan suatu aktifitas. Demikian juga kita juga dapat menambahkan button dan aktifitasnya pada pemrograman pada lingkup Odoo.

Pembuatan button dan aktifitasnya pada pemrograman Odoo ERP adalah seperti pada penjelasan-penjelasan berikut ini

  1. Mendefinisikan Aktifitas pada Model

    Kita masuk ke Virtualmin pada Webmin dan mengupdate model seperti pada gambar di atas.
  2. Menambahkan button pada view

    Kita melanjutkan ke View bagian form dan melakukan penambahan button seperti pada gambar di atas.
  3. Restart Odoo

    Karena kita mengubah model maka kita perlu melakukan restart Odoo seperti pada gambar di atas.
  4. Upgrade modul

    Karena kita mengunah view maka kita perlu melakukan Upgrade aplikasi seperti pada gambar di atas.
  5. Menggunakan form

    Kita kemudian masuk ke form dan mencoba tombol CANCEL seperti pada gambar di atas.


    Tampak property tersebut sudah pada stage CANCELLED.
  6. Source code model estate_property.py
    from odoo import api, fields, models
    
    class EstateProperty(models.Model):
    	_name = "estate.property"
    	_description = "Real Estate Property"
    
    	active = fields.Boolean(default=True)
    	state = fields.Selection(
        	[ 
            	('n', 'New'),
            	('o', 'Offer Received'),
            	('a', 'Offer Accepted'),
            	('s', 'Sold'),('c', 'Cancelled'),
            ],
        	'State', default='n'
        )
    
    	name = fields.Char(required=True,default='Rumah Baru')
    	description = fields.Text()
    
    	property_type_id = fields.Many2one("estate.property.type", string="Property Type")
    	tags_id = fields.Many2many("estate.property.tag", string="Property Tags")
    
    	postcode = fields.Char()
    	date_availability = fields.Date(copy=False)
    	expected_price = fields.Float()
    	selling_price = fields.Float(readonly=True,copy=False)
    	bedrooms = fields.Integer(default=2)
    	living_area = fields.Integer()
    	facades = fields.Integer()
    	garage = fields.Boolean()
    	garden = fields.Boolean()
    	garden_area = fields.Integer()
    	garden_orientation = fields.Selection(
        	[ 
            	('n', 'North'),
            	('s', 'South'),
            	('e', 'East'),
            	('w', 'West'),
            ],
        	'Garden Orientation', default='e'
    	)
    
    	user_id = fields.Many2one("res.users", string="Salesman",default=lambda self: self.env.user)
    	buyer_id = fields.Many2one("res.partner", string="Buyer",copy=False)
    
    	offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offer")
    
    	best_price = fields.Float("Best Offer", compute="_compute_best_price", help="Best offer received")
    
    	@api.depends("offer_ids.price")
    	def _compute_best_price(self):
    		for prop in self:
    			prop.best_price = max(prop.offer_ids.mapped("price")) if prop.offer_ids else 0.0
    
    	@api.onchange("garden")
    	def _onchange_garden(self):
    		if self.garden:
    			self.garden_area = 10
    			self.garden_orientation = "n"
    		else:
    			self.garden_area = 0
    			self.garden_orientation = False
    
    	def action_sold(self):
    		if "c" in self.mapped("state"):
    			raise UserError("Canceled properties cannot be sold.")
    		return self.write({"state": "s"})
    
    	def action_cancel(self):
    		if "s" in self.mapped("state"):
    			raise UserError("Sold properties cannot be canceled.")
    		return self.write({"state": "c"})
    
  7. Source code view estate_property_views.xml
    <?xml version="1.0"?>
    <odoo>
        <record id="estate_property_action" model="ir.actions.act_window">
            <field name="name">Property</field>
            <field name="res_model">estate.property</field>
            <field name="view_mode">tree,form</field>
        </record>
    
    	<record id="estate_property_view_tree" model="ir.ui.view">
            <field name="name">estate.property.tree</field>
            <field name="model">estate.property</field>
            <field name="arch" type="xml">
                <tree string="Rumahan" >
                    <field name="name"/>
                    <field name="property_type_id"/>
                	<field name="postcode"/>
                    <field name="bedrooms"/>
                    <field name="living_area"/>
                    <field name="expected_price"/>
                    <field name="selling_price"/>
                    <field name="date_availability"/>
                    <field name="user_id"/>
                    <field name="buyer_id"/>
                </tree>
            </field>
        </record>
    
    	<record id="estate_property_view_form" model="ir.ui.view">
            <field name="name">estate.property.form</field>
            <field name="model">estate.property</field>
            <field name="arch" type="xml">
            	<form string="Rumahan">
                    <header>
                        <button name="action_sold" type="object" string="Sold" states="n,o"/>
                        <button name="action_sold" type="object" string="Sold" states="o" class="oe_highlight"/>
                        <button name="action_cancel" type="object" string="Cancel" states="n,o,a"/>
                        <field name="state" widget="statusbar" statusbar_visible="n,o,a,s"/>
                    </header>            
       				 <sheet>
            			<group>
               				<group>
                    			<field name="name"/>
                    			<field name="property_type_id"/>
                    			<field name="tags_id" widget="many2many_tags"/>
                            	<field name="postcode"/>
                    			<field name="date_availability"/>
                			</group>
                			<group>
                    			<field name="expected_price"/>
                    			<field name="best_price"/>
                            	<field name="selling_price"/>
                			</group>
            			</group>
                		<notebook>
                    		<page string="Description">
               				<group>
                        		<field name="description"/>
                        		<field name="bedrooms"/>
                        		<field name="living_area"/>
                        		<field name="facades"/>
                        		<field name="garage"/>
                        		<field name="garden"/>
                        		<field name="garden_area"/>
                        		<field name="garden_orientation"/>
                			</group>
                    		</page>
                     		<page string="Offers">
                            	<group>
                    				<field name="offer_ids"/>
                                </group>
                        	</page>
                        	<page string="Other Info">
                            	<group>
                    				<field name="user_id"/>
                   			 		<field name="buyer_id"/>
                                </group>
                            </page>
                		</notebook>
        			</sheet>
    			</form>
            </field>
        </record>
    
    	<record id="estate_property_view_search" model="ir.ui.view">
            <field name="name">estate.property.tree</field>
            <field name="model">estate.property</field>
            <field name="arch" type="xml">
                <search string="Rumahan" >
                    <field name="name"/>
                    <field name="postcode"/>
                    <field name="expected_price"/>
                    <field name="bedrooms"/>
                    <field name="living_area"/>
                    <field name="facades"/>
    				<filter string="Archived" name="inactive" domain="[('active', '=', False)]"/>		
            	</search>
            </field>
        </record>
    
    </odoo>
    

Informasi lebih lanjut silahkan mengunjungi
1. https://www.odoo.com/documentation/16.0/developer/howtos/rdtraining/10_actions.html .
2. https://github.com/odoo/technical-training-solutions/tree/16.0-core/estate .

Kunjungi www.proweb.co.id untuk menambah wawasan anda.

Jika anda tertarik mengenai aplikasi Odoo ERP silahkan mengisi form di bawah ini

Pembuatan Button di Form pada Pemrograman Odoo ERP