# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 20014-2016 datenpol gmbh ().
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see .
#
##############################################################################
from openerp.osv import fields, osv
import openerp.modules as addons
import os
import base64
class res_company(osv.osv):
    _inherit = 'res.company'
    def get_image(self, img):
        def _get_dir():
            styles_dir = 'cam_reports/static/src/img'
            adps = addons.module.ad_paths
            for adp in adps:
                dir = os.path.join(adp, styles_dir)
                if os.path.isdir(dir):
                    return dir
            return False
        def _get_file_data(filename):
            try:
                f = open(filename , 'rb')
                data = base64.b64encode(f.read())
                f.close()
                return data
            except:
                print "Error openening file '%s'." % (filename)
                return False
        path = _get_dir()
        fname = os.path.join(path, img)
        img_data = _get_file_data(fname)
        return img_data
    def _get_rml_header(self, cr, uid, ids, _field_name, _args, context=None):
        result = dict.fromkeys(ids, False)
        # Get filename
        dirname = 'cam_reports/report'
        adps = addons.module.ad_paths
        for adp in adps:
            dir = os.path.join(adp, dirname)
            if os.path.isdir(dir):
                break
        if os.path.isdir(dir):
            filename = os.path.join(dir, 'briefkopf.rml')
            f = open(filename , 'rb')
            data = f.read()
            result = dict.fromkeys(ids, data)
        return result
    def _get_rml_header2(self, cr, uid, ids, _field_name, _args, context=None):
        result = dict.fromkeys(ids, False)
        # Get filename
        dirname = 'cam_reports/report'
        adps = addons.module.ad_paths
        for adp in adps:
            dir = os.path.join(adp, dirname)
            if os.path.isdir(dir):
                break
        if os.path.isdir(dir):
            filename = os.path.join(dir, 'page.rml')
            f = open(filename , 'rb')
            data = f.read()
            result = dict.fromkeys(ids, data)
        return result
    _columns = {
        'rml_header':  fields.function(_get_rml_header,  type='text', string='Briefkopf', readonly=True),
        'rml_header2': fields.function(_get_rml_header2, type='text', string='RML Header', readonly=True),
    }