[IMP]: Improved code to get/set default taxes and tax policy from config. Also set default tax on sale order.

bzr revid: uco@tinyerp.com-20120301065521-whgcng8d6zui5dfh
This commit is contained in:
Ujjvala Collins (OpenERP) 2012-03-01 12:25:21 +05:30
parent f739f06021
commit b4d57024d6
4 changed files with 72 additions and 9 deletions

View File

@ -20,6 +20,7 @@
##############################################################################
from osv import fields, osv
from tools.translate import _
class account_configuration(osv.osv_memory):
_inherit = 'res.config'
@ -37,6 +38,16 @@ class account_configuration(osv.osv_memory):
'tax_policy': 'global_on_order',
}
def get_default_tax_policy(self, cr, uid, ids, context=None):
applied_groups = self.get_default_applied_groups(cr, uid, ids, context=context)
if applied_groups.get('group_sale_taxes_global_on_order'):
applied_groups.update({'tax_policy': 'global_on_order'})
elif applied_groups.get('group_sale_taxes_on_order_line'):
applied_groups.update({'tax_policy': 'on_order_line'})
else:
applied_groups.update({'tax_policy': 'no_tax'})
return applied_groups
def _check_default_tax(self, cr, uid, context=None):
ir_values_obj = self.pool.get('ir.values')
for tax in ir_values_obj.get(cr, uid, 'default', False, ['product.product']):
@ -59,24 +70,58 @@ class account_configuration(osv.osv_memory):
if self._check_default_tax(cr, uid, context) and res['fields'].get('tax_value'):
res['fields']['tax_value'] = {'domain': [], 'views': {}, 'context': {}, 'selectable': True, 'type': 'many2one', 'relation': 'account.tax', 'string': 'Value'}
return res
def set_tax_policy(self, cr, uid, ids, vals, context=None):
data_obj = self.pool.get('ir.model.data')
users_obj = self.pool.get('res.users')
groups_obj = self.pool.get('res.groups')
ir_values_obj = self.pool.get('ir.values')
dummy,user_group_id = data_obj.get_object_reference(cr, uid, 'base', 'group_user')
tax_policy = vals.get('tax_policy')
order_group_id = data_obj.get_object(cr, uid, 'base', 'group_sale_taxes_global_on_order').id
order_line_group_id = data_obj.get_object(cr, uid, 'base', 'group_sale_taxes_on_order_line').id
group_id = False
remove_group_id = False
if tax_policy == 'global_on_order':
group_id = order_group_id
remove_group_id = order_line_group_id
elif tax_policy == 'on_order_line':
group_id = order_line_group_id
remove_group_id = order_group_id
if group_id:
groups_obj.write(cr, uid, [user_group_id], {'implied_ids': [(4,group_id)]})
users_obj.write(cr, uid, [uid], {'groups_id': [(4,group_id)]})
ir_values_obj.set(cr, uid, 'default', False, 'groups_id', ['res.users'], [(4,group_id)])
groups_obj.write(cr, uid, [user_group_id], {'implied_ids': [(3,remove_group_id)]})
users_obj.write(cr, uid, [uid], {'groups_id': [(3,remove_group_id)]})
ir_values_obj.set(cr, uid, 'default', False, 'groups_id', ['res.users'], [(3,remove_group_id)])
else:
groups += [order_group_id, remove_group_id]
for group_id in groups:
groups_obj.write(cr, uid, [user_group_id], {'implied_ids': [(3,group_id)]})
users_obj.write(cr, uid, [uid], {'groups_id': [(3,group_id)]})
ir_values_obj.set(cr, uid, 'default', False, 'groups_id', ['res.users'], [(3,group_id)])
def set_tax_value(self, cr, uid, ids, vals, context=None):
chart_account_obj = self.pool.get('wizard.multi.charts.accounts')
acc_installer_obj = self.pool.get('account.installer')
chart_template_obj = self.pool.get('account.chart.template')
tax_template_obj = self.pool.get('account.tax.template')
tax_obj = self.pool.get('account.tax')
chart_template_ids = chart_template_obj.search(cr, uid, [('visible', '=', True)], context=context)
taxes = []
if chart_template_ids:
taxes = tax_template_obj.search(cr, uid, [('chart_template_id', '=', chart_template_ids[0])], context=context)
result = {}
if not self._check_default_tax(cr, uid, context) and not taxes:
name = _('Tax %.2f%%') % vals.get('tax_value')
taxes = tax_obj.search(cr, uid, [('name', '=', name)], context=context)
check_default_taxes = self._check_default_tax(cr, uid, context)
if not check_default_taxes and not taxes:
installer_id = acc_installer_obj.create(cr, uid, {}, context=context)
acc_installer_obj.execute(cr, uid, [installer_id], context=context)
if chart_template_ids:
code_digits = chart_account_obj.onchange_chart_template_id(cr, uid, [], chart_template_ids[0], context=context)['value']['code_digits']
object_id = chart_account_obj.create(cr, uid, {'code_digits': code_digits}, context=context)
chart_account_obj.execute(cr, uid, [object_id], context=context)
return result
return check_default_taxes
account_configuration()

View File

@ -215,7 +215,7 @@ class sale_configuration(osv.osv_memory):
}, context=context)
return res
def set_groups(self, cr, uid, ids, vals, context=None):
data_obj = self.pool.get('ir.model.data')
users_obj = self.pool.get('res.users')
@ -234,6 +234,23 @@ class sale_configuration(osv.osv_memory):
users_obj.write(cr, uid, [uid], {'groups_id': [(3,group_id)]})
ir_values_obj.set(cr, uid, 'default', False, 'groups_id', ['res.users'], [(3,group_id)])
def onchange_tax_policy(self, cr, uid, ids, tax_policy, tax_value, context=None):
res = {'value': {}}
if isinstance(tax_value, (int)):
self.set_default_taxes(cr, uid, ids, {'tax_value': [tax_value]}, context=context)
self.set_tax_policy(cr, uid, ids, {'tax_policy': tax_policy}, context=context)
return res
def set_default_taxes(self, cr, uid, ids, vals, context=None):
ir_values_obj = self.pool.get('ir.values')
taxes = self._check_default_tax(cr, uid, context=context)
if isinstance(vals.get('tax_value'), list):
taxes = vals.get('tax_value')
if taxes:
ir_values_obj.set(cr, uid, 'default', False, 'tax_id', ['sale.order'], taxes[0])
ir_values_obj.set(cr, uid, 'default', False, 'tax_id', ['sale.order.line'], taxes)
ir_values_obj.set(cr, uid, 'default', False, 'taxes_id', ['product.product'], taxes)
sale_configuration()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -38,8 +38,9 @@
<field name="group_sale_pricelist_per_customer"/>
<newline/>
<group colspan="4" col="4">
<field name="tax_policy" />
<field name="tax_value" nolabel="1" attrs="{'invisible':[('tax_policy','=','no_tax')]}"/>
<field name="tax_policy" on_change="onchange_tax_policy(tax_policy,tax_value)"/>
<field name="tax_value" nolabel="1" attrs="{'invisible':[('tax_policy','=','no_tax')]}"
on_change="onchange_tax_policy(tax_policy,tax_value)"/>
</group>
<newline/>
<field name="sale_margin"/>

View File

@ -195,9 +195,9 @@
</field>
<newline/>
<group col="13" colspan="10">
<field name="tax_id" groups="base.group_sale_taxes_global_on_order"/>
<field name="amount_untaxed" sum="Untaxed amount" groups="base.group_sale_taxes_on_order_line,base.group_sale_taxes_global_on_order"/>
<field name="amount_tax" groups="base.group_sale_taxes_on_order_line,base.group_sale_taxes_global_on_order"/>
<field name="tax_id" groups="base.group_sale_taxes_global_on_order"/>
<field name="amount_total"/>
<button name="button_dummy" states="draft" string="Compute" type="object" icon="gtk-execute"/>
<button name="%(action_view_sale_advance_payment_inv)d" string="Advance Invoice" type="action" icon="gtk-execute" states="draft,manual" groups="base.group_extended"/>