2024-06-27 23:31:59 +02:00
|
|
|
import uuid from "react-native-uuid";
|
2024-07-06 00:00:08 +02:00
|
|
|
import { dimensions_t, area_t } from "./dimensions";
|
|
|
|
import { matchDimensions } from "./dimensions";
|
2024-07-31 19:01:45 +02:00
|
|
|
import { Area, Length, Unit } from "convert";
|
2024-06-28 23:53:48 +02:00
|
|
|
|
2024-06-27 16:06:39 +02:00
|
|
|
export type Id = string;
|
|
|
|
|
|
|
|
export type Currency = "USD";
|
|
|
|
|
|
|
|
export type ProductAttributes = {
|
2024-06-27 23:31:59 +02:00
|
|
|
id?: string,
|
2024-06-27 16:06:39 +02:00
|
|
|
name?: string,
|
|
|
|
image?: string,
|
|
|
|
description?: string,
|
|
|
|
depth?: string,
|
2024-06-27 23:31:59 +02:00
|
|
|
currency?: Currency,
|
2024-07-17 15:54:55 +02:00
|
|
|
[index:string]: any,
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
2024-07-31 19:01:45 +02:00
|
|
|
|
2024-07-06 00:00:08 +02:00
|
|
|
export function dimensionArea(d: dimensions_t) {
|
|
|
|
return "w" in d ? d.w * d.l : 0;
|
2024-06-28 23:53:48 +02:00
|
|
|
}
|
|
|
|
|
2024-07-31 19:01:45 +02:00
|
|
|
export const PRODUCT_TYPES = [
|
|
|
|
"lumber",
|
|
|
|
"area_rug",
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
export type product_type_t = typeof PRODUCT_TYPES[number];
|
|
|
|
|
2024-07-17 15:54:55 +02:00
|
|
|
|
|
|
|
export type Product = {
|
2024-07-06 00:00:08 +02:00
|
|
|
id?: Id;
|
|
|
|
pricePerUnit: number;
|
|
|
|
dimensions: dimensions_t;
|
2024-07-17 15:54:55 +02:00
|
|
|
type: product_type_t;
|
2024-07-06 00:00:08 +02:00
|
|
|
attributes?: ProductAttributes;
|
2024-06-30 18:37:27 +02:00
|
|
|
};
|
|
|
|
|
2024-07-17 15:54:55 +02:00
|
|
|
export type LumberProduct = Product & {
|
|
|
|
type: "lumber"
|
|
|
|
}
|
2024-06-30 18:37:27 +02:00
|
|
|
|
2024-07-17 15:54:55 +02:00
|
|
|
export type AreaRugProduct = Product & {
|
|
|
|
type: "lumber"
|
|
|
|
}
|
2024-06-27 16:06:39 +02:00
|
|
|
|
2024-08-10 19:06:25 +02:00
|
|
|
export function productPriceFor(product : Product, dimensions: dimensions_t, damage: number = 0): number {
|
2024-07-17 15:54:55 +02:00
|
|
|
if (Number.isNaN(damage)) damage = 0;
|
|
|
|
const dim = matchDimensions(dimensions, product.dimensions);
|
|
|
|
return (
|
|
|
|
dim.w ? dimensionArea(dim) / dimensionArea(product.dimensions) * product.pricePerUnit
|
|
|
|
: (dim.l / product.dimensions.l) * product.pricePerUnit
|
|
|
|
) * (1.0 - damage);
|
|
|
|
}
|
2024-06-27 16:06:39 +02:00
|
|
|
|
2024-07-17 15:54:55 +02:00
|
|
|
export function priceDisplay(price : number) {
|
|
|
|
return price.toLocaleString(undefined, {
|
|
|
|
minimumFractionDigits: 2,
|
|
|
|
maximumFractionDigits: 2,
|
|
|
|
})
|
|
|
|
}
|
2024-06-30 18:37:27 +02:00
|
|
|
|
2024-07-17 15:54:55 +02:00
|
|
|
export function pricePerUnitDisplay(product : Product) {
|
|
|
|
const p = priceDisplay(product.pricePerUnit);
|
|
|
|
const { l, u } = product.dimensions;
|
|
|
|
const w = (product.dimensions as area_t).w || null;
|
2024-07-01 21:23:45 +02:00
|
|
|
const d = w ? `${l}${u} x ${w}${u}` : `${l}${u}`;
|
2024-06-30 18:37:27 +02:00
|
|
|
return `$${p} per ${d}`
|
2024-07-17 15:54:55 +02:00
|
|
|
}
|
2024-06-30 18:37:27 +02:00
|
|
|
|
2024-07-17 15:54:55 +02:00
|
|
|
export function attributesAsList(attributes : {[key:string]: any}) {
|
|
|
|
return Object.entries(attributes).map(([key, value]) => {
|
|
|
|
return { key, value }
|
|
|
|
})
|
|
|
|
}
|
2024-06-30 18:37:27 +02:00
|
|
|
|
2024-07-17 15:54:55 +02:00
|
|
|
export function removeAttribute(attributes: {[key: string]: any}, key: string) {
|
|
|
|
return Object.fromEntries(
|
|
|
|
Object.entries(attributes).filter(
|
|
|
|
([k, v]) => {
|
|
|
|
k == key;
|
|
|
|
}
|
2024-06-30 18:37:27 +02:00
|
|
|
)
|
2024-07-17 15:54:55 +02:00
|
|
|
);
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|