v1.2 scaffold

This commit is contained in:
2026-04-25 20:43:37 +12:00
parent 658cda8c35
commit bc211ffcc8
58 changed files with 5104 additions and 0 deletions
@@ -0,0 +1,633 @@
<script lang="ts">
import { invalidateAll } from '$app/navigation';
import { api } from '$lib/api';
import { operatorSession } from '$lib/session';
import type { Mix, Product, ProductCostBreakdown } from '$lib/types';
let { data } = $props();
let isCreating = $state(false);
let pendingMaterialId = $state<number | null>(null);
let successMessage = $state('');
let errorMessage = $state('');
const today = new Date().toISOString().slice(0, 10);
function currency(value: number | null | undefined, digits = 2) {
if (value === null || value === undefined) {
return 'N/A';
}
return `$${value.toFixed(digits)}`;
}
function getImpactedMixes(materialId: number): Mix[] {
return data.mixes.filter((mix: Mix) => mix.ingredients.some((ingredient) => ingredient.raw_material_id === materialId));
}
function getImpactedProducts(materialId: number): Array<Product & { deliveredCost: ProductCostBreakdown | undefined }> {
const mixIds = new Set(getImpactedMixes(materialId).map((mix) => mix.id));
return data.products
.filter((product: Product) => mixIds.has(product.mix_id ?? -1))
.map((product: Product) => ({
...product,
deliveredCost: data.productCosts.find((row: ProductCostBreakdown) => row.product_id === product.id)
}));
}
async function handleCreateMaterial(event: SubmitEvent) {
event.preventDefault();
successMessage = '';
errorMessage = '';
isCreating = true;
const form = event.currentTarget as HTMLFormElement;
const formData = new FormData(form);
try {
await api.createRawMaterial({
name: String(formData.get('name') ?? '').trim(),
supplier: String(formData.get('supplier') ?? '').trim() || null,
unit_of_measure: String(formData.get('unit_of_measure') ?? '').trim(),
kg_per_unit: Number(formData.get('kg_per_unit')),
status: String(formData.get('status') ?? 'active'),
notes: String(formData.get('notes') ?? '').trim() || null,
initial_price: {
market_value: Number(formData.get('market_value')),
waste_percentage: Number(formData.get('waste_percentage')),
effective_date: String(formData.get('effective_date') ?? today),
status: 'active',
notes: String(formData.get('price_notes') ?? '').trim() || null
}
});
form.reset();
const effectiveDate = form.elements.namedItem('effective_date');
if (effectiveDate instanceof HTMLInputElement) {
effectiveDate.value = today;
}
successMessage = 'Raw material created and added to the costing model.';
await invalidateAll();
} catch (error) {
errorMessage = error instanceof Error ? error.message : 'Unable to create raw material';
} finally {
isCreating = false;
}
}
async function handleAddPrice(event: SubmitEvent, rawMaterialId: number) {
event.preventDefault();
successMessage = '';
errorMessage = '';
pendingMaterialId = rawMaterialId;
const form = event.currentTarget as HTMLFormElement;
const formData = new FormData(form);
try {
await api.addRawMaterialPrice(rawMaterialId, {
market_value: Number(formData.get('market_value')),
waste_percentage: Number(formData.get('waste_percentage')),
effective_date: String(formData.get('effective_date') ?? today),
status: 'active',
notes: String(formData.get('notes') ?? '').trim() || null
});
form.reset();
const effectiveDate = form.elements.namedItem('effective_date');
if (effectiveDate instanceof HTMLInputElement) {
effectiveDate.value = today;
}
successMessage = 'Price version saved. Mix and product costs have been refreshed.';
await invalidateAll();
} catch (error) {
errorMessage = error instanceof Error ? error.message : 'Unable to add price version';
} finally {
pendingMaterialId = null;
}
}
</script>
{#if !$operatorSession}
<section class="locked-panel">
<p class="eyebrow">Operator access required</p>
<h1>Sign in from the homepage before managing raw materials.</h1>
<p>This page is the input maintenance area for Mix Master and downstream product pricing.</p>
<a href="/">Return to login</a>
</section>
{:else}
<section class="page-header">
<div>
<p class="eyebrow">Raw material manager</p>
<h1>Maintain input costs and watch the costing model update downstream.</h1>
<p>
Every active price version feeds the existing mix calculation engine. Save a new price, then review the
impacted mixes and finished product outputs below.
</p>
</div>
<div class="header-status">
<span>{$operatorSession.email}</span>
<strong>{data.rawMaterials.length} materials under control</strong>
</div>
</section>
{#if successMessage}
<p class="feedback success">{successMessage}</p>
{/if}
{#if errorMessage}
<p class="feedback error">{errorMessage}</p>
{/if}
<section class="top-grid">
<article class="surface-card">
<div class="panel-heading">
<div>
<p class="eyebrow">Add raw material</p>
<h2>Create a new tracked input</h2>
</div>
</div>
<form class="material-form" onsubmit={handleCreateMaterial}>
<div class="form-grid">
<label>
Name
<input name="name" required />
</label>
<label>
Supplier
<input name="supplier" />
</label>
<label>
Unit of measure
<input name="unit_of_measure" value="tonne" required />
</label>
<label>
Kg per unit
<input name="kg_per_unit" type="number" min="0.0001" step="0.0001" value="1000" required />
</label>
<label>
Market value
<input name="market_value" type="number" min="0.0001" step="0.0001" required />
</label>
<label>
Waste percentage
<input name="waste_percentage" type="number" min="0" max="1" step="0.0001" value="0" required />
</label>
<label>
Effective date
<input name="effective_date" type="date" value={today} required />
</label>
<label>
Status
<select name="status">
<option value="active">Active</option>
<option value="draft">Draft</option>
<option value="inactive">Inactive</option>
</select>
</label>
</div>
<label>
Material notes
<textarea name="notes" rows="3"></textarea>
</label>
<label>
Price notes
<textarea name="price_notes" rows="2"></textarea>
</label>
<button type="submit" disabled={isCreating}>
{isCreating ? 'Creating material...' : 'Create raw material'}
</button>
</form>
</article>
<article class="surface-card">
<div class="panel-heading">
<div>
<p class="eyebrow">Downstream view</p>
<h2>Current mix and product snapshot</h2>
</div>
</div>
<div class="snapshot-list">
<article>
<h3>Mix Master</h3>
<ul>
{#each data.mixes as mix}
<li>
<strong>{mix.name}</strong>
<span>{currency(mix.mix_cost_per_kg, 4)} / kg</span>
</li>
{/each}
</ul>
</article>
<article>
<h3>Finished products</h3>
<ul>
{#each data.productCosts as row}
<li>
<strong>{row.product_name}</strong>
<span>{currency(row.finished_product_delivered)}</span>
</li>
{/each}
</ul>
</article>
</div>
</article>
</section>
<section class="material-list">
{#each data.rawMaterials as material}
{@const impactedMixes = getImpactedMixes(material.id)}
{@const impactedProducts = getImpactedProducts(material.id)}
<article class="surface-card material-card">
<div class="panel-heading">
<div>
<p class="eyebrow">Tracked input</p>
<h2>{material.name}</h2>
<p class="subtle">
{material.supplier || 'Supplier not set'} · {material.unit_of_measure} · {material.kg_per_unit} kg per
unit
</p>
</div>
<span class:inactive={material.status !== 'active'} class="status-pill">{material.status}</span>
</div>
<div class="material-grid">
<section class="detail-panel">
<div class="detail-row">
<span>Current market value</span>
<strong>{currency(material.current_price?.market_value)}</strong>
</div>
<div class="detail-row">
<span>Current waste</span>
<strong>
{material.current_price ? `${(material.current_price.waste_percentage * 100).toFixed(1)}%` : 'N/A'}
</strong>
</div>
<div class="detail-row">
<span>Cost per kg</span>
<strong>{currency(material.current_price?.cost_per_kg, 4)}</strong>
</div>
<div class="detail-row">
<span>Effective date</span>
<strong>{material.current_price?.effective_date ?? 'N/A'}</strong>
</div>
</section>
<form class="price-form" onsubmit={(event) => handleAddPrice(event, material.id)}>
<h3>Record a new price version</h3>
<div class="form-grid compact">
<label>
Market value
<input name="market_value" type="number" min="0.0001" step="0.0001" required />
</label>
<label>
Waste percentage
<input name="waste_percentage" type="number" min="0" max="1" step="0.0001" value="0" required />
</label>
<label>
Effective date
<input name="effective_date" type="date" value={today} required />
</label>
</div>
<label>
Notes
<textarea name="notes" rows="2"></textarea>
</label>
<button type="submit" disabled={pendingMaterialId === material.id}>
{pendingMaterialId === material.id ? 'Saving price...' : 'Save price version'}
</button>
</form>
</div>
<div class="impact-grid">
<section class="impact-panel">
<h3>Impacted mixes</h3>
{#if impactedMixes.length}
<ul>
{#each impactedMixes as mix}
<li>
<strong>{mix.name}</strong>
<span>{currency(mix.mix_cost_per_kg, 4)} / kg</span>
</li>
{/each}
</ul>
{:else}
<p class="empty">No mix currently references this material.</p>
{/if}
</section>
<section class="impact-panel">
<h3>Impacted products</h3>
{#if impactedProducts.length}
<ul>
{#each impactedProducts as product}
<li>
<strong>{product.name}</strong>
<span>{currency(product.deliveredCost?.finished_product_delivered)}</span>
</li>
{/each}
</ul>
{:else}
<p class="empty">No product currently depends on this material.</p>
{/if}
</section>
</div>
</article>
{/each}
</section>
{/if}
<style>
h1,
h2,
h3,
p {
margin: 0;
}
a {
color: var(--brand);
text-decoration: none;
}
.eyebrow {
color: var(--muted);
font-size: 0.78rem;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.locked-panel,
.page-header,
.surface-card,
.feedback {
background: rgba(255, 250, 241, 0.82);
border: 1px solid var(--line);
border-radius: 1.5rem;
box-shadow: var(--shadow);
}
.locked-panel,
.page-header,
.surface-card {
padding: 1.5rem;
}
.locked-panel {
display: grid;
gap: 0.75rem;
max-width: 42rem;
}
.page-header {
display: flex;
align-items: end;
justify-content: space-between;
gap: 1rem;
margin-bottom: 1rem;
}
.page-header p:last-child,
.subtle,
.empty,
.detail-row span,
.snapshot-list li span {
color: var(--muted);
}
.page-header h1 {
margin: 0.35rem 0 0.55rem;
font-size: clamp(1.8rem, 4vw, 3rem);
max-width: 16ch;
line-height: 1;
}
.header-status {
text-align: right;
}
.header-status span {
display: block;
color: var(--muted);
margin-bottom: 0.35rem;
}
.feedback {
padding: 0.95rem 1.1rem;
margin: 0 0 1rem;
font-weight: 600;
}
.feedback.success {
border-color: rgba(44, 106, 66, 0.2);
color: #245838;
}
.feedback.error {
border-color: rgba(163, 48, 29, 0.22);
color: #8d2b1f;
}
.top-grid,
.material-grid,
.impact-grid {
display: grid;
gap: 1rem;
}
.top-grid {
grid-template-columns: 1.2fr 0.8fr;
margin-bottom: 1rem;
}
.panel-heading {
display: flex;
align-items: start;
justify-content: space-between;
gap: 1rem;
margin-bottom: 1rem;
}
.material-form,
.price-form {
display: grid;
gap: 1rem;
}
.form-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 0.9rem;
}
.form-grid.compact {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
label {
display: grid;
gap: 0.35rem;
font-weight: 600;
color: var(--muted);
}
input,
textarea,
select,
button {
font: inherit;
}
input,
textarea,
select {
width: 100%;
padding: 0.85rem 0.95rem;
border-radius: 1rem;
border: 1px solid rgba(90, 45, 24, 0.16);
background: rgba(255, 255, 255, 0.85);
}
button {
padding: 0.95rem 1.1rem;
border: none;
border-radius: 1rem;
background: linear-gradient(135deg, var(--brand-deep), var(--brand));
color: #fff7ef;
font-weight: 700;
cursor: pointer;
justify-self: start;
}
button:disabled {
opacity: 0.7;
cursor: wait;
}
.snapshot-list {
display: grid;
gap: 1rem;
}
.snapshot-list article {
padding: 1rem;
border-radius: 1rem;
background: rgba(143, 79, 31, 0.06);
}
.snapshot-list ul,
.impact-panel ul {
list-style: none;
padding: 0;
margin: 0.9rem 0 0;
display: grid;
gap: 0.8rem;
}
.snapshot-list li,
.impact-panel li {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
}
.material-list {
display: grid;
gap: 1rem;
}
.material-card {
display: grid;
gap: 1rem;
}
.status-pill {
padding: 0.45rem 0.8rem;
border-radius: 999px;
background: rgba(44, 106, 66, 0.12);
color: #245838;
font-weight: 700;
text-transform: capitalize;
}
.status-pill.inactive {
background: rgba(143, 79, 31, 0.1);
color: var(--brand-deep);
}
.material-grid {
grid-template-columns: 0.75fr 1.25fr;
}
.detail-panel,
.impact-panel {
padding: 1rem;
border-radius: 1rem;
background: rgba(143, 79, 31, 0.06);
}
.detail-panel {
display: grid;
gap: 0.85rem;
}
.detail-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
}
.impact-grid {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (max-width: 1100px) {
.top-grid,
.material-grid,
.impact-grid {
grid-template-columns: 1fr;
}
}
@media (max-width: 760px) {
.page-header,
.panel-heading,
.snapshot-list li,
.impact-panel li,
.detail-row {
flex-direction: column;
align-items: start;
}
.form-grid,
.form-grid.compact {
grid-template-columns: 1fr;
}
.header-status {
text-align: left;
}
}
</style>