# -*- coding: utf-8 -*-
from odoo import api, fields, models
from odoo.exceptions import UserError

class ClassSchedulePrint(models.TransientModel):
    _name = 'class_schedule.print'

    start_date = fields.Date(string='起始日期')
    end_date = fields.Date(string='結束日期')
    company_id = fields.Many2one(comodel_name='res.company', string='當前公司',default=lambda self: self.env.user.company_id.id)

    def confirm_date(self):
        # ids = self.env['class.timetable'].search([('date', '>=', self.start_date),('date','<=',self.end_date),('company_id','=',self.env.user.company_id.id)], order="class_name asc").ids
        data = self.env['class.timetable'].search([('date', '>=', self.start_date), ('date', '<=', self.end_date),('company_id', '=', self.env.user.company_id.id)],order="class_name asc")
        if not data:
            raise UserError("此時間區間並無課表")
        for line in data:
            if not line.class_room:
                raise UserError("課程: %s ，教室未設定" % line.class_name.name)
        docargs = {
            'start_date': self.start_date,
            'end_date':self.end_date
        }
        return self.env.ref('jptip_core.class_list_xlsx').report_action(self, data=docargs)