mirror of
https://github.com/OCA/bank-payment.git
synced 2025-02-02 10:37:31 +02:00
account_banking: adds company awareness when configuring bank statement import settings
Accounts, journals and bank accounts are now filtered by the company setting on the payment mode. Only bank accounts from the company's partner can be selected. Upon selection of the bank account, the associated journal is selected automatically.
This commit is contained in:
@@ -81,6 +81,10 @@ class account_banking_account_settings(osv.osv):
|
||||
select=True, required=True),
|
||||
'journal_id': fields.many2one('account.journal', 'Journal',
|
||||
required=True),
|
||||
'partner_id': fields.related(
|
||||
'company_id', 'partner_id',
|
||||
type='many2one', relation='res.partner',
|
||||
string='Partner'),
|
||||
'default_credit_account_id': fields.many2one(
|
||||
'account.account', 'Default credit account', select=True,
|
||||
help=('The account to use when an unexpected payment was signaled. '
|
||||
@@ -127,44 +131,59 @@ class account_banking_account_settings(osv.osv):
|
||||
#),
|
||||
}
|
||||
|
||||
def _default_company(self, cursor, uid, context=None):
|
||||
user = self.pool.get('res.users').browse(cursor, uid, uid, context=context)
|
||||
if user.company_id:
|
||||
return user.company_id.id
|
||||
company_ids = self.pool.get('res.company').search(
|
||||
cursor, uid, [('parent_id', '=', False)])
|
||||
return len(company_ids) == 1 and company_ids[0] or False
|
||||
|
||||
def _default_journal(self, cr, uid, context=None):
|
||||
domain = [('type', '=', 'bank')]
|
||||
def _default_company(self, cr, uid, context=None):
|
||||
"""
|
||||
Return the user's company or the first company found
|
||||
in the database
|
||||
"""
|
||||
user = self.pool.get('res.users').read(
|
||||
cr, uid, uid, ['company_id'], context=context)
|
||||
if user['company_id']:
|
||||
domain.append(('company_id', '=', user['company_id'][0]))
|
||||
return user['company_id'][0]
|
||||
return self.pool.get('res.company').search(
|
||||
cr, uid, [('parent_id', '=', False)])[0]
|
||||
|
||||
def _default_partner_id(self, cr, uid, context=None, company_id=False):
|
||||
if not company_id:
|
||||
company_id = self._default_company(cr, uid, context=context)
|
||||
return self.pool.get('res.company').read(
|
||||
cr, uid, company_id, ['partner_id'],
|
||||
context=context)['partner_id'][0]
|
||||
|
||||
def _default_journal(self, cr, uid, context=None, company_id=False):
|
||||
if not company_id:
|
||||
company_id = self._default_company(cr, uid, context=context)
|
||||
journal_ids = self.pool.get('account.journal').search(
|
||||
cr, uid, domain)
|
||||
return len(journal_ids) == 1 and journal_ids[0] or False
|
||||
cr, uid, [('type', '=', 'bank'), ('company_id', '=', company_id)])
|
||||
return journal_ids and journal_ids[0] or False
|
||||
|
||||
def _default_partner_bank_id(self, cr, uid, context=None):
|
||||
user = self.pool.get('res.users').read(
|
||||
cr, uid, uid, ['company_id'], context=context)
|
||||
if user['company_id']:
|
||||
bank_ids = self.pool.get('res.partner.bank').search(
|
||||
cr, uid, [('company_id', '=', user['company_id'][0])])
|
||||
if len(bank_ids) == 1:
|
||||
return bank_ids[0]
|
||||
return False
|
||||
def _default_partner_bank_id(
|
||||
self, cr, uid, context=None, company_id=False):
|
||||
if not company_id:
|
||||
company_id = self._default_company(cr, uid, context=context)
|
||||
partner_id = self.pool.get('res.company').read(
|
||||
cr, uid, company_id, ['partner_id'], context=context)['partner_id'][0]
|
||||
bank_ids = self.pool.get('res.partner.bank').search(
|
||||
cr, uid, [('partner_id', '=', partner_id)], context=context)
|
||||
return bank_ids and bank_ids[0] or False
|
||||
|
||||
def _default_debit_account_id(self, cr, uid, context=None):
|
||||
def _default_debit_account_id(
|
||||
self, cr, uid, context=None, company_id=False):
|
||||
localcontext = context and context.copy() or {}
|
||||
localcontext['force_company'] = (
|
||||
company_id or self._default_company(cr, uid, context=context))
|
||||
account_def = self.pool.get('ir.property').get(
|
||||
cr, uid, 'property_account_receivable',
|
||||
'res.partner', context=context)
|
||||
'res.partner', context=localcontext)
|
||||
return account_def and account_def.id or False
|
||||
|
||||
def _default_credit_account_id(self, cr, uid, context=None):
|
||||
def _default_credit_account_id(self, cr, uid, context=None, company_id=False):
|
||||
localcontext = context and context.copy() or {}
|
||||
localcontext['force_company'] = (
|
||||
company_id or self._default_company(cr, uid, context=context))
|
||||
account_def = self.pool.get('ir.property').get(
|
||||
cr, uid, 'property_account_payable',
|
||||
'res.partner', context=context)
|
||||
'res.partner', context=localcontext)
|
||||
return account_def and account_def.id or False
|
||||
|
||||
def find(self, cr, uid, journal_id, partner_bank_id=False, context=None):
|
||||
@@ -173,8 +192,35 @@ class account_banking_account_settings(osv.osv):
|
||||
domain.append(('partner_bank_id','=',partner_bank_id))
|
||||
return self.search(cr, uid, domain, context=context)
|
||||
|
||||
def onchange_partner_bank_id(
|
||||
self, cr, uid, ids, partner_bank_id, context=None):
|
||||
values = {}
|
||||
if partner_bank_id:
|
||||
bank = self.pool.get('res.partner.bank').read(
|
||||
cr, uid, partner_bank_id, ['journal_id'], context=context)
|
||||
if bank['journal_id']:
|
||||
values['journal_id'] = bank['journal_id'][0]
|
||||
return {'value': values}
|
||||
|
||||
def onchange_company_id (
|
||||
self, cr, uid, ids, company_id=False, context=None):
|
||||
if not company_id:
|
||||
return {}
|
||||
result = {
|
||||
'partner_id': self._default_partner_id(
|
||||
cr, uid, company_id=company_id, context=context),
|
||||
'journal_id': self._default_journal(
|
||||
cr, uid, company_id=company_id, context=context),
|
||||
'default_debit_account_id': self._default_debit_account_id(
|
||||
cr, uid, company_id=company_id, context=context),
|
||||
'default_credit_account_id': self._default_credit_account_id(
|
||||
cr, uid, company_id=company_id, context=context),
|
||||
}
|
||||
return {'value': result}
|
||||
|
||||
_defaults = {
|
||||
'company_id': _default_company,
|
||||
'partner_id': _default_partner_id,
|
||||
'journal_id': _default_journal,
|
||||
'default_debit_account_id': _default_debit_account_id,
|
||||
'default_credit_account_id': _default_credit_account_id,
|
||||
|
||||
@@ -39,17 +39,31 @@
|
||||
<field name="type">form</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="Default Import Settings for Bank Account">
|
||||
<field name="company_id" />
|
||||
<field name="company_id"
|
||||
widget='selection'
|
||||
groups="base.group_multi_company"
|
||||
on_change="onchange_company_id(company_id)" />
|
||||
<separator string="Bank Account Details" colspan="4" />
|
||||
<field name="partner_bank_id" /> <!-- Needs domain for used companies /-->
|
||||
<field name="journal_id" domain="[('type','=','bank')]" />
|
||||
<field name="partner_id" invisible="1"/>
|
||||
<field name="partner_bank_id"
|
||||
domain="[('partner_id', '=', partner_id)]"
|
||||
on_change="onchange_partner_bank_id(partner_bank_id)" />
|
||||
<field name="journal_id"
|
||||
domain="[('type','=','bank'),
|
||||
('company_id', '=', company_id)]" />
|
||||
<separator string="Default Accounts for Unknown Movements" colspan="4" />
|
||||
<field name="default_credit_account_id" />
|
||||
<field name="default_debit_account_id" />
|
||||
<field name="default_credit_account_id"
|
||||
domain="[('company_id', '=', company_id)]" />
|
||||
<field name="default_debit_account_id"
|
||||
domain="[('company_id', '=', company_id)]" />
|
||||
<separator string="Generation of Bank Costs Invoices" colspan="4" />
|
||||
<field name="bank_partner_id" />
|
||||
<field name="costs_account_id" attrs="{'required': [('bank_partner_id', '<>', False)]}" />
|
||||
<field name="invoice_journal_id" attrs="{'required': [('bank_partner_id', '<>', False)]}" />
|
||||
<field name="costs_account_id"
|
||||
attrs="{'required': [('bank_partner_id', '!=', False)]}"
|
||||
domain="[('company_id', '=', company_id)]" />
|
||||
<field name="invoice_journal_id"
|
||||
attrs="{'required': [('bank_partner_id', '!=', False)]}"
|
||||
domain="[('company_id', '=', company_id)]" />
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
@@ -60,8 +74,8 @@
|
||||
<field name="arch" type="xml">
|
||||
<tree string="Default Import Settings for Bank Account">
|
||||
<field name="company_id" />
|
||||
<field name="partner_bank_id" /> <!-- Needs domain for used companies /-->
|
||||
<field name="journal_id" domain="[('type','=','bank')]" />
|
||||
<field name="partner_bank_id" />
|
||||
<field name="journal_id" />
|
||||
</tree>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
Reference in New Issue
Block a user