Esempio n. 1
0
def customer_update(request, id, template_name="customer/customer_update.html"):
    customer = Customer.objects.get(id=id)
    form = CustomerForm(request.POST or None, instance=customer)
    if form.is_valid():
        form.save()
        return redirect("customer_list")
    return render(request, template_name, {"form": form})
Esempio n. 2
0
def add_customer(request):
    #accounts = Account.objects.all()
    #users = User.objects.filter(is_active=True).order_by('email')
    #teams = Team.objects.all()
    #assignedto_list = request.POST.getlist('assigned_to')
    #teams_list = request.POST.getlist('teams')
    #lead_account = request.POST.get('account_name')
    customer_email = request.POST.get('email')
    customer_phone = request.POST.get('phone')
    form = CustomerForm()
    #address_form = BillingAddressForm()
    if request.method == 'POST':
        form = CustomerForm(request.POST)
        #address_form = BillingAddressForm(request.POST)
        if form.is_valid():
            customer_obj = form.save(commit=False)
            #address_object = address_form.save()
            #lead_obj.address = address_object
            #lead_obj.created_by = request.user
            customer_obj.save()
            #lead_obj.assigned_to.add(*assignedto_list)
            #lead_obj.teams.add(*teams_list)

            if request.POST.get("savenewform"):
                return HttpResponseRedirect(reverse("customer:add_customer"))
            else:
                return HttpResponseRedirect(reverse('customer:list'))
        else:
            return render(request, 'customers/create_customer.html',
                          {'customer_form': form})
    else:
        return render(request, 'customers/create_customer.html',
                      {'customer_form': form})
Esempio n. 3
0
def customer_detail(request, pk):
    """查看和修改客户信息"""
    """获取客户详细信息"""
    user = request.user
    customer = get_object_or_404(Customer, pk=pk, is_valid=True)
    # 拿到该客户名称,存到session中,供后面使用
    cus_name = str(customer)
    request.session['name'] = cus_name
    if request.method == 'POST':
        form = CustomerForm(request=request,
                            data=request.POST,
                            instance=customer)
        if form.is_valid():
            form.save()
            return redirect('customer:customer_list')
    else:
        form = CustomerForm(request=request, instance=customer)
    """获取该客户的其他信息"""
    businesse_list = CustomerBusiness.objects.filter(customer=customer,
                                                     is_valid=True)
    liaison_list = Liaison.objects.filter(customer=customer, is_valid=True)
    record_list = CustomerRecord.objects.filter(customer=customer,
                                                is_valid=True)
    return render(
        request, 'customer_detail.html', {
            'form': form,
            'businesse_list': businesse_list,
            'liaison_list': liaison_list,
            'record_list': record_list,
            'cus_id': pk,
        })
Esempio n. 4
0
    def post(self, request):
        customer_form = CustomerForm(request.POST)
        item_formset = self.OrderItemFormSet(request.POST)
        customer_selectform = OrderCustomerSelectForm(request.POST)

        if ((customer_selectform.is_valid() or customer_form.is_valid())
                and item_formset.is_valid()):

            if request.POST.get('reg'):
                customer = customer_selectform.cleaned_data.get('customer')
            else:
                customer = customer_form.save()

            sh = Shipment.objects.create(customer=customer)

            stocks = dict()
            for form in item_formset:
                name = form.cleaned_data['item']
                stocks[name] = (stocks.get(name, 0) +
                                form.cleaned_data.get('count'))
            for stock, count in stocks.items():
                ShipmentStock.objects.create(shipment=sh,
                                             stock=stock,
                                             number=count)

            messages.info(request, _('Заявка отправлена'))
            return redirect('shipment:order_successful')

        else:
            return redirect('shipment:order')
Esempio n. 5
0
def update(request, customer_id):
    customer = Customer.objects.get(id=customer_id)

    if request.method == "POST":
        customer_form = CustomerForm(request.POST)
        if customer_form.is_valid():
            name = customer_form.cleaned_data['name']
            phone = customer_form.cleaned_data['phone']
            address = customer_form.cleaned_data['address']

            if name:
                customer.name = name
            if phone:
                customer.phone = phone
            if address:
                customer.address = address

            customer.save()
            context = {'customer_id': customer_id}
            messages.add_message(request, messages.SUCCESS, '修改成功')
            return redirect(reverse('customer:index'))
        else:
            context = {'customer_form': customer_form}
            messages.add_message(request, messages.WARNING, '请检查填写的内容')
            return render(request, 'customer/edit.html', context)
    else:
        customer_form = CustomerForm({
            'id': customer.id,
            'name': customer.name,
            'phone': customer.phone,
            'address': customer.address,
            'create_time': customer.create_time
        })
        context = {'customer_form': customer_form, 'customer_id': customer_id}
        return render(request, 'customer/edit.html', context)
Esempio n. 6
0
def update(request, id):
    customer = Customer.objects.get(id=id)
    form = CustomerForm(request.POST, instance=customer)
    if form.is_valid():
        form.save()
        return redirect("/show")
    return render(request, 'edit.html', {'customer': customer})
Esempio n. 7
0
 def post(self, request, *args, **kwargs):
     self.object = None
     form_class = self.get_form_class()
     user_form = self.get_form(form_class)
     cust_form = CustomerForm(self.request.POST, self.request.FILES)
     if (user_form.is_valid() and cust_form.is_valid()):
         return self.form_valid(user_form, cust_form)
     else:
         return self.form_invalid(user_form, cust_form)
Esempio n. 8
0
def nuevo(request):
    if request.method == "POST":
        form = CustomerForm(request.POST)
        if form.is_valid():
            customer = form.save()
            customer.save()
            return redirect('customer.views.listar')
    else:
        form = CustomerForm
    return render(request, 'customer/customer_nuevo.html', {'form':form, 'nuevo':True, 'texto':"Guardar"})
Esempio n. 9
0
def editar(request, pk):
    cust = get_object_or_404(Customer, pk=pk)
    if request.method == "POST":
        form = CustomerForm(request.POST, instance=cust)
        if form.is_valid():
            customer = form.save()
            customer.save()
            return redirect('customer.views.listar')
    else:
        form = CustomerForm(instance=cust)
    return render(request, 'customer/customer_nuevo.html', {'form':form, 'texto':"Actualizar"},)
Esempio n. 10
0
def customer_add(request):
    """客户添加"""
    if request.method == 'POST':
        form = CustomerForm(data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('customer')
        else:
            print(form.errors.as_json)
    else:
        form = CustomerForm()
    return render(request, 'customer_add.html', {'form': form})
Esempio n. 11
0
def cust(request):
    if request.method == "POST":
        form = CustomerForm(request.POST)
        if form.is_valid():
            try:
                form.save()
                return redirect('/show')
            except:
                pass
    else:
        form = CustomerForm()
    return render(request, 'index.html', {'form': form})
Esempio n. 12
0
def profile(request, id):
    """A view that displays the profile page of a logged in user"""
    customer = get_object_or_404(Customer, pk=id)
    form = CustomerForm(instance=customer)
     
    if request.method == "POST":
        form = CustomerForm(request.POST, instance=customer)
        if form.is_valid():
            form = form.save()
            return redirect(profile, id)
        else:
            form = CustomerForm(instance=customer)
    return render(request, "profile.html", {'form': form})
Esempio n. 13
0
def customer_edit(request, pk):
    """客户编辑"""
    user = request.session.get('user_id')
    customer = get_object_or_404(Customer, pk=pk, user=user, is_valid=True)
    if request.method == 'POST':
        form = CustomerForm(data=request.POST, instance=customer)
        if form.is_valid():
            form.save()
            return redirect('customer')
        else:
            print(form.errors.as_json)
    else:
        form = CustomerForm(instance=customer)
    return render(request, 'customer_edit.html', {'form': form, 'pk': pk})
Esempio n. 14
0
def register(request):
    registered = False

    next_page = request.GET.get('next', '/')

    print(next_page)

    if request.user.is_authenticated:
        return HttpResponseRedirect(next_page)
    else:
        if request.method == 'POST':
            user_form = UserForm(data=request.POST)

            customer_form = CustomerForm(data=request.POST)

            if user_form.is_valid() and customer_form.is_valid():
                print("valid")
                user = user_form.save()
                user.set_password(user.password)
                user.save()

                customer = customer_form.save(commit=False)
                customer.user = user

                if 'photo' in request.FILES:
                    print('Photo found!')
                    customer.photo = request.FILES['photo']

                customer.save()

                registered = True

                login(request, user)

                return HttpResponseRedirect(next_page)

            else:
                print(user_form.errors, customer_form.errors)

        else:
            user_form = UserForm()
            customer_form = CustomerForm()

    context = {
        'user_form': user_form,
        'customer_form': customer_form,
        'registered': registered,
    }

    return render(request, 'register.html', context)
Esempio n. 15
0
def customer_edit(request, pk):
    """单个修改界面"""
    user = request.session.get('user_id')
    customer = get_object_or_404(Customer, pk=pk, user=user, is_valid=True)
    if request.method == 'POST':
        form = CustomerForm(request=request,
                            data=request.POST,
                            instance=customer)
        if form.is_valid():
            form.save()
            return redirect('customer:customer_list')
    else:
        form = CustomerForm(request=request, instance=customer)
    return render(request, 'customer_edit.html', {
        'form': form,
    })
Esempio n. 16
0
def profile(request, id):
    """A view that displays the profile page of a logged in user"""
    """get USer object for the user currently logged in """
    customer = get_object_or_404(Customer, pk=id)
    """create a customer form, pre-filled with the current user's info"""
    form = CustomerForm(instance=customer)

    if request.method == "POST":
        form = CustomerForm(request.POST, instance=customer)
        if form.is_valid():
            """If form is valid, save it"""
            form = form.save()
            """return to the profile page with the """
            """current user info displayed"""
            return redirect(profile, id)
        else:
            form = CustomerForm(instance=customer)
    return render(request, "profile.html", {'form': form})
Esempio n. 17
0
def customer_detail(request, pk):
    """客户详情(外带地址信息)"""
    user = request.session.get('user_id')
    customer = get_object_or_404(Customer, pk=pk, user=user, is_valid=True)
    if request.method == 'POST':
        form = CustomerForm(data=request.POST, instance=customer)
        if form.is_valid():
            form.save()
            return redirect('customer_detail', pk)
        else:
            print(form.errors.as_json)
    else:
        form = CustomerForm(instance=customer)
    # 添加上地址信息
    # 下面这里引入异常处理,如果数据库里没有该字段,那么表单渲染为空
    try:
        customer_shop = CustomerShop.objects.get(customer=pk)
        shopform = CustomerShopForm(instance=customer_shop)
    except CustomerShop.DoesNotExist:
        shopform = CustomerShopForm()

    try:
        customer_invoice = CustomerInvoice.objects.get(customer=pk)
        invoiceform = CustomerInvoiceForm(instance=customer_invoice)
    except CustomerInvoice.DoesNotExist:
        invoiceform = CustomerInvoiceForm()

    # 添加联系人,商机,拜访记录信息
    liaisons = Liaison.objects.filter(customer_id=pk, is_valid=True)
    records = Record.objects.filter(customer_id=pk, is_valid=True)
    businesses = Business.objects.filter(customer_id=pk, is_valid=True)

    return render(
        request, 'customer_detail.html', {
            'form': form,
            'pk': pk,
            'shopform': shopform,
            'invoiceform': invoiceform,
            'liaisons': liaisons,
            'records': records,
            'businesses': businesses
        })
Esempio n. 18
0
def customer_add(request):
    """添加客户信息"""
    user = request.session.get('user_id')
    customer = None
    now = datetime.datetime.now().month
    if request.method == 'POST':
        form = CustomerForm(request=request,
                            data=request.POST,
                            instance=customer)
        if form.is_valid():
            form.save()
            tody_cus = Customer.objects.filter(user=user,
                                               is_valid=True,
                                               created_at__month=now).count()
            Count.objects.get_or_create(user=user)
            Count.objects.filter(user=user).update(add_cus=tody_cus)
            return redirect('customer:customer_list')
    else:
        form = CustomerForm(request=request, instance=customer)
    return render(request, 'customer_add.html', {'form': form})
Esempio n. 19
0
def update_customer(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=request.user)
        customer_form = CustomerForm(request.POST,
                                     instance=request.user.customer)
        if user_form.is_valid() and customer_form.is_valid():
            user_form.save()
            customer_form.save()
            messages.success(request, 'Your profile was successfully updated')
            return redirect('settings:profile')
        else:
            messages.error(request, 'Please correct the error below')
    else:
        user_form = UserForm(request.POST, instance=request.user)
        customer_form = CustomerForm(request.POST,
                                     instance=request.user.customer)
    return render(request, 'profiles/profile.html', {
        'user_form': user_form,
        'customer_form': customer_form
    })
Esempio n. 20
0
def add(request):
    if request.method == "POST":
        customer_form = CustomerForm(request.POST)
        if customer_form.is_valid():
            name = customer_form.cleaned_data['name']
            phone = customer_form.cleaned_data['phone']
            address = customer_form.cleaned_data['address']

            new_customer = Customer.objects.create(name=name,
                                                   phone=phone,
                                                   address=address)
            context = {'id': new_customer.id}
            messages.add_message(request, messages.WARNING, '添加成功')
            return redirect(reverse('customer:index'))

        else:
            context = {'customer_form': customer_form}
            messages.add_message(request, messages.WARNING, '请检查填写的内容')
            return render(request, 'customer/add.html', context)
    else:
        customer_form = CustomerForm()
        context = {'customer_form': customer_form}
        return render(request, 'customer/add.html', context)
Esempio n. 21
0
def handle_uploaded_file(f):
    for chunk in f.chunks():
        lines = chunk.decode('utf-8').split('\n')
        for line in lines:
            if len(line) > 0:
                items = line.split('\t')
                customer_form = CustomerForm({
                    'id': items[0],
                    'first_name': items[1],
                    'last_name': items[2],
                    'street_address_line': items[3],
                    'state': items[4],
                    'zip_code': items[5],
                })
                product_form = ProductForm({
                    'id': items[7],
                    'name': items[8],
                    'purchase_amount': items[9],
                })

                if customer_form.is_valid() and product_form.is_valid():
                    customer, _ = Customer.objects.get_or_create(
                        **customer_form.data)
                    product, _ = Product.objects.get_or_create(
                        **product_form.data)
                    order, _ = Order.objects.get_or_create(customer=customer,
                                                           product=product,
                                                           defaults={
                                                               'date':
                                                               datetime.now(),
                                                           })
                    order.status = items[6]
                    order.date = items[10]
                    order.save()
                else:
                    print(customer_form.errors)
                    print(product_form.errors)
Esempio n. 22
0
def customer_create(request, template_name="customer/customer_add.html"):
    form = CustomerForm(request.POST or None)
    if form.is_valid():
        form.save()
        return redirect("customer_list")
    return render(request, template_name, {"form": form})
Esempio n. 23
0
 def post(self, request):
     form = CustomerForm(request.POST, request.FILES)
     if form.is_valid():
         form.save()
         return redirect('/display')
Esempio n. 24
0
def add_items(request):
    context = {}
    # coming from order page attached with PHONE
    if 'phone' in request.GET:
        phone = request.GET["phone"]
        cu = Customer.objects.filter(phone=phone).values('name')
        # coming from order page attached with PHONE with valid phone attached to a customer
        if cu:
            name = cu[0]['name']
            form = CustomerForm(initial={'name': name, 'phone': phone})
            context = {'form': form}
            Dataform = Repair_order_From()
            context['date'] = Dataform
            if request.method == 'GET':
                # print(context)
                # On first render print the formsets

                formset = repair_form_set(initial=[{
                    'type1': '-',
                    'type2': '-',
                    'type3': '-',
                    'option': '-',
                    'price': 0,
                    'summary': ''
                }])

                context['formset'] = formset

                return render(request, 'repair/AddItem.html', context)
            elif request.method == 'POST':
                # submitted N form for N items
                request_params = request.POST
                formset = repair_form_set(request_params)
                # print(type(formset[0]))  type: repair item form
                #print("HI")

                if formset.is_valid():
                    #print("HI")
                    for form in formset:
                        # print("valid")
                        if form.is_valid():
                            # if form.cleaned_data.get('type1') and form.cleaned_data.get('type2') and form.cleaned_data.get('type3')  and form.cleaned_data.get('option') and form.cleaned_data('summary') and form.cleaned_data('price'):
                            type1 = form.cleaned_data.get('type1')
                            type2 = form.cleaned_data.get('type2')
                            type3 = form.cleaned_data.get('type3')
                            option = form.cleaned_data.get('option')

                            summary = form.cleaned_data.get('summary')
                            price = form.cleaned_data.get('price')
                            order_pk = request.GET['order']
                            order = Repair_order.objects.get(pk=order_pk)
                            current_order_price = order.total_price
                            order.total_price = current_order_price + price
                            order.reminder = current_order_price + price
                            order.save()
                            item = Repair_item(type1=type1,
                                               type2=type2,
                                               type3=type3,
                                               repair_order_id=order,
                                               summary=summary,
                                               price=price)
                            item.save()
                            option_i = Repair_option(option_name=option, )
                            option_i.save()
                            option_i.items_option.add(item)

                url = reverse('repair:listrepairs')
                return redirect(url)
        else:
            if request.method == 'GET':
                formset = repair_form_set(initial=[{
                    'type1': '-',
                    'type2': '-',
                    'type3': '-',
                    'option': '-',
                    'price': 0,
                    'summary': ''
                }])
                context['formset'] = formset
                return render(request, 'repair/AddItem.html', context)
            elif request.method == 'POST':
                #print("hi")
                formset = repair_form_set(request.POST)
                if formset.is_valid():
                    #print("hi")
                    for form in formset:
                        type1 = form.cleaned_data.get('type1')
                        type2 = form.cleaned_data.get('type2')
                        type3 = form.cleaned_data.get('type3')
                        option = form.cleaned_data.get('option')
                        summary = form.cleaned_data('summary')
                        price = form.cleaned_data('price')
                    url = reverse('repair:listrepairs')
                    return redirect(url)
    return render(request, 'repair/AddItem.html', context)