Kolommen B-I (Firma, Van, Tot, Pauze, Uren, Galaxy, Auro 3D, Firma 3) op 2,5 cm (13.5 Excel-breedte-eenheden) per maandtabblad.
132 lines
4.4 KiB
Python
132 lines
4.4 KiB
Python
import argparse
|
|
import calendar
|
|
from copy import copy
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
import openpyxl
|
|
from openpyxl.styles import Font, Protection
|
|
from openpyxl.worksheet.datavalidation import DataValidation
|
|
|
|
TEMPLATE = Path(__file__).resolve().parent / "docs" / "Ineas.xlsx"
|
|
SETTINGS_SHEET = "Instellingen"
|
|
FIRM_NAMES = ["Galaxy", "Auro 3D", "Firma 3"]
|
|
FIRM_RATES = [20, 20, 40]
|
|
FIRM_COLS = ["G", "H", "I"]
|
|
MONTH_ABBR = ["jan", "feb", "mrt", "apr", "mei", "jun",
|
|
"jul", "aug", "sep", "okt", "nov", "dec"]
|
|
STYLE_COLS = "ABCDEFGHIJ"
|
|
WIDTH_COLS = "BCDEFGHI" # Firma Van Tot Pauze Uren Galaxy Auro 3D Firma 3
|
|
COL_WIDTH = round(2.5 * 96 / 2.54 / 7, 2) # 2,5 cm in Excel-karakterbreedte
|
|
|
|
|
|
def build_month(ws, year, month, day_styles, total_styles, protection):
|
|
days = calendar.monthrange(year, month)[1]
|
|
|
|
if ws.max_row >= 7:
|
|
ws.delete_rows(7, ws.max_row - 6)
|
|
|
|
ws["C1"] = datetime(year, month, 1)
|
|
|
|
for i, col in enumerate(FIRM_COLS):
|
|
ws[f"{col}4"] = f"={col}6"
|
|
ws[f"{col}5"] = f"={SETTINGS_SHEET}!$B${2 + i}"
|
|
ws[f"{col}6"] = f"={SETTINGS_SHEET}!$A${2 + i}"
|
|
ws[f"{col}6"].protection = Protection(locked=False)
|
|
ws[f"{col}6"].fill = copy(ws[f"{col}5"].fill)
|
|
|
|
for d in range(days):
|
|
r = 7 + d
|
|
ws[f"A{r}"] = "=C1" if d == 0 else f"=A{r - 1}+1"
|
|
ws[f"F{r}"] = f"=D{r}-C{r}-E{r}"
|
|
for col in FIRM_COLS:
|
|
ws[f"{col}{r}"] = f"=IF($B{r}={col}$4,$F{r}*{col}$5,0)"
|
|
for col in STYLE_COLS:
|
|
ws[f"{col}{r}"]._style = copy(day_styles[col])
|
|
ws.row_dimensions[r].height = None
|
|
|
|
total = 6 + days + 1
|
|
first, last = 7, 6 + days
|
|
ws[f"A{total}"] = "Totaal"
|
|
ws[f"F{total}"] = f"=SUM(F{first}:F{last})"
|
|
for col in FIRM_COLS:
|
|
ws[f"{col}{total}"] = f"=SUM({col}{first}:{col}{last})"
|
|
for col in STYLE_COLS:
|
|
ws[f"{col}{total}"]._style = copy(total_styles[col])
|
|
|
|
hours = total + 1
|
|
ws[f"A{hours}"] = "Uren per klant"
|
|
for col in STYLE_COLS:
|
|
ws[f"{col}{hours}"]._style = copy(total_styles[col])
|
|
for col in FIRM_COLS:
|
|
ws[f"{col}{hours}"] = f"=SUMIF($B${first}:$B${last},{col}$4,$F${first}:$F${last})"
|
|
ws[f"{col}{hours}"].number_format = "0.00"
|
|
|
|
dv = DataValidation(type="list", allow_blank=True,
|
|
formula1="$G$6:$I$6")
|
|
ws.add_data_validation(dv)
|
|
dv.add(f"B{first}:B{last}")
|
|
|
|
ws.protection = copy(protection)
|
|
for col in WIDTH_COLS:
|
|
ws.column_dimensions[col].width = COL_WIDTH
|
|
ws.print_area = f"A1:J{hours}"
|
|
ws.print_title_rows = "1:6"
|
|
|
|
|
|
def add_settings_sheet(wb):
|
|
ws = wb.create_sheet(SETTINGS_SHEET)
|
|
ws["A1"], ws["B1"] = "Firma", "Tarief/uur"
|
|
ws["A1"].font = Font(bold=True)
|
|
ws["B1"].font = Font(bold=True)
|
|
for i, (name, rate) in enumerate(zip(FIRM_NAMES, FIRM_RATES)):
|
|
ws[f"A{2 + i}"] = name
|
|
ws[f"B{2 + i}"] = rate
|
|
ws[f"B{2 + i}"].number_format = "#,##0.00"
|
|
ws.column_dimensions["A"].width = 20
|
|
ws.column_dimensions["B"].width = 12
|
|
return ws
|
|
|
|
|
|
def generate(year, months):
|
|
if not TEMPLATE.exists():
|
|
raise SystemExit(f"Template niet gevonden: {TEMPLATE}")
|
|
|
|
wb = openpyxl.load_workbook(TEMPLATE)
|
|
master = wb["Sheet1"]
|
|
day_styles = {c: copy(master[f"{c}7"]._style) for c in STYLE_COLS}
|
|
total_styles = {c: copy(master[f"{c}37"]._style) for c in STYLE_COLS}
|
|
protection = copy(master.protection)
|
|
|
|
sheets = [master] + [wb.copy_worksheet(master) for _ in range(len(months) - 1)]
|
|
for ws, month in zip(sheets, months):
|
|
ws.title = f"{month:02d}-{MONTH_ABBR[month - 1]}"
|
|
build_month(ws, year, month, day_styles, total_styles, protection)
|
|
|
|
add_settings_sheet(wb)
|
|
|
|
if len(months) == 12:
|
|
out = Path(f"prestatieformulier-{year}.xlsx")
|
|
else:
|
|
out = Path(f"prestatieformulier-{year}-{months[0]:02d}.xlsx")
|
|
wb.save(out)
|
|
return out
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description="Genereer een Excel prestatieformulier.")
|
|
parser.add_argument("--year", type=int, required=True,
|
|
help="Jaartal, bv. 2026")
|
|
parser.add_argument("--month", type=int, choices=range(1, 13),
|
|
help="Maand 1-12; weglaten voor een volledig jaar")
|
|
args = parser.parse_args()
|
|
|
|
months = [args.month] if args.month else list(range(1, 13))
|
|
out = generate(args.year, months)
|
|
print(f"Gegenereerd: {out}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|