# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models


class ResPartner(models.Model):
    _inherit = 'res.partner'

    @api.model
    def name_search(self, name, args=None, operator='ilike', limit=100):        
        """ This function filters Guests on Etoursim Booking based on Enterprise
        """
        context = self._context or {}
        if self.user_has_groups('ecom.group_ecom_hotel'):
            guests = []
            if self.env.uid:
                enterprise_rec = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
                enterprise_id = enterprise_rec.id
                self._cr.execute("""select guest_id from etourism_booking_group where enterprise_id=%s;"""%(enterprise_id))
                result = self._cr.fetchall()
                if result:
                    for res in result:
                        guests.append(res[0])

                self._cr.execute("""select r.partner_id from etourism_booking b,etourism_booking_res_partner_rel r where r.booking_id=b.id and b.enterprise_id=%s;"""%(enterprise_id)
                            )
                result = self._cr.fetchall()
                if result:
                    for res in result:
                        guests.append(res[0])

                self._cr.execute("""select p.id from res_partner p where p.is_guest=true 
				and p.id not in (select guest_id from etourism_booking_group where enterprise_id=%s and guest_id is not null)
				and p.id not in (select guest_id from etourism_booking_group where enterprise_id<>%s and guest_id is not null)
				and p.id not in (select r.partner_id from etourism_booking b,etourism_booking_res_partner_rel r where r.booking_id=b.id and b.enterprise_id=%s) 
				and p.id not in (select r.partner_id from etourism_booking b,etourism_booking_res_partner_rel r where r.booking_id=b.id and b.enterprise_id<>%s)"""%(enterprise_id,enterprise_id,enterprise_id,enterprise_id))
                result = self._cr.fetchall()
                if result:
                    for res in result:
                        guests.append(res[0])

                self._cr.execute("""select r.id from res_partner r where r.company_id=%s and r.customer = 't';"""%(self.env.user.company_id.id))
                result = self._cr.fetchall()
                if result:
                    for res in result:
                        guests.append(res[0])

                self._cr.execute("""select r.id from res_partner r, res_users u where u.partner_id=r.id""")
                result = self._cr.fetchall()
                if result:
                    for res in result:
                        guests = [x for x in guests if x != res[0]]

            guests = list(set(guests))

            #args = [('id','in', guests)]
            args.append((('id','in', guests)))
        return super(ResPartner, self).name_search(name, args, operator=operator, limit=limit)	

    @api.model
    def search(self, args, offset=0, limit=None, order=None, count=False):
        """ This function updates customer's purchases
        """
        context = self._context or {}
        if self.user_has_groups('ecom.group_ecom_hotel'):
            guests = []
            if self.env.uid:
                enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
                self._cr.execute("""select guest_id from etourism_booking_group where enterprise_id=%s;"""%(enterprise_id.id))
                result = self._cr.fetchall()
                if result:
                    for res in result:
                        guests.append(res[0])

                self._cr.execute("""select r.partner_id from etourism_booking b,etourism_booking_res_partner_rel r where r.booking_id=b.id and b.enterprise_id=%s;"""%(enterprise_id.id))

                result = self._cr.fetchall()
                if result:
                    for res in result:
                        guests.append(res[0])
						
                self._cr.execute("""select p.id from res_partner p where p.is_guest=true 
	    			and p.id not in (select guest_id from etourism_booking_group where enterprise_id=%s and guest_id is not null)
		    		and p.id not in (select guest_id from etourism_booking_group where enterprise_id<>%s and guest_id is not null)
			    	and p.id not in (select r.partner_id from etourism_booking b,etourism_booking_res_partner_rel r where r.booking_id=b.id and b.enterprise_id=%s)
				    and p.id not in (select r.partner_id from etourism_booking b,etourism_booking_res_partner_rel r where r.booking_id=b.id and b.enterprise_id<>%s)"""%(enterprise_id.id,enterprise_id.id,enterprise_id.id,enterprise_id.id))
                result = self._cr.fetchall()
                if result:
                    for res in result:
                        guests.append(res[0])

                self._cr.execute("""select r.id from res_partner r where r.company_id=%s and r.customer = 't';"""%(self.env.user.company_id.id))

                result = self._cr.fetchall()
                if result:
                    for res in result:
                        guests.append(res[0])
						
                self._cr.execute("""select r.id from res_partner r, res_users u where u.partner_id=r.id""")

                result = self._cr.fetchall()
                if result:
                    for res in result:
                        guests = [x for x in guests if x != res[0]]

            guests = list(set(guests))
            args.append((('id','in', guests)))
        return super(ResPartner, self).search(args, offset, limit, order, count=count)
