index.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <script setup lang="ts">
  2. import { ref } from "vue";
  3. import { ElButton, ElDialog, ElMessage } from "element-plus";
  4. import { execlUpload } from "/@/components/execlUpload";
  5. import { useResponseHandle } from "/@/hooks";
  6. import { useVModel } from "@vueuse/core";
  7. import { mapLabelToSource, mapLabelToType } from "../../config/shared";
  8. import { httpAdd } from "/@/api/invoiceInOut/inOutManager";
  9. import { columns, mapLabelToProp } from "./columns-config";
  10. import { mapGoodLabelToType, mapLabelToInvtype } from "/@/utils/status";
  11. import { isImportDataValid } from "./validator";
  12. import { useCompany } from "/@/hooks/core";
  13. const loading = ref(false);
  14. const tableData = ref([]);
  15. const emit = defineEmits(["refresh"]);
  16. const props = defineProps<{ visible: boolean }>()
  17. const { getCurrentLicense } = useCompany()
  18. const visible = useVModel(props, 'visible')
  19. const responseHandle = useResponseHandle();
  20. const Uploadsuccess = ({ results, header }) => {
  21. loading.value = true;
  22. if (results.length === 0) {
  23. ElMessage.error("表格无有效数据!");
  24. loading.value = false;
  25. return;
  26. }
  27. let headok = true;
  28. console.log(header.length, columns.length - 1)
  29. if (header.length !== columns.length - 1) {
  30. headok = false;
  31. } else {
  32. columns.slice(1).forEach((si, sii) => {
  33. if (si.label !== header[sii]) {
  34. headok = false;
  35. }
  36. });
  37. }
  38. if (!headok) {
  39. ElMessage.error("表头与导入模板不匹配!");
  40. loading.value = false;
  41. return;
  42. }
  43. tableData.value = [];
  44. const result = []
  45. for(const tableItem of results){
  46. const item = {}
  47. Object.keys(tableItem).forEach(label => {
  48. const prop = mapLabelToProp[label]
  49. let value = tableItem[label]
  50. if (label === '发票类型') {
  51. value = value.replace('(', '(')
  52. value = value.replace(')', ')')
  53. }
  54. // const labels = ['税率', '销售方公司纳税号', '购买方纳税号', '单位']
  55. // if (labels.includes(label.replace('_1', ''))) {
  56. // if ((prop || "").includes('inv_')) {
  57. // item[prop.replace('inv_', '')] = value
  58. // } else {
  59. // const prop = mapLabelToProp[label.replace('_1', '')]
  60. // item[prop] = value
  61. // }
  62. // } else {
  63. // }
  64. item[prop] = value.trim()
  65. })
  66. result.push(item)
  67. }
  68. const license = getCurrentLicense()
  69. const inv_buyer_codes = [...new Set(result.map(({ inv_buyer_code }) => inv_buyer_code))]
  70. if (inv_buyer_codes.length !== 1) {
  71. ElMessage.warning('导入数据的购买方公司必须一致')
  72. return
  73. }
  74. if (inv_buyer_codes[0] !== license) {
  75. ElMessage.warning('导入数据购买方公司与当前选择公司不一致')
  76. return
  77. }
  78. if(isImportDataValid(result)){ tableData.value = result }
  79. loading.value = false;
  80. };
  81. //提交
  82. const handleSubmit = async () => {
  83. try {
  84. if (loading.value) return;
  85. loading.value = true;
  86. const list = [];
  87. tableData.value.forEach(item => {
  88. list.push({
  89. ...item,
  90. inv_type: mapLabelToInvtype[item.inv_type],
  91. source: mapLabelToSource[item.source],
  92. type: mapLabelToType[item.type],
  93. goodType: mapGoodLabelToType[item.goodType],
  94. inv_num: Number(item.inv_num).toFixed(8),
  95. inv_subprice: Number(item.inv_subprice).toFixed(8),
  96. inv_subtotal: Number(item.inv_subtotal).toFixed(8),
  97. inv_price: Number(item.inv_price).toFixed(8),
  98. inv_total: Number(item.inv_tax_total).toFixed(8),
  99. platform_type: "1",
  100. channel: "2",
  101. relaArr: [{
  102. id: item.relaGoodNo,
  103. num: item.relaGoodNum
  104. }]
  105. })
  106. })
  107. // console.log(list)
  108. // return
  109. const { code, message,data: _d } = await httpAdd({ list });
  110. loading.value = false;
  111. responseHandle({
  112. code,
  113. message,
  114. noMessage: false,
  115. handler: () => {
  116. ElMessage.success("数据导入成功!");
  117. emit("refresh");
  118. visible.value = false;
  119. }
  120. });
  121. } catch (err) {
  122. console.log(err)
  123. }
  124. };
  125. const cancel = () => {
  126. tableData.value = [];
  127. };
  128. </script>
  129. <template>
  130. <ElDialog
  131. v-model="visible"
  132. :close-on-click-modal="false"
  133. title="非订单商品导入"
  134. width="1040px"
  135. top="8vh"
  136. center
  137. @close="tableData = []"
  138. >
  139. <execlUpload style="margin-bottom: 10px" @on-success="Uploadsuccess" v-if="tableData.length === 0" />
  140. <el-table
  141. :data="tableData"
  142. stripe
  143. border
  144. max-height="500px"
  145. size="small"
  146. style="width: 100%"
  147. >
  148. <el-table-column
  149. v-for="(si, sii) in columns"
  150. :minWidth="si.minWidth"
  151. show-overflow-tooltip
  152. :fixed="si.fixed"
  153. :prop="si.prop"
  154. :type="si.type"
  155. :key="sii"
  156. >
  157. <template #header>
  158. <span v-if="!si.required">
  159. {{ si.label }}
  160. </span>
  161. <p v-else>
  162. <span style="color: #f56c6c; font-size: 14px">* </span>
  163. {{ si.label }}
  164. </p>
  165. </template>
  166. </el-table-column>
  167. </el-table>
  168. <div
  169. flex
  170. justify-end
  171. gap-2
  172. v-if="tableData.length !== 0"
  173. style="padding: 10px 0 0 0"
  174. >
  175. <ElButton size="small" @click="cancel">取消</ElButton>
  176. <el-button
  177. size="small"
  178. type="primary"
  179. :loading="loading"
  180. @click="handleSubmit"
  181. >保存</el-button
  182. >
  183. </div>
  184. </ElDialog>
  185. </template>
  186. <style lang="scss" scoped></style>