def crear(request): data = {} if request.method == 'POST': datos = json.loads(request.POST['ventas']) if datos: with transaction.atomic(): c = Venta() c.fecha_venta = datos['fecha_venta'] c.cliente_id = datos['cliente'] c.empleado_id = request.user.id c.subtotal = float(datos['subtotal']) c.iva = float(datos['iva']) c.total = float(datos['total']) c.save() for i in datos['productos']: dv = Detalle_venta() dv.venta_id = c.id dv.producto_id = i['id'] dv.cantidad = int(i['cantidad']) dv.subtotal = float(i['subtotal']) dv.save() x = Producto.objects.get(pk=i['id']) x.stock = x.stock - int(i['cantidad']) x.save() data['id'] = c.id data['resp'] = True else: data['resp'] = False data['error'] = "Datos Incompletos" return HttpResponse(json.dumps(data), content_type="application/json")
def post(self, request, *args, **kwargs): data = {} action = request.POST['action'] try: if action == 'add': datos = json.loads(request.POST['ventas']) if datos: with transaction.atomic(): c = Venta() c.fecha = datos['fecha'] c.cliente_id = datos['cliente'] c.subtotal = float(datos['subtotal']) c.iva = float(datos['iva']) c.total = float(datos['total']) c.save() if datos['lotes']: for i in datos['lotes']: dv = Detalle_venta() dv.venta_id = c.id dv.lote_id = int(i['lote']['id']) dv.cantidad = int(i['cantidad']) dv.valor_libra = float(i['valor_libra']) dv.peso = float(i['peso_promedio']) dv.pvp_actual = float(i['valor_ave']) dv.subtotal = float(i['subtotal']) dv.save() dist = Distribucion.objects.get( id=int(i['id'])) dist.stock_actual -= int(i['cantidad']) dist.save() lot = Lote.objects.get(id=int(i['lote']['id'])) lot.stock_actual -= int(i['cantidad']) lot.save() data['id'] = c.id data['resp'] = True else: data['resp'] = False data['error'] = "Datos Incompletos" else: data['error'] = 'No ha seleccionado una opcion' except Exception as e: data['error'] = str(e) return HttpResponse(json.dumps(data), content_type='application/json')
def post(self, request, *args, **kwargs): data = {} action = request.POST['action'] try: if action == 'add': datos = json.loads(request.POST['ventas']) if datos: with transaction.atomic(): c = Venta() c.fecha = datos['fecha_venta'] c.cliente_id = datos['cliente'] c.subtotal = float(datos['subtotal']) c.iva = float(datos['iva']) c.total = float(datos['total']) c.save() if datos['productos']: for i in datos['productos']: dv = Detalle_venta() dv.venta_id = c.id dv.producto_id = int(i['id']) dv.cantidad = int(i['cantidad']) dv.pvp_actual = float(i['pvp']) dv.subtotal = float(i['subtotal']) dv.save() stock = Producto.objects.get(id=i['id']) stock.stock -= int(i['cantidad']) stock.save() data['id'] = c.id data['resp'] = True else: data['resp'] = False data['error'] = "Datos Incompletos" elif action == 'reserva': datos = json.loads(request.POST['ventas']) if datos: with transaction.atomic(): c = Venta() c.fecha = datos['fecha_venta'] c.cliente_id = datos['cliente'] c.subtotal = float(datos['subtotal']) c.iva = float(datos['iva']) c.total = float(datos['total']) c.estado = 2 c.save() if datos['productos']: for i in datos['productos']: dv = Detalle_venta() dv.venta_id = c.id dv.producto_id = int(i['id']) dv.cantidad = int(i['cantidad']) dv.pvp_actual = float(i['pvp']) dv.subtotal = float(i['subtotal']) dv.save() stock = Producto.objects.get(id=i['id']) stock.stock -= int(i['cantidad']) stock.save() data['id'] = c.id data['resp'] = True else: data['resp'] = False data['error'] = "Datos Incompletos" else: data['error'] = 'No ha seleccionado ninguna opciĆ³n' except Exception as e: data['error'] = str(e) return HttpResponse(json.dumps(data), content_type='application/json')
def post(self, request, *args, **kwargs): data = {} action = request.POST['action'] try: if action == 'add': datos = json.loads(request.POST['ventas']) if datos: with transaction.atomic(): c = Venta() c.fecha = datos['fecha_venta'] c.cliente_id = datos['cliente'] c.subtotal = float(datos['subtotal']) c.iva = float(datos['iva']) c.total = float(datos['total']) c.save() if datos['productos']: for i in datos['productos']: dv = Detalle_venta() dv.venta_id = c.id dv.producto_id = int(i['id']) dv.cantidad = int(i['cantidad']) dv.pvp_actual = float(i['pvp']) dv.subtotal = float(i['subtotal']) dv.save() stock = Producto.objects.get(id=i['id']) stock.stock -= int(i['cantidad']) stock.save() if int(datos['forma_pago']) == 1: verf = Cta_x_cobrar.objects.filter( venta__cliente_id=c.cliente_id, estado=0).count() if verf <= 2: cta = Cta_x_cobrar() cta.venta_id = c.id cta.valor = float(datos['total']) cta.interes = float(datos['interes']) cta.tolal_deuda = float(datos['total_deuda']) cta.saldo = float(datos['total_deuda']) cta.nro_cuotas = int(datos['nro_cuotas']) cta.save() c.tipo_pago = 1 c.save() x = 1 ahora = datetime.now() debito = (float(datos['letra']) * float(datos['nro_cuotas'])) calculo = (float(debito) - float(datos['total_deuda'])) for n in range(0, int(datos['nro_cuotas'])): fech = ahora + relativedelta(months=x) let = Pago_cta_x_cobrar() let.cta_cobrar_id = cta.id if fech.weekday() == 5: let.fecha = fech + dt.timedelta(days=2) elif fech.weekday() == 6: let.fecha = fech + dt.timedelta(days=1) else: let.fecha = fech if x == int(datos['nro_cuotas']): let.valor = float( datos['letra']) - float(calculo) let.saldo = float( datos['letra']) - float(calculo) print('valor ultimo') print(let.valor) else: let.valor = float(datos['letra']) let.saldo = float(datos['letra']) let.save() x = x + 1 else: data['error'] = 'Este cliente tiene mas de dos creditos activos, Por favor intenta ' \ 'con otro cliente' data['id'] = c.id data['resp'] = True else: data['resp'] = False data['error'] = "Datos Incompletos" elif action == 'reserva': datos = json.loads(request.POST['ventas']) if datos: with transaction.atomic(): c = Venta() c.fecha = datos['fecha_venta'] c.cliente_id = datos['cliente'] c.subtotal = float(datos['subtotal']) c.iva = float(datos['iva']) c.total = float(datos['total']) c.estado = 2 c.tipo_pago = 2 c.save() if datos['productos']: for i in datos['productos']: dv = Detalle_venta() dv.venta_id = c.id dv.producto_id = int(i['id']) dv.cantidad = int(i['cantidad']) dv.pvp_actual = float(i['pvp']) dv.subtotal = float(i['subtotal']) dv.save() stock = Producto.objects.get(id=i['id']) stock.stock -= int(i['cantidad']) stock.save() data['id'] = c.id data['resp'] = True else: data['resp'] = False data['error'] = "Datos Incompletos" elif action == 'list_list': data = [] ids = json.loads(request.POST['ids']) for c in Producto.objects.all().select_related( 'producto_base').select_related( 'presentacion').exclude(id__in=ids): data.append(c.toJSON()) elif action == 'search_no_stock': ids = json.loads(request.POST['ids']) data = [] term = request.POST['term'] query = Producto.objects.values('id', 'producto_base__nombre', 'presentacion__nombre'). \ filter(producto_base__nombre__icontains=term).select_related('producto_base').select_related('presentacion') for a in query.exclude(id__in=ids): result = { 'id': int(a['id']), 'text': str(a['producto_base__nombre']) + ' / ' + str(a['presentacion__nombre']) } data.append(result) elif action == 'search': data = [] ids = json.loads(request.POST['ids']) term = request.POST['term'] query = Producto.objects.filter( producto_base__nombre__icontains=term, stock__gte=1).select_related( 'producto_base').select_related('presentacion') for a in query.exclude(id__in=ids)[0:10]: result = { 'id': int(a.id), 'text': str(a.producto_base.nombre + ' / ' + str(a.presentacion.nombre)) } data.append(result) elif action == 'get': data = [] id = request.POST['id'] producto = Producto.objects.filter(pk=id) empresa = Empresa.objects.first() for i in producto: item = i.toJSON() item['cantidad'] = 1 item['subtotal'] = 0.00 item['iva_emp'] = empresa.iva data.append(item) elif action == 'search_cli': data = [] term = request.POST['term'] query = User.objects.filter(Q(first_name__icontains=term) | Q(last_name__icontains=term) | Q(cedula__icontains=term), tipo=0)[0:10] for a in query: item = a.toJSON() item['text'] = a.get_full_name() data.append(item) else: data['error'] = 'No ha seleccionado una opcion' except Exception as e: data['error'] = str(e) return HttpResponse(json.dumps(data), content_type='application/json')
def post(self, request, *args, **kwargs): data = {} try: action = request.POST['action'] if action == 'get': data = [] id = request.POST['id'] producto = Producto.objects.get(id=id) item = producto.toJSON() item['cantidad'] = 1 item['subtotal'] = 0 item['iva_emp'] = empresa.iva data.append(item) elif action == 'add': datos = json.loads(request.POST['ventas']) if datos: print(datos) with transaction.atomic(): c = Venta() c.cliente_id = datos['cliente'] c.subtotal = float(datos['subtotal']) c.iva = float(datos['iva']) c.total = float(datos['total']) c.save() if datos['productos']: for i in datos['productos']: dv = Detalle_venta() dv.venta_id = c.id dv.producto_id = int(i['id']) dv.cantidad = int(i['cantidad']) dv.pvp_actual = float(i['pvp']) dv.subtotal = float(i['subtotal']) dv.save() stock = Producto.objects.get(id=i['id']) stock.stock -= int(i['cantidad']) stock.save() data['id'] = c.id data['resp'] = True else: data['resp'] = False data['error'] = "Datos Incompletos" elif action == 'reserva': datos = json.loads(request.POST['ventas']) if datos: with transaction.atomic(): c = Venta() c.cliente_id = datos['cliente'] c.subtotal = float(datos['subtotal']) c.iva = float(datos['iva']) c.total = float(datos['total']) c.estado = 2 c.save() if datos['productos']: for i in datos['productos']: dv = Detalle_venta() dv.venta_id = c.id dv.producto_id = int(i['id']) dv.cantidad = int(i['cantidad']) dv.pvp_actual = float(i['pvp']) dv.subtotal = float(i['subtotal']) dv.save() stock = Producto.objects.get(id=i['id']) stock.stock -= int(i['cantidad']) stock.save() data['id'] = c.id data['resp'] = True else: data['resp'] = False data['error'] = "Datos Incompletos" except Exception as e: data['error'] = str(e) return HttpResponse(json.dumps(data), content_type='application/json')