2010-03-10 08:56:56 +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/>.
#
##############################################################################
2012-12-18 13:41:18 +00:00
from openerp . osv import osv , fields
2012-12-06 14:56:32 +00:00
from openerp . tools . translate import _
2013-11-21 12:06:11 +00:00
from openerp import workflow
2010-03-10 08:56:56 +00:00
class sale_order_line_make_invoice ( osv . osv_memory ) :
_name = " sale.order.line.make.invoice "
_description = " Sale OrderLine Make_invoice "
2013-03-26 17:02:02 +00:00
2010-11-22 10:37:53 +00:00
def make_invoices ( self , cr , uid , ids , context = None ) :
2010-10-25 12:54:08 +00:00
"""
2010-03-17 07:34:13 +00:00
To make invoices .
2010-10-25 12:54:08 +00:00
2010-03-12 13:42:55 +00:00
@param self : The object pointer .
@param cr : A database cursor
@param uid : ID of the user currently logged in
2010-10-25 12:54:08 +00:00
@param ids : the ID or list of IDs
@param context : A standard dictionary
@return : A dictionary which of fields with values .
"""
2011-11-30 06:45:04 +00:00
if context is None : context = { }
2010-03-10 08:56:56 +00:00
res = False
invoices = { }
#TODO: merge with sale.py/make_invoice
def make_invoice ( order , lines ) :
2010-10-25 12:54:08 +00:00
"""
2010-03-17 07:34:13 +00:00
To make invoices .
2010-10-25 12:54:08 +00:00
@param order :
@param lines :
@return :
"""
2010-03-10 08:56:56 +00:00
a = order . partner_id . property_account_receivable . id
if order . partner_id and order . partner_id . property_payment_term . id :
pay_term = order . partner_id . property_payment_term . id
else :
pay_term = False
inv = {
' name ' : order . name ,
' origin ' : order . name ,
' type ' : ' out_invoice ' ,
' reference ' : " P %d SO %d " % ( order . partner_id . id , order . id ) ,
' account_id ' : a ,
2012-11-26 15:51:39 +00:00
' partner_id ' : order . partner_invoice_id . id ,
2010-10-21 09:30:04 +00:00
' invoice_line ' : [ ( 6 , 0 , lines ) ] ,
2010-03-10 08:56:56 +00:00
' currency_id ' : order . pricelist_id . currency_id . id ,
' comment ' : order . note ,
' payment_term ' : pay_term ,
2010-05-19 06:31:40 +00:00
' fiscal_position ' : order . fiscal_position . id or order . partner_id . property_account_position . id ,
2011-08-08 09:29:20 +00:00
' user_id ' : order . user_id and order . user_id . id or False ,
2012-12-10 10:51:48 +00:00
' company_id ' : order . company_id and order . company_id . id or False ,
2012-12-17 16:07:01 +00:00
' date_invoice ' : fields . date . today ( ) ,
2010-03-10 08:56:56 +00:00
}
inv_id = self . pool . get ( ' account.invoice ' ) . create ( cr , uid , inv )
return inv_id
2010-03-11 07:47:54 +00:00
sales_order_line_obj = self . pool . get ( ' sale.order.line ' )
2010-03-12 07:19:16 +00:00
sales_order_obj = self . pool . get ( ' sale.order ' )
2010-11-22 10:37:53 +00:00
for line in sales_order_line_obj . browse ( cr , uid , context . get ( ' active_ids ' , [ ] ) , context = context ) :
2010-10-21 09:30:04 +00:00
if ( not line . invoiced ) and ( line . state not in ( ' draft ' , ' cancel ' ) ) :
2013-03-26 17:02:02 +00:00
if not line . order_id in invoices :
invoices [ line . order_id ] = [ ]
line_id = sales_order_line_obj . invoice_line_create ( cr , uid , [ line . id ] )
2010-03-10 08:56:56 +00:00
for lid in line_id :
2013-03-26 17:02:02 +00:00
invoices [ line . order_id ] . append ( lid )
for order , il in invoices . items ( ) :
2010-10-25 12:54:08 +00:00
res = make_invoice ( order , il )
cr . execute ( ' INSERT INTO sale_order_invoice_rel \
( order_id , invoice_id ) values ( % s , % s ) ' , (order.id, res))
2010-03-10 08:56:56 +00:00
flag = True
2013-02-20 05:42:49 +00:00
sales_order_obj . message_post ( cr , uid , [ order . id ] , body = _ ( " Invoice created " ) , context = context )
2013-03-25 08:45:02 +00:00
data_sale = sales_order_obj . browse ( cr , uid , order . id , context = context )
2010-03-10 08:56:56 +00:00
for line in data_sale . order_line :
if not line . invoiced :
flag = False
break
if flag :
2013-11-21 12:06:11 +00:00
workflow . trg_validate ( uid , ' sale.order ' , order . id , ' manual_invoice ' , cr )
2013-03-26 17:02:02 +00:00
sales_order_obj . write ( cr , uid , [ order . id ] , { ' state ' : ' progress ' } )
2010-03-10 08:56:56 +00:00
2010-05-19 10:28:00 +00:00
if not invoices :
2012-08-07 11:34:14 +00:00
raise osv . except_osv ( _ ( ' Warning! ' ) , _ ( ' Invoice cannot be created for this Sales Order Line due to one of the following reasons: \n 1.The state of this sales order line is either " draft " or " cancel " ! \n 2.The Sales Order Line is Invoiced! ' ) )
2012-11-23 10:46:18 +00:00
if context . get ( ' open_invoices ' , False ) :
2013-03-26 17:02:02 +00:00
return self . open_invoices ( cr , uid , ids , res , context = context )
2010-12-28 10:41:38 +00:00
return { ' type ' : ' ir.actions.act_window_close ' }
2010-03-10 08:56:56 +00:00
2012-11-23 10:46:18 +00:00
def open_invoices ( self , cr , uid , ids , invoice_ids , context = None ) :
""" open a view on one of the given invoice_ids """
ir_model_data = self . pool . get ( ' ir.model.data ' )
form_res = ir_model_data . get_object_reference ( cr , uid , ' account ' , ' invoice_form ' )
form_id = form_res and form_res [ 1 ] or False
tree_res = ir_model_data . get_object_reference ( cr , uid , ' account ' , ' invoice_tree ' )
tree_id = tree_res and tree_res [ 1 ] or False
2013-03-26 17:02:02 +00:00
2012-11-23 10:46:18 +00:00
return {
2012-11-27 21:59:08 +00:00
' name ' : _ ( ' Invoice ' ) ,
2012-11-23 10:46:18 +00:00
' view_type ' : ' form ' ,
' view_mode ' : ' form,tree ' ,
' res_model ' : ' account.invoice ' ,
' res_id ' : invoice_ids ,
' view_id ' : False ,
' views ' : [ ( form_id , ' form ' ) , ( tree_id , ' tree ' ) ] ,
' context ' : { ' type ' : ' out_invoice ' } ,
' type ' : ' ir.actions.act_window ' ,
}
2010-03-10 08:56:56 +00:00
2010-12-28 10:41:38 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: