-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.py
157 lines (133 loc) · 5.34 KB
/
statement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
import csv
from io import StringIO
from trytond.model import ModelSQL, fields
from trytond.pool import Pool, PoolMeta
from trytond.modules.company.model import CompanyValueMixin
from trytond.pyson import Eval, Bool, Not
from trytond.i18n import gettext
from trytond.exceptions import UserError
__all__ = ['Configuration', 'ConfigurationPaypal', 'Import',
'ImportStart']
locales = {
'en': {
'date_field': 'Date',
'description_field': 'Transaction ID',
'gross_field': 'Gross',
'net_field': 'Net',
'fee_field': 'Fee',
'date_pattern': '%m/%d/%Y',
'decimal_separator': '.',
'thousands_separator': ',',
},
'es': {
'date_field': 'Fecha',
'description_field': 'Id. de transacción',
'gross_field': 'Bruto',
'net_field': 'Neto',
'fee_field': 'Tarifa',
'date_pattern': '%d/%m/%Y',
'decimal_separator': ',',
'thousands_separator': '.',
}}
paypal_amount_field = fields.Selection([
('gross_field', 'Gross'),
('net_field', 'Net'),
], 'Field to import amount')
paypal_fee = fields.Boolean('Paypal Fee',
help="Import Paypal Fee.")
paypal_fee_account = fields.Many2One('account.account', 'Paypal Fee Account',
domain=[('type.expense', '=', True)],
states={
'required': Bool(Eval('paypal_fee')),
'invisible': Not(Bool(Eval('paypal_fee'))),
})
class Configuration(metaclass=PoolMeta):
__name__ = 'account.configuration'
paypal_amount_field = fields.MultiValue(paypal_amount_field)
paypal_fee = fields.MultiValue(paypal_fee)
paypal_fee_account = fields.MultiValue(paypal_fee_account)
@classmethod
def multivalue_model(cls, field):
pool = Pool()
if field in ('paypal_amount_field', 'paypal_fee', 'paypal_fee_account'):
return pool.get('account.configuration.paypal')
return super(Configuration, cls).multivalue_model(field)
@classmethod
def default_paypal_amount_field(cls, **pattern):
return cls.multivalue_model(
'paypal_amount_field').default_paypal_amount_field()
@classmethod
def default_paypal_fee(cls, **pattern):
return cls.multivalue_model('paypal_fee').default_paypal_fee()
class ConfigurationPaypal(ModelSQL, CompanyValueMixin, metaclass=PoolMeta):
"Bank Statement Paypal Configuration"
__name__ = 'account.configuration.paypal'
paypal_amount_field = paypal_amount_field
paypal_fee = paypal_fee
paypal_fee_account = paypal_fee_account
@classmethod
def default_paypal_amount_field(cls):
return 'net_field'
@classmethod
def default_paypal_fee(cls):
return False
class ImportStart(metaclass=PoolMeta):
__name__ = 'account.bank.statement.import.start'
@classmethod
def __setup__(cls):
super(ImportStart, cls).__setup__()
cls.type.selection += [
('paypal-en', 'Paypal EN'), ('paypal-es', 'Paypal ES')]
class Import(metaclass=PoolMeta):
__name__ = 'account.bank.statement.import'
def process(self, statement):
super(Import, self).process(statement)
if self.start.type not in ['paypal-en', 'paypal-es']:
return
pool = Pool()
Line = pool.get('account.bank.statement.line')
MoveLine = pool.get('account.bank.statement.move.line')
Config = pool.get('account.configuration')
config = Config(1)
csv_file = StringIO(self.start.import_file.decode('utf-8-sig'))
try:
reader = csv.DictReader(csv_file)
except csv.Error as e:
raise UserError(gettext('account_bank_statement.format_error',
error=str(e)))
locale = self.start.type.split('-')[1]
lines = []
for record in reader:
# Strip keys
record = dict((k.strip(), x) for k, x in record.items() if k)
line = Line()
line.statement = statement
line.date = self.string_to_date(
record[locales[locale]['date_field']],
patterns=(locales[locale]['date_pattern'],),
)
line.description = record[locales[locale]['description_field']]
line.amount = self.string_to_number(
record[locales[locale][config.paypal_amount_field]],
decimal_separator=locales[locale]['decimal_separator'],
thousands_separator=locales[locale]['thousands_separator'],
)
fee = self.string_to_number(
record[locales[locale]['fee_field']],
decimal_separator=locales[locale]['decimal_separator'],
thousands_separator=locales[locale]['thousands_separator'],
)
if fee and config.paypal_fee:
if not config.paypal_fee_account:
raise UserError(gettext(
'account_bank_statement_paypal.no_paypal_fee_account'))
move_line = MoveLine()
move_line.date = line.date.date()
move_line.amount = fee
move_line.account = config.paypal_fee_account
line.lines = [move_line]
lines.append(line)
Line.save(lines)