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