print.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. import { disAutoConnect, hiprint, defaultElementTypeProvider } from 'vue-plugin-hiprint'
  2. disAutoConnect()
  3. import panel from '@/utils/panel'
  4. import panel2 from '@/utils/panel2'
  5. import { addPrint, getDtailPrintDis } from '@/api/supply/pickup'
  6. import { getDeliverDetail } from '@/api/supply/deliver'
  7. import { addPrints } from '@/api/supply/pickup'
  8. import { getCompanyList } from '@/api/user'
  9. export default {
  10. data() {
  11. return {
  12. company: '', // 公司名称
  13. clonelData: [], // 克隆数据
  14. outputData: [], // 打印数据
  15. curPaper: {
  16. type: 'A5',
  17. width: 500,
  18. height: 147.6
  19. },
  20. paperTypes: {
  21. A3: {
  22. width: 420,
  23. height: 296.6
  24. },
  25. A4: {
  26. width: 210,
  27. height: 296.6
  28. },
  29. A5: {
  30. width: 210,
  31. height: 147.6
  32. },
  33. B3: {
  34. width: 500,
  35. height: 352.6
  36. },
  37. B4: {
  38. width: 250,
  39. height: 352.6
  40. },
  41. B5: {
  42. width: 250,
  43. height: 175.6
  44. }
  45. },
  46. scaleValue: 1,
  47. scaleMax: 5,
  48. scaleMin: 0.5,
  49. loading: false,
  50. hiprintTemplate: '',
  51. addIds: [],
  52. dataLength: 5,
  53. panelData: '',
  54. moduleType: ''
  55. }
  56. },
  57. computed: {
  58. curPaperType() {
  59. let type = 'other'
  60. const types = this.paperTypes
  61. for (const key in types) {
  62. const item = types[key]
  63. const { width, height } = this.curPaper
  64. if (item.width === width && item.height === height) {
  65. type = key
  66. }
  67. }
  68. return type
  69. }
  70. },
  71. created() {
  72. this.getCompanyLists()
  73. },
  74. methods: {
  75. // 初始化打印模板
  76. initPrint(type) {
  77. hiprint.init({
  78. providers: [new defaultElementTypeProvider()]
  79. })
  80. // 替换配置
  81. hiprint.setConfig({
  82. movingDistance: 2.5,
  83. text: {
  84. supportOptions: [
  85. {
  86. name: 'styler',
  87. hidden: true
  88. },
  89. {
  90. name: 'formatter',
  91. hidden: true
  92. }
  93. ]
  94. }
  95. })
  96. let panelData = panel
  97. if (type === 0) {
  98. panelData = panel2
  99. this.dataLength = 4
  100. }
  101. // eslint-disable-next-line no-undef
  102. hiprint.PrintElementTypeManager.buildByHtml($('.ep-draggable-item'))
  103. this.hiprintTemplate = new hiprint.PrintTemplate({
  104. template: panelData,
  105. settingContainer: '#PrintElementOptionSetting',
  106. paginationContainer: '.hiprint-printPagination'
  107. })
  108. // this.hiprintTemplate.design('#hiprint-printTemplate')
  109. // 获取当前放大比例, 当zoom时传true 才会有
  110. // this.scaleValue = hiprintTemplate.editingPanel.scale || 1;
  111. },
  112. /**
  113. * 获取需要打印数据详情
  114. * @param {Array} ids
  115. */
  116. async getDateil(ids, funcType = 'getDeliverDetail', check = null) {
  117. this.$startLoading()
  118. let loadingLen
  119. // 兼容多个打印数据
  120. ids = ids instanceof Array ? ids : [ids]
  121. console.log(ids)
  122. // 清空之前打印的数据
  123. this.outputData = []
  124. // 筛选id
  125. let formatting = []
  126. // 仓库认证请求接口参数
  127. const params = []
  128. // 获取数据id
  129. for (let i = ids.length; i > 0; i--) {
  130. formatting.push(ids[i - 1].id || ids[i - 1])
  131. if (funcType === 'getDtailPrintDis') {
  132. params.push({
  133. id: ids[i - 1].id,
  134. invoiceId: ids[i - 1].invoiceId
  135. })
  136. this.addIds.push(ids[i - 1].id)
  137. }
  138. }
  139. if (funcType === 'getDtailPrintDis') {
  140. const requestParams = params
  141. try {
  142. const { data } = await getDtailPrintDis(requestParams)
  143. loadingLen = data.length
  144. for (let i = data.length; i > 0; i--) {
  145. const newData = data[i - 1]
  146. await this.setPrintData(newData, funcType, check)
  147. loadingLen--
  148. }
  149. } catch (error) {
  150. this.$endLoading()
  151. console.error('获取打印数据失败')
  152. }
  153. } else {
  154. // id 去重
  155. formatting = [...new Set(formatting)]
  156. loadingLen = formatting.length
  157. for (let i = formatting.length; i > 0; i--) {
  158. const requestParams = {
  159. id: formatting[i - 1]
  160. }
  161. try {
  162. const { data } = await getDeliverDetail(requestParams)
  163. this.setPrintData(data)
  164. } catch (error) {
  165. this.$endLoading()
  166. this.$errorMsg(error)
  167. console.error('获取打印数据失败')
  168. }
  169. loadingLen--
  170. }
  171. }
  172. if (loadingLen == 0) {
  173. return Promise.resolve()
  174. }
  175. },
  176. // 获取公司名称
  177. async getCompanyLists() {
  178. try {
  179. const { data } = await getCompanyList()
  180. this.company = data ? data[0].companyName : ''
  181. } catch (error) {
  182. console.error('获取公司名称失败')
  183. }
  184. },
  185. // 获取当前时间
  186. nowDate() {
  187. var date = new Date()
  188. var seperator1 = '-'
  189. var year = date.getFullYear()
  190. var month = date.getMonth() + 1
  191. var strDate = date.getDate()
  192. if (month >= 1 && month <= 9) {
  193. month = '0' + month
  194. }
  195. if (strDate >= 0 && strDate <= 9) {
  196. strDate = '0' + strDate
  197. }
  198. var currentdate = year + seperator1 + month + seperator1 + strDate
  199. return currentdate
  200. },
  201. // 格式化时间
  202. dateToDayFilter(date) {
  203. if (!date) {
  204. return ''
  205. }
  206. return date.slice(0, 10)
  207. },
  208. // 添加次数
  209. async addPrint(funcType = 'getDeliverDetail') {
  210. let ids = []
  211. console.log(this.clonelData);
  212. console.log(this.addIds);
  213. for (let i = this.clonelData.length; i > 0; i--) {
  214. const tempData = this.clonelData[i - 1].invoicePickBeans
  215. if (tempData.length) {
  216. for (let e = tempData.length; e > 0; e--) {
  217. const newTempData = tempData[e - 1]
  218. ids.push(newTempData.id)
  219. }
  220. } else {
  221. return this.clonelData[i - 1].invoiceOrderId || this.clonelData[i - 1].id
  222. }
  223. }
  224. let requestParams = {}
  225. try {
  226. if (funcType === 'getDtailPrintDis') {
  227. ids = [...new Set(this.addIds)]
  228. requestParams = {
  229. ids: ids.join(',')
  230. }
  231. funcType = addPrint
  232. } else {
  233. requestParams = {
  234. ids: ids.join(',')
  235. }
  236. funcType = addPrints
  237. }
  238. console.log(requestParams,'ddsd');
  239. const promise = await funcType(requestParams)
  240. // 清空当前克隆数据,避免重复添加次数
  241. this.clonelData = []
  242. this.addIds =[]
  243. return promise
  244. } catch (error) {
  245. // console.error('添加打印次数失败')
  246. this.clonelData = []
  247. this.addIds =[]
  248. // this.$errorMsg('添加打印次数失败' + JSON.stringify(requestParams))
  249. return Promise.reject(error.message)
  250. }
  251. },
  252. /**
  253. * 设置纸张大小
  254. * @param type [A3, A4, A5, B3, B4, B5, other]
  255. * @param value {width,height} mm
  256. */
  257. setPaper(type, value) {
  258. try {
  259. if (Object.keys(this.paperTypes).includes(type)) {
  260. this.curPaper = {
  261. type: type,
  262. width: value.width,
  263. height: value.height
  264. }
  265. this.hiprintTemplate.setPaper(value.width, value.height)
  266. } else {
  267. this.curPaper = {
  268. type: 'other',
  269. width: value.width,
  270. height: value.height
  271. }
  272. this.hiprintTemplate.setPaper(value.width, value.height)
  273. }
  274. } catch (error) {
  275. this.$message.error(`操作失败: ${error}`)
  276. }
  277. },
  278. /**
  279. * 方法1
  280. * 自定义模板数据
  281. * @param {object} data
  282. * this.outputData 打印数据
  283. */
  284. setPrintData(data, funcType, check) {
  285. let salesOrderId, invoiceId // 出库单号,发货单号 默认数据中的第一个
  286. salesOrderId = data.invoicePickBeans[0].salesOrderId
  287. invoiceId = data.invoicePickBeans[0].invoiceId
  288. let refUseUnit, refEnginRecordNo
  289. refUseUnit = data.refUseUnit || data.invoicePickBeans[0].refUseUnit || ''
  290. refEnginRecordNo = data.refEnginRecordNo || data.invoicePickBeans[0].refEnginRecordNo || ''
  291. let remark
  292. remark = funcType === 'getDtailPrintDis' ? data.invoicePickBeans[0].remark || '' : data.remark || ''
  293. console.log(data.invoicePickBeans[0], 'oo')
  294. const tuiHuoRen = data.createBy
  295. let address = null
  296. if (data.pickType === 2) {
  297. address = data.invoicePickBeans[0].address || ''
  298. }
  299. // 数量合计
  300. let total = 0
  301. // 初始化打印次数
  302. let printNum = 0
  303. // 避免数据发生改变
  304. this.clonelData.push(JSON.parse(JSON.stringify(data)))
  305. // 需要计算长度和数据裁切
  306. const invoicePickBeans = data.invoicePickBeans
  307. // 获取length向上取整
  308. const len = Math.ceil(invoicePickBeans.length / this.dataLength)
  309. for (let index = 0; index < len; index++) {
  310. const table = []
  311. // length <= 0 则不执行打印
  312. if (invoicePickBeans.length) {
  313. // 取第一个条数据printNum
  314. printNum = invoicePickBeans[0].printNum
  315. const newInvoicePickBeans = invoicePickBeans.splice(0, this.dataLength)
  316. for (let e = newInvoicePickBeans.length; e > 0; e--) {
  317. const tempData = newInvoicePickBeans[e - 1]
  318. total += Math.abs(+tempData.refundableQty)
  319. // 添加表格数据
  320. table.push({
  321. id: tempData.id,
  322. createTime: tempData.id ? this.dateToDayFilter(data.createTime) : '',
  323. enginOrderType:
  324. tempData.orderType == 'HOME' ||
  325. tempData.orderType == 'TRADE' ||
  326. tempData.orderType === 'REQUISITION_TRADE' ||
  327. tempData.orderType === 'REQUISITION_HOME'
  328. ? tempData.enginOrderNo
  329. : tempData.mainOrderId,
  330. materialName: tempData.materialName,
  331. specification: tempData.specification,
  332. refundableQty: tempData.refundableQty,
  333. pjxh1Text: tempData.pjxh1Text
  334. })
  335. }
  336. }
  337. // 添加print输出数据
  338. this.outputData.push({
  339. pageNumber: `${len}-${index + 1}`,
  340. printNum: printNum + 1,
  341. salesId: salesOrderId,
  342. invoiceId: invoiceId,
  343. refUseUnit: refUseUnit,
  344. refEnginRecordNo: refEnginRecordNo,
  345. type: data.type,
  346. tiTui: data.type === 2 ? `退货人` : `提货人`,
  347. takerPhone: data.takerPhone || '',
  348. pickTypeName: data.pickType === null ? '' : data.pickType === 1 ? '商家自提' : '物流配送',
  349. headerRemark: remark,
  350. total_num: data.total_num,
  351. total: total,
  352. company: data.type === 2 ? `${this.company}销售退货单` : `${this.company}销售发货单`,
  353. pickOrderWater: data.pickOrderWater,
  354. customerNumber: data.customerNumber,
  355. takerDa: '',
  356. nowDate: this.nowDate(),
  357. takerName:
  358. data.type === 2
  359. ? `退货人:${check == 0 ? data.pickLogistics || '' : tuiHuoRen || ''}`
  360. : `提货人:${check == 0 ? data.pickLogistics || '' : data.takerName || ''}`,
  361. customerName: data.customerName || '',
  362. correspondName: data.correspondName,
  363. correspondNames: '',
  364. pickCar: data.pickCar || '',
  365. createBy: JSON.parse(localStorage.getItem('supply_user')).nickName,
  366. address,
  367. table
  368. })
  369. }
  370. },
  371. /**
  372. * 方法2
  373. * 自定义打印模板
  374. * @param {object} data
  375. * @returns HtmlDom
  376. */
  377. setPrintDom(data) {
  378. return `
  379. <div style="font-family:'黑体';">
  380. <h1 style="text-align:center">${this.company}其他收款单</h1>
  381. <div>
  382. <div>
  383. <span style="wdith:50%"></span>
  384. <span style="wdith:50%"></span>
  385. </div>
  386. <div>
  387. <span style="wdith:50%"></span>
  388. <span style="wdith:50%"></span>
  389. </div>
  390. </div>
  391. <div >
  392. <table border=".5" cellspacing="0" width="856" height="250"
  393. style="border-color: rgb(0,0,0);
  394. border-collapse: collapse;
  395. border-style: none;
  396. border: 1px solid rgb(0,0,0);
  397. font-weight: normal;
  398. direction: ltr;
  399. padding-bottom: 0pt;
  400. padding-left: 4pt;
  401. padding-right: 4pt;
  402. padding-top: 0pt;
  403. text-decoration: none;
  404. vertical-align: middle;
  405. box-sizing: border-box;
  406. word-wrap: break-word;
  407. word-break: break-all;">
  408. <tr>
  409. <td>单据编号</td>
  410. <td>${data.billNo}</td>
  411. <td>业务日期</td>
  412. <td>${data.theTime}</td>
  413. <td>打印日期</td>
  414. <td>${data.createTime}</td>
  415. </tr>
  416. <tr>
  417. <td>付款单位</td>
  418. <td colspan="3">${data.customerName}</td>
  419. <td>项目费用名称</td>
  420. <td>${data.customerType}</td>
  421. </tr>
  422. <tr>
  423. <td>钱包</td>
  424. <td>${data.walletName}</td>
  425. <td>实收金额</td>
  426. <td style="text-align:right">${numToFixed(data.amount)}</td>
  427. <td colspan="2">${data.amount}</td>
  428. </tr>
  429. <tr>
  430. <td>备注</td>
  431. <td colspan="5">${data.remark}</td>
  432. </tr>
  433. </table>
  434. </div>
  435. <div style="margin:100px 0 0 0">
  436. <div>
  437. <div>
  438. <span style="wdith:50%"></span>
  439. <span style="wdith:50%"></span>
  440. </div>
  441. <div>
  442. <span style="wdith:50%"></span>
  443. <span style="wdith:50%"></span>
  444. </div>
  445. </div>
  446. </div>
  447. </div>
  448. `
  449. },
  450. /**
  451. * 备份方法
  452. * 获取需要打印数据详情
  453. * @param {Array} ids
  454. */
  455. async getDateils(ids, funcType = 'getDeliverDetail') {
  456. this.$startLoading()
  457. let loadingLen
  458. // 兼容多个打印数据
  459. ids = ids instanceof Array ? ids : [ids]
  460. console.log(ids)
  461. // 清空之前打印的数据
  462. this.outputData = []
  463. // 筛选id
  464. let formatting = []
  465. // 仓库认证请求接口参数
  466. const params = []
  467. // 获取数据id
  468. for (let i = ids.length; i > 0; i--) {
  469. formatting.push(ids[i - 1].id || ids[i - 1])
  470. if (funcType === 'getDtailPrintDis') {
  471. params.push({
  472. id: ids[i - 1].id,
  473. invoiceId: ids[i - 1].invoiceId
  474. })
  475. this.addIds.push(ids[i - 1].id)
  476. }
  477. }
  478. if (funcType === 'getDtailPrintDis') {
  479. const requestParams = params
  480. try {
  481. const { data } = await getDtailPrintDis(requestParams)
  482. loadingLen = data.length
  483. for (let i = data.length; i > 0; i--) {
  484. const newData = data[i - 1]
  485. await this.setPrintData(newData)
  486. loadingLen--
  487. }
  488. } catch (error) {
  489. this.$endLoading()
  490. console.error('获取打印数据失败')
  491. }
  492. } else {
  493. // id 去重
  494. formatting = [...new Set(formatting)]
  495. loadingLen = formatting.length
  496. for (let i = formatting.length; i > 0; i--) {
  497. const requestParams = {
  498. id: formatting[i - 1]
  499. }
  500. try {
  501. const { data } = await getDeliverDetail(requestParams)
  502. this.setPrintData(data)
  503. } catch (error) {
  504. this.$endLoading()
  505. this.$errorMsg(error)
  506. console.error('获取打印数据失败')
  507. }
  508. loadingLen--
  509. }
  510. }
  511. if (loadingLen == 0) {
  512. return Promise.resolve()
  513. }
  514. }
  515. }
  516. }