warehouse_report_service.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import os
  2. import db
  3. import config
  4. from fpdf import FPDF
  5. from datetime import datetime
  6. class WarehouseReportService:
  7. @staticmethod
  8. def generate_report():
  9. # Fetch all stock items with material names
  10. query = """
  11. SELECT w.*, m.name_en as material_name, m.name_ru as material_name_ru
  12. FROM warehouse_stock w
  13. JOIN materials m ON w.material_id = m.id
  14. ORDER BY m.name_en, w.color_name
  15. """
  16. stock = db.execute_query(query)
  17. pdf = FPDF(orientation='P', unit='mm', format='A4')
  18. pdf.add_page()
  19. # Header
  20. pdf.set_font("helvetica", "B", 16)
  21. pdf.cell(0, 10, "Warehouse Stock Report", ln=True, align='C')
  22. pdf.set_font("helvetica", "", 10)
  23. pdf.cell(0, 5, f"Date: {datetime.now().strftime('%d.%m.%Y %H:%M')}", ln=True, align='C')
  24. pdf.ln(10)
  25. # Table Header
  26. pdf.set_fill_color(240, 240, 240)
  27. pdf.set_font("helvetica", "B", 10)
  28. pdf.cell(60, 10, "Material", border=1, fill=True)
  29. pdf.cell(40, 10, "Color", border=1, fill=True)
  30. pdf.cell(30, 10, "Unit Mass", border=1, fill=True, align='C')
  31. pdf.cell(30, 10, "Units", border=1, fill=True, align='C')
  32. pdf.cell(30, 10, "Total (kg)", border=1, fill=True, align='C')
  33. pdf.ln()
  34. # Table Body
  35. pdf.set_font("helvetica", "", 10)
  36. total_weight = 0
  37. for item in stock:
  38. pdf.cell(60, 10, str(item['material_name']), border=1)
  39. pdf.cell(40, 10, str(item['color_name']), border=1)
  40. pdf.cell(30, 10, f"{item['unit_mass']:.3f}", border=1, align='C')
  41. pdf.cell(30, 10, str(item['units_count']), border=1, align='C')
  42. pdf.cell(30, 10, f"{item['quantity']:.3f}", border=1, align='C')
  43. pdf.ln()
  44. total_weight += float(item['quantity'])
  45. # Total
  46. pdf.set_font("helvetica", "B", 10)
  47. pdf.cell(160, 10, "GRAND TOTAL WEIGHT (kg):", border=1, align='R')
  48. pdf.cell(30, 10, f"{total_weight:.3f}", border=1, align='C')
  49. # Output
  50. filename = f"warehouse_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
  51. report_dir = os.path.join(config.UPLOAD_DIR, "reports")
  52. os.makedirs(report_dir, exist_ok=True)
  53. filepath = os.path.join(report_dir, filename)
  54. pdf.output(filepath)
  55. return os.path.join("uploads", "reports", filename).replace("\\", "/")
  56. warehouse_report_service = WarehouseReportService()