← Back to team overview

openerp-community-reviewer team mailing list archive

lp:~omar7r/account-financial-tools/6_1_imp_acccount_admin_tools_importer into lp:account-financial-tools/6.1

 

Omar (Pexego) has proposed merging lp:~omar7r/account-financial-tools/6_1_imp_acccount_admin_tools_importer into lp:account-financial-tools/6.1.

Requested reviews:
  Account Core Editors (account-core-editors)

For more details, see:
https://code.launchpad.net/~omar7r/account-financial-tools/6_1_imp_acccount_admin_tools_importer/+merge/196281

Improvement on account_admin_tools_importer: Allow import account moves with another column, partner reference, for associating imported move to partner
-- 
https://code.launchpad.net/~omar7r/account-financial-tools/6_1_imp_acccount_admin_tools_importer/+merge/196281
Your team Account Core Editors is requested to review the proposed merge of lp:~omar7r/account-financial-tools/6_1_imp_acccount_admin_tools_importer into lp:account-financial-tools/6.1.
=== modified file 'account_admin_tools_importer/account_move_importer.py'
--- account_admin_tools_importer/account_move_importer.py	2013-01-04 14:19:25 +0000
+++ account_admin_tools_importer/account_move_importer.py	2013-11-22 13:37:30 +0000
@@ -3,7 +3,10 @@
 #
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2004-2009 Pexego Sistemas Informáticos. All Rights Reserved
-#    $Id$
+#    Copyright (c) 2012 Zikzakmedia S.L. (http://zikzakmedia.com)
+#                       All Rights Reserved.
+#                       Jordi Esteve <jesteve@xxxxxxxxxxxxxxx>
+#                       Jesús Martín <jmartin@xxxxxxxxxxxxxxx>
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU Affero General Public License as published
@@ -74,16 +77,18 @@
         #
         # Input file
         #
-        'input_file': fields.binary('File', filters="*.csv", required=True),
+        'input_file': fields.binary('File', filters="*.csv", 
+                                        required=True),
         'input_file_name': fields.char('File name', size=256),
         'csv_delimiter': fields.char('Delimiter', size=1, required=True),
         'csv_quotechar': fields.char('Quote', size=1, required=True),
         'csv_decimal_separator': fields.char('Decimal sep.', size=1,
-                                             required=True),
+                                        required=True),
         'csv_thousands_separator': fields.char('Thousands sep.', size=1,
-                                               required=True),
+                                        required=True),
         'csv_code_index': fields.integer('Code field', required=True),
-        'csv_code_regexp': fields.char('Code regexp', size=32, required=True),
+        'csv_code_regexp': fields.char('Code regexp', size=32, 
+                                        required=True),
         'csv_ref_index': fields.integer('Ref field', required=True),
         'csv_ref_regexp': fields.char('Ref regexp', size=32, required=True),
         'csv_debit_index': fields.integer('Debit field', required=True),
@@ -91,7 +96,19 @@
                                         required=True),
         'csv_credit_index': fields.integer('Credit field', required=True),
         'csv_credit_regexp': fields.char('Credit regexp', size=32,
-                                         required=True),
+                                        required=True),
+        'csv_partner_ref_index': fields.integer('Partner Ref field', 
+                                        required=True),
+        'csv_partner_ref_regexp': fields.char('Partner Ref regexp', 
+                                        size=32, required=True),
+        'oerp_partner_ref_field': fields.many2one('ir.model.fields',
+                'OpenERP Partner field', required=True,
+                domain=[
+                    ('model', '=', 'res.partner'),
+                    '|',
+                    ('ttype', '=', 'char'),
+                    ('ttype', '=', 'many2one')
+                ]),
     }
 
     def _get_default_period_id(self, cr, uid, context=None):
@@ -100,6 +117,28 @@
         """
         period_ids = self.pool.get('account.period').find(cr, uid)
         return period_ids and period_ids[0] or False
+        
+    def _get_default_ref_field(self, cr, uid, ids, context=None):
+        """ This method set the default value to ref field in
+            res.partner model for the oerp_partner_ref_field field
+            @param self: The object pointer
+            @param cr: The current row, from the database cursor,
+            @param uid: The current user’s ID for security checks,
+            @param ids: List of registers’ IDs
+            @param context: A standard dictionary for contextual values
+            @return: return default value
+        """
+        if context is None:
+            context = {}
+        model_field_obj = self.pool.get('ir.model.fields')
+        model_field_ids = model_field_obj.search(cr,
+                uid, [
+                    ('model', '=', 'res.partner'),
+                    ('name', '=', 'ref')
+                ], context)
+        default_model_field = model_field_obj.browse(cr, uid, 
+                model_field_ids, context)[0].id
+        return default_model_field
 
     _defaults = {
         'company_id': lambda self, cr, uid, context:
@@ -116,10 +155,13 @@
         'csv_ref_index': lambda *a: 1,
         'csv_debit_index': lambda *a: 2,
         'csv_credit_index': lambda *a: 3,
+        'csv_partner_ref_index': lambda * a: 4,
         'csv_code_regexp': lambda *a: r'^[0-9]+$',
         'csv_ref_regexp': lambda *a: r'^.*$',
         'csv_debit_regexp': lambda *a: r'^[0-9\-\.\,]*$',
         'csv_credit_regexp': lambda *a: r'^[0-9\-\.\,]*$',
+        'csv_partner_ref_regexp': lambda * a: r'^.*$',
+        'oerp_partner_ref_field': _get_default_ref_field,
     }
 
     def _get_accounts_map(self, cr, uid, context=None):
@@ -162,6 +204,9 @@
         """
         accounts_map = self._get_accounts_map(cr, uid, context=context)
         logger = logging.getLogger("account_move_importer")
+        field_obj = self.pool.get('ir.model.fields')
+        partner_obj = self.pool.get('res.partner')
+        reFloat = r'(^[+-]?\d+(?:\.\d+)?(?:[eE][+-]\d+)?$)'
         
         for wiz in self.browse(cr, uid, ids, context=context):
             if not wiz.input_file:
@@ -195,7 +240,7 @@
 
             for record in reader:
                 # Ignore short records
-                if len(record) > wiz.csv_code_index \
+                if wiz.csv_partner_ref_index and len(record) > wiz.csv_code_index \
                         and len(record) > wiz.csv_ref_index \
                         and len(record) > wiz.csv_debit_index \
                         and len(record) > wiz.csv_credit_index:
@@ -204,6 +249,10 @@
                     record_ref = record[wiz.csv_ref_index]
                     record_debit = record[wiz.csv_debit_index]
                     record_credit = record[wiz.csv_credit_index]
+                    record_partner_ref = False
+                    
+                    if len(record) > wiz.csv_partner_ref_index:
+                        record_partner_ref = record[wiz.csv_partner_ref_index]
 
                     #
                     # Ignore invalid records
@@ -213,10 +262,37 @@
                             and re.match(wiz.csv_debit_regexp, record_debit) \
                             and re.match(wiz.csv_credit_regexp, record_credit):
                         #
-                        # Clean the input amounts
+                        # Clean the input amounts or fail
                         #
-                        record_debit = float(record_debit.replace(wiz.csv_thousands_separator, '').replace(wiz.csv_decimal_separator, '.'))
-                        record_credit = float(record_credit.replace(wiz.csv_thousands_separator, '').replace(wiz.csv_decimal_separator, '.'))
+                        if record_debit:
+                            record_debit = record_debit.replace(
+                                    wiz.csv_thousands_separator, '').\
+                                    replace(
+                                        wiz.csv_decimal_separator, '.')
+                            if re.match(reFloat, record_debit):
+                                record_debit = float(record_debit)
+                            else:
+                                raise osv.except_osv(_('Error'),
+                                _("The debit quantity %s doesn't match with"\
+                                " a regex float value in account %s") %
+                                (record_debit, record_code))
+                        else:
+                            record_debit = 0.0
+                            
+                        if record_credit:
+                            record_credit = record_credit.replace(
+                                wiz.csv_thousands_separator, '').\
+                                replace(
+                                wiz.csv_decimal_separator, '.')
+                            if re.match(reFloat, record_credit):
+                                record_credit = float(record_credit)
+                            else:
+                                raise osv.except_osv(_('Error'),
+                                _("The credit quantity %s doesn't match with"\
+                                " a regex float value in account %s") %
+                                (record_credit, record_code))
+                        else:
+                            record_credit = 0.0
 
                         #
                         # Find the account (or fail!)
@@ -232,7 +308,22 @@
                         if not account_ids:
                             raise osv.except_osv(_('Error'), _("Account\
                                         not found: %s!") % record_code)
-
+                                        
+                        #
+                        # Find the partner searching in the 
+                        # oerp_partner_ref_field
+                        #
+                        partner_id = False
+                        if record_partner_ref and re.match(
+                                wiz.csv_partner_ref_regexp,
+                                record_partner_ref):
+                            field = field_obj.browse(cr, uid,
+                                wiz.oerp_partner_ref_field.id, context).name
+                            partner_ids = partner_obj.search(cr, uid,
+                                [(field, '=', record_partner_ref)])
+                            if partner_ids:
+                                partner_id = partner_ids[0]
+    
                         #
                         # Prepare the line data
                         #
@@ -244,8 +335,9 @@
                             'ref': False,
                             'currency_id': False,
                             'tax_amount': False,
-                            'partner_id': accounts_map.get(account_ids[0])
-                            or False,
+                            'partner_id': partner_id or 
+                                (accounts_map.get(account_ids[0]) or
+                                False),
                             'tax_code_id': False,
                             'date_maturity': False,
                             'amount_currency': False,

=== modified file 'account_admin_tools_importer/account_move_importer.xml'
--- account_admin_tools_importer/account_move_importer.xml	2013-01-04 14:19:25 +0000
+++ account_admin_tools_importer/account_move_importer.xml	2013-11-22 13:37:30 +0000
@@ -18,10 +18,10 @@
                             <field name="ref"/>
                             <field name="company_id"/>
                             <newline/>
-                            <field name="journal_id"/>
+                            <field name="journal_id" domain="[('company_id', '=', company_id)]"/>
                             <field name="type"/>
                             <newline/>
-                            <field name="period_id"/>
+                            <field name="period_id" domain="[('company_id', '=', company_id)]"/>
                             <field name="date"/>
                         </group>
                     </group>
@@ -47,6 +47,9 @@
                                 <field name="csv_debit_regexp"/>
                                 <field name="csv_credit_index"/>
                                 <field name="csv_credit_regexp"/>
+                                <field name="csv_partner_ref_index"/>
+                                <field name="csv_partner_ref_regexp"/>
+                                <field name="oerp_partner_ref_field"/>
                             </group>
                         </group>
                     </group>


Follow ups