v1.3 - client and admin scaffolding

This commit is contained in:
2026-04-25 22:51:36 +12:00
parent bc211ffcc8
commit 8cf9bfb441
54 changed files with 8882 additions and 1248 deletions
+435 -254
View File
@@ -1,8 +1,8 @@
<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';
import { clientSession } from '$lib/session';
import type { Mix, Product, ProductCostBreakdown, RawMaterial } from '$lib/types';
let { data } = $props();
@@ -21,6 +21,18 @@
return `$${value.toFixed(digits)}`;
}
function formatDate(value: string | null | undefined) {
if (!value) {
return 'No date';
}
return new Intl.DateTimeFormat('en-NZ', {
day: 'numeric',
month: 'short',
year: 'numeric'
}).format(new Date(value));
}
function getImpactedMixes(materialId: number): Mix[] {
return data.mixes.filter((mix: Mix) => mix.ingredients.some((ingredient) => ingredient.raw_material_id === materialId));
}
@@ -107,28 +119,49 @@
pendingMaterialId = null;
}
}
const totalSpend = $derived(
data.rawMaterials.reduce(
(sum: number, material: RawMaterial) => sum + (material.current_price?.market_value ?? 0),
0
)
);
const averageWaste = $derived(
data.rawMaterials.length
? data.rawMaterials.reduce(
(sum: number, material: RawMaterial) => sum + (material.current_price?.waste_percentage ?? 0),
0
) / data.rawMaterials.length
: 0
);
const latestEffectiveDate = $derived(
[...data.rawMaterials]
.map((material: RawMaterial) => material.current_price?.effective_date)
.filter(Boolean)
.sort()
.at(-1) ?? null
);
const activeMaterials = $derived(data.rawMaterials.filter((material: RawMaterial) => material.status === 'active'));
</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>
{#if !$clientSession}
<section class="locked-card">
<p class="eyebrow">Client Access Required</p>
<h2>Sign in on the Hunter Premium Produce home page before viewing raw material pricing.</h2>
<p>This workflow updates source inputs and pushes new values through mix and product calculations.</p>
<a href="/">Return to sign-in</a>
</section>
{:else}
<section class="page-header">
<section class="page-intro">
<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>
<p class="eyebrow">Input Cost Control</p>
<h2>Maintain raw materials with a cleaner operational workflow.</h2>
<p>Update source pricing, track downstream exposure, and keep the costing engine current from one workspace.</p>
</div>
<div class="header-status">
<span>{$operatorSession.email}</span>
<strong>{data.rawMaterials.length} materials under control</strong>
<div class="intro-chip">
<span>{$clientSession.email}</span>
<strong>{activeMaterials.length} active materials</strong>
</div>
</section>
@@ -140,13 +173,34 @@
<p class="feedback error">{errorMessage}</p>
{/if}
<section class="metric-row">
<article class="metric-card">
<span>Total Spend Tracked</span>
<strong>{currency(totalSpend)}</strong>
<p>Across current market values</p>
</article>
<article class="metric-card">
<span>Average Waste</span>
<strong>{(averageWaste * 100).toFixed(1)}%</strong>
<p>Current blended input loss</p>
</article>
<article class="metric-card">
<span>Latest Price Update</span>
<strong>{formatDate(latestEffectiveDate)}</strong>
<p>Most recent effective date on file</p>
</article>
</section>
<section class="top-grid">
<article class="surface-card">
<div class="panel-heading">
<article class="surface-card form-card">
<div class="section-heading">
<div>
<p class="eyebrow">Add raw material</p>
<h2>Create a new tracked input</h2>
<p class="eyebrow">Create Input</p>
<h3>Add a new raw material</h3>
</div>
<span class="soft-pill">Live costing source</span>
</div>
<form class="material-form" onsubmit={handleCreateMaterial}>
@@ -196,100 +250,116 @@
</label>
</div>
<label>
Material notes
<textarea name="notes" rows="3"></textarea>
</label>
<div class="form-grid single">
<label>
Material notes
<textarea name="notes" rows="3"></textarea>
</label>
<label>
Price notes
<textarea name="price_notes" rows="2"></textarea>
</label>
<label>
Price notes
<textarea name="price_notes" rows="3"></textarea>
</label>
</div>
<button type="submit" disabled={isCreating}>
<button class="primary-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 class="summary-stack">
<article class="surface-card">
<div class="section-heading">
<div>
<p class="eyebrow">Downstream Snapshot</p>
<h3>Mixes affected by current inputs</h3>
</div>
</div>
</div>
<div class="snapshot-list">
<article>
<h3>Mix Master</h3>
<ul>
{#each data.mixes as mix}
<li>
<div class="mini-list">
{#each data.mixes as mix}
<article>
<div>
<strong>{mix.name}</strong>
<span>{currency(mix.mix_cost_per_kg, 4)} / kg</span>
</li>
{/each}
</ul>
</article>
<span>{mix.client_name}</span>
</div>
<strong>{currency(mix.mix_cost_per_kg, 4)} / kg</strong>
</article>
{/each}
</div>
</article>
<article>
<h3>Finished products</h3>
<ul>
{#each data.productCosts as row}
<li>
<article class="surface-card">
<div class="section-heading">
<div>
<p class="eyebrow">Product Exposure</p>
<h3>Finished outputs linked to live pricing</h3>
</div>
</div>
<div class="mini-list">
{#each data.productCosts as row}
<article>
<div>
<strong>{row.product_name}</strong>
<span>{currency(row.finished_product_delivered)}</span>
</li>
{/each}
</ul>
</article>
</div>
</article>
<span>{row.warnings.length ? 'Check warnings' : 'Stable pricing'}</span>
</div>
<strong>{currency(row.finished_product_delivered)}</strong>
</article>
{/each}
</div>
</article>
</div>
</section>
<section class="material-list">
<section class="materials-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 class="material-header">
<div class="material-title">
<span class={`material-icon ${material.status === 'active' ? 'active' : 'muted'}`}>RM</span>
<div>
<h3>{material.name}</h3>
<p>{material.supplier || 'Supplier not set'} · {material.unit_of_measure} · {material.kg_per_unit} kg per unit</p>
</div>
</div>
<span class:inactive={material.status !== 'active'} class="status-pill">{material.status}</span>
<span class={`status-pill ${material.status === 'active' ? 'positive' : 'neutral'}`}>{material.status}</span>
</div>
<div class="material-grid">
<section class="detail-panel">
<div class="detail-row">
<span>Current market value</span>
<section class="stats-grid">
<article>
<span>Market value</span>
<strong>{currency(material.current_price?.market_value)}</strong>
</div>
<div class="detail-row">
<span>Current waste</span>
</article>
<article>
<span>Waste</span>
<strong>
{material.current_price ? `${(material.current_price.waste_percentage * 100).toFixed(1)}%` : 'N/A'}
</strong>
</div>
<div class="detail-row">
</article>
<article>
<span>Cost per kg</span>
<strong>{currency(material.current_price?.cost_per_kg, 4)}</strong>
</div>
<div class="detail-row">
</article>
<article>
<span>Effective date</span>
<strong>{material.current_price?.effective_date ?? 'N/A'}</strong>
</div>
<strong>{formatDate(material.current_price?.effective_date)}</strong>
</article>
</section>
<form class="price-form" onsubmit={(event) => handleAddPrice(event, material.id)}>
<h3>Record a new price version</h3>
<form class="price-card" onsubmit={(event) => handleAddPrice(event, material.id)}>
<div class="section-heading">
<div>
<p class="eyebrow">New Version</p>
<h4>Record a fresh price</h4>
</div>
</div>
<div class="form-grid compact">
<label>
@@ -313,42 +383,56 @@
<textarea name="notes" rows="2"></textarea>
</label>
<button type="submit" disabled={pendingMaterialId === material.id}>
<button class="primary-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>
<section class="impact-card">
<div class="impact-heading">
<h4>Impacted mixes</h4>
<span>{impactedMixes.length}</span>
</div>
{#if impactedMixes.length}
<ul>
<div class="impact-list">
{#each impactedMixes as mix}
<li>
<strong>{mix.name}</strong>
<span>{currency(mix.mix_cost_per_kg, 4)} / kg</span>
</li>
<article>
<div>
<strong>{mix.name}</strong>
<span>{mix.client_name}</span>
</div>
<strong>{currency(mix.mix_cost_per_kg, 4)} / kg</strong>
</article>
{/each}
</ul>
</div>
{:else}
<p class="empty">No mix currently references this material.</p>
<p class="empty">No active mix currently references this material.</p>
{/if}
</section>
<section class="impact-panel">
<h3>Impacted products</h3>
<section class="impact-card">
<div class="impact-heading">
<h4>Impacted products</h4>
<span>{impactedProducts.length}</span>
</div>
{#if impactedProducts.length}
<ul>
<div class="impact-list">
{#each impactedProducts as product}
<li>
<strong>{product.name}</strong>
<span>{currency(product.deliveredCost?.finished_product_delivered)}</span>
</li>
<article>
<div>
<strong>{product.name}</strong>
<span>{product.mix_name}</span>
</div>
<strong>{currency(product.deliveredCost?.finished_product_delivered)}</strong>
</article>
{/each}
</ul>
</div>
{:else}
<p class="empty">No product currently depends on this material.</p>
<p class="empty">No finished product currently depends on this material.</p>
{/if}
</section>
</div>
@@ -358,96 +442,115 @@
{/if}
<style>
h1,
h2,
h3,
h4,
p {
margin: 0;
}
a {
color: var(--brand);
text-decoration: none;
}
.eyebrow {
color: var(--muted);
color: #7f8e85;
font-size: 0.78rem;
letter-spacing: 0.1em;
font-weight: 600;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.locked-panel,
.page-header,
.surface-card,
.feedback {
background: rgba(255, 250, 241, 0.82);
.locked-card,
.page-intro,
.feedback,
.metric-card,
.surface-card {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 1.5rem;
border-radius: 1.35rem;
box-shadow: var(--shadow);
}
.locked-panel,
.page-header,
.surface-card {
padding: 1.5rem;
.locked-card,
.page-intro,
.feedback,
.metric-row,
.top-grid,
.materials-list {
margin-bottom: 1.25rem;
}
.locked-panel {
.locked-card,
.page-intro,
.surface-card {
padding: 1.2rem;
}
.locked-card {
display: grid;
gap: 0.75rem;
gap: 0.7rem;
max-width: 42rem;
}
.page-header {
.locked-card h2,
.page-intro h2 {
margin: 0.35rem 0 0.45rem;
font-size: clamp(1.7rem, 3vw, 2.25rem);
font-weight: 700;
}
.locked-card p:last-of-type,
.page-intro p:last-child,
.metric-card p,
.intro-chip span,
.mini-list span,
.material-title p,
.stats-grid span,
.impact-list span,
.empty {
color: var(--muted);
}
.locked-card a {
color: var(--green-deep);
font-weight: 600;
}
.page-intro {
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);
.intro-chip {
display: grid;
gap: 0.25rem;
padding: 0.95rem 1rem;
border: 1px solid var(--line);
border-radius: 1rem;
background: var(--panel-soft);
}
.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;
.intro-chip strong {
font-size: 1rem;
}
.feedback {
padding: 0.95rem 1.1rem;
margin: 0 0 1rem;
padding: 0.95rem 1rem;
font-weight: 600;
}
.feedback.success {
border-color: rgba(44, 106, 66, 0.2);
color: #245838;
color: var(--green-deep);
border-color: #d8ecdf;
background: #f6fcf8;
}
.feedback.error {
border-color: rgba(163, 48, 29, 0.22);
color: #8d2b1f;
color: #a03737;
border-color: #f0d9d9;
background: #fff8f8;
}
.metric-row,
.top-grid,
.material-grid,
.impact-grid {
@@ -455,21 +558,69 @@
gap: 1rem;
}
.metric-row {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
.metric-card {
padding: 1.15rem 1.2rem;
}
.metric-card span {
display: block;
color: var(--muted);
font-size: 0.9rem;
}
.metric-card strong {
display: block;
margin: 0.55rem 0 0.3rem;
font-size: 1.9rem;
font-weight: 700;
}
.top-grid {
grid-template-columns: 1.2fr 0.8fr;
grid-template-columns: minmax(0, 1.2fr) minmax(320px, 0.85fr);
}
.summary-stack {
display: grid;
gap: 1rem;
}
.section-heading,
.material-header,
.impact-heading,
.mini-list article,
.impact-list article {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 0.75rem;
}
.section-heading {
margin-bottom: 1rem;
}
.panel-heading {
display: flex;
align-items: start;
justify-content: space-between;
gap: 1rem;
margin-bottom: 1rem;
.section-heading h3,
.material-header h3,
.impact-heading h4 {
font-size: 1.12rem;
font-weight: 700;
}
.soft-pill {
padding: 0.48rem 0.8rem;
border-radius: 999px;
color: var(--green-deep);
background: var(--green-soft);
font-size: 0.86rem;
font-weight: 600;
}
.material-form,
.price-form {
.price-card {
display: grid;
gap: 1rem;
}
@@ -477,7 +628,11 @@
.form-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 0.9rem;
gap: 0.85rem;
}
.form-grid.single {
grid-template-columns: 1fr;
}
.form-grid.compact {
@@ -487,147 +642,173 @@
label {
display: grid;
gap: 0.35rem;
color: #53645b;
font-size: 0.9rem;
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);
padding: 0.9rem 0.95rem;
border: 1px solid var(--line-strong);
border-radius: 0.95rem;
background: var(--panel-soft);
color: var(--text);
}
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;
.primary-button {
justify-self: start;
padding: 0.85rem 1rem;
border: none;
border-radius: 0.9rem;
color: #fff;
background: linear-gradient(135deg, var(--green) 0%, var(--green-deep) 100%);
box-shadow: 0 8px 20px rgba(34, 169, 94, 0.18);
font-weight: 600;
cursor: pointer;
}
button:disabled {
.primary-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;
.mini-list,
.impact-list,
.materials-list {
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;
.mini-list article,
.impact-list article {
padding: 0.95rem 1rem;
border: 1px solid var(--line);
border-radius: 1rem;
background: var(--panel-soft);
}
.material-card {
display: grid;
gap: 1rem;
gap: 1.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;
.material-title {
display: flex;
align-items: center;
gap: 0.85rem;
}
.detail-row {
display: flex;
.material-icon {
width: 2.4rem;
height: 2.4rem;
display: inline-flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
justify-content: center;
border-radius: 0.85rem;
font-size: 0.76rem;
font-weight: 700;
letter-spacing: 0.05em;
flex-shrink: 0;
}
.material-icon.active {
color: #fff;
background: linear-gradient(135deg, var(--green) 0%, var(--green-deep) 100%);
}
.material-icon.muted {
color: #55685f;
background: #e9efeb;
}
.status-pill {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0.42rem 0.78rem;
border-radius: 999px;
font-size: 0.84rem;
font-weight: 600;
text-transform: capitalize;
}
.status-pill.positive {
color: var(--green-deep);
background: var(--green-soft);
}
.status-pill.neutral {
color: #5a6c63;
background: #edf2ef;
}
.material-grid {
grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr);
}
.stats-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 0.8rem;
}
.stats-grid article,
.price-card,
.impact-card {
padding: 1rem;
border: 1px solid var(--line);
border-radius: 1rem;
background: var(--panel-soft);
}
.stats-grid strong {
display: block;
margin-top: 0.35rem;
font-size: 1.1rem;
font-weight: 700;
}
.impact-grid {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (max-width: 1100px) {
.impact-heading {
margin-bottom: 0.9rem;
}
.impact-heading span {
color: var(--muted);
font-size: 0.92rem;
font-weight: 600;
}
@media (max-width: 1180px) {
.metric-row,
.top-grid,
.material-grid,
.impact-grid {
.impact-grid,
.stats-grid {
grid-template-columns: 1fr;
}
}
@media (max-width: 760px) {
.page-header,
.panel-heading,
.snapshot-list li,
.impact-panel li,
.detail-row {
@media (max-width: 820px) {
.page-intro,
.section-heading,
.material-header,
.impact-heading,
.mini-list article,
.impact-list article {
flex-direction: column;
align-items: start;
align-items: flex-start;
}
.form-grid,
.form-grid.compact {
grid-template-columns: 1fr;
}
.header-status {
text-align: left;
}
}
</style>