~openbias/bias-trunk/bias-public-trunk

« back to all changes in this revision

Viewing changes to bias_payment/wizard/wizard_statement_from_invoice.py

  • Committer: Jose Patricio
  • Date: 2011-10-19 03:16:40 UTC
  • Revision ID: josepato@bias.com.mx-20111019031640-05zd7r5lxwx084qu
el push inicial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2008 Camptocamp SA All Rights Reserved. (JGG)
 
5
#
 
6
# WARNING: This program as such is intended to be used by professional
 
7
# programmers who take the whole responsability of assessing all potential
 
8
# consequences resulting from its eventual inadequacies and bugs
 
9
# End users who are looking for a ready-to-use solution with commercial
 
10
# garantees and support are strongly adviced to contract a Free Software
 
11
# Service Company
 
12
#
 
13
# This program is Free Software; you can redistribute it and/or
 
14
# modify it under the terms of the GNU General Public License
 
15
# as published by the Free Software Foundation; either version 2
 
16
# of the License, or (at your option) any later version.
 
17
#
 
18
# This program is distributed in the hope that it will be useful,
 
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
# GNU General Public License for more details.
 
22
#
 
23
# You should have received a copy of the GNU General Public License
 
24
# along with this program; if not, write to the Free Software
 
25
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
26
#
 
27
##############################################################################
 
28
 
 
29
import wizard
 
30
import pooler
 
31
from tools.misc import UpdateableStr
 
32
import time
 
33
 
 
34
FORM = UpdateableStr()
 
35
 
 
36
FIELDS = {
 
37
    'lines': {'string': 'Invoices', 'type': 'many2many',
 
38
        'relation': 'account.move.line'},
 
39
        
 
40
}
 
41
 
 
42
START_FIELD = {
 
43
    'date': {'string': 'Date payment', 'type': 'date','required':True, 'default': lambda *a: time.strftime('%Y-%m-%d')},
 
44
    'journal_id': {'string': 'Journal', 'type': 'many2many', 'relation': 'account.journal', 'domain': '[("type","in",["sale","purchase","cash"])]', 'help': 'This field allow you to choose the accounting journals you want for filtering the invoices. If you left this field empty, it will search on all sale, purchase and cash journals.'},
 
45
}
 
46
 
 
47
START_FORM = '''<?xml version="1.0"?>
 
48
<form string="Import Invoices in Statement">
 
49
    <label string="Choose Journal and Payment Date" colspan="4"/>
 
50
    <field name="date"/>
 
51
    <field name="journal_id" colspan="4"/>
 
52
</form>'''
 
53
 
 
54
def _search_invoices(obj, cr, uid, data, context):
 
55
    pool = pooler.get_pool(cr.dbname)
 
56
    line_obj = pool.get('account.move.line')
 
57
    statement_obj = pool.get('account.bank.statement')
 
58
    journal_obj = pool.get('account.journal')
 
59
 
 
60
    statement = statement_obj.browse(cr, uid, data['id'], context=context)
 
61
    journal_ids = data['form']['journal_id'][0][2]
 
62
 
 
63
    if journal_ids == []:
 
64
        journal_ids = journal_obj.search(cr, uid, [('type', 'in', ('sale','cash','purchase'))], context=context)
 
65
 
 
66
    line_ids = line_obj.search(cr, uid, [
 
67
        ('reconcile_id', '=', False),
 
68
        ('journal_id', 'in', journal_ids),
 
69
        ('account_id.reconcile', '=', True)],
 
70
        #order='date DESC, id DESC', #doesn't work
 
71
        context=context)
 
72
        
 
73
    FORM.string = '''<?xml version="1.0"?>
 
74
<form string="Import Entries">
 
75
    <field name="lines" colspan="4" height="300" width="800" nolabel="1"
 
76
        domain="[('id', 'in', [%s])]"/>
 
77
</form>''' % (','.join([str(x) for x in line_ids]))
 
78
    return {}
 
79
 
 
80
def _populate_statement(obj, cursor, user, data, context):
 
81
    line_ids = data['form']['lines'][0][2]
 
82
    line_date=data['form']['date']
 
83
    if not line_ids:
 
84
        return {}
 
85
 
 
86
    pool = pooler.get_pool(cursor.dbname)
 
87
    line_obj = pool.get('account.move.line')
 
88
    statement_obj = pool.get('account.bank.statement')
 
89
    statement_line_obj = pool.get('account.bank.statement.line')
 
90
    currency_obj = pool.get('res.currency')
 
91
    statement_reconcile_obj = pool.get('account.bank.statement.reconcile')
 
92
 
 
93
    statement = statement_obj.browse(cursor, user, data['id'], context=context)
 
94
    # for each selected move lines
 
95
    for line in line_obj.browse(cursor, user, line_ids, context=context):
 
96
        ctx = context.copy()
 
97
        #  take the date for computation of currency => use payment date
 
98
        # if line.date_maturity:
 
99
        #     ctx['date'] = line.date_maturity 
 
100
        # else:
 
101
        ctx['date'] = line_date
 
102
        amount = 0.0
 
103
        if line.amount_currency:
 
104
            amount = currency_obj.compute(cursor, user, line.currency_id.id,
 
105
                statement.currency.id, line.amount_currency, context=ctx)
 
106
        else:
 
107
            if line.debit > 0:
 
108
                amount=line.debit
 
109
            elif line.credit > 0:
 
110
                amount=-line.credit
 
111
        reconcile_id = statement_reconcile_obj.create(cursor, user, {
 
112
            'line_ids': [(6, 0, [line.id])]
 
113
            }, context=context)
 
114
        if line.journal_id.type == 'sale':
 
115
            type = 'customer'
 
116
        elif line.journal_id.type == 'purchase':
 
117
            type = 'supplier'
 
118
        else:
 
119
            type = 'general'
 
120
        if not line.cost_center_id and line.cost_center_id.id:
 
121
            raise wizard.except_wizard(_('Error !'), _("The cost center field is required in the movements'%s'!") % (line.name,))
 
122
        statement_line_obj.create(cursor, user, {
 
123
            'name': line.name or '?',
 
124
            'amount': amount,
 
125
            'type': type,
 
126
            'partner_id': line.partner_id.id,
 
127
            'account_id': line.account_id.id,
 
128
            'statement_id': statement.id,
 
129
            'ref': line.ref,
 
130
            'reconcile_id': reconcile_id,
 
131
            'date':line_date, #time.strftime('%Y-%m-%d'), #line.date_maturity or,
 
132
            'cost_center_id': line.cost_center_id and line.cost_center_id.id,
 
133
            }, context=context)
 
134
    return {}
 
135
    
 
136
    
 
137
class PopulateStatementFromInvoice(wizard.interface):
 
138
    """
 
139
    Populate the current statement with selected invoices
 
140
    """
 
141
    states = {
 
142
        'init': {
 
143
            'actions': [],
 
144
            'result': {
 
145
                'type': 'form',
 
146
                'arch': START_FORM,
 
147
                'fields':START_FIELD,
 
148
                'state': [
 
149
                    ('end', '_Cancel'),
 
150
                    ('go', '_Go', '', True),
 
151
                ]
 
152
            },
 
153
        },
 
154
        'go': {
 
155
            'actions': [_search_invoices],
 
156
            'result': {
 
157
                'type': 'form',
 
158
                'arch': FORM,
 
159
                'fields': FIELDS,
 
160
                'state': [
 
161
                    ('end', '_Cancel','', True),
 
162
                    ('finish', 'O_k','', True)
 
163
                ]
 
164
            },
 
165
        },
 
166
 
 
167
        'finish': {
 
168
        'actions': [],
 
169
        'result': {
 
170
            'type': 'action',
 
171
            'action': _populate_statement,
 
172
            'state': 'end'
 
173
        },
 
174
    },
 
175
    }
 
176
PopulateStatementFromInvoice('populate_statement_from_invoice')
 
177
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: