# -*- coding: utf-8 -*-
###############################################################################
#
#    Tech-Receptives Solutions Pvt. Ltd.
#    Copyright (C) 2009-TODAY Tech-Receptives(<http://www.techreceptives.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

import time
import datetime
from dateutil.relativedelta import relativedelta
import math

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 ResPartner(models.Model):
    _inherit = ['res.partner']

    my_client = fields.Boolean(string='User Client', compute="_compute_my_client", search='_search_my_client')

    @api.one
    def _compute_my_client(self):
        is_client = False
        for partner in self:
            if self.user_has_groups('etourism_avt.group_etourism_avt_enterprise'):
                enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
                self._cr.execute("""select client_id
from etourism_avt_booking where enterprise_id = %s and client_id = %s
union
select client_id
from etourism_avt_booking_request where enterprise_id = %s and client_id = %s
"""%(enterprise_id.id, partner_id.id,enterprise_id.id, partner_id.id))
                result = self._cr.fetchall()
                if result:
                    for res in result:
                        client_id = res[0]
                        if client_id == partner.id:
                            is_client = True
                        else:
                            is_client = False
			
            partner.my_client = is_client

    def _search_my_client(self, operator, value):
        res = []
        if self.user_has_groups('etourism_avt.group_etourism_avt_enterprise'):
            enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id

            self._cr.execute("""select client_id
from etourism_avt_booking where enterprise_id = %s
union
select client_id
from etourism_avt_booking_request where enterprise_id = %s
"""%(enterprise_id.id,enterprise_id.id))
            result = self._cr.fetchall()
            for rlt in result:
                res.append(rlt[0])
        else:
            self._cr.execute("""select client_id
from etourism_avt_booking
union
select client_id
from etourism_avt_booking_request""")
            result = self._cr.fetchall()
            for rlt in result:
                res.append(rlt[0])

        return [('id', 'in', res)]
