add.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <template>
  2. <view class="mainBox">
  3. <uni-nav-bar background-color="#157A2C" color="#fff" fixed="true" statusBar="true" left-icon="back"
  4. :title="this.form.id?'修改采购需求':'新建采购需求'" @clickLeft="back">
  5. </uni-nav-bar>
  6. <u-subsection :list="list" :current="current" @change="sectionChange"></u-subsection>
  7. <u-cell-group v-show='current==0'>
  8. <u-cell :title="'需求类型'" arrow-direction="down">
  9. <view slot="title" style="display: flex; align-items: center;">
  10. <text class="required-mark">*</text>
  11. 需求类型
  12. </view>
  13. <uni-data-picker v-model="form.sourceType" slot="value" placeholder="请选择"
  14. :localdata="requirementSourceType" @change="sourceCodeOnchange">
  15. </uni-data-picker>
  16. </u-cell>
  17. <u-cell :title="'需求单名称'" arrow-direction="down">
  18. <view slot="title" style="display: flex; align-items: center;">
  19. <text class="required-mark">*</text>
  20. 需求单名称
  21. </view>
  22. <u--input slot="value" placeholder="请输入" border="surround" v-model="form.requirementName"></u--input>
  23. </u-cell>
  24. <u-cell :title="'需求部门'" arrow-direction="down">
  25. <view slot="title" style="display: flex; align-items: center;">
  26. <text class="required-mark">*</text>
  27. 需求部门
  28. </view>
  29. <!-- <u--input slot="value" placeholder="请输入" border="surround" v-model="form.requireDeptId"></u--input> -->
  30. <u--input slot="value" @click.native="classification" style="flex:1" border="surround"
  31. placeholder="请选择需求部门" v-model="form.requireDeptName">
  32. </u--input>
  33. </u-cell>
  34. <u-cell :title="'需求人'" arrow-direction="down">
  35. <view slot="title" style="display: flex; align-items: center;">
  36. <text class="required-mark">*</text>
  37. 需求人
  38. </view>
  39. <!-- <u--input slot="value" placeholder="请输入" border="requireUserId" v-model="form.name"></u--input> -->
  40. <u--input slot="value" style="flex:1" border="surround" v-model="form.requireUserName"
  41. placeholder="请选择需求人" @click.native="openSelector">
  42. </u--input>
  43. </u-cell>
  44. <u-cell title="销售合同" arrow-direction="down">
  45. <u--input slot="value" style="flex:1" placeholder="请选择销售合同" border="surround" v-model="form.saleContractNo" @click.native="selectContractShow"></u--input>
  46. </u-cell>
  47. <u-cell title="销售订单" arrow-direction="down">
  48. <!-- <u--input slot="value" placeholder="请输入" border="requireUserId" v-model="form.name"></u--input> -->
  49. <u--input slot="value" style="flex:1" border="surround" v-model="form.saleOrderNo"
  50. placeholder="请选择销售订单" @click.native="slectOrderShow">
  51. </u--input>
  52. </u-cell>
  53. <u-cell title="是否接受拆单" arrow-direction="down">
  54. <uni-data-picker v-model="form.acceptUnpack" slot="value" placeholder="请选择"
  55. :localdata="acceptUnpackList" @change="sourceCodeOnchange">
  56. </uni-data-picker>
  57. </u-cell>
  58. <u-cell title="'用途" arrow-direction="down">
  59. <view slot="title" style="display: flex; align-items: center;">
  60. <text class="required-mark">*</text>
  61. 用途
  62. </view>
  63. <u--textarea slot="value" placeholder="请输入" border="surround" v-model="form.useTo"></u--textarea>
  64. </u-cell>
  65. <u-cell title="附件" arrow-direction="down">
  66. <view slot="value" style="display: flex;align-items: center;">
  67. <fileMain v-model="form.fileId"></fileMain>
  68. </view>
  69. </u-cell>
  70. <u-cell title="备注" arrow-direction="down">
  71. <u--textarea slot="value" placeholder="请输入" border="surround" v-model="form.remark"></u--textarea>
  72. </u-cell>
  73. </u-cell-group>
  74. <view class="footerButton">
  75. <u-button type="default" text="返回" @click="back"></u-button>
  76. <u-button type="primary" @click="save" text="保存"></u-button>
  77. </view>
  78. <produceList ref="produceListRef" v-show="current==1" :isTemporary="true"></produceList>
  79. <ba-tree-picker ref="treePicker" key="verify" :multiple="false" @select-change="confirm" title="选择部门"
  80. :selectedData="selectedData" :localdata="classificationList" valueKey="id" textKey="name"
  81. childrenKey="children" />
  82. <search-select ref="selector" v-model="form.requireUserId" :data-list="executorList" title="选择需求人" @change="onClose">
  83. </search-select>
  84. <u-toast ref="uToast"></u-toast>
  85. </view>
  86. </template>
  87. <script>
  88. import { getByCode } from '@/api/pda/common.js'
  89. import searchSelect from '@/pages/salesServiceManagement/accessory/components/searchSelect.vue';
  90. import produceList from '../components/produceList.vue'
  91. import fileMain from "@/pages/doc/index.vue"
  92. import { requirementSourceType } from '@/enum/dict'
  93. import { listOrganizations } from '@/api/salesServiceManagement/workOrder/index.js';
  94. import { toTreeData } from '@/utils/utils.js';
  95. import { getUserPage } from '@/api/common.js';
  96. import { getTableList, getSaleOrderDetail } from '@/api/saleManage/saleorder/index'
  97. import {
  98. getDetail,
  99. addPurchaseNeedManage,
  100. UpdateInformation
  101. } from '@/api/purchasingManage/purchaseNeedManage.js'
  102. import { getInventoryTotal } from '@/api/pda/workOrder'
  103. import { contactQueryByCategoryIdsAPI } from '@/api/warehouseManagement/index'
  104. export default {
  105. components: {
  106. produceList,
  107. fileMain,
  108. searchSelect
  109. },
  110. data() {
  111. return {
  112. list: ['基本信息', '需求清单'],
  113. acceptUnpackList: [{text: '接受', value: 1}, { text: '不接受', value: 0}],
  114. current: 0,
  115. show: false,
  116. requirementSourceType,
  117. classificationList: [],
  118. isUpdate: false,
  119. purchase_origin: [],
  120. form: {
  121. id: '',
  122. receiveDate: null,
  123. remark: null,
  124. requireDeptId: '',
  125. requireDeptName: '',
  126. requireUserId: '',
  127. requireUserName: '',
  128. sourceCode: '',
  129. sourceId: '',
  130. sourceType: '',
  131. requirementName: '',
  132. saleContractName: '',
  133. saleContractNo: '',
  134. saleContractId: '',
  135. saleOrderId: '',
  136. saleOrderNo: '',
  137. files: [], //附件集合
  138. acceptUnpack: 1,
  139. fileId: [],
  140. },
  141. business_opport_code: [],
  142. linkList: [],
  143. selectedData: [], // 使用部门绑定值
  144. executorList: []
  145. }
  146. },
  147. created() {
  148. this.getByCode()
  149. //选择合同回调
  150. uni.$off('setContractList')
  151. uni.$on('setContractList', async (data) => {
  152. if (data && data.length > 0) {
  153. this.form = Object.assign({}, this.form, {
  154. saleContractId: data[0].id,
  155. saleContractName: data[0].contractName,
  156. saleContractNo: data[0].contractNo,
  157. });
  158. console.log('ddd~~~', data[0])
  159. this.getSaleOrderList(data[0].id);
  160. }
  161. })
  162. uni.$off('setSaleOrder')
  163. uni.$on('setSaleOrder', async (data) => {
  164. if(data && data.length > 0) {
  165. this.form = Object.assign({}, this.form, {
  166. saleContractId: '',
  167. saleContractName: '',
  168. saleContractNo: '',
  169. saleOrderId: data[0].id,
  170. saleOrderNo: data[0].orderNo
  171. });
  172. this.getSaleOrderDetail(this.form.saleOrderId)
  173. }
  174. })
  175. },
  176. onLoad(data) {
  177. this.getTreeList()
  178. if (data && data.id) {
  179. this.isUpdate = true
  180. getDetail(data.id).then(async res => {
  181. this.form = res
  182. this.form.fileId = JSON.parse(res.fileId)
  183. if(this.form.requireDeptId) {
  184. this.getUserList(this.form.requireDeptId);
  185. }
  186. // setTimeout(() => {
  187. // this.current = 0
  188. this.$nextTick(async () => {
  189. // this.$refs.infoRef.init(res)
  190. // 获取所有有productCode的产品ID和编码
  191. let validItems = this.form.detailList.filter(item => item.productCode);
  192. let productIds = validItems.map(item => item.productId);
  193. let productCodes = validItems.map(item => item.productCode);
  194. // 批量获取库存和供应商信息
  195. let inventoryTotalList = [];
  196. let supplierObj = {};
  197. if (productIds.length > 0) {
  198. inventoryTotalList = await getInventoryTotal(productCodes);
  199. supplierObj = await contactQueryByCategoryIdsAPI({
  200. categoryIds: productIds
  201. });
  202. // 更新每个产品的供应商和库存信息
  203. validItems.forEach((item, index) => {
  204. // 找到对应的索引位置
  205. let actualIndex = this.form.detailList.findIndex(i => i.productCode === item.productCode);
  206. if (actualIndex !== -1) {
  207. this.$set(
  208. this.form.detailList[actualIndex],
  209. 'supplierList',
  210. supplierObj[item.productId]?.map(i => ({
  211. ...i,
  212. text: i.name,
  213. value: i.id
  214. })) || []
  215. );
  216. let find = inventoryTotalList.find(key => key.code == item.productCode) || {};
  217. this.$set(
  218. this.form.detailList[actualIndex],
  219. 'availableCountBase',
  220. find.availableCountBase
  221. );
  222. }
  223. });
  224. }
  225. // 处理数据格式
  226. const tempData = this.form.detailList.map(item => ({
  227. ...item,
  228. provenanceName: item.provenance ? this.getProvenance(item.provenance) : '',
  229. // modelKey: item.modelKey?.split(',') || [],
  230. // colorKey: item.colorKey?.split(',') || [],
  231. packageDispositionList: item.packageDispositionList?.map(i => ({
  232. ...i,
  233. text: i.conversionUnit,
  234. value: i.id
  235. })) || []
  236. }));
  237. console.log('tempData~~~', tempData)
  238. this.$refs.produceListRef.init(tempData)
  239. // if (data.current) {
  240. // this.current = +data.current
  241. // }
  242. })
  243. // }, 300)
  244. })
  245. } else {
  246. const userInfo = uni.getStorageSync('userInfo')
  247. this.$set(this.form, 'requireUserId', userInfo.userId);
  248. this.$set(this.form, 'requireUserName', userInfo.name);
  249. this.$set(this.form, 'requireDeptId', userInfo.groupId);
  250. this.$set(this.form, 'requireDeptName', userInfo.groupName);
  251. }
  252. },
  253. onUnload() {
  254. uni.$off('setContractList')
  255. uni.$off('setSaleOrder')
  256. },
  257. methods: {
  258. getProvenance(item) {
  259. console.log('getProvenance~~~~~', item)
  260. // try {
  261. // 检查item和provenance是否存在
  262. if (!item) {
  263. return '';
  264. }
  265. let arr = item;
  266. if(!Array.isArray(arr)) {
  267. console.log('arr is not array, convert to array')
  268. arr = arr.split(',');
  269. }
  270. return arr && arr.length ? arr.map((i) => {
  271. return this.purchase_origin.find(p => p.value == i)?.text
  272. }).join(',') : '';
  273. },
  274. async getSaleOrderList(contractId) {
  275. let res = await getTableList({
  276. pageNum: 1,
  277. size: 10,
  278. contractId
  279. });
  280. this.form.saleOrderId = res.list[0]?.id;
  281. this.form.saleOrderNo = res.list[0]?.orderNo;
  282. this.getSaleOrderDetail(this.form.saleOrderId);
  283. },
  284. async getSaleOrderDetail(id) {
  285. const data = await getSaleOrderDetail(id);
  286. data.productList.forEach((item) => {
  287. item.expectReceiveDate = item.produceDeliveryDeadline;
  288. item.arrivalWay = 1;
  289. item['packageDispositionList'] = item.packageDispositionList?.map(i => {
  290. return {
  291. ...i,
  292. text: i.conversionUnit,
  293. value: i.id
  294. }
  295. }) || []
  296. });
  297. this.$nextTick(() => {
  298. this.$refs.produceListRef.init(data.productList);
  299. });
  300. },
  301. async getTreeList() {
  302. const data = await listOrganizations({});
  303. let treeList = toTreeData({
  304. data: data || [],
  305. idField: 'id',
  306. parentIdField: 'parentId'
  307. });
  308. this.classificationList = treeList;
  309. },
  310. // 打开部门弹窗
  311. classification() {
  312. this.$refs.treePicker._show();
  313. },
  314. // 使用部门选择
  315. confirm(id, name) {
  316. this.form.requireDeptName = name;
  317. this.form.requireDeptId = id[0];
  318. this.form.requireUserId = '';
  319. this.form.requireUserName = '';
  320. this.executorList = [];
  321. this.getUserList(id[0]);
  322. this.$refs.selector.clearSearchText();
  323. },
  324. // 打开需求人弹窗
  325. openSelector() {
  326. this.$refs.selector.open();
  327. },
  328. // 获取需求人数据
  329. async getUserList(id) {
  330. let params = {
  331. pageNum: 1,
  332. size: -1,
  333. groupId: id
  334. }
  335. try {
  336. const res = await getUserPage(params);
  337. let list = res.list && res.list.map((el) => {
  338. return {
  339. text: el.name,
  340. value: el.id
  341. }
  342. })
  343. this.executorList = list;
  344. } catch (error) {
  345. this.executorList = [];
  346. }
  347. },
  348. // 需求人选择
  349. onClose(item) {
  350. this.form.requireUserName = item.text;
  351. },
  352. //选择合同
  353. selectContractShow() {
  354. console.log('ddd~~~')
  355. uni.navigateTo({
  356. url: '/pages/purchasingManage/components/selectContract?isAll=' + 2
  357. })
  358. },
  359. slectOrderShow() {
  360. uni.navigateTo({
  361. url: '/pages/purchasingManage/components/selectSaleOrder?isAll=' + 2
  362. })
  363. },
  364. getByCode() {
  365. const codeS = ['business_opport_code', 'purchase_origin']
  366. codeS.forEach(async (code) => {
  367. const codeValue = await getByCode(code);
  368. this[code] = codeValue.map(item => {
  369. const key = Object.keys(item)[0]
  370. return {
  371. value: key,
  372. text: item[key]
  373. }
  374. })
  375. })
  376. },
  377. sourceCodeOnchange(e) {
  378. const value = e.detail.value
  379. this.form.sourceCode = value.map(item => item.value).toString()
  380. this.form.sourceName = value.map(item => item.text).toString()
  381. },
  382. radioChange(e) {
  383. this.form.pricingWay = e.detail.value
  384. this.$nextTick(() => {
  385. this.$refs.produceListRef.getTotalPrice()
  386. })
  387. },
  388. sectionChange(index) {
  389. this.current = index;
  390. },
  391. async save() {
  392. try {
  393. if (!this.form.sourceType) {
  394. this.$refs.uToast.show({
  395. type: "error",
  396. message: "请选择需求类型",
  397. })
  398. return
  399. }
  400. if (!this.form.requirementName) {
  401. this.$refs.uToast.show({
  402. type: "error",
  403. message: "请输入需求名称",
  404. })
  405. return
  406. }
  407. if (!this.form.requireDeptName) {
  408. this.$refs.uToast.show({
  409. type: "error",
  410. message: "请选择需求部门",
  411. })
  412. return
  413. }
  414. if (!this.form.requireUserName) {
  415. this.$refs.uToast.show({
  416. type: "error",
  417. message: "请选择需求人",
  418. })
  419. return
  420. }
  421. const detailList = this.$refs.produceListRef.getValue();
  422. if (detailList.length == 0) {
  423. this.$refs.uToast.show({
  424. type: "error",
  425. message: "产品清单不能为空",
  426. })
  427. return;
  428. }
  429. let isArrivalBatch = false;
  430. let isError = false;
  431. detailList.forEach((v) => {
  432. if (v.arrivalWay == 2 && (!v.arrivalBatch || v.arrivalBatch.length == 0) ) {
  433. isArrivalBatch = true;
  434. }
  435. if (!v.purchaseCount || !v.productName || !v.arrivalWay || (!v.expectReceiveDate && v.arrivalWay == 1)) {
  436. isError = true;
  437. }
  438. });
  439. if (isError) {
  440. uni.showToast({icon: none, title: '请完善产品信息'});
  441. return;
  442. }
  443. if (isArrivalBatch) {
  444. uni.showToast({icon: none, title: '请设置分批时间'});
  445. return;
  446. }
  447. uni.showLoading({
  448. title: '加载中'
  449. })
  450. const data = {
  451. ...this.form,
  452. detailList: detailList
  453. };
  454. data.fileId = JSON.stringify(data.fileId)
  455. data.detailList.forEach((item) => {
  456. if(item.provenance && typeof item.provenance == 'string') {
  457. item.provenance = item.provenance.split(',')
  458. }
  459. })
  460. console.log('data~~~', data)
  461. const requestApi = this.isUpdate ? UpdateInformation : addPurchaseNeedManage
  462. requestApi(data)
  463. .then((res) => {
  464. uni.hideLoading()
  465. this.back()
  466. })
  467. .catch((e) => {
  468. uni.hideLoading()
  469. });
  470. } catch (error) {
  471. uni.hideLoading()
  472. console.log(error, 'error')
  473. }
  474. },
  475. }
  476. }
  477. </script>
  478. <style lang="scss" scoped>
  479. .required-mark {
  480. color: #FF3333;
  481. margin-right: 4rpx;
  482. }
  483. /deep/.u-cell__body__content {
  484. flex: none;
  485. margin-right: 16rpx;
  486. }
  487. .mainBox {
  488. padding-bottom: 84rpx;
  489. }
  490. .footerButton {
  491. width: 100%;
  492. height: 84rpx;
  493. display: flex;
  494. position: fixed;
  495. bottom: 0;
  496. z-index: 10;
  497. background-color: #fff;
  498. /deep/.u-button {
  499. height: 100%;
  500. }
  501. >view {
  502. flex: 1;
  503. }
  504. }
  505. // /deep/.u-button {
  506. // // height: 100%;
  507. // }
  508. /deep/.u-subsection__item__text {
  509. font-size: 28rpx !important;
  510. }
  511. /deep/.uni-input-placeholder {
  512. font-size: 28rpx !important;
  513. }
  514. /deep/.selected-item {
  515. font-size: 28rpx !important;
  516. }
  517. /deep/.uni-input-input {
  518. font-size: 28rpx !important;
  519. }
  520. </style>