selectStockLedgerDialog.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. tabMixinsInit: false,
  168. widthsVersion: 0
  169. };
  170. },
  171. watch: {},
  172. computed: {
  173. columns() {
  174. const version = this.columnsVersion;
  175. // 强制重新计算(打开弹窗时重新读取缓存宽度)
  176. this.widthsVersion;
  177. const cacheWidths = this.getStorage(this.cacheKeyUrl + 'Widths') || {};
  178. const applyWidth = (col) => {
  179. if (col.prop && cacheWidths[col.prop]) {
  180. return { ...col, width: cacheWidths[col.prop], minWidth: undefined };
  181. }
  182. return col;
  183. };
  184. let data = null;
  185. if (this.currentIndex != -1) {
  186. data = {
  187. action: 'action',
  188. slot: 'action',
  189. align: 'center',
  190. label: '选择',
  191. reserveSelection: true
  192. };
  193. }
  194. if (this.currentIndex == -1) {
  195. data = {
  196. label: '选择',
  197. width: 45,
  198. type: 'selection',
  199. columnKey: 'selection',
  200. align: 'center',
  201. // 分页切换后保留已勾选的库存,确认时可以一次提交多页选择
  202. reserveSelection: true,
  203. selectable: (row) => {
  204. return this.getAvailableStockCount(row) != 0;
  205. }
  206. };
  207. }
  208. const tempData = [
  209. data,
  210. {
  211. columnKey: 'index',
  212. type: 'index',
  213. width: 50,
  214. align: 'center',
  215. showOverflowTooltip: true,
  216. label: '序号'
  217. },
  218. {
  219. prop: 'categoryCode',
  220. label: '编码',
  221. align: 'center',
  222. showOverflowTooltip: true,
  223. minWidth: 110
  224. },
  225. {
  226. prop: 'categoryName',
  227. label: '名称',
  228. align: 'center',
  229. showOverflowTooltip: true,
  230. minWidth: 110
  231. },
  232. {
  233. prop: 'saleCount',
  234. slot: 'saleCount',
  235. label: '订单数量',
  236. showOverflowTooltip: true,
  237. align: 'center',
  238. minWidth: 120,
  239. hide: !this.showSaleCount
  240. },
  241. {
  242. prop: 'brandNum',
  243. align: 'center',
  244. label: '牌号',
  245. showOverflowTooltip: true
  246. },
  247. this.dimension == '2'
  248. ? {
  249. prop: 'batchNo',
  250. label: '批次号',
  251. align: 'center',
  252. showOverflowTooltip: true
  253. }
  254. : {
  255. width: 1
  256. },
  257. this.dimension == '2'
  258. ? {
  259. prop: 'systemBatchNo',
  260. label: '系统内部批次号',
  261. align: 'center',
  262. minWidth: 180,
  263. showOverflowTooltip: true,
  264. formatter: (row) => row.systemBatchNo || '-'
  265. }
  266. : {
  267. width: 1
  268. },
  269. {
  270. prop: 'categoryModel',
  271. label: '型号',
  272. align: 'center',
  273. showOverflowTooltip: true
  274. },
  275. {
  276. prop: 'specification',
  277. label: '规格',
  278. align: 'center',
  279. showOverflowTooltip: true
  280. },
  281. {
  282. prop: 'colorKey',
  283. label: '颜色',
  284. align: 'center',
  285. showOverflowTooltip: true
  286. },
  287. {
  288. prop: 'modelKey',
  289. label: '机型',
  290. align: 'center',
  291. showOverflowTooltip: true
  292. },
  293. {
  294. prop: 'warehouseName',
  295. label: '仓库',
  296. showOverflowTooltip: true,
  297. align: 'center',
  298. minWidth: 90
  299. },
  300. {
  301. prop: 'inventoryCycle',
  302. label: '存货周期(天)',
  303. align: 'center',
  304. width: 150,
  305. sortable: true,
  306. showOverflowTooltip: true,
  307. hide: this.dimension == 1
  308. },
  309. {
  310. prop: 'measureQuantity',
  311. label: '库存',
  312. showOverflowTooltip: true,
  313. align: 'center',
  314. minWidth: 90,
  315. formatter: (row) => this.getStockCount(row)
  316. },
  317. {
  318. prop: 'lockQuantity',
  319. label: '锁库数量',
  320. showOverflowTooltip: true,
  321. align: 'center',
  322. minWidth: 90
  323. },
  324. {
  325. prop: 'useQuantity',
  326. label: '可用数量',
  327. showOverflowTooltip: true,
  328. align: 'center',
  329. minWidth: 90,
  330. formatter: (row) => {
  331. return this.getAvailableStockCount(row);
  332. }
  333. },
  334. {
  335. prop: 'measureUnit',
  336. label: '单位',
  337. showOverflowTooltip: true,
  338. align: 'center',
  339. minWidth: 90
  340. },
  341. {
  342. prop: 'weight',
  343. label: '重量',
  344. showOverflowTooltip: true,
  345. align: 'center',
  346. minWidth: 90
  347. },
  348. {
  349. prop: 'weightUnit',
  350. label: '重量单位',
  351. showOverflowTooltip: true,
  352. align: 'center',
  353. minWidth: 90
  354. },
  355. {
  356. prop: 'packingUnit',
  357. align: 'center',
  358. label: '包装单位',
  359. showOverflowTooltip: true
  360. },
  361. {
  362. prop: 'reelSpecification',
  363. label: '盘具',
  364. showOverflowTooltip: true,
  365. align: 'center'
  366. }
  367. ];
  368. // 应用 mixin 列配置与本地列宽缓存后返回
  369. return this.applyColumnWidth(tempData.filter((item) => !item.hide)).map(
  370. applyWidth
  371. );
  372. },
  373. // 库存解锁操作的来源行:优先取唯一勾选行,否则取当前点击高亮行
  374. unlockSourceRow() {
  375. if (this.selection.length === 1) {
  376. return this.selection[0];
  377. }
  378. return this.currentRow || null;
  379. },
  380. // 解锁按钮旁的选中数据提示,避免表格级操作定位不明
  381. unlockSourceLabel() {
  382. const row = this.unlockSourceRow;
  383. if (!row) {
  384. return '';
  385. }
  386. const code = row.categoryCode || '-';
  387. const batch = row.systemBatchNo || row.batchNo || '-';
  388. return `已选:${code} / ${batch} / 锁库 ${row.lockQuantity || 0}`;
  389. }
  390. },
  391. methods: {
  392. getDictV(code, val) {
  393. if (!this.dictList[code]) return '';
  394. return this.dictList[code].find((item) => item.value == val)?.label;
  395. },
  396. open(item, currentIndex) {
  397. console.log(item, currentIndex);
  398. this.widthsVersion++;
  399. this.currentIndex = currentIndex;
  400. this.visible = true;
  401. this.getTreeData();
  402. },
  403. async getTreeData() {
  404. try {
  405. this.treeLoading = true;
  406. let res = null;
  407. res = await getTreeByGroup({ type: 2 });
  408. this.treeLoading = false;
  409. if (res?.code === '0') {
  410. this.treeList = res.data;
  411. return this.treeList;
  412. }
  413. } catch (error) {
  414. console.log(error);
  415. }
  416. this.treeLoading = false;
  417. },
  418. /* 表格数据源 */
  419. async datasource({ page, limit, where, order }) {
  420. this.dimension = where.dimension || '2';
  421. let api = where.dimension == '1' ? getOutindetailtwoList : getBatchList;
  422. const data = await api({
  423. ...where,
  424. ...order,
  425. pageNum: page,
  426. size: limit
  427. });
  428. // 与 WMS 库存台账保持一致:库存取 stockCountBase,旧接口无该字段时回退 measureQuantity;
  429. // 可用数量按库存减锁库数量计算,并统一将负数归零。
  430. if (data && Array.isArray(data.list)) {
  431. data.list = data.list.map((item, index) => {
  432. const stockCountBase = Math.max(
  433. Number(
  434. item.stockCountBase !== undefined && item.stockCountBase !== null
  435. ? item.stockCountBase
  436. : item.measureQuantity
  437. ) || 0,
  438. 0
  439. );
  440. const lockQuantity = Number(item.lockQuantity) || 0;
  441. const availableCountBase = Math.max(
  442. stockCountBase - lockQuantity,
  443. 0
  444. );
  445. return {
  446. ...item,
  447. stockCountBase,
  448. availableCountBase,
  449. _tableRowKey: `${item.id || 'row'}_${page}_${index}`
  450. };
  451. });
  452. }
  453. // 库存接口在同一物品/批次存在多条台账时可能返回重复 id。
  454. // id 仍作为业务主键保留,表格使用仅用于渲染的唯一 key,避免 Vue patch 冲突。
  455. this.getTabColumns();
  456. return data;
  457. },
  458. /* 清空表格勾选与当前行:搜索、切换分页/排序、关闭弹窗时都必须重置 */
  459. clearTableSelection() {
  460. this.selection = [];
  461. this.currentRow = null;
  462. this.current = null;
  463. this.radio = '';
  464. if (this.$refs.table && this.$refs.table.clearSelection) {
  465. this.$refs.table.clearSelection();
  466. }
  467. },
  468. /* 刷新表格 */
  469. reload(where) {
  470. // 查询条件变化后原勾选行已不在结果集中,先清空避免带着旧数据继续操作
  471. this.clearTableSelection();
  472. this.$refs.table.reload({ pageNum: 1, where: where });
  473. },
  474. /* 分页、排序后清除当前行,避免解锁操作误用上一页数据,但保留跨页勾选 */
  475. handleTableDone() {
  476. this.currentRow = null;
  477. this.current = null;
  478. this.radio = '';
  479. },
  480. handleNodeClick(data, node) {
  481. this.curNodeData = data;
  482. if (data.id == this.currentId) {
  483. this.$refs.treeRef.setCurrentKey(null);
  484. this.reload();
  485. } else {
  486. this.reload({ categoryLevelId: data.id });
  487. }
  488. },
  489. async getSupplierObj(productList, queryName) {
  490. try {
  491. let categoryIds = productList
  492. .filter((item) => item.id)
  493. .map((item) => item.id);
  494. if (categoryIds.length > 0) {
  495. return await contactQueryByCategoryIdsAPI({
  496. categoryIds,
  497. isQueryEE: 1
  498. });
  499. } else {
  500. return Promise.resolve({});
  501. }
  502. } catch (e) {
  503. return Promise.resolve({});
  504. }
  505. },
  506. // 单击获取id
  507. cellClick(row) {
  508. if (this.currentIndex == -1) return;
  509. this.current = row;
  510. this.radio = row.id;
  511. },
  512. // 打开库存解锁申请弹窗(校验与数据规格化由子弹窗负责)
  513. openUnlock() {
  514. if (!this.unlockSourceRow) {
  515. return this.$message.warning('请先选择一条库存数据进行解锁');
  516. }
  517. this.$refs.unlockModalRef.open(this.unlockSourceRow);
  518. },
  519. // 列头拖拽结束 - 保存列宽到本地缓存
  520. handleHeaderDragend(newWidth, oldWidth, column) {
  521. if (!column.property) return;
  522. const widths = this.getStorage(this.cacheKeyUrl + 'Widths') || {};
  523. widths[column.property] = newWidth;
  524. this.setStorage(this.cacheKeyUrl + 'Widths', widths);
  525. },
  526. // 新接口返回 stockCountBase,旧接口使用 measureQuantity 表示当前库存
  527. getStockCount(row) {
  528. if (row.stockCountBase !== undefined && row.stockCountBase !== null) {
  529. return Number(row.stockCountBase);
  530. }
  531. return Number(row.measureQuantity || 0);
  532. },
  533. getAvailableStockCount(row) {
  534. if (row.availableCountBase !== undefined && row.availableCountBase !== null) {
  535. return Number(row.availableCountBase);
  536. }
  537. const available = this.getStockCount(row) - Number(row.lockQuantity || 0);
  538. return Math.max(Number(available.toFixed(4)), 0);
  539. },
  540. // 限制订单数量不超过当前可用数量
  541. handleSaleCountInput(row) {
  542. const available = this.getAvailableStockCount(row);
  543. const val = parseFloat(row.saleCount);
  544. if (!isNaN(val) && val > available) {
  545. this.$message.warning(`订单数量不能超过可用数量(${available})`);
  546. this.$set(row, 'saleCount', available);
  547. }
  548. },
  549. handleClose() {
  550. this.clearTableSelection();
  551. this.visible = false;
  552. },
  553. isSameStockRecord(source, target) {
  554. if (source.batchStockId && target.id) {
  555. return source.batchStockId == target.id;
  556. }
  557. const sourceProductId = source.productId || source.categoryId;
  558. const targetProductId = target.categoryId || target.productId;
  559. const sourceProductCode =
  560. source.productCode || source.categoryCode || source.code;
  561. const targetProductCode =
  562. target.categoryCode || target.productCode || target.code;
  563. const sameProduct =
  564. sourceProductId && targetProductId
  565. ? sourceProductId == targetProductId
  566. : sourceProductCode == targetProductCode;
  567. const sameWarehouse =
  568. !source.warehouseId ||
  569. !target.warehouseId ||
  570. source.warehouseId == target.warehouseId;
  571. return (
  572. sameProduct &&
  573. sameWarehouse &&
  574. source.batchNo == target.batchNo &&
  575. (source.systemBatchNo || '') == (target.systemBatchNo || '')
  576. );
  577. },
  578. async selected() {
  579. if (!this.current && !this.selection.length) {
  580. return this.$message.warning('请至少选择一条数据');
  581. }
  582. if (this.currentIndex != -1) {
  583. if (
  584. this.data.some((item) => this.isSameStockRecord(item, this.current))
  585. ) {
  586. return this.$message.error('选择的库存记录已经存在列表了');
  587. }
  588. } else {
  589. if (
  590. this.selection.some((item) =>
  591. this.data.some((source) => this.isSameStockRecord(source, item))
  592. )
  593. ) {
  594. return this.$message.error('选择的库存记录已经存在列表了');
  595. }
  596. }
  597. let list = this.currentIndex == -1 ? this.selection : [this.current];
  598. //获取供应商
  599. if (this.isSupplier) {
  600. let supplierList = await this.getSupplierObj(list);
  601. list.forEach((item) => {
  602. item['entrustedEnterpriseIdList'] = supplierList[item.id];
  603. if (supplierList[item.id]?.length === 1) {
  604. item['entrustedEnterpriseId'] = supplierList[item.id][0].id;
  605. }
  606. });
  607. }
  608. let idList = list.map((item) => item.categoryId);
  609. // 获取包装规格(接口异常时置空,不阻断选择流程)
  610. let packingSpecification = [];
  611. try {
  612. packingSpecification = await getCategoryPackageDisposition({
  613. categoryIds: idList
  614. });
  615. } catch (e) {
  616. console.error('获取包装规格失败:', e);
  617. }
  618. list.forEach((item, index) => {
  619. item.productName = item.categoryName;
  620. item.productCode = item.categoryCode;
  621. item.productCategoryName = '';
  622. item.productBrand = item.brandNum;
  623. item.provenance = item.provenance || [];
  624. if (this.showSaleCount) {
  625. item.saleCount = item.saleCount;
  626. }
  627. item['packageDispositionList'] = (packingSpecification || [])
  628. .filter(
  629. (ite) => item.categoryId == ite.categoryId && ite.conversionUnit
  630. )
  631. .sort((a, b) => a.sort - b.sort);
  632. this.$set(list[index], 'totalCount', 1);
  633. // item.totalCount = 1;
  634. item.measuringUnit = item.measureUnit;
  635. //item.weightUnit = item.weightUnit;
  636. // item.warehouseNum = item.measureQuantity;
  637. item.productId = item.categoryId;
  638. item.batchStockId = item.id;
  639. item.id = '';
  640. item.batchNo = this.dimension != 1 ? item.batchNo : '';
  641. item.systemBatchNo =
  642. this.dimension != 1 ? item.systemBatchNo || '' : '';
  643. // item.specification = item.specification;
  644. });
  645. this.$emit('changeParent', list, this.currentIndex);
  646. this.handleClose();
  647. }
  648. }
  649. };
  650. </script>
  651. <style lang="scss" scoped>
  652. .tree_col {
  653. border: 1px solid #eee;
  654. padding: 10px 0;
  655. box-sizing: border-box;
  656. height: 500px;
  657. overflow: auto;
  658. }
  659. .table_col {
  660. padding-left: 10px;
  661. ::v-deep .el-table th.el-table__cell {
  662. background: #f2f2f2;
  663. }
  664. }
  665. .pagination {
  666. text-align: right;
  667. padding: 10px 0;
  668. }
  669. .btns {
  670. text-align: center;
  671. padding: 10px 0;
  672. }
  673. .topsearch {
  674. margin-bottom: 15px;
  675. }
  676. /* 库存解锁按钮旁展示当前选中的库存数据,避免表格级操作定位不明 */
  677. .unlock-tip {
  678. margin-left: 8px;
  679. font-size: 12px;
  680. color: #909399;
  681. }
  682. </style>