@etsoo/appscript 1.3.90 → 1.3.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,285 @@
1
+ import { DataTypes, IStorage } from '@etsoo/shared';
2
+ /**
3
+ * Shopping cart owner
4
+ * 购物篮所有人
5
+ */
6
+ export type ShoppingCartOwner = DataTypes.IdNameItem & {
7
+ isSupplier?: boolean;
8
+ };
9
+ /**
10
+ * Shopping cart data
11
+ * 购物篮数据
12
+ */
13
+ export type ShoppingCartData<T extends ShoppingCartItem> = {
14
+ currency: string;
15
+ owner: ShoppingCartOwner;
16
+ items: T[];
17
+ promotions: ShoppingPromotion[];
18
+ formData?: any;
19
+ };
20
+ /**
21
+ * Shopping promotion
22
+ * 购物促销
23
+ */
24
+ export type ShoppingPromotion = {
25
+ /**
26
+ * Promotion id
27
+ * 促销编号
28
+ */
29
+ id: number;
30
+ /**
31
+ * Promotion title
32
+ * 促销标题
33
+ */
34
+ title: string;
35
+ /**
36
+ * Discount amount
37
+ * 折扣金额
38
+ */
39
+ amount: number;
40
+ };
41
+ /**
42
+ * Shopping cart base item
43
+ * 购物篮基础项目
44
+ */
45
+ export type ShoppingCartItemBase = {
46
+ /**
47
+ * Product id
48
+ * 产品编号
49
+ */
50
+ id: DataTypes.IdType;
51
+ /**
52
+ * Product title, default is name
53
+ * 产品标题,默认为name
54
+ */
55
+ title?: string;
56
+ /**
57
+ * Sale price
58
+ * 销售价格
59
+ */
60
+ price: number;
61
+ /**
62
+ * Qty
63
+ * 数量
64
+ */
65
+ qty: number;
66
+ /**
67
+ * Product level promotions
68
+ * 产品层次促销
69
+ */
70
+ promotions: ShoppingPromotion[];
71
+ };
72
+ /**
73
+ * Shopping cart item
74
+ * 购物篮项目
75
+ */
76
+ export type ShoppingCartItem = ShoppingCartItemBase & {
77
+ /**
78
+ * Product name
79
+ * 产品名称
80
+ */
81
+ name: string;
82
+ /**
83
+ * Current price for cache
84
+ * 当前缓存价格
85
+ */
86
+ currentPrice?: number;
87
+ /**
88
+ * Subtotal
89
+ * 小计
90
+ */
91
+ subtotal: number;
92
+ /**
93
+ * Total discount amount
94
+ * 总折扣金额
95
+ */
96
+ discount: number;
97
+ };
98
+ /**
99
+ * Shopping cart change reason
100
+ * 购物篮改变原因
101
+ */
102
+ export type ShoppingCartChangeReason = 'add' | 'clear' | 'remove' | 'title' | 'update';
103
+ /**
104
+ * Shopping cart
105
+ * 购物篮
106
+ */
107
+ export declare class ShoppingCart<T extends ShoppingCartItem> {
108
+ private readonly storage;
109
+ /**
110
+ * Create identifier key
111
+ * 创建识别键
112
+ * @param currency Currency
113
+ * @returns Result
114
+ */
115
+ static createKey(currency: string): string;
116
+ /**
117
+ * Clear shopping cart
118
+ * 清除购物篮
119
+ * @param identifier Identifier
120
+ * @param storage Storage
121
+ */
122
+ static clear(identifier: string, storage: IStorage): void;
123
+ /**
124
+ * Clear shopping cart
125
+ * 清除购物篮
126
+ * @param currency Currency
127
+ * @param storage Storage
128
+ */
129
+ static clearWith(currency: string, storage?: IStorage): void;
130
+ /**
131
+ * Owner data
132
+ * 所有者信息
133
+ */
134
+ owner?: ShoppingCartOwner;
135
+ /**
136
+ * ISO currency id
137
+ * 标准货币编号
138
+ */
139
+ readonly currency: string;
140
+ /**
141
+ * Items
142
+ * 项目
143
+ */
144
+ readonly items: T[];
145
+ /**
146
+ * Order level promotions
147
+ * 订单层面促销
148
+ */
149
+ readonly promotions: ShoppingPromotion[];
150
+ /**
151
+ * Related form data
152
+ * 关联的表单数据
153
+ */
154
+ formData: any;
155
+ /**
156
+ * Currency symbol
157
+ * 币种符号
158
+ */
159
+ readonly symbol: string | undefined;
160
+ /**
161
+ * Cart identifier
162
+ * 购物篮标识
163
+ */
164
+ private readonly identifier;
165
+ /**
166
+ * Lines count
167
+ * 项目数量
168
+ */
169
+ get lines(): number;
170
+ /**
171
+ * Total qty
172
+ * 总数量
173
+ */
174
+ get totalQty(): number;
175
+ /**
176
+ * Total amount
177
+ * 总金额
178
+ */
179
+ get totalAmount(): number;
180
+ /**
181
+ * Total amount string
182
+ * 总金额字符串
183
+ */
184
+ get totalAmountStr(): string;
185
+ /**
186
+ * Cached prices
187
+ * 缓存的价格
188
+ */
189
+ private readonly prices;
190
+ /**
191
+ * Onchange callback
192
+ * 改变时回调
193
+ */
194
+ onChange?: (reason: ShoppingCartChangeReason, changedItems: T[]) => void;
195
+ /**
196
+ * Constructor
197
+ * 构造函数
198
+ * @param currency Currency ISO code
199
+ * @param storage Data storage
200
+ */
201
+ constructor(currency: string, storage?: IStorage);
202
+ /**
203
+ * Constructor
204
+ * 构造函数
205
+ * @param state Initialization state
206
+ * @param storage Data storage
207
+ */
208
+ constructor(state: ShoppingCartData<T>, storage?: IStorage);
209
+ private doChange;
210
+ /**
211
+ * Add item
212
+ * 添加项目
213
+ * @param item New item
214
+ */
215
+ addItem(item: T): void;
216
+ /**
217
+ * Add items
218
+ * @param items New items
219
+ */
220
+ addItems(items: T[]): void;
221
+ /**
222
+ * Cache price
223
+ * @param id Item id
224
+ * @param price Price
225
+ */
226
+ cachePrice(id: T['id'], price: number): void;
227
+ /**
228
+ * Clear storage
229
+ * @param keepOwner Keep owner data
230
+ */
231
+ clear(keepOwner?: boolean): void;
232
+ /**
233
+ * Format amount
234
+ * @param amount Amount
235
+ * @returns Result
236
+ */
237
+ formatAmount(amount: number): string;
238
+ /**
239
+ * Get item
240
+ * @param id Item id
241
+ * @returns Result
242
+ */
243
+ getItem(id: T['id']): T | undefined;
244
+ /**
245
+ * Reset
246
+ * @param item Shopping cart item
247
+ */
248
+ reset(item: ShoppingCartItem): void;
249
+ /**
250
+ * Save cart data
251
+ * @param persisted For persisted storage
252
+ */
253
+ save(persisted?: boolean): ShoppingCartData<T> | undefined;
254
+ /**
255
+ * Trigger update
256
+ * 触发更新
257
+ */
258
+ update(): void;
259
+ /**
260
+ * Update discount
261
+ * @param item Shopping cart item
262
+ */
263
+ updateDiscount(item: ShoppingCartItem): void;
264
+ /**
265
+ * Update item
266
+ * 更新项目
267
+ * @param id Product id
268
+ * @param qty Qty
269
+ * @param itemCreator New item creator
270
+ * @returns Updated or not
271
+ */
272
+ updateItem(id: T['id'], qty: number | undefined, itemCreator?: () => Omit<T, 'id' | 'price' | 'qty' | 'subtotal' | 'discount' | 'promotions'>): boolean;
273
+ /**
274
+ * Update price
275
+ * @param id Item id
276
+ * @param price New price
277
+ */
278
+ updatePrice(id: T['id'], price: number): void;
279
+ /**
280
+ * Update title
281
+ * @param id Item id
282
+ * @param title New title
283
+ */
284
+ updateTitle(id: T['id'], title: string): void;
285
+ }
@@ -0,0 +1,306 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ShoppingCart = void 0;
4
+ const shared_1 = require("@etsoo/shared");
5
+ /**
6
+ * Shopping cart
7
+ * 购物篮
8
+ */
9
+ class ShoppingCart {
10
+ /**
11
+ * Create identifier key
12
+ * 创建识别键
13
+ * @param currency Currency
14
+ * @returns Result
15
+ */
16
+ static createKey(currency) {
17
+ return `ETSOO-CART-${currency}`;
18
+ }
19
+ /**
20
+ * Clear shopping cart
21
+ * 清除购物篮
22
+ * @param identifier Identifier
23
+ * @param storage Storage
24
+ */
25
+ static clear(identifier, storage) {
26
+ try {
27
+ storage.setData(identifier, null);
28
+ storage.setPersistedData(identifier, null);
29
+ }
30
+ catch (error) {
31
+ console.log('ShoppingCart clear', error);
32
+ }
33
+ }
34
+ /**
35
+ * Clear shopping cart
36
+ * 清除购物篮
37
+ * @param currency Currency
38
+ * @param storage Storage
39
+ */
40
+ static clearWith(currency, storage = new shared_1.WindowStorage()) {
41
+ const identifier = this.createKey(currency);
42
+ this.clear(identifier, storage);
43
+ }
44
+ /**
45
+ * Lines count
46
+ * 项目数量
47
+ */
48
+ get lines() {
49
+ return this.items.length;
50
+ }
51
+ /**
52
+ * Total qty
53
+ * 总数量
54
+ */
55
+ get totalQty() {
56
+ return this.items.map((item) => item.qty).sum();
57
+ }
58
+ /**
59
+ * Total amount
60
+ * 总金额
61
+ */
62
+ get totalAmount() {
63
+ const subtotal = this.items
64
+ .map((item) => item.subtotal - item.discount)
65
+ .sum();
66
+ const discount = this.promotions.sum('amount');
67
+ return subtotal - discount;
68
+ }
69
+ /**
70
+ * Total amount string
71
+ * 总金额字符串
72
+ */
73
+ get totalAmountStr() {
74
+ return this.formatAmount(this.totalAmount);
75
+ }
76
+ /**
77
+ * Constructor
78
+ * 构造函数
79
+ * @param currency Currency ISO code
80
+ * @param storage Data storage
81
+ * @param state Initialization state
82
+ */
83
+ constructor(currencyOrState, storage = new shared_1.WindowStorage()) {
84
+ var _a;
85
+ this.storage = storage;
86
+ /**
87
+ * Cached prices
88
+ * 缓存的价格
89
+ */
90
+ this.prices = {};
91
+ const isCurrency = typeof currencyOrState === 'string';
92
+ this.currency = isCurrency ? currencyOrState : currencyOrState.currency;
93
+ const key = ShoppingCart.createKey(this.currency);
94
+ this.identifier = key;
95
+ this.symbol = shared_1.NumberUtils.getCurrencySymbol(this.currency);
96
+ let state;
97
+ if (isCurrency) {
98
+ try {
99
+ state =
100
+ (_a = storage.getPersistedObject(key)) !== null && _a !== void 0 ? _a : storage.getObject(key);
101
+ }
102
+ catch (error) {
103
+ console.log('ShoppingCart constructor', error);
104
+ }
105
+ }
106
+ else {
107
+ state = currencyOrState;
108
+ }
109
+ const { owner, items = [], promotions = [], formData } = state !== null && state !== void 0 ? state : {};
110
+ this.owner = owner;
111
+ this.items = items;
112
+ this.promotions = promotions;
113
+ this.formData = formData;
114
+ }
115
+ doChange(reason, changedItems) {
116
+ if (this.onChange)
117
+ this.onChange(reason, changedItems);
118
+ }
119
+ /**
120
+ * Add item
121
+ * 添加项目
122
+ * @param item New item
123
+ */
124
+ addItem(item) {
125
+ this.addItems([item]);
126
+ }
127
+ /**
128
+ * Add items
129
+ * @param items New items
130
+ */
131
+ addItems(items) {
132
+ this.items.push(...items);
133
+ this.doChange('add', items);
134
+ }
135
+ /**
136
+ * Cache price
137
+ * @param id Item id
138
+ * @param price Price
139
+ */
140
+ cachePrice(id, price) {
141
+ this.prices[id] = price;
142
+ }
143
+ /**
144
+ * Clear storage
145
+ * @param keepOwner Keep owner data
146
+ */
147
+ clear(keepOwner) {
148
+ this.items.length = 0;
149
+ this.promotions.length = 0;
150
+ if (keepOwner) {
151
+ this.save();
152
+ }
153
+ else {
154
+ ShoppingCart.clear(this.identifier, this.storage);
155
+ }
156
+ this.doChange('clear', []);
157
+ }
158
+ /**
159
+ * Format amount
160
+ * @param amount Amount
161
+ * @returns Result
162
+ */
163
+ formatAmount(amount) {
164
+ return shared_1.NumberUtils.formatMoney(amount, this.currency);
165
+ }
166
+ /**
167
+ * Get item
168
+ * @param id Item id
169
+ * @returns Result
170
+ */
171
+ getItem(id) {
172
+ return this.items.find((item) => item.id === id);
173
+ }
174
+ /**
175
+ * Reset
176
+ * @param item Shopping cart item
177
+ */
178
+ reset(item) {
179
+ item.discount = 0;
180
+ item.currentPrice = undefined;
181
+ item.promotions = [];
182
+ }
183
+ /**
184
+ * Save cart data
185
+ * @param persisted For persisted storage
186
+ */
187
+ save(persisted = true) {
188
+ if (this.owner == null)
189
+ return;
190
+ const { currency, owner, items, promotions, formData } = this;
191
+ const data = {
192
+ currency,
193
+ owner,
194
+ items,
195
+ promotions,
196
+ formData
197
+ };
198
+ try {
199
+ if (persisted) {
200
+ this.storage.setPersistedData(this.identifier, data);
201
+ }
202
+ else {
203
+ this.storage.setData(this.identifier, data);
204
+ }
205
+ }
206
+ catch (error) {
207
+ console.log('ShoppingCart save', error);
208
+ }
209
+ return data;
210
+ }
211
+ /**
212
+ * Trigger update
213
+ * 触发更新
214
+ */
215
+ update() {
216
+ this.doChange('update', []);
217
+ }
218
+ /**
219
+ * Update discount
220
+ * @param item Shopping cart item
221
+ */
222
+ updateDiscount(item) {
223
+ item.discount = item.promotions.sum('amount');
224
+ }
225
+ /**
226
+ * Update item
227
+ * 更新项目
228
+ * @param id Product id
229
+ * @param qty Qty
230
+ * @param itemCreator New item creator
231
+ * @returns Updated or not
232
+ */
233
+ updateItem(id, qty, itemCreator) {
234
+ const index = this.items.findIndex((item) => item.id === id);
235
+ if (qty == null) {
236
+ // Remove the item
237
+ if (index !== -1) {
238
+ const removedItems = this.items.splice(index, 1);
239
+ this.doChange('remove', removedItems);
240
+ }
241
+ }
242
+ else if (index === -1) {
243
+ // New
244
+ if (itemCreator) {
245
+ const price = this.prices[id];
246
+ const newItem = {
247
+ ...itemCreator(),
248
+ id,
249
+ price,
250
+ qty,
251
+ subtotal: price * qty,
252
+ discount: 0
253
+ };
254
+ this.addItem(newItem);
255
+ }
256
+ return false;
257
+ }
258
+ else {
259
+ // Update
260
+ const item = this.items[index];
261
+ const price = item.price;
262
+ const newItem = {
263
+ ...item,
264
+ qty,
265
+ subtotal: price * qty,
266
+ discount: 0
267
+ };
268
+ this.items.splice(index, 1, newItem);
269
+ this.doChange('update', [item, newItem]);
270
+ }
271
+ return true;
272
+ }
273
+ /**
274
+ * Update price
275
+ * @param id Item id
276
+ * @param price New price
277
+ */
278
+ updatePrice(id, price) {
279
+ this.cachePrice(id, price);
280
+ const index = this.items.findIndex((item) => item.id === id);
281
+ if (index !== -1) {
282
+ const item = this.items[index];
283
+ const qty = item.qty;
284
+ const newItem = { ...item, price, subtotal: price * qty };
285
+ this.items.splice(index, 1, newItem);
286
+ this.doChange('update', [item, newItem]);
287
+ }
288
+ }
289
+ /**
290
+ * Update title
291
+ * @param id Item id
292
+ * @param title New title
293
+ */
294
+ updateTitle(id, title) {
295
+ const index = this.items.findIndex((item) => item.id === id);
296
+ if (index !== -1) {
297
+ const item = this.items[index];
298
+ const newItem = { ...item, title };
299
+ if (newItem.name === newItem.title)
300
+ newItem.title = undefined;
301
+ this.items.splice(index, 1, newItem);
302
+ this.doChange('title', [item, newItem]);
303
+ }
304
+ }
305
+ }
306
+ exports.ShoppingCart = ShoppingCart;
@@ -22,6 +22,7 @@ export * from './business/DataPrivacy';
22
22
  export * from './business/EntityStatus';
23
23
  export * from './business/ProductUnit';
24
24
  export * from './business/RepeatOption';
25
+ export * from './business/ShoppingCart';
25
26
  export * from './def/ListItem';
26
27
  export * from './erp/dto/AuditLineDto';
27
28
  export * from './erp/dto/CurrencyDto';
package/lib/cjs/index.js CHANGED
@@ -43,6 +43,7 @@ __exportStar(require("./business/DataPrivacy"), exports);
43
43
  __exportStar(require("./business/EntityStatus"), exports);
44
44
  __exportStar(require("./business/ProductUnit"), exports);
45
45
  __exportStar(require("./business/RepeatOption"), exports);
46
+ __exportStar(require("./business/ShoppingCart"), exports);
46
47
  // def
47
48
  __exportStar(require("./def/ListItem"), exports);
48
49
  // erp dto