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

« back to all changes in this revision

Viewing changes to bias_payment_wo_cc/wizard/wizard_import_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
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    $Id$
 
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
import wizard
 
23
import pooler
 
24
from tools.misc import UpdateableStr
 
25
import time
 
26
 
 
27
 
 
28
FORM = UpdateableStr()
 
29
 
 
30
FIELDS = {
 
31
    'entries': {'string':'Entries', 'type':'many2many', 'relation': 'account.move.line',},
 
32
}
 
33
field_duedate={
 
34
    'duedate': {'string':'Due Date', 'type':'date','required':True, 'default': lambda *a: time.strftime('%Y-%m-%d'),},
 
35
    }
 
36
arch_duedate='''<?xml version="1.0"?>
 
37
<form string="Search Payment lines">
 
38
    <field name="duedate" />
 
39
</form>'''
 
40
 
 
41
 
 
42
def _get_date(self, cr, uid, data, context):
 
43
    pool = pooler.get_pool(cr.dbname)
 
44
    data['form']['duedate'] = pool.get('payment.cheque').browse(cr, uid, data['id'], context=context).date
 
45
    return data['form']
 
46
 
 
47
def search_entries(self, cr, uid, data, context):
 
48
    search_due_date=data['form']['duedate']
 
49
 
 
50
    pool = pooler.get_pool(cr.dbname)
 
51
    chk_obj = pool.get('payment.cheque')
 
52
    line_obj = pool.get('account.move.line')
 
53
 
 
54
    cheque = chk_obj.browse(cr, uid, data['id'],
 
55
            context=context)
 
56
    ctx = ''
 
57
    if cheque.mode:
 
58
        ctx = '''context="{'journal_id': %d}"''' % cheque.mode.journal.id
 
59
 
 
60
    # Search for move line to pay:
 
61
    domain = [('reconcile_id', '=', False),('account_id.type', 'in', ['payable','receivable']),('partner_id','=',cheque.partner_id.id),
 
62
                ('journal_id.type','!=','cash')]
 
63
    #('amount_to_pay', '>', 0)
 
64
    domain = domain + ['|',('date_maturity','<',search_due_date),('date_maturity','=',False)]
 
65
    line_ids = line_obj.search(cr, uid, domain, context=context)
 
66
    FORM.string = '''<?xml version="1.0"?>
 
67
<form string="Populate Payment:">
 
68
    <field name="entries" colspan="4" height="300" width="1000" nolabel="1"
 
69
        domain="[('id', 'in', [%s])]" %s/>
 
70
</form>''' % (','.join([str(x) for x in line_ids]), ctx)
 
71
    return {}
 
72
 
 
73
def create_payment(self, cr, uid, data, context):
 
74
    line_ids= data['form']['entries'][0][2]
 
75
    if not line_ids: return {}
 
76
    pool= pooler.get_pool(cr.dbname)
 
77
    order_obj = pool.get('payment.cheque')
 
78
    bnk_obj = pool.get('res.partner.bank')
 
79
    line_obj = pool.get('account.move.line')
 
80
    cur_obj = pool.get('res.currency')
 
81
    payment = order_obj.browse(cr, uid, data['id'], context=context)
 
82
    t = payment.mode and payment.mode.type.id or None
 
83
    line2bank = pool.get('account.move.line').line2bank(cr, uid,
 
84
            line_ids, t, context)
 
85
    chk_name = payment.concept or ''
 
86
    ## Finally populate the current payment with new lines:
 
87
    partial_rec = []
 
88
    for line in line_obj.browse(cr, uid, line_ids, context=context):
 
89
        if chk_name:
 
90
            chk_name += ', '
 
91
        chk_name += line.ref or '/'
 
92
        if line.reconcile_partial_id:
 
93
            partial_rec = [x.id for x in line.reconcile_partial_id.line_partial_ids]
 
94
            partial_rec.remove(line.id)
 
95
    order_obj.write(cr, uid, data['id'], {'concept': chk_name, 'partial_line_id': map(lambda x: (4,x,False), partial_rec)})
 
96
    for line in line_obj.browse(cr, uid, line_ids, context=context):
 
97
        amount_currency, pay_rate, rate = 0, 1, 1
 
98
        date_to_pay = payment.date
 
99
        currency = line.currency_id.id or (line.invoice and line.invoice.currency_id.id) or line.account_id.company_id.currency_id.id
 
100
        company_currency = line.account_id.company_id.currency_id.id
 
101
        journal_currency = payment.mode.journal.currency.id
 
102
        calc_amount = amount_currency = line.amount_to_pay
 
103
        calc_currency = company_currency
 
104
        if currency == journal_currency != company_currency:
 
105
            amount_currency = abs((line.invoice and (line.credit > 0 and -line.invoice.residual or line.invoice.residual)) \
 
106
                        or (line.amount_to_pay and (line.credit > 0 and -line.amount_to_pay or line.amount_to_pay)) \
 
107
                        or line.amount_currency or (line.debit - line.credit) or 0.0)
 
108
            pay_rate = cur_obj._current_rate(cr, uid, [company_currency], 'rate', arg=None, context={'date':date_to_pay})[company_currency]
 
109
            calc_amount = amount_currency
 
110
            calc_currency = currency
 
111
        elif currency != company_currency:
 
112
            amount_currency = abs((line.invoice and (line.credit > 0 and -line.invoice.residual or line.invoice.residual)) \
 
113
                        or (line.amount_to_pay and (line.credit > 0 and -line.amount_to_pay or line.amount_to_pay)) \
 
114
                        or line.amount_currency or (line.debit - line.credit) or 0.0)
 
115
            pay_rate = cur_obj._current_rate(cr, uid, [company_currency], 'rate', arg=None, context={'date':date_to_pay})[company_currency]
 
116
            rate = abs((line.debit - line.credit) / amount_currency)
 
117
            calc_amount = cur_obj.compute(cr, uid, currency, company_currency, amount_currency, context={'date': date_to_pay})
 
118
        pool.get('payment.line').create(cr,uid,{
 
119
            'move_line_id': line.id,
 
120
            'calc_amount': calc_amount or 0.0,
 
121
            'amount_currency': amount_currency or 0.0,
 
122
            'currency': currency,
 
123
            'calc_currency': calc_currency,
 
124
            'bank_id': line2bank.get(line.id),
 
125
            'cheque_id': payment.id,
 
126
            'partner_id': line.partner_id and line.partner_id.id or False,
 
127
            'communication': line.ref or '/',
 
128
            'date': date_to_pay,
 
129
            'rate': rate,
 
130
            'pay_rate': pay_rate,
 
131
            'account_id': line.account_id.id,
 
132
            'company_currency': company_currency,
 
133
            }, context=context)
 
134
    return {}
 
135
 
 
136
class wizard_invoice_import(wizard.interface):
 
137
    states = {
 
138
 
 
139
        'init': {
 
140
            'actions': [_get_date],
 
141
            'result': {
 
142
                'type': 'form',
 
143
                'arch': arch_duedate,
 
144
                'fields':field_duedate,
 
145
                'state': [
 
146
                    ('end','_Cancel'),
 
147
                    ('search','_Search', '', True)
 
148
                ]
 
149
            },
 
150
         },
 
151
 
 
152
        'search': {
 
153
            'actions': [search_entries],
 
154
            'result': {
 
155
                'type': 'form',
 
156
                'arch': FORM,
 
157
                'fields': FIELDS,
 
158
                'state': [
 
159
                    ('end','_Cancel'),
 
160
                    ('create','_Add to payment order', '', True)
 
161
                ]
 
162
            },
 
163
         },
 
164
        'create': {
 
165
            'actions': [],
 
166
            'result': {
 
167
                'type': 'action',
 
168
                'action': create_payment,
 
169
                'state': 'end'}
 
170
            },
 
171
        }
 
172
 
 
173
wizard_invoice_import('import_invoice')
 
174
 
 
175
 
 
176
 
 
177
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
178