Dalam pembuatan aplikasi menggunakan Odoo ERP kita akan mendapati value suatu field berasal dari perhitungan field-field lain. Di sinilah kita membuat computed field pada Odoo ERP.

Computed field pada pengembangan aplikasi dengan Odoo ERP adalah seperti pada penjelasan-penjelasan berikut ini

  1. Update model


    Kita masuk ke Virtualmin dari Webmin, masuk ke File Manager,  kemudian menambahkan import api dan source code mulai best_price seperti pada gambar di atas.
  2. Update view

    Kita kemudian menambahkan field best_price pada view form 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

    Kita kemudian mengimplementasikan perubahan source dengan Upgrade modul seperti pada gambar di atas.
  5. Hasil pada tampilan form

    Tampak field best_price yang readonly karena merupakan hasil perhitungan dari field-field lain.
  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
    

    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">
       				 <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/09_compute_onchange.html .
2. https://www.odoo.com/documentation/16.0/developer/reference/backend/orm.html#reference-fields-compute .
3. https://www.w3schools.com/python/ref_func_max.asp .
4. https://www.odoo.com/documentation/16.0/developer/reference/backend/orm.html#map .
5. https://github.com/odoo/technical-training-solutions/tree/16.0-core/estate .
6. https://thispointer.com/python-if-else-in-one-line-a-ternary-operator/ .

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

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

Computed Field pada Pengembangan Aplikasi dengan Odoo ERP