Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,118 +1,113 @@
|
|
| 1 |
-
from io import BytesIO
|
| 2 |
-
from reportlab.lib.pagesizes import letter
|
| 3 |
-
from reportlab.pdfgen import canvas
|
| 4 |
-
from reportlab.
|
| 5 |
-
from
|
| 6 |
-
from convertion import
|
| 7 |
-
from
|
| 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 |
-
c
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
#
|
| 58 |
-
c.
|
| 59 |
-
c.
|
| 60 |
-
c.
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
c.
|
| 65 |
-
c.
|
| 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 |
-
# Create
|
| 91 |
-
data =
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
('
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
# Save the PDF
|
| 115 |
-
c.save()
|
| 116 |
-
|
| 117 |
-
buffer.seek(0)
|
| 118 |
return buffer
|
|
|
|
| 1 |
+
from io import BytesIO
|
| 2 |
+
from reportlab.lib.pagesizes import letter
|
| 3 |
+
from reportlab.pdfgen import canvas
|
| 4 |
+
from reportlab.lib.styles import getSampleStyleSheet
|
| 5 |
+
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
|
| 6 |
+
from convertion import convert_feet_to_inches,convert_inches_to_feet
|
| 7 |
+
from convertion import convert_meters_to_unit, convert_to_meters
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Common function for shutter and glass calculations
|
| 11 |
+
def calculate_reduced_dimensions(width, height, unit, selection, object_item_value, item_type="Door"):
|
| 12 |
+
# Use the selected unit for the width and height reduction
|
| 13 |
+
if unit == 'Feet':
|
| 14 |
+
reduction_width = convert_inches_to_feet(object_item_value[selection][item_type]['width'])
|
| 15 |
+
reduction_height = convert_inches_to_feet(object_item_value[selection][item_type]['height'])
|
| 16 |
+
else:
|
| 17 |
+
reduction_width = object_item_value[selection][item_type]['width']
|
| 18 |
+
reduction_height = object_item_value[selection][item_type]['height']
|
| 19 |
+
|
| 20 |
+
# Calculate the reduced dimensions based on the width and height
|
| 21 |
+
reduced_width = (width - reduction_width) / 2.0
|
| 22 |
+
reduced_height = height - reduction_height
|
| 23 |
+
return reduced_width, reduced_height
|
| 24 |
+
|
| 25 |
+
# Functions for door and window area/frame calculation
|
| 26 |
+
def calculate_area_and_frame(width, height, unit, item_type="Door"):
|
| 27 |
+
# Convert width and height to meters for area and frame calculation
|
| 28 |
+
width_in_meters = convert_to_meters(width, unit)
|
| 29 |
+
height_in_meters = convert_to_meters(height, unit)
|
| 30 |
+
area = width_in_meters * height_in_meters
|
| 31 |
+
frame_thickness = {'Door': 0.1, 'Window': 0.05}
|
| 32 |
+
frame_area = 2 * (width_in_meters + height_in_meters) * frame_thickness.get(item_type, 0.1)
|
| 33 |
+
|
| 34 |
+
# Convert area to square feet if necessary
|
| 35 |
+
if unit == "Feet":
|
| 36 |
+
area = area * 10.7639 # Convert square meters to square feet
|
| 37 |
+
frame_area = frame_area * 10.7639 # Convert frame area to square feet
|
| 38 |
+
# If the unit is inches, convert to square feet
|
| 39 |
+
elif unit == "Inches":
|
| 40 |
+
area = (area * 0.092903) * 10.7639 # Convert inches squared to square feet
|
| 41 |
+
frame_area = (frame_area * 0.092903) * 10.7639 # Convert frame area to square feet
|
| 42 |
+
|
| 43 |
+
return area, frame_area
|
| 44 |
+
|
| 45 |
+
def generate_pdf(dfs, company_name, address, unit, items):
|
| 46 |
+
buffer = BytesIO()
|
| 47 |
+
|
| 48 |
+
# Create a canvas for the PDF
|
| 49 |
+
c = canvas.Canvas(buffer, pagesize=letter)
|
| 50 |
+
width, height = letter
|
| 51 |
+
|
| 52 |
+
# Company details
|
| 53 |
+
c.setFont("Helvetica", 12)
|
| 54 |
+
c.drawString(30, height - 40, f"Company Name: {company_name}")
|
| 55 |
+
c.drawString(30, height - 60, f"Address: {address}")
|
| 56 |
+
|
| 57 |
+
# Draw a line under the address
|
| 58 |
+
c.setStrokeColor(colors.black)
|
| 59 |
+
c.setLineWidth(1)
|
| 60 |
+
c.line(30, height - 70, width - 30, height - 70) # Draw line under the address
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# Title
|
| 64 |
+
c.setFont("Helvetica-Bold", 16)
|
| 65 |
+
c.drawString(30, height - 100, f"Dimensions Calculation Results: for units ({unit})")
|
| 66 |
+
|
| 67 |
+
# Calculate the y-position to avoid overlapping
|
| 68 |
+
y_position = height - 150
|
| 69 |
+
|
| 70 |
+
# Iterate over each item in the items list
|
| 71 |
+
for i, item in enumerate(items):
|
| 72 |
+
# Add a new page if the y-position gets too low
|
| 73 |
+
if y_position < 100:
|
| 74 |
+
c.showPage()
|
| 75 |
+
y_position = height - 40 # Reset the y-position
|
| 76 |
+
|
| 77 |
+
# Display item label (e.g., Item 1, Item 2)
|
| 78 |
+
c.setFont("Helvetica-Bold", 12)
|
| 79 |
+
c.drawString(30, y_position, f"Item {i+1}: {item}")
|
| 80 |
+
y_position -= 130 # Move down after the label
|
| 81 |
+
|
| 82 |
+
# Retrieve the corresponding dataframe for this item
|
| 83 |
+
df = dfs[i]
|
| 84 |
+
|
| 85 |
+
# Create and style the table
|
| 86 |
+
data = [["Dimension", "Outer Frame", "Shutter", "Glass"]] # Table Header
|
| 87 |
+
for index, row in df.iterrows():
|
| 88 |
+
data.append([index] + [str(value) for value in row]) # Use row values directly for each column
|
| 89 |
+
|
| 90 |
+
# Create the table
|
| 91 |
+
table = Table(data, colWidths=[100, 100, 100, 100], rowHeights=30)
|
| 92 |
+
table.setStyle(TableStyle([
|
| 93 |
+
('TEXTCOLOR', (0, 0), (-1, 0), (0, 0, 0)),
|
| 94 |
+
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
| 95 |
+
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
| 96 |
+
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
|
| 97 |
+
('BACKGROUND', (0, 0), (-1, 0), (0.7, 0.7, 0.7)),
|
| 98 |
+
('GRID', (0, 0), (-1, -1), 0.5, (0, 0, 0))
|
| 99 |
+
]))
|
| 100 |
+
|
| 101 |
+
# Position the table just below the item label
|
| 102 |
+
table.wrapOn(c, width, height)
|
| 103 |
+
table.drawOn(c, 30, y_position)
|
| 104 |
+
|
| 105 |
+
# Adjust the y-position for the next table
|
| 106 |
+
y_position -= len(data) + 20 # Move down for next table
|
| 107 |
+
c.line(30, y_position , width - 30, y_position) # Draw line under the address
|
| 108 |
+
|
| 109 |
+
# Save the PDF
|
| 110 |
+
c.save()
|
| 111 |
+
|
| 112 |
+
buffer.seek(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
return buffer
|