import base64 import tempfile import hashlib import json import os from odoo import models, fields, _ from tbc_bank_integration_service_lib.client import TBCBankClient from odoo.exceptions import UserError class BankMovements(models.Model): _name = 'bank.raw.movements' _description = 'Bank Movements' json_data = fields.Json(string="Json Data") json_hash_code = fields.Char(string="Hash Code") bid = fields.Char(string="Movement Id") parent_id = fields.Many2one('bank.raw.movements', string="Parent Record") history_ids = fields.One2many(comodel_name="bank.raw.movements", inverse_name='parent_id', string="History") def create_or_update_raw_data(self, data, model, bid): json_data = json.dumps(data, sort_keys=True, indent=4, ensure_ascii=False) hash_object = hashlib.sha256(json_data.encode()) hash_hex = hash_object.hexdigest() found_raw_record = self.env[model].search([ ('bid', '=', bid), ]) if found_raw_record: if found_raw_record.json_hash_code != hash_hex: # found_raw_record.write({ # 'json_data': json_data, # 'json_hash_code': hash_hex # }) new_created_record = self.env[model].create({ 'json_data': json_data, 'json_hash_code': hash_hex, 'bid': bid, }) found_raw_record.parent_id = new_created_record.id else: found_raw_record = self.env[model].create({ 'json_data': json_data, 'json_hash_code': hash_hex, 'bid': bid }) def get_movements(self): client = self.env['bank.settings'].get_request_object() try: movement_id = '013762510743.2' movements_by_id = client.get_account_movements_by_id('111111', movement_id) self.create_or_update_raw_data(movements_by_id, 'bank.raw.movements', movements_by_id['data'].get('movementId')) except Exception as e: _logger.error(e)