36 lines
1.8 KiB
Python

from odoo import api, fields, models
class EducationLibraryBook(models.Model):
_name = 'education.library.book'
_description = 'Library Book Master'
name = fields.Char(string='Title', related='title', store=True)
title = fields.Char(string='Title', required=True)
subtitle = fields.Char(string='Subtitle')
isbn = fields.Char(string='ISBN')
author = fields.Char(string='Author', required=True)
publisher = fields.Char(string='Publisher')
edition = fields.Char(string='Edition')
publication_year = fields.Char(string='Publication Year')
language = fields.Char(string='Language', default='English')
category_id = fields.Many2one('education.library.category', string='Category')
subject_id = fields.Many2one('education.subject', string='Subject')
description = fields.Text(string='Description')
cover_image = fields.Image(string='Cover Image', max_width=1920, max_height=1920)
active = fields.Boolean(default=True)
copy_ids = fields.One2many('education.library.book.copy', 'book_id', string='Physical Copies')
total_copies = fields.Integer(string='Total Copies', compute='_compute_copy_stats', store=True)
available_copies = fields.Integer(string='Available Copies', compute='_compute_copy_stats', store=True)
issued_copies = fields.Integer(string='Issued Copies', compute='_compute_copy_stats', store=True)
@api.depends('copy_ids', 'copy_ids.status', 'copy_ids.active')
def _compute_copy_stats(self):
for book in self:
active_copies = book.copy_ids.filtered(lambda c: c.active)
book.total_copies = len(active_copies)
book.available_copies = len(active_copies.filtered(lambda c: c.status == 'available'))
book.issued_copies = len(active_copies.filtered(lambda c: c.status == 'issued'))