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 osv import fields, osv
24 from tools.translate import _
26 class stock_invoice_onshipping(osv.osv_memory):
28 def _get_journal_id(self, cr, uid, context=None):
32 model = context.get('active_model')
33 if not model or model != 'stock.picking':
36 model_pool = self.pool.get(model)
37 acct_obj = self.pool.get('account.journal')
38 res_ids = context and context.get('active_ids', [])
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:
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
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
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
61 _name = "stock.invoice.onshipping"
62 _description = "Stock Invoice Onshipping"
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'),
72 def view_init(self, cr, uid, fields_list, context=None):
75 res = super(stock_invoice_onshipping, self).view_init(cr, uid, fields_list, context=context)
76 pick_obj = self.pool.get('stock.picking')
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':
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.'))
88 def open_invoice(self, cr, uid, ids, context=None):
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)
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")
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))+"])]"
114 def create_invoice(self, cr, uid, ids, context=None):
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'],
134 stock_invoice_onshipping()
136 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: