Example #1
0
def etapePrescription(request):
    c = {}
    formPrescription = FormAjoutPrescription()
    formAjoutPrescripteur = FormAjoutPrescripteur(prefix="ajPrescripteur")
    if 'client_id' in request.session['appFacture']:
        c['listePrescriptions'] = Prescription.objects.filter(client=request.session['appFacture']['client_id'])

    # ============================
    # Restauration
    if request.method == 'GET' and 'appFacture' in request.session:

        prescription = func.getPrescription(request)
        if prescription is not None:
            formPrescription = FormAjoutPrescription(instance=prescription)
    # Fin restauration
    # ============================

    if request.method == 'POST':

        if 'ajPrescription' in request.POST:
            formPrescription = FormAjoutPrescription(request.POST)
            if formPrescription.is_valid():
                prescription = formPrescription.save(commit=False)
                func.enrPrescription(prescription, request)
                return func.etapeSuivante(request)

        elif 'ajPrescripteur-nom' in request.POST:
            formAjoutPrescripteur = FormAjoutPrescripteur(request.POST, prefix="ajPrescripteur")
            if formAjoutPrescripteur.is_valid():
                formAjoutPrescripteur.save()
                #On recharge le formulaire de prescription
                formPrescription = FormAjoutPrescription()
                formAjoutPrescripteur = FormAjoutPrescripteur(prefix="ajPrescripteur")

    c['formPrescription'] = formPrescription
    c['formAjoutPrescripteur'] = formAjoutPrescripteur
    c.update(csrf(request))
    return render_to_response("facture/etapePrescription.html", c, context_instance=RequestContext(request))
Example #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))