@@ -494,6 +494,41 @@ def _prepare_inv_line(self, cr, uid, account_id, order_line, context=None):
494
494
'purchase_line_id' : order_line .id ,
495
495
}
496
496
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
+
497
532
def action_cancel_draft (self , cr , uid , ids , context = None ):
498
533
if not len (ids ):
499
534
return False
@@ -512,7 +547,7 @@ def action_invoice_create(self, cr, uid, ids, context=None):
512
547
"""
513
548
if context is None :
514
549
context = {}
515
- journal_obj = self . pool . get ( 'account.journal' )
550
+
516
551
inv_obj = self .pool .get ('account.invoice' )
517
552
inv_line_obj = self .pool .get ('account.invoice.line' )
518
553
@@ -525,37 +560,18 @@ def action_invoice_create(self, cr, uid, ids, context=None):
525
560
#then re-do a browse to read the property fields for the good company.
526
561
context ['force_company' ] = order .company_id .id
527
562
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
+
534
564
# generate invoice line correspond to PO line and link that to created invoice (inv_id) and PO line
535
565
inv_lines = []
536
566
for po_line in order .order_line :
537
567
acc_id = self ._choose_account_from_po_line (cr , uid , po_line , context = context )
538
568
inv_line_data = self ._prepare_inv_line (cr , uid , acc_id , po_line , context = context )
539
569
inv_line_id = inv_line_obj .create (cr , uid , inv_line_data , context = context )
540
570
inv_lines .append (inv_line_id )
541
-
542
571
po_line .write ({'invoice_lines' : [(4 , inv_line_id )]}, context = context )
543
572
544
573
# 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 )
559
575
inv_id = inv_obj .create (cr , uid , inv_data , context = context )
560
576
561
577
# compute the invoice
0 commit comments