@things-factory/warehouse-base 4.0.0-alpha.8 → 4.0.0-beta.2

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 (562) hide show
  1. package/dist-server/constants/index.js +1 -1
  2. package/dist-server/constants/inventory.js +4 -1
  3. package/dist-server/constants/inventory.js.map +1 -1
  4. package/dist-server/controllers/ecommerce/ecommerce-controller.js +10 -4
  5. package/dist-server/controllers/ecommerce/ecommerce-controller.js.map +1 -1
  6. package/dist-server/controllers/ecommerce/index.js +2 -1
  7. package/dist-server/controllers/ecommerce/index.js.map +1 -1
  8. package/dist-server/controllers/ecommerce/sellercraft-controller.js +48 -0
  9. package/dist-server/controllers/ecommerce/sellercraft-controller.js.map +1 -0
  10. package/dist-server/controllers/index.js +1 -1
  11. package/dist-server/controllers/warehouse-controller.js +42 -3
  12. package/dist-server/controllers/warehouse-controller.js.map +1 -1
  13. package/dist-server/entities/inventory-change.js +30 -30
  14. package/dist-server/entities/inventory-change.js.map +1 -1
  15. package/dist-server/entities/inventory-history.js +49 -41
  16. package/dist-server/entities/inventory-history.js.map +1 -1
  17. package/dist-server/entities/inventory.js +44 -44
  18. package/dist-server/entities/inventory.js.map +1 -1
  19. package/dist-server/entities/location.js +18 -18
  20. package/dist-server/entities/location.js.map +1 -1
  21. package/dist-server/entities/movement.js +13 -13
  22. package/dist-server/entities/movement.js.map +1 -1
  23. package/dist-server/entities/pallet-count.js +11 -11
  24. package/dist-server/entities/pallet-count.js.map +1 -1
  25. package/dist-server/entities/pallet-history.js +15 -15
  26. package/dist-server/entities/pallet-history.js.map +1 -1
  27. package/dist-server/entities/pallet.js +16 -16
  28. package/dist-server/entities/pallet.js.map +1 -1
  29. package/dist-server/entities/reduced-inventory-history.js +35 -34
  30. package/dist-server/entities/reduced-inventory-history.js.map +1 -1
  31. package/dist-server/entities/warehouse.js +14 -14
  32. package/dist-server/entities/warehouse.js.map +1 -1
  33. package/dist-server/graphql/index.js +1 -1
  34. package/dist-server/graphql/resolvers/index.js +1 -1
  35. package/dist-server/graphql/resolvers/inventory/check-carton-identicality.js +1 -1
  36. package/dist-server/graphql/resolvers/inventory/check-carton-identicality.js.map +1 -1
  37. package/dist-server/graphql/resolvers/inventory/check-inventory-owner.js +2 -2
  38. package/dist-server/graphql/resolvers/inventory/check-inventory-owner.js.map +1 -1
  39. package/dist-server/graphql/resolvers/inventory/check-product-identicality.js +2 -2
  40. package/dist-server/graphql/resolvers/inventory/check-product-identicality.js.map +1 -1
  41. package/dist-server/graphql/resolvers/inventory/delete-inventories.js +1 -1
  42. package/dist-server/graphql/resolvers/inventory/delete-inventories.js.map +1 -1
  43. package/dist-server/graphql/resolvers/inventory/inventories-by-bundle.js +4 -4
  44. package/dist-server/graphql/resolvers/inventory/inventories-by-bundle.js.map +1 -1
  45. package/dist-server/graphql/resolvers/inventory/inventories-by-product.js +3 -3
  46. package/dist-server/graphql/resolvers/inventory/inventories-by-product.js.map +1 -1
  47. package/dist-server/graphql/resolvers/inventory/inventories-by-strategy.js +38 -4
  48. package/dist-server/graphql/resolvers/inventory/inventories-by-strategy.js.map +1 -1
  49. package/dist-server/graphql/resolvers/inventory/inventories.js +4 -4
  50. package/dist-server/graphql/resolvers/inventory/inventories.js.map +1 -1
  51. package/dist-server/graphql/resolvers/inventory/inventory-by-pallet.js +2 -2
  52. package/dist-server/graphql/resolvers/inventory/inventory-by-pallet.js.map +1 -1
  53. package/dist-server/graphql/resolvers/inventory/inventory-transfer.js +1 -1
  54. package/dist-server/graphql/resolvers/inventory/inventory-transfer.js.map +1 -1
  55. package/dist-server/graphql/resolvers/inventory/inventory.js +1 -1
  56. package/dist-server/graphql/resolvers/inventory/inventory.js.map +1 -1
  57. package/dist-server/graphql/resolvers/inventory/update-multiple-inventory.js +1 -1
  58. package/dist-server/graphql/resolvers/inventory/update-multiple-inventory.js.map +1 -1
  59. package/dist-server/graphql/resolvers/inventory-change/approve-inventory-changes.js +35 -13
  60. package/dist-server/graphql/resolvers/inventory-change/approve-inventory-changes.js.map +1 -1
  61. package/dist-server/graphql/resolvers/inventory-change/delete-inventory-changes.js +1 -1
  62. package/dist-server/graphql/resolvers/inventory-change/delete-inventory-changes.js.map +1 -1
  63. package/dist-server/graphql/resolvers/inventory-change/inventory-change.js +1 -1
  64. package/dist-server/graphql/resolvers/inventory-change/inventory-change.js.map +1 -1
  65. package/dist-server/graphql/resolvers/inventory-change/inventory-changes.js +3 -3
  66. package/dist-server/graphql/resolvers/inventory-change/inventory-changes.js.map +1 -1
  67. package/dist-server/graphql/resolvers/inventory-change/reject-inventory-changes.js +1 -1
  68. package/dist-server/graphql/resolvers/inventory-change/reject-inventory-changes.js.map +1 -1
  69. package/dist-server/graphql/resolvers/inventory-change/submit-inventory-changes.js.map +1 -1
  70. package/dist-server/graphql/resolvers/inventory-history/bizplace-inventory-histories.js +9 -9
  71. package/dist-server/graphql/resolvers/inventory-history/bizplace-inventory-histories.js.map +1 -1
  72. package/dist-server/graphql/resolvers/inventory-history/delete-inventory-histories.js +1 -1
  73. package/dist-server/graphql/resolvers/inventory-history/delete-inventory-histories.js.map +1 -1
  74. package/dist-server/graphql/resolvers/inventory-history/inventory-histories.js +3 -3
  75. package/dist-server/graphql/resolvers/inventory-history/inventory-histories.js.map +1 -1
  76. package/dist-server/graphql/resolvers/inventory-history/inventory-history-pallet-report.js +1 -1
  77. package/dist-server/graphql/resolvers/inventory-history/inventory-history-pallet-report.js.map +1 -1
  78. package/dist-server/graphql/resolvers/inventory-history/inventory-history-summary-report.js +12 -11
  79. package/dist-server/graphql/resolvers/inventory-history/inventory-history-summary-report.js.map +1 -1
  80. package/dist-server/graphql/resolvers/inventory-history/inventory-history.js +1 -1
  81. package/dist-server/graphql/resolvers/inventory-history/inventory-history.js.map +1 -1
  82. package/dist-server/graphql/resolvers/inventory-history/onhand-inventories.js +7 -4
  83. package/dist-server/graphql/resolvers/inventory-history/onhand-inventories.js.map +1 -1
  84. package/dist-server/graphql/resolvers/location/index.js +2 -1
  85. package/dist-server/graphql/resolvers/location/index.js.map +1 -1
  86. package/dist-server/graphql/resolvers/location/location-by-name.js +1 -1
  87. package/dist-server/graphql/resolvers/location/location-by-name.js.map +1 -1
  88. package/dist-server/graphql/resolvers/location/location-occupancies.js +15 -28
  89. package/dist-server/graphql/resolvers/location/location-occupancies.js.map +1 -1
  90. package/dist-server/graphql/resolvers/location/location-with-inventories.js +75 -0
  91. package/dist-server/graphql/resolvers/location/location-with-inventories.js.map +1 -0
  92. package/dist-server/graphql/resolvers/location/location.js +3 -2
  93. package/dist-server/graphql/resolvers/location/location.js.map +1 -1
  94. package/dist-server/graphql/resolvers/location/locations-by-group.js +2 -2
  95. package/dist-server/graphql/resolvers/location/locations-by-group.js.map +1 -1
  96. package/dist-server/graphql/resolvers/location/locations.js +2 -2
  97. package/dist-server/graphql/resolvers/location/locations.js.map +1 -1
  98. package/dist-server/graphql/resolvers/location/update-multiple-location.js +2 -2
  99. package/dist-server/graphql/resolvers/location/update-multiple-location.js.map +1 -1
  100. package/dist-server/graphql/resolvers/movement/create-movement.js +1 -1
  101. package/dist-server/graphql/resolvers/movement/create-movement.js.map +1 -1
  102. package/dist-server/graphql/resolvers/movement/delete-movement.js +1 -1
  103. package/dist-server/graphql/resolvers/movement/delete-movement.js.map +1 -1
  104. package/dist-server/graphql/resolvers/movement/inbound-movements-counter.js +7 -9
  105. package/dist-server/graphql/resolvers/movement/inbound-movements-counter.js.map +1 -1
  106. package/dist-server/graphql/resolvers/movement/movement.js +2 -2
  107. package/dist-server/graphql/resolvers/movement/movement.js.map +1 -1
  108. package/dist-server/graphql/resolvers/movement/movements.js +3 -3
  109. package/dist-server/graphql/resolvers/movement/movements.js.map +1 -1
  110. package/dist-server/graphql/resolvers/movement/outbound-movements-counter.js +7 -9
  111. package/dist-server/graphql/resolvers/movement/outbound-movements-counter.js.map +1 -1
  112. package/dist-server/graphql/resolvers/movement/update-movement.js +4 -4
  113. package/dist-server/graphql/resolvers/movement/update-movement.js.map +1 -1
  114. package/dist-server/graphql/resolvers/movement/weekly-movements-counter.js +16 -34
  115. package/dist-server/graphql/resolvers/movement/weekly-movements-counter.js.map +1 -1
  116. package/dist-server/graphql/resolvers/pallet/delete-pallets.js +1 -1
  117. package/dist-server/graphql/resolvers/pallet/delete-pallets.js.map +1 -1
  118. package/dist-server/graphql/resolvers/pallet/pallet-by-status.js +1 -1
  119. package/dist-server/graphql/resolvers/pallet/pallet-by-status.js.map +1 -1
  120. package/dist-server/graphql/resolvers/pallet/pallet-inbound-validate.js +1 -1
  121. package/dist-server/graphql/resolvers/pallet/pallet-inbound-validate.js.map +1 -1
  122. package/dist-server/graphql/resolvers/pallet/pallet-outbound-validate.js +1 -1
  123. package/dist-server/graphql/resolvers/pallet/pallet-outbound-validate.js.map +1 -1
  124. package/dist-server/graphql/resolvers/pallet/pallet.js +1 -1
  125. package/dist-server/graphql/resolvers/pallet/pallet.js.map +1 -1
  126. package/dist-server/graphql/resolvers/pallet/pallets.js +2 -2
  127. package/dist-server/graphql/resolvers/pallet/pallets.js.map +1 -1
  128. package/dist-server/graphql/resolvers/pallet-count/delete-pallet-counts.js +1 -1
  129. package/dist-server/graphql/resolvers/pallet-count/delete-pallet-counts.js.map +1 -1
  130. package/dist-server/graphql/resolvers/pallet-count/pallet-count.js +1 -1
  131. package/dist-server/graphql/resolvers/pallet-count/pallet-count.js.map +1 -1
  132. package/dist-server/graphql/resolvers/pallet-count/pallet-counts.js +2 -2
  133. package/dist-server/graphql/resolvers/pallet-count/pallet-counts.js.map +1 -1
  134. package/dist-server/graphql/resolvers/pallet-count/update-pallet-count-seq.js +2 -2
  135. package/dist-server/graphql/resolvers/pallet-count/update-pallet-count-seq.js.map +1 -1
  136. package/dist-server/graphql/resolvers/pallet-history/delete-pallet-histories.js +1 -1
  137. package/dist-server/graphql/resolvers/pallet-history/delete-pallet-histories.js.map +1 -1
  138. package/dist-server/graphql/resolvers/pallet-history/pallet-histories.js +2 -2
  139. package/dist-server/graphql/resolvers/pallet-history/pallet-histories.js.map +1 -1
  140. package/dist-server/graphql/resolvers/pallet-history/pallet-history.js +1 -1
  141. package/dist-server/graphql/resolvers/pallet-history/pallet-history.js.map +1 -1
  142. package/dist-server/graphql/resolvers/warehouse/update-multiple-warehouse.js +2 -2
  143. package/dist-server/graphql/resolvers/warehouse/update-multiple-warehouse.js.map +1 -1
  144. package/dist-server/graphql/resolvers/warehouse/warehouse.js +1 -1
  145. package/dist-server/graphql/resolvers/warehouse/warehouse.js.map +1 -1
  146. package/dist-server/graphql/resolvers/warehouse/warehouses.js +2 -2
  147. package/dist-server/graphql/resolvers/warehouse/warehouses.js.map +1 -1
  148. package/dist-server/graphql/types/index.js +1 -1
  149. package/dist-server/graphql/types/inventory/index.js +1 -1
  150. package/dist-server/graphql/types/inventory/inventory-bundle-group-detail.js +1 -1
  151. package/dist-server/graphql/types/inventory/inventory-bundle-group-detail.js.map +1 -1
  152. package/dist-server/graphql/types/inventory/inventory-bundle-group.js +1 -1
  153. package/dist-server/graphql/types/inventory/inventory-bundle-group.js.map +1 -1
  154. package/dist-server/graphql/types/inventory/inventory-list.js +1 -1
  155. package/dist-server/graphql/types/inventory/inventory-list.js.map +1 -1
  156. package/dist-server/graphql/types/inventory/inventory-patch.js +2 -1
  157. package/dist-server/graphql/types/inventory/inventory-patch.js.map +1 -1
  158. package/dist-server/graphql/types/inventory/inventory.js +1 -1
  159. package/dist-server/graphql/types/inventory/inventory.js.map +1 -1
  160. package/dist-server/graphql/types/inventory/new-inventory.js +1 -1
  161. package/dist-server/graphql/types/inventory/new-inventory.js.map +1 -1
  162. package/dist-server/graphql/types/inventory-change/inventory-change-list.js +1 -1
  163. package/dist-server/graphql/types/inventory-change/inventory-change-list.js.map +1 -1
  164. package/dist-server/graphql/types/inventory-change/inventory-change-patch.js +1 -1
  165. package/dist-server/graphql/types/inventory-change/inventory-change-patch.js.map +1 -1
  166. package/dist-server/graphql/types/inventory-change/inventory-change.js +2 -1
  167. package/dist-server/graphql/types/inventory-change/inventory-change.js.map +1 -1
  168. package/dist-server/graphql/types/inventory-change/new-inventory-change.js +1 -1
  169. package/dist-server/graphql/types/inventory-change/new-inventory-change.js.map +1 -1
  170. package/dist-server/graphql/types/inventory-history/inventory-history-list.js +1 -1
  171. package/dist-server/graphql/types/inventory-history/inventory-history-list.js.map +1 -1
  172. package/dist-server/graphql/types/inventory-history/inventory-history-pallet-report-list.js +1 -1
  173. package/dist-server/graphql/types/inventory-history/inventory-history-pallet-report-list.js.map +1 -1
  174. package/dist-server/graphql/types/inventory-history/inventory-history-pallet-report.js +1 -1
  175. package/dist-server/graphql/types/inventory-history/inventory-history-pallet-report.js.map +1 -1
  176. package/dist-server/graphql/types/inventory-history/inventory-history-patch.js +2 -1
  177. package/dist-server/graphql/types/inventory-history/inventory-history-patch.js.map +1 -1
  178. package/dist-server/graphql/types/inventory-history/inventory-history-summary-list.js +1 -1
  179. package/dist-server/graphql/types/inventory-history/inventory-history-summary-list.js.map +1 -1
  180. package/dist-server/graphql/types/inventory-history/inventory-history-summary.js +1 -1
  181. package/dist-server/graphql/types/inventory-history/inventory-history-summary.js.map +1 -1
  182. package/dist-server/graphql/types/inventory-history/inventory-history.js +3 -2
  183. package/dist-server/graphql/types/inventory-history/inventory-history.js.map +1 -1
  184. package/dist-server/graphql/types/inventory-history/new-inventory-history.js +2 -1
  185. package/dist-server/graphql/types/inventory-history/new-inventory-history.js.map +1 -1
  186. package/dist-server/graphql/types/location/index.js +14 -3
  187. package/dist-server/graphql/types/location/index.js.map +1 -1
  188. package/dist-server/graphql/types/location/location-inventories.js +14 -0
  189. package/dist-server/graphql/types/location/location-inventories.js.map +1 -0
  190. package/dist-server/graphql/types/location/location-inventory.js +27 -0
  191. package/dist-server/graphql/types/location/location-inventory.js.map +1 -0
  192. package/dist-server/graphql/types/location/location-list.js +1 -1
  193. package/dist-server/graphql/types/location/location-list.js.map +1 -1
  194. package/dist-server/graphql/types/location/location-occupancy.js +1 -1
  195. package/dist-server/graphql/types/location/location-occupancy.js.map +1 -1
  196. package/dist-server/graphql/types/location/location-patch.js +1 -1
  197. package/dist-server/graphql/types/location/location-patch.js.map +1 -1
  198. package/dist-server/graphql/types/location/location.js +1 -1
  199. package/dist-server/graphql/types/location/location.js.map +1 -1
  200. package/dist-server/graphql/types/location/new-location.js +1 -1
  201. package/dist-server/graphql/types/location/new-location.js.map +1 -1
  202. package/dist-server/graphql/types/movement/movement-count.js +1 -1
  203. package/dist-server/graphql/types/movement/movement-count.js.map +1 -1
  204. package/dist-server/graphql/types/movement/movement-list.js +1 -1
  205. package/dist-server/graphql/types/movement/movement-list.js.map +1 -1
  206. package/dist-server/graphql/types/movement/movement-patch.js +1 -1
  207. package/dist-server/graphql/types/movement/movement-patch.js.map +1 -1
  208. package/dist-server/graphql/types/movement/movement.js +1 -1
  209. package/dist-server/graphql/types/movement/movement.js.map +1 -1
  210. package/dist-server/graphql/types/movement/new-movement.js +1 -1
  211. package/dist-server/graphql/types/movement/new-movement.js.map +1 -1
  212. package/dist-server/graphql/types/pallet/new-pallet.js +1 -1
  213. package/dist-server/graphql/types/pallet/new-pallet.js.map +1 -1
  214. package/dist-server/graphql/types/pallet/pallet-list.js +1 -1
  215. package/dist-server/graphql/types/pallet/pallet-list.js.map +1 -1
  216. package/dist-server/graphql/types/pallet/pallet-patch.js +1 -1
  217. package/dist-server/graphql/types/pallet/pallet-patch.js.map +1 -1
  218. package/dist-server/graphql/types/pallet/pallet-validate.js +1 -1
  219. package/dist-server/graphql/types/pallet/pallet-validate.js.map +1 -1
  220. package/dist-server/graphql/types/pallet/pallet.js +1 -1
  221. package/dist-server/graphql/types/pallet/pallet.js.map +1 -1
  222. package/dist-server/graphql/types/pallet-count/new-pallet-count.js +1 -1
  223. package/dist-server/graphql/types/pallet-count/new-pallet-count.js.map +1 -1
  224. package/dist-server/graphql/types/pallet-count/pallet-count-list.js +1 -1
  225. package/dist-server/graphql/types/pallet-count/pallet-count-list.js.map +1 -1
  226. package/dist-server/graphql/types/pallet-count/pallet-count-patch.js +1 -1
  227. package/dist-server/graphql/types/pallet-count/pallet-count-patch.js.map +1 -1
  228. package/dist-server/graphql/types/pallet-count/pallet-count.js +1 -1
  229. package/dist-server/graphql/types/pallet-count/pallet-count.js.map +1 -1
  230. package/dist-server/graphql/types/pallet-history/new-pallet-history.js +1 -1
  231. package/dist-server/graphql/types/pallet-history/new-pallet-history.js.map +1 -1
  232. package/dist-server/graphql/types/pallet-history/pallet-history-list.js +1 -1
  233. package/dist-server/graphql/types/pallet-history/pallet-history-list.js.map +1 -1
  234. package/dist-server/graphql/types/pallet-history/pallet-history-patch.js +1 -1
  235. package/dist-server/graphql/types/pallet-history/pallet-history-patch.js.map +1 -1
  236. package/dist-server/graphql/types/pallet-history/pallet-history.js +1 -1
  237. package/dist-server/graphql/types/pallet-history/pallet-history.js.map +1 -1
  238. package/dist-server/graphql/types/warehouse/index.js.map +1 -1
  239. package/dist-server/graphql/types/warehouse/new-warehouse.js +1 -1
  240. package/dist-server/graphql/types/warehouse/new-warehouse.js.map +1 -1
  241. package/dist-server/graphql/types/warehouse/warehouse-list.js +1 -1
  242. package/dist-server/graphql/types/warehouse/warehouse-list.js.map +1 -1
  243. package/dist-server/graphql/types/warehouse/warehouse-patch.js +1 -1
  244. package/dist-server/graphql/types/warehouse/warehouse-patch.js.map +1 -1
  245. package/dist-server/graphql/types/warehouse/warehouse.js +1 -1
  246. package/dist-server/graphql/types/warehouse/warehouse.js.map +1 -1
  247. package/dist-server/index.js +13 -13
  248. package/dist-server/index.js.map +1 -1
  249. package/dist-server/service/index.js +71 -0
  250. package/dist-server/service/index.js.map +1 -0
  251. package/dist-server/service/inventory/index.js +9 -0
  252. package/dist-server/service/inventory/index.js.map +1 -0
  253. package/dist-server/service/inventory/inventory-mutation.js +324 -0
  254. package/dist-server/service/inventory/inventory-mutation.js.map +1 -0
  255. package/dist-server/service/inventory/inventory-query.js +678 -0
  256. package/dist-server/service/inventory/inventory-query.js.map +1 -0
  257. package/dist-server/service/inventory/inventory-types.js +405 -0
  258. package/dist-server/service/inventory/inventory-types.js.map +1 -0
  259. package/dist-server/service/inventory/inventory.js +284 -0
  260. package/dist-server/service/inventory/inventory.js.map +1 -0
  261. package/dist-server/service/inventory-change/index.js +9 -0
  262. package/dist-server/service/inventory-change/index.js.map +1 -0
  263. package/dist-server/service/inventory-change/inventory-change-mutation.js +530 -0
  264. package/dist-server/service/inventory-change/inventory-change-mutation.js.map +1 -0
  265. package/dist-server/service/inventory-change/inventory-change-query.js +121 -0
  266. package/dist-server/service/inventory-change/inventory-change-query.js.map +1 -0
  267. package/dist-server/service/inventory-change/inventory-change-types.js +65 -0
  268. package/dist-server/service/inventory-change/inventory-change-types.js.map +1 -0
  269. package/dist-server/service/inventory-change/inventory-change.js +185 -0
  270. package/dist-server/service/inventory-change/inventory-change.js.map +1 -0
  271. package/dist-server/service/inventory-history/index.js +9 -0
  272. package/dist-server/service/inventory-history/index.js.map +1 -0
  273. package/dist-server/service/inventory-history/inventory-history-mutation.js +121 -0
  274. package/dist-server/service/inventory-history/inventory-history-mutation.js.map +1 -0
  275. package/dist-server/service/inventory-history/inventory-history-query.js +1283 -0
  276. package/dist-server/service/inventory-history/inventory-history-query.js.map +1 -0
  277. package/dist-server/service/inventory-history/inventory-history-types.js +382 -0
  278. package/dist-server/service/inventory-history/inventory-history-types.js.map +1 -0
  279. package/dist-server/service/inventory-history/inventory-history.js +233 -0
  280. package/dist-server/service/inventory-history/inventory-history.js.map +1 -0
  281. package/dist-server/service/location/index.js +9 -0
  282. package/dist-server/service/location/index.js.map +1 -0
  283. package/dist-server/service/location/location-mutation.js +157 -0
  284. package/dist-server/service/location/location-mutation.js.map +1 -0
  285. package/dist-server/service/location/location-query.js +247 -0
  286. package/dist-server/service/location/location-query.js.map +1 -0
  287. package/dist-server/service/location/location-types.js +223 -0
  288. package/dist-server/service/location/location-types.js.map +1 -0
  289. package/dist-server/service/location/location.js +125 -0
  290. package/dist-server/service/location/location.js.map +1 -0
  291. package/dist-server/service/movement/index.js +9 -0
  292. package/dist-server/service/movement/index.js.map +1 -0
  293. package/dist-server/service/movement/movement-mutation.js +80 -0
  294. package/dist-server/service/movement/movement-mutation.js.map +1 -0
  295. package/dist-server/service/movement/movement-query.js +286 -0
  296. package/dist-server/service/movement/movement-query.js.map +1 -0
  297. package/dist-server/service/movement/movement-types.js +117 -0
  298. package/dist-server/service/movement/movement-types.js.map +1 -0
  299. package/dist-server/service/movement/movement.js +101 -0
  300. package/dist-server/service/movement/movement.js.map +1 -0
  301. package/dist-server/service/pallet/index.js +9 -0
  302. package/dist-server/service/pallet/index.js.map +1 -0
  303. package/dist-server/service/pallet/pallet-mutation.js +192 -0
  304. package/dist-server/service/pallet/pallet-mutation.js.map +1 -0
  305. package/dist-server/service/pallet/pallet-query.js +164 -0
  306. package/dist-server/service/pallet/pallet-query.js.map +1 -0
  307. package/dist-server/service/pallet/pallet-types.js +109 -0
  308. package/dist-server/service/pallet/pallet-types.js.map +1 -0
  309. package/dist-server/service/pallet/pallet.js +112 -0
  310. package/dist-server/service/pallet/pallet.js.map +1 -0
  311. package/dist-server/service/pallet-count/index.js +9 -0
  312. package/dist-server/service/pallet-count/index.js.map +1 -0
  313. package/dist-server/service/pallet-count/pallet-count-mutation.js +156 -0
  314. package/dist-server/service/pallet-count/pallet-count-mutation.js.map +1 -0
  315. package/dist-server/service/pallet-count/pallet-count-query.js +86 -0
  316. package/dist-server/service/pallet-count/pallet-count-query.js.map +1 -0
  317. package/dist-server/service/pallet-count/pallet-count-types.js +65 -0
  318. package/dist-server/service/pallet-count/pallet-count-types.js.map +1 -0
  319. package/dist-server/service/pallet-count/pallet-count.js +89 -0
  320. package/dist-server/service/pallet-count/pallet-count.js.map +1 -0
  321. package/dist-server/service/pallet-history/index.js +9 -0
  322. package/dist-server/service/pallet-history/index.js.map +1 -0
  323. package/dist-server/service/pallet-history/pallet-history-mutation.js +120 -0
  324. package/dist-server/service/pallet-history/pallet-history-mutation.js.map +1 -0
  325. package/dist-server/service/pallet-history/pallet-history-query.js +87 -0
  326. package/dist-server/service/pallet-history/pallet-history-query.js.map +1 -0
  327. package/dist-server/service/pallet-history/pallet-history-types.js +65 -0
  328. package/dist-server/service/pallet-history/pallet-history-types.js.map +1 -0
  329. package/dist-server/service/pallet-history/pallet-history.js +107 -0
  330. package/dist-server/service/pallet-history/pallet-history.js.map +1 -0
  331. package/dist-server/service/reduced-inventory-history/index.js +6 -0
  332. package/dist-server/service/reduced-inventory-history/index.js.map +1 -0
  333. package/dist-server/service/reduced-inventory-history/reduced-inventory-history.js +133 -0
  334. package/dist-server/service/reduced-inventory-history/reduced-inventory-history.js.map +1 -0
  335. package/dist-server/service/warehouse/index.js +9 -0
  336. package/dist-server/service/warehouse/index.js.map +1 -0
  337. package/dist-server/service/warehouse/warehouse-mutation.js +135 -0
  338. package/dist-server/service/warehouse/warehouse-mutation.js.map +1 -0
  339. package/dist-server/service/warehouse/warehouse-query.js +91 -0
  340. package/dist-server/service/warehouse/warehouse-query.js.map +1 -0
  341. package/dist-server/service/warehouse/warehouse-types.js +82 -0
  342. package/dist-server/service/warehouse/warehouse-types.js.map +1 -0
  343. package/dist-server/service/warehouse/warehouse.js +102 -0
  344. package/dist-server/service/warehouse/warehouse.js.map +1 -0
  345. package/dist-server/utils/index.js +1 -1
  346. package/dist-server/utils/inventory-no-generator.js +2 -2
  347. package/dist-server/utils/inventory-no-generator.js.map +1 -1
  348. package/dist-server/utils/inventory-util.js +47 -13
  349. package/dist-server/utils/inventory-util.js.map +1 -1
  350. package/package.json +8 -7
  351. package/server/constants/inventory.ts +4 -1
  352. package/server/controllers/ecommerce/ecommerce-controller.ts +14 -4
  353. package/server/controllers/ecommerce/index.ts +1 -0
  354. package/server/controllers/ecommerce/sellercraft-controller.ts +58 -0
  355. package/server/controllers/warehouse-controller.ts +54 -2
  356. package/server/index.ts +8 -4
  357. package/server/service/index.ts +61 -0
  358. package/server/service/inventory/index.ts +6 -0
  359. package/server/{graphql/resolvers/inventory/update-multiple-inventory.ts → service/inventory/inventory-mutation.ts} +158 -7
  360. package/server/service/inventory/inventory-query.ts +745 -0
  361. package/server/service/inventory/inventory-types.ts +289 -0
  362. package/server/{entities → service/inventory}/inventory.ts +86 -8
  363. package/server/service/inventory-change/index.ts +6 -0
  364. package/server/{graphql/resolvers/inventory-change/approve-inventory-changes.ts → service/inventory-change/inventory-change-mutation.ts} +303 -13
  365. package/server/{graphql/resolvers/inventory-change/inventory-changes.ts → service/inventory-change/inventory-change-query.ts} +35 -4
  366. package/server/service/inventory-change/inventory-change-types.ts +36 -0
  367. package/server/{entities → service/inventory-change}/inventory-change.ts +56 -6
  368. package/server/service/inventory-history/index.ts +6 -0
  369. package/server/service/inventory-history/inventory-history-mutation.ts +119 -0
  370. package/server/service/inventory-history/inventory-history-query.ts +1435 -0
  371. package/server/service/inventory-history/inventory-history-types.ts +269 -0
  372. package/server/{entities → service/inventory-history}/inventory-history.ts +74 -15
  373. package/server/service/location/index.ts +6 -0
  374. package/server/service/location/location-mutation.ts +134 -0
  375. package/server/service/location/location-query.ts +205 -0
  376. package/server/service/location/location-types.ts +151 -0
  377. package/server/{entities → service/location}/location.ts +40 -3
  378. package/server/service/movement/index.ts +6 -0
  379. package/server/service/movement/movement-mutation.ts +61 -0
  380. package/server/service/movement/movement-query.ts +258 -0
  381. package/server/service/movement/movement-types.ts +74 -0
  382. package/server/{entities → service/movement}/movement.ts +35 -2
  383. package/server/service/pallet/index.ts +6 -0
  384. package/server/service/pallet/pallet-mutation.ts +244 -0
  385. package/server/service/pallet/pallet-query.ts +101 -0
  386. package/server/service/pallet/pallet-types.ts +68 -0
  387. package/server/{entities → service/pallet}/pallet.ts +31 -4
  388. package/server/service/pallet-count/index.ts +6 -0
  389. package/server/service/pallet-count/pallet-count-mutation.ts +148 -0
  390. package/server/service/pallet-count/pallet-count-query.ts +46 -0
  391. package/server/service/pallet-count/pallet-count-types.ts +36 -0
  392. package/server/service/pallet-count/pallet-count.ts +70 -0
  393. package/server/service/pallet-history/index.ts +6 -0
  394. package/server/service/pallet-history/pallet-history-mutation.ts +114 -0
  395. package/server/service/pallet-history/pallet-history-query.ts +46 -0
  396. package/server/service/pallet-history/pallet-history-types.ts +36 -0
  397. package/server/{entities → service/pallet-history}/pallet-history.ts +34 -12
  398. package/server/service/reduced-inventory-history/index.ts +3 -0
  399. package/server/{entities → service/reduced-inventory-history}/reduced-inventory-history.ts +7 -12
  400. package/server/service/warehouse/index.ts +6 -0
  401. package/server/service/warehouse/warehouse-mutation.ts +114 -0
  402. package/server/service/warehouse/warehouse-query.ts +51 -0
  403. package/server/service/warehouse/warehouse-types.ts +50 -0
  404. package/server/{entities → service/warehouse}/warehouse.ts +29 -4
  405. package/server/utils/inventory-util.ts +58 -4
  406. package/server/entities/index.ts +0 -36
  407. package/server/entities/pallet-count.ts +0 -40
  408. package/server/graphql/index.ts +0 -7
  409. package/server/graphql/resolvers/index.ts +0 -33
  410. package/server/graphql/resolvers/inventory/check-carton-identicality.ts +0 -32
  411. package/server/graphql/resolvers/inventory/check-inventory-owner.ts +0 -26
  412. package/server/graphql/resolvers/inventory/check-product-identicality.ts +0 -24
  413. package/server/graphql/resolvers/inventory/create-inventory.ts +0 -15
  414. package/server/graphql/resolvers/inventory/delete-inventories.ts +0 -14
  415. package/server/graphql/resolvers/inventory/delete-inventory.ts +0 -10
  416. package/server/graphql/resolvers/inventory/index.ts +0 -38
  417. package/server/graphql/resolvers/inventory/inventories-by-bundle.ts +0 -168
  418. package/server/graphql/resolvers/inventory/inventories-by-product.ts +0 -164
  419. package/server/graphql/resolvers/inventory/inventories-by-strategy.ts +0 -106
  420. package/server/graphql/resolvers/inventory/inventories.ts +0 -129
  421. package/server/graphql/resolvers/inventory/inventory-by-pallet.ts +0 -15
  422. package/server/graphql/resolvers/inventory/inventory-transfer.ts +0 -50
  423. package/server/graphql/resolvers/inventory/inventory.ts +0 -14
  424. package/server/graphql/resolvers/inventory/update-inventory-remark.ts +0 -19
  425. package/server/graphql/resolvers/inventory/update-inventory.ts +0 -31
  426. package/server/graphql/resolvers/inventory-change/create-inventory-change.ts +0 -17
  427. package/server/graphql/resolvers/inventory-change/delete-inventory-change.ts +0 -12
  428. package/server/graphql/resolvers/inventory-change/delete-inventory-changes.ts +0 -15
  429. package/server/graphql/resolvers/inventory-change/index.ts +0 -27
  430. package/server/graphql/resolvers/inventory-change/inventory-change.ts +0 -14
  431. package/server/graphql/resolvers/inventory-change/reject-inventory-changes.ts +0 -42
  432. package/server/graphql/resolvers/inventory-change/submit-inventory-changes.ts +0 -101
  433. package/server/graphql/resolvers/inventory-change/update-inventory-change.ts +0 -21
  434. package/server/graphql/resolvers/inventory-change/update-multiple-inventory-change.ts +0 -47
  435. package/server/graphql/resolvers/inventory-history/bizplace-inventory-histories.ts +0 -81
  436. package/server/graphql/resolvers/inventory-history/create-inventory-history.ts +0 -17
  437. package/server/graphql/resolvers/inventory-history/delete-inventory-histories.ts +0 -15
  438. package/server/graphql/resolvers/inventory-history/delete-inventory-history.ts +0 -12
  439. package/server/graphql/resolvers/inventory-history/index.ts +0 -34
  440. package/server/graphql/resolvers/inventory-history/inventory-histories.ts +0 -62
  441. package/server/graphql/resolvers/inventory-history/inventory-history-pallet-detail-report.ts +0 -166
  442. package/server/graphql/resolvers/inventory-history/inventory-history-pallet-report.ts +0 -133
  443. package/server/graphql/resolvers/inventory-history/inventory-history-pallet-storage-report.ts +0 -146
  444. package/server/graphql/resolvers/inventory-history/inventory-history-report.ts +0 -233
  445. package/server/graphql/resolvers/inventory-history/inventory-history-summary-report.ts +0 -317
  446. package/server/graphql/resolvers/inventory-history/inventory-history.ts +0 -13
  447. package/server/graphql/resolvers/inventory-history/onhand-inventories.ts +0 -233
  448. package/server/graphql/resolvers/inventory-history/update-inventory-history.ts +0 -21
  449. package/server/graphql/resolvers/inventory-history/update-multiple-inventory-history.ts +0 -48
  450. package/server/graphql/resolvers/location/create-location.ts +0 -25
  451. package/server/graphql/resolvers/location/delete-all-locations.ts +0 -12
  452. package/server/graphql/resolvers/location/delete-location.ts +0 -16
  453. package/server/graphql/resolvers/location/delete-locations.ts +0 -16
  454. package/server/graphql/resolvers/location/index.ts +0 -30
  455. package/server/graphql/resolvers/location/location-by-name.ts +0 -17
  456. package/server/graphql/resolvers/location/location-occupancies.ts +0 -45
  457. package/server/graphql/resolvers/location/location.ts +0 -10
  458. package/server/graphql/resolvers/location/locations-by-group.ts +0 -29
  459. package/server/graphql/resolvers/location/locations.ts +0 -18
  460. package/server/graphql/resolvers/location/update-location.ts +0 -27
  461. package/server/graphql/resolvers/location/update-multiple-location.ts +0 -29
  462. package/server/graphql/resolvers/movement/create-movement.ts +0 -13
  463. package/server/graphql/resolvers/movement/delete-movement.ts +0 -9
  464. package/server/graphql/resolvers/movement/inbound-movements-counter.ts +0 -68
  465. package/server/graphql/resolvers/movement/index.ts +0 -23
  466. package/server/graphql/resolvers/movement/movement.ts +0 -16
  467. package/server/graphql/resolvers/movement/movements.ts +0 -17
  468. package/server/graphql/resolvers/movement/outbound-movements-counter.ts +0 -70
  469. package/server/graphql/resolvers/movement/update-movement.ts +0 -25
  470. package/server/graphql/resolvers/movement/weekly-movements-counter.ts +0 -94
  471. package/server/graphql/resolvers/pallet/create-pallet.ts +0 -18
  472. package/server/graphql/resolvers/pallet/delete-pallet.ts +0 -13
  473. package/server/graphql/resolvers/pallet/delete-pallets.ts +0 -44
  474. package/server/graphql/resolvers/pallet/index.ts +0 -29
  475. package/server/graphql/resolvers/pallet/pallet-by-status.ts +0 -16
  476. package/server/graphql/resolvers/pallet/pallet-inbound-validate.ts +0 -21
  477. package/server/graphql/resolvers/pallet/pallet-outbound-validate.ts +0 -21
  478. package/server/graphql/resolvers/pallet/pallet-return.ts +0 -57
  479. package/server/graphql/resolvers/pallet/pallet.ts +0 -15
  480. package/server/graphql/resolvers/pallet/pallets.ts +0 -22
  481. package/server/graphql/resolvers/pallet/update-multiple-pallet.ts +0 -95
  482. package/server/graphql/resolvers/pallet/update-pallet.ts +0 -21
  483. package/server/graphql/resolvers/pallet-count/create-pallet-count.ts +0 -17
  484. package/server/graphql/resolvers/pallet-count/delete-pallet-count.ts +0 -12
  485. package/server/graphql/resolvers/pallet-count/delete-pallet-counts.ts +0 -15
  486. package/server/graphql/resolvers/pallet-count/index.ts +0 -23
  487. package/server/graphql/resolvers/pallet-count/pallet-count.ts +0 -14
  488. package/server/graphql/resolvers/pallet-count/pallet-counts.ts +0 -14
  489. package/server/graphql/resolvers/pallet-count/update-multiple-pallet-count.ts +0 -47
  490. package/server/graphql/resolvers/pallet-count/update-pallet-count-seq.ts +0 -44
  491. package/server/graphql/resolvers/pallet-count/update-pallet-count.ts +0 -21
  492. package/server/graphql/resolvers/pallet-history/create-pallet-history.ts +0 -17
  493. package/server/graphql/resolvers/pallet-history/delete-pallet-histories.ts +0 -16
  494. package/server/graphql/resolvers/pallet-history/delete-pallet-history.ts +0 -13
  495. package/server/graphql/resolvers/pallet-history/index.ts +0 -21
  496. package/server/graphql/resolvers/pallet-history/pallet-histories.ts +0 -14
  497. package/server/graphql/resolvers/pallet-history/pallet-history.ts +0 -14
  498. package/server/graphql/resolvers/pallet-history/update-multiple-pallet-history.ts +0 -46
  499. package/server/graphql/resolvers/pallet-history/update-pallet-history.ts +0 -21
  500. package/server/graphql/resolvers/warehouse/create-warehouse.ts +0 -22
  501. package/server/graphql/resolvers/warehouse/delete-warehouse.ts +0 -16
  502. package/server/graphql/resolvers/warehouse/delete-warehouses.ts +0 -16
  503. package/server/graphql/resolvers/warehouse/index.ts +0 -22
  504. package/server/graphql/resolvers/warehouse/update-multiple-warehouse.ts +0 -29
  505. package/server/graphql/resolvers/warehouse/update-warehouse.ts +0 -23
  506. package/server/graphql/resolvers/warehouse/warehouse.ts +0 -17
  507. package/server/graphql/resolvers/warehouse/warehouses.ts +0 -16
  508. package/server/graphql/types/index.ts +0 -45
  509. package/server/graphql/types/inventory/index.ts +0 -61
  510. package/server/graphql/types/inventory/inventory-bundle-group-detail.ts +0 -8
  511. package/server/graphql/types/inventory/inventory-bundle-group.ts +0 -33
  512. package/server/graphql/types/inventory/inventory-list.ts +0 -8
  513. package/server/graphql/types/inventory/inventory-patch.ts +0 -41
  514. package/server/graphql/types/inventory/inventory.ts +0 -50
  515. package/server/graphql/types/inventory/new-inventory.ts +0 -29
  516. package/server/graphql/types/inventory-change/index.ts +0 -47
  517. package/server/graphql/types/inventory-change/inventory-change-list.ts +0 -8
  518. package/server/graphql/types/inventory-change/inventory-change-patch.ts +0 -10
  519. package/server/graphql/types/inventory-change/inventory-change.ts +0 -32
  520. package/server/graphql/types/inventory-change/new-inventory-change.ts +0 -8
  521. package/server/graphql/types/inventory-history/index.ts +0 -54
  522. package/server/graphql/types/inventory-history/inventory-history-list.ts +0 -8
  523. package/server/graphql/types/inventory-history/inventory-history-pallet-report-list.ts +0 -9
  524. package/server/graphql/types/inventory-history/inventory-history-pallet-report.ts +0 -13
  525. package/server/graphql/types/inventory-history/inventory-history-patch.ts +0 -36
  526. package/server/graphql/types/inventory-history/inventory-history-summary-list.ts +0 -10
  527. package/server/graphql/types/inventory-history/inventory-history-summary.ts +0 -17
  528. package/server/graphql/types/inventory-history/inventory-history.ts +0 -55
  529. package/server/graphql/types/inventory-history/new-inventory-history.ts +0 -28
  530. package/server/graphql/types/location/index.ts +0 -42
  531. package/server/graphql/types/location/location-list.ts +0 -8
  532. package/server/graphql/types/location/location-occupancy.ts +0 -10
  533. package/server/graphql/types/location/location-patch.ts +0 -17
  534. package/server/graphql/types/location/location.ts +0 -21
  535. package/server/graphql/types/location/new-location.ts +0 -15
  536. package/server/graphql/types/movement/index.ts +0 -31
  537. package/server/graphql/types/movement/movement-count.ts +0 -11
  538. package/server/graphql/types/movement/movement-list.ts +0 -8
  539. package/server/graphql/types/movement/movement-patch.ts +0 -12
  540. package/server/graphql/types/movement/movement.ts +0 -17
  541. package/server/graphql/types/movement/new-movement.ts +0 -12
  542. package/server/graphql/types/pallet/index.ts +0 -48
  543. package/server/graphql/types/pallet/new-pallet.ts +0 -11
  544. package/server/graphql/types/pallet/pallet-list.ts +0 -8
  545. package/server/graphql/types/pallet/pallet-patch.ts +0 -14
  546. package/server/graphql/types/pallet/pallet-validate.ts +0 -8
  547. package/server/graphql/types/pallet/pallet.ts +0 -18
  548. package/server/graphql/types/pallet-count/index.ts +0 -38
  549. package/server/graphql/types/pallet-count/new-pallet-count.ts +0 -8
  550. package/server/graphql/types/pallet-count/pallet-count-list.ts +0 -8
  551. package/server/graphql/types/pallet-count/pallet-count-patch.ts +0 -10
  552. package/server/graphql/types/pallet-count/pallet-count.ts +0 -14
  553. package/server/graphql/types/pallet-history/index.ts +0 -34
  554. package/server/graphql/types/pallet-history/new-pallet-history.ts +0 -8
  555. package/server/graphql/types/pallet-history/pallet-history-list.ts +0 -8
  556. package/server/graphql/types/pallet-history/pallet-history-patch.ts +0 -10
  557. package/server/graphql/types/pallet-history/pallet-history.ts +0 -14
  558. package/server/graphql/types/warehouse/index.ts +0 -35
  559. package/server/graphql/types/warehouse/new-warehouse.ts +0 -10
  560. package/server/graphql/types/warehouse/warehouse-list.ts +0 -8
  561. package/server/graphql/types/warehouse/warehouse-patch.ts +0 -12
  562. package/server/graphql/types/warehouse/warehouse.ts +0 -17
@@ -0,0 +1,1435 @@
1
+ import { Arg, Args, Ctx, Directive, FieldResolver, Query, Resolver, Root } from 'type-graphql'
2
+ import { Between, Brackets, EntityManager, getRepository, In, Raw, SelectQueryBuilder } from 'typeorm'
3
+
4
+ import { User } from '@things-factory/auth-base'
5
+ import { Bizplace, getMyBizplace, getPermittedBizplaceIds } from '@things-factory/biz-base'
6
+ import { Product } from '@things-factory/product-base'
7
+ import { convertListParams, Domain, Filter, ListParam, Pagination, Sorting } from '@things-factory/shell'
8
+
9
+ import { InventoryList } from '../inventory/inventory-types'
10
+ import { Location } from '../location/location'
11
+ import { Warehouse } from '../warehouse/warehouse'
12
+ import { InventoryHistory } from './inventory-history'
13
+ import {
14
+ InventoryHistoryList,
15
+ InventoryHistoryPalletReportList,
16
+ InventoryHistoryPatch,
17
+ InventoryHistorySummaryList
18
+ } from './inventory-history-types'
19
+
20
+ @Resolver(InventoryHistory)
21
+ export class InventoryHistoryQuery {
22
+ @Query(returns => InventoryHistoryList)
23
+ async inventoryHistories(@Args() params: ListParam, @Ctx() context: any): Promise<InventoryHistoryList> {
24
+ const { domain, user }: { domain: Domain; user: User } = context.state
25
+
26
+ if (!params.filters.find((filter: any) => filter.name === 'bizplace')) {
27
+ params.filters.push({
28
+ name: 'bizplace',
29
+ operator: 'in',
30
+ value: await getPermittedBizplaceIds(domain, user)
31
+ })
32
+ }
33
+
34
+ const convertedParams = convertListParams(params)
35
+ let [items, total] = await getRepository(InventoryHistory).findAndCount({
36
+ ...convertedParams,
37
+ relations: ['domain', 'bizplace', 'product', 'location', 'warehouse', 'creator', 'updater'],
38
+ order: {
39
+ palletId: 'DESC',
40
+ createdAt: 'ASC'
41
+ }
42
+ })
43
+
44
+ items = (await Promise.all(
45
+ items.map(async item => {
46
+ switch (item.transactionType) {
47
+ case 'UNLOADING':
48
+ item.description = 'Inbound'
49
+ break
50
+ case 'UNDO_UNLOADING':
51
+ item.description = 'Undo Unloading'
52
+ item.orderRefNo = ''
53
+ break
54
+ case 'LOADING':
55
+ item.description = 'Loading'
56
+ break
57
+ case 'UNDO_LOADING':
58
+ item.description = 'Undo Loading'
59
+ break
60
+ default:
61
+ item.description = String(item.transactionType)
62
+ break
63
+ }
64
+ item.description = item.description.toUpperCase()
65
+ return {
66
+ ...item,
67
+ orderRefNo: item.orderRefNo,
68
+ orderNo: item.orderNo,
69
+ qty: item.qty,
70
+ openingQty: item.openingQty
71
+ }
72
+ })
73
+ )) as any
74
+
75
+ return { items, total }
76
+ }
77
+
78
+ @Query(returns => InventoryHistory)
79
+ async inventoryHistory(@Arg('name') name: string, @Ctx() context: any): Promise<InventoryHistory> {
80
+ const { domain }: { domain: Domain } = context.state
81
+
82
+ return await getRepository(InventoryHistory).findOne({
83
+ where: { domain: domain, name, relations: ['domain', 'creator', 'updater'] }
84
+ })
85
+ }
86
+
87
+ @Directive('@transaction')
88
+ @Query(returns => InventoryHistoryList)
89
+ async bizplaceInventoryHistories(
90
+ @Ctx() context: any,
91
+ @Arg('inventoryHistory', type => InventoryHistoryPatch, { nullable: true })
92
+ inventoryHistory?: InventoryHistoryPatch,
93
+ @Arg('filters', type => [Filter], { nullable: true }) filters?: Filter[],
94
+ @Arg('pagination', type => Pagination, { nullable: true }) pagination?: Pagination,
95
+ @Arg('sortings', type => [Sorting], { nullable: true }) sortings?: Sorting[]
96
+ ): Promise<InventoryHistoryList> {
97
+ const { domain, user, tx }: { domain: Domain; user: User; tx: EntityManager } = context.state
98
+
99
+ const ownerBizplace: Bizplace = await getMyBizplace(domain, user)
100
+ const customerBizplace: Bizplace = await tx.getRepository(Bizplace).findOne(inventoryHistory.bizplace.id)
101
+
102
+ const productFilters = filters.find(x => x.name == 'product_info')
103
+ filters = filters.filter(x => x.name != 'product_info')
104
+
105
+ const fromDate: Date = new Date(inventoryHistory.fromDate)
106
+ const toDate: Date = new Date(inventoryHistory.toDate)
107
+
108
+ const convertedParams = convertListParams({ filters, pagination, sortings })
109
+ let where = { domain: domain }
110
+
111
+ if (productFilters) {
112
+ let productFilterValue = `%${productFilters.value.toLowerCase()}%`
113
+ const productQb: SelectQueryBuilder<Product> = getRepository(Product).createQueryBuilder('prd')
114
+
115
+ productQb.where(
116
+ new Brackets(qb => {
117
+ qb.where('Lower(prd.sku) LIKE :productInfo', { productInfo: productFilterValue })
118
+ .orWhere('Lower(prd.name) LIKE :productInfo', { productInfo: productFilterValue })
119
+ .orWhere('Lower(prd.description) LIKE :productInfo', { productInfo: productFilterValue })
120
+ })
121
+ )
122
+
123
+ const _products = await productQb.getMany()
124
+
125
+ where['product'] = In(_products.map((product: Product) => product.id))
126
+ }
127
+
128
+ if (inventoryHistory && inventoryHistory.warehouseName) {
129
+ const _warehouses: Warehouse[] = await tx.getRepository(Warehouse).find({
130
+ domain: domain,
131
+ name: Raw(alias => `LOWER(${alias}) LIKE '${inventoryHistory.warehouseName.toLowerCase()}'`)
132
+ })
133
+ where['warehouse'] = In(_warehouses.map((warehouse: Warehouse) => warehouse.id))
134
+ }
135
+
136
+ if (inventoryHistory && inventoryHistory.locationName) {
137
+ const _locations = await tx.getRepository(Location).find({
138
+ where: {
139
+ domain: domain,
140
+ name: Raw(alias => `LOWER(${alias}) LIKE '${inventoryHistory.locationName.toLowerCase()}'`)
141
+ }
142
+ })
143
+ where['location'] = In(_locations.map((location: Location) => location.id))
144
+ }
145
+
146
+ where['createdAt'] = Between(fromDate, toDate)
147
+
148
+ convertedParams.where = {
149
+ ...convertedParams.where,
150
+ ...where,
151
+ bizplace: customerBizplace
152
+ }
153
+
154
+ const result = await tx.getRepository(InventoryHistory).findAndCount({
155
+ ...convertedParams,
156
+ relations: ['domain', 'product', 'warehouse', 'location', 'bizplace', 'updater'],
157
+ order: {
158
+ palletId: 'ASC',
159
+ seq: 'ASC'
160
+ }
161
+ })
162
+
163
+ let items = result[0] as any
164
+ let total = result[1]
165
+
166
+ return { items, total }
167
+ }
168
+
169
+ @Directive('@transaction')
170
+ @Query(returns => [InventoryHistory])
171
+ async inventoryHistoryReport(@Args() params: ListParam, @Ctx() context: any): Promise<InventoryHistory[]> {
172
+ try {
173
+ const { domain, tx }: { domain: Domain; tx: EntityManager } = context.state
174
+
175
+ let bizplaceFilter = { name: '', operator: '', value: '' }
176
+ let userFilter = params.filters.find(data => data.name === 'user')
177
+ let fromDate = params.filters.find(data => data.name === 'fromDate')
178
+ let toDate = params.filters.find(data => data.name === 'toDate')
179
+ let batchNo = params.filters.find(data => data.name === 'batchNo')
180
+ let product = params.filters.find(data => data.name === 'product')
181
+ let productDesc = params.filters.find(data => data.name === 'productDescription')
182
+ let hasTransactionOrBalanceFilter = params.filters.find(data => data.name === 'hasTransactionOrBalance')
183
+ let tzoffset = params.filters.find(data => data.name === 'tzoffset').value + ' seconds'
184
+
185
+ if (userFilter) {
186
+ const user: User = await tx.getRepository(User).findOne({
187
+ where: { id: userFilter.value },
188
+ relations: ['bizplaces']
189
+ })
190
+
191
+ const bizplace: Bizplace = user.bizplaces[0]
192
+
193
+ if (!bizplace) throw 'Invalid input'
194
+
195
+ bizplaceFilter = { name: 'bizplace', operator: 'eq', value: bizplace.id }
196
+ } else {
197
+ bizplaceFilter = params.filters.find(data => data.name === 'bizplace')
198
+ }
199
+
200
+ if (!bizplaceFilter || !fromDate || !toDate) throw 'Invalid input'
201
+
202
+ const bizplace: Bizplace = await tx.getRepository(Bizplace).findOne({
203
+ id: bizplaceFilter.value
204
+ })
205
+
206
+ let batchNoQuery = ''
207
+ if (batchNo) {
208
+ batchNoQuery =
209
+ 'AND Lower(invh.batch_id) LIKE ANY(ARRAY[' +
210
+ batchNo.value
211
+ .toLowerCase()
212
+ .split(',')
213
+ .map(prod => {
214
+ return "'%" + prod.trim().replace(/'/g, "''") + "%'"
215
+ })
216
+ .join(',') +
217
+ '])'
218
+ }
219
+
220
+ let productQuery = ''
221
+ if (product) {
222
+ let productValue = product.value
223
+ .toLowerCase()
224
+ .split(',')
225
+ .map(prod => {
226
+ return "'%" + prod.trim().replace(/'/g, "''") + "%'"
227
+ })
228
+ .join(',')
229
+ productQuery = `AND (
230
+ Lower(prd.name) LIKE ANY(ARRAY[${productValue}])
231
+ OR Lower(prd.sku) LIKE ANY(ARRAY[${productValue}])
232
+ OR Lower(prd.description) LIKE ANY(ARRAY[${productValue}])
233
+ )`
234
+ }
235
+
236
+ let productDescQuery = ''
237
+ if (productDesc) {
238
+ productDescQuery = "AND Lower(description) LIKE '%" + productDesc.value.toLowerCase() + "%'"
239
+ }
240
+
241
+ let hasTransactionOrBalanceQuery = ''
242
+ if (hasTransactionOrBalanceFilter && hasTransactionOrBalanceFilter.value) {
243
+ hasTransactionOrBalanceQuery = 'and (src.totalRow > 1 or src.totalQty <> 0)'
244
+ }
245
+
246
+ await tx.query(
247
+ `
248
+ create temp table temp_products AS
249
+ (
250
+ select prd.*, prd.id::varchar as product_id from products prd
251
+ inner join bizplaces b on b.id = prd.bizplace_id
252
+ inner join companies c on c.domain_id = b.domain_id
253
+ inner join bizplaces b2 on b2.company_id = c.id
254
+ where b2.id = $1
255
+ ${productQuery}
256
+ )`,
257
+ [bizplace.id]
258
+ )
259
+
260
+ await tx.query(
261
+ `
262
+ create temp table temp_data_src AS
263
+ (
264
+ SELECT prd.name AS product_name, prd.description AS product_description, prd.type as product_type, prd.aux_value_1 AS product_aux_value_1, trim(invh.batch_id) as batch_id, invh.product_id,
265
+ invh.packing_type, invh.bizplace_id, invh.domain_id,
266
+ invh.ref_order_id, invh.order_no, invh.order_ref_no, invh.transaction_type, invh.created_at,
267
+ invh.qty, invh.opening_qty,
268
+ invh.uom, COALESCE(invh.uom_value, 0) as uom_value, COALESCE(invh.opening_uom_value, 0) as opening_uom_value
269
+ FROM reduced_inventory_histories invh
270
+ INNER JOIN temp_products prd ON prd.id = invh.product_id::uuid
271
+ WHERE
272
+ invh.domain_id = $1
273
+ AND invh.bizplace_id = $2
274
+ AND invh.created_at <= $3::timestamp
275
+ ${batchNoQuery}
276
+ )
277
+ `,
278
+ [domain.id, bizplace.id, toDate.value]
279
+ )
280
+
281
+ await tx.query(
282
+ `
283
+ create temp table temp_inv_history as (
284
+ SELECT src.product_name, src.product_description, src.product_type, src.product_aux_value_1, src.batch_id, src.product_id, src.packing_type, src.uom,
285
+ src.bizplace_id, src.domain_id,
286
+ SUM(COALESCE(src.qty,0)) AS qty,
287
+ SUM(COALESCE(src.opening_qty,0)) AS opening_qty,
288
+ SUM(COALESCE(src.uom_value,0)) AS uom_value,
289
+ SUM(COALESCE(src.opening_uom_value,0)) AS opening_uom_value,
290
+ 'Opening Balance' AS order_name,
291
+ '-' AS ref_no,
292
+ 0 AS rn,
293
+ $1::timestamp AS created_at,
294
+ $1::date AS created_date
295
+ FROM temp_data_src src
296
+ WHERE src.created_at < $1::timestamp
297
+ GROUP BY src.product_name, src.product_description, src.product_type, src.product_aux_value_1, src.batch_id, src.product_id, src.packing_type, src.uom,
298
+ src.bizplace_id, src.domain_id
299
+ UNION ALL
300
+ SELECT product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
301
+ domain_id, sum(qty) as qty, sum(opening_qty) as opening_qty, sum(uom_value) as uom_value, sum(opening_uom_value) as opening_uom_value,
302
+ order_name, ref_no, 1 AS rn, MIN(created_at) - $2::interval as created_at, (MIN(created_at) - $2::interval)::DATE as created_date
303
+ FROM (
304
+ SELECT invh.product_name, invh.product_description, invh.product_type, invh.product_aux_value_1, invh.batch_id, invh.product_id, invh.packing_type, invh.bizplace_id,
305
+ invh.domain_id,
306
+ invh.qty, invh.opening_qty, invh.uom_value, invh.uom, invh.opening_uom_value,
307
+ COALESCE(order_no, '-') AS order_name,
308
+ COALESCE(order_ref_no, '-') AS ref_no,
309
+ invh.created_at,
310
+ invh.created_at::date as created_date
311
+ FROM temp_data_src invh
312
+ WHERE (invh.qty <> 0 OR invh.uom_value <> 0) AND
313
+ invh.transaction_type <> 'ADJUSTMENT' AND
314
+ invh.transaction_type <>'NEW'
315
+ AND invh.created_at >= $1::timestamp
316
+ ) AS inv_movement
317
+ GROUP BY product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
318
+ domain_id, order_name, ref_no, rn
319
+ UNION ALL
320
+ SELECT product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
321
+ domain_id, sum(qty) as qty, sum(opening_qty) as opening_qty, sum(uom_value) as uom_value, sum(opening_uom_value) as opening_uom_value,
322
+ order_name, ref_no, 1 AS rn, created_at - $2::interval as created_at, created_at::date as created_date
323
+ FROM (
324
+ SELECT invh.product_name, invh.product_description, invh.product_type, invh.product_aux_value_1, invh.batch_id, invh.product_id, invh.packing_type, invh.uom, invh.bizplace_id,
325
+ invh.domain_id,
326
+ invh.qty, invh.opening_qty, invh.uom_value, invh.opening_uom_value,
327
+ invh.transaction_type AS order_name,
328
+ invh.transaction_type AS ref_no,
329
+ invh.created_at,
330
+ invh.created_at:: date as created_date
331
+ FROM temp_data_src invh
332
+ WHERE (invh.qty <> 0 OR invh.uom_value <> 0) AND
333
+ (invh.transaction_type = 'ADJUSTMENT' OR
334
+ invh.transaction_type = 'NEW')
335
+ AND invh.created_at >= $1::timestamp
336
+ ) AS inv_movement
337
+ GROUP BY product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
338
+ domain_id, order_name, ref_no, rn, created_at
339
+ )`,
340
+ [fromDate.value, tzoffset]
341
+ )
342
+
343
+ const result: any = await tx.query(
344
+ `
345
+ select product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
346
+ domain_id, qty, opening_qty, round(uom_value::decimal,2) as uom_value, opening_uom_value, order_name, ref_no, created_at::date
347
+ from temp_inv_history invh where
348
+ exists (
349
+ select * from (
350
+ select batch_id, product_name, packing_type, sum(qty) as totalQty, count(*) as totalRow from temp_inv_history ih2
351
+ group by batch_id, product_name, packing_type
352
+ ) src
353
+ where src.batch_id = invh.batch_id and src.product_name = invh.product_name and src.packing_type = invh.packing_type
354
+ ${hasTransactionOrBalanceQuery}
355
+ )
356
+ ORDER BY invh.product_name asc, invh.product_description asc, invh.packing_type asc, invh.batch_id asc, invh.uom asc, invh.rn asc, invh.created_at asc
357
+ `
358
+ )
359
+
360
+ tx.query(
361
+ `
362
+ drop table temp_products, temp_data_src, temp_inv_history
363
+ `
364
+ )
365
+
366
+ let items = result as any
367
+ items = items.map(item => {
368
+ return {
369
+ batchId: item.batch_id,
370
+ bizplace: bizplace,
371
+ packingType: item.packing_type,
372
+ product: {
373
+ id: item.product_id,
374
+ name:
375
+ item.product_name +
376
+ ' ( ' +
377
+ item.product_description +
378
+ ' ) ' +
379
+ (item.product_aux_value_1 === null ? '' : ' [ ' + item.product_aux_value_1 + ' ] '),
380
+ type: item.product_type
381
+ },
382
+ qty: item.qty,
383
+ uom: item.uom,
384
+ uomValue: item.uom_value,
385
+ openingQty: item.opening_qty,
386
+ openingUomValue: item.opening_uom_value,
387
+ orderName: item.order_name,
388
+ orderRefNo: item.ref_no,
389
+ createdAt: item.created_at
390
+ }
391
+ })
392
+
393
+ return items
394
+ } catch (error) {
395
+ throw error
396
+ }
397
+ }
398
+
399
+ @Directive('@transaction')
400
+ @Query(returns => [InventoryHistory])
401
+ async inventoryHistoryPalletReport(@Args() params: ListParam, @Ctx() context: any): Promise<InventoryHistory[]> {
402
+ try {
403
+ const { domain, tx }: { domain: Domain; tx: EntityManager } = context.state
404
+ let userFilter = params.filters.find(data => data.name === 'user')
405
+
406
+ let bizplaceFilter = { name: '', operator: '', value: '' }
407
+
408
+ if (userFilter) {
409
+ const user: User = await tx.getRepository(User).findOne({
410
+ where: { id: userFilter.value },
411
+ relations: ['bizplaces']
412
+ })
413
+
414
+ const bizplace: Bizplace = user.bizplaces[0]
415
+
416
+ if (!bizplace) throw 'Invalid input'
417
+
418
+ bizplaceFilter = { name: 'bizplace', operator: 'eq', value: bizplace.id }
419
+ } else {
420
+ bizplaceFilter = params.filters.find(data => data.name === 'bizplace')
421
+ }
422
+
423
+ let fromDate = params.filters.find(data => data.name === 'fromDate')
424
+ let toDate = params.filters.find(data => data.name === 'toDate')
425
+ let product = params.filters.find(data => data.name === 'product')
426
+
427
+ if (!fromDate || !toDate) throw 'Invalid input'
428
+
429
+ let bizplaceQuery = ''
430
+ if (bizplaceFilter) {
431
+ const bizplace: Bizplace = await tx.getRepository(Bizplace).findOne({
432
+ id: bizplaceFilter.value
433
+ })
434
+
435
+ if (bizplace) {
436
+ bizplaceQuery = "AND i2.bizplace_id = '" + bizplace.id + "'"
437
+ } else {
438
+ throw 'Invalid input'
439
+ }
440
+ }
441
+
442
+ let productQuery = ''
443
+ if (product) {
444
+ productQuery =
445
+ 'AND prd.name ILIKE ANY(ARRAY[' +
446
+ product.value
447
+ .split(',')
448
+ .map(prod => {
449
+ return "'%" + prod.trim().replace(/'/g, "''") + "%'"
450
+ })
451
+ .join(',') +
452
+ '])'
453
+ }
454
+
455
+ const result = await tx.getRepository(InventoryHistory).query(`
456
+ with invHistory as (
457
+ select case when ar.name is null then 'NEW' else ar.name end as arrival_notice_name,
458
+ case when js.container_size is null then '' else js.container_size end as container_size,
459
+ i2.batch_id,
460
+ bzp.name as bizplace_name,
461
+ i2.pallet_id, ih.seq, ih.status, ih.transaction_type, i2.product_id, prd.name as product_name,
462
+ prd.description as product_description, ih.id as inventory_history_id, ih.packing_type, ih.qty, ih.opening_qty,
463
+ ih.created_at::date
464
+ from inventories i2
465
+ inner join reduced_inventory_histories ih on ih.pallet_id = i2.pallet_id and ih.domain_id = i2.domain_id
466
+ inner join products prd on prd.id = i2.product_id
467
+ inner join bizplaces bzp on bzp.id = ih.bizplace_id
468
+ left join order_inventories oi on oi.inventory_id = i2.id and oi.arrival_notice_id is not null
469
+ left join arrival_notices ar on ar.id = oi.arrival_notice_id
470
+ left join job_sheets js on js.id = ar.job_sheet_id
471
+ where
472
+ i2.domain_id = '${domain.id}'
473
+ and (
474
+ (ih.status = 'STORED' and ih.transaction_type = 'NEW')
475
+ or (ih.status = 'STORED' and ih.transaction_type = 'CANCEL_ORDER')
476
+ or (ih.status = 'STORED' and ih.transaction_type = 'RETURN')
477
+ or (ih.status = 'STORED' and ih.transaction_type = 'PUTAWAY')
478
+ or (ih.status = 'TERMINATED' and ih.transaction_type = 'TERMINATED')
479
+ )
480
+ ${bizplaceQuery}
481
+ ${productQuery}
482
+ order by ih.pallet_id, ih.seq
483
+ ), inventoryHistoryMovement as (
484
+ select bizplace_name, container_size, pallet_id, seq, status, transaction_type, product_id, product_name, product_description, batch_id,
485
+ inventory_history_id, packing_type, qty, opening_qty, created_at, arrival_notice_name from (
486
+ select row_number() over(partition by pallet_id order by created_at asc) as rn, * from invHistory where status = 'STORED'
487
+ )as invIn where rn = 1
488
+ union all
489
+ select bizplace_name, container_size, pallet_id, seq, status, transaction_type, product_id, product_name, product_description, batch_id,
490
+ inventory_history_id, packing_type, qty, opening_qty, created_at, arrival_notice_name from (
491
+ select row_number() over(partition by pallet_id order by created_at desc) as rn, * from invHistory
492
+ )as invOut where rn = 1 and status = 'TERMINATED'
493
+ ), inventoryHistoriesByPallet as (
494
+ select invHistory.bizplace_name, invHistory.container_size, invHistory.product_name, invHistory.product_description, invHistory.arrival_notice_name, invHistory.batch_id,
495
+ SUM(case when invHistory.created_at <= '${new Date(fromDate.value).toLocaleDateString()} 00:00:00' then
496
+ case when invHistory.status = 'STORED' then 1 else -1 end
497
+ else 0 end) as opening_balance,
498
+ SUM(case when invHistory.created_at >= '${new Date(fromDate.value).toLocaleDateString()} 00:00:00'
499
+ and invHistory.created_at <= '${new Date(toDate.value).toLocaleDateString()} 23:59:59' then
500
+ case when invHistory.status = 'STORED' then 1 else 0 end
501
+ else 0 end) as in_balance,
502
+ SUM(case when invHistory.created_at >= '${new Date(fromDate.value).toLocaleDateString()} 00:00:00'
503
+ and invHistory.created_at <= '${new Date(toDate.value).toLocaleDateString()} 23:59:59' then
504
+ case when invHistory.status = 'TERMINATED' then 1 else 0 end
505
+ else 0 end) as out_balance
506
+ from(
507
+ select * from inventoryHistoryMovement
508
+ ) as invHistory group by bizplace_name, container_size, product_name, product_description, arrival_notice_name, batch_id
509
+ )
510
+ select invh.*, invh.opening_balance + invh.in_balance - invh.out_balance as closing_balance
511
+ , (
512
+ select json_agg(json_build_object('created_at', created_at, 'in_balance', in_balance, 'out_balance', out_balance)) as jsonData from
513
+ (
514
+ select bizplace_name, product_name, batch_id, arrival_notice_name, created_at,
515
+ SUM(case when status = 'STORED' then 1 else 0 end) as in_balance,
516
+ SUM(case when status = 'TERMINATED' then 1 else 0 end) as out_balance
517
+ from inventoryHistoryMovement
518
+ where created_at >= '${new Date(fromDate.value).toLocaleDateString()} 00:00:00' and
519
+ created_at <= '${new Date(toDate.value).toLocaleDateString()} 23:59:59' and
520
+ bizplace_name = invh.bizplace_name and product_name = invh.product_name and
521
+ batch_id = invh.batch_id and arrival_notice_name = invh.arrival_notice_name
522
+ group by bizplace_name, product_name, batch_id, arrival_notice_name, created_at
523
+ order by created_at
524
+ )
525
+ as t
526
+ )::varchar as json_date_movement
527
+ from inventoryHistoriesByPallet invh
528
+ where invh.opening_balance >= 0
529
+ and invh.in_balance >= 0
530
+ and invh.out_balance >= 0
531
+ and (invh.opening_balance > 0 or invh.in_balance > 0)
532
+ order by invh.bizplace_name, invh.product_name, invh.batch_id;
533
+ `)
534
+
535
+ let items = result as any
536
+ items = items.map(item => {
537
+ return {
538
+ bizplace: { name: item.bizplace_name },
539
+ product: {
540
+ name: item.product_name.trim() + ' ( ' + (item.product_description || '-').trim() + ' )'
541
+ },
542
+ arrivalNoticeName: item.arrival_notice_name,
543
+ batchId: item.batch_id,
544
+ openingBalance: item.opening_balance,
545
+ inBalance: item.in_balance,
546
+ outBalance: item.out_balance,
547
+ closingBalance: item.closing_balance,
548
+ jsonDateMovement: item.json_date_movement,
549
+ containerSize: item.container_size
550
+ }
551
+ })
552
+
553
+ return items
554
+ } catch (error) {
555
+ throw error
556
+ }
557
+ }
558
+
559
+ @Directive('@transaction')
560
+ @Query(returns => [InventoryHistory])
561
+ async inventoryHistoryPalletDetailReport(
562
+ @Args() params: ListParam,
563
+ @Ctx() context: any
564
+ ): Promise<InventoryHistory[]> {
565
+ try {
566
+ const { domain, tx }: { domain: Domain; tx: EntityManager } = context.state
567
+ let userFilter = params.filters.find(data => data.name === 'user')
568
+
569
+ let bizplaceFilter = { name: '', operator: '', value: '' }
570
+
571
+ if (userFilter) {
572
+ const user: User = await tx.getRepository(User).findOne({
573
+ where: { id: userFilter.value },
574
+ relations: ['bizplaces']
575
+ })
576
+
577
+ const bizplace: Bizplace = user.bizplaces[0]
578
+
579
+ if (!bizplace) throw 'Invalid input'
580
+
581
+ bizplaceFilter = { name: 'bizplace', operator: 'eq', value: bizplace.id }
582
+ } else {
583
+ bizplaceFilter = params.filters.find(data => data.name === 'bizplace')
584
+ }
585
+
586
+ let fromDate = params.filters.find(data => data.name === 'fromDate')
587
+ let toDate = params.filters.find(data => data.name === 'toDate')
588
+ let product = params.filters.find(data => data.name === 'product')
589
+
590
+ if (!fromDate || !toDate) throw 'Invalid input'
591
+
592
+ let bizplaceQuery = ''
593
+ if (bizplaceFilter) {
594
+ const bizplace: Bizplace = await tx.getRepository(Bizplace).findOne({
595
+ id: bizplaceFilter.value
596
+ })
597
+
598
+ if (bizplace) {
599
+ bizplaceQuery = "AND i2.bizplace_id = '" + bizplace.id + "'"
600
+ } else {
601
+ throw 'Invalid input'
602
+ }
603
+ }
604
+
605
+ let productQuery = ''
606
+ if (product) {
607
+ productQuery =
608
+ 'AND prd.name ILIKE ANY(ARRAY[' +
609
+ product.value
610
+ .split(',')
611
+ .map(prod => {
612
+ return "'%" + prod.trim().replace(/'/g, "''") + "%'"
613
+ })
614
+ .join(',') +
615
+ '])'
616
+ }
617
+
618
+ const result = await tx.getRepository(InventoryHistory).query(`
619
+ with invHistory as (
620
+ select case when ar.name is null then 'NEW' else ar.name end as arrival_notice_name,
621
+ case when js.container_size is null then '' else js.container_size end as container_size,
622
+ i2.batch_id,
623
+ bzp.name as bizplace_name,
624
+ i2.pallet_id, ih.seq, ih.status, ih.transaction_type, i2.product_id, prd.name as product_name,
625
+ prd.description as product_description, ih.id as inventory_history_id, ih.packing_type, ih.qty, ih.opening_qty,
626
+ ih.created_at::date
627
+ from inventories i2
628
+ inner join reduced_inventory_histories ih on ih.pallet_id = i2.pallet_id and ih.domain_id = i2.domain_id
629
+ inner join products prd on prd.id = i2.product_id
630
+ inner join bizplaces bzp on bzp.id = ih.bizplace_id
631
+ left join order_inventories oi on oi.inventory_id = i2.id and oi.arrival_notice_id is not null
632
+ left join arrival_notices ar on ar.id = oi.arrival_notice_id
633
+ left join job_sheets js on js.id = ar.job_sheet_id
634
+ where
635
+ i2.domain_id = '${domain.id}'
636
+ and (
637
+ (ih.status = 'STORED' and ih.transaction_type = 'NEW')
638
+ or (ih.status = 'STORED' and ih.transaction_type = 'CANCEL_ORDER')
639
+ or (ih.status = 'STORED' and ih.transaction_type = 'RETURN')
640
+ or (ih.status = 'STORED' and ih.transaction_type = 'PUTAWAY')
641
+ or (ih.status = 'TERMINATED' and ih.transaction_type = 'TERMINATED')
642
+ )
643
+ ${bizplaceQuery}
644
+ ${productQuery}
645
+ order by ih.pallet_id, ih.seq
646
+ ), inventoryHistoryMovement as (
647
+ select bizplace_name, container_size, pallet_id, seq, status, transaction_type, product_id, product_name, product_description, batch_id,
648
+ inventory_history_id, packing_type, qty, opening_qty, created_at, arrival_notice_name from (
649
+ select row_number() over(partition by pallet_id order by created_at asc) as rn, * from invHistory where status = 'STORED'
650
+ )as invIn where rn = 1
651
+ union all
652
+ select bizplace_name, container_size, pallet_id, seq, status, transaction_type, product_id, product_name, product_description, batch_id,
653
+ inventory_history_id, packing_type, qty, opening_qty, created_at, arrival_notice_name from (
654
+ select row_number() over(partition by pallet_id order by created_at desc) as rn, * from invHistory
655
+ )as invOut where rn = 1 and status = 'TERMINATED'
656
+ ), inventoryHistoriesByPallet as (
657
+ select invHistory.bizplace_name, invHistory.container_size, invHistory.product_name, invHistory.product_description, invHistory.arrival_notice_name, invHistory.batch_id,
658
+ SUM(case when invHistory.created_at <= '${new Date(fromDate.value).toLocaleDateString()} 00:00:00' then
659
+ case when invHistory.status = 'STORED' then 1 else -1 end
660
+ else 0 end) as opening_balance,
661
+ SUM(case when invHistory.created_at >= '${new Date(fromDate.value).toLocaleDateString()} 00:00:00'
662
+ and invHistory.created_at <= '${new Date(toDate.value).toLocaleDateString()} 23:59:59' then
663
+ case when invHistory.status = 'STORED' then 1 else 0 end
664
+ else 0 end) as in_balance,
665
+ SUM(case when invHistory.created_at >= '${new Date(fromDate.value).toLocaleDateString()} 00:00:00'
666
+ and invHistory.created_at <= '${new Date(toDate.value).toLocaleDateString()} 23:59:59' then
667
+ case when invHistory.status = 'TERMINATED' then 1 else 0 end
668
+ else 0 end) as out_balance
669
+ from(
670
+ select * from inventoryHistoryMovement
671
+ ) as invHistory group by bizplace_name, container_size, product_name, product_description, arrival_notice_name, batch_id
672
+ )
673
+ select invh.*, invh.opening_balance + invh.in_balance - invh.out_balance as closing_balance
674
+ , (
675
+ select json_agg(json_build_object('created_at', created_at, 'in_balance', in_balance, 'out_balance', out_balance)) as jsonData from
676
+ (
677
+ select bizplace_name, product_name, batch_id, arrival_notice_name, created_at,
678
+ SUM(case when status = 'STORED' then 1 else 0 end) as in_balance,
679
+ SUM(case when status = 'TERMINATED' then 1 else 0 end) as out_balance
680
+ from inventoryHistoryMovement
681
+ where created_at >= '${new Date(fromDate.value).toLocaleDateString()} 00:00:00' and
682
+ created_at <= '${new Date(toDate.value).toLocaleDateString()} 23:59:59' and
683
+ bizplace_name = invh.bizplace_name and product_name = invh.product_name and
684
+ batch_id = invh.batch_id and arrival_notice_name = invh.arrival_notice_name
685
+ group by bizplace_name, product_name, batch_id, arrival_notice_name, created_at
686
+ order by created_at
687
+ )
688
+ as t
689
+ )::varchar as json_date_movement
690
+ from inventoryHistoriesByPallet invh
691
+ where invh.opening_balance >= 0
692
+ and invh.in_balance >= 0
693
+ and invh.out_balance >= 0
694
+ and (invh.opening_balance > 0 or invh.in_balance > 0)
695
+ order by invh.bizplace_name, invh.product_name, invh.batch_id;
696
+ `)
697
+
698
+ let items = result as any
699
+ items = items.map(item => {
700
+ return {
701
+ bizplace: { name: item.bizplace_name },
702
+ product: {
703
+ name: item.product_name.trim() + ' ( ' + (item.product_description || '-').trim() + ' )'
704
+ },
705
+ arrivalNoticeName: item.arrival_notice_name,
706
+ batchId: item.batch_id,
707
+ openingBalance: item.opening_balance,
708
+ inBalance: item.in_balance,
709
+ outBalance: item.out_balance,
710
+ closingBalance: item.closing_balance,
711
+ jsonDateMovement: item.json_date_movement,
712
+ containerSize: item.container_size
713
+ }
714
+ })
715
+
716
+ return items
717
+ } catch (error) {
718
+ throw error
719
+ }
720
+ }
721
+
722
+ @Directive('@transaction')
723
+ @Query(returns => InventoryHistoryPalletReportList)
724
+ async inventoryHistoryPalletStorageReport(
725
+ @Args() params: ListParam,
726
+ @Ctx() context: any
727
+ ): Promise<InventoryHistoryPalletReportList> {
728
+ try {
729
+ const { domain, tx }: { domain: Domain; tx: EntityManager } = context.state
730
+
731
+ let userFilter = params.filters.find(data => data.name === 'user')
732
+
733
+ let bizplaceFilter = { name: '', operator: '', value: '' }
734
+
735
+ if (userFilter) {
736
+ const user: User = await tx.getRepository(User).findOne({
737
+ where: { id: userFilter.value },
738
+ relations: ['bizplaces']
739
+ })
740
+
741
+ const bizplace: Bizplace = user.bizplaces[0]
742
+
743
+ if (!bizplace) throw 'Invalid input'
744
+
745
+ bizplaceFilter = { name: 'bizplace', operator: 'eq', value: bizplace.id }
746
+ } else {
747
+ bizplaceFilter = params.filters.find(data => data.name === 'bizplace')
748
+ }
749
+
750
+ let fromDate = params.filters.find(data => data.name === 'fromDate')
751
+ let toDate = params.filters.find(data => data.name === 'toDate')
752
+
753
+ if (!bizplaceFilter || !fromDate || !toDate) throw 'Invalid input'
754
+
755
+ const bizplace: Bizplace = await tx.getRepository(Bizplace).findOne({
756
+ id: bizplaceFilter.value
757
+ })
758
+
759
+ let queryFilter = ``
760
+ let queryParams = [fromDate.value, toDate.value]
761
+ let locationType = params.filters.find(data => data.name === 'locationType')
762
+ if (locationType) {
763
+ queryFilter = queryFilter + `AND location_type = $` + (queryParams.length + 1)
764
+ queryParams.push(locationType.value)
765
+ }
766
+
767
+ let zone = params.filters.find(data => data.name === 'zone')
768
+ if (zone) {
769
+ queryFilter = queryFilter + `AND LOWER(location_zone) like $` + (queryParams.length + 1)
770
+ queryParams.push(zone.value.toLowerCase())
771
+ }
772
+
773
+ await tx.query(
774
+ `
775
+ create temp table temp_history as (
776
+ select distinct on (ih.pallet_id)
777
+ ih.pallet_id, ih.created_at as in_at, loc.name as location_name, loc."type" as location_type, loc."zone" as location_zone,
778
+ bz.name as bizplace_name
779
+ from reduced_inventory_histories ih
780
+ inner join inventories inv on inv.pallet_id = ih.pallet_id and inv.domain_id = ih.domain_id
781
+ inner join locations loc on loc.id = inv.location_id
782
+ inner join bizplaces bz on bz.id = inv.bizplace_id
783
+ where ih.domain_id = $1
784
+ and inv.bizplace_id = $2
785
+ and not exists(
786
+ SELECT domain_id, pallet_id
787
+ FROM reduced_inventory_histories ih2
788
+ where ih2.domain_id = $1
789
+ and ih2.created_at < $3
790
+ and ih2.transaction_type = 'TERMINATED' and ih2.status ='TERMINATED'
791
+ and ih2.pallet_id = ih.pallet_id
792
+ group by ih2.domain_id , ih2.pallet_id
793
+ )
794
+ and ih.created_at <= $4
795
+ order by ih.pallet_id , ih.seq
796
+ )
797
+ `,
798
+ [domain.id, bizplace.id, fromDate.value, toDate.value]
799
+ )
800
+
801
+ await tx.query(
802
+ `
803
+ create temp table temp_pallet_storage_history as (
804
+ select * from (
805
+ select bizplace_name, location_type, location_zone, location_name,
806
+ COUNT(pallet_id) filter (where in_at between $1 and $2) as inbound_qty,
807
+ COUNT(*) as total_qty,
808
+ string_agg(case when in_at between $1 and $2 then pallet_id end, ', ') as inbound_pallet_id,
809
+ string_agg(case when in_at not between $1 and $2 then pallet_id end, ', ') as pallet_id
810
+ from temp_history where location_type = 'SHELF'
811
+ group by bizplace_name, location_name, location_type, location_zone
812
+ union
813
+ select bizplace_name, location_type, location_zone, location_name,
814
+ case when in_at between $1 and $2 then 1 else 0 end as inbound_qty,
815
+ 1 as total_qty,
816
+ case when in_at between $1 and $2 then pallet_id end as inbound_pallet_id,
817
+ case when in_at not between $1 and $2 then pallet_id end pallet_id
818
+ from temp_history where location_type = 'FLOOR'
819
+ ) as src where 1 = 1
820
+ ${queryFilter}
821
+ )
822
+ `,
823
+ queryParams
824
+ )
825
+
826
+ const total: any = await tx.query(`select count(*) from temp_pallet_storage_history`)
827
+
828
+ const totalWithOpeningBalance: any = await tx.query(
829
+ `select count(*) from temp_pallet_storage_history where pallet_id notnull`
830
+ )
831
+
832
+ const result: any = await tx.query(
833
+ `
834
+ select * from temp_pallet_storage_history order by location_type OFFSET $1 LIMIT $2
835
+ `,
836
+ [(params.pagination.page - 1) * params.pagination.limit, params.pagination.limit]
837
+ )
838
+
839
+ tx.query(`
840
+ drop table temp_history, temp_pallet_storage_history
841
+ `)
842
+
843
+ let items = result.map(itm => {
844
+ return {
845
+ ...itm,
846
+ bizplace: {
847
+ name: itm.bizplace_name
848
+ },
849
+ location: {
850
+ name: itm.location_name,
851
+ type: itm.location_type,
852
+ zone: itm.location_zone
853
+ },
854
+ palletId: itm.pallet_id,
855
+ inboundPalletId: itm.inbound_pallet_id,
856
+ inboundQty: itm.inbound_qty,
857
+ totalQty: itm.total_qty
858
+ }
859
+ })
860
+
861
+ return { items, total: total[0].count, totalWithOpeningBalance: totalWithOpeningBalance[0].count }
862
+ } catch (ex) {
863
+ throw ex
864
+ }
865
+ }
866
+
867
+ @Directive('@transaction')
868
+ @Query(returns => InventoryHistorySummaryList)
869
+ async inventoryHistorySummaryReport(
870
+ @Args() params: ListParam,
871
+ @Ctx() context: any
872
+ ): Promise<InventoryHistorySummaryList> {
873
+ try {
874
+ const { tx }: { tx: EntityManager } = context.state
875
+
876
+ let bizplaceFilter = { name: '', operator: '', value: '' }
877
+
878
+ let userFilter = params.filters.find(data => data.name === 'user')
879
+ let fromDate = params.filters.find(data => data.name === 'fromDate')
880
+ let toDate = params.filters.find(data => data.name === 'toDate')
881
+ let byPallet = params.filters.find(data => data.name === 'byPallet')
882
+
883
+ if (userFilter) {
884
+ const user: User = await tx.getRepository(User).findOne({
885
+ where: { id: userFilter.value },
886
+ relations: ['bizplaces']
887
+ })
888
+
889
+ const bizplace: Bizplace = user.bizplaces[0]
890
+
891
+ if (!bizplace) throw 'Invalid input'
892
+
893
+ bizplaceFilter = { name: 'bizplace', operator: 'eq', value: bizplace.id }
894
+ } else {
895
+ bizplaceFilter = params.filters.find(data => data.name === 'bizplace')
896
+ }
897
+
898
+ if (!bizplaceFilter || !fromDate || !toDate) throw 'Invalid input'
899
+
900
+ const bizplace: Bizplace = await tx.getRepository(Bizplace).findOne({
901
+ id: bizplaceFilter.value
902
+ })
903
+
904
+ let [result, total] = byPallet.value
905
+ ? await massageInventoryPalletSummary(tx, params, bizplace, context)
906
+ : await massageInventorySummary(tx, params, bizplace, context)
907
+
908
+ let items = result.map(itm => {
909
+ return {
910
+ ...itm,
911
+ batchId: itm.batch_id,
912
+ packingType: itm.packing_type,
913
+ packingSize: itm.packing_size,
914
+ openingQty: itm.opening_qty,
915
+ adjustmentQty: itm.adjustment_qty,
916
+ closingQty: itm.closing_qty,
917
+ totalInQty: itm.total_in_qty,
918
+ totalOutQty: itm.total_out_qty,
919
+ product: {
920
+ id: itm.product_id,
921
+ sku: itm.product_sku,
922
+ name: itm.product_name,
923
+ description: itm.product_description,
924
+ type: itm.product_type
925
+ }
926
+ }
927
+ })
928
+
929
+ return {
930
+ items,
931
+ total: total[0].count,
932
+ totalInboundQty: total[0].totalinqty || 0,
933
+ totalOpeningBal: total[0].totalopeningbal || 0
934
+ }
935
+ } catch (error) {
936
+ throw error
937
+ }
938
+ }
939
+
940
+ @Directive('@transaction')
941
+ @Query(returns => InventoryList)
942
+ async onhandInventories(
943
+ @Ctx() context: any,
944
+ @Arg('filters', type => [Filter], { nullable: true }) filters?: Filter[],
945
+ @Arg('pagination', type => Pagination, { nullable: true }) pagination?: Pagination,
946
+ @Arg('sortings', type => [Sorting], { nullable: true }) sortings?: Sorting[],
947
+ @Arg('locationSortingRules', type => [Sorting], { nullable: true }) locationSortingRules?: Sorting[]
948
+ ): Promise<InventoryList> {
949
+ const { domain, user, tx }: { domain: Domain; user: User; tx: EntityManager } = context.state
950
+
951
+ // define all inputs
952
+ let userFilter = filters.find(data => data.name === 'user')
953
+ let product = filters.find(data => data.name === 'product')
954
+ let createdAt = filters.find(data => data.name === 'created_at')
955
+ let location = filters.find(data => data.name === 'location')
956
+ let batchId = filters.find(data => data.name === 'batchId')
957
+ let palletId = filters.find(data => data.name === 'palletId')
958
+ let cartonId = filters.find(data => data.name === 'cartonId')
959
+ let packingType = filters.find(data => data.name === 'packingType')
960
+ let timezoneOffset = filters.find(data => data.name === 'timezoneOffset').value + ' minutes'
961
+
962
+ // expiration date filter
963
+ let expiration = filters.find(data => data.name === 'expiration').value
964
+ let expFromDate = filters.find(data => data.name === 'expFromDate')
965
+ let expToDate = filters.find(data => data.name === 'expToDate')
966
+
967
+ let bizplaceFilter = { name: '', operator: '', value: [] }
968
+
969
+ // prepare query filters
970
+ if (userFilter) {
971
+ const user: User = await tx.getRepository(User).findOne({
972
+ where: { id: userFilter.value },
973
+ relations: ['bizplaces']
974
+ })
975
+
976
+ const bizplace: Bizplace = user.bizplaces[0]
977
+
978
+ if (!bizplace) throw 'Invalid input'
979
+
980
+ bizplaceFilter = { name: 'bizplace', operator: 'eq', value: [bizplace.id] }
981
+ } else {
982
+ bizplaceFilter = !filters.find((filter: any) => filter.name === 'bizplace')
983
+ ? {
984
+ name: 'bizplace',
985
+ operator: 'in',
986
+ value: await getPermittedBizplaceIds(domain, user),
987
+ relation: true
988
+ }
989
+ : {
990
+ ...filters.find(data => data.name === 'bizplace'),
991
+ value: [filters.find(data => data.name === 'bizplace').value]
992
+ }
993
+ }
994
+
995
+ let queryFilter = ``
996
+ let bizplaces = bizplaceFilter.value.map(id => `('${id}')`).join(',')
997
+
998
+ if (product) {
999
+ let products = product.value
1000
+ .toLowerCase()
1001
+ .split(',')
1002
+ .map(prod => {
1003
+ return "'%" + prod.trim().replace(/'/g, "''") + "%'"
1004
+ })
1005
+ .join(',')
1006
+
1007
+ queryFilter =
1008
+ queryFilter +
1009
+ ` and exists (
1010
+ select prd.id as product_id from products prd
1011
+ inner join bizplaces b on b.id = prd.bizplace_id
1012
+ inner join companies c on c.domain_id = b.domain_id
1013
+ inner join bizplaces b2 on b2.company_id = c.id
1014
+ where exists (
1015
+ SELECT * FROM tmp_bizfilter bizFilter
1016
+ WHERE bizFilter.bizplace_id = "b2"."id"
1017
+ )
1018
+ and (lower(prd.name) like any(array[${products}])
1019
+ or lower(prd.description) like any(array[${products}])
1020
+ or lower(prd.sku) like any(array[${products}]))
1021
+ and prd.id = rih.product_id
1022
+ group by prd.id
1023
+ )`
1024
+ }
1025
+
1026
+ if (location) {
1027
+ let locations = location.value
1028
+ .map(loc => {
1029
+ return "'" + loc + "'"
1030
+ })
1031
+ .join(',')
1032
+
1033
+ queryFilter =
1034
+ queryFilter +
1035
+ `and exists (
1036
+ select loc.id from locations loc where
1037
+ loc.id = any(array[${locations}]::uuid[])
1038
+ and loc.id = rih.location_id
1039
+ )`
1040
+ }
1041
+
1042
+ // default sorting
1043
+ let queryOrder = 'order by created_at desc'
1044
+ if (sortings && sortings.length > 0) {
1045
+ const arrObjectMap = [
1046
+ { name: 'palletId', value: 'rih.pallet_id' },
1047
+ { name: 'cartonId', value: 'rih.carton_id' },
1048
+ { name: 'batchId', value: 'rih.batch_id' },
1049
+ { name: 'initialInboundAt', value: 'rih.initial_inbound_at' },
1050
+ { name: 'bizplace|name', value: 'bz.name' },
1051
+ { name: 'product|name', value: 'prd.sku' },
1052
+ { name: 'remainQty', value: 'rih.qty' },
1053
+ { name: 'location|name', value: 'loc.name' }
1054
+ ]
1055
+ sortings.forEach((sorting, idx) => {
1056
+ let itmIndex = arrObjectMap.findIndex(x => x.name === sorting.name)
1057
+
1058
+ queryOrder = `${idx === 0 ? 'order by' : queryOrder + ','} ${
1059
+ itmIndex >= 0 ? arrObjectMap[itmIndex].value : sorting.name
1060
+ } ${sorting.desc ? 'DESC' : 'ASC'}`
1061
+ })
1062
+ }
1063
+
1064
+ if (batchId) queryFilter = `${queryFilter} and lower(rih.batch_id) like '${batchId.value.toLowerCase()}' `
1065
+
1066
+ if (palletId) queryFilter = `${queryFilter} and rih.pallet_id like '${palletId.value}' `
1067
+
1068
+ if (cartonId) queryFilter = `${queryFilter} and rih.carton_id like '${cartonId.value}' `
1069
+
1070
+ if (packingType) queryFilter = `${queryFilter} and rih.packing_type like '${packingType.value}' `
1071
+
1072
+ let expirationQueryFilter = ``
1073
+ if (expiration)
1074
+ expirationQueryFilter = ` and iv.expiration_date >= '${expFromDate.value}'::timestamp + '${timezoneOffset}'::INTERVAL and
1075
+ iv.expiration_date <= '${expToDate.value}'::timestamp + '${timezoneOffset}'::INTERVAL + '1 day'::INTERVAL`
1076
+
1077
+ // query the results
1078
+ await tx.query(`
1079
+ CREATE TEMP TABLE tmp_bizfilter AS (
1080
+ SELECT bizplace_id::uuid FROM (VALUES ${bizplaces}) AS bizFilter(bizplace_id)
1081
+ );
1082
+ `)
1083
+
1084
+ await tx.query(
1085
+ `
1086
+ create temp table tmp_src as (
1087
+ select domain_id, pallet_id, carton_id, seq, qty, uom_value, packing_type, batch_id, batch_id_ref, created_at, status,
1088
+ product_id::uuid as product_id, bizplace_id::uuid as bizplace_id, location_id::uuid as location_id
1089
+ from reduced_inventory_histories rih
1090
+ where domain_id = $1 and created_at < $2::timestamp + $3::INTERVAL + '1 day'::INTERVAL
1091
+ );
1092
+ `,
1093
+ [domain.id, createdAt.value, timezoneOffset]
1094
+ )
1095
+
1096
+ await tx.query(
1097
+ `
1098
+ create temp table tmp_data as (
1099
+ select dt.*, rih.carton_id, rih.batch_id, rih.batch_id_ref, rih.product_id, rih.packing_type, rih.bizplace_id, rih.location_id, iv.uom,
1100
+ iv.expiration_date, iv.reusable_pallet_id, iv.remark
1101
+ from
1102
+ (
1103
+ select ih.domain_id, ih.pallet_id, sum(ih.qty) as qty, sum(ih.uom_value) as uom_value, max(ih.seq) as last_seq, max(ih.created_at) as created_at,
1104
+ min(ih.created_at) as initial_inbound_at
1105
+ from tmp_src ih
1106
+ group by ih.domain_id, ih.pallet_id
1107
+ ) dt
1108
+ inner join tmp_src rih on dt.domain_id = rih.domain_id and dt.pallet_id = rih.pallet_id and dt.last_seq = rih.seq and rih.status <> 'TERMINATED'
1109
+ inner join inventories iv on iv.domain_id = dt.domain_id and iv.pallet_id = dt.pallet_id
1110
+ where exists (
1111
+ SELECT * FROM tmp_bizfilter bizFilter
1112
+ WHERE bizFilter.bizplace_id = "rih"."bizplace_id"
1113
+ )
1114
+ ${queryFilter}
1115
+ ${expirationQueryFilter}
1116
+ );
1117
+ `
1118
+ )
1119
+
1120
+ const total: any = await tx.query(`select count(*) from tmp_data`)
1121
+
1122
+ const result: any = await tx.query(
1123
+ `
1124
+ select
1125
+ rih.domain_id, rih.pallet_id as "palletId", rih.carton_id as "cartonId", rih.qty as "remainQty", rih.uom_value as "uomValue", rih.uom, rih.last_seq, rih.created_at,
1126
+ rih.initial_inbound_at as "initialInboundAt",
1127
+ case when rih.reusable_pallet_id is not null then concat(rih.batch_id, ' (', plt.name, ')') else rih.batch_id end as "batchId",
1128
+ rih.batch_id_ref as "batchIdRef", rih.product_id, rih.packing_type as "packingType", rih.bizplace_id, rih.location_id,
1129
+ prd.id as product_id, prd.name as product_name, prd.sku as product_sku, prd.description as product_description,
1130
+ bz.id as bizplace_id, bz.name as bizplace_name,
1131
+ loc.id as location_id, loc.name as location_name, loc."zone" as location_zone, loc."row" as location_row, loc."column" as location_column, loc.shelf as location_shelf,
1132
+ wh.name as warehouse_name, plt.name as reusable_pallet_name,
1133
+ rih.remark,
1134
+ rih.expiration_date as "expirationDate"
1135
+ from tmp_data rih
1136
+ left join pallets plt on plt.id = rih.reusable_pallet_id
1137
+ inner join products prd on prd.id = rih.product_id
1138
+ inner join bizplaces bz on bz.id = rih.bizplace_id
1139
+ inner join locations loc on loc.id = rih.location_id
1140
+ inner join warehouses wh on wh.id = loc.warehouse_id
1141
+ WHERE 1 = 1
1142
+ ${queryOrder}
1143
+ OFFSET $1 LIMIT $2;
1144
+ `,
1145
+ [(pagination.page - 1) * pagination.limit, pagination.limit]
1146
+ )
1147
+
1148
+ tx.query(`
1149
+ drop table tmp_src, tmp_data, tmp_bizfilter
1150
+ `)
1151
+
1152
+ // map all results
1153
+ let items = result.map(itm => {
1154
+ return {
1155
+ ...itm,
1156
+ bizplace: {
1157
+ id: itm.bizplace_id,
1158
+ name: itm.bizplace_name
1159
+ },
1160
+ location: {
1161
+ id: itm.location_id,
1162
+ name: itm.location_name,
1163
+ type: itm.location_type,
1164
+ zone: itm.location_zone
1165
+ },
1166
+ product: {
1167
+ id: itm.product_id,
1168
+ name: itm.product_name,
1169
+ description: itm.product_description,
1170
+ sku: itm.product_sku
1171
+ }
1172
+ }
1173
+ })
1174
+
1175
+ return { items, total: total[0].count }
1176
+ }
1177
+
1178
+ @FieldResolver(type => Domain)
1179
+ async domain(@Root() inventoryHistory: InventoryHistory) {
1180
+ return await getRepository(Domain).findOne(inventoryHistory.domainId)
1181
+ }
1182
+
1183
+ @FieldResolver(type => User)
1184
+ async updater(@Root() inventoryHistory: InventoryHistory) {
1185
+ return await getRepository(User).findOne(inventoryHistory.updaterId)
1186
+ }
1187
+
1188
+ @FieldResolver(type => User)
1189
+ async creator(@Root() inventoryHistory: InventoryHistory) {
1190
+ return await getRepository(User).findOne(inventoryHistory.creatorId)
1191
+ }
1192
+ }
1193
+
1194
+ async function massageInventorySummary(tx: EntityManager, params: ListParam, bizplace: Bizplace, context: any) {
1195
+ await productsQuery(tx, params, bizplace)
1196
+ await filterInventoryQuery(tx, params, bizplace, context)
1197
+
1198
+ let fromDate = params.filters.find(data => data.name === 'fromDate')
1199
+ let hasTransactionOrBalanceFilter = params.filters.find(data => data.name === 'hasTransactionOrBalance')
1200
+
1201
+ let hasTransactionOrBalanceQuery = ''
1202
+ if (hasTransactionOrBalanceFilter && hasTransactionOrBalanceFilter.value) {
1203
+ hasTransactionOrBalanceQuery = 'and (opening_qty > 0 or total_in_qty > 0 or adjustment_qty > 0)'
1204
+ }
1205
+ await tx.query(
1206
+ `
1207
+ create temp table temp_inventory_summary as (
1208
+ select src.*,
1209
+ opening_qty + adjustment_qty + total_in_qty + total_out_qty as closing_qty
1210
+ from (
1211
+ select prd.sku AS product_sku, prd.name as product_name, prd.description as product_description, prd.type AS product_type, ih.packing_type, ih.packing_size,
1212
+ sum(case when ih.created_at >= $1 and ih.transaction_type = 'ADJUSTMENT' then ih.qty else 0 end) as adjustment_qty,
1213
+ sum(case when ih.created_at < $1 then qty else 0 end) as opening_qty,
1214
+ sum(case when ih.created_at >= $1 then case when ih.qty > 0 and ih.transaction_type <> 'ADJUSTMENT' then ih.qty else 0 end else 0 end) as total_in_qty,
1215
+ sum(case when ih.created_at >= $1 then case when ih.qty < 0 and ih.transaction_type <> 'ADJUSTMENT' then ih.qty else 0 end else 0 end) as total_out_qty,
1216
+ ih.product_id
1217
+ from temp_inv_history ih
1218
+ inner join temp_products prd on prd.id = ih.product_id
1219
+ group by prd.sku, prd.name, prd.description, prd.type, ih.product_id, ih.packing_type, ih.packing_size
1220
+ ) src
1221
+ where 1=1
1222
+ ${hasTransactionOrBalanceQuery}
1223
+ )
1224
+ `,
1225
+ [fromDate.value]
1226
+ )
1227
+
1228
+ const total: any = await tx.query(`select count(*) from temp_inventory_summary`)
1229
+
1230
+ const result: any = await tx.query(
1231
+ `
1232
+ select * from temp_inventory_summary ORDER BY product_name, product_description, product_type, packing_type, packing_size OFFSET $1 LIMIT $2
1233
+ `,
1234
+ [(params.pagination.page - 1) * params.pagination.limit, params.pagination.limit]
1235
+ )
1236
+
1237
+ tx.query(`
1238
+ drop table temp_products, temp_inv_history, temp_inventory_summary
1239
+ `)
1240
+
1241
+ return [result, total]
1242
+ }
1243
+
1244
+ async function massageInventoryPalletSummary(tx: EntityManager, params: ListParam, bizplace: Bizplace, context: any) {
1245
+ await productsQuery(tx, params, bizplace)
1246
+ await filterInventoryQuery(tx, params, bizplace, context)
1247
+
1248
+ let fromDate = params.filters.find(data => data.name === 'fromDate')
1249
+ let toDate = params.filters.find(data => data.name === 'toDate')
1250
+ let hasTransactionOrBalanceFilter = params.filters.find(data => data.name === 'hasTransactionOrBalance')
1251
+
1252
+ let hasTransactionOrBalanceQuery = ''
1253
+ if (hasTransactionOrBalanceFilter && hasTransactionOrBalanceFilter.value) {
1254
+ hasTransactionOrBalanceQuery = 'and (opening_qty > 0 or total_in_qty > 0 or adjustment_qty > 0)'
1255
+ }
1256
+ await tx.query(
1257
+ `
1258
+ create temp table temp_inventory_pallet_summary as (
1259
+ select invh.*,
1260
+ invh.opening_qty + invh.total_in_qty + invh.total_out_qty + invh.adjustment_qty as closing_qty from (
1261
+ select product_sku, product_id, product_name, product_description, product_type,
1262
+ SUM(case when invHistory.created_at < $1 then
1263
+ case when invHistory.status = 'STORED' then 1
1264
+ when invhistory.status = 'TERMINATED' then -1
1265
+ else 0 end
1266
+ else 0 end) as opening_qty,
1267
+ SUM(case when invHistory.created_at >= $1 and invHistory.created_at <= $2 then
1268
+ case when (invHistory.transaction_type = 'UNLOADING' or invHistory.transaction_type = 'NEW') then 1 else 0 end
1269
+ else 0 end) as total_in_qty,
1270
+ SUM(case when invHistory.created_at >= $1 and invHistory.created_at <= $2 then
1271
+ case when invHistory.status = 'TERMINATED' AND invHistory.transaction_type <> 'ADJUSTMENT' then -1 else 0 end
1272
+ else 0 end) as total_out_qty,
1273
+ SUM(case when invHistory.created_at >= $1 and invHistory.created_at <= $2 then
1274
+ case when invHistory.status = 'TERMINATED' AND invHistory.transaction_type = 'ADJUSTMENT' then -1
1275
+ when invHistory.status = 'STORED' AND invHistory.transaction_type = 'ADJUSTMENT' then 1 else 0 end
1276
+ else 0 end) as adjustment_qty
1277
+ from(
1278
+ select pallet_id, seq, status, transaction_type, product_sku, product_id, product_name, product_description, product_type,
1279
+ inventory_history_id, packing_type, packing_size, qty, opening_qty, created_at from (
1280
+ select row_number() over(partition by invh.pallet_id order by invh.created_at asc) as rn, invh.* ,
1281
+ prd.sku as product_sku, prd.name as product_name, prd.description as product_description, prd.type AS product_type
1282
+ from temp_inv_history invh
1283
+ inner join temp_products prd on prd.id = invh.product_id
1284
+ where (transaction_type = 'UNLOADING' or invh.transaction_type = 'NEW')
1285
+ and invh.created_at >= $1
1286
+ ) as invIn where rn = 1
1287
+ union all
1288
+ select pallet_id, seq, status, transaction_type, product_sku, product_id, product_name, product_description, product_type,
1289
+ inventory_history_id, packing_type, packing_size, qty, opening_qty, created_at from (
1290
+ SELECT row_number() over(partition by pallet_id, invh.product_id order by seq asc) as rn,
1291
+ invh.pallet_id, invh.product_id, invh.packing_type, invh.packing_size,
1292
+ invh.batch_id, invh.inventory_history_id,
1293
+ invh.seq, 'STORED' AS status, invh.transaction_type, invh.qty, invh.opening_qty,
1294
+ invh.created_at,
1295
+ prd.sku as product_sku, prd.name as product_name, prd.description as product_description, prd.type AS product_type
1296
+ from temp_inv_history invh
1297
+ inner join temp_products prd on prd.id = invh.product_id
1298
+ where invh.created_at < $1
1299
+ and (invh.status = 'STORED' or invh.status = 'UNLOADED')
1300
+ ) as invStored where rn = 1
1301
+ union all
1302
+ select pallet_id, seq, status, transaction_type, product_sku, product_id, product_name, product_description, product_type,
1303
+ inventory_history_id, packing_type, packing_size, qty, opening_qty, started_at as created_at from (
1304
+ select row_number() over(partition by invh.pallet_id, invh.product_id order by invh.seq desc) as rn, invh.*,
1305
+ prd.sku as product_sku, prd.name as product_name, prd.description as product_description, prd.type AS product_type, repeatedGroup.started_at
1306
+ from temp_inv_history invh
1307
+ inner join (
1308
+ select pallet_id, min(created_at) as started_at, max(created_at) as ended_at, min(seq) as min_seq, max(seq) as max_seq, status from (
1309
+ select startData.*, sum(startflag) over (partition by pallet_id order by seq) as grp from (
1310
+ select s.*,
1311
+ (case when lag(status) over (partition by pallet_id order by seq) = status then 0 else 1 end) as startflag
1312
+ from temp_inv_history s
1313
+ ) startData
1314
+ ) endData
1315
+ group by pallet_id, grp, status
1316
+ ) repeatedGroup on repeatedGroup.pallet_id = invh.pallet_id and repeatedGroup.min_seq <= invh.seq and repeatedGroup.max_seq >= invh.seq
1317
+ inner join temp_products prd on prd.id = invh.product_id
1318
+ ) as invOut where rn = 1 AND status ='TERMINATED'
1319
+ union all
1320
+ SELECT invh.pallet_id, invh.seq, invh.status, invh.transaction_type,
1321
+ prd.sku AS product_sku, invh.product_id, prd.name AS product_name, prd.description AS product_description, prd.type AS product_type,
1322
+ invh.inventory_history_id, invh.packing_type, invh.packing_size, invh.qty, invh.opening_qty, invh.created_at
1323
+ FROM temp_inv_history invh
1324
+ INNER JOIN (
1325
+ SELECT invh.pallet_id, invh.seq AS stored_seq, invh2.seq AS terminated_seq FROM temp_inv_history invh
1326
+ INNER JOIN temp_inv_history invh2 ON invh2.pallet_id = invh.pallet_id AND invh2.status='STORED' AND invh2.seq = invh.seq + 1 AND invh2.product_id <> invh.product_id
1327
+ WHERE invh.transaction_type = 'ADJUSTMENT'
1328
+ ) dt ON dt.pallet_id = invh.pallet_id AND (dt.stored_seq = invh.seq OR dt.terminated_seq = invh.seq)
1329
+ inner join temp_products prd on prd.id = invh.product_id
1330
+ where invh.created_at >= $1
1331
+ ORDER BY product_name, pallet_id, seq
1332
+ ) as invHistory
1333
+ group by product_sku, product_id, product_name, product_description, product_type
1334
+ ) invh
1335
+ where 1=1
1336
+ ${hasTransactionOrBalanceQuery}
1337
+ )
1338
+ `,
1339
+ [fromDate.value, toDate.value]
1340
+ )
1341
+
1342
+ const total: any = await tx.query(
1343
+ `select count(*) as count, sum(total_in_qty) as totalInQty, sum(opening_qty) as totalOpeningBal from temp_inventory_pallet_summary`
1344
+ )
1345
+
1346
+ const result: any = await tx.query(
1347
+ `
1348
+ select *, 'PALLET' as packing_type from temp_inventory_pallet_summary ORDER BY product_name, product_description OFFSET $1 LIMIT $2
1349
+ `,
1350
+ [(params.pagination.page - 1) * params.pagination.limit, params.pagination.limit]
1351
+ )
1352
+
1353
+ tx.query(`
1354
+ drop table temp_products, temp_inv_history, temp_inventory_pallet_summary
1355
+ `)
1356
+
1357
+ return [result, total]
1358
+ }
1359
+
1360
+ async function productsQuery(tx: EntityManager, params: ListParam, bizplace: Bizplace) {
1361
+ let product = params.filters.find(data => data.name === 'product')
1362
+ let productDesc = params.filters.find(data => data.name === 'productDescription')
1363
+
1364
+ let productQuery = ''
1365
+ if (product) {
1366
+ let productValue = product.value
1367
+ .toLowerCase()
1368
+ .split(',')
1369
+ .map(prod => {
1370
+ return "'%" + prod.trim().replace(/'/g, "''") + "%'"
1371
+ })
1372
+ .join(',')
1373
+ productQuery = `AND (
1374
+ Lower(prd.name) LIKE ANY(ARRAY[${productValue}])
1375
+ OR Lower(prd.sku) LIKE ANY(ARRAY[${productValue}])
1376
+ OR Lower(prd.description) LIKE ANY(ARRAY[${productValue}])
1377
+ )`
1378
+ }
1379
+
1380
+ let productDescQuery = ''
1381
+ if (productDesc) {
1382
+ productDescQuery = "AND Lower(description) LIKE '%" + productDesc.value.toLowerCase() + "%'"
1383
+ }
1384
+
1385
+ await tx.query(
1386
+ `
1387
+ create temp table temp_products AS
1388
+ (
1389
+ select prd.id, prd.name, prd.sku, prd.description, prd.id::varchar as product_id, prd.type from products prd
1390
+ inner join bizplaces b on b.id = prd.bizplace_id
1391
+ inner join companies c on c.domain_id = b.domain_id
1392
+ inner join bizplaces b2 on b2.company_id = c.id
1393
+ where b2.id = $1
1394
+ ${productQuery}
1395
+ GROUP BY prd.id, prd.sku, prd.name, prd.description
1396
+ )`,
1397
+ [bizplace.id]
1398
+ )
1399
+ }
1400
+
1401
+ async function filterInventoryQuery(tx: EntityManager, params: ListParam, bizplace: Bizplace, context: any) {
1402
+ let toDate = params.filters.find(data => data.name === 'toDate')
1403
+ let batchNo = params.filters.find(data => data.name === 'batchNo')
1404
+
1405
+ let batchNoQuery = ''
1406
+ if (batchNo) {
1407
+ batchNoQuery =
1408
+ 'AND Lower(ih.batch_id) LIKE ANY(ARRAY[' +
1409
+ batchNo.value
1410
+ .toLowerCase()
1411
+ .split(',')
1412
+ .map(prod => {
1413
+ return "'%" + prod.trim().replace(/'/g, "''") + "%'"
1414
+ })
1415
+ .join(',') +
1416
+ '])'
1417
+ }
1418
+
1419
+ await tx.query(
1420
+ `
1421
+ create temp table temp_inv_history as (
1422
+ select i2.pallet_id, ih.product_id::uuid, ih.packing_type, i2.packing_size, ih.batch_id,
1423
+ ih.id as inventory_history_id, ih.seq, ih.status, ih.transaction_type, ih.qty, ih.opening_qty, ih.created_at
1424
+ from inventories i2
1425
+ inner join reduced_inventory_histories ih on ih.pallet_id = i2.pallet_id and ih.domain_id = i2.domain_id
1426
+ where
1427
+ i2.domain_id = $1
1428
+ and i2.bizplace_id = $2
1429
+ and ih.created_at <= $3
1430
+ ${batchNoQuery}
1431
+ )
1432
+ `,
1433
+ [context.state.domain.id, bizplace.id, toDate.value]
1434
+ )
1435
+ }