|
@@ -1,6 +1,7 @@
|
|
|
import 'server-only'
|
|
import 'server-only'
|
|
|
|
|
|
|
|
import { db } from '@/lib/db'
|
|
import { db } from '@/lib/db'
|
|
|
|
|
+import { effectiveInvoiceDate } from '@/lib/invoice-utils'
|
|
|
import { zonedDayKey } from '@/lib/timezone'
|
|
import { zonedDayKey } from '@/lib/timezone'
|
|
|
import { parseUploadUrl } from '@/lib/upload-url'
|
|
import { parseUploadUrl } from '@/lib/upload-url'
|
|
|
import { workshopMoneyFormatter } from '@/lib/workshop-currency'
|
|
import { workshopMoneyFormatter } from '@/lib/workshop-currency'
|
|
@@ -276,7 +277,10 @@ export async function getPortalThreadEvents(
|
|
|
where: {
|
|
where: {
|
|
|
...mine,
|
|
...mine,
|
|
|
status: 'completed',
|
|
status: 'completed',
|
|
|
- AND: [{ OR: [{ issuedAt: { gte: since } }, { sentAt: { gte: since } }] }],
|
|
|
|
|
|
|
+ AND: [
|
|
|
|
|
+ { OR: [{ issuedAt: { not: null } }, { sentAt: { not: null } }] },
|
|
|
|
|
+ invoiceEventWindow(since),
|
|
|
|
|
+ ],
|
|
|
},
|
|
},
|
|
|
select: {
|
|
select: {
|
|
|
id: true,
|
|
id: true,
|
|
@@ -285,6 +289,9 @@ export async function getPortalThreadEvents(
|
|
|
totalAmount: true,
|
|
totalAmount: true,
|
|
|
issuedAt: true,
|
|
issuedAt: true,
|
|
|
sentAt: true,
|
|
sentAt: true,
|
|
|
|
|
+ invoiceDate: true,
|
|
|
|
|
+ startDateTime: true,
|
|
|
|
|
+ serviceDate: true,
|
|
|
invoiceDueDate: true,
|
|
invoiceDueDate: true,
|
|
|
manuallyPaid: true,
|
|
manuallyPaid: true,
|
|
|
payments: { select: { amount: true } },
|
|
payments: { select: { amount: true } },
|
|
@@ -306,6 +313,7 @@ export async function getPortalThreadEvents(
|
|
|
id: `work_started:${job.id}`,
|
|
id: `work_started:${job.id}`,
|
|
|
kind: 'work_started',
|
|
kind: 'work_started',
|
|
|
at: at.toISOString(),
|
|
at: at.toISOString(),
|
|
|
|
|
+ dayOnly: false,
|
|
|
vehicle: carName(job.vehicle),
|
|
vehicle: carName(job.vehicle),
|
|
|
title: job.title || null,
|
|
title: job.title || null,
|
|
|
amount: null,
|
|
amount: null,
|
|
@@ -320,6 +328,7 @@ export async function getPortalThreadEvents(
|
|
|
id: `update:${report.id}`,
|
|
id: `update:${report.id}`,
|
|
|
kind: 'update',
|
|
kind: 'update',
|
|
|
at: report.sentAt.toISOString(),
|
|
at: report.sentAt.toISOString(),
|
|
|
|
|
+ dayOnly: false,
|
|
|
vehicle: carName(report.serviceRecord?.vehicle),
|
|
vehicle: carName(report.serviceRecord?.vehicle),
|
|
|
title: shorten(report.message) ?? shorten(report.title),
|
|
title: shorten(report.message) ?? shorten(report.title),
|
|
|
amount: null,
|
|
amount: null,
|
|
@@ -335,6 +344,7 @@ export async function getPortalThreadEvents(
|
|
|
id: `quote:${quote.id}`,
|
|
id: `quote:${quote.id}`,
|
|
|
kind: 'quote',
|
|
kind: 'quote',
|
|
|
at: at.toISOString(),
|
|
at: at.toISOString(),
|
|
|
|
|
+ dayOnly: false,
|
|
|
vehicle: carName(quote.vehicle),
|
|
vehicle: carName(quote.vehicle),
|
|
|
title: quote.title || quote.quoteNumber || null,
|
|
title: quote.title || quote.quoteNumber || null,
|
|
|
amount: money(quote.totalAmount),
|
|
amount: money(quote.totalAmount),
|
|
@@ -345,8 +355,8 @@ export async function getPortalThreadEvents(
|
|
|
}
|
|
}
|
|
|
const today = zonedDayKey(now, timeZone)
|
|
const today = zonedDayKey(now, timeZone)
|
|
|
for (const invoice of invoices) {
|
|
for (const invoice of invoices) {
|
|
|
- const at = invoice.issuedAt ?? invoice.sentAt
|
|
|
|
|
- if (!at || at < since) continue
|
|
|
|
|
|
|
+ const { at, dayOnly } = invoiceEventTime(invoice)
|
|
|
|
|
+ if (at < since) continue
|
|
|
// Overdue is owed money past its due day, and that day is the workshop's.
|
|
// Overdue is owed money past its due day, and that day is the workshop's.
|
|
|
const owed = portalPayment(invoice).balance > 0
|
|
const owed = portalPayment(invoice).balance > 0
|
|
|
const overdue =
|
|
const overdue =
|
|
@@ -355,6 +365,7 @@ export async function getPortalThreadEvents(
|
|
|
id: `invoice:${invoice.id}`,
|
|
id: `invoice:${invoice.id}`,
|
|
|
kind: 'invoice',
|
|
kind: 'invoice',
|
|
|
at: at.toISOString(),
|
|
at: at.toISOString(),
|
|
|
|
|
+ dayOnly,
|
|
|
vehicle: carName(invoice.vehicle),
|
|
vehicle: carName(invoice.vehicle),
|
|
|
title: invoice.invoiceNumber || invoice.title || null,
|
|
title: invoice.invoiceNumber || invoice.title || null,
|
|
|
amount: money(invoice.totalAmount),
|
|
amount: money(invoice.totalAmount),
|
|
@@ -370,6 +381,50 @@ export async function getPortalThreadEvents(
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * When an invoice stands in the thread: the moment the customer would
|
|
|
|
|
+ * recognise.
|
|
|
|
|
+ *
|
|
|
|
|
+ * That is when it was sent to them, a real moment with a time of day. An
|
|
|
|
|
+ * invoice that was never sent (shared by link only, or paid at the counter)
|
|
|
|
|
+ * stands on its own date, the one printed on it and shown on its page in the
|
|
|
|
|
+ * portal (`effectiveInvoiceDate`: the invoice date, else the scheduled start,
|
|
|
|
|
+ * else the day of the work): a calendar day, so the chat shows the day and
|
|
|
|
|
+ * no clock time. `issuedAt` is never it: that is when the sheet was frozen,
|
|
|
|
|
+ * which can be a payment weeks after the work, and its time of day means
|
|
|
|
|
+ * nothing to a customer.
|
|
|
|
|
+ *
|
|
|
|
|
+ * `invoiceEventWindow` is the same precedence as a query: change both together.
|
|
|
|
|
+ */
|
|
|
|
|
+export function invoiceEventTime(invoice: {
|
|
|
|
|
+ sentAt: Date | null
|
|
|
|
|
+ invoiceDate?: Date | null
|
|
|
|
|
+ startDateTime?: Date | null
|
|
|
|
|
+ serviceDate: Date
|
|
|
|
|
+}): { at: Date; dayOnly: boolean } {
|
|
|
|
|
+ if (invoice.sentAt) return { at: invoice.sentAt, dayOnly: false }
|
|
|
|
|
+ return { at: effectiveInvoiceDate(invoice), dayOnly: true }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * The invoices whose place in the thread (`invoiceEventTime`) is on or after
|
|
|
|
|
+ * `since`, as a Prisma filter. Each fallback only counts for a row that has
|
|
|
|
|
+ * no date of higher rank, so the rows the query returns are the rows that
|
|
|
|
|
+ * are drawn: a filter on any of the dates let old invoices in by a date that
|
|
|
|
|
+ * is not the one they stand on, and they used up the row limit before being
|
|
|
|
|
+ * dropped, hiding a recent invoice.
|
|
|
|
|
+ */
|
|
|
|
|
+export function invoiceEventWindow(since: Date) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ OR: [
|
|
|
|
|
+ { sentAt: { gte: since } },
|
|
|
|
|
+ { sentAt: null, invoiceDate: { gte: since } },
|
|
|
|
|
+ { sentAt: null, invoiceDate: null, startDateTime: { gte: since } },
|
|
|
|
|
+ { sentAt: null, invoiceDate: null, startDateTime: null, serviceDate: { gte: since } },
|
|
|
|
|
+ ],
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* From when a thread shows events. The window is `days` long, and it never
|
|
* From when a thread shows events. The window is `days` long, and it never
|
|
|
* starts before the oldest loaded message while older messages are still
|
|
* starts before the oldest loaded message while older messages are still
|