const system = require("../../../system");
const ServiceBase = require("../../svems.base")
const settings = require("../../../../config/settings")

class SaasinvoiceService extends ServiceBase {
  constructor() {
    super();
    this.DEFAULT_BUSINESSMEN_TYPE = "10";   //默认的 销售方类型  个体工商户
    this.DEFAULT_CHANNEL_ID = "2";          //默认的 渠道类型    
  }

  async platforminvoicePage(params) {
    let rs = await this.callms("invoice", "platforminvoicePage", params);
    if (rs && rs.data && rs.data.rows) {
      this.transOrderField(rs.data.rows);
    }
    return rs;
  }


  async platforminvoiceapplyPage(params) {
    let rs = await this.callms("invoice", "platforminvoiceapplyPage", params);
    if (rs && rs.data && rs.data.rows) {
      this.transOrderField(rs.data.rows);
    }
    return rs;
  }

  async audit(params) {
    let rs = await this.callms("invoice", "invoiceapplyAudit", params);
    return rs;
  }

  async makeout(params) {
    let rs = await this.callms("invoice", "invoiceapplyMakeOut", params);
    return rs;
  }

  async invoiceapplyById(params) {
    let rs = await this.callms("invoice", "invoiceapplyById", params);
    return rs;
  }

  async applyBulkSave(params) {
    let rs = await this.callms("invoice", "invoiceapplyBulkSave", params);
    return rs;
  }

  transOrderField(rows) {
    if (!rows || rows.length == 0) {
      return;
    }

    for (let row of rows) {
      row.price = system.f2y(row.price);
    }
  }

  async invoiceapplyStatus(params) {
    let rs = await this.callms("invoice", "invoiceapplyStatus", params);
    return rs;
  }

  async platformHandle(params) {
    let apply = await this.invoiceapplyById(params);
    apply = apply.data;
    if (!apply) {
      return system.getResult(null, "发票申请不存在");
    }
    if (apply.status != "1010") {
      return system.getResult(null, "发票状态错误，请刷新重试");
    }
    // TODO 总统 推送发票信息 begin
    await this.pushInvoiceToDeliver(apply);
    // TODO 总统 推送发票信息 end

    let updateStatus = {
      id: apply.id,
      preStatus: "1010",
      status: "1050",
    };
    let rs = await this.invoiceapplyStatus(updateStatus);
    return rs;
  }

  /**
   * fn：发票推送
   * @param {*} params 
   */
  async saveSaasInvoice(params) {
    var rs = await this.callms("invoice", "saveSaasInvoice", params);
    return rs;
  }

  /**
   * fn:发票申请推送到交付系统
   * @param {*} invoice   发票实体对象
   * 
   *  1. 开票方类型  不存在 （目前默认是个体工商户）
   *  2. 开票内容    不存在
   *  3. 核对channel ID 是不是SAAS_ID
   *  4. 申请时间    不存在
   */
  async pushInvoiceToDeliver(invoice) {
    try {
      let params = {
        "businessmen_type": this.DEFAULT_BUSINESSMEN_TYPE,
        "from_id": invoice.from_id || "",
        "from_credit_code": invoice.from_credit_code,
        "from_name": invoice.from_name,
        "from_addr": invoice.from_addr,
        "from_mobile": invoice.from_mobile,
        "from_bank": invoice.from_bank || "",
        "from_account": invoice.from_account || "",
        "mail_to": invoice.mail_to,
        "mail_mobile": invoice.mail_mobile,
        "mail_addr": invoice.mail_addr,
        "to_id": invoice.to_id || "",
        "to_name": invoice.to_name,
        "to_credit_code": invoice.to_credit_code,
        "to_addr": invoice.to_addr,
        "to_mobile": invoice.to_mobile,
        "to_bank": invoice.to_bank,
        "to_account": invoice.to_account,
        "apply_no": invoice.id,
        "invoice_type": invoice.invoice_type,
        "apply_time": invoice.apply_time,
        "invoice_amount": invoice.invoice_amount,
        "invoice_content": invoice_content || "",
        "personal_invoice_tax": invoice.personal_invoice_tax,
        "additional_tax": invoice.additional_tax,
        "value_added_tax": invoice.value_added_tax,
        "channel_id": this.DEFAULT_CHANNEL_ID
      }
      let rs = await this.callms("invoice", "invoiceApply", params);
      return rs;
    } catch (error) {
      console.log(error);
      return system.getResult(null, `系统错误`);
    }
  }
}
module.exports = SaasinvoiceService;