Auto-allocate delivery number via getnextnumberwithprefixandpadding

This commit is contained in:
seb
2026-08-08 10:09:08 +02:00
parent 91e7c03655
commit fb88cb92a5
3 changed files with 12 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
import { CREATE_MANIFEST, DELIVERY_TYPE } from '../../sql/delivery.js';
import request from '../request.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import nextNumber from '../numbers/nextNumber.js';
import taxEnsure from '../tax/ensure.js';
import { insertLines, normalizeLines, parseDate } from '../manifestLines.js';
import { loadDelivery } from './helpers.js';
@@ -9,13 +10,15 @@ import { assertSupplier } from './writeLines.js';
export default function create(pool) {
const access = resolveSelectedCompanyAccess(pool);
const ensureTax = taxEnsure(pool);
const allocateNumber = nextNumber(pool);
/**
* Creates a new delivery for the user's selected company with its lines.
* The delivery number is allocated internally via `getnextnumberwithprefixandpadding`
* (`type` `delivery`); supply `externalNumber` for an external (supplier) reference.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} body - The delivery input.
* @param {string} body.number - The delivery number.
* @param {string} body.date - The delivery date.
* @param {number} body.supplierId - The id of the supplier.
* @param {string} [body.externalNumber] - An external (supplier) reference number.
@@ -24,13 +27,12 @@ export default function create(pool) {
* @throws {Error} If the delivery is invalid.
*
* @example
* const delivery = await store.delivery.create(42, { number: 'D-1', date: '2026-01-01', supplierId: 5, lines: [...] });
* const delivery = await store.delivery.create(42, { date: '2026-01-01', supplierId: 5, lines: [...] });
* // => { id, number, type, entityId, externalNumber, date, lines: [...] }
*/
return async (googleAuthId, body = {}) => {
const number = body.number?.trim?.() ? body.number.trim() : null;
const date = parseDate(body.date);
if (!number || !date) {
if (!date) {
throw new Error('Invalid delivery');
}
const externalNumber = body.externalNumber?.trim?.()
@@ -52,6 +54,8 @@ export default function create(pool) {
body.supplierId ?? null,
);
const number = await allocateNumber(companyId, DELIVERY_TYPE, tx);
const created = await request(pool, tx)
.input('companyId', companyId)
.input('number', number)