summaryrefslogtreecommitdiff
path: root/main.py
blob: 4e3d680da84eabf1bee6bdf3c8160af156e0ea41 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/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 re
from datetime import date
import fitz #pymupdf

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

reg_date = "[0-9]{2}/[0-9]{2}/[0-9]{4}"
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")
        list_date = []
        for j in text:
            if re.search(reg_date, j):
                list_date.append(j)
    
        date_f = list_date[0].split("/") # facture
        date_p = []
        list_date_p = list_date[1].split(" ") # paiement
        for j in date_p:
            if re.search(reg_date, j):
                date_p = j.split("/")

        key = date_f[2] + "-" + date_f[1]
        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]["nom_factures"] = []
            dict_final[key]["type_reglement"] = {}
            dict_final[key]["temps_fabrication"] = []

        is_pc = False
        for j in text:
            if texte_montage in j:
                is_pc = True

        delta = date(int(date_f[2]), int(date_f[1]), int(date_f[0])) \
                - date(int(date_p[2][:4]), int(date_p[1]), int(date_p[0]))
        dict_final[key]["temps_fabrication"].append(delta.total_seconds() / 86400)

        if is_pc:
            dict_final[key]["pc"] += 1
            dict_final[key]["nom_factures"].append(
                                            i 
                                            + " PC    Paiement: " 
                                            + "/".join(date_p) 
                                            + " Facture: " 
                                            + "/".join(date_f)
                                            + " Delta: "
                                            + str(delta.total_seconds() / 86400)
                                            + " jours."
                                            )
        else:
            dict_final[key]["piece"] += 1
            dict_final[key]["nom_factures"].append(
                                            i 
                                            + " PIECE Paiement: " 
                                            + "/".join(date_p) 
                                            + " Facture: " 
                                            + "/".join(date_f)
                                            + " Delta: "
                                            + str(delta.total_seconds() / 86400)
                                            + " jours."
                                            )

        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### DELAI MOYEN ###"\
        + "\nDelai moyen de " + str(round(sum(dict_final[i]["temps_fabrication"]) / len(dict_final[i]["temps_fabrication"]), 2)) + " jours."\
        + "\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)
"""