invoice-saveinfo.js 7.6 KB
Newer Older
D
Devil 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
const app = getApp();
Page({
  data: {
    params: null,
    form_submit_loading: false,
    data_list_loding_status: 1,
    data_list_loding_msg: '',
    data_bottom_line_status: false,
    data_base: null,
    apply_type_list: null,
    can_invoice_type_list: null,
    invoice_content_list: null,
    save_base_data: null,
    data: null,

    form_invoice_type_index: 0,
    form_apply_type_index: 0,
    form_apply_type_disabled: false,
    company_container: false,
    company_special_container: false,
    addressee_container: false,
    email_container: true,
  },

  onLoad(params) {
    this.setData({ params: params });
    this.init();
  },

  onShow() {},

  init() {
    var self = this;
    wx.showLoading({ title: "加载中..." });
    this.setData({
      data_list_loding_status: 1
    });

    wx.request({
      url: app.get_request_url("saveinfo", "user", "invoice"),
      method: "POST",
      data: this.data.params,
      dataType: "json",
      success: res => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code == 0) {
          var data = res.data.data;
          self.setData({
            data_base: data.base || null,
            apply_type_list: data.apply_type_list || null,
            can_invoice_type_list: data.can_invoice_type_list || null,
            invoice_content_list: data.invoice_content_list || null,
            save_base_data: data.save_base_data,
            data: ((data.data || null) == null || data.data.length == 0) ? null : data.data,
            data_list_loding_status: 0,
            data_bottom_line_status: true,
            data_list_loding_msg: (data.save_base_data.total_price <= 0) ? '发票金额必须大于0' : '',
          });
        } else {
          self.setData({
            data_list_loding_status: 2,
            data_bottom_line_status: false,
            data_list_loding_msg: res.data.msg,
          });
          if (app.is_login_check(res.data, self, 'init')) {
            app.showToast(res.data.msg);
          }
        }
      },
      fail: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        self.setData({
          data_list_loding_status: 2,
          data_bottom_line_status: false,
          data_list_loding_msg: '服务器请求出错',
        });

        app.showToast("服务器请求出错");
      }
    });
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.init();
  },

  // 发票类型事件
  form_invoice_type_event(e) {
    this.setData({
      form_invoice_type_index: e.detail.value
    });
    this.invoice_container_handle();
  },

  // 发票类型事件
  form_apply_type_event(e) {
    this.setData({
      form_apply_type_index: e.detail.value
    });
    this.invoice_container_handle();
  },

  // 容器显隐处理
  invoice_container_handle() {
    // 发票类型
    var invoice_type = this.data.can_invoice_type_list[this.data.form_invoice_type_index]['id'];
    if(invoice_type == 2)
    {
      // 选择专票的时候申请类型必须是企业
      this.setData({
        form_apply_type_index: 1,
        form_apply_type_disabled: true,
      });
    } else {
      this.setData({
        form_apply_type_disabled: false,
      });
    }

    // 申请类型
    switch(invoice_type)
    {
      // 增值税普通电子发票
      case 0 :
        this.setData({
          company_special_container: false,
          addressee_container: false,
          email_container: true,
        });
        break;

      // 增值税普通纸质发票
      case 1 :
        this.setData({
          company_special_container: false,
          addressee_container: true,
          email_container: false,
        });
        break;

      // 增值税专业纸质发票
      case 2 :
        this.setData({
          company_container: true,
          company_special_container: true,
          addressee_container: true,
          email_container: false,
        });
        break;
    }

    // 增值税专业纸质发票情况下个人类型处理
    if(invoice_type != 2)
    {
      var apply_type = this.data.apply_type_list[this.data.form_apply_type_index]['id'];
      if(apply_type == 0)
      {
        this.setData({
          company_container: false,
        });
      } else {
        this.setData({
          company_container: true,
        });
      }
    }
  },

  // 表单提交
  formSubmit(e)
  {
    var data = e.detail.value;
    if((this.data.data || null) == null)
    {
      data['ids'] = this.data.params.ids || '';
      data['type'] = this.data.params.type || '';
    } else {
      data['id'] = this.data.data.id;
    }

    // 数据验证
    var validation = [
      {fields: 'invoice_type', msg: '请选择发票类型', is_can_zero: 1},
      {fields: 'apply_type', msg: '请选择申请类型', is_can_zero: 1},
      {fields: 'invoice_title', msg: '请填写发票抬头、最多200个字符'}
    ];
    if(app.fields_check(data, validation))
    {
      var invoice_type = this.data.can_invoice_type_list[this.data.form_invoice_type_index]['id'];
      var apply_type = this.data.apply_type_list[this.data.form_apply_type_index]['id'];
      if(apply_type == 1)
      {
        validation.push({fields: 'invoice_code', msg: '请填写企业统一社会信用代码或纳税识别号、最多160个字符'});
      }
      if(invoice_type == 2)
      {
        validation.push({fields: 'invoice_bank', msg: '请填写企业开户行名称、最多200个字符'});
        validation.push({fields: 'invoice_account', msg: '请填写企业开户帐号、最多160个字符'});
        validation.push({fields: 'invoice_tel', msg: '请填写企业联系电话 6~15 个字符'});
        validation.push({fields: 'invoice_address', msg: '请填写企业注册地址、最多230个字符'});
      }
      if(invoice_type != 0)
      {
        validation.push({fields: 'name', msg: '请填写收件人姓名格式 2~30 个字符之间'});
        validation.push({fields: 'tel', msg: '请填写收件人电话 6~15 个字符'});
        validation.push({fields: 'address', msg: '请填写收件人地址、最多230个字符'});
      }
      if(app.fields_check(data, validation))
      {
        wx.showLoading({title: '提交中...'});
        this.setData({form_submit_loading: true});

        // 网络请求
        var self = this;
        wx.request({
          url: app.get_request_url("save", "user", "invoice"),
          method: 'POST',
          data: data,
          dataType: 'json',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          success: (res) => {
            wx.hideLoading();

            if(res.data.code == 0)
            {
              app.showToast(res.data.msg, "success");
              setTimeout(function()
              {
                // 是否关闭页面进入我的发票、适合从订单开票中过来提交成功直接进入我的发票列表
                if((self.data.params || null) != null && (self.data.params.is_redirect || 0) == 1)
                {
                  wx.redirectTo({
                    url: "/pages/plugins/invoice/invoice/invoice"
                  });
                } else {
                  wx:wx.navigateBack();
                }
              }, 2000);
            } else {
              this.setData({form_submit_loading: false});
              if (app.is_login_check(res.data)) {
                app.showToast(res.data.msg);
              } else {
                app.showToast('提交失败,请重试!');
              }
            }
          },
          fail: () => {
            wx.hideLoading();
            this.setData({form_submit_loading: false});
            app.showToast('服务器请求出错');
          }
        });
      }
    }
  },
});