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):
|
|
|
|
_name = "stock.invoice.onshipping"
|
|
|
|
_description = "Stock Invoice Onshipping"
|
|
|
|
_columns = {
|
2010-08-13 12:20:05 +00:00
|
|
|
'journal_id': fields.many2one('account.journal', 'Destination Journal', required=True),
|
|
|
|
'group': fields.boolean("Group by partner"),
|
|
|
|
'type': fields.selection([('out_invoice', 'Customer Invoice'),
|
|
|
|
('in_invoice', 'Supplier Invoice'),
|
|
|
|
('out_refund', 'Customer Refund'),
|
|
|
|
('in_refund', 'Supplier Refund')] , 'Type', required=True),
|
|
|
|
'invoice_date': fields.date('Invoiced date'),
|
2010-05-26 12:59:30 +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 = {}
|
|
|
|
res= super(stock_invoice_onshipping, self).view_init(cr, uid, fields_list, context=context)
|
|
|
|
pick_obj=self.pool.get('stock.picking')
|
|
|
|
count=0
|
|
|
|
for pick in pick_obj.browse(cr, uid,context.get('active_ids', False)):
|
|
|
|
if pick.invoice_state != '2binvoiced':
|
|
|
|
count=count+1
|
2010-10-15 09:44:18 +00:00
|
|
|
if len(context.get('active_ids'))==1 and count==1:
|
2010-10-15 09:13:41 +00:00
|
|
|
raise osv.except_osv(_('Warning !'),'This picking list does not require invoicing.')
|
2010-10-15 09:44:18 +00:00
|
|
|
if len(context.get('active_ids'))==count:
|
2010-10-15 09:13:41 +00:00
|
|
|
raise osv.except_osv(_('Warning !'),'None of these picking lists require invoicing.')
|
|
|
|
return res
|
|
|
|
|
2010-05-19 07:39:58 +00:00
|
|
|
def _get_type(self, cr, uid, context=None):
|
2010-05-26 12:59:30 +00:00
|
|
|
""" To get invoice type.
|
|
|
|
@param self: The object pointer.
|
|
|
|
@param cr: A database cursor
|
|
|
|
@param uid: ID of the user currently logged in
|
2010-08-13 12:20:05 +00:00
|
|
|
@param ids: The ID or list of IDs if we want more than one
|
|
|
|
@param context: A standard dictionary
|
2010-05-26 12:59:30 +00:00
|
|
|
@return: Invoice type
|
2010-05-19 07:39:58 +00:00
|
|
|
"""
|
|
|
|
if context is None:
|
2010-08-13 12:20:05 +00:00
|
|
|
context = {}
|
2010-03-10 09:42:47 +00:00
|
|
|
picking_obj = self.pool.get('stock.picking')
|
2010-09-13 10:27:24 +00:00
|
|
|
src_usage = dest_usage = None
|
2010-10-15 09:13:41 +00:00
|
|
|
for pick in picking_obj.browse(cr, uid, context['active_ids'], context=context):
|
|
|
|
if pick.invoice_state=='2binvoiced':
|
|
|
|
if pick.move_lines:
|
|
|
|
src_usage = pick.move_lines[0].location_id.usage
|
|
|
|
dest_usage = pick.move_lines[0].location_dest_id.usage
|
|
|
|
if pick.type == 'out' and dest_usage == 'supplier':
|
|
|
|
type = 'in_refund'
|
|
|
|
elif pick.type == 'out' and dest_usage == 'customer':
|
|
|
|
type = 'out_invoice'
|
|
|
|
elif pick.type == 'in' and src_usage == 'supplier':
|
|
|
|
type = 'in_invoice'
|
|
|
|
elif pick.type == 'in' and src_usage == 'customer':
|
|
|
|
type = 'out_refund'
|
|
|
|
else:
|
|
|
|
type = 'out_invoice'
|
2010-03-10 09:42:47 +00:00
|
|
|
return type
|
|
|
|
|
|
|
|
_defaults = {
|
2010-08-13 12:20:05 +00:00
|
|
|
'type': _get_type,
|
2010-05-26 12:59:30 +00:00
|
|
|
}
|
2010-03-10 09:42:47 +00:00
|
|
|
|
|
|
|
def create_invoice(self, cr, uid, ids, context):
|
2010-05-26 12:59:30 +00:00
|
|
|
""" To create invoice
|
|
|
|
@param self: The object pointer.
|
|
|
|
@param cr: A database cursor
|
|
|
|
@param uid: ID of the user currently logged in
|
2010-08-13 12:20:05 +00:00
|
|
|
@param ids: the ID or list of IDs if we want more than one
|
|
|
|
@param context: A standard dictionary
|
|
|
|
@return: Invoice ids
|
|
|
|
"""
|
2010-03-10 09:42:47 +00:00
|
|
|
result = []
|
2010-03-11 07:47:54 +00:00
|
|
|
picking_obj = self.pool.get('stock.picking')
|
|
|
|
mod_obj = self.pool.get('ir.model.data')
|
|
|
|
act_obj = self.pool.get('ir.actions.act_window')
|
2010-03-10 09:42:47 +00:00
|
|
|
for onshipdata_obj in self.read(cr, uid, ids, ['journal_id', 'group', 'type', 'invoice_date']):
|
|
|
|
if context.get('new_picking', False):
|
|
|
|
onshipdata_obj[id] = onshipdata_obj.new_picking
|
|
|
|
onshipdata_obj[ids] = onshipdata_obj.new_picking
|
|
|
|
|
|
|
|
type = onshipdata_obj['type']
|
|
|
|
context['date_inv'] = onshipdata_obj['invoice_date']
|
|
|
|
res = picking_obj.action_invoice_create(cr, uid,context['active_ids'],
|
|
|
|
journal_id = onshipdata_obj['journal_id'],
|
|
|
|
group=onshipdata_obj['group'],
|
|
|
|
type=type,
|
|
|
|
context=context)
|
|
|
|
invoice_ids = res.values()
|
|
|
|
if not invoice_ids:
|
2010-05-19 07:41:13 +00:00
|
|
|
raise osv.except_osv(_('Error'), _('Invoice is not created'))
|
2010-03-10 09:42:47 +00:00
|
|
|
|
2010-10-15 09:44:18 +00:00
|
|
|
return {
|
|
|
|
'domain': "[('id','in', ["+','.join(map(str,invoice_ids))+"])]",
|
|
|
|
'name' : _('New picking invoices'),
|
|
|
|
'view_type': 'form',
|
|
|
|
'view_mode': 'tree,form',
|
|
|
|
'res_model': 'account.invoice',
|
2010-10-15 10:08:26 +00:00
|
|
|
'views': False,
|
2010-10-15 09:44:18 +00:00
|
|
|
'context': context,
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
}
|
2010-03-10 09:42:47 +00:00
|
|
|
|
|
|
|
stock_invoice_onshipping()
|
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|