Пример #1
0
def add_invoice(request, *args, **kwargs):
    """add a new invoice"""
    formset_data = {}

    c = {"user": request.user}
    cptr = 0
    addtional_item = ""

    try:
        org = Organization.objects.get(owner__pk=request.user.pk)

    except Organization.DoesNotExist:
        return HttpResponseRedirect(reverse('add_organization'))
    # initilisation du nombre d'item
    try:
        item_number = int(request.POST['form-TOTAL_FORMS'])
    except:
        item_number = 3

    if 'add_item' in request.POST or 'add_client' in request.POST:
        #Incremente le nombre total d'item si Add_item est utilisé
        item_number += 1
        #Copie de la requete post pour pouvoir modifier le contenu
        addtional_item = request.POST.copy()
        #Modification du nombre total d'tem
        addtional_item.update({'form-TOTAL_FORMS': item_number})

        formset_data = {'form-TOTAL_FORMS': item_number,
                        'form-INITIAL_FORMS': u'2',
                        'form-MAX_NUM_FORMS': u''
                        }

        for item in addtional_item:
            try:
                formset_data["form-%d-price" % cptr] = \
                             addtional_item["form-%d-price" % cptr]
                formset_data["form-%d-quantity" % cptr] = \
                             addtional_item["form-%d-quantity" % cptr]
                formset_data["form-%d-description" % cptr] =\
                             addtional_item["form-%d-description" % cptr]
            except MultiValueDictKeyError:
                formset_data["form-%d-price" % cptr] = u""
                formset_data["form-%d-quantity" % cptr] = u""
                formset_data["form-%d-description" % cptr] = u""
            cptr = cptr + 1

        header = {
                    'type': addtional_item["type"],
                    'tax_rate': addtional_item["tax_rate"],
                    'location': addtional_item["location"],
                    'number': addtional_item["number"],
                    'client': addtional_item["client"],
                    'date': addtional_item["date"]
                 }

    InvoiceItemFormSet = formset_factory(InvoiceItemform,\
                                         extra=item_number)
    client = ''
    # on verifie si last_client est dans la requette session
    if 'last_client' in request.session:
        # on enleve la valeur de last_client et on le met dans client
        client = request.session.pop('last_client')

    try:
        if request.GET['display'] == 'facture':
            link_page = 'Facture'
            c.update({"link_page": link_page})
        if request.GET['display'] == 'proforma':
            link_page1 = 'Proforma'
            c.update({"link_page1": link_page1})
    except:
        pass

    if  'add_invoice' in request.POST and request.method == 'POST':

        try:
            formset = InvoiceItemFormSet(request.POST, addtional_item)
            form = InvoiceForm(request, request.POST, addtional_item)
        except:
            formset = InvoiceItemFormSet()
            form = InvoiceForm(request)
        # on verifie si le bouton cliqué est celui de add_invoice
        if 'add_invoice' in request.POST:

            # on verifie si le form et le formset sont valides
            if form.is_valid() and formset.is_valid():
                # on sauvegarde la facture avec ses items
                invoice = form.save()
                for f in formset.forms:
                    if f.cleaned_data:
                        item = f.save(commit=False)
                        item.invoice = invoice
                        item.save()
                return HttpResponseRedirect(reverse('invoice_done'))
    else:
        if 'add_item' in request.POST:
            form = HeaderForm(header)
            formset = InvoiceItemFormSet(formset_data)
            c.update({'valide': 'send'})
        else:
            form = InvoiceForm(request=request)
            formset = InvoiceItemFormSet()

    # on verifie si create_invoice_data est dans la requette session
    if 'create_invoice_data' in request.session and \
        'nouveau' not in request.POST:

        # on enleve la valeur de create_invoice_data et on le met
        # dans request.POST
        request.POST = request.session.pop('create_invoice_data')
        # on verifie si il a crée un nouveau client
        if client:
            # on met le nouveau client dans requette post
            request.POST['client'] = client.id
        request.method = 'POST'
        if request.method == 'POST':
            form = InvoiceForm(request, request.POST)
            formset = InvoiceItemFormSet(request.POST)
        c.update({'valide': 'send'})
    # on verifie si le bouton cliqué est celui de add_client
    if 'add_client' in request.POST:
        # on sauvegarde une copie de la requette post dans la
        # requette session et on supprime le nom du bouton
        # add_client de cette copie
        request.session['create_invoice_data'] = request.POST.copy()
        request.session['create_invoice_data'].pop('add_client')
        # puis on redige sur la vue add_client
        return HttpResponseRedirect(reverse('add_client'))

    c.update({'formset': formset, 'form': form, 'organization': org})
    c.update(csrf(request))

    return render_to_response("invoiceitem.html", c)
Пример #2
0
def modification(request, *args, **kwargs):
    """ modify """
    c = data = {}
    cptr = 0
    try:
        org = Organization.objects.get(owner__pk=request.user.pk)
    except Organization.DoesNotExist:
        return HttpResponseRedirect(reverse('add_organization'))
    num = kwargs["num"] or 1
    invoice_edit = Invoice.objects.get(id=num)
    items = invoice_edit.invoiceitem_set.all()
    n = len(items)
    # envoie du type de la facture
    c.update({'type': invoice_edit.type})

    if request.method == 'GET':
        item_number_ = n
    else:
        item_number_ = int(request.POST['form-TOTAL_FORMS'])

    for item in items:
        data["form-%d-invoice" % cptr] = invoice_edit.id
        data["form-%d-price" % cptr] = item.price
        data["form-%d-quantity" % cptr] = item.quantity
        data["form-%d-description" % cptr] = item.description
        cptr = cptr + 1
    if 'add_item_' in request.POST:

        #Incremente le nombre total d'item si Add_item est utilisé
        item_number_ += 1
        #Copie de la requete post pour pouvoir modifier le contenu
        addtional_item_ = request.POST.copy()
        #Modification du nombre total d'tem
        addtional_item_.update({'form-TOTAL_FORMS': item_number_})
        for item in addtional_item_:
            try:
                data["form-%d-price" % cptr] = \
                                    addtional_item_["form-%d-price"\
                                     % cptr]
                data["form-%d-quantity" % cptr] = \
                                    addtional_item_["form-%d-quantity"\
                                    % cptr]
                data["form-%d-description" % cptr] =\
                                  addtional_item_["form-%d-description"\
                                  % cptr]

            except MultiValueDictKeyError:
                data["form-%d-price" % cptr] = u""
                data["form-%d-quantity" % cptr] = u""
                data["form-%d-description" % cptr] = u""
            cptr = cptr + 1

    dict = {'type': invoice_edit.type,
            'tax_rate': invoice_edit.tax_rate,
            'tax': invoice_edit.tax,
            'subject': invoice_edit.subject,
            'location': invoice_edit.location,
            'client': invoice_edit.client.id,
            'number': invoice_edit.number,
            'date': invoice_edit.date}

    InvoiceItemFormSet = formset_factory(InvoiceItemform,\
                                         extra=item_number_)

    data.update({'form-TOTAL_FORMS': item_number_,
            'form-INITIAL_FORMS': u'2',
            'form-MAX_NUM_FORMS': u'',
            })

    if 'add_invoice' in request.POST and request.method == 'POST':
        try:
            formset = InvoiceItemFormSet(request.POST, addtional_item)
            form = InvoiceForm(request, request.POST, addtional_item)
        except:
            formset = InvoiceItemFormSet(request.POST)
            form = InvoiceForm(request, request.POST)
        # on verifie si le bouton cliqué est celui de add_invoice
        if 'add_invoice' in request.POST:
            # on verifie si le form et le formset sont valides
            if form.is_valid() and formset.is_valid():

                # on sauvegarde la facture avec ses items
                invoice = form.save()
                for f in formset.forms:
                    if f.cleaned_data:
                        item = f.save(commit=False)
                        item.invoice = invoice
                        item.save()
                invoice_del = Invoice.objects.\
                                      filter(number=invoice.number).\
                                      order_by('id')[0]
                items_del = invoice_del.invoiceitem_set.all()
                items_del.delete()
                invoice_del.delete()

                return HttpResponseRedirect(reverse('invoice_done'))

    else:
        form = InvoiceForm(request, dict)
        formset = InvoiceItemFormSet(data)

    c.update({'formset': formset, 'form': form, 'organization':\
                                          org, 'valide': 'send'})
    c.update(csrf(request))

    return render_to_response("modification.html", c)