summaryrefslogtreecommitdiff
path: root/main.py
blob: d1d690be1f39d646adef479fa38cdc83ad4e14fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python3

"""
Cahier des charges:
- Nombre de facture PC monté ou pièces détachées
- Si payé en plusieurs fois
- Prix panier moyen
"""

import os
import sys
import fitz #pymupdf

if len(sys.argv) != 2:
    print("Il faut indiquer le dossier ou sont les factures.")
    exit(1)

folder = sys.argv[1]
files = os.listdir(folder)
texte_date_emission = "Date d'émission :"
texte_montage = "monPCsurmesure.fr - Montage "
texte_mode_reglement = "Mode de règlement :"
texte_prix_ttc = "Total T.T.C."

dict_final = {}

for i in files:
    if os.path.isfile(i) and i.lower().endswith(".pdf"):
        with fitz.open(os.path.join(folder, i)) as doc:
            texte = ""
            for page in doc:
                texte += page.get_text()

        text = texte.split("\n")
        try:    
            date_emission = text[text.index(texte_date_emission) + 1].split("/")[1:]
        except:
            print("Erreur date: ", i)
            continue

        key = date_emission[1] + "-" + date_emission[0]
        if key not in dict_final:
            dict_final[key] = {}
            dict_final[key]["pc"] = 0
            dict_final[key]["piece"] = 0
            dict_final[key]["prix_pc"] = 0
            dict_final[key]["prix_piece"] = 0
            dict_final[key]["type_reglement"] = {}
            dict_final[key]["nom_factures"] = []

        is_pc = False
        for j in text:
            if texte_montage in j:
                dict_final[key]["pc"] += 1
                is_pc = True

        if is_pc:
            dict_final[key]["nom_factures"].append(i + " - PC")
        else:
            dict_final[key]["piece"] += 1
            dict_final[key]["nom_factures"].append(i + " - PIECE")

        if texte_prix_ttc in text:
            if is_pc:
                dict_final[key]["prix_pc"] += float(text[text.index(texte_prix_ttc) + 1])
            else:
                dict_final[key]["prix_piece"] += float(text[text.index(texte_prix_ttc) + 1])

        if texte_mode_reglement in text:
            reglement = text[text.index(texte_mode_reglement) + 1]
            if reglement in dict_final[key]["type_reglement"]:
                dict_final[key]["type_reglement"][reglement] += 1
            else:
                dict_final[key]["type_reglement"][reglement] = 1


for i in dict_final:
    if not os.path.isdir(os.path.join(folder, i)):
        os.mkdir(os.path.join(folder, i))
    if dict_final[i]["pc"] == 0:
        dict_final[i]["pc"] = 1
    if dict_final[i]["piece"] == 0:
        dict_final[i]["piece"] = 1

    texte_final = "\nPC sur mesure: " + i\
        + "\n\n### PC ###"\
        + "\nNb PC: " + str(dict_final[i]["pc"])\
        + "\nPrix PC: " + str(round(dict_final[i]["prix_pc"], 2)) + "€"\
        + "\nPrix moyen: " + str(round(dict_final[i]["prix_pc"] / dict_final[i]["pc"], 2)) + "€"\
        + "\n\n### PIECE DETACHE ###"\
        + "\nNb Pieces: " + str(dict_final[i]["piece"])\
        + "\nPrix Pieces: " + str(round(dict_final[i]["prix_piece"], 2)) + "€"\
        + "\nPrix moyen: " + str(round(dict_final[i]["prix_piece"] / dict_final[i]["piece"], 2)) + "€"\
        + "\n\n### TYPE DE REGLEMENT ###\n"

    for j in dict_final[i]["type_reglement"]:
        texte_final = texte_final + j + ": " + str(dict_final[i]["type_reglement"][j]) + "\n"

    texte_final = texte_final + "\n### NOM DES FACTURES ###\n"
    for j in dict_final[i]["nom_factures"]:
        texte_final = texte_final + j + "\n"
        os.rename(j.split(" ")[0], os.path.join(i, j.split(" ")[0]))

    with open(i + "_pcmesure.txt", "w") as f:
        f.write(texte_final)

"""
# Pourrait être utile ?
# Pièces du PC
usefull = text.split("P.T. H.T")[1].split("Mode de livraison :")[0]
chunks = usefull.split("\n")
final = []
pieces = []
loop = 0
for i in chunks:
    pieces.append(i)
    if loop == 6:
        final.append(pieces)
        pieces = []
        loop = 0
    loop += 1
print(len(final[:-2]))
print(final)
"""