# -*- coding: utf-8 -*-
##############################################################################
#
#    Author: Nicolas Bessi, Joel Grand-Guillaume
#    Copyright 2011-2012 Camptocamp SA
#
#    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 <http://www.gnu.org/licenses/>.
#
##############################################################################

"""
Wizard to import financial institute date in bank statement
"""

from osv import fields, osv
from tools.translate import _
import os

class CreditPartnerStatementImporter(osv.osv_memory):
    _name = "credit.statement.import"
    
    def default_get(self, cr, uid, fields, context=None):
        if context is None: context = {}
        res = {}
        if (context.get('active_model', False) == 'account.statement.profile' and
            context.get('active_ids', False)):
            ids = context['active_ids']
            assert len(ids) == 1, 'You cannot use this on more than one profile !'
            res['profile_id'] = ids[0]
            other_vals = self.onchange_profile_id(cr, uid, [], res['profile_id'], context=context)
            res.update(other_vals.get('value',{}))
        return res
    
    _columns = {
        'profile_id': fields.many2one('account.statement.profile',
                                      'Import configuration parameter',
                                      required=True),
        'input_statement': fields.binary('Statement file', required=True),
        'partner_id': fields.many2one('res.partner',
                                      'Credit insitute partner',
                                      ),
        'journal_id': fields.many2one('account.journal',
                                      'Financial journal to use transaction',
                                      ),
        'input_statement': fields.binary('Statement file', required=True),
        'file_name': fields.char('File Name', size=128),
        'commission_account_id': fields.many2one('account.account',
                                                         'Commission account',
                                                         ),
        'commission_analytic_id': fields.many2one('account.analytic.account',
                                                     'Commission analytic account',
                                                 ),
        'receivable_account_id': fields.many2one('account.account',
                                                 'Force Receivable/Payable Account'),
        'force_partner_on_bank': fields.boolean('Force partner on bank move', 
                                                    help="Tic that box if you want to use the credit insitute partner\
                                                    in the counterpart of the treasury/banking move."
                                                    ),
        'balance_check': fields.boolean('Balance check', 
                                                    help="Tic that box if you want OpenERP to control the start/end balance\
                                                    before confirming a bank statement. If don't ticked, no balance control will be done."
                                                    ),
    }   
    
    def onchange_profile_id(self, cr, uid, ids, profile_id, context=None):
        res={}
        if profile_id:
            c = self.pool.get("account.statement.profile").browse(cr,uid,profile_id)
            res = {'value': {'partner_id': c.partner_id and c.partner_id.id or False,
                    'journal_id': c.journal_id and c.journal_id.id or False, 'commission_account_id': \
                    c.commission_account_id and c.commission_account_id.id or False, 
                    'receivable_account_id': c.receivable_account_id and c.receivable_account_id.id or False,
                    'commission_a':c.commission_analytic_id and c.commission_analytic_id.id or False,
                    'force_partner_on_bank':c.force_partner_on_bank,
                    'balance_check':c.balance_check,}}
        return res

    def _check_extension(self, filename):
        (shortname, ftype) = os.path.splitext(filename)
        if not ftype:
            #We do not use osv exception we do not want to have it logged
            raise Exception(_('Please use a file with an extention'))
        return ftype

    def import_statement(self, cursor, uid, req_id, context=None):
        """This Function import credit card agency statement"""
        context = context or {}
        if isinstance(req_id, list):
            req_id = req_id[0]
        importer = self.browse(cursor, uid, req_id, context)
        ftype = self._check_extension(importer.file_name)
        sid = self.pool.get(
                'account.statement.profile').statement_import(
                                            cursor,
                                            uid,
                                            False,
                                            importer.profile_id.id,
                                            importer.input_statement,
                                            ftype.replace('.',''),
                                            context=context
                                        )
        return {
            'domain': "[('id','in', ["+','.join(map(str,[sid]))+"])]",
            'name': 'Imported Bank Statement',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'account.bank.statement',
            'type': 'ir.actions.act_window',
        }

@@ -5873,245 +5873,199 @@
     
-return %7B%0A            'domain': %22%5B('id','in', %5B%22+','.join(map(str,%5Bsid%5D))+%22%5D)%5D%22,%0A            'name': 'Imported Bank Statement',%0A            'view_type': 'form',%0A            'view_mode': 'tree,form',%0A            'res_model': 'account.
+model_obj = self.pool.get('ir.model.data')%0A        action_obj = self.pool.get('ir.actions.act_window')%0A        action_id = model_obj.get_object_reference(cursor, uid, 'account', 'action_
 bank
-.
+_
 stat
@@ -6073,62 +6073,159 @@
 ment
-',%0A            'type': 'ir.actions.act_window',%0A        %7D
+_tree')%5B1%5D%0A        res = action_obj.read(cursor, uid, action_id)%0A        res%5B'domain'%5D = res%5B'domain'%5D%5B:-1%5D + %22,('id', '=', %25d)%5D%22 %25 sid%0A        return res
 %0A

fix the redirection in the bank statement import wizard