u
This commit is contained in:
@@ -1,4 +1,12 @@
|
||||
import { queriesFor, requireId, requireText } from '../logistics/helpers.js';
|
||||
import {
|
||||
FIND_ARTICLES_FOR_HISTORY as MSSQL_FIND_ARTICLES,
|
||||
INSERT_ARTICLE_HISTORY as MSSQL_INSERT_HISTORY,
|
||||
} from '../../providers/mssql/queries/ledger.js';
|
||||
import {
|
||||
FIND_ARTICLES_FOR_HISTORY as SQLITE_FIND_ARTICLES,
|
||||
INSERT_ARTICLE_HISTORY as SQLITE_INSERT_HISTORY,
|
||||
} from '../../providers/sqlite3/queries/ledger.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import nextNumber from '../numbers/nextNumber.js';
|
||||
import ledgerAdd from '../ledger/add.js';
|
||||
@@ -19,12 +27,15 @@ export default function create(provider) {
|
||||
const allocateNumber = nextNumber(provider);
|
||||
const addLedger = ledgerAdd(provider);
|
||||
const q = queriesFor(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const findArticlesSql = isSqlite ? SQLITE_FIND_ARTICLES : MSSQL_FIND_ARTICLES;
|
||||
const insertHistorySql = isSqlite ? SQLITE_INSERT_HISTORY : MSSQL_INSERT_HISTORY;
|
||||
|
||||
/**
|
||||
* Creates a shipping for a manifest (order). The number is allocated from the
|
||||
* `shipping` numbers sequence (own prefix + counter). If state is 'shipped',
|
||||
* adjusts stock by appending negative ledger movements for tracked order lines,
|
||||
* writes article history, and ensures the order was not already shipped.
|
||||
* writes article history for all items, and ensures the order was not already shipped.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {object} input - Shipping input.
|
||||
@@ -46,7 +57,7 @@ export default function create(provider) {
|
||||
const isShipped = state.trim().toLowerCase() === 'shipped';
|
||||
|
||||
const run = async (runner, innerTx) => {
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
const { companyId, companyuserId } = await access(googleAuthId, runner);
|
||||
|
||||
// Verify manifest exists and belongs to company
|
||||
const manifestRes = await runner.query(q.GET_MANIFEST_HEADER_BY_ID, {
|
||||
@@ -91,8 +102,9 @@ export default function create(provider) {
|
||||
});
|
||||
const lines = linesRes.rows ?? [];
|
||||
|
||||
// Check which lines need warehouse stock deduction
|
||||
// Check which lines need warehouse stock deduction vs untracked history
|
||||
const trackedLines = [];
|
||||
const untrackedLines = [];
|
||||
for (const line of lines) {
|
||||
const sku = line.sku ? String(line.sku).trim() : '';
|
||||
const amount = Number(line.amount);
|
||||
@@ -107,11 +119,24 @@ export default function create(provider) {
|
||||
const isUntracked = (trackRes.rows ?? []).some(
|
||||
(r) => Number(r.track_stock ?? 1) === 0
|
||||
);
|
||||
if (!isUntracked && (trackRes.rows ?? []).length > 0) {
|
||||
if (isUntracked) {
|
||||
untrackedLines.push({ ...line, amount });
|
||||
} else if ((trackRes.rows ?? []).length > 0) {
|
||||
trackedLines.push({ ...line, amount });
|
||||
}
|
||||
}
|
||||
|
||||
const manifestNumber = manifest.number || String(manifestId);
|
||||
|
||||
let customerName = null;
|
||||
if (manifest.entityId) {
|
||||
const entRes = await runner.query('SELECT name FROM entity WHERE id = @entityId', {
|
||||
entityId: manifest.entityId,
|
||||
});
|
||||
customerName = entRes.rows[0]?.name ?? null;
|
||||
}
|
||||
|
||||
// Book warehouse ledger movements for tracked lines
|
||||
if (trackedLines.length > 0) {
|
||||
let warehouseId = null;
|
||||
if (input.warehouseId != null) {
|
||||
@@ -134,7 +159,6 @@ export default function create(provider) {
|
||||
warehouseId = defaultWh.rows[0].id;
|
||||
}
|
||||
|
||||
const manifestNumber = manifest.number || String(manifestId);
|
||||
for (const line of trackedLines) {
|
||||
await addLedger(
|
||||
googleAuthId,
|
||||
|
||||
Reference in New Issue
Block a user