[IMP] purchase: force the company when creating an invoice from the PO, using context['force_company']
bzr revid: qdp-launchpad@openerp.com-20110707145832-xwdc0u3qhbaffehe
This commit is contained in:
parent
8c08d3841b
commit
e688cd74d0
|
@ -22,6 +22,7 @@
|
||||||
import time
|
import time
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
|
import tools
|
||||||
|
|
||||||
from osv import osv, fields
|
from osv import osv, fields
|
||||||
import netsvc
|
import netsvc
|
||||||
|
@ -345,53 +346,59 @@ class purchase_order(osv.osv):
|
||||||
self.log(cr, uid, id, message)
|
self.log(cr, uid, id, message)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def action_invoice_create(self, cr, uid, ids, *args):
|
def action_invoice_create(self, cr, uid, ids, context=None, *args):
|
||||||
|
if context is None:
|
||||||
|
context = {}
|
||||||
res = False
|
res = False
|
||||||
|
|
||||||
journal_obj = self.pool.get('account.journal')
|
journal_obj = self.pool.get('account.journal')
|
||||||
for o in self.browse(cr, uid, ids):
|
set_company = tools.get_and_sort_by_field(cr, uid, obj=self, ids=ids, field='company_id', context=context)
|
||||||
il = []
|
for company_id, po_ids in set_company.items():
|
||||||
todo = []
|
ctx = context.copy()
|
||||||
for ol in o.order_line:
|
ctx.update({'force_company': company_id})
|
||||||
todo.append(ol.id)
|
for o in self.browse(cr, uid, po_ids, context=ctx):
|
||||||
if ol.product_id:
|
il = []
|
||||||
a = ol.product_id.product_tmpl_id.property_account_expense.id
|
todo = []
|
||||||
if not a:
|
for ol in o.order_line:
|
||||||
a = ol.product_id.categ_id.property_account_expense_categ.id
|
todo.append(ol.id)
|
||||||
if not a:
|
if ol.product_id:
|
||||||
raise osv.except_osv(_('Error !'), _('There is no expense account defined for this product: "%s" (id:%d)') % (ol.product_id.name, ol.product_id.id,))
|
a = ol.product_id.product_tmpl_id.property_account_expense.id
|
||||||
else:
|
if not a:
|
||||||
a = self.pool.get('ir.property').get(cr, uid, 'property_account_expense_categ', 'product.category').id
|
a = ol.product_id.categ_id.property_account_expense_categ.id
|
||||||
fpos = o.fiscal_position or False
|
if not a:
|
||||||
a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, a)
|
raise osv.except_osv(_('Error !'), _('There is no expense account defined for this product: "%s" (id:%d)') % (ol.product_id.name, ol.product_id.id,))
|
||||||
il.append(self.inv_line_create(cr, uid, a, ol))
|
else:
|
||||||
|
a = self.pool.get('ir.property').get(cr, uid, 'property_account_expense_categ', 'product.category').id
|
||||||
a = o.partner_id.property_account_payable.id
|
fpos = o.fiscal_position or False
|
||||||
journal_ids = journal_obj.search(cr, uid, [('type', '=','purchase'),('company_id', '=', o.company_id.id)], limit=1)
|
a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, a)
|
||||||
if not journal_ids:
|
il.append(self.inv_line_create(cr, uid, a, ol))
|
||||||
raise osv.except_osv(_('Error !'),
|
|
||||||
_('There is no purchase journal defined for this company: "%s" (id:%d)') % (o.company_id.name, o.company_id.id))
|
a = o.partner_id.property_account_payable.id
|
||||||
inv = {
|
journal_ids = journal_obj.search(cr, uid, [('type', '=','purchase'),('company_id', '=', o.company_id.id)], limit=1)
|
||||||
'name': o.partner_ref or o.name,
|
if not journal_ids:
|
||||||
'reference': o.partner_ref or o.name,
|
raise osv.except_osv(_('Error !'),
|
||||||
'account_id': a,
|
_('There is no purchase journal defined for this company: "%s" (id:%d)') % (o.company_id.name, o.company_id.id))
|
||||||
'type': 'in_invoice',
|
inv = {
|
||||||
'partner_id': o.partner_id.id,
|
'name': o.partner_ref or o.name,
|
||||||
'currency_id': o.pricelist_id.currency_id.id,
|
'reference': o.partner_ref or o.name,
|
||||||
'address_invoice_id': o.partner_address_id.id,
|
'account_id': a,
|
||||||
'address_contact_id': o.partner_address_id.id,
|
'type': 'in_invoice',
|
||||||
'journal_id': len(journal_ids) and journal_ids[0] or False,
|
'partner_id': o.partner_id.id,
|
||||||
'origin': o.name,
|
'currency_id': o.pricelist_id.currency_id.id,
|
||||||
'invoice_line': il,
|
'address_invoice_id': o.partner_address_id.id,
|
||||||
'fiscal_position': o.fiscal_position.id or o.partner_id.property_account_position.id,
|
'address_contact_id': o.partner_address_id.id,
|
||||||
'payment_term': o.partner_id.property_payment_term and o.partner_id.property_payment_term.id or False,
|
'journal_id': len(journal_ids) and journal_ids[0] or False,
|
||||||
'company_id': o.company_id.id,
|
'origin': o.name,
|
||||||
}
|
'invoice_line': il,
|
||||||
inv_id = self.pool.get('account.invoice').create(cr, uid, inv, {'type':'in_invoice'})
|
'fiscal_position': o.fiscal_position.id or o.partner_id.property_account_position.id,
|
||||||
self.pool.get('account.invoice').button_compute(cr, uid, [inv_id], {'type':'in_invoice'}, set_total=True)
|
'payment_term': o.partner_id.property_payment_term and o.partner_id.property_payment_term.id or False,
|
||||||
self.pool.get('purchase.order.line').write(cr, uid, todo, {'invoiced':True})
|
'company_id': o.company_id.id,
|
||||||
self.write(cr, uid, [o.id], {'invoice_ids': [(4, inv_id)]})
|
}
|
||||||
res = inv_id
|
inv_id = self.pool.get('account.invoice').create(cr, uid, inv, {'type':'in_invoice'})
|
||||||
|
self.pool.get('account.invoice').button_compute(cr, uid, [inv_id], {'type':'in_invoice'}, set_total=True)
|
||||||
|
self.pool.get('purchase.order.line').write(cr, uid, todo, {'invoiced':True})
|
||||||
|
self.write(cr, uid, [o.id], {'invoice_ids': [(4, inv_id)]})
|
||||||
|
res = inv_id
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def has_stockable_product(self,cr, uid, ids, *args):
|
def has_stockable_product(self,cr, uid, ids, *args):
|
||||||
|
|
Loading…
Reference in New Issue