# -*- coding: utf-8 -*-
##############################################################################
#
#    Odoo, Open Source Management Solution
#    Copyright (C) 2016-TODAY Prime Consulting SA, Cape Verde (<http://prime.cv>).
#
##############################################################################
import time
from datetime import datetime, timedelta

from odoo import models, fields, api


class ReportBookingsByRoomType(models.TransientModel):
    _name = "report.bookings.by.room.type"

    enterprise_id = fields.Many2one('etourism.enterprise', 'Enterprise',required=True)
    start_date = fields.Date('Start date')
    end_date = fields.Date('End date')

    @api.model
    def default_get(self, fields):
        res = super(ReportBookingsByRoomType, 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.multi
    def action_print_report(self):
        return self.env['report'].get_action(self, 'etourism_reports.report_bookings_by_room_type') 

    @api.multi
    def get_bookings_info(self, enterprise_id,start_date,end_date):
        result = []
        return_header = ''
        if enterprise_id and start_date and end_date:
            self._cr.execute("""select c.name as room_type_name,count(*) as cont from view_bookings_reports v, product_category c 
			where c.id=v.room_type_id and v.enterprise_id=%s and v.checkin >= '%s' and v.checkin <= '%s' and v.room_type_id is not null group by c.name"""%
                             (enterprise_id,start_date,end_date))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    return_header += """<tr>"""
                    return_header += """<td class="border-top">"""+ res[0].encode('utf-8') + """</td>"""
                    return_header += """<td class="border-left-top">"""+ str(res[1]) + """</td>"""
                    return_header += """</tr>"""
        return return_header

    @api.multi
    def get_ebusiness_contacts(self,field=''):
        email,phone,mobile = '','',''
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        ebusiness_id = enterprise_id.ebusiness_id
        for contact in ebusiness_id.contacts_line:
            if contact.default_contact:
                email = contact.email
                phone = contact.phone
                mobile = contact.mobile
        if field == 'email':
            return email
        elif field == 'phone':
            return phone
        elif field == 'mobile':
            return mobile

class ReportBookingsByFare(models.TransientModel):
    _name = "report.bookings.by.fare"

    enterprise_id = fields.Many2one('etourism.enterprise', 'Enterprise',required=True)
    start_date = fields.Date('Start date')
    end_date = fields.Date('End date')

    @api.model
    def default_get(self, fields):
        res = super(ReportBookingsByFare, 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.multi
    def action_print_report(self):
        return self.env['report'].get_action(self, 'etourism_reports.report_bookings_by_fare') 

    @api.multi
    def get_bookings_info(self, enterprise_id,start_date,end_date):
        result = []
        return_header = ''
        if enterprise_id and start_date and end_date:
            self._cr.execute("""select c.name as booking_condition_name,count(*) as cont from view_bookings_reports v, etourism_booking_condition c 
			where c.id=v.booking_condition_id and v.enterprise_id=%s and v.checkin >= '%s' and v.checkin <= '%s' and v.booking_condition_id is not null group by c.name"""%
                             (enterprise_id,start_date,end_date))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    return_header += """<tr>"""
                    return_header += """<td class="border-top">"""+ res[0].encode('utf-8') + """</td>"""
                    return_header += """<td class="border-left-top">"""+ str(res[1]) + """</td>"""
                    return_header += """</tr>"""
        return return_header

    @api.multi
    def get_ebusiness_contacts(self,field=''):
        email,phone,mobile = '','',''
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        ebusiness_id = enterprise_id.ebusiness_id
        for contact in ebusiness_id.contacts_line:
            if contact.default_contact:
                email = contact.email
                phone = contact.phone
                mobile = contact.mobile
        if field == 'email':
            return email
        elif field == 'phone':
            return phone
        elif field == 'mobile':
            return mobile

class ReportBookingsByOvernightStays(models.TransientModel):
    _name = "report.bookings.overnight.stays"

    enterprise_id = fields.Many2one('etourism.enterprise', 'Enterprise',required=True)
    start_date = fields.Date('Start date')
    end_date = fields.Date('End date')

    @api.model
    def default_get(self, fields):
        res = super(ReportBookingsByOvernightStays, 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.multi
    def action_print_report(self):
        return self.env['report'].get_action(self, 'etourism_reports.report_bookings_overnight_stays') 

    @api.multi
    def get_bookings_info(self, enterprise_id,start_date,end_date):
        result = 0
        if enterprise_id and start_date and end_date:
            domain = [('enterprise_id','=',enterprise_id),('checkin', '>=', start_date),('checkin', '<=', end_date)]
            result = self.env['view.bookings.reports'].search_count(domain)
        return result

    @api.multi
    def get_ebusiness_contacts(self,field=''):
        email,phone,mobile = '','',''
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        ebusiness_id = enterprise_id.ebusiness_id
        for contact in ebusiness_id.contacts_line:
            if contact.default_contact:
                email = contact.email
                phone = contact.phone
                mobile = contact.mobile
        if field == 'email':
            return email
        elif field == 'phone':
            return phone
        elif field == 'mobile':
            return mobile


