1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (c) 2009 Zikzakmedia S.L. (http://zikzakmedia.com) All Rights Reserved.
6
# Jordi Esteve <jesteve@zikzakmedia.com>
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation, either version 3 of the License, or
12
# (at your option) any later version.
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
19
# You should have received a copy of the GNU General Public License
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
##############################################################################
29
accounts_create_form = """<?xml version="1.0" encoding="utf-8"?>
30
<form string="Create accounts">
31
<field name="parent_receivable"/>
32
<field name="parent_payable"/>
33
<field name="number_digits"/>
35
<label string="Do you want to create accounts for the selected partners?" colspan="4"/>
38
accounts_create_fields = {
39
'parent_receivable': {'string':'Parent Receivable Account', 'type':'char', 'size': 16, 'required':'True', 'help':'Parent for the receivable account, like 4300 for clients, 4400 for debtors, ... It also will be used as the code prefix of the created account.\nTip: Save the most used parent account as the default value.'},
40
'parent_payable': {'string':'Parent Payable Account', 'type':'char', 'size': 16, 'required':'True', 'help':'Parent for the payable account, like 4000 for suppliers, 4100 for creditors, ... It also will be used as the code prefix of the created account.\nTip: Save the most used parent account as the default value.'},
41
'number_digits': {'string':'Number of digits', 'type':'integer', 'required':'True', 'help':'Number of digits of the account codes.'},
45
# Elimina los ceros de la derecha en una cadena de texto de dígitos
46
return str(int(cadena[::-1]))[::-1]
49
# Elimina los ceros de la izquierda en una cadena de texto de dígitos
50
return str(int(cadena))
53
class create_accounts(wizard.interface):
55
def _data_load(self, cr, uid, data, context):
56
""" Para saber el número de dígitos de las cuentas de la compañía
57
miramos la cuenta a cobrar por defecto de la compañía a la que pertenece el usuario"""
58
pool = pooler.get_pool(cr.dbname)
59
users_obj = pool.get('res.users')
60
property_obj = pool.get('ir.property')
61
account_obj = pool.get('account.account')
62
user = users_obj.browse(cr, uid, uid, context)
63
property_ids = property_obj.search(cr, uid, [('name','=','property_account_receivable' ), ('company_id','=',user.company_id.id), ('res_id','=',False), ('value','!=',False)])
66
value = property_obj.read(cr, uid, property_ids[0], ['value'], context)['value']
68
# value is a reference field, tipically content is: account.account,519, where 519 is the id of the 430000 account
69
account_id = int(value.partition(',')[2])
70
code = account_obj.read(cr, uid, account_id, ['code'], context)['code']
71
number_digits = len(code)
72
return {'number_digits': number_digits}
75
def _createAccounts(self, cr, uid, data, context):
76
pool = pooler.get_pool(cr.dbname)
77
partner_obj = pool.get('res.partner')
78
account_obj = pool.get('account.account')
79
sequence_obj = pool.get('ir.sequence')
81
account_type_obj = pool.get('account.account.type')
82
ids = account_type_obj.search(cr, uid, [('code', '=', 'terceros - rec')]) # Busca tipo cuenta de usuario rec
83
acc_user_type_rec = ids and ids[0]
84
ids = account_type_obj.search(cr, uid, [('code', '=', 'terceros - pay')]) # Busca tipo cuenta de usuario pay
85
acc_user_type_pay = ids and ids[0]
86
if not acc_user_type_rec and not acc_user_type_pay:
89
def link_account(ref, parent_code, acc_type, acc_user_type, acc_property):
91
parent_code: Código del padre (Ej.: 4300)
92
type: Puede ser 'payable' o 'receivable'
93
acc_property: 'property_account_receivable' o 'property_account_payable'"""
94
acc_code = parent_code + ref # acc_code es el nuevo código de subcuenta
95
args = [('code', '=', acc_code)]
96
if not account_obj.search(cr, uid, args):
101
'user_type': acc_user_type,
102
'shortcut': strip0d(acc_code[:4]) + "." + strip0i(acc_code[-5:]),
105
args = [('code', '=', parent_code)]
106
parent_acc_ids = account_obj.search(cr, uid, args) # Busca id de la subcuenta padre
108
vals['parent_id'] = parent_acc_ids[0]
109
acc_id = account_obj.create(cr, uid, vals)
110
vals = {acc_property: acc_id}
111
partner_obj.write(cr, uid, [partner.id], vals) # Asocia la nueva subcuenta con el partner
113
for partner in partner_obj.browse(cr, uid, data['ids'], context=context):
114
if not partner.customer and not partner.supplier:
116
if not partner.ref or not partner.ref.strip():
117
ref = sequence_obj.get(cr, uid, 'res.partner')
119
partner_obj.write(cr, uid, [partner.id], vals)
123
ref = ''.join([i for i in ref if i in string.digits]) # Solo nos interesa los dígitos del código
126
len_ref = data['form']['number_digits'] - len(data['form']['parent_receivable']) # longitud del código que aprovechamos
128
link_account(ref[-len_ref:].zfill(len_ref), data['form']['parent_receivable'], 'receivable', acc_user_type_rec, 'property_account_receivable')
131
len_ref = data['form']['number_digits'] - len(data['form']['parent_payable']) # longitud del código que aprovechamos
133
link_account(ref[-len_ref:].zfill(len_ref), data['form']['parent_payable'], 'payable', acc_user_type_pay, 'property_account_payable')
139
'actions' : [_data_load],
140
'result' : {'type' : 'form',
141
'arch' : accounts_create_form,
142
'fields' : accounts_create_fields,
143
'state' : [('end', 'Cancel'),('create', 'Create accounts') ]}
147
'result' : {'type' : 'action',
148
'action' : _createAccounts,
152
create_accounts("l10n_ES_partner_seq.create_accounts")