# -*- 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 EtourismBookingCancel(models.TransientModel):
    _inherit = 'etourism.booking.cancel.wizard'
    _description = 'Open Booking Cancel Wizard'

    @api.multi
    def action_cancel(self):
        print "LET'S CANCEL::::::::::::"
        for booking in self:
            booking_group_id=booking.booking_group_id
            cancellation_reason_id = booking.cancellation_reason_id.id
            booking_lines = self.env['etourism.booking']
            cancelIds =[]
            if booking.booking_ids:
                for bk in booking.booking_ids:
                    print "BOOKING::::::::::::: ",bk.id
                    cancelIds.append(bk.id)
                    bk.write({'state': 'cancelled'})
            bk_not_selected = booking_lines.sudo().search([('id', 'not in', cancelIds), ('state', '!=', 'cancelled'), ('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:
                print "WILL WRITE AND SEND NOTIFICATION:::::::::::::", cancellation_reason_id
                #raise ValidationError(_('Test'))
                booking_group_id.write({'state': 'cancelled','cancellation_reason_id':cancellation_reason_id})
                _logger.info('Sending Email notification on booking cancellation. Booking Id: %s'%(self.id))
                booking_group_id.sudo().send_email_notification('mail_template_etourism_booking_cancellation')
            #raise ValidationError(_('Test'))
            #booking_group_id.write({'state': 'cancelled'})
