let isSC2013 = true;

function overwriteRationCalcBases(taxType) {
  if (typeof rationCalcBases == "undefined") return;
  for (let key in rationCalcBases) delete rationCalcBases[key];

  rationCalcBases["人工费"] = function (node, isTender) {
    return calcTools.rationBaseFee(node, [gljType.LABOUR], priceTypes.ptMarketPrice, isTender);
  };
  rationCalcBases["材料费"] = function (node, isTender) {
    return calcTools.rationBaseFee(node, baseMaterialTypes, priceTypes.ptMarketPrice, isTender);
  };
  rationCalcBases["施工机械使用费"] = function (node, isTender) {
    return calcTools.rationBaseFee(node, baseMachineTypes, priceTypes.ptMarketPrice, isTender);
  };
  rationCalcBases["商品砼费"] = function (node, isTender) {
    return calcTools.rationBaseFee(node, [gljType.COMMERCIAL_CONCRETE, gljType.COMMERCIAL_MORTAR], priceTypes.ptMarketPrice, isTender);
  };
  rationCalcBases["外购砼构件费"] = function (node, isTender) {
    return calcTools.rationBaseFee(node, [gljType.PURCHASE_COMPONENT], priceTypes.ptMarketPrice, isTender);
  };
}

(function overwriteFeeTypes() {
  if (typeof cpFeeTypes == "undefined") return;
  cpFeeTypes = [
    { type: "marketDirect", name: "直接费" },
    { type: "marketDirectWork", name: "直接工程费" },
    { type: "marketLabour", name: "人工费" },
    { type: "marketMaterial", name: "材料费" },
    { type: "marketMachine", name: "施工机械使用费" },
    { type: "otherFee", name: "其他工程费" },
    { type: "otherFee1", name: "其他工程费I" },
    { type: "otherFee2", name: "其他工程费II" },
    { type: "indirect", name: "间接费" },
    { type: "manage", name: "企业管理费" },
    { type: "base", name: "基本费用" },
    { type: "force", name: "规费" },
    { type: "profit", name: "利润" },
    { type: "tax", name: "税金" },
    { type: "common", name: "建安费" },
  ];
})();

if (typeof gljUtil !== "undefined") {
  gljUtil.getAssUsedPrice = function (g) {
    //四川辅助生产人工费取市场价
    return g.marketPrice;
  };
  gljUtil.getElecCode = function () {
    return "865";
  };
}

// 清单基数
const progression = ["养护单位管理费"];
const deficiency = {};
if (typeof baseFigureMap !== "undefined") {
  const { fixedFlag } = commonConstants;
  const boqMap = {
    // 显示：除清单固定类别是“公路养护工程费”的以外部分可显示
    公路养护工程费: {
      base: "GLYHGCF",
      fixedFlag: fixedFlag.CONSTRUCTION_INSTALL_FEE,
      filter: [fixedFlag.CONSTRUCTION_INSTALL_FEE],
      pick: false,
    },
    "养护工程费(不含保险费、施工环保水保费、安全生产费)": {
      base: "YHGCFBHBXHBSBAQ",
      fixedFlag: fixedFlag.CONSTRUCTION_INSTALL_FEE,
      filter: [fixedFlag.CONSTRUCTION_INSTALL_FEE],
      pick: false,
    },
    // 显示：仅清单固定类别是“养护工程其他费用”部分可显示。
    养护单位管理费: {
      isProgressive: true,
      base: "YHDWGLF",
      fixedFlag: null,
      filter: [fixedFlag.MAINTENANCE_EXPENSES],
      pick: true,
    },
    // {一、二、三部分合计} 只显示在 预备费及其子项下
    "一、二、三部分合计": {
      base: "YESBFHJ",
      fixedFlag: null,
      filter: [fixedFlag.BUDGET_FEE],
      pick: true,
    },
    // 显示：仅“价差预备费”可显示
    价差预备费: {
      base: "JCYBF",
      fixedFlag: null,
      filter: [fixedFlag.SPREAD_BUDGET_FEE],
      pick: true,
    },
    预算和标底审核费: {
      base: "YSHBDSHF",
      fixedFlag: null,
      filter: [fixedFlag.MAINTENANCE_EXPENSES],
      pick: true,
    },
    独立桥隧及地质病害工程养护工程设计费: {
      base: "DLQSJDZBHGCYHGCSJF",
      fixedFlag: null,
      filter: [fixedFlag.MAINTENANCE_EXPENSES],
      pick: true,
    },
    路基路面及其他工程养护工程设计费: {
      base: "LJLMJQTGCYHGCSJF",
      fixedFlag: null,
      filter: [fixedFlag.MAINTENANCE_EXPENSES],
      pick: true,
    },
    预算标底编制费: {
      base: "YSBDBZF",
      fixedFlag: null,
      filter: [fixedFlag.MAINTENANCE_EXPENSES],
      pick: true,
    },
    //四川2013读的是 建筑安装工程费行的值
    各章清单合计: {
      base: "GZQDHJ",
      fixedFlag: fixedFlag.CONSTRUCTION_INSTALL_FEE,
      filter: [fixedFlag.CONSTRUCTION_INSTALL_FEE],
      pick: false,
    },
    // 清单固定行[第100章至700章清单]下的[第100章清单]需要允许清单可使用基数{100章以外合计},因此{100章以外合计}不设置关联的清单固定行
    //仅允许用于固定类别为“100章清单”引用
    "100章以外清单合计": {
      base: "YBZYHQDHJ",
      fixedFlag: null,
      filter: [fixedFlag.ONE_HUNDRED_BILLS],
      pick: true,
    },
  };
  baseFigureMap.boq = boqMap;
}

if (typeof baseFigureTemplate !== "undefined") {
  const { fixedFlag } = commonConstants;
  baseFigureTemplate.boq = {
    // 公路养护工程费 算法：取清单固定类别是“建筑安装工程费”的金额。
    GLYHGCF(tender) {
      return cbTools.getBaseFee(fixedFlag.CONSTRUCTION_INSTALL_FEE, tender, "common");
    },
    // 养护工程费(不含保险费、施工环保水保费、安全生产费
    YHGCFBHBXHBSBAQ(tender) {
      const baseFee = cbTools.getBaseFee(fixedFlag.CONSTRUCTION_INSTALL_FEE, tender, "common");
      const fee1 = cbTools.getBaseFee(fixedFlag.INSURANCE_FEE, tender, "common");
      const fee2 = cbTools.getBaseFee(fixedFlag.CONSTRUCTION_ENVIROMENTAL_PROTECTION_FEE, tender, "common");
      const fee3 = cbTools.getBaseFee(fixedFlag.EP_WP_INSURANCE_FEE, tender, "common");
      const fee4 = cbTools.getBaseFee(fixedFlag.SAFE_COST, tender, "common");
      return (baseFee - fee1 - fee2 - fee3 - fee4).toDecimal(decimalObj.bills.totalPrice);
    },
    // 养护单位管理费 算法：以{公路养护工程费总额}为基数，采用累进办法计算
    YHDWGLF(tender) {
      const baseFee = this["GLYHGCF"](tender);
      if (!tender) {
        calcBase.baseProgressiveFee = baseFee;
      }
      return calculateUtil.getProgressiveFee(
        baseFee,
        "养护单位管理费",
        projectObj.project.property.progressiveInterval,
        decimalObj.bills.totalPrice,
        deficiency
      );
    },
    // 一二三部分合计 算法：取清单固定类别是“一二三部分合计”的金额
    YESBFHJ(tender) {
      return cbTools.getBaseFee(fixedFlag.ONE_TO_THREE_TOTAL, tender, "common");
    },
    /*  价差预备费 算法：以建筑安装工程费为基数，按设计文件编制年始至养护项目工程竣工年终的年数和年工程造价增涨率计算。
          价差预备费 P * [(1+i)^(n-1) -1]
          P——建筑安装工程费总额（元）；
          i——年工程造价增涨率（%）；
          n——设计文件编制年至养护项目开工年+养护项目建设期限（年）。
      */
    JCYBF(tender) {
      //公路养护工程费（即建筑安装工程费）作为基数
      const installFee = this["GLYHGCF"](tender);
      //年造价增涨
      const costGrowthRate = calcBase.project.property.costGrowthRate ? calcBase.project.property.costGrowthRate : 0;
      //增涨计费年限
      const growthPeriod = projectObj.project.property.growthPeriod ? calcBase.project.property.growthPeriod : 0;
      //= P * [(1+i)^(n-1) -1]
      return (installFee * (Math.pow(1 + costGrowthRate, growthPeriod - 1) - 1)).toDecimal(decimalObj.bills.totalPrice);
    },
    // 预算和标底审核费
    YSHBDSHF(tender) {
      const baseFee = this["GLYHGCF"](tender);
      if (!tender) {
        calcBase.baseProgressiveFee = baseFee;
      }
      return calculateUtil.getProgressiveFee(
        baseFee,
        "预算和标底审核费",
        projectObj.project.property.progressiveInterval,
        decimalObj.bills.totalPrice,
        deficiency
      );
    },
    // 独立桥隧及地质病害工程养护工程设计费
    DLQSJDZBHGCYHGCSJF(tender) {
      const baseFee = this["GLYHGCF"](tender);
      if (!tender) {
        calcBase.baseProgressiveFee = baseFee;
      }
      return calculateUtil.getProgressiveFee(
        baseFee,
        "独立桥隧及地质病害工程养护工程设计费",
        projectObj.project.property.progressiveInterval,
        decimalObj.bills.totalPrice,
        deficiency
      );
    },
    // 路基路面及其他工程养护工程设计费
    LJLMJQTGCYHGCSJF(tender) {
      const baseFee = this["GLYHGCF"](tender);
      if (!tender) {
        calcBase.baseProgressiveFee = baseFee;
      }
      return calculateUtil.getProgressiveFee(
        baseFee,
        "路基路面及其他工程养护工程设计费",
        projectObj.project.property.progressiveInterval,
        decimalObj.bills.totalPrice,
        deficiency
      );
    },
    // 预算标底编制费
    YSBDBZF(tender) {
      const baseFee = this["GLYHGCF"](tender);
      if (!tender) {
        calcBase.baseProgressiveFee = baseFee;
      }
      return calculateUtil.getProgressiveFee(
        baseFee,
        "预算标底编制费",
        projectObj.project.property.progressiveInterval,
        decimalObj.bills.totalPrice,
        deficiency
      );
    },
    //{各章清单合计}
    // 四川2013读的是 建筑安装工程费行的值
    GZQDHJ: function (tender) {
      return cbTools.getBaseFee(fixedFlag.CONSTRUCTION_INSTALL_FEE, tender, "common");
    },
    //{100章以外清单合计} 取清单固定清单[第100章至700章清单]的金额，但扣除清单100章下的金额。如果是固定清单[第100章至700章清单]下100章以外清单引用此基数，要排除自身(目前只允许100章的清单使用，所以暂时不需要此判断)
    YBZYHQDHJ: function (tender) {
      // let oneToSeven = cbTools.findNodeByFlag(fixedFlag.ONE_SEVEN_BILLS); // 少了清单“第100章至700章清单”, 直接取“第一部分 公路养护工程费”
      let oneToSeven = cbTools.findNodeByFlag(fixedFlag.CONSTRUCTION_INSTALL_FEE);
      if (!oneToSeven) {
        return 0;
      }
      //100-700章固定节点的所有子节点
      let allChildren = [];
      function getChildren(nodes) {
        allChildren = allChildren.concat(nodes);
        for (let node of nodes) {
          if (node.children.length > 0) {
            getChildren(node.children);
          }
        }
      }
      getChildren(oneToSeven.children);
      //扣除的节点：100章的节点[100-200)
      let deductNodes = allChildren.filter(cbTools.withingOneHundred);
      //计算金额
      let fullFeeField = tender ? "common.tenderTotalFee" : "common.totalFee";
      return projectObj.project.calcProgram.getTotalFee([oneToSeven], deductNodes, fullFeeField).toDecimal(decimalObj.bills.totalPrice);
    },
  };
}

if (typeof feeRateObject !== "undefined") {
  feeRateObject.feeRateSpecialHandle = function (subRate, value) {
    let result = {};
    if (subRate.name == "工地转移(km)" && value && value < 50) {
      //工地转移50km以内按不计算
      result.valueKey = "不计";
      result.value = scMathUtil.roundForObj(value, getDecimal("feeRate")); //设置显示的节点值
    }
    return result;
  };
}

if (typeof module !== "undefined") {
  const defaultDecimal = {
    bills: { unitPrice: 2, totalPrice: 0 },
    ration: { quantity: 3, unitPrice: 2, totalPrice: 0 },
    glj: { quantity: 3, unitPriceHasMix: 2, unitPrice: 2 },
    feeRate: 2,
    quantity_detail: 4,
    material: 5, //三材系数
    process: 6,
    marketPriceProcess: 2,
    temProcess: 6,
  };
  const defaultCalcMode = 1;
  module.exports = {
    getDefalutAssistProductionFeeRate: function () {
      return 5;
    },
    progression,
    deficiency,
    defaultCalcMode,
    defaultDecimal,
  };
}
