# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import time
import datetime
from dateutil.relativedelta import relativedelta
import math
import logging
_logger = logging.getLogger(__name__)

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, _
from odoo.tools.misc import formatLang
import json

import urlparse
import uuid

from datetime import date

class EtourismBookingNoShow(models.TransientModel):
    _inherit = 'etourism.booking.no_show.wizard'
    _description = 'Open Booking No Show Wizard'

    @api.multi
    def action_noshow(self):
        print "LET'S MARK AS NO SHOW::::::::::::"
        for booking in self:
            booking_group_id = booking.booking_group_id
            booking_lines = self.env['etourism.booking']
            noShowIds = []
            if booking.booking_ids:
                for bk in booking.booking_ids:
                    print "BOOKING::::::::::::: ", bk.id
                    noShowIds.append(bk.id)
                    bk.write({'state': 'cancelled'})
            bk_not_selected = booking_lines.sudo().search([('id', 'not in', noShowIds), ('state', '!=', 'no_show'),
                                                           ('booking_group_id', '=', booking_group_id.id)])
            for a in booking_group_id.booking_line:
                print "STATES:::::::::::::", a.state
            print "NOT SELECTED:::::::::::", len(bk_not_selected)
            if len(bk_not_selected) == 0:
                booking_group_id.write({'state': 'no_show'})
                _logger.info('Sending Email notification on booking no-show. Booking Id: %s'%(self.id))
                booking_group_id.sudo().send_email_notification('mail_template_etourism_booking_noshow')
            #raise ValidationError(_('Test'))
