var system = require("../../../system")
const CtlBase = require("../../ctlms.base");
const validation = system.getObject("util.validation");

class InvoiceCtl extends CtlBase {
  constructor() {
    super();
    this.invoiceSve = system.getObject("service.saas.invoiceSve");
    this.tradeSve = system.getObject("service.trade.tradeSve");
  }

  // 审核
  async audit(params, pobj2, req) {
    try {
      params.id = this.trim(params.id);
      if (!params.id) {
        return system.getResult(null, "发票申请不存在");
      }
      params.remark = this.trim(params.remark);
      if (!params.remark) {
        return system.getResult(null, "请填写审核备注");
      }
      let rs = await this.invoiceSve.audit(params)
      if (rs.status === 0 && !params.status) {
        await this.tradeSve.cancelInvoice({saas_invoice_id: params.id});
      }
      return rs;
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  // 发票开具
  // 审核
  async makeout(params, pobj2, req) {
    try {
      validation.check(params, 'invoice_no', {name: "发票代码", is_require: true});
      validation.check(params, 'invoice_number', {name: "发票号码", is_require: true});
      validation.check(params, 'invoice_time', {name: "开票时间", is_require: true});
      validation.check(params, 'mail_no', {name: "快递单号", is_require: true});
      validation.check(params, 'invoice_img', {name: "发票图片", is_require: true});
      let rs = await this.invoiceSve.makeout(params);
      return rs;
    } catch (error) {
      let msg = error.message;
      if (msg.startsWith("bpo-validation-error:")) {
        return system.getResult(null, msg.replace("bpo-validation-error:", ""));
      }
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }
  async platformTitleAddr(params, pobj2, req) {
    try {
      let id = params.id;
      this.invoiceSve.byid


      let result = {};

      //
      result.title = {"merchant_name": "舟山兰和有限公司", "merchant_credit_code": "KHSDLKFJAFJ", "merchant_tax_type": "00", "merchant_addr": "河南信阳", "merchant_mobile": "18833836395", "merchant_bank": "北京银行栓秀支行", "merchant_account": "zhousanlanhe"};
      result.addr = {"mail_addr": "北京朝阳区国创元", "mail_mobile": "010-4525821-44", "mail_to": "张娇哒哒哒",};
      return system.getResultSuccess(result);
    } catch (error) {
      let msg = error.message;
      if (msg.startsWith("bpo-validation-error:")) {
        return system.getResult(null, msg.replace("bpo-validation-error:", ""));
      }
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async platformInvoiceInfo(params, pobj2, req) {
    try {
      let result = {};
      result.tax = {"additional_tax_total": 5000, "personal_invoice_tax_total": 5000, "value_added_tax_total": 700};
      result.invoiceList = [{"name": "舟山兰和有限公司", "credit_code": "KHSDLKFJAFJ", "is_bank": 0, "invoice_amount": 1000000, "personal_invoice_tax": 3000, "additional_tax": 5000, "service_tax": 5000, "value_added_tax": 700, "unit": "", "quantity": 0, "price": 0, "remark": ""}, {"name": "舟山兰和有限公司", "credit_code": "KHSDLKFJAFJ", "is_bank": 0, "invoice_amount": 1000000, "personal_invoice_tax": 3000, "additional_tax": 5000, "service_tax": 5000, "value_added_tax": 700, "unit": "", "quantity": 0, "price": 0, "remark": ""}];
      return system.getResultSuccess(result);
    } catch (error) {
      let msg = error.message;
      if (msg.startsWith("bpo-validation-error:")) {
        return system.getResult(null, msg.replace("bpo-validation-error:", ""));
      }
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async platformInvoiceApply(params, pobj2, req) {
    try {
      return system.getResultSuccess();
    } catch (error) {
      let msg = error.message;
      if (msg.startsWith("bpo-validation-error:")) {
        return system.getResult(null, msg.replace("bpo-validation-error:", ""));
      }
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  //发票列表
  async invoicePage(params, pobj2, req) {
    try {
      let rs = await this.invoiceSve.platforminvoicePage(params);
      return rs;
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  //发票申请列表
  async applyPage(params, pobj2, req) {
    try {
      params.owner_type = "00";
      let rs = await this.invoiceSve.platforminvoiceapplyPage(params);
      return rs;
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  //发票申请列表
  async platformApplyPage(params, pobj2, req) {
    try {
      params.owner_type = "10";
      let rs = await this.invoiceSve.platforminvoiceapplyPage(params);
      return rs;
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  // 审核
  async platformAudit(params, pobj2, req) {
    try {
      let id = this.trim(params.id);
      if (!id) {
        return system.getResult(null, "发票申请不存在");
      }
      let status = params.status || 0;
      let remark = this.trim(params.remark);
      if (!remark) {
        return system.getResult(null, "请填写审核备注");
      }
      return system.getResultSuccess();
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }


}

module.exports = InvoiceCtl;
