Files
jtlPosSync/src/queries/customer-count.js
2026-08-15 03:04:14 +02:00

36 lines
1.1 KiB
JavaScript

import sql from 'mssql';
import { isDemoMode } from '../demo/mode.js';
import { getDemoCustomerCount } from '../demo/store.js';
import { getPool } from '../db.js';
import { getActiveShopId, getActiveShopSubshopId } from '../shop.js';
const CUSTOMER_COUNT_SQL = `
SELECT COUNT(DISTINCT k.kKunde) AS CustomerCount
FROM dbo.tkunde k
LEFT JOIN dbo.tInetKundeShop iks
ON iks.kKunde = k.kKunde
AND iks.kShop = @kShop
AND iks.kSubShop = @SubShopId
WHERE k.kKundenGruppe = 1
AND (@kShop = 0 OR EXISTS (
SELECT 1 FROM dbo.tInetKundeShop x
WHERE x.kKunde = k.kKunde AND x.kShop = @kShop
))
AND CONVERT(BIGINT, k.bRowversion) > @cursor;
`;
export async function getCustomerCount({ cursor = 0 } = {}) {
if (isDemoMode()) {
return getDemoCustomerCount({ cursor });
}
const result = await getPool()
.request()
.input('kShop', sql.Int, getActiveShopId())
.input('SubShopId', sql.Int, getActiveShopSubshopId())
.input('cursor', sql.BigInt, cursor)
.query(CUSTOMER_COUNT_SQL);
return result.recordset[0]?.CustomerCount ?? 0;
}