# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import uuid

from odoo import api, fields, models, _
from odoo.exceptions import UserError


class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    @api.model
    def create(self, vals):
        """Set Enterprise id for hotels
        """
        context = self._context or {}
        company_id = context.get('company_id', False)
        user_id = False
        print "CREATE INVOICE POS:::::::::::::::::::::::"
        print "COMPANY:::::::::::::::::::::::",company_id
        print context
        print self.env.uid
        if company_id:
            self._cr.execute("""select v.* from (select u.id
,(select count(*) from res_company_users_rel r where r.user_id=u.id) as cont
from res_users u where u.company_id=%s) v 
where v.cont = 1 limit 1"""%(company_id))
            result = self._cr.fetchone()
            if result:
                user_id = result[0]
            print "USER ID:::::::::::::::::::::::",user_id

            if user_id:
                user_rec = self.env['res.users'].browse(user_id)
                if user_rec:
                    if user_rec.enterprise_id:
                        enterprise_id = user_rec.enterprise_id.id
                        print "ENTERPRISE ID:::::::::::::::::::::::",enterprise_id
                        vals['enterprise_id'] = enterprise_id
                        print "ENTERPRISE ID VALS:::::::::::::::::::::::",vals['enterprise_id']
        return super(AccountInvoice, self).create(vals)
