reportist/plugins/consumables/renderer/render.typ

611 lines
13 KiB
Plaintext

// poc2.typ
//
// Usage:
// typst compile poc2.typ poc2.pdf
//
// Expects:
// export-consumables-2026-09-15.csv
#let csv-path = sys.inputs.at("consumables", default: "")
#let theme-name = sys.inputs.at("theme", default: "default")
#let themes = yaml("config/themes.yaml")
#assert(theme-name in themes, message: "Unknown theme: " + theme-name)
#let theme = themes.at(theme-name)
// =============================================================================
// Theme
// =============================================================================
#let ink = rgb(theme.ink)
#let muted = rgb(theme.muted)
#let faint = rgb("#94a0ad")
#let accent = rgb(theme.accent)
#let accent-soft = rgb(theme.paper_soft)
#let accent-dark = rgb(theme.accent)
#let surface = rgb("#ffffff")
#let surface-alt = rgb(theme.paper_soft)
#let surface-strong = rgb(theme.paper_soft)
#let border = rgb(theme.line)
#let good = rgb(theme.success)
#let good-soft = rgb("#e9f3ed")
#let warning = rgb(theme.warning)
#let warning-soft = rgb("#f7f0e3")
#let danger = rgb(theme.critical)
#let danger-soft = rgb("#f8eaea")
// Category colors are independent of the stock-status colors.
#let category-style(category) = {
let key = lower(category.trim())
if key in ("pile", "piles") {
(color: rgb("#71501c"), soft: rgb("#f6e9c9"))
} else if key in ("ruban", "rubans", "rubalise") {
(color: rgb("#216b64"), soft: rgb("#def1eb"))
} else if key == "tape" {
(color: rgb("#68458c"), soft: rgb("#eee4f6"))
} else {
(color: accent-dark, soft: accent-soft)
}
}
// =============================================================================
// Components
// =============================================================================
// Generic compact corporate pill.
#let pill(
body,
fill: accent-soft,
color: accent-dark,
border-color: none,
size: 6.8pt,
weight: "semibold",
) = {
box(
fill: fill,
stroke: if border-color == none { none } else { 0.5pt + border-color },
radius: 10pt,
inset: (x: 6pt, y: 2.2pt),
text(
size: size,
weight: weight,
fill: color,
body,
),
)
}
// Convert CSV string values safely to integers.
#let as-int(value) = {
let value = str(value).trim()
if value == "" {
0
} else {
int(value)
}
}
// Stock status.
//
// We treat:
// stock <= minimum -> critical
// stock <= minimum * 1.5 -> warning
// otherwise -> healthy
#let stock-status(stock, minimum) = {
let stock = as-int(stock)
let minimum = as-int(minimum)
if stock <= minimum {
(
label: "Critique",
color: danger,
soft: danger-soft,
)
} else if stock <= minimum * 1.5 {
(
label: "Bas",
color: warning,
soft: warning-soft,
)
} else {
(
label: "OK",
color: good,
soft: good-soft,
)
}
}
// Stock bar.
//
// 100% means we have at least twice the minimum stock.
// This makes the minimum threshold land around 50%,
// which is much more informative than simply doing stock/minimum.
#let stock-progress(stock, minimum) = {
let stock = as-int(stock)
let minimum = as-int(minimum)
let ratio = if minimum <= 0 {
1
} else {
calc.min(stock / (minimum * 2), 1)
}
let status = stock-status(stock, minimum)
block(
width: 100%,
height: 2.5pt,
fill: surface-strong,
radius: 2pt,
)[
#block(
width: 100% * ratio,
height: 100%,
fill: status.color,
radius: 2pt,
)
]
}
#let stock-cell(stock, minimum) = {
let status = stock-status(stock, minimum)
block(width: 100%, breakable: false)[
#set par(spacing: 0pt)
#grid(
columns: (1fr, auto),
align: (left + horizon, right + horizon),
text(
size: 7.5pt,
weight: "regular",
fill: ink,
)[
#stock
],
pill(
status.label,
fill: status.soft,
color: status.color,
size: 6.4pt,
weight: "medium",
),
)
#v(1.5pt)
#stock-progress(stock, minimum)
]
}
// =============================================================================
// Load Snipe-IT CSV
// =============================================================================
#let raw = csv(
csv-path,
delimiter: ",",
row-type: dictionary,
)
#let required-columns = ("Name", "Category", "Supplier", "Model No.", "Remaining", "Min. QTY", "Location")
#assert(raw.len() > 0, message: "CSV has no data rows")
#for column in required-columns {
assert(column in raw.first(), message: "Missing CSV column: " + column)
}
// Snipe-IT adds a final totals row.
// Ignore rows without a name.
#let consumables = raw.filter(
row => row.at("Name", default: "").trim() != ""
)
#assert(consumables.len() > 0, message: "CSV has no named consumables")
#for row in consumables {
for column in ("Remaining", "Min. QTY") {
let value = row.at(column).trim()
assert(value != "", message: "Missing " + column + " for " + row.at("Name"))
let parsed = int(value)
assert(parsed >= 0, message: "Negative " + column + " for " + row.at("Name"))
}
}
// =============================================================================
// Mapping
// =============================================================================
#let location-name(location) = {
if location == "Bureaux" {
"BUREAU REGIE"
} else {
location.upper()
}
}
#let normalize(row) = (
lieu: location-name(row.at("Location", default: "")),
categorie: row.at("Category", default: ""),
nom: row.at("Name", default: ""),
fournisseur: row.at("Supplier", default: ""),
reference: row.at("Model No.", default: ""),
stock: row.at("Remaining", default: ""),
minimum: row.at("Min. QTY", default: ""),
)
#let data = consumables.map(normalize)
// =============================================================================
// Statistics
// =============================================================================
#let total-references = data.len()
#let total-stock = data.fold(
0,
(sum, row) => sum + as-int(row.stock),
)
#let critical-count = data.filter(
row => as-int(row.stock) <= as-int(row.minimum)
).len()
#let low-count = data.filter(row => {
let stock = as-int(row.stock)
let minimum = as-int(row.minimum)
stock > minimum and stock <= minimum * 1.5
}).len()
// =============================================================================
// Table cells
// =============================================================================
// Merge each consecutive run of rows sharing a location.
#let cells = {
let result = ()
for (index, row) in data.enumerate() {
let category = category-style(row.categorie)
if index > 0 and data.at(index - 1).categorie != row.categorie {
result.push(table.hline(
y: index + 1,
start: 1,
stroke: 1pt + accent,
))
}
if index == 0 or data.at(index - 1).lieu != row.lieu {
let count = 1
for next in data.slice(index + 1) {
if next.lieu != row.lieu {
break
}
count += 1
}
result.push(
table.cell(
rowspan: count,
align: center + horizon,
fill: accent-dark,
inset: 4pt,
rotate(
-90deg,
reflow: true,
text(
size: 7pt,
weight: "bold",
fill: white,
tracking: 0.06em,
row.lieu,
),
),
)
)
}
result += (
[
#pill(
row.categorie,
fill: category.soft,
color: category.color,
size: 6.4pt,
weight: "medium",
)
],
[
#text(size: 7.8pt, weight: "regular", fill: ink)[
#row.nom
]
],
[
#if row.fournisseur != "" {
pill(
row.fournisseur,
fill: surface-strong,
color: muted,
size: 6.4pt,
weight: "medium",
)
} else {
text(fill: faint)[—]
}
],
[
#if row.reference != "" {
text(
font: "DejaVu Sans Mono",
size: 6.8pt,
fill: muted,
row.reference,
)
} else {
text(fill: faint)[—]
}
],
[
#stock-cell(row.stock, row.minimum)
],
[
#align(center + horizon)[
#text(
size: 7.5pt,
weight: "regular",
fill: muted,
)[
#row.minimum
]
]
],
)
}
result
}
// =============================================================================
// Document
// =============================================================================
#set page(
paper: "a4",
flipped: true,
margin: (
x: 12mm,
top: 11mm,
bottom: 10mm,
),
footer: context {
grid(
columns: (1fr, auto),
align: (left, right),
text(
size: 6.5pt,
fill: faint,
)[
BEFFROI · INVENTAIRE CONSOMMABLES
],
text(
size: 6.5pt,
fill: faint,
)[
#counter(page).display("1 / 1", both: true)
],
)
},
)
#set text(
font: theme.font,
size: 8.5pt,
fill: ink,
lang: "fr",
)
#set par(leading: 0.55em, spacing: 2pt)
// =============================================================================
// Header
// =============================================================================
#grid(
columns: (1fr, auto),
align: (left + horizon, right + horizon),
[
#text(
size: 7pt,
weight: "bold",
tracking: 0.12em,
fill: accent,
)[
BEFFROI / INVENTAIRE
]
#v(2pt)
#text(
size: 18pt,
weight: "bold",
fill: ink,
)[
Liste des consommables
]
#v(2pt)
#text(size: 8pt, fill: muted)[
État consolidé du stock depuis Snipe-IT
]
],
[
#pill(
"SNIPE-IT",
fill: accent-soft,
color: accent-dark,
)
],
)
#v(8pt)
// =============================================================================
// KPI strip
// =============================================================================
#let metric(label, value, note: none, color: accent) = {
block(
width: 100%,
fill: surface-alt,
stroke: 0.5pt + border,
radius: 4pt,
inset: 7pt,
)[
#text(
size: 6.5pt,
weight: "bold",
tracking: 0.08em,
fill: muted,
)[
#label
]
#v(2pt)
#text(
size: 15pt,
weight: "bold",
fill: color,
)[
#value
]
#if note != none {
v(1pt)
text(size: 6.5pt, fill: faint)[#note]
}
]
}
#grid(
columns: (1fr,) * 4,
gutter: 6pt,
metric(
"RÉFÉRENCES",
str(total-references),
note: "consommables suivis",
),
metric(
"UNITÉS EN STOCK",
str(total-stock),
note: "stock disponible",
),
metric(
"STOCK BAS",
str(low-count),
note: "proche du minimum",
color: warning,
),
metric(
"CRITIQUES",
str(critical-count),
note: "au seuil ou en dessous",
color: danger,
),
)
#v(8pt)
// =============================================================================
// Table
// =============================================================================
#table(
columns: (
0.35fr, // Lieu
1.15fr, // Catégorie
2.45fr, // Nom
1.25fr, // Fournisseur
1.65fr, // Référence
1.55fr, // Stock
0.55fr, // Mini
),
inset: (x: 5pt, y: 2.5pt),
stroke: (x, y) => (
if y == 0 {
0pt
} else {
0.45pt + border
}
),
align: (
center,
left + horizon,
left + horizon,
left + horizon,
left + horizon,
left + horizon,
center + horizon,
),
fill: (x, y) => {
if y == 0 {
ink
} else if calc.even(y) {
surface-alt
} else {
surface
}
},
table.header(
repeat: true,
table.cell(
fill: ink,
align: center + horizon,
)[
#text(size: 6.5pt, weight: "bold", fill: white)[LIEU]
],
[#text(size: 6.5pt, weight: "bold", fill: white)[CATÉGORIE]],
[#text(size: 6.5pt, weight: "bold", fill: white)[CONSOMMABLE]],
[#text(size: 6.5pt, weight: "bold", fill: white)[FOURNISSEUR]],
[#text(size: 6.5pt, weight: "bold", fill: white)[RÉFÉRENCE]],
[#text(size: 6.5pt, weight: "bold", fill: white)[STOCK]],
[#text(size: 6.5pt, weight: "bold", fill: white)[MIN.]],
),
..cells,
// Match the outer frame to the category separators.
table.hline(y: 0, stroke: 1pt + accent),
table.hline(y: data.len() + 1, stroke: 1pt + accent),
table.vline(x: 0, stroke: 1pt + accent),
table.vline(x: 7, stroke: 1pt + accent),
)