-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.py
281 lines (242 loc) · 13 KB
/
account.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp import netsvc
import csv
import base64
from cStringIO import StringIO
import datetime
import os
default_domain = [('type', '=', 'in_invoice'), ('state', '=', 'draft'), ('synchronized', '=', False)]
class res_partner(osv.osv):
_name = "res.partner"
_inherit = "res.partner"
_columns = {
'is_cmms_supplier': fields.boolean(string="CMMS Supplier"),
}
class account_invoice_line(osv.osv):
_name = "account.invoice.line"
_inherit = "account.invoice.line"
_columns = {
'parent_type': fields.related('invoice_id', 'type', type="char", store=True),
'cost_purpose': fields.char('Cost Purpose'),
'object_nr': fields.char('Object Nr'),
'object_description': fields.char('Object Description'),
'project_nr': fields.char('Project Nr'),
'project_description': fields.char('Project Description'),
'wo_nr': fields.char('WO Nr'),
'wo_description': fields.char('WO Description'),
'part_nr': fields.char('Part Nr'),
'part_description': fields.char('Part Description'),
'po_nr': fields.char('PO Nr'),
'po_line': fields.char('PO Line'),
'asset': fields.char('Asset', size=80),
'deviation': fields.char('Deviation', size=80),
'state': fields.related('invoice_id', 'state', type="char", string="Status"),
}
class account_invoice(osv.osv):
_name = "account.invoice"
_inherit = "account.invoice"
_columns = {
'invoice_line': fields.one2many('account.invoice.line', 'invoice_id', 'Invoice Lines', readonly=True, states={'draft':[('readonly',False)], 'synchronized':[('readonly',False)]}),
'approved': fields.char(string='Approved'),
'cost_purpose': fields.char(string="Cost Purpose"),
'synchronized': fields.boolean(string="Synchronized"),
'state': fields.selection([
('draft','Draft'),
('proforma','Pro-forma'),
('proforma2','Pro-forma'),
('sent','Sent to CMMS'),
('synchronized', 'Synchronized'),
('open','Validated'),
('paid','Paid'),
('cancel','Cancelled'),
],'Status', select=True, readonly=True, track_visibility='onchange',
help=' * The \'Draft\' status is used when a user is encoding a new and unconfirmed Invoice. \
\n* The \'Pro-forma\' when invoice is in Pro-forma status,invoice does not have an invoice number. \
\n* The \'Open\' status is used when user create invoice,a invoice number is generated.Its in open status till user does not pay invoice. \
\n* The \'Paid\' status is set automatically when the invoice is paid. Its related journal entries may or may not be reconciled. \
\n* The \'Cancelled\' status is used when user cancel invoice.'),
'internal_number': fields.char('Invoice Number', size=32, help="Unique number of the invoice, computed automatically when the invoice is created."),
'supplier_invoice_number': fields.char('Supplier Invoice Number', size=64, help="The reference of this invoice as provided by the supplier.", required=True, states={'draft':[('readonly',False)]}),
'po_number': fields.char('PO number', size=32),
'is_cmms_supplier': fields.related('partner_id', 'is_cmms_supplier', type='boolean'),
'template': fields.boolean(string="Template"),
}
_defaults = {
'synchronized': False,
}
def onchange_supplier_number(self, cr, uid, ids, supplier_invoice_number, context=None):
n = self.search(cr, uid, [
('supplier_invoice_number', '=', supplier_invoice_number),
], count=True)
if n > 0:
warning = {
'title': _('Supplier Invoice Number'),
'message': _('This Supplier Invoice Number is already used!')
}
return {'warning': warning}
return {}
def action_cancel_draft(self, cr, uid, ids, *args):
self.write(cr, uid, ids, {'state':'draft', 'synchronized': False})
wf_service = netsvc.LocalService("workflow")
for inv_id in ids:
wf_service.trg_delete(uid, 'account.invoice', inv_id, cr)
wf_service.trg_create(uid, 'account.invoice', inv_id, cr)
return True
def val_synch(self, cr, uid, ids, context=None):
wf_service = netsvc.LocalService("workflow")
for index in ids:
wf_service.trg_validate(uid, "account.invoice", int(index), "invoice_cancel", cr)
self.action_cancel_draft(cr, uid, ids, [])
self.write(cr, uid, ids, {'synchronized': True}, context=context)
for index in ids:
wf_service.trg_validate(uid, "account.invoice", int(index), "invoice_open", cr)
return True
def write(self, cr, uid, ids, vals, context=None):
if 'synchronized' in vals and vals['synchronized']:
to_synch_ids = self.search(cr, uid, [('id', 'in', ids), ('synchronized', '=', False), ('state', '=', 'sent')], context=context)
to_update_ids = self.search(cr, uid, [('id', 'in', ids), ('state', '=', 'synchronized')], context=context)
invoice_line_reg = self.pool.get('account.invoice.line')
invoice_tax_reg = self.pool.get('account.invoice.tax')
if to_synch_ids:
invoice_line_ids = invoice_line_reg.search(cr, uid, [('invoice_id', 'in', to_synch_ids)], context=context)
invoice_line_reg.unlink(cr, uid, invoice_line_ids, context=context)
to_synch_ids.extend(to_update_ids)
invoice_tax_ids = invoice_tax_reg.search(cr, uid, [('invoice_id', 'in', to_synch_ids)], context=context)
invoice_tax_reg.unlink(cr, uid, invoice_tax_ids, context=context)
self.write(cr, uid, to_synch_ids, {'state': 'draft'}, context=context)
super(account_invoice, self).write(cr, uid, to_synch_ids, vals, context=context)
self.write(cr, uid, to_synch_ids, {'state': 'synchronized'}, context=context)
return True
else:
return super(account_invoice, self).write(cr, uid, ids, vals, context=context)
def create(self, cr, uid, vals, context=None):
vals.update({'synchronized': False})
return super(account_invoice, self).create(cr, uid, vals, context=context)
def _prepare_refund(self, cr, uid, invoice, date=None, period_id=None, description=None, journal_id=None, context=None):
res = super(account_invoice, self)._prepare_refund(cr, uid, invoice, date=date, period_id=period_id, description=description, journal_id=journal_id, context=context)
res['supplier_invoice_number'] = 'supplier_invoice_number' in invoice and invoice['supplier_invoice_number'] or False
return res
class account_voucher(osv.osv):
_name = 'account.voucher'
_inherit = 'account.voucher'
def proforma_voucher(self, cr, uid, ids, context=None):
for voucher in self.browse(cr, uid, ids, context=context):
for elmt in voucher.line_dr_ids:
if elmt.move_line_id:
invoice = elmt.move_line_id.invoice
if invoice and invoice.type and invoice.type == 'in_invoice':
if not 'state' in invoice or not invoice.state or not invoice.state == 'open' or not 'approved' in invoice or (not invoice.approved == 'A' and not invoice.approved == 'a'):
raise osv.except_osv(_('Error!'),_("The invoice must be approved before payment."))
return super(account_voucher, self).proforma_voucher(cr, uid, ids, context=context)
def button_proforma_voucher(self, cr, uid, ids, context=None):
context = context or {}
if context.get('active_ids'):
invoice = self.pool.get('account.invoice').browse(cr, uid, context.get('active_ids')[0], context=context)
if invoice.type == 'in_invoice':
if not 'approved' in invoice or not invoice.approved == 'A':
raise osv.except_osv(_('Error!'),_("The invoice must be approved before payment."))
return super(account_voucher, self).button_proforma_voucher(cr, uid, ids, context=context)
_columns = {
'template': fields.boolean(string="Template"),
}
class account_export(osv.osv_memory):
_name = "account.export"
def export(self, cr, uid, ids, context=None):
invoice_reg = self.pool.get('account.invoice')
wiz = self.browse(cr, uid, ids, context=context)[0]
invoice_ids = wiz.use_criteria and wiz.invoice_with_criteria_ids or wiz.invoice_ids
if len(invoice_ids)==0:
return True
data = self.get_export_data(cr, uid, invoice_ids, context=context)
encode_text = base64.encodestring(data)
self.write(cr, uid, ids, {
'file': encode_text,
'state': 'saved',
'datas_fname': 'supplier_invoice_' + str(datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')) + '.csv'
}, context=context)
return {
'type': 'ir.actions.act_window',
'name': 'Export Invoice to CMMS',
'res_model': 'account.export',
'res_id': ids[0],
'view_mode': 'form',
'view_type': 'form',
'target': 'new',
'context': context,
}
def auto_export(self, cr, uid, force_run=False):
base_path = self.pool.get('ir.config_parameter').get_param(cr, uid, 'LCT path export', default='/tmp')
invoice_reg = self.pool.get('account.invoice')
inv_ids = invoice_reg.search(cr, uid, default_domain)
invoices = invoice_reg.browse(cr, uid, inv_ids)
filename = os.path.join(base_path, "invoice_cmms_%s.csv" % (datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d_%H:%M:%S'), ))
data = self.get_export_data(cr, uid, invoices)
with open(filename, 'wb') as ondisk:
ondisk.write(data)
def get_export_data(self, cr, uid, invoice_ids, context=None):
invoice_reg = self.pool.get('account.invoice')
for inv in invoice_ids:
if inv.state == 'draft' and not inv.synchronized and inv.type == 'in_invoice':
invoice_reg.action_date_assign(cr, uid, [inv.id])
invoice_reg.action_move_create(cr, uid, [inv.id])
invoice_reg.action_number(cr, uid, [inv.id])
invoice_reg.write(cr, uid, [inv.id], {'state': 'sent'}, context=context)
# the export
fields = ['id', 'partner_id', 'currency_id', 'date_invoice', 'date_due', 'invoice_line/price_subtotal', 'invoice_line/invoice_line_tax_id', 'amount_total', 'internal_number', 'po_number']
rows = invoice_reg.export_data(cr, uid, [inv.id for inv in invoice_ids], fields, context=context)
fp = StringIO()
writer = csv.writer(fp, quoting=csv.QUOTE_ALL, delimiter=';')
writer.writerow([name.encode('utf-8') for name in fields])
for data in rows['datas']:
row = []
for d in data:
if isinstance(d, basestring):
d = d.replace('\n',' ').replace('\t',' ')
try:
d = d.encode('utf-8')
except UnicodeError:
pass
if d is False: d = None
row.append(d)
writer.writerow(row)
fp.seek(0)
data = fp.read()
fp.close()
return data
def _get_invoices(self, cr, uid, context=None):
return self.pool.get("account.invoice").search(cr, uid, default_domain, context=context)
_columns = {
'invoice_ids': fields.many2many('account.invoice', string="Export", domain=default_domain),
'datas_fname': fields.char("File Name", 128),
'file': fields.binary('File', readonly=True),
'state': fields.selection([
('draft','Draft'),
('saved', 'Saved')], readonly=True),
'use_criteria': fields.boolean('Search by criteria'),
'invoice_with_criteria_ids': fields.many2many('account.invoice', string="Export"),
}
_defaults = {
'invoice_ids': _get_invoices,
'state': 'draft',
}