def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context['title'] = 'Creación de una Venta'
     context['entity'] = 'Ventas'
     context['list_url'] = self.success_url
     context['action'] = 'add'
     context['det'] = []
     context['frmClient'] = ClientForm()
     return context
Beispiel #2
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context['title'] = 'Edición de una Venta'
     context['entity'] = 'Ventas'
     context['list_url'] = self.success_url
     context['action'] = 'edit'
     context['det'] = json.dumps(self.get_details_product())
     context['frmClient'] = ClientForm()
     return context
Beispiel #3
0
 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)