Skip to content

Commit

Permalink
Merge 76e8434 into c6753bd
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiBForgeFlow committed Dec 18, 2019
2 parents c6753bd + 76e8434 commit dd595b0
Show file tree
Hide file tree
Showing 22 changed files with 831 additions and 0 deletions.
2 changes: 2 additions & 0 deletions account_bank_statement_import_txt_xlsx/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizards
28 changes: 28 additions & 0 deletions account_bank_statement_import_txt_xlsx/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2014-2017 Akretion (http://www.akretion.com).
# @author Alexis de Lattre <alexis.delattre@akretion.com>
# @author Sébastien BEAU <sebastien.beau@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Account Bank Statement Import TXT XLSX",
'summary': 'Import TXT/CSV or XLSX files as Bank Statements in Odoo',
"version": "12.0.1.0.1",
"category": "Accounting",
"website": "https://github.com/OCA/bank-statement-import",
"author": " Eficent, Odoo Community Association (OCA)",
"license": "AGPL-3",
"installable": True,
"depends": [
"account_bank_statement_import",
],
"external_dependencies": {
"python": ["xlrd"],
},
"data": [
"security/ir.model.access.csv",
"data/txt_map_data.xml",
"wizards/create_map_lines_from_file_views.xml",
"wizards/account_bank_statement_import_view.xml",
"views/account_journal_views.xml",
"views/txt_map_views.xml",
]
}
53 changes: 53 additions & 0 deletions account_bank_statement_import_txt_xlsx/data/txt_map_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo noupdate="1">

<record id="txt_map" model="account.bank.statement.import.map">
<field name="name">Sample Statement</field>
<field name="float_thousands_sep">comma</field>
<field name="float_decimal_sep">dot</field>
</record>

<record id="txt_map_line_date" model="account.bank.statement.import.map.line">
<field name="name">Date</field>
<field name="sequence">0</field>
<field name="map_parent_id" ref="txt_map"/>
<field name="field_to_assign">date</field>
<field name="date_format">%m/%d/%Y</field>
</record>
<record id="txt_map_line_name" model="account.bank.statement.import.map.line">
<field name="name">Label</field>
<field name="sequence">1</field>
<field name="map_parent_id" ref="txt_map"/>
<field name="field_to_assign">name</field>
</record>
<record id="txt_map_line_currency" model="account.bank.statement.import.map.line">
<field name="name">Currency</field>
<field name="sequence">2</field>
<field name="map_parent_id" ref="txt_map"/>
<field name="field_to_assign">currency</field>
</record>
<record id="txt_map_line_amount" model="account.bank.statement.import.map.line">
<field name="name">Amount</field>
<field name="sequence">3</field>
<field name="map_parent_id" ref="txt_map"/>
<field name="field_to_assign">amount</field>
</record>
<record id="txt_map_line_amount_currency" model="account.bank.statement.import.map.line">
<field name="name">Amount Currency</field>
<field name="sequence">4</field>
<field name="map_parent_id" ref="txt_map"/>
<field name="field_to_assign">amount_currency</field>
</record>
<record id="txt_map_line_partner_name" model="account.bank.statement.import.map.line">
<field name="name">Partner Name</field>
<field name="sequence">5</field>
<field name="map_parent_id" ref="txt_map"/>
<field name="field_to_assign">partner_name</field>
</record>
<record id="txt_map_line_account_number" model="account.bank.statement.import.map.line">
<field name="name">Bank Account</field>
<field name="sequence">6</field>
<field name="map_parent_id" ref="txt_map"/>
<field name="field_to_assign">account_number</field>
</record>
</odoo>
2 changes: 2 additions & 0 deletions account_bank_statement_import_txt_xlsx/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import account_bank_statement_import_txt_map
from . import account_journal
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Copyright 2019 Tecnativa - Vicent Cubells
# Copyright 2019 Eficent Business and IT Consulting Services, S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models, api


class AccountBankStatementImportTxtMap(models.Model):
_name = 'account.bank.statement.import.map'
_description = 'Account Bank Statement Import Txt Map'

name = fields.Char(
required=True,
)
map_line_ids = fields.One2many(
comodel_name='account.bank.statement.import.map.line',
inverse_name='map_parent_id',
string="Map lines",
required=True,
copy=True,
)
float_thousands_sep = fields.Selection(
[('dot', 'dot (.)'),
('comma', 'comma (,)'),
('none', 'none'),
],
string='Thousands separator',
# forward compatibility: this was the value assumed
# before the field was added.
default='dot',
required=True
)
float_decimal_sep = fields.Selection(
[('dot', 'dot (.)'),
('comma', 'comma (,)'),
('none', 'none'),
],
string='Decimals separator',
# forward compatibility: this was the value assumed
# before the field was added.
default='comma',
required=True
)
file_encoding = fields.Selection(
string='Encoding',
selection=[
('utf-8', 'UTF-8'),
('utf-16 ', 'UTF-16'),
('windows-1252', 'Windows-1252'),
('latin1', 'latin1'),
('latin2', 'latin2'),
('big5', 'big5'),
('gb18030', 'gb18030'),
('shift_jis', 'shift_jis'),
('windows-1251', 'windows-1251'),
('koir8_r', 'koir9_r'),
],
default='utf-8',
)
delimiter = fields.Selection(
string='Separated by',
selection=[
('.', 'dot (.)'),
(',', 'comma (,)'),
(';', 'semicolon (;)'),
('', 'none'),
('\t', 'Tab'),
(' ', 'Space'),
],
default=',',
)
quotechar = fields.Char(string='String delimiter', size=1,
default='"')

@api.onchange('float_thousands_sep')
def onchange_thousands_separator(self):
if 'dot' == self.float_thousands_sep == self.float_decimal_sep:
self.float_decimal_sep = 'comma'
elif 'comma' == self.float_thousands_sep == self.float_decimal_sep:
self.float_decimal_sep = 'dot'

@api.onchange('float_decimal_sep')
def onchange_decimal_separator(self):
if 'dot' == self.float_thousands_sep == self.float_decimal_sep:
self.float_thousands_sep = 'comma'
elif 'comma' == self.float_thousands_sep == self.float_decimal_sep:
self.float_thousands_sep = 'dot'

def _get_separators(self):
separators = {'dot': '.',
'comma': ',',
'none': '',
}
return (separators[self.float_thousands_sep],
separators[self.float_decimal_sep])


class AccountBankStatementImportTxtMapLine(models.Model):
_name = 'account.bank.statement.import.map.line'
_description = 'Account Bank Statement Import Txt Map Line'
_order = "sequence asc, id asc"

sequence = fields.Integer(
string="Field number",
required=True,
)
name = fields.Char(
string="Header Name",
required=True,
)
map_parent_id = fields.Many2one(
comodel_name='account.bank.statement.import.map',
required=True,
ondelete='cascade',
)
field_to_assign = fields.Selection(
selection=[
('date', 'Date'),
('name', 'Label'),
('currency', 'Currency'),
('amount', 'Amount in the journal currency'),
('amount_currency', 'Amount in foreign currency'),
('ref', 'Reference'),
('note', 'Notes'),
('partner_name', 'Name'),
('account_number', 'Bank Account Number'),
],
string="Statement Field to Assign",
)
date_format = fields.Selection(
selection=[
('%d/%m/%Y', 'i.e. 15/12/2019'),
('%m/%d/%Y', 'i.e. 12/15/2019'),
],
string="Date Format",
)
19 changes: 19 additions & 0 deletions account_bank_statement_import_txt_xlsx/models/account_journal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2019 Tecnativa - Vicent Cubells
# Copyright 2019 Eficent Business and IT Consulting Services, S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class AccountJournal(models.Model):
_inherit = "account.journal"

statement_import_txt_map_id = fields.Many2one(
comodel_name='account.bank.statement.import.map',
string='Statement Import Txt Map',
)

def _get_bank_statements_available_import_formats(self):
res = super()._get_bank_statements_available_import_formats()
res.append('Txt')
return res
12 changes: 12 additions & 0 deletions account_bank_statement_import_txt_xlsx/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
* Create or go to a bank journal where you want to import the txt statement.
* Edit that journal and set a Txt map in **Statement Import Map** section in **Advanced
Settings** tab.

* Now you can import Text based statements in that journal.

Note: if existent Txt Map does not fit to your file to import, you can
create another map in **Invoicing > Configuration > Accounting >
Statement Import Map**.

You can import headers from any Txt file in **Action > Create Map
Lines** and set every line with which field of statement have to match.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* Alexis de Lattre <alexis.delattre@akretion.com>
* Sebastien BEAU <sebastien.beau@akretion.com>
* Tecnativa (https://www.tecnativa.com)
* Vicent Cubells <vicent.cubells@tecnativa.com>
* Victor M.M. Torres <victor.martin@tecnativa.com>
* Eficent (https://www.eficent.com)
* Jordi Ballester Alomar <jordi.ballester@eficent.com>
2 changes: 2 additions & 0 deletions account_bank_statement_import_txt_xlsx/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module allows you to import the any TXT/CSV or XLSX file in Odoo as bank
statements.
3 changes: 3 additions & 0 deletions account_bank_statement_import_txt_xlsx/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
To use this module, you need to:

#. Go to your bank online and download your Bank Statement in TXT/CSV or XLSX format.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
access_account_bank_statement_import_map,map manager,model_account_bank_statement_import_map,account.group_account_manager,1,1,1,1
access_account_bank_statement_import_map_line,map line manager,model_account_bank_statement_import_map_line,account.group_account_manager,1,1,1,1
1 change: 1 addition & 0 deletions account_bank_statement_import_txt_xlsx/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_txt_statement_import
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"Date","Label","Currency","Amount","Amount Currency","Partner Name","Bank Account"
"12/15/2018","Your best supplier","USD","-33.50","0.0","John Doe","123456789"
"12/15/2018","Your payment","EUR","1,525.00","1,000.00","Azure Interior",""
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright 2019 Tecnativa - Vicent Cubells
# Copyright 2019 Eficent Business and IT Consulting Services, S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import os
from io import StringIO
import base64
from odoo.tests import common


class TestTxtFile(common.TransactionCase):

def setUp(self):
super(TestTxtFile, self).setUp()

self.map = self.env['account.bank.statement.import.map'].create({
'name': 'Txt Map Test',
})
usd = self.env.ref('base.USD')
self.journal = self.env['account.journal'].create({
'name': 'Txt Bank',
'type': 'bank',
'code': 'TXT',
'currency_id': (
usd.id if self.env.user.company_id.currency_id != usd
else False
),
})

def _do_import_xlsx(self, file_name ):
file_name = os.path.join(os.path.dirname(__file__), file_name)
with open(file_name, 'rb') as fin:
data = fin.read()
return data

def _do_import(self, file_name ):
file_name = os.path.join(os.path.dirname(__file__), file_name)
return open(file_name).read()

def test_import_header(self):
file = self._do_import('sample_statement_en.csv')
file = base64.b64encode(file.encode("utf-8"))
wizard = self.env['wizard.txt.map.create'].with_context({
'journal_id': self.journal.id,
'active_ids': [self.map.id],
}).create({'data_file': file})
wizard.create_map_lines()
self.assertEqual(len(self.map.map_line_ids.ids), 7)

def test_import_txt_file(self):
# Current statements before to run the wizard
old_statements = self.env['account.bank.statement'].search([])
# This journal is for Txt statements
txt_map = self.env.ref(
'account_bank_statement_import_txt_xlsx.txt_map'
)
self.journal.statement_import_txt_map_id = txt_map.id
file = self._do_import('sample_statement_en.csv')
file = base64.b64encode(file.encode("utf-8"))
wizard = self.env['account.bank.statement.import'].with_context({
'journal_id': self.journal.id,
}).create({'data_file': file})
wizard.import_file()
staments_now = self.env['account.bank.statement'].search([])
statement = staments_now - old_statements
self.assertEqual(len(statement.line_ids), 2)
self.assertEqual(len(statement.mapped('line_ids').filtered(
lambda x: x.partner_id)), 1)
self.assertAlmostEqual(
sum(statement.mapped('line_ids.amount')), 1491.50
)
self.assertAlmostEqual(
sum(statement.mapped('line_ids.amount_currency')), 1000.00
)

def test_import_xlsx_file(self):
# Current statements before to run the wizard
old_statements = self.env['account.bank.statement'].search([])
# This journal is for Txt statements
txt_map = self.env.ref(
'account_bank_statement_import_txt_xlsx.txt_map'
)
self.journal.statement_import_txt_map_id = txt_map.id
file = self._do_import_xlsx('sample_statement_en.xlsx')
file = base64.b64encode(file)
wizard = self.env['account.bank.statement.import'].with_context({
'journal_id': self.journal.id,
}).create({'data_file': file})
wizard.import_file()
staments_now = self.env['account.bank.statement'].search([])
statement = staments_now - old_statements
self.assertEqual(len(statement.line_ids), 2)
self.assertEqual(len(statement.mapped('line_ids').filtered(
lambda x: x.partner_id)), 1)
self.assertAlmostEqual(
sum(statement.mapped('line_ids.amount')), 1491.50
)
self.assertAlmostEqual(
sum(statement.mapped('line_ids.amount_currency')), 1000.00
)
Loading

0 comments on commit dd595b0

Please sign in to comment.