from odoo import fields, models from odoo.exceptions import UserError from datetime import date class LibraryBook(models.Model): _name = "library.book" _description = "Library Book" name = fields.Char(string="Tên sách", required=True) author = fields.Char(string="Tác giả") published_date = fields.Date(string="Ngày xuất bản") isbn = fields.Char(string="Số ISBN") price = fields.Float(string="Giá sách") borrowed_by = fields.Many2one("library.member", string="Được mượn bởi") return_date = fields.Date(string="Ngày trả") state = fields.Selection([ ('available', 'Có sẵn'), ('borrowed', 'Đã mượn'), ('overdue', 'Quá hạn') ], string='State', default='available') def action_mark_as_borrowed(self): for book in self: if book.borrowed_by: raise UserError("Sách này đã được mượn!") else: book.borrowed_by = self.env.user.partner_id.id def update_book_status(self): books = self.search([ ('return_date', '<', date.today()), ('state', '=', 'borrowed') ]) books.write({'state': 'overdue'})