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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
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
55
56
57
58
59
60
61
62
63
64
65
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
"""Generate the demo documents bundled in this Space (synthetic, no real data)."""

from __future__ import annotations

from pathlib import Path

import fitz

HERE = Path(__file__).resolve().parents[1]
OUT = HERE / "samples"
OUT.mkdir(parents=True, exist_ok=True)

PAGE_W, PAGE_H = 612, 792
MARGIN = 46
INK = (0.09, 0.10, 0.13)
MUTED = (0.42, 0.45, 0.50)
RULE = (0.80, 0.82, 0.86)


def text(page, x, y, s, size=9.5, bold=False, color=INK, align="left"):
    font = "hebo" if bold else "helv"
    width = fitz.get_text_length(s, fontname=font, fontsize=size)
    if align == "right":
        x -= width
    elif align == "center":
        x -= width / 2
    page.insert_text((x, y), s, fontname=font, fontsize=size, color=color)
    return width


def rule(page, x0, y, x1, color=RULE, width=0.6):
    page.draw_line((x0, y), (x1, y), color=color, width=width)


def header(page, doc_title):
    text(page, MARGIN, 58, "NORTHWIND INDUSTRIAL SUPPLY", 15, bold=True)
    text(page, MARGIN, 74, "4400 Harbor Way, Oakland, CA 94607", 8.5, color=MUTED)
    text(
        page,
        MARGIN,
        86,
        "VAT / Tax ID 94-2817733  ·  accounts@northwind-supply.example",
        8.5,
        color=MUTED,
    )
    text(page, PAGE_W - MARGIN, 58, doc_title, 22, bold=True, align="right")
    rule(page, MARGIN, 100, PAGE_W - MARGIN, color=(0.20, 0.72, 0.60), width=1.4)


def meta_block(page, y):
    rows = [
        ("Invoice No.", "KY-2041"),
        ("Invoice Date", "2026-03-14"),
        ("Due Date", "2026-04-13"),
        ("PO Number", "PO-7731"),
        ("Terms", "Net 30"),
        ("Currency", "USD"),
    ]
    for i, (label, value) in enumerate(rows):
        text(page, PAGE_W - MARGIN - 150, y + i * 14, label, 8.5, color=MUTED)
        text(page, PAGE_W - MARGIN, y + i * 14, value, 9.5, bold=True, align="right")


def address_block(page, y, title, lines):
    text(page, MARGIN, y, title, 7.5, bold=True, color=MUTED)
    for i, line in enumerate(lines):
        text(page, MARGIN, y + 14 + i * 12, line, 9.5)
    return y + 14 + len(lines) * 12


ITEMS = [
    (1, "HI-4402", 'Hydraulic hose, 3/4" ID, 2-wire braid', 12, 48.50, 582.00),
    (2, "BR-2210", "Brass fittings, pack of 25", 6, 31.75, 190.50),
    (3, "FL-9001", "Filter element, 10 micron, spin-on", 24, 12.40, 297.60),
    (4, "SE-1180", "Seal kit, 63 mm cylinder", 8, 76.20, 609.60),
    (5, "VA-3345", "Directional valve, 4-way, 24 VDC", 3, 412.00, 1236.00),
    (6, "GA-0071", "Pressure gauge, 0-400 bar, glycerin filled", 10, 28.95, 289.50),
    (7, "PI-5520", 'Steel pipe, 1" schedule 80, 6 m', 40, 9.85, 394.00),
    (8, "LB-6630", "Lubricant, ISO VG 46, 5 L", 5, 54.30, 271.50),
    (9, "TK-8802", "Tool kit, seal puller and installers", 2, 96.75, 193.50),
]

COLS = [
    ("Line", 40, "left"),
    ("SKU", 95, "left"),
    ("Description", 190, "left"),
    ("Qty", 452, "right"),
    ("Unit price", 512, "right"),
    ("Amount", 566, "right"),
]


def items_table(page, y):
    for label, x, align in COLS:
        text(page, x, y, label, 8, bold=True, color=MUTED, align=align)
    rule(page, MARGIN, y + 5, PAGE_W - MARGIN, color=INK, width=0.8)
    y += 17
    for line, sku, desc, qty, price, amount in ITEMS:
        text(page, 40, y, str(line), 9)
        text(page, 95, y, sku, 9)
        text(page, 190, y, desc, 9)
        text(page, 452, y, f"{qty}", 9, align="right")
        text(page, 512, y, f"{price:,.2f}", 9, align="right")
        text(page, 566, y, f"{amount:,.2f}", 9, align="right")
        rule(page, MARGIN, y + 5, PAGE_W - MARGIN)
        y += 19
    return y


def totals(page, y):
    rows = [
        ("Subtotal", "4,064.20"),
        ("Discount (2%)", "-81.28"),
        ("Shipping (FOB origin)", "145.00"),
        ("Tax (8.25%)", "328.59"),
    ]
    for label, value in rows:
        text(page, 500, y, label, 9, color=MUTED, align="right")
        text(page, 566, y, value, 9, align="right")
        y += 15
    rule(page, 500, y - 5, 566, color=INK, width=0.8)
    y += 10
    text(page, 500, y, "Total due (USD)", 10.5, bold=True, align="right")
    text(page, 566, y, "4,456.51", 11.5, bold=True, align="right")
    return y


SCHEDULE = [
    (1, "HI-4402", 12, "2026-03-20", "OAK-1"),
    (2, "BR-2210", 6, "2026-03-20", "OAK-1"),
    (3, "FL-9001", 24, "2026-03-24", "SAC-2"),
    (4, "SE-1180", 8, "2026-03-24", "SAC-2"),
    (5, "VA-3345", 3, "2026-03-27", "RNO-3"),
    (6, "GA-0071", 10, "2026-03-27", "RNO-3"),
    (7, "PI-5520", 40, "2026-03-31", "OAK-1"),
    (8, "LB-6630", 5, "2026-04-02", "SAC-2"),
    (9, "TK-8802", 2, "2026-04-02", "RNO-3"),
]


def page_one(page):
    header(page, "INVOICE")
    meta_block(page, 126)
    y = address_block(
        page,
        126,
        "VENDOR",
        [
            "Northwind Industrial Supply",
            "4400 Harbor Way",
            "Oakland, CA 94607",
            "United States",
        ],
    )
    address_block(
        page,
        y + 18,
        "BILL TO / SHIP TO",
        [
            "Meridian Fabrication Ltd.",
            "1180 Foundry Road, Building C",
            "Sacramento, CA 95814",
            "United States",
        ],
    )

    y = max(y + 90, 250)
    text(page, MARGIN, y, "Line items", 11, bold=True)
    y = items_table(page, y + 18)
    totals(page, y + 18)

    rule(page, MARGIN, 700, PAGE_W - MARGIN)
    text(
        page,
        MARGIN,
        716,
        "Payment terms: Net 30 from the invoice date. Remit to Northwind Industrial Supply,",
        8,
        color=MUTED,
    )
    text(
        page,
        MARGIN,
        728,
        "Account 4400-118-226, Routing 121000248. Please quote invoice KY-2041.",
        8,
        color=MUTED,
    )
    text(page, PAGE_W - MARGIN, 716, "Page 1 of 2", 8, color=MUTED, align="right")


def page_two(page):
    header(page, "INVOICE")
    text(page, MARGIN, 126, "Invoice KY-2041  ·  delivery schedule", 13, bold=True)
    text(
        page,
        MARGIN,
        144,
        "Lines shipped from three warehouses; quantities match the invoice lines.",
        8.5,
        color=MUTED,
    )

    cols = [
        ("Line", 40, "left"),
        ("SKU", 100, "left"),
        ("Qty", 300, "right"),
        ("Ship date", 420, "left"),
        ("Warehouse", 566, "right"),
    ]
    y = 176
    for label, x, align in cols:
        text(page, x, y, label, 8, bold=True, color=MUTED, align=align)
    rule(page, MARGIN, y + 5, PAGE_W - MARGIN, color=INK, width=0.8)
    y += 18
    for line, sku, qty, ship, wh in SCHEDULE:
        text(page, 40, y, str(line), 9)
        text(page, 100, y, sku, 9)
        text(page, 300, y, str(qty), 9, align="right")
        text(page, 420, y, ship, 9)
        text(page, 566, y, wh, 9, align="right")
        rule(page, MARGIN, y + 5, PAGE_W - MARGIN)
        y += 19

    y += 36
    text(page, MARGIN, y, "Terms and conditions", 11, bold=True)
    paragraphs = [
        "Goods remain the property of Northwind Industrial Supply until paid in full. Risk passes to the",
        "buyer on delivery. Claims for shortage or damage must be made in writing within 7 days of receipt;",
        "after that period the delivery is deemed accepted.",
        "",
        "Late payments accrue interest at 1.5% per month or the maximum permitted by law, whichever is lower.",
        "Prices are exclusive of duties and local taxes unless stated otherwise. Returns require a written",
        "authorisation and are subject to a 15% restocking fee.",
        "",
        "This invoice is generated for demonstration purposes; the company, addresses and identifiers are",
        "fictional.",
    ]
    for i, line in enumerate(paragraphs):
        if line:
            text(
                page, MARGIN, y + 18 + i * 14, line, 8.5, color=INK if i < 6 else MUTED
            )

    text(page, PAGE_W - MARGIN, 740, "Page 2 of 2", 8, color=MUTED, align="right")


def build_invoice():
    doc = fitz.open()
    page = doc.new_page(width=PAGE_W, height=PAGE_H)
    page_one(page)
    page = doc.new_page(width=PAGE_W, height=PAGE_H)
    page_two(page)
    doc.set_metadata(
        {"title": "Invoice KY-2041", "author": "Northwind Industrial Supply"}
    )
    target = OUT / "invoice_ky-2041.pdf"
    doc.save(target, garbage=4, deflate=True)
    doc.close()
    return target


def build_ledger():
    from openpyxl import Workbook
    from openpyxl.styles import Alignment, Font, PatternFill
    from openpyxl.utils import get_column_letter

    rows = [
        (
            "SUP-1001",
            "Apex Castings GmbH",
            "Germany",
            "Castings",
            812400,
            "Low",
            "2027-01-31",
        ),
        (
            "SUP-1002",
            "Bharat Precision Pvt Ltd",
            "India",
            "Machining",
            486300,
            "Medium",
            "2026-11-30",
        ),
        (
            "SUP-1003",
            "Cascade Polymers Inc.",
            "United States",
            "Plastics",
            274150,
            "Low",
            "2027-03-15",
        ),
        (
            "SUP-1004",
            "Delta Hydraulics S.A.",
            "Spain",
            "Hydraulics",
            631900,
            "Medium",
            "2026-12-31",
        ),
        (
            "SUP-1005",
            "Evergreen Fasteners Co.",
            "Taiwan",
            "Fasteners",
            158700,
            "Low",
            "2027-06-30",
        ),
        (
            "SUP-1006",
            "Fushun Steel Works",
            "China",
            "Raw steel",
            1240800,
            "High",
            "2026-10-31",
        ),
        (
            "SUP-1007",
            "Granite Tool & Die",
            "United States",
            "Tooling",
            392250,
            "Low",
            "2027-02-28",
        ),
        (
            "SUP-1008",
            "Helios Coatings AB",
            "Sweden",
            "Surface treatment",
            211600,
            "Low",
            "2027-05-31",
        ),
        (
            "SUP-1009",
            "Iberia Bearings SL",
            "Spain",
            "Bearings",
            344800,
            "Medium",
            "2027-01-15",
        ),
        (
            "SUP-1010",
            "Jinlong Electronics",
            "China",
            "Electronics",
            908450,
            "High",
            "2026-09-30",
        ),
        (
            "SUP-1011",
            "Kestrel Logistics BV",
            "Netherlands",
            "Logistics",
            176900,
            "Low",
            "2027-04-30",
        ),
        (
            "SUP-1012",
            "Larsen Composites A/S",
            "Denmark",
            "Composites",
            289300,
            "Medium",
            "2027-03-31",
        ),
        (
            "SUP-1013",
            "Meridian Rubber Ltd",
            "United Kingdom",
            "Seals",
            197450,
            "Low",
            "2027-07-31",
        ),
        (
            "SUP-1014",
            "Nordic Aluminium AS",
            "Norway",
            "Aluminium",
            705600,
            "Medium",
            "2027-02-28",
        ),
        (
            "SUP-1015",
            "Orion Sensors Inc.",
            "United States",
            "Sensors",
            421750,
            "Low",
            "2027-08-31",
        ),
        (
            "SUP-1016",
            "Pacific Wire & Cable",
            "United States",
            "Cable",
            233900,
            "Medium",
            "2026-12-15",
        ),
        (
            "SUP-1017",
            "Qingdao Plating Co.",
            "China",
            "Plating",
            168200,
            "High",
            "2026-10-15",
        ),
        (
            "SUP-1018",
            "Rheinmetall Fluidtechnik",
            "Germany",
            "Hydraulics",
            964300,
            "Low",
            "2027-05-15",
        ),
        (
            "SUP-1019",
            "Sakura Precision KK",
            "Japan",
            "Machining",
            578400,
            "Low",
            "2027-06-15",
        ),
        (
            "SUP-1020",
            "Tatra Forge a.s.",
            "Slovakia",
            "Forgings",
            312650,
            "Medium",
            "2027-01-31",
        ),
        (
            "SUP-1021",
            "UniBelt Transmissions",
            "Italy",
            "Belts",
            143800,
            "Low",
            "2027-04-15",
        ),
        (
            "SUP-1022",
            "Vulcan Heat Treating",
            "United States",
            "Heat treatment",
            268900,
            "Medium",
            "2027-02-15",
        ),
        (
            "SUP-1023",
            "Wuhan Precision Gears",
            "China",
            "Gears",
            652100,
            "High",
            "2026-11-15",
        ),
        (
            "SUP-1024",
            "Zenith Abrasives SA",
            "France",
            "Abrasives",
            118450,
            "High",
            "2026-12-01",
        ),
    ]

    wb = Workbook()
    ws = wb.active
    ws.title = "Suppliers"
    headers = [
        "Supplier ID",
        "Supplier",
        "Country",
        "Category",
        "Annual Spend (USD)",
        "Risk Rating",
        "Contract Renewal",
    ]
    ws.append(headers)
    header_fill = PatternFill("solid", fgColor="0F172A")
    for cell in ws[1]:
        cell.font = Font(color="FFFFFF", bold=True)
        cell.fill = header_fill
        cell.alignment = Alignment(horizontal="center")
    for row in rows:
        ws.append(list(row))

    widths = [13, 30, 16, 20, 20, 13, 18]
    for i, width in enumerate(widths, start=1):
        ws.column_dimensions[get_column_letter(i)].width = width
    for cell in ws["E"][1:]:
        cell.number_format = "#,##0"
    ws.freeze_panes = "A2"

    summary = wb.create_sheet("Summary")
    summary["A1"] = "Supplier ledger — annual spend summary"
    summary["A1"].font = Font(bold=True, size=13)
    summary.append([])
    summary.append(["As of", "2026-09-30"])
    summary.append(["Suppliers", len(rows)])
    summary.append(["Total annual spend (USD)", sum(r[4] for r in rows)])
    summary.append(["High-risk suppliers", sum(1 for r in rows if r[5] == "High")])
    summary["B5"].number_format = "#,##0"
    summary.column_dimensions["A"].width = 28
    summary.column_dimensions["B"].width = 16

    target = OUT / "supplier_ledger.xlsx"
    wb.save(target)
    return target


if __name__ == "__main__":
    print(build_invoice())
    print(build_ledger())