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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
|
#!/usr/bin/env python
#-*- encoding:utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011 TeMPO Consulting, MSF. All Rights Reserved
# All Rigts Reserved
# Developer: Olivier DOSSMANN
#
# 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 osv import osv
from osv import fields
from tools.translate import _
from register_tools import _get_third_parties
from register_tools import _set_third_parties
from register_tools import previous_register_is_closed
from register_tools import create_cashbox_lines
from register_tools import totally_or_partial_reconciled
import time
from datetime import datetime
import decimal_precision as dp
from tools.misc import flatten
def _get_fake(cr, table, ids, *a, **kw):
ret = {}
for id in ids:
ret[id] = False
return ret
def _search_fake(*a, **kw):
return []
class hr_employee(osv.osv):
_name = 'hr.employee'
_inherit = 'hr.employee'
_columns = {
'filter_for_third_party': fields.function(_get_fake, type='char', string="Internal Field", fnct_search=_search_fake, method=False),
}
hr_employee()
class res_partner(osv.osv):
_name = 'res.partner'
_inherit = 'res.partner'
def _get_fake(self, cr, table, ids, field_name, arg, context):
ret = {}
for id in ids:
ret[id] = False
return ret
def _search_filter_third(self, cr, uid, obj, name, args, context):
if not context:
context = {}
if not args:
return []
if args[0][2]:
t = self.pool.get('account.account').read(cr, uid, args[0][2], ['type', 'type_for_register'])
if t['type'] == 'payable' and t['type_for_register'] != 'down_payment':
return [('property_account_payable', '=', args[0][2])]
if t['type'] == 'receivable' and t['type_for_register'] != 'down_payment':
return [('property_account_receivable', '=', args[0][2])]
return []
_columns = {
'filter_for_third_party': fields.function(_get_fake, type='char', string="Internal Field", fnct_search=_search_filter_third, method=True),
}
res_partner()
class account_journal(osv.osv):
_name = 'account.journal'
_inherit = 'account.journal'
def _get_fake(self, cr, table, ids, field_name, arg, context):
ret = {}
for id in ids:
ret[id] = False
return ret
def _search_filter_third(self, cr, uid, obj, name, args, context):
if not context:
context = {}
dom = [('type', 'in', ['cash', 'bank', 'cheque'])]
if not args or not context.get('curr'):
return dom
if args[0][2]:
t = self.pool.get('account.account').read(cr, uid, args[0][2], ['type_for_register'])
if t['type_for_register'] == 'transfer_same':
return dom+[('currency', 'in', [context['curr']])]
return dom
_columns = {
'filter_for_third_party': fields.function(_get_fake, type='char', string="Internal Field", fnct_search=_search_filter_third, method=True),
}
account_journal()
class account_bank_statement(osv.osv):
_name = "account.bank.statement"
_inherit = "account.bank.statement"
_sql_constraints = [
('period_journal_uniq', 'unique (period_id, journal_id)', 'You cannot have a register on the same period and the same journal!')
]
def __init__(self, pool, cr):
super(account_bank_statement, self).__init__(pool, cr)
if self.pool._store_function.get(self._name, []):
newstore = []
for fct in self.pool._store_function[self._name]:
if fct[1] != 'balance_end':
newstore.append(fct)
self.pool._store_function[self._name] = newstore
def _end_balance(self, cr, uid, ids, field_name=None, arg=None, context=None):
"""
Calculate register's balance
"""
if context is None:
context = {}
st_line_obj = self.pool.get("account.bank.statement.line")
res = {}
# Add this context in order to escape cheque register filter
ctx = context.copy()
ctx.update({'from_end_balance': True})
for statement in self.browse(cr, uid, ids, context=ctx):
res[statement.id] = statement.balance_start
for st_line in statement.line_ids:
res[statement.id] += st_line.amount or 0.0
return res
def _get_register_id(self, cr, uid, ids, field_name=None, arg=None, context=None):
"""
Get current register id
"""
res = {}
for st in self.browse(cr, uid, ids, context=context):
res[st.id] = st.id
return res
def _balance_gap_compute(self, cr, uid, ids, name, attr, context=None):
"""
Calculate Gap between bank register balance (balance_end_real) and calculated balance (balance_end)
"""
res = {}
for statement in self.browse(cr, uid, ids):
res[statement.id] = ((statement.balance_end_real or 0.0) - (statement.balance_end or 0.0)) or 0.0
return res
_columns = {
'balance_end': fields.function(_end_balance, method=True, store=False, string='Calculated Balance', \
help="Calculated balance"),
'virtual_id': fields.function(_get_register_id, method=True, store=False, type='integer', string='Id', readonly="1",
help='Virtual Field that take back the id of the Register'),
'balance_end_real': fields.float('Closing Balance', digits_compute=dp.get_precision('Account'), states={'confirm':[('readonly', True)]},
help="Please enter manually the end-of-month balance, as per the printed bank statement received. Before confirming closing balance & closing the register, you must make sure that the calculated balance of the bank register is equal to that amount."),
'closing_balance_frozen': fields.boolean(string="Closing balance freezed?", readonly="1"),
'name': fields.char('Register Name', size=64, required=True, states={'confirm': [('readonly', True)]},
help='If you give the Name other then /, its created Accounting Entries Move will be with same name as statement name. This allows the statement entries to have the same references than the statement itself'),
'journal_id': fields.many2one('account.journal', 'Journal Name', required=True, readonly=True, states={'draft':[('readonly',False)]}),
'filter_for_third_party': fields.function(_get_fake, type='char', string="Internal Field", fnct_search=_search_fake, method=False),
'balance_gap': fields.function(_balance_gap_compute, method=True, string='Gap', readonly=True),
'notes': fields.text('Comments'),
}
_defaults = {
'balance_start': lambda *a: 0.0,
}
def balance_check(self, cr, uid, register_id, journal_type='bank', context=None):
"""
Check the balance for Registers
"""
if not context:
context = {}
# Disrupt cheque verification
if journal_type == 'cheque':
return True
# Add other verification for cash register
if journal_type == 'cash':
if not self._equal_balance(cr, uid, register_id, context):
raise osv.except_osv(_('Error !'), _('CashBox Balance is not matching with Calculated Balance !'))
st = self.browse(cr, uid, register_id, context=context)
if not (abs((st.balance_end or 0.0) - st.balance_end_real) < 0.0001):
raise osv.except_osv(_('Error !'),
_('The statement balance is incorrect !\n') +
_('The expected balance (%.2f) is different than the computed one. (%.2f)') % (st.balance_end_real, st.balance_end))
return True
def write(self, cr, uid, ids, values, context=None):
"""
Bypass disgusting default account_bank_statement write function.
"""
if values.get('open_advance_amount', False):
values.update({'open_advance_amount': abs(values.get('open_advance_amount'))})
if values.get('unrecorded_expenses_amount', False):
values.update({'unrecorded_expenses_amount': abs(values.get('unrecorded_expenses_amount'))})
return osv.osv.write(self, cr, uid, ids, values, context=context)
def unlink(self, cr, uid, ids, context=None):
"""
Delete a bank statement is forbidden!
"""
if context and context.get('from', False) and context.get('from') == "journal_deletion":
return super(account_bank_statement, self).unlink(cr, uid, ids)
raise osv.except_osv(_('Warning'), _('Delete a Register is totally forbidden!'))
return True
def button_open_bank(self, cr, uid, ids, context=None):
"""
when pressing 'Open Bank' button
"""
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Verify that previous register is open, unless this register is the first register
return self.write(cr, uid, ids, {'state': 'open'})
def check_status_condition(self, cr, uid, state, journal_type='bank'):
"""
Check Status of Register
"""
return state =='draft' or state == 'open'
def button_confirm_bank(self, cr, uid, ids, context=None):
"""
Confirm Bank Register
"""
# First verify that all lines are in hard state
for register in self.browse(cr, uid, ids, context=context):
for line in register.line_ids:
if line.state != 'hard':
raise osv.except_osv(_('Warning'), _('All entries must be hard posted before closing this Register!'))
# @@@override@account.account_bank_statement.button_confirm_bank()
# done = []
obj_seq = self.pool.get('ir.sequence')
if context is None:
context = {}
for st in self.browse(cr, uid, ids, context=context):
j_type = st.journal_id.type
company_currency_id = st.journal_id.company_id.currency_id.id
if not self.check_status_condition(cr, uid, st.state, journal_type=j_type):
continue
# modification of balance_check for cheque registers
if st.journal_id.type in ['bank', 'cash']:
self.balance_check(cr, uid, st.id, journal_type=j_type, context=context)
if (not st.journal_id.default_credit_account_id) \
or (not st.journal_id.default_debit_account_id):
raise osv.except_osv(_('Configuration Error !'),
_('Please verify that an account is defined in the journal.'))
if not st.name == '/':
st_number = st.name
else:
if st.journal_id.sequence_id:
c = {'fiscalyear_id': st.period_id.fiscalyear_id.id}
st_number = obj_seq.get_id(cr, uid, st.journal_id.sequence_id.id, context=c)
else:
st_number = obj_seq.get(cr, uid, 'account.bank.statement')
for line in st.move_line_ids:
if line.state <> 'valid':
raise osv.except_osv(_('Error !'),
_('The account entries lines are not in valid state.'))
for st_line in st.line_ids:
if st_line.analytic_account_id:
if not st.journal_id.analytic_journal_id:
raise osv.except_osv(_('No Analytic Journal !'),_("You have to define an analytic journal on the '%s' journal!") % (st.journal_id.name,))
if not st_line.amount:
continue
# st_line_number = self.get_next_st_line_number(cr, uid, st_number, st_line, context)
# Lines are hard posted. That's why create move lines is useless
# self.create_move_from_st_line(cr, uid, st_line.id, company_currency_id, st_line_number, context)
# Verify lines reconciliation status
#if not totally_or_partial_reconciled(self, cr, uid, [x.id for x in st.line_ids], context=context):
# raise osv.except_osv(_('Warning'), _("Some lines are not reconciled. Please verify that all lines are reconciled totally or partially."))
self.write(cr, uid, [st.id], {'name': st_number}, context=context)
# Verify that the closing balance is freezed
if not st.closing_balance_frozen and st.journal_id.type in ['bank', 'cash']:
raise osv.except_osv(_('Error'), _("Please confirm closing balance before closing register named '%s'") % st.name or '')
# done.append(st.id)
# Display the bank confirmation wizard
title = "Bank"
if context.get('confirm_from', False) and context.get('confirm_from') == 'cheque':
title = "Cheque"
title += " confirmation wizard"
return {
'name': title,
'type': 'ir.actions.act_window',
'res_model': 'wizard.confirm.bank',
'target': 'new',
'view_mode': 'form',
'view_type': 'form',
'context':
{
'active_id': ids[0],
'active_ids': ids,
'statement_id': st.id,
}
}
# @@@end
def button_create_invoice(self, cr, uid, ids, context=None):
"""
Create a direct invoice
"""
if context is None:
context = {}
# Search the customized view we made for Supplier Invoice (for * Register's users)
currency = self.read(cr, uid, ids, ['currency'])[0]['currency']
if isinstance(currency, tuple):
currency =currency[0]
id = self.pool.get('wizard.account.invoice').search(cr, uid, [('currency_id','=',currency), ('register_id', '=', ids[0])])
if not id:
id = self.pool.get('wizard.account.invoice').create(cr, uid, {'currency_id': currency, 'register_id': ids[0], 'type': 'in_invoice'},
context={'journal_type': 'purchase', 'type': 'in_invoice'})
return {
'name': "Supplier Direct Invoice",
'type': 'ir.actions.act_window',
'res_model': 'wizard.account.invoice',
'target': 'new',
'view_mode': 'form',
'view_type': 'form',
'res_id': id,
'context':
{
'active_id': ids[0],
'type': 'in_invoice',
'journal_type': 'purchase',
'active_ids': ids,
}
}
def button_wiz_import_invoices(self, cr, uid, ids, context=None):
"""
When pressing 'Import Invoices' button then opening a wizard to select some invoices and add them into the register by changing their states to 'paid'.
"""
# statement_id is useful for making some line's registration.
# currency_id is useful to filter invoices in the same currency
st = self.browse(cr, uid, ids[0], context=context)
id = self.pool.get('wizard.import.invoice').create(cr, uid, {'statement_id': ids[0] or None, 'currency_id': st.currency.id or None}, context=context)
# Remember if we come from a cheque register (for adding some fields)
from_cheque = False
if st.journal_id.type == 'cheque':
from_cheque = True
return {
'name': "Import Invoice",
'type': 'ir.actions.act_window',
'res_model': 'wizard.import.invoice',
'target': 'new',
'view_mode': 'form,tree',
'view_type': 'form',
'res_id': [id],
'context':
{
'active_id': ids[0],
'active_ids': ids,
'from_cheque': from_cheque,
}
}
def button_wiz_import_cheques(self, cr, uid, ids, context=None):
"""
When pressing 'Import Cheques' button then opening a wizard to select some cheques from a register and add them into the present register
in a temp post state.
"""
# statement_id is useful for making some line's registration.
# currency_id is useful to filter cheques in the same currency
# period_id is useful to filter cheques drawn in the same period
st = self.browse(cr, uid, ids[0], context=context)
id = self.pool.get('wizard.import.cheque').create(cr, uid, {'statement_id': ids[0] or None, 'currency_id': st.currency.id or None,
'period_id': st.period_id.id}, context=context)
return {
'name': "Import Cheque",
'type': 'ir.actions.act_window',
'res_model': 'wizard.import.cheque',
'target': 'new',
'view_mode': 'form,tree',
'view_type': 'form',
'res_id': [id],
'context':
{
'active_id': ids[0],
'active_ids': ids,
}
}
def button_confirm_closing_balance(self, cr, uid, ids, context=None):
"""
Confirm that the closing balance could not be editable.
"""
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
res = []
for reg in self.browse(cr, uid, ids, context=context):
# Validate register only if this one is open
if reg.state == 'open':
res_id = self.write(cr, uid, [reg.id], {'closing_balance_frozen': True}, context=context)
res.append(res_id)
# Create next starting balance for cash registers
if reg.journal_id.type == 'cash':
create_cashbox_lines(self, cr, uid, reg.id, context=context)
# For bank register, give balance_end
elif reg.journal_id.type == 'bank':
# Verify that another bank statement exists
st_prev_ids = self.search(cr, uid, [('prev_reg_id', '=', reg.id)], context=context)
if len(st_prev_ids) > 1:
raise osv.except_osv(_('Error'), _('A problem occured: More than one register have this one as previous register!'))
if st_prev_ids:
self.write(cr, uid, st_prev_ids, {'balance_start': reg.balance_end_real}, context=context)
return res
def button_confirm_closing_bank_balance(self, cr, uid, ids, context=None):
"""
Verify bank register balance before closing it.
"""
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
for reg in self.browse(cr, uid, ids):
# Verify that the closing balance (balance_end_real) correspond to the calculated balance (balance_end)
# NB: UTP-187 reveals that some difference appears between balance_end_real and balance_end. These fields are float. And balance_end_real is calculated. In python this imply some difference.
# Because of fields values with 2 digits, we compare the two fields difference with 0.001 (10**-3)
if (abs(round(reg.balance_end_real, 2) - round(reg.balance_end, 2))) > 10**-3:
raise osv.except_osv(_('Warning'), _('Bank register balance is not equal to Calculated balance.'))
return self.button_confirm_closing_balance(cr, uid, ids, context=context)
def button_open_advances(self, cr, uid, ids, context=None):
"""
Open a list of open advances
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
domain = []
date = time.strftime('%Y-%m-%d')
registers = self.browse(cr, uid, ids, context=context)
register = registers and registers[0] or False
if not register:
raise osv.except_osv(_('Error'), _('Please select a register first.'))
domain = [('account_id.type_for_register', '=', 'advance'), ('state', '=', 'hard'), ('reconciled', '=', False), ('amount', '<=', 0.0), ('date', '<=', date)]
name = _('Open Advances')
if register.journal_id and register.journal_id.currency:
# prepare some values
name += ' - ' + register.journal_id.currency.name
domain.append(('statement_id.journal_id.currency', '=', register.journal_id.currency.id))
# Prepare view
view = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'register_accounting', 'view_account_bank_statement_line_tree')
view_id = view and view[1] or False
# Prepare search view
search_view = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'register_accounting', 'view_account_bank_statement_line_filter')
search_view_id = search_view and search_view[1] or False
context.update({'open_advance': register.id})
return {
'name': name,
'type': 'ir.actions.act_window',
'res_model': 'account.bank.statement.line',
'view_type': 'form',
'view_mode': 'tree,form',
'view_id': [view_id],
'search_view_id': search_view_id,
'domain': domain,
'context': context,
'target': 'current',
}
def get_register_lines(self, cr, uid, ids, context=None):
"""
Return all register lines from first given register
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
domain = [('statement_id', '=', ids[0])]
# Search valid ids
reg = self.browse(cr, uid, ids[0])
return {
'name': reg and reg.name or 'Register Lines',
'type': 'ir.actions.act_window',
'res_model': 'account.bank.statement.line',
'view_type': 'form',
'view_mode': 'tree,form',
'context': context,
'domain': domain,
'target': 'current',
}
def get_analytic_register_lines(self, cr, uid, ids, context=None):
"""
Return all analytic lines attached to register lines from first given register
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
valid_ids = []
# Search valid ids
reg = self.browse(cr, uid, ids[0])
domain = [('account_id.category', '=', 'FUNDING'), ('move_id.statement_id', 'in', [ids[0]])]
context.update({'display_fp': True})
return {
'name': reg and 'Analytic Entries from ' + reg.name or 'Analytic Entries',
'type': 'ir.actions.act_window',
'res_model': 'account.analytic.line',
'view_type': 'form',
'view_mode': 'tree,form',
'context': context,
'domain': domain,
'target': 'current',
}
account_bank_statement()
class account_bank_statement_line(osv.osv):
_name = "account.bank.statement.line"
_inherit = "account.bank.statement.line"
_order = 'date desc, id desc'
def _get_state(self, cr, uid, ids, field_name=None, arg=None, context=None):
"""
Return account_bank_statement_line state in order to know if the bank statement line is in :
- draft
- temp posting
- hard posting
- unknown if an error occured or anything else (for an example the move have a new state)
"""
# Prepare some variables
res = {}
for absl in self.browse(cr, uid, ids, context=context):
# Verify move existence
if not absl.move_ids:
res[absl.id] = 'draft'
continue
# If exists, check move state
state = absl.move_ids[0].state
if state == 'draft':
res[absl.id] = 'temp'
elif state == 'posted':
res[absl.id] = 'hard'
else:
res[absl.id] = 'unknown'
return res
def _get_amount(self, cr, uid, ids, field_name=None, arg=None, context=None):
"""
Get the amount from amount_in and amount_on
"""
# Variable initialisation
default_amount = 0.0
res = {}
# Browse account bank statement lines
for absl in self.browse(cr, uid, ids, context=context):
# amount is positive so he should be in amount_in
if absl.amount > 0 and field_name == "amount_in":
res[absl.id] = abs(absl.amount)
# amount is negative, it should be in amount_out
elif absl.amount < 0 and field_name == "amount_out":
res[absl.id] = abs(absl.amount)
# if no resultat, we display 0.0 (default amount)
else:
res[absl.id] = default_amount
return res
def _search_state(self, cr, uid, obj, name, args, context=None):
"""
Search elements by state :
- draft
- temp
"""
# Test how many arguments we have
if not len(args):
return []
# We just support = and in operators
if args[0][1] not in ['=', 'in']:
raise osv.except_osv(_('Warning'), _('This filter is not implemented yet!'))
# Case where we search draft lines
sql_draft = """
SELECT st.id FROM account_bank_statement_line st
LEFT JOIN account_bank_statement_line_move_rel rel ON rel.move_id = st.id
WHERE rel.move_id is null
"""
sql_temp = """SELECT st.id FROM account_bank_statement_line st
LEFT JOIN account_bank_statement_line_move_rel rel ON rel.move_id = st.id
LEFT JOIN account_move m ON m.id = rel.statement_id
WHERE m.state = 'draft'
"""
sql_hard = """SELECT st.id FROM account_bank_statement_line st
LEFT JOIN account_bank_statement_line_move_rel rel ON rel.move_id = st.id
LEFT JOIN account_move m ON m.id = rel.statement_id
WHERE m.state = 'posted'
"""
ids = []
filterok = False
if args[0][1] == '=' and args[0][2] == 'draft' or 'draft' in args[0][2]:
sql = sql_draft
cr.execute(sql)
ids += [x[0] for x in cr.fetchall()]
filterok = True
# Case where we search temp lines
if args[0][1] == '=' and args[0][2] == 'temp' or 'temp' in args[0][2]:
sql = sql_temp
cr.execute(sql)
ids += [x[0] for x in cr.fetchall()]
filterok = True
if args[0][1] == '=' and args[0][2] == 'hard' or 'hard' in args[0][2]:
sql = sql_hard
cr.execute(sql)
ids += [x[0] for x in cr.fetchall()]
filterok = True
# Non expected case
if not filterok:
raise osv.except_osv(_('Warning'), _('This filter is not implemented yet!'))
return [('id', 'in', ids)]
def _get_reconciled_state(self, cr, uid, ids, field_name=None, args=None, context=None):
"""
Give the state of reconciliation for the account bank statement line
"""
# Prepare some values
res = {}
# browse account bank statement lines
for absl in self.browse(cr, uid, ids):
# browse each move and move lines
if absl.move_ids and absl.state == 'hard':
res[absl.id] = False
for move in absl.move_ids:
for move_line in move.line_id:
# Result is True if the account is reconciliable and a reconcile_id exists
if move_line.account_id.reconcile and move_line.reconcile_id:
res[absl.id] = True
break
else:
res[absl.id] = False
return res
def _search_reconciled(self, cr, uid, obj, name, args, context=None):
"""
Search all lines that are reconciled or not
"""
result = []
# Test how many arguments we have
if not len(args):
return []
# We just support "=" case
if args[0][1] not in ['=', 'in']:
raise osv.except_osv(_('Warning'), _('This filter is not implemented yet!'))
# Search statement lines that have move lines and which moves are posted
if args[0][2] == True:
sql_posted_moves = """
SELECT st.id FROM account_bank_statement_line st
LEFT JOIN account_bank_statement_line_move_rel rel ON rel.move_id = st.id
LEFT JOIN account_move move ON rel.statement_id = move.id
LEFT JOIN account_move_line line ON line.move_id = move.id
LEFT JOIN account_account ac ON ac.id = line.account_id
WHERE rel.move_id is not null AND move.state = 'posted'
GROUP BY st.id HAVING COUNT(reconcile_id IS NULL AND ac.reconcile='t' OR NULL)=0
"""
else:
sql_posted_moves = """
SELECT st.id FROM account_bank_statement_line st
LEFT JOIN account_bank_statement_line_move_rel rel ON rel.move_id = st.id
LEFT JOIN account_move move ON rel.statement_id = move.id
LEFT JOIN account_move_line line ON line.move_id = move.id
LEFT JOIN account_account ac ON ac.id = line.account_id
WHERE
rel.move_id is null OR move.state != 'posted' OR (line.reconcile_id IS NULL AND ac.reconcile='t')
"""
cr.execute(sql_posted_moves)
return [('id', 'in', [x[0] for x in cr.fetchall()])]
def _get_number_imported_invoice(self, cr, uid, ids, field_name=None, args=None, context=None):
ret = {}
for i in self.read(cr, uid, ids, ['imported_invoice_line_ids']):
ret[i['id']] = len(i['imported_invoice_line_ids'])
return ret
def _get_down_payment_state(self, cr, uid, ids, field_name=None, args=None, context=None):
"""
Verify down payment eligibility:
- account should be a down_payment type for register
- amount should be negative
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
res = {}
# Browse elements
for line in self.browse(cr, uid, ids, context=context):
res[line.id] = False
if line.account_id and line.account_id.user_type and line.account_id.type_for_register == 'down_payment' and line.amount < 0.0:
res[line.id] = True
return res
def _get_transfer_with_change_state(self, cr, uid, ids, field_name=None, args=None, context=None):
"""
If account is a transfer with change, then True. Otherwise False.
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
res = {}
# Browse elements
for line in self.browse(cr, uid, ids, context=context):
res[line.id] = False
if line.account_id and line.account_id.type_for_register and line.account_id.type_for_register == 'transfer':
res[line.id] = True
return res
def _get_sequence(self, cr, uid, ids, field_name=None, args=None, context=None):
res = {}
for line in self.browse(cr, uid, ids):
if len(line.move_ids) > 0:
res[line.id] = line.move_ids[0].name
else:
res[line.id] = ''
return res
_columns = {
'register_id': fields.many2one("account.bank.statement", "Register"),
'transfer_journal_id': fields.many2one("account.journal", "Journal"),
'employee_id': fields.many2one("hr.employee", "Employee"),
'amount_in': fields.function(_get_amount, method=True, string="Amount In", type='float'),
'amount_out': fields.function(_get_amount, method=True, string="Amount Out", type='float'),
'state': fields.function(_get_state, fnct_search=_search_state, method=True, string="Status", type='selection', selection=[
('draft', 'Draft'), ('temp', 'Temp'), ('hard', 'Hard'), ('unknown', 'Unknown')]),
'partner_type': fields.function(_get_third_parties, fnct_inv=_set_third_parties, type='reference', method=True,
string="Third Parties", selection=[('res.partner', 'Partner'), ('account.journal', 'Journal'), ('hr.employee', 'Employee'),
('account.bank.statement', 'Register')], multi="third_parties_key"),
'partner_type_mandatory': fields.boolean('Third Party Mandatory'),
'reconciled': fields.function(_get_reconciled_state, fnct_search=_search_reconciled, method=True, string="Amount Reconciled",
type='boolean', store=False),
'sequence_for_reference': fields.function(_get_sequence, method=True, string="Sequence", type="char"),
'date': fields.date('Posting Date', required=True),
'document_date': fields.date(string="Document Date", required=True),
'cheque_number': fields.char(string="Cheque Number", size=120),
'from_cash_return': fields.boolean(string='Come from a cash return?'),
'direct_invoice': fields.boolean(string='Direct invoice?'),
'invoice_id': fields.many2one('account.invoice', "Invoice", required=False),
'first_move_line_id': fields.many2one('account.move.line', "Register Move Line"),
'third_parties': fields.function(_get_third_parties, type='reference', method=True,
string="Third Parties", selection=[('res.partner', 'Partner'), ('account.journal', 'Journal'), ('hr.employee', 'Employee'),
('account.bank.statement', 'Register')], help="To use for python code when registering", multi="third_parties_key"),
'imported_invoice_line_ids': fields.many2many('account.move.line', 'imported_invoice', 'st_line_id', 'move_line_id',
string="Imported Invoices", required=False, readonly=True),
'number_imported_invoice': fields.function(_get_number_imported_invoice, method=True, string='Number Invoices', type='integer'),
'is_down_payment': fields.function(_get_down_payment_state, method=True, string="Is a down payment line?",
type='boolean', store=False),
'from_import_cheque_id': fields.many2one('account.move.line', "Cheque Line",
help="This move line has been taken for create an Import Cheque in a bank register."),
'is_transfer_with_change': fields.function(_get_transfer_with_change_state, method=True, string="Is a transfer with change line?",
type='boolean', store=False),
'down_payment_id': fields.many2one('purchase.order', "Down payment", readonly=True),
'transfer_amount': fields.float(string="Amount", help="Amount used for Transfers"),
'type_for_register': fields.related('account_id','type_for_register', string="Type for register", type='selection', selection=[('none','None'),('transfer', 'Internal Transfer'), ('transfer_same','Internal Transfer (same currency)'), ('advance', 'Operational Advance'), ('payroll', 'Third party required - Payroll'), ('down_payment', 'Down payment'), ('donation', 'Donation')] )
}
_defaults = {
'from_cash_return': lambda *a: 0,
'direct_invoice': lambda *a: 0,
'transfer_amount': lambda *a: 0,
}
def create_move_from_st_line(self, cr, uid, st_line_id, company_currency_id, st_line_number, context=None):
"""
Create move from the register line
"""
# @@@override@ account.account_bank_statement.create_move_from_st_line()
if context is None:
context = {}
res_currency_obj = self.pool.get('res.currency')
account_move_obj = self.pool.get('account.move')
account_move_line_obj = self.pool.get('account.move.line')
st_line = self.browse(cr, uid, st_line_id, context=context)
st = st_line.statement_id
context.update({'date': st_line.date})
# Prepare partner_type
partner_type = False
if st_line.third_parties:
partner_type = ','.join([str(st_line.third_parties._table_name), str(st_line.third_parties.id)])
# end of add
move_id = account_move_obj.create(cr, uid, {
'journal_id': st.journal_id.id,
'period_id': st.period_id.id,
'date': st_line.date,
'name': st_line_number,
'ref': st_line.ref or False,
## Add partner_type
# 'partner_type': partner_type or False,
# end of add
}, context=context)
self.write(cr, uid, [st_line.id], {
'move_ids': [(4, move_id, False)]
})
torec = []
if st_line.amount >= 0:
account_id = st.journal_id.default_credit_account_id.id
else:
account_id = st.journal_id.default_debit_account_id.id
acc_cur = ((st_line.amount<=0) and st.journal_id.default_debit_account_id) or st_line.account_id
context.update({
'res.currency.compute.account': acc_cur,
})
amount = res_currency_obj.compute(cr, uid, st.currency.id,
company_currency_id, st_line.amount, context=context)
val = {
'name': st_line.name,
'date': st_line.date,
'document_date': st_line.document_date,
'move_id': move_id,
'partner_id': ((st_line.partner_id) and st_line.partner_id.id) or False,
# Add employee_id, register_id and partner_type support
'employee_id': ((st_line.employee_id) and st_line.employee_id.id) or False,
'register_id': ((st_line.register_id) and st_line.register_id.id) or False,
'transfer_journal_id': ((st_line.transfer_journal_id) and st_line.transfer_journal_id.id) or False,
# 'partner_type': partner_type or False,
'partner_type_mandatory': st_line.partner_type_mandatory or False,
# end of add
'account_id': (st_line.account_id) and st_line.account_id.id,
'credit': ((amount>0) and amount) or 0.0,
'debit': ((amount<0) and -amount) or 0.0,
'statement_id': st.id,
'journal_id': st.journal_id.id,
'period_id': st.period_id.id,
'currency_id': st.currency.id,
'analytic_account_id': st_line.analytic_account_id and st_line.analytic_account_id.id or False,
'transfer_amount': st_line.transfer_amount or 0.0,
}
if st_line.analytic_distribution_id:
val.update({'analytic_distribution_id': self.pool.get('analytic.distribution').copy(cr, uid,
st_line.analytic_distribution_id.id, {}, context=context) or False})
if st.currency.id <> company_currency_id:
amount_cur = res_currency_obj.compute(cr, uid, company_currency_id,
st.currency.id, amount, context=context)
val['amount_currency'] = -st_line.amount
if st_line.account_id and st_line.account_id.currency_id and st_line.account_id.currency_id.id <> company_currency_id:
val['currency_id'] = st_line.account_id.currency_id.id
amount_cur = res_currency_obj.compute(cr, uid, company_currency_id,
st_line.account_id.currency_id.id, amount, context=context)
val['amount_currency'] = -amount_cur
move_line_id = account_move_line_obj.create(cr, uid, val, context=context)
torec.append(move_line_id)
# Fill the secondary amount/currency
# if currency is not the same than the company
amount_currency = False
currency_id = False
if st.currency.id <> company_currency_id:
amount_currency = st_line.amount
currency_id = st.currency.id
# Add register_line_id variable
first_move_line_id = account_move_line_obj.create(cr, uid, {
'name': st_line.name,
'date': st_line.date,
'document_date': st_line.document_date,
'move_id': move_id,
'partner_id': ((st_line.partner_id) and st_line.partner_id.id) or False,
# Add employee_id and register_id support
'employee_id': ((st_line.employee_id) and st_line.employee_id.id) or False,
'register_id': ((st_line.register_id) and st_line.register_id.id) or False,
'transfer_journal_id': ((st_line.transfer_journal_id) and st_line.transfer_journal_id.id) or False,
# 'partner_type': partner_type or False,
'partner_type_mandatory': st_line.partner_type_mandatory or False,
# end of add
'account_id': account_id,
'credit': ((amount < 0) and -amount) or 0.0,
'debit': ((amount > 0) and amount) or 0.0,
'statement_id': st.id,
'journal_id': st.journal_id.id,
'period_id': st.period_id.id,
'amount_currency': amount_currency,
'currency_id': currency_id,
}, context=context)
for line in account_move_line_obj.browse(cr, uid, [x.id for x in
account_move_obj.browse(cr, uid, move_id,
context=context).line_id],
context=context):
if line.state <> 'valid':
raise osv.except_osv(_('Error !'),
_('Journal Item "%s" is not valid') % line.name)
# @@@end
# Removed post from original method
self.write(cr, uid, [st_line_id], {'first_move_line_id': first_move_line_id}, context=context)
return move_id
def _update_amount(self, values):
"""
Update amount in 'values' with the difference between amount_in and amount_out.
"""
res = values.copy()
amount = None
if 'amount_in' not in values and 'amount_out' not in values:
return res
if values:
amount_in = values.get('amount_in', 0.0)
amount_out = values.get('amount_out', 0.0)
if amount_in > 0 and amount_out == 0:
amount = amount_in
elif amount_in == 0 and amount_out > 0:
amount = - amount_out
else:
raise osv.except_osv(_('Error'), _('Please correct amount fields!'))
if amount:
res.update({'amount': amount})
return res
def _verify_dates(self, cr, uid, ids, context=None):
"""
Verify that the given parameter contains date. Then validate date with regarding register period.
"""
for st_line in self.browse(cr, uid, ids, context=context):
# Prepare some values
register = st_line.statement_id
account_id = st_line.account_id.id
date = st_line.date
period_start = register.period_id.date_start
period_stop = register.period_id.date_stop
# Verify that the date is included between period_start and period_stop
if date < period_start or date > period_stop:
raise osv.except_osv(_('Error'), _('The date for "%s" is outside the register period!') % (st_line.name,))
# Verify that the date is useful with default debit or credit account activable date
#+ (in fact the default debit and credit account have an activation date, and the given account_id too)
#+ That means default debit and credit account are required for registers !
register_debit_account_id = register.journal_id.default_debit_account_id.id
register_credit_account_id = register.journal_id.default_credit_account_id.id
amount = st_line.amount
acc_obj = self.pool.get('account.account')
register_account = None
if amount > 0:
register_account = acc_obj.browse(cr, uid, register_debit_account_id, context=context)
elif amount < 0:
register_account = acc_obj.browse(cr, uid, register_credit_account_id, context=context)
if register_account:
if date < register_account.activation_date or (register_account.inactivation_date and date > register_account.inactivation_date):
raise osv.except_osv(_('Error'),
_('Posting date for "%s" is outside the validity period of the default account for this register!') % (st_line.name,))
if account_id:
account = acc_obj.browse(cr, uid, account_id, context=context)
if date < account.activation_date or (account.inactivation_date and date > account.inactivation_date):
raise osv.except_osv(_('Error'),
_('Posting date for "%s" is outside the validity period of the selected account for this record!') % (st_line.name,))
return True
_constraints = [
(_verify_dates, "Date is not correct. Verify that it's in the register's period. ", ['date']),
]
def _update_move_from_st_line(self, cr, uid, st_line_id=None, values=None, context=None):
"""
Update move lines from given statement lines
"""
if not st_line_id or not values:
return False
if context is None:
context = {}
# Prepare some values
move_line_values = dict(values)
acc_move_line_obj = self.pool.get('account.move.line')
st_line = self.browse(cr, uid, st_line_id, context=context)
# Get first line (from Register account)
register_line = st_line.first_move_line_id
# Delete 'from_import_cheque_id' field not to break the account move line write
if 'from_import_cheque_id' in move_line_values:
del(move_line_values['from_import_cheque_id'])
# Delete down_payment value not to be given to account_move_line
if 'down_payment_id' in move_line_values:
del(move_line_values['down_payment_id'])
# Delete analytic distribution from move_line_values because each move line should have its own analytic distribution
if 'analytic_distribution_id' in move_line_values:
del(move_line_values['analytic_distribution_id'])
if register_line:
# Search second move line
other_line_id = acc_move_line_obj.search(cr, uid, [('move_id', '=', st_line.move_ids[0].id), ('id', '!=', register_line.id)], context=context)[0]
other_line = acc_move_line_obj.browse(cr, uid, other_line_id, context=context)
other_account_id = move_line_values.get('account_id', other_line.account_id.id)
amount = move_line_values.get('amount', st_line.amount)
# Search all data for move lines
register_account_id = st_line.statement_id.journal_id.default_debit_account_id.id
if amount < 0:
register_account_id = st_line.statement_id.journal_id.default_credit_account_id.id
register_debit = 0.0
register_credit = abs(amount)
other_debit = abs(amount)
other_credit = 0.0
else:
register_debit = amount
register_credit = 0.0
other_debit = 0.0
other_credit = amount
# What's about register currency ?
register_amount_currency = False
other_amount_currency = False
currency_id = st_line.statement_id.currency.id
if st_line.statement_id.currency.id != st_line.statement_id.company_id.currency_id.id:
# Prepare value
res_currency_obj = self.pool.get('res.currency')
# Get date for having a good change rate
context.update({'date': move_line_values.get('date', st_line.date)})
# Change amount
new_amount = res_currency_obj.compute(cr, uid, \
st_line.statement_id.journal_id.currency.id, st_line.company_id.currency_id.id, abs(amount), round=False, context=context)
# Take currency for the move lines
currency_id = st_line.statement_id.journal_id.currency.id
# Default amount currency
register_amount_currency = False
if amount < 0:
register_amount_currency = -abs(amount)
register_debit = 0.0
register_credit = new_amount
other_debit = new_amount
other_credit = 0.0
else:
register_amount_currency = abs(amount)
register_debit = new_amount
register_credit = 0.0
other_debit = 0.0
other_credit = new_amount
# Amount currency for "other line" is the opposite of "register line"
other_amount_currency = -register_amount_currency
# Update values for register line
for el in ['is_transfer_with_change', 'transfer_amount']:
if el in move_line_values:
del(move_line_values[el])
move_line_values.update({'account_id': register_account_id, 'debit': register_debit, 'credit': register_credit,
'amount_currency': register_amount_currency, 'currency_id': currency_id,})
# Write move line object for register line
acc_move_line_obj.write(cr, uid, [register_line.id], move_line_values, context=context)
# Update values for other line
move_line_values.update({'account_id': other_account_id, 'debit': other_debit, 'credit': other_credit, 'amount_currency': other_amount_currency,
'currency_id': currency_id,})
if st_line.is_transfer_with_change:
move_line_values.update({'is_transfer_with_change': True})
if st_line.transfer_amount:
move_line_values.update({'transfer_amount': st_line.transfer_amount or 0.0})
# Write move line object for other line
acc_move_line_obj.write(cr, uid, [other_line.id], move_line_values, context=context)
# Update analytic distribution lines
analytic_amount = acc_move_line_obj.read(cr, uid, [other_line.id], ['amount_currency'], context=context)[0].get('amount_currency', False)
if analytic_amount:
self.pool.get('analytic.distribution').update_distribution_line_amount(cr, uid, [st_line.analytic_distribution_id.id],
amount=analytic_amount, context=context)
# Update move
# first prepare partner_type
partner_type = False
if st_line.third_parties:
partner_type = ','.join([str(st_line.third_parties._table_name), str(st_line.third_parties.id)])
# finally write move object
self.pool.get('account.move').write(cr, uid, [register_line.move_id.id], {'partner_type': partner_type}, context=context)
return True
def do_direct_expense(self, cr, uid, ids, context=None):
"""
Do a direct expense when the line is hard posted and content a supplier
"""
if context is None:
context = {}
for st_line in self.browse(cr, uid, ids, context=context):
# Do the treatment only if the line is hard posted and have a partner who is a supplier
if st_line.state == "hard" and st_line.partner_id and st_line.account_id.user_type.code in ('expense', 'income') and \
st_line.direct_invoice is False:
# Prepare some elements
move_obj = self.pool.get('account.move')
move_line_obj = self.pool.get('account.move.line')
curr_date = time.strftime('%Y-%m-%d')
# Create a move
move_vals= {
'journal_id': st_line.statement_id.journal_id.id,
'period_id': st_line.statement_id.period_id.id,
'date': st_line.document_date or st_line.date or curr_date,
'name': 'DirectExpense/' + st_line.name,
'partner_id': st_line.partner_id.id,
}
move_id = move_obj.create(cr, uid, move_vals, context=context)
# Create move lines
account_id = False
if st_line.account_id.user_type.code == 'expense':
account_id = st_line.partner_id.property_account_payable.id or False
elif st_line.account_id.user_type.code == 'income':
account_id = st_line.partner_id.property_account_receivable.id or False
if not account_id:
raise osv.except_osv(_('Warning'), _('The supplier seems not to have a correct account. \
Please contact an accountant administrator to resolve this problem.'))
val = {
'name': st_line.name,
'date': st_line.date or curr_date,
'document_date': st_line.document_date or curr_date,
'ref': st_line.ref,
'move_id': move_id,
'partner_id': st_line.partner_id.id or False,
'partner_type_mandatory': True,
'account_id': account_id,
'credit': 0.0,
'debit': 0.0,
'statement_id': st_line.statement_id.id,
'journal_id': st_line.statement_id.journal_id.id,
'period_id': st_line.statement_id.period_id.id,
'currency_id': st_line.statement_id.currency.id,
'analytic_account_id': st_line.analytic_account_id and st_line.analytic_account_id.id or False
}
amount = abs(st_line.amount)
# update values if we have a different currency that company currency
if st_line.statement_id.currency.id != st_line.statement_id.company_id.currency_id.id:
context['date'] = st_line.document_date or st_line.date or curr_date
amount = self.pool.get('res.currency').compute(cr, uid, st_line.statement_id.currency.id,
st_line.statement_id.company_id.currency_id.id, amount, round=False, context=context)
val.update({'debit': amount, 'credit': 0.0, 'amount_currency': abs(st_line.amount)})
move_line_debit_id = move_line_obj.create(cr, uid, val, context=context)
val.update({'debit': 0.0, 'credit': amount, 'amount_currency': -abs(st_line.amount)})
move_line_credit_id = move_line_obj.create(cr, uid, val, context=context)
# Post the move
move_res_id = move_obj.post(cr, uid, [move_id], context=context)
# Do reconciliation
move_line_res_id = move_line_obj.reconcile_partial(cr, uid, [move_line_debit_id, move_line_credit_id])
# Disable the cash return button on this line
self.write(cr, uid, [st_line.id], {'from_cash_return': True}, context=context)
return True
def do_import_invoices_reconciliation(self, cr, uid, st_lines=None, context=None):
"""
Reconcile line that come from an import invoices wizard in 3 steps :
- split line into the move
- post move
- reconcile lines from the move
"""
# Some verifications
if not context:
context = {}
if not st_lines or isinstance(st_lines, (int, long)):
st_lines = []
# Prepare some values
absl_obj = self.pool.get('account.bank.statement.line')
move_line_obj = self.pool.get('account.move.line')
move_obj = self.pool.get('account.move')
# Parse register lines
for st_line in absl_obj.browse(cr, uid, st_lines, context=context):
# Verification if st_line have some imported invoice lines
if not st_line.imported_invoice_line_ids:
continue
total_amount = 0
for invoice_move_line in st_line.imported_invoice_line_ids:
total_amount += invoice_move_line.amount_currency
## STEP 1 : Split lines
# Prepate some values
move_ids = [x.id for x in st_line.move_ids]
# Search move lines that are attached to move_ids
move_lines = move_line_obj.search(cr, uid, [('move_id', 'in', move_ids),
('id', '!=', st_line.first_move_line_id.id)]) # move lines that have been created AFTER import invoice wizard
# Add new lines
amount = abs(st_line.first_move_line_id.amount_currency)
sign = 1
if st_line.first_move_line_id.amount_currency > 0:
sign = -1
res_ml_ids = []
process_invoice_move_line_ids = []
total_payment = True
if st_line.first_move_line_id.amount_currency != total_amount:
# multi unpartial payment
total_payment = False
# Delete them
move_line_obj.unlink(cr, uid, move_lines, context=context)
for invoice_move_line in sorted(st_line.imported_invoice_line_ids, key=lambda x: abs(x.amount_currency)):
if abs(invoice_move_line.amount_currency) <= amount:
amount_to_write = sign * abs(invoice_move_line.amount_currency)
else:
amount_to_write = sign * amount
# create a new move_line corresponding to this invoice move line
aml_vals = {
'name': invoice_move_line.invoice.number,
'move_id': move_ids[0],
'partner_id': invoice_move_line.partner_id.id,
'account_id': st_line.account_id.id,
'amount_currency': amount_to_write,
'statement_id': st_line.statement_id.id,
'currency_id': st_line.statement_id.currency.id,
'from_import_invoice_ml_id': invoice_move_line.id, # FIXME: add this ONLY IF total amount was paid
'date': st_line.date,
'document_date': st_line.document_date,
}
process_invoice_move_line_ids.append(invoice_move_line.id)
move_line_id = move_line_obj.create(cr, uid, aml_vals, context=context)
res_ml_ids.append(move_line_id)
amount -= abs(amount_to_write)
if not amount:
todo = [x.id for x in st_line.imported_invoice_line_ids if x.id not in process_invoice_move_line_ids]
# remove remaining invoice lines
if todo:
absl_obj.write(cr, uid, [st_line.id], {'imported_invoice_line_ids': [(3, x) for x in todo]}, context=context)
break
# STEP 2 : Post moves
move_obj.post(cr, uid, move_ids, context=context)
# STEP 3 : Reconcile
if total_payment:
move_line_obj.reconcile_partial(cr, uid, move_lines+[x.id for x in st_line.imported_invoice_line_ids], context=context)
else:
for ml in move_line_obj.browse(cr, uid, res_ml_ids, context=context):
# reconcile lines
move_line_obj.reconcile_partial(cr, uid, [ml.id, ml.from_import_invoice_ml_id.id], context=context)
return True
def do_import_cheque_reconciliation(self, cr, uid, st_lines=None, context=None):
"""
Do a reconciliation of an imported cheque and the current register line
"""
# Some verifications
if not context:
context = {}
if not st_lines or isinstance(st_lines, (int, long)):
st_lines = []
# Prepare some values
move_obj = self.pool.get('account.move')
move_line_obj = self.pool.get('account.move.line')
# Parse register lines
for st_line in self.browse(cr, uid, st_lines, context=context):
# Verification if st_line have some imported invoice lines
if not st_line.from_import_cheque_id:
continue
move_obj.post(cr, uid, [st_line.move_ids[0].id], context=context)
# Search the line that would be reconcile after hard post
move_line_id = move_line_obj.search(cr, uid, [('move_id', '=', st_line.move_ids[0].id), ('id', '!=', st_line.first_move_line_id.id)],
context=context)
# Do reconciliation
move_line_obj.reconcile_partial(cr, uid, [st_line.from_import_cheque_id.id, move_line_id[0]], context=context)
return True
def analytic_distribution_is_mandatory(self, cr, uid, id, context=None):
"""
Verify that no analytic distribution is mandatory. It's not until one of test is true
"""
# Some verifications
if isinstance(id, (list)):
id = id[0]
if not context:
context = {}
# Tests
absl = self.browse(cr, uid, id, context=context)
if absl.account_id.user_type.code in ['expense'] and not absl.analytic_distribution_id:
return True
elif absl.account_id.user_type.code in ['expense'] and not absl.analytic_distribution_id.funding_pool_lines:
return True
return False
def create_down_payment_link(self, cr, uid, ids, context=None):
"""
Copy down_payment link to right move line
"""
# some verifications
if isinstance(ids, (int, long)):
ids = [ids]
# browse all bank statement line
for absl in self.browse(cr, uid, ids):
if not absl.is_down_payment:
continue
move_ids = [x.id or None for x in absl.move_ids]
# Search line that have same account for given register line
line_ids = self.pool.get('account.move.line').search(cr, uid, [('account_id', '=', absl.account_id.id), ('move_id', 'in', move_ids)])
# Add down_payment link
for line_id in line_ids:
self.pool.get('account.move.line').write(cr, uid, [line_id], {'down_payment_id': absl.down_payment_id.id})
return True
def create(self, cr, uid, values, context=None):
"""
Create a new account bank statement line with values
"""
# First update amount
values = self._update_amount(values=values)
# Then create a new bank statement line
return super(account_bank_statement_line, self).create(cr, uid, values, context=context)
def write(self, cr, uid, ids, values, context=None):
"""
Write some existing account bank statement lines with 'values'.
"""
if isinstance(ids, (int, long)):
ids = [ids]
if context is None:
context = {}
# Prepare some values
state = self._get_state(cr, uid, ids, context=context).values()[0]
# Verify that the statement line isn't in hard state
if state == 'hard':
if values == {'from_cash_return': True} or values.get('analytic_distribution_id', False) or (values.get('invoice_id', False) and len(values.keys()) == 2 and values.get('from_cash_return')) or 'from_correction' in context:
return super(account_bank_statement_line, self).write(cr, uid, ids, values, context=context)
raise osv.except_osv(_('Warning'), _('You cannot write a hard posted entry.'))
# First update amount
values = self._update_amount(values=values)
# Case where _update_amount return False ! => this imply there is a problem with amount columns
if not values:
return False
# In case of Temp Posting, we also update attached account move lines
if state == 'temp':
# method write removes date in value: save it, then restore it
saveddate = False
if values.get('date'):
saveddate = values['date']
for id in ids:
self._update_move_from_st_line(cr, uid, id, values, context=context)
if saveddate:
values['date'] = saveddate
# Update the bank statement lines with 'values'
res = super(account_bank_statement_line, self).write(cr, uid, ids, values, context=context)
# Amount verification regarding Down payments
for line in self.browse(cr, uid, ids):
if line.is_down_payment and line.down_payment_id:
if not self.pool.get('wizard.down.payment').check_register_line_and_po(cr, uid, line.id, line.down_payment_id.id, context=context):
raise osv.except_osv(_('Warning'), _('An error occured on down_payment check. Please contact an administrator to resolve this problem.'))
return res
def copy(self, cr, uid, id, default=None, context=None):
"""
Create a copy of given line
"""
# Some verifications
if not context:
context = {}
if not default:
default = {}
# Update vals
default.update({
'analytic_account_id': False,
'analytic_distribution_id': False,
'direct_invoice': False,
'first_move_line_id': False,
'from_cash_return': False,
'from_import_cheque_id': False,
'imported_invoice_line_ids': False,
'invoice_id': False,
'move_ids': False,
'reconciled': False,
'sequence': False,
'sequence_for_reference': False,
'state': 'draft',
'transfer_amount': False,
'transfer_currency': False,
'down_payment_id': False,
})
return super(osv.osv, self).copy(cr, uid, id, default, context=context)
def update_analytic_lines(self, cr, uid, ids, distrib=False, context=None):
"""
Update analytic lines for temp posted lines
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
ml_obj = self.pool.get('account.move.line')
distro_obj = self.pool.get('analytic.distribution')
aal_obj = self.pool.get('account.analytic.line')
for absl in self.browse(cr, uid, ids):
if absl.state != 'temp':
continue
# Search all moves lines linked to this register line
move_ids = [x.id for x in absl.move_ids]
move_line_ids = ml_obj.search(cr, uid, [('move_id', 'in', move_ids)])
# Renew analytic lines
for line in ml_obj.browse(cr, uid, move_line_ids):
if line.analytic_distribution_id:
# remove distribution
distro_obj.unlink(cr, uid, line.analytic_distribution_id.id)
distrib_id = distrib or (absl.analytic_distribution_id and absl.analytic_distribution_id.id) or False
if not distrib_id:
raise osv.except_osv(_('Error'), _('Problem with analytic distribution for this line: %s') % (absl.name or '',))
new_distrib_id = distro_obj.copy(cr, uid, distrib_id, {})
ml_obj.write(cr, uid, [line.id], {'analytic_distribution_id': new_distrib_id})
aal_ids = aal_obj.search(cr, uid, [('move_id', 'in', move_line_ids)], context=context)
# first delete them
aal_obj.unlink(cr, uid, aal_ids)
# then create them again
ml_obj.create_analytic_lines(cr, uid, move_line_ids)
return True
def posting(self, cr, uid, ids, postype, context=None):
"""
Write some statement line into some account move lines with a state that depends on postype.
"""
if not context:
context = {}
if postype not in ('hard', 'temp'):
raise osv.except_osv(_('Warning'), _('Post type has to be hard or temp'))
if not len(ids):
raise osv.except_osv(_('Warning'), _('There is no active_id. Please contact an administrator to resolve the problem.'))
acc_move_obj = self.pool.get("account.move")
# browse all statement lines for creating move lines
for absl in self.browse(cr, uid, ids, context=context):
if absl.state == "hard":
raise osv.except_osv(_('Warning'), _('You can\'t re-post a hard posted entry !'))
elif absl.state == "temp" and postype == "temp":
raise osv.except_osv(_('Warning'), _('You can\'t temp re-post a temp posted entry !'))
# Analytic distribution
# Check analytic distribution presence
if self.analytic_distribution_is_mandatory(cr, uid, absl.id, context=context) and not context.get('from_yml'):
raise osv.except_osv(_('Error'), _('Analytic distribution is mandatory for this line: %s') % (absl.name or '',))
# Check analytic distribution validity
if absl.account_id.user_type.code in ['expense'] and absl.analytic_distribution_state != 'valid' and not context.get('from_yml'):
raise osv.except_osv(_('Error'), _('Analytic distribution is not valid for this line: %s') % (absl.name or '',))
if absl.state == "draft":
self.create_move_from_st_line(cr, uid, absl.id, absl.statement_id.journal_id.company_id.currency_id.id, '/', context=context)
# reset absl browse_record cache, because move_ids have been created by create_move_from_st_line
absl = self.browse(cr, uid, absl.id, context=context)
if postype == "hard":
# Update analytic lines
if absl.account_id.user_type.code in ['expense']:
self.update_analytic_lines(cr, uid, absl.id)
# some verifications
if absl.is_transfer_with_change:
if not absl.transfer_journal_id:
raise osv.except_osv(_('Warning'), _('Third party is required in order to hard post a transfer with change register line!'))
if absl.is_down_payment and not absl.down_payment_id:
raise osv.except_osv(_('Error'), _('Link with a PO for Down Payment is missing!'))
elif absl.is_down_payment:
self.pool.get('wizard.down.payment').check_register_line_and_po(cr, uid, absl.id, absl.down_payment_id.id, context=context)
self.create_down_payment_link(cr, uid, absl.id, context=context)
seq = self.pool.get('ir.sequence').get(cr, uid, 'all.registers')
self.write(cr, uid, [absl.id], {'sequence_for_reference': seq}, context=context)
# Case where this line come from an "Import Invoices" Wizard
if absl.imported_invoice_line_ids:
self.do_import_invoices_reconciliation(cr, uid, [absl.id], context=context)
elif absl.from_import_cheque_id:
self.do_import_cheque_reconciliation(cr, uid, [absl.id], context=context)
else:
acc_move_obj.post(cr, uid, [x.id for x in absl.move_ids], context=context)
# do a move that enable a complete supplier follow-up
self.do_direct_expense(cr, uid, [absl.id], context=context)
return True
def button_hard_posting(self, cr, uid, ids, context=None):
"""
Write some statement line into some account move lines in posted state.
"""
return self.posting(cr, uid, ids, 'hard', context=context)
def button_temp_posting(self, cr, uid, ids, context=None):
"""
Write some statement lines into some account move lines in draft state.
"""
return self.posting(cr, uid, ids, 'temp', context=context)
def unlink(self, cr, uid, ids, context=None):
"""
Permit to delete some account_bank_statement_line. But do some treatments on temp posting lines and do nothing for hard posting lines.
"""
# We browse all ids
for st_line in self.browse(cr, uid, ids):
# if the line have a link to a move we have to make some treatments
if st_line.move_ids:
# in case of hard posting line : do nothing (because not allowed to change an entry which was posted!
if st_line.state == "hard":
raise osv.except_osv(_('Error'), _('You are not allowed to delete hard posting lines!'))
else:
# In case of line that content some move_line that come from imported invoices
# delete link between account_move_line and register_line that will be unlinked
#if st_line.imported_invoice_line_ids:
# self.pool.get('account.move.line').write(cr, uid, [x['id'] for x in st_line.imported_invoice_line_ids],
# {'imported_invoice_line_ids': (3, st_line.id, False)}, context=context)
self.pool.get('account.move').unlink(cr, uid, [x.id for x in st_line.move_ids])
return super(account_bank_statement_line, self).unlink(cr, uid, ids)
def button_advance(self, cr, uid, ids, context=None):
"""
Launch a wizard when you press "Advance return" button on a bank statement line in a Cash Register
"""
if context is None:
context = {}
# Some verifications
if len(ids) > 1:
raise osv.except_osv(_('Error'), _('This wizard only accept ONE advance line.'))
# others verifications
for st_line in self.browse(cr, uid, ids, context=context):
# verify that the journal id is a cash journal
if not st_line.statement_id or not st_line.statement_id.journal_id or not st_line.statement_id.journal_id.type \
or st_line.statement_id.journal_id.type != 'cash':
raise osv.except_osv(_('Error'), _("The attached journal is not a Cash Journal"))
# verify that there is a third party, particularly an employee_id in order to do something
if not st_line.employee_id:
raise osv.except_osv(_('Error'), _("The staff field is not filled in. Please complete the third parties field with an employee/staff."))
# then display the wizard with an active_id = cash_register_id, and giving in the context a number of the bank statement line
st_obj = self.pool.get('account.bank.statement.line')
st = st_obj.browse(cr, uid, ids[0]).statement_id
if 'open_advance' in context:
st = self.pool.get('account.bank.statement').browse(cr, uid, context.get('open_advance'), context=context)
if st and st.state != 'open':
raise osv.except_osv(_('Error'), _('You cannot do a cash return in Register which is in another state that "open"!'))
statement_id = st.id
amount = self.read(cr, uid, ids[0], ['amount']).get('amount', 0.0)
if amount >= 0:
raise osv.except_osv(_('Warning'), _('Please select a line with a filled out "amount out"!'))
wiz_obj = self.pool.get('wizard.cash.return')
wiz_id = wiz_obj.create(cr, uid, {'returned_amount': 0.0, 'initial_amount': abs(amount), 'advance_st_line_id': ids[0], \
'currency_id': st_line.statement_id.currency.id}, context=context)
context.update({
'active_id': ids[0],
'active_ids': ids,
'statement_line_id': ids[0],
'statement_id': statement_id,
'amount': amount
})
if statement_id:
return {
'name' : "Advance Return",
'type' : 'ir.actions.act_window',
'res_model' :"wizard.cash.return",
'target': 'new',
'view_mode': 'form',
'view_type': 'form',
'res_id': [wiz_id],
'context': context,
}
else:
return False
def button_open_invoices(self, cr, uid, ids, context=None):
l = self.read(cr, uid, ids, ['imported_invoice_line_ids'])[0]
if not l['imported_invoice_line_ids']:
raise osv.except_osv(_('Error'), _("No related invoice line"))
return {
'name': "Invoice Lines",
'type': 'ir.actions.act_window',
'res_model': 'account.move.line',
'target': 'new',
'view_mode': 'tree,form',
'view_type': 'form',
'domain': [('id', 'in', l['imported_invoice_line_ids'])]
}
def button_open_invoice(self, cr, uid, ids, context=None):
"""
Open the attached invoice
"""
for st_line in self.browse(cr, uid, ids, context=context):
if not st_line.direct_invoice or not st_line.invoice_id:
raise osv.except_osv(_('Warning'), _('No invoice founded.'))
# Search the customized view we made for Supplier Invoice (for * Register's users)
irmd_obj = self.pool.get('ir.model.data')
view_ids = irmd_obj.search(cr, uid, [('name', '=', 'invoice_supplier_form'), ('model', '=', 'ir.ui.view')])
# Préparation de l'élément permettant de trouver la vue à afficher
if view_ids:
view = irmd_obj.read(cr, uid, view_ids[0])
view_id = (view.get('res_id'), view.get('name'))
else:
raise osv.except_osv(_('Error'), _("View not found."))
return {
'name': "Supplier Direct Invoice",
'type': 'ir.actions.act_window',
'res_model': 'account.invoice',
'target': 'new',
'view_mode': 'form',
'view_type': 'form',
'view_id': view_id,
'res_id': self.browse(cr, uid, ids[0], context=context).invoice_id.id,
'context':
{
'active_id': ids[0],
'type': 'in_invoice',
'active_ids': ids,
}
}
def button_duplicate(self, cr, uid, ids, context=None):
"""
Copy given lines and delete all links
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Browse lines
for line in self.browse(cr, uid, ids, context=context):
if line.statement_id and line.statement_id.state != 'open':
raise osv.except_osv(_('Warning'), _("Register not open, you can't duplicate lines."))
default_vals = ({
'name': '(copy) ' + line.name,
})
self.copy(cr, uid, line.id, default_vals, context=context)
return True
def button_down_payment(self, cr, uid, ids, context=None):
"""
Open Down Payment wizard
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
vals = {}
register_line = self.browse(cr, uid, ids[0], context=context)
vals.update({'register_line_id': register_line.id})
if register_line and register_line.down_payment_id:
vals.update({'purchase_id': register_line.down_payment_id.id})
if register_line and register_line.state and register_line.state != 'hard':
vals.update({'state': 'draft'})
if register_line and register_line.currency_id:
vals.update({'currency_id': register_line.currency_id.id})
if register_line and register_line.partner_id:
vals.update({'partner_id': register_line.partner_id.id})
wiz_id = self.pool.get('wizard.down.payment').create(cr, uid, vals, context=context)
# Return view with register_line id
context.update({
'active_id': wiz_id,
'active_ids': [wiz_id],
'register_line_id': ids[0],
})
return {
'name': _("Down Payment"),
'type': 'ir.actions.act_window',
'res_model': 'wizard.down.payment',
'target': 'new',
'res_id': [wiz_id],
'view_mode': 'form',
'view_type': 'form',
'context': context,
}
def button_transfer(self, cr, uid, ids, context=None):
"""
Open Transfer with change wizard
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
vals = {}
absl = self.browse(cr, uid, ids[0], context=context)
if absl.account_id and absl.account_id.type_for_register and absl.account_id.type_for_register != 'transfer':
raise osv.except_osv(_('Error'), _('Open transfer with change wizard is only possible with transfer account in other currency!'))
# Create wizard
vals = {'absl_id': ids[0],}
transfer_type = 'to'
amount_field = 'amount_to'
curr_field = 'currency_to'
if absl and absl.amount:
if absl.amount >= 0:
transfer_type = 'from'
amount_field = 'amount_from'
curr_field = 'currency_from'
if absl and absl.transfer_amount:
vals.update({amount_field: absl.transfer_amount,})
elif absl and absl.transfer_journal_id:
vals.update({'currency_id': absl.transfer_journal_id.currency.id, curr_field: absl.transfer_journal_id.currency.id})
if absl and absl.state == 'hard':
vals.update({'state': 'closed',})
vals.update({'type': transfer_type,})
wiz_id = self.pool.get('wizard.transfer.with.change').create(cr, uid, vals, context=context)
# Return view with register_line id
context.update({
'active_id': wiz_id,
'active_ids': [wiz_id],
'register_line_id': ids[0],
})
return {
'name': _("Transfer with change"),
'type': 'ir.actions.act_window',
'res_model': 'wizard.transfer.with.change',
'target': 'new',
'res_id': [wiz_id],
'view_mode': 'form',
'view_type': 'form',
'context': context,
}
def onchange_account(self, cr, uid, ids, account_id=None, statement_id=None, context=None):
"""
Update Third Party type regarding account type_for_register field.
"""
# Prepare some values
acc_obj = self.pool.get('account.account')
third_type = [('res.partner', 'Partner'), ('hr.employee', 'Employee')]
third_required = False
third_selection = 'res.partner,0'
# if an account is given, then attempting to change third_type and information about the third required
if account_id:
a = acc_obj.read(cr, uid, account_id, ['type_for_register'])
if a['type_for_register'] in ['transfer', 'transfer_same']:
# UF-428: transfer type shows only Journals instead of Registers as before
third_type = [('account.journal', 'Journal')]
third_required = True
third_selection = 'account.journal,0'
elif a['type_for_register'] == 'advance':
third_type = [('hr.employee', 'Employee')]
third_required = True
third_selection = 'hr.employee,0'
elif a['type_for_register'] == 'down_payment':
third_type = [('res.partner', 'Partner')]
third_required = True
third_selection = 'res.partner,0'
return {'value': {'partner_type_mandatory': third_required, 'partner_type': {'options': third_type, 'selection': third_selection}}}
def onchange_partner_type(self, cr, uid, ids, partner_type=None, amount_in=None, amount_out=None, context=None):
"""
Update account_id field if partner_type change
NB:
- amount_out = debit
- amount_in = credit
"""
res = {}
amount = (amount_in or 0.0) - (amount_out or 0.0)
if not partner_type and not amount:
return res
if partner_type:
partner_data = partner_type.split(",")
if partner_data:
obj = partner_data[0]
id = int(partner_data[1])
# Case where the partner_type is res.partner
if obj == 'res.partner':
# if amount is inferior to 0, then we give the account_payable
res_account = None
if amount < 0:
res_account = self.pool.get('res.partner').read(cr, uid, [id],
['property_account_payable'], context=context)[0].get('property_account_payable', False)
elif amount > 0:
res_account = self.pool.get('res.partner').read(cr, uid, [id],
['property_account_receivable'], context=context)[0].get('property_account_receivable', False)
if res_account:
res['value'] = {'account_id': res_account[0]}
# # Case where the partner_type is account.bank.statement
# if obj == 'account.bank.statement':
# # if amount is inferior to 0, then we give the debit account
# register = self.pool.get('account.bank.statement').browse(cr, uid, [id], context=context)
# account_id = False
# if register and amount < 0:
# account_id = register[0].journal_id.default_debit_account_id.id
# elif register and amount > 0:
# account_id = register[0].journal_id.default_credit_account_id.id
# if account_id:
# res['value'] = {'account_id': account_id}
return res
account_bank_statement_line()
class ir_values(osv.osv):
_name = 'ir.values'
_inherit = 'ir.values'
def get(self, cr, uid, key, key2, models, meta=False, context=None, res_id_req=False, without_user=True, key2_req=True):
if context is None:
context = {}
values = super(ir_values, self).get(cr, uid, key, key2, models, meta, context, res_id_req, without_user, key2_req)
if context.get('type_posting') and key == 'action' and key2 == 'client_action_multi' and 'account.bank.statement.line' in [x[0] for x in models]:
new_act = []
for v in values:
if v[1] != 'act_wizard_temp_posting' and context['type_posting'] == 'hard' or v[1] != 'act_wizard_hard_posting' and context['type_posting'] == 'temp':
new_act.append(v)
values = new_act
elif context.get('journal_type') and key == 'action' and key2 == 'client_print_multi' and 'account.bank.statement' in [x[0] for x in models]:
new_act = []
for v in values:
if v[1] == 'Bank Reconciliation' and context['journal_type'] == 'bank' \
or v[1] == 'Cash Inventory' and context['journal_type'] == 'cash' \
or v[1] == 'Open Advances' and context['journal_type'] == 'cash' \
or v[1] == 'Cheque Inventory' and context['journal_type'] == 'cheque' \
or v[1] == 'Liquidity Position' and context['journal_type'] != 'cheque':
new_act.append(v)
values = new_act
return values
ir_values()
|