f0b570284e
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
54 lines
2.1 KiB
TypeScript
54 lines
2.1 KiB
TypeScript
import { convertQuantity, formatQuantity, UNITS, type Unit } from "./quantity";
|
||
|
||
/**
|
||
* Ürün (marka/varyant) motoru (Bölüm 6.11). Bir paketin ürün tipinin birimindeki karşılığı:
|
||
* - paket birimi ürün tipinin birimiyle aynıysa paket miktarı,
|
||
* - aynı boyuttaysa (kg↔g, lt↔ml) çevrilmiş miktar,
|
||
* - değilse elle girilen dönüşüm ("5 kg = 50 yıkama", "1 paket = 40 tablet"); yoksa null
|
||
* (kayıtta zorunlu; ürün tipinin birimi sonradan değişirse "dönüşüm gerekli" uyarısı).
|
||
* Miktarlar binde bir.
|
||
*/
|
||
|
||
const isStandard = (u: string): u is Unit => Object.hasOwn(UNITS, u);
|
||
|
||
/** Paket birimi ürün tipinin birimine kendiliğinden çevrilebiliyor mu? */
|
||
export function autoConvertible(packageUnit: string, typeUnit: string): boolean {
|
||
if (packageUnit === typeUnit) return true;
|
||
return (
|
||
isStandard(packageUnit) &&
|
||
isStandard(typeUnit) &&
|
||
UNITS[packageUnit].dimension === UNITS[typeUnit].dimension
|
||
);
|
||
}
|
||
|
||
/** Bir paketin ürün tipinin birimindeki miktarı (çevrilemiyorsa null). */
|
||
export function packageInTypeUnit(input: {
|
||
packageQuantity: number;
|
||
packageUnit: string;
|
||
typeUnit: string;
|
||
/** Elle girilen dönüşüm: bir paket = bu kadar ürün tipi birimi. */
|
||
typeQuantity: number | null;
|
||
}): number | null {
|
||
const { packageQuantity, packageUnit, typeUnit } = input;
|
||
if (packageUnit === typeUnit) return packageQuantity;
|
||
if (autoConvertible(packageUnit, typeUnit))
|
||
return convertQuantity(packageQuantity, packageUnit as Unit, typeUnit as Unit);
|
||
return input.typeQuantity && input.typeQuantity > 0 ? input.typeQuantity : null;
|
||
}
|
||
|
||
/** "5 kg", dönüşüm varsa "5 kg · 50 yıkama". */
|
||
export function describePackage(input: {
|
||
packageQuantity: number;
|
||
packageUnit: string;
|
||
typeUnit: string;
|
||
typeQuantity: number | null;
|
||
}): string {
|
||
const pkg = formatQuantity(input.packageQuantity, input.packageUnit);
|
||
if (input.packageUnit === input.typeUnit) return pkg;
|
||
const inType = packageInTypeUnit(input);
|
||
return inType === null ? pkg : `${pkg} · ${formatQuantity(inType, input.typeUnit)}`;
|
||
}
|
||
|
||
/** Barkod: 8–14 rakam (EAN-8, UPC-A, EAN-13, GTIN-14). */
|
||
export const BARCODE_PATTERN = /^\d{8,14}$/;
|