~slobodni-programi/addons-sp/61

« back to all changes in this revision

Viewing changes to account_vat_on_cash_receipts/account.py

  • Committer: Goran Kliska
  • Date: 2011-11-10 09:30:26 UTC
  • Revision ID: goran.kliska@slobodni-programi.hr-20111110093026-q4dqc6lmfoo60ngh
croatian VAT rules and postings on payment

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2011 Domsense s.r.l. (<http://www.domsense.com>).
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
from osv import fields, osv
 
23
from tools.translate import _
 
24
 
 
25
class account_invoice(osv.osv):
 
26
    _inherit = "account.invoice"
 
27
    _columns = {
 
28
        'vat_on_cash': fields.boolean('Vat on cash receipts'),
 
29
        }
 
30
account_invoice()
 
31
 
 
32
class account_voucher(osv.osv):
 
33
    _inherit = "account.voucher"
 
34
 
 
35
    def action_move_line_create(self, cr, uid, ids, context=None):
 
36
        if isinstance(ids, (int, long)):
 
37
            ids = [ids]
 
38
        if not context:
 
39
            context={}
 
40
        res = super(account_voucher, self).action_move_line_create(cr, uid, ids, context)
 
41
        invoice_pool=self.pool.get('account.invoice')
 
42
        move_pool=self.pool.get('account.move')
 
43
        line_pool=self.pool.get('account.move.line')
 
44
 
 
45
        invoices = []
 
46
        # search for reconciled invoices and compute partial payment
 
47
        for voucher in self.browse(cr, uid, ids):
 
48
            for line in voucher.move_ids:
 
49
                if line.reconcile_partial_id:
 
50
                    paid_amount = 0.0
 
51
                    inv_amount = 0.0
 
52
                    for reconcile_line in line.reconcile_partial_id.line_partial_ids:
 
53
                        if reconcile_line.invoice:
 
54
                            invoice_id = reconcile_line.invoice.id
 
55
                            inv_amount = reconcile_line.invoice.amount_total
 
56
                        elif reconcile_line.credit > 0.0:
 
57
                            paid_amount += reconcile_line.credit
 
58
                    inv_tuple = (invoice_id, paid_amount / inv_amount)
 
59
                    if inv_tuple not in invoices:
 
60
                        invoices.append(inv_tuple)
 
61
                if line.reconcile_id:
 
62
                    for reconcile_line in line.reconcile_id.line_id:
 
63
                        if reconcile_line.invoice:
 
64
                            if (reconcile_line.invoice.id,1) not in invoices:
 
65
                                invoices.append((reconcile_line.invoice.id,1))
 
66
        
 
67
        for invoice_tuple in invoices:
 
68
            invoice = invoice_pool.browse(cr, uid, invoice_tuple[0], context)
 
69
            if invoice.vat_on_cash:
 
70
                move_data = {
 
71
                    'name': _('VAT on cash') + ' - ' + invoice.move_id.name,
 
72
                    }
 
73
                if invoice.journal_id.vat_on_cash_related_journal_id:
 
74
                    move_data['journal_id'
 
75
                        ] = invoice.journal_id.vat_on_cash_related_journal_id.id
 
76
                else:
 
77
                    move_data['journal_id'] = invoice.journal_id.id
 
78
                move_id = move_pool.create(cr, uid, move_data)
 
79
                move = move_pool.browse(cr, uid, move_id)
 
80
 
 
81
                for move_line in invoice.move_id.line_id:
 
82
                    line_count = 0
 
83
                    if move_line.credit > 0.0:
 
84
                        if move_line.account_id.vat_on_cash_related_account_id:
 
85
                            line_count +=1
 
86
 
 
87
                            line_data = {
 
88
                                'name': _('VAT on cash') + ' - ' + move_line.name,
 
89
                                'debit': move_line.debit * invoice_tuple[1],
 
90
                                'credit': move_line.credit * invoice_tuple[1],
 
91
                                'account_id': move_line.account_id.vat_on_cash_related_account_id.id,
 
92
                                'move_id': move_id,
 
93
                                'journal_id':  move.journal_id.id,
 
94
                                'period_id':  move.period_id.id,
 
95
                                }
 
96
 
 
97
                            shadow_line_data = {
 
98
                                'name': _('VAT on cash') + ' - ' + move_line.name,
 
99
                                'debit': move_line.credit * invoice_tuple[1],
 
100
                                'credit': move_line.debit * invoice_tuple[1],
 
101
                                'account_id': move_line.account_id.id,
 
102
                                'move_id': move_id,
 
103
                                'journal_id':  move.journal_id.id,
 
104
                                'period_id':  move.period_id.id,
 
105
                                }
 
106
 
 
107
                            if move_line.tax_code_id:
 
108
                                shadow_line_data['tax_code_id'] = move_line.tax_code_id.id
 
109
                                shadow_line_data['tax_amount'] = (shadow_line_data['credit']
 
110
                                    - shadow_line_data['debit'])
 
111
                                if move_line.tax_code_id.vat_on_cash_related_tax_code_id:
 
112
                                    line_data['tax_code_id'
 
113
                                        ] = move_line.tax_code_id.vat_on_cash_related_tax_code_id.id
 
114
                                    line_data['tax_amount'] = (line_data['credit']
 
115
                                        - line_data['debit'])
 
116
 
 
117
                            line_pool.create(cr, uid, line_data)
 
118
                            line_pool.create(cr, uid, shadow_line_data)
 
119
                    if line_count == 0:
 
120
                        raise osv.except_osv(_('Error!'), _('You are trying to reconcile a "Vat on cash receipts" invoice, but no account used by the invoice has a related account used for real registrations on a VAT on cash basis'))
 
121
 
 
122
                move_pool.post(cr, uid, [move_id], context={})
 
123
 
 
124
        return res
 
125
 
 
126
account_voucher()
 
127
 
 
128
class account_account(osv.osv):
 
129
    _inherit = "account.account"
 
130
    _columns = {
 
131
        'vat_on_cash_related_account_id': fields.many2one('account.account', 'Account for VAT on cash', help='Related account used for real registrations on a VAT on cash basis'),
 
132
        }
 
133
account_account()
 
134
 
 
135
class account_tax_code(osv.osv):
 
136
    _inherit = "account.tax.code"
 
137
    _columns = {
 
138
        'vat_on_cash_related_tax_code_id': fields.many2one('account.tax.code', 'Tax code for VAT on cash', help='Related tax code used for real registrations on a VAT on cash basis'),
 
139
        }
 
140
account_tax_code()
 
141
 
 
142
class account_journal(osv.osv):
 
143
    _inherit = "account.journal"
 
144
    _columns = {
 
145
        'vat_on_cash_related_journal_id': fields.many2one('account.journal', 'Journal for VAT on cash', help='Related journal used for real registrations on a VAT on cash basis'),
 
146
        }
 
147
account_journal()