selectStockLedgerDialog.vue 21 KB

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