[fix] problem in o2m
[odoo/odoo.git] / addons / auction / wizard / auction_lots_invoice.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import osv, fields
23 import netsvc
24 from tools.translate import _
25
26 class auction_lots_invoice(osv.osv_memory):
27     _name = 'auction.lots.invoice'
28     _description = "Auction Lots Invoice"
29
30     _columns = {
31         'amount': fields.float('Invoiced Amount', required=True, readonly=True),
32         'amount_topay': fields.float('Amount to pay', required=True, readonly=True),
33         'amount_paid': fields.float('Amount paid', readonly=True),
34         'objects': fields.integer('# of objects', required=True, readonly=True),
35         'ach_uid': fields.many2one('res.partner','Buyer Name', required=True ),
36         'number': fields.integer('Invoice Number'),
37     }
38
39     def default_get(self, cr, uid, fields, context=None):
40         """
41          To get default values for the object.
42          @param self: The object pointer.
43          @param cr: A database cursor
44          @param uid: ID of the user currently logged in
45          @param fields: List of fields for which we want default values
46          @param context: A standard dictionary
47          @return: A dictionary which of fields with values.
48         """
49         if context is None: 
50             context = {}
51         res = super(auction_lots_invoice, self).default_get(cr, uid, fields, context=context)
52         lots = self.pool.get('auction.lots').read(cr, uid, context.get('active_ids', []))
53         auction = self.pool.get('auction.dates').read(cr, uid, [lots[0]['auction_id'][0]])[0]
54
55         price = 0.0
56         price_topay = 0.0
57         price_paid = 0.0
58         for lot in lots:
59             price_lot = lot['obj_price'] or 0.0
60
61             costs = self.pool.get('auction.lots').compute_buyer_costs(cr, uid, [lot['id']])
62             price_lot += costs['amount']
63             price += price_lot
64
65             if lot['ach_uid']:
66                 if uid and (lot['ach_uid'][0]<>uid):
67                     raise osv.except_osv(_('UserError'), _('Two different buyers for the same invoice !\nPlease correct this problem before invoicing'))
68                 uid = lot['ach_uid'][0]
69             elif lot['ach_login']:
70                 refs = self.pool.get('res.partner').search(cr, uid, [('ref','=',lot['ach_login'])])
71                 if len(refs):
72                     uid = refs[-1]
73             if 'ach_pay_id' in lot and lot['ach_pay_id']:
74                 price_paid += price_lot
75                 #*tax
76             else:
77                 price_topay += price_lot
78                 #*tax
79
80     #TODO: recuperer id next invoice (de la sequence)???
81         invoice_number = False
82         for lot in self.pool.get('auction.lots').browse(cr, uid, context.get('active_ids', []), context=context):
83             if 'objects' in fields:
84                 res.update({'objects':len(context.get('active_ids', []))})
85             if 'amount' in fields:
86                 res.update({'amount': price})
87             if 'ach_uid' in fields:
88                 res.update({'ach_uid': uid})
89             if 'amount_topay' in fields:
90                 res.update({'amount_topay':price_topay})
91             if 'amount_paid' in fields:
92                 res.update({'amount_paid':price_paid})
93             if 'number' in fields:
94                 res.update({'number':invoice_number})
95         return res
96
97     def print_report(self, cr, uid, ids, context=None):
98         """
99         Create an invoice report.
100         @param cr: the current row, from the database cursor.
101         @param uid: the current user’s ID for security checks.
102         @param ids: List of Auction lots make invoice buyer’s IDs
103         @return: dictionary of  account invoice form.
104         """
105         if context is None: 
106             context = {}
107         datas = {'ids' : context.get('active_ids',[])}
108         res = self.read(cr, uid, ids, ['number','ach_uid'])
109         res = res and res[0] or {}
110         datas['form'] = res
111         return {
112             'type' : 'ir.actions.report.xml',
113             'report_name':'auction.invoice',
114             'datas' : datas,
115         }
116
117 auction_lots_invoice()
118
119 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: