selectStockLedgerDialog.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. <template>
  2. <ele-modal
  3. title="库存台账"
  4. :visible.sync="visible"
  5. v-if="visible"
  6. :close-on-click-modal="false"
  7. :before-close="handleClose"
  8. :close-on-press-escape="false"
  9. destroy-on-close
  10. top="5vh"
  11. append-to-body
  12. width="70%"
  13. :maxable="true"
  14. >
  15. <el-card shadow="never">
  16. <item-search @search="reload" ref="refSeavch"></item-search>
  17. <ele-split-layout
  18. width="244px"
  19. allow-collapse
  20. :right-style="{ overflow: 'hidden' }"
  21. >
  22. <div class="ele-border-lighter split-layout-right-content">
  23. <el-tree
  24. :data="treeList"
  25. :props="defaultProps"
  26. ref="treeRef"
  27. :default-expanded-keys="
  28. curNodeData && curNodeData.id ? [curNodeData.id] : []
  29. "
  30. :highlight-current="true"
  31. node-key="id"
  32. :expand-on-click-node="false"
  33. @node-click="handleNodeClick"
  34. ><span class="custom-tree-node" slot-scope="{ node, data }">
  35. <span
  36. >{{ data.factoryName ? data.factoryName + '-' : ''
  37. }}{{ data.name }}</span
  38. >
  39. </span></el-tree
  40. >
  41. </div>
  42. <!-- 表格 -->
  43. <template v-slot:content>
  44. <ele-pro-table
  45. ref="table"
  46. :columns="columns"
  47. :datasource="datasource"
  48. row-key="_tableRowKey"
  49. height="calc(100vh - 480px)"
  50. class="dict-table"
  51. @cell-click="cellClick"
  52. :selection.sync="selection"
  53. :current.sync="currentRow"
  54. highlight-current-row
  55. :cache-key="cacheKeyUrl"
  56. @columns-change="handleColumnChange"
  57. @done="handleTableDone"
  58. :page-size="20"
  59. @header-dragend="handleHeaderDragend"
  60. >
  61. <template v-slot:toolbar>
  62. <el-button
  63. v-if="dimension == '2'"
  64. type="primary"
  65. size="small"
  66. @click="openUnlock"
  67. >库存解锁</el-button
  68. >
  69. <span
  70. v-if="dimension == '2' && unlockSourceLabel"
  71. class="unlock-tip"
  72. >{{ unlockSourceLabel }}</span
  73. >
  74. </template>
  75. <template v-slot:saleCount="{ row }">
  76. <el-input
  77. v-model="row.saleCount"
  78. style="width: 100%"
  79. placeholder="请输入"
  80. type="number"
  81. :min="0"
  82. @input="handleSaleCountInput(row)"
  83. ></el-input>
  84. </template>
  85. <template v-slot:action="{ row }">
  86. <el-radio class="radio" v-model="radio" :label="row.id"
  87. ><i></i
  88. ></el-radio>
  89. </template>
  90. </ele-pro-table>
  91. </template>
  92. </ele-split-layout>
  93. </el-card>
  94. <unlock-modal ref="unlockModalRef"></unlock-modal>
  95. <div slot="footer">
  96. <el-button v-if="!isView" type="primary" size="small" @click="selected"
  97. >选择</el-button
  98. >
  99. <el-button size="small" @click="handleClose">关闭</el-button>
  100. </div>
  101. </ele-modal>
  102. </template>
  103. <script>
  104. import {
  105. getOutindetailtwoList,
  106. getTreeByGroup,
  107. getBatchList,
  108. getCategoryPackageDisposition
  109. } from '@/api/wms';
  110. import ItemSearch from './item-search.vue';
  111. import UnlockModal from './unlockModal.vue';
  112. import { contactQueryByCategoryIdsAPI } from '@/api/saleManage/contact';
  113. import tabMixins from '@/mixins/tableColumnsMixin';
  114. export default {
  115. mixins: [tabMixins],
  116. components: { ItemSearch, UnlockModal },
  117. props: {
  118. /*已选择的产品*/
  119. data: {
  120. type: Array,
  121. default: () => []
  122. },
  123. /*是否需要供应商*/
  124. isSupplier: {
  125. type: Boolean,
  126. default: false
  127. },
  128. /*是否显示销售数量*/
  129. showSaleCount: {
  130. type: Boolean,
  131. default: false
  132. },
  133. // 是否按库存余额和锁库数量展示可用数量
  134. showStockCount: {
  135. type: Boolean,
  136. default: false
  137. },
  138. cacheKeyUrl: {
  139. type: String,
  140. default: 'eom-sales-selectStockLedgerDialog-202605131603'
  141. },
  142. isView: {
  143. type: Boolean,
  144. default: false
  145. }
  146. },
  147. data() {
  148. return {
  149. visible: false,
  150. currentIndex: null,
  151. radio: null,
  152. dictList: {},
  153. curNodeData: {},
  154. selection: [],
  155. // 表格当前高亮行(点击行即选中),用于库存解锁定位库存数据
  156. currentRow: null,
  157. treeData: [],
  158. dimension: '2',
  159. defaultProps: {
  160. children: 'children',
  161. label: 'name'
  162. },
  163. treeList: [],
  164. treeLoading: false,
  165. // cacheKeyUrl: '',
  166. columnsVersion: 1
  167. };
  168. },
  169. watch: {},
  170. computed: {
  171. columns() {
  172. const version = this.columnsVersion;
  173. // 列宽由 tabMixins 的 applyColumnWidth 处理(从 cacheKeyUrl+'Cols' 还原,
  174. // 拖拽时由 mixin 的 handleHeaderDragend 持久化到同一 key + 同步服务端)
  175. let data = null;
  176. if (this.currentIndex != -1) {
  177. data = {
  178. action: 'action',
  179. slot: 'action',
  180. align: 'center',
  181. label: '选择',
  182. reserveSelection: true
  183. };
  184. }
  185. if (this.currentIndex == -1) {
  186. data = {
  187. label: '选择',
  188. width: 45,
  189. type: 'selection',
  190. columnKey: 'selection',
  191. align: 'center',
  192. // 分页切换后保留已勾选的库存,确认时可以一次提交多页选择
  193. reserveSelection: true,
  194. selectable: (row) => {
  195. return this.getAvailableStockCount(row) != 0;
  196. }
  197. };
  198. }
  199. const tempData = [
  200. data,
  201. {
  202. columnKey: 'index',
  203. type: 'index',
  204. width: 50,
  205. align: 'center',
  206. showOverflowTooltip: true,
  207. label: '序号'
  208. },
  209. {
  210. prop: 'categoryCode',
  211. label: '编码',
  212. align: 'center',
  213. showOverflowTooltip: true,
  214. minWidth: 110
  215. },
  216. {
  217. prop: 'categoryName',
  218. label: '名称',
  219. align: 'center',
  220. showOverflowTooltip: true,
  221. minWidth: 110
  222. },
  223. {
  224. prop: 'saleCount',
  225. slot: 'saleCount',
  226. label: '订单数量',
  227. showOverflowTooltip: true,
  228. align: 'center',
  229. minWidth: 120,
  230. hide: !this.showSaleCount
  231. },
  232. {
  233. prop: 'brandNum',
  234. align: 'center',
  235. label: '牌号',
  236. showOverflowTooltip: true
  237. },
  238. this.dimension == '2'
  239. ? {
  240. prop: 'batchNo',
  241. label: '批次号',
  242. align: 'center',
  243. showOverflowTooltip: true
  244. }
  245. : {
  246. width: 1
  247. },
  248. this.dimension == '2'
  249. ? {
  250. prop: 'systemBatchNo',
  251. label: '系统内部批次号',
  252. align: 'center',
  253. minWidth: 180,
  254. showOverflowTooltip: true,
  255. formatter: (row) => row.systemBatchNo || '-'
  256. }
  257. : {
  258. width: 1
  259. },
  260. {
  261. prop: 'categoryModel',
  262. label: '型号',
  263. align: 'center',
  264. showOverflowTooltip: true
  265. },
  266. {
  267. prop: 'specification',
  268. label: '规格',
  269. align: 'center',
  270. showOverflowTooltip: true
  271. },
  272. {
  273. prop: 'colorKey',
  274. label: '颜色',
  275. align: 'center',
  276. showOverflowTooltip: true
  277. },
  278. {
  279. prop: 'modelKey',
  280. label: '机型',
  281. align: 'center',
  282. showOverflowTooltip: true
  283. },
  284. {
  285. prop: 'warehouseName',
  286. label: '仓库',
  287. showOverflowTooltip: true,
  288. align: 'center',
  289. minWidth: 90
  290. },
  291. {
  292. prop: 'inventoryCycle',
  293. label: '存货周期(天)',
  294. align: 'center',
  295. width: 150,
  296. sortable: true,
  297. showOverflowTooltip: true,
  298. hide: this.dimension == 1
  299. },
  300. {
  301. prop: 'measureQuantity',
  302. label: '库存',
  303. showOverflowTooltip: true,
  304. align: 'center',
  305. minWidth: 90,
  306. formatter: (row) => this.getStockCount(row)
  307. },
  308. {
  309. prop: 'lockQuantity',
  310. label: '锁库数量',
  311. showOverflowTooltip: true,
  312. align: 'center',
  313. minWidth: 90
  314. },
  315. {
  316. prop: 'useQuantity',
  317. label: '可用数量',
  318. showOverflowTooltip: true,
  319. align: 'center',
  320. minWidth: 90,
  321. formatter: (row) => {
  322. return this.getAvailableStockCount(row);
  323. }
  324. },
  325. {
  326. prop: 'measureUnit',
  327. label: '单位',
  328. showOverflowTooltip: true,
  329. align: 'center',
  330. minWidth: 90
  331. },
  332. {
  333. prop: 'weight',
  334. label: '重量',
  335. showOverflowTooltip: true,
  336. align: 'center',
  337. minWidth: 90
  338. },
  339. {
  340. prop: 'weightUnit',
  341. label: '重量单位',
  342. showOverflowTooltip: true,
  343. align: 'center',
  344. minWidth: 90
  345. },
  346. {
  347. prop: 'packingUnit',
  348. align: 'center',
  349. label: '包装单位',
  350. showOverflowTooltip: true
  351. },
  352. {
  353. prop: 'reelSpecification',
  354. label: '盘具',
  355. showOverflowTooltip: true,
  356. align: 'center'
  357. }
  358. ];
  359. // 应用 mixin 列配置(列宽由 tabMixins.applyColumnWidth 处理)
  360. return this.applyColumnWidth(tempData.filter((item) => !item.hide));
  361. },
  362. // 库存解锁操作的来源行:优先取唯一勾选行,否则取当前点击高亮行
  363. unlockSourceRow() {
  364. if (this.selection.length === 1) {
  365. return this.selection[0];
  366. }
  367. return this.currentRow || null;
  368. },
  369. // 解锁按钮旁的选中数据提示,避免表格级操作定位不明
  370. unlockSourceLabel() {
  371. const row = this.unlockSourceRow;
  372. if (!row) {
  373. return '';
  374. }
  375. const code = row.categoryCode || '-';
  376. const batch = row.systemBatchNo || row.batchNo || '-';
  377. return `已选:${code} / ${batch} / 锁库 ${row.lockQuantity || 0}`;
  378. }
  379. },
  380. methods: {
  381. getDictV(code, val) {
  382. if (!this.dictList[code]) return '';
  383. return this.dictList[code].find((item) => item.value == val)?.label;
  384. },
  385. open(item, currentIndex) {
  386. console.log(item, currentIndex);
  387. // tabMixins 的 getTabColumns() 末尾会递增 columnsVersion 触发重算,
  388. // 此处不需要手动重算。
  389. this.currentIndex = currentIndex;
  390. this.visible = true;
  391. this.getTreeData();
  392. },
  393. async getTreeData() {
  394. try {
  395. this.treeLoading = true;
  396. let res = null;
  397. res = await getTreeByGroup({ type: 2 });
  398. this.treeLoading = false;
  399. if (res?.code === '0') {
  400. this.treeList = res.data;
  401. return this.treeList;
  402. }
  403. } catch (error) {
  404. console.log(error);
  405. }
  406. this.treeLoading = false;
  407. },
  408. /* 表格数据源 */
  409. async datasource({ page, limit, where, order }) {
  410. this.dimension = where.dimension || '2';
  411. let api = where.dimension == '1' ? getOutindetailtwoList : getBatchList;
  412. const data = await api({
  413. ...where,
  414. ...order,
  415. pageNum: page,
  416. size: limit
  417. });
  418. // 与 WMS 库存台账保持一致:库存取 stockCountBase,旧接口无该字段时回退 measureQuantity;
  419. // 可用数量按库存减锁库数量计算,并统一将负数归零。
  420. if (data && Array.isArray(data.list)) {
  421. data.list = data.list.map((item, index) => {
  422. const stockCountBase = Math.max(
  423. Number(
  424. item.stockCountBase !== undefined &&
  425. item.stockCountBase !== null
  426. ? item.stockCountBase
  427. : item.measureQuantity
  428. ) || 0,
  429. 0
  430. );
  431. const lockQuantity = Number(item.lockQuantity) || 0;
  432. const availableCountBase = Math.max(
  433. stockCountBase - lockQuantity,
  434. 0
  435. );
  436. return {
  437. ...item,
  438. stockCountBase,
  439. availableCountBase,
  440. _tableRowKey: `${item.id || 'row'}_${page}_${index}`
  441. };
  442. });
  443. }
  444. // 库存接口在同一物品/批次存在多条台账时可能返回重复 id。
  445. // id 仍作为业务主键保留,表格使用仅用于渲染的唯一 key,避免 Vue patch 冲突。
  446. this.getTabColumns();
  447. return data;
  448. },
  449. /* 清空表格勾选与当前行:搜索、切换分页/排序、关闭弹窗时都必须重置 */
  450. clearTableSelection() {
  451. this.selection = [];
  452. this.currentRow = null;
  453. this.current = null;
  454. this.radio = '';
  455. if (this.$refs.table && this.$refs.table.clearSelection) {
  456. this.$refs.table.clearSelection();
  457. }
  458. },
  459. /* 刷新表格 */
  460. reload(where) {
  461. // 查询条件变化后原勾选行已不在结果集中,先清空避免带着旧数据继续操作
  462. this.clearTableSelection();
  463. this.$refs.table.reload({ pageNum: 1, where: where });
  464. },
  465. /* 分页、排序后清除当前行,避免解锁操作误用上一页数据,但保留跨页勾选 */
  466. handleTableDone() {
  467. this.currentRow = null;
  468. this.current = null;
  469. this.radio = '';
  470. },
  471. handleNodeClick(data, node) {
  472. this.curNodeData = data;
  473. if (data.id == this.currentId) {
  474. this.$refs.treeRef.setCurrentKey(null);
  475. this.reload();
  476. } else {
  477. this.reload({ categoryLevelId: data.id });
  478. }
  479. },
  480. async getSupplierObj(productList, queryName) {
  481. try {
  482. let categoryIds = productList
  483. .filter((item) => item.id)
  484. .map((item) => item.id);
  485. if (categoryIds.length > 0) {
  486. return await contactQueryByCategoryIdsAPI({
  487. categoryIds,
  488. isQueryEE: 1
  489. });
  490. } else {
  491. return Promise.resolve({});
  492. }
  493. } catch (e) {
  494. return Promise.resolve({});
  495. }
  496. },
  497. // 单击获取id
  498. cellClick(row) {
  499. if (this.currentIndex == -1) return;
  500. this.current = row;
  501. this.radio = row.id;
  502. },
  503. // 打开库存解锁申请弹窗(校验与数据规格化由子弹窗负责)
  504. openUnlock() {
  505. if (!this.unlockSourceRow) {
  506. return this.$message.warning('请先选择一条库存数据进行解锁');
  507. }
  508. this.$refs.unlockModalRef.open(this.unlockSourceRow);
  509. },
  510. // 新接口返回 stockCountBase,旧接口使用 measureQuantity 表示当前库存
  511. getStockCount(row) {
  512. if (row.stockCountBase !== undefined && row.stockCountBase !== null) {
  513. return Number(row.stockCountBase);
  514. }
  515. return Number(row.measureQuantity || 0);
  516. },
  517. getAvailableStockCount(row) {
  518. if (
  519. row.availableCountBase !== undefined &&
  520. row.availableCountBase !== null
  521. ) {
  522. return Number(row.availableCountBase);
  523. }
  524. const available =
  525. this.getStockCount(row) - Number(row.lockQuantity || 0);
  526. return Math.max(Number(available.toFixed(4)), 0);
  527. },
  528. // 限制订单数量不超过当前可用数量
  529. handleSaleCountInput(row) {
  530. const available = this.getAvailableStockCount(row);
  531. const val = parseFloat(row.saleCount);
  532. if (!isNaN(val) && val > available) {
  533. this.$message.warning(`订单数量不能超过可用数量(${available})`);
  534. this.$set(row, 'saleCount', available);
  535. }
  536. },
  537. handleClose() {
  538. this.clearTableSelection();
  539. this.visible = false;
  540. },
  541. isSameStockRecord(source, target) {
  542. if (source.batchStockId && target.id) {
  543. return source.batchStockId == target.id;
  544. }
  545. const sourceProductId = source.productId || source.categoryId;
  546. const targetProductId = target.categoryId || target.productId;
  547. const sourceProductCode =
  548. source.productCode || source.categoryCode || source.code;
  549. const targetProductCode =
  550. target.categoryCode || target.productCode || target.code;
  551. const sameProduct =
  552. sourceProductId && targetProductId
  553. ? sourceProductId == targetProductId
  554. : sourceProductCode == targetProductCode;
  555. const sameWarehouse =
  556. !source.warehouseId ||
  557. !target.warehouseId ||
  558. source.warehouseId == target.warehouseId;
  559. return (
  560. sameProduct &&
  561. sameWarehouse &&
  562. source.batchNo == target.batchNo &&
  563. (source.systemBatchNo || '') == (target.systemBatchNo || '')
  564. );
  565. },
  566. async selected() {
  567. if (!this.current && !this.selection.length) {
  568. return this.$message.warning('请至少选择一条数据');
  569. }
  570. if (this.currentIndex != -1) {
  571. if (
  572. this.data.some((item) => this.isSameStockRecord(item, this.current))
  573. ) {
  574. return this.$message.error('选择的库存记录已经存在列表了');
  575. }
  576. } else {
  577. if (
  578. this.selection.some((item) =>
  579. this.data.some((source) => this.isSameStockRecord(source, item))
  580. )
  581. ) {
  582. return this.$message.error('选择的库存记录已经存在列表了');
  583. }
  584. }
  585. let list = this.currentIndex == -1 ? this.selection : [this.current];
  586. //获取供应商
  587. if (this.isSupplier) {
  588. let supplierList = await this.getSupplierObj(list);
  589. list.forEach((item) => {
  590. item['entrustedEnterpriseIdList'] = supplierList[item.id];
  591. if (supplierList[item.id]?.length === 1) {
  592. item['entrustedEnterpriseId'] = supplierList[item.id][0].id;
  593. }
  594. });
  595. }
  596. let idList = list.map((item) => item.categoryId);
  597. // 获取包装规格(接口异常时置空,不阻断选择流程)
  598. let packingSpecification = [];
  599. try {
  600. packingSpecification = await getCategoryPackageDisposition({
  601. categoryIds: idList
  602. });
  603. } catch (e) {
  604. console.error('获取包装规格失败:', e);
  605. }
  606. list.forEach((item, index) => {
  607. item.productName = item.categoryName;
  608. item.productCode = item.categoryCode;
  609. item.productCategoryName = '';
  610. item.productBrand = item.brandNum;
  611. item.provenance = item.purchaseOrigins?.map((val) => val + '') || [];
  612. if (this.showSaleCount) {
  613. item.saleCount = item.saleCount;
  614. }
  615. item['packageDispositionList'] = (packingSpecification || [])
  616. .filter(
  617. (ite) => item.categoryId == ite.categoryId && ite.conversionUnit
  618. )
  619. .sort((a, b) => a.sort - b.sort);
  620. this.$set(list[index], 'totalCount', 1);
  621. // item.totalCount = 1;
  622. item.measuringUnit = item.measureUnit;
  623. //item.weightUnit = item.weightUnit;
  624. // item.warehouseNum = item.measureQuantity;
  625. item.productId = item.categoryId;
  626. item.batchStockId = item.id;
  627. item.id = '';
  628. item.batchNo = this.dimension != 1 ? item.batchNo : '';
  629. item.systemBatchNo =
  630. this.dimension != 1 ? item.systemBatchNo || '' : '';
  631. // item.specification = item.specification;
  632. });
  633. this.$emit('changeParent', list, this.currentIndex);
  634. this.handleClose();
  635. }
  636. }
  637. };
  638. </script>
  639. <style lang="scss" scoped>
  640. .tree_col {
  641. border: 1px solid #eee;
  642. padding: 10px 0;
  643. box-sizing: border-box;
  644. height: 500px;
  645. overflow: auto;
  646. }
  647. .table_col {
  648. padding-left: 10px;
  649. ::v-deep .el-table th.el-table__cell {
  650. background: #f2f2f2;
  651. }
  652. }
  653. .pagination {
  654. text-align: right;
  655. padding: 10px 0;
  656. }
  657. .btns {
  658. text-align: center;
  659. padding: 10px 0;
  660. }
  661. .topsearch {
  662. margin-bottom: 15px;
  663. }
  664. /* 库存解锁按钮旁展示当前选中的库存数据,避免表格级操作定位不明 */
  665. .unlock-tip {
  666. margin-left: 8px;
  667. font-size: 12px;
  668. color: #909399;
  669. }
  670. </style>