~sergio-incaser/openerp-spain/openerp-spain

« back to all changes in this revision

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

  • Committer: Jordi Esteve
  • Date: 2009-12-14 17:53:50 UTC
  • mfrom: (81.1.90 lp-openerp-spain-5.0)
  • Revision ID: jesteve@zikzakmedia.com-20091214175350-6vzzt3avtsof25a2
Recuperación del estado actual del repositorio de localización española de OpenERP: Aplicación de todos los merges pendientes desde la versión 81 (15-11-2008) hasta la actual (02-12-2009)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# -*- coding: utf-8 -*-
3
 
 
 
1
# -*- encoding: utf-8 -*-
4
2
##############################################################################
5
3
#
6
 
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
7
 
#
8
 
# $Id: make_picking.py 1070 2005-07-29 12:41:24Z nicoe $
9
 
#
10
 
# WARNING: This program as such is intended to be used by professional
11
 
# programmers who take the whole responsability of assessing all potential
12
 
# consequences resulting from its eventual inadequacies and bugs
13
 
# End users who are looking for a ready-to-use solution with commercial
14
 
# garantees and support are strongly adviced to contract a Free Software
15
 
# Service Company
16
 
#
17
 
# This program is Free Software; you can redistribute it and/or
18
 
# modify it under the terms of the GNU General Public License
19
 
# as published by the Free Software Foundation; either version 2
20
 
# of the License, or (at your option) any later version.
21
 
#
22
 
# This program is distributed in the hope that it will be useful,
23
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 
# GNU General Public License for more details.
26
 
#
27
 
# You should have received a copy of the GNU General Public License
28
 
# along with this program; if not, write to the Free Software
29
 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
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/>.
30
21
#
31
22
##############################################################################
32
23
 
33
24
import wizard
34
25
import netsvc
35
26
import pooler
 
27
import string
36
28
 
37
 
accounts_create_form = """<?xml version="1.0"?>
 
29
accounts_create_form = """<?xml version="1.0" encoding="utf-8"?>
38
30
<form string="Create accounts">
39
 
        <separator string="Do you want to create accounts for the selected partners" />
 
31
    <label string="Do you want to create accounts for the selected partners?" />
40
32
</form>"""
41
33
 
42
34
accounts_create_fields = {}
43
35
 
44
36
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
 
        
 
37
    # Elimina los ceros de la derecha en una cadena de texto de dígitos
 
38
    return str(int(cadena[::-1]))[::-1]
 
39
 
48
40
def strip0i(cadena):
49
 
        # Elimina los ceros de la izquierda en una cadena de texto de dígitos
50
 
        return str(int(cadena))
51
 
         
 
41
    # Elimina los ceros de la izquierda en una cadena de texto de dígitos
 
42
    return str(int(cadena))
 
43
 
52
44
 
53
45
def _createAccounts(self, cr, uid, data, context):
54
 
        partner_obj = pooler.get_pool(cr.dbname).get('res.partner')
55
 
        account_obj = pooler.get_pool(cr.dbname).get('account.account')
56
 
        sequence_obj = pooler.get_pool(cr.dbname).get('ir.sequence')
57
 
        
58
 
        def link_account(ref,parent_code, acc_type, acc_property):
59
 
                # parent_code: Código del padre (Ej.: 4300)
60
 
                # type: Puede ser 'payable' o 'receivable'
61
 
                # acc_property: 'property_account_receivable' o 'property_account_payable'
62
 
                acc_code = parent_code + ref[2:]  # acc_code es el nuevo código de subcuenta      
63
 
                args = [('code', '=', acc_code)]
64
 
                if not account_obj.search(cr, uid, args):
65
 
                        args = [('code', '=', parent_code)]
66
 
                        parent_acc_ids = account_obj.search(cr, uid, args) # Busca id de la subcuenta padre
67
 
                        vals = {
68
 
                        'name': partner.name,
69
 
                        'code': acc_code,
70
 
                        'type': acc_type,
71
 
                        'parent_id': [(6,0,parent_acc_ids)], # acc_ids es un diccionario
72
 
                        'sign': 1,
73
 
                        'close_method': 'unreconciled',
74
 
                        'shortcut': strip0d(acc_code[:4]) + "." + strip0i(acc_code[-5:]),
75
 
                        }
76
 
                        acc_id = account_obj.create(cr, uid, vals)                 
77
 
                        vals = {acc_property: acc_id}
78
 
                        partner_obj.write(cr, uid, [partner.id], vals)   # Asocia la nueva subcuenta con el partner
79
 
           
80
 
        for partner in partner_obj.browse(cr, uid, data['ids'], context=context):
81
 
                if not partner.ref or not partner.ref.strip():
82
 
                        ref = sequence_obj.get(cr, uid, 'res.partner')
83
 
                        vals = {'ref': ref}
84
 
                        partner_obj.write(cr, uid, [partner.id], vals)
85
 
                else:
86
 
                         ref =  partner.ref  
87
 
                                                
88
 
                if (len(ref) == 7) and (ref[2:].isdigit()):                     
89
 
                        for category in partner.category_id:
90
 
                                if category.name.lower() == 'cliente':
91
 
                                        link_account(ref,'4300', 'receivable', 'property_account_receivable')
92
 
 
93
 
                                if category.name.lower() == 'proveedor':
94
 
                                        link_account(ref,'4000', 'payable', 'property_account_payable')
95
 
        return {}
 
46
    partner_obj = pooler.get_pool(cr.dbname).get('res.partner')
 
47
    account_obj = pooler.get_pool(cr.dbname).get('account.account')
 
48
    sequence_obj = pooler.get_pool(cr.dbname).get('ir.sequence')
 
49
 
 
50
    account_type_obj = pooler.get_pool(cr.dbname).get('account.account.type')
 
51
    ids = account_type_obj.search(cr, uid, [('code', '=', 'terceros - rec')]) # Busca tipo cuenta de usuario rec
 
52
    acc_user_type_rec = ids and ids[0]
 
53
    ids = account_type_obj.search(cr, uid, [('code', '=', 'terceros - pay')]) # Busca tipo cuenta de usuario pay
 
54
    acc_user_type_pay = ids and ids[0]
 
55
    if not acc_user_type_rec and not acc_user_type_pay:
 
56
        return
 
57
 
 
58
    def link_account(ref, parent_code, acc_type, acc_user_type, acc_property):
 
59
        """
 
60
        parent_code: Código del padre (Ej.: 4300)
 
61
        type: Puede ser 'payable' o 'receivable'
 
62
        acc_property: 'property_account_receivable' o 'property_account_payable'"""
 
63
        acc_code = parent_code + ref  # acc_code es el nuevo código de subcuenta
 
64
        args = [('code', '=', acc_code)]
 
65
        if not account_obj.search(cr, uid, args):
 
66
            vals = {
 
67
            'name': partner.name,
 
68
            'code': acc_code,
 
69
            'type': acc_type,
 
70
            'user_type': acc_user_type,
 
71
            'shortcut': strip0d(acc_code[:4]) + "." + strip0i(acc_code[-5:]),
 
72
            }
 
73
            args = [('code', '=', parent_code)]
 
74
            parent_acc_ids = account_obj.search(cr, uid, args) # Busca id de la subcuenta padre
 
75
            if parent_acc_ids:
 
76
                vals['parent_id'] = parent_acc_ids[0]
 
77
            acc_id = account_obj.create(cr, uid, vals)
 
78
            vals = {acc_property: acc_id}
 
79
            partner_obj.write(cr, uid, [partner.id], vals) # Asocia la nueva subcuenta con el partner
 
80
 
 
81
    for partner in partner_obj.browse(cr, uid, data['ids'], context=context):
 
82
        if not partner.customer and not partner.supplier:
 
83
            continue
 
84
        if not partner.ref or not partner.ref.strip():
 
85
            ref = sequence_obj.get(cr, uid, 'res.partner')
 
86
            vals = {'ref': ref}
 
87
            partner_obj.write(cr, uid, [partner.id], vals)
 
88
        else:
 
89
             ref = partner.ref
 
90
 
 
91
        ref = ''.join([i for i in ref if i in string.digits]) # Solo nos interesa los dígitos del código
 
92
        if (ref.isdigit()):
 
93
            if partner.customer:
 
94
                link_account(ref, '430', 'receivable', acc_user_type_rec, 'property_account_receivable')
 
95
 
 
96
            if partner.supplier:
 
97
                link_account(ref, '400', 'payable', acc_user_type_pay, 'property_account_payable')
 
98
    return {}
96
99
 
97
100
 
98
101
class create_accounts(wizard.interface):
99
 
        states = {
100
 
                'init' : {
101
 
                        'actions' : [],
102
 
                        'result' : {'type' : 'form',
103
 
                                        'arch' : accounts_create_form,
104
 
                                        'fields' : accounts_create_fields,
105
 
                                        'state' : [('end', 'Cancel'),('create', 'Create accounts') ]}
106
 
                },
107
 
                'create' : {
108
 
                        'actions' : [],
109
 
                        'result' : {'type' : 'action',
110
 
                                        'action' : _createAccounts,
111
 
                                        'state' : 'end'}
112
 
                },
113
 
        }
114
 
create_accounts("partner_seq.create_accounts")
 
102
    states = {
 
103
        'init' : {
 
104
            'actions' : [],
 
105
            'result' : {'type' : 'form',
 
106
                    'arch' : accounts_create_form,
 
107
                    'fields' : accounts_create_fields,
 
108
                    'state' : [('end', 'Cancel'),('create', 'Create accounts') ]}
 
109
        },
 
110
        'create' : {
 
111
            'actions' : [],
 
112
            'result' : {'type' : 'action',
 
113
                    'action' : _createAccounts,
 
114
                    'state' : 'end'}
 
115
        },
 
116
    }
 
117
create_accounts("l10n_ES_partner_seq.create_accounts")