# -*- coding: utf-8 -*-
###############################################################################
#
#    Tech-Receptives Solutions Pvt. Ltd.
#    Copyright (C) 2009-TODAY Tech-Receptives(<http://www.techreceptives.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
import time

from odoo.osv import expression
from odoo.tools.float_utils import float_round as round
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.exceptions import UserError, ValidationError
from odoo import api, fields, models, _
import datetime

import odoo.addons.decimal_precision as dp
		
class EtourismStopSales(models.Model):
    _name = 'etourism.stop.sales'
    _order = 'start_date DESC'

    #pricelist_id = fields.Many2one('product.pricelist', 'Pricelist', index=True, required=True)
    enterprise_id = fields.Many2one('etourism.enterprise', string='Enterprise', required=True)
    start_date = fields.Datetime('Start Date')
    end_date = fields.Datetime('End Date')
    room_type_id = fields.Many2one('product.category', 'Room Type')

    @api.model
    def default_get(self, fields):
        res = super(EtourismStopSales, self).default_get(fields)
        if self.env.uid:
            enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
            if enterprise_id:
                res.update({'enterprise_id': enterprise_id.id})
        return res

    @api.constrains('start_date', 'end_date')
    def start_end_dates(self):
        """
        End date should be greater than the start date.
        """
        if self.start_date and self.end_date:
            start_date = datetime.datetime.strptime(self.start_date, '%Y-%m-%d %H:%M:%S').date()
            end_date = datetime.datetime.strptime(self.end_date, '%Y-%m-%d %H:%M:%S').date()
            if self.end_date < self.start_date:
                raise ValidationError(_('End date should be equal or greater than Start date.'))

    @api.onchange('start_date')
    def onchange_start_date(self):
        if not (self.start_date):
            return {'value': {}}
        else:
            start_dt = datetime.datetime.strptime(self.start_date,'%Y-%m-%d %H:%M:%S')
            start_date = start_dt.replace(hour=15, minute=00, second=00)
            self.start_date=start_date
            if self.end_date:
                end_dt = datetime.datetime.strptime(self.end_date,'%Y-%m-%d %H:%M:%S') 
                end_date = end_dt.replace(hour=13, minute=00, second=00)
                if end_date <= start_date:
                    end_date = start_date + datetime.timedelta(days=1)
                self.end_date=end_date

    @api.onchange('end_date')
    def onchange_end_date(self):
        if not (self.end_date):
            return {'value': {}}
        else:
            if self.start_date:
                start_dt = datetime.datetime.strptime(self.start_date,'%Y-%m-%d %H:%M:%S') 
                start_date = start_dt.replace(hour=15, minute=00, second=00)
                end_dt = datetime.datetime.strptime(self.end_date,'%Y-%m-%d %H:%M:%S')
                end_date = end_dt.replace(hour=13, minute=00, second=00)
                if start_dt >= end_date:
                    end_dt = start_dt + datetime.timedelta(days=1)
                    end_date = end_dt.replace(hour=13, minute=00, second=00)
                end_date_compare = end_dt.replace(hour=15, minute=00, second=01)
                end_date_dur = end_dt.replace(hour=15, minute=00, second=00)
                if start_date >= end_date_compare:
                    start_date = end_date_dur - datetime.timedelta(days=1)
                self.start_date=start_date
            else:
                end_dt = datetime.datetime.strptime(self.end_date,'%Y-%m-%d %H:%M:%S')
                end_date = end_dt.replace(hour=13, minute=00, second=00)
            self.end_date=end_date

    '''@api.onchange('start_date','end_date')
    def onchange_dates(self):
        #print "ON DATE CHANGE"
        if not (self.start_date and self.end_date):
            return {'value': {}}
        elif self.enterprise_id:
            if self.start_date and self.end_date and self.end_date < self.start_date:
                self.end_date = self.start_date
            start_date = datetime.strptime(self.start_date, '%Y-%m-%d %H:%M:%S').replace(hour=15, minute=00,second=00)
            end_date = datetime.strptime(self.end_date, '%Y-%m-%d %H:%M:%S').replace(hour=13, minute=00,second=00)

            if start_date > end_date:
                raise ValidationError("End date must be greater or equal then start date.")
            self.start_date = start_date
            self.end_date = end_date'''
