var system = require("../../../system")
var settings = require("../../../../config/settings");
const CtlBase = require("../../ctlms.base");
var cacheBaseComp = null;
class InvoiceCtl extends CtlBase {
  constructor() {
    super();
    this.invoiceSve = system.getObject("service.invoice.invoiceSve");
  }

  //获取所有发票信息 (条件分页)
  async invoices(pobj, pobj2, req){
    try {
      pobj.uid = req.loginUser.ucid;
      return await this.invoiceSve.invoices(pobj);
    } catch (error) {
      return system.getResultFail(500, "接口错误");
    }
  }

  //创建发票（批量创建）
  async create(pobj, pobj2, req){
    try {
      var invoices = pobj.invoices || [];
      if(invoices.length == 0) {
        return system.getResult(null, "请至少填写一条发票信息");
      }
      var list = [];
      for(var invoice of invoices) {
        var obj = {};
        obj.merchantId = this.trim(invoice.merchant_id);
        obj.uid = req.loginUser.ucid;

        obj.type = this.trim(invoice.type);
        obj.taxNo = this.trim(invoice.taxNo);
        obj.purchaserName = this.trim(invoice.purchaserName);
        obj.purPhoneAddr = this.trim(invoice.purPhoneAddr);
        obj.purBankAcc = this.trim(invoice.purBankAcc);

        obj.amount = this.trim(invoice.amount);
        obj.statementsUrl = this.trim(invoice.statementsUrl);
        obj.contractUrl = this.trim(invoice.contractUrl);

        obj.mailAddr = this.trim(invoice.mailAddr);
        obj.mailTo = this.trim(invoice.mailTo);
        obj.mailMobile = this.trim(invoice.mailMobile);
        obj.isPay = 0;
        obj.status = 1;

        obj.merchantCode = this.trim(invoice.creditCode);
        obj.merchantName = this.trim(invoice.name );
        obj.corporations = this.trim(invoice.legalName);
        obj.identityCode = this.trim(invoice.legalIdcard);
        obj.bank = invoice.isBank ? 1 : 0;

        list.push(obj);
      }
      return await this.invoiceSve.create(list);
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, "接口错误");
    }
  }

  //更新发票
  async update(params){
    try {
      return await this.invoiceSve.update(params);
    } catch (error) {
      return system.getResultFail(500, "接口错误");
    }
  }
}
module.exports = InvoiceCtl;