Agregando precio del producto al seleccionar el producto #24 #30
@@ -37,6 +37,21 @@ class Product(models.Model):
 | 
				
			|||||||
    def __str__(self):
 | 
					    def __str__(self):
 | 
				
			||||||
        return self.name
 | 
					        return self.name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @classmethod
 | 
				
			||||||
 | 
					    def to_list(cls):
 | 
				
			||||||
 | 
					        products_list = []
 | 
				
			||||||
 | 
					        all_products = cls.objects.all()
 | 
				
			||||||
 | 
					        for product in all_products:
 | 
				
			||||||
 | 
					            rproduct = {
 | 
				
			||||||
 | 
					                "id": product.id,
 | 
				
			||||||
 | 
					                "name": product.name,
 | 
				
			||||||
 | 
					                "price_list": product.price,
 | 
				
			||||||
 | 
					                "uom": product.measuring_unit,
 | 
				
			||||||
 | 
					                "categories": [c.name for c in product.categories.all()]
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            products_list.append(rproduct)
 | 
				
			||||||
 | 
					        return products_list
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Sale(models.Model):
 | 
					class Sale(models.Model):
 | 
				
			||||||
    customer = models.ForeignKey(Customer, on_delete=models.PROTECT)
 | 
					    customer = models.ForeignKey(Customer, on_delete=models.PROTECT)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,5 +25,6 @@ document.addEventListener('DOMContentLoaded', function(){
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    formContainer.appendChild(newForm);
 | 
					    formContainer.appendChild(newForm);
 | 
				
			||||||
    totalForms.value = formCount + 1;
 | 
					    totalForms.value = formCount + 1;
 | 
				
			||||||
 | 
					    setPriceListeners();
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										22
									
								
								tienda_ilusion/don_confiao/static/js/buy_general.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								tienda_ilusion/don_confiao/static/js/buy_general.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
				
			|||||||
 | 
					setPriceListeners();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function setPriceListeners() {
 | 
				
			||||||
 | 
					    document.querySelectorAll('select[id^="id_saleline_set-"][id$="-product"]').forEach((input) => {
 | 
				
			||||||
 | 
					        input.addEventListener('change', (e) => setLinePrice(e));
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function setLinePrice(e) {
 | 
				
			||||||
 | 
					    let input = e.target;
 | 
				
			||||||
 | 
					    const idLine = input.id.split('-')[1];
 | 
				
			||||||
 | 
					    const productId = input.value;
 | 
				
			||||||
 | 
					    const priceInput = document.getElementById(`id_saleline_set-${idLine}-unit_price`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const product = listProducts.find((product) => product.id == productId);
 | 
				
			||||||
 | 
					    if (product) {
 | 
				
			||||||
 | 
					        priceInput.value = product.price_list;
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        priceInput.value = '';
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1,6 +1,9 @@
 | 
				
			|||||||
{% extends 'don_confiao/base.html' %}
 | 
					{% extends 'don_confiao/base.html' %}
 | 
				
			||||||
{% block content %}
 | 
					{% block content %}
 | 
				
			||||||
{% load static %}
 | 
					{% load static %}
 | 
				
			||||||
 | 
					<script>
 | 
				
			||||||
 | 
					  let listProducts = JSON.parse("{{ list_products|escapejs }}");
 | 
				
			||||||
 | 
					</script>
 | 
				
			||||||
<div class="flex h-full">
 | 
					<div class="flex h-full">
 | 
				
			||||||
  <div class="h-full w-10/12 flex flex-col p-5">
 | 
					  <div class="h-full w-10/12 flex flex-col p-5">
 | 
				
			||||||
    <form id="complete_form_purchase" method="POST" class="h-10/12 w-full max-h-full overflow-auto">
 | 
					    <form id="complete_form_purchase" method="POST" class="h-10/12 w-full max-h-full overflow-auto">
 | 
				
			||||||
@@ -16,7 +19,7 @@
 | 
				
			|||||||
        {% endfor %}
 | 
					        {% endfor %}
 | 
				
			||||||
      </div>
 | 
					      </div>
 | 
				
			||||||
    <div class="h-2/12 flex justify-center">
 | 
					    <div class="h-2/12 flex justify-center">
 | 
				
			||||||
        <button id="add_line" type="button" class="bg-yellow-400 shadow hover:shadow-lg py-2 px-5 rounded-full font-bold hover:bg-violet-200 ease-in duration-150" onclick="add_line">Añadir Linea</button>
 | 
					      <button id="add_line" type="button" class="bg-yellow-400 shadow hover:shadow-lg py-2 px-5 rounded-full font-bold hover:bg-violet-200 ease-in duration-150">Añadir Linea</button>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
  </div>
 | 
					  </div>
 | 
				
			||||||
  <div class="h-full w-3/12 bg-green-400 p-5 shadow hover:shadow-lg flex flex-col gap-y-3 font-semibold justify-around">
 | 
					  <div class="h-full w-3/12 bg-green-400 p-5 shadow hover:shadow-lg flex flex-col gap-y-3 font-semibold justify-around">
 | 
				
			||||||
@@ -27,7 +30,9 @@
 | 
				
			|||||||
  </div>
 | 
					  </div>
 | 
				
			||||||
      </form>
 | 
					      </form>
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script src="https://cdn.tailwindcss.com/"></script>
 | 
					<script src="https://cdn.tailwindcss.com/"></script>
 | 
				
			||||||
 | 
					<script src="{% static 'js/buy_general.js' %}"></script>
 | 
				
			||||||
<script src="{% static 'js/add_line.js' %}"></script>
 | 
					<script src="{% static 'js/add_line.js' %}"></script>
 | 
				
			||||||
<script src="{% static 'js/sale_summary.js' %}"></script>
 | 
					<script src="{% static 'js/sale_summary.js' %}"></script>
 | 
				
			||||||
<script src="{% static 'js/calculate_subtotal_line.js' %}"></script>
 | 
					<script src="{% static 'js/calculate_subtotal_line.js' %}"></script>
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										22
									
								
								tienda_ilusion/don_confiao/tests/test_buy_form.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								tienda_ilusion/don_confiao/tests/test_buy_form.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
				
			|||||||
 | 
					from django.test import Client, TestCase
 | 
				
			||||||
 | 
					from ..models import Product
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestBuyForm(TestCase):
 | 
				
			||||||
 | 
					    def setUp(self):
 | 
				
			||||||
 | 
					        self.client = Client()
 | 
				
			||||||
 | 
					        self.product = Product()
 | 
				
			||||||
 | 
					        self.product.name = "Arroz"
 | 
				
			||||||
 | 
					        self.product.price = 5000
 | 
				
			||||||
 | 
					        self.product.save()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_buy_contains_products_list(self):
 | 
				
			||||||
 | 
					        response = self.client.get('/don_confiao/comprar')
 | 
				
			||||||
 | 
					        self.assertIn(
 | 
				
			||||||
 | 
					            self.product.name,
 | 
				
			||||||
 | 
					            response.context['list_products']
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        content = response.content.decode('utf-8')
 | 
				
			||||||
 | 
					        self.assertIn('5000', content)
 | 
				
			||||||
 | 
					        self.assertIn('Arroz', content)
 | 
				
			||||||
 | 
					        self.assertIn(str(self.product.id), content)
 | 
				
			||||||
@@ -8,8 +8,16 @@ from .forms import ImportProductsForm, PurchaseForm, SaleLineFormSet, Reconcilia
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import csv
 | 
					import csv
 | 
				
			||||||
import io
 | 
					import io
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					from decimal import Decimal
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class DecimalEncoder(json.JSONEncoder):
 | 
				
			||||||
 | 
					    def default(self, obj):
 | 
				
			||||||
 | 
					        if isinstance(obj, Decimal):
 | 
				
			||||||
 | 
					            return float(obj)
 | 
				
			||||||
 | 
					        return json.JSONEncoder.default(self, obj)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def index(request):
 | 
					def index(request):
 | 
				
			||||||
    return render(request, 'don_confiao/index.html')
 | 
					    return render(request, 'don_confiao/index.html')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -47,6 +55,7 @@ def buy(request):
 | 
				
			|||||||
            'sale_form': sale_form,
 | 
					            'sale_form': sale_form,
 | 
				
			||||||
            'linea_formset': line_formset,
 | 
					            'linea_formset': line_formset,
 | 
				
			||||||
            'summary_form': sale_summary_form,
 | 
					            'summary_form': sale_summary_form,
 | 
				
			||||||
 | 
					            'list_products': json.dumps(Product.to_list(), cls=DecimalEncoder),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -60,18 +69,7 @@ def purchases(request):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def products(request):
 | 
					def products(request):
 | 
				
			||||||
    rproducts = []
 | 
					    return JsonResponse(Product.to_list(), safe=False)
 | 
				
			||||||
    products = Product.objects.all()
 | 
					 | 
				
			||||||
    for product in products:
 | 
					 | 
				
			||||||
        rproduct = {
 | 
					 | 
				
			||||||
            "name": product.name,
 | 
					 | 
				
			||||||
            "price_list": product.price,
 | 
					 | 
				
			||||||
            "uom": product.measuring_unit,
 | 
					 | 
				
			||||||
            "categories": [c.name for c in product.categories.all()]
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        rproducts.append(rproduct)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return JsonResponse(rproducts, safe=False)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def import_products(request):
 | 
					def import_products(request):
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user