~openerp-spain-team/openerp-spain/6.0-git

« back to all changes in this revision

Viewing changes to l10n_es_extras/l10n_ES_partner_seq/wizard/create_accounts.py

  • Committer: Borja L.S.
  • Date: 2010-10-18 10:04:25 UTC
  • Revision ID: git-v1:271c47a993616dbba60585d48b8b98d603199d93
[REF] *: Refactorización para portar a 6.0 - Paso 1.

- Se han renombrado los módulos para usar la nomenclatura propuesta
  por OpenERP: l10n_es para el módulo base de localización (plan de 
  cuentas), l10n_es_* para el resto de módulos.

- Se eliminan los módulos extra_addons/* que deberían moverse a 
  los extra-addons genéricos (no son específicos de España).

- Se renombran los __terp__.py por __openerp__.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- encoding: utf-8 -*-
2
 
##############################################################################
3
 
#
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>
7
 
#    $Id$
8
 
#
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.
13
 
#
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.
18
 
#
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/>.
21
 
#
22
 
##############################################################################
23
 
 
24
 
import wizard
25
 
import netsvc
26
 
import pooler
27
 
import string
28
 
 
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"/>
34
 
    <newline/>
35
 
    <label string="Do you want to create accounts for the selected partners?" colspan="4"/>
36
 
</form>"""
37
 
 
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.'},
42
 
}
43
 
 
44
 
def strip0d(cadena):
45
 
    # Elimina los ceros de la derecha en una cadena de texto de dígitos
46
 
    return str(int(cadena[::-1]))[::-1]
47
 
 
48
 
def strip0i(cadena):
49
 
    # Elimina los ceros de la izquierda en una cadena de texto de dígitos
50
 
    return str(int(cadena))
51
 
 
52
 
 
53
 
class create_accounts(wizard.interface):
54
 
 
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)])
64
 
        number_digits = False
65
 
        if property_ids:
66
 
            value = property_obj.read(cr, uid, property_ids[0], ['value'], context)['value']
67
 
            if 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}
73
 
       
74
 
 
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')
80
 
 
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:
87
 
            return
88
 
 
89
 
        def link_account(ref, parent_code, acc_type, acc_user_type, acc_property):
90
 
            """
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):
97
 
                vals = {
98
 
                'name': partner.name,
99
 
                'code': acc_code,
100
 
                'type': acc_type,
101
 
                'user_type': acc_user_type,
102
 
                'shortcut': strip0d(acc_code[:4]) + "." + strip0i(acc_code[-5:]),
103
 
                'reconcile': True,
104
 
                }
105
 
                args = [('code', '=', parent_code)]
106
 
                parent_acc_ids = account_obj.search(cr, uid, args) # Busca id de la subcuenta padre
107
 
                if parent_acc_ids:
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
112
 
 
113
 
        for partner in partner_obj.browse(cr, uid, data['ids'], context=context):
114
 
            if not partner.customer and not partner.supplier:
115
 
                continue
116
 
            if not partner.ref or not partner.ref.strip():
117
 
                ref = sequence_obj.get(cr, uid, 'res.partner')
118
 
                vals = {'ref': ref}
119
 
                partner_obj.write(cr, uid, [partner.id], vals)
120
 
            else:
121
 
                 ref = partner.ref
122
 
 
123
 
            ref = ''.join([i for i in ref if i in string.digits]) # Solo nos interesa los dígitos del código
124
 
            if (ref.isdigit()):
125
 
                if partner.customer:
126
 
                    len_ref = data['form']['number_digits'] - len(data['form']['parent_receivable']) # longitud del código que aprovechamos
127
 
                    if len_ref > 0:
128
 
                        link_account(ref[-len_ref:].zfill(len_ref), data['form']['parent_receivable'], 'receivable', acc_user_type_rec, 'property_account_receivable')
129
 
 
130
 
                if partner.supplier:
131
 
                    len_ref = data['form']['number_digits'] - len(data['form']['parent_payable']) # longitud del código que aprovechamos
132
 
                    if len_ref > 0:
133
 
                        link_account(ref[-len_ref:].zfill(len_ref), data['form']['parent_payable'], 'payable', acc_user_type_pay, 'property_account_payable')
134
 
        return {}
135
 
 
136
 
 
137
 
    states = {
138
 
        'init' : {
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') ]}
144
 
        },
145
 
        'create' : {
146
 
            'actions' : [],
147
 
            'result' : {'type' : 'action',
148
 
                    'action' : _createAccounts,
149
 
                    'state' : 'end'}
150
 
        },
151
 
    }
152
 
create_accounts("l10n_ES_partner_seq.create_accounts")