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

@@ -6,7 +6,7 @@ This document is auto-generated from the JSDoc comments in `src/store` by runnin
*Source: `src/store/delivery/create.js`*
Creates a new delivery for the user's selected company with its lines.
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.
**Parameters**
@@ -14,7 +14,6 @@ Creates a new delivery for the user's selected company with its lines.
| --- | --- | --- |
| `googleAuthId` | `number` | The id of the Google auth user. |
| `body` | `object` | The delivery input. |
| `body.number` | `string` | The delivery number. |
| `body.date` | `string` | The delivery date. |
| `body.supplierId` | `number` | The id of the supplier. |
| `[body.externalNumber]` | `string` | An external (supplier) reference number. |
@@ -31,7 +30,7 @@ Creates a new delivery for the user's selected company with its lines.
**Example**
```js
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: [...] }
```

View File

@@ -293,7 +293,7 @@ Company-scoped goods receipts on `manifest` / `manifestarticle` with fixed `type
| --- | --- | --- |
| `list` | `(googleAuthId)` | `DeliverySummary[]` |
| `getById` | `(googleAuthId, id)` | `Delivery` |
| `create` | `(googleAuthId, { number, date, supplierId?, externalNumber?, lines })` | `Delivery` |
| `create` | `(googleAuthId, { date, supplierId?, externalNumber?, lines })` | `Delivery` |
| `update` | `(googleAuthId, id, { number?, date?, supplierId?, externalNumber?, lines? })` | `Delivery` |
| `delete` | `(googleAuthId, id)` | `void` |
@@ -321,7 +321,7 @@ type DeliveryLine = {
type Delivery = DeliverySummary & { lines: DeliveryLine[] }
```
`create` inserts header + lines in one transaction. `supplierId` null or a Lieferanten `entity` of the company (else `Supplier not found`). Each line requires `sku` (resolved in the company category tree → `Article not found`), `amount`, `net`, `tax` (rate → `tax.ensure`); `rebate` defaults `0`. Empty/invalid `number` or `date` → `Invalid delivery`; bad line payload → `Invalid lines`.
`create` inserts header + lines in one transaction. The `number` is auto-allocated via `getnextnumberwithprefixandpadding` (`numbers` type `delivery`); supply `externalNumber` for an external (supplier) reference. `supplierId` null or a Lieferanten `entity` of the company (else `Supplier not found`). Each line requires `sku` (resolved in the company category tree → `Article not found`), `amount`, `net`, `tax` (rate → `tax.ensure`); `rebate` defaults `0`. Empty/invalid `date` → `Invalid delivery`; bad line payload → `Invalid lines`.
`update` patches header fields that are present. If `lines` is present, replace-all lines; if omitted, leave lines unchanged. Returns full `Delivery`.

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)