~banking-addons-team/banking-addons/bank-statement-reconcile-70

« back to all changes in this revision

Viewing changes to account_statement_base_completion/statement.py

  • Committer: Alexandre Fayolle
  • Author(s): nicolas.bessi at camptocamp
  • Date: 2013-03-07 08:50:56 UTC
  • mfrom: (83.1.7 add_rule)
  • Revision ID: alexandre.fayolle@camptocamp.com-20130307085056-vix8qip2e4jzhe6y
[MRG] Add a completion rule to allows supplier invoice completion baser on invoice number

Show diffs side-by-side

added added

removed removed

Lines of Context:
107
107
        """
108
108
        return [
109
109
            ('get_from_ref_and_invoice', 'From line reference (based on invoice number)'),
 
110
            ('get_from_ref_and_supplier_invoice', 'From line reference (based on supplier invoice number)'),
110
111
            ('get_from_ref_and_so', 'From line reference (based on SO number)'),
111
112
            ('get_from_label_and_partner_field', 'From line label (based on partner field)'),
112
113
            ('get_from_label_and_partner_name', 'From line label (based on partner name)'),
122
123
        'function_to_call': fields.selection(_get_functions, 'Method'),
123
124
    }
124
125
 
 
126
    def get_from_ref_and_supplier_invoice(self, cr, uid, line_id, context=None):
 
127
        """
 
128
        Match the partner based on the invoice supplier invoice number and the reference of the statement
 
129
        line. Then, call the generic get_values_for_line method to complete other values.
 
130
        If more than one partner matched, raise the ErrorTooManyPartner error.
 
131
 
 
132
        :param int/long line_id: id of the concerned account.bank.statement.line
 
133
        :return:
 
134
            A dict of value that can be passed directly to the write method of
 
135
            the statement line or {}
 
136
           {'partner_id': value,
 
137
            'account_id' : value,
 
138
 
 
139
            ...}
 
140
        """
 
141
        st_obj = self.pool['account.bank.statement.line']
 
142
        st_line = st_obj.browse(cr, uid, line_id, context=context)
 
143
        res = {}
 
144
        inv_obj = self.pool.get('account.invoice')
 
145
        if st_line:
 
146
            inv_id = inv_obj.search(cr,
 
147
                                    uid,
 
148
                                    [('supplier_invoice_number', '=', st_line.ref),
 
149
                                     ('type', 'in', ('in_invoice', 'in_refund'))],
 
150
                                    context=context)
 
151
            if inv_id:
 
152
                if len(inv_id) == 1:
 
153
                    inv = inv_obj.browse(cr, uid, inv_id[0], context=context)
 
154
                    res['partner_id'] = inv.partner_id.id
 
155
                else:
 
156
                    raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by more '
 
157
                                                'than one partner.') % (st_line.name, st_line.ref))
 
158
                st_vals = st_obj.get_values_for_line(cr,
 
159
                                                     uid,
 
160
                                                     profile_id=st_line.statement_id.profile_id.id,
 
161
                                                     partner_id=res.get('partner_id', False),
 
162
                                                     line_type="supplier",
 
163
                                                     amount=st_line.amount,
 
164
                                                     context=context)
 
165
                res.update(st_vals)
 
166
        return res
 
167
 
125
168
    def get_from_ref_and_invoice(self, cr, uid, line_id, context=None):
126
169
        """
127
170
        Match the partner based on the invoice number and the reference of the statement
134
177
            the statement line or {}
135
178
           {'partner_id': value,
136
179
            'account_id' : value,
137
 
 
138
180
            ...}
139
181
        """
140
182
        st_obj = self.pool.get('account.bank.statement.line')
142
184
        res = {}
143
185
        if st_line:
144
186
            inv_obj = self.pool.get('account.invoice')
145
 
            inv_id = inv_obj.search(
146
 
                    cr,
147
 
                    uid,
148
 
                    [('number', '=', st_line.ref)],
149
 
                    context=context)
 
187
            inv_id = inv_obj.search(cr,
 
188
                                    uid,
 
189
                                    [('number', '=', st_line.ref)],
 
190
                                    context=context)
150
191
            if inv_id:
151
 
                if inv_id and len(inv_id) == 1:
 
192
                if len(inv_id) == 1:
152
193
                    inv = inv_obj.browse(cr, uid, inv_id[0], context=context)
153
194
                    res['partner_id'] = inv.partner_id.id
154
 
                elif inv_id and len(inv_id) > 1:
155
 
                    raise ErrorTooManyPartner(
156
 
                            _('Line named "%s" (Ref:%s) was matched by more '
157
 
                              'than one partner.') % (st_line.name, st_line.ref))
158
 
                st_vals = st_obj.get_values_for_line(
159
 
                        cr,
160
 
                        uid,
161
 
                        profile_id=st_line.statement_id.profile_id.id,
162
 
                        partner_id=res.get('partner_id', False),
163
 
                        line_type=st_line.type,
164
 
                        amount=st_line.amount,
165
 
                        context=context)
 
195
                else:
 
196
                    raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by more '
 
197
                                                'than one partner.') % (st_line.name, st_line.ref))
 
198
                st_vals = st_obj.get_values_for_line(cr,
 
199
                                                     uid,
 
200
                                                     profile_id=st_line.statement_id.profile_id.id,
 
201
                                                     partner_id=res.get('partner_id', False),
 
202
                                                     line_type=st_line.type,
 
203
                                                     amount=st_line.amount,
 
204
                                                     context=context)
166
205
                res.update(st_vals)
167
206
        return res
168
207