[IMP] account_voucher: onchange_partner_id small refactoring + improved the splitting of the amount by existing voucher lines
bzr revid: qdp-launchpad@openerp.com-20111020144217-t4owndjuntdeuts9
This commit is contained in:
parent
6588eeaae2
commit
9ecff1245a
|
@ -427,36 +427,31 @@ class account_voucher(osv.osv):
|
|||
if date:
|
||||
context_multi_currency.update({'date': date})
|
||||
|
||||
line_pool = self.pool.get('account.voucher.line')
|
||||
line_ids = ids and line_pool.search(cr, uid, [('voucher_id', '=', ids[0])]) or False
|
||||
if line_ids:
|
||||
line_pool.unlink(cr, uid, line_ids)
|
||||
|
||||
currency_pool = self.pool.get('res.currency')
|
||||
move_line_pool = self.pool.get('account.move.line')
|
||||
partner_pool = self.pool.get('res.partner')
|
||||
journal_pool = self.pool.get('account.journal')
|
||||
line_pool = self.pool.get('account.voucher.line')
|
||||
|
||||
#get default values
|
||||
vals = self.onchange_journal(cr, uid, ids, journal_id, [], False, partner_id, context)
|
||||
vals = vals.get('value')
|
||||
|
||||
journal = journal_pool.browse(cr, uid, journal_id, context=context)
|
||||
currency_id = vals.get('currency_id', currency_id)
|
||||
default = {
|
||||
'value':{'line_ids':[], 'line_dr_ids':[], 'line_cr_ids':[], 'pre_line': False, 'currency_id':currency_id},
|
||||
}
|
||||
currency_id = currency_id or journal.company_id.currency_id.id
|
||||
|
||||
#drop existing lines
|
||||
line_ids = ids and line_pool.search(cr, uid, [('voucher_id', '=', ids[0])]) or False
|
||||
if line_ids:
|
||||
line_pool.unlink(cr, uid, line_ids)
|
||||
|
||||
if not partner_id:
|
||||
return default
|
||||
|
||||
if not partner_id and ids:
|
||||
line_ids = line_pool.search(cr, uid, [('voucher_id', '=', ids[0])])
|
||||
if line_ids:
|
||||
line_pool.unlink(cr, uid, line_ids)
|
||||
return default
|
||||
|
||||
journal = journal_pool.browse(cr, uid, journal_id, context=context)
|
||||
partner = partner_pool.browse(cr, uid, partner_id, context=context)
|
||||
currency_id = currency_id or journal.company_id.currency_id.id
|
||||
account_id = False
|
||||
if journal.type in ('sale','sale_refund'):
|
||||
account_id = partner.property_account_receivable.id
|
||||
|
@ -484,64 +479,78 @@ class account_voucher(osv.osv):
|
|||
ids = move_line_pool.search(cr, uid, [('state','=','valid'), ('account_id.type', '=', account_type), ('reconcile_id', '=', False), ('partner_id', '=', partner_id)], context=context)
|
||||
else:
|
||||
ids = context['move_line_ids']
|
||||
invoice_id = context.get('invoice_id', False)
|
||||
company_currency = journal.company_id.currency_id.id
|
||||
move_line_found = False
|
||||
|
||||
#order the lines by most old first
|
||||
ids.reverse()
|
||||
moves = move_line_pool.browse(cr, uid, ids, context=context)
|
||||
|
||||
#company_currency = journal.company_id.currency_id.id
|
||||
#if company_currency != currency_id and ttype == 'payment':
|
||||
# total_debit = currency_pool.compute(cr, uid, currency_id, company_currency, total_debit, context=context_multi_currency)
|
||||
#elif company_currency != currency_id and ttype == 'receipt':
|
||||
# total_credit = currency_pool.compute(cr, uid, currency_id, company_currency, total_credit, context=context_multi_currency)
|
||||
|
||||
company_currency = journal.company_id.currency_id.id
|
||||
for line in moves:
|
||||
if line.credit and line.reconcile_partial_id and ttype == 'receipt':
|
||||
continue
|
||||
if line.debit and line.reconcile_partial_id and ttype == 'payment':
|
||||
continue
|
||||
if invoice_id:
|
||||
if line.invoice.id == invoice_id:
|
||||
#if the invoice linked to the voucher line is equal to the invoice_id in context
|
||||
#then we assign the amount on that line, whatever the other voucher lines
|
||||
move_line_found = line.id
|
||||
break
|
||||
elif currency_id == company_currency:
|
||||
#otherwise treatments is the same but with other field names
|
||||
if line.amount_residual == price:
|
||||
#if the amount residual is equal the amount voucher, we assign it to that voucher
|
||||
#line, whatever the other voucher lines
|
||||
move_line_found = line.id
|
||||
break
|
||||
#otherwise we will split the voucher amount on each line (by most old first)
|
||||
total_credit += line.credit or 0.0
|
||||
total_debit += line.debit or 0.0
|
||||
elif currency_id == line.currency_id.id:
|
||||
if line.amount_residual_currency == price:
|
||||
move_line_found = line.id
|
||||
break
|
||||
total_credit += line.credit and line.amount_currency or 0.0
|
||||
total_debit += line.debit and line.amount_currency or 0.0
|
||||
|
||||
if line.currency_id and currency_id==line.currency_id.id:
|
||||
total_credit += line.amount_currency <0 and -line.amount_currency or 0.0
|
||||
total_debit += line.amount_currency >0 and line.amount_currency or 0.0
|
||||
else:
|
||||
total_credit += currency_pool.compute(cr, uid, company_currency, currency_id, line.credit or 0.0)
|
||||
total_debit += currency_pool.compute(cr, uid, company_currency, currency_id, line.debit or 0.0)
|
||||
|
||||
#voucher line creation
|
||||
for line in moves:
|
||||
if line.credit and line.reconcile_partial_id and ttype == 'receipt':
|
||||
continue
|
||||
if line.debit and line.reconcile_partial_id and ttype == 'payment':
|
||||
continue
|
||||
|
||||
if line.currency_id and currency_id==line.currency_id.id:
|
||||
amount_original = abs(line.amount_currency)
|
||||
amount_unreconciled = abs(line.amount_residual_currency)
|
||||
else:
|
||||
amount_original = currency_pool.compute(cr, uid, company_currency, currency_id, line.credit or line.debit or 0.0)
|
||||
amount_unreconciled = currency_pool.compute(cr, uid, company_currency, currency_id, abs(line.amount_residual))
|
||||
|
||||
#original_amount = line.credit or line.debit or 0.0
|
||||
#amount_unreconciled = currency_pool.compute(cr, uid, line.currency_id and line.currency_id.id or company_currency, currency_id, abs(line.amount_residual_currency), context=context_multi_currency)
|
||||
rs = {
|
||||
'name':line.move_id.name,
|
||||
'type': line.credit and 'dr' or 'cr',
|
||||
'move_line_id':line.id,
|
||||
'account_id':line.account_id.id,
|
||||
'amount_original': amount_original,
|
||||
'amount': (move_line_found == line.id) and min(price, amount_unreconciled) or 0.0,
|
||||
'date_original':line.date,
|
||||
'date_due':line.date_maturity,
|
||||
'amount_unreconciled': amount_unreconciled,
|
||||
|
||||
}
|
||||
|
||||
if line.credit:
|
||||
amount = min(amount_unreconciled, total_debit)
|
||||
rs['amount'] = amount
|
||||
total_debit -= amount
|
||||
else:
|
||||
amount = min(amount_unreconciled, total_credit)
|
||||
rs['amount'] = amount
|
||||
total_credit -= amount
|
||||
#split voucher amount by most old first, but only for lines in the same currency
|
||||
if not move_line_found:
|
||||
line_currency_id = line.currency_id and line.currency_id.id or company_currency
|
||||
if currency_id == line_currency_id:
|
||||
if line.credit:
|
||||
amount = min(amount_unreconciled, abs(total_debit))
|
||||
rs['amount'] = amount
|
||||
total_debit -= amount
|
||||
else:
|
||||
amount = min(amount_unreconciled, abs(total_credit))
|
||||
rs['amount'] = amount
|
||||
total_credit -= amount
|
||||
|
||||
default['value']['line_ids'].append(rs)
|
||||
if rs['type'] == 'cr':
|
||||
|
|
|
@ -45,10 +45,11 @@ class invoice(osv.osv):
|
|||
'close_after_process': True,
|
||||
'invoice_type':inv.type,
|
||||
'invoice_id':inv.id,
|
||||
'default_type': inv.type in ('out_invoice','out_refund') and 'receipt' or 'payment'
|
||||
'default_type': inv.type in ('out_invoice','out_refund') and 'receipt' or 'payment',
|
||||
'type': inv.type in ('out_invoice','out_refund') and 'receipt' or 'payment'
|
||||
}
|
||||
}
|
||||
|
||||
invoice()
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
|
|
@ -74,14 +74,14 @@
|
|||
<field name="arch" type="xml">
|
||||
<form string="Bill Payment">
|
||||
<group col="6" colspan="4">
|
||||
<field name="partner_id" required="1" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date)" context="{'invoice_currency':currency_id}" string="Supplier"/>
|
||||
<field name="amount" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date)"/>
|
||||
<field name="partner_id" required="1" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)" context="{'invoice_currency':currency_id}" string="Supplier"/>
|
||||
<field name="amount" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)"/>
|
||||
<field name="journal_id"
|
||||
domain="[('type','in',['bank', 'cash'])]"
|
||||
widget="selection" select="1"
|
||||
on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date)"
|
||||
on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)"
|
||||
string="Payment Method"/>
|
||||
<field name="date" select="1" on_change="onchange_date(partner_id, journal_id, amount, currency_id, type, date)"/>
|
||||
<field name="date" select="1" on_change="onchange_date(partner_id, journal_id, amount, currency_id, type, date, context)"/>
|
||||
<field name="reference" select="1" string="Payment Ref"/>
|
||||
<field name="name" colspan="2"/>
|
||||
<field name="account_id"
|
||||
|
@ -92,7 +92,7 @@
|
|||
</group>
|
||||
<notebook colspan="4">
|
||||
<page string="Payment Information">
|
||||
<field name="line_dr_ids" attrs="{'invisible': [('type', '=', 'receipt')]}" default_get="{'journal_id':journal_id, 'type':type, 'partner_id':partner_id}" colspan="4" nolabel="1" height="140" on_change="onchange_line_ids(line_dr_ids, line_cr_ids, amount)">
|
||||
<field name="line_dr_ids" attrs="{'invisible': [('type', '=', 'receipt')]}" default_get="{'journal_id':journal_id, 'type':type, 'partner_id':partner_id}" colspan="4" nolabel="1" height="140" on_change="onchange_line_ids(line_dr_ids, line_cr_ids, amount, context)">
|
||||
<tree string="Open Supplier Journal Entries" editable="bottom">
|
||||
<field name="move_line_id" context="{'journal_id':parent.journal_id, 'partner_id':parent.partner_id}"
|
||||
on_change="onchange_move_line_id(move_line_id)"
|
||||
|
@ -106,7 +106,7 @@
|
|||
<field name="amount" sum="Payment"/>
|
||||
</tree>
|
||||
</field>
|
||||
<field name="line_cr_ids" colspan="4" nolabel="1" attrs="{'invisible': [('type', '=', 'payment')]}" default_get="{'journal_id':journal_id, 'partner_id':partner_id}" on_change="onchange_line_ids(line_dr_ids, line_cr_ids, amount)">
|
||||
<field name="line_cr_ids" colspan="4" nolabel="1" attrs="{'invisible': [('type', '=', 'payment')]}" default_get="{'journal_id':journal_id, 'partner_id':partner_id}" on_change="onchange_line_ids(line_dr_ids, line_cr_ids, amount, context)">
|
||||
<tree string="Open Customer Journal Entries" editable="bottom">
|
||||
<field name="move_line_id"/>
|
||||
<field name="account_id" groups="base.group_extended" domain="[('type','=','receivable')]"/>
|
||||
|
@ -137,15 +137,15 @@
|
|||
<field name="arch" type="xml">
|
||||
<form string="Bill Payment">
|
||||
<group col="6" colspan="4">
|
||||
<field name="partner_id" domain="[('supplier','=',True)]" required="1" invisible="context.get('line_type', False)" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date)" context="{'invoice_currency':currency_id}" string="Supplier"/>
|
||||
<field name="partner_id" domain="[('supplier','=',True)]" required="1" invisible="context.get('line_type', False)" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)" context="{'invoice_currency':currency_id}" string="Supplier"/>
|
||||
<field name="amount" invisible="context.get('line_type', False)" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date)"/>
|
||||
<field name="journal_id"
|
||||
domain="[('type','in',['bank', 'cash'])]"
|
||||
invisible="context.get('line_type', False)"
|
||||
widget="selection" select="1"
|
||||
on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date)"
|
||||
on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)"
|
||||
string="Payment Method"/>
|
||||
<field name="date" select="1" invisible="context.get('line_type', False)" on_change="onchange_date(partner_id, journal_id, amount, currency_id, type, date)"/>
|
||||
<field name="date" select="1" invisible="context.get('line_type', False)" on_change="onchange_date(partner_id, journal_id, amount, currency_id, type, date, context)"/>
|
||||
<field name="reference" select="1" invisible="context.get('line_type', False)" string="Payment Ref"/>
|
||||
<field name="name" colspan="2" invisible="context.get('line_type', False)"/>
|
||||
<field name="company_id" widget="selection" groups="base.group_multi_company"/>
|
||||
|
@ -279,19 +279,19 @@
|
|||
<field name="arch" type="xml">
|
||||
<form string="Customer Payment">
|
||||
<group col="6" colspan="4">
|
||||
<field name="partner_id" required="1" invisible="context.get('line_type', False)" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date)" string="Customer"/>
|
||||
<field name="partner_id" required="1" invisible="context.get('line_type', False)" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)" string="Customer"/>
|
||||
<field name="currency_id" invisible="1"/>
|
||||
<field name="amount"
|
||||
invisible="context.get('line_type', False)"
|
||||
string="Paid Amount"
|
||||
on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date)"/>
|
||||
on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)"/>
|
||||
<field name="journal_id"
|
||||
domain="[('type','in',['bank', 'cash'])]"
|
||||
invisible="context.get('line_type', False)"
|
||||
widget="selection" select="1"
|
||||
on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date)"
|
||||
on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)"
|
||||
string="Payment Method"/>
|
||||
<field name="date" select="1" invisible="context.get('line_type', False)" on_change="onchange_date(partner_id, journal_id, amount, currency_id, type, date)"/>
|
||||
<field name="date" select="1" invisible="context.get('line_type', False)" on_change="onchange_date(partner_id, journal_id, amount, currency_id, type, date, context)"/>
|
||||
<field name="reference" select="1" invisible="context.get('line_type', False)" string="Payment Ref"/>
|
||||
<field name="name" colspan="2" invisible="context.get('line_type', False)"/>
|
||||
<field name="company_id" widget="selection" groups="base.group_multi_company"/>
|
||||
|
@ -303,7 +303,7 @@
|
|||
</group>
|
||||
<notebook colspan="4">
|
||||
<page string="Payment Information">
|
||||
<field name="line_cr_ids" default_get="{'journal_id':journal_id, 'type':type, 'partner_id':partner_id}" colspan="4" nolabel="1" height="140" on_change="onchange_line_ids(line_dr_ids, line_cr_ids, amount)">
|
||||
<field name="line_cr_ids" default_get="{'journal_id':journal_id, 'type':type, 'partner_id':partner_id}" colspan="4" nolabel="1" height="140" on_change="onchange_line_ids(line_dr_ids, line_cr_ids, amount, context)">
|
||||
<tree string="Invoices and outstanding transactions" editable="bottom">
|
||||
<field name="move_line_id" context="{'journal_id':parent.journal_id, 'partner_id':parent.partner_id}"
|
||||
on_change="onchange_move_line_id(move_line_id)"
|
||||
|
@ -318,7 +318,7 @@
|
|||
<field name="amount" sum="Payment"/>
|
||||
</tree>
|
||||
</field>
|
||||
<field name="line_dr_ids" colspan="4" nolabel="1" attrs="{'invisible': [('pre_line','=',False)]}" default_get="{'journal_id':journal_id, 'partner_id':partner_id}" on_change="onchange_line_ids(line_dr_ids, line_cr_ids, amount)">
|
||||
<field name="line_dr_ids" colspan="4" nolabel="1" attrs="{'invisible': [('pre_line','=',False)]}" default_get="{'journal_id':journal_id, 'partner_id':partner_id}" on_change="onchange_line_ids(line_dr_ids, line_cr_ids, amount, context)">
|
||||
<tree string="Credits" editable="bottom">
|
||||
<field name="move_line_id"/>
|
||||
<field name="account_id" groups="base.group_extended" domain="[('type','=','receivable')]"/>
|
||||
|
|
Loading…
Reference in New Issue