refactor: organize code by domain-driven design
Refactoriza la estructura del proyecto siguiendo principios de Domain-Driven Design, organizando serializers, API views y servicios por dominios de negocio. Cambios principales: ## Serializers (serializers/) - Dividido serializers.py en módulos por dominio: * products.py: ProductSerializer, ListProductSerializer * customers.py: CustomerSerializer, ListCustomerSerializer * sales.py: SaleSerializer, SaleLineSerializer, CatalogSaleSerializer, etc. * payments.py: ReconciliationJarSerializer, PaymentMethodSerializer * __init__.py: Exporta todos los serializers para mantener compatibilidad ## API Views (api/) - Dividido api_views.py en módulos por dominio: * products.py: ProductView, ProductsFromTrytonView * customers.py: CustomerView, CustomersFromTrytonView * sales.py: SaleView, CatalogSaleView, SaleSummary, SalesForTrytonView, SalesToTrytonView * payments.py: ReconciliateJarView, ReconciliateJarModelView, PaymentMethodView, SalesForReconciliationView * admin.py: AdminCodeValidateView * __init__.py: Exporta todas las vistas para facilitar importaciones ## Services Layer (services/tryton/) - Nueva capa de servicios para lógica de negocio Tryton: * client.py: get_tryton_client(), TrytonSale, TrytonLineSale, configuración * products.py: ProductTrytonService - sincronización de productos * customers.py: CustomerTrytonService - sincronización de clientes * sales.py: SaleTrytonService - sincronización de ventas * __init__.py: Exporta servicios y utilidades ## Actualización de URLs - Actualizado urls.py para importar desde nuevos módulos - Mantiene todas las rutas existentes sin cambios ## Eliminación de archivos antiguos - Eliminado serializers.py (refactorizado a serializers/) - Eliminado api_views.py (refactorizado a api/) ## Beneficios ✅ Cohesión: Código organizado por dominio de negocio ✅ Separación de responsabilidades: API, Serializers y Services separados ✅ Mantenibilidad: Archivos más pequeños y enfocados ✅ Escalabilidad: Fácil agregar nuevos dominios ✅ Testabilidad: Mejor organización para pruebas por dominio ✅ Reutilización: Servicios Tryton pueden usarse desde cualquier vista ## Estructura final: - models/ (ya existía organizado por dominio) - serializers/ (nuevo, organizado por dominio) - api/ (nuevo, organizado por dominio) - services/tryton/ (nuevo, capa de servicios) Tests: 46 tests pasando ✓
This commit is contained in:
13
tienda_ilusion/don_confiao/services/__init__.py
Normal file
13
tienda_ilusion/don_confiao/services/__init__.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from .tryton import (
|
||||
get_tryton_client,
|
||||
ProductTrytonService,
|
||||
CustomerTrytonService,
|
||||
SaleTrytonService,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"get_tryton_client",
|
||||
"ProductTrytonService",
|
||||
"CustomerTrytonService",
|
||||
"SaleTrytonService",
|
||||
]
|
||||
13
tienda_ilusion/don_confiao/services/tryton/__init__.py
Normal file
13
tienda_ilusion/don_confiao/services/tryton/__init__.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from .client import get_tryton_client, TrytonSale, TrytonLineSale
|
||||
from .products import ProductTrytonService
|
||||
from .customers import CustomerTrytonService
|
||||
from .sales import SaleTrytonService
|
||||
|
||||
__all__ = [
|
||||
"get_tryton_client",
|
||||
"TrytonSale",
|
||||
"TrytonLineSale",
|
||||
"ProductTrytonService",
|
||||
"CustomerTrytonService",
|
||||
"SaleTrytonService",
|
||||
]
|
||||
77
tienda_ilusion/don_confiao/services/tryton/client.py
Normal file
77
tienda_ilusion/don_confiao/services/tryton/client.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import os
|
||||
from sabatron_tryton_rpc_client.client import Client
|
||||
|
||||
TRYTON_HOST = os.environ.get("TRYTON_HOST", "localhost")
|
||||
TRYTON_DATABASE = os.environ.get("TRYTON_DATABASE", "tryton")
|
||||
TRYTON_USERNAME = os.environ.get("TRYTON_USERNAME", "admin")
|
||||
TRYTON_PASSWORD = os.environ.get("TRYTON_PASSWORD", "admin")
|
||||
TRYTON_COP_CURRENCY = 31
|
||||
TRYTON_COMPANY_ID = 1
|
||||
TRYTON_SHOPS = [1]
|
||||
|
||||
|
||||
def get_tryton_client():
|
||||
"""Factory para crear cliente Tryton conectado"""
|
||||
client = Client(
|
||||
hostname=TRYTON_HOST,
|
||||
database=TRYTON_DATABASE,
|
||||
username=TRYTON_USERNAME,
|
||||
password=TRYTON_PASSWORD,
|
||||
)
|
||||
client.connect()
|
||||
return client
|
||||
|
||||
|
||||
class TrytonSale:
|
||||
"""Representa una venta para exportación a Tryton"""
|
||||
|
||||
def __init__(self, sale, lines):
|
||||
self.sale = sale
|
||||
self.lines = lines
|
||||
|
||||
def _format_date(self, _date):
|
||||
return {
|
||||
"__class__": "date",
|
||||
"year": _date.year,
|
||||
"month": _date.month,
|
||||
"day": _date.day,
|
||||
}
|
||||
|
||||
def to_tryton(self):
|
||||
return {
|
||||
"company": TRYTON_COMPANY_ID,
|
||||
"shipment_address": self.sale.customer.address_external_id,
|
||||
"invoice_address": self.sale.customer.address_external_id,
|
||||
"currency": TRYTON_COP_CURRENCY,
|
||||
"comment": self.sale.description or "",
|
||||
"description": "Metodo pago: " + str(self.sale.payment_method or ""),
|
||||
"party": self.sale.customer.external_id,
|
||||
"reference": "don_confiao " + str(self.sale.id),
|
||||
"sale_date": self._format_date(self.sale.date),
|
||||
"lines": [
|
||||
[
|
||||
"create",
|
||||
[TrytonLineSale(line).to_tryton() for line in self.lines],
|
||||
]
|
||||
],
|
||||
"self_pick_up": True,
|
||||
}
|
||||
|
||||
|
||||
class TrytonLineSale:
|
||||
"""Representa una línea de venta para exportación a Tryton"""
|
||||
|
||||
def __init__(self, sale_line):
|
||||
self.sale_line = sale_line
|
||||
|
||||
def _format_decimal(self, number):
|
||||
return {"__class__": "Decimal", "decimal": str(number)}
|
||||
|
||||
def to_tryton(self):
|
||||
return {
|
||||
"product": self.sale_line.product.external_id,
|
||||
"quantity": self._format_decimal(self.sale_line.quantity),
|
||||
"type": "line",
|
||||
"unit": self.sale_line.product.unit_external_id,
|
||||
"unit_price": self._format_decimal(self.sale_line.unit_price),
|
||||
}
|
||||
81
tienda_ilusion/don_confiao/services/tryton/customers.py
Normal file
81
tienda_ilusion/don_confiao/services/tryton/customers.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from ...models.customers import Customer
|
||||
|
||||
|
||||
class CustomerTrytonService:
|
||||
"""Servicio para sincronización de clientes con Tryton"""
|
||||
|
||||
def __init__(self, tryton_client):
|
||||
self.client = tryton_client
|
||||
|
||||
def import_from_tryton(self):
|
||||
"""Importa clientes desde Tryton"""
|
||||
method = "model.party.party.search"
|
||||
context = {"company": 1}
|
||||
params = [[], 0, 1000, [["name", "ASC"], ["id", None]], context]
|
||||
party_ids = self.client.call(method, params)
|
||||
tryton_parties = self._get_party_details(party_ids, context)
|
||||
|
||||
checked_tryton_parties = party_ids
|
||||
failed_parties = []
|
||||
updated_customers = []
|
||||
created_customers = []
|
||||
untouched_customers = []
|
||||
|
||||
for tryton_party in tryton_parties:
|
||||
try:
|
||||
customer = Customer.objects.get(external_id=tryton_party.get("id"))
|
||||
except Customer.DoesNotExist:
|
||||
customer = self._create_customer(tryton_party)
|
||||
created_customers.append(customer.id)
|
||||
continue
|
||||
|
||||
if self._need_update(customer, tryton_party):
|
||||
self._update_customer(customer, tryton_party)
|
||||
updated_customers.append(customer.id)
|
||||
else:
|
||||
untouched_customers.append(customer.id)
|
||||
|
||||
return {
|
||||
"checked_tryton_parties": checked_tryton_parties,
|
||||
"failed_parties": failed_parties,
|
||||
"updated_customers": updated_customers,
|
||||
"created_customers": created_customers,
|
||||
"untouched_customers": untouched_customers,
|
||||
}
|
||||
|
||||
def _get_party_details(self, party_ids, context):
|
||||
"""Obtiene detalles de clientes desde Tryton"""
|
||||
tryton_fields = ["id", "name", "addresses"]
|
||||
method = "model.party.party.read"
|
||||
params = (party_ids, tryton_fields, context)
|
||||
response = self.client.call(method, params)
|
||||
return response
|
||||
|
||||
def _need_update(self, customer, tryton_party):
|
||||
"""Verifica si el cliente necesita actualización"""
|
||||
if not customer.name == tryton_party.get("name"):
|
||||
return True
|
||||
if tryton_party.get("addresses") and tryton_party.get("addresses")[0]:
|
||||
if not customer.address_external_id == str(
|
||||
tryton_party.get("addresses")[0]
|
||||
):
|
||||
return True
|
||||
return False
|
||||
|
||||
def _create_customer(self, tryton_party):
|
||||
"""Crea un nuevo cliente desde datos de Tryton"""
|
||||
customer = Customer()
|
||||
customer.name = tryton_party.get("name")
|
||||
customer.external_id = tryton_party.get("id")
|
||||
if tryton_party.get("addresses") and tryton_party.get("addresses")[0]:
|
||||
customer.address_external_id = tryton_party.get("addresses")[0]
|
||||
customer.save()
|
||||
return customer
|
||||
|
||||
def _update_customer(self, customer, tryton_party):
|
||||
"""Actualiza un cliente existente con datos de Tryton"""
|
||||
customer.name = tryton_party.get("name")
|
||||
customer.external_id = tryton_party.get("id")
|
||||
if tryton_party.get("addresses") and tryton_party.get("addresses")[0]:
|
||||
customer.address_external_id = tryton_party.get("addresses")[0]
|
||||
customer.save()
|
||||
104
tienda_ilusion/don_confiao/services/tryton/products.py
Normal file
104
tienda_ilusion/don_confiao/services/tryton/products.py
Normal file
@@ -0,0 +1,104 @@
|
||||
from ...models.products import Product
|
||||
|
||||
|
||||
class ProductTrytonService:
|
||||
"""Servicio para sincronización de productos con Tryton"""
|
||||
|
||||
def __init__(self, tryton_client):
|
||||
self.client = tryton_client
|
||||
|
||||
def import_from_tryton(self):
|
||||
"""Importa productos desde Tryton"""
|
||||
method = "model.product.product.search"
|
||||
context = {"company": 1}
|
||||
params = [
|
||||
[["salable", "=", True]],
|
||||
0,
|
||||
1000,
|
||||
[["rec_name", "ASC"], ["id", None]],
|
||||
context,
|
||||
]
|
||||
product_ids = self.client.call(method, params)
|
||||
tryton_products = self._get_product_details(product_ids, context)
|
||||
|
||||
checked_tryton_products = product_ids
|
||||
failed_products = []
|
||||
updated_products = []
|
||||
created_products = []
|
||||
untouched_products = []
|
||||
|
||||
for tryton_product in tryton_products:
|
||||
try:
|
||||
product = Product.objects.get(external_id=tryton_product.get("id"))
|
||||
except Product.DoesNotExist:
|
||||
try:
|
||||
product = self._create_product(tryton_product)
|
||||
created_products.append(product.id)
|
||||
continue
|
||||
except Exception as e:
|
||||
print(
|
||||
f"Error al importar productos: {e}El producto: {tryton_product}"
|
||||
)
|
||||
failed_products.append(tryton_product.get("id"))
|
||||
continue
|
||||
|
||||
if self._need_update(product, tryton_product):
|
||||
self._update_product(product, tryton_product)
|
||||
updated_products.append(product.id)
|
||||
else:
|
||||
untouched_products.append(product.id)
|
||||
|
||||
return {
|
||||
"checked_tryton_products": checked_tryton_products,
|
||||
"failed_products": failed_products,
|
||||
"updated_products": updated_products,
|
||||
"created_products": created_products,
|
||||
"untouched_products": untouched_products,
|
||||
}
|
||||
|
||||
def _get_product_details(self, product_ids, context):
|
||||
"""Obtiene detalles de productos desde Tryton"""
|
||||
tryton_fields = [
|
||||
"id",
|
||||
"name",
|
||||
"default_uom.id",
|
||||
"default_uom.rec_name",
|
||||
"list_price",
|
||||
]
|
||||
method = "model.product.product.read"
|
||||
params = (product_ids, tryton_fields, context)
|
||||
response = self.client.call(method, params)
|
||||
return response
|
||||
|
||||
def _need_update(self, product, tryton_product):
|
||||
"""Verifica si el producto necesita actualización"""
|
||||
if not product.name == tryton_product.get("name"):
|
||||
return True
|
||||
if not product.price == tryton_product.get("list_price"):
|
||||
return True
|
||||
unit = tryton_product.get("default_uom.")
|
||||
if not product.measuring_unit == unit.get("rec_name"):
|
||||
return True
|
||||
return False
|
||||
|
||||
def _create_product(self, tryton_product):
|
||||
"""Crea un nuevo producto desde datos de Tryton"""
|
||||
product = Product()
|
||||
product.name = tryton_product.get("name")
|
||||
product.price = tryton_product.get("list_price")
|
||||
product.external_id = tryton_product.get("id")
|
||||
unit = tryton_product.get("default_uom.")
|
||||
product.measuring_unit = unit.get("rec_name")
|
||||
product.unit_external_id = unit.get("id")
|
||||
product.save()
|
||||
return product
|
||||
|
||||
def _update_product(self, product, tryton_product):
|
||||
"""Actualiza un producto existente con datos de Tryton"""
|
||||
product.name = tryton_product.get("name")
|
||||
product.price = tryton_product.get("list_price")
|
||||
product.external_id = tryton_product.get("id")
|
||||
unit = tryton_product.get("default_uom.")
|
||||
product.measuring_unit = unit.get("rec_name")
|
||||
product.unit_external_id = unit.get("id")
|
||||
product.save()
|
||||
41
tienda_ilusion/don_confiao/services/tryton/sales.py
Normal file
41
tienda_ilusion/don_confiao/services/tryton/sales.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from ...models.sales import Sale, SaleLine
|
||||
from .client import TrytonSale, TRYTON_COMPANY_ID, TRYTON_SHOPS
|
||||
|
||||
|
||||
class SaleTrytonService:
|
||||
"""Servicio para sincronización de ventas con Tryton"""
|
||||
|
||||
def __init__(self, tryton_client):
|
||||
self.client = tryton_client
|
||||
|
||||
def send_to_tryton(self):
|
||||
"""Envía ventas sin external_id a Tryton"""
|
||||
method = "model.sale.sale.create"
|
||||
tryton_context = {
|
||||
"company": TRYTON_COMPANY_ID,
|
||||
"shops": TRYTON_SHOPS,
|
||||
}
|
||||
|
||||
successful = []
|
||||
failed = []
|
||||
|
||||
sales = Sale.objects.filter(external_id=None)
|
||||
for sale in sales:
|
||||
try:
|
||||
lines = SaleLine.objects.filter(sale=sale.id)
|
||||
tryton_params = self._to_tryton_params(sale, lines, tryton_context)
|
||||
external_ids = self.client.call(method, tryton_params)
|
||||
sale.external_id = external_ids[0]
|
||||
sale.save()
|
||||
successful.append(sale.id)
|
||||
except Exception as e:
|
||||
print(f"Error al enviar la venta: {e}venta_id: {sale.id}")
|
||||
failed.append(sale.id)
|
||||
continue
|
||||
|
||||
return {"successful": successful, "failed": failed}
|
||||
|
||||
def _to_tryton_params(self, sale, lines, tryton_context):
|
||||
"""Convierte venta a parámetros para Tryton"""
|
||||
sale_tryton = TrytonSale(sale, lines)
|
||||
return [[sale_tryton.to_tryton()], tryton_context]
|
||||
Reference in New Issue
Block a user