[IMP] sale: merge the actions 'Create Final Invoice' and 'Advance Invoice' into a single wizard

bzr revid: rco@openerp.com-20120723152841-643jvecafb5eqqnm
This commit is contained in:
Raphael Collet 2012-07-23 17:28:41 +02:00
parent 395fa84aa1
commit 396f5fdc9e
3 changed files with 175 additions and 149 deletions

View File

@ -147,7 +147,6 @@
<button name="ship_recreate" states="shipping_except" string="Recreate Delivery Order"/>
<button name="ship_corrected" states="shipping_except" string="Ignore Exception"/>
<button name="action_quotation_send" string="Send by Mail" type="object" states="draft,sent" class="oe_highlight"/>
<button name="manual_invoice" states="manual" string="Create Final Invoice" type="object" class="oe_highlight"/>
<button name="print_quotation" string="Send by Post" type="object" states="draft,sent" class="oe_highlight"/>
<button name="action_button_confirm" states="draft" string="Confirm" type="object"/>
<button name="action_button_confirm" states="sent" string="Confirm" class="oe_highlight" type="object"/>
@ -155,7 +154,8 @@
attrs="{'invisible': ['|','|',('state', '!=','progress'), ('invoiced', '=', True),('order_policy','=','picking')]}"/>
<button name="action_view_delivery" string="Open Delivery Order" type="object" class="oe_highlight"
attrs="{'invisible': ['|','|','|',('picking_ids','=',False),('picking_ids','=',[]), ('state', 'not in', ('progress','manual')),('shipped','=',True)]}"/>
<button name="%(action_view_sale_advance_payment_inv)d" string="Advance Invoice" type="action" states="manual"/>
<button name="%(action_view_sale_advance_payment_inv)d" string="Create Invoice"
type="action" states="manual" class="oe_highlight"/>
<button name="cancel" states="draft,sent" string="Cancel"/>
<button name="action_cancel" states="manual,progress" string="Cancel" type="object"/>
<button name="ship_cancel" states="shipping_except" string="Cancel"/>

View File

@ -26,164 +26,175 @@ class sale_advance_payment_inv(osv.osv_memory):
_name = "sale.advance.payment.inv"
_description = "Sales Advance Payment Invoice"
def _default_product_id(self, cr, uid, context=None):
try:
product_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'sale', 'advance_product_0')
except ValueError:
#a ValueError is returned if the xml id given is not found in the table ir_model_data
return False
return product_id[1]
_columns = {
'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."),
'amount': fields.float('Advance Amount', digits_compute= dp.get_precision('Sale Price'), required=True, help="The amount to be invoiced in advance."),
'qtty': fields.float('Quantity', digits=(16, 2), required=True),
'advance_payment_method':fields.selection([('percentage','Percentage'), ('fixed','Fixed Price')], 'Type', required=True, help="Use Fixed Price if you want to give specific amound in Advance, Use Percentage if you want to give percentage of Total Invoice Amount."),
'advance_payment_method':fields.selection(
[('all', 'All'), ('percentage','Percentage'), ('fixed','Fixed Price'),
('lines', 'Some Order Lines')],
'Type', required=True,
help="""Use All to create the final invoice.
Use Percentage to invoice a percentage of the total amount.
Use Fixed Price to invoice a specific amound in advance.
Use Some Order Lines to invoice a selection of the sale order lines."""),
'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."""),
'amount': fields.float('Advance Amount', digits_compute= dp.get_precision('Sale Price'),
help="The amount to be invoiced in advance."),
}
def _get_advance_product(self, cr, uid, context=None):
try:
product = self.pool.get('ir.model.data').get_object(cr, uid, 'sale', 'advance_product_0')
except ValueError:
# a ValueError is returned if the xml id given is not found in the table ir_model_data
return False
return product.id
_defaults = {
'qtty': 1.0,
'advance_payment_method': 'fixed',
'product_id': _default_product_id,
'advance_payment_method': 'all',
'product_id': _get_advance_product,
}
def onchange_advance_payment_method(self, cr, uid, ids, advance_payment_method, product_id, context=None):
def onchange_method(self, cr, uid, ids, advance_payment_method, product_id, context=None):
if advance_payment_method == 'percentage':
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}}
if product_id:
product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
return {'value': {'amount': product.list_price}}
return {'value': {'amount': 0}}
def create_invoices(self, cr, uid, ids, context=None):
"""
To create invoices.
@param self: The object pointer.
@param cr: A database cursor
@param uid: ID of the user currently logged in
@param ids: the ID or list of IDs if we want more than one
@param context: A standard dictionary
@return:
"""
list_inv = []
obj_sale = self.pool.get('sale.order')
obj_lines = self.pool.get('account.invoice.line')
inv_obj = self.pool.get('account.invoice')
""" create invoices for the active sale orders """
if context is None:
context = {}
wizard = self.browse(cr, uid, ids[0], context)
sale_ids = context.get('active_ids', [])
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):
create_ids = []
ids_inv = []
if sale.order_policy == 'postpaid':
raise osv.except_osv(
_('Error'),
_("You cannot make an advance on a sales order \
that is defined as 'Automatic Invoice after delivery'."))
val = obj_lines.product_id_change(cr, uid, [], sale_adv_obj.product_id.id,
uom = False, partner_id = sale.partner_id.id, fposition_id = sale.fiscal_position.id)
res = val['value']
if wizard.advance_payment_method == 'all':
# create the final invoices of the active sale orders
res = self.pool.get('sale.order').manual_invoice(cr, uid, sale_ids, context)
if context.get('open_invoices', False):
return res
return {'type': 'ir.actions.act_window_close'}
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)
if not account_id:
raise osv.except_osv(_('Configuration Error !'),
_('There is no income account defined as global property.'))
res['account_id'] = account_id
if wizard.advance_payment_method == 'lines':
# open the list view of sale order lines to invoice
act_window = self.pool.get('ir.actions.act_window')
res = act_window.for_xml_id(cr, uid, 'sale', 'action_order_line_tree2', context)
res['context'] = {
'search_default_uninvoiced': 1,
'search_default_order_id': sale_ids and sale_ids[0] or False,
}
return res
if not res.get('account_id'):
assert wizard.advance_payment_method in ('fixed', 'percentage')
sale_obj = self.pool.get('sale.order')
inv_obj = self.pool.get('account.invoice')
inv_line_obj = self.pool.get('account.invoice.line')
inv_ids = []
for sale in sale_obj.browse(cr, uid, sale_ids, context=context):
if sale.order_policy == 'postpaid':
raise osv.except_osv(
_('Error'),
_("You cannot make an advance on a sales order \
that is defined as 'Automatic Invoice after delivery'."))
val = inv_line_obj.product_id_change(cr, uid, [], wizard.product_id.id,
uom=False, partner_id=sale.partner_id.id, fposition_id=sale.fiscal_position.id)
res = val['value']
# determine and check income account
if not wizard.product_id.id :
prop = self.pool.get('ir.property').get(cr, uid,
'property_account_income_categ', 'product.category', context=context)
prop_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, prop_id)
if not account_id:
raise osv.except_osv(_('Configuration Error !'),
_('There is no income account defined ' \
'for this product: "%s" (id:%d)') % \
(sale_adv_obj.product_id.name, sale_adv_obj.product_id.id,))
_('There is no income account defined as global property.'))
res['account_id'] = account_id
if not res.get('account_id'):
raise osv.except_osv(_('Configuration Error !'),
_('There is no income account defined for this product: "%s" (id:%d)') % \
(wizard.product_id.name, wizard.product_id.id,))
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!'))
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)
else:
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)
# determine invoice amount
if wizard.amount <= 0.00:
raise osv.except_osv(_('Incorrect Data'),
_('The value of Advance Amount must be positive.'))
if wizard.advance_payment_method == 'percentage':
inv_amount = sale.amount_total * wizard.amount / 100
if not res.get('name'):
res['name'] = _("Advance of %s %%") % (wizard.amount)
else:
inv_amount = wizard.amount
if not res.get('name'):
#TODO: should find a way to call formatLang() from rml_parse
symbol = sale.pricelist_id.currency_id.symbol
if sale.pricelist_id.currency_id.position == 'after':
res['name'] = _("Advance of %s %s") % (inv_amount, symbol)
else:
res['name'] = _("Advance of %s %s") % (symbol, inv_amount)
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
# determine taxes
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
line_id = obj_lines.create(cr, uid, {
# create the invoice
inv_line_values = {
'name': res.get('name'),
'account_id': res['account_id'],
'price_unit': inv_amount,
'quantity': 1.0,
'discount': False,
'uos_id': res.get('uos_id', False),
'product_id': wizard.product_id.id,
'invoice_line_tax_id': res.get('invoice_line_tax_id'),
'account_analytic_id': sale.project_id.id or False,
}
inv_values = {
'name': sale.client_order_ref or sale.name,
'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': [(0, 0, inv_line_values)],
'currency_id': sale.pricelist_id.currency_id.id,
'comment': '',
'payment_term': sale.payment_term.id,
'fiscal_position': sale.fiscal_position.id or sale.partner_id.property_account_position.id
}
inv_id = inv_obj.create(cr, uid, inv_values, context=context)
inv_obj.button_reset_taxes(cr, uid, [inv_id], context=context)
inv_ids.append(inv_id)
# add the invoice to the sale order's invoices
sale.write({'invoice_ids': [(4, inv_id)]})
# If invoice on picking: add the cost on the SO
# If not, the advance will be deduced when generating the final invoice
if sale.order_policy == 'picking':
vals = {
'order_id': sale.id,
'name': res.get('name'),
'account_id': res['account_id'],
'price_unit': final_amount,
'quantity': sale_adv_obj.qtty or 1.0,
'price_unit': -inv_amount,
'product_uom_qty': 1.0,
'product_uos_qty': 1.0,
'product_uos': res.get('uos_id', False),
'product_uom': res.get('uom_id', False),
'product_id': wizard.product_id.id or False,
'discount': False,
'uos_id': res.get('uos_id', False),
'product_id': sale_adv_obj.product_id.id,
'invoice_line_tax_id': res.get('invoice_line_tax_id'),
'account_analytic_id': sale.project_id.id or False,
#'note':'',
})
create_ids.append(line_id)
inv = {
'name': sale.client_order_ref or sale.name,
'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)],
'currency_id': sale.pricelist_id.currency_id.id,
'comment': '',
'payment_term': sale.payment_term.id,
'fiscal_position': sale.fiscal_position.id or sale.partner_id.property_account_position.id
'tax_id': res.get('invoice_line_tax_id'),
}
self.pool.get('sale.order.line').create(cr, uid, vals, context=context)
inv_id = inv_obj.create(cr, uid, inv)
inv_obj.button_reset_taxes(cr, uid, [inv_id], context=context)
for inv in sale.invoice_ids:
ids_inv.append(inv.id)
ids_inv.append(inv_id)
obj_sale.write(cr, uid, [sale.id], {'invoice_ids': [(6, 0, ids_inv)]})
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
#
if sale.order_policy == 'picking':
vals = {
'order_id': sale.id,
'name': res.get('name'),
'price_unit': -final_amount,
'product_uom_qty': sale_adv_obj.qtty or 1.0,
'product_uos_qty': sale_adv_obj.qtty or 1.0,
'product_uos': res.get('uos_id', False),
'product_uom': res.get('uom_id', False),
'product_id': sale_adv_obj.product_id.id or False,
'discount': False,
'tax_id': res.get('invoice_line_tax_id'),
}
self.pool.get('sale.order.line').create(cr, uid, vals, context=context)
if context.get('open_invoices'):
return self.open_invoices( cr, uid, ids, list_inv, context=context)
if context.get('open_invoices', False):
return self.open_invoices( cr, uid, ids, inv_ids, context=context)
return {'type': 'ir.actions.act_window_close'}
def open_invoices(self, cr, uid, ids, invoice_ids, context=None):

View File

@ -7,19 +7,34 @@
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Advance Invoice" version="7.0">
<header>
<button name="create_invoices" string="Create Invoice" type="object" context="{'open_invoices': False}" class="oe_highlight" />
<button name="create_invoices" string="Create and view Invoice" type="object" context="{'open_invoices': True}" class="oe_highlight" />
or
<button string="Cancel" class="oe_link" special="cancel" />
</header>
<group col="4">
<field name="advance_payment_method" on_change="onchange_advance_payment_method(advance_payment_method,product_id)"/>
<newline />
<field name="qtty" invisible="1"/>
<field name="product_id" on_change="onchange_advance_payment_method(advance_payment_method,product_id)" context="{'search_default_services':1}" attrs="{'invisible': [('advance_payment_method','=','percentage')]}" colspan="2"/>
<field name="amount" colspan="2"/>
<group>
<field name="advance_payment_method"
on_change="onchange_method(advance_payment_method, product_id)"/>
<field name="product_id"
on_change="onchange_method(advance_payment_method, product_id)"
context="{'search_default_services': 1}"
attrs="{'invisible': [('advance_payment_method','!=','fixed')]}"/>
<label for="amount" attrs="{'invisible': [('advance_payment_method', 'not in', ('fixed','percentage'))]}"/>
<div attrs="{'invisible': [('advance_payment_method', 'not in', ('fixed','percentage'))]}">
<field name="amount"
attrs="{'required': [('advance_payment_method', 'in', ('fixed','percentage'))]}"/>
<label string="%%"
attrs="{'invisible': [('advance_payment_method', '!=', 'percentage')]}"/>
</div>
</group>
<footer>
<button name="create_invoices" string="Create Invoice" type="object"
class="oe_highlight"
attrs="{'invisible': [('advance_payment_method', '=', 'lines')]}"/>
<button name="create_invoices" string="Create and view Invoice" type="object"
context="{'open_invoices': True}" class="oe_highlight"
attrs="{'invisible': [('advance_payment_method', '=', 'lines')]}"/>
<button name="create_invoices" string="Show Lines to Invoice" type="object"
class="oe_highlight"
attrs="{'invisible': [('advance_payment_method', '!=', 'lines')]}"/>
or
<button string="Cancel" class="oe_link" special="cancel"/>
</footer>
</form>
</field>
</record>