# -*- coding: utf-8 -*-
# Author : Peter Wu

from odoo import models,fields,api
import datetime


class jtteacherbatchline2(models.Model):
    _name = "jt.teacher.batch.linelist"
    _description = '排課同步參考表'

    order_id = fields.Integer(string="LINE1_ID", index=True)
    timing_date = fields.Date(string='日期',index=True)
    teacher_id = fields.Many2one('jt.teacher', string='老師',index=True)
    timing_set = fields.Many2one('jt.timing.manager', string="時段")
    company_id = fields.Many2one(related='teacher_id.company_id', string='Company', store=True, readonly=True)

class teacherbatchinherit(models.Model):
    _inherit = "jt.teacher.batch"

    line1_ids = fields.One2many('jt.teacher.batch.line1', 'order_id')
    line2_ids = fields.One2many('jt.teacher.batch.line2', 'timing_id')
    # month_type = fields.Char()

    def gen_jt_teacher_line(self):
        myid = self.env.context.get('jt_batch_id')
        myrec = self.env['jt.teacher.batch'].search([('id','=',myid)])
        mystartdate = myrec.date_from
        myenddate = myrec.date_to
        mycompany = myrec.company_id.id
        d = datetime.datetime.strptime(mystartdate,'%Y-%m-%d')
        d1 = datetime.datetime.strptime(myenddate,'%Y-%m-%d')
        mydays = ((d1 - d).days)
        mydays1 = int(mydays) + 1
        self.env.cr.execute("""select gen_jtbatchline(%d,%d,%d)""" % (myid,mydays1,mycompany))
        self.env.cr.execute("""select update_jtbatchline(%d)""" % myid)
        self.env.cr.execute("""select update_linelist(%d)""" % myid)

    @api.model
    def create(self,vals):
        res=super(teacherbatchinherit,self).create(vals)
        self.env.cr.execute("""select update_jtbatchline(%d)""" % res.id)
        self.env.cr.execute("""select update_linelist(%d)""" % res.id)
        return res

    @api.multi
    def write(self,vals):
        res=super(teacherbatchinherit,self).write(vals)
        self.env.cr.execute("""select update_jtbatchline(%d)""" % self.id)
        self.env.cr.execute("""select update_linelist(%d)""" % self.id)
        return res




class jtteacherbatchline1(models.Model):
    _name = "jt.teacher.batch.line1"

    order_id = fields.Many2one('jt.teacher.batch',ondelete='cascade')
    teacher_id = fields.Many2one('jt.teacher', string='老師')
    company_id = fields.Many2one(related='order_id.company_id', string='Company', store=True, readonly=True)
    teacher_days = fields.Integer(string='合計',default=0)
    # month_days = fields.Integer()
    month_type = fields.Char()    # 28 days :'1' , 29 days : '2' ,30 days : '3', 31 days :'4'
    date_1 = fields.Many2one('jt.timing.manager', string='1')
    date_2 = fields.Many2one('jt.timing.manager', string='2')
    date_3 = fields.Many2one('jt.timing.manager', string='3')
    date_4 = fields.Many2one('jt.timing.manager', string='4')
    date_5 = fields.Many2one('jt.timing.manager', string='5')
    date_6 = fields.Many2one('jt.timing.manager', string='6')
    date_7 = fields.Many2one('jt.timing.manager', string='7')
    date_8 = fields.Many2one('jt.timing.manager', string='8')
    date_9 = fields.Many2one('jt.timing.manager', string='9')
    date_10 = fields.Many2one('jt.timing.manager', string='10')
    date_11 = fields.Many2one('jt.timing.manager', string='11')
    date_12 = fields.Many2one('jt.timing.manager', string='12')
    date_13 = fields.Many2one('jt.timing.manager', string='13')
    date_14 = fields.Many2one('jt.timing.manager', string='14')
    date_15 = fields.Many2one('jt.timing.manager', string='15')
    date_16 = fields.Many2one('jt.timing.manager', string='16')
    date_17 = fields.Many2one('jt.timing.manager', string='17')
    date_18 = fields.Many2one('jt.timing.manager', string='18')
    date_19 = fields.Many2one('jt.timing.manager', string='19')
    date_20 = fields.Many2one('jt.timing.manager', string='20')
    date_21 = fields.Many2one('jt.timing.manager', string='21')
    date_22 = fields.Many2one('jt.timing.manager', string='22')
    date_23 = fields.Many2one('jt.timing.manager', string='23')
    date_24 = fields.Many2one('jt.timing.manager', string='24')
    date_25 = fields.Many2one('jt.timing.manager', string='25')
    date_26 = fields.Many2one('jt.timing.manager', string='26')
    date_27 = fields.Many2one('jt.timing.manager', string='27')
    date_28 = fields.Many2one('jt.timing.manager', string='28')
    date_29 = fields.Many2one('jt.timing.manager', string='29')
    date_30 = fields.Many2one('jt.timing.manager', string='30')
    date_31 = fields.Many2one('jt.timing.manager', string='31')


class jtteacherbatchline2(models.Model):
    _name = "jt.teacher.batch.line2"

    timing_id = fields.Many2one('jt.teacher.batch',ondelete='cascade')
    timing_set = fields.Many2one('jt.timing.manager',string="時段")
    teacher_tot = fields.Integer(string='合計',default=0)
    company_id = fields.Many2one(related='timing_id.company_id', string='Company', store=True, readonly=True)
    date_1 = fields.Text(string='  1  ')
    date_2 = fields.Text(string='  2  ')
    date_3 = fields.Text(string='  3  ')
    date_4 = fields.Text(string='  4  ')
    date_5 = fields.Text(string='  5  ')
    date_6 = fields.Text(string='  6  ')
    date_7 = fields.Text(string='  7  ')
    date_8 = fields.Text(string='  8  ')
    date_9 = fields.Text( string='  9  ')
    date_10 = fields.Text(string='  10 ')
    date_11 = fields.Text(string='  11 ')
    date_12 = fields.Text(string='  12 ')
    date_13 = fields.Text(string='  13 ')
    date_14 = fields.Text(string='  14 ')
    date_15 = fields.Text(string='  15 ')
    date_16 = fields.Text(string='  16 ')
    date_17 = fields.Text(string='  17 ')
    date_18 = fields.Text(string='  18 ')
    date_19 = fields.Text(string='  19 ')
    date_20 = fields.Text(string='  20 ')
    date_21 = fields.Text(string='  21 ')
    date_22 = fields.Text(string='  22 ')
    date_23 = fields.Text(string='  23 ')
    date_24 = fields.Text(string='  24 ')
    date_25 = fields.Text(string='  25 ')
    date_26 = fields.Text(string='  26 ')
    date_27 = fields.Text(string='  27 ')
    date_28 = fields.Text(string='  28 ')
    date_29 = fields.Text(string='  29 ')
    date_30 = fields.Text(string='  30 ')
    date_31 = fields.Text(string='  31 ')

