Versione 1.0 - Reset Totale Progetto

This commit is contained in:
Francesco
2026-02-04 19:20:04 +01:00
commit c99a069d06
5 changed files with 324 additions and 0 deletions

205
cemento.py Executable file
View File

@@ -0,0 +1,205 @@
# -*- coding: utf-8 -*-
from flask import Flask, render_template, request, Response
import math
import subprocess
app = Flask(__name__)
def calcola(d):
try:
def to_m(v, u):
val = float(v.replace(',', '.')) if v else 0
return val / 1000 if u == "mm" else val / 100 if u == "cm" else val
# Salvo le dimensioni originali per il report
dim_orig = {"a": d.get('a','0'), "ua": d.get('ua','m'), "l": d.get('l','0'), "ul": d.get('ul','m'), "p": d.get('p','0'), "up": d.get('up','m')}
vol = to_m(dim_orig['a'], dim_orig['ua']) * to_m(dim_orig['l'], dim_orig['ul']) * to_m(dim_orig['p'], dim_orig['up'])
peso_tot = vol * 2400
mats = ['cem', 'sab', 'ghi']
p = {m: float(d.get(f'p_{m}', '0').replace(',','.')) for m in mats}
w = {m: float(d.get(f'w_{m}', '25').replace(',','.')) for m in mats}
c = {m: float(d.get(f'c_{m}', '0').replace(',','.')) for m in mats}
somma_p = sum(p.values())
perc = {m: (p[m]/somma_p)*100 if somma_p > 0 else 0 for m in mats}
res = {"vol": f"{vol:.3f}", "peso": f"{peso_tot:.0f}", "perc": perc, "dim": dim_orig, "parti": p}
tot_s, costo_t = 0, 0
for m in mats:
kg = (p[m]/somma_p)*peso_tot if somma_p > 0 else 0
s_esatti = kg/w[m] if w[m] > 0 else 0
res[f's_{m}'] = math.ceil(s_esatti)
tot_s += math.ceil(s_esatti)
costo_t += math.ceil(s_esatti)*c[m]
res.update({"tot_sacc": tot_s, "costo_tot": f"{costo_t:.2f}", "acqua": f"{vol * 150:.0f}"})
v_b = float(d.get('v_bet', '160').replace(',','.'))
n_c = max(1, math.ceil((vol*1000)/v_b)) if vol > 0 else 1
res["n_c"] = n_c
for m in mats:
kg_tot = (p[m]/somma_p)*peso_tot if somma_p > 0 else 0
res[f'bc_{m}'] = f"{(kg_tot/w[m])/n_c:.2f}"
res["bc_aq"] = f"{(vol*150)/n_c:.2f}"
return res
except Exception as e:
print(f"Errore nel calcolo: {e}")
return None
@app.route('/', methods=['GET', 'POST'])
def index():
res = None
msg_stampa = None
if request.method == 'POST':
res = calcola(request.form)
if 'btn_stampa_cups' in request.form:
# Passa sia il form che i risultati del calcolo (se disponibili)
msg_stampa = esegui_stampa_cups(request.form, res)
return render_template('index.html', res=res, form=request.form, msg_stampa=msg_stampa)
def esegui_stampa_cups(f, res_calcolo=None):
try:
# Ottieni i dati: prima dal form, poi da res_calcolo, poi dai campi originali
def get_val(key, default='0'):
# 1. Cerca nel form con il prefisso 'd_' (campi nascosti)
if key in ['a', 'ua', 'l', 'ul', 'p', 'up']:
val = f.get(f'd_{key}', None)
if val:
return val
# 2. Cerca nel form direttamente
val = f.get(key, None)
if val:
return val
# 3. Cerca in res_calcolo se disponibile
if res_calcolo:
if key in ['a', 'ua', 'l', 'ul', 'p', 'up']:
return res_calcolo['dim'].get(key, default)
elif key == 'vol':
return res_calcolo.get('vol', default)
elif key == 'peso':
return res_calcolo.get('peso', default)
return default
# Estrai tutti i valori necessari
d_a = get_val('a', '0')
d_ua = get_val('ua', 'm')
d_l = get_val('l', '0')
d_ul = get_val('ul', 'm')
d_p = get_val('p', '0')
d_up = get_val('up', 'm')
# Ottieni i risultati con valori di default
res_vol = f.get('res_vol') or (res_calcolo['vol'] if res_calcolo and 'vol' in res_calcolo else '0')
res_peso = f.get('res_peso') or (res_calcolo['peso'] if res_calcolo and 'peso' in res_calcolo else '0')
res_s_cem = f.get('res_s_cem') or (res_calcolo['s_cem'] if res_calcolo and 's_cem' in res_calcolo else '0')
res_s_sab = f.get('res_s_sab') or (res_calcolo['s_sab'] if res_calcolo and 's_sab' in res_calcolo else '0')
res_s_ghi = f.get('res_s_ghi') or (res_calcolo['s_ghi'] if res_calcolo and 's_ghi' in res_calcolo else '0')
res_aq = f.get('res_aq') or (res_calcolo['acqua'] if res_calcolo and 'acqua' in res_calcolo else '0')
res_tot_s = f.get('res_tot_s') or (res_calcolo['tot_sacc'] if res_calcolo and 'tot_sacc' in res_calcolo else '0')
res_costo_t = f.get('res_costo_t') or (res_calcolo['costo_tot'] if res_calcolo and 'costo_tot' in res_calcolo else '0')
res_nc = f.get('res_nc') or (res_calcolo['n_c'] if res_calcolo and 'n_c' in res_calcolo else '1')
res_bc_cem = f.get('res_bc_cem') or (res_calcolo['bc_cem'] if res_calcolo and 'bc_cem' in res_calcolo else '0')
res_bc_sab = f.get('res_bc_sab') or (res_calcolo['bc_sab'] if res_calcolo and 'bc_sab' in res_calcolo else '0')
res_bc_ghi = f.get('res_bc_ghi') or (res_calcolo['bc_ghi'] if res_calcolo and 'bc_ghi' in res_calcolo else '0')
res_bc_aq = f.get('res_bc_aq') or (res_calcolo['bc_aq'] if res_calcolo and 'bc_aq' in res_calcolo else '0')
p_cem = f.get('p_cem', '1')
p_sab = f.get('p_sab', '3')
p_ghi = f.get('p_ghi', '5')
pc_cem = f.get('pc_cem', '0')
pc_sab = f.get('pc_sab', '0')
pc_ghi = f.get('pc_ghi', '0')
ps_content = f"""%!PS
/Helvetica-Bold findfont 11 scalefont setfont
70 760 moveto (DIMENSIONI GETTO) show
/Helvetica findfont 10 scalefont setfont
70 745 moveto (Altezza: {d_a} {d_ua} | Larghezza: {d_l} {d_ul} | Profondita: {d_p} {d_up}) show
/Helvetica-Bold findfont 11 scalefont setfont
70 720 moveto (VOLUME: {res_vol} m3 | Peso: {res_peso} Kg) show
70 700 moveto (ACQUISTI) show
/Helvetica findfont 10 scalefont setfont
70 685 moveto (o Cemento: {res_s_cem} sacchetti) show
70 670 moveto (o Sabbia: {res_s_sab} sacchetti) show
70 655 moveto (o Ghiaia: {res_s_ghi} sacchetti) show
70 640 moveto (o Acqua: {res_aq} L | Totale: {res_tot_s} sacch.) show
/Helvetica-Bold findfont 11 scalefont setfont
70 620 moveto (COSTO TOTALE: {res_costo_t} Euro) show
70 600 moveto (PROPORZIONI) show
/Helvetica findfont 10 scalefont setfont
70 585 moveto (Cem: {p_cem} ({pc_cem}%) | Sab: {p_sab} ({pc_sab}%) | Ghi: {p_ghi} ({pc_ghi}%)) show
70 560 moveto (PER OGNI CARICO ({res_nc} CARICHI):) show
70 545 moveto (o Cemento: {res_bc_cem} sacchetti) show
70 530 moveto (o Sabbia: {res_bc_sab} sacchetti) show
70 515 moveto (o Ghiaia: {res_bc_ghi} sacchetti) show
70 500 moveto (o Acqua: {res_bc_aq} L) show
showpage"""
# Salva il contenuto PS per debug
try:
with open('/tmp/stampa_cups.ps', 'w', encoding='utf-8') as f_debug:
f_debug.write(ps_content)
except:
pass # Ignora errori di scrittura del debug
# Converti in bytes
ps_bytes = ps_content.encode('utf-8')
# Esegui il comando CUPS - VERSIONE SEMPLIFICATA
result = subprocess.run(
["docker", "exec", "-i", "cups-pdf-server", "lp", "-d", "Virtual_PDF"],
input=ps_bytes,
capture_output=True,
timeout=30
)
if result.returncode == 0:
return "OK - PDF generato in /output del container cups-pdf-server!"
else:
error_msg = result.stderr.decode('utf-8', errors='ignore') if result.stderr else "Errore sconosciuto"
return f"Errore CUPS: {error_msg}"
except subprocess.TimeoutExpired:
return "Errore: Timeout durante la stampa CUPS"
except FileNotFoundError:
return "Errore: Docker o container cups-pdf-server non trovato"
except Exception as e:
# Gestisci l'errore in modo sicuro
error_msg = str(e)
if "'bytes' object has no attribute 'encode'" in error_msg:
return "Errore interno: problema nella conversione dei dati per la stampa"
else:
return f"Errore di stampa: {error_msg}"
@app.route('/download', methods=['POST'])
def download():
f = request.form
report = (
f"DIMENSIONI GETTO\n"
f"Altezza: {f.get('d_a', '0')} {f.get('d_ua', 'm')} Larghezza: {f.get('d_l', '0')} {f.get('d_ul', 'm')} Profondità: {f.get('d_p', '0')} {f.get('d_up', 'm')}\n\n"
f"VOLUME: {f.get('res_vol', '0')} m3 | Peso: {f.get('res_peso', '0')} Kg\n\n"
f"ACQUISTI\n"
f"• Cemento: \t{f.get('res_s_cem', '0')} sacchetti\n"
f"• Sabbia: \t{f.get('res_s_sab', '0')} sacchetti\n"
f"• Ghiaia: \t{f.get('res_s_ghi', '0')} sacchetti\n"
f"• Acqua: \t{f.get('res_aq', '0')} L | Totale: {f.get('res_tot_s', '0')} sacch.\n\n"
f"COSTO TOTALE: \t{f.get('res_costo_t', '0')}\n\n"
f"PROPORZIONI\n"
f"Cemento: {f.get('p_cem', '1')} \tSabbia: {f.get('p_sab', '3')} \tGhiaia: {f.get('p_ghi', '5')}\n"
f"Cemento: {f.get('pc_cem', '0')}% \tSabbia: {f.get('pc_sab', '0')}% \tGhiaia: {f.get('pc_ghi', '0')}%\n\n"
f"PER OGNI CARICO ({f.get('res_nc', '1')} CARICHI):\n\n"
f"• Cemento: \t{f.get('res_bc_cem', '0')} sacchetti\n"
f"• Sabbia: \t{f.get('res_bc_sab', '0')} sacchetti\n"
f"• Ghiaia: \t{f.get('res_bc_ghi', '0')} sacchetti\n"
f"• Acqua: \t{f.get('res_bc_aq', '0')} L"
)
return Response(report, mimetype="text/plain", headers={"Content-disposition":"attachment;filename=Report_V1_2.txt"})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)

4
log.txt Normal file
View File

@@ -0,0 +1,4 @@
* Serving Flask app 'cemento'
* Debug mode: on
Address already in use
Port 5000 is in use by another program. Either identify and stop that program, or start the server with a different port.

4
output/report.pdf Executable file
View File

@@ -0,0 +1,4 @@
RISULTATI
Volume: 0.000 m3
Betoniera: 160.0 L
Carichi: 1

4
output/report.txt Executable file
View File

@@ -0,0 +1,4 @@
RISULTATI
Volume: 0.000 m3
Betoniera: 160.0 L
Carichi: 1

107
templates/index.html Executable file
View File

@@ -0,0 +1,107 @@
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<title>Software Cemento V 1.2</title>
<style>
:root { --mac-bg: #f2f2f7; --mac-blue: #007aff; --mac-red: #ff3b30; --mac-green: #34c759; --mac-gray: #8e8e93; }
body { font-family: -apple-system, sans-serif; background: var(--mac-bg); padding: 20px; font-size: 14px; }
.container { display: flex; flex-wrap: wrap; gap: 20px; justify-content: center; max-width: 1200px; margin: auto; }
.panel { background: rgba(255,255,255,0.9); border-radius: 12px; padding: 20px; flex: 1; min-width: 320px; box-shadow: 0 4px 15px rgba(0,0,0,0.05); }
table { width: 100%; border-collapse: collapse; margin-bottom: 15px; border-radius: 8px; overflow: hidden; background: white; }
th { padding: 10px; font-size: 13px; color: white; text-transform: uppercase; }
td { padding: 8px; border: 1px solid #e5e5ea; text-align: center; }
.h-blue { background: var(--mac-blue); }
.h-red { background: var(--mac-red); }
input { width: 85%; border: 1px solid #d1d1d6; border-radius: 5px; padding: 4px; text-align: center; }
.btn { width: 100%; padding: 12px; border-radius: 8px; border: none; font-weight: bold; cursor: pointer; margin-top: 10px; color: white; text-transform: uppercase; }
.btn-calc { background: var(--mac-green); }
.btn-cups { background: var(--mac-blue); }
.btn-exp { background: #5856d6; }
.btn-device { background: #c7c7cc; color: #1c1c1e; }
.res-box { border: 1px solid #d1d1d6; border-radius: 10px; padding: 15px; background: #fff; line-height: 1.6; }
ul { list-style: none; padding: 0; margin: 0; }
li { padding-left: 15px; position: relative; }
li::before { content: "•"; position: absolute; left: 0; }
hr { border: 0; border-top: 1px solid #d1d1d6; margin: 15px 0; }
.footer { text-align: center; font-size: 10px; color: var(--mac-gray); margin-top: 15px; }
@media print { .no-print { display: none !important; } }
</style>
</head>
<body>
<div class="container">
<div class="panel no-print">
<form method="POST">
<table><tr class="h-blue"><th colspan="3">Dimensioni</th></tr>
<tr><td>Altezza</td><td><input type="text" name="a" value="{{form.get('a','0')}}"></td><td><select name="ua">{% for u in ['m','cm','mm'] %}<option value="{{u}}" {% if form.get('ua','m')==u %}selected{% endif %}>{{u}}</option>{% endfor %}</select></td></tr>
<tr><td>Larghezza</td><td><input type="text" name="l" value="{{form.get('l','0')}}"></td><td><select name="ul">{% for u in ['m','cm','mm'] %}<option value="{{u}}" {% if form.get('ul','m')==u %}selected{% endif %}>{{u}}</option>{% endfor %}</select></td></tr>
<tr><td>Profondità</td><td><input type="text" name="p" value="{{form.get('p','0')}}"></td><td><select name="up">{% for u in ['m','cm','mm'] %}<option value="{{u}}" {% if form.get('up','m')==u %}selected{% endif %}>{{u}}</option>{% endfor %}</select></td></tr>
</table>
<table><tr class="h-red"><th colspan="3">Miscela (Parti)</th></tr>
<tr><td>Cem:<br><input type="text" name="p_cem" value="{{form.get('p_cem','1')}}"></td><td>Sab:<br><input type="text" name="p_sab" value="{{form.get('p_sab','3')}}"></td><td>Ghi:<br><input type="text" name="p_ghi" value="{{form.get('p_ghi','5')}}"></td></tr>
</table>
<table><tr class="h-red"><th colspan="3">Peso sacchette (Kg)</th></tr>
<tr><td>Cem:<input type="text" name="w_cem" value="{{form.get('w_cem','25')}}"></td><td>Sab:<input type="text" name="w_sab" value="{{form.get('w_sab','25')}}"></td><td>Ghi:<input type="text" name="w_ghi" value="{{form.get('w_ghi','25')}}"></td></tr>
</table>
<table><tr class="h-red"><th colspan="3">Costo sacchette (€)</th></tr>
<tr><td>Cem:<input type="text" name="c_cem" value="{{form.get('c_cem','1')}}"></td><td>Sab:<input type="text" name="c_sab" value="{{form.get('c_sab','1')}}"></td><td>Ghi:<input type="text" name="c_ghi" value="{{form.get('c_ghi','1')}}"></td></tr>
</table>
<table><tr class="h-red"><th>Attrezzatura</th></tr><tr><td>Volume Betoniera: <input type="text" name="v_bet" value="{{form.get('v_bet','160')}}" style="width:60px;"> L</td></tr></table>
<button type="submit" class="btn btn-calc">Esegui Calcolo</button>
</form>
<div class="footer">VERSIONE SOFTWARE: V 1.2 | DATA: 20/01/2026</div>
</div>
<div class="panel">
<h3>Risultati</h3>
{% if res %}
<div class="res-box">
<b>DIMENSIONI GETTO</b><br>
Altezza: {{res.dim.a}} {{res.dim.ua}} &nbsp; Larghezza: {{res.dim.l}} {{res.dim.ul}} &nbsp; Profondità: {{res.dim.p}} {{res.dim.up}}<br><br>
<b>VOLUME:</b> {{ res.vol }} m³ | <b>Peso:</b> {{ res.peso }} Kg
<hr>
<b>ACQUISTI</b>
<ul>
<li>Cemento: {{ res.s_cem }} sacchetti</li>
<li>Sabbia: {{ res.s_sab }} sacchetti</li>
<li>Ghiaia: {{ res.s_ghi }} sacchetti</li>
<li>Acqua: {{ res.acqua }} L | <b>Totale: {{ res.tot_sacc }} sacch.</b></li>
</ul>
<br>
<b>COSTO TOTALE: {{ res.costo_tot }} €</b>
<hr>
<b>PROPORZIONI</b><br>
Cemento: {{res.parti.cem}} &nbsp; Sabbia: {{res.parti.sab}} &nbsp; Ghiaia: {{res.parti.ghi}}<br>
Cemento: {{ "%.1f"|format(res.perc.cem) }}% &nbsp; Sabbia: {{ "%.1f"|format(res.perc.sab) }}% &nbsp; Ghiaia: {{ "%.1f"|format(res.perc.ghi) }}%
<hr>
<b>PER OGNI CARICO ({{ res.n_c }} CARICHI):</b>
<ul>
<li>• Cemento: {{ res.bc_cem }} sacchetti</li>
<li>• Sabbia: {{ res.bc_sab }} sacchetti</li>
<li>• Ghiaia: {{ res.bc_ghi }} sacchetti</li>
<li>• Acqua: {{ res.bc_aq }} L</li>
</ul>
</div>
<form method="POST" class="no-print">
{% for k,v in form.items() %}<input type="hidden" name="{{k}}" value="{{v}}">{% endfor %}
<input type="hidden" name="d_a" value="{{res.dim.a}}"><input type="hidden" name="d_ua" value="{{res.dim.ua}}">
<input type="hidden" name="d_l" value="{{res.dim.l}}"><input type="hidden" name="d_ul" value="{{res.dim.ul}}">
<input type="hidden" name="d_p" value="{{res.dim.p}}"><input type="hidden" name="d_up" value="{{res.dim.up}}">
<input type="hidden" name="p_cem" value="{{res.parti.cem}}"><input type="hidden" name="p_sab" value="{{res.parti.sab}}"><input type="hidden" name="p_ghi" value="{{res.parti.ghi}}">
<input type="hidden" name="pc_cem" value="{{'%.1f'|format(res.perc.cem)}}"><input type="hidden" name="pc_sab" value="{{'%.1f'|format(res.perc.sab)}}"><input type="hidden" name="pc_ghi" value="{{'%.1f'|format(res.perc.ghi)}}">
<input type="hidden" name="res_vol" value="{{res.vol}}"><input type="hidden" name="res_peso" value="{{res.peso}}">
<input type="hidden" name="res_aq" value="{{res.acqua}}"><input type="hidden" name="res_tot_s" value="{{res.tot_sacc}}">
<input type="hidden" name="res_costo_t" value="{{res.costo_tot}}"><input type="hidden" name="res_nc" value="{{res.n_c}}">
<input type="hidden" name="res_s_cem" value="{{res.s_cem}}"><input type="hidden" name="res_s_sab" value="{{res.s_sab}}"><input type="hidden" name="res_s_ghi" value="{{res.s_ghi}}">
<input type="hidden" name="res_bc_cem" value="{{res.bc_cem}}"><input type="hidden" name="res_bc_sab" value="{{res.bc_sab}}"><input type="hidden" name="res_bc_ghi" value="{{res.bc_ghi}}"><input type="hidden" name="res_bc_aq" value="{{res.bc_aq}}">
<button type="submit" name="btn_stampa_cups" class="btn btn-cups">Stampa PDF (CUPS)</button>
<button type="submit" formaction="/download" class="btn btn-exp">Esporta TXT</button>
<button type="button" onclick="window.print()" class="btn btn-device">Stampa Pagina</button>
</form>
{% if msg_stampa %}<p style="color:var(--mac-green); font-weight:bold; text-align:center;">{{ msg_stampa }}</p>{% endif %}
{% endif %}
</div>
</div>
</body>
</html>