2010-03-10 09:42:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from osv import fields, osv
|
2010-08-04 11:07:44 +00:00
|
|
|
|
2010-03-10 09:42:47 +00:00
|
|
|
from tools.translate import _
|
|
|
|
|
|
|
|
class stock_invoice_onshipping(osv.osv_memory):
|
2010-11-03 10:20:01 +00:00
|
|
|
|
2010-11-04 08:35:07 +00:00
|
|
|
def _get_journal_id(self, cr, uid, context=None):
|
2010-11-03 10:20:01 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2010-11-04 08:35:07 +00:00
|
|
|
|
|
|
|
model = context.get('active_model')
|
|
|
|
if not model or model != 'stock.picking':
|
|
|
|
return []
|
|
|
|
|
|
|
|
model_pool = self.pool.get(model)
|
|
|
|
acct_obj = self.pool.get('account.journal')
|
|
|
|
res_ids = context and context.get('active_ids', [])
|
|
|
|
vals=[]
|
|
|
|
pick_types = list(set(map(lambda x: x.type, model_pool.browse(cr, uid, res_ids, context=context))))
|
|
|
|
for type in pick_types:
|
|
|
|
if type == 'out':
|
2010-12-14 16:34:26 +00:00
|
|
|
value = acct_obj.search(cr, uid, [('type', 'in',('sale','purchase_refund') )])
|
2010-11-03 10:20:01 +00:00
|
|
|
for jr_type in acct_obj.browse(cr, uid, value, context=context):
|
|
|
|
t1 = jr_type.id,jr_type.name
|
|
|
|
vals.append(t1)
|
|
|
|
|
2010-11-04 08:35:07 +00:00
|
|
|
elif type == 'in':
|
2010-12-14 16:34:26 +00:00
|
|
|
value = acct_obj.search(cr, uid, [('type', 'in',('purchase','sale_refund') )])
|
2010-11-03 10:20:01 +00:00
|
|
|
for jr_type in acct_obj.browse(cr, uid, value, context=context):
|
|
|
|
t1 = jr_type.id,jr_type.name
|
|
|
|
vals.append(t1)
|
|
|
|
else:
|
|
|
|
value = acct_obj.search(cr, uid, [('type', 'in',('cash','bank','general','situation') )])
|
|
|
|
for jr_type in acct_obj.browse(cr, uid, value, context=context):
|
|
|
|
t1 = jr_type.id,jr_type.name
|
|
|
|
vals.append(t1)
|
|
|
|
return vals
|
|
|
|
|
|
|
|
|
2010-03-10 09:42:47 +00:00
|
|
|
_name = "stock.invoice.onshipping"
|
|
|
|
_description = "Stock Invoice Onshipping"
|
2010-11-03 10:20:01 +00:00
|
|
|
|
|
|
|
|
2010-03-10 09:42:47 +00:00
|
|
|
_columns = {
|
2010-11-03 10:20:01 +00:00
|
|
|
'journal_id': fields.selection(_get_journal_id, 'Destination Journal',required=True),
|
2010-08-13 12:20:05 +00:00
|
|
|
'group': fields.boolean("Group by partner"),
|
|
|
|
'invoice_date': fields.date('Invoiced date'),
|
2010-05-26 12:59:30 +00:00
|
|
|
}
|
2010-10-15 11:58:17 +00:00
|
|
|
|
2010-11-03 10:20:01 +00:00
|
|
|
|
2010-10-15 09:13:41 +00:00
|
|
|
def view_init(self, cr, uid, fields_list, context=None):
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
2010-10-15 11:58:17 +00:00
|
|
|
res = super(stock_invoice_onshipping, self).view_init(cr, uid, fields_list, context=context)
|
|
|
|
pick_obj = self.pool.get('stock.picking')
|
|
|
|
count = 0
|
|
|
|
active_ids = context.get('active_ids',[])
|
2010-11-22 10:37:53 +00:00
|
|
|
for pick in pick_obj.browse(cr, uid, active_ids, context=context):
|
2010-10-15 09:13:41 +00:00
|
|
|
if pick.invoice_state != '2binvoiced':
|
2010-10-15 11:58:17 +00:00
|
|
|
count += 1
|
|
|
|
if len(active_ids) == 1 and count:
|
|
|
|
raise osv.except_osv(_('Warning !'), _('This picking list does not require invoicing.'))
|
|
|
|
if len(active_ids) == count:
|
|
|
|
raise osv.except_osv(_('Warning !'), _('None of these picking lists require invoicing.'))
|
2010-10-15 09:13:41 +00:00
|
|
|
return res
|
2010-03-10 09:42:47 +00:00
|
|
|
|
2010-11-22 13:16:15 +00:00
|
|
|
|
2010-03-10 09:42:47 +00:00
|
|
|
|
2010-10-15 11:58:17 +00:00
|
|
|
def create_invoice(self, cr, uid, ids, context=None):
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
2010-11-12 12:54:47 +00:00
|
|
|
picking_pool = self.pool.get('stock.picking')
|
|
|
|
data_pool = self.pool.get('ir.model.data')
|
2010-10-15 11:58:17 +00:00
|
|
|
onshipdata_obj = self.read(cr, uid, ids[0], ['journal_id', 'group', 'invoice_date'])
|
|
|
|
if context.get('new_picking', False):
|
|
|
|
onshipdata_obj['id'] = onshipdata_obj.new_picking
|
|
|
|
onshipdata_obj[ids] = onshipdata_obj.new_picking
|
|
|
|
|
|
|
|
context['date_inv'] = onshipdata_obj['invoice_date']
|
|
|
|
invoice_ids = []
|
2010-11-12 12:54:47 +00:00
|
|
|
active_ids = context.get('active_ids', [])
|
|
|
|
active_picking = picking_pool.browse(cr, uid, context.get('active_id',False), context=context)
|
|
|
|
inv_type = picking_pool._get_invoice_type(active_picking)
|
2010-03-10 09:42:47 +00:00
|
|
|
|
2010-11-12 12:54:47 +00:00
|
|
|
res = picking_pool.action_invoice_create(cr, uid, active_ids,
|
|
|
|
journal_id = onshipdata_obj['journal_id'],
|
|
|
|
group = onshipdata_obj['group'],
|
|
|
|
type = None,
|
|
|
|
context=context)
|
|
|
|
invoice_ids += res.values()
|
2010-10-15 11:58:17 +00:00
|
|
|
if not invoice_ids:
|
2010-11-12 12:54:47 +00:00
|
|
|
raise osv.except_osv(_('Error'), _('No Invoices were created'))
|
|
|
|
action_model = False
|
|
|
|
action = {}
|
|
|
|
if inv_type == "out_invoice":
|
|
|
|
action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree1")
|
|
|
|
elif inv_type == "in_invoice":
|
|
|
|
action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree2")
|
|
|
|
elif inv_type == "out_refund":
|
|
|
|
action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree3")
|
|
|
|
elif inv_type == "in_refund":
|
|
|
|
action_model,action_id = data_pool.get_object_reference(cr, uid, 'account', "action_invoice_tree4")
|
|
|
|
if action_model:
|
|
|
|
action_pool = self.pool.get(action_model)
|
|
|
|
action = action_pool.read(cr, uid, action_id, context=context)
|
|
|
|
action['domain'] = "[('id','in', ["+','.join(map(str,invoice_ids))+"])]"
|
|
|
|
return action
|
2010-03-10 09:42:47 +00:00
|
|
|
|
|
|
|
stock_invoice_onshipping()
|
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|