1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from openerp.osv import fields, osv
24 from openerp.tools.translate import _
26 class stock_invoice_onshipping(osv.osv_memory):
28 def _get_journal(self, cr, uid, context=None):
29 res = self._get_journal_id(cr, uid, context=context)
34 def _get_journal_id(self, cr, uid, context=None):
38 model = context.get('active_model')
39 if not model or 'stock.picking' not in model:
42 model_pool = self.pool[model]
43 journal_obj = self.pool.get('account.journal')
44 res_ids = context and context.get('active_ids', [])
46 browse_picking = model_pool.browse(cr, uid, res_ids, context=context)
48 for pick in browse_picking:
49 if not pick.move_lines:
51 src_usage = pick.move_lines[0].location_id.usage
52 dest_usage = pick.move_lines[0].location_dest_id.usage
54 if type == 'out' and dest_usage == 'supplier':
55 journal_type = 'purchase_refund'
56 elif type == 'out' and dest_usage == 'customer':
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'
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
72 _name = "stock.invoice.onshipping"
73 _description = "Stock Invoice Onshipping"
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'),
82 'journal_id' : _get_journal,
85 def view_init(self, cr, uid, fields_list, context=None):
88 res = super(stock_invoice_onshipping, self).view_init(cr, uid, fields_list, context=context)
89 pick_obj = self.pool.get('stock.picking')
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':
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.'))
101 def open_invoice(self, cr, uid, ids, context=None):
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)
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")
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))+"])]"
127 def create_invoice(self, cr, uid, ids, context=None):
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'],
150 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: