ticketBuy.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // pages/ticketBuy/ticketBuy.js
  2. const utils = require('../../utils/util.js')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. userInfo: '',
  9. seller: '',
  10. proList: [{ //产品列表
  11. price: '',
  12. taxRate: '',
  13. taxRateArr: [], // 税率数组
  14. taxRateArrIndex: 0, //选中税率下标
  15. noRatePrice: '', //不含税单价
  16. noRateTotal: '', //不含税金额
  17. totalMoney: '', //价税合计
  18. totalRate: '', //合计税额
  19. quantity: '',
  20. spreadFlag: false
  21. }],
  22. moneyTotal: 0, //价税合计
  23. noRateTotal: 0, //合计金额(不含税)
  24. rateTotal: 0, //合计税额
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onLoad: function (options) {
  30. this.getUser()
  31. },
  32. //销方信息
  33. getUser() {
  34. this.setData({
  35. userInfo: utils.getInfo(),
  36. seller:utils.getCurrEntInfo()
  37. })
  38. // utils.getCompany().then(res => {
  39. // this.setData({
  40. // seller: res.data
  41. // })
  42. // })
  43. },
  44. //提交开票
  45. submitData() {
  46. //产品信息
  47. let proList = this.data.proList
  48. let proListSubmit = []
  49. for (let i = 0; i < proList.length; i++) {
  50. if (proList[i].totalMoney && proList[i].productName) {
  51. proListSubmit.push(proList[i])
  52. }
  53. }
  54. let COMMON_FPKJ_XMXX = []
  55. if (proListSubmit.length <= 0) {
  56. utils.toast('商品信息不能为空')
  57. return
  58. } else {
  59. for (let i = 0; i < proListSubmit.length; i++) {
  60. let item = {
  61. "FPHXZ": 0,
  62. "SPBM": proListSubmit[i].taxationCateCode,
  63. "ZXBM": '',
  64. "YHZCBS": proListSubmit[i].preferentialFlag,
  65. "LSLBS": proListSubmit[i].zeroRateFlag,
  66. "ZZSTSGL": proListSubmit[i].preferentialType,
  67. "XMMC": proListSubmit[i].productName,
  68. "GGXH": proListSubmit[i].specsModel,
  69. "DW": proListSubmit[i].unit,
  70. "XMJE": proListSubmit[i].noRateTotal,
  71. "SL": proListSubmit[i].taxRate,
  72. "SE": proListSubmit[i].totalRate
  73. }
  74. // 如果数量为存在并且不为0则传递参数
  75. if (proListSubmit[i].quantity) {
  76. item.XMSL = proListSubmit[i].quantity
  77. }
  78. // 如果不含税单价存在(为0也可以)则传递参数
  79. if (proListSubmit[i].noRatePrice !== '') {
  80. item.XMDJ = proListSubmit[i].noRatePrice
  81. }
  82. COMMON_FPKJ_XMXX.push(item)
  83. }
  84. }
  85. //要传递的参数
  86. let params = {
  87. mobile: this.data.userInfo.mobile,
  88. reqChannel: 5,
  89. cmdParam: {
  90. "REQUEST_COMMON_FPKJ": {
  91. "KPLX": "0",
  92. "FPZL": 1,
  93. "KPF_NSRSBH": this.data.seller.entTaxId,
  94. "KPF_MC": this.data.seller.entName,
  95. "KPF_DZ": this.data.seller.entAddress,
  96. "KPF_DH": this.data.seller.entPhone,
  97. "KPF_YHZH": this.data.seller.bankAccountNumber,
  98. "KPF_KHHMC": this.data.seller.bankName,
  99. "KPR": this.data.seller.entName,
  100. "SKR": this.data.seller.payees,
  101. "FHR": this.data.seller.reviewers,
  102. "JSHJ": this.data.moneyTotal,
  103. "HJJE": this.data.noRateTotal,
  104. "HJSE": this.data.rateTotal,
  105. "TSPZ": "00",
  106. "COMMON_FPKJ_XMXXS": {
  107. "COMMON_FPKJ_XMXX": COMMON_FPKJ_XMXX
  108. }
  109. }
  110. }
  111. }
  112. utils.axios({
  113. method: 'post',
  114. url: '/sys/fapiao/todo/initSave',
  115. data: params,
  116. sendBefore() {
  117. wx.showLoading({
  118. title: '提交中...',
  119. mask: true
  120. })
  121. },
  122. complete() {
  123. wx.hideLoading()
  124. },
  125. success: res => {
  126. wx.navigateTo({
  127. url: `/pages/ticketBuy/qrcode?recordId=${res.data.recordId}`
  128. })
  129. }
  130. })
  131. },
  132. //计算发票总金额、总税额
  133. calcMoney() {
  134. console.log("数据");
  135. console.log(this.data.proList);
  136. let list = this.data.proList
  137. let moneyTotal = 0
  138. let noRateTotal = 0
  139. let rateTotal = 0
  140. for (let i = 0; i < list.length; i++) {
  141. //计算不含税单价、不含税项目金额、价税合计金额
  142. if (list[i].price !== null && list[i].price !== '' && list[i].quantity) {
  143. list[i].noRatePrice = parseFloat(list[i].price / (1 + (list[i].taxRate ? list[i].taxRate : 0))).toFixed(6)
  144. list[i].noRateTotal = parseFloat(list[i].noRatePrice * (list[i].quantity ? list[i].quantity : 0)).toFixed(2)
  145. list[i].totalMoney = parseFloat(list[i].price * (list[i].quantity ? list[i].quantity : 0)).toFixed(2)
  146. //税额计算公式:价税合计金额/(1+税率)*税率
  147. list[i].totalRate = parseFloat(list[i].totalMoney / (1 + (list[i].taxRate ? list[i].taxRate : 0)) * (list[i].taxRate ? list[i].taxRate : 0)).toFixed(2)
  148. } else {
  149. list[i].totalMoney = 0
  150. list[i].noRateTotal = 0
  151. list[i].totalRate = 0
  152. }
  153. //总的相加
  154. moneyTotal += Number(list[i].totalMoney)
  155. noRateTotal += Number(list[i].noRateTotal)
  156. rateTotal += Number(list[i].totalRate)
  157. }
  158. this.setData({
  159. moneyTotal: parseFloat(moneyTotal).toFixed(2),
  160. noRateTotal: parseFloat(noRateTotal).toFixed(2),
  161. rateTotal: parseFloat(rateTotal).toFixed(2),
  162. proList: list
  163. })
  164. },
  165. getTotalMoney(e) {
  166. let moneyTotal = 0
  167. let noRateTotal = 0
  168. let rateTotal = 0
  169. let list = this.data.proList
  170. let index = Number(e.currentTarget.dataset.index)
  171. let totalMoney = e.detail.value
  172. list[index].totalMoney = parseFloat(Number(totalMoney)).toFixed(2)
  173. if (list[index].quantity) {
  174. list[index].price = list[index].totalMoney / list[index].quantity
  175. list[index].noRatePrice = parseFloat(list[index].price / (1 + (list[index].taxRate ? list[index].taxRate : 0))).toFixed(6)
  176. } else {
  177. list[index].price = ''
  178. list[index].noRatePrice = ''
  179. }
  180. for (let i = 0; i < list.length; i++) {
  181. //计算不含税单价、不含税项目金额、价税合计金额
  182. if (list[i].totalMoney) {
  183. list[i].noRateTotal = parseFloat(list[i].totalMoney / (1 + (list[i].taxRate ? list[i].taxRate : 0))).toFixed(2)
  184. if (i !== index) {
  185. list[i].totalMoney = parseFloat(list[i].price * (list[i].quantity ? list[i].quantity : 0)).toFixed(2)
  186. }
  187. //税额计算公式:价税合计金额/(1+税率)*税率
  188. list[i].totalRate = parseFloat(list[i].totalMoney / (1 + (list[i].taxRate ? list[i].taxRate : 0)) * (list[i].taxRate ? list[i].taxRate : 0)).toFixed(2)
  189. }
  190. //总的相加
  191. moneyTotal += Number(list[i].totalMoney)
  192. noRateTotal += Number(list[i].noRateTotal)
  193. rateTotal += Number(list[i].totalRate)
  194. }
  195. this.setData({
  196. moneyTotal: parseFloat(moneyTotal).toFixed(2),
  197. noRateTotal: parseFloat(noRateTotal).toFixed(2),
  198. rateTotal: parseFloat(rateTotal).toFixed(2),
  199. proList: list
  200. })
  201. },
  202. //查询商品
  203. toSearchPro(e) {
  204. let index = e.currentTarget.dataset.index
  205. wx.navigateTo({
  206. url: `/pages/searchPro/searchPro?index=${index}`
  207. })
  208. },
  209. //添加商品
  210. addTicket() {
  211. let proList = this.data.proList
  212. proList.push({
  213. price: '',
  214. taxRate: '',
  215. taxRateArr: [],
  216. taxRateArrIndex: 0,
  217. noRatePrice: '',
  218. noRateTotal: '',
  219. totalMoney: '',
  220. totalRate: '',
  221. quantity: '',
  222. spreadFlag: false
  223. })
  224. this.setData({
  225. proList
  226. })
  227. },
  228. // 获取商品编号税率
  229. getTaxRate(prevIndex, taxCateCode, taxRate) {
  230. let params = {
  231. taxCateCode,
  232. entTaxId: wx.getStorageSync('entTaxId')
  233. }
  234. utils.axios({
  235. method: 'get',
  236. url: '/sys/entInfo/rates',
  237. data: params,
  238. sendBefore() {},
  239. complete() {},
  240. success: res => {
  241. let index = prevIndex
  242. let proList = this.data.proList
  243. let result = res.data
  244. for (let i = 0; i < result.length; i++) {
  245. result[i] = Number(result[i])
  246. if (result[i] === taxRate) {
  247. proList[prevIndex].taxRateArrIndex = i
  248. }
  249. }
  250. proList[prevIndex].taxRateArr = result
  251. this.setData({
  252. proList
  253. })
  254. this.calcMoney()
  255. }
  256. })
  257. },
  258. setRate(e) {
  259. let index = e.currentTarget.dataset.index
  260. let proList = this.data.proList
  261. proList[index].taxRateArrIndex = e.detail.value
  262. proList[index].taxRate = proList[index].taxRateArr[proList[index].taxRateArrIndex]
  263. this.setData({
  264. proList
  265. })
  266. this.calcMoney()
  267. },
  268. //单个商品展开收起
  269. spread(e) {
  270. let index = e.currentTarget.dataset.index
  271. this.setData({
  272. [`proList[${index}].spreadFlag`]: !this.data.proList[index].spreadFlag
  273. })
  274. },
  275. delete(e) {
  276. let index = e.currentTarget.dataset.index
  277. let proList = this.data.proList
  278. proList.splice(index, 1)
  279. this.setData({
  280. proList
  281. })
  282. this.calcMoney()
  283. },
  284. //购物车加
  285. add(e) {
  286. let proList = this.data.proList
  287. let index = e.currentTarget.dataset.index
  288. let curNum = proList[index].quantity
  289. curNum++
  290. let str = 'proList[' + index + '].quantity'
  291. this.setData({
  292. [str]: curNum
  293. })
  294. this.calcMoney()
  295. },
  296. //购物车减
  297. minus(e) {
  298. let proList = this.data.proList
  299. let index = e.currentTarget.dataset.index
  300. let curNum = proList[index].quantity
  301. curNum--
  302. if (curNum <= 1) {
  303. curNum = 1
  304. }
  305. let str = 'proList[' + index + '].quantity'
  306. this.setData({
  307. [str]: curNum
  308. })
  309. this.calcMoney()
  310. },
  311. //input输入框
  312. inputNum(e) {
  313. let index = e.currentTarget.dataset.index
  314. let str = 'proList[' + index + '].quantity'
  315. this.setData({
  316. [str]: Number(e.detail)
  317. })
  318. this.calcMoney()
  319. },
  320. getPrice(e) {
  321. console.log(e);
  322. let index = e.currentTarget.dataset.index
  323. let str = 'proList[' + index + '].price'
  324. this.setData({
  325. [str]: Number(e.detail.value)
  326. })
  327. this.calcMoney()
  328. },
  329. /**
  330. * 生命周期函数--监听页面初次渲染完成
  331. */
  332. onReady: function () {
  333. },
  334. /**
  335. * 生命周期函数--监听页面显示
  336. */
  337. onShow: function () {
  338. },
  339. /**
  340. * 生命周期函数--监听页面隐藏
  341. */
  342. onHide: function () {
  343. },
  344. /**
  345. * 生命周期函数--监听页面卸载
  346. */
  347. onUnload: function () {
  348. },
  349. /**
  350. * 页面相关事件处理函数--监听用户下拉动作
  351. */
  352. onPullDownRefresh: function () {
  353. },
  354. /**
  355. * 页面上拉触底事件的处理函数
  356. */
  357. onReachBottom: function () {
  358. },
  359. /**
  360. * 用户点击右上角分享
  361. */
  362. onShareAppMessage: function () {
  363. return {
  364. title: '诺信云',
  365. path: '/pages/index/index'
  366. }
  367. }
  368. })