invoice.js 2.43 KB
import Yas from '../../../common/yas';

let yas;

Page({
  data: {
    invocesType: 2
  },
  onLoad(options) {
    if (!options.skey) {
      return;
    }

    this.tryToDo(() => {
      let invoice = wx.getStorageSync(options.skey) || {};

      this.setData(invoice);
      this.defaultReceiver = invoice.receiver;
    });

    this.tryToDo(() => {
      wx.removeStorage({key: options.skey});
    });

    yas = new Yas();

    yas.pageOpenReport();
  },
  tryToDo(fn) {
    try {
      fn();
    } catch (e) {
      console.log(JSON.stringify(e));
    }
  },
  validateInvoceInfo() {
    const info = this.data || {};
    const receiver = this.defaultReceiver;
    let isPass = true;

    if (info.invoiceCompany && (!info.invoiceTitle || !info.taxNumber)) {
      isPass = false;
    }

    if (isPass && info.invocesType * 1 === 2) {
      if (!info.receiver) {
        isPass = false;
      } else if (info.receiver !== receiver && !/^[0-9]{11,18}$/.test(info.receiver)) {
        isPass = false;
      }
    }

    if (isPass !== this.data.canSubmit) {
      this.setData({canSubmit: isPass});
    }

    return isPass;
  },
  showInvoceNotice() {
    wx.showModal({
      title: '发票须知',
            content: '1、发票金额不含优惠券/有货币/红包/优惠券/优惠码/运费,只包含商品实付金额;\r\n2、电子发票是税务局认可的、有效的收付款凭证,其法律效力、基本用途及使用规则同纸质发票,如需纸质发票可自行下载打印;', // eslint-disable-line
      confirmText: '我知道了',
      showCancel: false
    });
  },
  changeTitleType(e) {
    let isCompany = e.currentTarget.dataset.type === 'company';

    if (isCompany !== this.data.invoiceCompany) {
      this.setData({invoiceCompany: isCompany});
      this.validateInvoceInfo();
    }
  },
  changeTitle(e) {
    this.data.invoiceTitle = e.detail.value;
    this.validateInvoceInfo();
  },
  changeTaxNumber(e) {
    this.data.taxNumber = e.detail.value;
    this.validateInvoceInfo();
  },
  changeReceiver(e) {
    this.data.receiver = e.detail.value;
    this.validateInvoceInfo();
  },
  invoiceSubmit() {
    if (!this.validateInvoceInfo()) {
      return;
    }

    let currentPages = getCurrentPages();

    if (currentPages.length > 1 && currentPages[currentPages.length - 2].changeInvoiceCallback) {
      currentPages[currentPages.length - 2].changeInvoiceCallback(this.data);
    }

    wx.navigateBack({delta: 1});
  }
});