115 lines
5.3 KiB
Python
115 lines
5.3 KiB
Python
# -*- 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: 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:
|
|
msg_stampa = esegui_stampa_cups(request.form)
|
|
return render_template('index.html', res=res, form=request.form, msg_stampa=msg_stampa)
|
|
|
|
def esegui_stampa_cups(f):
|
|
try:
|
|
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: {f.get('d_a')} {f.get('d_ua')} | Larghezza: {f.get('d_l')} {f.get('d_ul')} | Profondita: {f.get('d_p')} {f.get('d_up')}) show
|
|
/Helvetica-Bold findfont 11 scalefont setfont
|
|
70 720 moveto (VOLUME: {f.get('res_vol')} m3 | Peso: {f.get('res_peso')} Kg) show
|
|
70 700 moveto (ACQUISTI) show
|
|
/Helvetica findfont 10 scalefont setfont
|
|
70 685 moveto (o Cemento: {f.get('res_s_cem')} sacchetti) show
|
|
70 670 moveto (o Sabbia: {f.get('res_s_sab')} sacchetti) show
|
|
70 655 moveto (o Ghiaia: {f.get('res_s_ghi')} sacchetti) show
|
|
70 640 moveto (o Acqua: {f.get('res_aq')} L | Totale: {f.get('res_tot_s')} sacch.) show
|
|
/Helvetica-Bold findfont 11 scalefont setfont
|
|
70 620 moveto (COSTO TOTALE: {f.get('res_costo_t')} Euro) show
|
|
70 600 moveto (PROPORZIONI) show
|
|
/Helvetica findfont 10 scalefont setfont
|
|
70 585 moveto (Cem: {f.get('p_cem')} ({f.get('pc_cem')}%) | Sab: {f.get('p_sab')} ({f.get('pc_sab')}%) | Ghi: {f.get('p_ghi')} ({f.get('pc_ghi')}%)) show
|
|
70 560 moveto (PER OGNI CARICO ({f.get('res_nc')} CARICHI):) show
|
|
70 545 moveto (o Cemento: {f.get('res_bc_cem')} sacchetti) show
|
|
70 530 moveto (o Sabbia: {f.get('res_bc_sab')} sacchetti) show
|
|
70 515 moveto (o Ghiaia: {f.get('res_bc_ghi')} sacchetti) show
|
|
70 500 moveto (o Acqua: {f.get('res_bc_aq')} L) show
|
|
showpage"""
|
|
subprocess.run(["docker", "exec", "-i", "cups-pdf-server", "lp", "-d", "Virtual_PDF"], input=ps_content.encode('utf-8'))
|
|
return "OK - PDF generato in /output!"
|
|
except: return "Errore di stampa"
|
|
|
|
@app.route('/download', methods=['POST'])
|
|
def download():
|
|
f = request.form
|
|
report = (
|
|
f"DIMENSIONI GETTO\n"
|
|
f"Altezza: {f.get('d_a')} {f.get('d_ua')} Larghezza: {f.get('d_l')} {f.get('d_ul')} Profondità: {f.get('d_p')} {f.get('d_up')}\n\n"
|
|
f"VOLUME: {f.get('res_vol')} m3 | Peso: {f.get('res_peso')} Kg\n\n"
|
|
f"ACQUISTI\n"
|
|
f"• Cemento: \t{f.get('res_s_cem')} sacchetti\n"
|
|
f"• Sabbia: \t{f.get('res_s_sab')} sacchetti\n"
|
|
f"• Ghiaia: \t{f.get('res_s_ghi')} sacchetti\n"
|
|
f"• Acqua: \t{f.get('res_aq')} L | Totale: {f.get('res_tot_s')} sacch.\n\n"
|
|
f"COSTO TOTALE: \t{f.get('res_costo_t')} €\n\n"
|
|
f"PROPORZIONI\n"
|
|
f"Cemento: {f.get('p_cem')} \tSabbia: {f.get('p_sab')} \tGhiaia: {f.get('p_ghi')}\n"
|
|
f"Cemento: {f.get('pc_cem')}% \tSabbia: {f.get('pc_sab')}% \tGhiaia: {f.get('pc_ghi')}%\n\n"
|
|
f"PER OGNI CARICO ({f.get('res_nc')} CARICHI):\n\n"
|
|
f"• Cemento: \t{f.get('res_bc_cem')} sacchetti\n"
|
|
f"• Sabbia: \t{f.get('res_bc_sab')} sacchetti\n"
|
|
f"• Ghiaia: \t{f.get('res_bc_ghi')} sacchetti\n"
|
|
f"• Acqua: \t{f.get('res_bc_aq')} 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) |