-
Notifications
You must be signed in to change notification settings - Fork 2
/
payment_selection.py
667 lines (569 loc) · 23.9 KB
/
payment_selection.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# -*- coding: utf-8 -*-
##############################################################################
#
# Account Analytic Online, for OpenERP
# Copyright (C) 2013-2015 XCG Consulting (www.xcg-consulting.fr)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from ast import literal_eval as leval
from copy import deepcopy
import itertools
from openerp.osv import fields, osv
from openerp.tools.translate import _
msg_invalid_line_type = _('Account type %s is not usable in payment vouchers.')
msg_invalid_partner_type_supplier = _('Partner %s is not a supplier.')
msg_invalid_partner_type_customer = _('Partner %s is not a customer.')
msg_define_dc_on_journal = _(
'Please define default credit/debit accounts on the journal "%s".')
msg_already_reconciled = _(
'The line %s is already reconciled.'
)
move_line_domain = [
'|',
('reconcile_id', '=', False),
('reconcile_partial_id', '!=', False),
('account_id.type', 'in', ['payable', 'receivable']),
('state', '=', 'valid'),
('move_id.state', '=', 'posted')
]
class good_to_pay(osv.TransientModel):
"""create vouchers for all invoices that have been selected
"""
class __container:
lines = set()
def add(self, ids, key):
self.lines.update(ids)
if key not in self._dict:
self._dict[key] = set(ids)
else:
self._dict[key].update(ids)
def delete(self, ids):
self.lines.difference_update(ids)
_name = "account.move.line.goodtopay"
_description = "Payment selection for good to pay"
_columns = {
'journal_id': fields.many2one(
'account.journal',
'Payment Method',
required=True,
domain=[('type', 'in', ['bank', 'cash'])]
),
'line_ids': fields.many2many(
'account.move.line',
'good_to_pay_rel_',
'line_id',
'good_to_pay_id',
_('Lines'),
),
'generate_report': fields.boolean('Generate Report'),
'nb_lines': fields.integer('Number of lines'),
'total_amount': fields.float('Total Amount'),
'view_selection': fields.selection(
[('complete', 'Complete view'),
('detailed', 'Detailed view')],
translate=True,
string='View selector',
required=True,
),
'partner_id': fields.many2one(
'res.partner',
string='Selected Partner',
),
'context_saved': fields.text(),
}
def default_get(self, cr, uid, field_list=None, context=None):
if 'active_ids' not in context:
return {}
vals = {}
vals['context_saved'] = (
"{'lines_by_partner': {},"
"'state_line_ids': 'entering_wizard'}"
)
aml_obj = self.pool['account.move.line']
move_lines = aml_obj.search(
cr, uid,
move_line_domain + [
('id', 'in', context.get('active_ids', [])),
],
context=context
)
move_lines = [(6, 0, move_lines)]
vals['line_ids'] = move_lines
vals['generate_report'] = True
return vals
def create(self, cr, uid, vals, context=None):
"""Override this function to add read-only fields to the list of
values; otherwise, the _add_missing_default_values function (see
orm.py) will call a second default_get at validation (which we
absolutely want to avoid as we initialize some structures there).
"""
vals['nb_lines'] = 0
vals['total_amount'] = 0.0
return super(good_to_pay, self).create(
cr, uid, vals, context=context
)
def _generate_report(self, cr, uid, active_ids, context=None):
''' Generate a Payment Suggestion report. '''
# active_ids contains move-line ids; remove them or the payment
# suggestion object will use them by default.
if 'active_ids' in context:
del context['active_ids']
return (self.pool['payment.suggestion']
.print_payment_suggestion(cr, uid, active_ids,
context=context))
def _get_account_conflicts(self, cr, uid, line_ids, context=None):
"""Find every partner that is found with two or more different accounts
in the move lines given in argument.
Return those partners associated with their accounts in a dictionary.
"""
aml_osv = self.pool['account.move.line']
partner_dict = {}
conflicts = {}
lines = aml_osv.browse(cr, uid, line_ids, context=context)
for line in lines:
if not line.partner_id:
continue
partner = line.partner_id.id
account = line.account_id.id
if partner in partner_dict:
first_account = partner_dict[partner]
if first_account != account:
if partner not in conflicts:
conflicts[partner] = {first_account}
conflicts[partner].add(account)
else:
partner_dict[partner] = account
for partner in conflicts:
conflicts[partner] = list(conflicts[partner])
return conflicts
def _print_conflicts(
self, cr, uid, conflicts, max_accounts=15, max_partners=5, context=None
):
"""Return a dialog box-friendly representation of the conflicts."""
tr_accounts = tr_partners = unt_accounts = unt_partners = 0
partner_osv = self.pool['res.partner']
account_osv = self.pool['account.account']
partner_ids = conflicts.keys()
msg = _(
u"Different accounts are being referred for the same partner(s):"
)
for partner_id in partner_ids:
account_ids = conflicts[partner_id]
if tr_partners >= max_partners or tr_accounts >= max_accounts:
unt_partners += 1
unt_accounts += len(account_ids)
continue
else:
treated_local = 0
tr_partners += 1
partner_name = partner_osv.read(
cr, uid, [partner_id], ['name'], context=context
)[0]['name']
msg += '\n' + partner_name + ':'
for account_id in account_ids:
if tr_accounts >= max_accounts:
break
else:
tr_accounts += 1
treated_local += 1
account = account_osv.read(
cr, uid, [account_id], ['code', 'name'], context=context
)[0]
msg += '\n* ' + account['code'] + ' ' + account['name']
if treated_local != len(account_ids):
pattern = _(u"({0} more conflicting accounts)")
msg += '\n' + pattern.format(len(account_ids) - treated_local)
if unt_partners:
pattern = _(u"({0} more partners with {1} accounts)")
msg += '\n' + pattern.format(unt_partners, unt_accounts)
return msg
def good_to_pay(self, cr, uid, ids, context=None):
aml_osv = self.pool['account.move.line']
avl_osv = self.pool['account.voucher.line']
voucher_osv = self.pool['account.voucher']
journal_osv = self.pool['account.journal']
supplier_to_voucher_map = dict()
voucher_amounts = dict()
action = {'type': 'ir.actions.act_window_close'}
for form in self.read(cr, uid, ids, context=context):
conflicts = self._get_account_conflicts(
cr, uid, form['line_ids'], context=context
)
if conflicts:
raise osv.except_osv(
_('Error'),
self._print_conflicts(cr, uid, conflicts, context=context)
)
context_saved = leval(form['context_saved'])
# Both debit and credit are allowed, no checks anymore
auto = form['generate_report']
active_ids = list(
itertools.chain.from_iterable(
context_saved['lines_by_partner'].values()
)
)
for aml in aml_osv.browse(
cr, uid, active_ids, context=context):
# first we need to make sure the line is acceptable to be
# used in a voucher (ie: account is marked as payable
if aml.account_id.type not in ('payable', 'receivable'):
msg = msg_invalid_line_type % aml.account_id.type
raise osv.except_osv(_('Error!'), msg)
if (
aml.account_id.type == 'payable'
and (not aml.partner_id or not aml.partner_id.supplier)
):
msg = msg_invalid_partner_type_supplier % aml.partner_id.name
raise osv.except_osv(_('Error!'), msg)
if (
aml.account_id.type == 'receivable'
and (not aml.partner_id or not aml.partner_id.customer)
):
msg = msg_invalid_partner_type_customer % aml.partner_id.name
raise osv.except_osv(_('Error!'), msg)
if aml.reconcile_id:
msg = msg_already_reconciled % aml.name
raise osv.except_osv(_('Error!'), msg)
partner_id = aml.partner_id.id
if partner_id not in supplier_to_voucher_map:
# we don't have a voucher for this supplier yet...
# just create a new one for our own use
vals = dict()
vals['partner_id'] = partner_id
# id is stored in fist column, name in second column
vals['journal_id'] = form['journal_id'][0]
if aml.account_id.type in ('payable'):
vals['type'] = 'payment'
if aml.account_id.type in ('receivable'):
vals['type'] = 'receipt'
journal = journal_osv.browse(
cr, uid, vals['journal_id'])
vals['amount'] = 0.0
vals['payment_option'] = 'without_writeoff'
# Define "pre_line" to ensure the voucher is aware of the
# lines we are going to add; otherwise it doesn't show all
# of them.
vals['pre_line'] = True
if not journal.default_credit_account_id or \
not journal.default_debit_account_id:
raise osv.except_osv(
_('Error!'),
msg_define_dc_on_journal % journal.name)
account_id = journal.default_credit_account_id.id or \
journal.default_debit_account_id.id
vals['account_id'] = account_id
bank_osv = self.pool['res.partner.bank']
bank_id = bank_osv.search(
cr, uid, [('partner_id', '=', partner_id)],
context=context
)
if bank_id:
vals['partner_bank_id'] = bank_id[0]
voucher_id = voucher_osv.create(cr, uid, vals,
context=context)
supplier_to_voucher_map[partner_id] = voucher_id
voucher_amounts[voucher_id] = 0.0
else:
voucher_id = supplier_to_voucher_map[partner_id]
# now that we have a voucher id we'll add our line to it
line_vals = dict()
line_vals['name'] = aml.name
line_vals['voucher_id'] = voucher_id
# Voucher lines must use the same account as the move lines,
# in order to be able to reconcile them with the move lines
# created during the validation of the voucher.
line_vals['account_id'] = aml.account_id.id
line_vals['type'] = 'dr' if aml.credit else 'cr'
line_vals['move_line_id'] = aml.id
avl_id = avl_osv.create(cr, uid, line_vals, context=context)
avl = avl_osv.browse(cr, uid, avl_id, context=context)
line_vals2 = dict()
line_vals2['reconcile'] = True
line_vals2['amount'] = avl.amount_unreconciled
avl_osv.write(cr, uid, [avl_id], line_vals2)
# Add credits, substract debits.
voucher_amounts[voucher_id] += avl.amount_unreconciled * (
1 if aml.credit else -1
) * (
1 if aml.account_id.type == 'payable' else -1
)
# once every voucher is finished we recompute the voucher totals
# and write them back to the vouchers
for voucher_id in voucher_amounts.keys():
voucher_osv.write(
cr, uid, [voucher_id],
{'amount': voucher_amounts[voucher_id]})
if auto:
action = self._generate_report(
cr, uid, voucher_amounts.keys(), context
)
return action
def onchange_view_selector(
self, cr, uid, ids, selector, partner_id, context_saved, context=None
):
context_saved = leval(context_saved)
if context_saved['state_line_ids'] == 'entering_wizard':
context_saved['state_line_ids'] = None
return {'value': {'context_saved': str(context_saved)}}
domain = {}
value = {}
if not selector:
selector = 'complete'
value['view_selection'] = 'complete'
list_ids = []
if selector == 'complete':
list_ids = list(
itertools.chain.from_iterable(
context_saved['lines_by_partner'].values()
)
)
partner_id = None
else:
if (
context_saved['lines_by_partner'] and
partner_id != context_saved['lines_by_partner'].keys()[0]
):
partner_id = context_saved['lines_by_partner'].keys()[0]
else:
domain['line_ids'] = [('partner_id', '=', -1)]
if list_ids:
value['line_ids'] = [(6, 0, list_ids)]
value['partner_id'] = partner_id
context_saved['state_line_ids'] = 'view_changed'
value['context_saved'] = str(context_saved)
return {
'value': value,
'domain': domain,
}
def onchange_partner_id(
self, cr, uid, ids, partner_id, context_saved, context=None
):
context_saved = leval(context_saved)
domain = deepcopy(move_line_domain)
if (
context_saved['state_line_ids'] == 'entering_wizard' or
not partner_id
):
return {
'value': {},
'domain': {'line_ids': domain}
}
domain.append(('partner_id', '=', partner_id))
list_ids = context_saved['lines_by_partner'][partner_id]
context_saved['state_line_ids'] = 'partner_changed'
res = {
'value': {
'line_ids': [(6, 0, list_ids)],
'context_saved': str(context_saved),
},
'domain': {'line_ids': domain},
}
conflicts = self._get_account_conflicts(
cr, uid, list_ids, context=context
)
if conflicts:
res['warning'] = {
'title': _('Warning'),
'message': self._print_conflicts(
cr, uid, conflicts, context=context
)
}
return res
def __compile_list_dict(self, list_dict):
""" This function compile the list of dict into
a dict of list with an easier format
i.e: [{'partner_id: (3556, name1), 'id': 20},
{'partner_id: (5024, name2), 'id': 53},
{'partner_id: (3556, name1), 'id': 59}]
-> {3556: [20, 59], 5024: [53]}
"""
res = {}
if len(list_dict) == 0:
return res
for _dict in list_dict:
if not _dict['partner_id'][0] in res:
res[_dict['partner_id'][0]] = [_dict['id']]
else:
res[_dict['partner_id'][0]].append(_dict['id'])
return res
def __calcul_partner_domain(self, uid, context):
return {
'partner_id': [('id', 'in', context['lines_by_partner'].keys())]
}
def __entering_wizard(self, cr, uid, ids, list_ids, context):
move_line_osv = self.pool['account.move.line']
reads = move_line_osv.read(
cr, uid, list_ids, ['partner_id'], context=context
)
# The "partner_id" field of accounting lines is not compulsory; we
# however need it to produce vouchers.
line_ids_wo_partner = [
read['id'] for read in reads if not read['partner_id']
]
if line_ids_wo_partner:
lines_wo_partner = move_line_osv.browse(
cr, uid, line_ids_wo_partner, context=context
)
moves_wo_partner = u", ".join({
line_wo_partner.id: line_wo_partner.move_id.name
for line_wo_partner in lines_wo_partner
}.itervalues())
raise osv.except_osv(
_(u"Error"),
_(
u"Partners undefined in the following accounting move "
u"entries: %s."
) % moves_wo_partner
)
context['lines_by_partner'] = self.__compile_list_dict(reads)
res = {
'value': {
'view_selection': 'complete',
'context_saved': context,
},
'domain': self.__calcul_partner_domain(uid, context),
}
conflicts = self._get_account_conflicts(
cr, uid, list_ids, context=context
)
if conflicts:
res['warning'] = {
'title': _('Warning'),
'message': self._print_conflicts(
cr, uid, conflicts, context=context
)
}
return res
def __substract_wo_dups(self, src, dest):
return list(set(dest) - set(src))
def __check_add_del(self, uid, ids, context):
""" -1 : del, 0: equal (should not happen), 1: add
"""
src_set = set(
itertools.chain.from_iterable(
context['lines_by_partner'].values()
)
)
dst_set = set(ids)
if dst_set - src_set:
return 1, dst_set - src_set
if src_set - dst_set:
return -1, src_set - dst_set
return 0, []
def __delete_line(self, diff, uid, context):
res = {}
line = list(diff)[0]
for key, value in context['lines_by_partner'].items():
if line in context['lines_by_partner'][key]:
context['lines_by_partner'][key] = list(set(value) - diff)
if not context['lines_by_partner'][key]:
del context['lines_by_partner'][key]
res['partner_id'] = None
res['view_selection'] = 'complete'
res['context_saved'] = context
return res
def __add_line(self, cr, uid, ids, context):
move_line_osv = self.pool['account.move.line']
reads = move_line_osv.read(
cr, uid, ids, ['partner_id'], context
)
reads = reads if type(reads) == list else [reads]
reads = [read for read in reads if read['partner_id']]
dict_ids = self.__compile_list_dict(reads)
for partner_id in dict_ids.keys():
if partner_id in context['lines_by_partner']:
context['lines_by_partner'][partner_id].extend(
self.__substract_wo_dups(
context['lines_by_partner'][partner_id],
dict_ids[partner_id]
)
)
else:
context['lines_by_partner'][partner_id] = dict_ids[partner_id]
return {'context_saved': context}
def __compute_sum_and_nb_lines(self, cr, uid, context):
res = {}
all_line_ids = []
for line_ids in context['lines_by_partner'].values():
all_line_ids.extend(line_ids)
aml_osv = self.pool['account.move.line']
res['total_amount'] = 0
aml_brl = aml_osv.browse(cr, uid, all_line_ids, context)
for aml_br in aml_brl:
res['total_amount'] += (aml_br.credit - aml_br.debit) * (
1 if aml_br.account_id.type == 'payable' else -1
)
res['nb_lines'] = len(aml_brl)
return res
def __add_del_element(
self, cr, uid, ids, selector, partner_id, list_ids, context
):
values = {}
type, diff = self.__check_add_del(uid, list_ids, context)
if type == -1:
values = self.__delete_line(diff, uid, context)
elif type == 1:
values = self.__add_line(cr, uid, diff, context)
else:
return {'value': {}}
return {
'domain': self.__calcul_partner_domain(uid, context),
'value': values
}
def __view_changed(self, cr, uid, ids, list_ids, context):
context['state_line_ids'] = None
return {'value': {'context_saved': context}}
def __partner_changed(self, cr, uid, ids, list_ids, context):
context['state_line_ids'] = None
return {'value': {'context_saved': context}}
def onchange_line_ids(
self, cr, uid, ids, selector, partner_id,
line_ids, context_saved, context=None
):
""" print the line selected or the line filtered by state
4 states : 'entering_wizard'
'view_changed'
'partner_changed
'add_del_element' = None
"""
context_saved = leval(context_saved)
# We cut the ids from the magic tuple [(6, False, [ids])]
list_ids = line_ids[0][2]
res = {}
# and we store the ids in the many2many
# depending on the state we are.
if context_saved['state_line_ids'] == 'entering_wizard':
res = self.__entering_wizard(cr, uid, ids, list_ids, context_saved)
elif not context_saved['state_line_ids']:
res = self.__add_del_element(
cr, uid, ids, selector, partner_id, list_ids, context_saved
)
elif context_saved['state_line_ids'] == 'partner_changed':
res = self.__partner_changed(cr, uid, ids, list_ids, context_saved)
elif context_saved['state_line_ids'] == 'view_changed':
res = self.__view_changed(cr, uid, ids, list_ids, context_saved)
if context_saved['lines_by_partner']:
res['value'].update(
self.__compute_sum_and_nb_lines(cr, uid, context_saved)
)
else:
res['value'].update(
{'nb_lines': 0, 'total_amount': 0.0}
)
res['value']['context_saved'] = str(
res.get('context_saved', context_saved)
)
return res