Dalam pembuatan aplikasi pada Odoo 17 mungkin kita ingin menghubungkan dan meletakkan tambahan-tambahan data tabel kita dengan tabel yang sudah ada di Odoo demi kesederhanan pembuatan aplikasi dan kemudahan pemeliharaan. Kita dapat melakukan hal ini dengan delegation inheritance.
Delegation inheritance pada pemrograman Odoo 17 adalah seperti pada langkah-langkah berikut ini
- Start SSH file system
- Model asrama.student
Alternatif 1:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849from datetime import datetime, timedelta
from odoo import fields, models, api
class
AsramaStudent(models.Model):
_name =
'asrama.student'
_inherits = {
'res.partner'
:
'partner_id'
}
_description =
'Informasi Asrama Student'
#name = fields.Char(
'Nama pelajar'
)
partner_id = fields.Many2one(
'res.partner'
,ondelete=
'cascade'
)
gender = fields.Selection(
[(
'male'
,
'Male'
),(
'female'
,
'Female'
),(
'other'
,
'Other'
)],
string=
'Jenis kelamin'
,
help=
'Jenis kelamin pelajar'
,
)
active = fields.Boolean(
'Active'
,
default
=True,help=
'Activate/Deactive record'
)
room_id = fields.Many2one(
'asrama.room'
,
'Room'
,help=
'Pilih room'
)
hostel_id = fields.Many2one(
'asrama.hostel'
,related=
'room_id.hostel_id'
)
status = fields.Selection(
[(
'draft'
,
'Draft'
),(
'reservation'
,
'Reservation'
),(
'pending'
,
'Pending'
),(
'paid'
,
'Done'
),(
'discharge'
,
'Discharge'
),(
'cancel'
,
'Cancel'
)],
string=
'Status'
,
copy
=False,
default
=
'draft'
,
help=
'Status asrama student'
)
@api.depends(
'admission_date'
,
'discharge_date'
)
def _compute_check_duration(self):
""
"Mengecek durasi"
""
for
rec in self:
if
rec.discharge_date
and
rec.admission_date:
rec.duration = (rec.discharge_date - rec.admission_date).days
def _inverse_duration(self):
for
stu in self:
if
stu.discharge_date
and
stu.admission_date:
duration = (stu.discharge_date - stu.admission_date).days
if
duration != stu.duration:
stu.discharge_date = (stu.admission_date + timedelta(days=stu.duration)).
strftime
(
'%Y-%m-%d'
)
admission_date = fields.
Date
(
'Tanggal masuk'
,help=
'Tangga di mana pelajar masuk asrama'
,
default
=fields.Datetime.today())
discharge_date = fields.
Date
(
'Tanggal keluar'
,help=
'Tanggal di mana pelajar keluar asrama'
)
duration = fields.Integer(
'Durasi'
, compute=
'_compute_check_duration'
,inverse=
'_inverse_duration'
,
help=
'Durasi lamanya tinggal'
)
Alternatif 2:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647from datetime import datetime, timedelta
from odoo import fields, models, api
class
AsramaStudent(models.Model):
_name =
'asrama.student'
_description =
'Informasi Asrama Student'
partner_id = fields.Many2one(
'res.partner'
, ondelete=
'cascade'
, delegate=True)
name = fields.Char(
'Nama pelajar'
)
gender = fields.Selection(
[(
'male'
,
'Male'
),(
'female'
,
'Female'
),(
'other'
,
'Other'
)],
string=
'Jenis kelamin'
,
help=
'Jenis kelamin pelajar'
,
)
active = fields.Boolean(
'Active'
,
default
=True,help=
'Activate/Deactive record'
)
room_id = fields.Many2one(
'asrama.room'
,
'Room'
,help=
'Pilih room'
)
hostel_id = fields.Many2one(
'asrama.hostel'
,related=
'room_id.hostel_id'
)
status = fields.Selection(
[(
'draft'
,
'Draft'
),(
'reservation'
,
'Reservation'
),(
'pending'
,
'Pending'
),(
'paid'
,
'Done'
),(
'discharge'
,
'Discharge'
),(
'cancel'
,
'Cancel'
)],
string=
'Status'
,
copy
=False,
default
=
'draft'
,
help=
'Status asrama student'
)
@api.depends(
'admission_date'
,
'discharge_date'
)
def _compute_check_duration(self):
""
"Mengecek durasi"
""
for
rec in self:
if
rec.discharge_date
and
rec.admission_date:
rec.duration = (rec.discharge_date - rec.admission_date).days
def _inverse_duration(self):
for
stu in self:
if
stu.discharge_date
and
stu.admission_date:
duration = (stu.discharge_date - stu.admission_date).days
if
duration != stu.duration:
stu.discharge_date = (stu.admission_date + timedelta(days=stu.duration)).
strftime
(
'%Y-%m-%d'
)
admission_date = fields.
Date
(
'Tanggal masuk'
,help=
'Tangga di mana pelajar masuk asrama'
,
default
=fields.Datetime.today())
discharge_date = fields.
Date
(
'Tanggal keluar'
,help=
'Tanggal di mana pelajar keluar asrama'
)
duration = fields.Integer(
'Durasi'
, compute=
'_compute_check_duration'
,inverse=
'_inverse_duration'
,
help=
'Durasi lamanya tinggal'
)
- View asrama_student
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051<?xml version=
"1.0"
encoding=
"utf-8"
?>
<odoo>
<!-- asrama.student form view -->
<record id=
"asrama_student_view_form"
model=
"ir.ui.view"
>
<field name=
"name"
>asrama.student.view.form</field>
<field name=
"model"
>asrama.student</field>
<field name=
"arch"
type=
"xml"
>
<form string=
""
>
<sheet>
<group>
<group>
<field name=
"name"
/>
<field name=
"city"
/>
<field name=
"zip"
/>
<field name=
"country_id"
/>
<field name=
"gender"
/>
<field name=
"active"
/>
</group>
<group>
<field name=
"room_id"
/>
<field name=
"hostel_id"
/>
<field name=
"status"
/>
<field name=
"admission_date"
/>
<field name=
"discharge_date"
/>
<field name=
"duration"
/>
</group>
</group>
</sheet>
</form>
</field>
</record>
<record model=
"ir.actions.act_window"
id=
"action_asrama_student"
>
<field name=
"name"
>Students</field>
<field name=
"type"
>ir.actions.act_window</field>
<field name=
"res_model"
>asrama.student</field>
<field name=
"view_mode"
>tree,form</field>
<field name=
"domain"
>[]</field>
<field name=
"context"
>{}</field>
<field name=
"help"
type=
"html"
>
<p
class
=
"oe_view_nocontent_create"
>
Create Student.
</p>
</field>
</record>
<!-- This Menu Item must have a parent
and
an action -->
<menuitem id=
"asrama_student_menu"
name=
"Pelajar-pelajar"
parent=
"hostel_main_menu"
action=
"action_asrama_student"
sequence=
"3"
/>
</odoo>
- Start Odoo dengan upgrade modul
- Tabel res_partner
- Tabel asrama_student
Tabel:
Foreign key:
- Form asrama student
Kunjungi www.proweb.co.id/implementasi-odoo/ untuk menambah wawasan implementasi Odoo ERP.