From 75605a4ef6d1c155929f4abccc03756536cc977c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Fri, 28 Jun 2024 09:47:32 +0200 Subject: [PATCH] [FIX]pms_ocr_klippa: improvement log errors and fix search partner --- pms_ocr_klippa/models/klippa_log.py | 1 + pms_ocr_klippa/models/pms_property.py | 24 +++++++++++++++++------ pms_ocr_klippa/views/klippa_log_views.xml | 6 +++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/pms_ocr_klippa/models/klippa_log.py b/pms_ocr_klippa/models/klippa_log.py index a3e0ddd0b..d379ddf5b 100644 --- a/pms_ocr_klippa/models/klippa_log.py +++ b/pms_ocr_klippa/models/klippa_log.py @@ -5,6 +5,7 @@ from odoo import fields, models class KlippaLog(models.Model): _name = "klippa.log" + _order = "id desc" pms_property_id = fields.Many2one( string="PMS Property", diff --git a/pms_ocr_klippa/models/pms_property.py b/pms_ocr_klippa/models/pms_property.py index e904413e3..dee2588a8 100644 --- a/pms_ocr_klippa/models/pms_property.py +++ b/pms_ocr_klippa/models/pms_property.py @@ -1,4 +1,5 @@ import logging +import traceback from datetime import date, datetime import requests @@ -130,7 +131,7 @@ class PmsProperty(models.Model): except Exception as e: log_data.update( { - "error": str(e), + "error": traceback.format_exc(), "final_status": "error", "total_duration": ( datetime.now() - log_data["request_datetime"] @@ -296,14 +297,21 @@ class PmsProperty(models.Model): ("klippa_code", "=", klippa_type), ("country_ids", "=", False), ], - limit=1, ) elif not document_type: document_type = self.env["res.partner.id_category"].search( [ ("klippa_code", "=", klippa_type), ], - limit=1, + ) + if len(document_type) > 1: + # Try find document type by klippa_subtype_code, if not found, get the first + document_subtype = document_type.filtered( + lambda dt: dt.klippa_subtype_code + == document_data.get("document_subtype").get("value") + ) + document_type = ( + document_subtype[0] if document_subtype else document_type[0] ) if not document_type: document_type = self.env.ref("pms.document_type_identification_document") @@ -353,7 +361,7 @@ class PmsProperty(models.Model): ) if candidates[1] >= 90: country_state = self.env["res.country.state"].search( - domain + [("name", "=", candidates[0])] + domain + [("name", "=", candidates[0])], limit=1 ) mapped_data["country_state"] = country_state.id if not country_record and country_state: @@ -500,7 +508,7 @@ class PmsProperty(models.Model): ) if country_state_record[1] >= 90: country_state = self.env["res.country.state"].search( - [("name", "=", country_state_record[0])] + [("name", "=", country_state_record[0])], limit=1 ) mapped_data["country_state"] = country_state.id if not mapped_data.get("residence_city", False): @@ -515,6 +523,10 @@ class PmsProperty(models.Model): def _complete_mapped_from_partner(self, document, mapped_data): for key, field in CHECKIN_FIELDS.items(): - if not mapped_data.get(key, False) and document.mapped(field)[0]: + if ( + not mapped_data.get(key, False) + and document.mapped(field) + and document.mapped(field)[0] + ): mapped_data[key] = document.mapped(field)[0] return mapped_data diff --git a/pms_ocr_klippa/views/klippa_log_views.xml b/pms_ocr_klippa/views/klippa_log_views.xml index 8ca869a8c..20a307f0a 100644 --- a/pms_ocr_klippa/views/klippa_log_views.xml +++ b/pms_ocr_klippa/views/klippa_log_views.xml @@ -4,7 +4,11 @@ klippa.log.tree klippa.log - +