-
Notifications
You must be signed in to change notification settings - Fork 2
/
account_move_line.py
1065 lines (947 loc) · 41.1 KB
/
account_move_line.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- 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 openerp.osv import fields, osv
import openerp.addons.decimal_precision as dp
from openerp.tools.translate import _
from openerp import netsvc
import time
from lxml import etree
from openerp.addons.analytic_structure.MetaAnalytic import MetaAnalytic
# fields that are considered as forbidden once the move_id has been posted
forbidden_fields = ["move_id"]
msg_invalid_move = _('You cannot add line(s) into an already posted entry')
msg_cannot_remove_line = _(
'You cannot remove line(s) from an already posted entry')
msg_invalid_journal = _('You cannot move line(s) between journal types')
# Copied from account.py of module account because it is not in a variable
ENUM_MOVE_STATE = [('draft','Unposted'), ('posted','Posted')]
class aml_streamline_mail_thread(osv.AbstractModel):
"""Inherit from mail.thread by copy (with a different name) and bypass its
"create" function as we want to avoid the generation of notifications at
creation.
"""
_name = 'account.move.line.streamline.mail.thread'
_inherit = 'mail.thread'
def create(self, cr, uid, vals, context=None):
return osv.AbstractModel.create(
self, cr, uid, vals, context=context
)
class account_move_line(osv.osv):
__metaclass__ = MetaAnalytic
_name = 'account.move.line'
_inherit = [
'account.move.line',
'account.move.line.streamline.mail.thread',
]
def _get_reconcile_date(self, cr, uid, ids, field_name, arg, context):
move_line_osv = self.pool['account.move.line']
result = {}
move_lines = move_line_osv.browse(cr, uid, ids, context=context)
for move_line in move_lines:
result[move_line.id] = (
move_line.reconcile_id and
move_line.reconcile_id.create_date or
None
)
return result
def onchange_partner_id(self, cr, uid, ids,
move_id, partner_id, account_id=None,
debit=0, credit=0,
date=False, journal=False,
context=None):
"""
we override this function to set the date_maturity if it is not set
"""
res = super(account_move_line, self).onchange_partner_id(
cr, uid, ids, move_id,
partner_id, account_id,
debit, credit, date, journal,
context=context)
if (
'date_maturity' in res['value'] and
not res['value']['date_maturity']
):
res['value']['date_maturity'] = date
return res
_columns = dict(
currency_id=fields.many2one('res.currency',
'Currency',
help="The mandatory currency code"),
move_state=fields.related('move_id', 'state',
type='selection', string="Status",
selection=ENUM_MOVE_STATE, readonly=True),
debit_curr=fields.float('Debit T',
digits_compute=dp.get_precision('Account'),
help="This is the debit amount "
"in transaction currency"),
credit_curr=fields.float('Credit T',
digits_compute=dp.get_precision('Account'),
help="This is the credit "
"amount in transaction currency"),
currency_rate=fields.float('Used rate', digits=(12, 6)),
date_reconcile=fields.function(
_get_reconcile_date,
method=True,
string="Reconcile Date",
type='date',
store={
'account.move.line': (
lambda self, cr, uid, ids, c={}: ids,
['reconcile_id'],
20
),
}
),
# Redefine these fields for mail-thread tracking.
# Track the description and the account to know which lines events are
# for.
name=fields.char(
'Name',
size=64,
required=True,
track_visibility='always',
),
account_id=fields.many2one(
'account.account',
'Account',
required=True,
ondelete='cascade',
domain=[('type', '<>', 'view'), ('type', '<>', 'closed')],
select=2,
track_visibility='always',
),
date_maturity=fields.date(
'Due date',
select=True,
track_visibility='onchange',
),
)
_analytic = 'account_move_line'
def __modify_analysis_fields(self, doc, field, ans_dict, context):
'''
Factorization
'''
doc.xpath("//field[@name='%s']" % field)[0].\
set('modifiers', '{"tree_invisible": %s, "readonly": true}' %
str(
(
(not 'analytic_view' in context) and
(not 'complete_view' in context) and
(not 'item_complete_view' in context) and
(not 'item_analytic_view' in context)
) or (not len(ans_dict))
).lower())
def __set_column_invisible_by_context(self, doc, arch,
field, list_test,
context):
if not field in arch:
return
for test in list_test:
if test in context:
doc.xpath("//field[@name='%s']" % field)[0].set(
'modifiers',
'{"tree_invisible": true, "readonly": true}'
)
break
def __render_columns(self, doc, arch, context):
# Set the columns invisible depending on the context
self.__set_column_invisible_by_context(
doc, arch, 'partner_id',
[
'payment_view',
'reconcile_view',
'simple_view'
],
context
)
self.__set_column_invisible_by_context(
doc, arch, 'journal_id',
[
'analytic_view',
'payment_view',
],
context
)
self.__set_column_invisible_by_context(
doc, arch, 'internal_sequence_number',
[
'simple_view',
'analytic_view',
'item_simple_view',
'item_analytic_view',
'reconcile_view',
],
context
)
self.__set_column_invisible_by_context(
doc, arch, 'date',
[
'simple_view',
'complete_view',
'item_complete_view',
'item_simple_view',
'item_analytic_view',
],
context
)
self.__set_column_invisible_by_context(
doc, arch, 'date_maturity',
[
'simple_view',
'analytic_view',
'item_simple_view',
'item_analytic_view',
],
context
)
self.__set_column_invisible_by_context(
doc, arch, 'date_created',
[
'simple_view',
'analytic_view',
'item_simple_view',
'item_analytic_view',
],
context
)
self.__set_column_invisible_by_context(
doc, arch, 'currency_id',
[
'analytic_view',
'item_analytic_view',
],
context
)
self.__set_column_invisible_by_context(
doc, arch, 'debit',
[
'analytic_view',
'item_analytic_view',
],
context
)
self.__set_column_invisible_by_context(
doc, arch, 'credit',
[
'analytic_view',
'item_analytic_view',
],
context
)
self.__set_column_invisible_by_context(
doc, arch, 'account_tax_id',
[
'simple_view',
],
context
)
self.__set_column_invisible_by_context(
doc, arch, 'state',
[
'payment_view',
'reconcile_view',
],
context
)
def convert_modifiers(self, doc):
fields = doc.xpath("//field")
for field in fields:
mod = field.get('modifiers')
if mod is not None:
mod = mod.replace('"invisible"', '"tree_invisible"')
doc.xpath(
"//field[@name='%s']" % field.get(
'name')
)[0].set('modifiers', mod)
def fields_view_get(self, cr, uid, view_id=None, view_type='form',
context=None, toolbar=False, submenu=False):
"""Override this function to dynamically hide undefined analytic
fields. Note that there is no need to rename them as that has been done
before via the "fields_get" function.
"""
if context is None:
context = {}
res = super(account_move_line, self).\
fields_view_get(cr, uid, view_id=view_id,
view_type=view_type, context=context,
toolbar=toolbar, submenu=False)
ans_obj = self.pool['analytic.structure']
# display analysis codes only when present on a related structure,
# with dimension name as label
ans_dict = ans_obj.get_dimensions_names(
cr, uid, 'account_move_line', context=context
)
doc = etree.XML(res['arch'])
if 'fields' in res:
fields = res['fields']
for ordering in ans_dict:
anf = 'a{}_id'.format(ordering)
if anf in fields:
self.__modify_analysis_fields(doc, anf, ans_dict, context)
self.__render_columns(doc, fields, context)
self.convert_modifiers(doc)
res['arch'] = etree.tostring(doc)
return res
def _get_currency(self, cr, uid, context=None):
"""
override default so the currency is
always present (coming from company)
"""
if context is None:
context = {}
if not context.get('journal_id', False):
return False
jrn = self.pool['account.journal'].browse(cr, uid,
context['journal_id'])
cur = jrn.currency and jrn.currency.id or jrn.company_id.currency_id.id
return cur or False
_defaults = {
# recall standard default to make ure it is locally used (not in super)
'currency_id': _get_currency,
'credit_curr': 0.0,
'debit_curr': 0.0,
'currency_rate': 1.0,
}
def _check_currency_company(self, cr, uid, ids, context=None):
"""
disable check constraint on secondary currency.
The idea is to always have a currency and a
rate on every single move line.
"""
return True
_constraints = [
(_check_currency_company,
"If you see this, the constraint redefined "
"in account_streamline.account_move_line._check_currency_company "
"is not working!",
['currency_id']),
]
def _default_get(self, cr, uid, fields, context=None):
"""add other default values related to multicurrency
"""
data = super(account_move_line, self)._default_get(cr, uid,
fields,
context=context)
move_obj = self.pool['account.move']
if context.get('journal_id'):
total_curr = 0.0
# in account.move form view, it is not possible total
# compute total debit and credit using
# a browse record. So we must use the context to pass
# the whole one2many field and compute the total
if context.get('line_id'):
for move_line_dict in move_obj.resolve_2many_commands(
cr, uid, 'line_id', context.get('line_id'), context=context
):
data['currency_id'] = (
data.get('currency_id') or
move_line_dict.get('currency_id')
)
total_curr += (
move_line_dict.get('debit_curr', 0.0) -
move_line_dict.get('credit_curr', 0.0)
)
# compute the total of current move
data['debit_curr'] = total_curr < 0 and -total_curr or 0.0
data['credit_curr'] = total_curr > 0 and total_curr or 0.0
return data
def is_move_posted(self, cr, uid, move_id, context=None):
"""internal helper function
"""
move_osv = self.pool.get('account.move')
if move_id:
move = move_osv.browse(cr, uid, move_id, context=context)
return move.state == 'posted'
def _compute_multicurrency(self, cr, uid, vals, context=None):
if context is None:
context = {}
# some data to evaluate
account_obj = self.pool['account.account']
cur_obj = self.pool['res.currency']
amount_trans = vals.get('amount_currency', 0.0)
amount_curr = (
vals.get('debit_curr', 0.0) -
vals.get('credit_curr', 0.0)
)
amount_base = vals.get('debit', 0.0) - vals.get('credit', 0.0)
currency_trans = vals.get('currency_id', False)
cur_browse = cur_obj.browse(cr, uid, currency_trans, context=context)
# report net currency amount if necessary
if amount_trans == 0.0 and not amount_curr == 0.0:
amount_trans = vals['amount_currency'] = amount_curr
# compute actual rate ONLY when amounts in
# BOTH base and transaction curr are given
if (
not amount_trans == 0.0 and
currency_trans and
not amount_base == 0.0
):
# vals['currency_rate'] = cur_obj.round(
# cr, uid, cur_browse, abs(amount_trans / amount_base)
# )
vals['currency_rate'] = abs(amount_trans / amount_base)
# make sure the secondary currency is always present
# by copying the base amount and currency
if 'account_id' in vals and ('debit' in vals or 'credit' in vals):
currency_base = account_obj.browse(
cr, uid, vals['account_id'], context=context
).company_id.currency_id.id
if (
amount_trans == 0.0 and
(currency_trans == currency_base or not currency_trans)
):
amount_trans = vals['amount_currency'] = cur_obj.compute(
cr, uid, currency_base, currency_base,
amount_base,
context=context
)
currency_trans = vals['currency_id'] = currency_base
cur_browse = cur_obj.browse(
cr, uid, currency_trans, context=context)
vals['currency_rate'] = 1.0
#TODO : create proper tests!!!
# compute debit and credit in transaction currency when not provided
# this should happen only with generated transaction,
# not with manual entries
if not amount_trans == 0.0 and currency_trans:
vals['debit_curr'] = cur_obj.round(
cr, uid, cur_browse, amount_trans > 0.0 and amount_trans)
vals['credit_curr'] = cur_obj.round(
cr, uid, cur_browse, amount_trans < 0.0 and -amount_trans)
return vals
def onchange_currency(self, cr, uid, ids, account_id,
debit, credit, currency_id, date=False,
journal=False, context=None):
"""override onchange to pass both debit and
credit amount, not only signed amount
that might be computed only during the save process
"""
amount = debit - credit
result = super(account_move_line, self).onchange_currency(
cr, uid, ids, account_id, amount, currency_id,
date=date, journal=journal, context=context
)
# the context is already updated by the previous statement
context_rate = currency_id and self.pool['res.currency'].browse(
cr, uid, currency_id, context=context).rate
if 'value' in result:
result['value']['amount_currency'] = amount
result['value']['currency_rate'] = context_rate
return result
def write(self, cr, uid, ids, vals, context=None, check=True,
update_check=True):
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# processing vals to get complete multicurrency data
vals = self._compute_multicurrency(cr, uid, vals, context=context)
# enforce stricter security rules on
# the account.move.line / account.move relationship related to the
# posted status
target_move_id = vals.get('move_id', False)
target_journal_id = None
if target_move_id:
move_osv = self.pool.get('account.move')
target_journal_id = move_osv.browse(cr, uid, target_move_id,
context=context).journal_id.id
# Whether it is safe to remove the "move_id" key from values being set;
# this is necessary when modifying lines from posted account.move
# objects as unmodified lines are written with a "move_id" key which
# results in validation checks not passing as they think the "move_id"
# is being modified.
same_move_id = target_move_id
for aml in self.browse(cr, uid, ids, context=context):
current_move = getattr(aml, 'move_id', None)
if current_move:
current_move_id = aml.move_id.id
current_journal_id = aml.move_id.journal_id.id
else:
current_move_id = None
current_journal_id = None
# if the user tries to move away a line from an account_move which
# if already posted
if current_move_id and self.is_move_posted(
cr, uid, current_move_id, context=context) and \
target_move_id and \
not current_move_id == target_move_id:
raise osv.except_osv(_('Error!'), msg_cannot_remove_line)
# if the user is trying to move an acm into an account_move
# which is posted
if target_move_id and not target_move_id == current_move_id and \
self.is_move_posted(cr, uid, target_move_id,
context=context):
raise osv.except_osv(_('Error!'), msg_invalid_move)
if same_move_id and current_move_id != target_move_id:
same_move_id = False
# we don't allow switching from one journal_id (journal type)
# to the other even for draft entries
if target_journal_id and \
not target_journal_id == current_journal_id:
raise osv.except_osv(_('Error!'), msg_invalid_journal)
if same_move_id:
vals.pop('move_id')
return super(account_move_line, self).write(
cr, uid, ids, vals,
context=context, check=check,
update_check=update_check)
def create(self, cr, uid, vals, context=None):
if context is None:
context = {}
# No notification when creating lines.
context.update({
'mail_create_nolog': True,
'mail_create_nosubscribe': True,
})
# add a security check to ensure no one is
# creating new account.move.line inside and already posted account.move
#TODO : write proper tests!!!
move_id = vals.get('move_id', False)
if move_id and self.is_move_posted(cr, uid, move_id, context=context):
raise osv.except_osv(_('Error!'), msg_invalid_move)
# processing vals to get complete multicurrency data
vals = self._compute_multicurrency(cr, uid, vals, context=context)
return super(account_move_line, self).create(
cr, uid, vals, context=context
)
def _get_lines_to_reconcile(self, cr, uid, ids, context={}):
"""This method make several integrity checks, and return the
lines that are not reconciled among the ids provided.
:param cr: cursor
:param uid: user id
:param ids: list of id (int or long) of move lines
:param context: the context (a dictionary)
:return: iterable of browse_record
"""
if context is None:
context = {}
# if some lines are already reconciled, they are just ignored
lines = self.browse(cr, uid, ids, context=context)
unrec_lines = filter(lambda x: not x['reconcile_id'], lines)
# Better than a constraint in the account_move_reconcile object
# as it would be raised at the end, wasting time and resources
if not unrec_lines:
raise osv.except_osv(
_('Error!'),
_('Entry is already reconciled.')
)
# Maybe change the SELECT to use count(distinct (a,p))?
cr.execute('SELECT account_id, partner_id '
'FROM account_move_line '
'WHERE id IN %s '
'GROUP BY account_id,partner_id',
(tuple(ids),))
r = cr.fetchall()
if len(r) > 1:
raise osv.except_osv(
_('Error!'),
_('All entries must have the same account AND same partner '
'to be reconciled.')
)
return unrec_lines
def _compute(self, cr, uid, unrec_lines, context={}):
"""Compute debit and credit and some associated values on a list of
unreconciled move lines.
Also do the following checks::
- that each line is valid,
- that each line are on the same company
- that all line have the same second currency if not reconciling on
company currency.
"""
credit = debit = credit_curr = debit_curr = 0.0
amount_currency_writeoff = writeoff = currency_rate_difference = 0.0
account_id = False
partner_id = False
currency_id = False
# XXX a SQL request might faster there (rather than calculating until
# a company is not in the list
company_list = []
# unrec_ids will be used to store all the id for this reconcile, and
# also all the newly created lines too
unrec_ids = []
is_partial = context.get('reconcile_partial', False)
# Added to avoid trying to add same lines twice if doing partial
merges_rec = set()
for line in unrec_lines:
# these are the received lines filtered out of already reconciled
# lines we compute allocation totals in both currencies
account_id = line.account_id.id
partner_id = line.partner_id and line.partner_id.id or False
currency_id = line.currency_id and line.currency_id.id or False
if line.state != 'valid':
raise osv.except_osv(
_('Error!'),
_('Entry "%s" is not valid!') % line.name
)
if company_list and not line.company_id.id in company_list:
raise osv.except_osv(
_("Warning!"),
_("To reconcile the entries company "
"should be the same for all entries.")
)
company_list.append(line.company_id.id)
# control on second currency : must always be the same
if (
context.get('reconcile_second_currency', True) and
currency_id and not currency_id == line['currency_id']['id']
):
raise osv.except_osv(
_("Error!"),
_("All entries must have the same second currency! "
"Reconcile on company currency otherwise.")
)
credit += line.credit or 0.0
credit_curr += line.credit_curr or 0.0
debit += line.debit or 0.0
debit_curr += line.debit_curr or 0.0
# the computed write off is the net currency amount
amount_currency_writeoff += (
(line.debit_curr or 0.0) - (line.credit_curr or 0.0)
)
# get only relevant ids of lines to reconcile
unrec_ids.append(line.id)
# if the reconcile is partial, count also non reconciled but
# partially reconciled lines with same partial id as one
# of our reconciled line
if (
is_partial
and line.reconcile_partial_id
and line.reconcile_partial_id not in merges_rec
):
merges_rec.add(line.reconcile_partial_id)
for line2 in line.reconcile_partial_id.line_partial_ids:
if not line2.reconcile_id:
if line2 not in unrec_lines:
# only put it at the end
unrec_lines.append(line2)
# Get account to check for reconcile flag
account_obj = self.pool['account.account']
account = account_obj.browse(cr, uid, account_id, context=context)
if not account.reconcile:
raise osv.except_osv(
_("Error"),
_("The account is not defined to be reconciled!")
)
company_currency = account.company_id.currency_id
currency_obj = self.pool['res.currency']
currency = currency_obj.browse(cr, uid, currency_id, context=context)
# Use date in context or today
date = context.get('date_p', time.strftime('%Y-%m-%d'))
# We will be using a context key to define the
# reconciliation currency (base or trans)
if context.get('reconcile_second_currency', True):
# the actual write off is the conversion of the second
# currency net amount to base currency at current date
writeoff = currency_obj.compute(
cr, uid, currency.id, company_currency.id,
amount_currency_writeoff, context={'date': date}
)
currency_rate_difference = debit - credit - writeoff
else:
writeoff = debit - credit
return (credit, debit, credit_curr, debit_curr,
amount_currency_writeoff, writeoff, currency_rate_difference,
account, partner_id , currency, unrec_ids, merges_rec
)
def reconcile_partial(self, cr, uid, ids, type='auto',
writeoff_acc_id=False, writeoff_period_id=False,
writeoff_journal_id=False, context=None):
"""This method is completely overridden in order
to include a full multicurrency support
The context will determine if second currency is used
for reconciliation or not 'reconcile_second_currency'
Many optimisations and integrity controls are added.
Finally, the original code is documented for an easier maintenance.
"""
move_rec_obj = self.pool['account.move.reconcile']
if context is None:
context = {}
# if some lines are already reconciled, they are just ignored
unrec_lines = self._get_lines_to_reconcile(cr, uid, ids, context)
compute_context = dict(context, reconcile_partial=True)
(credit, debit, credit_curr, debit_curr, amount_currency_writeoff,
writeoff, currency_rate_difference, account_id, partner_id ,
currency_id, unrec_ids, merges_rec) = self._compute(
cr, uid, unrec_lines, compute_context)
merges_ids = {merge.id for merge in merges_rec}
# FIXME when to do normal reconcile? maybe base on writeoff?
if self.pool['res.currency'].is_zero(cr, uid, currency_id, writeoff):
# TODO split reconcile in two to avoid doing _check/_compute twice
res = self.reconcile(
cr, uid, unrec_ids, context=context,
writeoff_acc_id=writeoff_acc_id,
writeoff_period_id=writeoff_period_id,
writeoff_journal_id=writeoff_journal_id)
return res
# marking the lines as reconciled does not change their validity, so there is no need
# to revalidate their moves completely.
reconcile_context = dict(context, novalidate=True)
r_id = move_rec_obj.create(cr, uid,
{'type': type,
},
context=reconcile_context)
# Do not use the magic tuples as it is extremely costly on large collections
# XXX we do not need to put reconcile_id to None/False, right?
self.write(cr, uid, unrec_ids, {'reconcile_partial_id': r_id,
},
context=reconcile_context)
merges_ids.add(r_id)
# FIXME This method does not do any currency magic, it might need to
# be replaced
move_rec_obj.reconcile_partial_check(
cr, uid, list(merges_ids), context=reconcile_context)
return True
def reconcile(self, cr, uid, ids, type='auto',
writeoff_acc_id=False, writeoff_period_id=False,
writeoff_journal_id=False, context=None):
"""This method is completely overridden in order
to include a full multicurrency support
The context will determine if second currency is used
for reconciliation or not 'reconcile_second_currency'
In addition, when matching the transaction amounts,
differences must be processed as both exchange difference and
write-off, when applicable. Many optimisations and integrity
controls are added. Finally, the original code is
documented for an easier maintenance.
"""
move_obj = self.pool['account.move']
move_rec_obj = self.pool['account.move.reconcile']
partner_obj = self.pool['res.partner']
currency_obj = self.pool['res.currency']
# if some lines are already reconciled, they are just ignored
unrec_lines = self._get_lines_to_reconcile(cr, uid, ids, context)
if context is None:
context = {}
# unrec_ids will be used to store all the id for this reconcile, and
# also all the newly created lines too
(credit, debit, credit_curr, debit_curr, amount_currency_writeoff,
writeoff, currency_rate_difference, account, partner_id ,
currency, unrec_ids, partial_reconcile_ids) = self._compute(
cr, uid, unrec_lines, context)
# we need some browse records
company_currency = account.company_id.currency_id
# Use date in context or today
date = context.get('date_p', time.strftime('%Y-%m-%d'))
# If date_p in context => take this date
# this is so old school...
# if context.has_key('date_p') and context['date_p']:
# date = context['date_p']
# else:
# date = time.strftime('%Y-%m-%d')
if context.get('fy_closing'):
# We don't want to generate any write-off
# when being called from the
# wizard used to close a fiscal year (and it doesn't give us any
# writeoff_acc_id).
pass
else:
# this condition is replaced as we separate
# write off from exchange difference
# elif (not currency_obj.is_zero(
# cr, uid, account.company_id.currency_id, writeoff
# )) or
# (account.currency_id and
# (not currency_obj.is_zero(
# cr, uid, account.currency_id, currency
# ))):
# create exchange difference transaction
if not currency_obj.is_zero(
cr, uid, currency, currency_rate_difference
):
if currency_rate_difference > 0:
# this is a gain account comes from account_voucher module
exchange_diff_acc_id = account.company_id.income_currency_exchange_account_id.id
debit = currency_rate_difference
credit = 0.0
self_credit = currency_rate_difference
self_debit = 0.0
else:
# this is a loss
exchange_diff_acc_id = account.company_id.expense_currency_exchange_account_id.id
debit = 0.0
credit = -currency_rate_difference
self_credit = 0.0
self_debit = -currency_rate_difference
if not exchange_diff_acc_id:
raise osv.except_osv(
_('Warning!'),
_('You have to configure an account '
'for the exchange gain/loss.')
)
libelle = _('Exchange difference')
exchange_diff_lines = [
(0, 0, {
'name': libelle,
'debit': self_debit,
'credit': self_credit,
'account_id': account.id,
'date': date,
'partner_id': partner_id,
'currency_id': (
currency.id or
(account.currency_id.id or False)
),
'amount_currency': 0.0
}),
(0, 0, {
'name': libelle,
'debit': debit,
'credit': credit,
'account_id': exchange_diff_acc_id,
'analytic_account_id': context.get(
'analytic_id', False
),
'date': date,
'partner_id': partner_id,
'currency_id': (
currency.id or
(account.currency_id.id or False)
),
'amount_currency': 0.0
})
]
exchange_diff_move_id = move_obj.create(cr, uid, {
'period_id': writeoff_period_id,
'journal_id': writeoff_journal_id,
'date': date,
'state': 'draft',
'line_id': exchange_diff_lines
})
# The generated transaction needs to be
# added to the allocation block
exchange_diff_line_ids = self.search(
cr, uid,
[
('move_id', '=', exchange_diff_move_id),
('account_id', '=', account.id)
]
)
# the following case should never happen but still...
if account.id == exchange_diff_acc_id:
exchange_diff_line_ids = [exchange_diff_line_ids[1]]
# add the created lines to the reconcile block
unrec_ids += exchange_diff_line_ids
# create write off transaction
if not currency_obj.is_zero(cr, uid, company_currency, writeoff):
if not writeoff_acc_id:
raise osv.except_osv(
_('Warning!'),
_('You have to provide an account '
'for the write off entry.')
)
if writeoff > 0:
debit = writeoff
credit = 0.0
self_credit = writeoff
self_debit = 0.0
else:
debit = 0.0
credit = -writeoff
self_credit = 0.0
self_debit = -writeoff
# If comment exist in context, take it
if 'comment' in context and context['comment']:
libelle = context['comment']
else:
libelle = _('Write-Off')
writeoff_lines = [
(0, 0, {
'name': libelle,
'debit': self_debit,
'credit': self_credit,
'account_id': account.id,
'date': date,
'partner_id': partner_id,
'currency_id': (
currency.id or
(account.currency_id.id or False)
),
'amount_currency':-1 * amount_currency_writeoff
}),
(0, 0, {
'name': libelle,
'debit': debit,
'credit': credit,
'account_id': writeoff_acc_id,
'analytic_account_id': context.get(
'analytic_id', False
),
'date': date,
'partner_id': partner_id,
'currency_id': (
currency.id or