Exportando ventas para tryton desde api #4
@ -6,9 +6,11 @@ from rest_framework.pagination import PageNumberPagination
|
||||
|
||||
from .models import Sale, SaleLine, Customer, Product, ReconciliationJar, PaymentMethods, AdminCode
|
||||
from .serializers import SaleSerializer, ProductSerializer, CustomerSerializer, ReconciliationJarSerializer, PaymentMethodSerializer, SaleForRenconciliationSerializer, SaleSummarySerializer
|
||||
from .views import sales_to_tryton_csv
|
||||
|
||||
from decimal import Decimal
|
||||
import json
|
||||
import io
|
||||
import csv
|
||||
|
||||
|
||||
class Pagination(PageNumberPagination):
|
||||
@ -128,12 +130,14 @@ class SalesForReconciliationView(APIView):
|
||||
|
||||
return Response(grouped_sales)
|
||||
|
||||
|
||||
class SaleSummary(APIView):
|
||||
def get(self, request, id):
|
||||
sale = Sale.objects.get(pk=id)
|
||||
serializer = SaleSummarySerializer(sale)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
class AdminCodeValidateView(APIView):
|
||||
def get(self, request, code):
|
||||
codes = AdminCode.objects.filter(value=code)
|
||||
@ -144,3 +148,19 @@ class ReconciliateJarModelView(viewsets.ModelViewSet):
|
||||
queryset = ReconciliationJar.objects.all().order_by('-date_time')
|
||||
pagination_class = Pagination
|
||||
serializer_class = ReconciliationJarSerializer
|
||||
|
||||
|
||||
class SalesForTrytonView(APIView):
|
||||
def get(self, request):
|
||||
sales = Sale.objects.all()
|
||||
csv = self._generate_sales_CSV(sales)
|
||||
return Response({'csv': csv})
|
||||
|
||||
def _generate_sales_CSV(self, sales):
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output)
|
||||
csv_data = sales_to_tryton_csv(sales)
|
||||
|
||||
for row in csv_data:
|
||||
writer.writerow(row)
|
||||
return output.getvalue()
|
||||
|
@ -1,4 +1,7 @@
|
||||
import json
|
||||
import csv
|
||||
import io
|
||||
|
||||
from django.urls import reverse
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
@ -45,6 +48,58 @@ class TestAPI(APITestCase):
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(self.customer.name, json_response[0]['name'])
|
||||
|
||||
def test_get_sales_for_tryton(self):
|
||||
url = '/don_confiao/api/sales/for_tryton'
|
||||
self._create_sale()
|
||||
response = self.client.get(url)
|
||||
json_response = json.loads(response.content.decode('utf-8'))
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertIn('csv', json_response)
|
||||
self.assertGreater(len(json_response['csv']), 0)
|
||||
|
||||
def test_csv_structure_in_sales_for_tryton(self):
|
||||
url = '/don_confiao/api/sales/for_tryton'
|
||||
self._create_sale()
|
||||
response = self.client.get(url)
|
||||
json_response = json.loads(response.content.decode('utf-8'))
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
csv_reader = csv.reader(io.StringIO(json_response['csv']))
|
||||
expected_header = [
|
||||
"Tercero",
|
||||
"Dirección de facturación",
|
||||
"Dirección de envío",
|
||||
"Descripción",
|
||||
"Referencia",
|
||||
"Fecha venta",
|
||||
"Plazo de pago",
|
||||
"Almacén",
|
||||
"Moneda",
|
||||
"Líneas/Producto",
|
||||
"Líneas/Cantidad",
|
||||
"Líneas/Precio unitario",
|
||||
"Líneas/Unidad",
|
||||
"Empresa",
|
||||
"Tienda",
|
||||
"Terminal de venta",
|
||||
"Autorecogida",
|
||||
"Comentario"
|
||||
]
|
||||
self.assertEqual(next(csv_reader), expected_header)
|
||||
|
||||
expected_rows = [
|
||||
[self.customer.name, self.customer.name, self.customer.name, "",
|
||||
"", "2024-09-02 00:00:00+00:00", "Contado", "Almacén",
|
||||
"Peso colombiano", self.product.name, "2", "3000.00", "Unidad",
|
||||
"TIENDA LA ILUSIÓN", "Tienda La Ilusion", "La Ilusion", "True", ""
|
||||
],
|
||||
["", "", "", "", "", "", "", "", "", self.product.name, "3",
|
||||
"5000.00", "Unidad", "", "", "", "", ""
|
||||
],
|
||||
]
|
||||
rows = list(csv_reader)
|
||||
self.assertEqual(rows, expected_rows)
|
||||
|
||||
def _create_sale(self):
|
||||
url = '/don_confiao/api/sales/'
|
||||
data = {
|
||||
|
@ -30,5 +30,6 @@ urlpatterns = [
|
||||
path('purchases/for_reconciliation', api_views.SalesForReconciliationView.as_view(), name='sales_for_reconciliation'),
|
||||
path('reconciliate_jar', api_views.ReconciliateJarView.as_view()),
|
||||
path('api/admin_code/validate/<code>', api_views.AdminCodeValidateView.as_view()),
|
||||
path('api/sales/for_tryton', api_views.SalesForTrytonView.as_view()),
|
||||
path('api/', include(router.urls)),
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user