def billings_edit(request, id): billing_id = int(id) billing = Billing.objects.get(id=billing_id) if request.method == 'POST': form = BillingEditForm(request.POST, instance=billing) if form.is_valid(): action = form.save(commit=False) full_name = request.POST['full_name'] nit = request.POST['nit'] if Customer.objects.filter(nit=nit).exists(): customer = Customer.objects.get(nit=nit) else: customer = Customer(full_name=full_name, nit=nit) customer.save() action.customer_id = customer.id save = form.save() message = 'Factura Actualizada correctamente!' messages.add_message(request, messages.SUCCESS, message) return HttpResponseRedirect(reverse('billings-show', kwargs={'id': save.id})) else: form = BillingEditForm(instance=billing, initial={'full_name': billing.customer.full_name, 'nit': billing.customer.nit}) return render(request, 'billings/edit.html', { 'form': form, 'customers': Customer.objects.all(), 'instance_model_customer': Customer, })
def billings_new(request): if request.method == 'POST': form = BillingForm(request.POST.copy(), initial={'company': request.session['company_id'], 'user': request.user}) if form.is_valid(): action = form.save(commit=False) full_name = request.POST['full_name'] nit = request.POST['nit'] number_autorization = request.POST['number_autorization'] if Customer.objects.filter(nit=nit).exists(): customer = Customer.objects.get(nit=nit) else: customer = Customer(full_name=full_name, nit=nit, number_autorization=number_autorization) customer.save() action.customer_id = customer.id save = form.save() message = 'Factura guardado correctamente!' messages.add_message(request, messages.SUCCESS, message) return HttpResponseRedirect(reverse('billings-show', kwargs={'id': save.id})) else: max_code = Billing.objects.filter(company_id=request.session['company_id'], is_sale=True, is_electronic=True).aggregate(Max('code')) if not max_code['code__max']: max_code['code__max'] = 0 form = BillingForm(initial={'company': request.session['company_id'], 'user': request.user, 'code': (int(max_code['code__max']) + 1)}) return render(request, 'billings/new.html', { 'form': form, 'customers': Customer.objects.all(), 'instance_model_customer': Customer, })