[IMP]
[odoo/odoo.git] / addons / auction / wizard / wizard_pay.py
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
23 import wizard
24 import netsvc
25 import netsvc
26 import osv
27 import time
28 import pooler
29 pay_form = '''<?xml version="1.0"?>
30 <form string="Pay objects">
31     <field name="amount"/>
32     <field name="statement_id1" domain="[('state','=','draft')]"/>
33     <field name="amount2"/>
34     <field name="statement_id2"  domain="[('state','=','draft')]"/>
35     <field name="amount3"/>
36     <field name="statement_id3"  domain="[('state','=','draft')]"/>
37     <newline/>
38     <field name="buyer_id"/>
39     <field name="total"/>
40 </form>'''
41
42 def _start(self,cr,uid,data,context):
43     pool = pooler.get_pool(cr.dbname)
44     rec=pool.get('auction.lots').browse(cr,uid,data['ids'],context)
45     amount1=0.0
46     for r in rec:
47         amount1+=r.buyer_price
48         buyer= r and r.ach_uid.id or False
49         if r.is_ok:
50             raise wizard.except_wizard('Error !', 'Some lots of the selection are already paid.')
51     return {'amount':amount1, 'total':amount1,'buyer_id':buyer}
52
53 pay_fields = {
54     'amount': {'string': 'Amount paid', 'type':'float'},
55     'buyer_id': {'string': 'Buyer', 'type': 'many2one', 'relation':'res.partner'},
56     'statement_id1': {'string':'Statement', 'type':'many2one', 'required':True, 'relation':'account.bank.statement'},
57     'amount2': {'string': 'Amount paid', 'type':'float'},
58     'statement_id2': {'string':'Statement', 'type':'many2one', 'relation':'account.bank.statement'},
59     'amount3': {'string': 'Amount paid', 'type':'float'},
60     'statement_id3': {'string':'Statement', 'type':'many2one', 'relation':'account.bank.statement'},
61     'total': {'string': 'Amount to paid', 'type':'float','readonly':True}
62 }
63
64 def _pay_and_reconcile(self, cr, uid, data, context):
65     if not abs(data['form']['total'] - (data['form']['amount']+data['form']['amount2']+data['form']['amount3']))<0.01:
66         rest=data['form']['total']-(data['form']['amount']+data['form']['amount2']+data['form']['amount3'])
67         raise wizard.except_wizard('Payment aborted !', 'You should pay all the total: "%.2f" are missing to accomplish the payment.' %(round(rest,2)))
68     
69     pool = pooler.get_pool(cr.dbname)
70     lots = pool.get('auction.lots').browse(cr,uid,data['ids'],context)
71     ref_bk_s=pooler.get_pool(cr.dbname).get('account.bank.statement.line')
72     
73     for lot in lots:
74         if data['form']['buyer_id']:
75             pool.get('auction.lots').write(cr,uid,[lot.id],{'ach_uid':data['form']['buyer_id']})
76         if not lot.auction_id:
77             raise wizard.except_wizard('Error !', 'No auction date for "%s": Please set one.'%(lot.name))
78         pool.get('auction.lots').write(cr,uid,[lot.id],{'is_ok':True})
79     
80     for st,stamount in [('statement_id1','amount'),('statement_id2','amount2'),('statement_id3','amount3')]:
81         if data['form'][st]:
82             new_id=ref_bk_s.create(cr,uid,{
83                 'name':'Buyer:'+str(lot.ach_login or '')+', auction:'+ lots[0].auction_id.name,
84                 'date': time.strftime('%Y-%m-%d'),
85                 'partner_id':data['form']['buyer_id'] or False,
86                 'type':'customer',
87                 'statement_id':data['form'][st],
88                 'account_id':lot.auction_id.acc_income.id,
89                 'amount':data['form'][stamount]
90             })
91             for lot in lots:
92                 pool.get('auction.lots').write(cr,uid,[lot.id],{'statement_id':[(4,new_id)]})
93     return {}
94
95
96 class wiz_auc_lots_pay(wizard.interface):
97     states = {
98         'init': {
99             'actions': [_start],
100             'result': {'type': 'form', 'arch':pay_form, 'fields': pay_fields, 'state':[('end','Cancel'),('pay','Pay')]}
101         },
102         'pay': {
103         'actions': [_pay_and_reconcile],
104         'result': {'type': 'state', 'state':'end'}
105         }}
106 wiz_auc_lots_pay('auction.pay.buy')
107
108
109 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
110