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 ledgerAdd from '../ledger/add.js';
|
||||
import makeRunner from '../runner.js';
|
||||
@@ -24,6 +32,9 @@ export default function update(provider) {
|
||||
const access = resolveSelectedCompanyAccess(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;
|
||||
|
||||
/**
|
||||
* Updates a shipping record.
|
||||
@@ -42,7 +53,7 @@ export default function update(provider) {
|
||||
const shippingId = requireId(id, 'Shipping');
|
||||
|
||||
const run = async (runner, innerTx) => {
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
const { companyId, companyuserId } = await access(googleAuthId, runner);
|
||||
|
||||
const check = await runner.query(q.GET_SHIPPING_BY_ID, {
|
||||
id: shippingId,
|
||||
@@ -98,6 +109,7 @@ export default function update(provider) {
|
||||
});
|
||||
const lines = linesRes.rows ?? [];
|
||||
const trackedLines = [];
|
||||
const untrackedLines = [];
|
||||
for (const line of lines) {
|
||||
const sku = line.sku ? String(line.sku).trim() : '';
|
||||
const amount = Number(line.amount);
|
||||
@@ -110,11 +122,23 @@ export default function update(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;
|
||||
}
|
||||
|
||||
if (trackedLines.length > 0) {
|
||||
let warehouseId = null;
|
||||
if (patch.warehouseId != null) {
|
||||
@@ -137,7 +161,6 @@ export default function update(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