Versione 1.0 - Reset Totale Progetto
This commit is contained in:
205
cemento.py
Executable file
205
cemento.py
Executable 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)
|
||||
Reference in New Issue
Block a user