Exemple #1
0
def create(request, customer_id):
    customer = get_object_or_404(Customer, pk=customer_id)

    if request.method == 'POST':
        contacts = ContactList.post_dict(request.POST)
        form = QuotationForm(request.POST)
        if form.is_valid():
            quotation = form.save()
            msg = quotation.contact_list.update_contacts(contacts)
            if msg:
                messages.warning(request, msg)
            else:
                messages.success(request, 'Customer created')

            History.created_history(quotation, request.user)
            messages.success(request, 'Quotation created.')
            return redirect('quotations:update', quotation.pk)
    else:
        contacts = {}
        form = QuotationForm(initial={'customer': customer_id})

    data = {
        'contacts': contacts,
        'form': form,
    }

    return render_to_response(
        'quotations/create.html',
        data,
        context_instance=RequestContext(request),
    )
Exemple #2
0
def update(request, quotation_id):
    quotation = get_object_or_404(Quotation, pk=quotation_id)

    if request.method == 'POST':
        if 'convert-to-cash-sale' in request.POST:
           cash_sale = quotation.to_cash_sale()
           return redirect('sales:update', 'cash', cash_sale.pk)
        elif 'convert-to-credit-sale' in request.POST:
           credit_sale = quotation.to_credit_sale()
           return redirect('sales:update', 'credit', credit_sale.pk)

        contacts = quotation.contact_list.post_dict(request.POST)
        form = QuotationForm(request.POST, instance=quotation)
        stock_item_code = request.POST.get('stock-item-code', '')
        
        if stock_item_code:
            msg = quotation.cart.add_item(stock_item_code)
            if msg.get('success', ''):
                messages.success(request, msg.get('success', ''))
            else:
                messages.warning(request, msg.get('warning', ''))
        else:
            if form.is_valid():
                form.save()
                quotation.cart.update_items(request.POST)
                messages.success(request, 'Quotation updated.')

        msg = quotation.contact_list.update_contacts(contacts)
        if msg:
            messages.warning(request, msg)
    else:
        try:
            contacts = quotation.contact_list.get_dict()
        except AttributeError:
            quotation.save()
            contacts = {}
        form = QuotationForm(instance=quotation)

    stock_items = quotation.cart.stockcartitem_set.all()

    data = {
        'cart': quotation.cart,
        'contacts': contacts,
        'form': form,
        'price_type': quotation.customer.price_type,
        'quotation': quotation,
        'stock_items': stock_items,
    }

    return render_to_response(
        'quotations/update.html',
        data,
        context_instance=RequestContext(request),
    )