from datetime import datetime
from dateutil.relativedelta import relativedelta
from operator import itemgetter
-from os.path import join as opj
import netsvc
import pooler
-import tools
from osv import fields, osv
import decimal_precision as dp
from tools.translate import _
_description = "Journal"
_columns = {
'name': fields.char('Journal Name', size=64, required=True),
- 'code': fields.char('Code', size=5, required=True, help="The code will be used to generate the numbers of the journal entries of this journal."),
+ 'code': fields.char('Code', size=5, required=True, help="The code will be displayed on reports."),
'type': fields.selection([('sale', 'Sale'),('sale_refund','Sale Refund'), ('purchase', 'Purchase'), ('purchase_refund','Purchase Refund'), ('cash', 'Cash'), ('bank', 'Bank and Cheques'), ('general', 'General'), ('situation', 'Opening/Closing Situation')], 'Type', size=32, required=True,
help="Select 'Sale' for Sale journal to be used at the time of making invoice."\
" Select 'Purchase' for Purchase Journal to be used at the time of approving purchase order."\
seq = {
'name': name,
'code': code,
+ 'company_id': vals['company_id'],
'active': True,
'prefix': code + "/%(year)s/",
'padding': 4,
#CHECKME: shouldn't we check the state of the period?
ids = self.search(cr, uid, [('date_start','<=',dt),('date_stop','>=',dt)])
if not ids:
- raise osv.except_osv(_('Error !'), _('No period defined for this date: %s !\nPlease create a fiscal year.')%dt)
+ raise osv.except_osv(_('Error !'), _('No period defined for this date: %s !\nPlease create one.')%dt)
return ids
def action_draft(self, cr, uid, ids, *args):
'partner_id': fields.related('line_id', 'partner_id', type="many2one", relation="res.partner", string="Partner", store=True),
'amount': fields.function(_amount_compute, string='Amount', digits_compute=dp.get_precision('Account'), type='float', fnct_search=_search_amount),
'date': fields.date('Date', required=True, states={'posted':[('readonly',True)]}, select=True),
- 'narration':fields.text('Narration'),
+ 'narration':fields.text('Internal Note'),
'company_id': fields.related('journal_id','company_id',type='many2one',relation='res.company',string='Company', store=True, readonly=True),
}
_defaults = {
'child_parent_ids':fields.one2many('account.account.template', 'parent_id', 'Children'),
'tax_ids': fields.many2many('account.tax.template', 'account_account_template_tax_rel', 'account_id', 'tax_id', 'Default Taxes'),
'nocreate': fields.boolean('Optional create', help="If checked, the new chart of accounts will not contain this by default."),
+ 'chart_template_id': fields.many2one('account.chart.template', 'Chart Template'),
}
_defaults = {
res.append((record['id'],name ))
return res
+
+ def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
+ # This method is used to get only account templates relavant to the current COA template
+ # but not sure of it, if its really to be used, put proper value of context in xml to use it
+ if context is None:
+ context = {}
+ if context.get("coa_template"):
+ args += ['|',('chart_template_id', '=', context.get("coa_template")),('chart_template_id', '=', False)]
+ return super(account_account_template, self).search(cr, uid, args, offset, limit,
+ order, context=context, count=count)
+
+
+ def generate_account(self, cr, uid, account_root_id, tax_template_ref, code_digits, company_id, context=None):
+ """
+ This method for generating accounts from templates.
+ @param cr: A database cursor.
+ @param uid: ID of the user currently logged in.
+ @param account_root_id: Root account id getting from current template.
+ @param tax_template_ref: Taxes templates reference for write taxes_id in account_account.
+ @param code_digits: Digit getting from wizard.multi.charts.accounts.,this is use for account code.
+ @param company_id: company_id selected from wizard.multi.charts.accounts.
+ @return : return acc_template_ref for reference purpose.
+
+ """
+ if context is None:
+ context = {}
+ obj_acc = self.pool.get('account.account')
+ company_name = self.pool.get('res.company').browse(cr, uid, company_id, context=context).name
+ acc_template_ref = {}
+ #deactivate the parent_store functionnality on account_account for rapidity purpose
+ ctx = context.copy()
+ ctx.update({'defer_parent_store_computation': True})
+ children_acc_template = self.search(cr, uid, [('parent_id','child_of', [account_root_id]),('nocreate','!=',True)], order='id')
+ for account_template in self.browse(cr, uid, children_acc_template, context=context):
+ tax_ids = []
+ for tax in account_template.tax_ids:
+ tax_ids.append(tax_template_ref[tax.id])
+
+ code_main = account_template.code and len(account_template.code) or 0
+ code_acc = account_template.code or ''
+ if code_main > 0 and code_main <= code_digits and account_template.type != 'view':
+ code_acc = str(code_acc) + (str('0'*(code_digits-code_main)))
+ vals={
+ 'name': (account_root_id == account_template.id) and company_name or account_template.name,
+ 'currency_id': account_template.currency_id and account_template.currency_id.id or False,
+ 'code': code_acc,
+ 'type': account_template.type,
+ 'user_type': account_template.user_type and account_template.user_type.id or False,
+ 'reconcile': account_template.reconcile,
+ 'shortcut': account_template.shortcut,
+ 'note': account_template.note,
+ 'parent_id': account_template.parent_id and ((account_template.parent_id.id in acc_template_ref) and acc_template_ref[account_template.parent_id.id]) or False,
+ 'tax_ids': [(6,0,tax_ids)],
+ 'company_id': company_id,
+ }
+ new_account = obj_acc.create(cr, uid, vals, context=ctx)
+ acc_template_ref[account_template.id] = new_account
+
+ #reactivate the parent_store functionnality on account_account
+ obj_acc._parent_store_compute(cr)
+ return acc_template_ref
+
account_account_template()
class account_add_tmpl_wizard(osv.osv_memory):
_columns={
'name': fields.char('Name', size=64, required=True),
'parent_id': fields.many2one('account.chart.template', 'Parent Chart Template'),
+ 'code_digits': fields.integer('# of Digits', required=True, help="No. of Digits to use for account code"),
'visible': fields.boolean('Can be Visible?', help="Set this to False if you don't want this template to be used actively in the wizard that generate Chart of Accounts from templates, this is useful when you want to generate accounts of this template only when loading its child template."),
- 'set_tax_complete': fields.boolean('Complete Set Of Tax', help="Set this False, then instead of the 2 many2one for default sale and purchase taxes, we display the default sale and purchase rates float field in the wizard that generate Chart of Accounts from templates"),
+ 'set_tax_complete': fields.boolean('Complete Set of Tax', help="Check this if you want to create new default Sales and Purchase taxes from selected rate instead of choosing it from list of taxes."),
'account_root_id': fields.many2one('account.account.template', 'Root Account', domain=[('parent_id','=',False)]),
'tax_code_root_id': fields.many2one('account.tax.code.template', 'Root Tax Code', domain=[('parent_id','=',False)]),
'tax_template_ids': fields.one2many('account.tax.template', 'chart_template_id', 'Tax Template List', help='List of all the taxes that have to be installed by the wizard'),
'property_account_expense_opening': fields.many2one('account.account.template', 'Opening Entries Expense Account'),
}
+ _defaults = {
+ 'visible': True,
+ 'code_digits': 6
+ }
+
account_chart_template()
class account_tax_template(osv.osv):
'description': fields.char('Internal Name', size=32),
'type_tax_use': fields.selection([('sale','Sale'),('purchase','Purchase'),('all','All')], 'Tax Use In', required=True,),
'price_include': fields.boolean('Tax Included in Price', help="Check this if the price you use on the product and invoices includes this tax."),
+ 'installable': fields.boolean('Should be Installed', help="Set this to False if you do not want to create real tax object from this template.")
}
def name_get(self, cr, uid, ids, context=None):
'include_base_amount': False,
'type_tax_use': 'all',
'price_include': 0,
+ 'installable': True
}
_order = 'sequence'
+ def generate_tax(self, cr, uid, tax_templates, tax_code_template_ref, company_id, context=None):
+ """
+ This method generate taxes from templates.
+ @param cr: A database cursor.
+ @param uid: ID of the user currently logged in.
+ @param tax_templates: Tax templates.
+ @param tax_code_template_ref: Taxcode templates reference.
+ @param company_id: if tax generated from account multi wizard at that time company_id is wizard company_id field
+ or logged user company_id.
+ @param Return:
+ {'taxes_id': New generated taxes ids,
+ 'account_dict': Used this reference value for Account Tax,
+ 'tax_template_ref': Used this reference value for Fiscal Position
+ }
+ """
+ if context is None:
+ context = {}
+ res = {}
+ todo_dict = {}
+ tax_template_ref = {}
+ tax_template_to_tax = {}
+ for tax in tax_templates:
+ vals_tax = {
+ 'name':tax.name,
+ 'sequence': tax.sequence,
+ 'amount': tax.amount,
+ 'type': tax.type,
+ 'applicable_type': tax.applicable_type,
+ 'domain': tax.domain,
+ 'parent_id': tax.parent_id and ((tax.parent_id.id in tax_template_ref) and tax_template_ref[tax.parent_id.id]) or False,
+ 'child_depend': tax.child_depend,
+ 'python_compute': tax.python_compute,
+ 'python_compute_inv': tax.python_compute_inv,
+ 'python_applicable': tax.python_applicable,
+ 'base_code_id': tax.base_code_id and ((tax.base_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.base_code_id.id]) or False,
+ 'tax_code_id': tax.tax_code_id and ((tax.tax_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.tax_code_id.id]) or False,
+ 'base_sign': tax.base_sign,
+ 'tax_sign': tax.tax_sign,
+ 'ref_base_code_id': tax.ref_base_code_id and ((tax.ref_base_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.ref_base_code_id.id]) or False,
+ 'ref_tax_code_id': tax.ref_tax_code_id and ((tax.ref_tax_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.ref_tax_code_id.id]) or False,
+ 'ref_base_sign': tax.ref_base_sign,
+ 'ref_tax_sign': tax.ref_tax_sign,
+ 'include_base_amount': tax.include_base_amount,
+ 'description': tax.description,
+ 'company_id': company_id,
+ 'type_tax_use': tax.type_tax_use,
+ 'price_include': tax.price_include
+ }
+ new_tax = self.pool.get('account.tax').create(cr, uid, vals_tax)
+ tax_template_to_tax[tax.id] = new_tax
+ #as the accounts have not been created yet, we have to wait before filling these fields
+ todo_dict[new_tax] = {
+ 'account_collected_id': tax.account_collected_id and tax.account_collected_id.id or False,
+ 'account_paid_id': tax.account_paid_id and tax.account_paid_id.id or False,
+ }
+ tax_template_ref[tax.id] = new_tax
+ res.update({'taxes_id': tax_template_to_tax, 'account_dict': todo_dict, 'tax_template_ref': tax_template_ref})
+ return res
+
account_tax_template()
# Fiscal Position Templates
'tax_ids': fields.one2many('account.fiscal.position.tax.template', 'position_id', 'Tax Mapping')
}
+ def generate_fiscal_position(self, cr, uid, chart_temp_id, tax_template_ref, acc_template_ref, company_id, context=None):
+ """
+ This method generate Fiscal Position , Fiscal Position Accounts and Fiscal Position Taxes from templates.
+ @param cr: A database cursor.
+ @param uid: ID of the user currently logged in.
+ @param chart_temp_id: Chart Template Id.
+ @param taxes_ids: Taxes templates reference for generating account.fiscal.position.tax.
+ @param acc_template_ref: Account templates reference for generating account.fiscal.position.account.
+ @param company_id: company_id selected from wizard.multi.charts.accounts.
+ """
+ if context is None:
+ context = {}
+ obj_tax_fp = self.pool.get('account.fiscal.position.tax')
+ obj_ac_fp = self.pool.get('account.fiscal.position.account')
+ obj_fiscal_position = self.pool.get('account.fiscal.position')
+ fp_ids = self.search(cr, uid, [('chart_template_id', '=', chart_temp_id)])
+ for position in self.browse(cr, uid, fp_ids, context=context):
+ new_fp = obj_fiscal_position.create(cr, uid, {'company_id': company_id, 'name': position.name})
+ for tax in position.tax_ids:
+ obj_tax_fp.create(cr, uid, {
+ 'tax_src_id': tax_template_ref[tax.tax_src_id.id],
+ 'tax_dest_id': tax.tax_dest_id and tax_template_ref[tax.tax_dest_id.id] or False,
+ 'position_id': new_fp
+ })
+ for acc in position.account_ids:
+ obj_ac_fp.create(cr, uid, {
+ 'account_src_id': acc_template_ref[acc.account_src_id.id],
+ 'account_dest_id': acc_template_ref[acc.account_dest_id.id],
+ 'position_id': new_fp
+ })
+ return {}
+
account_fiscal_position_template()
class account_fiscal_position_tax_template(osv.osv):
'bank_accounts_id': fields.one2many('account.bank.accounts.wizard', 'bank_account_id', 'Bank Accounts', required=True),
'code_digits':fields.integer('# of Digits', required=True, help="No. of Digits to use for account code"),
'seq_journal':fields.boolean('Separated Journal Sequences', help="Check this box if you want to use a different sequence for each created journal. Otherwise, all will use the same sequence."),
- "sale_tax": fields.many2one("account.tax.template", "Default Sale Tax"),
+ "sale_tax": fields.many2one("account.tax.template", "Default Sales Tax"),
"purchase_tax": fields.many2one("account.tax.template", "Default Purchase Tax"),
- 'sale_tax_rate': fields.float('Sale Tax(%)'),
+ 'sale_tax_rate': fields.float('Sales Tax(%)'),
'purchase_tax_rate': fields.float('Purchase Tax(%)'),
'complete_tax': fields.boolean('Complete Tax'),
}
def onchange_chart_template_id(self, cr, uid, ids, chart_template_id=False, context=None):
res = {}
tax_templ_obj = self.pool.get('account.tax.template')
- res['value'] = {'complete_tax': False, 'sale_tax': False, 'purchase_tax': False}
+ res['value'] = {'complete_tax': False, 'sale_tax': False, 'purchase_tax': False, 'code_digits': 0}
if chart_template_id:
- complete_tax = self.pool.get('account.chart.template').browse(cr, uid, chart_template_id, context=context).set_tax_complete
+ data = self.pool.get('account.chart.template').browse(cr, uid, chart_template_id, context=context)
+ complete_tax = data.set_tax_complete
if complete_tax:
# default tax is given by the lowesst sequence. For same sequence we will take the latest created as it will be the case for tax created while isntalling the generic chart of account
sale_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id"
- , "=", chart_template_id), ('type_tax_use', 'in', ('sale','all'))], order="sequence, id desc")
+ , "=", chart_template_id), ('type_tax_use', 'in', ('sale','all')), ('installable', '=', True)], order="sequence, id desc")
purchase_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id"
- , "=", chart_template_id), ('type_tax_use', 'in', ('purchase','all'))], order="sequence, id desc")
+ , "=", chart_template_id), ('type_tax_use', 'in', ('purchase','all')), ('installable', '=', True)], order="sequence, id desc")
res['value'].update({'sale_tax': sale_tax_ids and sale_tax_ids[0] or False, 'purchase_tax': purchase_tax_ids and purchase_tax_ids[0] or False})
- res['value'].update({'complete_tax': complete_tax})
+ res['value'].update({'complete_tax': complete_tax, 'code_digits': data.code_digits})
return res
{'acc_name': _('Cash'), 'account_type': 'cash'}]})
if 'company_id' in fields:
res.update({'company_id': self.pool.get('res.users').browse(cr, uid, [uid], context=context)[0].company_id.id})
- if 'code_digits' in fields:
- res.update({'code_digits': 6})
if 'seq_journal' in fields:
res.update({'seq_journal': True})
res.update({'chart_template_id': ids[0]})
if 'sale_tax' in fields:
sale_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id"
- , "=", ids[0]), ('type_tax_use', 'in', ('sale','all'))], order="sequence")
+ , "=", ids[0]), ('type_tax_use', 'in', ('sale','all')), ('installable', '=', True)], order="sequence")
res.update({'sale_tax': sale_tax_ids and sale_tax_ids[0] or False})
if 'purchase_tax' in fields:
purchase_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id"
- , "=", ids[0]), ('type_tax_use', 'in', ('purchase','all'))], order="sequence")
+ , "=", ids[0]), ('type_tax_use', 'in', ('purchase','all')), ('installable', '=', True)], order="sequence")
res.update({'purchase_tax': purchase_tax_ids and purchase_tax_ids[0] or False})
+
return res
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
res['fields'][field]['selection'] = template_select
return res
- def execute(self, cr, uid, ids, context=None):
- obj_multi = self.browse(cr, uid, ids[0])
- obj_acc = self.pool.get('account.account')
- obj_acc_tax = self.pool.get('account.tax')
- obj_tax_temp = self.pool.get('account.tax.template')
- obj_journal = self.pool.get('account.journal')
- obj_acc_template = self.pool.get('account.account.template')
- obj_fiscal_position_template = self.pool.get('account.fiscal.position.template')
- obj_fiscal_position = self.pool.get('account.fiscal.position')
+ def generate_journals(self, cr, uid, chart_template_id, acc_template_ref, company_id, context=None):
+ """
+ This method used for creating journals.
+ @param cr: A database cursor.
+ @param uid: ID of the user currently logged in.
+ @param chart_temp_id: Chart Template Id.
+ @param acc_template_ref: Account templates reference.
+ @param company_id: company_id selected from wizard.multi.charts.accounts.
+ """
+
obj_data = self.pool.get('ir.model.data')
analytic_journal_obj = self.pool.get('account.analytic.journal')
- obj_tax_code = self.pool.get('account.tax.code')
- obj_tax_code_template = self.pool.get('account.tax.code.template')
- ir_values_obj = self.pool.get('ir.values')
- # Creating Account
- obj_acc_root = obj_multi.chart_template_id.account_root_id
- tax_code_root_id = obj_multi.chart_template_id.tax_code_root_id.id
- company_id = obj_multi.company_id.id
+ obj_journal = self.pool.get('account.journal')
- #new code
- acc_template_ref = {}
- tax_template_ref = {}
- tax_code_template_ref = {}
- todo_dict = {}
+ data = obj_data.get_object_reference(cr, uid, 'account', 'account_sp_journal_view')
+ view_id = data and data[1] or False
- # create tax templates and real taxes from purchase_tax_rate,sale_tax_rate fields
- if not obj_multi.complete_tax:
- fp = tools.file_open(opj('account', 'configurable_account_chart.xml'))
- tools.convert_xml_import(cr, 'account', fp, {}, 'init', True, None)
- fp.close()
- s_tax = (obj_multi.sale_tax_rate/100)
- p_tax = (obj_multi.purchase_tax_rate/100)
- pur_temp_tax = obj_data.get_object_reference(cr, uid, 'account', 'tax_code_base_purchases')
- pur_temp_tax_id = pur_temp_tax and pur_temp_tax[1] or False
-
- pur_temp_tax_paid = obj_data.get_object_reference(cr, uid, 'account', 'tax_code_output')
- pur_temp_tax_paid_id = pur_temp_tax_paid and pur_temp_tax_paid[1] or False
-
- sale_temp_tax = obj_data.get_object_reference(cr, uid, 'account', 'tax_code_base_sales')
- sale_temp_tax_id = sale_temp_tax and sale_temp_tax[1] or False
-
- sale_temp_tax_paid = obj_data.get_object_reference(cr, uid, 'account', 'tax_code_input')
- sale_temp_tax_paid_id = sale_temp_tax_paid and sale_temp_tax_paid[1] or False
-
- chart_temp_id = obj_multi.chart_template_id.id or False
- if s_tax * 100 > 0.0:
- tax_account_ids = obj_acc_template.search(cr, uid, [('name', '=', 'Tax Received')], context=context)
- sales_tax_account_id = tax_account_ids and tax_account_ids[0] or False
- vals_tax_code_temp = {
- 'name': _('TAX %s%%') % (s_tax*100),
- 'code': _('TAX %s%%') % (s_tax*100),
- 'parent_id': sale_temp_tax_id
- }
- new_tax_code_temp = obj_tax_code_template.create(cr, uid, vals_tax_code_temp, context=context)
- vals_paid_tax_code_temp = {
- 'name': _('TAX Received %s%%') % (s_tax*100),
- 'code': _('TAX Received %s%%') % (s_tax*100),
- 'parent_id': sale_temp_tax_paid_id
- }
- new_paid_tax_code_temp = obj_tax_code_template.create(cr, uid, vals_paid_tax_code_temp, context=context)
- sales_tax_temp = obj_tax_temp.create(cr, uid, {
- 'name': _('TAX %s%%') % (s_tax*100),
- 'description': _('TAX %s%%') % (s_tax*100),
- 'amount': s_tax,
- 'base_code_id': new_tax_code_temp,
- 'tax_code_id': new_paid_tax_code_temp,
- 'ref_base_code_id': new_tax_code_temp,
- 'ref_tax_code_id': new_paid_tax_code_temp,
- 'type_tax_use': 'sale',
- 'type': 'percent',
- 'sequence': 0,
- 'account_collected_id': sales_tax_account_id,
- 'account_paid_id': sales_tax_account_id,
- 'chart_template_id': chart_temp_id,
- }, context=context)
- if p_tax * 100 > 0.0:
- tax_account_ids = obj_acc_template.search(cr, uid, [('name', '=', 'Tax Paid')], context=context)
- purchase_tax_account_id = tax_account_ids and tax_account_ids[0] or False
- vals_tax_code_temp = {
- 'name': _('TAX %s%%') % (p_tax*100),
- 'code': _('TAX %s%%') % (p_tax*100),
- 'parent_id': pur_temp_tax_id
- }
- new_tax_code_temp = obj_tax_code_template.create(cr, uid, vals_tax_code_temp, context=context)
- vals_paid_tax_code_temp = {
- 'name': _('TAX Paid %s%%') % (p_tax*100),
- 'code': _('TAX Paid %s%%') % (p_tax*100),
- 'parent_id': pur_temp_tax_paid_id
- }
- new_paid_tax_code_temp = obj_tax_code_template.create(cr, uid, vals_paid_tax_code_temp, context=context)
- purchase_tax_temp = obj_tax_temp.create(cr, uid, {
- 'name': _('TAX %s%%') % (p_tax*100),
- 'description': _('TAX %s%%') % (p_tax*100),
- 'amount': p_tax,
- 'base_code_id': new_tax_code_temp,
- 'tax_code_id': new_paid_tax_code_temp,
- 'ref_base_code_id': new_tax_code_temp,
- 'ref_tax_code_id': new_paid_tax_code_temp,
- 'type_tax_use': 'purchase',
- 'type': 'percent',
- 'sequence': 0,
- 'account_collected_id': purchase_tax_account_id,
- 'account_paid_id': purchase_tax_account_id,
- 'chart_template_id': chart_temp_id,
- }, context=context)
+ income_acc_id = acc_template_ref.get(chart_template_id.property_account_income_categ.id)
+ expense_acc_id = acc_template_ref.get(chart_template_id.property_account_expense_categ.id)
+ credit_acc_id = acc_template_ref.get(chart_template_id.property_account_income_opening.id)
+ debit_acc_id = acc_template_ref.get(chart_template_id.property_account_expense_opening.id)
- #create all the tax code
- children_tax_code_template = obj_tax_code_template.search(cr, uid, [('parent_id','child_of',[tax_code_root_id])], order='id')
- children_tax_code_template.sort()
- for tax_code_template in obj_tax_code_template.browse(cr, uid, children_tax_code_template, context=context):
- vals = {
- 'name': (tax_code_root_id == tax_code_template.id) and obj_multi.company_id.name or tax_code_template.name,
- 'code': tax_code_template.code,
- 'info': tax_code_template.info,
- 'parent_id': tax_code_template.parent_id and ((tax_code_template.parent_id.id in tax_code_template_ref) and tax_code_template_ref[tax_code_template.parent_id.id]) or False,
- 'company_id': company_id,
- 'sign': tax_code_template.sign,
- }
- new_tax_code = obj_tax_code.create(cr, uid, vals)
- #recording the new tax code to do the mapping
- tax_code_template_ref[tax_code_template.id] = new_tax_code
-
- #create all the tax
- tax_template_to_tax = {}
- for tax in obj_multi.chart_template_id.tax_template_ids:
- #create it
- vals_tax = {
- 'name':tax.name,
- 'sequence': tax.sequence,
- 'amount':tax.amount,
- 'type':tax.type,
- 'applicable_type': tax.applicable_type,
- 'domain':tax.domain,
- 'parent_id': tax.parent_id and ((tax.parent_id.id in tax_template_ref) and tax_template_ref[tax.parent_id.id]) or False,
- 'child_depend': tax.child_depend,
- 'python_compute': tax.python_compute,
- 'python_compute_inv': tax.python_compute_inv,
- 'python_applicable': tax.python_applicable,
- 'base_code_id': tax.base_code_id and ((tax.base_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.base_code_id.id]) or False,
- 'tax_code_id': tax.tax_code_id and ((tax.tax_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.tax_code_id.id]) or False,
- 'base_sign': tax.base_sign,
- 'tax_sign': tax.tax_sign,
- 'ref_base_code_id': tax.ref_base_code_id and ((tax.ref_base_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.ref_base_code_id.id]) or False,
- 'ref_tax_code_id': tax.ref_tax_code_id and ((tax.ref_tax_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.ref_tax_code_id.id]) or False,
- 'ref_base_sign': tax.ref_base_sign,
- 'ref_tax_sign': tax.ref_tax_sign,
- 'include_base_amount': tax.include_base_amount,
- 'description':tax.description,
- 'company_id': company_id,
- 'type_tax_use': tax.type_tax_use,
- 'price_include': tax.price_include
- }
- new_tax = obj_acc_tax.create(cr, uid, vals_tax)
- tax_template_to_tax[tax.id] = new_tax
- #as the accounts have not been created yet, we have to wait before filling these fields
- todo_dict[new_tax] = {
- 'account_collected_id': tax.account_collected_id and tax.account_collected_id.id or False,
- 'account_paid_id': tax.account_paid_id and tax.account_paid_id.id or False,
- }
- tax_template_ref[tax.id] = new_tax
- #deactivate the parent_store functionnality on account_account for rapidity purpose
- ctx = context and context.copy() or {}
- ctx['defer_parent_store_computation'] = True
-
- children_acc_template = obj_acc_template.search(cr, uid, [('parent_id','child_of',[obj_acc_root.id]),('nocreate','!=',True)])
- children_acc_template.sort()
- for account_template in obj_acc_template.browse(cr, uid, children_acc_template, context=context):
- tax_ids = []
- for tax in account_template.tax_ids:
- tax_ids.append(tax_template_ref[tax.id])
- #create the account_account
-
- dig = obj_multi.code_digits
- code_main = account_template.code and len(account_template.code) or 0
- code_acc = account_template.code or ''
- if code_main>0 and code_main<=dig and account_template.type != 'view':
- code_acc=str(code_acc) + (str('0'*(dig-code_main)))
- vals={
- 'name': (obj_acc_root.id == account_template.id) and obj_multi.company_id.name or account_template.name,
- 'currency_id': account_template.currency_id and account_template.currency_id.id or False,
- 'code': code_acc,
- 'type': account_template.type,
- 'user_type': account_template.user_type and account_template.user_type.id or False,
- 'reconcile': account_template.reconcile,
- 'shortcut': account_template.shortcut,
- 'note': account_template.note,
- 'parent_id': account_template.parent_id and ((account_template.parent_id.id in acc_template_ref) and acc_template_ref[account_template.parent_id.id]) or False,
- 'tax_ids': [(6,0,tax_ids)],
- 'company_id': company_id,
- }
- new_account = obj_acc.create(cr, uid, vals, context=ctx)
- acc_template_ref[account_template.id] = new_account
-
-
- #reactivate the parent_store functionnality on account_account
- obj_acc._parent_store_compute(cr)
-
- for key,value in todo_dict.items():
- if value['account_collected_id'] or value['account_paid_id']:
- obj_acc_tax.write(cr, uid, [key], {
- 'account_collected_id': acc_template_ref.get(value['account_collected_id'], False),
- 'account_paid_id': acc_template_ref.get(value['account_paid_id'], False),
- })
-
- # Creating Journals
- data_id = obj_data.search(cr, uid, [('model','=','account.journal.view'), ('name','=','account_sp_journal_view')])
- data = obj_data.browse(cr, uid, data_id[0], context=context)
- view_id = data.res_id
-
#Sales Journal
- analytical_sale_ids = analytic_journal_obj.search(cr,uid,[('type','=','sale')])
+ analytical_sale_ids = analytic_journal_obj.search(cr, uid, [('type','=','sale')], context=context)
analytical_journal_sale = analytical_sale_ids and analytical_sale_ids[0] or False
vals_journal = {
'analytic_journal_id': analytical_journal_sale,
}
- if obj_multi.chart_template_id.property_account_receivable:
- vals_journal['default_credit_account_id'] = acc_template_ref[obj_multi.chart_template_id.property_account_income_categ.id]
- vals_journal['default_debit_account_id'] = acc_template_ref[obj_multi.chart_template_id.property_account_income_categ.id]
-
- obj_journal.create(cr,uid,vals_journal)
+ if chart_template_id.property_account_receivable:
+ vals_journal.update({
+ 'default_credit_account_id': income_acc_id,
+ 'default_debit_account_id': income_acc_id
+ })
+ obj_journal.create(cr, uid, vals_journal, context=context)
# Purchase Journal
- analytical_purchase_ids = analytic_journal_obj.search(cr,uid,[('type','=','purchase')])
+ analytical_purchase_ids = analytic_journal_obj.search(cr,uid,[('type','=','purchase')], context=context)
analytical_journal_purchase = analytical_purchase_ids and analytical_purchase_ids[0] or False
vals_journal = {
'type': 'purchase',
'code': _('EXJ'),
'view_id': view_id,
- 'company_id': company_id,
+ 'company_id': company_id,
'analytic_journal_id': analytical_journal_purchase,
}
- if obj_multi.chart_template_id.property_account_payable:
- vals_journal['default_credit_account_id'] = acc_template_ref[obj_multi.chart_template_id.property_account_expense_categ.id]
- vals_journal['default_debit_account_id'] = acc_template_ref[obj_multi.chart_template_id.property_account_expense_categ.id]
- obj_journal.create(cr,uid,vals_journal)
+ if chart_template_id.property_account_payable:
+ vals_journal.update({
+ 'default_credit_account_id': expense_acc_id,
+ 'default_debit_account_id': expense_acc_id
+ })
+ obj_journal.create(cr, uid, vals_journal, context=context)
# Creating Journals Sales Refund and Purchase Refund
- data_id = obj_data.search(cr, uid, [('model', '=', 'account.journal.view'), ('name', '=', 'account_sp_refund_journal_view')], context=context)
- data = obj_data.browse(cr, uid, data_id[0], context=context)
- view_id = data.res_id
+ data = obj_data.get_object_reference(cr, uid, 'account', 'account_sp_refund_journal_view')
+ view_id = data and data[1] or False
#Sales Refund Journal
vals_journal = {
'analytic_journal_id': analytical_journal_sale,
'company_id': company_id
}
-
- if obj_multi.chart_template_id.property_account_receivable:
- vals_journal['default_credit_account_id'] = acc_template_ref[obj_multi.chart_template_id.property_account_income_categ.id]
- vals_journal['default_debit_account_id'] = acc_template_ref[obj_multi.chart_template_id.property_account_income_categ.id]
-
+ if chart_template_id.property_account_receivable:
+ vals_journal.update({
+ 'default_credit_account_id': income_acc_id,
+ 'default_debit_account_id': income_acc_id
+ })
obj_journal.create(cr, uid, vals_journal, context=context)
# Purchase Refund Journal
'analytic_journal_id': analytical_journal_purchase,
'company_id': company_id
}
-
- if obj_multi.chart_template_id.property_account_payable:
- vals_journal['default_credit_account_id'] = acc_template_ref[obj_multi.chart_template_id.property_account_expense_categ.id]
- vals_journal['default_debit_account_id'] = acc_template_ref[obj_multi.chart_template_id.property_account_expense_categ.id]
-
+ if chart_template_id.property_account_payable:
+ vals_journal.update({
+ 'default_credit_account_id': expense_acc_id,
+ 'default_debit_account_id': expense_acc_id
+ })
obj_journal.create(cr, uid, vals_journal, context=context)
# Miscellaneous Journal
- data_id = obj_data.search(cr, uid, [('model','=','account.journal.view'), ('name','=','account_journal_view')])
- data = obj_data.browse(cr, uid, data_id[0], context=context)
- view_id = data.res_id
+ data = obj_data.get_object_reference(cr, uid, 'account', 'account_journal_view')
+ view_id = data and data[1] or False
analytical_miscellaneous_ids = analytic_journal_obj.search(cr, uid, [('type', '=', 'situation')], context=context)
- analytical_journal_miscellaneous = analytical_miscellaneous_ids and analytical_miscellaneous_ids[0] or False
vals_journal = {
'name': _('Miscellaneous Journal'),
'type': 'general',
'code': _('MISC'),
'view_id': view_id,
- 'analytic_journal_id': analytical_journal_miscellaneous,
+ 'analytic_journal_id': analytical_miscellaneous_ids and analytical_miscellaneous_ids[0] or False,
'company_id': company_id
}
-
obj_journal.create(cr, uid, vals_journal, context=context)
# Opening Entries Journal
- if obj_multi.chart_template_id.property_account_income_opening and obj_multi.chart_template_id.property_account_expense_opening:
+ if chart_template_id.property_account_income_opening and chart_template_id.property_account_expense_opening:
vals_journal = {
'name': _('Opening Entries Journal'),
'type': 'situation',
'view_id': view_id,
'company_id': company_id,
'centralisation': True,
- 'default_credit_account_id': acc_template_ref[obj_multi.chart_template_id.property_account_income_opening.id],
- 'default_debit_account_id': acc_template_ref[obj_multi.chart_template_id.property_account_expense_opening.id]
+ 'default_credit_account_id': credit_acc_id,
+ 'default_debit_account_id': debit_acc_id
}
obj_journal.create(cr, uid, vals_journal, context=context)
+ return True
- # Bank Journals
- data_id = obj_data.search(cr, uid, [('model','=','account.journal.view'), ('name','=','account_journal_bank_view')])
- data = obj_data.browse(cr, uid, data_id[0], context=context)
- view_id_cash = data.res_id
-
- data_id = obj_data.search(cr, uid, [('model','=','account.journal.view'), ('name','=','account_journal_bank_view_multi')])
- data = obj_data.browse(cr, uid, data_id[0], context=context)
- view_id_cur = data.res_id
- ref_acc_bank = obj_multi.chart_template_id.bank_account_view_id
-
- current_num = 1
- valid = True
- for line in obj_multi.bank_accounts_id:
- #create the account_account for this bank journal
- tmp = line.acc_name
- dig = obj_multi.code_digits
- if not ref_acc_bank.code:
- raise osv.except_osv(_('Configuration Error !'), _('The bank account defined on the selected chart of account hasn\'t a code.'))
- while True:
- new_code = str(ref_acc_bank.code.ljust(dig-len(str(current_num)), '0')) + str(current_num)
- ids = obj_acc.search(cr, uid, [('code', '=', new_code), ('company_id', '=', company_id)])
- if not ids:
- break
- else:
- current_num += 1
- vals = {
- 'name': tmp,
- 'currency_id': line.currency_id and line.currency_id.id or False,
- 'code': new_code,
- 'type': 'liquidity',
- 'user_type': account_template.user_type and account_template.user_type.id or False,
- 'reconcile': True,
- 'parent_id': acc_template_ref[ref_acc_bank.id] or False,
- 'company_id': company_id,
- }
- acc_cash_id = obj_acc.create(cr,uid,vals)
-
- #create the bank journal
- vals_journal = {
- 'name': vals['name'],
- 'code': _('BNK') + str(current_num),
- 'type': line.account_type == 'cash' and 'cash' or 'bank',
- 'company_id': company_id,
- 'analytic_journal_id': False,
- 'currency_id': False,
- }
- if line.currency_id:
- vals_journal['view_id'] = view_id_cur
- vals_journal['currency'] = line.currency_id.id
- else:
- vals_journal['view_id'] = view_id_cash
- vals_journal['default_credit_account_id'] = acc_cash_id
- vals_journal['default_debit_account_id'] = acc_cash_id
- obj_journal.create(cr, uid, vals_journal)
- current_num += 1
- valid = True
-
- #create the properties
+ def generate_properties(self, cr, uid, chart_template_id, acc_template_ref, company_id, context=None):
+ """
+ This method used for creating properties.
+ @param cr: A database cursor.
+ @param uid: ID of the user currently logged in.
+ @param chart_temp_id: Chart Template Id.
+ @param acc_template_ref: Account templates reference.
+ @param company_id: company_id selected from wizard.multi.charts.accounts.
+ """
+
property_obj = self.pool.get('ir.property')
- fields_obj = self.pool.get('ir.model.fields')
-
+ field_obj = self.pool.get('ir.model.fields')
todo_list = [
('property_account_receivable','res.partner','account.account'),
('property_account_payable','res.partner','account.account'),
('property_account_income','product.template','account.account'),
('property_reserve_and_surplus_account','res.company','account.account')
]
+ template_id = self.pool.get('account.chart.template').browse(cr, uid, chart_template_id, context=context)
for record in todo_list:
- r = []
- r = property_obj.search(cr, uid, [('name','=', record[0] ),('company_id','=',company_id)])
- account = getattr(obj_multi.chart_template_id, record[0])
- field = fields_obj.search(cr, uid, [('name','=',record[0]),('model','=',record[1]),('relation','=',record[2])])
+ rec_list = []
+ rec_list = property_obj.search(cr, uid, [('name','=', record[0]),('company_id', '=', company_id)], context=context)
+ account = getattr(template_id, record[0])
+ field = field_obj.search(cr, uid, [('name', '=', record[0]),('model', '=', record[1]),('relation', '=', record[2])], context=context)
vals = {
'name': record[0],
'company_id': company_id,
'value': account and 'account.account,' + str(acc_template_ref[account.id]) or False,
}
- if r:
+ if rec_list:
#the property exist: modify it
- property_obj.write(cr, uid, r, vals)
+ property_obj.write(cr, uid, rec_list, vals, context=context)
else:
#create the property
- property_obj.create(cr, uid, vals)
+ property_obj.create(cr, uid, vals, context=context)
+
+ return True
+
+ def _install_template(self, cr, uid, ids, template_id, company_id, code_digits=None ,tax_data={}, context=None):
+ print 'install', template_id
+ template = self.pool.get('account.chart.template').browse(cr, uid, template_id, context=context)
+# TOFIX: Improve relation between COA template and account template
+# If we have COA template struct like :
+# COA A
+# COA B
+#
+# and account template struct like:
+# A0
+# - A01
+# - B02
+# A1
+# - B11
+# - B12
+# where prefix A is intended to load for COA A, and B for B
+#
+# now I am processing COA B
+# B02 is child of A0 so children_acc_template = obj_acc_template.search(cr, uid, [('parent_id','child_of',[obj_acc_root.id]),('nocreate','!=',True)])
+# will search all acc templates of A and B
+
+# if template.parent_id:
+# self._install_template(cr, uid, ids, template.parent_id.id, company_id, code_digits=code_digits, context=context)
+ return self._load_template(cr, uid, ids, template_id, company_id, code_digits=code_digits, tax_data=tax_data, context=context)
+
+ def _load_template(self, cr, uid, ids, template_id, company_id, code_digits=None, tax_data={}, context=None):
+ template = self.pool.get('account.chart.template').browse(cr, uid, template_id, context=context)
+ obj_tax_code_template = self.pool.get('account.tax.code.template')
+ obj_tax_code = self.pool.get('account.tax.code')
+ obj_tax_temp = self.pool.get('account.tax.template')
+ obj_acc_template = self.pool.get('account.account.template')
+ obj_fiscal_position_template = self.pool.get('account.fiscal.position.template')
+ ir_values_obj = self.pool.get('ir.values')
- fp_ids = obj_fiscal_position_template.search(cr, uid, [('chart_template_id', '=', obj_multi.chart_template_id.id)])
+ # create tax templates and real taxes from purchase_tax_rate,sale_tax_rate fields
+ if not template.set_tax_complete:
+ tax_dict = {'sale': tax_data['sale'], 'purchase': tax_data['purchase']}
+ for tax_type, value in tax_dict.items():
+ tax_name = tax_type == 'sale' and 'TAX Received' or 'TAX Paid'
+ if value > 0.0:
+ tax_string = _('TAX %s%%') % (value)
+ new_tax_code_temp = obj_tax_code_template.create(cr, uid, {'name': tax_string, 'code': tax_string}, context=context)
+ new_paid_tax_code_temp = obj_tax_code_template.create(cr, uid, {'name': _('%s %s%%') % (tax_name, value), 'code': _('%s %s%%') % (tax_name, value)}, context=context)
+ sales_tax_temp = obj_tax_temp.create(cr, uid, {
+ 'name': tax_string,
+ 'description': tax_string,
+ 'amount': value/100,
+ 'base_code_id': new_tax_code_temp,
+ 'tax_code_id': new_paid_tax_code_temp,
+ 'ref_base_code_id': new_tax_code_temp,
+ 'ref_tax_code_id': new_paid_tax_code_temp,
+ 'type_tax_use': tax_type,
+ 'installable': True,
+ 'type': 'percent',
+ 'sequence': 0,
+ 'chart_template_id': template_id or False,
+ }, context=context)
- if fp_ids:
- obj_tax_fp = self.pool.get('account.fiscal.position.tax')
- obj_ac_fp = self.pool.get('account.fiscal.position.account')
- for position in obj_fiscal_position_template.browse(cr, uid, fp_ids, context=context):
+ # create all the tax code [TOCHECK: finds children of tax_code_root_id and processes]
+ tax_code_template_ref = {}
+ tax_code_root_id = template.tax_code_root_id.id
+ children_tax_code_template = obj_tax_code_template.search(cr, uid, [('parent_id','child_of',[tax_code_root_id])], order='id')
+ company_name = self.pool.get('res.company').browse(cr, uid, company_id, context=context)
+ for tax_code_template in obj_tax_code_template.browse(cr, uid, children_tax_code_template, context=context):
+ vals = {
+ 'name': (tax_code_root_id == tax_code_template.id) and company_name or tax_code_template.name,
+ 'code': tax_code_template.code,
+ 'info': tax_code_template.info,
+ 'parent_id': tax_code_template.parent_id and ((tax_code_template.parent_id.id in tax_code_template_ref) and tax_code_template_ref[tax_code_template.parent_id.id]) or False,
+ 'company_id': company_id,
+ 'sign': tax_code_template.sign,
+ }
+ new_tax_code = obj_tax_code.create(cr, uid, vals)
+ #recording the new tax code to do the mapping
+ tax_code_template_ref[tax_code_template.id] = new_tax_code
- vals_fp = {
- 'company_id': company_id,
- 'name': position.name,
- }
- new_fp = obj_fiscal_position.create(cr, uid, vals_fp)
-
- for tax in position.tax_ids:
- vals_tax = {
- 'tax_src_id': tax_template_ref[tax.tax_src_id.id],
- 'tax_dest_id': tax.tax_dest_id and tax_template_ref[tax.tax_dest_id.id] or False,
- 'position_id': new_fp,
- }
- obj_tax_fp.create(cr, uid, vals_tax)
-
- for acc in position.account_ids:
- vals_acc = {
- 'account_src_id': acc_template_ref[acc.account_src_id.id],
- 'account_dest_id': acc_template_ref[acc.account_dest_id.id],
- 'position_id': new_fp,
- }
- obj_ac_fp.create(cr, uid, vals_acc)
-
- if obj_multi.sale_tax:
- ir_values_obj.set(cr, uid, key='default', key2=False, name="taxes_id", company=obj_multi.company_id.id,
- models =[('product.product',False)], value=[tax_template_to_tax[obj_multi.sale_tax.id]])
- if obj_multi.purchase_tax:
- ir_values_obj.set(cr, uid, key='default', key2=False, name="supplier_taxes_id", company=obj_multi.company_id.id,
- models =[('product.product',False)], value=[tax_template_to_tax[obj_multi.purchase_tax.id]])
+ # Generate taxes from templates.
+ tax_template_to_tax = {}
+ tax_templates = [x for x in template.tax_template_ids if x.installable]
+ taxes_ref = obj_tax_temp.generate_tax(cr, uid, tax_templates, tax_code_template_ref, company_id, context=context)
+
+ # Generating Accounts from templates.
+ acc_template_ref = obj_acc_template.generate_account(cr, uid, template.account_root_id.id, taxes_ref['tax_template_ref'], code_digits, company_id, context=context)
+
+ # writing account values on tax after creation of accounts
+ for key,value in taxes_ref['account_dict'].items():
+ if value['account_collected_id'] or value['account_paid_id']:
+ obj_acc_tax.write(cr, uid, [key], {
+ 'account_collected_id': acc_template_ref.get(value['account_collected_id'], False),
+ 'account_paid_id': acc_template_ref.get(value['account_paid_id'], False),
+ })
+
+ # Create Jourals
+ #TODO: pass template_id
+ self.generate_journals(cr, uid, template, acc_template_ref, company_id, context)
+
+ # Create Bank Journals (Can be done in the generate_journals function)
+ #TODO: Create a new function
+
+ # generate properties function
+ self.generate_properties(cr, uid, template_id, acc_template_ref, company_id, context=context)
+
+ # Generate Fiscal Position , Fiscal Position Accounts and Fiscal Position Taxes from templates
+ obj_fiscal_position_template.generate_fiscal_position(cr, uid, template_id, taxes_ref['tax_template_ref'], acc_template_ref, company_id, context=context)
+
+ # write values of default taxes for product
+ if tax_data:
+ if tax_data['sale_tax'] and taxes_ref['taxes_id']:
+ ir_values_obj.set(cr, uid, key='default', key2=False, name="taxes_id", company=company_id,
+ models =[('product.product',False)], value=[taxes_ref['taxes_id'][tax_data['sale_tax']]])
+ if tax_data['purchase_tax'] and taxes_ref['taxes_id']:
+ ir_values_obj.set(cr, uid, key='default', key2=False, name="supplier_taxes_id", company=company_id,
+ models =[('product.product',False)], value=[taxes_ref['taxes_id'][tax_data['purchase_tax']]])
+ return True
+
+ def execute(self, cr, uid, ids, context=None):
+ obj_tax_temp = self.pool.get('account.tax.template')
+ obj_multi = self.browse(cr, uid, ids[0])
+ company_id = obj_multi.company_id.id
+ tax_data = {
+ 'sale': obj_multi.sale_tax_rate,
+ 'purchase': obj_multi.purchase_tax_rate,
+ 'sale_tax': obj_multi.complete_tax and obj_multi.sale_tax.id or False,
+ 'purchase_tax': obj_multi.complete_tax and obj_multi.purchase_tax.id or False,
+ }
+ self._install_template(cr, uid, ids, obj_multi.chart_template_id.id, company_id, code_digits=obj_multi.code_digits, tax_data=tax_data, context=context)
+ return {}
wizard_multi_charts_accounts()