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
|
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
# $Id$
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv
from openerp.tools.translate import _
class account_invoice(osv.osv):
_name = 'account.invoice'
_inherit = 'account.invoice'
def _default_journal_id(self, cr, uid, context={}):
return False
_defaults = {
'journal_id' : _default_journal_id,
}
def workflow_action_validate(self, cr, uid, ids, context={}):
for invoice in self.browse(cr, uid, ids):
if not self._create_sequence(cr, uid, invoice):
return False
if not self._create_account_move(cr, uid, invoice, context):
return False
if not self._check_tax(cr, uid, invoice.id):
return False
self.write(cr, uid, [invoice.id], {'state' : 'open'})
return True
def _check_tax(self, cr, uid, id):
obj_tax = self.pool.get('account.invoice.tax')
invoice = self.browse(cr, uid, [id])[0]
compute_taxes = obj_tax.compute(cr, uid, id)
self.check_tax_lines(cr, uid, invoice, compute_taxes, obj_tax)
return True
def _create_sequence(self, cr, uid, invoice):
obj_sequence = self.pool.get('ir.sequence')
context = {'source_date' : invoice.date_invoice}
if invoice.internal_number != '':
sequence_id = invoice.journal_id.sequence_id.id
sequence = obj_sequence.next_by_id(cr, uid, sequence_id, context=context)
self.write(cr, uid, [invoice.id], {'internal_number' : sequence})
return True
def _create_account_move(self, cr, uid, invoice, context=None):
obj_invoice_tax = self.pool.get('account.invoice.tax')
obj_currency = self.pool.get('account.currency')
obj_period = self.pool.get('account.period')
obj_payment_term = self.pool.get('account.payment.term')
obj_journal = self.pool.get('account.journal')
obj_move = self.pool.get('account.move')
obj_line = self.pool.get('account.invoice.line')
obj_tax = self.pool.get('account.invoice.tax')
if context is None:
context = {}
if not self._check_journal_sequence(cr, uid, invoice):
return False
if not self._check_invoice_line(cr, uid, invoice):
return False
# create account.move
move_id = self._create_account_move_header(cr, uid, invoice)
if not move_id:
return False
# create account.move.line for header
header_line_id = self._create_account_move_line(cr, uid, invoice, move_id)
if not header_line_id:
return False
# create account.move.line for each detail
for line in invoice.invoice_line:
move_line_id = obj_line._create_account_move_line(cr, uid, line, move_id)
if not move_line_id:
return False
# create account.move.line for each tax
if invoice.tax_lines:
for tax in invoice.tax_lines:
tax_move_line_id = obj_tax._create_account_move_line(cr, uid, tax, move_id)
if not tax_move_line_id:
return False
# reconcile if needed
for line in invoice.invoice_line:
if not obj_line._reconcile(cr, uid, line):
return False
invoice = self.browse(cr, uid, [invoice.id])[0]
self.write(cr, uid, [invoice.id], {'move_id' : move_id, 'move_name' : invoice.name})
#validate account move
obj_move.button_validate(cr, uid, [move_id])
return True
def _check_journal_sequence(self, cr, uid, invoice):
if not invoice.journal_id.sequence_id:
raise osv.except_osv(_('Warning'),_('Please defined sequence for journal'))
return False
return True
def _check_invoice_line(self, cr, uid, invoice):
if not invoice.invoice_line:
raise osv.except_osv(_('Warning!'),_('Please add detail'))
return False
return True
def _create_account_move_header(self, cr, uid, invoice):
obj_move = self.pool.get('account.move')
obj_period = self.pool.get('account.period')
invoice = self.browse(cr, uid, [invoice.id])[0]
move_id = False
period_ids = obj_period.find(cr, uid, invoice.date_invoice)
if period_ids:
period_id = period_ids[0]
value = {
'name' : invoice.internal_number,
'journal_id' : invoice.journal_id.id,
'date' : invoice.date_invoice,
'period_id' : period_id,
'company_id' : invoice.company_id.id,
}
move_id = obj_move.create(cr, uid, value)
return move_id
def _get_header_amount(self, cr, uid, invoice):
obj_currency = self.pool.get('res.currency')
result = {
'debit' : 0.0,
'credit' : 0.0,
'amount_currency' : 0.0,
'currency_id' : False,
}
invoice_currency = invoice.currency_id
company_currency = invoice.company_id.currency_id
if invoice_currency.id != company_currency.id:
result['currency_id'] = invoice_currency.id
exchange_rate = obj_currency.compute(cr, uid, invoice_currency.id, company_currency.id, 1.0, context={'date' : invoice.date_invoice})
amount_total = invoice.amount_total * exchange_rate
if invoice.type == 'out_invoice' or invoice.type == 'in_refund':
result['debit'] = amount_total
result['amount_currency'] = result['currency_id'] and invoice.amount_total or 0.0
else:
result['credit'] = amount_total
result['amount_currency'] = result['currency_id'] and -1.0 * invoice.amount_total or 0.0
return result
def _create_account_move_line(self, cr, uid, invoice, move_id):
obj_line = self.pool.get('account.move.line')
obj_period = self.pool.get('account.period')
period_ids = obj_period.find(cr, uid, invoice.date_invoice)
if period_ids :
period_id = period_ids[0]
amount = self._get_header_amount(cr, uid, invoice)
value = {
'move_id' : move_id,
'name' : invoice.name and invoice.name or '/',
'date' : invoice.date_invoice,
'date_maturity' : invoice.date_due,
'period_id' : period_id,
'debit' : amount['debit'],
'credit' : amount['credit'],
'account_id' : invoice.account_id.id,
'partner_id' : invoice.partner_id.id,
'currency_id' : amount['currency_id'],
'amount_currency' : amount['amount_currency'],
'direct_analytic_ids' : [],
'indirect_analytic_ids' : [],
}
move_id = obj_line.create(cr, uid, value)
return move_id
account_invoice()
|