~akretion-team/banking-addons/bank-statement-reconcile-sale-order-completion

« back to all changes in this revision

Viewing changes to account_statement_base_import/parser/generic_file_parser.py

  • Committer: Guewen Baconnier
  • Author(s): Virgil Dupras
  • Date: 2013-07-22 09:06:56 UTC
  • mfrom: (90.2.5 purge-base-import)
  • Revision ID: guewen.baconnier@camptocamp.com-20130722090656-sm1zkp5tqq5tn72q
[IMP] Extracted the commission-handling feature from account_statement_base_import to a new module account_statement_commission.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
    raise Exception(_('Please install python lib xlrd'))
31
31
 
32
32
 
33
 
def float_or_zero(val):
34
 
    """ Conversion function used to manage
35
 
    empty string into float usecase"""
36
 
    return float(val) if val else 0.0
37
 
 
38
 
 
39
33
class GenericFileParser(FileParser):
40
34
    """
41
35
    Standard parser that use a define format in csv or xls to import into a
43
37
    parser, but will also be useful as it allow to import a basic flat file.
44
38
    """
45
39
 
46
 
    def __init__(self, parse_name, ftype='csv'):
47
 
        conversion_dict = {
48
 
                            'ref': unicode,
49
 
                            'label': unicode,
50
 
                            'date': datetime.datetime,
51
 
                            'amount': float_or_zero,
52
 
                            'commission_amount': float_or_zero
53
 
                          }
54
 
        # Order of cols does not matter but first row of the file has to be header
55
 
        keys_to_validate = ['ref', 'label', 'date', 'amount', 'commission_amount']
56
 
        super(GenericFileParser, self).__init__(parse_name, keys_to_validate=keys_to_validate, ftype=ftype, conversion_dict=conversion_dict)
 
40
    def __init__(self, parse_name, ftype='csv', **kwargs):
 
41
        super(GenericFileParser, self).__init__(parse_name, ftype=ftype, **kwargs)
57
42
 
58
43
    @classmethod
59
44
    def parser_for(cls, parser_name):
78
63
                    'amount':value,
79
64
                    'ref':value,
80
65
                    'label':value,
81
 
                    'commission_amount':value,
82
66
                }
83
 
        In this generic parser, the commission is given for every line, so we store it
84
 
        for each one.
85
 
        """
86
 
        return {'name': line.get('label', line.get('ref', '/')),
87
 
                'date': line.get('date', datetime.datetime.now().date()),
88
 
                'amount': line.get('amount', 0.0),
89
 
                'ref': line.get('ref', '/'),
90
 
                'label': line.get('label', ''),
91
 
                'commission_amount': line.get('commission_amount', 0.0)}
92
 
 
93
 
    def _post(self, *args, **kwargs):
94
 
        """
95
 
        Compute the commission from value of each line
96
 
        """
97
 
        res = super(GenericFileParser, self)._post(*args, **kwargs)
98
 
        val = 0.0
99
 
        for row in self.result_row_list:
100
 
            val += row.get('commission_amount', 0.0)
101
 
        self.commission_global_amount = val
102
 
        return res
 
67
        """
 
68
        return {
 
69
            'name': line.get('label', line.get('ref', '/')),
 
70
            'date': line.get('date', datetime.datetime.now().date()),
 
71
            'amount': line.get('amount', 0.0),
 
72
            'ref': line.get('ref', '/'),
 
73
            'label': line.get('label', ''),
 
74
        }