← Back to team overview

credativ team mailing list archive

[Bug 909248] Re: Invoice account search in PO is not multi-company

 

I have cleaned a lot of the ir_property table and could get to workable
environment with standard code so we should close this bug for the time
being.

The malfunction was indeed here due to account setup in ir_property
table (and getting accounts from one company to another whatever the
setup is a bug) but current code seems to work in normal/simple case.

-- 
You received this bug notification because you are a member of OpenERP
Framework Experts, which is subscribed to OpenERP Addons.
https://bugs.launchpad.net/bugs/909248

Title:
  Invoice account search in PO is not multi-company

Status in OpenERP Addons (modules):
  Triaged

Bug description:
  When creating a PO in multi-company environment, accounts selected for the invoice are in the wrong company due to this piece of code:
                  if ol.product_id:
                      acc_id = ol.product_id.product_tmpl_id.property_account_expense.id
                      if not acc_id:
                          acc_id = ol.product_id.categ_id.property_account_expense_categ.id
  If there are 2 companies, it selects the accounts for the first company, whatever the company of the PO.

  I suggest the following code as a patch (inspired from invoice.py) to
  search for the correct code.

      def action_invoice_create(self, cr, uid, ids, *args):
          res = False
          journal_obj = self.pool.get('account.journal')
          for o in self.browse(cr, uid, ids):
              il = []
              todo = []
              for ol in o.order_line:
                  todo.append(ol.id)
                  if ol.product_id:
                      if o.company_id.id:
                          company_id=o.company_id.id
                          property_obj = self.pool.get('ir.property')
                          account_obj = self.pool.get('account.account')
                          exp_pro_id = property_obj.search(cr, uid, [('name','=','property_account_expense'),('res_id','=','product.template,'+str(ol.product_id.product_tmpl_id.id)+''),('company_id','=',company_id)])
                          if not exp_pro_id:
                              exp_pro_id = property_obj.search(cr, uid, [('name','=','property_account_expense_categ'),('res_id','=','product.category,'+str(ol.product_id.categ_id.id)+''),('company_id','=',company_id)])#EC
                          if not exp_pro_id:
                              ex_acc = ol.product_id.product_tmpl_id.property_account_expense
                              ex_acc_cate = ol.product_id.categ_id.property_account_expense_categ
                              if ex_acc:
                                  app_acc_exp = ex_acc
                              else:
                                  app_acc_exp = ex_acc_cate
                          else:
                              # Get the fields from the ir.property record
                              my_value = property_obj.read(cr,uid,exp_pro_id,['name','value_reference','res_id'])
                              # Parse the value_reference field to get the ID of the account.account record
                              account_id = int (my_value[0]["value_reference"].split(",")[1])
                              # Use the ID of the account.account record in the browse for the account.account record
                              app_acc_exp = account_obj.browse(cr, uid, account_id)
                          if not exp_pro_id:
                              ex_acc = ol.product_id.product_tmpl_id.property_account_expense
                              ex_acc_cate = ol.product_id.categ_id.property_account_expense_categ
                              if ex_acc:
                                  app_acc_exp = ex_acc
                              else:
                                  app_acc_exp = ex_acc_cate
                          if app_acc_exp and app_acc_exp.company_id.id != company_id:
                              exp_res_id = account_obj.search(cr, uid, [('name','=',app_acc_exp.name),('company_id','=',company_id)])
                              if not exp_res_id:
                                  raise osv.except_osv(_('Configuration Error !'),
                                      _('Can not find account chart for this company, Please Create account.'))
                              exp_obj_acc = account_obj.browse(cr, uid, exp_res_id)
                              if ex_acc:
                                  ol.product_id.product_tmpl_id.property_account_expense = exp_obj_acc[0]
                              else:
                                  ol.product_id.categ_id.property_account_expense_categ = exp_obj_acc[0]

                          a = app_acc_exp.id
                      else:
                          a = ol.product_tmpl_id.property_account_expense.id
                          if not a:
                              a = ol.categ_id.property_account_expense_categ.id
                      if not 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,))
                  else:
                      a = self.pool.get('ir.property').get(cr, uid, 'property_account_expense_categ', 'product.category').id
                  fpos = o.fiscal_position or False
                  a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, a)
                  il.append(self.inv_line_create(cr, uid, a, ol))

              a = o.partner_id.property_account_payable.id
              journal_ids = journal_obj.search(cr, uid, [('type', '=','purchase'),('company_id', '=', o.company_id.id)], limit=1)
              if not journal_ids:
                  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))
              inv = {
                  'name': o.partner_ref or o.name,
                  'reference': o.partner_ref or o.name,
                  'account_id': a,
                  'type': 'in_invoice',
                  'partner_id': o.partner_id.id,
                  'currency_id': o.pricelist_id.currency_id.id,
                  'address_invoice_id': o.partner_address_id.id,
                  'address_contact_id': o.partner_address_id.id,
                  'journal_id': len(journal_ids) and journal_ids[0] or False,
                  'origin': o.name,
                  'invoice_line': il,
                  'fiscal_position': o.fiscal_position.id or o.partner_id.property_account_position.id,
                  'payment_term': o.partner_id.property_payment_term and o.partner_id.property_payment_term.id or False,
                  'company_id': o.company_id.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

To manage notifications about this bug go to:
https://bugs.launchpad.net/openobject-addons/+bug/909248/+subscriptions


References