def post(self, request, *args, **kwargs): data = {} try: action = request.POST['action'] if action == 'search_products': data = [] prods = Product.objects.filter( name__icontains=request.POST['term'])[0:10] for i in prods: item = i.toJSON() item['value'] = i.name data.append(item) elif action == 'edit': with transaction.atomic(): vents = json.loads(request.POST['vents']) # sale = Sale.objects.get(pk=self.get_object().id) sale = self.get_object() sale.date_joined = vents['date_joined'] sale.cli_id = vents['cli'] sale.subtotal = float(vents['subtotal']) sale.iva = float(vents['iva']) sale.total = float(vents['total']) sale.save() sale.detsale_set.all().delete() for i in vents['products']: det = DetSale() det.sale_id = sale.id det.prod_id = i['id'] det.cant = int(i['cant']) det.price = float(i['pvp']) det.subtotal = float(i['subtotal']) det.save() data = {'id': sale.id} elif action == 'search_clients': data = [] term = request.POST['term'] clients = Client.objects.filter( Q(names__icontains=term) | Q(surnames__icontains=term) | Q(dni__icontains=term))[0:10] for i in clients: item = i.toJSON() item['text'] = i.get_full_name() data.append(item) elif action == 'create_client': with transaction.atomic(): frmClient = ClientForm(request.POST) data = frmClient.save() else: data['error'] = 'No ha ingresado a ninguna opción' except Exception as e: data['error'] = str(e) return JsonResponse(data, safe=False)
def post(self, request, *args, **kwargs): data = {} try: action = request.POST['action'] if action == 'search_products': data = [] prods = Product.objects.filter( name__icontains=request.POST['term'])[0:10] for i in prods: item = i.toJSON() # item['value'] = i.name item['text'] = i.name data.append(item) elif action == 'add': with transaction.atomic(): vents = json.loads(request.POST['vents']) sale = Sale() sale.date_joined = vents['date_joined'] sale.cli_id = vents['cli'] sale.subtotal = float(vents['subtotal']) sale.iva = float(vents['iva']) sale.total = float(vents['total']) sale.save() for i in vents['products']: det = DetSale() det.sale_id = sale.id det.prod_id = i['id'] det.cant = int(i['cant']) det.price = float(i['pvp']) det.subtotal = float(i['subtotal']) det.save() data = {'id': sale.id} else: data['error'] = 'No ha ingresado a ninguna opción' except Exception as e: data['error'] = str(e) return JsonResponse(data, safe=False)
def post(self, request, *args, **kwargs): data = {} try: action = request.POST['action'] if action == 'search_products': data=[] prods = Product.objects.filter(name__icontains=request.POST['term'])[0:10] #guardo los productos que vienen por el form.js en la variable term pero solo diez productos for i in prods: item = i.toJson() item['value'] = i.name #el autocomplete tiene una variable value que necesita para poder presentarse en la busqueda cuando se va tecleando data.append(item) #agrego el producto iterado al array data elif action == 'add': with transaction.atomic(): #meto toda la lógica dentro del transaction, esto me permite volver atrás en caso de que ocurra un error en el detalle o la factura, entonces no se guarda nada en caso de error. #venta vents = json.loads(request.POST['vents']) sale = Sale() sale.date_joined = vents['date_joined'] sale.client_id = vents['client'] sale.subtotal = float(vents['subtotal']) sale.iva = float(vents['iva']) sale.total = float(vents['total']) sale.save() #detalle de venta for i in vents['products']: detalle = DetSale() detalle.sale_id = sale.id detalle.prod_id = i['id'] detalle.cant = int(i['cant']) detalle.price = float(i['pvp']) detalle.subtotal = float(i['subtotal']) detalle.save() else: data['error'] = 'No ha ingresado a ninguna opción' except Exception as e: data['error'] = str(e) return JsonResponse(data, safe=False) #para que se pueda serializar indico que el safe=false
def post(self, request, *args, **kwargs): data = {} try: action = request.POST['action'] if action == 'search_products': data = [] ids_exclude = json.loads(request.POST['ids']) term = request.POST['term'].strip() products = Product.objects.filter(stock__gt=0) if len(term): products = products.filter(name__icontains=term) for i in products.exclude(id__in=ids_exclude)[0:10]: item = i.toJSON() item['value'] = i.name # item['text'] = i.name data.append(item) elif action == 'search_autocomplete': data = [] ids_exclude = json.loads(request.POST['ids']) term = request.POST['term'].strip() data.append({'id': term, 'text': term}) products = Product.objects.filter(name__icontains=term, stock__gt=0) for i in products.exclude(id__in=ids_exclude)[0:10]: item = i.toJSON() item['text'] = i.name data.append(item) elif action == 'add': with transaction.atomic(): vents = json.loads(request.POST['vents']) sale = Sale() sale.date_joined = vents['date_joined'] sale.cli_id = vents['cli'] sale.subtotal = float(vents['subtotal']) sale.iva = float(vents['iva']) sale.total = float(vents['total']) sale.save() for i in vents['products']: det = DetSale() det.sale_id = sale.id det.prod_id = i['id'] det.cant = int(i['cant']) det.price = float(i['pvp']) det.subtotal = float(i['subtotal']) det.save() det.prod.stock -= det.cant det.prod.save() data = {'id': sale.id} elif action == 'search_clients': data = [] term = request.POST['term'] clients = Client.objects.filter( Q(names__icontains=term) | Q(surnames__icontains=term) | Q(dni__icontains=term))[0:10] for i in clients: item = i.toJSON() item['text'] = i.get_full_name() data.append(item) elif action == 'create_client': with transaction.atomic(): frmClient = ClientForm(request.POST) data = frmClient.save() else: data['error'] = 'No ha ingresado a ninguna opción' except Exception as e: data['error'] = str(e) return JsonResponse(data, safe=False)
from django.db.models import Sum from django.db.models.functions import Coalesce from core.erp.models import Sale, DetSale for m in range(0, 6): pedids = random.randint(18, 29) for d in range(1, pedids): vent = Sale() vent.cli_id = random.randint(1, 3) vent.date_joined = datetime(2020, m + 1, d) vent.save() food = random.randint(1, 10) for i in range(0, food): det = DetSale() det.sale_id = vent.id det.prod_id = random.randint(1, 23) det.price = det.prod.pvp det.cant = random.randint(1, 4) det.subtotal = float(det.price) * det.cant det.save() vent.subtotal = vent.detsale_set.all().aggregate( r=Coalesce(Sum('subtotal'), 0)).get('r') vent.iva = float(vent.subtotal) * 0.12 vent.total = float(vent.subtotal) + float(vent.iva) vent.save() print('Terminado')