-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpayment.py
357 lines (311 loc) · 11.3 KB
/
payment.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# -*- coding: utf-8 -*-
"""
payment
:copyright: (c) 2014 by Openlabs Technologies & Consulting (P) Limited
:license: BSD, see LICENSE for more details.
"""
from decimal import Decimal
from trytond.model import ModelSQL, ModelView, fields
from trytond.pool import PoolMeta, Pool
from trytond.pyson import Eval, Not, If
from trytond.transaction import Transaction
from trytond import backend
from sql.functions import Trim, Substring
from sql.operators import Concat
__all__ = ['Payment']
__metaclass__ = PoolMeta
class Payment(ModelSQL, ModelView):
'Payment'
__name__ = 'sale.payment'
sequence = fields.Integer('Sequence', required=True, select=True)
party = fields.Function(
fields.Many2One('party.party', 'Party'),
getter='on_change_with_party'
)
amount = fields.Numeric(
'Amount', required=True, digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits'],
)
sale = fields.Many2One(
'sale.sale', 'Sale', required=True, select=True, ondelete='CASCADE'
)
gateway = fields.Many2One(
'payment_gateway.gateway', 'Gateway', required=True,
ondelete='RESTRICT', select=True,
)
payment_transactions = fields.One2Many(
'payment_gateway.transaction', 'sale_payment', 'Payment Transactions',
readonly=True
)
currency_digits = fields.Function(
fields.Integer('Currency Digits'),
getter='on_change_with_currency_digits'
)
amount_consumed = fields.Function(
fields.Numeric(
'Amount Consumed', digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits'],
), 'get_amount'
)
amount_available = fields.Function(
fields.Numeric(
'Amount Remaining', digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits'],
), 'get_amount'
)
amount_authorized = fields.Function(
fields.Numeric(
'Amount Authorized', digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits'],
), 'get_amount'
)
amount_captured = fields.Function(
fields.Numeric(
'Amount Captured', digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits'],
), 'get_amount'
)
payment_profile = fields.Many2One(
'party.payment_profile', 'Payment Profile',
domain=[
('party', '=', Eval('party')),
('gateway', '=', Eval('gateway')),
],
states={
'required': Eval('method') == 'credit_card'
},
ondelete='RESTRICT', depends=['party', 'gateway', 'method'],
)
method = fields.Function(fields.Char('Payment Method'), 'get_method')
provider = fields.Function(fields.Char('Payment Provider'), 'get_provider')
reference = fields.Char(
'Reference', states={
'invisible': Not(Eval('method') == 'manual'),
}
)
description = fields.Function(
fields.Char('Description'), 'get_payment_description'
)
company = fields.Function(
fields.Many2One(
'company.company', 'Company',
domain=[
('id', If(Eval('context', {}).contains('company'), '=', '!='),
Eval('context', {}).get('company', -1)),
],
),
getter='on_change_with_company',
searcher='search_company',
)
credit_account = fields.Many2One(
'account.account', 'Credit Account', required=True
)
def get_rec_name(self, name):
if self.payment_profile:
return "%s - %s - %s" % (
self.gateway, self.payment_profile, self.amount
)
return "%s - %s" % (self.gateway, self.amount)
def get_provider(self, name=None):
"""
Return the gateway provider based on the gateway
"""
return self.gateway and self.gateway.provider or None
def get_method(self, name=None):
"""
Return the method based on the gateway
"""
return self.gateway and self.gateway.method or None
@classmethod
def __register__(cls, module_name):
Party = Pool().get('party.party')
Sale = Pool().get('sale.sale')
Model = Pool().get('ir.model')
ModelField = Pool().get('ir.model.field')
Property = Pool().get('ir.property')
TableHandler = backend.get('TableHandler')
cursor = Transaction().cursor
table = TableHandler(cursor, cls, module_name)
migration_needed = False
if not table.column_exist('credit_account'):
migration_needed = True
super(Payment, cls).__register__(module_name)
if migration_needed and not Pool.test:
# Migration
# Set party's receivable account as the credit_account on
# sale payments
payment = cls.__table__()
party = Party.__table__()
property = Property.__table__()
sale = Sale.__table__()
account_model, = Model.search([
('model', '=', 'party.party'),
])
account_receivable_field, = ModelField.search([
('model', '=', account_model.id),
('name', '=', 'account_receivable'),
('ttype', '=', 'many2one'),
])
update = payment.update(
columns=[payment.credit_account],
values=[
Trim(
Substring(property.value, ',.*'), 'LEADING', ','
).cast(cls.credit_account.sql_type().base)
],
from_=[sale, party, property],
where=(
payment.sale == sale.id
) & (
sale.party == party.id
) & (
property.res == Concat(Party.__name__ + ',', party.id)
) & (
property.field == account_receivable_field.id
) & (
property.company == sale.company
)
)
cursor.execute(*update)
@classmethod
def __setup__(cls):
super(Payment, cls).__setup__()
cls._error_messages.update({
'cannot_delete_payment':
"Payment cannot be deleted as placeholder for amount consumed "
"has already been consumed.",
})
cls.credit_account.domain = [
('company', '=', Eval('company', -1)),
('kind', 'in', cls._credit_account_domain())
]
cls.credit_account.depends = ['company']
@fields.depends('sale')
def on_change_with_credit_account(self, name=None):
if self.sale and self.sale.party:
return self.sale.party.account_receivable and \
self.sale.party.account_receivable.id
@fields.depends('sale')
def on_change_with_company(self, name=None):
return self.sale and self.sale.company.id or None
@fields.depends('sale')
def on_change_with_party(self, name=None):
return self.sale and self.sale.party.id or None
@fields.depends('sale')
def on_change_with_currency_digits(self, name=None):
if self.sale.currency:
return self.sale.currency.digits
return 2
@classmethod
def search_company(cls, name, clause):
"""
Searcher for field delivery_date
"""
return [('sale.company', ) + tuple(clause[1:])]
def get_amount(self, name):
"""Getter method for fetching amounts.
"""
PaymentTransaction = Pool().get('payment_gateway.transaction')
payment_transactions = PaymentTransaction.search([
('sale_payment', '=', self.id)
])
sum_transactions = lambda txns: sum((txn.amount for txn in txns))
if name == "amount_consumed":
return sum_transactions(filter(
lambda t: t.state in ('authorized', 'completed', 'posted'),
payment_transactions
))
elif name == "amount_authorized":
return sum_transactions(filter(
lambda t: t.state == 'authorized',
payment_transactions
))
elif name == "amount_captured":
return sum_transactions(filter(
lambda t: t.state in ('completed', 'posted'),
payment_transactions
))
elif name == "amount_available":
return max(self.amount - self.amount_consumed, Decimal('0'))
@staticmethod
def default_sequence():
return 10
@classmethod
def cancel(cls, payments):
"""
Cancel all payment transactions related to payment
"""
PaymentTransaction = Pool().get('payment_gateway.transaction')
payment_transactions = []
for payment in payments:
payment_transactions.extend(payment.payment_transactions)
PaymentTransaction.cancel(payment_transactions)
def _create_payment_transaction(self, amount, description):
"""Creates an active record for gateway transaction.
"""
PaymentTransaction = Pool().get('payment_gateway.transaction')
Date = Pool().get('ir.date')
return PaymentTransaction(
description=description or 'Auto charge from sale',
date=Date.today(),
party=self.sale.party,
credit_account=self.credit_account,
payment_profile=self.payment_profile,
address=(
self.payment_profile and
self.payment_profile.address or self.sale.invoice_address
),
amount=self.sale.currency.round(amount),
currency=self.sale.currency,
gateway=self.gateway,
sale_payment=self.id,
provider_reference=self.reference,
)
def authorize(self):
"""Authorize all the transactions associated with the payment.
"""
PaymentTransaction = Pool().get('payment_gateway.transaction')
PaymentTransaction.authorize(
filter(
lambda txn: txn.state == 'draft',
self.payment_transactions
)
)
def capture(self):
"""
Captures the given amount from this transaction
"""
PaymentTransaction = Pool().get('payment_gateway.transaction')
PaymentTransaction.capture(
filter(
lambda txn: txn.state in ('draft', 'authorized', 'in-progress'),
self.payment_transactions
)
)
@classmethod
def delete(cls, payments):
"""
Delete a payment only if there is no amount consumed
"""
for payment in payments:
if payment.amount_consumed:
cls.raise_user_error("cannot_delete_payment")
super(Payment, cls).delete(payments)
def get_payment_description(self, name):
"""
Return a short description of the sale payment
This can be used in documents to show payment details
"""
if self.method == 'manual':
return "Paid by Cash"
elif self.method == 'credit_card':
return (
"Paid by Card " + "(" + ("xxxx " * 3) +
self.payment_profile.last_4_digits + ")"
)
@classmethod
def _credit_account_domain(cls):
"""
Return a list of account kind
"""
return ['receivable']