Merge pull request 'Agregando precio del producto al seleccionar el producto #24' (#30) from automatic_product_price_#24 into main

Reviewed-on: OneTeam/don_confiao#30
This commit is contained in:
mono 2024-08-31 13:01:54 -05:00
commit 02fbf51659
6 changed files with 80 additions and 17 deletions

View File

@ -37,6 +37,21 @@ class Product(models.Model):
def __str__(self):
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):
customer = models.ForeignKey(Customer, on_delete=models.PROTECT)

View File

@ -25,5 +25,6 @@ document.addEventListener('DOMContentLoaded', function(){
formContainer.appendChild(newForm);
totalForms.value = formCount + 1;
setPriceListeners();
});
});

View 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 = '';
}
}

View File

@ -1,6 +1,9 @@
{% extends 'don_confiao/base.html' %}
{% block content %}
{% load static %}
<script>
let listProducts = JSON.parse("{{ list_products|escapejs }}");
</script>
<div class="flex h-full">
<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">
@ -12,12 +15,12 @@
<table class="w-3/4 my-5 shadow-inner" style="border: solid 1px #178E79;">
{{ form.as_table }}
</table>
</div>
</div>
{% endfor %}
</div>
<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>
</div>
<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">Añadir Linea</button>
</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">
<p id="sale_resume_title" class="text-center decoration-solid font-mono font-bold text-xl page_title">Resumen de Venta</p>
@ -25,9 +28,11 @@
{{ summary_form }}
<button class="font-bold my-10 py-2 px-4 rounded-full bg-yellow-400 shadow hover:shadow-lg hover:bg-violet-200 ease-in duration-150" name="form" type="submit" >Comprar</button>
</div>
</form>
</form>
</div>
<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/sale_summary.js' %}"></script>
<script src="{% static 'js/calculate_subtotal_line.js' %}"></script>

View 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)

View File

@ -8,8 +8,16 @@ from .forms import ImportProductsForm, PurchaseForm, SaleLineFormSet, Reconcilia
import csv
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):
return render(request, 'don_confiao/index.html')
@ -47,6 +55,7 @@ def buy(request):
'sale_form': sale_form,
'linea_formset': line_formset,
'summary_form': sale_summary_form,
'list_products': json.dumps(Product.to_list(), cls=DecimalEncoder),
}
)
@ -60,18 +69,7 @@ def purchases(request):
def products(request):
rproducts = []
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)
return JsonResponse(Product.to_list(), safe=False)
def import_products(request):