1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 NovaPoint Group LLC (<http://www.novapointgroup.com>)
6
# Copyright (C) 2004-2010 OpenERP SA (<http://www.openerp.com>)
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>
21
##############################################################################
23
from osv import osv, fields
24
from tools.translate import _
26
class print_check(osv.osv_memory):
31
_description = "Print Check"
33
'name': fields.char('Next Check Number', size=32, help='Next check number'),
34
'nxt_seq': fields.char('Next Check Number', size=32, help='Next check number'),
35
'new_no': fields.integer('Update Check Number', help= 'Enter new check number here if you wish to update'),
36
'preprint_msg': fields.text('Message'),
37
'status': fields.selection([('voided', 'Voided'), ('stop_pay', 'Stop Pay Placed'), ('lost', 'Lost'), ('unk', 'Unknown')], 'Status'),
38
'pre_printed': fields.boolean('Pre Printed'),
39
'state':fields.selection([
41
('printed_reprint', 'printed_reprint'),
42
('printed', 'printed'),
43
('reprint_new', 'reprint_new'),
44
('reprint', 'reprint'),
45
('update_check_no', 'update_check_no'),
46
('do_update', 'do_update'),
47
('do_action', 'do_action'),
50
('bottom', 'bottom')], 'State'),
51
'print_new': fields.boolean('Print a New Check'),
52
'reprint': fields.boolean('Reprint existing check'),
53
'update_check_no': fields.boolean('Update Supplier Payment’s Check No'),
56
def get_id(self, cr, uid, sequence_id, test='id', context=None):
58
Function to find next sequence number
60
seq_pool = self.pool.get('ir.sequence')
61
assert test in ('code', 'id')
62
company_id = self.pool.get('res.users').read(cr, uid, uid, ['company_id'], context=context)['company_id'][0] or None
63
cr.execute('''SELECT id, number_next, prefix, suffix, padding
67
AND (company_id = %%s or company_id is NULL)
68
ORDER BY company_id, id
69
FOR UPDATE NOWAIT''' % test,
70
(sequence_id, company_id))
71
res = cr.dictfetchone()
73
if res['number_next']:
74
return (seq_pool._process(res['prefix']) + '%%0%sd' % res['padding'] % res['number_next'] + seq_pool._process(res['suffix']),
77
return (seq_pool._process(res['prefix']) + seq_pool._process(res['suffix']), 0)
80
def _get_nxt_no(self, cr, uid, context=None):
82
Function to find next sequence number and update sequence
86
if 'active_ids' in context:
87
voucher_id = self.pool.get('account.voucher').browse(cr, uid, context['active_ids'], context=context)[0]
88
if not voucher_id.journal_id.check_sequence:
89
raise osv.except_osv(_('Warning!'), _('Please add "Check Sequence" for journal %s.'%str(voucher_id.journal_id.name)))
90
res = self.get_id(cr, uid, voucher_id.journal_id.check_sequence.id, test='id', context=context)
95
def _get_new_no(self, cr, uid, context=None):
97
Function to get the next number used to generate sequence
102
if 'active_ids' in context:
103
voucher_id = self.pool.get('account.voucher').browse(cr, uid, context['active_ids'][0], context=context)
104
res = self.get_id(cr, uid, voucher_id.journal_id.check_sequence.id , test='id', context=context)
105
return res and res[1]
107
def _get_state(self, cr, uid, context=None):
109
Function to initialise state
114
if not (context.get('active_model') == 'account.voucher' and context.get('active_ids')):
115
raise osv.except_osv(_('Warning!'), _('Wrong model or unable to find active ids'))
116
for voucher_id in self.pool.get('account.voucher').browse(cr, uid, context['active_ids'], context=context):
117
if voucher_id.chk_seq and voucher_id.journal_id.use_preprint_check:
119
elif voucher_id.chk_seq and state != 'printed':
120
state = 'printed_reprint'
121
if voucher_id.state != 'posted':
122
raise osv.except_osv(_('Warning!'), _('Payment is not posted. Please Validate Payment First!'))
123
if not voucher_id.journal_id.check_sequence:
124
raise osv.except_osv(_('Warning!'), _('Please add "Check Sequence" for journal %s.'%str(voucher_id.journal_id.name)))
127
def _get_pre_printed(self, cr, uid, context=None):
129
Function to check whether the check is pre printed or not
133
if not (context.get('active_model') == 'account.voucher' and context.get('active_ids')):
134
raise osv.except_osv(_('Warning!'), _('Wrong model or unable to find active ids'))
135
for voucher_id in self.pool.get('account.voucher').browse(cr, uid, context['active_ids'], context=context):
136
return voucher_id.journal_id.use_preprint_check
139
def _get_msg(self, cr, uid, context=None):
141
Function to initialize preprint_msg
146
msg1 = 'This Payment has already been paid with check:\n'
147
msg2 = 'These Payments have already been paid with checks:\n'
148
msg3 = 'Some of these Payments have already been paid with checks:\n'
151
voucher_ids = self.pool.get('account.voucher').browse(cr, uid, context.get('active_ids', []), context=context)
152
for voucher in voucher_ids:
154
chk_nos.append(str(voucher.chk_seq))
156
if len(chk_nos) == 1:
157
msg = msg1 + str(chk_nos[0])
158
elif len(chk_nos) == len(context.get('active_ids')):
159
msg = msg2 + '\n'.join(chk_nos)
161
msg = msg3 + '\n'.join(chk_nos)
165
'name': 'Check sequence',
166
'nxt_seq': _get_nxt_no,
167
'new_no': _get_new_no,
168
'preprint_msg': _get_msg,
169
'pre_printed': _get_pre_printed,
173
def check_option(self, cr, uid, ids, context=None):
175
Function to check the option if check is already printed
179
data = self.browse(cr, uid, ids[0], context=context)
181
msg = 'What happened to the existing check no ' + str(data.preprint_msg.split(':\n')[1]).replace('\n', ', ') + '?'
182
self.write(cr, uid, ids, {'preprint_msg': msg, 'state': 'reprint_new'}, context=context)
184
company_obj = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
185
if company_obj.check_layout == 'top':
186
report_name = 'account.print.check.top'
187
if company_obj.check_layout == 'middle':
188
report_name = 'account.print.check.middle'
189
if company_obj.check_layout == 'bottom':
190
report_name = 'account.print.check.bottom'
193
'type': 'ir.actions.report.xml',
194
'report_name': report_name,
196
'model': 'account.voucher',
197
'id': context.get('active_ids', False) and context['active_ids'][0],
198
'ids': context.get('active_ids', []),
203
elif data.update_check_no:
204
msg = 'What happened to the existing check no ' + str(data.preprint_msg.split(':\n')[1]).replace('\n', ', ') + '?'
205
self.write(cr, uid, ids, {'preprint_msg': msg, 'state': 'update_check_no'}, context=context)
206
return {'nodestroy': True}
208
def get_nxt_seq(self, cr, uid, check_sequence_id, test='id', context=None):
210
Function to find the next check number without conflict
212
next_seq = self.pool.get('ir.sequence').get_id(cr, uid, voucher.journal_id.check_sequence.id, test='id', context=context)
213
while(self.pool.get('check.log').search(cr, uid, [('check_no', '=', next_seq)], context=context)):
214
next_seq = self.pool.get('ir.sequence').get_id(cr, uid, voucher.journal_id.check_sequence.id, test='id', context=context)
217
def print_check(self, cr, uid, ids, context=None):
219
Function to print check
224
if not context.get('active_ids'):
228
voucher_objs = self.pool.get('account.voucher').browse(cr, uid, context['active_ids'], context=context)
229
data = self.browse(cr, uid, ids[0], context=context)
230
#FIXME: Please check whether we need 3 loops here.
231
for voucher in voucher_objs:
232
if voucher.journal_id.check_sequence:
233
seq[voucher.journal_id.check_sequence.id] = True
235
raise wizard.except_wizard(_('Warning'), _('Please add "Check Sequence" for journal %s.'%str(voucher.journal_id.name)))
237
#FIXME: Please check whether this call is necessary
238
nxt_no = self.pool.get('ir.sequence').read(cr, uid, seq_id, ['number_next'], context=context)['number_next']
239
self.pool.get('ir.sequence').write(cr, uid, [seq_id], {'number_next': data.new_no}, context=context)
241
for voucher in voucher_objs:
242
next_seq = self.pool.get('ir.sequence').get_id(cr, uid, voucher.journal_id.check_sequence.id, test='id', context=context)
243
self.pool.get('account.voucher').write(cr, uid, [voucher.id],{'chk_seq': next_seq, 'chk_status': True}, context=context)
244
self.pool.get('check.log').create(cr, uid, {'name': voucher.id, 'status': 'active', 'check_no': next_seq}, context=context)
246
if data.state == 'print':
247
company_obj = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
248
if company_obj.check_layout == 'top':
249
report_name = 'account.print.check.top'
250
if company_obj.check_layout == 'middle':
251
report_name = 'account.print.check.middle'
252
if company_obj.check_layout == 'bottom':
253
report_name = 'account.print.check.bottom'
255
'type': 'ir.actions.report.xml',
256
'report_name': report_name,
258
'model': 'account.voucher',
259
'id': context.get('active_ids', False) and context.get('active_ids')[0],
260
'ids': context.get('active_ids', []),
268
def onchange_chkbx(self, cr, uid, ids, value, field, context=None):
270
Function to update check box print_new, reprint and update_check_no
275
'update_check_no': False,
279
return {'value': ret}
281
def _update_check_log_status(self, cr, uid, ids, context=None):
284
data = self.browse(cr, uid, ids[0], context=context)
285
voucher_ids = self.pool.get('account.voucher').browse(cr, uid, context.get('active_ids', []), context=context)
286
check_log_obj = self.pool.get('check.log')
288
for voucher in voucher_ids:
290
chk_log_ids = check_log_obj.search(cr, uid, [('check_no', '=', voucher.chk_seq), ('status', '=', 'active')], context=context)
291
check_log_obj.write(cr, uid, chk_log_ids, {'status': data.status or 'unk'}, context=context)
293
def update_no(self, cr, uid, ids, context=None):
295
Function to update check log status
297
self._update_check_log_status(cr, uid, ids, context=context)
298
self.write(cr, uid, ids, {'preprint_msg': '', 'state': 'do_update'}, context=context)
300
def print_new(self, cr, uid, ids, context=None):
302
Function to update check log status
304
self._update_check_log_status(cr, uid, ids, context=context)
305
self.write(cr, uid, ids,{'preprint_msg': '', 'state': 'print'}, context=context)
309
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: