#-*- coding:utf-8 -*-

from openerp import fields, models, api
from openerp.exceptions import ValidationError
#import xlrd
import base64
import logging
import datetime
from dateutil.relativedelta import relativedelta

_logger = logging.getLogger(__name__)

class PlanningWizard(models.TransientModel):
    _name = "etourism.planning.wizard"
    _description = "Create Planning"

    enterprise_id = fields.Many2one('etourism.enterprise', 'Enterprise',required=True)

    @api.multi
    def action_create_planning(self):
        """Create planning
        """
        count=0
        enterprise_id=self.enterprise_id.id
        bookings = self.env['etourism.booking'].search([('enterprise_id','=',enterprise_id)])
        for booking in bookings:
            checkin = datetime.datetime.strptime(booking.checkin, '%Y-%m-%d %H:%M:%S').date()
            checkout = datetime.datetime.strptime(booking.checkout, '%Y-%m-%d %H:%M:%S').date()

            planning=self.env['etourism.booking.planning'].create({
                        'enterprise_id':booking.enterprise_id.id,
                        'booking_date':booking.booking_date,
                        'checkin':checkin,
                        'checkout':checkout,
                        'duration':booking.duration,
                        'room_id':booking.room_id.id,
                        'booking_group_id':booking.booking_group_id.id,
                        'booking_id':booking.id,
                        'state':booking.state,
                        'amount':booking.amount,
                        'unitary_amount':booking.unitary_amount,
                        'num_adults':booking.num_adults,
                        'num_children':booking.num_children,
                        'pricelist_item_id':booking.pricelist_item_id.id,
                        'num_extra_bed':booking.num_extra_bed,
                        'extra_beds_amount':booking.extra_beds_amount,
                        'online_booking':booking.online_booking
            })
            count=count+1

        if count > 0:
            return self.env['warning'].info(title='Success', message="The planning was created successfully!!")
        else:
            return self.env['warning'].warning(title='Warning', message="No planning were created!!")
