~serpent-consulting-services/openerp-usa/fix-shipping_api_ups_cc

« back to all changes in this revision

Viewing changes to account_check_writing/wizard/check_print.py

  • Committer: npgllc
  • Date: 2012-08-02 17:13:27 UTC
  • Revision ID: npgllc-20120802171327-2xgyyjjb5d1kx26y
Removed all the 6.0 compatible modules

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 NovaPoint Group LLC (<http://www.novapointgroup.com>)
6
 
#    Copyright (C) 2004-2010 OpenERP SA (<http://www.openerp.com>)
7
 
#
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.
12
 
#
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.
17
 
#
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/>
20
 
#
21
 
##############################################################################
22
 
 
23
 
from osv import osv, fields
24
 
from tools.translate import _
25
 
 
26
 
class print_check(osv.osv_memory):
27
 
    """
28
 
    Check Printing
29
 
    """
30
 
    _name = "print.check"
31
 
    _description = "Print Check"
32
 
    _columns = {
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([
40
 
            ('print', 'print'),
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'),
48
 
            ('top', 'top'),
49
 
            ('middle', 'middle'),
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'),
54
 
        }
55
 
 
56
 
    def get_id(self, cr, uid, sequence_id, test='id', context=None):
57
 
        """
58
 
        Function to find next sequence number
59
 
        """
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
64
 
                      FROM ir_sequence
65
 
                      WHERE %s=%%s
66
 
                      AND active=true
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()
72
 
        if res:
73
 
            if res['number_next']:
74
 
                return (seq_pool._process(res['prefix']) + '%%0%sd' % res['padding'] % res['number_next'] + seq_pool._process(res['suffix']),
75
 
                        res['number_next'])
76
 
            else:
77
 
                return (seq_pool._process(res['prefix']) + seq_pool._process(res['suffix']), 0)
78
 
        return False
79
 
 
80
 
    def _get_nxt_no(self, cr, uid, context=None):
81
 
        """
82
 
        Function to find next sequence number and update sequence
83
 
        """
84
 
        if context is None:
85
 
            context = {}
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)
91
 
            return res and res[0]
92
 
        else:
93
 
            return False
94
 
 
95
 
    def _get_new_no(self, cr, uid, context=None):
96
 
        """
97
 
        Function to get the next number used to generate sequence
98
 
        """
99
 
        if context is None:
100
 
            context = {}
101
 
        res = []
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]
106
 
 
107
 
    def _get_state(self, cr, uid, context=None):
108
 
        """
109
 
        Function to initialise state
110
 
        """
111
 
        if context is None:
112
 
            context = {}
113
 
        state = 'print'
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:
118
 
                state = 'printed'
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)))
125
 
        return state
126
 
 
127
 
    def _get_pre_printed(self, cr, uid, context=None):
128
 
        """
129
 
        Function to check whether the check is pre printed or not
130
 
        """
131
 
        if context is None:
132
 
            context = {}
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
137
 
        return False
138
 
 
139
 
    def _get_msg(self, cr, uid, context=None):
140
 
        """
141
 
        Function to initialize preprint_msg
142
 
        """
143
 
        if context is None:
144
 
            context = {}
145
 
 
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'
149
 
 
150
 
        chk_nos = []
151
 
        voucher_ids = self.pool.get('account.voucher').browse(cr, uid, context.get('active_ids', []), context=context)
152
 
        for voucher in voucher_ids:
153
 
            if voucher.chk_seq:
154
 
                chk_nos.append(str(voucher.chk_seq))
155
 
        msg = ''
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)
160
 
        else:
161
 
            msg = msg3 + '\n'.join(chk_nos)
162
 
        return msg
163
 
 
164
 
    _defaults = {
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,
170
 
        'state': _get_state
171
 
        }
172
 
 
173
 
    def check_option(self, cr, uid, ids, context=None):
174
 
        """
175
 
        Function to check the option if check is already printed
176
 
        """
177
 
        if context is None:
178
 
            context = {}
179
 
        data = self.browse(cr, uid, ids[0], context=context)
180
 
        if data.print_new:
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)
183
 
        elif data.reprint:
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'
191
 
 
192
 
            return {
193
 
            'type': 'ir.actions.report.xml',
194
 
            'report_name': report_name,
195
 
            'datas': {
196
 
                'model': 'account.voucher',
197
 
                'id': context.get('active_ids', False) and context['active_ids'][0],
198
 
                'ids': context.get('active_ids', []),
199
 
                'report_type': 'pdf'
200
 
                },
201
 
            'nodestroy': False
202
 
            }
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}
207
 
 
208
 
    def get_nxt_seq(self, cr, uid, check_sequence_id, test='id', context=None):
209
 
        """
210
 
        Function to find the next check number without conflict
211
 
        """
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)
215
 
        return next_seq
216
 
 
217
 
    def print_check(self, cr, uid, ids, context=None):
218
 
        """
219
 
        Function to print check
220
 
        """
221
 
        if context is None:
222
 
            context = {}
223
 
 
224
 
        if not context.get('active_ids'):
225
 
            return []
226
 
 
227
 
        seq = {}
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
234
 
            else:
235
 
                raise wizard.except_wizard(_('Warning'), _('Please add "Check Sequence" for journal %s.'%str(voucher.journal_id.name)))
236
 
        for seq_id in seq:
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)
240
 
 
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)
245
 
 
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'
254
 
            return {
255
 
                'type': 'ir.actions.report.xml',
256
 
                'report_name': report_name,
257
 
                'datas': {
258
 
                    'model': 'account.voucher',
259
 
                    'id': context.get('active_ids', False) and context.get('active_ids')[0],
260
 
                    'ids': context.get('active_ids', []),
261
 
                    'report_type': 'pdf'
262
 
                    },
263
 
                'nodestroy': False
264
 
                }
265
 
        else:
266
 
            return {}
267
 
 
268
 
    def onchange_chkbx(self, cr, uid, ids, value, field, context=None):
269
 
        """
270
 
        Function to update check box print_new, reprint and update_check_no
271
 
        """
272
 
        ret = {
273
 
            'print_new': False,
274
 
            'reprint': False,
275
 
            'update_check_no': False,
276
 
            }
277
 
        if value:
278
 
            ret[field] = True
279
 
        return {'value': ret}
280
 
 
281
 
    def _update_check_log_status(self, cr, uid, ids, context=None):
282
 
        if context is None:
283
 
            context = {}
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')
287
 
        if data.status:
288
 
            for voucher in voucher_ids:
289
 
                if voucher.chk_seq:
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)
292
 
 
293
 
    def update_no(self, cr, uid, ids, context=None):
294
 
        """
295
 
        Function to update check log status
296
 
        """
297
 
        self._update_check_log_status(cr, uid, ids, context=context)
298
 
        self.write(cr, uid, ids, {'preprint_msg': '', 'state': 'do_update'}, context=context)
299
 
 
300
 
    def print_new(self, cr, uid, ids, context=None):
301
 
        """
302
 
        Function to update check log status
303
 
        """
304
 
        self._update_check_log_status(cr, uid, ids, context=context)
305
 
        self.write(cr, uid, ids,{'preprint_msg': '', 'state': 'print'}, context=context)
306
 
 
307
 
print_check()
308
 
 
309
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: