Merge pull request 'purchase_view_a_formularios_de_django' (#8) from purchase_view_a_formularios_de_django into main
Reviewed-on: OneTeam/don_confiao#8
This commit is contained in:
		@@ -1,4 +1,39 @@
 | 
				
			|||||||
from django import forms
 | 
					from django import forms
 | 
				
			||||||
 | 
					from django.forms.widgets import DateInput
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from .models import Sale, SaleLine
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ImportProductsForm(forms.Form):
 | 
					class ImportProductsForm(forms.Form):
 | 
				
			||||||
    csv_file = forms.FileField()
 | 
					    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__'
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,6 +8,9 @@ class Sale(models.Model):
 | 
				
			|||||||
    phone = models.CharField(max_length=13, null=True, blank=True)
 | 
					    phone = models.CharField(max_length=13, null=True, blank=True)
 | 
				
			||||||
    description = models.CharField(max_length=255, 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):
 | 
					class SaleLine(models.Model):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -17,6 +20,9 @@ class SaleLine(models.Model):
 | 
				
			|||||||
    unit_price = models.DecimalField(max_digits=9, decimal_places=2)
 | 
					    unit_price = models.DecimalField(max_digits=9, decimal_places=2)
 | 
				
			||||||
    description = models.CharField(max_length=255, null=True, blank=True)
 | 
					    description = models.CharField(max_length=255, null=True, blank=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __str__(self):
 | 
				
			||||||
 | 
					        return f"{self.sale} - {self.product}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class MeasuringUnits(models.TextChoices):
 | 
					class MeasuringUnits(models.TextChoices):
 | 
				
			||||||
    UNIT = 'UNIT', _('Unit')
 | 
					    UNIT = 'UNIT', _('Unit')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,43 +1,12 @@
 | 
				
			|||||||
<!doctype html>
 | 
					<!doctype html>
 | 
				
			||||||
<form action="{% url 'buy'%}" method="POST">
 | 
					<form method="POST">
 | 
				
			||||||
    {% csrf_token %}
 | 
					    {% csrf_token %}
 | 
				
			||||||
    <fieldset>
 | 
					        {{ sale_form}}
 | 
				
			||||||
        <legend>Purchase</legend>
 | 
					  {{ linea_formset.management_form }}
 | 
				
			||||||
        <label for="customer">Customer</label>:
 | 
					  {% for form in linea_formset %}
 | 
				
			||||||
        <input type="search" name="customer" id="customer"/>
 | 
					    <table style="border: solid 1px blue; margin: 10px">
 | 
				
			||||||
 | 
					        {{ form.as_table }}
 | 
				
			||||||
        <label for="date">Date</label>:
 | 
					    </table>
 | 
				
			||||||
        <input type="date" name="date" id="date"/>
 | 
					    {% endfor %}
 | 
				
			||||||
 | 
					    <br/><button name="form" type="submit" >Comprar</button>
 | 
				
			||||||
        <label for="description">Description</label>:
 | 
					 | 
				
			||||||
        <input type="text" name="description" id="description"/>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        <fieldset>
 | 
					 | 
				
			||||||
            <legend>Purchase Line</legend>
 | 
					 | 
				
			||||||
            <label for="product">Product</label>:
 | 
					 | 
				
			||||||
            <input type="search" name="product" id="product"/>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            <label for="unit_price">Unit Price</label>:
 | 
					 | 
				
			||||||
            <input type="number" name="unit_price" min="0" id="unit_price"/>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            <label for="quantity">Quantity</label>:
 | 
					 | 
				
			||||||
            <input type="number" name="quantity" min="0" id="quantity"/>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            <label for="subtotal">Subtotal</label>:
 | 
					 | 
				
			||||||
            <input type="number" name="subtotal" min="0" id="line_amount"/>
 | 
					 | 
				
			||||||
        </fieldset>
 | 
					 | 
				
			||||||
    </fieldset>
 | 
					 | 
				
			||||||
    <fieldset>
 | 
					 | 
				
			||||||
        <label for="total_amount">Total</label>:
 | 
					 | 
				
			||||||
        <input type="number" name="total_amount" min="0" id="total_amount"/>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        <label for="Paid">Paid:</label>
 | 
					 | 
				
			||||||
        <select id="paid" name="paid">
 | 
					 | 
				
			||||||
            <option value="yes">Yes</option>
 | 
					 | 
				
			||||||
            <option value="no">No</option>
 | 
					 | 
				
			||||||
        </select>
 | 
					 | 
				
			||||||
    </fieldset>
 | 
					 | 
				
			||||||
    <div class="button-container">
 | 
					 | 
				
			||||||
		<button name="form" type="submit">Enviar</button>
 | 
					 | 
				
			||||||
	</div>
 | 
					 | 
				
			||||||
</form>
 | 
					</form>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,7 +3,7 @@ from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
 | 
				
			|||||||
from django.template import loader
 | 
					from django.template import loader
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from .models import Sale, Product, ProductCategory
 | 
					from .models import Sale, Product, ProductCategory
 | 
				
			||||||
from .forms import ImportProductsForm
 | 
					from .forms import ImportProductsForm, PurchaseForm, LineaFormSet
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import csv
 | 
					import csv
 | 
				
			||||||
import io
 | 
					import io
 | 
				
			||||||
@@ -11,16 +11,26 @@ import io
 | 
				
			|||||||
def index(request):
 | 
					def index(request):
 | 
				
			||||||
    return render(request, 'don_confiao/index.html')
 | 
					    return render(request, 'don_confiao/index.html')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
def buy(request):
 | 
					def buy(request):
 | 
				
			||||||
    context = {}
 | 
					    if request.method == "POST":
 | 
				
			||||||
    if request.POST:
 | 
					        sale_form = PurchaseForm(request.POST)
 | 
				
			||||||
        raise Exception(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:
 | 
					    else:
 | 
				
			||||||
        raise Exception(request.POST)
 | 
					        sale_form = PurchaseForm()
 | 
				
			||||||
 | 
					        sale_linea_form = LineaFormSet()
 | 
				
			||||||
    return render(
 | 
					    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):
 | 
					def purchases(request):
 | 
				
			||||||
    purchases = Sale.objects.all()
 | 
					    purchases = Sale.objects.all()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user