summaryrefslogtreecommitdiff
path: root/packages/merchant-backoffice-ui/src/components/form/InputStock.tsx
blob: 8104d1f9ff51c8fe2996c245686ffcdba4b1bbc6 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 This file is part of GNU Taler
 (C) 2021-2024 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/>
 */

/**
 *
 * @author Sebastian Javier Marchano (sebasjm)
 */
import { TalerMerchantApi, TalerProtocolTimestamp } from "@gnu-taler/taler-util";
import { useTranslationContext } from "@gnu-taler/web-util/browser";
import { Fragment, h } from "preact";
import { useLayoutEffect, useState } from "preact/hooks";
import { FormErrors, FormProvider } from "./FormProvider.js";
import { InputDate } from "./InputDate.js";
import { InputGroup } from "./InputGroup.js";
import { InputLocation } from "./InputLocation.js";
import { InputNumber } from "./InputNumber.js";
import { InputProps, useField } from "./useField.js";

export interface Props<T> extends InputProps<T> {
  alreadyExist?: boolean;
}

type Entity = Stock;

export interface Stock {
  current: number;
  lost: number;
  sold: number;
  address?: TalerMerchantApi.Location;
  nextRestock?: TalerProtocolTimestamp;
}

interface StockDelta {
  incoming: number;
  lost: number;
}

export function InputStock<T>({
  name,
  tooltip,
  label,
  alreadyExist,
}: Props<keyof T>) {
  const { error, value, onChange } = useField<T>(name);

  const [errors, setErrors] = useState<FormErrors<Entity>>({});

  const [formValue, valueHandler] = useState<Partial<Entity>>(value);
  const [addedStock, setAddedStock] = useState<StockDelta>({
    incoming: 0,
    lost: 0,
  });
  const { i18n } = useTranslationContext();

  useLayoutEffect(() => {
    if (!formValue) {
      onChange(undefined as any);
    } else {
      onChange({
        ...formValue,
        current: (formValue?.current || 0) + addedStock.incoming,
        lost: (formValue?.lost || 0) + addedStock.lost,
      } as any);
    }
  }, [formValue, addedStock]);

  if (!formValue) {
    return (
      <Fragment>
        <div class="field is-horizontal">
          <div class="field-label is-normal">
            <label class="label">
              {label}
              {tooltip && (
                <span class="icon has-tooltip-right" data-tooltip={tooltip}>
                  <i class="mdi mdi-information" />
                </span>
              )}
            </label>
          </div>
          <div class="field-body is-flex-grow-3">
            <div class="field has-addons">
              {!alreadyExist ? (
                <button
                  class="button"
                  data-tooltip={i18n.str`click here to configure the stock of the product, leave it as is and the backend will not control stock`}
                  onClick={(): void => {
                    valueHandler({
                      current: 0,
                      lost: 0,
                      sold: 0,
                    } as Stock as any);
                  }}
                >
                  <span>
                    <i18n.Translate>Manage stock</i18n.Translate>
                  </span>
                </button>
              ) : (
                <button
                  class="button"
                  data-tooltip={i18n.str`this product has been configured without stock control`}
                  disabled
                >
                  <span>
                    <i18n.Translate>Infinite</i18n.Translate>
                  </span>
                </button>
              )}
            </div>
          </div>
        </div>
      </Fragment>
    );
  }

  const currentStock =
    (formValue.current || 0) - (formValue.lost || 0) - (formValue.sold || 0);

  const stockAddedErrors: FormErrors<typeof addedStock> = {
    lost:
      currentStock + addedStock.incoming < addedStock.lost
        ? i18n.str`lost cannot be greater than current and incoming (max ${currentStock + addedStock.incoming
          })`
        : undefined,
  };

  // const stockUpdateDescription = stockAddedErrors.lost ? '' : (
  //   !!addedStock.incoming || !!addedStock.lost ?
  //     i18n.str`current stock will change from ${currentStock} to ${currentStock + addedStock.incoming - addedStock.lost}` :
  //     i18n.str`current stock will stay at ${currentStock}`
  // )

  return (
    <Fragment>
      <div class="card">
        <header class="card-header">
          <p class="card-header-title">
            {label}
            {tooltip && (
              <span class="icon" data-tooltip={tooltip}>
                <i class="mdi mdi-information" />
              </span>
            )}
          </p>
        </header>
        <div class="card-content">
          <FormProvider<Entity>
            name="stock"
            errors={errors}
            object={formValue}
            valueHandler={valueHandler}
          >
            {alreadyExist ? (
              <Fragment>
                <FormProvider
                  name="added"
                  errors={stockAddedErrors}
                  object={addedStock}
                  valueHandler={setAddedStock as any}
                >
                  <InputNumber name="incoming" label={i18n.str`Incoming`} />
                  <InputNumber name="lost" label={i18n.str`Lost`} />
                </FormProvider>

                {/* <div class="field is-horizontal">
              <div class="field-label is-normal" />
              <div class="field-body is-flex-grow-3">
                <div class="field">
                  {stockUpdateDescription}
                </div>
              </div>
            </div> */}
              </Fragment>
            ) : (
              <InputNumber<Entity>
                name="current"
                label={i18n.str`Current`}
                side={
                  <button
                    class="button is-danger"
                    data-tooltip={i18n.str`remove stock control for this product`}
                    onClick={(): void => {
                      valueHandler(undefined as any);
                    }}
                  >
                    <span>
                      <i18n.Translate>without stock</i18n.Translate>
                    </span>
                  </button>
                }
              />
            )}

            <InputDate<Entity>
              name="nextRestock"
              label={i18n.str`Next restock`}
              withTimestampSupport
            />

            <InputGroup<Entity> name="address" label={i18n.str`Warehouse address`}>
              <InputLocation name="address" />
            </InputGroup>
          </FormProvider>
        </div>
      </div>
    </Fragment>
  );
}
// (