BaseForm.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <script setup lang="ts">
  2. import { computed, onMounted, reactive, ref } from "vue";
  3. import { cg_order_type_options, xs_inv_type_list, tax_list } from "/@/utils/status"
  4. import { ElButton, ElCol, ElForm, ElFormItem, ElMessage, ElOption, ElSelect, ElTooltip } from "element-plus"
  5. import { httpCompanies } from "/@/api/invoiceInOut/productManager"
  6. import RemoteSelect from "/@/components/RemoteSelect";
  7. import InvoiceTitle from "./InvoiceTitle.vue";
  8. import { rules } from "./rules"
  9. import { UnitInput } from "/@/components/Input"
  10. import { convertInvoiceTitle, convertInvoiceTitleData, convertPurchaseInvoiceTitleData, createInitalData } from "./shared"
  11. import { useCurrentCompany, useTask } from "/@/hooks/core"
  12. import { useRouter } from "vue-router"
  13. import ChooseGoodModal from "./ChooseGoodModal/index.vue"
  14. import ChildGoodEditModal from "./ChildGoodEditModal.vue";
  15. import { httpInvoiceList } from "/@/api/InvoiceSales/invoiceApply";
  16. import { httpAdd, httpCatist } from "/@/api/invoiceInOut/productManager";
  17. import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
  18. import QueryCategory from "./query-category.vue";
  19. import dayjs from "dayjs";
  20. const { current } = useCurrentCompany()
  21. const rulesRef = computed(() => ({
  22. ...rules,
  23. skuCode: [{ required: formData.value.good_source == '1', message: '请输入商品编号', trigger: 'change' }]
  24. }))
  25. const router = useRouter()
  26. const formRef = ref<InstanceType<typeof ElForm> | null>(null)
  27. const state = reactive({
  28. editVisible: false,
  29. visible: false,
  30. companies: [],
  31. index: -1,
  32. data: {}
  33. })
  34. const createTask = useTask({ success(){ router.push('/invoiceInOut/productManager') } })
  35. const formData = ref(createInitalData())
  36. const sellerInvoiceTitle = ref<Record<string, string>>({});
  37. const purchaserInvoiceTitle = ref<Record<string, string>>({});
  38. async function submit(){
  39. if(formData.value.is_combind === '1' && formData.value.childArr.length === 0){
  40. ElMessage.warning('请选择至少一个子商品')
  41. return
  42. }
  43. if(formData.value.is_combind === '0'){
  44. formData.value.childArr = []
  45. }
  46. try{
  47. await formRef.value?.validate()
  48. createTask.run(httpAdd({
  49. ...formData.value,
  50. skuCode: formData.value.good_source == '1' ? formData.value.skuCode : generateNumber(),
  51. childArr: formData.value.childArr.map(({ child_id, child_num }) => ({ child_id, child_num }))
  52. }))
  53. }catch(err){
  54. console.log(err)
  55. }
  56. }
  57. function handleInvoiceTitle(_isSeller: boolean, invoiceTitle: Record<string, string> ) {
  58. if (!_isSeller) {
  59. invoiceTitle = state.companies.find(item => item.company_license === formData.value.buyer_code)
  60. formData.value.buyer_name = invoiceTitle.company_name
  61. // formData.value.seller_code = invoiceTitle.company_name
  62. //支持的开票方式
  63. // const { invoiceType, denomination: _denomination } = invoiceTitle;
  64. // const chunks = invoiceType.split(",");
  65. // ruleForm.value.invtype = "";
  66. // invoiceTypes.value = xs_inv_type_list.filter(({ value }) => chunks.includes(value));
  67. // formData.value.seller_name =
  68. } else{
  69. formData.value.seller_name = invoiceTitle.name
  70. }
  71. if (!invoiceTitle) {
  72. if (_isSeller) { return (sellerInvoiceTitle.value = {}); }
  73. return (purchaserInvoiceTitle.value = {});
  74. }
  75. _isSeller
  76. ? (sellerInvoiceTitle.value = convertInvoiceTitleData(convertInvoiceTitle(invoiceTitle)))
  77. : (purchaserInvoiceTitle.value = convertPurchaseInvoiceTitleData(invoiceTitle));
  78. }
  79. const setSellerInvoiceTitle = handleInvoiceTitle.bind(null, true);
  80. const setPurchaserInvoiceTitle = handleInvoiceTitle.bind(null, false);
  81. function handleSubmit(list: any[]){
  82. const currentIds = formData.value.childArr.map(({ id }) => id)
  83. const appends: any[] = []
  84. for(const item of list){
  85. if(currentIds.includes(item.id)){ continue }
  86. appends.push(item)
  87. }
  88. formData.value.childArr = [ ...formData.value.childArr, ...appends ]
  89. }
  90. function handleChildGoodEdit(index: number){
  91. state.index = index
  92. state.editVisible = true
  93. state.data = { ...formData.value.childArr[index] }
  94. }
  95. (async function initialData(){
  96. const result = await httpCompanies({ size: 10000 })
  97. if(result.code == 0){
  98. state.companies = [...result.data.list]
  99. const item = state.companies.find(item => item.companyNo === current.value.companyNo)
  100. formData.value.buyer_code = item.company_license
  101. setPurchaserInvoiceTitle(item)
  102. }
  103. })()
  104. function getRandomIntInclusive(min, max) {
  105. min = Math.ceil(min);
  106. max = Math.floor(max);
  107. return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
  108. }
  109. function generateNumber(){
  110. let date = dayjs(new Date()).format('YY-MM-DD HH:mm:ss')
  111. date = date.replace(/-/g, '')
  112. date = date.replace(/:/g, '')
  113. date = date.replace(/ /g, '')
  114. let ms = String(dayjs().get('millisecond'))
  115. if(ms.length === 2){ ms = '0' + ms }
  116. if(ms.length === 1){ ms = '00' + ms }
  117. let prefix = formData.value.is_combind == '0' ? 'PKU' : 'PSU'
  118. const randomSuffix = getRandomIntInclusive(10, 99)
  119. const code = prefix + date + ms + randomSuffix
  120. return code
  121. }
  122. function handleGoodSourceChange(value){
  123. if(value == '2') formData.value.skuCode = ''
  124. }
  125. </script>
  126. <template>
  127. <ElForm ref="formRef" :rules="rulesRef" :model="formData" v-loading="createTask.loading">
  128. <ElRow :gutter="10">
  129. <ElCol :span="12">
  130. <ElFormItem label="销售方公司" prop="seller_code">
  131. <RemoteSelect style="width: 100%" request-prop="name" :api="httpInvoiceList" response-val-prop="registercode"
  132. placeholder="销售方公司抬头" v-model:value="formData.seller_code" response-label-prop="name"
  133. @item-change="setSellerInvoiceTitle" />
  134. </ElFormItem>
  135. <InvoiceTitle :detail="sellerInvoiceTitle" />
  136. </ElCol>
  137. <ElCol :span="12">
  138. <ElFormItem label="购买方公司" prop="buyer_code">
  139. <ElSelect style="width: 100%" placeholder="购买方公司" v-model="formData.buyer_code"
  140. @change="setPurchaserInvoiceTitle" disabled>
  141. <ElOption v-for="item in state.companies" :label="item.company_name" :value="item.company_license" />
  142. </ElSelect>
  143. </ElFormItem>
  144. <InvoiceTitle :detail="purchaserInvoiceTitle" />
  145. </ElCol>
  146. <ElCol :span="6">
  147. <ElFormItem label="商品来源" prop="good_source">
  148. <ElSelect @change="handleGoodSourceChange" v-model="formData.good_source" placeholder="商品来源"
  149. style="width: 100%">
  150. <ElOption label="采销商品" value="1" />
  151. <ElOption label="非采销商品" value="2" />
  152. </ElSelect>
  153. </ElFormItem>
  154. </ElCol>
  155. <ElCol :span="6">
  156. <ElFormItem label="组合类型" prop="is_combind">
  157. <ElSelect style="width: 100%" v-model="formData.is_combind">
  158. <ElOption label="非组合商品" value="0" />
  159. <ElOption label="组合商品" value="1" />
  160. </ElSelect>
  161. </ElFormItem>
  162. </ElCol>
  163. <ElCol :span="6">
  164. <ElFormItem label="订单商品类型" prop="good_type">
  165. <ElSelect v-model="formData.good_type" placeholder="订单商品类型" style="width: 100%">
  166. <ElOption v-for="opt in cg_order_type_options" :value="opt.value" :label="opt.label" />
  167. </ElSelect>
  168. </ElFormItem>
  169. </ElCol>
  170. <ElCol :span="6">
  171. <ElFormItem label="进项成本状态" prop="basic_status">
  172. <ElSelect v-model="formData.basic_status" placeholder="进项成本状态">
  173. <ElOption label="真实成本信息" value="1" />
  174. <ElOption label="预估成本信息" value="2" />
  175. </ElSelect>
  176. </ElFormItem>
  177. </ElCol>
  178. <ElCol :span="12">
  179. <ElFormItem label="发票商品名称" prop="inv_good_name">
  180. <ElInput v-model="formData.inv_good_name" placeholder="发票商品名称" />
  181. </ElFormItem>
  182. </ElCol>
  183. <ElCol :span="12">
  184. <ElFormItem label="订单商品名称" prop="goodName">
  185. <ElInput v-model="formData.goodName" placeholder="订单商品名称" />
  186. </ElFormItem>
  187. </ElCol>
  188. <ElCol :span="6">
  189. <ElFormItem label="商品编号" prop="skuCode">
  190. <ElInput placeholder="商品编号" v-model="formData.skuCode" :disabled="formData.good_source == '2'" />
  191. </ElFormItem>
  192. </ElCol>
  193. <ElCol :span="6">
  194. <ElFormItem label="单位" prop="unit">
  195. <ElInput v-model="formData.unit" placeholder="单位" />
  196. </ElFormItem>
  197. </ElCol>
  198. <ElCol :span="6">
  199. <ElFormItem label="规格" prop="spec">
  200. <ElInput v-model="formData.spec" placeholder="规格" />
  201. </ElFormItem>
  202. </ElCol>
  203. <ElCol :span="6">
  204. <ElFormItem label="重量" prop="unit_weight">
  205. <UnitInput unit="g" v-model="formData.unit_weight" placeholder="重量" />
  206. </ElFormItem>
  207. </ElCol>
  208. <ElCol :span="6">
  209. <ElFormItem label="发票类型" prop="inv_type">
  210. <ElSelect v-model="formData.inv_type" placeholder="发票类型" style="width: 100%">
  211. <ElOption label="普票" value="0" />
  212. <ElOption label="专票" value="1" />
  213. </ElSelect>
  214. </ElFormItem>
  215. </ElCol>
  216. <ElCol :span="6">
  217. <ElFormItem label="成本税前单价" prop=" subunit_price">
  218. <UnitInput unit="元" v-model="formData.subunit_price" placeholder="成本税前单价" />
  219. </ElFormItem>
  220. </ElCol>
  221. <ElCol :span="6">
  222. <ElFormItem label="成本税后单价" prop="unit_price">
  223. <UnitInput unit="元" v-model="formData.unit_price" placeholder="成本税后单价" />
  224. </ElFormItem>
  225. </ElCol>
  226. <ElCol :span="6">
  227. <ElFormItem label="商品代码" prop="good_code">
  228. <ElInput v-model="formData.good_code" placeholder="商品代码" />
  229. </ElFormItem>
  230. </ElCol>
  231. <ElCol :span="12">
  232. <ElFormItem label="进项税目" prop="cat_code">
  233. <QueryCategory v-model="formData.cat_code" />
  234. </ElFormItem>
  235. </ElCol>
  236. <ElCol :span="6">
  237. <ElFormItem label="进项税率" prop="cat_tax">
  238. <ElSelect v-model="formData.cat_tax" placeholder="进项税率">
  239. <ElOption v-for="tax in tax_list" v-bind="tax" />
  240. </ElSelect>
  241. </ElFormItem>
  242. </ElCol>
  243. <ElCol :span="6">
  244. <ElFormItem label="分光" prop="spectral">
  245. <ElInput v-model="formData.spectral" placeholder="分光" style="width: 100%" />
  246. </ElFormItem>
  247. </ElCol>
  248. </ElRow>
  249. <ElRow v-if="formData.is_combind == '1'">
  250. <ElCol :span="24">
  251. <ElFormItem label="子商品">
  252. <ElTable border size="small" :data="formData.childArr">
  253. <ElTableColumn label="比例" prop="child_num" width="60px" />
  254. <ElTableColumn label="商品编号" prop="skuCode" />
  255. <ElTableColumn label="商品类型" prop="good_type">
  256. <template #="{ row }">
  257. <ElTag size="small">
  258. {{ cg_order_type_options.find(item => item.value == row.good_type)?.label }}
  259. </ElTag>
  260. </template>
  261. </ElTableColumn>
  262. <ElTableColumn label="商品名称" prop="goodName" />
  263. <ElTableColumn width="80px">
  264. <template #header>
  265. <div class="w-full flex justify-between">
  266. <p>操作</p>
  267. <ElButton link size="small" type="primary" :icon="useRenderIcon('add')"
  268. @click="state.visible = true" />
  269. </div>
  270. </template>
  271. <template #="{ $index }">
  272. <ElTooltip content="删除" placement="top">
  273. <ElButton link size="small" type="danger" :icon="useRenderIcon('delete')"
  274. @click="formData.childArr.splice($index, 1)" />
  275. </ElTooltip>
  276. <ElTooltip content="编辑" placement="top">
  277. <ElButton link size="small" type="primary" :icon="useRenderIcon('edits')"
  278. @click="handleChildGoodEdit($index)" />
  279. </ElTooltip>
  280. </template>
  281. </ElTableColumn>
  282. </ElTable>
  283. </ElFormItem>
  284. </ElCol>
  285. </ElRow>
  286. <div class="w-full flex justify-end">
  287. <ElButton type="primary" @click="submit">保存</ElButton>
  288. </div>
  289. <ChooseGoodModal v-model:visible="state.visible" @submit="handleSubmit" />
  290. <ChildGoodEditModal v-model:visible="state.editVisible" :index="state.index" :data="state.data"
  291. @submit="(index, data) => formData.childArr[index] = { ...data }" />
  292. </ElForm>
  293. </template>