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

from odoo import api, fields, models, _
import urlparse

class EtourismEnterprise(models.Model):
    _inherit = 'etourism.enterprise'

    pricelist_line_avt = fields.One2many('product.pricelist', 'enterprise_id', string="Pricelists")
    package_line = fields.One2many('etourism.avt.package', 'enterprise_id', string="Packages")
    auto_confirm_bookings = fields.Boolean(string='Auto Confirm Bookings?', default=False)

    @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 {}
        #print "ENTERPRISE NAME SEARCH::::::::::::::::::::::::::::::::"
        #print context
        enterprise_id = None
        if context and 'uid' in context:
            user_id = self.env['res.users'].search([('id', '=', context['uid'])], limit=1)
            if user_id and user_id.enterprise_id:
                enterprise_id = user_id.enterprise_id
        if context and 'default_enterprise' in context and self.user_has_groups('etourism_avt.group_etourism_avt_enterprise'):
                args.append(('id','=',user_id.enterprise_id.id))
        if context and 'except_default_enterprise' in context and self.user_has_groups('etourism_avt.group_etourism_avt_enterprise'):
                args.append(('id','!=',user_id.enterprise_id.id))

        return super(EtourismEnterprise, self).name_search(name, args=args, operator=operator, limit=limit)

    @api.model
    def search(self, args, offset=0, limit=None, order=None, count=False):
        context = self._context or {}
        #print "ENTERPRISE SEARCH:::::::::::::::::::::::::::::::: ",args,offset,limit,order,count
        partner_id = False
        #print "CONTEXT: ", context, self.user_has_groups
        if context and 'default_user_id' in context and self.user_has_groups('etourism_avt.group_etourism_avt_enterprise'):
            user_id = self.env['res.users'].search([('id', '=', context['default_user_id'])], limit=1)
            if user_id and user_id.enterprise_id:
                #print "FILTER ENTERPRISE_ID: ", user_id.enterprise_id
                args.append(('id','=',user_id.enterprise_id.id))

        return super(EtourismEnterprise, self).search(args, offset, limit, order, count=count)

    @api.multi
    def get_logo_url(self, model, field, rec_id):
        url = ''
        if model and rec_id:
            base_url = self.env['ir.config_parameter'].get_param('web.base.url')
            url = "%s" % urlparse.urljoin(base_url, 'web/image/%s/%s/%s' % (model, rec_id, field))
        return url


    @api.one
    @api.depends('pricelist_line_avt')
    def _get_pricelist_data_avt(self):
        if not self.pricelist_line_avt:
            return {}
        res = {}
        avg_price = 0.0
        room_price = 0.0
        count = 0
        for enterprise in self:
            if enterprise.pricelist_line_avt:
                for pricelist in enterprise.pricelist_line:
                    for item in pricelist.item_ids:
                        count = count + 1
                        room_price=room_price+item.fixed_price
                avg_price=room_price / count
        self.avg_price_rooms=avg_price
