This repository was archived by the owner on Jun 1, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathinvoice.py
More file actions
306 lines (265 loc) · 10 KB
/
Copy pathinvoice.py
File metadata and controls
306 lines (265 loc) · 10 KB
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
# -*- coding: utf-8 -*-
"""
invoice
:copyright: (c) 2014-2015 by Openlabs Technologies & Consulting (P) Limited
:license: BSD, see LICENSE for more details.
"""
from trytond.pool import PoolMeta, Pool
from trytond.exceptions import UserError
from trytond.model import fields, ModelView
from trytond.pyson import Eval, Bool, And, Or, Id, Not, If
from trytond.transaction import Transaction
from trytond.wizard import Wizard, StateView, StateTransition, Button
from trytond.modules.payment_gateway.transaction import BaseCreditCardViewMixin
__all__ = [
'Invoice', 'PayInvoiceUsingTransactionStart', 'PayInvoiceUsingTransaction',
'PaymentTransaction'
]
__metaclass__ = PoolMeta
class Invoice:
__name__ = 'account.invoice'
@classmethod
def __setup__(cls):
super(Invoice, cls).__setup__()
cls._buttons.update({
'pay_using_payment_transaction': {
'invisible': Eval('state') != 'posted',
'readonly': ~Eval('groups', []).contains(
Id('account', 'group_account')),
},
})
def pay_using_transaction(self, payment_transaction):
"""
Pay an invoice using an existing payment_transaction
:param payment_transaction: Active record of a payment transaction
"""
AccountMoveLine = Pool().get('account.move.line')
for line in payment_transaction.move.lines:
if line.account == self.account:
self.write(
[self], {'payment_lines': [('add', [line.id])]}
)
if self.amount_to_pay == 0:
# Reconcile lines to pay and payment lines from transaction
try:
AccountMoveLine.reconcile(
self.lines_to_pay + self.payment_lines
)
except UserError:
# If reconcilation fails, do not raise the error
pass
return line
raise Exception('Missing account')
@classmethod
@ModelView.button_action(
'invoice_payment_gateway.wizard_pay_using_transaction')
def pay_using_payment_transaction(cls, invoices): # pragma: no cover
pass
class PayInvoiceUsingTransactionStart(BaseCreditCardViewMixin, ModelView):
'Pay using Transaction Wizard'
__name__ = 'account.invoice.pay_using_transaction.start'
invoice = fields.Many2One(
'account.invoice', 'Invoice', required=True, readonly=True
)
party = fields.Many2One('party.party', 'Party', readonly=True)
gateway = fields.Many2One(
'payment_gateway.gateway', 'Gateway', required=True,
domain=[('users', '=', Eval('user'))],
depends=['user']
)
method = fields.Function(
fields.Char('Payment Gateway Method'), 'get_method'
)
use_existing_card = fields.Boolean(
'Use existing Card?', states={
'invisible': Eval('method') != 'credit_card'
}, depends=['method']
)
payment_profile = fields.Many2One(
'party.payment_profile', 'Payment Profile',
domain=[
('party', '=', Eval('party')),
('gateway', '=', Eval('gateway')),
],
states={
'required': And(
Eval('method') == 'credit_card', Bool(Eval('use_existing_card'))
),
'invisible': ~Bool(Eval('use_existing_card'))
}, depends=['method', 'use_existing_card', 'party', 'gateway']
)
user = fields.Many2One(
"res.user", "Tryton User", readonly=True
)
amount = fields.Numeric(
'Amount', digits=(16, Eval('currency_digits', 2)),
required=True, depends=['currency_digits'],
)
currency_digits = fields.Function(
fields.Integer('Currency Digits'),
'get_currency_digits'
)
reference = fields.Char(
'Reference', states={
'invisible': Not(Eval('method') == 'manual'),
}
)
company = fields.Many2One(
'company.company', 'Company', readonly=True, required=True,
domain=[
('id', If(Eval('context', {}).contains('company'), '=', '!='),
Eval('context', {}).get('company', -1)),
],
)
credit_account = fields.Many2One(
'account.account', 'Credit Account', required=True
)
@classmethod
def __setup__(cls):
super(PayInvoiceUsingTransactionStart, cls).__setup__()
INV = Or(
Eval('method') == 'manual',
And(
Eval('method') == 'credit_card',
Bool(Eval('use_existing_card'))
)
)
STATE1 = {
'required': And(
~Bool(Eval('use_existing_card')),
Eval('method') == 'credit_card'
),
'invisible': INV
}
DEPENDS = ['use_existing_card', 'method']
cls.owner.states.update(STATE1)
cls.owner.depends.extend(DEPENDS)
cls.number.states.update(STATE1)
cls.number.depends.extend(DEPENDS)
cls.expiry_month.states.update(STATE1)
cls.expiry_month.depends.extend(DEPENDS)
cls.expiry_year.states.update(STATE1)
cls.expiry_year.depends.extend(DEPENDS)
cls.csc.states.update(STATE1)
cls.csc.depends.extend(DEPENDS)
cls.swipe_data.states = {'invisible': INV}
cls.swipe_data.depends = ['method']
cls.credit_account.domain = [
('company', '=', Eval('company', -1)),
('kind', 'in', cls._credit_account_domain())
]
cls.credit_account.depends = ['company']
def get_currency_digits(self, name): # pragma: no cover
return self.invoice.currency_digits if self.invoice else 2
def get_method(self, name=None): # pragma: no cover
"""
Return the method based on the gateway
"""
return self.gateway.method
@fields.depends('gateway')
def on_change_gateway(self): # pragma: no cover
if self.gateway:
return {
'method': self.gateway.method,
}
return {}
@classmethod
def _credit_account_domain(cls):
"""
Return a list of account kind
"""
return ['receivable']
class PayInvoiceUsingTransaction(Wizard):
'Pay using Transaction Wizard'
__name__ = 'account.invoice.pay_using_transaction'
start = StateView(
'account.invoice.pay_using_transaction.start',
'invoice_payment_gateway.pay_using_transaction_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Ok', 'pay', 'tryton-ok', default=True),
]
)
pay = StateTransition()
def default_start(self, field=None):
Invoice = Pool().get('account.invoice')
invoice = Invoice(Transaction().context.get('active_id'))
res = {
'invoice': invoice.id,
'company': invoice.company.id,
'party': invoice.party.id,
'credit_account': invoice.party.account_receivable.id,
'owner': invoice.party.name,
'currency_digits': invoice.currency_digits,
'amount': invoice.amount_to_pay_today or invoice.amount_to_pay,
'user': Transaction().user,
}
return res
def create_payment_transaction(self, profile=None):
"""
Helper function to create new payment transaction
"""
PaymentTransaction = Pool().get('payment_gateway.transaction')
Date = Pool().get('ir.date')
return PaymentTransaction(
origin='%s,%s' % (
self.start.invoice.__name__, self.start.invoice.id),
party=self.start.party,
credit_account=self.start.credit_account,
address=self.start.invoice.invoice_address,
gateway=self.start.gateway,
payment_profile=profile,
amount=self.start.amount,
description=self.start.reference or None,
date=Date.today(),
)
def create_payment_profile(self):
"""
Helper function to create payment profile
"""
Invoice = Pool().get('account.invoice')
ProfileWizard = Pool().get(
'party.party.payment_profile.add', type="wizard"
)
profile_wizard = ProfileWizard(
ProfileWizard.create()[0]
)
profile_wizard.card_info.owner = self.start.owner
profile_wizard.card_info.number = self.start.number
profile_wizard.card_info.expiry_month = self.start.expiry_month
profile_wizard.card_info.expiry_year = self.start.expiry_year
profile_wizard.card_info.csc = self.start.csc or ''
profile_wizard.card_info.gateway = self.start.gateway
profile_wizard.card_info.provider = self.start.gateway.provider
profile_wizard.card_info.address = Invoice(
Transaction().context.get('active_id')
).invoice_address
profile_wizard.card_info.party = self.start.party
with Transaction().set_context(return_profile=True):
profile = profile_wizard.transition_add()
return profile
def transition_pay(self):
"""
Creates a new payment transaction and pay invoice with it
"""
PaymentTransaction = Pool().get('payment_gateway.transaction')
profile = self.start.payment_profile
if self.start.method == 'credit_card' and (
not self.start.use_existing_card
):
profile = self.create_payment_profile()
transaction = self.create_payment_transaction(profile=profile)
transaction.save()
# Capture Transaction
PaymentTransaction.capture([transaction])
# Pay invoice using above captured transaction
self.start.invoice.pay_using_transaction(transaction)
return 'end'
class PaymentTransaction:
'Gateway Transaction'
__name__ = 'payment_gateway.transaction'
@classmethod
def _get_origin(cls):
'Add invoice to the selections'
res = super(PaymentTransaction, cls)._get_origin()
res.append('account.invoice')
return res