Ejemplo n.º 1
0
def assign_customer_select_customer(request, id):
    """
    Have user decide if site exists from list and reassign all rma to selected site and delete site
    :param request:
    :param id:
    :return:
    """
    site = Customer.objects.get(pk=id)
    site_rmas = Rma.objects.filter(customer=site)
    if request.method == 'GET':
        data = {
            'name': site_rmas[0].customer.name,
        }
        customer_name_site_form = CustomerCompanyAndSiteNameForm(initial=data)
        return render(request,
                      'operations/assign_customer_select_customer.html', {
                          'site': site,
                          'form': customer_name_site_form,
                          'site_rmas': site_rmas
                      },
                      context_instance=RequestContext(request))
    if request.method == 'POST':
        customer_name_site_form = CustomerCompanyAndSiteNameForm(request.POST)
        if customer_name_site_form.is_valid():

            new_site_name = customer_name_site_form.cleaned_data['name']
            existing_customer_id = int(
                customer_name_site_form.cleaned_data['customer'])
            existing_customer = CustomerCompany.objects.get(
                pk=existing_customer_id)
            new_site = Customer()
            new_site.customer = existing_customer
            new_site.name = new_site_name
            new_site.save()
            for s in site_rmas:
                s.customer = new_site
                s.save()
            site.delete()

            return HttpResponseRedirect(
                reverse('reassign_customers_to_customer_sites'))
        else:
            return render(request,
                          'operations/assign_customer_select_customer.html', {
                              'site': site,
                              'form': customer_name_site_form,
                              'site_rmas': site_rmas
                          },
                          context_instance=RequestContext(request))
Ejemplo n.º 2
0
def assign_customer_to_site_new_customer(request, id):

    if request.method == 'GET':
        site = Customer.objects.get(pk=id)
        site_rmas = Rma.objects.filter(customer=site)
        customer_name_form = CustomerForm()
        site_form = CustomerSiteIdNameForm(instance=site)
        return render(request,
                      'operations/edit_site_new_customer.html', {
                          'site': site,
                          'customer_name_form': customer_name_form,
                          'site_form': site_form,
                          'site_rmas': site_rmas
                      },
                      context_instance=RequestContext(request))

    if request.method == 'POST':
        site = Customer.objects.get(pk=id)
        error_count = 0
        customer_name_form = CustomerForm(request.POST)
        site_form = CustomerSiteIdNameForm(request.POST, instance=site)
        site_rmas = Rma.objects.filter(customer=site)
        if site_form.is_valid() is not True:
            error_count += 1
        if customer_name_form.is_valid() is not True:
            error_count += 1

        if error_count > 0:
            return render(request,
                          'operations/edit_site_new_customer.html', {
                              'site': site,
                              'customer_name_form': customer_name_form,
                              'site_form': site_form,
                              'site_rmas': site_rmas,
                          },
                          context_instance=RequestContext(request))
        else:
            new_customer = customer_name_form.save()
            new_customer_site = Customer()
            new_customer_site.name = site_form.cleaned_data['name']
            new_customer_site.customer = new_customer
            new_customer_site.save()

            return HttpResponseRedirect(
                reverse('view_customer_site_to_reassign',
                        kwargs={'id': new_customer_site.id}))