view: calculando resumen de compra.
This commit is contained in:
		@@ -34,6 +34,10 @@ class PurchaseLineForm(forms.ModelForm):
 | 
			
		||||
            "description",
 | 
			
		||||
        ]
 | 
			
		||||
 | 
			
		||||
class PurchaseSummaryForm(forms.Form):
 | 
			
		||||
    quantity_lines = forms.IntegerField()
 | 
			
		||||
    quantity_products = forms.IntegerField()
 | 
			
		||||
    ammount = forms.DecimalField(max_digits=10, decimal_places=2)
 | 
			
		||||
 | 
			
		||||
LineaFormSet = inlineformset_factory(
 | 
			
		||||
    Sale,
 | 
			
		||||
@@ -42,7 +46,6 @@ LineaFormSet = inlineformset_factory(
 | 
			
		||||
    fields='__all__'
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ReconciliationJarForm(forms.ModelForm):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = ReconciliationJar
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										33
									
								
								tienda_ilusion/don_confiao/static/js/sale_summary.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								tienda_ilusion/don_confiao/static/js/sale_summary.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
const complete_form = document.getElementById('complete_form_purchase')
 | 
			
		||||
const quantityLines = document.getElementById('id_quantity_lines');
 | 
			
		||||
const quantityProducts = document.getElementById('id_quantity_products');
 | 
			
		||||
const ammountInput = document.getElementById('id_ammount');
 | 
			
		||||
const idPrefix = 'id_saleline_set-';
 | 
			
		||||
 | 
			
		||||
complete_form.addEventListener('change', function(event){
 | 
			
		||||
    if (event.target.matches('[id^="${idPrefix}"][id$="-quantity"]')) {
 | 
			
		||||
        calculateSummary();
 | 
			
		||||
    }
 | 
			
		||||
    if (event.target.matches('[id^="id_saleline_set-"][id$="-unit_price"]')) {
 | 
			
		||||
        calculateSummary();
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
function calculateSummary() {
 | 
			
		||||
    let quantity = 0;
 | 
			
		||||
    let ammount = 0;
 | 
			
		||||
    const quantityInputs = document.querySelectorAll('[id^="id_saleline_set-"][id$="-quantity"]');
 | 
			
		||||
    const ids = Array.prototype.map.call(quantityInputs, function(input) {
 | 
			
		||||
        return input.id.match(/\d+/)[0];
 | 
			
		||||
    });
 | 
			
		||||
    ids.forEach(function(id) {
 | 
			
		||||
        let lineQuantity = document.getElementById(`id_saleline_set-${id}-quantity`)
 | 
			
		||||
        let linePrice = document.getElementById(`id_saleline_set-${id}-unit_price`)
 | 
			
		||||
        quantity += parseFloat(lineQuantity.value);
 | 
			
		||||
        ammount += parseFloat(linePrice.value) * parseFloat(lineQuantity.value);
 | 
			
		||||
    });
 | 
			
		||||
    quantityProducts.value = quantity;
 | 
			
		||||
    quantityLines.value = quantityInputs.length;
 | 
			
		||||
    ammountInput.value = ammount;
 | 
			
		||||
}
 | 
			
		||||
@@ -1,8 +1,8 @@
 | 
			
		||||
<!doctype html>
 | 
			
		||||
{% load static %}
 | 
			
		||||
<form method="POST">
 | 
			
		||||
<form id="complete_form_purchase" method="POST">
 | 
			
		||||
  {% csrf_token %}
 | 
			
		||||
  {{ sale_form}}
 | 
			
		||||
  {{ sale_form }}
 | 
			
		||||
  {{ linea_formset.management_form }}
 | 
			
		||||
  <div id="formset-container">
 | 
			
		||||
    {% for form in linea_formset %}
 | 
			
		||||
@@ -13,7 +13,9 @@
 | 
			
		||||
      </div>
 | 
			
		||||
    {% endfor %}
 | 
			
		||||
  </div>
 | 
			
		||||
  {{ summary_form }}
 | 
			
		||||
  <button id="add_line" type="button" onclick="add_line">Añadir Linea</button>
 | 
			
		||||
  <br/><button name="form" type="submit" >Comprar</button>
 | 
			
		||||
  <script src="{% static 'js/add_line.js' %}"></script>
 | 
			
		||||
  <script src="{% static 'js/sale_summary.js' %}"></script>
 | 
			
		||||
</form>
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,32 @@
 | 
			
		||||
from django.test import Client, TestCase
 | 
			
		||||
 | 
			
		||||
from ..models import Payment, Sale
 | 
			
		||||
 | 
			
		||||
class TestPurchaseWithPayment(TestCase):
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
        self.client = Client()
 | 
			
		||||
 | 
			
		||||
    def test_generate_payment_when_it_has_payment(self):
 | 
			
		||||
        response = self.client.post(
 | 
			
		||||
            '/don_confiao/comprar',
 | 
			
		||||
            {
 | 
			
		||||
                "customer": "Noelba Lopez",
 | 
			
		||||
                "date": "2024-07-27",
 | 
			
		||||
                "phone": "3010101000",
 | 
			
		||||
                "description": "Venta de contado",
 | 
			
		||||
                "saleline_set-TOTAL_FORMS": "1",
 | 
			
		||||
                "saleline_set-INITIAL_FORMS": "0",
 | 
			
		||||
                "saleline_set-MIN_NUM_FORMS": "0",
 | 
			
		||||
                "saleline_set-MAX_NUM_FORMS": "1000",
 | 
			
		||||
                "saleline_set-0-product": "Papayita",
 | 
			
		||||
                "saleline_set-0-quantity": "2",
 | 
			
		||||
                "saleline_set-0-unit_price": "22030",
 | 
			
		||||
                "saleline_set-0-description": "Linea de Venta",
 | 
			
		||||
                "saleline_set-0-sale": "",
 | 
			
		||||
                "saleline_set-0-id": "",
 | 
			
		||||
            }
 | 
			
		||||
        )
 | 
			
		||||
        purchases = Sale.objects.all()
 | 
			
		||||
        self.assertEqual(1, len(purchases))
 | 
			
		||||
        # payments = Payment.objects.all()
 | 
			
		||||
        # self.assertEqual(1, len(payments))
 | 
			
		||||
@@ -5,7 +5,7 @@ from django.core.exceptions import ValidationError
 | 
			
		||||
from django.views.generic import ListView
 | 
			
		||||
 | 
			
		||||
from .models import Sale, Product, ProductCategory, Payment
 | 
			
		||||
from .forms import ImportProductsForm, PurchaseForm, LineaFormSet, ReconciliationJarForm
 | 
			
		||||
from .forms import ImportProductsForm, PurchaseForm, LineaFormSet, ReconciliationJarForm, PurchaseSummaryForm
 | 
			
		||||
 | 
			
		||||
import csv
 | 
			
		||||
import io
 | 
			
		||||
@@ -29,12 +29,14 @@ def buy(request):
 | 
			
		||||
    else:
 | 
			
		||||
        sale_form = PurchaseForm()
 | 
			
		||||
        sale_linea_form = LineaFormSet()
 | 
			
		||||
        sale_summary_form = PurchaseSummaryForm()
 | 
			
		||||
    return render(
 | 
			
		||||
        request,
 | 
			
		||||
        'don_confiao/purchase.html',
 | 
			
		||||
        {
 | 
			
		||||
            'sale_form': sale_form,
 | 
			
		||||
            'linea_formset': sale_linea_form
 | 
			
		||||
            'linea_formset': sale_linea_form,
 | 
			
		||||
            'summary_form': sale_summary_form,
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user