# -*- 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, tools, SUPERUSER_ID, _
from odoo.tools.misc import formatLang
import json

import urlparse
import uuid

from datetime import date

class MassMailing(models.Model):
    _inherit = ['mail.mass_mailing']

    company_id = fields.Many2one('res.company', related="create_uid.company_id", string='Company', store=True)
	
    @api.model
    def search(self, args, offset=0, limit=None, order=None, count=False):
        """ This function searches for booking lines
        """
        context = self._context or {}
        if self._uid == SUPERUSER_ID:
            pass
        else:
            mailings = []
            company_id = self.env['res.users'].browse([self.env.uid])[0].company_id
            self._cr.execute("""select id from mail_mass_mailing where company_id=%s"""%(company_id.id))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    mailings.append(res[0])

                mailings = list(set(mailings))

                args.append((('id','in', mailings)))
        return super(MassMailing, self).search(args, offset, limit, order, count=count)

