Skip to content

Commit 5248027

Browse files
committed
[IMP] purchase: invoice creation more modular/extensible through the introduction of a _prepare_invoice method()
This is similar to what was previously done for Sales Orders. bzr revid: [email protected]
1 parent 7dedde9 commit 5248027

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

addons/purchase/purchase.py

+38-22
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,41 @@ def _prepare_inv_line(self, cr, uid, account_id, order_line, context=None):
494494
'purchase_line_id': order_line.id,
495495
}
496496

497+
def _prepare_invoice(self, cr, uid, order, line_ids, context=None):
498+
"""Prepare the dict of values to create the new invoice for a
499+
purchase order. This method may be overridden to implement custom
500+
invoice generation (making sure to call super() to establish
501+
a clean extension chain).
502+
503+
:param browse_record order: purchase.order record to invoice
504+
:param list(int) line_ids: list of invoice line IDs that must be
505+
attached to the invoice
506+
:return: dict of value to create() the invoice
507+
"""
508+
journal_ids = self.pool['account.journal'].search(
509+
cr, uid, [('type', '=', 'purchase'),
510+
('company_id', '=', order.company_id.id)],
511+
limit=1)
512+
if not journal_ids:
513+
raise osv.except_osv(
514+
_('Error!'),
515+
_('Define purchase journal for this company: "%s" (id:%d).') % \
516+
(order.company_id.name, order.company_id.id))
517+
return {
518+
'name': order.partner_ref or order.name,
519+
'reference': order.partner_ref or order.name,
520+
'account_id': order.partner_id.property_account_payable.id,
521+
'type': 'in_invoice',
522+
'partner_id': order.partner_id.id,
523+
'currency_id': order.currency_id.id,
524+
'journal_id': len(journal_ids) and journal_ids[0] or False,
525+
'invoice_line': [(6, 0, line_ids)],
526+
'origin': order.name,
527+
'fiscal_position': order.fiscal_position.id or False,
528+
'payment_term': order.payment_term_id.id or False,
529+
'company_id': order.company_id.id,
530+
}
531+
497532
def action_cancel_draft(self, cr, uid, ids, context=None):
498533
if not len(ids):
499534
return False
@@ -512,7 +547,7 @@ def action_invoice_create(self, cr, uid, ids, context=None):
512547
"""
513548
if context is None:
514549
context = {}
515-
journal_obj = self.pool.get('account.journal')
550+
516551
inv_obj = self.pool.get('account.invoice')
517552
inv_line_obj = self.pool.get('account.invoice.line')
518553

@@ -525,37 +560,18 @@ def action_invoice_create(self, cr, uid, ids, context=None):
525560
#then re-do a browse to read the property fields for the good company.
526561
context['force_company'] = order.company_id.id
527562
order = self.browse(cr, uid, order.id, context=context)
528-
pay_acc_id = order.partner_id.property_account_payable.id
529-
journal_ids = journal_obj.search(cr, uid, [('type', '=', 'purchase'), ('company_id', '=', order.company_id.id)], limit=1)
530-
if not journal_ids:
531-
raise osv.except_osv(_('Error!'),
532-
_('Define purchase journal for this company: "%s" (id:%d).') % (order.company_id.name, order.company_id.id))
533-
563+
534564
# generate invoice line correspond to PO line and link that to created invoice (inv_id) and PO line
535565
inv_lines = []
536566
for po_line in order.order_line:
537567
acc_id = self._choose_account_from_po_line(cr, uid, po_line, context=context)
538568
inv_line_data = self._prepare_inv_line(cr, uid, acc_id, po_line, context=context)
539569
inv_line_id = inv_line_obj.create(cr, uid, inv_line_data, context=context)
540570
inv_lines.append(inv_line_id)
541-
542571
po_line.write({'invoice_lines': [(4, inv_line_id)]}, context=context)
543572

544573
# get invoice data and create invoice
545-
inv_data = {
546-
'name': order.partner_ref or order.name,
547-
'reference': order.partner_ref or order.name,
548-
'account_id': pay_acc_id,
549-
'type': 'in_invoice',
550-
'partner_id': order.partner_id.id,
551-
'currency_id': order.currency_id.id,
552-
'journal_id': len(journal_ids) and journal_ids[0] or False,
553-
'invoice_line': [(6, 0, inv_lines)],
554-
'origin': order.name,
555-
'fiscal_position': order.fiscal_position.id or False,
556-
'payment_term': order.payment_term_id.id or False,
557-
'company_id': order.company_id.id,
558-
}
574+
inv_data = self._prepare_invoice(cr, uid, order, inv_lines, context=context)
559575
inv_id = inv_obj.create(cr, uid, inv_data, context=context)
560576

561577
# compute the invoice

0 commit comments

Comments
 (0)