editInvoice.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. // pages/invoice/editInvoice/editInvoice.js
  2. const app = getApp();
  3. const api = app.api;
  4. import WxValidate from '../../../utils/WxValidate'
  5. import {pointNum} from "../../../utils/util"
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. saleInfo:null,
  12. pickerInvoiceKindIndex:0,
  13. pickerInvoiceKind:[
  14. {
  15. name:"增值税普通发票",
  16. value:0,
  17. },
  18. // {
  19. // name:"增值税开票",
  20. // value:1,
  21. // }
  22. ],
  23. /* pickerInvoiceKind:[
  24. {
  25. name:"普通开票",
  26. value:0,
  27. },
  28. {
  29. name:"服务费开票",
  30. value:1,
  31. }
  32. ],*/
  33. pickerServeIndex:"",
  34. // pickerServe:[
  35. // '会务服务',
  36. // '展览展示服务',
  37. // '文化艺术交流策划',
  38. // '企业管理咨询',
  39. // '商务信息咨询',
  40. // '市场信息咨询与调查',
  41. // '企业营销策划',
  42. // '企业形象策划'
  43. // ],
  44. pickerServe:[
  45. '绘画作品',
  46. '雕塑作品',
  47. '装置作品',
  48. '综合作品',
  49. '摄影作品',
  50. '新媒体作品',
  51. '影像作品',
  52. '陶瓷作品'
  53. ],
  54. invoiceBuySearchValue:'',
  55. invoiceBuyFocus:false,
  56. invoiceBuyList:[],
  57. isShowInvoiceBuyList:false,
  58. searchTimer:null,
  59. formData: {
  60. amount:"", //合计开票金额
  61. buyIdNumber:"", //购买方纳税人识别号
  62. buyName:"", //购买方
  63. invoiceKind:0, //0普通开票 1服务费开票
  64. invoiceOrderId:20, //订单id
  65. invoiceType:0, //发票类型
  66. provinceName:"",//
  67. cityName:"",
  68. expAreaName:"",
  69. address:"",
  70. // receiverAddress:"", //收件人地址
  71. /* receiverName:"", //收件人姓名
  72. receiverPhoneNumber:"", //收件人电话*/
  73. name:"",//收件人姓名
  74. mobile:"",//收件人姓名
  75. serviceContentInfo:{
  76. serverName:"",
  77. number:1,
  78. },
  79. saleId:"", //销售方id
  80. saleIdNumber:"", //销售方身份证
  81. saleName:"", //销售方信息
  82. salePhoneNumber:"", //销售方电话
  83. },
  84. rules: {
  85. invoiceType:{
  86. required: true,
  87. },
  88. myServerName:{
  89. required: true,
  90. },
  91. buyName: {
  92. required: true
  93. },
  94. buyIdNumber: {
  95. required: true,
  96. egAndNumber: true,
  97. },
  98. amount: {
  99. required: true,
  100. number:true,
  101. },
  102. invoiceKind:{
  103. required: true
  104. },
  105. address:{
  106. required: true,
  107. },
  108. saleId:{
  109. required: true
  110. }
  111. },
  112. messages: {
  113. invoiceType:{
  114. required: '请选择发票类型',
  115. },
  116. myServerName:{
  117. required: '请选择服务内容',
  118. },
  119. amount: {
  120. required: '请填写开票金额',
  121. number:"请输入正确的开票金额"
  122. },
  123. buyIdNumber: {
  124. required: '请填写纳税人识别号',
  125. egAndNumber: '税号只能输入数字和英文'
  126. },
  127. buyName: {
  128. required: '请填写发票抬头'
  129. },
  130. invoiceKind:{
  131. required: '请填写开票种类',
  132. },
  133. address:{
  134. required: '请填写发票收货地址',
  135. },
  136. saleId:{
  137. required: '请选择销售方'
  138. }
  139. },
  140. isValidate:false,
  141. isSubmit:false
  142. },
  143. /**
  144. * 生命周期函数--监听页面加载
  145. */
  146. onLoad: function (options) {
  147. const { rules, messages } = this.data;
  148. this.WxValidate = new WxValidate(rules, messages);
  149. console.log('WxValidate',this.WxValidate);
  150. this.setData({
  151. invoiceId:options.invoiceId||'',
  152. invoiceOrderId:options.id||'',
  153. 'formData.invoiceId':options.invoiceId||'',
  154. 'formData.invoiceOrderId':options.id||''
  155. },()=>{
  156. //有提交过发票 未更改状态获取之前记录
  157. if(options.invoiceId&&options.invoiceId!=""){
  158. this.getData();
  159. }
  160. });
  161. wx.hideShareMenu();
  162. },
  163. /**
  164. * 生命周期函数--监听页面初次渲染完成
  165. */
  166. onReady: function () {
  167. },
  168. /**
  169. * 生命周期函数--监听页面显示
  170. */
  171. onShow: function () {
  172. this.getInvoiceBuy(null);
  173. this.getSaleList();
  174. },
  175. /** input change value**/
  176. changeInput(e){
  177. console.log(e);
  178. const id = e.currentTarget.id;
  179. const number = e.currentTarget.dataset.number;
  180. let value = e.detail.value;
  181. if(number=='pointNum'){
  182. value=pointNum(value)
  183. }
  184. this.setData({
  185. [id]: value
  186. });
  187. },
  188. /** pick value**/
  189. bindPickerChangeKind: function(e) {
  190. console.log('picker发送选择改变,携带值为', e.detail.value)
  191. this.setData({
  192. pickerInvoiceKindIndex: e.detail.value,
  193. 'formData.invoiceType':e.detail.value
  194. },()=>{
  195. this.validateForm()
  196. })
  197. },
  198. bindPickerChangeServe: function(e) {
  199. console.log('picker发送选择改变,携带值为', e.detail.value)
  200. this.setData({
  201. pickerServeIndex: e.detail.value,
  202. 'formData.myServerName':e.detail.value,
  203. 'formData.serviceContentInfo.serverName':this.data.pickerServe[e.detail.value]
  204. },()=>{
  205. this.validateForm()
  206. })
  207. },
  208. /**已提交获取数据**/
  209. getData(){
  210. const {invoiceId,invoiceOrderId}=this.data;
  211. app.post(api.queryOrderInvoiceInfo,{
  212. id:invoiceId,
  213. invoiceOrderId
  214. }).then(res=>{
  215. const {
  216. invoiceType,
  217. serviceContentInfo,
  218. saleId,
  219. saleIdNumber,
  220. saleName,
  221. salePhoneNumber,
  222. buyName,
  223. buyIdNumber,
  224. invoiceAmount,
  225. provinceName,
  226. cityName,
  227. expAreaName,
  228. address,
  229. mobile,
  230. name,
  231. }=res.data;
  232. this.setData({
  233. pickerInvoiceKindIndex:invoiceType,
  234. 'formData.invoiceType':invoiceType,
  235. 'formData.serviceContentInfo.':{
  236. serverName:serviceContentInfo.serverName,
  237. number:1,
  238. },
  239. 'formData.myServerName':serviceContentInfo.serverName,
  240. 'formData.provinceName': provinceName,
  241. 'formData.cityName': cityName,
  242. 'formData.expAreaName': expAreaName,
  243. 'formData.address': address,
  244. 'formData.mobile':mobile,
  245. 'formData.name':name,
  246. 'formData.amount':invoiceAmount/100,
  247. 'formData.buyName':buyName,
  248. 'formData.buyIdNumber':buyIdNumber,
  249. 'formData.saleId':saleId, //销售方id
  250. 'formData.saleIdNumber':saleIdNumber, //销售方身份证
  251. 'formData.saleName':saleName, //销售方信息
  252. 'formData.salePhoneNumber':salePhoneNumber, //销售方电话
  253. },()=>{
  254. this.validateForm()
  255. })
  256. })
  257. },
  258. validateForm(showInfo=false){
  259. const {formData} = this.data;
  260. const submitFormData=Object.assign({},formData);
  261. let flag=true;
  262. // 传入表单数据,调用验证方法
  263. if (!this.WxValidate.checkForm(submitFormData)) {
  264. flag=false
  265. if(showInfo===true){
  266. const error = this.WxValidate.errorList[0];
  267. wx.showToast({
  268. title: error.msg,
  269. icon: 'none',
  270. duration: 2000
  271. })
  272. }
  273. }
  274. if (submitFormData.amount< 5000){
  275. flag=false
  276. if(showInfo===true) {
  277. wx.showToast({
  278. title: '开票金额不能小于5000元',
  279. icon: 'none',
  280. duration: 2000
  281. })
  282. }
  283. }
  284. this.setData({
  285. isValidate:flag
  286. });
  287. return flag
  288. },
  289. /** 保存数据 **/
  290. saveData() {
  291. const {formData,isSubmit} = this.data;
  292. const submitFormData=Object.assign({},formData)
  293. if(isSubmit){
  294. return
  295. }
  296. // // 传入表单数据,调用验证方法
  297. // if (!this.WxValidate.checkForm(submitFormData)) {
  298. // const error = this.WxValidate.errorList[0];
  299. // wx.showToast({
  300. // title: error.msg,
  301. // icon: 'none',
  302. // duration: 2000
  303. // })
  304. // return false
  305. // }
  306. // if (submitFormData.amount< 5000){
  307. // wx.showToast({
  308. // title: '开票金额不能小于5000元',
  309. // icon: 'none',
  310. // duration: 2000
  311. // })
  312. // return false
  313. // }
  314. // //console.log(this.WxValidate);
  315. let flag=this.validateForm(true);
  316. if(!flag){
  317. return;
  318. }
  319. //元转成分单位
  320. submitFormData.amount= parseInt(submitFormData.amount*100);
  321. console.log('转成分',submitFormData.amount)
  322. this.setData({
  323. isSubmit:true
  324. });
  325. app.post(api.addInvoice, submitFormData).then(res => {
  326. wx.showToast({
  327. title: '添加成功',
  328. duration: 2000
  329. });
  330. const {invoiceId,invoiceOrderId}=res.data;
  331. this.setData({
  332. isSubmit:false,
  333. });
  334. wx.navigateTo({
  335. url:`/pages/invoice/preview/preview?invoiceId=${invoiceId}&invoiceOrderId=${invoiceOrderId}`
  336. })
  337. //wx.navigateBack();
  338. }).catch(()=>{
  339. this.setData({
  340. isSubmit:false
  341. })
  342. })
  343. },
  344. /** 获取收货地址 **/
  345. getAddress(){
  346. const settingAddress=function(res){
  347. this.setData({
  348. 'formData.name':res.userName,
  349. 'formData.provinceName':res.provinceName,
  350. 'formData.cityName': res.cityName,
  351. 'formData.expAreaName':res.countyName,
  352. 'formData.address':res.detailInfo,
  353. 'formData.mobile':res.telNumber,
  354. },()=>{
  355. this.validateForm()
  356. })
  357. }.bind(this);
  358. wx.chooseAddress({
  359. success:(res)=>{
  360. // console.log(res);
  361. settingAddress(res);
  362. },
  363. fail:(error)=>{
  364. //console.log(error);
  365. if(error.errMsg=="chooseAddress:fail auth deny"){
  366. wx.showModal({
  367. title: '是否授权通讯地址',
  368. content: '需要获取您通讯地址,请确认授权,否则获取地址功能将无法使用',
  369. success: (tip)=> {
  370. if (tip.confirm) {
  371. wx.openSetting({
  372. success: function (data) {
  373. //console.log(data);
  374. if (data.authSetting["scope.address"] === true) {
  375. wx.showToast({
  376. title: '授权成功',
  377. icon: 'success',
  378. duration: 1000
  379. });
  380. //授权成功之后,再调用chooseLocation选择
  381. wx.chooseAddress({
  382. success: (res)=> {
  383. settingAddress(res);
  384. },
  385. })
  386. } else {
  387. wx.showToast({
  388. title: '授权失败',
  389. icon: 'success',
  390. duration: 1000
  391. })
  392. }
  393. }
  394. })
  395. }
  396. }
  397. })
  398. }
  399. }
  400. })
  401. },
  402. /** 跳转销售方 **/
  403. goSaleList(){
  404. wx.navigateTo({
  405. url:`/pages/invoice/saleList/saleList?id=${this.data.formData.saleId}`
  406. })
  407. },
  408. /**获取销售方信息**/
  409. getSaleList(){
  410. const saleInfo=wx.getStorageSync('saleInfo');
  411. if(saleInfo){
  412. let {saleId,identityNumber,name,phoneNumber}=saleInfo;
  413. this.setData({
  414. 'formData.saleId':saleId, //销售方id
  415. 'formData.saleIdNumber':identityNumber, //销售方身份证
  416. 'formData.saleName':name, //销售方信息
  417. 'formData.salePhoneNumber':phoneNumber, //销售方电话
  418. },()=>{
  419. this.validateForm()
  420. })
  421. }
  422. wx.removeStorageSync('saleInfo')
  423. },
  424. /**
  425. * 触发发票抬头列表input
  426. * */
  427. changeInputInvoiceBuy(e){
  428. const {value}=e.detail;
  429. let {searchTimer}=this.data;
  430. this.setData({
  431. 'formData.buyName':value
  432. },()=>{
  433. clearInterval(searchTimer)
  434. searchTimer=setTimeout(()=>{
  435. this.getInvoiceBuy(value);
  436. },500);
  437. this.setData({
  438. searchTimer:searchTimer
  439. })
  440. })
  441. },
  442. setInvoiceBuyValue(e){
  443. const {name,code}=e.currentTarget.dataset;
  444. console.info(name)
  445. console.info(code)
  446. // todo: 税号有时带出不出来 先加一个延时测试一下
  447. setTimeout(()=>{
  448. this.setData({
  449. //invoiceBuySearchValue:value,
  450. 'formData.buyName':name,
  451. 'formData.buyIdNumber':code
  452. },()=>{
  453. this.validateForm()
  454. });
  455. },30)
  456. },
  457. showInvoiceBuyList() {
  458. this.setData({
  459. isShowInvoiceBuyList:true,
  460. invoiceBuyFocus:true
  461. });
  462. },
  463. hideInvoiceBuyList() {
  464. this.setData({
  465. isShowInvoiceBuyList:false,
  466. invoiceBuyFocus:false
  467. },()=>{
  468. this.validateForm()
  469. });
  470. },
  471. /**
  472. * 获取发票抬头列表
  473. * **/
  474. getInvoiceBuy(name){
  475. app.post(api.queryInvoiceBuy,{
  476. buyLike:name, //购买方纳税人识别号
  477. }).then(res=>{
  478. this.setData({
  479. invoiceBuyList:res.data
  480. });
  481. })
  482. },
  483. /**
  484. * 返回上一级
  485. */
  486. back(){
  487. wx.navigateBack();
  488. },
  489. /**
  490. * 生命周期函数--监听页面隐藏
  491. */
  492. onHide: function () {
  493. },
  494. /**
  495. * 生命周期函数--监听页面卸载
  496. */
  497. onUnload: function () {
  498. },
  499. /**
  500. * 页面相关事件处理函数--监听用户下拉动作
  501. */
  502. onPullDownRefresh: function () {
  503. },
  504. /**
  505. * 页面上拉触底事件的处理函数
  506. */
  507. onReachBottom: function () {
  508. },
  509. /**
  510. * 用户点击右上角分享
  511. */
  512. onShareAppMessage: function () {
  513. }
  514. })