[IMP] delivery: refactor shipping cost invoicing to be more modular
This will make modular extensions easier and cleaner to implement. bzr revid: odo@openerp.com-20111221181149-a4xus72g3h4x3lko
This commit is contained in:
parent
fd50a36583
commit
f8e1ddfbc1
|
@ -68,70 +68,69 @@ class stock_picking(osv.osv):
|
|||
'number_of_packages': fields.integer('Number of Packages'),
|
||||
}
|
||||
|
||||
def action_invoice_create(self, cursor, user, ids, journal_id=False,
|
||||
def _prepare_shipping_invoice_line(self, cr, uid, picking, invoice, context=None):
|
||||
"""Prepare the invoice line to add to the shipping costs to the shipping's
|
||||
invoice.
|
||||
|
||||
:param browse_record picking: the stock picking being invoiced
|
||||
:param browse_record invoice: the stock picking's invoice
|
||||
:return: dict containing the values to create the invoice line,
|
||||
or None to create nothing
|
||||
"""
|
||||
carrier_obj = self.pool.get('delivery.carrier')
|
||||
grid_obj = self.pool.get('delivery.grid')
|
||||
if not picking.carrier_id or \
|
||||
any(inv_line.product_id.id == picking.carrier_id.product_id.id
|
||||
for inv_line in invoice.invoice_line):
|
||||
return None
|
||||
grid_id = carrier_obj.grid_get(cr, uid, [picking.carrier_id.id],
|
||||
picking.address_id.id, context=context)
|
||||
if not grid_id:
|
||||
raise osv.except_osv(_('Warning'),
|
||||
_('The carrier %s (id: %d) has no delivery grid!') \
|
||||
% (picking.carrier_id.name,
|
||||
picking.carrier_id.id))
|
||||
price = grid_obj.get_price_from_picking(cr, uid, grid_id,
|
||||
invoice.amount_untaxed, picking.weight, picking.volume,
|
||||
context=context)
|
||||
account_id = picking.carrier_id.product_id.property_account_income.id
|
||||
if not account_id:
|
||||
account_id = picking.carrier_id.product_id.categ_id\
|
||||
.property_account_income_categ.id
|
||||
|
||||
taxes = picking.carrier_id.product_id.taxes_id
|
||||
partner = picking.address_id.partner_id or False
|
||||
if partner:
|
||||
account_id = self.pool.get('account.fiscal.position').map_account(cr, uid, partner.property_account_position, account_id)
|
||||
taxes_ids = self.pool.get('account.fiscal.position').map_tax(cr, uid, partner.property_account_position, taxes)
|
||||
else:
|
||||
taxes_ids = [x.id for x in taxes]
|
||||
|
||||
return {
|
||||
'name': picking.carrier_id.name,
|
||||
'invoice_id': invoice.id,
|
||||
'uos_id': picking.carrier_id.product_id.uos_id.id,
|
||||
'product_id': picking.carrier_id.product_id.id,
|
||||
'account_id': account_id,
|
||||
'price_unit': price,
|
||||
'quantity': 1,
|
||||
'invoice_line_tax_id': [(6, 0, taxes_ids)],
|
||||
}
|
||||
|
||||
def action_invoice_create(self, cr, uid, ids, journal_id=False,
|
||||
group=False, type='out_invoice', context=None):
|
||||
invoice_obj = self.pool.get('account.invoice')
|
||||
picking_obj = self.pool.get('stock.picking')
|
||||
carrier_obj = self.pool.get('delivery.carrier')
|
||||
grid_obj = self.pool.get('delivery.grid')
|
||||
invoice_line_obj = self.pool.get('account.invoice.line')
|
||||
|
||||
result = super(stock_picking, self).action_invoice_create(cursor, user,
|
||||
invoice_line_obj = self.pool.get('account.invoice.line')
|
||||
result = super(stock_picking, self).action_invoice_create(cr, uid,
|
||||
ids, journal_id=journal_id, group=group, type=type,
|
||||
context=context)
|
||||
|
||||
picking_ids = result.keys()
|
||||
invoice_ids = result.values()
|
||||
|
||||
invoices = {}
|
||||
for invoice in invoice_obj.browse(cursor, user, invoice_ids,
|
||||
context=context):
|
||||
invoices[invoice.id] = invoice
|
||||
|
||||
for picking in picking_obj.browse(cursor, user, picking_ids,
|
||||
context=context):
|
||||
if not picking.carrier_id:
|
||||
continue
|
||||
grid_id = carrier_obj.grid_get(cursor, user, [picking.carrier_id.id],
|
||||
picking.address_id.id, context=context)
|
||||
if not grid_id:
|
||||
raise osv.except_osv(_('Warning'),
|
||||
_('The carrier %s (id: %d) has no delivery grid!') \
|
||||
% (picking.carrier_id.name,
|
||||
picking.carrier_id.id))
|
||||
invoice = invoices[result[picking.id]]
|
||||
price = grid_obj.get_price_from_picking(cursor, user, grid_id,
|
||||
invoice.amount_untaxed, picking.weight, picking.volume,
|
||||
context=context)
|
||||
account_id = picking.carrier_id.product_id.product_tmpl_id\
|
||||
.property_account_income.id
|
||||
if not account_id:
|
||||
account_id = picking.carrier_id.product_id.categ_id\
|
||||
.property_account_income_categ.id
|
||||
|
||||
taxes = picking.carrier_id.product_id.taxes_id
|
||||
|
||||
partner_id=picking.address_id.partner_id and picking.address_id.partner_id.id or False
|
||||
taxes_ids = [x.id for x in picking.carrier_id.product_id.taxes_id]
|
||||
if partner_id:
|
||||
partner = picking.address_id.partner_id
|
||||
account_id = self.pool.get('account.fiscal.position').map_account(cursor, user, partner.property_account_position, account_id)
|
||||
taxes_ids = self.pool.get('account.fiscal.position').map_tax(cursor, user, partner.property_account_position, taxes)
|
||||
|
||||
if any(inv_line.product_id.id == picking.carrier_id.product_id.id for inv_line in invoice.invoice_line):
|
||||
continue
|
||||
else:
|
||||
invoice_line_obj.create(cursor, user, {
|
||||
'name': picking.carrier_id.name,
|
||||
'invoice_id': invoice.id,
|
||||
'uos_id': picking.carrier_id.product_id.uos_id.id,
|
||||
'product_id': picking.carrier_id.product_id.id,
|
||||
'account_id': account_id,
|
||||
'price_unit': price,
|
||||
'quantity': 1,
|
||||
'invoice_line_tax_id': [(6, 0,taxes_ids)],
|
||||
})
|
||||
invoice_obj.button_compute(cursor, user, [invoice.id], context=context)
|
||||
for picking in picking_obj.browse(cr, uid, result.keys(), context=context):
|
||||
invoice = invoice_obj.browse(cr, uid, result[picking.id], context=context)
|
||||
invoice_line = self._prepare_shipping_invoice_line(cr, uid, picking, invoice, context=context)
|
||||
if invoice_line:
|
||||
invoice_line_obj.create(cr, uid, invoice_line)
|
||||
invoice_obj.button_compute(cr, uid, [invoice.id], context=context)
|
||||
return result
|
||||
|
||||
stock_picking()
|
||||
|
|
Loading…
Reference in New Issue