1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/*
This file is part of GNU Taler
(C) 2021-2023 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
import { useTranslationContext } from "@gnu-taler/web-util/lib/index.browser";
import { h, VNode } from "preact";
import { useState } from "preact/hooks";
import { MerchantBackend, WithId } from "../../declaration.js";
import { ProductMap } from "../../paths/instance/orders/create/CreatePage.js";
import { FormErrors, FormProvider } from "../form/FormProvider.js";
import { InputNumber } from "../form/InputNumber.js";
import { InputSearchProduct } from "../form/InputSearchProduct.js";
type Form = {
product: MerchantBackend.Products.ProductDetail & WithId;
quantity: number;
};
interface Props {
currentProducts: ProductMap;
onAddProduct: (
product: MerchantBackend.Products.ProductDetail & WithId,
quantity: number,
) => void;
inventory: (MerchantBackend.Products.ProductDetail & WithId)[];
}
export function InventoryProductForm({
currentProducts,
onAddProduct,
inventory,
}: Props): VNode {
const initialState = { quantity: 1 };
const [state, setState] = useState<Partial<Form>>(initialState);
const [errors, setErrors] = useState<FormErrors<Form>>({});
const { i18n } = useTranslationContext();
const productWithInfiniteStock =
state.product && state.product.total_stock === -1;
const submit = (): void => {
if (!state.product) {
setErrors({
product: i18n.str`You must enter a valid product identifier.`,
});
return;
}
if (productWithInfiniteStock) {
onAddProduct(state.product, 1);
} else {
if (!state.quantity || state.quantity <= 0) {
setErrors({ quantity: i18n.str`Quantity must be greater than 0!` });
return;
}
const currentStock =
state.product.total_stock -
state.product.total_lost -
state.product.total_sold;
const p = currentProducts[state.product.id];
if (p) {
if (state.quantity + p.quantity > currentStock) {
const left = currentStock - p.quantity;
setErrors({
quantity: i18n.str`This quantity exceeds remaining stock. Currently, only ${left} units remain unreserved in stock.`,
});
return;
}
onAddProduct(state.product, state.quantity + p.quantity);
} else {
if (state.quantity > currentStock) {
const left = currentStock;
setErrors({
quantity: i18n.str`This quantity exceeds remaining stock. Currently, only ${left} units remain unreserved in stock.`,
});
return;
}
onAddProduct(state.product, state.quantity);
}
}
setState(initialState);
};
return (
<FormProvider<Form> errors={errors} object={state} valueHandler={setState}>
<InputSearchProduct
selected={state.product}
onChange={(p) => setState((v) => ({ ...v, product: p }))}
products={inventory}
/>
{state.product && (
<div class="columns mt-5">
<div class="column is-two-thirds">
{!productWithInfiniteStock && (
<InputNumber<Form>
name="quantity"
label={i18n.str`Quantity`}
tooltip={i18n.str`how many products will be added`}
/>
)}
</div>
<div class="column">
<div class="buttons is-right">
<button class="button is-success" onClick={submit}>
<i18n.Translate>Add from inventory</i18n.Translate>
</button>
</div>
</div>
</div>
)}
</FormProvider>
);
}
|