@open-mercato/core 0.10.1-develop.7709.1.046124599c → 0.10.1-develop.7714.1.9dbb8200d8

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.
Files changed (276) hide show
  1. package/.turbo/turbo-build.log +2 -2
  2. package/dist/generated/entities/asn/index.js +19 -0
  3. package/dist/generated/entities/asn/index.js.map +7 -0
  4. package/dist/generated/entities/putaway_task/index.js +25 -0
  5. package/dist/generated/entities/putaway_task/index.js.map +7 -0
  6. package/dist/generated/entities/receiving_line/index.js +21 -0
  7. package/dist/generated/entities/receiving_line/index.js.map +7 -0
  8. package/dist/generated/entities.ids.generated.js +4 -1
  9. package/dist/generated/entities.ids.generated.js.map +2 -2
  10. package/dist/generated/entity-fields-registry.js +34 -0
  11. package/dist/generated/entity-fields-registry.js.map +2 -2
  12. package/dist/modules/communication_channels/lib/sanitize-channel-html.js +22 -3
  13. package/dist/modules/communication_channels/lib/sanitize-channel-html.js.map +2 -2
  14. package/dist/modules/communication_channels/widgets/injection/channel-payload-renderer/widget.client.js +40 -11
  15. package/dist/modules/communication_channels/widgets/injection/channel-payload-renderer/widget.client.js.map +2 -2
  16. package/dist/modules/customers/components/calendar/CalendarEventEditor.js +13 -2
  17. package/dist/modules/customers/components/calendar/CalendarEventEditor.js.map +2 -2
  18. package/dist/modules/wms/acl.js +2 -0
  19. package/dist/modules/wms/acl.js.map +2 -2
  20. package/dist/modules/wms/api/asns/[id]/complete/route.js +61 -0
  21. package/dist/modules/wms/api/asns/[id]/complete/route.js.map +7 -0
  22. package/dist/modules/wms/api/asns/[id]/receive/route.js +68 -0
  23. package/dist/modules/wms/api/asns/[id]/receive/route.js.map +7 -0
  24. package/dist/modules/wms/api/asns/route.js +197 -0
  25. package/dist/modules/wms/api/asns/route.js.map +7 -0
  26. package/dist/modules/wms/api/inventory/helpers.js +42 -34
  27. package/dist/modules/wms/api/inventory/helpers.js.map +2 -2
  28. package/dist/modules/wms/api/inventory/receive/route.js +1 -1
  29. package/dist/modules/wms/api/inventory/receive/route.js.map +2 -2
  30. package/dist/modules/wms/api/listEnrichers.js +23 -0
  31. package/dist/modules/wms/api/listEnrichers.js.map +2 -2
  32. package/dist/modules/wms/api/putaway-tasks/[id]/assign/route.js +56 -0
  33. package/dist/modules/wms/api/putaway-tasks/[id]/assign/route.js.map +7 -0
  34. package/dist/modules/wms/api/putaway-tasks/[id]/cancel/route.js +56 -0
  35. package/dist/modules/wms/api/putaway-tasks/[id]/cancel/route.js.map +7 -0
  36. package/dist/modules/wms/api/putaway-tasks/[id]/complete/route.js +65 -0
  37. package/dist/modules/wms/api/putaway-tasks/[id]/complete/route.js.map +7 -0
  38. package/dist/modules/wms/api/putaway-tasks/[id]/start/route.js +56 -0
  39. package/dist/modules/wms/api/putaway-tasks/[id]/start/route.js.map +7 -0
  40. package/dist/modules/wms/api/putaway-tasks/create-from-balance/route.js +51 -0
  41. package/dist/modules/wms/api/putaway-tasks/create-from-balance/route.js.map +7 -0
  42. package/dist/modules/wms/api/putaway-tasks/route.js +192 -0
  43. package/dist/modules/wms/api/putaway-tasks/route.js.map +7 -0
  44. package/dist/modules/wms/api/receiving-lines/route.js +180 -0
  45. package/dist/modules/wms/api/receiving-lines/route.js.map +7 -0
  46. package/dist/modules/wms/api/scan/putaway/route.js +104 -0
  47. package/dist/modules/wms/api/scan/putaway/route.js.map +7 -0
  48. package/dist/modules/wms/api/scan/receive/route.js +109 -0
  49. package/dist/modules/wms/api/scan/receive/route.js.map +7 -0
  50. package/dist/modules/wms/api/scan/resolve-location/route.js +89 -0
  51. package/dist/modules/wms/api/scan/resolve-location/route.js.map +7 -0
  52. package/dist/modules/wms/api/scan/resolve-lot/route.js +89 -0
  53. package/dist/modules/wms/api/scan/resolve-lot/route.js.map +7 -0
  54. package/dist/modules/wms/backend/wms/asns/[id]/page.js +9 -0
  55. package/dist/modules/wms/backend/wms/asns/[id]/page.js.map +7 -0
  56. package/dist/modules/wms/backend/wms/asns/[id]/page.meta.js +18 -0
  57. package/dist/modules/wms/backend/wms/asns/[id]/page.meta.js.map +7 -0
  58. package/dist/modules/wms/backend/wms/asns/page.js +9 -0
  59. package/dist/modules/wms/backend/wms/asns/page.js.map +7 -0
  60. package/dist/modules/wms/backend/wms/asns/page.meta.js +18 -0
  61. package/dist/modules/wms/backend/wms/asns/page.meta.js.map +7 -0
  62. package/dist/modules/wms/backend/wms/putaway/page.js +9 -0
  63. package/dist/modules/wms/backend/wms/putaway/page.js.map +7 -0
  64. package/dist/modules/wms/backend/wms/putaway/page.meta.js +18 -0
  65. package/dist/modules/wms/backend/wms/putaway/page.meta.js.map +7 -0
  66. package/dist/modules/wms/backend/wms/receiving/page.js +8 -0
  67. package/dist/modules/wms/backend/wms/receiving/page.js.map +7 -0
  68. package/dist/modules/wms/backend/wms/receiving/page.meta.js +18 -0
  69. package/dist/modules/wms/backend/wms/receiving/page.meta.js.map +7 -0
  70. package/dist/modules/wms/ce.js +27 -0
  71. package/dist/modules/wms/ce.js.map +2 -2
  72. package/dist/modules/wms/commands/asn.js +1746 -0
  73. package/dist/modules/wms/commands/asn.js.map +7 -0
  74. package/dist/modules/wms/commands/index.js +2 -0
  75. package/dist/modules/wms/commands/index.js.map +2 -2
  76. package/dist/modules/wms/commands/inventory-actions.js +347 -243
  77. package/dist/modules/wms/commands/inventory-actions.js.map +2 -2
  78. package/dist/modules/wms/commands/putaway.js +945 -0
  79. package/dist/modules/wms/commands/putaway.js.map +7 -0
  80. package/dist/modules/wms/commands/shared.js +51 -0
  81. package/dist/modules/wms/commands/shared.js.map +2 -2
  82. package/dist/modules/wms/components/backend/AssignPutawayTaskDialog.js +278 -0
  83. package/dist/modules/wms/components/backend/AssignPutawayTaskDialog.js.map +7 -0
  84. package/dist/modules/wms/components/backend/CompletePutawayTaskDialog.js +327 -0
  85. package/dist/modules/wms/components/backend/CompletePutawayTaskDialog.js.map +7 -0
  86. package/dist/modules/wms/components/backend/CreateAsnDialog.js +404 -0
  87. package/dist/modules/wms/components/backend/CreateAsnDialog.js.map +7 -0
  88. package/dist/modules/wms/components/backend/InventoryOperationsSection.js +16 -3
  89. package/dist/modules/wms/components/backend/InventoryOperationsSection.js.map +2 -2
  90. package/dist/modules/wms/components/backend/ReceiveAsnLineDialog.js +476 -0
  91. package/dist/modules/wms/components/backend/ReceiveAsnLineDialog.js.map +7 -0
  92. package/dist/modules/wms/components/backend/ReceiveInventoryDialog.js +2 -2
  93. package/dist/modules/wms/components/backend/ReceiveInventoryDialog.js.map +2 -2
  94. package/dist/modules/wms/components/backend/WmsAsnDetailPage.js +524 -0
  95. package/dist/modules/wms/components/backend/WmsAsnDetailPage.js.map +7 -0
  96. package/dist/modules/wms/components/backend/WmsAsnsListPage.js +339 -0
  97. package/dist/modules/wms/components/backend/WmsAsnsListPage.js.map +7 -0
  98. package/dist/modules/wms/components/backend/WmsLocationDetailPage.js +5 -6
  99. package/dist/modules/wms/components/backend/WmsLocationDetailPage.js.map +2 -2
  100. package/dist/modules/wms/components/backend/WmsLotDetailPage.js +5 -6
  101. package/dist/modules/wms/components/backend/WmsLotDetailPage.js.map +2 -2
  102. package/dist/modules/wms/components/backend/WmsOperationalDashboardPage.js +14 -10
  103. package/dist/modules/wms/components/backend/WmsOperationalDashboardPage.js.map +2 -2
  104. package/dist/modules/wms/components/backend/WmsPutawayQueuePage.js +598 -0
  105. package/dist/modules/wms/components/backend/WmsPutawayQueuePage.js.map +7 -0
  106. package/dist/modules/wms/components/backend/WmsSkuDetailPage.js +5 -6
  107. package/dist/modules/wms/components/backend/WmsSkuDetailPage.js.map +2 -2
  108. package/dist/modules/wms/components/backend/asnCompleteOptimisticLock.js +32 -0
  109. package/dist/modules/wms/components/backend/asnCompleteOptimisticLock.js.map +7 -0
  110. package/dist/modules/wms/components/backend/asnReceivingLinesLoader.js +36 -0
  111. package/dist/modules/wms/components/backend/asnReceivingLinesLoader.js.map +7 -0
  112. package/dist/modules/wms/components/backend/inboundStatusUi.js +106 -0
  113. package/dist/modules/wms/components/backend/inboundStatusUi.js.map +7 -0
  114. package/dist/modules/wms/components/backend/inventoryMutationLoaders.js +17 -0
  115. package/dist/modules/wms/components/backend/inventoryMutationLoaders.js.map +2 -2
  116. package/dist/modules/wms/components/backend/putawayQueueOptimisticLock.js +14 -0
  117. package/dist/modules/wms/components/backend/putawayQueueOptimisticLock.js.map +7 -0
  118. package/dist/modules/wms/components/backend/useWmsInventoryMutationAccess.js +4 -0
  119. package/dist/modules/wms/components/backend/useWmsInventoryMutationAccess.js.map +2 -2
  120. package/dist/modules/wms/data/enrichers.js +117 -12
  121. package/dist/modules/wms/data/enrichers.js.map +2 -2
  122. package/dist/modules/wms/data/entities.js +150 -1
  123. package/dist/modules/wms/data/entities.js.map +2 -2
  124. package/dist/modules/wms/data/validators.js +214 -0
  125. package/dist/modules/wms/data/validators.js.map +2 -2
  126. package/dist/modules/wms/events.js +12 -1
  127. package/dist/modules/wms/events.js.map +2 -2
  128. package/dist/modules/wms/extension-points.js +3 -1
  129. package/dist/modules/wms/extension-points.js.map +2 -2
  130. package/dist/modules/wms/lib/asnReceiving.js +124 -0
  131. package/dist/modules/wms/lib/asnReceiving.js.map +7 -0
  132. package/dist/modules/wms/lib/asnVendor.js +52 -0
  133. package/dist/modules/wms/lib/asnVendor.js.map +7 -0
  134. package/dist/modules/wms/lib/flashMutationError.js +4 -0
  135. package/dist/modules/wms/lib/flashMutationError.js.map +2 -2
  136. package/dist/modules/wms/lib/inventoryDisplayUi.js +24 -0
  137. package/dist/modules/wms/lib/inventoryDisplayUi.js.map +2 -2
  138. package/dist/modules/wms/lib/inventoryPolicy.js +17 -0
  139. package/dist/modules/wms/lib/inventoryPolicy.js.map +2 -2
  140. package/dist/modules/wms/lib/loadOperationalDashboard.js +67 -1
  141. package/dist/modules/wms/lib/loadOperationalDashboard.js.map +2 -2
  142. package/dist/modules/wms/lib/putaway.js +141 -0
  143. package/dist/modules/wms/lib/putaway.js.map +7 -0
  144. package/dist/modules/wms/lib/roleFeatures.js +3 -1
  145. package/dist/modules/wms/lib/roleFeatures.js.map +2 -2
  146. package/dist/modules/wms/lib/salesOrderInventoryAutomation.js +99 -1
  147. package/dist/modules/wms/lib/salesOrderInventoryAutomation.js.map +2 -2
  148. package/dist/modules/wms/lib/scanResolve.js +147 -0
  149. package/dist/modules/wms/lib/scanResolve.js.map +7 -0
  150. package/dist/modules/wms/lib/wmsIntegrationToggles.js +1 -1
  151. package/dist/modules/wms/lib/wmsIntegrationToggles.js.map +2 -2
  152. package/dist/modules/wms/migrations/Migration20260812120000.js +43 -0
  153. package/dist/modules/wms/migrations/Migration20260812120000.js.map +7 -0
  154. package/dist/modules/wms/migrations/Migration20260812130000.js +30 -0
  155. package/dist/modules/wms/migrations/Migration20260812130000.js.map +7 -0
  156. package/dist/modules/wms/migrations/Migration20260822120000.js +17 -0
  157. package/dist/modules/wms/migrations/Migration20260822120000.js.map +7 -0
  158. package/dist/modules/wms/migrations/Migration20260822130000.js +17 -0
  159. package/dist/modules/wms/migrations/Migration20260822130000.js.map +7 -0
  160. package/dist/modules/wms/migrations/Migration20261029120000.js +13 -0
  161. package/dist/modules/wms/migrations/Migration20261029120000.js.map +7 -0
  162. package/dist/modules/wms/search.js +85 -0
  163. package/dist/modules/wms/search.js.map +2 -2
  164. package/dist/modules/wms/subscribers/asn-line-received-reevaluate-reservations.js +14 -0
  165. package/dist/modules/wms/subscribers/asn-line-received-reevaluate-reservations.js.map +7 -0
  166. package/dist/modules/wms/subscribers/invalidate-enricher-cache-asn.js +12 -0
  167. package/dist/modules/wms/subscribers/invalidate-enricher-cache-asn.js.map +7 -0
  168. package/dist/modules/wms/subscribers/procurement-goods-receipt-created.js +163 -0
  169. package/dist/modules/wms/subscribers/procurement-goods-receipt-created.js.map +7 -0
  170. package/dist/modules/wms/subscribers/putaway-completed-reevaluate-reservations.js +14 -0
  171. package/dist/modules/wms/subscribers/putaway-completed-reevaluate-reservations.js.map +7 -0
  172. package/dist/modules/wms/widgets/injection/sales-order-stock-context/widget.client.js +37 -8
  173. package/dist/modules/wms/widgets/injection/sales-order-stock-context/widget.client.js.map +2 -2
  174. package/generated/entities/asn/index.ts +8 -0
  175. package/generated/entities/putaway_task/index.ts +11 -0
  176. package/generated/entities/receiving_line/index.ts +9 -0
  177. package/generated/entities.ids.generated.ts +4 -1
  178. package/generated/entity-fields-registry.ts +34 -0
  179. package/package.json +7 -7
  180. package/src/modules/auth/i18n/de.json +2 -0
  181. package/src/modules/auth/i18n/en.json +2 -0
  182. package/src/modules/auth/i18n/es.json +2 -0
  183. package/src/modules/auth/i18n/ko.json +2 -0
  184. package/src/modules/auth/i18n/pl.json +2 -0
  185. package/src/modules/communication_channels/i18n/de.json +2 -0
  186. package/src/modules/communication_channels/i18n/en.json +2 -0
  187. package/src/modules/communication_channels/i18n/es.json +2 -0
  188. package/src/modules/communication_channels/i18n/ko.json +2 -0
  189. package/src/modules/communication_channels/i18n/pl.json +2 -0
  190. package/src/modules/communication_channels/lib/sanitize-channel-html.ts +23 -3
  191. package/src/modules/communication_channels/widgets/injection/channel-payload-renderer/widget.client.tsx +44 -12
  192. package/src/modules/customers/components/calendar/CalendarEventEditor.tsx +16 -2
  193. package/src/modules/wms/acl.ts +2 -0
  194. package/src/modules/wms/api/asns/[id]/complete/route.ts +62 -0
  195. package/src/modules/wms/api/asns/[id]/receive/route.ts +74 -0
  196. package/src/modules/wms/api/asns/route.ts +200 -0
  197. package/src/modules/wms/api/inventory/helpers.ts +57 -34
  198. package/src/modules/wms/api/inventory/receive/route.ts +2 -1
  199. package/src/modules/wms/api/listEnrichers.ts +38 -0
  200. package/src/modules/wms/api/putaway-tasks/[id]/assign/route.ts +56 -0
  201. package/src/modules/wms/api/putaway-tasks/[id]/cancel/route.ts +56 -0
  202. package/src/modules/wms/api/putaway-tasks/[id]/complete/route.ts +66 -0
  203. package/src/modules/wms/api/putaway-tasks/[id]/start/route.ts +56 -0
  204. package/src/modules/wms/api/putaway-tasks/create-from-balance/route.ts +50 -0
  205. package/src/modules/wms/api/putaway-tasks/route.ts +199 -0
  206. package/src/modules/wms/api/receiving-lines/route.ts +180 -0
  207. package/src/modules/wms/api/scan/putaway/route.ts +108 -0
  208. package/src/modules/wms/api/scan/receive/route.ts +118 -0
  209. package/src/modules/wms/api/scan/resolve-location/route.ts +92 -0
  210. package/src/modules/wms/api/scan/resolve-lot/route.ts +92 -0
  211. package/src/modules/wms/backend/wms/asns/[id]/page.meta.ts +14 -0
  212. package/src/modules/wms/backend/wms/asns/[id]/page.tsx +5 -0
  213. package/src/modules/wms/backend/wms/asns/page.meta.ts +14 -0
  214. package/src/modules/wms/backend/wms/asns/page.tsx +5 -0
  215. package/src/modules/wms/backend/wms/putaway/page.meta.ts +14 -0
  216. package/src/modules/wms/backend/wms/putaway/page.tsx +5 -0
  217. package/src/modules/wms/backend/wms/receiving/page.meta.ts +14 -0
  218. package/src/modules/wms/backend/wms/receiving/page.tsx +5 -0
  219. package/src/modules/wms/ce.ts +27 -0
  220. package/src/modules/wms/commands/asn.ts +2122 -0
  221. package/src/modules/wms/commands/index.ts +2 -0
  222. package/src/modules/wms/commands/inventory-actions.ts +413 -243
  223. package/src/modules/wms/commands/putaway.ts +1169 -0
  224. package/src/modules/wms/commands/shared.ts +51 -0
  225. package/src/modules/wms/components/backend/AssignPutawayTaskDialog.tsx +328 -0
  226. package/src/modules/wms/components/backend/CompletePutawayTaskDialog.tsx +366 -0
  227. package/src/modules/wms/components/backend/CreateAsnDialog.tsx +427 -0
  228. package/src/modules/wms/components/backend/InventoryOperationsSection.tsx +20 -2
  229. package/src/modules/wms/components/backend/ReceiveAsnLineDialog.tsx +535 -0
  230. package/src/modules/wms/components/backend/ReceiveInventoryDialog.tsx +2 -2
  231. package/src/modules/wms/components/backend/WmsAsnDetailPage.tsx +647 -0
  232. package/src/modules/wms/components/backend/WmsAsnsListPage.tsx +407 -0
  233. package/src/modules/wms/components/backend/WmsLocationDetailPage.tsx +4 -5
  234. package/src/modules/wms/components/backend/WmsLotDetailPage.tsx +4 -5
  235. package/src/modules/wms/components/backend/WmsOperationalDashboardPage.tsx +12 -8
  236. package/src/modules/wms/components/backend/WmsPutawayQueuePage.tsx +677 -0
  237. package/src/modules/wms/components/backend/WmsSkuDetailPage.tsx +4 -5
  238. package/src/modules/wms/components/backend/asnCompleteOptimisticLock.ts +52 -0
  239. package/src/modules/wms/components/backend/asnReceivingLinesLoader.ts +83 -0
  240. package/src/modules/wms/components/backend/inboundStatusUi.ts +154 -0
  241. package/src/modules/wms/components/backend/inventoryMutationLoaders.ts +20 -0
  242. package/src/modules/wms/components/backend/putawayQueueOptimisticLock.ts +20 -0
  243. package/src/modules/wms/components/backend/useWmsInventoryMutationAccess.ts +4 -0
  244. package/src/modules/wms/data/enrichers.ts +175 -13
  245. package/src/modules/wms/data/entities.ts +155 -0
  246. package/src/modules/wms/data/validators.ts +256 -0
  247. package/src/modules/wms/events.ts +11 -0
  248. package/src/modules/wms/extension-points.ts +2 -0
  249. package/src/modules/wms/i18n/de.json +205 -3
  250. package/src/modules/wms/i18n/en.json +205 -3
  251. package/src/modules/wms/i18n/es.json +205 -3
  252. package/src/modules/wms/i18n/ko.json +205 -3
  253. package/src/modules/wms/i18n/pl.json +205 -3
  254. package/src/modules/wms/lib/asnReceiving.ts +226 -0
  255. package/src/modules/wms/lib/asnVendor.ts +80 -0
  256. package/src/modules/wms/lib/flashMutationError.ts +4 -0
  257. package/src/modules/wms/lib/inventoryDisplayUi.ts +46 -0
  258. package/src/modules/wms/lib/inventoryPolicy.ts +31 -1
  259. package/src/modules/wms/lib/loadOperationalDashboard.ts +89 -1
  260. package/src/modules/wms/lib/putaway.ts +210 -0
  261. package/src/modules/wms/lib/roleFeatures.ts +2 -0
  262. package/src/modules/wms/lib/salesOrderInventoryAutomation.ts +150 -1
  263. package/src/modules/wms/lib/scanResolve.ts +185 -0
  264. package/src/modules/wms/lib/wmsIntegrationToggles.ts +2 -1
  265. package/src/modules/wms/migrations/.snapshot-open-mercato.json +1000 -0
  266. package/src/modules/wms/migrations/Migration20260812120000.ts +43 -0
  267. package/src/modules/wms/migrations/Migration20260812130000.ts +28 -0
  268. package/src/modules/wms/migrations/Migration20260822120000.ts +17 -0
  269. package/src/modules/wms/migrations/Migration20260822130000.ts +15 -0
  270. package/src/modules/wms/migrations/Migration20261029120000.ts +11 -0
  271. package/src/modules/wms/search.ts +95 -0
  272. package/src/modules/wms/subscribers/asn-line-received-reevaluate-reservations.ts +21 -0
  273. package/src/modules/wms/subscribers/invalidate-enricher-cache-asn.ts +12 -0
  274. package/src/modules/wms/subscribers/procurement-goods-receipt-created.ts +238 -0
  275. package/src/modules/wms/subscribers/putaway-completed-reevaluate-reservations.ts +21 -0
  276. package/src/modules/wms/widgets/injection/sales-order-stock-context/widget.client.tsx +63 -13
@@ -0,0 +1,366 @@
1
+ "use client"
2
+
3
+ import * as React from 'react'
4
+ import { z } from 'zod'
5
+ import { useQueryClient } from '@tanstack/react-query'
6
+ import { flash } from '@open-mercato/ui/backend/FlashMessages'
7
+ import { flashMutationError } from '../../lib/flashMutationError'
8
+ import { useGuardedMutation } from '@open-mercato/ui/backend/injection/useGuardedMutation'
9
+ import { ComboboxInput } from '@open-mercato/ui/backend/inputs/ComboboxInput'
10
+ import { apiCall } from '@open-mercato/ui/backend/utils/apiCall'
11
+ import {
12
+ buildOptimisticLockHeader,
13
+ extractOptimisticLockConflict,
14
+ } from '@open-mercato/ui/backend/utils/optimisticLock'
15
+ import { raiseCrudError } from '@open-mercato/ui/backend/utils/serverErrors'
16
+ import { surfaceRecordConflict } from '@open-mercato/ui/backend/conflicts'
17
+ import { Button } from '@open-mercato/ui/primitives/button'
18
+ import {
19
+ Dialog,
20
+ DialogContent,
21
+ DialogDescription,
22
+ DialogFooter,
23
+ DialogHeader,
24
+ DialogTitle,
25
+ } from '@open-mercato/ui/primitives/dialog'
26
+ import { FormField } from '@open-mercato/ui/primitives/form-field'
27
+ import { Input } from '@open-mercato/ui/primitives/input'
28
+ import { KbdShortcut } from '@open-mercato/ui/primitives/kbd'
29
+ import { useT } from '@open-mercato/shared/lib/i18n/context'
30
+ import {
31
+ loadBinLocationOptions,
32
+ resolveLocationLabel,
33
+ } from './inventoryMutationLoaders'
34
+ import type { WmsInventoryMutationAccess } from './useWmsInventoryMutationAccess'
35
+
36
+ export type PutawayTaskTarget = {
37
+ id: string
38
+ warehouseId: string
39
+ quantity: number
40
+ targetLocationId?: string | null
41
+ catalogVariantId?: string | null
42
+ sourceLocationId?: string | null
43
+ serialNumber?: string | null
44
+ updatedAt?: string | null
45
+ }
46
+
47
+ type CompletePutawayTaskDialogProps = {
48
+ open: boolean
49
+ onOpenChange: (open: boolean) => void
50
+ access: WmsInventoryMutationAccess
51
+ task: PutawayTaskTarget | null
52
+ onSuccess?: () => void
53
+ }
54
+
55
+ const formSchema = z.object({
56
+ confirmedQuantity: z.number().positive(),
57
+ targetLocationId: z.string().uuid(),
58
+ })
59
+
60
+ export function CompletePutawayTaskDialog({
61
+ open,
62
+ onOpenChange,
63
+ access,
64
+ task,
65
+ onSuccess,
66
+ }: CompletePutawayTaskDialogProps) {
67
+ const t = useT()
68
+ const queryClient = useQueryClient()
69
+ const { runMutation, retryLastMutation } = useGuardedMutation<{
70
+ retryLastMutation: () => Promise<boolean>
71
+ }>({
72
+ contextId: 'wms-putaway-complete',
73
+ })
74
+ const mutationContext = React.useMemo(
75
+ () => ({ retryLastMutation }),
76
+ [retryLastMutation],
77
+ )
78
+
79
+ const [confirmedQuantity, setConfirmedQuantity] = React.useState(1)
80
+ const [targetLocationId, setTargetLocationId] = React.useState('')
81
+ const [serialConfirmation, setSerialConfirmation] = React.useState('')
82
+ const [fieldErrors, setFieldErrors] = React.useState<Record<string, string>>({})
83
+ const [submitting, setSubmitting] = React.useState(false)
84
+ const [lockUpdatedAt, setLockUpdatedAt] = React.useState<string | null>(null)
85
+ /** Fresh If-Match after 409; ref so guarded retry reads latest without re-seed. */
86
+ const lockUpdatedAtRef = React.useRef<string | null>(null)
87
+
88
+ const closeDialog = React.useCallback(() => {
89
+ if (submitting) return
90
+ onOpenChange(false)
91
+ }, [onOpenChange, submitting])
92
+
93
+ const warehouseId = task?.warehouseId?.trim() || ''
94
+ const expectedSerial = task?.serialNumber?.trim() ?? ''
95
+
96
+ // Stable identity so ComboboxInput's debounced suggestion effect is not torn
97
+ // down on every parent re-render (queue refetch / access churn) — an inline
98
+ // arrow would recreate each render and can starve the fetch, leaving the
99
+ // portaled list stuck on "Loading suggestions…" with no selectable options.
100
+ const loadTargetLocationSuggestions = React.useCallback(
101
+ async (query?: string) => (warehouseId ? loadBinLocationOptions(warehouseId, query) : []),
102
+ [warehouseId],
103
+ )
104
+
105
+ const resolveTargetLocationLabel = React.useCallback(
106
+ async (value: string) => (await resolveLocationLabel(value)) ?? value,
107
+ [],
108
+ )
109
+
110
+ React.useEffect(() => {
111
+ if (!open || !task) return
112
+ setConfirmedQuantity(task.quantity > 0 ? task.quantity : 1)
113
+ setTargetLocationId(task.targetLocationId?.trim() || '')
114
+ setSerialConfirmation('')
115
+ setFieldErrors({})
116
+ setSubmitting(false)
117
+ const seed = task.updatedAt ?? null
118
+ lockUpdatedAtRef.current = seed
119
+ setLockUpdatedAt(seed)
120
+ }, [open, task])
121
+
122
+ const handleSubmit = React.useCallback(
123
+ async (event?: React.FormEvent) => {
124
+ event?.preventDefault()
125
+ if (!task) return
126
+ if (!access.scopeReady || !access.organizationId || !access.tenantId || !access.userId) {
127
+ flash(
128
+ t(
129
+ 'wms.backend.inventory.mutations.errors.scope',
130
+ 'Select an organization and sign in before posting inventory changes.',
131
+ ),
132
+ 'error',
133
+ )
134
+ return
135
+ }
136
+
137
+ const parsed = formSchema.safeParse({
138
+ confirmedQuantity,
139
+ targetLocationId: targetLocationId.trim(),
140
+ })
141
+ const nextErrors: Record<string, string> = {}
142
+ if (!parsed.success) {
143
+ for (const issue of parsed.error.issues) {
144
+ const key = String(issue.path[0] ?? 'form')
145
+ if (!nextErrors[key]) nextErrors[key] = t(issue.message, issue.message)
146
+ }
147
+ }
148
+ if (expectedSerial && serialConfirmation.trim() !== expectedSerial) {
149
+ nextErrors.serialConfirmation = t(
150
+ 'wms.backend.putaway.complete.errors.serialMismatch',
151
+ 'The scanned serial does not match this task.',
152
+ )
153
+ }
154
+ if (!parsed.success || nextErrors.serialConfirmation) {
155
+ setFieldErrors(nextErrors)
156
+ return
157
+ }
158
+
159
+ setSubmitting(true)
160
+ setFieldErrors({})
161
+ const payload = {
162
+ organizationId: access.organizationId,
163
+ tenantId: access.tenantId,
164
+ confirmedQuantity: parsed.data.confirmedQuantity,
165
+ targetLocationId: parsed.data.targetLocationId,
166
+ performedBy: access.userId,
167
+ }
168
+
169
+ try {
170
+ let conflictHandled = false
171
+ await runMutation({
172
+ operation: async () => {
173
+ const call = await apiCall<{ ok?: boolean }>(
174
+ `/api/wms/putaway-tasks/${encodeURIComponent(task.id)}/complete`,
175
+ {
176
+ method: 'POST',
177
+ headers: {
178
+ 'content-type': 'application/json',
179
+ ...buildOptimisticLockHeader(lockUpdatedAtRef.current ?? undefined),
180
+ },
181
+ body: JSON.stringify(payload),
182
+ },
183
+ )
184
+ if (!call.ok) {
185
+ const refreshLockFromConflict = () => {
186
+ const conflict = extractOptimisticLockConflict({
187
+ status: call.status,
188
+ body: call.result,
189
+ })
190
+ if (conflict?.currentUpdatedAt) {
191
+ lockUpdatedAtRef.current = conflict.currentUpdatedAt
192
+ setLockUpdatedAt(conflict.currentUpdatedAt)
193
+ }
194
+ void queryClient.invalidateQueries({ queryKey: ['wms-putaway-queue'] })
195
+ }
196
+ if (
197
+ surfaceRecordConflict({ status: call.status, body: call.result }, t, {
198
+ onRefresh: refreshLockFromConflict,
199
+ })
200
+ ) {
201
+ refreshLockFromConflict()
202
+ conflictHandled = true
203
+ return {}
204
+ }
205
+ await raiseCrudError(
206
+ call.response,
207
+ t('wms.backend.putaway.complete.errors.submit', 'Failed to complete putaway task.'),
208
+ )
209
+ }
210
+ return call.result ?? {}
211
+ },
212
+ context: mutationContext,
213
+ mutationPayload: payload,
214
+ })
215
+
216
+ if (conflictHandled) return
217
+
218
+ flash(t('wms.backend.putaway.complete.flash.success', 'Putaway completed'), 'success')
219
+ await queryClient.invalidateQueries({ queryKey: ['wms-putaway-queue'] })
220
+ closeDialog()
221
+ onSuccess?.()
222
+ } catch (error) {
223
+ flashMutationError(
224
+ error,
225
+ t('wms.backend.putaway.complete.errors.submit', 'Failed to complete putaway task.'),
226
+ t,
227
+ )
228
+ } finally {
229
+ setSubmitting(false)
230
+ }
231
+ },
232
+ [
233
+ access,
234
+ closeDialog,
235
+ confirmedQuantity,
236
+ expectedSerial,
237
+ lockUpdatedAt,
238
+ serialConfirmation,
239
+ mutationContext,
240
+ onSuccess,
241
+ queryClient,
242
+ runMutation,
243
+ t,
244
+ targetLocationId,
245
+ task,
246
+ ],
247
+ )
248
+
249
+ const handleKeyDown = React.useCallback(
250
+ (event: React.KeyboardEvent<HTMLDivElement>) => {
251
+ if (event.key === 'Escape') {
252
+ event.preventDefault()
253
+ closeDialog()
254
+ return
255
+ }
256
+ if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {
257
+ event.preventDefault()
258
+ if (!submitting) void handleSubmit()
259
+ }
260
+ },
261
+ [closeDialog, handleSubmit, submitting],
262
+ )
263
+
264
+ if (!task) return null
265
+
266
+ return (
267
+ <Dialog open={open} onOpenChange={(next) => (next ? onOpenChange(true) : closeDialog())}>
268
+ <DialogContent className="max-w-lg gap-0 overflow-hidden p-0" onKeyDown={handleKeyDown}>
269
+ <div className="border-b px-6 py-4 pr-12">
270
+ <DialogHeader className="space-y-1 text-left">
271
+ <DialogTitle>
272
+ {t('wms.backend.putaway.complete.dialog.title', 'Complete putaway')}
273
+ </DialogTitle>
274
+ <DialogDescription>
275
+ {t(
276
+ 'wms.backend.putaway.complete.dialog.description',
277
+ 'Confirm quantity and destination bin to move stock from staging.',
278
+ )}
279
+ </DialogDescription>
280
+ </DialogHeader>
281
+ </div>
282
+
283
+ <form onSubmit={handleSubmit} className="flex min-h-0 flex-1 flex-col">
284
+ <div className="flex flex-col gap-5 overflow-y-auto px-6 py-6">
285
+ {expectedSerial ? (
286
+ <>
287
+ <p className="text-sm text-muted-foreground" data-testid="complete-putaway-expected-serial">
288
+ {t('wms.backend.putaway.complete.form.serial', 'Serial')}: {expectedSerial}
289
+ </p>
290
+ <FormField
291
+ label={t('wms.backend.putaway.complete.form.serialConfirm', 'Confirm serial')}
292
+ required
293
+ disabled={submitting}
294
+ error={fieldErrors.serialConfirmation}
295
+ description={t(
296
+ 'wms.backend.putaway.complete.form.serialConfirmHint',
297
+ 'Scan or type the serial on the unit. It must match this task.',
298
+ )}
299
+ >
300
+ <Input
301
+ value={serialConfirmation}
302
+ onChange={(event) => setSerialConfirmation(event.target.value)}
303
+ data-testid="complete-putaway-serial"
304
+ />
305
+ </FormField>
306
+ </>
307
+ ) : null}
308
+ <FormField
309
+ label={t('wms.backend.putaway.complete.form.quantity', 'Confirmed quantity')}
310
+ required
311
+ error={fieldErrors.confirmedQuantity}
312
+ >
313
+ <Input
314
+ type="number"
315
+ min={0.0001}
316
+ step="any"
317
+ value={confirmedQuantity}
318
+ onChange={(event) => setConfirmedQuantity(Number(event.target.value))}
319
+ disabled={submitting}
320
+ data-testid="complete-putaway-qty"
321
+ />
322
+ </FormField>
323
+
324
+ <FormField
325
+ label={t('wms.backend.putaway.complete.form.target', 'Target location')}
326
+ required
327
+ error={fieldErrors.targetLocationId}
328
+ >
329
+ <ComboboxInput
330
+ value={targetLocationId}
331
+ onChange={setTargetLocationId}
332
+ loadSuggestions={loadTargetLocationSuggestions}
333
+ resolveLabel={resolveTargetLocationLabel}
334
+ placeholder={t(
335
+ 'wms.backend.putaway.complete.form.targetPlaceholder',
336
+ 'Select destination bin',
337
+ )}
338
+ allowCustomValues={false}
339
+ disabled={submitting}
340
+ />
341
+ </FormField>
342
+ </div>
343
+
344
+ <DialogFooter bordered={false} className="border-t px-6 py-4 sm:justify-between">
345
+ <span className="hidden text-xs text-muted-foreground sm:inline">
346
+ <KbdShortcut keys={['Ctrl/⌘', 'Enter']} />{' '}
347
+ {t('wms.backend.putaway.complete.dialog.shortcutHint', 'to complete')}
348
+ </span>
349
+ <div className="flex gap-2">
350
+ <Button type="button" variant="outline" onClick={closeDialog} disabled={submitting}>
351
+ {t('wms.backend.putaway.complete.dialog.cancel', 'Cancel')}
352
+ </Button>
353
+ <Button
354
+ type="submit"
355
+ disabled={submitting || access.loading || !access.scopeReady}
356
+ data-testid="complete-putaway-submit"
357
+ >
358
+ {t('wms.backend.putaway.complete.dialog.submit', 'Complete putaway')}
359
+ </Button>
360
+ </div>
361
+ </DialogFooter>
362
+ </form>
363
+ </DialogContent>
364
+ </Dialog>
365
+ )
366
+ }