[IMP] STOCK :RD Project- old style wizard conversion-add one new Func get_return_history
[odoo/odoo.git] / addons / stock / wizard / stock_invoice_onshipping.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 fields, osv
23
24 from tools.translate import _
25
26 class stock_invoice_onshipping(osv.osv_memory):
27
28     def _get_journal_id(self, cr, uid, context=None):
29         if context is None:
30             context = {}
31
32         model = context.get('active_model')
33         if not model or model != 'stock.picking':
34             return []
35
36         model_pool = self.pool.get(model)
37         acct_obj = self.pool.get('account.journal')
38         res_ids = context and context.get('active_ids', [])
39         vals=[]
40         pick_types = list(set(map(lambda x: x.type, model_pool.browse(cr, uid, res_ids, context=context))))
41         for type in pick_types:
42             if type == 'out':
43                value = acct_obj.search(cr, uid, [('type', 'in',('sale','purchase_refund') )])
44                for jr_type in acct_obj.browse(cr, uid, value, context=context):
45                    t1 = jr_type.id,jr_type.name
46                    vals.append(t1)
47
48             elif type == 'in':
49                value = acct_obj.search(cr, uid, [('type', 'in',('purchase','sale_refund') )])
50                for jr_type in acct_obj.browse(cr, uid, value, context=context):
51                    t1 = jr_type.id,jr_type.name
52                    vals.append(t1)
53             else:
54                value = acct_obj.search(cr, uid, [('type', 'in',('cash','bank','general','situation') )])
55                for jr_type in acct_obj.browse(cr, uid, value, context=context):
56                    t1 = jr_type.id,jr_type.name
57                    vals.append(t1)
58         return vals
59
60
61     _name = "stock.invoice.onshipping"
62     _description = "Stock Invoice Onshipping"
63
64
65     _columns = {
66         'journal_id': fields.selection(_get_journal_id, 'Destination Journal',required=True),
67         'group': fields.boolean("Group by partner"),
68         'invoice_date': fields.date('Invoiced date'),
69     }
70
71
72     def view_init(self, cr, uid, fields_list, context=None):
73         if context is None:
74             context = {}
75         res = super(stock_invoice_onshipping, self).view_init(cr, uid, fields_list, context=context)
76         pick_obj = self.pool.get('stock.picking')
77         count = 0
78         active_ids = context.get('active_ids',[])
79         for pick in pick_obj.browse(cr, uid, active_ids, context=context):
80             if pick.invoice_state != '2binvoiced':
81                 count += 1
82         if len(active_ids) == 1 and count:
83             raise osv.except_osv(_('Warning !'), _('This picking list does not require invoicing.'))
84         if len(active_ids) == count:
85             raise osv.except_osv(_('Warning !'), _('None of these picking lists require invoicing.'))
86         return res
87
88     def open_invoice(self, cr, uid, ids, context=None):
89         if context is None:
90             context = {}
91         invoice_ids = []
92         data_pool = self.pool.get('ir.model.data')
93         res = self.create_invoice(cr, uid, ids, context=context)
94         invoice_ids += res.values()
95         inv_type = context.get('inv_type', False)
96         action_model = False
97         action = {}
98         if not invoice_ids:
99             raise osv.except_osv(_('Error'), _('No Invoices were created'))
100         if inv_type == "out_invoice":
101             action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree1")
102         elif inv_type == "in_invoice":
103             action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree2")
104         elif inv_type == "out_refund":
105             action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree3")
106         elif inv_type == "in_refund":
107             action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree4")
108         if action_model:
109             action_pool = self.pool.get(action_model)
110             action = action_pool.read(cr, uid, action_id, context=context)
111             action['domain'] = "[('id','in', ["+','.join(map(str,invoice_ids))+"])]"
112         return action
113
114     def create_invoice(self, cr, uid, ids, context=None):
115         if context is None:
116             context = {}
117         picking_pool = self.pool.get('stock.picking')
118         onshipdata_obj = self.read(cr, uid, ids, ['journal_id', 'group', 'invoice_date'])
119         if context.get('new_picking', False):
120             onshipdata_obj['id'] = onshipdata_obj.new_picking
121             onshipdata_obj[ids] = onshipdata_obj.new_picking
122         context['date_inv'] = onshipdata_obj[0]['invoice_date']
123         active_ids = context.get('active_ids', [])
124         active_picking = picking_pool.browse(cr, uid, context.get('active_id',False), context=context)
125         inv_type = picking_pool._get_invoice_type(active_picking)
126         context['inv_type'] = inv_type
127         res = picking_pool.action_invoice_create(cr, uid, active_ids,
128               journal_id = onshipdata_obj[0]['journal_id'],
129               group = onshipdata_obj[0]['group'],
130               type = None,
131               context=context)
132         return res
133
134 stock_invoice_onshipping()
135
136 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: