createByApply.uvue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <template>
  2. <uni-navbar-lite :showRight=false title="创建采购单"></uni-navbar-lite>
  3. <view class="page-container">
  4. <scroll-view class="page-content" scroll-y="true">
  5. <view v-if="applyList.length === 0" class="empty-tip">
  6. <text class="empty-tip-text">暂无申请单数据</text>
  7. </view>
  8. <view v-else>
  9. <view class="purchase-info-section">
  10. <view class="purchase-info-row">
  11. <text class="purchase-info-label">采购单名称</text>
  12. <view class="purchase-info-value-wrap">
  13. <input
  14. class="purchase-info-input"
  15. v-model="purchaseName"
  16. placeholder="请输入采购单名称"
  17. />
  18. </view>
  19. </view>
  20. <view class="purchase-remark-section">
  21. <text class="purchase-remark-label">采购单备注</text>
  22. <textarea
  23. class="purchase-remark-input"
  24. placeholder="请输入采购单备注"
  25. v-model="purchaseRemark"
  26. :maxlength="500"
  27. ></textarea>
  28. </view>
  29. </view>
  30. <view v-for="(apply, applyIndex) in applyList" :key="applyIndex" class="section">
  31. <view class="section-header">
  32. <view class="section-indicator"></view>
  33. <text class="section-title">申请单 {{ apply.applyCode }}</text>
  34. </view>
  35. <view class="info-card">
  36. <view class="info-row">
  37. <text class="info-label">申请人</text>
  38. <text class="info-value">{{ apply.nickName }}</text>
  39. </view>
  40. <view class="info-row">
  41. <text class="info-label">申请时间</text>
  42. <text class="info-value">{{ apply.createTime }}</text>
  43. </view>
  44. <view class="info-row">
  45. <text class="info-label">用途</text>
  46. <text class="info-value">{{ apply.remark }}</text>
  47. </view>
  48. </view>
  49. <view class="material-section">
  50. <view class="material-section-header">
  51. <text class="material-section-title">物料明细</text>
  52. </view>
  53. <view class="material-list">
  54. <view
  55. v-for="(item, itemIndex) in apply.lineList"
  56. :key="itemIndex"
  57. class="material-item"
  58. >
  59. <view class="material-info">
  60. <text class="material-name">{{ getItemName(item) }}</text>
  61. <text class="material-spec" v-if="getSpecification(item)">规格:{{ getSpecification(item) }}</text>
  62. </view>
  63. <view class="material-detail">
  64. <view class="detail-row">
  65. <text class="detail-label">申请数量</text>
  66. <text class="detail-value">{{ getQuantity(item) }} {{ getMeasureName(item) }}</text>
  67. </view>
  68. <view class="detail-row purchase-row">
  69. <text class="detail-label">采购数量</text>
  70. <view class="purchase-input-wrap">
  71. <input
  72. class="purchase-input"
  73. type="number"
  74. v-model="item.purchaseQty"
  75. @input="handlePurchaseQtyChange"
  76. />
  77. <text class="purchase-unit">{{ getMeasureName(item) }}</text>
  78. </view>
  79. </view>
  80. </view>
  81. </view>
  82. </view>
  83. </view>
  84. </view>
  85. </view>
  86. <view class="bottom-space"></view>
  87. </scroll-view>
  88. <view class="bottom-buttons">
  89. <button class="submit-btn" @click="handleSubmit">生成采购单</button>
  90. </view>
  91. </view>
  92. </template>
  93. <script setup lang="uts">
  94. import { ref } from 'vue'
  95. import { getPurchaseApplyById } from '../../api/apply/index'
  96. import { createMergePurchase } from '../../api/purchase/index'
  97. import { getUserInfo } from '../../utils/storage'
  98. const applyIds = ref<string>("")
  99. const idList = ref<string[]>([])
  100. const applyList = ref<UTSJSONObject[]>([])
  101. const loading = ref<boolean>(false)
  102. const purchaseName = ref<string>("")
  103. const purchaseRemark = ref<string>("")
  104. const currentUserId = ref<string>("")
  105. const getItemName = (item: UTSJSONObject): string => {
  106. if (item == null) return ''
  107. const val = item['itemName']
  108. return val != null ? val.toString() : ''
  109. }
  110. const getSpecification = (item: UTSJSONObject): string => {
  111. if (item == null) return ''
  112. const val = item['specification']
  113. return val != null ? val.toString() : ''
  114. }
  115. const getQuantity = (item: UTSJSONObject): string => {
  116. if (item == null) return '0'
  117. const val = item['quantityApply']
  118. return val != null ? val.toString() : '0'
  119. }
  120. const getMeasureName = (item: UTSJSONObject): string => {
  121. if (item == null) return ''
  122. const val = item['measureName']
  123. return val != null ? val.toString() : ''
  124. }
  125. const handlePurchaseQtyChange = (): void => {
  126. }
  127. const loadApplyDetails = (): void => {
  128. if (idList.value.length === 0) return
  129. loading.value = true
  130. const promises = idList.value.map((id) => {
  131. return getPurchaseApplyById(id)
  132. })
  133. Promise.all(promises).then((responses: any[]) => {
  134. const resultList: UTSJSONObject[] = []
  135. responses.forEach((response: any) => {
  136. const res = response as UTSJSONObject
  137. const data = res["data"] as UTSJSONObject
  138. const apply = new UTSJSONObject()
  139. apply['applyId'] = data['applyId'] != null ? parseInt(data['applyId'].toString()) : 0
  140. apply['applyCode'] = data['applyCode'] != null ? data['applyCode'].toString() : ''
  141. apply['nickName'] = data['nickName'] != null ? data['nickName'].toString() : ''
  142. apply['createTime'] = data['createTime'] != null ? data['createTime'].toString() : ''
  143. apply['remark'] = data['remark'] != null ? data['remark'].toString() : ''
  144. apply['applyUser'] = data['applyUser'] != null ? data['applyUser'].toString() : ''
  145. apply['applyUserId'] = data['applyUserId'] != null ? parseInt(data['applyUserId'].toString()) : 0
  146. apply['applyDeptName'] = data['applyDeptName'] != null ? data['applyDeptName'].toString() : ''
  147. const lines = data['wmPurchaseApplyLineList']
  148. const lineList: UTSJSONObject[] = []
  149. if (lines != null) {
  150. (lines as UTSJSONObject[]).forEach((line: UTSJSONObject) => {
  151. const lineItem = new UTSJSONObject()
  152. lineItem['lineId'] = line['id'] != null ? line['id'].toString() : ''
  153. lineItem['itemId'] = line['itemId'] != null ? line['itemId'].toString() : ''
  154. lineItem['itemCode'] = line['itemCode'] != null ? line['itemCode'].toString() : ''
  155. lineItem['itemName'] = line['itemName'] != null ? line['itemName'].toString() : ''
  156. lineItem['specification'] = line['specification'] != null ? line['specification'].toString() : ''
  157. lineItem['unitOfMeasure'] = line['unitOfMeasure'] != null ? line['unitOfMeasure'].toString() : ''
  158. lineItem['measureName'] = line['measureName'] != null ? line['measureName'].toString() : ''
  159. lineItem['quantityApply'] = line['quantityApply'] != null ? line['quantityApply'] : 0
  160. lineItem['purchaseQty'] = line['quantityApply'] != null ? line['quantityApply'].toString() : '0'
  161. lineList.push(lineItem)
  162. })
  163. }
  164. apply['lineList'] = lineList
  165. resultList.push(apply)
  166. })
  167. applyList.value = resultList
  168. loading.value = false
  169. generatePurchaseName()
  170. }).catch((e) => {
  171. console.error('加载申请单详情失败:', e)
  172. loading.value = false
  173. uni.showToast({ title: '加载失败', icon: 'none' })
  174. })
  175. }
  176. const generatePurchaseName = (): void => {
  177. if (applyList.value.length === 0) {
  178. purchaseName.value = ''
  179. return
  180. }
  181. const now = new Date()
  182. const year = now.getFullYear()
  183. const month = String(now.getMonth() + 1).padStart(2, '0')
  184. const day = String(now.getDate()).padStart(2, '0')
  185. const hours = String(now.getHours()).padStart(2, '0')
  186. const minutes = String(now.getMinutes()).padStart(2, '0')
  187. const seconds = String(now.getSeconds()).padStart(2, '0')
  188. const dateStr = `${year}${month}${day}${hours}${minutes}${seconds}`
  189. const applicants = new Set<string>()
  190. applyList.value.forEach((apply) => {
  191. const name = apply['nickName']
  192. if (name != null && name.toString().length > 0) {
  193. applicants.add(name.toString())
  194. }
  195. })
  196. const applicantStr = Array.from(applicants).join(',')
  197. purchaseName.value = `${dateStr}-${applicantStr}`
  198. }
  199. const handleSubmit = (): void => {
  200. const purchaseData = new UTSJSONObject()
  201. const mergedItems: UTSJSONObject[] = []
  202. let totalQuantity = 0
  203. applyList.value.forEach((apply) => {
  204. const applyId = apply['applyId']
  205. const applyRemark = apply['remark'] != null ? apply['remark'].toString() : ''
  206. const applyUser = apply['applyUser'] != null ? apply['applyUser'].toString() : ''
  207. const applyUserId = apply['applyUserId']
  208. const applyDeptName = apply['applyDeptName'] != null ? apply['applyDeptName'].toString() : ''
  209. const lineList = apply['lineList'] as UTSJSONObject[]
  210. lineList.forEach((item) => {
  211. const qty = parseInt(item['purchaseQty'])
  212. if (qty > 0 && qty <= parseInt(getQuantity(item))) {
  213. const lineItem = new UTSJSONObject()
  214. lineItem['applyId'] = applyId
  215. lineItem['applyLineId'] = parseInt(item['lineId'])
  216. lineItem['itemId'] = parseInt(item['itemId'])
  217. lineItem['itemCode'] = item['itemCode'] != null ? item['itemCode'].toString() : ''
  218. lineItem['itemName'] = item['itemName'] != null ? item['itemName'].toString() : ''
  219. lineItem['specification'] = item['specification'] != null ? item['specification'].toString() : ''
  220. lineItem['unitOfMeasure'] = item['unitOfMeasure'] != null ? item['unitOfMeasure'].toString() : ''
  221. lineItem['measureName'] = item['measureName'] != null ? item['measureName'].toString() : ''
  222. lineItem['quantityApply'] = parseInt(getQuantity(item))
  223. lineItem['quantityPurchase'] = qty
  224. lineItem['applyRemark'] = applyRemark
  225. lineItem['applyUser'] = applyUser
  226. lineItem['applyUserId'] = applyUserId
  227. lineItem['applyDeptName'] = applyDeptName
  228. mergedItems.push(lineItem)
  229. totalQuantity += qty
  230. }
  231. })
  232. })
  233. if (mergedItems.length === 0) {
  234. uni.showToast({ title: '请填写有效的采购数量', icon: 'none' })
  235. return
  236. }
  237. const mergePurchase = new UTSJSONObject()
  238. mergePurchase['mergeName'] = purchaseName.value
  239. mergePurchase['remark'] = purchaseRemark.value
  240. mergePurchase['totalQuantity'] = totalQuantity
  241. purchaseData['mergePurchase'] = mergePurchase
  242. purchaseData['applyUserId'] = currentUserId.value.length > 0 ? parseInt(currentUserId.value) : 0
  243. purchaseData['applyIds'] = idList.value.map(id => parseInt(id))
  244. purchaseData['mergedItems'] = mergedItems
  245. purchaseData['platform'] = 'mobile'
  246. uni.showModal({
  247. title: '提示',
  248. content: '确定生成采购单吗?',
  249. success: (res) => {
  250. if (res.confirm) {
  251. uni.showLoading({ title: '生成中...' })
  252. createMergePurchase(purchaseData).then((response: any) => {
  253. uni.hideLoading()
  254. const result = response as UTSJSONObject
  255. if (result['code'] === 200) {
  256. uni.showToast({ title: '生成成功', icon: 'success' })
  257. setTimeout(() => {
  258. uni.redirectTo({
  259. url: '/pages/apply/index?refresh=1'
  260. })
  261. }, 1500)
  262. } else {
  263. const msg = result['msg'] != null ? result['msg'].toString() : '生成失败'
  264. uni.showToast({ title: msg, icon: 'none' })
  265. }
  266. }).catch((e) => {
  267. uni.hideLoading()
  268. const error = e as UTSError
  269. const errMsg = error?.message
  270. uni.showToast({ title: errMsg != null ? errMsg.toString() : '生成失败', icon: 'none' })
  271. })
  272. }
  273. }
  274. })
  275. }
  276. onLoad((options: any) => {
  277. const userInfo = getUserInfo()
  278. if (userInfo != null) {
  279. const userId = userInfo['userId']
  280. currentUserId.value = userId != null ? userId.toString() : ''
  281. }
  282. const params = options as UTSJSONObject
  283. if (params != null && params['applyIds'] != null) {
  284. applyIds.value = params['applyIds'].toString()
  285. idList.value = applyIds.value.split(',').filter((id: string) => id.trim().length > 0)
  286. loadApplyDetails()
  287. }
  288. })
  289. </script>
  290. <style lang="scss">
  291. .page-container {
  292. flex: 1;
  293. background-color: #e8f0f9;
  294. }
  295. .page-content {
  296. flex: 1;
  297. padding: 20rpx;
  298. padding-bottom: 140rpx;
  299. }
  300. .purchase-info-section {
  301. margin-bottom: 20rpx;
  302. background: #ffffff;
  303. border-radius: 16rpx;
  304. padding: 20rpx;
  305. }
  306. .purchase-info-row {
  307. flex-direction: row;
  308. justify-content: space-between;
  309. align-items: center;
  310. margin-bottom: 20rpx;
  311. }
  312. .purchase-info-label {
  313. font-size: 28rpx;
  314. color: #666666;
  315. }
  316. .purchase-info-value-wrap {
  317. flex: 1;
  318. margin-left: 20rpx;
  319. background-color: #f8f9fa;
  320. border-radius: 8rpx;
  321. padding: 12rpx 16rpx;
  322. }
  323. .purchase-info-value {
  324. font-size: 28rpx;
  325. color: #333333;
  326. font-weight: bold;
  327. }
  328. .purchase-info-input {
  329. font-size: 28rpx;
  330. color: #333333;
  331. width: 100%;
  332. background-color: transparent;
  333. }
  334. .purchase-remark-section {
  335. margin-top: 16rpx;
  336. }
  337. .purchase-remark-label {
  338. font-size: 28rpx;
  339. color: #666666;
  340. margin-bottom: 12rpx;
  341. display: block;
  342. }
  343. .purchase-remark-input {
  344. width: 100%;
  345. min-height: 160rpx;
  346. background-color: #f8f9fa;
  347. border-radius: 8rpx;
  348. padding: 16rpx;
  349. font-size: 28rpx;
  350. color: #333333;
  351. box-sizing: border-box;
  352. }
  353. .section {
  354. margin-bottom: 20rpx;
  355. background: #ffffff;
  356. border-radius: 16rpx;
  357. padding: 20rpx;
  358. }
  359. .section-header {
  360. flex-direction: row;
  361. align-items: center;
  362. margin-bottom: 20rpx;
  363. }
  364. .section-indicator {
  365. width: 6rpx;
  366. height: 32rpx;
  367. background-color: #007aff;
  368. border-radius: 3rpx;
  369. margin-right: 12rpx;
  370. }
  371. .section-title {
  372. font-size: 32rpx;
  373. color: #333333;
  374. font-weight: bold;
  375. }
  376. .info-card {
  377. background-color: #f8f9fa;
  378. border-radius: 8rpx;
  379. padding: 20rpx;
  380. margin-bottom: 20rpx;
  381. }
  382. .info-row {
  383. flex-direction: row;
  384. justify-content: space-between;
  385. margin-bottom: 16rpx;
  386. &:last-child {
  387. margin-bottom: 0;
  388. }
  389. }
  390. .info-label {
  391. font-size: 28rpx;
  392. color: #666666;
  393. }
  394. .info-value {
  395. font-size: 28rpx;
  396. color: #333333;
  397. }
  398. .material-section {
  399. background-color: #f8f9fa;
  400. border-radius: 8rpx;
  401. padding: 20rpx;
  402. }
  403. .material-section-header {
  404. margin-bottom: 16rpx;
  405. }
  406. .material-section-title {
  407. font-size: 28rpx;
  408. color: #333333;
  409. font-weight: bold;
  410. }
  411. .material-list {
  412. background-color: #ffffff;
  413. border-radius: 8rpx;
  414. padding: 16rpx;
  415. }
  416. .material-item {
  417. padding: 16rpx 0;
  418. border-bottom: 1rpx solid #f0f0f0;
  419. &:last-child {
  420. border-bottom: none;
  421. }
  422. }
  423. .material-info {
  424. margin-bottom: 12rpx;
  425. }
  426. .material-name {
  427. font-size: 28rpx;
  428. color: #333333;
  429. font-weight: bold;
  430. display: block;
  431. }
  432. .material-spec {
  433. font-size: 24rpx;
  434. color: #999999;
  435. margin-top: 4rpx;
  436. display: block;
  437. }
  438. .material-detail {
  439. flex-direction: row;
  440. flex-wrap: wrap;
  441. }
  442. .detail-row {
  443. flex-direction: row;
  444. width: 50%;
  445. align-items: center;
  446. margin-bottom: 8rpx;
  447. }
  448. .detail-label {
  449. font-size: 26rpx;
  450. color: #666666;
  451. margin-right: 8rpx;
  452. }
  453. .detail-value {
  454. font-size: 26rpx;
  455. color: #ff0000;
  456. }
  457. .purchase-row {
  458. justify-content: flex-end;
  459. }
  460. .purchase-input-wrap {
  461. flex-direction: row;
  462. align-items: center;
  463. background-color: #f5f5f5;
  464. border-radius: 8rpx;
  465. padding: 8rpx 12rpx;
  466. }
  467. .purchase-input {
  468. width: 120rpx;
  469. height: 48rpx;
  470. font-size: 26rpx;
  471. color: #ff0000;
  472. text-align: right;
  473. background: transparent;
  474. }
  475. .purchase-unit {
  476. font-size: 24rpx;
  477. color: #666666;
  478. margin-left: 8rpx;
  479. }
  480. .empty-tip {
  481. align-items: center;
  482. padding: 100rpx 20rpx;
  483. }
  484. .empty-tip-text {
  485. color: #999999;
  486. font-size: 28rpx;
  487. }
  488. .bottom-space {
  489. height: 40rpx;
  490. }
  491. .bottom-buttons {
  492. position: fixed;
  493. bottom: 0;
  494. left: 0;
  495. right: 0;
  496. padding: 20rpx 30rpx;
  497. background-color: #ffffff;
  498. border-top: 1rpx solid #e5e5e5;
  499. }
  500. .submit-btn {
  501. width: 100%;
  502. height: 88rpx;
  503. background-color: #007aff;
  504. color: #ffffff;
  505. font-size: 32rpx;
  506. font-weight: 600;
  507. border-radius: 22rpx;
  508. border: none;
  509. }
  510. </style>