# -*- 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 api, fields, models, _

class ReportCheckin(models.TransientModel):
    _name = "report.checkin"

    checkin_date = fields.Date('Checkin date', required=True)

    @api.multi
    def action_print_report(self):
        return self.env['report'].get_action(self, 'etourism_reports.report_checkins') 

    @api.multi
    def get_checkins_info(self,checkin_date):
        result,client_type = [],''
        context = self._context
        return_header = ''
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        count = 0
        if enterprise_id and checkin_date:
            self._cr.execute("""select distinct r.partner_id as id, pr.display_name,t.name as room_name,b.checkin,b.checkout
,c.name as room_type_name,cd.name as condition,par.partner_type,par.name as client_name
from etourism_booking b,etourism_booking_res_partner_rel r,res_partner pr, etourism_room rm,product_product p, product_template t
, product_category c, etourism_booking_condition cd,etourism_booking_group g, res_partner par 
where r.booking_id=b.id and r.partner_id=pr.id and rm.id=b.room_id and rm.product_id=p.id and p.product_tmpl_id=t.id 
and rm.room_type_id=c.id and b.booking_condition_id=cd.id and b.booking_group_id=g.id and g.guest_id=par.id
 and b.enterprise_id=%s and b.checkin::date='%s'"""%(enterprise_id.id,checkin_date))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    if count % 2 == 0:
                        return_header += """<tr style="background-color:#FFFFFF;">"""
                    else:
                        return_header += """<tr style="background-color:#F0F0F0;">"""
                    client_type = str(res[7])
                    if client_type == 'guest':
                        client_type = _('Particular')
                    else:
                        client_type = res[8]

                    return_header += """<td>"""+ str(res[1]) + """</td>"""
                    return_header += """<td>"""+ str(client_type) + """</td>"""
                    return_header += """<td>"""+ str(res[2]) + """</td>"""
                    return_header += """<td>"""+ str(res[3])[:10] + """</td>"""
                    return_header += """<td>"""+ str(res[4])[:10] + """</td>"""
                    return_header += """<td>"""+ str(res[5]) + """</td>"""
                    return_header += """<td>"""+ str(res[6]) + """</td>"""
                    return_header += """</tr>"""
                    count += 1
        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

