2010-03-10 08:56:56 +00:00
##############################################################################
#
# 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
from tools . translate import _
2012-05-23 17:15:27 +00:00
import decimal_precision as dp
2010-03-10 08:56:56 +00:00
class sale_advance_payment_inv ( osv . osv_memory ) :
_name = " sale.advance.payment.inv "
2010-12-13 06:07:23 +00:00
_description = " Sales Advance Payment Invoice "
2012-05-10 11:02:05 +00:00
def _default_product_id ( self , cr , uid , context = None ) :
try :
2012-05-23 17:15:27 +00:00
product_id = self . pool . get ( ' ir.model.data ' ) . get_object_reference ( cr , uid , ' sale ' , ' advance_product_0 ' )
2012-05-14 07:29:58 +00:00
except ValueError :
2012-05-23 17:15:27 +00:00
#a ValueError is returned if the xml id given is not found in the table ir_model_data
2012-05-14 07:29:58 +00:00
return False
2012-05-23 17:15:27 +00:00
return product_id [ 1 ]
2012-05-10 11:02:05 +00:00
2010-03-10 08:56:56 +00:00
_columns = {
2012-05-22 09:49:28 +00:00
' product_id ' : fields . many2one ( ' product.product ' , ' Advance Product ' , help = " Select a product of type service which is called ' Advance Product ' . You may have to create it and set it as a default value on this field. " ) ,
2012-05-23 17:15:27 +00:00
' amount ' : fields . float ( ' Advance Amount ' , digits_compute = dp . get_precision ( ' Sale Price ' ) , required = True , help = " The amount to be invoiced in advance. " ) ,
2011-07-19 14:08:32 +00:00
' qtty ' : fields . float ( ' Quantity ' , digits = ( 16 , 2 ) , required = True ) ,
2012-07-13 10:17:51 +00:00
' advance_payment_method ' : fields . selection ( [ ( ' percentage ' , ' Percentage ' ) , ( ' fixed ' , ' Fixed Price ' ) ] , ' Type ' , required = True , help = " Use Fixed Price if you want to give specific amount in Advance. Use Percentage if you want to give percentage of Total Invoice Amount. " ) ,
2010-03-10 08:56:56 +00:00
}
2012-05-10 11:02:05 +00:00
2010-08-18 06:22:59 +00:00
_defaults = {
2012-05-10 11:02:05 +00:00
' qtty ' : 1.0 ,
2012-05-14 11:26:14 +00:00
' advance_payment_method ' : ' fixed ' ,
2012-05-10 11:02:05 +00:00
' product_id ' : _default_product_id ,
2010-08-13 12:20:05 +00:00
}
2010-08-18 06:22:59 +00:00
2012-05-22 09:49:28 +00:00
def onchange_advance_payment_method ( self , cr , uid , ids , advance_payment_method , product_id , context = None ) :
2012-05-14 11:26:14 +00:00
if advance_payment_method == ' percentage ' :
2012-05-23 17:15:27 +00:00
return { ' value ' : { ' amount ' : 0 , ' product_id ' : False } }
if not product_id :
return { ' value ' : { ' amount ' : 0 } }
product = self . pool . get ( ' product.product ' ) . browse ( cr , uid , product_id , context = context )
return { ' value ' : { ' amount ' : product . list_price } }
2012-05-10 11:02:05 +00:00
2010-11-22 10:37:53 +00:00
def create_invoices ( self , cr , uid , ids , context = None ) :
2010-06-10 13:04:07 +00:00
"""
2010-03-17 07:34:13 +00:00
To create invoices .
2010-06-10 13:04:07 +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-06-10 13:04:07 +00:00
@param ids : the ID or list of IDs if we want more than one
@param context : A standard dictionary
@return :
"""
2010-03-10 08:56:56 +00:00
list_inv = [ ]
obj_sale = self . pool . get ( ' sale.order ' )
obj_lines = self . pool . get ( ' account.invoice.line ' )
2010-03-11 07:47:54 +00:00
inv_obj = self . pool . get ( ' account.invoice ' )
2010-11-23 07:05:05 +00:00
if context is None :
2010-11-22 10:37:53 +00:00
context = { }
2010-06-10 13:04:07 +00:00
2010-11-22 10:37:53 +00:00
for sale_adv_obj in self . browse ( cr , uid , ids , context = context ) :
for sale in obj_sale . browse ( cr , uid , context . get ( ' active_ids ' , [ ] ) , context = context ) :
2010-03-10 08:56:56 +00:00
create_ids = [ ]
ids_inv = [ ]
if sale . order_policy == ' postpaid ' :
raise osv . except_osv (
_ ( ' Error ' ) ,
2010-12-13 06:07:23 +00:00
_ ( " You cannot make an advance on a sales order \
2010-03-10 08:56:56 +00:00
that is defined as ' Automatic Invoice after delivery ' . " ))
val = obj_lines . product_id_change ( cr , uid , [ ] , sale_adv_obj . product_id . id ,
2010-07-10 06:30:54 +00:00
uom = False , partner_id = sale . partner_id . id , fposition_id = sale . fiscal_position . id )
2011-09-23 12:26:42 +00:00
res = val [ ' value ' ]
2012-05-10 11:02:05 +00:00
if not sale_adv_obj . product_id . id :
prop = self . pool . get ( ' ir.property ' ) . get ( cr , uid ,
' property_account_income_categ ' , ' product.category ' ,
context = context )
account_id = prop and prop . id or False
account_id = self . pool . get ( ' account.fiscal.position ' ) . map_account ( cr , uid , sale . fiscal_position . id or False , account_id )
2012-05-14 11:50:13 +00:00
if not account_id :
2012-05-10 11:02:05 +00:00
raise osv . except_osv ( _ ( ' Configuration Error ! ' ) ,
2012-05-14 11:29:14 +00:00
_ ( ' There is no income account defined as global property. ' ) )
2012-05-23 17:15:27 +00:00
res [ ' account_id ' ] = account_id
2012-05-10 11:02:05 +00:00
2011-09-23 12:26:42 +00:00
if not res . get ( ' account_id ' ) :
raise osv . except_osv ( _ ( ' Configuration Error ! ' ) ,
2011-09-23 11:59:02 +00:00
_ ( ' There is no income account defined ' \
' for this product: " %s " (id: %d ) ' ) % \
( sale_adv_obj . product_id . name , sale_adv_obj . product_id . id , ) )
2012-05-10 11:02:05 +00:00
final_amount = 0
if sale_adv_obj . amount < = 0.00 :
raise osv . except_osv ( _ ( ' Data Insufficient ! ' ) ,
_ ( ' Please check the Advance Amount, it should not be 0 or less! ' ) )
2012-05-23 17:15:27 +00:00
if sale_adv_obj . advance_payment_method == ' percentage ' :
final_amount = sale . amount_total * sale_adv_obj . amount / 100
if not res . get ( ' name ' ) :
res [ ' name ' ] = _ ( " Advance of %s %% " ) % ( sale_adv_obj . amount )
2012-05-10 11:02:05 +00:00
else :
2012-05-23 17:15:27 +00:00
final_amount = sale_adv_obj . amount
if not res . get ( ' name ' ) :
#TODO: should find a way to call formatLang() from rml_parse
if sale . pricelist_id . currency_id . position == ' after ' :
res [ ' name ' ] = _ ( " Advance of %s %s " ) % ( final_amount , sale . pricelist_id . currency_id . symbol )
else :
res [ ' name ' ] = _ ( " Advance of %s %s " ) % ( sale . pricelist_id . currency_id . symbol , final_amount )
2012-05-10 11:02:05 +00:00
if res . get ( ' invoice_line_tax_id ' ) :
res [ ' invoice_line_tax_id ' ] = [ ( 6 , 0 , res . get ( ' invoice_line_tax_id ' ) ) ]
else :
res [ ' invoice_line_tax_id ' ] = False
2010-08-18 06:22:59 +00:00
line_id = obj_lines . create ( cr , uid , {
2011-09-23 13:05:20 +00:00
' name ' : res . get ( ' name ' ) ,
2011-09-23 12:26:42 +00:00
' account_id ' : res [ ' account_id ' ] ,
2012-05-10 11:02:05 +00:00
' price_unit ' : final_amount ,
' quantity ' : sale_adv_obj . qtty or 1.0 ,
2010-03-10 08:56:56 +00:00
' discount ' : False ,
2012-05-23 17:15:27 +00:00
' uos_id ' : res . get ( ' uos_id ' , False ) ,
2010-03-10 08:56:56 +00:00
' product_id ' : sale_adv_obj . product_id . id ,
2012-05-10 11:02:05 +00:00
' invoice_line_tax_id ' : res . get ( ' invoice_line_tax_id ' ) ,
2010-03-10 08:56:56 +00:00
' account_analytic_id ' : sale . project_id . id or False ,
#'note':'',
} )
create_ids . append ( line_id )
inv = {
2010-07-21 06:44:41 +00:00
' name ' : sale . client_order_ref or sale . name ,
2010-03-10 08:56:56 +00:00
' origin ' : sale . name ,
' type ' : ' out_invoice ' ,
' reference ' : False ,
' account_id ' : sale . partner_id . property_account_receivable . id ,
' partner_id ' : sale . partner_id . id ,
' invoice_line ' : [ ( 6 , 0 , create_ids ) ] ,
2010-10-21 09:30:04 +00:00
' currency_id ' : sale . pricelist_id . currency_id . id ,
2010-03-10 08:56:56 +00:00
' comment ' : ' ' ,
2010-10-21 09:30:04 +00:00
' payment_term ' : sale . payment_term . id ,
2010-05-19 06:31:40 +00:00
' fiscal_position ' : sale . fiscal_position . id or sale . partner_id . property_account_position . id
2010-08-13 12:20:05 +00:00
}
2010-06-10 13:04:07 +00:00
2010-03-10 08:56:56 +00:00
inv_id = inv_obj . create ( cr , uid , inv )
2010-10-17 16:42:26 +00:00
inv_obj . button_reset_taxes ( cr , uid , [ inv_id ] , context = context )
2010-03-10 08:56:56 +00:00
for inv in sale . invoice_ids :
ids_inv . append ( inv . id )
ids_inv . append ( inv_id )
2012-04-03 20:57:06 +00:00
obj_sale . write ( cr , uid , [ sale . id ] , { ' invoice_ids ' : [ ( 6 , 0 , ids_inv ) ] } )
2010-03-10 08:56:56 +00:00
list_inv . append ( inv_id )
#
# If invoice on picking: add the cost on the SO
# If not, the advance will be deduced when generating the final invoice
#
2010-10-15 08:48:13 +00:00
if sale . order_policy == ' picking ' :
2012-05-10 11:02:05 +00:00
vals = {
2010-03-10 08:56:56 +00:00
' order_id ' : sale . id ,
2012-05-23 17:15:27 +00:00
' name ' : res . get ( ' name ' ) ,
2012-05-10 11:02:05 +00:00
' price_unit ' : - final_amount ,
' product_uom_qty ' : sale_adv_obj . qtty or 1.0 ,
' product_uos_qty ' : sale_adv_obj . qtty or 1.0 ,
2012-05-23 17:15:27 +00:00
' product_uos ' : res . get ( ' uos_id ' , False ) ,
' product_uom ' : res . get ( ' uom_id ' , False ) ,
2012-05-10 11:02:05 +00:00
' product_id ' : sale_adv_obj . product_id . id or False ,
2010-03-10 08:56:56 +00:00
' discount ' : False ,
2012-05-10 11:02:05 +00:00
' tax_id ' : res . get ( ' invoice_line_tax_id ' ) ,
}
2012-05-23 17:15:27 +00:00
self . pool . get ( ' sale.order.line ' ) . create ( cr , uid , vals , context = context )
2010-03-10 08:56:56 +00:00
2010-06-10 13:04:07 +00:00
context . update ( { ' invoice_id ' : list_inv } )
2012-05-10 11:02:05 +00:00
if context . get ( ' open_invoices ' ) :
return self . open_invoices ( cr , uid , ids , context = context )
2012-05-23 17:15:27 +00:00
return { ' type ' : ' ir.actions.act_window_close ' }
2010-03-10 08:56:56 +00:00
2012-05-10 11:02:05 +00:00
def open_invoices ( self , cr , uid , ids , context = None ) :
2010-10-29 04:17:01 +00:00
if context is None :
context = { }
2010-03-10 08:56:56 +00:00
mod_obj = self . pool . get ( ' ir.model.data ' )
2010-11-22 10:37:53 +00:00
for advance_pay in self . browse ( cr , uid , ids , context = context ) :
2010-10-28 13:16:19 +00:00
form_res = mod_obj . get_object_reference ( cr , uid , ' account ' , ' invoice_form ' )
form_id = form_res and form_res [ 1 ] or False
tree_res = mod_obj . get_object_reference ( cr , uid , ' account ' , ' invoice_tree ' )
tree_id = tree_res and tree_res [ 1 ] or False
2010-03-10 08:56:56 +00:00
return {
2010-10-17 16:42:26 +00:00
' name ' : _ ( ' Advance Invoice ' ) ,
2010-03-10 08:56:56 +00:00
' view_type ' : ' form ' ,
2010-06-10 13:04:07 +00:00
' view_mode ' : ' form,tree ' ,
2010-03-10 08:56:56 +00:00
' res_model ' : ' account.invoice ' ,
2010-06-10 13:04:07 +00:00
' res_id ' : int ( context [ ' invoice_id ' ] [ 0 ] ) ,
' view_id ' : False ,
2010-10-28 13:16:19 +00:00
' views ' : [ ( form_id , ' form ' ) , ( tree_id , ' tree ' ) ] ,
2010-10-21 09:30:04 +00:00
' context ' : " { ' type ' : ' out_invoice ' } " ,
2010-03-10 08:56:56 +00:00
' type ' : ' ir.actions.act_window ' ,
}
2010-06-10 13:04:07 +00:00
2012-05-10 11:02:05 +00:00
sale_advance_payment_inv ( )
2010-03-10 08:56:56 +00:00
2011-07-19 14:08:32 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: