Пример #1
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))
Пример #2
0
def etapeVerres(request):
    c = {}
    montures = func.getMontures(request)
    formsets = []
    for m in montures:
        if m.vision == 'M':
            progressif = True
        else:
            progressif = False

        extra = 2  # ODG
        LigneFormSet = formset_factory(LigneForm, extra=extra)
        LigneFormSet.form = staticmethod(curry(LigneForm, progressif=progressif))
        formSetLigne = LigneFormSet(prefix=str(m.numero))
        formSetLigne.forms[0].empty_permitted = False
        formsets.append({"monture": m, "formset": formSetLigne})

    # ============================
    # Restauration / chargement à partir de la session
    if request.method == 'GET' and 'appFacture' in request.session:
        verres = func.getVerres(request)
        if len(verres) != 0:
            if len(verres) != len(montures):
                c['restauration_erreur'] = 1
            else:
                formsets = []
                for m in montures:
                    if m.numero < len(verres):
                        if m.vision == 'M':
                            progressif = True
                        else:
                            progressif = False

                        if len(verres[m.numero]) == 2:
                            if progressif != any(verres[m.numero][0].vtype.progressif, verres[m.numero][1].vtype.progressif):
                                c['restauration_erreur'] = 2
                        else:
                            if progressif != verres[m.numero][0].vtype.progressif:
                                c['restauration_erreur'] = 2
                                LigneFormSet.form = staticmethod(curry(LigneForm, progressif=progressif))
                                formSetLigne = LigneFormSet(prefix=str(m.numero))
                                formSetLigne.forms[0].empty_permitted = False
                                for form in formSetLigne:
                                    chdata = form._get_changed_data()
                                    if 'vtype' in chdata:
                                        form.filtre_vtype(form._raw_value('vtype'))

                                formsets.append({"monture": m, "formset": formSetLigne})
                            else:
                                prefix = str(m.numero)
                                data = {
                                        prefix + '-TOTAL_FORMS': u'2',
                                        prefix + '-INITIAL_FORMS': u'0',
                                        prefix + '-MAX_NUM_FORMS': u'',
                                        prefix + '-0-vtype': verres[m.numero][0].vtype,
                                        prefix + '-0-diametre': verres[m.numero][0].diametre,
                                        prefix + '-0-couleur': verres[m.numero][0].couleur,
                                        prefix + '-0-traitement': verres[m.numero][0].traitement,
                                        }

                                if len(verres[m.numero]) == 2:
                                    dataG = {
                                             prefix + '-1-vtype': verres[m.numero][1].vtype,
                                             prefix + '-1-diametre': verres[m.numero][1].diametre,
                                             prefix + '-1-couleur': verres[m.numero][1].couleur,
                                             prefix + '-1-traitement': verres[m.numero][1].traitement,
                                             }
                                    data.update(dataG)

                                LigneFormSet.form = staticmethod(curry(LigneForm, progressif=progressif))
                                formSetLigne = LigneFormSet(data, prefix=str(m.numero))
                                formSetLigne.forms[0].empty_permitted = False
                                for form in formSetLigne:
                                    chdata = form._get_changed_data()
                                    if 'vtype' in chdata:
                                        form.filtre_vtype(form._raw_value('vtype'))

                                formsets.append({"monture": m, "formset": formSetLigne})

    # Fin restauration
    # ============================

    if request.method == 'POST':

        if 'ajVerres' in request.POST:
            formsets = []
            valide = True
            for m in montures:
                if m.vision == 'M':
                    progressif = True
                else:
                    progressif = False

                extra = 2
                LigneFormSet = formset_factory(LigneForm, extra=extra)
                LigneFormSet.form = staticmethod(curry(LigneForm, progressif=progressif))
                formSetLigne = LigneFormSet(request.POST, prefix=str(m.numero))
                formSetLigne.forms[0].empty_permitted = False

                # On charge les choix disponibles pour validation...
                for form in formSetLigne:
                    chdata = form._get_changed_data()
                    if 'vtype' in chdata:
                        vtype = Type.objects.get(id=form._raw_value('vtype'))
                        form.filtre_vtype(vtype)

                if formSetLigne.is_valid():
                    valide = valide and True
                else:
                    valide = False

                formsets.append({"monture": m, "formset": formSetLigne})

            # -endfor

            if valide == True:
                func.enrVerres(formsets, request)
                return func.etapeSuivante(request)
            else:
                formsets = []
                for m in montures:
                    if m.vision == 'M':
                        progressif = True
                    else:
                        progressif = False

                    extra = 2
                    LigneFormSet = formset_factory(LigneForm, extra=extra)
                    LigneFormSet.form = staticmethod(curry(LigneForm, progressif=progressif))
                    formSetLigne = LigneFormSet(request.POST, prefix=str(m.numero))  # Si on ne refait pas un formset, le is_valid casse tout...
                    formSetLigne.forms[0].empty_permitted = False
                    for form in formSetLigne:
                        chdata = form._get_changed_data()
                        if 'vtype' in chdata:
                            vtype = Type.objects.get(id=form._raw_value('vtype'))
                            form.filtre_vtype(vtype)

                    formsets.append({"monture": m, "formset": formSetLigne})

        # -endif ajVerres
    # -endif POST

    c['formsets'] = formsets
    c.update(csrf(request))
    return render_to_response("facture/etapeVerres.html", c, context_instance=RequestContext(request))