Compare commits
60 Commits
0.1.5
...
feature/37
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
079f4e3806 | ||
|
|
6473a8348c | ||
| 1d7beadf96 | |||
|
|
5442a52ff6 | ||
| b852772c76 | |||
|
|
0d5a34d366 | ||
|
|
7e2c03c81b | ||
|
|
83029afd5b | ||
|
|
2ab328b913 | ||
| a05061c14e | |||
| 7c0047b4d3 | |||
| c021104b62 | |||
| 7a9034943a | |||
| 4812160ea2 | |||
| fb4c82a94c | |||
| fb3124246c | |||
| f3d3681bc4 | |||
| e6d2160d2e | |||
| f323873d80 | |||
| b730d24855 | |||
| 6261d64206 | |||
| 64f07a2ce2 | |||
| 308e2d08c1 | |||
| e1ff427856 | |||
| bf70c47551 | |||
| f02f754ae7 | |||
| 1668a37091 | |||
| b33937d4a5 | |||
| a265b94460 | |||
| 253fcbae27 | |||
| d127609508 | |||
| 604bbd3ab9 | |||
| e17b8f6973 | |||
| e3f571afc5 | |||
| 477405a094 | |||
| 4dae669397 | |||
| 937fe06de4 | |||
| 69185f2460 | |||
| 7ac28154eb | |||
| e7eda79c69 | |||
| 5f40b4098c | |||
|
|
80864137b6 | ||
|
|
2e8e956b69 | ||
| 2e4c6592a3 | |||
| 6b149b0134 | |||
| 1b30076876 | |||
| 271f9b2942 | |||
| 4734636b4f | |||
| 59fbc8872a | |||
| d1e137d387 | |||
| c33c6f630a | |||
| 76e525735d | |||
| 3a5e13624f | |||
| 3d9feeac43 | |||
| 81e4c0bc0d | |||
| cf0f6dc4b5 | |||
| 1d3160ae92 | |||
| ba9ef039f4 | |||
| 46e7181653 | |||
| 62d39c97c0 |
17
.dockerignore
Normal file
17
.dockerignore
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
*.so
|
||||||
|
.Python
|
||||||
|
*.db
|
||||||
|
*.sqlite3
|
||||||
|
.env
|
||||||
|
.venv
|
||||||
|
venv/
|
||||||
|
ENV/
|
||||||
|
.git/
|
||||||
|
*.egg-info/
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
.coverage
|
||||||
|
htmlcov/
|
||||||
4
.env_example
Normal file
4
.env_example
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
TRYTON_HOST=localhost
|
||||||
|
TRYTON_DATABASE=tryton
|
||||||
|
TRYTON_USERNAME=admin
|
||||||
|
TRYTON_PASSWORD=admin
|
||||||
108
AGENTS.md
Normal file
108
AGENTS.md
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
# Don Confiao Backend - Contexto del Proyecto
|
||||||
|
|
||||||
|
## Tipo de Proyecto
|
||||||
|
Backend Django con Django REST Framework
|
||||||
|
|
||||||
|
## Estructura del Proyecto
|
||||||
|
```
|
||||||
|
don_confiao_backend/
|
||||||
|
├── requirements.txt # Dependencias Python
|
||||||
|
├── docker-compose.yml # Configuración Docker
|
||||||
|
├── django.Dockerfile # Dockerfile Django
|
||||||
|
├── .env # Variables de entorno
|
||||||
|
├── .env_example # Ejemplo de variables de entorno
|
||||||
|
├── README.rst # Documentación básica
|
||||||
|
├── Rakefile # Tareas rake
|
||||||
|
├── doc/ # Documentación adicional
|
||||||
|
│ └── requests.org
|
||||||
|
└── tienda_ilusion/ # Proyecto Django
|
||||||
|
├── manage.py
|
||||||
|
├── db.sqlite3 # Base de datos SQLite
|
||||||
|
├── don_confiao/ # App principal
|
||||||
|
│ ├── models.py # Modelos: Customer, Product, Sale, SaleLine, Payment, ReconciliationJar, AdminCode
|
||||||
|
│ ├── views.py
|
||||||
|
│ ├── api_views.py
|
||||||
|
│ ├── serializers.py
|
||||||
|
│ ├── forms.py
|
||||||
|
│ ├── admin.py
|
||||||
|
│ ├── urls.py
|
||||||
|
│ ├── export_csv.py
|
||||||
|
│ ├── tests/ # Tests
|
||||||
|
│ └── migrations/
|
||||||
|
├── users/ # App de usuarios
|
||||||
|
│ ├── models.py
|
||||||
|
│ ├── views.py
|
||||||
|
│ ├── serializers.py
|
||||||
|
│ ├── urls.py
|
||||||
|
│ └── tests/
|
||||||
|
└── tienda_ilusion/ # Configuración Django
|
||||||
|
├── settings.py
|
||||||
|
├── urls.py
|
||||||
|
├── wsgi.py
|
||||||
|
└── asgi.py
|
||||||
|
```
|
||||||
|
|
||||||
|
## Dependencias Principales
|
||||||
|
- Django==5.0.6
|
||||||
|
- djangorestframework
|
||||||
|
- django-cors-headers
|
||||||
|
- djangorestframework-simplejwt
|
||||||
|
- sabatron-tryton-rpc-client==7.4.0 (integración con Tryton ERP)
|
||||||
|
|
||||||
|
## Modelos Principales (don_confiao/models.py)
|
||||||
|
- **Customer**: Clientes (name, address, email, phone, external_id)
|
||||||
|
- **Product**: Productos (name, price, measuring_unit, categories)
|
||||||
|
- **ProductCategory**: Categorías de productos
|
||||||
|
- **Sale**: Ventas (customer, date, phone, description, payment_method, reconciliation)
|
||||||
|
- **SaleLine**: Líneas de venta (sale, product, quantity, unit_price, description)
|
||||||
|
- **Payment**: Pagos (date_time, type_payment, amount, reconciliation_jar)
|
||||||
|
- **PaymentSale**: Relación muchos a muchos entre Payment y Sale
|
||||||
|
- **ReconciliationJar**: Arqueo de caja (is_valid, date_time, reconcilier, cash_taken, cash_discrepancy)
|
||||||
|
- **AdminCode**: Códigos de administrador
|
||||||
|
|
||||||
|
## Autenticación
|
||||||
|
- JWT con djangorestframework-simplejwt
|
||||||
|
- ACCESS_TOKEN_LIFETIME: 30 minutos
|
||||||
|
- REFRESH_TOKEN_LIFETIME: 1 día
|
||||||
|
|
||||||
|
## API Endpoints
|
||||||
|
- REST API en don_confiao/api_views.py y users/
|
||||||
|
- URLs en don_confiao/urls.py y users/urls.py
|
||||||
|
|
||||||
|
## Ejecución con Docker Compose
|
||||||
|
|
||||||
|
El proyecto se ejecuta con docker-compose. Todos los comandos `manage.py` deben ejecutarse dentro del contenedor:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Ejecutar tests
|
||||||
|
docker-compose run --rm django python manage.py test
|
||||||
|
|
||||||
|
# Migraciones
|
||||||
|
docker-compose run --rm django python manage.py makemigrations
|
||||||
|
docker-compose run --rm django python manage.py migrate
|
||||||
|
|
||||||
|
# Servidor desarrollo
|
||||||
|
docker-compose up
|
||||||
|
|
||||||
|
# Shell Django
|
||||||
|
docker-compose run --rm django python manage.py shell
|
||||||
|
|
||||||
|
# Crear superuser
|
||||||
|
docker-compose run --rm django python manage.py createsuperuser
|
||||||
|
```
|
||||||
|
|
||||||
|
Nota: El volumen monta `tienda_ilusion/` en `/app/`, por lo que el path correcto es `python manage.py` (no `python tienda_ilusion/manage.py`).
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
- Framework: Django unittest
|
||||||
|
- Directorio: don_confiao/tests/
|
||||||
|
- Ejecutar: `docker-compose run --rm django python manage.py test`
|
||||||
|
|
||||||
|
## Comandos Útiles (dentro del contenedor)
|
||||||
|
- Migraciones: `docker-compose run --rm django python manage.py makemigrations && docker-compose run --rm django python manage.py migrate`
|
||||||
|
- Servidor desarrollo: `docker-compose up`
|
||||||
|
- Shell Django: `docker-compose run --rm django python manage.py shell`
|
||||||
|
- Superuser: `docker-compose run --rm django python manage.py createsuperuser`
|
||||||
|
|
||||||
|
## Integraciones
|
||||||
|
- **Tryton ERP**: Integración mediante sabatron-tryton-rpc-client para sincronización de clientes, productos y ventas
|
||||||
@@ -5,4 +5,6 @@ WORKDIR /app/
|
|||||||
COPY requirements.txt ./
|
COPY requirements.txt ./
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
COPY tienda_ilusion/ ./
|
||||||
|
|
||||||
CMD ["python", "manage.py", "runserver", "0.0.0.0:9090"]
|
CMD ["python", "manage.py", "runserver", "0.0.0.0:9090"]
|
||||||
|
|||||||
76
doc/requests.org
Normal file
76
doc/requests.org
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
* Requests
|
||||||
|
Ejemplo de request contra la api usando [[https://github.com/federicotdn/verb][verb]]
|
||||||
|
|
||||||
|
** Autenticación :verb:
|
||||||
|
template http://localhost:7000/api
|
||||||
|
Content-Type: application/json;
|
||||||
|
*** Solicitar token
|
||||||
|
post /token/
|
||||||
|
|
||||||
|
{
|
||||||
|
"username": "admin",
|
||||||
|
"password": "123"
|
||||||
|
}
|
||||||
|
**** respuesta
|
||||||
|
#+begin_src json
|
||||||
|
{
|
||||||
|
"refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTc3MTE4NzYxOSwiaWF0IjoxNzcxMTAxMjE5LCJqdGkiOiI5ZTgzNGRlM2QzMmQ0NmQyODEwZGQ2MjI2ODUwNjgzNyIsInVzZXJfaWQiOiIyIn0.JaUOqEAZ2T8vVT36mXfweMmYjEWsP7toD07jeeyrl1k",
|
||||||
|
"access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzcxMTAzMDE5LCJpYXQiOjE3NzExMDEyMTksImp0aSI6ImFmOWFjNGM1MzBiZjQ4ZGE4Yzg2MWFjYzIzNjQ3NjU3IiwidXNlcl9pZCI6IjIifQ.6wH5sx1fyFn3Wt3DVZGYbiYi79rGthUZkgGmTqzebXc"
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
*** Perfil de usuario
|
||||||
|
get /users/me/
|
||||||
|
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzcxMTAzMDE5LCJpYXQiOjE3NzExMDEyMTksImp0aSI6ImFmOWFjNGM1MzBiZjQ4ZGE4Yzg2MWFjYzIzNjQ3NjU3IiwidXNlcl9pZCI6IjIifQ.6wH5sx1fyFn3Wt3DVZGYbiYi79rGthUZkgGmTqzebXc
|
||||||
|
**** Respuesta
|
||||||
|
#+begin_src json
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"username": "admin",
|
||||||
|
"email": "correo@example.com",
|
||||||
|
"first_name": "",
|
||||||
|
"last_name": ""
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
*** Renovar token
|
||||||
|
post /token/refresh/
|
||||||
|
|
||||||
|
{
|
||||||
|
"refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTc3MTE4NzYxOSwiaWF0IjoxNzcxMTAxMjE5LCJqdGkiOiI5ZTgzNGRlM2QzMmQ0NmQyODEwZGQ2MjI2ODUwNjgzNyIsInVzZXJfaWQiOiIyIn0.JaUOqEAZ2T8vVT36mXfweMmYjEWsP7toD07jeeyrl1k"
|
||||||
|
}
|
||||||
|
**** response
|
||||||
|
#+begin_src json
|
||||||
|
{
|
||||||
|
"access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzcxMTAzNjA1LCJpYXQiOjE3NzExMDE4MDUsImp0aSI6ImJjZTY5ZTA3MTIyOTQxMTg5NmFjYzk1ZDNiOThhMTI0IiwidXNlcl9pZCI6IjIifQ.b4Z1c_Yi5tsLZ-7F0KZcM2tai-f1VeaE881j2pKDwYA"
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
** Don confiao :verb:
|
||||||
|
template http://localhost:7000/don_confiao/api/
|
||||||
|
Content-Type: application/json;
|
||||||
|
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzcxMTAzNjA1LCJpYXQiOjE3NzExMDE4MDUsImp0aSI6ImJjZTY5ZTA3MTIyOTQxMTg5NmFjYzk1ZDNiOThhMTI0IiwidXNlcl9pZCI6IjIifQ.b4Z1c_Yi5tsLZ-7F0KZcM2tai-f1VeaE881j2pKDwYA
|
||||||
|
*** todas las rutas
|
||||||
|
get
|
||||||
|
**** response
|
||||||
|
#+begin_src json
|
||||||
|
{
|
||||||
|
"sales": "http://localhost:7000/don_confiao/api/sales/",
|
||||||
|
"customers": "http://localhost:7000/don_confiao/api/customers/",
|
||||||
|
"products": "http://localhost:7000/don_confiao/api/products/",
|
||||||
|
"reconciliate_jar": "http://localhost:7000/don_confiao/api/reconciliate_jar/"
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
*** customers
|
||||||
|
get customers/
|
||||||
|
**** response
|
||||||
|
#+begin_src json
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "Consumidor Final",
|
||||||
|
"address": "",
|
||||||
|
"email": "",
|
||||||
|
"phone": "",
|
||||||
|
"external_id": "2753"
|
||||||
|
},
|
||||||
|
...
|
||||||
|
]
|
||||||
|
#+end_src
|
||||||
11
docker-compose.override.yml
Normal file
11
docker-compose.override.yml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
services:
|
||||||
|
django:
|
||||||
|
build:
|
||||||
|
context: ./
|
||||||
|
dockerfile: django.Dockerfile
|
||||||
|
volumes:
|
||||||
|
- ./tienda_ilusion:/app/
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
ports:
|
||||||
|
- "7000:9090"
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
services:
|
services:
|
||||||
django:
|
django:
|
||||||
build:
|
image: gitea.onecluster.org/oneteam/don_confiao_backend:latest
|
||||||
context: ./
|
env_file:
|
||||||
dockerfile: django.Dockerfile
|
- .env
|
||||||
volumes:
|
|
||||||
- ./tienda_ilusion:/app/
|
|
||||||
ports:
|
ports:
|
||||||
- "7000:9090"
|
- "7000:9090"
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
Django==5.0.6
|
Django==5.0.6
|
||||||
djangorestframework
|
djangorestframework
|
||||||
django-cors-headers
|
django-cors-headers
|
||||||
|
djangorestframework-simplejwt
|
||||||
|
sabatron-tryton-rpc-client==7.4.0
|
||||||
|
|||||||
@@ -3,14 +3,26 @@ from rest_framework.response import Response
|
|||||||
from rest_framework.status import HTTP_400_BAD_REQUEST
|
from rest_framework.status import HTTP_400_BAD_REQUEST
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.pagination import PageNumberPagination
|
from rest_framework.pagination import PageNumberPagination
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
|
||||||
from .models import Sale, SaleLine, Customer, Product, ReconciliationJar, PaymentMethods, AdminCode
|
from .models import Sale, SaleLine, Customer, Product, ReconciliationJar, PaymentMethods, AdminCode
|
||||||
from .serializers import SaleSerializer, ProductSerializer, CustomerSerializer, ReconciliationJarSerializer, PaymentMethodSerializer, SaleForRenconciliationSerializer, SaleSummarySerializer
|
from .serializers import SaleSerializer, ProductSerializer, CustomerSerializer, ReconciliationJarSerializer, PaymentMethodSerializer, SaleForRenconciliationSerializer, SaleSummarySerializer
|
||||||
from .views import sales_to_tryton_csv
|
from .views import sales_to_tryton_csv
|
||||||
|
from .permissions import IsAdministrator
|
||||||
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
from sabatron_tryton_rpc_client.client import Client
|
||||||
import io
|
import io
|
||||||
import csv
|
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')
|
||||||
|
TRYTON_COP_CURRENCY = 31
|
||||||
|
TRYTON_COMPANY_ID = 1
|
||||||
|
TRYTON_SHOPS = [1]
|
||||||
|
|
||||||
|
|
||||||
class Pagination(PageNumberPagination):
|
class Pagination(PageNumberPagination):
|
||||||
@@ -28,10 +40,12 @@ class SaleView(viewsets.ModelViewSet):
|
|||||||
date = data['date']
|
date = data['date']
|
||||||
lines = data['saleline_set']
|
lines = data['saleline_set']
|
||||||
payment_method = data['payment_method']
|
payment_method = data['payment_method']
|
||||||
|
description = data.get('notes', '')
|
||||||
sale = Sale.objects.create(
|
sale = Sale.objects.create(
|
||||||
customer=customer,
|
customer=customer,
|
||||||
date=date,
|
date=date,
|
||||||
payment_method=payment_method
|
payment_method=payment_method,
|
||||||
|
description=description
|
||||||
)
|
)
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
@@ -62,6 +76,8 @@ class CustomerView(viewsets.ModelViewSet):
|
|||||||
|
|
||||||
|
|
||||||
class ReconciliateJarView(APIView):
|
class ReconciliateJarView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||||
|
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
data = request.data
|
data = request.data
|
||||||
cash_purchases_id = data.get('cash_purchases')
|
cash_purchases_id = data.get('cash_purchases')
|
||||||
@@ -87,7 +103,8 @@ class ReconciliateJarView(APIView):
|
|||||||
|
|
||||||
def _is_valid_total(self, purchases, total):
|
def _is_valid_total(self, purchases, total):
|
||||||
calculated_total = sum(p.get_total() for p in purchases)
|
calculated_total = sum(p.get_total() for p in purchases)
|
||||||
return calculated_total == Decimal(total)
|
return Decimal(calculated_total).quantize(Decimal('.0001')) == (
|
||||||
|
Decimal(total).quantize(Decimal('.0001')))
|
||||||
|
|
||||||
def _get_other_purchases(self, other_totals):
|
def _get_other_purchases(self, other_totals):
|
||||||
if not other_totals:
|
if not other_totals:
|
||||||
@@ -118,6 +135,8 @@ class PaymentMethodView(APIView):
|
|||||||
|
|
||||||
|
|
||||||
class SalesForReconciliationView(APIView):
|
class SalesForReconciliationView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
sales = Sale.objects.filter(reconciliation=None)
|
sales = Sale.objects.filter(reconciliation=None)
|
||||||
grouped_sales = {}
|
grouped_sales = {}
|
||||||
@@ -139,6 +158,8 @@ class SaleSummary(APIView):
|
|||||||
|
|
||||||
|
|
||||||
class AdminCodeValidateView(APIView):
|
class AdminCodeValidateView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||||
|
|
||||||
def get(self, request, code):
|
def get(self, request, code):
|
||||||
codes = AdminCode.objects.filter(value=code)
|
codes = AdminCode.objects.filter(value=code)
|
||||||
return Response({'validCode': bool(codes)})
|
return Response({'validCode': bool(codes)})
|
||||||
@@ -148,9 +169,12 @@ class ReconciliateJarModelView(viewsets.ModelViewSet):
|
|||||||
queryset = ReconciliationJar.objects.all().order_by('-date_time')
|
queryset = ReconciliationJar.objects.all().order_by('-date_time')
|
||||||
pagination_class = Pagination
|
pagination_class = Pagination
|
||||||
serializer_class = ReconciliationJarSerializer
|
serializer_class = ReconciliationJarSerializer
|
||||||
|
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||||
|
|
||||||
|
|
||||||
class SalesForTrytonView(APIView):
|
class SalesForTrytonView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
sales = Sale.objects.all()
|
sales = Sale.objects.all()
|
||||||
csv = self._generate_sales_CSV(sales)
|
csv = self._generate_sales_CSV(sales)
|
||||||
@@ -164,3 +188,270 @@ class SalesForTrytonView(APIView):
|
|||||||
for row in csv_data:
|
for row in csv_data:
|
||||||
writer.writerow(row)
|
writer.writerow(row)
|
||||||
return output.getvalue()
|
return output.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
class SalesToTrytonView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||||
|
|
||||||
|
def post(self, request):
|
||||||
|
tryton_client = Client(
|
||||||
|
hostname=TRYTON_HOST,
|
||||||
|
database=TRYTON_DATABASE,
|
||||||
|
username=TRYTON_USERNAME,
|
||||||
|
password=TRYTON_PASSWORD
|
||||||
|
)
|
||||||
|
tryton_client.connect()
|
||||||
|
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 = tryton_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}"
|
||||||
|
f"venta_id: {sale.id}")
|
||||||
|
failed.append(sale.id)
|
||||||
|
continue
|
||||||
|
|
||||||
|
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": 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:
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ProductsFromTrytonView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||||
|
|
||||||
|
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 = [[["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
|
||||||
|
)
|
||||||
|
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}"
|
||||||
|
f"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 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('list_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()
|
||||||
|
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):
|
||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
class CustomersFromTrytonView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||||
|
|
||||||
|
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, [["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 = []
|
||||||
|
|
||||||
|
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', 'addresses']
|
||||||
|
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
|
||||||
|
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
|
||||||
|
|
||||||
|
def __create_customer(self, tryton_party):
|
||||||
|
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):
|
||||||
|
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()
|
||||||
|
|||||||
@@ -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),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -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),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -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),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -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),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -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),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# Generated by Django 5.0.6 on 2026-03-15 04:39
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('don_confiao', '0043_customer_address_external_id'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='payment',
|
||||||
|
name='type_payment',
|
||||||
|
field=models.CharField(choices=[('CASH', 'Efectivo'), ('CONFIAR', 'Confiar'), ('BANCOLOMBIA', 'Bancolombia'), ('CREDIT', 'Crédito')], default='CASH', max_length=30),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='sale',
|
||||||
|
name='payment_method',
|
||||||
|
field=models.CharField(choices=[('CASH', 'Efectivo'), ('CONFIAR', 'Confiar'), ('BANCOLOMBIA', 'Bancolombia'), ('CREDIT', 'Crédito')], default='CASH', max_length=30),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -10,6 +10,7 @@ class PaymentMethods(models.TextChoices):
|
|||||||
CASH = 'CASH', _('Efectivo')
|
CASH = 'CASH', _('Efectivo')
|
||||||
CONFIAR = 'CONFIAR', _('Confiar')
|
CONFIAR = 'CONFIAR', _('Confiar')
|
||||||
BANCOLOMBIA = 'BANCOLOMBIA', _('Bancolombia')
|
BANCOLOMBIA = 'BANCOLOMBIA', _('Bancolombia')
|
||||||
|
CREDIT = 'CREDIT', _('Crédito')
|
||||||
|
|
||||||
|
|
||||||
class Customer(models.Model):
|
class Customer(models.Model):
|
||||||
@@ -17,6 +18,8 @@ class Customer(models.Model):
|
|||||||
address = models.CharField(max_length=100, null=True, blank=True)
|
address = models.CharField(max_length=100, null=True, blank=True)
|
||||||
email = 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)
|
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):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
@@ -41,7 +44,9 @@ class Product(models.Model):
|
|||||||
choices=MeasuringUnits.choices,
|
choices=MeasuringUnits.choices,
|
||||||
default=MeasuringUnits.UNIT
|
default=MeasuringUnits.UNIT
|
||||||
)
|
)
|
||||||
|
unit_external_id = models.CharField(max_length=100, null=True, blank=True)
|
||||||
categories = models.ManyToManyField(ProductCategory)
|
categories = models.ManyToManyField(ProductCategory)
|
||||||
|
external_id = models.CharField(max_length=100, null=True, blank=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
@@ -105,6 +110,7 @@ class Sale(models.Model):
|
|||||||
related_name='Sales',
|
related_name='Sales',
|
||||||
null=True
|
null=True
|
||||||
)
|
)
|
||||||
|
external_id = models.CharField(max_length=100, null=True, blank=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.date} {self.customer}"
|
return f"{self.date} {self.customer}"
|
||||||
|
|||||||
6
tienda_ilusion/don_confiao/permissions.py
Normal file
6
tienda_ilusion/don_confiao/permissions.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from rest_framework.permissions import BasePermission
|
||||||
|
|
||||||
|
|
||||||
|
class IsAdministrator(BasePermission):
|
||||||
|
def has_permission(self, request, view):
|
||||||
|
return request.user and request.user.is_staff
|
||||||
@@ -15,7 +15,7 @@ class SaleSerializer(serializers.ModelSerializer):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = Sale
|
model = Sale
|
||||||
fields = ['id', 'customer', 'date', 'saleline_set',
|
fields = ['id', 'customer', 'date', 'saleline_set',
|
||||||
'total', 'payment_method']
|
'total', 'payment_method', 'external_id']
|
||||||
|
|
||||||
|
|
||||||
class ProductSerializer(serializers.ModelSerializer):
|
class ProductSerializer(serializers.ModelSerializer):
|
||||||
@@ -27,7 +27,7 @@ class ProductSerializer(serializers.ModelSerializer):
|
|||||||
class CustomerSerializer(serializers.ModelSerializer):
|
class CustomerSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Customer
|
model = Customer
|
||||||
fields = ['id', 'name', 'address', 'email', 'phone']
|
fields = ['id', 'name', 'address', 'email', 'phone', 'external_id']
|
||||||
|
|
||||||
|
|
||||||
class ReconciliationJarSerializer(serializers.ModelSerializer):
|
class ReconciliationJarSerializer(serializers.ModelSerializer):
|
||||||
|
|||||||
19
tienda_ilusion/don_confiao/tests/Mixins.py
Normal file
19
tienda_ilusion/don_confiao/tests/Mixins.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
from django.contrib.auth.models import User
|
||||||
|
from rest_framework_simplejwt.tokens import RefreshToken
|
||||||
|
from rest_framework.test import APIClient
|
||||||
|
|
||||||
|
|
||||||
|
class LoginMixin:
|
||||||
|
def login(self):
|
||||||
|
self.user = User.objects.create_superuser(
|
||||||
|
username='admin',
|
||||||
|
email='admin@example.com',
|
||||||
|
password='adminpass'
|
||||||
|
)
|
||||||
|
|
||||||
|
refresh = RefreshToken.for_user(self.user)
|
||||||
|
self.access_token = str(refresh.access_token)
|
||||||
|
|
||||||
|
self.client = APIClient()
|
||||||
|
self.client.credentials(
|
||||||
|
HTTP_AUTHORIZATION=f'Bearer {self.access_token}')
|
||||||
@@ -1,20 +1,21 @@
|
|||||||
from django.test import TestCase, Client
|
from django.test import TestCase
|
||||||
|
|
||||||
from ..models import AdminCode
|
from ..models import AdminCode
|
||||||
|
from .Mixins import LoginMixin
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
class TestAdminCode(TestCase):
|
class TestAdminCode(TestCase, LoginMixin):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
self.login()
|
||||||
|
|
||||||
self.valid_code = 'some valid code'
|
self.valid_code = 'some valid code'
|
||||||
admin_code = AdminCode()
|
admin_code = AdminCode()
|
||||||
admin_code.value = self.valid_code
|
admin_code.value = self.valid_code
|
||||||
admin_code.clean()
|
admin_code.clean()
|
||||||
admin_code.save()
|
admin_code.save()
|
||||||
|
|
||||||
self.client = Client()
|
|
||||||
|
|
||||||
def test_validate_code(self):
|
def test_validate_code(self):
|
||||||
url = '/don_confiao/api/admin_code/validate/' + self.valid_code
|
url = '/don_confiao/api/admin_code/validate/' + self.valid_code
|
||||||
response = self.client.get(url)
|
response = self.client.get(url)
|
||||||
|
|||||||
@@ -2,21 +2,24 @@ import json
|
|||||||
import csv
|
import csv
|
||||||
import io
|
import io
|
||||||
|
|
||||||
from django.urls import reverse
|
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
from ..models import Sale, Product, Customer
|
from ..models import Sale, Product, Customer
|
||||||
|
from .Mixins import LoginMixin
|
||||||
|
|
||||||
|
|
||||||
class TestAPI(APITestCase):
|
class TestAPI(APITestCase, LoginMixin):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
self.login()
|
||||||
|
|
||||||
self.product = Product.objects.create(
|
self.product = Product.objects.create(
|
||||||
name='Panela',
|
name='Panela',
|
||||||
price=5000,
|
price=5000,
|
||||||
measuring_unit='UNIT'
|
measuring_unit='UNIT'
|
||||||
)
|
)
|
||||||
self.customer = Customer.objects.create(
|
self.customer = Customer.objects.create(
|
||||||
name='Camilo'
|
name='Camilo',
|
||||||
|
external_id='18'
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_create_sale(self):
|
def test_create_sale(self):
|
||||||
@@ -27,12 +30,13 @@ class TestAPI(APITestCase):
|
|||||||
sale = Sale.objects.all()[0]
|
sale = Sale.objects.all()[0]
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
sale.customer.name,
|
sale.customer.name,
|
||||||
self.customer.name
|
self.customer.name,
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
sale.id,
|
sale.id,
|
||||||
content['id']
|
content['id']
|
||||||
)
|
)
|
||||||
|
self.assertIsNone(sale.external_id)
|
||||||
|
|
||||||
def test_create_sale_with_decimal(self):
|
def test_create_sale_with_decimal(self):
|
||||||
response = self._create_sale_with_decimal()
|
response = self._create_sale_with_decimal()
|
||||||
@@ -66,6 +70,23 @@ class TestAPI(APITestCase):
|
|||||||
json_response = json.loads(response.content.decode('utf-8'))
|
json_response = json.loads(response.content.decode('utf-8'))
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(self.customer.name, json_response[0]['name'])
|
self.assertEqual(self.customer.name, json_response[0]['name'])
|
||||||
|
self.assertEqual(
|
||||||
|
self.customer.external_id,
|
||||||
|
json_response[0]['external_id']
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_get_sales(self):
|
||||||
|
url = '/don_confiao/api/sales/'
|
||||||
|
self._create_sale()
|
||||||
|
|
||||||
|
response = self.client.get(url)
|
||||||
|
json_response = json.loads(response.content.decode('utf-8'))
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(self.customer.id, json_response[0]['customer'])
|
||||||
|
self.assertEqual(
|
||||||
|
None,
|
||||||
|
json_response[0]['external_id']
|
||||||
|
)
|
||||||
|
|
||||||
def test_get_sales_for_tryton(self):
|
def test_get_sales_for_tryton(self):
|
||||||
url = '/don_confiao/api/sales/for_tryton'
|
url = '/don_confiao/api/sales/for_tryton'
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
import json
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
from ..models import Customer
|
||||||
|
from .Mixins import LoginMixin
|
||||||
|
|
||||||
|
|
||||||
|
class TestCustomersFromTryton(TestCase, LoginMixin):
|
||||||
|
def setUp(self):
|
||||||
|
self.login()
|
||||||
|
|
||||||
|
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, [['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', 'addresses'], {'company': 1})
|
||||||
|
if (args == (party_read, read_args)):
|
||||||
|
return [
|
||||||
|
{'id': 5, 'name': 'Carlos', 'addresses': [303]},
|
||||||
|
{'id': 6, 'name': 'Cristian', 'addresses': []},
|
||||||
|
{'id': 7, 'name': 'Ana', 'addresses': [302]},
|
||||||
|
{'id': 8, 'name': 'José', 'addresses': []},
|
||||||
|
]
|
||||||
|
|
||||||
|
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')
|
||||||
|
self.assertEqual(created_customer.address_external_id, str(302))
|
||||||
|
|
||||||
|
updated_customer = Customer.objects.get(id=1)
|
||||||
|
self.assertEqual(updated_customer.external_id, str(5))
|
||||||
|
self.assertEqual(updated_customer.name, 'Carlos')
|
||||||
|
self.assertIn(updated_customer.address_external_id, str(303))
|
||||||
@@ -1,24 +1,34 @@
|
|||||||
import csv
|
import csv
|
||||||
|
import json
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
from django.test import TestCase, Client
|
from django.test import TestCase
|
||||||
from django.urls import reverse
|
|
||||||
|
|
||||||
from ..models import Sale, SaleLine, Product, Customer
|
from ..models import Sale, SaleLine, Product, Customer
|
||||||
|
from .Mixins import LoginMixin
|
||||||
|
|
||||||
class TestExportarVentasParaTryton(TestCase):
|
|
||||||
|
class TestExportarVentasParaTryton(TestCase, LoginMixin):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
self.login()
|
||||||
|
|
||||||
self.product = Product.objects.create(
|
self.product = Product.objects.create(
|
||||||
name='Panela',
|
name='Panela',
|
||||||
price=5000,
|
price=5000,
|
||||||
measuring_unit='UNIT'
|
measuring_unit='UNIT',
|
||||||
|
unit_external_id=1,
|
||||||
|
external_id=1
|
||||||
)
|
)
|
||||||
self.customer = Customer.objects.create(
|
self.customer = Customer.objects.create(
|
||||||
name='Camilo'
|
name='Camilo',
|
||||||
|
external_id=1,
|
||||||
|
address_external_id=307,
|
||||||
)
|
)
|
||||||
self.sale = Sale.objects.create(
|
self.sale = Sale.objects.create(
|
||||||
customer=self.customer,
|
customer=self.customer,
|
||||||
date='2024-09-02',
|
date='2024-09-02',
|
||||||
payment_method='CASH',
|
payment_method='CASH',
|
||||||
|
description='un comentario'
|
||||||
)
|
)
|
||||||
self.sale_line1 = SaleLine.objects.create(
|
self.sale_line1 = SaleLine.objects.create(
|
||||||
product=self.product,
|
product=self.product,
|
||||||
@@ -34,9 +44,8 @@ class TestExportarVentasParaTryton(TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def test_exportar_ventas_para_tryton(self):
|
def test_exportar_ventas_para_tryton(self):
|
||||||
client = Client()
|
|
||||||
url = '/don_confiao/exportar_ventas_para_tryton'
|
url = '/don_confiao/exportar_ventas_para_tryton'
|
||||||
response = client.get(url)
|
response = self.client.get(url)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response['Content-Type'], 'text/csv')
|
self.assertEqual(response['Content-Type'], 'text/csv')
|
||||||
csv_content = response.content.decode('utf-8')
|
csv_content = response.content.decode('utf-8')
|
||||||
@@ -65,9 +74,32 @@ class TestExportarVentasParaTryton(TestCase):
|
|||||||
self.assertEqual(next(csv_reader), expected_header)
|
self.assertEqual(next(csv_reader), expected_header)
|
||||||
|
|
||||||
expected_rows = [
|
expected_rows = [
|
||||||
["Camilo", "Camilo", "Camilo", "", "", "2024-09-02", "Contado", "Almacén", "Peso colombiano", "Panela", "2.00", "3000.00", "Unidad", "TIENDA LA ILUSIÓN", "Tienda La Ilusion", "La Ilusion", "True", ""],
|
["Camilo", "Camilo", "Camilo", "un comentario", "un comentario", "2024-09-02", "Contado", "Almacén", "Peso colombiano", "Panela", "2.00", "3000.00", "Unidad", "TIENDA LA ILUSIÓN", "Tienda La Ilusion", "La Ilusion", "True", "un comentario"],
|
||||||
["", "", "", "", "", "", "", "", "", "Panela", "3.00", "5000.00", "Unidad", "", "", "", "", ""],
|
["", "", "", "", "", "", "", "", "", "Panela", "3.00", "5000.00", "Unidad", "", "", "", "", ""],
|
||||||
]
|
]
|
||||||
csv_rows = list(csv_reader)
|
csv_rows = list(csv_reader)
|
||||||
self.assertEqual(csv_rows[0], expected_rows[0])
|
self.assertEqual(csv_rows[0], expected_rows[0])
|
||||||
self.assertEqual(csv_rows[1], expected_rows[1])
|
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):
|
||||||
|
external_id = '23423'
|
||||||
|
url = '/don_confiao/api/enviar_ventas_a_tryton'
|
||||||
|
mock_connect.return_value = None
|
||||||
|
mock_call.return_value = [external_id]
|
||||||
|
response = self.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, 'shipment_address': '307', 'invoice_address': '307', 'currency': 31, 'comment': 'un comentario', 'description': 'Metodo pago: CASH', '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'}}]]], 'self_pick_up': True}], {'company': 1, 'shops': [1]}])
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
from django.test import TestCase, Client
|
from django.test import TestCase
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from ..models import Sale, Product, SaleLine, Customer, ReconciliationJar
|
from ..models import Sale, Product, SaleLine, Customer, ReconciliationJar
|
||||||
|
from .Mixins import LoginMixin
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
class TestJarReconcliation(TestCase):
|
class TestJarReconcliation(TestCase, LoginMixin):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
self.login()
|
||||||
|
|
||||||
customer = Customer()
|
customer = Customer()
|
||||||
customer.name = 'Alejo Mono'
|
customer.name = 'Alejo Mono'
|
||||||
customer.save()
|
customer.save()
|
||||||
|
|
||||||
self.client = Client()
|
|
||||||
|
|
||||||
purchase = Sale()
|
purchase = Sale()
|
||||||
purchase.customer = customer
|
purchase.customer = customer
|
||||||
purchase.date = "2024-07-30"
|
purchase.date = "2024-07-30"
|
||||||
@@ -235,6 +236,47 @@ class TestJarReconcliation(TestCase):
|
|||||||
[sale['payment_method'] for sale in content['Sales']]
|
[sale['payment_method'] for sale in content['Sales']]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_create_reconciliation_with_decimal_on_sale_lines(self):
|
||||||
|
customer = Customer()
|
||||||
|
customer.name = 'Consumidor final'
|
||||||
|
customer.save()
|
||||||
|
|
||||||
|
product = Product()
|
||||||
|
product.name = "Mantequilla natural gramos"
|
||||||
|
product.price = "57.50"
|
||||||
|
product.save()
|
||||||
|
|
||||||
|
purchase = Sale()
|
||||||
|
purchase.customer = customer
|
||||||
|
purchase.date = "2024-07-30"
|
||||||
|
purchase.payment_method = 'CASH'
|
||||||
|
purchase.clean()
|
||||||
|
purchase.save()
|
||||||
|
|
||||||
|
line = SaleLine()
|
||||||
|
line.sale = purchase
|
||||||
|
line.product = product
|
||||||
|
line.quantity = "0.24"
|
||||||
|
line.unit_price = "57.50"
|
||||||
|
line.save()
|
||||||
|
|
||||||
|
url = '/don_confiao/reconciliate_jar'
|
||||||
|
total_purchases = 13.80
|
||||||
|
data = {
|
||||||
|
'date_time': '2024-12-02T21:07',
|
||||||
|
'reconcilier': 'carlos',
|
||||||
|
'total_cash_purchases': total_purchases,
|
||||||
|
'cash_taken': total_purchases,
|
||||||
|
'cash_discrepancy': 0,
|
||||||
|
'cash_purchases': [purchase.id],
|
||||||
|
}
|
||||||
|
response = self.client.post(
|
||||||
|
url, data=json.dumps(data).encode('utf-8'),
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
def _create_simple_reconciliation(self):
|
def _create_simple_reconciliation(self):
|
||||||
reconciliation = ReconciliationJar()
|
reconciliation = ReconciliationJar()
|
||||||
reconciliation.date_time = "2024-07-30"
|
reconciliation.date_time = "2024-07-30"
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ class TestCustomer(TestCase):
|
|||||||
customer.save()
|
customer.save()
|
||||||
|
|
||||||
self.assertIsInstance(customer, Customer)
|
self.assertIsInstance(customer, Customer)
|
||||||
|
self.assertIsNone(customer.external_id)
|
||||||
|
self.assertIsNone(customer.address_external_id)
|
||||||
|
|
||||||
def test_don_create_customer_without_name(self):
|
def test_don_create_customer_without_name(self):
|
||||||
customer = Customer()
|
customer = Customer()
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
from django.test import Client, TestCase
|
from django.test import TestCase
|
||||||
|
from .Mixins import LoginMixin
|
||||||
|
|
||||||
# from ..models import PaymentMethods
|
|
||||||
|
|
||||||
class TestPaymentMethods(TestCase):
|
class TestPaymentMethods(TestCase, LoginMixin):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.client = Client()
|
self.login()
|
||||||
|
|
||||||
def test_keys_in_payment_methods_to_select(self):
|
def test_keys_in_payment_methods_to_select(self):
|
||||||
response = self.client.get(
|
response = self.client.get(
|
||||||
@@ -21,3 +21,4 @@ class TestPaymentMethods(TestCase):
|
|||||||
self.assertIn('CASH', [method.get('value') for method in methods])
|
self.assertIn('CASH', [method.get('value') for method in methods])
|
||||||
self.assertIn('CONFIAR', [method.get('value') for method in methods])
|
self.assertIn('CONFIAR', [method.get('value') for method in methods])
|
||||||
self.assertIn('BANCOLOMBIA', [method.get('value') for method in methods])
|
self.assertIn('BANCOLOMBIA', [method.get('value') for method in methods])
|
||||||
|
self.assertIn('CREDIT', [method.get('value') for method in methods])
|
||||||
|
|||||||
@@ -10,6 +10,16 @@ class TestProducts(TestCase):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.client = Client()
|
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)
|
||||||
|
self.assertIsNone(product.unit_external_id)
|
||||||
|
|
||||||
def test_import_products(self):
|
def test_import_products(self):
|
||||||
self._import_csv()
|
self._import_csv()
|
||||||
all_products = self._get_products()
|
all_products = self._get_products()
|
||||||
|
|||||||
131
tienda_ilusion/don_confiao/tests/test_products_from_tryton.py
Normal file
131
tienda_ilusion/don_confiao/tests/test_products_from_tryton.py
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
import json
|
||||||
|
from decimal import Decimal
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
from ..models import Product
|
||||||
|
from .Mixins import LoginMixin
|
||||||
|
|
||||||
|
|
||||||
|
class TestProductsFromTryton(TestCase, LoginMixin):
|
||||||
|
def setUp(self):
|
||||||
|
self.login()
|
||||||
|
|
||||||
|
self.product = Product.objects.create(
|
||||||
|
name='Panela',
|
||||||
|
price=5000,
|
||||||
|
measuring_unit='UNIT',
|
||||||
|
unit_external_id=1,
|
||||||
|
external_id=191
|
||||||
|
)
|
||||||
|
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):
|
||||||
|
mock_connect.return_value = None
|
||||||
|
|
||||||
|
def fake_call(*args, **kwargs):
|
||||||
|
product_search = 'model.product.product.search'
|
||||||
|
search_args = [[["salable", "=", True]], 0, 1000, [['rec_name', 'ASC'], ['id', None]], {'company': 1}]
|
||||||
|
if (args == (product_search, search_args)):
|
||||||
|
return [190, 191, 192]
|
||||||
|
|
||||||
|
product_read = 'model.product.product.read'
|
||||||
|
product_args = ([190, 191, 192],
|
||||||
|
['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'}},
|
||||||
|
{'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}")
|
||||||
|
|
||||||
|
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, 191, 192],
|
||||||
|
'created_products': [3],
|
||||||
|
'untouched_products': [2],
|
||||||
|
'failed_products': [],
|
||||||
|
'updated_products': [1]
|
||||||
|
}
|
||||||
|
self.assertEqual(content, expected_response)
|
||||||
|
|
||||||
|
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'))
|
||||||
|
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')
|
||||||
|
|
||||||
|
@patch('sabatron_tryton_rpc_client.client.Client.call')
|
||||||
|
@patch('sabatron_tryton_rpc_client.client.Client.connect')
|
||||||
|
def test_import_duplicated_name_products(self, mock_connect, mock_call):
|
||||||
|
mock_connect.return_value = None
|
||||||
|
def fake_call(*args, **kwargs):
|
||||||
|
product_search = 'model.product.product.search'
|
||||||
|
search_args = [[["salable", "=", True]], 0, 1000, [['rec_name', 'ASC'], ['id', None]], {'company': 1}]
|
||||||
|
if (args == (product_search, search_args)):
|
||||||
|
return [200]
|
||||||
|
|
||||||
|
product_read = 'model.product.product.read'
|
||||||
|
product_args = ([200],
|
||||||
|
['id', 'name', 'default_uom.id',
|
||||||
|
'default_uom.rec_name', 'list_price'],
|
||||||
|
{'company': 1}
|
||||||
|
)
|
||||||
|
if (args == (product_read, product_args)):
|
||||||
|
return [
|
||||||
|
{'id': 200, 'list_price': Decimal('25000'),
|
||||||
|
'name': self.product.name,
|
||||||
|
'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': [200],
|
||||||
|
'created_products': [],
|
||||||
|
'untouched_products': [],
|
||||||
|
'failed_products': [200],
|
||||||
|
'updated_products': [],
|
||||||
|
}
|
||||||
|
self.assertEqual(content, expected_response)
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
from django.test import TestCase, Client
|
from django.test import TestCase
|
||||||
from ..models import Sale, Product, SaleLine, Customer
|
from ..models import Sale, Product, SaleLine, Customer
|
||||||
|
from .Mixins import LoginMixin
|
||||||
|
|
||||||
|
|
||||||
class TestSummaryViewPurchase(TestCase):
|
class TestSummaryViewPurchase(TestCase, LoginMixin):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
self.login()
|
||||||
|
|
||||||
customer = Customer()
|
customer = Customer()
|
||||||
customer.name = 'Alejo Mono'
|
customer.name = 'Alejo Mono'
|
||||||
customer.save()
|
customer.save()
|
||||||
|
|
||||||
self.client = Client()
|
|
||||||
purchase = Sale()
|
purchase = Sale()
|
||||||
purchase.customer = customer
|
purchase.customer = customer
|
||||||
purchase.date = "2024-07-30"
|
purchase.date = "2024-07-30"
|
||||||
|
|||||||
@@ -20,10 +20,17 @@ urlpatterns = [
|
|||||||
path("productos", views.products, name="products"),
|
path("productos", views.products, name="products"),
|
||||||
path("lista_productos", views.ProductListView.as_view(), name='product_list'),
|
path("lista_productos", views.ProductListView.as_view(), name='product_list'),
|
||||||
path("importar_productos", views.import_products, name="import_products"),
|
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("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",
|
path("exportar_ventas_para_tryton",
|
||||||
views.exportar_ventas_para_tryton,
|
views.exportar_ventas_para_tryton,
|
||||||
name="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/<int:id>", views.purchase_summary, name="purchase_summary"),
|
path("resumen_compra/<int:id>", views.purchase_summary, name="purchase_summary"),
|
||||||
path("resumen_compra_json/<int:id>", api_views.SaleSummary.as_view(), name="purchase_json_summary"),
|
path("resumen_compra_json/<int:id>", 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"),
|
path("payment_methods/all/select_format", api_views.PaymentMethodView.as_view(), name="payment_methods_to_select"),
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/5.0/ref/settings/
|
|||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
@@ -44,7 +45,9 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
|
'rest_framework.authtoken',
|
||||||
'corsheaders',
|
'corsheaders',
|
||||||
|
'users',
|
||||||
# 'don_confiao'
|
# 'don_confiao'
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -57,7 +60,6 @@ MIDDLEWARE = [
|
|||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
'corsheaders.middleware.CorsMiddleware',
|
'corsheaders.middleware.CorsMiddleware',
|
||||||
'django.middleware.common.CommonMiddleware',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
ROOT_URLCONF = 'tienda_ilusion.urls'
|
ROOT_URLCONF = 'tienda_ilusion.urls'
|
||||||
@@ -65,7 +67,7 @@ ROOT_URLCONF = 'tienda_ilusion.urls'
|
|||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
'DIRS': [],
|
'DIRS': [os.path.join(BASE_DIR, 'tienda_ilusion/templates')],
|
||||||
'APP_DIRS': True,
|
'APP_DIRS': True,
|
||||||
'OPTIONS': {
|
'OPTIONS': {
|
||||||
'context_processors': [
|
'context_processors': [
|
||||||
@@ -134,3 +136,22 @@ STATIC_URL = 'static/'
|
|||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||||
|
|
||||||
FIXTURE_DIRS = ['don_confiao/tests/Fixtures']
|
FIXTURE_DIRS = ['don_confiao/tests/Fixtures']
|
||||||
|
|
||||||
|
REST_FRAMEWORK = {
|
||||||
|
'DEFAULT_AUTHENTICATION_CLASSES': [
|
||||||
|
"rest_framework_simplejwt.authentication.JWTAuthentication",
|
||||||
|
],
|
||||||
|
'DEFAULT_PERMISSION_CLASSES': [
|
||||||
|
'rest_framework.permissions.IsAuthenticated',
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
SIMPLE_JWT = {
|
||||||
|
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=30),
|
||||||
|
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
|
||||||
|
"AUTH_HEADER_TYPES": ("Bearer",),
|
||||||
|
}
|
||||||
|
|
||||||
|
# CORS_ALLOWED_ORIGINS = [
|
||||||
|
# "http://localhost:5173",
|
||||||
|
# ]
|
||||||
|
|||||||
@@ -16,11 +16,19 @@ Including another URLconf
|
|||||||
"""
|
"""
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
|
from rest_framework_simplejwt.views import (
|
||||||
|
TokenObtainPairView,
|
||||||
|
TokenRefreshView,
|
||||||
|
)
|
||||||
|
|
||||||
app_name = "don_confiao"
|
app_name = "don_confiao"
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("don_confiao/", include("don_confiao.urls")),
|
path("don_confiao/", include("don_confiao.urls")),
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
|
path('api/token/', TokenObtainPairView.as_view(),
|
||||||
|
name='token_obtain_pair'),
|
||||||
|
path('api/token/refresh/', TokenRefreshView.as_view(),
|
||||||
|
name='token_refresh'),
|
||||||
|
path('api/users/', include('users.urls')),
|
||||||
]
|
]
|
||||||
|
|||||||
0
tienda_ilusion/users/__init__.py
Normal file
0
tienda_ilusion/users/__init__.py
Normal file
3
tienda_ilusion/users/admin.py
Normal file
3
tienda_ilusion/users/admin.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
# Register your models here.
|
||||||
5
tienda_ilusion/users/apps.py
Normal file
5
tienda_ilusion/users/apps.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class UsersConfig(AppConfig):
|
||||||
|
name = 'users'
|
||||||
0
tienda_ilusion/users/migrations/__init__.py
Normal file
0
tienda_ilusion/users/migrations/__init__.py
Normal file
3
tienda_ilusion/users/models.py
Normal file
3
tienda_ilusion/users/models.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from django.db import models
|
||||||
|
|
||||||
|
# Create your models here.
|
||||||
13
tienda_ilusion/users/serializers.py
Normal file
13
tienda_ilusion/users/serializers.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from django.contrib.auth.models import User
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
|
||||||
|
class UserSerializer(serializers.ModelSerializer):
|
||||||
|
role = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = ('id', 'username', 'email', 'first_name', 'last_name', 'role')
|
||||||
|
|
||||||
|
def get_role(self, obj):
|
||||||
|
return 'administrator' if obj.is_staff else 'user'
|
||||||
98
tienda_ilusion/users/tests.py
Normal file
98
tienda_ilusion/users/tests.py
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
from django.urls import reverse
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
from rest_framework.test import APIClient
|
||||||
|
from rest_framework_simplejwt.tokens import RefreshToken
|
||||||
|
|
||||||
|
|
||||||
|
class MeEndpointTests(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.user = User.objects.create_superuser(
|
||||||
|
username='admin',
|
||||||
|
email='admin@example.com',
|
||||||
|
password='adminpass'
|
||||||
|
)
|
||||||
|
|
||||||
|
refresh = RefreshToken.for_user(self.user)
|
||||||
|
self.access_token = str(refresh.access_token)
|
||||||
|
|
||||||
|
self.client = APIClient()
|
||||||
|
self.client.credentials(
|
||||||
|
HTTP_AUTHORIZATION=f'Bearer {self.access_token}')
|
||||||
|
|
||||||
|
def test_me_endpoint_returns_correct_user_data(self):
|
||||||
|
"""
|
||||||
|
Verifica que GET /api/users/me/ devuelve los datos del usuario
|
||||||
|
autenticado.
|
||||||
|
"""
|
||||||
|
url = reverse('current-user')
|
||||||
|
response = self.client.get(url)
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
expected_fields = {'id', 'username', 'email',
|
||||||
|
'first_name', 'last_name', 'role'}
|
||||||
|
self.assertTrue(expected_fields.issubset(response.json().keys()))
|
||||||
|
|
||||||
|
data = response.json()
|
||||||
|
self.assertEqual(data['username'], self.user.username)
|
||||||
|
self.assertEqual(data['email'], self.user.email)
|
||||||
|
self.assertEqual(data['first_name'], self.user.first_name)
|
||||||
|
self.assertEqual(data['last_name'], self.user.last_name)
|
||||||
|
self.assertEqual(data['role'], 'administrator')
|
||||||
|
|
||||||
|
def test_regular_user_role_is_user(self):
|
||||||
|
"""
|
||||||
|
Verifica que un usuario sin permisos de staff recibe role 'user'.
|
||||||
|
"""
|
||||||
|
regular_user = User.objects.create_user(
|
||||||
|
username='regular',
|
||||||
|
email='regular@example.com',
|
||||||
|
password='regularpass',
|
||||||
|
is_staff=False
|
||||||
|
)
|
||||||
|
|
||||||
|
refresh = RefreshToken.for_user(regular_user)
|
||||||
|
access_token = str(refresh.access_token)
|
||||||
|
|
||||||
|
client = APIClient()
|
||||||
|
client.credentials(HTTP_AUTHORIZATION=f'Bearer {access_token}')
|
||||||
|
|
||||||
|
url = reverse('current-user')
|
||||||
|
response = client.get(url)
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.json()['role'], 'user')
|
||||||
|
|
||||||
|
def test_staff_user_role_is_administrator(self):
|
||||||
|
"""
|
||||||
|
Verifica que un usuario con is_staff=True recibe role 'administrator'.
|
||||||
|
"""
|
||||||
|
staff_user = User.objects.create_user(
|
||||||
|
username='staff',
|
||||||
|
email='staff@example.com',
|
||||||
|
password='staffpass',
|
||||||
|
is_staff=True
|
||||||
|
)
|
||||||
|
|
||||||
|
refresh = RefreshToken.for_user(staff_user)
|
||||||
|
access_token = str(refresh.access_token)
|
||||||
|
|
||||||
|
client = APIClient()
|
||||||
|
client.credentials(HTTP_AUTHORIZATION=f'Bearer {access_token}')
|
||||||
|
|
||||||
|
url = reverse('current-user')
|
||||||
|
response = client.get(url)
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.json()['role'], 'administrator')
|
||||||
|
|
||||||
|
def test_me_endpoint_requires_authentication(self):
|
||||||
|
"""
|
||||||
|
Sin token el endpoint debe devolver 401 Unauthorized.
|
||||||
|
"""
|
||||||
|
client_no_auth = APIClient()
|
||||||
|
url = reverse('current-user')
|
||||||
|
response = client_no_auth.get(url)
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 401)
|
||||||
6
tienda_ilusion/users/urls.py
Normal file
6
tienda_ilusion/users/urls.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from django.urls import path
|
||||||
|
from .views import CurrentUserView
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('me/', CurrentUserView.as_view(), name='current-user'),
|
||||||
|
]
|
||||||
12
tienda_ilusion/users/views.py
Normal file
12
tienda_ilusion/users/views.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
from rest_framework.views import APIView
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
from .serializers import UserSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class CurrentUserView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def get(self, request):
|
||||||
|
serializer = UserSerializer(request.user)
|
||||||
|
return Response(serializer.data)
|
||||||
Reference in New Issue
Block a user