From 62d39c97c0059245dfaf7ba5ca00cdd0892a6f4c Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sat, 19 Jul 2025 10:57:28 -0500 Subject: [PATCH 01/15] #9 feat(Sale): add external_id field. --- .../migrations/0039_sale_external_id.py | 18 ++++++++++++++++++ tienda_ilusion/don_confiao/models.py | 1 + tienda_ilusion/don_confiao/tests/test_api.py | 3 ++- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tienda_ilusion/don_confiao/migrations/0039_sale_external_id.py diff --git a/tienda_ilusion/don_confiao/migrations/0039_sale_external_id.py b/tienda_ilusion/don_confiao/migrations/0039_sale_external_id.py new file mode 100644 index 0000000..f1d8dbd --- /dev/null +++ b/tienda_ilusion/don_confiao/migrations/0039_sale_external_id.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.6 on 2025-07-19 15:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('don_confiao', '0038_alter_saleline_quantity'), + ] + + operations = [ + migrations.AddField( + model_name='sale', + name='external_id', + field=models.CharField(blank=True, max_length=100, null=True), + ), + ] diff --git a/tienda_ilusion/don_confiao/models.py b/tienda_ilusion/don_confiao/models.py index 4b6c2c9..1b30425 100644 --- a/tienda_ilusion/don_confiao/models.py +++ b/tienda_ilusion/don_confiao/models.py @@ -105,6 +105,7 @@ class Sale(models.Model): related_name='Sales', null=True ) + external_id = models.CharField(max_length=100, null=True, blank=True) def __str__(self): return f"{self.date} {self.customer}" diff --git a/tienda_ilusion/don_confiao/tests/test_api.py b/tienda_ilusion/don_confiao/tests/test_api.py index 642bf80..30bd4e0 100644 --- a/tienda_ilusion/don_confiao/tests/test_api.py +++ b/tienda_ilusion/don_confiao/tests/test_api.py @@ -27,12 +27,13 @@ class TestAPI(APITestCase): sale = Sale.objects.all()[0] self.assertEqual( sale.customer.name, - self.customer.name + self.customer.name, ) self.assertEqual( sale.id, content['id'] ) + self.assertIsNone(sale.external_id) def test_create_sale_with_decimal(self): response = self._create_sale_with_decimal() -- 2.45.2 From 46e718165307b26ee0bfde250d9f94e2ef16f76e Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sat, 19 Jul 2025 17:28:47 -0500 Subject: [PATCH 02/15] #9 feat(Sale): add external_id field. --- .../migrations/0040_customer_external_id.py | 18 ++++++++++++++++++ tienda_ilusion/don_confiao/models.py | 1 + tienda_ilusion/don_confiao/tests/test_party.py | 1 + 3 files changed, 20 insertions(+) create mode 100644 tienda_ilusion/don_confiao/migrations/0040_customer_external_id.py diff --git a/tienda_ilusion/don_confiao/migrations/0040_customer_external_id.py b/tienda_ilusion/don_confiao/migrations/0040_customer_external_id.py new file mode 100644 index 0000000..77d36aa --- /dev/null +++ b/tienda_ilusion/don_confiao/migrations/0040_customer_external_id.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.6 on 2025-07-19 22:27 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('don_confiao', '0039_sale_external_id'), + ] + + operations = [ + migrations.AddField( + model_name='customer', + name='external_id', + field=models.CharField(blank=True, max_length=100, null=True), + ), + ] diff --git a/tienda_ilusion/don_confiao/models.py b/tienda_ilusion/don_confiao/models.py index 1b30425..180e3af 100644 --- a/tienda_ilusion/don_confiao/models.py +++ b/tienda_ilusion/don_confiao/models.py @@ -17,6 +17,7 @@ class Customer(models.Model): address = models.CharField(max_length=100, null=True, blank=True) email = models.CharField(max_length=100, null=True, blank=True) phone = models.CharField(max_length=100, null=True, blank=True) + external_id = models.CharField(max_length=100, null=True, blank=True) def __str__(self): return self.name diff --git a/tienda_ilusion/don_confiao/tests/test_party.py b/tienda_ilusion/don_confiao/tests/test_party.py index 25d78e2..72c8a6f 100644 --- a/tienda_ilusion/don_confiao/tests/test_party.py +++ b/tienda_ilusion/don_confiao/tests/test_party.py @@ -14,6 +14,7 @@ class TestCustomer(TestCase): customer.save() self.assertIsInstance(customer, Customer) + self.assertIsNone(customer.external_id) def test_don_create_customer_without_name(self): customer = Customer() -- 2.45.2 From ba9ef039f490913b10c33f993eb038fc0f3059ad Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sat, 19 Jul 2025 17:32:35 -0500 Subject: [PATCH 03/15] #9 feat(Product): add external_id field. --- .../migrations/0041_product_external_id.py | 18 ++++++++++++++++++ tienda_ilusion/don_confiao/models.py | 1 + .../don_confiao/tests/test_products.py | 9 +++++++++ 3 files changed, 28 insertions(+) create mode 100644 tienda_ilusion/don_confiao/migrations/0041_product_external_id.py diff --git a/tienda_ilusion/don_confiao/migrations/0041_product_external_id.py b/tienda_ilusion/don_confiao/migrations/0041_product_external_id.py new file mode 100644 index 0000000..e43910c --- /dev/null +++ b/tienda_ilusion/don_confiao/migrations/0041_product_external_id.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.6 on 2025-07-19 22:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('don_confiao', '0040_customer_external_id'), + ] + + operations = [ + migrations.AddField( + model_name='product', + name='external_id', + field=models.CharField(blank=True, max_length=100, null=True), + ), + ] diff --git a/tienda_ilusion/don_confiao/models.py b/tienda_ilusion/don_confiao/models.py index 180e3af..22dbcef 100644 --- a/tienda_ilusion/don_confiao/models.py +++ b/tienda_ilusion/don_confiao/models.py @@ -43,6 +43,7 @@ class Product(models.Model): default=MeasuringUnits.UNIT ) categories = models.ManyToManyField(ProductCategory) + external_id = models.CharField(max_length=100, null=True, blank=True) def __str__(self): return self.name diff --git a/tienda_ilusion/don_confiao/tests/test_products.py b/tienda_ilusion/don_confiao/tests/test_products.py index 0c12f74..6104a74 100644 --- a/tienda_ilusion/don_confiao/tests/test_products.py +++ b/tienda_ilusion/don_confiao/tests/test_products.py @@ -10,6 +10,15 @@ class TestProducts(TestCase): def setUp(self): self.client = Client() + def test_create_product(self): + product = Product() + product.name = "Un producto" + product.price = 1000 + product.save() + + self.assertIsInstance(product, Product) + self.assertIsNone(product.external_id) + def test_import_products(self): self._import_csv() all_products = self._get_products() -- 2.45.2 From 1d3160ae92e69fdc138aa18c679e16e015de54ed Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sat, 19 Jul 2025 18:17:18 -0500 Subject: [PATCH 04/15] #9 feat(Product): add unit_external_id field. --- .../0042_product_unit_external_id.py | 18 ++++++++++++++++++ tienda_ilusion/don_confiao/models.py | 1 + .../don_confiao/tests/test_products.py | 1 + 3 files changed, 20 insertions(+) create mode 100644 tienda_ilusion/don_confiao/migrations/0042_product_unit_external_id.py diff --git a/tienda_ilusion/don_confiao/migrations/0042_product_unit_external_id.py b/tienda_ilusion/don_confiao/migrations/0042_product_unit_external_id.py new file mode 100644 index 0000000..768d0ad --- /dev/null +++ b/tienda_ilusion/don_confiao/migrations/0042_product_unit_external_id.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.6 on 2025-07-19 23:15 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('don_confiao', '0041_product_external_id'), + ] + + operations = [ + migrations.AddField( + model_name='product', + name='unit_external_id', + field=models.CharField(blank=True, max_length=100, null=True), + ), + ] diff --git a/tienda_ilusion/don_confiao/models.py b/tienda_ilusion/don_confiao/models.py index 22dbcef..8fb52c0 100644 --- a/tienda_ilusion/don_confiao/models.py +++ b/tienda_ilusion/don_confiao/models.py @@ -42,6 +42,7 @@ class Product(models.Model): choices=MeasuringUnits.choices, default=MeasuringUnits.UNIT ) + unit_external_id = models.CharField(max_length=100, null=True, blank=True) categories = models.ManyToManyField(ProductCategory) external_id = models.CharField(max_length=100, null=True, blank=True) diff --git a/tienda_ilusion/don_confiao/tests/test_products.py b/tienda_ilusion/don_confiao/tests/test_products.py index 6104a74..1330fd0 100644 --- a/tienda_ilusion/don_confiao/tests/test_products.py +++ b/tienda_ilusion/don_confiao/tests/test_products.py @@ -18,6 +18,7 @@ class TestProducts(TestCase): self.assertIsInstance(product, Product) self.assertIsNone(product.external_id) + self.assertIsNone(product.unit_external_id) def test_import_products(self): self._import_csv() -- 2.45.2 From cf0f6dc4b5c6827cb70ec4b3a2300a7d6ee49eee Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sat, 19 Jul 2025 19:00:33 -0500 Subject: [PATCH 05/15] #9 feat(Tryton): enviar_ventas_a_tryton --- requirements.txt | 1 + tienda_ilusion/don_confiao/api_views.py | 77 +++++++++++++++++++ .../tests/test_exportar_ventas_para_tryton.py | 33 +++++++- tienda_ilusion/don_confiao/urls.py | 1 + 4 files changed, 110 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index e7bbb4d..360a414 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ Django==5.0.6 djangorestframework django-cors-headers +sabatron-tryton-rpc-client==7.4.0 diff --git a/tienda_ilusion/don_confiao/api_views.py b/tienda_ilusion/don_confiao/api_views.py index 23dc12e..9059957 100644 --- a/tienda_ilusion/don_confiao/api_views.py +++ b/tienda_ilusion/don_confiao/api_views.py @@ -9,6 +9,7 @@ from .serializers import SaleSerializer, ProductSerializer, CustomerSerializer, from .views import sales_to_tryton_csv from decimal import Decimal +from sabatron_tryton_rpc_client.client import Client import io import csv @@ -164,3 +165,79 @@ class SalesForTrytonView(APIView): for row in csv_data: writer.writerow(row) return output.getvalue() + + +class SalesToTrytonView(APIView): + def post(self, request): + tryton_client = Client( + hostname='localhost', + database='tryton', + username='admin', + password='admin' + ) + tryton_client.connect() + method = 'model.sale.sale.create' + tryton_context = {} + + successful = [] + failed = [] + + sales = Sale.objects.filter(external_id=None) + for sale in sales: + lines = SaleLine.objects.filter(sale=sale.id) + tryton_params = self.__to_tryton_params(sale, lines, tryton_context) + external_ids = tryton_client.call(method, tryton_params) + sale.external_id = external_ids[0] + sale.save() + successful.append(sale.id) + + return Response( + {'successful': successful, 'failed': failed}, + status=200 + ) + + def __to_tryton_params(self, sale, lines, tryton_context): + sale_tryton = TrytonSale(sale, lines) + return [[sale_tryton.to_tryton()], tryton_context] + + +class TrytonSale: + + 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": 1, + "currency": 1, + "description": self.sale.description 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] + ]] + } + + +class TrytonLineSale: + 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) + } diff --git a/tienda_ilusion/don_confiao/tests/test_exportar_ventas_para_tryton.py b/tienda_ilusion/don_confiao/tests/test_exportar_ventas_para_tryton.py index dd6a5de..dde3a9b 100644 --- a/tienda_ilusion/don_confiao/tests/test_exportar_ventas_para_tryton.py +++ b/tienda_ilusion/don_confiao/tests/test_exportar_ventas_para_tryton.py @@ -1,4 +1,6 @@ import csv +import json +from unittest.mock import patch from django.test import TestCase, Client from django.urls import reverse @@ -10,10 +12,13 @@ class TestExportarVentasParaTryton(TestCase): self.product = Product.objects.create( name='Panela', price=5000, - measuring_unit='UNIT' + measuring_unit='UNIT', + unit_external_id=1, + external_id=1 ) self.customer = Customer.objects.create( - name='Camilo' + name='Camilo', + external_id=1 ) self.sale = Sale.objects.create( customer=self.customer, @@ -71,3 +76,27 @@ class TestExportarVentasParaTryton(TestCase): csv_rows = list(csv_reader) self.assertEqual(csv_rows[0], expected_rows[0]) self.assertEqual(csv_rows[1], expected_rows[1]) + + @patch('sabatron_tryton_rpc_client.client.Client.call') + @patch('sabatron_tryton_rpc_client.client.Client.connect') + def test_send_sales_to_tryton(self, mock_connect, mock_call): + client = Client() + external_id = '23423' + url = '/don_confiao/api/enviar_ventas_a_tryton' + mock_connect.return_value = None + mock_call.return_value = [external_id] + response = client.post(url) + + self.assertEqual(response.status_code, 200) + content = json.loads(response.content.decode('utf-8')) + expected_response = { + 'successful': [self.sale.id], + 'failed': [], + } + self.assertEqual(content, expected_response) + + updated_sale = Sale.objects.get(id=self.sale.id) + self.assertEqual(updated_sale.external_id, external_id) + mock_connect.assert_called_once() + mock_call.assert_called_once() + mock_call.assert_called_with('model.sale.sale.create', [[{'company': 1, 'currency': 1, 'description': '', 'party': '1', 'reference': 'don_confiao 1', 'sale_date': {'__class__': 'date', 'year': 2024, 'month': 9, 'day': 2}, 'lines': [['create', [{'product': '1', 'quantity': {'__class__': 'Decimal', 'decimal': '2.00'}, 'type': 'line', 'unit': '1', 'unit_price': {'__class__': 'Decimal', 'decimal': '3000.00'}}, {'product': '1', 'quantity': {'__class__': 'Decimal', 'decimal': '3.00'}, 'type': 'line', 'unit': '1', 'unit_price': {'__class__': 'Decimal', 'decimal': '5000.00'}}]]]}], {}]) diff --git a/tienda_ilusion/don_confiao/urls.py b/tienda_ilusion/don_confiao/urls.py index dc731b7..9098660 100644 --- a/tienda_ilusion/don_confiao/urls.py +++ b/tienda_ilusion/don_confiao/urls.py @@ -24,6 +24,7 @@ urlpatterns = [ path("exportar_ventas_para_tryton", views.exportar_ventas_para_tryton, name="exportar_ventas_para_tryton"), + path('api/enviar_ventas_a_tryton', api_views.SalesToTrytonView.as_view(), name="send_tryton"), path("resumen_compra/", views.purchase_summary, name="purchase_summary"), path("resumen_compra_json/", api_views.SaleSummary.as_view(), name="purchase_json_summary"), path("payment_methods/all/select_format", api_views.PaymentMethodView.as_view(), name="payment_methods_to_select"), -- 2.45.2 From 81e4c0bc0da3a318ce0af833d2e576e5dc62ee28 Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sat, 19 Jul 2025 19:04:58 -0500 Subject: [PATCH 06/15] #9 feat(Customer): add address_external_id field. --- .../0043_customer_address_external_id.py | 18 ++++++++++++++++++ tienda_ilusion/don_confiao/models.py | 1 + tienda_ilusion/don_confiao/tests/test_party.py | 1 + 3 files changed, 20 insertions(+) create mode 100644 tienda_ilusion/don_confiao/migrations/0043_customer_address_external_id.py diff --git a/tienda_ilusion/don_confiao/migrations/0043_customer_address_external_id.py b/tienda_ilusion/don_confiao/migrations/0043_customer_address_external_id.py new file mode 100644 index 0000000..b36b319 --- /dev/null +++ b/tienda_ilusion/don_confiao/migrations/0043_customer_address_external_id.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.6 on 2025-07-20 00:04 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('don_confiao', '0042_product_unit_external_id'), + ] + + operations = [ + migrations.AddField( + model_name='customer', + name='address_external_id', + field=models.CharField(blank=True, max_length=100, null=True), + ), + ] diff --git a/tienda_ilusion/don_confiao/models.py b/tienda_ilusion/don_confiao/models.py index 8fb52c0..a815b1e 100644 --- a/tienda_ilusion/don_confiao/models.py +++ b/tienda_ilusion/don_confiao/models.py @@ -18,6 +18,7 @@ class Customer(models.Model): email = models.CharField(max_length=100, null=True, blank=True) phone = models.CharField(max_length=100, null=True, blank=True) external_id = models.CharField(max_length=100, null=True, blank=True) + address_external_id = models.CharField(max_length=100, null=True, blank=True) def __str__(self): return self.name diff --git a/tienda_ilusion/don_confiao/tests/test_party.py b/tienda_ilusion/don_confiao/tests/test_party.py index 72c8a6f..4e1da29 100644 --- a/tienda_ilusion/don_confiao/tests/test_party.py +++ b/tienda_ilusion/don_confiao/tests/test_party.py @@ -15,6 +15,7 @@ class TestCustomer(TestCase): self.assertIsInstance(customer, Customer) self.assertIsNone(customer.external_id) + self.assertIsNone(customer.address_external_id) def test_don_create_customer_without_name(self): customer = Customer() -- 2.45.2 From 3d9feeac4341caa9bbde24f500140deca1a64515 Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sat, 19 Jul 2025 19:09:53 -0500 Subject: [PATCH 07/15] #9 feat(Tryton): add address_external_id to tryton. --- tienda_ilusion/don_confiao/api_views.py | 2 ++ .../don_confiao/tests/test_exportar_ventas_para_tryton.py | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tienda_ilusion/don_confiao/api_views.py b/tienda_ilusion/don_confiao/api_views.py index 9059957..c58229a 100644 --- a/tienda_ilusion/don_confiao/api_views.py +++ b/tienda_ilusion/don_confiao/api_views.py @@ -214,6 +214,8 @@ class TrytonSale: def to_tryton(self): return { "company": 1, + "shipment_address": self.sale.customer.address_external_id, + "invoice_address": self.sale.customer.address_external_id, "currency": 1, "description": self.sale.description or '', "party": self.sale.customer.external_id, diff --git a/tienda_ilusion/don_confiao/tests/test_exportar_ventas_para_tryton.py b/tienda_ilusion/don_confiao/tests/test_exportar_ventas_para_tryton.py index dde3a9b..c05065d 100644 --- a/tienda_ilusion/don_confiao/tests/test_exportar_ventas_para_tryton.py +++ b/tienda_ilusion/don_confiao/tests/test_exportar_ventas_para_tryton.py @@ -18,7 +18,8 @@ class TestExportarVentasParaTryton(TestCase): ) self.customer = Customer.objects.create( name='Camilo', - external_id=1 + external_id=1, + address_external_id=1 ) self.sale = Sale.objects.create( customer=self.customer, @@ -99,4 +100,4 @@ class TestExportarVentasParaTryton(TestCase): self.assertEqual(updated_sale.external_id, external_id) mock_connect.assert_called_once() mock_call.assert_called_once() - mock_call.assert_called_with('model.sale.sale.create', [[{'company': 1, 'currency': 1, 'description': '', 'party': '1', 'reference': 'don_confiao 1', 'sale_date': {'__class__': 'date', 'year': 2024, 'month': 9, 'day': 2}, 'lines': [['create', [{'product': '1', 'quantity': {'__class__': 'Decimal', 'decimal': '2.00'}, 'type': 'line', 'unit': '1', 'unit_price': {'__class__': 'Decimal', 'decimal': '3000.00'}}, {'product': '1', 'quantity': {'__class__': 'Decimal', 'decimal': '3.00'}, 'type': 'line', 'unit': '1', 'unit_price': {'__class__': 'Decimal', 'decimal': '5000.00'}}]]]}], {}]) + mock_call.assert_called_with('model.sale.sale.create', [[{'company': 1, 'shipment_address': '1', 'invoice_address': '1', 'currency': 1, 'description': '', 'party': '1', 'reference': 'don_confiao 1', 'sale_date': {'__class__': 'date', 'year': 2024, 'month': 9, 'day': 2}, 'lines': [['create', [{'product': '1', 'quantity': {'__class__': 'Decimal', 'decimal': '2.00'}, 'type': 'line', 'unit': '1', 'unit_price': {'__class__': 'Decimal', 'decimal': '3000.00'}}, {'product': '1', 'quantity': {'__class__': 'Decimal', 'decimal': '3.00'}, 'type': 'line', 'unit': '1', 'unit_price': {'__class__': 'Decimal', 'decimal': '5000.00'}}]]]}], {}]) -- 2.45.2 From 3a5e13624fc9990b67594c4f58317e01d998fe50 Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sun, 20 Jul 2025 22:01:34 -0500 Subject: [PATCH 08/15] #9 config(Tryton): read tryton params from environment. --- tienda_ilusion/don_confiao/api_views.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tienda_ilusion/don_confiao/api_views.py b/tienda_ilusion/don_confiao/api_views.py index c58229a..c1f23fb 100644 --- a/tienda_ilusion/don_confiao/api_views.py +++ b/tienda_ilusion/don_confiao/api_views.py @@ -12,6 +12,12 @@ from decimal import Decimal from sabatron_tryton_rpc_client.client import Client import io import csv +import os + +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') class Pagination(PageNumberPagination): @@ -170,10 +176,10 @@ class SalesForTrytonView(APIView): class SalesToTrytonView(APIView): def post(self, request): tryton_client = Client( - hostname='localhost', - database='tryton', - username='admin', - password='admin' + hostname=TRYTON_HOST, + database=TRYTON_DATABASE, + username=TRYTON_USERNAME, + password=TRYTON_PASSWORD ) tryton_client.connect() method = 'model.sale.sale.create' -- 2.45.2 From 76e525735dea181d847a834bc157e37bfa980a6d Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sun, 27 Jul 2025 22:57:44 -0500 Subject: [PATCH 09/15] #9 feat(Tryton): create products from tryton. --- tienda_ilusion/don_confiao/api_views.py | 74 +++++++++++++++++++ .../tests/test_products_from_tryton.py | 57 ++++++++++++++ tienda_ilusion/don_confiao/urls.py | 3 + 3 files changed, 134 insertions(+) create mode 100644 tienda_ilusion/don_confiao/tests/test_products_from_tryton.py diff --git a/tienda_ilusion/don_confiao/api_views.py b/tienda_ilusion/don_confiao/api_views.py index c1f23fb..2b6a24e 100644 --- a/tienda_ilusion/don_confiao/api_views.py +++ b/tienda_ilusion/don_confiao/api_views.py @@ -249,3 +249,77 @@ class TrytonLineSale: "unit": self.sale_line.product.unit_external_id, "unit_price": self._format_decimal(self.sale_line.unit_price) } + + +class ProductsFromTrytonView(APIView): + def post(self, request): + tryton_client = Client( + hostname=TRYTON_HOST, + database=TRYTON_DATABASE, + username=TRYTON_USERNAME, + password=TRYTON_PASSWORD + ) + tryton_client.connect() + method = 'model.product.product.search' + context = {'company': 1} + params = [[], 0, 1000, [["rec_name", "ASC"], ["id", None]], context] + product_ids = tryton_client.call(method, params) + tryton_products = self.__get_product_datails_from_tryton( + product_ids, tryton_client, 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: + product = self.__create_product(tryton_product) + created_products.append(product.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 Response( + { + 'checked_tryton_products': checked_tryton_products, + 'failed_products': failed_products, + 'updated_products': updated_products, + 'created_products': created_products, + 'untouched_products': untouched_products, + }, + status=200 + ) + + def __get_product_datails_from_tryton(self, product_ids, tryton_client, context): + 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 = tryton_client.call(method, params) + return response + + def __need_update(self, product, tryton_product): + if not product.name == tryton_product.get('name'): + return True + if not product.price == tryton_product.get('price'): + return True + + def __create_product(self, tryton_product): + 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 diff --git a/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py b/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py new file mode 100644 index 0000000..529b19f --- /dev/null +++ b/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py @@ -0,0 +1,57 @@ +import json +from decimal import Decimal +from unittest.mock import patch + +from django.test import Client, TestCase +from ..models import ProductCategory, Product + + +class TestProductsFromTryton(TestCase): + def setUp(self): + self.client = Client() + + @patch('sabatron_tryton_rpc_client.client.Client.call') + @patch('sabatron_tryton_rpc_client.client.Client.connect') + def test_import_products(self, mock_connect, mock_call): + mock_connect.return_value = None + + def fake_call(*args, **kwargs): + product_search = 'model.product.product.search' + search_args = [[], 0, 1000, [['rec_name', 'ASC'], ['id', None]], {'company': 1}] + if (args == (product_search, search_args)): + return [190] + + product_read = 'model.product.product.read' + product_args = ([190], + ['id', 'name', 'default_uom.id', + 'default_uom.rec_name', 'list_price'], + {'company': 1} + ) + if (args == (product_read, product_args)): + return [{'id': 190, 'list_price': Decimal('25000'), + 'name': 'Producto 1', + 'default_uom.': {'id': 1, 'rec_name': 'Unit'}}] + + raise Exception(f"Sorry, args non expected on this test: {args}") + + mock_call.side_effect = fake_call + + url = '/don_confiao/api/importar_productos_de_tryton' + response = self.client.post(url) + self.assertEqual(response.status_code, 200) + + content = json.loads(response.content.decode('utf-8')) + expected_response = { + 'checked_tryton_products': [190], + 'created_products': [1], + 'untouched_products': [], + 'failed_products': [], + 'updated_products': [] + } + self.assertEqual(content, expected_response) + + created_product = Product.objects.get(id=1) + self.assertEqual(created_product.external_id, str(190)) + self.assertEqual(created_product.name, 'Producto 1') + self.assertEqual(created_product.price, Decimal('25000')) + self.assertEqual(created_product.measuring_unit, 'Unit') diff --git a/tienda_ilusion/don_confiao/urls.py b/tienda_ilusion/don_confiao/urls.py index 9098660..cd7e062 100644 --- a/tienda_ilusion/don_confiao/urls.py +++ b/tienda_ilusion/don_confiao/urls.py @@ -20,6 +20,9 @@ urlpatterns = [ path("productos", views.products, name="products"), path("lista_productos", views.ProductListView.as_view(), name='product_list'), path("importar_productos", views.import_products, name="import_products"), + path('api/importar_productos_de_tryton', + api_views.ProductsFromTrytonView.as_view(), + name="products_from_tryton"), path("importar_terceros", views.import_customers, name="import_customers"), path("exportar_ventas_para_tryton", views.exportar_ventas_para_tryton, -- 2.45.2 From c33c6f630a21b0ea3317394b3fdad126258ca194 Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sun, 27 Jul 2025 23:11:34 -0500 Subject: [PATCH 10/15] #9 feat(Tryton): update products from tryton. --- tienda_ilusion/don_confiao/api_views.py | 14 ++++++- .../tests/test_products_from_tryton.py | 40 ++++++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/tienda_ilusion/don_confiao/api_views.py b/tienda_ilusion/don_confiao/api_views.py index 2b6a24e..54f4295 100644 --- a/tienda_ilusion/don_confiao/api_views.py +++ b/tienda_ilusion/don_confiao/api_views.py @@ -283,7 +283,7 @@ class ProductsFromTrytonView(APIView): created_products.append(product.id) continue if self.__need_update(product, tryton_product): - self.update_product(product, tryton_product) + self.__update_product(product, tryton_product) updated_products.append(product.id) else: untouched_products.append(product.id) @@ -312,6 +312,9 @@ class ProductsFromTrytonView(APIView): return True if not product.price == tryton_product.get('price'): return True + unit = tryton_product.get('default_uom.') + if not product.measuring_unit == unit.get('rec_name'): + return True def __create_product(self, tryton_product): product = Product() @@ -323,3 +326,12 @@ class ProductsFromTrytonView(APIView): product.unit_external_id = unit.get('id') product.save() return product + + def __update_product(self, product, tryton_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() diff --git a/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py b/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py index 529b19f..518bfbf 100644 --- a/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py +++ b/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py @@ -8,29 +8,41 @@ from ..models import ProductCategory, Product class TestProductsFromTryton(TestCase): def setUp(self): - self.client = Client() + self.product = Product.objects.create( + name='Panela', + price=5000, + measuring_unit='UNIT', + unit_external_id=1, + external_id=191 + ) + self.product.save() @patch('sabatron_tryton_rpc_client.client.Client.call') @patch('sabatron_tryton_rpc_client.client.Client.connect') - def test_import_products(self, mock_connect, mock_call): + def test_create_import_products(self, mock_connect, mock_call): mock_connect.return_value = None def fake_call(*args, **kwargs): product_search = 'model.product.product.search' search_args = [[], 0, 1000, [['rec_name', 'ASC'], ['id', None]], {'company': 1}] if (args == (product_search, search_args)): - return [190] + return [190, 191] product_read = 'model.product.product.read' - product_args = ([190], + product_args = ([190, 191], ['id', 'name', 'default_uom.id', 'default_uom.rec_name', 'list_price'], {'company': 1} ) if (args == (product_read, product_args)): - return [{'id': 190, 'list_price': Decimal('25000'), - 'name': 'Producto 1', - 'default_uom.': {'id': 1, 'rec_name': 'Unit'}}] + return [ + {'id': 190, 'list_price': Decimal('25000'), + 'name': 'Producto 1', + 'default_uom.': {'id': 1, 'rec_name': 'Unit'}}, + {'id': 191, 'list_price': Decimal('6000'), + 'name': 'Panela2', + 'default_uom.': {'id': 1, 'rec_name': 'Unit'}}, + ] raise Exception(f"Sorry, args non expected on this test: {args}") @@ -42,16 +54,22 @@ class TestProductsFromTryton(TestCase): content = json.loads(response.content.decode('utf-8')) expected_response = { - 'checked_tryton_products': [190], - 'created_products': [1], + 'checked_tryton_products': [190, 191], + 'created_products': [2], 'untouched_products': [], 'failed_products': [], - 'updated_products': [] + 'updated_products': [1] } self.assertEqual(content, expected_response) - created_product = Product.objects.get(id=1) + created_product = Product.objects.get(id=2) self.assertEqual(created_product.external_id, str(190)) self.assertEqual(created_product.name, 'Producto 1') self.assertEqual(created_product.price, Decimal('25000')) self.assertEqual(created_product.measuring_unit, 'Unit') + + updated_product = Product.objects.get(id=1) + self.assertEqual(updated_product.external_id, str(191)) + self.assertEqual(updated_product.name, 'Panela2') + self.assertEqual(updated_product.price, Decimal('6000')) + self.assertEqual(updated_product.measuring_unit, 'Unit') -- 2.45.2 From d1e137d387bd0a447e56f9e3ce62b3480567332b Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sun, 27 Jul 2025 23:22:58 -0500 Subject: [PATCH 11/15] #9 feat(Tryton): untouched products on sync from tryton. --- tienda_ilusion/don_confiao/api_views.py | 2 +- .../tests/test_products_from_tryton.py | 24 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/tienda_ilusion/don_confiao/api_views.py b/tienda_ilusion/don_confiao/api_views.py index 54f4295..3de6f63 100644 --- a/tienda_ilusion/don_confiao/api_views.py +++ b/tienda_ilusion/don_confiao/api_views.py @@ -310,7 +310,7 @@ class ProductsFromTrytonView(APIView): def __need_update(self, product, tryton_product): if not product.name == tryton_product.get('name'): return True - if not product.price == tryton_product.get('price'): + 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'): diff --git a/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py b/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py index 518bfbf..37f8419 100644 --- a/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py +++ b/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py @@ -17,6 +17,15 @@ class TestProductsFromTryton(TestCase): ) self.product.save() + self.product2 = Product.objects.create( + name='Papa', + price=4500, + measuring_unit='Kilogram', + unit_external_id=2, + external_id=192 + ) + self.product2.save() + @patch('sabatron_tryton_rpc_client.client.Client.call') @patch('sabatron_tryton_rpc_client.client.Client.connect') def test_create_import_products(self, mock_connect, mock_call): @@ -26,10 +35,10 @@ class TestProductsFromTryton(TestCase): product_search = 'model.product.product.search' search_args = [[], 0, 1000, [['rec_name', 'ASC'], ['id', None]], {'company': 1}] if (args == (product_search, search_args)): - return [190, 191] + return [190, 191, 192] product_read = 'model.product.product.read' - product_args = ([190, 191], + product_args = ([190, 191, 192], ['id', 'name', 'default_uom.id', 'default_uom.rec_name', 'list_price'], {'company': 1} @@ -42,6 +51,9 @@ class TestProductsFromTryton(TestCase): {'id': 191, 'list_price': Decimal('6000'), 'name': 'Panela2', 'default_uom.': {'id': 1, 'rec_name': 'Unit'}}, + {'id': 192, 'list_price': Decimal('4500'), + 'name': 'Papa', + 'default_uom.': {'id': 2, 'rec_name': 'Kilogram'}}, ] raise Exception(f"Sorry, args non expected on this test: {args}") @@ -54,15 +66,15 @@ class TestProductsFromTryton(TestCase): content = json.loads(response.content.decode('utf-8')) expected_response = { - 'checked_tryton_products': [190, 191], - 'created_products': [2], - 'untouched_products': [], + 'checked_tryton_products': [190, 191, 192], + 'created_products': [3], + 'untouched_products': [2], 'failed_products': [], 'updated_products': [1] } self.assertEqual(content, expected_response) - created_product = Product.objects.get(id=2) + created_product = Product.objects.get(id=3) self.assertEqual(created_product.external_id, str(190)) self.assertEqual(created_product.name, 'Producto 1') self.assertEqual(created_product.price, Decimal('25000')) -- 2.45.2 From 59fbc8872afae2ed8f3cf69c3d9cfc03905693eb Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sun, 27 Jul 2025 23:34:07 -0500 Subject: [PATCH 12/15] #9 feat(Tryton): get products salables from tryton only. --- tienda_ilusion/don_confiao/api_views.py | 2 +- tienda_ilusion/don_confiao/tests/test_products_from_tryton.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tienda_ilusion/don_confiao/api_views.py b/tienda_ilusion/don_confiao/api_views.py index 3de6f63..e67fd80 100644 --- a/tienda_ilusion/don_confiao/api_views.py +++ b/tienda_ilusion/don_confiao/api_views.py @@ -262,7 +262,7 @@ class ProductsFromTrytonView(APIView): tryton_client.connect() method = 'model.product.product.search' context = {'company': 1} - params = [[], 0, 1000, [["rec_name", "ASC"], ["id", None]], context] + params = [[["salable", "=", True]], 0, 1000, [["rec_name", "ASC"], ["id", None]], context] product_ids = tryton_client.call(method, params) tryton_products = self.__get_product_datails_from_tryton( product_ids, tryton_client, context diff --git a/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py b/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py index 37f8419..92620a1 100644 --- a/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py +++ b/tienda_ilusion/don_confiao/tests/test_products_from_tryton.py @@ -33,7 +33,7 @@ class TestProductsFromTryton(TestCase): def fake_call(*args, **kwargs): product_search = 'model.product.product.search' - search_args = [[], 0, 1000, [['rec_name', 'ASC'], ['id', None]], {'company': 1}] + search_args = [[["salable", "=", True]], 0, 1000, [['rec_name', 'ASC'], ['id', None]], {'company': 1}] if (args == (product_search, search_args)): return [190, 191, 192] -- 2.45.2 From 4734636b4f0dced090391510d23f6195be2cba23 Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sat, 9 Aug 2025 14:12:31 -0500 Subject: [PATCH 13/15] feat (Tryton): get customers from tryton. --- tienda_ilusion/don_confiao/api_views.py | 74 +++++++++++++++++++ .../tests/test_customers_from_tryton.py | 66 +++++++++++++++++ tienda_ilusion/don_confiao/urls.py | 3 + 3 files changed, 143 insertions(+) create mode 100644 tienda_ilusion/don_confiao/tests/test_customers_from_tryton.py diff --git a/tienda_ilusion/don_confiao/api_views.py b/tienda_ilusion/don_confiao/api_views.py index e67fd80..3f02592 100644 --- a/tienda_ilusion/don_confiao/api_views.py +++ b/tienda_ilusion/don_confiao/api_views.py @@ -335,3 +335,77 @@ class ProductsFromTrytonView(APIView): product.measuring_unit = unit.get('rec_name') product.unit_external_id = unit.get('id') product.save() + + +class CustomersFromTrytonView(APIView): + def post(self, request): + tryton_client = Client( + hostname=TRYTON_HOST, + database=TRYTON_DATABASE, + username=TRYTON_USERNAME, + password=TRYTON_PASSWORD + ) + tryton_client.connect() + method = 'model.party.party.search' + context = {'company': 1} + params = [[], 0, 1000, [["rec_name", "ASC"], ["id", None]], context] + party_ids = tryton_client.call(method, params) + tryton_parties = self.__get_party_datails( + party_ids, tryton_client, context + ) + checked_tryton_parties = party_ids + failed_parties = [] + updated_customers = [] + created_customers = [] + untouched_customers = [] + print('aqui') + print(tryton_parties) + + 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 Response( + { + 'checked_tryton_parties': checked_tryton_parties, + 'failed_parties': failed_parties, + 'updated_customers': updated_customers, + 'created_customers': created_customers, + 'untouched_customers': untouched_customers, + }, + status=200 + ) + + def __get_party_datails(self, party_ids, tryton_client, context): + tryton_fields = ['id', 'name'] + method = 'model.party.party.read' + params = (party_ids, tryton_fields, context) + response = tryton_client.call(method, params) + return response + + def __need_update(self, customer, tryton_party): + if not customer.name == tryton_party.get('name'): + return True + + def __create_customer(self, tryton_party): + customer = Customer() + customer.name = tryton_party.get('name') + customer.external_id = tryton_party.get('id') + customer.save() + return customer + + def __update_customer(self, customer, tryton_party): + customer.name = tryton_party.get('name') + customer.external_id = tryton_party.get('id') + customer.save() diff --git a/tienda_ilusion/don_confiao/tests/test_customers_from_tryton.py b/tienda_ilusion/don_confiao/tests/test_customers_from_tryton.py new file mode 100644 index 0000000..a499fc2 --- /dev/null +++ b/tienda_ilusion/don_confiao/tests/test_customers_from_tryton.py @@ -0,0 +1,66 @@ +import json +from unittest.mock import patch + +from django.test import Client, TestCase +from ..models import Customer + + +class TestCustomersFromTryton(TestCase): + def setUp(self): + self.customer = Customer.objects.create( + name='Calos', + external_id=5 + ) + self.customer.save() + + self.customer2 = Customer.objects.create( + name='Cristian', + external_id=6 + ) + self.customer2.save() + + @patch('sabatron_tryton_rpc_client.client.Client.call') + @patch('sabatron_tryton_rpc_client.client.Client.connect') + def test_create_import_customer(self, mock_connect, mock_call): + def fake_call(*args, **kwargs): + party_search = 'model.party.party.search' + search_args = [[], 0, 1000, [['rec_name', 'ASC'], ['id', None]], {'company': 1}] + + if (args == (party_search, search_args)): + return [5, 6, 7, 8] + + party_read = 'model.party.party.read' + read_args = ([5, 6, 7, 8], ['id', 'name'], {'company': 1}) + if (args == (party_read, read_args)): + return [ + {'id': 5, 'name': 'Carlos'}, + {'id': 6, 'name': 'Cristian'}, + {'id': 7, 'name': 'Ana'}, + {'id': 8, 'name': 'José'}, + ] + + raise Exception(f"Sorry, args non expected on this test: {args}") + mock_call.side_effect = fake_call + + url = '/don_confiao/api/importar_clientes_de_tryton' + response = self.client.post(url) + self.assertEqual(response.status_code, 200) + + + content = json.loads(response.content.decode('utf-8')) + expected_response = { + 'checked_tryton_parties': [5, 6, 7, 8], + 'created_customers': [3, 4], + 'untouched_customers': [2], + 'failed_parties': [], + 'updated_customers': [1] + } + self.assertEqual(content, expected_response) + + created_customer = Customer.objects.get(id=3) + self.assertEqual(created_customer.external_id, str(7)) + self.assertEqual(created_customer.name, 'Ana') + + updated_customer = Customer.objects.get(id=1) + self.assertEqual(updated_customer.external_id, str(5)) + self.assertEqual(updated_customer.name, 'Carlos') diff --git a/tienda_ilusion/don_confiao/urls.py b/tienda_ilusion/don_confiao/urls.py index cd7e062..b2ebf87 100644 --- a/tienda_ilusion/don_confiao/urls.py +++ b/tienda_ilusion/don_confiao/urls.py @@ -24,6 +24,9 @@ urlpatterns = [ api_views.ProductsFromTrytonView.as_view(), name="products_from_tryton"), path("importar_terceros", views.import_customers, name="import_customers"), + path('api/importar_clientes_de_tryton', + api_views.CustomersFromTrytonView.as_view(), + name="customers_from_tryton"), path("exportar_ventas_para_tryton", views.exportar_ventas_para_tryton, name="exportar_ventas_para_tryton"), -- 2.45.2 From 271f9b294272b046e81bd1c49a66d5009a972425 Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sat, 9 Aug 2025 15:27:17 -0500 Subject: [PATCH 14/15] fix(Tryton): fix import customers from tryton. --- tienda_ilusion/don_confiao/api_views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tienda_ilusion/don_confiao/api_views.py b/tienda_ilusion/don_confiao/api_views.py index 3f02592..ed8e958 100644 --- a/tienda_ilusion/don_confiao/api_views.py +++ b/tienda_ilusion/don_confiao/api_views.py @@ -348,7 +348,7 @@ class CustomersFromTrytonView(APIView): tryton_client.connect() method = 'model.party.party.search' context = {'company': 1} - params = [[], 0, 1000, [["rec_name", "ASC"], ["id", None]], context] + params = [[], 0, 1000, [["name", "ASC"], ["id", None]], context] party_ids = tryton_client.call(method, params) tryton_parties = self.__get_party_datails( party_ids, tryton_client, context -- 2.45.2 From 1b30076876efb1fe4446fe6ffece241ad245909e Mon Sep 17 00:00:00 2001 From: Mono Mono Date: Sat, 9 Aug 2025 15:27:39 -0500 Subject: [PATCH 15/15] environment: add .env to docker-compose. --- docker-compose.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 5a2a9bb..b8623e8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,8 @@ services: build: context: ./ dockerfile: django.Dockerfile + env_file: + - .env volumes: - ./tienda_ilusion:/app/ ports: -- 2.45.2