Skip to Content
Menu
This question has been flagged
5 Replies
3992 Views

hai,

Hello everyone, I'm newbie, I want to write @api so that when there are customers renting that room on the table room, the status fields will automatically turn into 'busy' and vice versa will return 'free'

class booking_room(models.Model):
_name = 'room'
_rec_name = 'name'

name = fields.Char(string="Mã phòng", required=True)
   status = fields.Selection([('1', 'Trống'), ('2', 'Bận')], string="Trạng thái", required=True)
kind = fields.Selection([('3', 'A'), ('4', 'B'), ('5', 'C'), ('6', 'Vip')], string="Loại phòng", required=True)
price = fields.Integer(string="Giá phòng", required=True)
description = fields.Text("Ghi chú")
-------------------------

class guest_room(models.Model):
_name = 'guest'

name = fields.Char(string="Họ tên", required=True)
cmnd = fields.Char(string="Số CMND", required=True)
sdt = fields.Char(string="SĐT", required=True)
time_in = fields.Datetime(string="Ngày đến")
time_out = fields.Datetime(string="Ngày trả")
servicesd = fields.Many2one('service', string="Dịch vụ sử dụng")
roomsd = fields.Many2one('room', string="Phòng mượn")

Thanks

Avatar
Discard

Try to make question readable and explain details.

thank you for the help. changed a bit and it worked, but when "status" by default is "busy"

Vào Th 4, 26 thg 9, 2018 vào lúc 06:17 Juan Albarracin <aprados.juan@gmail.com> đã viết:

A new answer on @api odoo11 has been posted. Click here to access the post :

See post

--
Juan Albarracin

Sent by Odoo S.A. using Odoo.

thank you for the help. But it does not work for me

Vào Th 3, 25 thg 9, 2018 vào lúc 15:53 NIKHIL KRISHNAN <nikhilkrishnan0101@gmail.com> đã viết:

A new question @api odoo11 on Help has been posted. Click here to access the question :

See question


Sent by Odoo S.A. using Odoo.

Best Answer

Hai vu van tien,

You can define a form view for the room table.

Then create a button for allocation and free the rooms.

XML

<button name="roomallocationandfree" string="Allocation/Free" type="object" class="btn-primary"/>

Python

@api.multi
def roomallocationandfree(self):
if self.status == 1:
self.status = 2
else:
self.status = 1

Regards,

Nikhilkrishnan

Avatar
Discard
Best Answer

Hello, I think you want the field to be computed on the fly, without having to assign the value with a button right?

You can make the status field a computed field and assign its value like in my code below.

Remember to import datetime at the top of your python file, and add the field to the views you prefer!

#import required library to get current datetime
from datetime import datetime as dt
class booking_room(models.Model):
_name = 'room'
name = fields.Char(string="Mã phòng", required=True)
   status = fields.Selection([('1', 'Trống'), ('2', 'Bận')], string="Trạng thái",
        required=True, compute="_get_status"
    )
kind = fields.Selection([('3', 'A'), ('4', 'B'), ('5', 'C'), ('6', 'Vip')],
        string="Loại phòng", required=True
    )
price = fields.Integer(string="Giá phòng", required=True)
description = fields.Text("Ghi chú")
@api.multi
def _get_status(self):
        # Get the current datetime in the format that time_in and time_out fields
        # are stored in the database
        now = dt.now().strftime('%Y-%m-%d %H:%M:%S')
        for room in self:
            # look for guests that have a room booked now, and whose room id is the current
            # the order is important because as the guest table grows,
            # the nº of rows that satisfy the conditions will be lower for the first one,
            # bigger for the second, and the biggest for the last one,
            # and the search will be faster this way.
            # guest_in_room will be True is len(search)>0, False otherwise.
            guest_in_room = len(self.env['guest'].search([
                ('time_out', '>', now),
                ('time_in', '<', now),
                ('roomsd', '=', room.id)
            ])) > 0
            # If guest_in_room is True, room's status is busy, free otherwise
            if guest_in_room:
                room.status = '2'
            else:
                room.status = '1'

 Regards,

Juan Albarracin

Avatar
Discard
Best Answer

You can use state:

Ex:

https://github.com/odoo/odoo/blob/11.0/addons/hr_holidays/models/hr_holidays.py#L159-L170

https://github.com/odoo/odoo/blob/11.0/addons/hr_holidays/views/hr_holidays_views.xml#L83-L89

Avatar
Discard
Author Best Answer
 thank you
Juan Albarracin
  has answered, but to me, it does not work. fields 'status' does not work, whether or not the "guest" reservation or not "status" returned is always "blank" and can not be tampered with or created.
Avatar
Discard
Related Posts Replies Views Activity
0
Apr 19
9751
0
Oct 24
84
0
Dec 22
1324
0
Sep 17
2231
0
May 23
1684