Hello, I am trying to automatically generate a draft invoice once the inventory validates incoming items.
version: Odoo v12 CE
The Receipts order is automatically generated by Odoo when Purchase Order is confirmed, so the Source Document field 'origin' and 'purchase_id' is set accordingly.
Invoice policy is also set to 'delivered quantity'.
In model 'stock.picking', the Validate button action is as following
@api.multi
def button_validate(self):
self.ensure_one()
if not self.move_lines and not self.move_line_ids:
raise UserError(_('Please add some items to move.'))
# If no lots when needed, raise error
picking_type = self.picking_type_id
precision_digits = self.env['decimal.precision'].precision_get('Product Unit of Measure')
no_quantities_done = all(float_is_zero(move_line.qty_done, precision_digits=precision_digits) for move_line in self.move_line_ids)
no_reserved_quantities = all(float_is_zero(move_line.product_qty, precision_rounding=move_line.product_uom_id.rounding) for move_line in self.move_line_ids)
if no_reserved_quantities and no_quantities_done:
raise UserError(_('You cannot validate a transfer if no quantites are reserved nor done. To force the transfer, switch in edit more and encode the done quantities.'))
if picking_type.use_create_lots or picking_type.use_existing_lots:
lines_to_check = self.move_line_ids
if not no_quantities_done:
lines_to_check = lines_to_check.filtered(
lambda line: float_compare(line.qty_done, 0,
precision_rounding=line.product_uom_id.rounding)
)
for line in lines_to_check:
product = line.product_id
if product and product.tracking != 'none':
if not line.lot_name and not line.lot_id:
raise UserError(_('You need to supply a Lot/Serial number for product %s.') % product.display_name)
if no_quantities_done:
view = self.env.ref('stock.view_immediate_transfer')
wiz = self.env['stock.immediate.transfer'].create({'pick_ids': [(4, self.id)]})
return {
'name': _('Immediate Transfer?'),
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'stock.immediate.transfer',
'views': [(view.id, 'form')],
'view_id': view.id,
'target': 'new',
'res_id': wiz.id,
'context': self.env.context,
}
if self._get_overprocessed_stock_moves() and not self._context.get('skip_overprocessed_check'):
view = self.env.ref('stock.view_overprocessed_transfer')
wiz = self.env['stock.overprocessed.transfer'].create({'picking_id': self.id})
return {
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'stock.overprocessed.transfer',
'views': [(view.id, 'form')],
'view_id': view.id,
'target': 'new',
'res_id': wiz.id,
'context': self.env.context,
}
# Check backorder should check for other barcodes
if self._check_backorder():
return self.action_generate_backorder_wizard()
self.action_done()
return
In model 'purchase.order' the Create Bill button action is as following
@api.multi
def action_view_invoice(self):
'''
This function returns an action that display existing vendor bills of given purchase order ids.
When only one found, show the vendor bill immediately.
'''
action = self.env.ref('account.action_vendor_bill_template')
result = action.read()[0]
create_bill = self.env.context.get('create_bill', False)
# override the context to get rid of the default filtering
result['context'] = {
'type': 'in_invoice',
'default_purchase_id': self.id,
'default_currency_id': self.currency_id.id,
'default_company_id': self.company_id.id,
'company_id': self.company_id.id
}
# choose the view_mode accordingly
if len(self.invoice_ids) > 1 and not create_bill:
result['domain'] = "[('id', 'in', " + str(self.invoice_ids.ids) + ")]"
else:
res = self.env.ref('account.invoice_supplier_form', False)
result['views'] = [(res and res.id or False, 'form')]
# Do not set an invoice_id if we want to create a new bill.
if not create_bill:
result['res_id'] = self.invoice_ids.id or False
return result
My understanding it to inherit 'stock.picking', override method 'button_validate' and add logic to pass value to model 'purchase.order' to trigger 'action_view_invoice', then finally save the record in database.
Here comes my code,
class InheritPuchaseOrder(models.Model):
_inherit = 'stock.picking'
@api.multi
def button_validate(self):
super().button_validate()
po = self.env["purchase.order"].browse(self.purchase_id.id)
I don't know what to do next, whether or not I am on the right track.
Any help is appreciated, THX!