unpackDialog.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <template>
  2. <ele-modal
  3. width="60vw"
  4. :visible.sync="visible"
  5. :close-on-click-modal="false"
  6. custom-class="ele-dialog-form"
  7. :maxable="true"
  8. :title="'拆分工单'"
  9. >
  10. <div class="form-wrapper">
  11. <el-form :model="requestData" label-width="0" :show-message="false">
  12. <el-descriptions title="" :column="2" border>
  13. <el-descriptions-item label="生产订单号">
  14. {{ formData.code }}</el-descriptions-item
  15. >
  16. <el-descriptions-item label="产品编码">
  17. {{ formData.productCode }}</el-descriptions-item
  18. >
  19. <el-descriptions-item label="产品名称">{{
  20. formData.productName
  21. }}</el-descriptions-item>
  22. <el-descriptions-item label="牌号|型号">
  23. {{ formData.brandNo }}|{{ formData.model }}</el-descriptions-item
  24. >
  25. <el-descriptions-item label="要求生产数量">{{
  26. formData.formingNum
  27. }}</el-descriptions-item>
  28. <el-descriptions-item label="批次号">{{
  29. formData.batchNo
  30. }}</el-descriptions-item>
  31. <el-descriptions-item label="计划开始时间">{{
  32. formData.planStartTime
  33. }}</el-descriptions-item>
  34. <el-descriptions-item label="要求完成日期">{{
  35. formData.planCompleteTime
  36. }}</el-descriptions-item>
  37. </el-descriptions>
  38. </el-form>
  39. <headerTitle title="剩余拆单" class="mt20"> </headerTitle>
  40. <el-form ref="form" :model="form" :rules="rules">
  41. <ele-pro-table
  42. ref="table"
  43. :needPage="false"
  44. :columns="columns"
  45. :datasource="form.surplusUnpack"
  46. >
  47. <template v-slot:toolbar>
  48. <el-button
  49. size="small"
  50. type="primary"
  51. icon="el-icon-plus"
  52. class="ele-btn-icon"
  53. :disabled="formingNum == 0 ? true : false"
  54. @click="openUnpack"
  55. >
  56. 拆单
  57. </el-button>
  58. </template>
  59. <template v-slot:formingNum="scope">
  60. <el-form-item
  61. v-if="formingNum != 0"
  62. :prop="'surplusUnpack.' + scope.$index + '.formingNum'"
  63. :rules="[
  64. {
  65. required: true,
  66. message: '请输入要求生产数量',
  67. trigger: 'change'
  68. },
  69. {
  70. pattern: /^\d+(\.{0,1}\d+){0,1}$/,
  71. message: '拆单数超过生产数量',
  72. trigger: ['blur', 'change']
  73. }
  74. ]"
  75. >
  76. <el-input
  77. disabled
  78. v-model="formingNum"
  79. placeholder="请输入"
  80. ></el-input>
  81. </el-form-item>
  82. <el-form-item v-else> 工单已全部拆完 </el-form-item>
  83. </template>
  84. <template v-slot:planStartTime="scope">
  85. <el-form-item
  86. v-if="formingNum != 0"
  87. :prop="'surplusUnpack.' + scope.$index + '.planStartTime'"
  88. :rules="{
  89. required: true,
  90. message: '请选择开始计划时间',
  91. trigger: 'change'
  92. }"
  93. >
  94. <el-date-picker
  95. class="w100"
  96. disabled
  97. v-model="scope.row.planStartTime"
  98. type="date"
  99. value-format="yyyy-MM-dd"
  100. ></el-date-picker>
  101. </el-form-item>
  102. </template>
  103. <template v-slot:planCompleteTime="scope">
  104. <el-form-item
  105. v-if="formingNum != 0"
  106. :rules="{
  107. required: true,
  108. message: '请选择计划结束时间',
  109. trigger: 'change'
  110. }"
  111. >
  112. <el-date-picker
  113. class="w100"
  114. disabled
  115. v-model="scope.row.planCompleteTime"
  116. type="date"
  117. value-format="yyyy-MM-dd"
  118. ></el-date-picker>
  119. </el-form-item>
  120. </template>
  121. </ele-pro-table>
  122. <headerTitle title="拆单" class="mt20"> </headerTitle>
  123. <ele-pro-table
  124. ref="table"
  125. :needPage="false"
  126. :columns="columns2"
  127. :datasource="form.unpackList"
  128. >
  129. <template v-slot:formingNum="scope">
  130. <el-form-item
  131. :prop="'unpackList.' + scope.$index + '.formingNum'"
  132. :rules="{
  133. required: true,
  134. message: '请输入要求生产数量',
  135. trigger: 'change'
  136. }"
  137. >
  138. <!-- <el-input-number type="number" @change="changeNum(scope.$index)" :min="0" v-model="scope.row.formingNum"
  139. placeholder="请输入"></el-input-number> -->
  140. <el-input
  141. v-model="scope.row.formingNum"
  142. type="number"
  143. @input="changeNum(scope.$index)"
  144. :min="0"
  145. placeholder="请输入内容"
  146. ></el-input>
  147. </el-form-item>
  148. </template>
  149. <template v-slot:planStartTime="scope">
  150. <el-form-item
  151. :prop="'unpackList.' + scope.$index + '.planStartTime'"
  152. :rules="{
  153. required: true,
  154. message: '请选择开始计划时间',
  155. trigger: 'change'
  156. }"
  157. >
  158. <el-date-picker
  159. class="w100"
  160. v-model="scope.row.planStartTime"
  161. type="datetime"
  162. @change="handleStartTimeChange(scope.row)"
  163. value-format="yyyy-MM-dd HH:mm:ss"
  164. :picker-options="{
  165. disabledDate: (time) => {
  166. const end = scope.row.planCompleteTime;
  167. if (!end) return false; // 无结束时间,不禁用
  168. // 将结束时间和当前时间都转换为“当天0点”,仅比较年月日
  169. const endDay = new Date(end);
  170. endDay.setHours(0, 0, 0, 0);
  171. const currentDay = new Date(time);
  172. currentDay.setHours(0, 0, 0, 0);
  173. return currentDay > endDay; // 仅当天0点更晚时禁用
  174. }
  175. }"
  176. ></el-date-picker>
  177. <!-- :picker-options="pickerOptions" -->
  178. </el-form-item>
  179. </template>
  180. <template v-slot:planCompleteTime="scope">
  181. <el-form-item
  182. :prop="'unpackList.' + scope.$index + '.planCompleteTime'"
  183. :rules="{
  184. required: true,
  185. message: '请选择计划结束时间',
  186. trigger: 'change'
  187. }"
  188. >
  189. <el-date-picker
  190. class="w100"
  191. v-model="scope.row.planCompleteTime"
  192. type="datetime"
  193. @change="handleEndTimeChange(scope.row)"
  194. value-format="yyyy-MM-dd HH:mm:ss"
  195. :picker-options="{
  196. disabledDate: (time) => {
  197. const start = scope.row.planStartTime;
  198. if (!start) return false; // 无开始时间,不禁用
  199. // 将开始时间和当前时间都转换为“当天0点”,仅比较年月日
  200. const startDay = new Date(start);
  201. startDay.setHours(0, 0, 0, 0);
  202. const currentDay = new Date(time);
  203. currentDay.setHours(0, 0, 0, 0);
  204. return currentDay < startDay; // 仅当天0点更早时禁用
  205. },
  206. // 新增:限制同一天内的时间必须晚于开始时间
  207. disabledTime: (date) => {
  208. const start = scope.row.planStartTime;
  209. if (!start) {
  210. return {
  211. disabledHours: () => [],
  212. disabledMinutes: () => [],
  213. disabledSeconds: () => []
  214. };
  215. }
  216. const startDate = new Date(start);
  217. const currentDate = new Date(date);
  218. // 判断是否是同一天
  219. if (
  220. currentDate.getFullYear() === startDate.getFullYear() &&
  221. currentDate.getMonth() === startDate.getMonth() &&
  222. currentDate.getDate() === startDate.getDate()
  223. ) {
  224. const startHour = startDate.getHours();
  225. const startMinute = startDate.getMinutes();
  226. const startSecond = startDate.getSeconds();
  227. return {
  228. // 禁用比 startHour 早的小时
  229. disabledHours: () =>
  230. Array.from({ length: startHour }, (_, i) => i),
  231. // 若小时相同,禁用比 startMinute 早的分钟
  232. disabledMinutes: (hour) =>
  233. hour === startHour
  234. ? Array.from({ length: startMinute }, (_, i) => i)
  235. : [],
  236. // 若小时和分钟都相同,禁用比 startSecond 早的秒
  237. disabledSeconds: (hour, minute) =>
  238. hour === startHour && minute === startMinute
  239. ? Array.from({ length: startSecond }, (_, i) => i)
  240. : []
  241. };
  242. } else {
  243. // 不同天,时间无限制
  244. return {
  245. disabledHours: () => [],
  246. disabledMinutes: () => [],
  247. disabledSeconds: () => []
  248. };
  249. }
  250. }
  251. }"
  252. ></el-date-picker>
  253. <!-- :picker-options="pickerOptionsEnd" -->
  254. </el-form-item>
  255. </template>
  256. <template v-slot:action="{ $index }">
  257. <el-popconfirm
  258. class="ele-action"
  259. title="确定要删除此子单吗?"
  260. @confirm="remove($index)"
  261. >
  262. <template v-slot:reference>
  263. <el-link type="danger" :underline="false" icon="el-icon-delete">
  264. 删除
  265. </el-link>
  266. </template>
  267. </el-popconfirm>
  268. </template>
  269. </ele-pro-table>
  270. </el-form>
  271. </div>
  272. <div slot="footer">
  273. <el-button plain @click="cancel" :loading="loading">取消</el-button>
  274. <el-button type="primary" @click="confirm" :loading="loading"
  275. >确定</el-button
  276. >
  277. </div>
  278. </ele-modal>
  279. </template>
  280. <script>
  281. import { splitBatch } from '@/api/workOrder/index.js';
  282. import { deepClone } from '@/utils';
  283. export default {
  284. components: {},
  285. data() {
  286. return {
  287. visible: false,
  288. formData: {},
  289. requestData: {
  290. deviceCode: '',
  291. deviceName: '',
  292. deviceId: ''
  293. },
  294. formingNum: 0,
  295. storageNum: 0,
  296. form: {
  297. surplusUnpack: [],
  298. unpackList: []
  299. },
  300. pickerOptions: {
  301. disabledDate: (time) => {
  302. // 根据某个条件动态设置最大日期
  303. if (this.formData.planStartTime) {
  304. const maxDate = new Date(this.formData.planStartTime); // 您的逻辑方法
  305. return time.getTime() < maxDate.getTime();
  306. } else {
  307. return false;
  308. }
  309. }
  310. },
  311. pickerOptionsEnd: {
  312. disabledDate: (time) => {
  313. // 根据某个条件动态设置最大日期
  314. if (this.formData.planCompleteTime) {
  315. const maxDate = new Date(this.formData.planCompleteTime); // 您的逻辑方法
  316. return time.getTime() > maxDate.getTime();
  317. } else {
  318. return false;
  319. }
  320. }
  321. },
  322. rules: {},
  323. columns: [
  324. {
  325. prop: 'originalCode',
  326. label: '生产订单号',
  327. align: 'center',
  328. minWidth: 200
  329. },
  330. {
  331. prop: 'formingNum',
  332. label: '要求生产数量',
  333. align: 'center',
  334. slot: 'formingNum',
  335. minWidth: 200
  336. },
  337. {
  338. prop: 'planStartTime',
  339. label: '计划开始时间',
  340. align: 'center',
  341. slot: 'planStartTime',
  342. minWidth: 200
  343. },
  344. {
  345. prop: 'planCompleteTime',
  346. label: '计划结束时间',
  347. align: 'center',
  348. slot: 'planCompleteTime',
  349. minWidth: 200
  350. }
  351. ],
  352. columns2: [
  353. {
  354. prop: 'originalCode',
  355. label: '生产订单号',
  356. align: 'center',
  357. minWidth: 200
  358. },
  359. {
  360. prop: 'formingNum',
  361. label: '要求生产数量',
  362. align: 'center',
  363. slot: 'formingNum',
  364. minWidth: 200
  365. },
  366. {
  367. prop: 'planStartTime',
  368. label: '计划开始时间',
  369. align: 'center',
  370. slot: 'planStartTime',
  371. minWidth: 200
  372. },
  373. {
  374. prop: 'planCompleteTime',
  375. label: '计划结束时间',
  376. align: 'center',
  377. slot: 'planCompleteTime',
  378. minWidth: 200
  379. },
  380. {
  381. columnKey: 'action',
  382. label: '操作',
  383. width: 160,
  384. align: 'center',
  385. resizable: false,
  386. fixed: 'right',
  387. slot: 'action',
  388. showOverflowTooltip: true
  389. }
  390. ],
  391. loading: false
  392. };
  393. },
  394. methods: {
  395. open(row) {
  396. this.visible = true;
  397. this.formData = deepClone(row);
  398. this.formingNum = this.residualVal();
  399. this.storageNum = this.residualVal();
  400. this.form.surplusUnpack = [];
  401. this.form.unpackList = [];
  402. this.setSurplus();
  403. },
  404. // 要求生产数量( 剩余值 )
  405. residualVal() {
  406. if (this.formData.splitResidue) {
  407. return this.formData.splitResidue;
  408. }
  409. if (this.formData.splitResidue === 0) {
  410. return 0;
  411. }
  412. return this.formData.formingNum;
  413. },
  414. checkEndTimeValid(row) {
  415. const { planStartTime: start, planCompleteTime: end } = row;
  416. if (!start || !end) return; // 开始/结束时间未填,跳过
  417. const startTime = new Date(start);
  418. const endTime = new Date(end);
  419. if (endTime < startTime) {
  420. row.planCompleteTime = new Date(startTime); // 修正为开始时间
  421. this.$message.info('结束时间不能早于开始时间,已自动设为开始时间');
  422. }
  423. },
  424. // 【开始时间变化时】触发
  425. handleStartTimeChange(row) {
  426. this.checkEndTimeValid(row);
  427. },
  428. // 【结束时间变化时】触发
  429. handleEndTimeChange(row) {
  430. this.checkEndTimeValid(row);
  431. },
  432. setSurplus() {
  433. this.form.surplusUnpack.push({
  434. originalCode: this.formData.code,
  435. // formingNum: this.formData.formingNum,
  436. formingNum: this.residualVal(),
  437. planStartTime: this.formData.planStartTime,
  438. planCompleteTime: this.formData.planCompleteTime,
  439. isCopy: 1
  440. });
  441. },
  442. openUnpack() {
  443. this.form.unpackList.push({
  444. originalCode: this.formData.code,
  445. formingNum: '',
  446. planStartTime: this.formData.planStartTime || '',
  447. planCompleteTime: this.formData.planCompleteTime || ''
  448. });
  449. },
  450. surplusUnpackFn(index) {
  451. console.log(this.form.unpackList);
  452. },
  453. changeNum(index) {
  454. // let num = this.formData.formingNum;
  455. let num = this.storageNum;
  456. this.form.unpackList.forEach((e) => {
  457. if (e.formingNum != null && e.formingNum != undefined) {
  458. num = num - Number(e.formingNum);
  459. if (num >= 0) {
  460. this.formingNum = num;
  461. } else {
  462. // this.$nextTick(() => {
  463. this.$set(this.form.unpackList[index], 'formingNum', 0);
  464. //
  465. // });
  466. this.$message.error('拆单数超过生产数量');
  467. this.calculateQuantity();
  468. return;
  469. }
  470. }
  471. });
  472. },
  473. // 计算数量 在只有一条数据的时候之前算法有问题
  474. calculateQuantity() {
  475. this.$nextTick(() => {
  476. let total = 0;
  477. this.form.unpackList.map((el) => {
  478. if (el.formingNum) {
  479. total += el.formingNum - 0;
  480. }
  481. });
  482. if (total == 0) {
  483. this.formingNum = this.storageNum;
  484. }
  485. });
  486. },
  487. remove(index) {
  488. this.form.unpackList.splice(index, 1);
  489. if (this.form.unpackList.length == 0) {
  490. this.formingNum = this.storageNum;
  491. } else {
  492. this.changeNum(index);
  493. }
  494. },
  495. cancel() {
  496. this.formData = {};
  497. this.visible = false;
  498. this.$refs.form.resetFields();
  499. },
  500. confirm() {
  501. if (this.form.unpackList.length === 0) {
  502. return this.$message.warning('请至少有一条拆单数据');
  503. }
  504. this.$refs.form.validate(async (value) => {
  505. if (value) {
  506. // if (
  507. // this.form.unpackList.length == 1 &&
  508. // this.form.unpackList[0].formingNum == this.formData.formingNum
  509. // ) {
  510. // this.$message.info('拆单数要大于1');
  511. // return false;
  512. // }
  513. // if (
  514. // this.form.surplusUnpack.length == 1 &&
  515. // this.form.unpackList.length == 0
  516. // ) {
  517. // this.$message.info('拆单数要大于1');
  518. // return false;
  519. // }
  520. // let params = [];
  521. // if (this.formingNum != 0) {
  522. // this.form.surplusUnpack[0].formingNum = this.formingNum;
  523. // params = [...this.form.unpackList, ...this.form.surplusUnpack];
  524. // } else {
  525. // params = this.form.unpackList;
  526. // }
  527. for (let i = 0; i < this.form.unpackList.length; i++) {
  528. let item = this.form.unpackList[i];
  529. if (item.formingNum == 0) {
  530. this.$message.warning(`第${i + 1}条拆单要求生产数据不能为0`);
  531. return;
  532. }
  533. }
  534. this.form.surplusUnpack[0].formingNum = this.formingNum;
  535. let params = [...this.form.unpackList, ...this.form.surplusUnpack];
  536. try {
  537. this.loading = true;
  538. const res = await splitBatch(params);
  539. this.loading = false;
  540. if (res) {
  541. this.$message.success('拆分成功!');
  542. this.$emit('success');
  543. this.cancel();
  544. }
  545. } catch (err) {
  546. this.loading = false;
  547. }
  548. }
  549. });
  550. }
  551. }
  552. };
  553. </script>
  554. <style lang="scss" scoped>
  555. .mt20 {
  556. margin-top: 20px;
  557. }
  558. .el-form-item {
  559. margin-bottom: 0 !important;
  560. }
  561. </style>