mirror of
https://github.com/OCA/bank-statement-import.git
synced 2025-01-20 12:37:43 +02:00
[IMP] account_bank_statement_import_paypal
This commit is contained in:
committed by
Pedro M. Baeza
parent
57583b151d
commit
b0e11f3527
@@ -1,2 +1,6 @@
|
||||
from . import account_bank_statement_import_paypal_map
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
from . import account_bank_statement_import_paypal_mapping
|
||||
from . import account_bank_statement_import_paypal_parser
|
||||
from . import account_bank_statement_import
|
||||
from . import account_journal
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
# Copyright 2014-2017 Akretion (http://www.akretion.com).
|
||||
# Copyright 2019 Tecnativa - Vicent Cubells
|
||||
# Copyright 2019 Brainbean Apps (https://brainbeanapps.com)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
import logging
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AccountBankStatementImport(models.TransientModel):
|
||||
_inherit = 'account.bank.statement.import'
|
||||
|
||||
paypal_mapping_id = fields.Many2one(
|
||||
string='PayPal mapping',
|
||||
comodel_name='account.bank.statement.import.paypal.mapping',
|
||||
)
|
||||
|
||||
@api.multi
|
||||
def _parse_file(self, data_file):
|
||||
self.ensure_one()
|
||||
try:
|
||||
Parser = self.env['account.bank.statement.import.paypal.parser']
|
||||
return Parser.parse(
|
||||
self.paypal_mapping_id,
|
||||
data_file,
|
||||
self.filename
|
||||
)
|
||||
except:
|
||||
if self.env.context.get(
|
||||
'account_bank_statement_import_paypal_test'):
|
||||
raise
|
||||
_logger.warning('PayPal parser error', exc_info=True)
|
||||
return super()._parse_file(data_file)
|
||||
@@ -1,110 +0,0 @@
|
||||
# Copyright 2019 Tecnativa - Vicent Cubells
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models, api
|
||||
|
||||
|
||||
class AccountBankStatementImportPaypalMap(models.Model):
|
||||
_name = 'account.bank.statement.import.paypal.map'
|
||||
_description = 'Account Bank Statement Import Paypal Map'
|
||||
|
||||
name = fields.Char(
|
||||
required=True,
|
||||
)
|
||||
map_line_ids = fields.One2many(
|
||||
comodel_name='account.bank.statement.import.paypal.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
|
||||
)
|
||||
|
||||
@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 AccountBankStatementImportPaypalMapLine(models.Model):
|
||||
_name = 'account.bank.statement.import.paypal.map.line'
|
||||
_description = 'Account Bank Statement Import Paypal 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.paypal.map',
|
||||
required=True,
|
||||
ondelete='cascade',
|
||||
)
|
||||
field_to_assign = fields.Selection(
|
||||
selection=[
|
||||
('date', 'Date'),
|
||||
('time', 'Time'),
|
||||
('description', 'Description'),
|
||||
('currency', 'Currency'),
|
||||
('amount', 'Gross'),
|
||||
('commission', 'Fee'),
|
||||
('balance', 'Balance'),
|
||||
('transaction_id', 'Transaction ID'),
|
||||
('email', 'From Email Address'),
|
||||
('partner_name', 'Name'),
|
||||
('bank_name', 'Bank Name'),
|
||||
('bank_account', 'Bank Account'),
|
||||
('invoice_number', 'Invoice ID'),
|
||||
('origin_transaction_id', 'Origin Transaction ID'),
|
||||
],
|
||||
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",
|
||||
)
|
||||
@@ -0,0 +1,130 @@
|
||||
# Copyright 2019 Tecnativa - Vicent Cubells
|
||||
# Copyright 2019 Brainbean Apps (https://brainbeanapps.com)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class AccountBankStatementImportPayPalMapping(models.Model):
|
||||
_name = 'account.bank.statement.import.paypal.mapping'
|
||||
_description = 'Account Bank Statement Import PayPal Mapping'
|
||||
|
||||
name = fields.Char(
|
||||
required=True,
|
||||
)
|
||||
float_thousands_sep = fields.Selection(
|
||||
string='Thousands Separator',
|
||||
selection=[
|
||||
('dot', 'dot (.)'),
|
||||
('comma', 'comma (,)'),
|
||||
('none', 'none'),
|
||||
],
|
||||
default='dot',
|
||||
required=True,
|
||||
)
|
||||
float_decimal_sep = fields.Selection(
|
||||
string='Decimals Separator',
|
||||
selection=[
|
||||
('dot', 'dot (.)'),
|
||||
('comma', 'comma (,)'),
|
||||
('none', 'none'),
|
||||
],
|
||||
default='comma',
|
||||
required=True,
|
||||
)
|
||||
date_format = fields.Char(
|
||||
string='Date Format',
|
||||
required=True,
|
||||
)
|
||||
time_format = fields.Char(
|
||||
string='Time Format',
|
||||
required=True,
|
||||
)
|
||||
date_column = fields.Char(
|
||||
string='"Date" column',
|
||||
required=True,
|
||||
)
|
||||
time_column = fields.Char(
|
||||
string='"Time" column',
|
||||
required=True,
|
||||
)
|
||||
tz_column = fields.Char(
|
||||
string='"Timezone" column',
|
||||
required=True,
|
||||
)
|
||||
name_column = fields.Char(
|
||||
string='"Name" column',
|
||||
required=True,
|
||||
)
|
||||
currency_column = fields.Char(
|
||||
string='"Currency" column',
|
||||
required=True,
|
||||
)
|
||||
gross_column = fields.Char(
|
||||
string='"Gross" column',
|
||||
required=True,
|
||||
)
|
||||
fee_column = fields.Char(
|
||||
string='"Fee" column',
|
||||
required=True,
|
||||
)
|
||||
balance_column = fields.Char(
|
||||
string='"Balance" column',
|
||||
required=True,
|
||||
)
|
||||
transaction_id_column = fields.Char(
|
||||
string='"Transaction ID" column',
|
||||
required=True,
|
||||
)
|
||||
description_column = fields.Char(
|
||||
string='"Description" column',
|
||||
)
|
||||
type_column = fields.Char(
|
||||
string='"Type" column',
|
||||
)
|
||||
from_email_address_column = fields.Char(
|
||||
string='"From Email Address" column',
|
||||
)
|
||||
to_email_address_column = fields.Char(
|
||||
string='"To Email Address" column',
|
||||
)
|
||||
invoice_id_column = fields.Char(
|
||||
string='"Invoice ID" column',
|
||||
)
|
||||
subject_column = fields.Char(
|
||||
string='"Subject" column',
|
||||
)
|
||||
note_column = fields.Char(
|
||||
string='"Note" column',
|
||||
)
|
||||
bank_name_column = fields.Char(
|
||||
string='"Bank Name" column',
|
||||
)
|
||||
bank_account_column = fields.Char(
|
||||
string='"Bank Account" column',
|
||||
)
|
||||
|
||||
@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'
|
||||
|
||||
@api.multi
|
||||
def _get_float_separators(self):
|
||||
self.ensure_one()
|
||||
separators = {
|
||||
'dot': '.',
|
||||
'comma': ',',
|
||||
'none': '',
|
||||
}
|
||||
return (separators[self.float_thousands_sep],
|
||||
separators[self.float_decimal_sep])
|
||||
@@ -0,0 +1,265 @@
|
||||
# Copyright 2019 Tecnativa - Vicent Cubells
|
||||
# Copyright 2019 Brainbean Apps (https://brainbeanapps.com)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import api, models, _
|
||||
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from io import StringIO
|
||||
from os import path
|
||||
import itertools
|
||||
from pytz import timezone, utc
|
||||
|
||||
import logging
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
try:
|
||||
from csv import reader
|
||||
except (ImportError, IOError) as err:
|
||||
_logger.error(err)
|
||||
|
||||
|
||||
class AccountBankStatementImportPayPalParser(models.TransientModel):
|
||||
_name = 'account.bank.statement.import.paypal.parser'
|
||||
_description = 'Account Bank Statement Import PayPal Parser'
|
||||
|
||||
@api.model
|
||||
def parse_header(self, data_file):
|
||||
data = StringIO(data_file.decode('utf-8-sig'))
|
||||
csv_data = reader(data)
|
||||
return list(next(csv_data))
|
||||
|
||||
@api.model
|
||||
def parse(self, mapping, data_file, filename):
|
||||
journal = self.env['account.journal'].browse(
|
||||
self.env.context.get('journal_id')
|
||||
)
|
||||
currency_code = (
|
||||
journal.currency_id or journal.company_id.currency_id
|
||||
).name
|
||||
account_number = journal.bank_account_id.acc_number
|
||||
|
||||
name = _('%s: %s') % (
|
||||
journal.code,
|
||||
path.basename(filename),
|
||||
)
|
||||
lines = self._parse_lines(mapping, data_file, currency_code)
|
||||
if not lines:
|
||||
return currency_code, account_number, [{
|
||||
'name': name,
|
||||
'transactions': [],
|
||||
}]
|
||||
|
||||
lines = list(sorted(
|
||||
lines,
|
||||
key=lambda line: line['timestamp']
|
||||
))
|
||||
first_line = lines[0]
|
||||
balance_start = first_line['balance_amount']
|
||||
balance_start -= first_line['gross_amount']
|
||||
balance_start -= first_line['fee_amount']
|
||||
last_line = lines[-1]
|
||||
balance_end = last_line['balance_amount']
|
||||
|
||||
transactions = list(itertools.chain.from_iterable(map(
|
||||
lambda line: self._convert_line_to_transactions(line),
|
||||
lines
|
||||
)))
|
||||
|
||||
return currency_code, account_number, [{
|
||||
'name': name,
|
||||
'date': first_line['timestamp'].date(),
|
||||
'balance_start': float(balance_start),
|
||||
'balance_end_real': float(balance_end),
|
||||
'transactions': transactions,
|
||||
}]
|
||||
|
||||
def _parse_lines(self, mapping, data_file, currency_code):
|
||||
data = StringIO(data_file.decode('utf-8-sig'))
|
||||
csv_data = reader(data)
|
||||
|
||||
header = list(next(csv_data))
|
||||
date_column = header.index(mapping.date_column)
|
||||
time_column = header.index(mapping.time_column)
|
||||
tz_column = header.index(mapping.tz_column)
|
||||
name_column = header.index(mapping.name_column)
|
||||
currency_column = header.index(mapping.currency_column)
|
||||
gross_column = header.index(mapping.gross_column)
|
||||
fee_column = header.index(mapping.fee_column)
|
||||
balance_column = header.index(mapping.balance_column)
|
||||
transaction_id_column = header.index(mapping.transaction_id_column)
|
||||
try:
|
||||
description_column = header.index(mapping.description_column)
|
||||
except ValueError:
|
||||
description_column = None
|
||||
try:
|
||||
type_column = header.index(mapping.type_column)
|
||||
except ValueError:
|
||||
type_column = None
|
||||
try:
|
||||
from_email_address_column = header.index(
|
||||
mapping.from_email_address_column
|
||||
)
|
||||
except ValueError:
|
||||
from_email_address_column = None
|
||||
try:
|
||||
to_email_address_column = header.index(
|
||||
mapping.to_email_address_column
|
||||
)
|
||||
except ValueError:
|
||||
to_email_address_column = None
|
||||
try:
|
||||
invoice_id_column = header.index(mapping.invoice_id_column)
|
||||
except ValueError:
|
||||
invoice_id_column = None
|
||||
try:
|
||||
subject_column = header.index(mapping.subject_column)
|
||||
except ValueError:
|
||||
subject_column = None
|
||||
try:
|
||||
note_column = header.index(mapping.note_column)
|
||||
except ValueError:
|
||||
note_column = None
|
||||
try:
|
||||
bank_name_column = header.index(mapping.bank_name_column)
|
||||
except ValueError:
|
||||
bank_name_column = None
|
||||
try:
|
||||
bank_account_column = header.index(mapping.bank_account_column)
|
||||
except ValueError:
|
||||
bank_account_column = None
|
||||
|
||||
lines = []
|
||||
for row in csv_data:
|
||||
row = list(row)
|
||||
date_value = row[date_column]
|
||||
time_value = row[time_column]
|
||||
tz_value = row[tz_column]
|
||||
name_value = row[name_column]
|
||||
currency_value = row[currency_column]
|
||||
gross_value = row[gross_column]
|
||||
fee_value = row[fee_column]
|
||||
balance_value = row[balance_column]
|
||||
transaction_id_value = row[transaction_id_column]
|
||||
description_value = row[description_column] \
|
||||
if description_column is not None else None
|
||||
type_value = row[type_column] \
|
||||
if type_column is not None else None
|
||||
from_email_address_value = row[from_email_address_column] \
|
||||
if from_email_address_column is not None else None
|
||||
to_email_address_value = row[to_email_address_column] \
|
||||
if to_email_address_column is not None else None
|
||||
invoice_id_value = row[invoice_id_column] \
|
||||
if invoice_id_column is not None else None
|
||||
subject_value = row[subject_column] \
|
||||
if subject_column is not None else None
|
||||
note_value = row[note_column] \
|
||||
if note_column is not None else None
|
||||
bank_name_value = row[bank_name_column] \
|
||||
if bank_name_column is not None else None
|
||||
bank_account_value = row[bank_account_column] \
|
||||
if bank_account_column is not None else None
|
||||
|
||||
if currency_value != currency_code:
|
||||
continue
|
||||
|
||||
date = datetime.strptime(date_value, mapping.date_format).date()
|
||||
time = datetime.strptime(time_value, mapping.time_format).time()
|
||||
timestamp = datetime.combine(date, time)
|
||||
tz_value = self._normalize_tz(tz_value)
|
||||
tz = timezone(tz_value)
|
||||
timestamp = timestamp.replace(tzinfo=tz)
|
||||
timestamp = timestamp.astimezone(utc).replace(tzinfo=None)
|
||||
gross_amount = self._parse_decimal(gross_value, mapping)
|
||||
fee_amount = self._parse_decimal(fee_value, mapping)
|
||||
balance_amount = self._parse_decimal(balance_value, mapping)
|
||||
bank = '%s - %s' % (
|
||||
bank_name_value,
|
||||
bank_account_value,
|
||||
) if bank_name_value and bank_account_value else None
|
||||
if to_email_address_column is None:
|
||||
payer_email = from_email_address_value
|
||||
else:
|
||||
payer_email = to_email_address_value \
|
||||
if gross_amount < 0.0 else from_email_address_value
|
||||
|
||||
lines.append({
|
||||
'transaction_id': transaction_id_value,
|
||||
'invoice': invoice_id_value,
|
||||
'description': description_value or type_value,
|
||||
'details': subject_value or note_value or bank,
|
||||
'timestamp': timestamp,
|
||||
'gross_amount': gross_amount,
|
||||
'fee_amount': fee_amount,
|
||||
'balance_amount': balance_amount,
|
||||
'payer_name': name_value,
|
||||
'payer_email': payer_email,
|
||||
})
|
||||
return lines
|
||||
|
||||
@api.model
|
||||
def _convert_line_to_transactions(self, line):
|
||||
transactions = []
|
||||
|
||||
transaction_id = line['transaction_id']
|
||||
invoice = line['invoice']
|
||||
description = line['description']
|
||||
details = line['details']
|
||||
timestamp = line['timestamp']
|
||||
gross_amount = line['gross_amount']
|
||||
fee_amount = line['fee_amount']
|
||||
payer_name = line['payer_name']
|
||||
payer_email = line['payer_email']
|
||||
if invoice:
|
||||
invoice = _('Invoice %s') % invoice
|
||||
note = '%s %s' % (
|
||||
description,
|
||||
transaction_id,
|
||||
)
|
||||
if details:
|
||||
note += ': %s' % details
|
||||
if payer_email:
|
||||
note += ' (%s)' % payer_email
|
||||
|
||||
unique_import_id = '%s-%s' % (
|
||||
transaction_id,
|
||||
int(timestamp.timestamp()),
|
||||
)
|
||||
name = invoice or details or description or '',
|
||||
transaction = {
|
||||
'name': invoice or details or description or '',
|
||||
'amount': str(gross_amount),
|
||||
'date': timestamp,
|
||||
'note': note,
|
||||
'unique_import_id': unique_import_id,
|
||||
}
|
||||
if payer_name:
|
||||
line.update({
|
||||
'partner_name': payer_name,
|
||||
})
|
||||
transactions.append(transaction)
|
||||
|
||||
if fee_amount:
|
||||
transactions.append({
|
||||
'name': _('Fee for %s') % (name or transaction_id),
|
||||
'amount': str(fee_amount),
|
||||
'date': timestamp,
|
||||
'partner_name': 'PayPal',
|
||||
'unique_import_id': '%s-FEE' % unique_import_id,
|
||||
'note': _('Transaction fee for %s') % note,
|
||||
})
|
||||
return transactions
|
||||
|
||||
@api.model
|
||||
def _parse_decimal(self, value, mapping):
|
||||
thousands, decimal = mapping._get_float_separators()
|
||||
value = value.replace(thousands, '')
|
||||
value = value.replace(decimal, '.')
|
||||
return Decimal(value)
|
||||
|
||||
@api.model
|
||||
def _normalize_tz(self, value):
|
||||
if value in ['PDT', 'PST']:
|
||||
return 'PST8PDT'
|
||||
return value
|
||||
@@ -1,18 +1,14 @@
|
||||
# Copyright 2019 Tecnativa - Vicent Cubells
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
# Copyright 2019 Brainbean Apps (https://brainbeanapps.com)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
from odoo import models
|
||||
|
||||
|
||||
class AccountJournal(models.Model):
|
||||
_inherit = "account.journal"
|
||||
|
||||
paypal_map_id = fields.Many2one(
|
||||
comodel_name='account.bank.statement.import.paypal.map',
|
||||
string='Paypal Map',
|
||||
)
|
||||
_inherit = 'account.journal'
|
||||
|
||||
def _get_bank_statements_available_import_formats(self):
|
||||
res = super()._get_bank_statements_available_import_formats()
|
||||
res.append('Paypal')
|
||||
res.append('PayPal Reports')
|
||||
return res
|
||||
|
||||
Reference in New Issue
Block a user