@pisell/pisellos 0.10.96 → 0.10.97

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.
@@ -126,7 +126,6 @@ class Server {
126
126
  localPaymentUpdateQueues = new Map();
127
127
  paymentIdentityRefreshes = new Map();
128
128
  receiptFulfillmentConfigInFlight = new Map();
129
- cloudInvoiceSubmissions = new Map();
130
129
  floorPlanQuerySubscribers = new Map();
131
130
 
132
131
  // 模块注册表 - 定义所有可用的模块配置
@@ -4146,37 +4145,8 @@ class Server {
4146
4145
  external_sale_number: localOrder.external_sale_number ?? definedInput.external_sale_number ?? definedInput.externalSaleNumber
4147
4146
  };
4148
4147
  }
4149
- async getCloudInvoiceDevice(shopId) {
4150
- const readDevice = this.getAppData('async_iot_device_info');
4151
- const iot = await readDevice?.();
4152
- if (iot?.shop_id != null && String(iot.shop_id) !== shopId) {
4153
- throw new Error('Cloud invoice device belongs to another shop');
4154
- }
4155
- const iotId = String(iot?.id || '').trim();
4156
- const fullNumber = String(iot?.number || !iotId && this.getAppData('device_id') || '').trim();
4157
- if (fullNumber && fullNumber !== '0') {
4158
- return {
4159
- id: fullNumber,
4160
- target: iot?.short_number || fullNumber.slice(-2)
4161
- };
4162
- }
4163
- if (iotId && iotId !== '0') return {
4164
- id: 'iot-id:' + iotId,
4165
- target: iot?.short_number || iotId
4166
- };
4167
- throw new Error('Cloud invoice device is unavailable');
4168
- }
4169
- assertCloudInvoiceContext(context) {
4170
- if (String(this.getAppData('shop_id') ?? '') !== context.shopId || String(this.getAppData('tenant_id') ?? '') !== context.tenantId) {
4171
- throw new Error('Cloud invoice shop context changed');
4172
- }
4173
- }
4174
- async loadCloudInvoiceOrder(context) {
4148
+ async loadCloudInvoiceOrder(orderId, shopId, assertCurrentShop) {
4175
4149
  if (!this.order) throw new Error('Cloud invoice order service is unavailable');
4176
- const {
4177
- orderId,
4178
- shopId
4179
- } = context;
4180
4150
  const hasReceiptDetail = order => {
4181
4151
  // Use the same amount fields as the receipt builder, including historical
4182
4152
  // top-level amounts. Zero is a valid total; a missing total in a list row is not.
@@ -4186,7 +4156,7 @@ class Server {
4186
4156
  const local = await this.order.getLocalOrderByLookup(orderId, {
4187
4157
  exactOrderId: true
4188
4158
  });
4189
- this.assertCloudInvoiceContext(context);
4159
+ assertCurrentShop();
4190
4160
  if (local && String(local.order_id) === orderId) {
4191
4161
  if (local.shop_id != null && String(local.shop_id) !== shopId) {
4192
4162
  throw new Error('Cloud invoice order belongs to another shop');
@@ -4199,7 +4169,7 @@ class Server {
4199
4169
  const result = await this.fetchAndPersistSalesOrderByLookup(orderId, {
4200
4170
  with: ['all']
4201
4171
  }, 'cloudOrderInvoice', undefined, fresh => {
4202
- this.assertCloudInvoiceContext(context);
4172
+ assertCurrentShop();
4203
4173
  if (String(fresh.order_id) !== orderId || fresh.shop_id != null && String(fresh.shop_id) !== shopId || !hasReceiptDetail(fresh)) {
4204
4174
  throw new Error('Cloud invoice order identity or detail is invalid');
4205
4175
  }
@@ -4209,136 +4179,81 @@ class Server {
4209
4179
  shop_id: Number(shopId)
4210
4180
  } : fresh;
4211
4181
  });
4212
- this.assertCloudInvoiceContext(context);
4182
+ assertCurrentShop();
4213
4183
  const saved = result.order;
4214
4184
  if (!saved || String(saved.order_id) !== orderId || String(saved.shop_id) !== shopId || !hasReceiptDetail(saved)) {
4215
4185
  throw new Error(result.message || 'Cloud invoice order detail is unavailable');
4216
4186
  }
4217
4187
  return saved;
4218
4188
  }
4219
- async resolveCloudOrderInvoice(context, input, submit) {
4220
- const deviceTask = this.getDeviceTaskPlugin();
4221
- if (!deviceTask?.findByIdempotencyKey || !deviceTask.dispatch) {
4222
- throw new Error('Cloud invoice task service is unavailable');
4223
- }
4224
- const prior = await deviceTask.findByIdempotencyKey(context.taskKey);
4225
- this.assertCloudInvoiceContext(context);
4226
- if (prior) {
4227
- if (!prior.uuid || String(prior.payload?.data?.order_id) !== context.orderId || prior.payload?.channel !== context.channel) {
4228
- throw new Error('Cloud invoice message UUID conflicts with another task');
4229
- }
4230
- // Acceptance transfers ownership to DeviceTask, including its retry lifecycle.
4231
- return {
4232
- task_uuid: prior.uuid
4233
- };
4234
- }
4235
- const order = await this.loadCloudInvoiceOrder(context);
4236
- const businessCode = order.business_code.trim();
4237
- if (!submit) return {
4238
- business_code: businessCode,
4239
- device_id: context.deviceId
4240
- };
4241
- if (input.business_code !== businessCode || typeof input.template_settings?.template !== 'string' || !input.template_settings.template.trim() || !Array.isArray(input.template_settings.modules)) {
4242
- throw new Error('Cloud invoice template is missing or belongs to another business');
4243
- }
4244
- const settings = this.normalizePrintInvoiceTemplateSettings(input.template_settings);
4245
- const receipt = await this.rebuildOrderReceipt(order, settings);
4246
- const smallTicketData = receipt.payment_info?.small_ticket_data;
4247
- if (!(0, _smallTicket.hasSmallTicketData)(smallTicketData)) throw new Error('Cloud invoice snapshot is unavailable');
4248
- const device = await this.getCloudInvoiceDevice(context.shopId);
4249
- if (device.id !== context.deviceId) throw new Error('Cloud invoice device changed');
4250
- this.assertCloudInvoiceContext(context);
4251
- const task = await deviceTask.dispatch({
4252
- action: 'print_all',
4253
- device_id: device.target,
4254
- name: 'PRINT_V1',
4255
- source: 'server',
4256
- source_type: 'cloud_order_invoice',
4257
- source_id: order.order_id,
4258
- durability: 'durable',
4259
- idempotency_key: context.taskKey,
4260
- payload: {
4261
- type: '0',
4262
- shop_id: context.shopId,
4263
- device_id: context.deviceId,
4264
- message_uuid: context.messageUuid,
4265
- channel: context.channel,
4266
- template_settings: settings,
4267
- data: {
4268
- order_id: order.order_id,
4269
- small_ticket_data: smallTicketData
4270
- }
4271
- }
4272
- });
4273
- if (!task?.uuid) throw new Error('Cloud invoice task was not accepted');
4274
- return {
4275
- task_uuid: task.uuid
4276
- };
4277
- }
4278
4189
  async handleCloudOrderInvoice(input, submit) {
4279
- const orderId = String(input?.order_id ?? '').trim();
4280
- const shopId = String(input?.shop_id ?? '').trim();
4281
- const messageUuid = typeof input?.message_uuid === 'string' ? input.message_uuid.trim() : '';
4282
- const channel = typeof input?.channel === 'string' ? input.channel.trim() : '';
4283
- if (!/^[1-9]\d*$/.test(orderId) || !/^[1-9]\d*$/.test(shopId) || !Number.isSafeInteger(Number(shopId)) || typeof input.order_id === 'number' && !Number.isSafeInteger(input.order_id) || !messageUuid || !channel) {
4190
+ const orderId = typeof input?.order_id === 'string' ? input.order_id.trim() : Number.isSafeInteger(input?.order_id) ? String(input.order_id) : '';
4191
+ if (!/^[1-9]\d*$/.test(orderId)) {
4284
4192
  return {
4285
4193
  code: 400,
4286
4194
  status: false,
4287
- message: 'Invalid cloud invoice command',
4195
+ message: 'Invalid cloud invoice order ID',
4288
4196
  data: null
4289
4197
  };
4290
4198
  }
4291
4199
  try {
4200
+ const shopId = String(this.getAppData('shop_id') ?? '');
4292
4201
  const tenantId = String(this.getAppData('tenant_id') ?? '');
4293
- const {
4294
- id: deviceId
4295
- } = await this.getCloudInvoiceDevice(shopId);
4296
- if (submit && input.device_id !== deviceId) throw new Error('Cloud invoice device changed');
4297
- const taskKey = 'cloud_order_invoice:' + JSON.stringify([shopId, deviceId, messageUuid]);
4298
- const context = {
4299
- taskKey,
4300
- shopId,
4301
- tenantId,
4302
- deviceId,
4303
- orderId,
4304
- messageUuid,
4305
- channel
4306
- };
4307
- this.assertCloudInvoiceContext(context);
4308
- let result;
4309
- const pending = this.cloudInvoiceSubmissions.get(taskKey);
4310
- if (pending) {
4311
- if (pending.context.orderId !== orderId || pending.context.channel !== channel || pending.context.tenantId !== tenantId) {
4312
- throw new Error('Cloud invoice message UUID conflicts with an in-flight order');
4202
+ if (!/^[1-9]\d*$/.test(shopId) || !Number.isSafeInteger(Number(shopId))) {
4203
+ throw new Error('Cloud invoice shop is unavailable');
4204
+ }
4205
+ const assertCurrentShop = () => {
4206
+ if (String(this.getAppData('shop_id') ?? '') !== shopId || String(this.getAppData('tenant_id') ?? '') !== tenantId) {
4207
+ throw new Error('Cloud invoice shop context changed');
4313
4208
  }
4314
- result = await pending.result;
4315
- } else if (submit) {
4316
- const submission = this.resolveCloudOrderInvoice(context, input, true);
4317
- this.cloudInvoiceSubmissions.set(taskKey, {
4318
- context,
4319
- result: submission
4320
- });
4321
- try {
4322
- result = await submission;
4323
- } finally {
4324
- this.cloudInvoiceSubmissions.delete(taskKey);
4209
+ };
4210
+ const order = await this.loadCloudInvoiceOrder(orderId, shopId, assertCurrentShop);
4211
+ const businessCode = order.business_code.trim();
4212
+ if (!submit) return {
4213
+ code: 200,
4214
+ status: true,
4215
+ message: '',
4216
+ data: {
4217
+ business_code: businessCode
4325
4218
  }
4326
- } else {
4327
- result = await this.resolveCloudOrderInvoice(context, input, false);
4219
+ };
4220
+ if (input.business_code !== businessCode || typeof input.template_settings?.template !== 'string' || !input.template_settings.template.trim() || !Array.isArray(input.template_settings.modules)) {
4221
+ throw new Error('Cloud invoice template is missing or belongs to another business');
4328
4222
  }
4223
+ const settings = this.normalizePrintInvoiceTemplateSettings(input.template_settings);
4224
+ const receipt = await this.rebuildOrderReceipt(order, settings);
4225
+ const smallTicketData = receipt.payment_info?.small_ticket_data;
4226
+ if (!(0, _smallTicket.hasSmallTicketData)(smallTicketData)) throw new Error('Cloud invoice snapshot is unavailable');
4227
+ const deviceTask = this.getDeviceTaskPlugin();
4228
+ if (!deviceTask?.dispatch) throw new Error('Print task service is unavailable');
4229
+ const deviceId = await this.getShortNumberOrDeviceId();
4230
+ assertCurrentShop();
4231
+ await deviceTask.dispatch({
4232
+ action: 'print_all',
4233
+ device_id: deviceId,
4234
+ name: 'PRINT_V1',
4235
+ source: 'server',
4236
+ source_type: 'order',
4237
+ source_id: order.order_id,
4238
+ payload: {
4239
+ type: '0',
4240
+ template_settings: settings,
4241
+ data: {
4242
+ order_id: order.order_id,
4243
+ small_ticket_data: smallTicketData
4244
+ }
4245
+ }
4246
+ });
4329
4247
  return {
4330
4248
  code: 200,
4331
4249
  status: true,
4332
4250
  message: '',
4333
- data: result
4251
+ data: null
4334
4252
  };
4335
4253
  } catch (error) {
4336
4254
  const message = this.getUnknownErrorMessage(error);
4337
4255
  this.logWarning('Cloud invoice request failed', {
4338
- shopId,
4339
4256
  orderId,
4340
- messageUuid,
4341
- channel,
4342
4257
  message
4343
4258
  });
4344
4259
  return {
@@ -342,7 +342,7 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
342
342
  date: string;
343
343
  status: string;
344
344
  week: string;
345
- weekNum: 0 | 5 | 1 | 2 | 4 | 3 | 6;
345
+ weekNum: 0 | 1 | 2 | 5 | 4 | 3 | 6;
346
346
  }[]>;
347
347
  submitTimeSlot(timeSlots: TimeSliceItem): void;
348
348
  private getScheduleDataByIds;
@@ -2,7 +2,7 @@ import dayjs from 'dayjs';
2
2
  import { Module, ModuleOptions, PisellCore } from '../../types';
3
3
  import { BaseModule } from '../../modules/BaseModule';
4
4
  import type { RequestPlugin } from '../../plugins/request';
5
- import { SalesState, SalesModuleAPI, Reservation, SalesResourceBookingItem, SalesResourceBookingItemWithGroups, SalesTimelineHighlights, ChangeBookingStatusParams, GetResourceBookingItemParams, SalesPrintLocalOrderReceiptOptions, CloudOrderInvoiceRequest, CloudOrderInvoiceResult } from './types';
5
+ import { SalesState, SalesModuleAPI, Reservation, SalesResourceBookingItem, SalesResourceBookingItemWithGroups, SalesTimelineHighlights, ChangeBookingStatusParams, GetResourceBookingItemParams, SalesPrintLocalOrderReceiptOptions, CloudOrderInvoiceRequest } from './types';
6
6
  import type { BookingData } from '../../server/modules/order/types';
7
7
  export * from './types';
8
8
  /**
@@ -55,7 +55,7 @@ export declare class SalesImpl extends BaseModule implements Module, SalesModule
55
55
  private logInvoiceOpenDataFallback;
56
56
  private resolveInvoiceConfigs;
57
57
  printLocalOrderReceipt<T = any>(lookup: string | number, options: SalesPrintLocalOrderReceiptOptions): Promise<T>;
58
- printCloudOrderInvoice(input: CloudOrderInvoiceRequest): Promise<CloudOrderInvoiceResult>;
58
+ printCloudOrderInvoice(input: CloudOrderInvoiceRequest): Promise<void>;
59
59
  /** 清除独立 Documents 缓存;下一次打印会按当前 target 重新获取。 */
60
60
  clearOpenDataCache(): void;
61
61
  destroy(): Promise<void>;
@@ -28,7 +28,6 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
28
28
  const UNPAID_PAYMENT_STATUSES = new Set(['unpaid', 'partially_paid', 'payment_processing']);
29
29
  const INVOICE_OPEN_DATA_SECTION_CODES = ['documents'];
30
30
  const INVOICE_OPEN_DATA_CACHE_TTL = 5 * 60 * 1000;
31
-
32
31
  /**
33
32
  * Sales 解决方案基础骨架
34
33
  *
@@ -288,57 +287,32 @@ class SalesImpl extends _BaseModule.BaseModule {
288
287
  });
289
288
  }
290
289
  async printCloudOrderInvoice(input) {
290
+ const orderId = input?.order_id;
291
+ const channel = typeof input?.channel === 'string' ? input.channel.trim() : '';
292
+ if (!channel) throw new Error('Cloud Invoice requires a channel');
291
293
  const openData = this.invoiceOpenDataModule;
292
294
  if (!openData) throw new Error('Cloud Invoice is not initialized');
293
- const orderId = typeof input?.order_id === 'string' ? input.order_id.trim() : Number.isSafeInteger(input?.order_id) ? String(input.order_id) : '';
294
- const channel = typeof input?.channel === 'string' ? input.channel.trim() : '';
295
- const messageUuid = typeof input?.message_uuid === 'string' ? input.message_uuid.trim() : '';
296
- if (!/^[1-9]\d*$/.test(orderId) || !channel || !messageUuid) {
297
- throw new Error('Cloud Invoice requires order_id, channel and message_uuid');
298
- }
299
- const scope = openData.captureCloudInvoiceScope(input.shop_id);
300
- const event = {
301
- order_id: orderId,
302
- channel,
303
- message_uuid: messageUuid,
304
- shop_id: scope.shopId
305
- };
306
295
  const requestOptions = {
307
296
  osServer: true,
308
297
  customToast: () => {}
309
298
  };
310
- const lookup = await this.request.post('/shop/local/cloud-order-invoice/lookup', event, requestOptions);
311
- scope.assertCurrent();
299
+ const lookup = await this.request.post('/shop/local/cloud-order-invoice/lookup', {
300
+ order_id: orderId
301
+ }, requestOptions);
312
302
  if (lookup?.status !== true || lookup.code !== 200) {
313
303
  throw new Error(lookup?.message || 'Cloud Invoice order lookup failed');
314
304
  }
315
- const accepted = lookup.data?.task_uuid;
316
- if (typeof accepted === 'string' && accepted.trim()) return {
317
- task_uuid: accepted
318
- };
319
- const rawBusinessCode = lookup.data?.business_code;
320
- const businessCode = typeof rawBusinessCode === 'string' ? rawBusinessCode.trim() : '';
305
+ const businessCode = lookup.data?.business_code?.trim();
321
306
  if (!businessCode) throw new Error('Cloud Invoice order has no business_code');
322
- const deviceId = lookup.data?.device_id;
323
- if (typeof deviceId !== 'string' || !deviceId.trim()) {
324
- throw new Error('Cloud Invoice lookup has no device_id');
325
- }
326
- const settings = await openData.loadCloudInvoiceTemplate(`${businessCode}+${channel}`, scope);
327
- scope.assertCurrent();
328
- const submitted = await this.request.post('/shop/local/cloud-order-invoice/submit', {
329
- ...event,
307
+ const settings = await openData.loadCloudInvoiceTemplate(`${businessCode}+${channel}`);
308
+ const result = await this.request.post('/shop/local/cloud-order-invoice/submit', {
309
+ order_id: orderId,
330
310
  business_code: businessCode,
331
- device_id: deviceId,
332
- template_settings: (0, _lodashEs.cloneDeep)(settings)
311
+ template_settings: settings
333
312
  }, requestOptions);
334
- scope.assertCurrent();
335
- const taskUuid = submitted?.data?.task_uuid;
336
- if (submitted?.status !== true || submitted.code !== 200 || typeof taskUuid !== 'string' || !taskUuid.trim()) {
337
- throw new Error(submitted?.message || 'Cloud Invoice task was not accepted');
313
+ if (result?.status !== true || result.code !== 200) {
314
+ throw new Error(result?.message || 'Cloud Invoice dispatch failed');
338
315
  }
339
- return {
340
- task_uuid: taskUuid
341
- };
342
316
  }
343
317
 
344
318
  /** 清除独立 Documents 缓存;下一次打印会按当前 target 重新获取。 */
@@ -56,17 +56,11 @@ export interface SalesPrintLocalOrderReceiptOptions {
56
56
  /** 当前销售入口;缺失或为 system 时按 pos 处理。 */
57
57
  channel?: string;
58
58
  }
59
- /** Order and delivery identity supplied by the native cloud-print listener. */
59
+ /** Native cloud-order notification. */
60
60
  export interface CloudOrderInvoiceRequest {
61
- shop_id: string | number;
62
61
  order_id: string | number;
63
- message_uuid: string;
64
62
  channel: string;
65
63
  }
66
- export interface CloudOrderInvoiceResult {
67
- /** DeviceTask accepted for this message on the current device. */
68
- task_uuid: string;
69
- }
70
64
  /**
71
65
  * Sales 解决方案状态
72
66
  *
@@ -101,7 +95,7 @@ export interface SalesModuleAPI {
101
95
  changeBookingStatus: (params: ChangeBookingStatusParams) => Promise<any>;
102
96
  /** 按本地订单标识打印小票;本地未命中时由 OS Server 负责远端兜底。 */
103
97
  printLocalOrderReceipt: <T = any>(lookup: string | number, options: SalesPrintLocalOrderReceiptOptions) => Promise<T>;
104
- printCloudOrderInvoice: (request: CloudOrderInvoiceRequest) => Promise<CloudOrderInvoiceResult>;
98
+ printCloudOrderInvoice: (request: CloudOrderInvoiceRequest) => Promise<void>;
105
99
  /** 清除 Legacy Sales 独立维护的 Documents OpenData 缓存。 */
106
100
  clearOpenDataCache: () => void;
107
101
  /** 获取资源维度的预约占用列表 */
@@ -18,7 +18,7 @@ let SalesHooks = exports.SalesHooks = /*#__PURE__*/function (SalesHooks) {
18
18
  * 将 Server/Order 原始数据转换为真正的 Reservation DTO,
19
19
  * 当前不要直接把 OrderData 当作最终 UI 数据结构。
20
20
  */
21
- /** Order and delivery identity supplied by the native cloud-print listener. */
21
+ /** Native cloud-order notification. */
22
22
  /**
23
23
  * Sales 解决方案状态
24
24
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "0.10.96",
4
+ "version": "0.10.97",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",