@nuxt-customer-portal/invoice-timesheets 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/app/plugins/invoice-timesheets-source.ts +9 -0
- package/migrations/0000_baseline.sql +24 -0
- package/migrations/meta/0000_snapshot.json +120 -0
- package/migrations/meta/_journal.json +13 -0
- package/nuxt.config.ts +4 -0
- package/package.json +51 -0
- package/portal.manifest.mjs +8 -0
- package/server/api/invoice-timesheets/invoices.post.ts +22 -0
- package/server/api/invoice-timesheets/sources.get.ts +23 -0
- package/server/db/schema/invoice-timesheets.ts +24 -0
- package/server/plugins/timesheet-reopen-guard.ts +14 -0
- package/server/utils/invoice-timesheets.ts +215 -0
- package/shared/validation.ts +18 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# @nuxt-customer-portal/invoice-timesheets
|
|
2
|
+
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- ca166fd: Extract invoices into an independent Nuxt layer and add an optional Timesheets source bridge.
|
|
8
|
+
|
|
9
|
+
This removes the legacy `/timesheets/invoices`, `/admin/timesheets/invoices`, and Timesheets invoice API routes without redirects or delegates. Use `/invoices`, `/admin/invoices`, `/api/invoices`, and `/api/invoice-timesheets` instead.
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Updated dependencies [ca166fd]
|
|
14
|
+
- @nuxt-customer-portal/timesheets@0.3.0
|
|
15
|
+
- @nuxt-customer-portal/invoices@0.3.0
|
|
16
|
+
- @nuxt-customer-portal/core@0.3.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nuxt Customer Portal contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export default defineNuxtPlugin(() => {
|
|
2
|
+
useInvoiceSources().register({
|
|
3
|
+
id: 'timesheets',
|
|
4
|
+
labelKey: 'features.invoices.admin.approvedTime',
|
|
5
|
+
descriptionKey: 'features.invoices.admin.approvedTimeDescription',
|
|
6
|
+
load: (query) => $fetch('/api/invoice-timesheets/sources', { query }),
|
|
7
|
+
create: (input) => $fetch<{ id: string }>('/api/invoice-timesheets/invoices', { method: 'POST', body: input })
|
|
8
|
+
})
|
|
9
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
CREATE SCHEMA "invoice_timesheets";
|
|
2
|
+
--> statement-breakpoint
|
|
3
|
+
CREATE TABLE "invoice_timesheets"."invoice_time_entry" (
|
|
4
|
+
"id" text PRIMARY KEY NOT NULL,
|
|
5
|
+
"invoice_id" text NOT NULL,
|
|
6
|
+
"invoice_line_id" text NOT NULL,
|
|
7
|
+
"time_entry_id" text NOT NULL
|
|
8
|
+
);
|
|
9
|
+
--> statement-breakpoint
|
|
10
|
+
ALTER TABLE "invoice_timesheets"."invoice_time_entry" ADD CONSTRAINT "invoice_time_entry_invoice_id_invoice_id_fk" FOREIGN KEY ("invoice_id") REFERENCES "invoices"."invoice"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
11
|
+
ALTER TABLE "invoice_timesheets"."invoice_time_entry" ADD CONSTRAINT "invoice_time_entry_invoice_line_id_invoice_line_id_fk" FOREIGN KEY ("invoice_line_id") REFERENCES "invoices"."invoice_line"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
12
|
+
ALTER TABLE "invoice_timesheets"."invoice_time_entry" ADD CONSTRAINT "invoice_time_entry_time_entry_id_time_entry_id_fk" FOREIGN KEY ("time_entry_id") REFERENCES "timesheets"."time_entry"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
|
|
13
|
+
CREATE INDEX "invoice_time_entry_invoice_idx" ON "invoice_timesheets"."invoice_time_entry" USING btree ("invoice_id");--> statement-breakpoint
|
|
14
|
+
CREATE INDEX "invoice_time_entry_entry_idx" ON "invoice_timesheets"."invoice_time_entry" USING btree ("time_entry_id");--> statement-breakpoint
|
|
15
|
+
DO $$
|
|
16
|
+
BEGIN
|
|
17
|
+
IF to_regclass('invoices._legacy_timesheet_source') IS NOT NULL THEN
|
|
18
|
+
INSERT INTO invoice_timesheets.invoice_time_entry SELECT * FROM invoices._legacy_timesheet_source ON CONFLICT (id) DO NOTHING;
|
|
19
|
+
IF EXISTS (SELECT id FROM invoices._legacy_timesheet_source EXCEPT SELECT id FROM invoice_timesheets.invoice_time_entry) THEN
|
|
20
|
+
RAISE EXCEPTION 'Invoice Timesheets source migration verification failed';
|
|
21
|
+
END IF;
|
|
22
|
+
DROP TABLE invoices._legacy_timesheet_source;
|
|
23
|
+
END IF;
|
|
24
|
+
END $$;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "457e372c-5ffc-4b6c-b67d-2fe9313eca44",
|
|
3
|
+
"prevId": "00000000-0000-0000-0000-000000000000",
|
|
4
|
+
"version": "7",
|
|
5
|
+
"dialect": "postgresql",
|
|
6
|
+
"tables": {
|
|
7
|
+
"invoice_timesheets.invoice_time_entry": {
|
|
8
|
+
"name": "invoice_time_entry",
|
|
9
|
+
"schema": "invoice_timesheets",
|
|
10
|
+
"columns": {
|
|
11
|
+
"id": {
|
|
12
|
+
"name": "id",
|
|
13
|
+
"type": "text",
|
|
14
|
+
"primaryKey": true,
|
|
15
|
+
"notNull": true
|
|
16
|
+
},
|
|
17
|
+
"invoice_id": {
|
|
18
|
+
"name": "invoice_id",
|
|
19
|
+
"type": "text",
|
|
20
|
+
"primaryKey": false,
|
|
21
|
+
"notNull": true
|
|
22
|
+
},
|
|
23
|
+
"invoice_line_id": {
|
|
24
|
+
"name": "invoice_line_id",
|
|
25
|
+
"type": "text",
|
|
26
|
+
"primaryKey": false,
|
|
27
|
+
"notNull": true
|
|
28
|
+
},
|
|
29
|
+
"time_entry_id": {
|
|
30
|
+
"name": "time_entry_id",
|
|
31
|
+
"type": "text",
|
|
32
|
+
"primaryKey": false,
|
|
33
|
+
"notNull": true
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"indexes": {
|
|
37
|
+
"invoice_time_entry_invoice_idx": {
|
|
38
|
+
"name": "invoice_time_entry_invoice_idx",
|
|
39
|
+
"columns": [
|
|
40
|
+
{
|
|
41
|
+
"expression": "invoice_id",
|
|
42
|
+
"isExpression": false,
|
|
43
|
+
"asc": true,
|
|
44
|
+
"nulls": "last"
|
|
45
|
+
}
|
|
46
|
+
],
|
|
47
|
+
"isUnique": false,
|
|
48
|
+
"concurrently": false,
|
|
49
|
+
"method": "btree",
|
|
50
|
+
"with": {}
|
|
51
|
+
},
|
|
52
|
+
"invoice_time_entry_active_entry_uidx": {
|
|
53
|
+
"name": "invoice_time_entry_active_entry_uidx",
|
|
54
|
+
"columns": [
|
|
55
|
+
{
|
|
56
|
+
"expression": "time_entry_id",
|
|
57
|
+
"isExpression": false,
|
|
58
|
+
"asc": true,
|
|
59
|
+
"nulls": "last"
|
|
60
|
+
}
|
|
61
|
+
],
|
|
62
|
+
"isUnique": true,
|
|
63
|
+
"concurrently": false,
|
|
64
|
+
"method": "btree",
|
|
65
|
+
"with": {}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"foreignKeys": {
|
|
69
|
+
"invoice_time_entry_invoice_id_invoice_id_fk": {
|
|
70
|
+
"name": "invoice_time_entry_invoice_id_invoice_id_fk",
|
|
71
|
+
"tableFrom": "invoice_time_entry",
|
|
72
|
+
"tableTo": "invoice",
|
|
73
|
+
"schemaTo": "invoices",
|
|
74
|
+
"columnsFrom": ["invoice_id"],
|
|
75
|
+
"columnsTo": ["id"],
|
|
76
|
+
"onDelete": "cascade",
|
|
77
|
+
"onUpdate": "no action"
|
|
78
|
+
},
|
|
79
|
+
"invoice_time_entry_invoice_line_id_invoice_line_id_fk": {
|
|
80
|
+
"name": "invoice_time_entry_invoice_line_id_invoice_line_id_fk",
|
|
81
|
+
"tableFrom": "invoice_time_entry",
|
|
82
|
+
"tableTo": "invoice_line",
|
|
83
|
+
"schemaTo": "invoices",
|
|
84
|
+
"columnsFrom": ["invoice_line_id"],
|
|
85
|
+
"columnsTo": ["id"],
|
|
86
|
+
"onDelete": "cascade",
|
|
87
|
+
"onUpdate": "no action"
|
|
88
|
+
},
|
|
89
|
+
"invoice_time_entry_time_entry_id_time_entry_id_fk": {
|
|
90
|
+
"name": "invoice_time_entry_time_entry_id_time_entry_id_fk",
|
|
91
|
+
"tableFrom": "invoice_time_entry",
|
|
92
|
+
"tableTo": "time_entry",
|
|
93
|
+
"schemaTo": "timesheets",
|
|
94
|
+
"columnsFrom": ["time_entry_id"],
|
|
95
|
+
"columnsTo": ["id"],
|
|
96
|
+
"onDelete": "restrict",
|
|
97
|
+
"onUpdate": "no action"
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
"compositePrimaryKeys": {},
|
|
101
|
+
"uniqueConstraints": {},
|
|
102
|
+
"policies": {},
|
|
103
|
+
"checkConstraints": {},
|
|
104
|
+
"isRLSEnabled": false
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
"enums": {},
|
|
108
|
+
"schemas": {
|
|
109
|
+
"invoice_timesheets": "invoice_timesheets"
|
|
110
|
+
},
|
|
111
|
+
"sequences": {},
|
|
112
|
+
"roles": {},
|
|
113
|
+
"policies": {},
|
|
114
|
+
"views": {},
|
|
115
|
+
"_meta": {
|
|
116
|
+
"columns": {},
|
|
117
|
+
"schemas": {},
|
|
118
|
+
"tables": {}
|
|
119
|
+
}
|
|
120
|
+
}
|
package/nuxt.config.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nuxt-customer-portal/invoice-timesheets",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"homepage": "https://nuxt-customer-portal.com",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/ludulicious/nuxt-customer-portal.git"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./nuxt.config.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"LICENSE",
|
|
14
|
+
"CHANGELOG.md",
|
|
15
|
+
"README.md",
|
|
16
|
+
"app",
|
|
17
|
+
"migrations",
|
|
18
|
+
"server",
|
|
19
|
+
"shared",
|
|
20
|
+
"nuxt.config.ts",
|
|
21
|
+
"portal.manifest.mjs"
|
|
22
|
+
],
|
|
23
|
+
"exports": {
|
|
24
|
+
".": "./nuxt.config.ts",
|
|
25
|
+
"./schema": "./server/db/schema/invoice-timesheets.ts",
|
|
26
|
+
"./portal-manifest": "./portal.manifest.mjs",
|
|
27
|
+
"./server/*": "./server/*.ts",
|
|
28
|
+
"./shared/*": "./shared/*.ts"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@types/pg": "8.15.5",
|
|
35
|
+
"drizzle-orm": "^0.45.2",
|
|
36
|
+
"nanoid": "^5.1.6",
|
|
37
|
+
"pg": "^8.22.0",
|
|
38
|
+
"zod": "^4.4.3",
|
|
39
|
+
"@nuxt-customer-portal/core": "^0.3.0",
|
|
40
|
+
"@nuxt-customer-portal/invoices": "^0.3.0",
|
|
41
|
+
"@nuxt-customer-portal/timesheets": "^0.3.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"nuxt": "^4.5.1",
|
|
45
|
+
"vue": "^3.5.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"nuxt": "^4.5.1",
|
|
49
|
+
"vue": "^3.5.0"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { requireFeatureAccess } from '@nuxt-customer-portal/core/server/portal'
|
|
2
|
+
import { invoicesFeature } from '@nuxt-customer-portal/invoices/feature'
|
|
3
|
+
import { createInvoiceFromTimesheets } from '@nuxt-customer-portal/invoice-timesheets/server/utils/invoice-timesheets'
|
|
4
|
+
import { timesheetInvoiceCreateSchema } from '@nuxt-customer-portal/invoice-timesheets/shared/validation'
|
|
5
|
+
|
|
6
|
+
defineRouteMeta({
|
|
7
|
+
openAPI: {
|
|
8
|
+
tags: ['Invoices'],
|
|
9
|
+
operationId: 'invoiceTimesheetsInvoicesPost',
|
|
10
|
+
summary: 'Create an invoice from Timesheets',
|
|
11
|
+
description: 'Atomically validates approved billable entries, creates an invoice, and links its source entries.'
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
export default defineEventHandler(async (event) => {
|
|
16
|
+
const { organizationId, session } = await requireFeatureAccess(event, invoicesFeature.policy, 'manage')
|
|
17
|
+
return createInvoiceFromTimesheets(
|
|
18
|
+
organizationId,
|
|
19
|
+
session.user.id,
|
|
20
|
+
timesheetInvoiceCreateSchema.parse(await readBody(event))
|
|
21
|
+
)
|
|
22
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { requireFeatureAccess } from '@nuxt-customer-portal/core/server/portal'
|
|
2
|
+
import { invoicesFeature } from '@nuxt-customer-portal/invoices/feature'
|
|
3
|
+
import { listTimesheetInvoiceSources } from '@nuxt-customer-portal/invoice-timesheets/server/utils/invoice-timesheets'
|
|
4
|
+
|
|
5
|
+
defineRouteMeta({
|
|
6
|
+
openAPI: {
|
|
7
|
+
tags: ['Invoices'],
|
|
8
|
+
operationId: 'invoiceTimesheetsSourcesGet',
|
|
9
|
+
summary: 'List invoiceable Timesheets entries',
|
|
10
|
+
description:
|
|
11
|
+
'Lists approved, billable, uninvoiced entries when the active provider Timesheets workspace is enabled.'
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
export default defineEventHandler(async (event) => {
|
|
16
|
+
const { organizationId } = await requireFeatureAccess(event, invoicesFeature.policy, 'manage')
|
|
17
|
+
const query = getQuery(event)
|
|
18
|
+
return listTimesheetInvoiceSources(
|
|
19
|
+
organizationId,
|
|
20
|
+
typeof query.from === 'string' ? query.from : undefined,
|
|
21
|
+
typeof query.to === 'string' ? query.to : undefined
|
|
22
|
+
)
|
|
23
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { index, pgSchema, text } from 'drizzle-orm/pg-core'
|
|
2
|
+
import { invoice, invoiceLine } from '@nuxt-customer-portal/invoices/schema'
|
|
3
|
+
import { timeEntry } from '@nuxt-customer-portal/timesheets/schema'
|
|
4
|
+
|
|
5
|
+
export const invoiceTimesheetsSchema = pgSchema('invoice_timesheets')
|
|
6
|
+
export const invoiceTimeEntry = invoiceTimesheetsSchema.table(
|
|
7
|
+
'invoice_time_entry',
|
|
8
|
+
{
|
|
9
|
+
id: text('id').primaryKey(),
|
|
10
|
+
invoiceId: text('invoice_id')
|
|
11
|
+
.notNull()
|
|
12
|
+
.references(() => invoice.id, { onDelete: 'cascade' }),
|
|
13
|
+
invoiceLineId: text('invoice_line_id')
|
|
14
|
+
.notNull()
|
|
15
|
+
.references(() => invoiceLine.id, { onDelete: 'cascade' }),
|
|
16
|
+
timeEntryId: text('time_entry_id')
|
|
17
|
+
.notNull()
|
|
18
|
+
.references(() => timeEntry.id, { onDelete: 'restrict' })
|
|
19
|
+
},
|
|
20
|
+
(table) => [
|
|
21
|
+
index('invoice_time_entry_invoice_idx').on(table.invoiceId),
|
|
22
|
+
index('invoice_time_entry_entry_idx').on(table.timeEntryId)
|
|
23
|
+
]
|
|
24
|
+
)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { registerTimeEntryReopenGuard } from '@nuxt-customer-portal/core/server/utils/business-hooks'
|
|
2
|
+
import { hasActiveInvoiceSource } from '@nuxt-customer-portal/invoice-timesheets/server/utils/invoice-timesheets'
|
|
3
|
+
|
|
4
|
+
export default defineNitroPlugin(() =>
|
|
5
|
+
registerTimeEntryReopenGuard(async (organizationId, entryIds) => {
|
|
6
|
+
if (await hasActiveInvoiceSource(organizationId, entryIds)) {
|
|
7
|
+
throw createError({
|
|
8
|
+
statusCode: 409,
|
|
9
|
+
message: 'Invoiced timesheets cannot be reopened',
|
|
10
|
+
data: { code: 'TIMESHEET_INVOICED' }
|
|
11
|
+
})
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
)
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { and, eq, inArray, sql } from 'drizzle-orm'
|
|
2
|
+
import { nanoid } from 'nanoid'
|
|
3
|
+
import { db } from '@nuxt-customer-portal/core/server/portal'
|
|
4
|
+
import { invoice, invoiceLine } from '@nuxt-customer-portal/invoices/schema'
|
|
5
|
+
import {
|
|
6
|
+
createInvoiceInTransaction,
|
|
7
|
+
requireInvoicesEnabled
|
|
8
|
+
} from '@nuxt-customer-portal/invoices/server/utils/invoice-repository'
|
|
9
|
+
import { timeEntry, timesheetClientReview, timesheetSubmission } from '@nuxt-customer-portal/timesheets/schema'
|
|
10
|
+
import {
|
|
11
|
+
getReport,
|
|
12
|
+
requireTimesheetWorkspace
|
|
13
|
+
} from '@nuxt-customer-portal/timesheets/server/utils/timesheet-repository'
|
|
14
|
+
import { invoiceTimeEntry } from '@nuxt-customer-portal/invoice-timesheets/server/db/schema/invoice-timesheets'
|
|
15
|
+
|
|
16
|
+
export const listTimesheetInvoiceSources = async (organizationId: string, from?: string, to?: string) => {
|
|
17
|
+
try {
|
|
18
|
+
await requireTimesheetWorkspace(organizationId)
|
|
19
|
+
} catch {
|
|
20
|
+
return { enabled: false, entries: [] }
|
|
21
|
+
}
|
|
22
|
+
const report = await getReport(organizationId, { status: 'APPROVED', billable: true, from, to })
|
|
23
|
+
if (!report.rows.length) {
|
|
24
|
+
return { enabled: true, entries: [] }
|
|
25
|
+
}
|
|
26
|
+
const ids = report.rows.map((row) => row.entryId)
|
|
27
|
+
const [contexts, linked, disputed] = await Promise.all([
|
|
28
|
+
db
|
|
29
|
+
.select({
|
|
30
|
+
entryId: timeEntry.id,
|
|
31
|
+
submissionId: timeEntry.submissionId,
|
|
32
|
+
clientOrganizationId: timeEntry.clientOrganizationId,
|
|
33
|
+
projectId: timeEntry.projectId
|
|
34
|
+
})
|
|
35
|
+
.from(timeEntry)
|
|
36
|
+
.where(and(eq(timeEntry.organizationId, organizationId), inArray(timeEntry.id, ids))),
|
|
37
|
+
db
|
|
38
|
+
.select({ timeEntryId: invoiceTimeEntry.timeEntryId })
|
|
39
|
+
.from(invoiceTimeEntry)
|
|
40
|
+
.innerJoin(invoice, eq(invoice.id, invoiceTimeEntry.invoiceId))
|
|
41
|
+
.where(and(inArray(invoiceTimeEntry.timeEntryId, ids), inArray(invoice.status, ['DRAFT', 'ISSUED', 'PAID']))),
|
|
42
|
+
db
|
|
43
|
+
.select({
|
|
44
|
+
submissionId: timesheetClientReview.submissionId,
|
|
45
|
+
clientOrganizationId: timesheetClientReview.clientOrganizationId
|
|
46
|
+
})
|
|
47
|
+
.from(timesheetClientReview)
|
|
48
|
+
.innerJoin(timesheetSubmission, eq(timesheetSubmission.id, timesheetClientReview.submissionId))
|
|
49
|
+
.where(and(eq(timesheetSubmission.organizationId, organizationId), eq(timesheetClientReview.status, 'DISPUTED')))
|
|
50
|
+
])
|
|
51
|
+
const context = new Map(contexts.map((item) => [item.entryId, item]))
|
|
52
|
+
const used = new Set(linked.map((item) => item.timeEntryId))
|
|
53
|
+
const disputes = new Set(disputed.map((item) => `${item.submissionId}:${item.clientOrganizationId}`))
|
|
54
|
+
return {
|
|
55
|
+
enabled: true,
|
|
56
|
+
entries: report.rows.flatMap((row) => {
|
|
57
|
+
const item = context.get(row.entryId)
|
|
58
|
+
return !item ||
|
|
59
|
+
item.clientOrganizationId === organizationId ||
|
|
60
|
+
used.has(row.entryId) ||
|
|
61
|
+
disputes.has(`${item.submissionId}:${item.clientOrganizationId}`)
|
|
62
|
+
? []
|
|
63
|
+
: [
|
|
64
|
+
{
|
|
65
|
+
entryId: row.entryId,
|
|
66
|
+
date: row.date,
|
|
67
|
+
client: row.client,
|
|
68
|
+
clientOrganizationId: item.clientOrganizationId,
|
|
69
|
+
project: row.project,
|
|
70
|
+
projectId: item.projectId,
|
|
71
|
+
person: row.person,
|
|
72
|
+
activity: row.activity,
|
|
73
|
+
minutes: row.minutes,
|
|
74
|
+
hourlyRateMinor: row.hourlyRateMinor,
|
|
75
|
+
amountMinor: row.amountMinor,
|
|
76
|
+
currency: row.currency,
|
|
77
|
+
note: row.note
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const createInvoiceFromTimesheets = async (
|
|
85
|
+
organizationId: string,
|
|
86
|
+
actorUserId: string,
|
|
87
|
+
input: {
|
|
88
|
+
clientOrganizationId: string
|
|
89
|
+
contactId?: string | null
|
|
90
|
+
number: string
|
|
91
|
+
currency: string
|
|
92
|
+
issueDate: string
|
|
93
|
+
dueDate: string
|
|
94
|
+
subject?: string | null
|
|
95
|
+
notes?: string | null
|
|
96
|
+
lines: Array<{
|
|
97
|
+
description: string
|
|
98
|
+
quantityMilli: number
|
|
99
|
+
unit: string
|
|
100
|
+
unitPriceMinor: number
|
|
101
|
+
vatRateBasisPoints: number
|
|
102
|
+
timeEntryIds: string[]
|
|
103
|
+
}>
|
|
104
|
+
}
|
|
105
|
+
) => {
|
|
106
|
+
await requireTimesheetWorkspace(organizationId)
|
|
107
|
+
await requireInvoicesEnabled(organizationId)
|
|
108
|
+
if (input.clientOrganizationId === organizationId) {
|
|
109
|
+
throw createError({ statusCode: 400, message: 'Internal time entries cannot be invoiced' })
|
|
110
|
+
}
|
|
111
|
+
return db.transaction(async (tx) => {
|
|
112
|
+
const sourceIds = input.lines.flatMap((line) => line.timeEntryIds)
|
|
113
|
+
if (new Set(sourceIds).size !== sourceIds.length) {
|
|
114
|
+
throw createError({ statusCode: 400, message: 'A time entry can only occur once on an invoice' })
|
|
115
|
+
}
|
|
116
|
+
for (const sourceId of [...sourceIds].sort()) {
|
|
117
|
+
await tx.execute(sql`select pg_advisory_xact_lock(hashtext(${`invoice-timesheet:${sourceId}`}))`)
|
|
118
|
+
}
|
|
119
|
+
const [entries, existing, disputed] = await Promise.all([
|
|
120
|
+
tx
|
|
121
|
+
.select({ entry: timeEntry, status: timesheetSubmission.status })
|
|
122
|
+
.from(timeEntry)
|
|
123
|
+
.innerJoin(timesheetSubmission, eq(timesheetSubmission.id, timeEntry.submissionId))
|
|
124
|
+
.where(
|
|
125
|
+
and(
|
|
126
|
+
eq(timeEntry.organizationId, organizationId),
|
|
127
|
+
eq(timeEntry.clientOrganizationId, input.clientOrganizationId),
|
|
128
|
+
inArray(timeEntry.id, sourceIds)
|
|
129
|
+
)
|
|
130
|
+
),
|
|
131
|
+
tx
|
|
132
|
+
.select({ id: invoiceTimeEntry.id })
|
|
133
|
+
.from(invoiceTimeEntry)
|
|
134
|
+
.innerJoin(invoice, eq(invoice.id, invoiceTimeEntry.invoiceId))
|
|
135
|
+
.where(
|
|
136
|
+
and(inArray(invoiceTimeEntry.timeEntryId, sourceIds), inArray(invoice.status, ['DRAFT', 'ISSUED', 'PAID']))
|
|
137
|
+
),
|
|
138
|
+
tx
|
|
139
|
+
.select({ submissionId: timesheetClientReview.submissionId })
|
|
140
|
+
.from(timesheetClientReview)
|
|
141
|
+
.innerJoin(timesheetSubmission, eq(timesheetSubmission.id, timesheetClientReview.submissionId))
|
|
142
|
+
.where(
|
|
143
|
+
and(
|
|
144
|
+
eq(timesheetSubmission.organizationId, organizationId),
|
|
145
|
+
eq(timesheetClientReview.clientOrganizationId, input.clientOrganizationId),
|
|
146
|
+
eq(timesheetClientReview.status, 'DISPUTED')
|
|
147
|
+
)
|
|
148
|
+
)
|
|
149
|
+
])
|
|
150
|
+
const disputedSubmissions = new Set(disputed.map((item) => item.submissionId))
|
|
151
|
+
if (
|
|
152
|
+
entries.length !== sourceIds.length ||
|
|
153
|
+
existing.length ||
|
|
154
|
+
entries.some(
|
|
155
|
+
(row) =>
|
|
156
|
+
row.status !== 'APPROVED' ||
|
|
157
|
+
disputedSubmissions.has(row.entry.submissionId) ||
|
|
158
|
+
!row.entry.billableSnapshot ||
|
|
159
|
+
row.entry.currencySnapshot !== input.currency
|
|
160
|
+
)
|
|
161
|
+
) {
|
|
162
|
+
throw createError({ statusCode: 409, message: 'One or more time entries are unavailable for invoicing' })
|
|
163
|
+
}
|
|
164
|
+
const byId = new Map(entries.map((row) => [row.entry.id, row.entry]))
|
|
165
|
+
for (const line of input.lines) {
|
|
166
|
+
const sources = line.timeEntryIds.map((id) => byId.get(id)!)
|
|
167
|
+
if (
|
|
168
|
+
new Set(sources.map((entry) => entry.hourlyRateMinorSnapshot)).size !== 1 ||
|
|
169
|
+
line.unitPriceMinor !== sources[0]!.hourlyRateMinorSnapshot ||
|
|
170
|
+
line.quantityMilli !== Math.round((sources.reduce((sum, entry) => sum + entry.durationMinutes, 0) * 1000) / 60)
|
|
171
|
+
) {
|
|
172
|
+
throw createError({
|
|
173
|
+
statusCode: 400,
|
|
174
|
+
message: 'Timesheet invoice line totals do not match their source entries'
|
|
175
|
+
})
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const created = await createInvoiceInTransaction(tx, organizationId, actorUserId, {
|
|
179
|
+
...input,
|
|
180
|
+
lines: input.lines.map(({ timeEntryIds: _ids, ...line }) => line)
|
|
181
|
+
})
|
|
182
|
+
const createdLines = await tx.select().from(invoiceLine).where(eq(invoiceLine.invoiceId, created.id))
|
|
183
|
+
for (const [position, source] of input.lines.entries()) {
|
|
184
|
+
const line = createdLines.find((item) => item.position === position)!
|
|
185
|
+
await tx.insert(invoiceTimeEntry).values(
|
|
186
|
+
source.timeEntryIds.map((timeEntryId) => ({
|
|
187
|
+
id: nanoid(),
|
|
188
|
+
invoiceId: created.id,
|
|
189
|
+
invoiceLineId: line.id,
|
|
190
|
+
timeEntryId
|
|
191
|
+
}))
|
|
192
|
+
)
|
|
193
|
+
}
|
|
194
|
+
return created
|
|
195
|
+
})
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export const hasActiveInvoiceSource = async (organizationId: string, entryIds: string[]) => {
|
|
199
|
+
if (!entryIds.length) {
|
|
200
|
+
return false
|
|
201
|
+
}
|
|
202
|
+
const [selected] = await db
|
|
203
|
+
.select({ id: invoiceTimeEntry.id })
|
|
204
|
+
.from(invoiceTimeEntry)
|
|
205
|
+
.innerJoin(invoice, eq(invoice.id, invoiceTimeEntry.invoiceId))
|
|
206
|
+
.where(
|
|
207
|
+
and(
|
|
208
|
+
eq(invoice.organizationId, organizationId),
|
|
209
|
+
inArray(invoiceTimeEntry.timeEntryId, entryIds),
|
|
210
|
+
inArray(invoice.status, ['DRAFT', 'ISSUED', 'PAID'])
|
|
211
|
+
)
|
|
212
|
+
)
|
|
213
|
+
.limit(1)
|
|
214
|
+
return Boolean(selected)
|
|
215
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import { invoiceCreateSchema } from '@nuxt-customer-portal/invoices/server/utils/invoice-validation'
|
|
3
|
+
|
|
4
|
+
export const timesheetInvoiceCreateSchema = invoiceCreateSchema.safeExtend({
|
|
5
|
+
lines: z
|
|
6
|
+
.array(
|
|
7
|
+
z.object({
|
|
8
|
+
description: z.string().trim().min(1).max(500),
|
|
9
|
+
quantityMilli: z.number().int().positive(),
|
|
10
|
+
unit: z.string().trim().min(1).max(30),
|
|
11
|
+
unitPriceMinor: z.number().int().min(0),
|
|
12
|
+
vatRateBasisPoints: z.number().int().min(0).max(10_000),
|
|
13
|
+
timeEntryIds: z.array(z.string().trim().min(1)).min(1)
|
|
14
|
+
})
|
|
15
|
+
)
|
|
16
|
+
.min(1)
|
|
17
|
+
.max(500)
|
|
18
|
+
})
|