mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[IMP] Partner creation and update
This commit is contained in:
@@ -2,8 +2,9 @@
|
|||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from odoo import _, api, fields, models
|
from odoo import api, models
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
class ResPartner(models.Model):
|
class ResPartner(models.Model):
|
||||||
@@ -13,122 +14,112 @@ class ResPartner(models.Model):
|
|||||||
@api.model
|
@api.model
|
||||||
def rm_add_customer(self, customer):
|
def rm_add_customer(self, customer):
|
||||||
# RoomMatik API CREACIÓN DE CLIENTE
|
# RoomMatik API CREACIÓN DE CLIENTE
|
||||||
partnername = customer['LastName1'] + ' ' + customer[
|
_logger = logging.getLogger(__name__)
|
||||||
'LastName2'] + ' ' + customer['FirstName']
|
|
||||||
partner_res = self.env['res.partner'].search(
|
|
||||||
[('name', '=', partnername)])
|
|
||||||
# Need a smart search function here (Check name, document, mail) return
|
|
||||||
# unique or null customer.
|
|
||||||
|
|
||||||
json_response = dict()
|
partner_res = self.env['res.partner'].search([(
|
||||||
|
'document_number', '=',
|
||||||
|
customer['IdentityDocument'][0]['Number'])])
|
||||||
|
|
||||||
|
json_response = {'Id': 0}
|
||||||
if any(partner_res):
|
if any(partner_res):
|
||||||
json_response = {
|
# Change customer data
|
||||||
"Id": partner_res.id,
|
_logger.info('ROOMMATIK %s exist in BD %s res.artner id Rewriting',
|
||||||
"FirstName": partner_res.firstname,
|
partner_res[0].document_number,
|
||||||
"LastName1": partner_res.lastname,
|
partner_res[0].id,)
|
||||||
"LastName2": partner_res.lastname2,
|
try:
|
||||||
"Birthday": partner_res.birthdate_date,
|
partner_res[0].update(self.rm_preare_customer(customer))
|
||||||
"Sex": partner_res.gender,
|
write_custumer = partner_res[0]
|
||||||
"IdentityDocument": [{
|
except:
|
||||||
"Number": "Null",
|
_logger.error('ROOMMATIK Rewriting %s in BD %s ID',
|
||||||
"Type": "Null",
|
partner_res[0].document_number,
|
||||||
"ExpiryDate": "dateTime",
|
partner_res[0].id,)
|
||||||
"ExpeditionDate": "dateTime",
|
|
||||||
"Address": [{
|
|
||||||
"Nationality": "Null",
|
|
||||||
"Country": partner_res.country_id.name,
|
|
||||||
"ZipCode": partner_res.zip,
|
|
||||||
"City": partner_res.city,
|
|
||||||
"Street": partner_res.street,
|
|
||||||
"House": "Null",
|
|
||||||
"Flat": "Null",
|
|
||||||
"Number": "Null",
|
|
||||||
"Province": partner_res.state_id.name,
|
|
||||||
}],
|
|
||||||
}],
|
|
||||||
"Contact": [{
|
|
||||||
"Telephone": partner_res.phone,
|
|
||||||
"Fax": "Null",
|
|
||||||
"Mobile": partner_res.mobile,
|
|
||||||
"Email": partner_res.email,
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
else:
|
else:
|
||||||
# Create new customer
|
# Create new customer
|
||||||
json_response = {'Id': 0}
|
try:
|
||||||
|
write_custumer = self.create(self.rm_preare_customer(customer))
|
||||||
|
_logger.info('ROOMMATIK Create %s in BD like %s ID',
|
||||||
|
write_custumer.document_number,
|
||||||
|
write_custumer.id,)
|
||||||
|
except:
|
||||||
|
_logger.error('ROOMMATIK Creating %s in BD %s ID',
|
||||||
|
write_custumer.document_number,
|
||||||
|
write_custumer.id,)
|
||||||
|
|
||||||
# Check Sex string
|
json_response = {'Id': write_custumer.id,
|
||||||
if customer['Sex'] not in {'male', 'female'}:
|
'FirstName': write_custumer.firstname,
|
||||||
customer['Sex'] = ''
|
'LastName1': write_custumer.lastname,
|
||||||
|
'LastName2': write_custumer.lastname2,
|
||||||
# Check state_id
|
'Birthday': write_custumer.birthdate_date,
|
||||||
city_srch = self.env['res.country.state'].search([
|
'Sex': write_custumer.gender,
|
||||||
('name', 'ilike',
|
'Address': {
|
||||||
customer['IdentityDocument'][0][
|
'Nationality': write_custumer.zip,
|
||||||
'Address'][0]['Province'])])
|
'Country': write_custumer.zip,
|
||||||
|
'ZipCode': write_custumer.zip,
|
||||||
# Create Street2
|
'City': write_custumer.city,
|
||||||
street_2 = 'Nº ' + customer['IdentityDocument'][0][
|
'Street': write_custumer.street,
|
||||||
'Address'][0]['House']
|
'House': customer['IdentityDocument'][0][
|
||||||
street_2 += ', ' + customer['IdentityDocument'][0][
|
'Address'][0]['House'],
|
||||||
'Address'][0]['Flat']
|
'Flat': customer['IdentityDocument'][0][
|
||||||
street_2 += ', ' + customer['IdentityDocument'][0][
|
'Address'][0]['Flat'],
|
||||||
'Address'][0]['Number']
|
'Number': customer['IdentityDocument'][0][
|
||||||
|
'Address'][0]['Number'],
|
||||||
# Check birthdate_date
|
'Province': customer['IdentityDocument'][0][
|
||||||
# Here need to convert birthdate_date to '%d%m%Y' fomat
|
'Address'][0]['Province'],
|
||||||
|
},
|
||||||
write_custumer = self.create({
|
'IdentityDocument': {
|
||||||
'firstname': customer['FirstName'],
|
'Number': write_custumer.document_number,
|
||||||
'lastname': customer['LastName1'],
|
'Type': write_custumer.document_type,
|
||||||
'lastname2': customer['LastName2'],
|
'ExpiryDate': customer[
|
||||||
'birthdate_date': datetime.strptime(
|
'IdentityDocument'][0]['ExpiryDate'],
|
||||||
customer['Birthday'], '%d%m%Y'),
|
'ExpeditionDate': write_custumer.document_expedition_date,
|
||||||
'gender': customer['Sex'],
|
},
|
||||||
'zip': customer['IdentityDocument'][0][
|
'Contact': {
|
||||||
'Address'][0]['ZipCode'],
|
'Telephone': write_custumer.phone,
|
||||||
'city': customer['IdentityDocument'][0][
|
'Fax': customer['Contact'][0]['Fax'],
|
||||||
'Address'][0]['City'],
|
'Mobile': write_custumer.mobile,
|
||||||
'street': customer['IdentityDocument'][0][
|
'Email': write_custumer.email,
|
||||||
'Address'][0]['Street'],
|
}
|
||||||
'street2': street_2,
|
}
|
||||||
'state_id': city_srch.id,
|
|
||||||
'phone': customer['Contact'][0]['Telephone'],
|
|
||||||
'mobile': customer['Contact'][0]['Mobile'],
|
|
||||||
'email': customer['Contact'][0]['Email'],
|
|
||||||
})
|
|
||||||
|
|
||||||
json_response = {'Id': write_custumer.id}
|
|
||||||
|
|
||||||
# Id: será 0 en solicitud y diferente de 0 si el cliente se ha creado
|
|
||||||
# correctamente en el PMS.
|
|
||||||
# FirstName: nombre.
|
|
||||||
# LastName1: primer apellido.
|
|
||||||
# LastName2: segundo apellido.
|
|
||||||
# Birthday: fecha de nacimiento.
|
|
||||||
# Sex: sexo. Puede ser “M” para masculino o “F” para femenino.
|
|
||||||
# IdentityDocument: documento de identidad, formado por los siguientes
|
|
||||||
# valores:
|
|
||||||
# o Number: número de documento.
|
|
||||||
# o Type: tipo de documento. Puede ser:
|
|
||||||
# ▪ C: permiso de conducir.
|
|
||||||
# ▪ X: permiso de residencia europeo.
|
|
||||||
# ▪ D: DNI.
|
|
||||||
# ▪ I: documento de identidad.
|
|
||||||
# ▪ P: pasaporte.
|
|
||||||
# ▪ N: permiso de residencia español.
|
|
||||||
# o Expedition: fecha de expedición.
|
|
||||||
# o Expiration: fecha de caducidad.
|
|
||||||
# Address: dirección, formada por los siguientes valores:
|
|
||||||
# o City: ciudad.
|
|
||||||
# o Country: país (código ISO 3).
|
|
||||||
# o Flat: piso.
|
|
||||||
# o Nationality: nacionalidad (código ISO 3).
|
|
||||||
# o Number: número.
|
|
||||||
# o StateOrProvince: estado o provincia.
|
|
||||||
# o Street: calle.
|
|
||||||
# o ZipCode: código postal.
|
|
||||||
|
|
||||||
json_response = json.dumps(json_response)
|
json_response = json.dumps(json_response)
|
||||||
|
|
||||||
return json_response
|
return json_response
|
||||||
|
|
||||||
|
def rm_preare_customer(self, customer):
|
||||||
|
# Check Sex string
|
||||||
|
if customer['Sex'] not in {'male', 'female'}:
|
||||||
|
customer['Sex'] = ''
|
||||||
|
# Check state_id
|
||||||
|
city_srch = self.env['res.country.state'].search([
|
||||||
|
('name', 'ilike', customer['IdentityDocument'][0][
|
||||||
|
'Address'][0]['Province'])])
|
||||||
|
# Create Street2
|
||||||
|
street_2 = 'Nº ' + customer['IdentityDocument'][0][
|
||||||
|
'Address'][0]['House']
|
||||||
|
street_2 += ', ' + customer['IdentityDocument'][0][
|
||||||
|
'Address'][0]['Flat']
|
||||||
|
street_2 += ', ' + customer['IdentityDocument'][0][
|
||||||
|
'Address'][0]['Number']
|
||||||
|
return {
|
||||||
|
'firstname': customer['FirstName'],
|
||||||
|
'lastname': customer['LastName1'],
|
||||||
|
'lastname2': customer['LastName2'],
|
||||||
|
'birthdate_date': datetime.strptime(customer['Birthday'],
|
||||||
|
"%d%m%Y").date(),
|
||||||
|
'gender': customer['Sex'],
|
||||||
|
'zip': customer['IdentityDocument'][0][
|
||||||
|
'Address'][0]['ZipCode'],
|
||||||
|
'city': customer['IdentityDocument'][0][
|
||||||
|
'Address'][0]['City'],
|
||||||
|
'street': customer['IdentityDocument'][0][
|
||||||
|
'Address'][0]['Street'],
|
||||||
|
'street2': street_2,
|
||||||
|
'state_id': city_srch.id,
|
||||||
|
'phone': customer['Contact'][0]['Telephone'],
|
||||||
|
'mobile': customer['Contact'][0]['Mobile'],
|
||||||
|
'email': customer['Contact'][0]['Email'],
|
||||||
|
'document_number': customer['IdentityDocument'][0]['Number'],
|
||||||
|
'document_type': customer['IdentityDocument'][0]['Type'],
|
||||||
|
'document_expedition_date': datetime.strptime(customer[
|
||||||
|
'IdentityDocument'][0]['ExpeditionDate'],
|
||||||
|
"%d%m%Y").date(),
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user