From 8142b22b213958e3f3b9267bf2cde7826ba39489 Mon Sep 17 00:00:00 2001 From: default Date: Thu, 25 Dec 2025 17:11:15 +0000 Subject: [PATCH] Print Log Update --- .../components/prints/EditPrintModal.svelte | 605 +++++++++++------- .../components/prints/LogPrintModal.svelte | 109 ++-- src/routes/prints/+page.server.ts | 62 +- svelte.config.js | 5 +- 4 files changed, 487 insertions(+), 294 deletions(-) diff --git a/src/lib/components/prints/EditPrintModal.svelte b/src/lib/components/prints/EditPrintModal.svelte index 79578b9..7bd4cf2 100644 --- a/src/lib/components/prints/EditPrintModal.svelte +++ b/src/lib/components/prints/EditPrintModal.svelte @@ -1,259 +1,376 @@ - {#if print} -
{ - isSubmitting = true; - return async ({ update }) => { - await update(); - isSubmitting = false; - handleClose(); - }; - }} - class="space-y-4" - > - + {#if print} + { + // Convert hours + minutes to total minutes + const hours = Number(formData.get("duration_hours") || 0); + const mins = Number(formData.get("duration_mins") || 0); + formData.set("duration_minutes", String(hours * 60 + mins)); - -
- - -
- - - - -
-
+ // Convert elapsed hours + minutes to total minutes + const elapsedHours = Number(formData.get("elapsed_hours") || 0); + const elapsedMins = Number(formData.get("elapsed_mins") || 0); + formData.set( + "elapsed_minutes", + String(elapsedHours * 60 + elapsedMins), + ); - + isSubmitting = true; + return async ({ update }) => { + await update(); + isSubmitting = false; + handleClose(); + }; + }} + class="space-y-4" + > + - {#if editStatus === "In Progress"} - -
-
- - - -
-
- - - -
-
+ +
+ + +
+ + + + +
+
-
-

- - Update the total print time and how long it's been running. -

-
- - -
-
- - -
-
- {:else} - -
- - - -
- {/if} + -
- -
- - -
-
-
- {/if} + {#if editStatus === "In Progress"} + +
+
+ + + +
+
+ + + +
+
+ +
+

+ + Update the total print time and how long it's been running. +

+
+
+ + +
+
+ + hr +
+
+ + min +
+
+
+
+ + +
+
+ + hr +
+
+ + min +
+
+
+
+
+ + +
+
+ {:else} + +
+
+ + +
+
+ + hr +
+
+ + min +
+
+
+
+ + +
+
+ {/if} + +
+ +
+ + +
+
+ + {/if}
diff --git a/src/lib/components/prints/LogPrintModal.svelte b/src/lib/components/prints/LogPrintModal.svelte index 5fae20d..8b88bb3 100644 --- a/src/lib/components/prints/LogPrintModal.svelte +++ b/src/lib/components/prints/LogPrintModal.svelte @@ -26,7 +26,17 @@
{ + use:enhance={({ formData }) => { + // Convert hours + minutes to total minutes + const hours = Number(formData.get('duration_hours') || 0); + const mins = Number(formData.get('duration_mins') || 0); + formData.set('duration_minutes', String(hours * 60 + mins)); + + // Convert elapsed hours + minutes to total minutes + const elapsedHours = Number(formData.get('elapsed_hours') || 0); + const elapsedMins = Number(formData.get('elapsed_mins') || 0); + formData.set('elapsed_minutes', String(elapsedHours * 60 + elapsedMins)); + isSubmitting = true; return async ({ update }) => { await update(); @@ -152,21 +162,35 @@ Enter the expected total print time and how long it's been running.

-
- - +
+
+ + +
+
+ + hr +
+
+ + min +
+
+
+
+ + +
+
+ + hr +
+
+ + min +
+
+
{:else} -
- - - +
+
+ + +
+
+ + hr +
+
+ + min +
+
+
+
+ + +
{/if} diff --git a/src/routes/prints/+page.server.ts b/src/routes/prints/+page.server.ts index 3924e21..51f2153 100644 --- a/src/routes/prints/+page.server.ts +++ b/src/routes/prints/+page.server.ts @@ -1,6 +1,7 @@ import { PrintJob } from '$lib/models/PrintJob'; import { Spool } from '$lib/models/Spool'; import { Printer } from '$lib/models/Printer'; +import { User } from '$lib/models/User'; import { connectDB } from '$lib/server/db'; import type { PageServerLoad, Actions } from './$types'; import { fail, redirect } from '@sveltejs/kit'; @@ -59,16 +60,35 @@ export const actions: Actions = { const printer = await Printer.findOne({ _id: printer_id, user_id: locals.user.id }); if (!printer) return fail(404, { printerNotFound: true }); + // Get user's electricity rate + const user = await User.findById(locals.user.id); + const electricityRate = user?.electricity_rate || 0.12; // Default $/kWh + const weightUsed = Number(filament_used_g); + const durationMins = Number(duration_minutes); - // Calculate Cost: use manual if provided, otherwise calculate + // Calculate Filament Cost: use manual if provided, otherwise calculate let costFilament: number; if (manual_cost && String(manual_cost).trim() !== '') { + // If manual cost provided, it's the total cost (filament + electricity) costFilament = Number(manual_cost); } else { costFilament = (spool.price / spool.weight_initial_g) * weightUsed; } + // Calculate Electricity Cost: Power (kW) × Duration (hours) × Rate ($/kWh) + const powerKw = (printer.power_consumption_watts || 0) / 1000; + const durationHours = durationMins / 60; + const costEnergy = powerKw * durationHours * electricityRate; + + // Total cost = filament + electricity (only if not manual) + let totalCost: number; + if (manual_cost && String(manual_cost).trim() !== '') { + totalCost = Number(manual_cost); + } else { + totalCost = costFilament + costEnergy; + } + // 2. Create Print Job const isInProgress = status === 'In Progress'; @@ -84,9 +104,10 @@ export const actions: Actions = { name: name || 'Untitled Print', spool_id, printer_id, - duration_minutes: Number(duration_minutes), + duration_minutes: durationMins, filament_used_g: weightUsed, - calculated_cost_filament: Number(costFilament.toFixed(2)), + calculated_cost_filament: Number(totalCost.toFixed(2)), + calculated_cost_energy: Number(costEnergy.toFixed(2)), status, started_at: startedAt, date: new Date() @@ -126,19 +147,43 @@ export const actions: Actions = { await connectDB(); try { - const printJob = await PrintJob.findOne({ _id: id, user_id: locals.user.id }).populate('spool_id'); + const printJob = await PrintJob.findOne({ _id: id, user_id: locals.user.id }).populate('spool_id').populate('printer_id'); if (!printJob) return fail(404, { notFound: true }); + // Get user's electricity rate + const user = await User.findById(locals.user.id); + const electricityRate = user?.electricity_rate || 0.12; + const weightUsed = Number(filament_used_g); + const durationMins = Number(duration_minutes); - // Calculate Cost: use manual if provided, otherwise calculate + // Get printer for power calculation + const printerForCalc = printer_id + ? await Printer.findById(printer_id) + : printJob.printer_id; + + // Calculate Filament Cost: use manual if provided, otherwise calculate let costFilament: number; if (manual_cost && String(manual_cost).trim() !== '') { + // Manual cost is the total, we'll calculate energy separately for tracking costFilament = Number(manual_cost); } else if (printJob.spool_id?.price && printJob.spool_id?.weight_initial_g) { costFilament = (printJob.spool_id.price / printJob.spool_id.weight_initial_g) * weightUsed; } else { - costFilament = printJob.calculated_cost_filament || 0; + costFilament = 0; + } + + // Calculate Electricity Cost + const powerKw = (printerForCalc?.power_consumption_watts || 0) / 1000; + const durationHours = durationMins / 60; + const costEnergy = powerKw * durationHours * electricityRate; + + // Total cost + let totalCost: number; + if (manual_cost && String(manual_cost).trim() !== '') { + totalCost = Number(manual_cost); + } else { + totalCost = costFilament + costEnergy; } // Calculate started_at based on elapsed time for In Progress @@ -155,9 +200,10 @@ export const actions: Actions = { // Build update object const updateData: any = { name, - duration_minutes: Number(duration_minutes), + duration_minutes: durationMins, filament_used_g: weightUsed, - calculated_cost_filament: Number(costFilament.toFixed(2)), + calculated_cost_filament: Number(totalCost.toFixed(2)), + calculated_cost_energy: Number(costEnergy.toFixed(2)), status, started_at: startedAt }; diff --git a/svelte.config.js b/svelte.config.js index fb67358..b4b7de8 100644 --- a/svelte.config.js +++ b/svelte.config.js @@ -5,10 +5,7 @@ import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'; const config = { preprocess: vitePreprocess(), kit: { - adapter: adapter(), - csrf: { - checkOrigin: true - } + adapter: adapter() } };