Beispiel #1
0
def facturer(pfid):
    u"""Crée une Facture à partir d'un facture pro forma

    Argument:
    pfid -- ID de la proforma

    """
    try:
        Facture.objects.get(proforma__id=pfid)
    except Facture.DoesNotExist:
        pass
    else:
        return -1  # -1: proforma déjà facturée

    # =====
    # Créer une nouvelle Facture à partir de la proforma
    # class Facture > bproforma = False & proforma__id = pfid
    # class LigneFacture, Option, Monture > dupliquer et à relier à la nouvelle facture
    proforma = Facture.objects.get(id=pfid)
    fac = Facture.objects.get(id=pfid)
    fac.pk = None  # duplique lors du save()
    fac.bproforma = False
    fac.proforma = proforma

    sauvFacture(fac)  # génération du code et enregistrement

    lfs = proforma.lignefacture_set.all()
    for lf in lfs:
        lf.pk = None
        lf.facture = fac
        lf.save()

    os = proforma.option_set.all()
    for o in os:
        o.pk = None
        o.facture = fac
        o.save()

    montures = proforma.monture_set.all()
    for m in montures:
        m.pk = None
        m.facture = fac
        m.save()

    fourfunc.stock_ou_commande(fac.lignefacture_set.all())
    return fac
Beispiel #2
0
def etapeRecapitulatif(request):
    c = {}
    s_aF = request.session['appFacture']
    formFacture = ChoixFactureForm()
    dico_client = func.getClient(request)
    client_orig = None
    solde = 0
    remise = 0

    if dico_client['client'] == None:
        client = Client.objects.get(id=dico_client['client_id'])
    else:
        client = dico_client['client']
        if dico_client['client_id']:
            client_orig = Client.objects.get(id=dico_client['client_id'])

    t_verres = func.getVerres(request)
    t_options = func.getOptions(request)
    t_montures = func.getMontures(request)
    prescription = func.getPrescription(request)
    prescription_t = func.getPrescription_T(request)

    # ======================
    # calcul du solde et remise
    ra = 0
    t_remise = [0, 0, 0]
    t_monturetarif = [0, 0, 0]

    for grp in t_verres:
        for v in grp:
            solde += v.tarif
            remise += v.calculRemise()
            t_remise[v.monture] += v.calculRemise()

    for o in t_options:
        solde += o.tarif

    for m in t_montures:
        solde += m.tarif
        t_monturetarif[m.numero] += m.tarif

    for m in range(0, len(t_remise)):
        if t_monturetarif[m] >= t_remise[m]:
            ra += t_remise[m]
        elif t_monturetarif[m] < t_remise[m]:
            ra += t_monturetarif[m]

    # ======================
    # traitment POST
    if request.method == 'POST':
        '''
        Enregistrement du client (s'il n'existe pas)
        ou Enregistrement des modifications (s'il y en a)
        Enregistrement de la prescrition
        Enregistrement de la Facture (génération ID)

        Enregistrement LigneFacture (verres)
        Si stock : traitement / Si "à commander" traitement.

        Enregistrement Monture(s)
        Enregistrement Options

        Reset assistant
        '''
        # Traitement Facture (-> facture.id)
        formFacture = ChoixFactureForm(request.POST)
        if formFacture.is_valid():
            facture = formFacture.save(commit=False)
            cd = formFacture.cleaned_data

            # Traitement client (-> client.id)
            if s_aF['b_creation'] == True:
                client = func.getClient(request)['client']
                client = cfunc.sauvClient(client)
            elif 'b_modification' in s_aF and s_aF['b_modification'] == True:
                client = func.getClient(request)['client']
                client_orig = Client.objects.get(id=dico_client['client_id'])

                client_orig.nom = client.nom
                client_orig.prenom = client.prenom
                client_orig.telephone = client.telephone
                client_orig.email = client.email
                client = client_orig.save()
            else:
                client = Client.objects.get(id=dico_client['client_id'])

            facture.client = client

            # Traitement de la prescription
            prescription = func.getPrescription(request)
            prescription.client = client
            prescription.save()

            facture.prescription = prescription

            # enregistrement de la facture
            facture = func.sauvFacture(facture)
            request.session['appFacture']['fid'] = facture.id

            # Traitement LigneFacture
            t_lignes = func.getVerres(request)
            for grp in t_lignes:
                for ligne in grp:
                    ligne.facture = facture
                    ligne.save()

            # Traitement monture(s)
            t_montures = func.getMontures(request)
            for monture in t_montures:
                monture.facture = facture
                monture.save()

            # Traitement option(s)
            t_options = func.getOptions(request)
            for option in t_options:
                option.facture = facture
                option.save()

            #actualisation du solde
            if cd['avance'] is not None:
                facture.solde = facture.total() - cd['avance']
            else:
                facture.solde = facture.total()

            facture.save()

            # Si facture: Actualisation Commande ou Stock
            if not facture.bproforma:
                fourfunc.stock_ou_commande(facture.lignefacture_set.all())

            return func.etapeSuivante(request)

    c['client'] = client
    c['client_orig'] = client_orig
    c['t_verres'] = t_verres
    c['t_options'] = t_options
    c['t_montures'] = t_montures
    c['prescription'] = prescription
    c['prescription_t'] = prescription_t
    c['formFacture'] = formFacture
    c['soldeinitial'] = solde
    c['solde'] = solde - ra
    c['remise_max'] = remise
    c['remise_accordee'] = ra
    c.update(csrf(request))
    return render_to_response("facture/etapeRecapitulatif.html", c, context_instance=RequestContext(request))