diff --git a/tienda_ilusion/don_confiao/forms.py b/tienda_ilusion/don_confiao/forms.py index a9ef913..4db321a 100644 --- a/tienda_ilusion/don_confiao/forms.py +++ b/tienda_ilusion/don_confiao/forms.py @@ -1,4 +1,39 @@ from django import forms +from django.forms.widgets import DateInput + +from .models import Sale, SaleLine + class ImportProductsForm(forms.Form): csv_file = forms.FileField() + +class PurchaseForm(forms.ModelForm): + class Meta: + model = Sale + fields = [ + "customer", + "date", + "phone", + "description", + ] + widgets = { + 'date': DateInput(attrs={'type': 'date'}) + } + +class PurchaseLineForm(forms.ModelForm): + class Meta: + model = SaleLine + fields = [ + "product", + "quantity", + "unit_price", + "description", + ] + + +LineaFormSet = forms.models.inlineformset_factory( + Sale, + SaleLine, + extra=2, + fields='__all__' +) diff --git a/tienda_ilusion/don_confiao/models.py b/tienda_ilusion/don_confiao/models.py index 6d40cac..7d7ee0a 100644 --- a/tienda_ilusion/don_confiao/models.py +++ b/tienda_ilusion/don_confiao/models.py @@ -8,6 +8,9 @@ class Sale(models.Model): phone = models.CharField(max_length=13, null=True, blank=True) description = models.CharField(max_length=255, null=True, blank=True) + def __str__(self): + return f"{self.date} {self.customer}" + class SaleLine(models.Model): @@ -17,6 +20,9 @@ class SaleLine(models.Model): unit_price = models.DecimalField(max_digits=9, decimal_places=2) description = models.CharField(max_length=255, null=True, blank=True) + def __str__(self): + return f"{self.sale} - {self.product}" + class MeasuringUnits(models.TextChoices): UNIT = 'UNIT', _('Unit') diff --git a/tienda_ilusion/don_confiao/templates/don_confiao/purchase.html b/tienda_ilusion/don_confiao/templates/don_confiao/purchase.html index 71b860f..0d2c2f4 100644 --- a/tienda_ilusion/don_confiao/templates/don_confiao/purchase.html +++ b/tienda_ilusion/don_confiao/templates/don_confiao/purchase.html @@ -1,43 +1,12 @@ -
+ {% csrf_token %} -
- Purchase - : - - - : - - - : - - -
- Purchase Line - : - - - : - - - : - - - : - -
-
-
- : - - - - -
-
- -
+ {{ sale_form}} + {{ linea_formset.management_form }} + {% for form in linea_formset %} + + {{ form.as_table }} +
+ {% endfor %} +
diff --git a/tienda_ilusion/don_confiao/views.py b/tienda_ilusion/don_confiao/views.py index 671a934..1904ab6 100644 --- a/tienda_ilusion/don_confiao/views.py +++ b/tienda_ilusion/don_confiao/views.py @@ -3,7 +3,7 @@ from django.http import HttpResponse, HttpResponseRedirect, JsonResponse from django.template import loader from .models import Sale, Product, ProductCategory -from .forms import ImportProductsForm +from .forms import ImportProductsForm, PurchaseForm, LineaFormSet import csv import io @@ -11,16 +11,26 @@ import io def index(request): return render(request, 'don_confiao/index.html') - def buy(request): - context = {} - if request.POST: - raise Exception(request.POST) + if request.method == "POST": + sale_form = PurchaseForm(request.POST) + sale_linea_form = LineaFormSet(request.POST) + if sale_form.is_valid(): + sale_form.save() + if sale_linea_form.is_valid(): + sale_linea_form.save() + return HttpResponseRedirect("productos") else: - raise Exception(request.POST) + sale_form = PurchaseForm() + sale_linea_form = LineaFormSet() return render( - request, 'don_confiao/purchase.html', context) - + request, + 'don_confiao/purchase.html', + { + 'sale_form': sale_form, + 'linea_formset': sale_linea_form + } + ) def purchases(request): purchases = Sale.objects.all()