云
2 小时以前 7a93a85f75bcfc8001ea1d3b921860806a8209ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<script lang="ts" setup>
import type { FormType } from '../data';
 
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesWmMiscIssueLineApi } from '#/api/mes/wm/miscissue/line';
 
import type { TraceResult } from '#/utils/traceLookup';
 
import { computed } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
 
import { message } from 'ant-design-vue';
 
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  deleteMiscIssueLine,
  getMiscIssueLinePage,
} from '#/api/mes/wm/miscissue/line';
import TraceScanPanel from '#/components/TraceScanPanel.vue';
import { $t } from '#/locales';
import { findMaterialStocks, traceQuantity } from '#/utils/traceLookup';
 
import { useLineGridColumns } from '../data';
import LineForm from './line-form.vue';
 
const props = defineProps<{
  formType: FormType;
  issueId: number;
}>();
 
const isEditable = computed(() =>
  // 是否可编辑明细行
  ['create', 'update'].includes(props.formType),
);
 
const [LineFormModal, lineFormModalApi] = useVbenModal({
  connectedComponent: LineForm,
  destroyOnClose: true,
});
 
/** 刷新表格 */
function handleRefresh() {
  gridApi.query();
}
 
/** 添加物料 */
function handleCreate() {
  lineFormModalApi.setData({ issueId: props.issueId }).open();
}
 
/** 扫码带出:按追溯码匹配库存记录后打开新增行表单 */
async function handleTraceConfirm(trace: TraceResult) {
  try {
    const stocks = await findMaterialStocks(trace);
    const stock = stocks.find((item) => (item.quantity ?? 0) > 0) ?? stocks[0];
    if (!stock) {
      message.warning('未找到该追溯码对应的库存记录');
      return;
    }
    const quantity = traceQuantity(trace);
    lineFormModalApi
      .setData({
        defaults: {
          areaId: stock.areaId,
          batchCode: stock.batchCode,
          itemId: stock.itemId,
          locationId: stock.locationId,
          materialStockId: stock.id,
          quantity:
            quantity && stock.quantity
              ? Math.min(quantity, stock.quantity)
              : (quantity ?? stock.quantity),
          quantityMax: stock.quantity,
          warehouseId: stock.warehouseId,
        },
        issueId: props.issueId,
      })
      .open();
  } catch (e) {
    message.error(e instanceof Error ? e.message : '扫码带出失败');
  }
}
 
/** 编辑物料 */
function handleEdit(row: MesWmMiscIssueLineApi.MiscIssueLine) {
  lineFormModalApi.setData({ id: row.id, issueId: props.issueId }).open();
}
 
/** 删除物料 */
async function handleDelete(row: MesWmMiscIssueLineApi.MiscIssueLine) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', [row.itemName]),
    duration: 0,
  });
  try {
    await deleteMiscIssueLine(row.id!);
    message.success($t('ui.actionMessage.deleteSuccess', [row.itemName]));
    handleRefresh();
  } finally {
    hideLoading();
  }
}
 
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: useLineGridColumns(isEditable.value),
    height: 360,
    keepSource: true,
    proxyConfig: {
      ajax: {
        query: async ({ page }) => {
          if (!props.issueId) {
            return { list: [], total: 0 };
          }
          return await getMiscIssueLinePage({
            issueId: props.issueId,
            pageNo: page.currentPage,
            pageSize: page.pageSize,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    toolbarConfig: {
      refresh: true,
    },
  } as VxeTableGridOptions<MesWmMiscIssueLineApi.MiscIssueLine>,
});
</script>
 
<template>
  <div>
    <LineFormModal @success="handleRefresh" />
    <TraceScanPanel
      v-if="isEditable"
      title="扫码带出出库明细"
      @confirm="handleTraceConfirm"
    />
    <Grid table-title="物料信息">
      <template v-if="isEditable" #toolbar-tools>
        <TableAction
          :actions="[
            {
              label: '添加物料',
              type: 'primary',
              icon: ACTION_ICON.ADD,
              onClick: handleCreate,
            },
          ]"
        />
      </template>
      <template #actions="{ row }">
        <TableAction
          :actions="[
            {
              label: $t('common.edit'),
              type: 'link',
              icon: ACTION_ICON.EDIT,
              ifShow: isEditable,
              onClick: handleEdit.bind(null, row),
            },
            {
              label: $t('common.delete'),
              type: 'link',
              danger: true,
              icon: ACTION_ICON.DELETE,
              ifShow: isEditable,
              popConfirm: {
                title: $t('ui.actionMessage.deleteConfirm', [row.itemName]),
                confirm: handleDelete.bind(null, row),
              },
            },
          ]"
        />
      </template>
    </Grid>
  </div>
</template>