~unifield-team/unifield-wm/us-826

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
#!/usr/bin/env python
#-*- encoding:utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2011 TeMPO Consulting, MSF. All Rights 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 time import strftime
from tools.translate import _
from lxml import etree
import re
import netsvc


import decimal_precision as dp

class account_invoice(osv.osv):
    _name = 'account.invoice'
    _inherit = 'account.invoice'

    def _get_invoice_report_name(self, cr, uid, ids, context=None):
        '''
        Returns the name of the invoice according to its type
        '''
        if isinstance(ids, list):
            ids = ids[0]

        inv = self.browse(cr, uid, ids, context=context)
        inv_name = inv.number or inv.name or 'No_description'
        prefix = 'STV_'

        if inv.type == 'out_refund': # Customer refund
            prefix = 'CR_'
        elif inv.type == 'in_refund': # Supplier refund
            prefix = 'SR_'
        elif inv.type == 'out_invoice':
            # Stock transfer voucher
            prefix = 'STV_'
            # Debit note
            if inv.is_debit_note and not inv.is_inkind_donation and not inv.is_intermission:
                prefix = 'DN_'
            # Intermission voucher OUT
            elif not inv.is_debit_note and not inv.is_inkind_donation and inv.is_intermission:
                prefix = 'IMO_'
        elif inv.type == 'in_invoice':
            # Supplier invoice
            prefix = 'SI_'
            # Intermission voucher IN
            if not inv.is_debit_note and not inv.is_inkind_donation and inv.is_intermission:
                prefix = 'IMI_'
            # Direct invoice
            elif inv.is_direct_invoice:
                prefix = 'DI_'
            # In-kind donation
            elif not inv.is_debit_note and inv.is_inkind_donation:
                prefix = 'DON_'
        return '%s%s' % (prefix, inv_name)

    def _get_journal(self, cr, uid, context=None):
        """
        WARNING: This method has been taken from account module from OpenERP
        """
        # @@@override@account.invoice.py
        if context is None:
            context = {}
        type_inv = context.get('type', 'out_invoice')
        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
        company_id = context.get('company_id', user.company_id.id)
        type2journal = {'out_invoice': 'sale', 'in_invoice': 'purchase', 'out_refund': 'sale_refund', 'in_refund': 'purchase_refund'}
        refund_journal = {'out_invoice': False, 'in_invoice': False, 'out_refund': True, 'in_refund': True}
        args = [('type', '=', type2journal.get(type_inv, 'sale')),
                ('company_id', '=', company_id),
                ('refund_journal', '=', refund_journal.get(type_inv, False))]
        if user.company_id.instance_id:
            args.append(('is_current_instance','=',True))
        journal_obj = self.pool.get('account.journal')
        res = journal_obj.search(cr, uid, args, limit=1)
        return res and res[0] or False

    def _get_fake(self, cr, uid, ids, field_name=None, arg=None, context=None):
        """
        Fake method for 'ready_for_import_in_debit_note' field
        """
        res = {}
        for i in ids:
            res[i] = False
        return res

    def _search_ready_for_import_in_debit_note(self, cr, uid, obj, name, args, context=None):
        if not args:
            return []
        account_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.import_invoice_default_account and \
            self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.import_invoice_default_account.id or False
        if not account_id:
            raise osv.except_osv(_('Error'), _('No default account for import invoice on Debit Note!'))
        dom1 = [
            ('account_id','=',account_id),
            ('reconciled','=',False),
            ('state', '=', 'open'),
            ('type', '=', 'out_invoice'),
            ('journal_id.type', 'not in', ['migration']),
            ('partner_id.partner_type', '=', 'section'),
        ]
        return dom1+[('is_debit_note', '=', False)]

    def _get_fake_m2o_id(self, cr, uid, ids, field_name=None, arg=None, context=None):
        """
        Get many2one field content
        """
        res = {}
        name = field_name.replace("fake_", '')
        for i in self.browse(cr, uid, ids):
            if context and context.get('is_intermission', False):
                res[i.id] = False
                if name == 'account_id':
                    user = self.pool.get('res.users').browse(cr, uid, [uid], context=context)
                    if user[0].company_id.intermission_default_counterpart:
                        res[i.id] = user[0].company_id.intermission_default_counterpart.id
                elif name == 'journal_id':
                    int_journal_id = self.pool.get('account.journal').search(cr, uid, [('type', '=', 'intermission')], context=context)
                    if int_journal_id:
                        if isinstance(int_journal_id, (int, long)):
                            int_journal_id = [int_journal_id]
                        res[i.id] = int_journal_id[0]
                elif name == 'currency_id':
                    user = self.pool.get('res.users').browse(cr, uid, [uid], context=context)
                    if user[0].company_id.currency_id:
                        res[i.id] = user[0].company_id.currency_id.id
            else:
                res[i.id] = getattr(i, name, False) and getattr(getattr(i, name, False), 'id', False) or False
        return res

    def _get_have_donation_certificate(self, cr, uid, ids, field_name=None, arg=None, context=None):
        """
        If this invoice have a stock picking in which there is a Certificate of Donation, return True. Otherwise return False.
        """
        res = {}
        for i in self.browse(cr, uid, ids):
            res[i.id] = False
            if i.picking_id:
                a_ids = self.pool.get('ir.attachment').search(cr, uid, [('res_model', '=', 'stock.picking'), ('res_id', '=', i.picking_id.id), ('description', '=', 'Certificate of Donation')])
                if a_ids:
                    res[i.id] = True
        return res

    def _get_virtual_fields(self, cr, uid, ids, field_name=None, arg=None, context=None):
        """
        Get fields in order to transform them into 'virtual fields" (kind of field duplicity):
         - currency_id
         - account_id
         - supplier
        """
        res = {}
        for inv in self.browse(cr, uid, ids, context=context):
            res[inv.id] = {'virtual_currency_id': inv.currency_id.id or False, 'virtual_account_id': inv.account_id.id or False,
            'virtual_partner_id': inv.partner_id.id or False}
        return res

    def _get_vat_ok(self, cr, uid, ids, field_name, args, context=None):
        '''
        Return True if the system configuration VAT management is set to True
        '''
        vat_ok = self.pool.get('unifield.setup.configuration').get_config(cr, uid).vat_ok
        res = {}
        for id in ids:
            res[id] = vat_ok

        return res

    def _get_can_merge_lines(self, cr, uid, ids, field_name, args,
        context=None):
        res = {}
        if not ids:
            return res
        if isinstance(ids, (int, long, )):
            ids = [ids]

        for inv_br in self.browse(cr, uid, ids, context=context):
            # US-357: allow merge of line only for draft SI
            res[inv_br.id] = inv_br.state and inv_br.state == 'draft' \
                and inv_br.invoice_line \
                and inv_br.type == 'in_invoice' \
                and not inv_br.is_direct_invoice \
                and not inv_br.is_inkind_donation \
                and not inv_br.is_debit_note \
                and not inv_br.is_intermission \
                or False

        return res

    _columns = {
        'from_yml_test': fields.boolean('Only used to pass addons unit test', readonly=True, help='Never set this field to true !'),
        'sequence_id': fields.many2one('ir.sequence', string='Lines Sequence', ondelete='cascade',
            help="This field contains the information related to the numbering of the lines of this order."),
        'date_invoice': fields.date('Posting Date', states={'paid':[('readonly',True)], 'open':[('readonly',True)],
            'close':[('readonly',True)]}, select=True),
        'document_date': fields.date('Document Date', states={'paid':[('readonly',True)], 'open':[('readonly',True)],
            'close':[('readonly',True)]}, select=True),
        'is_debit_note': fields.boolean(string="Is a Debit Note?"),
        'is_inkind_donation': fields.boolean(string="Is an In-kind Donation?"),
        'is_intermission': fields.boolean(string="Is an Intermission Voucher?"),
        'ready_for_import_in_debit_note': fields.function(_get_fake, fnct_search=_search_ready_for_import_in_debit_note, type="boolean",
            method=True, string="Can be imported as invoice in a debit note?",),
        'imported_invoices': fields.one2many('account.invoice.line', 'import_invoice_id', string="Imported invoices", readonly=True),
        'partner_move_line': fields.one2many('account.move.line', 'invoice_partner_link', string="Partner move line", readonly=True),
        'fake_account_id': fields.function(_get_fake_m2o_id, method=True, type='many2one', relation="account.account", string="Account", readonly="True"),
        'fake_journal_id': fields.function(_get_fake_m2o_id, method=True, type='many2one', relation="account.journal", string="Journal", readonly="True"),
        'fake_currency_id': fields.function(_get_fake_m2o_id, method=True, type='many2one', relation="res.currency", string="Currency", readonly="True"),
        'have_donation_certificate': fields.function(_get_have_donation_certificate, method=True, type='boolean', string="Have a Certificate of donation?"),
        'purchase_list': fields.boolean(string='Purchase List ?', help='Check this box if the invoice comes from a purchase list', readonly=True, states={'draft':[('readonly',False)]}),
        'virtual_currency_id': fields.function(_get_virtual_fields, method=True, store=False, multi='virtual_fields', string="Currency",
            type='many2one', relation="res.currency", readonly=True),
        'virtual_account_id': fields.function(_get_virtual_fields, method=True, store=False, multi='virtual_fields', string="Account",
            type='many2one', relation="account.account", readonly=True),
        'virtual_partner_id': fields.function(_get_virtual_fields, method=True, store=False, multi='virtual_fields', string="Supplier",
            type='many2one', relation="res.partner", readonly=True),
        'register_line_ids': fields.one2many('account.bank.statement.line', 'invoice_id', string="Register Lines"),
        'is_direct_invoice': fields.boolean("Is direct invoice?", readonly=True),
        'address_invoice_id': fields.many2one('res.partner.address', 'Invoice Address', readonly=True, required=False,
            states={'draft':[('readonly',False)]}),
        'register_posting_date': fields.date(string="Register posting date for Direct Invoice", required=False),
        'vat_ok': fields.function(_get_vat_ok, method=True, type='boolean', string='VAT OK', store=False, readonly=True),
        'st_lines': fields.one2many('account.bank.statement.line', 'invoice_id', string="Register lines", readonly=True, help="Register lines that have a link to this invoice."),
        'can_merge_lines': fields.function(_get_can_merge_lines, method=True, type='boolean', string='Can merge lines ?'),
        'is_merged_by_account': fields.boolean("Is merged by account"),
    }

    _defaults = {
        'journal_id': _get_journal,
        'from_yml_test': lambda *a: False,
        'date_invoice': lambda *a: strftime('%Y-%m-%d'),
        'is_debit_note': lambda obj, cr, uid, c: c.get('is_debit_note', False),
        'is_inkind_donation': lambda obj, cr, uid, c: c.get('is_inkind_donation', False),
        'is_intermission': lambda obj, cr, uid, c: c.get('is_intermission', False),
        'is_direct_invoice': lambda *a: False,
        'vat_ok': lambda obj, cr, uid, context: obj.pool.get('unifield.setup.configuration').get_config(cr, uid).vat_ok,
        'can_merge_lines': lambda *a: False,
        'is_merged_by_account': lambda *a: False,
    }

    def onchange_company_id(self, cr, uid, ids, company_id, part_id, ctype, invoice_line, currency_id):
        """
        This is a method to redefine the journal_id domain with the current_instance taken into account
        """
        res = super(account_invoice, self).onchange_company_id(cr, uid, ids, company_id, part_id, ctype, invoice_line, currency_id)
        if company_id and ctype:
            res.setdefault('domain', {})
            res.setdefault('value', {})
            ass = {
                'out_invoice': 'sale',
                'in_invoice': 'purchase',
                'out_refund': 'sale_refund',
                'in_refund': 'purchase_refund',
            }
            journal_ids = self.pool.get('account.journal').search(cr, uid, [
                ('company_id','=',company_id), ('type', '=', ass.get(ctype, 'purchase')), ('is_current_instance', '=', True)
            ])
            if not journal_ids:
                raise osv.except_osv(_('Configuration Error !'), _('Can\'t find any account journal of %s type for this company.\n\nYou can create one in the menu: \nConfiguration\Financial Accounting\Accounts\Journals.') % (ass.get(type, 'purchase'), ))
            res['value']['journal_id'] = journal_ids[0]
            # TODO: it's very bad to set a domain by onchange method, no time to rewrite UniField !
            res['domain']['journal_id'] = [('id', 'in', journal_ids)]
        return res

    def onchange_partner_id(self, cr, uid, ids, ctype, partner_id,\
        date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False, is_inkind_donation=False, is_intermission=False, is_debit_note=False, is_direct_invoice=False):
        """
        Update fake_account_id field regarding account_id result.
        Get default donation account for Donation invoices.
        Get default intermission account for Intermission Voucher IN/OUT invoices.
        Get default currency from partner if this one is linked to a pricelist.
        Ticket utp917 - added code to avoid currency cd change if a direct invoice
        """
        res = super(account_invoice, self).onchange_partner_id(cr, uid, ids, ctype, partner_id, date_invoice, payment_term, partner_bank_id, company_id)
        if is_inkind_donation and partner_id:
            partner = self.pool.get('res.partner').browse(cr, uid, partner_id)
            account_id = partner and partner.donation_payable_account and partner.donation_payable_account.id or False
            res['value']['account_id'] = account_id
        if is_intermission and partner_id:
            intermission_default_account = self.pool.get('res.users').browse(cr, uid, uid).company_id.intermission_default_counterpart
            account_id = intermission_default_account and intermission_default_account.id or False
            if not account_id:
                raise osv.except_osv(_('Error'), _('Please configure a default intermission account in Company configuration.'))
            res['value']['account_id'] = account_id
        if res.get('value', False) and 'account_id' in res['value']:
            res['value'].update({'fake_account_id': res['value'].get('account_id')})
        if partner_id and ctype:
            p = self.pool.get('res.partner').browse(cr, uid, partner_id)
            ai_direct_invoice = False
            if ids: #utp917
                ai = self.browse(cr, uid, ids)[0]
                ai_direct_invoice = ai.is_direct_invoice
            if p:
                c_id = False
                if ctype in ['in_invoice', 'out_refund'] and p.property_product_pricelist_purchase:
                    c_id = p.property_product_pricelist_purchase.currency_id.id
                elif ctype in ['out_invoice', 'in_refund'] and p.property_product_pricelist:
                    c_id = p.property_product_pricelist.currency_id.id
                # UFTP-121: regarding UTP-917, we have to change currency when changing partner, but not for direct invoices
                if c_id and (not is_direct_invoice and not ai_direct_invoice):
                    if not res.get('value', False):
                        res['value'] = {'currency_id': c_id}
                    else:
                        res['value'].update({'currency_id': c_id})
        # UFTP-168: If debit note, set account to False value
        if is_debit_note:
            res['value'].update({'account_id': False, 'fake_account_id': False})
        return res

    def _check_document_date(self, cr, uid, ids):
        """
        Check that document's date is done BEFORE posting date
        """
        if isinstance(ids, (int, long)):
            ids = [ids]
        for i in self.browse(cr, uid, ids):
            self.pool.get('finance.tools').check_document_date(cr, uid,
                i.document_date, i.date_invoice)
        return True

    def _check_invoice_merged_lines(self, cr, uid, ids, context=None):
        """
        US-357:
            merge of lines by account break lines descriptions (required field)
            => before next workflow stage from draft (validate, split)
               check that user has entered description on each line
               (force user to enter a custom description)
        """
        for self_br in self.browse(cr, uid, ids, context=context):
            if self_br.is_merged_by_account:
                if not all([ l.name for l in self_br.invoice_line ]):
                    raise osv.except_osv(
                        _('Error'),
                        _('Please enter a description in each merged line' \
                            ' before invoice validation')
                    )

    def _refund_cleanup_lines(self, cr, uid, lines):
        """
        Remove useless fields
        """
        for line in lines:
            if line.get('move_lines',False):
                del line['move_lines']
            if line.get('import_invoice_id',False):
                del line['import_invoice_id']
        res = super(account_invoice, self)._refund_cleanup_lines(cr, uid, lines)
        return res

    def check_po_link(self, cr, uid, ids, context=None):
        """
        Check that invoice (only supplier invoices) has no link with a PO. This is because of commitments presence.
        """
        if not context:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        purchase_obj = self.pool.get('purchase.order')
        commitment_obj = self.pool.get('account.commitment')
        for inv in self.read(cr, uid, ids, ['purchase_ids', 'type', 'is_inkind_donation', 'is_debit_note', 'state']):
            if inv.get('type', '') == 'in_invoice' and not inv.get('is_inkind_donation', False) and not inv.get('is_debit_note', False):
                if inv.get('purchase_ids', False):
                    # UTP-808: Allow draft invoice deletion. If commitment exists, set them as done.
                    if inv.get('state', '') != 'draft':
                        raise osv.except_osv(_('Warning'), _('You cannot cancel or delete a supplier invoice linked to a PO.'))
                    else:
                        for purchase in purchase_obj.browse(cr, uid, inv.get('purchase_ids', []), context=context):
                            commitment_obj.action_commitment_done(cr, uid, [x.id for x in purchase.commitment_ids])
        return True

    def _hook_period_id(self, cr, uid, inv, context=None):
        """
        Give matches period that are not draft and not HQ-closed from given date.
        Do not use special periods as period 13, 14 and 15.
        """
        # Some verifications
        if not context:
            context = {}
        if not inv:
            return False
        # NB: there is some period state. So we define that we choose only open period (so not draft and not done)
        res = self.pool.get('account.period').search(cr, uid, [('date_start','<=',inv.date_invoice or strftime('%Y-%m-%d')),
            ('date_stop','>=',inv.date_invoice or strftime('%Y-%m-%d')), ('state', 'not in', ['created', 'done']),
            ('company_id', '=', inv.company_id.id), ('special', '=', False)], context=context, order="date_start ASC, name ASC")
        return res

    def __hook_lines_before_pay_and_reconcile(self, cr, uid, lines):
        """
        Add document date to account_move_line before pay and reconcile
        """
        for line in lines:
            if line[2] and 'date' in line[2] and not line[2].get('document_date', False):
                line[2].update({'document_date': line[2].get('date')})
        return lines

    def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
        """
        Rename Supplier/Customer to "Donor" if view_type == tree
        """
        if not context:
            context = {}
        res = super(account_invoice, self).fields_view_get(cr, uid, view_id, view_type, context=context, toolbar=toolbar, submenu=submenu)
        if view_type == 'tree' and (context.get('journal_type', False) == 'inkind' or context.get('journal_type', False) == 'intermission'):
            doc = etree.XML(res['arch'])
            nodes = doc.xpath("//field[@name='partner_id']")
            name = _('Donor')
            if context.get('journal_type') == 'intermission':
                name = _('Partner')
            for node in nodes:
                node.set('string', name)
            res['arch'] = etree.tostring(doc)
        elif view_type in ('tree', 'search') and context.get('type') in ['out_invoice', 'out_refund']:
            doc = etree.XML(res['arch'])
            nodes = doc.xpath("//field[@name='supplier_reference']")
            for node in nodes:
                node.getparent().remove(node)
            res['arch'] = etree.tostring(doc)
        return res

    def default_get(self, cr, uid, fields, context=None):
        """
        Fill in fake account and fake currency for intermission invoice (in context).
        """
        defaults = super(account_invoice, self).default_get(cr, uid, fields, context=context)
        if context and context.get('is_intermission', False):
            intermission_type = context.get('intermission_type', False)
            if intermission_type in ('in', 'out'):
                # UF-2270: manual intermission (in or out)
                if defaults is None:
                    defaults = {}
                user = self.pool.get('res.users').browse(cr, uid, [uid], context=context)
                if user and user[0] and user[0].company_id:
                    # get company default currency
                    if user[0].company_id.currency_id:
                        defaults['fake_currency_id'] = user[0].company_id.currency_id.id
                        defaults['currency_id'] = defaults['fake_currency_id']
                    # get 'intermission counter part' account
                    if user[0].company_id.intermission_default_counterpart:
                        defaults['fake_account_id'] = user[0].company_id.intermission_default_counterpart.id
                        defaults['account_id'] = defaults['fake_account_id']
                    else:
                        raise osv.except_osv("Error","Company Intermission Counterpart Account must be set")
                # 'INT' intermission journal
                int_journal_id = self.pool.get('account.journal').search(cr, uid, [('type', '=', 'intermission')], context=context)
                if int_journal_id:
                    if isinstance(int_journal_id, (int, long)):
                        int_journal_id = [int_journal_id]
                    defaults['fake_journal_id'] = int_journal_id[0]
                    defaults['journal_id'] = defaults['fake_journal_id']
        return defaults

    def copy(self, cr, uid, inv_id, default=None, context=None):
        """
        Delete period_id from invoice.
        Check context for splitting invoice.
        Reset register_line_ids.
        """
        # Some checks
        if context is None:
            context = {}
        if default is None:
            default = {}
        default.update({
            'period_id': False,
            'purchase_ids': False,  # UFTP-24 do not copy linked POs
            'purchase_list': False,  # UFTP-24 do not copy linked: reset of potential purchase list flag (from a PO direct purchase)
            'partner_move_line': False,
            'imported_invoices': False
        })
        # Reset register_line_ids if not given in default
        if 'register_line_ids' not in default:
            default['register_line_ids'] = []
        # US-267: Reset st_lines if not given in default, otherwise a new line in Reg will be added
        if 'st_lines' not in default:
            default['st_lines'] = []
        # Default behaviour
        new_id = super(account_invoice, self).copy(cr, uid, inv_id, default, context)
        # Case where you split an invoice
        if 'split_it' in context:
            purchase_obj = self.pool.get('purchase.order')
            sale_obj = self.pool.get('sale.order')
            if purchase_obj:
                # attach new invoice to PO
                purchase_ids = purchase_obj.search(cr, uid, [('invoice_ids', 'in', [inv_id])], context=context)
                if purchase_ids:
                    purchase_obj.write(cr, uid, purchase_ids, {'invoice_ids': [(4, new_id)]}, context=context)
            if sale_obj:
                # attach new invoice to SO
                sale_ids = sale_obj.search(cr, uid, [('invoice_ids', 'in', [inv_id])], context=context)
                if sale_ids:
                    sale_obj.write(cr, uid, sale_ids, {'invoice_ids': [(4, new_id)]}, context=context)
        return new_id

    def create(self, cr, uid, vals, context=None):
        """
        Filled in 'from_yml_test' to True if we come from tests
        """
        if not context:
            context = {}
        if 'document_date' in vals and 'date_invoice' in vals:
            self.pool.get('finance.tools').check_document_date(cr, uid,
                vals['document_date'], vals['date_invoice'], context=context)

        # Create a sequence for this new invoice
        res_seq = self.create_sequence(cr, uid, vals, context)
        vals.update({'sequence_id': res_seq,})

        # UTP-317 # Check that no inactive partner have been used to create this invoice
        if 'partner_id' in vals:
            partner_id = vals.get('partner_id')
            if isinstance(partner_id, (str)):
                partner_id = int(partner_id)
            partner = self.pool.get('res.partner').browse(cr, uid, [partner_id])
            if partner and partner[0] and not partner[0].active:
                raise osv.except_osv(_('Warning'), _("Partner '%s' is not active.") % (partner[0] and partner[0].name or '',))

        return super(account_invoice, self).create(cr, uid, vals, context)

    def write(self, cr, uid, ids, vals, context=None):
        """
        Check document_date
        """
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]

        # US_286: Forbit possibility to add include price tax
        # in bottom left corner
        if 'tax_line' in vals:
            tax_obj = self.pool.get('account.tax')
            for tax_line in vals['tax_line']:
                if tax_line[2]:
                    if 'account_tax_id' in tax_line[2]:
                        args = [('price_include', '=', '1'),
                                ('id', '=', tax_line[2]['account_tax_id'])]
                        tax_ids = tax_obj.search(cr, uid, args, limit=1,
                                order='NO_ORDER', context=context)
                        if tax_ids:
                            raise osv.except_osv(_('Error'),
                                                 _('Tax included in price can not be tied to the whole invoice.'))

        res = super(account_invoice, self).write(cr, uid, ids, vals, context=context)
        self._check_document_date(cr, uid, ids)
        return res

    def unlink(self, cr, uid, ids, context=None):
        """
        Delete register line if this invoice is a Direct Invoice.
        Don't delete an invoice that is linked to a PO. This is only for supplier invoices.
        """
        if not context:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        # Check register lines
        for inv in self.browse(cr, uid, ids):
            if inv.is_direct_invoice and inv.register_line_ids:
                if not context.get('from_register', False):
                    self.pool.get('account.bank.statement.line').unlink(cr, uid, [x.id for x in inv.register_line_ids], {'from_direct_invoice': True})
        # Check PO
        self.check_po_link(cr, uid, ids)
        return super(account_invoice, self).unlink(cr, uid, ids, context)

    def create_sequence(self, cr, uid, vals, context=None):
        """
        Create new entry sequence for every new invoice
        """
        seq_pool = self.pool.get('ir.sequence')
        seq_typ_pool = self.pool.get('ir.sequence.type')

        name = 'Invoice L' # For Invoice Lines
        code = 'account.invoice'

        types = {
            'name': name,
            'code': code
        }
        seq_typ_pool.create(cr, uid, types)

        seq = {
            'name': name,
            'code': code,
            'prefix': '',
            'padding': 0,
        }
        return seq_pool.create(cr, uid, seq)

    def log(self, cr, uid, inv_id, message, secondary=False, context=None):
        """
        Change first "Invoice" word from message into "Debit Note" if this invoice is a debit note.
        Change it to "In-kind donation" if this invoice is an In-kind donation.
        """
        if not context:
            context = {}
        local_ctx = context.copy()
        # Prepare some values
        # Search donation view and return it
        try:
            # try / except for runbot
            debit_res = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account_override', 'view_debit_note_form')
            inkind_res = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account_override', 'view_inkind_donation_form')
            intermission_res = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account_override', 'view_intermission_form')
            supplier_invoice_res = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'invoice_supplier_form')
            customer_invoice_res = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'invoice_form')
            supplier_direct_invoice_res = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'register_accounting', 'direct_supplier_invoice_form')
        except ValueError, e:
            return super(account_invoice, self).log(cr, uid, inv_id, message, secondary, context)
        debit_view_id = debit_res and debit_res[1] or False
        debit_note_ctx = {'view_id': debit_view_id, 'type':'out_invoice', 'journal_type': 'sale', 'is_debit_note': True}
        # Search donation view and return it
        inkind_view_id = inkind_res and inkind_res[1] or False
        inkind_ctx = {'view_id': inkind_view_id, 'type':'in_invoice', 'journal_type': 'inkind', 'is_inkind_donation': True}
        # Search intermission view
        intermission_view_id = intermission_res and intermission_res[1] or False
        intermission_ctx = {'view_id': intermission_view_id, 'journal_type': 'intermission', 'is_intermission': True}
        customer_view_id = customer_invoice_res[1] or False
        customer_ctx = {'view_id': customer_view_id, 'type': 'out_invoice', 'journal_type': 'sale'}
        message_changed = False
        pattern = re.compile('^(Invoice)')
        for el in [('is_debit_note', 'Debit Note', debit_note_ctx), ('is_inkind_donation', 'In-kind Donation', inkind_ctx), ('is_intermission', 'Intermission Voucher', intermission_ctx)]:
            if self.read(cr, uid, inv_id, [el[0]]).get(el[0], False) is True:
                m = re.match(pattern, message)
                if m and m.groups():
                    message = re.sub(pattern, el[1], message, 1)
                    message_changed = True
                local_ctx.update(el[2])
        # UF-1112: Give all customer invoices a name as "Stock Transfer Voucher".
        if not message_changed and self.read(cr, uid, inv_id, ['type']).get('type', False) == 'out_invoice':
            message = re.sub(pattern, 'Stock Transfer Voucher', message, 1)

            local_ctx.update(customer_ctx)
        # UF-1307: for supplier invoice log (from the incoming shipment), the context was not
        # filled with all the information; this leaded to having a "Sale" journal in the supplier
        # invoice if it was saved after coming from this link. Here's the fix.
        if local_ctx.get('type', False) == 'in_invoice':
            if not local_ctx.get('journal_type', False):
                supplier_view_id = supplier_invoice_res and supplier_invoice_res[1] or False
                local_ctx.update({'journal_type': 'purchase',
                                'view_id': supplier_view_id})
            elif local_ctx.get('direct_invoice_view', False): # UFTP-166: The wrong context saved in log
                supplier_view_id = supplier_direct_invoice_res and supplier_direct_invoice_res[1] or False
                local_ctx = {'journal_type': 'purchase',
                             'view_id': supplier_view_id}
        return super(account_invoice, self).log(cr, uid, inv_id, message, secondary, local_ctx)

    def invoice_open(self, cr, uid, ids, context=None):
        """
        No longer fills the date automatically, but requires it to be set
        """
        # Some verifications
        if not context:
            context = {}
        self._check_invoice_merged_lines(cr, uid, ids, context=context)

        # Prepare workflow object
        wf_service = netsvc.LocalService("workflow")
        for inv in self.browse(cr, uid, ids):
            values = {}
            curr_date = strftime('%Y-%m-%d')
            if not inv.date_invoice and not inv.document_date:
                values.update({'date': curr_date, 'document_date': curr_date, 'state': 'date'})
            elif not inv.date_invoice:
                values.update({'date': curr_date, 'document_date': inv.document_date, 'state': 'date'})
            elif not inv.document_date:
                values.update({'date': inv.date_invoice, 'document_date': curr_date, 'state': 'date'})
            if inv.type in ('in_invoice', 'in_refund') and abs(inv.check_total - inv.amount_total) >= (inv.currency_id.rounding/2.0):
                state = values and 'both' or 'amount'
                values.update({'check_total': inv.check_total , 'amount_total': inv.amount_total, 'state': state})
            if values:
                values['invoice_id'] = inv.id
                wiz_id = self.pool.get('wizard.invoice.date').create(cr, uid, values, context)
                return {
                    'name': "Missing Information",
                    'type': 'ir.actions.act_window',
                    'res_model': 'wizard.invoice.date',
                    'target': 'new',
                    'view_mode': 'form',
                    'view_type': 'form',
                    'res_id': wiz_id,
                    }

            wf_service.trg_validate(uid, 'account.invoice', inv.id, 'invoice_open', cr)
        return True

    def action_reconcile_imported_invoice(self, cr, uid, ids, context=None):
        """
        Reconcile each imported invoice with its attached invoice line
        """
        # some verifications
        if isinstance(ids, (int, long)):
            ids = [ids]
        # browse all given invoices
        for inv in self.browse(cr, uid, ids):
            for invl in inv.invoice_line:
                if not invl.import_invoice_id:
                    continue
                imported_invoice = invl.import_invoice_id
                # reconcile partner line from import invoice with this invoice line attached move line
                import_invoice_partner_move_lines = self.pool.get('account.move.line').search(cr, uid, [('invoice_partner_link', '=', imported_invoice.id)])
                invl_move_lines = [x.id or None for x in invl.move_lines]
                rec = self.pool.get('account.move.line').reconcile_partial(cr, uid, [import_invoice_partner_move_lines[0], invl_move_lines[0]], 'auto', context=context)
                if not rec:
                    return False
        return True

    def action_reconcile_direct_invoice(self, cr, uid, inv, context=None):
        """
        Reconcile move line if invoice is a Direct Invoice
        NB: In order to define that an invoice is a Direct Invoice, we need to have register_line_ids not null
        """
        # Verify that this invoice is linked to a register line and have a move
        if not inv:
            return False
        if inv.move_id and inv.register_line_ids:
            ml_obj = self.pool.get('account.move.line')
            # First search move line that becomes from invoice
            res_ml_ids = ml_obj.search(cr, uid, [
                ('move_id', '=', inv.move_id.id),
                ('account_id', '=', inv.account_id.id),
                ('invoice_line_id', '=', False),  # US-254: do not seek invoice line's JIs (if same account as header)
            ])
            if len(res_ml_ids) > 1:
                raise osv.except_osv(_('Error'), _('More than one journal items found for this invoice.'))
            invoice_move_line_id = res_ml_ids[0]
            # Then search move line that corresponds to the register line
            reg_line = inv.register_line_ids[0]
            reg_ml_ids = ml_obj.search(cr, uid, [('move_id', '=', reg_line.move_ids[0].id), ('account_id', '=', reg_line.account_id.id)])
            if len(reg_ml_ids) > 1:
                raise osv.except_osv(_('Error'), _('More than one journal items found for this register line.'))
            register_move_line_id = reg_ml_ids[0]
            # Finally do reconciliation
            ml_obj.reconcile_partial(cr, uid, [invoice_move_line_id, register_move_line_id])
        return True

    def action_cancel(self, cr, uid, ids, *args):
        """
        Reverse move if this object is a In-kind Donation. Otherwise do normal job: cancellation.
        Don't delete an invoice that is linked to a PO. This is only for supplier invoices.
        """
        to_cancel = []
        for i in self.browse(cr, uid, ids):
            if i.is_inkind_donation:
                move_id = i.move_id.id
                tmp_res = self.pool.get('account.move').reverse(cr, uid, [move_id], strftime('%Y-%m-%d'))
                # If success change invoice to cancel and detach move_id
                if tmp_res:
                    # Change invoice state
                    self.write(cr, uid, [i.id], {'state': 'cancel', 'move_id':False})
                continue
            to_cancel.append(i.id)
        # Check PO link
        self.check_po_link(cr, uid, ids)
        return super(account_invoice, self).action_cancel(cr, uid, to_cancel, args)

    def action_date_assign(self, cr, uid, ids, *args):
        """
        Check Document date.
        """
        # Prepare some values
        period_obj = self.pool.get('account.period')
        # Default behaviour to add date
        res = super(account_invoice, self).action_date_assign(cr, uid, ids, args)
        # Process invoices
        for i in self.browse(cr, uid, ids):
            if not i.date_invoice:
                self.write(cr, uid, i.id, {'date_invoice': strftime('%Y-%m-%d')})
                i = self.browse(cr, uid, i.id) # This permit to refresh the browse of this element
            if not i.document_date:
                raise osv.except_osv(_('Warning'), _('Document Date is a mandatory field for validation!'))
            # UFTP-105: Search period and raise an exeception if this one is not open
            period_ids = period_obj.get_period_from_date(cr, uid, i.date_invoice)
            if not period_ids:
                raise osv.except_osv(_('Error'), _('No period found for this posting date: %s') % (i.date_invoice))
            for period in period_obj.browse(cr, uid, period_ids):
                if period.state != 'draft':
                    raise osv.except_osv(_('Warning'), _('You cannot validate this document in the given period: %s because it\'s not open. Change the date of the document or open the period.') % (period.name))
        # Posting date should not be done BEFORE document date
        self._check_document_date(cr, uid, ids)
        return res

    def action_open_invoice(self, cr, uid, ids, context=None, *args):
        """
        Give function to use when changing invoice to open state
        """
        if not context:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        if not self.action_date_assign(cr, uid, ids, context, args):
            return False
        if not self.action_move_create(cr, uid, ids, context, args):
            return False
        if not self.action_number(cr, uid, ids, context):
            return False
        if not self.action_reconcile_imported_invoice(cr, uid, ids, context):
            return False
        return True

    def line_get_convert(self, cr, uid, x, part, date, context=None):
        """
        Add these field into invoice line:
        - invoice_line_id
        """
        if not context:
            context = {}
        res = super(account_invoice, self).line_get_convert(cr, uid, x, part, date, context)
        res.update({'invoice_line_id': x.get('invoice_line_id', False)})
        return res

    def finalize_invoice_move_lines(self, cr, uid, inv, line):
        """
        Hook that changes move line data before write them.
        Add a link between partner move line and invoice.
        Add invoice document date to data.
        """
        def is_partner_line(dico):
            if isinstance(dico, dict):
                if dico:
                    # In case where no amount_currency filled in, then take debit - credit for amount comparison
                    amount = dico.get('amount_currency', False) or (dico.get('debit', 0.0) - dico.get('credit', 0.0))
                    if amount == inv.amount_total and dico.get('partner_id', False) == inv.partner_id.id:
                        return True
            return False
        new_line = []
        for el in line:
            if el[2]:
                el[2].update({'document_date': inv.document_date})
            if el[2] and is_partner_line(el[2]):
                el[2].update({'invoice_partner_link': inv.id})
                new_line.append((el[0], el[1], el[2]))
            else:
                new_line.append(el)
        return super(account_invoice, self).finalize_invoice_move_lines(cr, uid, inv, new_line)

    def button_debit_note_import_invoice(self, cr, uid, ids, context=None):
        """
        Launch wizard that permits to import invoice on a debit note
        """
        # Some verifications
        if not context:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        # Browse all given invoices
        for inv in self.browse(cr, uid, ids):
            if inv.type != 'out_invoice' or inv.is_debit_note == False:
                raise osv.except_osv(_('Error'), _('You can only do import invoice on a Debit Note!'))
            w_id = self.pool.get('debit.note.import.invoice').create(cr, uid, {'invoice_id': inv.id, 'currency_id': inv.currency_id.id,
                'partner_id': inv.partner_id.id}, context=context)
            context.update({
                'active_id': inv.id,
                'active_ids': ids,
            })
            return {
                'type': 'ir.actions.act_window',
                'res_model': 'debit.note.import.invoice',
                'name': 'Import invoice',
                'view_type': 'form',
                'view_mode': 'form',
                'res_id': w_id,
                'context': context,
                'target': 'new',
            }

    def button_split_invoice(self, cr, uid, ids, context=None):
        """
        Launch the split invoice wizard to split an invoice in two elements.
        """
        # Some verifications
        if not context:
            context={}
        if isinstance(ids, (int, long)):
            ids = [ids]
        self._check_invoice_merged_lines(cr, uid, ids, context=context)

        # Prepare some value
        wiz_lines_obj = self.pool.get('wizard.split.invoice.lines')
        inv_lines_obj = self.pool.get('account.invoice.line')
        # Creating wizard
        wizard_id = self.pool.get('wizard.split.invoice').create(cr, uid, {'invoice_id': ids[0]}, context=context)
        # Add invoices_lines into the wizard
        invoice_line_ids = self.pool.get('account.invoice.line').search(cr, uid, [('invoice_id', '=', ids[0])], context=context)
        # Some other verifications
        if not len(invoice_line_ids):
            raise osv.except_osv(_('Error'), _('No invoice line in this invoice or not enough elements'))
        for invl in inv_lines_obj.browse(cr, uid, invoice_line_ids, context=context):
            wiz_lines_obj.create(cr, uid, {'invoice_line_id': invl.id, 'product_id': invl.product_id.id, 'quantity': invl.quantity,
                'price_unit': invl.price_unit, 'description': invl.name, 'wizard_id': wizard_id}, context=context)
        # Return wizard
        if wizard_id:
            return {
                'name': "Split Invoice",
                'type': 'ir.actions.act_window',
                'res_model': 'wizard.split.invoice',
                'target': 'new',
                'view_mode': 'form,tree',
                'view_type': 'form',
                'res_id': [wizard_id],
                'context':
                {
                    'active_id': ids[0],
                    'active_ids': ids,
                    'wizard_id': wizard_id,
                }
            }
        return False

    def button_donation_certificate(self, cr, uid, ids, context=None):
        """
        Open a view containing a list of all donation certificates linked to the given invoice.
        """
        for inv in self.browse(cr, uid, ids):
            pick_id = inv.picking_id and inv.picking_id.id or ''
            domain = "[('res_model', '=', 'stock.picking'), ('res_id', '=', " + str(pick_id) + "), ('description', '=', 'Certificate of Donation')]"
            view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account_override', 'view_attachment_tree_2')
            view_id = view_id and view_id[1] or False
            search_view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account_override', 'view_attachment_search_2')
            search_view_id = search_view_id and search_view_id[1] or False
            return {
                'name': "Certificate of Donation",
                'type': 'ir.actions.act_window',
                'res_model': 'ir.attachment',
                'view_type': 'form',
                'view_mode': 'tree,form',
                'view_id': [view_id],
                'search_view_id': search_view_id,
                'domain': domain,
                'context': context,
                'target': 'current',
            }
        return False

    def button_dummy_compute_total(self, cr, uid, ids, context=None):
        return True

    def button_merge_lines(self, cr, uid, ids, context=None):
        # US-357 merge lines (by account) button for draft SIs
        def check(inv_br):
            if not inv_br.can_merge_lines:
                raise osv.except_osv(_('Error'),
                    _("Invoice not eligible for lines merging"))

            account_iterations = {}
            for l in inv_br.invoice_line:
                account_iterations[l.account_id.id] = \
                    account_iterations.setdefault(l.account_id.id, 0) + 1

            any_to_merge = False
            if account_iterations:
                for a in account_iterations:
                    if account_iterations[a] > 1:
                        any_to_merge = True
                        break

            if not any_to_merge:
                raise osv.except_osv(_('Error'),
                    _("Invoice has no line to merge by account"))

        def compute_merge(inv_br):
            """
            :result:
                - A: lines vals by line number
                - B: and list of inv id to keep (1 line by account (not merged))
            :rtype : [dict, list]

            NOTES:
            - no impact on 'import_invoice_id', 'is_corrected' as the 
              invoice is draft so not imported, and no accounting entries
            - for order_line_id and sale_order_line_id these m2o are used
              for AD at line level but when merging we keep only AD from header
            """
            index = 1
            vals_template = {
                '_index_': index,  # internal merged line index

                'account_id': False,
                'company_id': inv_br.company_id.id,
                'discount': 0.,
                'invoice_id': inv_br.id,
                'invoice_line_tax_id': None,  # m2m (None to distinguished False)
                'name': '',
                'partner_id': inv_br.partner_id.id,
                'price_unit': 0.,
                'quantity': 1.,
            }

            by_account_vals = {}  # key: account_id
            for l in inv_br.invoice_line:
                # get current merge vals for account or create new
                if l.account_id.id in by_account_vals:
                    vals = by_account_vals[l.account_id.id]
                else:
                    # new account to merge
                    vals = vals_template.copy()
                    vals.update({
                        '_index_': index,
                        'account_id': l.account_id.id,
                    })
                    index += 1

                # merge line
                vals['price_unit'] += l.price_subtotal  # qty 1 and price
                if vals['invoice_line_tax_id'] is None:
                    vals['invoice_line_tax_id'] = l.invoice_line_tax_id \
                        and [ t.id for t in l.invoice_line_tax_id ] or False
                else:
                    # get rid of the product tax line if <> between merged lines
                    if vals['invoice_line_tax_id'] is None:
                        # first tax line browsed for the account
                        if l.invoice_line_tax_id:
                            vals['invoice_line_tax_id'] = [ 
                                t.id for t in l.invoice_line_tax_id ]
                        else:
                            vals['invoice_line_tax_id'] = False
                    elif vals['invoice_line_tax_id'] and l.invoice_line_tax_id:
                        # track <> tax lines, if the case abort tax(es) in merge
                        tax_ids = [ t.id for t in l.invoice_line_tax_id ]
                        if cmp(vals['invoice_line_tax_id'], tax_ids) != 0:
                            vals['invoice_line_tax_id'] = False
                    else:
                        # no tax(es) for this line,  abort tax(es) in merge
                        vals['invoice_line_tax_id'] = False

                # update merge line
                by_account_vals[l.account_id.id] = vals

                # internal merged lines ids
                if not '_ids_' in by_account_vals[l.account_id.id]:
                    by_account_vals[l.account_id.id]['_ids_'] = []
                by_account_vals[l.account_id.id]['_ids_'].append(l.id)

            # result by index
            res = [{}, []]
            for a in by_account_vals:
                if len(by_account_vals[a]['_ids_']) > 1:
                    # more than 1 inv line by account
                    index = by_account_vals[a]['_index_']
                    del by_account_vals[a]['_index_']
                    del by_account_vals[a]['_ids_']
                    res[0][index] = by_account_vals[a]
                else:
                    res[1].append(by_account_vals[a]['_ids_'][0])
            return res

        def delete_lines(inv_br, skip_ids):
            # get ids to delete
            ad_to_del_ids = []
            line_to_del_ids = []

            for l in inv_br.invoice_line:
                if l.id in skip_ids:
                    continue  # line not to del (1 by account)
                # delete AD
                if l.analytic_distribution_id \
                    and not l.analytic_distribution_id.id in ad_to_del_ids:
                    ad_to_del_ids.append(l.analytic_distribution_id.id)
                line_to_del_ids.append(l.id)

            # delete ADs
            if ad_to_del_ids:
                ad_obj.unlink(cr, uid, ad_to_del_ids, context=context)

            # delete lines
            if line_to_del_ids:
                ail_obj.unlink(cr, uid, line_to_del_ids, context=context)

        def do_merge(inv_br, lines_vals, not_merged_ids):
            """
            :param lines_vals: lines vals in order
            :type lines_vals: dict
            """
            # the invoice is reviewed with merge lines
            # => reset the line number sequence from 1
            if inv_br.sequence_id:
                inv_br.sequence_id.write({'number_next': 1}, context=context)

            # create merge lines
            for ln in sorted(lines_vals.keys()):
                vals = lines_vals[ln]

                # post encode tax m2m
                vals['invoice_line_tax_id'] = vals['invoice_line_tax_id'] \
                    and [(6, 0, vals['invoice_line_tax_id'])] or False

                # create merge line
                if not self.pool.get('account.invoice.line').create(cr, uid,
                    vals, context=context):
                    break

            # recompute seq number for not merged lines
            ail_obj = self.pool.get('account.invoice.line')
            if not_merged_ids:
                for lid in not_merged_ids:
                    ln = inv_br.sequence_id.get_id(code_or_id='id')
                    ail_obj.write(cr, uid, [lid], {
                        'line_number': ln,
                    })

        def merge_invoice(inv_br):
            check(inv_br)
            merge_res = compute_merge(inv_br)
            delete_lines(inv_br, merge_res[1])
            do_merge(inv_br, merge_res[0], merge_res[1])

            # set merged flag
            inv_br.write({'is_merged_by_account': True}, context=context)

            # recompute taxes (reset not manual ones)
            self.button_reset_taxes(cr, uid, [inv_br.id], context=context)

        def post_merge(inv_br):
            inv_br.write({
                # update check total for accurate check amount at validation
                'check_total':
                    inv_br.amount_total or inv_br.check_amount or 0.,
            }, context=context)

        res = {}
        if not ids:
            return False
        if isinstance(ids, (int, long, )):
            ids = [ids]

        ail_obj = self.pool.get('account.invoice.line')
        ad_obj = self.pool.get('analytic.distribution')

        # merging
        for inv_br in self.browse(cr, uid, ids, context=context):
            merge_invoice(inv_br)

        # post processing (reload invoices)
        for inv_br in self.browse(cr, uid, ids, context=context):
            post_merge(inv_br)

        return res

account_invoice()

class account_invoice_line(osv.osv):
    _name = 'account.invoice.line'
    _inherit = 'account.invoice.line'

    def _uom_constraint(self, cr, uid, ids, context=None):
        for obj in self.browse(cr, uid, ids, context=context):
            if not self.pool.get('uom.tools').check_uom(cr, uid, obj.product_id.id, obj.uos_id.id, context):
                raise osv.except_osv(_('Error'), _('You have to select a product UOM in the same category than the purchase UOM of the product !'))
        return True

    _constraints = [(_uom_constraint, 'Constraint error on Uom', [])]

    def _have_been_corrected(self, cr, uid, ids, name, args, context=None):
        """
        Return True if ALL elements are OK:
         - a journal items is linked to this invoice line
         - the journal items is linked to an analytic line that have been reallocated
        """
        if context is None:
            context = {}
        res = {}

        def has_ana_reallocated(move):
            for ml in move.move_lines or []:
                for al in ml.analytic_lines or []:
                    if al.is_reallocated:
                        return True
            return False

        for il in self.browse(cr, uid, ids, context=context):
            res[il.id] = has_ana_reallocated(il)
        return res

    def _get_product_code(self, cr, uid, ids, field_name=None, arg=None, context=None):
        """
        Give product code for each invoice line
        """
        res = {}
        for inv_line in self.browse(cr, uid, ids, context=context):
            res[inv_line.id] = ''
            if inv_line.product_id:
                res[inv_line.id] = inv_line.product_id.default_code

        return res
    def _get_vat_ok(self, cr, uid, ids, field_name, args, context=None):
        '''
        Return True if the system configuration VAT management is set to True
        '''
        vat_ok = self.pool.get('unifield.setup.configuration').get_config(cr, uid).vat_ok
        res = {}
        for id in ids:
            res[id] = vat_ok

        return res

    _columns = {
        'from_yml_test': fields.boolean('Only used to pass addons unit test', readonly=True, help='Never set this field to true !'),
        'line_number': fields.integer(string='Line Number'),
        'price_unit': fields.float('Unit Price', required=True, digits_compute= dp.get_precision('Account Computation')),
        'import_invoice_id': fields.many2one('account.invoice', string="From an import invoice", readonly=True),
        'move_lines':fields.one2many('account.move.line', 'invoice_line_id', string="Journal Item", readonly=True),
        'is_corrected': fields.function(_have_been_corrected, method=True, string="Have been corrected?", type='boolean',
            readonly=True, help="This informs system if this item have been corrected in analytic lines. Criteria: the invoice line is linked to a journal items that have analytic item which is reallocated.",
            store=False),
        'product_code': fields.function(_get_product_code, method=True, store=False, string="Product Code", type='char'),
        'reference': fields.char(string="Reference", size=64),
        'vat_ok': fields.function(_get_vat_ok, method=True, type='boolean', string='VAT OK', store=False, readonly=True),
    }

    _defaults = {
        'price_unit': lambda *a: 0.00,
        'from_yml_test': lambda *a: False,
        'is_corrected': lambda *a: False,
        'vat_ok': lambda obj, cr, uid, context: obj.pool.get('unifield.setup.configuration').get_config(cr, uid).vat_ok,
    }

    _order = 'line_number'

    def create(self, cr, uid, vals, context=None):
        """
        Give a line_number to invoice line.
        NB: This appends only for account invoice line and not other object (for an example direct invoice line)
        If invoice is a Direct Invoice and is in draft state:
         - compute total amount (check_total field)
         - write total to the register line
        """
        if not context:
            context = {}
        # Create new number with invoice sequence
        if vals.get('invoice_id') and self._name in ['account.invoice.line']:
            invoice = self.pool.get('account.invoice').browse(cr, uid, vals['invoice_id'])
            if invoice and invoice.sequence_id:
                sequence = invoice.sequence_id
                line = sequence.get_id(code_or_id='id', context=context)
                vals.update({'line_number': line})
        return super(account_invoice_line, self).create(cr, uid, vals, context)

    def write(self, cr, uid, ids, vals, context=None):
        """
        Give a line_number in invoice_id in vals
        NB: This appends only for account invoice line and not other object (for an example direct invoice line)
        If invoice is a Direct Invoice and is in draft state:
         - compute total amount (check_total field)
         - write total to the register line
        """

        if not context:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        if vals.get('invoice_id') and self._name in ['account.invoice.line']:
            for il in self.browse(cr, uid, ids):
                if not il.line_number and il.invoice_id.sequence_id:
                    sequence = il.invoice_id.sequence_id
                    il_number = sequence.get_id(code_or_id='id', context=context)
                    vals.update({'line_number': il_number})
        res = super(account_invoice_line, self).write(cr, uid, ids, vals, context)
        for invl in self.browse(cr, uid, ids):
            if invl.invoice_id and invl.invoice_id.is_direct_invoice and invl.invoice_id.state == 'draft':
                amount = 0.0
                for l in invl.invoice_id.invoice_line:
                    amount += l.price_subtotal
                self.pool.get('account.invoice').write(cr, uid, [invl.invoice_id.id], {'check_total': amount}, context)
                self.pool.get('account.bank.statement.line').write(cr, uid, [x.id for x in invl.invoice_id.register_line_ids], {'amount': -1 * amount}, context)
        return res

    def copy(self, cr, uid, inv_id, default=None, context=None):
        """
        Check context to see if we come from a split. If yes, we create the link between invoice and PO/FO.
        """
        if not context:
            context = {}
        if not default:
            default = {}

        new_id = super(account_invoice_line, self).copy(cr, uid, inv_id, default, context)

        if 'split_it' in context:
            purchase_lines_obj = self.pool.get('purchase.order.line')
            sale_lines_obj = self.pool.get('sale.order.line')

            if purchase_lines_obj:
                purchase_line_ids = purchase_lines_obj.search(cr, uid,
                        [('invoice_lines', 'in', [inv_id])], order='NO_ORDER')
                if purchase_line_ids:
                    purchase_lines_obj.write(cr, uid, purchase_line_ids, {'invoice_lines': [(4, new_id)]})

            if sale_lines_obj:
                sale_lines_ids =  sale_lines_obj.search(cr, uid,
                        [('invoice_lines', 'in', [inv_id])], order='NO_ORDER')
                if sale_lines_ids:
                    sale_lines_obj.write(cr, uid,  sale_lines_ids, {'invoice_lines': [(4, new_id)]})

        return new_id

    def copy_data(self, cr, uid, inv_id, default=None, context=None):
        """
        Copy an invoice line without its move lines
        """
        if default is None:
            default = {}
        default.update({'move_lines': False,})
        return super(account_invoice_line, self).copy_data(cr, uid, inv_id, default, context)

    def unlink(self, cr, uid, ids, context=None):
        """
        If invoice is a Direct Invoice and is in draft state:
         - compute total amount (check_total field)
         - write total to the register line
        """
        if not context:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        # Fetch all invoice_id to check
        direct_invoice_ids = []
        abst_obj = self.pool.get('account.bank.statement.line')
        for invl in self.browse(cr, uid, ids):
            if invl.invoice_id and invl.invoice_id.is_direct_invoice and invl.invoice_id.state == 'draft':
                direct_invoice_ids.append(invl.invoice_id.id)
                # find account_bank_statement_lines and used this to delete the account_moves and associated records
                absl_ids = abst_obj.search(cr, uid,
                        [('invoice_id','=',invl.invoice_id.id)],
                        order='NO_ORDER')
                if absl_ids:
                    abst_obj.unlink_moves(cr, uid, absl_ids, context)
        # Normal behaviour
        res = super(account_invoice_line, self).unlink(cr, uid, ids, context)
        # See all direct invoice
        for inv in self.pool.get('account.invoice').browse(cr, uid, direct_invoice_ids):
            amount = 0.0
            for l in inv.invoice_line:
                amount += l.price_subtotal
            self.pool.get('account.invoice').write(cr, uid, [inv.id], {'check_total': amount}, context)
            self.pool.get('account.bank.statement.line').write(cr, uid, [x.id for x in inv.register_line_ids], {'amount': -1 * amount}, context)
        return res

    def move_line_get_item(self, cr, uid, line, context=None):
        """
        Add a link between move line and its invoice line
        """
        # some verification
        if not context:
            context = {}
        # update default dict with invoice line ID
        res = super(account_invoice_line, self).move_line_get_item(cr, uid, line, context=context)
        res.update({'invoice_line_id': line.id})
        return res

    def button_open_analytic_lines(self, cr, uid, ids, context=None):
        """
        Return analytic lines linked to this invoice line.
        First we takes all journal items that are linked to this invoice line.
        Then for all journal items, we take all analytic journal items.
        Finally we display the result for "button_open_analytic_corrections" of analytic lines
        """
        # Some checks
        if not context:
            context = {}
        # Prepare some values
        al_ids = []
        # Browse give invoice lines
        for il in self.browse(cr, uid, ids, context=context):
            if il.move_lines:
                for ml in il.move_lines:
                    if ml.analytic_lines:
                        al_ids += [x.id for x in ml.analytic_lines]
        return self.pool.get('account.analytic.line').button_open_analytic_corrections(cr, uid, al_ids, context=context)

account_invoice_line()


class res_partner(osv.osv):
    _description='Partner'
    _inherit = "res.partner"

    def _get_fake(self, cr, uid, ids, name, args, context=None):
        res = {}
        if not ids:
            return res
        if isinstance(ids, (int, long)):
            ids = [ids]
        for id in ids:
            res[id] = False
        return res

    def _get_search_by_invoice_type(self, cr, uid, obj, name, args,
        context=None):
        res = []
        if not len(args):
            return res
        if context is None:
            context = {}
        if len(args) != 1:
            msg = _("Domain %s not suported") % (str(args), )
            raise osv.except_osv(_('Error'), msg)
        if args[0][1] != '=':
            msg = _("Operator '%s' not suported") % (args[0][1], )
            raise osv.except_osv(_('Error'), msg)
        if not args[0][2]:
            return res

        invoice_type = context.get('type', False)
        if invoice_type:
            if invoice_type in ('in_invoice', 'in_refund', ):
                # in invoices: only supplier partner
                res = [('supplier', '=', True)]
            elif invoice_type in ('out_invoice', 'out_refund', ):
                # out invoices: only customer partner
                res = [('customer', '=', True)]

        return res

    _columns = {
        'by_invoice_type': fields.function(_get_fake, type='boolean',
            fnct_search=_get_search_by_invoice_type, method=True),
    }

    def name_search(self, cr, uid, name='', args=None, operator='ilike',
        context=None, limit=100):
        # BKLG-50: IN/OUT invoice/refund partner autocompletion filter
        # regarding supplier/customer
        if context is None:
            context = {}

        alternate_domain = False
        invoice_type = context.get('type', False)
        if invoice_type:
            if invoice_type in ('in_invoice', 'in_refund', ):
                alternate_domain = [('supplier', '=', True)]
            elif invoice_type in ('out_invoice', 'out_refund', ):
                alternate_domain = [('customer', '=', True)]
        if alternate_domain:
            args += alternate_domain

        return super(res_partner, self).name_search(cr, uid, name=name,
            args=args, operator=operator, context=context, limit=limit)

res_partner()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: