def _complete_stmts_vals(self, stmts_vals, journal, account_number):
        for st_vals in stmts_vals:
            st_vals['journal_id'] = journal.id
            if not st_vals.get('reference'):
                st_vals['reference'] = " ".join(
                    self.attachment_ids.mapped('name'))
            if st_vals.get('number'):
                # build the full name like BNK/2016/00135 by just giving the number '135'
                st_vals['name'] = journal.sequence_id.with_context(
                    ir_sequence_date=st_vals.get('date')).get_next_char(
                        st_vals['number'])
                del (st_vals['number'])
            for line_vals in st_vals['transactions']:
                unique_import_id = line_vals.get('unique_import_id')
                if unique_import_id:
                    sanitized_account_number = sanitize_account_number(
                        account_number)
                    line_vals['unique_import_id'] = (
                        sanitized_account_number and sanitized_account_number +
                        '-' or '') + str(journal.id) + '-' + unique_import_id

                if not line_vals.get('bank_account_id'):
                    # Find the partner and his bank account or create the bank account. The partner selected during the
                    # reconciliation process will be linked to the bank when the statement is closed.
                    identifying_string = line_vals.get('account_number')
                    if identifying_string:
                        partner_bank = self.env['res.partner.bank'].search(
                            [('acc_number', '=', identifying_string)], limit=1)
                        if partner_bank:
                            line_vals['bank_account_id'] = partner_bank.id
                            line_vals[
                                'partner_id'] = partner_bank.partner_id.id
        return stmts_vals
    def _find_additional_data(self, currency_code, account_number):
        """ Look for a res.currency and account.journal using values extracted from the
            statement and make sure it's consistent.
        """
        company_currency = self.env.company.currency_id
        journal_obj = self.env['account.journal']
        currency = None
        sanitized_account_number = sanitize_account_number(account_number)

        if currency_code:
            currency = self.env['res.currency'].search(
                [('name', '=ilike', currency_code)], limit=1)
            if not currency:
                raise UserError(
                    _("No currency found matching '%s'.") % currency_code)
            if currency == company_currency:
                currency = False

        journal = journal_obj.browse(self.env.context.get('journal_id', []))
        if account_number:
            # No bank account on the journal : create one from the account number of the statement
            if journal and not journal.bank_account_id:
                journal.set_bank_account(account_number)
            # No journal passed to the wizard : try to find one using the account number of the statement
            elif not journal:
                journal = journal_obj.search([
                    ('bank_account_id.sanitized_acc_number', '=',
                     sanitized_account_number)
                ])
            # Already a bank account on the journal : check it's the same as on the statement
            else:
                if not self._check_journal_bank_account(
                        journal, sanitized_account_number):
                    raise UserError(
                        _('The account of this statement (%s) is not the same as the journal (%s).'
                          ) %
                        (account_number, journal.bank_account_id.acc_number))

        # If importing into an existing journal, its currency must be the same as the bank statement
        if journal:
            journal_currency = journal.currency_id
            if currency is None:
                currency = journal_currency
            if currency and currency != journal_currency:
                statement_cur_code = not currency and company_currency.name or currency.name
                journal_cur_code = not journal_currency and company_currency.name or journal_currency.name
                raise UserError(
                    _('The currency of the bank statement (%s) is not the same as the currency of the journal (%s).'
                      ) % (statement_cur_code, journal_cur_code))

        # If we couldn't find / can't create a journal, everything is lost
        if not journal and not account_number:
            raise UserError(
                _('Cannot find in which journal import this statement. Please manually select a journal.'
                  ))

        return currency, journal
示例#3
0
 def _validate_qr_iban(self, qr_iban):
     # Check first if it's a valid IBAN.
     validate_iban(qr_iban)
     # We sanitize first so that _check_qr_iban_range() can extract correct IID from IBAN to validate it.
     sanitized_qr_iban = sanitize_account_number(qr_iban)
     # Now, check if it's valid QR-IBAN (based on its IID).
     if not self._check_qr_iban_range(sanitized_qr_iban):
         raise ValidationError(_("QR-IBAN '%s' is invalid.") % qr_iban)
     return True
示例#4
0
 def _l10n_ch_get_qr_vals(self, amount, currency, debtor_partner,
                          free_communication, structured_communication):
     qr_vals = super()._l10n_ch_get_qr_vals(amount, currency,
                                            debtor_partner,
                                            free_communication,
                                            structured_communication)
     # If there is a QR IBAN we use it for the barcode instead of the account number
     if self.l10n_ch_qr_iban:
         qr_vals[3] = sanitize_account_number(self.l10n_ch_qr_iban)
     return qr_vals
示例#5
0
    def _onchange_set_l10n_ch_postal(self):
        try:
            validate_iban(self.bank_acc_number)
            is_iban = True
        except ValidationError:
            is_iban = False

        if is_iban:
            self.l10n_ch_postal = self.env[
                'res.partner.bank']._retrieve_l10n_ch_postal(
                    sanitize_account_number(self.bank_acc_number))
        else:
            self.l10n_ch_postal = self.bank_acc_number
示例#6
0
 def set_bank_account(self, acc_number, bank_id=None):
     """ Create a res.partner.bank (if not exists) and set it as value of the field bank_account_id """
     self.ensure_one()
     res_partner_bank = self.env['res.partner.bank'].search([
         ('sanitized_acc_number', '=', sanitize_account_number(acc_number)),
         ('company_id', '=', self.company_id.id)
     ],
                                                            limit=1)
     if res_partner_bank:
         self.bank_account_id = res_partner_bank.id
     else:
         self.bank_account_id = self.env['res.partner.bank'].create({
             'acc_number':
             acc_number,
             'bank_id':
             bank_id,
             'company_id':
             self.company_id.id,
             'currency_id':
             self.currency_id.id,
             'partner_id':
             self.company_id.partner_id.id,
         }).id