'use strict';

/**
 *
 *
 * @author Zhong
 * @date 2019/8/15
 * @version
 */
let isGD2018 = true;

//允许使用的工料机类型：人工、普通材料、其他材料费、混凝土、砂浆、配合比、商品混凝土、商品砂浆
//机械台班、机上人工、机械组成物、主材、设备、企业管理费
if(typeof allowGljType !== 'undefined'){
    allowGljType = [1, 201, 202, 203, 204, 205, 206, 207, 301, 302, 303, 4,5, 6];
}
if(typeof allowComponent !== 'undefined'){
    //允许含有组成物的工料机类型：混凝土、砂浆、配合比、机械台班、主材
    allowComponent = [202, 203, 204, 301, 4];
}
if(typeof componentType !== 'undefined'){
    //可以作为组成物的工料机类型：普通材料、机械组成物、机上人工、主材
    componentType = [201, 302, 303, 4];
}
if(typeof machineAllowComponent !== 'undefined'){
    //允许含有组成物的机械工料机类型：机械台班器仪表
    machineAllowComponent = [301];
}
if(typeof machineComponent !== 'undefined'){
    //可以作为机械工料机组成物的工料机类型：机械组成物、机上人工
    machineComponent = [302, 303];
}
if(typeof materialAllowComponent !== 'undefined'){
    //允许含有组成物的材料工料机类型：混凝土、砂浆、配合比
    materialAllowComponent = [202, 203, 204];
}
if(typeof materialComponent !== 'undefined'){
    //可以作为材料工料机组成物的工料机类型：普通材料
    materialComponent = [201];
}

function overwriteRationCalcBases (){
    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.MAIN_MATERIAL], priceTypes.ptMarketPrice, isTender);
    };
    rationCalcBases['设备费'] = function (node, isTender) {
        return calcTools.rationBaseFee(node, [gljType.EQUIPMENT], priceTypes.ptMarketPrice, isTender);
    };
    // 广东建筑管理费特殊计算：管理费=ROUND(∑(人工费+施工机具费)*定额的管理费费率*0.01，精度)。与定额同级的量价、人材机则按管理费费率为0计算。
    rationCalcBases['管理费'] = function (node, isTender) {
        let rst = 0;
        if (calcTools.isCalcManageRation(node)  && node.data.gljList && node.data.manageFeeRate) {
            rst = (rationCalcBases['人工费'](node, isTender) + rationCalcBases['机具费'](node, isTender)) * node.data.manageFeeRate * 0.01;
            rst = rst.toDecimal(decimalObj.ration.unitPrice);
        };
        return rst;
    };
    rationCalcBases['工日合计'] = function (node, isTender) {
        return calcTools.labourDays(node, isTender);
    };
    rationCalcBases['甲供人工费'] = function (node, isTender) {
        return calcTools.partASupplyFee(node, '甲供人工费', isTender, false);
    };
    rationCalcBases['甲供材料费'] = function (node, isTender) {
        return calcTools.partASupplyFee(node, '甲供材料费', isTender, false);
    };
    rationCalcBases['甲供机具费'] = function (node, isTender) {
        return calcTools.partASupplyFee(node, '甲供机械费', isTender, false);
    };
    rationCalcBases['甲供主材费'] = function (node, isTender) {
        return calcTools.partASupplyFee(node, '甲供主材费', isTender, false);
    };
    rationCalcBases['甲供设备费'] = function (node, isTender) {
        return calcTools.partASupplyFee(node, '甲供设备费', isTender, false);
    };
    rationCalcBases['甲定人工费'] = function (node, isTender) {
        return calcTools.partASupplyFee(node, '甲定人工费', isTender, false);
    };
    rationCalcBases['甲定材料费'] = function (node, isTender) {
        return calcTools.partASupplyFee(node, '甲定材料费', isTender, false);
    };
    rationCalcBases['甲定机具费'] = function (node, isTender) {
        return calcTools.partASupplyFee(node, '甲定机械费', isTender, false);
    };
    rationCalcBases['甲定主材费'] = function (node, isTender) {
        return calcTools.partASupplyFee(node, '甲定主材费', isTender, false);
    };
    rationCalcBases['甲定设备费'] = function (node, isTender) {
        return calcTools.partASupplyFee(node, '甲定设备费', isTender, false);
    };
    rationCalcBases['暂估材料费'] = function (node, isTender) {
        return calcTools.estimateFee(node, true, isTender);
    };
};

(function overwriteFeeTypes() {
    if (typeof cpFeeTypes == 'undefined') return;
    cpFeeTypes = [
        {type: 'labour', name: '人工费'},
        {type: 'material', name: '材料费'},
        {type: 'machine', name: '机具费'},
        {type: 'mainMaterial', name: '主材费'},
        {type: 'equipment', name: '设备费'},
        {type: 'direct', name: '直接费'},
        {type: 'measure', name: '措施项目费'},
        {type: 'manage', name: '管理费'},
        {type: 'profit', name: '利润'},
        {type: 'tax', name: '税金'},
        {type: 'common', name: '工程造价'}
    ];
})();


if (typeof module !== 'undefined') {

} else {
    // 广东接口排版规则，添加内容默认项目特征
    $(function () {
        $('#add-content').val('3');
    });

    // 隐藏项目属性中：取费方式中的子目含量取费选项
    if ($('#rationContent')) {
        $('#rationContent').parent().hide();
    }

    //清单计算基数相关
    if(typeof baseFigureMap !== 'undefined'){
        baseFigureMap = {
            '分部分项工程费': {base: 'FBFXGCF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
            '分部分项人工费': {base: 'FBFXDEJJRGF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
            '分部分项材料费': {base: 'FBFXDEJJCLF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
            '分部分项机具费': {base: 'FBFXDEJJJXF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
            '分部分项主材费': {base: 'FBFXZCF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
            '分部分项设备费': {base: 'FBFXSBF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
            '分部分项人工工日': {base: 'FBFXRGGR', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
            '建筑面积': {base: 'JZMJ', class: 'FBFX'},
            '措施项目费': {base: 'CSXMF', fixedFlag: fixedFlag.MEASURE, class: 'CSXM'},
            '其他项目费': {base: 'QTXMF',  fixedFlag: fixedFlag.OTHER, class: 'QTXM'},
            '甲供人工费': {base: 'JGDEJJRGF', class: 'RCJ'},
            '甲供材料费': {base: 'JGDEJJCLF', class: 'RCJ'},
            '甲供机具费': {base: 'JGDEJJJXF', class: 'RCJ'},
            '甲供主材费': {base: 'JGZCF', class: 'RCJ'},
            '甲供设备费': {base: 'JGSBF', class: 'RCJ'},
            '甲定人工费': {base: 'JDDEJJRGF', class: 'RCJ'},
            '甲定材料费': {base: 'JDDEJJCLF', class: 'RCJ'},
            '甲定机具费': {base: 'JDDEJJJXF', class: 'RCJ'},
            '甲定主材费': {base: 'JDZCF', class: 'RCJ'},
            '甲定设备费': {base: 'JDSBF', class: 'RCJ'},
            '暂估材料费(从子目汇总)': {base: 'ZGCLFFZM', class: 'RCJ'},
            '税金': {base: 'SJ', fixedFlag: fixedFlag.TAX, class: 'SJ'}
        };
    }
    if(typeof baseFigureTemplate !== 'undefined'){
        baseFigureTemplate['ZGCLFFZM'] = function (tender) { //暂估材料费(从子目汇总)
            const feeField = 'estimate';
            const subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
            // 分部分项暂估合价
            const subEngineeringFee = cbTools.getBillsFee(fixedFlag.SUB_ENGINERRING, feeField, subFeeField);
            // 措施项目暂估合价
            let measureFee = cbTools.getBillsFee(fixedFlag.MEASURE, feeField, subFeeField);
            return (subEngineeringFee + measureFee).toDecimal(decimalObj.bills.totalPrice);
        };
    }
}

if(typeof gljCol !== 'undefined'){
    gljCol.showTaxRate = true;
}

// 人材机库追加的列头
if (typeof repositoryGljObj !== 'undefined') {
    repositoryGljObj.setting.header = [
        {headerName:"税率",headerWidth:60,dataCode:"taxRate", dataType: "Number", hAlign: "center", vAlign: "center"},
        { headerName: "机型", headerWidth: 60, dataCode: "model", dataType: "Number", hAlign: "center", vAlign: "center" },
        { headerName: "是否新增", headerWidth: 60, dataCode: "isComplementary", hAlign: "center", vAlign: "center" }
    ]
}

// 定额人材机选择页面表头
if (typeof gljOprObj !== 'undefined') {
    gljOprObj.gljLibSheetSetting.header = [
        {headerName: "选择", headerWidth: 40, dataCode: "select", hAlign: "center", vAlign: "center", cellType: "checkBox"},
        {headerName: "编码", headerWidth: 80, dataCode: "code", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"},
        {headerName: "名称", headerWidth: 160, dataCode: "name", dataType: "String", hAlign: "left", vAlign: "center"},
        {headerName: "规格型号", headerWidth: 160, dataCode: "specs", dataType: "String", hAlign: "left", vAlign: "center"},
        {headerName: "单位", headerWidth: 40, dataCode: "unit", dataType: "String", hAlign: "center", vAlign: "center"},
        {headerName: "单价", headerWidth: 55, dataCode: "basePrice", dataType: "Number", formatter: "0.00", hAlign: "right", vAlign: "center"},
        {headerName: "税率", headerWidth: 40, dataCode: "taxRate", dataType: "Number", formatter: "0.00", hAlign: "right", vAlign: "center"},
        {headerName: "类型", headerWidth: 60, dataCode: "typeName", dataType: "String", hAlign: "center", vAlign: "center"},
        {headerName: "新增", headerWidth: 40, dataCode: "isComplementary", dataType: "String", hAlign: "center", vAlign: "center", cellType: "checkBox", readOnly: true}
    ]
}

if (typeof installationFeeObj !== 'undefined') {
  installationFeeObj.getMeasureParentNode=function(){
    return projectObj.project.Bills.getMeasureNode();//取措施项目
  },
  installationFeeObj.feeRateMap={
    "AZF0101":"39.86",
    "AZF0102":"39.92",
    "AZF0103":"39.92",
    "AZF0104":"39.86",
    "AZF0105":"39.86",
    "AZF0106":"28.74",
    "AZF0107":"30.66",
    "AZF0108":"29.43",
    "AZF0109":"27.72",
    "AZF0110":"29.35",
    "AZF0111":"27.73",
    "AZF0112":"27.72",
    "AZF0113":"29.44",
    "AZF0114":"20.56",
    "AZF0115":"39.86",
    "AZF0116":"39.86",
    "AZF0117":"39.86",
    "AZF0118":"39.86",
    "AZF0119":"39.86",
    "AZF0120":"39.86",
    "AZF0121":"39.86",
    "AZF0122":"39.86",
    "AZF0123":"39.86",
    "AZF0124":"28.74",
    "AZF0125":"30.66",
    "AZF0126":"30.66",
    "AZF0127":"30.66",
    "AZF0128":"27.72",
    "AZF0129":"29.35",
    "AZF0130":"29.35",
    "AZF0131":"27.73",
    "AZF0132":"27.73",
    "AZF0133":"27.73",
    "AZF0134":"27.73",
    "AZF0135":"27.72",
    "AZF0136":"27.72",
    "AZF0137":"27.72",
    "AZF0138":"27.72",
    "AZF0139":"29.44",
    "AZF0140":"29.44",
    "AZF0141":"29.44",
    "AZF0142":"20.56",
    "AZF0143":"20.56",
    "AZF0144":"20.56",
    "AZF0145":"20.56",
    "AZF0146":"20.56",
    "AZF0147":"20.56",
    "AZF0148":"20.56",
    "AZF0149":"20.56",
    "AZF0150":"39.92",
    "AZF0151":"28.74",
    "AZF0152":"30.66",
    "AZF0153":"29.43",
    "AZF0154":"27.72",
    "AZF0155":"29.35",
    "AZF0156":"27.73",
    "AZF0157":"27.72",
    "AZF0158":"29.44",
    "AZF0159":"20.56",
    "AZF0160":"28.74",
    "AZF0161":"28.74",
    "AZF0162":"28.74",
    "AZF0163":"28.74",
    "AZF0164":"28.74",
    "AZF0165":"28.74",
    "AZF0166":"28.74",
    "AZF0167":"28.74",
    "AZF0168":"28.74",
    "AZF0169":"28.74",
    "AZF0170":"28.74",
    "AZF0171":"28.74",
    "AZF0172":"28.74",
    "AZF0173":"28.74",
    "AZF0174":"28.74",
    "AZF0175":"28.74",
    "AZF0176":"28.74",
    "AZF0177":"28.74",
    "AZF0178":"28.74",
    "AZF0179":"28.74",
    "AZF0180":"28.74",
    "AZF0181":"28.74",
    "AZF0182":"28.74",
    "AZF0183":"28.74",
    "AZF0184":"30.66",
    "AZF0185":"30.66",
    "AZF0186":"30.66",
    "AZF0187":"30.66",
    "AZF0188":"30.66",
    "AZF0189":"30.66",
    "AZF0190":"30.66",
    "AZF0191":"30.66",
    "AZF0192":"30.66",
    "AZF0193":"30.66",
    "AZF0194":"30.66",
    "AZF0195":"30.66",
    "AZF0196":"30.66",
    "AZF0197":"30.66",
    "AZF0198":"30.66",
    "AZF0199":"30.66",
    "AZF0200":"30.66",
    "AZF0201":"30.66",
    "AZF0202":"30.66",
    "AZF0203":"30.66",
    "AZF0204":"30.66",
    "AZF0205":"30.66",
    "AZF0206":"30.66",
    "AZF0207":"30.66",
    "AZF0208":"27.72",
    "AZF0209":"27.72",
    "AZF0210":"27.72",
    "AZF0211":"27.72",
    "AZF0212":"27.72",
    "AZF0213":"27.72",
    "AZF0214":"27.72",
    "AZF0215":"27.72",
    "AZF0216":"27.72",
    "AZF0217":"27.72",
    "AZF0218":"27.72",
    "AZF0219":"27.72",
    "AZF0220":"27.72",
    "AZF0221":"27.72",
    "AZF0222":"27.72",
    "AZF0223":"27.72",
    "AZF0224":"27.72",
    "AZF0225":"27.72",
    "AZF0226":"27.72",
    "AZF0227":"27.72",
    "AZF0228":"27.72",
    "AZF0229":"27.72",
    "AZF0230":"27.72",
    "AZF0231":"27.72",
    "AZF0232":"27.73",
    "AZF0233":"27.73",
    "AZF0234":"27.73",
    "AZF0235":"27.73",
    "AZF0236":"27.73",
    "AZF0237":"27.73",
    "AZF0238":"27.73",
    "AZF0239":"27.73",
    "AZF0240":"27.73",
    "AZF0241":"27.73",
    "AZF0242":"27.73",
    "AZF0243":"27.73",
    "AZF0244":"27.73",
    "AZF0245":"27.73",
    "AZF0246":"27.73",
    "AZF0247":"27.73",
    "AZF0248":"27.73",
    "AZF0249":"27.73",
    "AZF0250":"27.73",
    "AZF0251":"27.73",
    "AZF0252":"27.73",
    "AZF0253":"27.73",
    "AZF0254":"27.73",
    "AZF0255":"27.73",
    "AZF0256":"27.72",
    "AZF0257":"27.72",
    "AZF0258":"27.72",
    "AZF0259":"27.72",
    "AZF0260":"27.72",
    "AZF0261":"27.72",
    "AZF0262":"27.72",
    "AZF0263":"27.72",
    "AZF0264":"27.72",
    "AZF0265":"27.72",
    "AZF0266":"27.72",
    "AZF0267":"27.72",
    "AZF0268":"27.72",
    "AZF0269":"27.72",
    "AZF0270":"27.72",
    "AZF0271":"27.72",
    "AZF0272":"27.72",
    "AZF0273":"27.72",
    "AZF0274":"27.72",
    "AZF0275":"27.72",
    "AZF0276":"27.72",
    "AZF0277":"27.72",
    "AZF0278":"27.72",
    "AZF0279":"27.72",
    "AZF0280":"29.44",
    "AZF0281":"29.44",
    "AZF0282":"29.44",
    "AZF0283":"29.44",
    "AZF0284":"29.44",
    "AZF0285":"29.44",
    "AZF0286":"29.44",
    "AZF0287":"29.44",
    "AZF0288":"29.44",
    "AZF0289":"29.44",
    "AZF0290":"29.44",
    "AZF0291":"29.44",
    "AZF0292":"29.44",
    "AZF0293":"29.44",
    "AZF0294":"29.44",
    "AZF0295":"29.44",
    "AZF0296":"29.44",
    "AZF0297":"29.44",
    "AZF0298":"29.44",
    "AZF0299":"29.44",
    "AZF0300":"29.44",
    "AZF0301":"29.44",
    "AZF0302":"29.44",
    "AZF0303":"29.44",
    "AZF0304":"20.56",
    "AZF0305":"20.56",
    "AZF0306":"20.56",
    "AZF0307":"20.56",
    "AZF0308":"20.56",
    "AZF0309":"20.56",
    "AZF0310":"20.56",
    "AZF0311":"20.56",
    "AZF0312":"20.56",
    "AZF0313":"20.56",
    "AZF0314":"20.56",
    "AZF0315":"20.56",
    "AZF0316":"20.56",
    "AZF0317":"20.56",
    "AZF0318":"20.56",
    "AZF0319":"20.56",
    "AZF0320":"20.56",
    "AZF0321":"20.56",
    "AZF0322":"20.56",
    "AZF0323":"20.56",
    "AZF0324":"20.56",
    "AZF0325":"20.56",
    "AZF0326":"20.56",
    "AZF0327":"20.56",
    "AZF0328":"27.72",
    "AZF0329":"27.72",
    "AZF0330":"39.86",
    "AZF0331":"27.72"
    }
}

// 导入导出接口
// 需要用固定类别关联的费用字典，用固定类别来映射
// 数据节选自标准pdf文件《《建设工程政府投资项目造价数据标准》信息公开版》，附录C-费用名称与费用代号
// 若映射表中没有映射关系，则费用字典取名称首字母
let FlagFeeCodeMap = {};
let FormulaFeeCodeMap = {};
if (typeof commonConstants !== 'undefined') {
    FlagFeeCodeMap = {
        // 分部分项工程
        [commonConstants.fixedFlag.SUB_ENGINERRING]: 'QDF',
        // 措施项目
        [commonConstants.fixedFlag.MEASURE]: 'CSF',
        // 其他项目
        [commonConstants.fixedFlag.OTHER]: 'QTF',
        // 措施项目的子项
        [commonConstants.fixedFlag.SAFETY_CONSTRUCTION]: 'AQWMSGF', // 绿色施工安全防护措施费
        [commonConstants.fixedFlag.OTHER_MEASURE_FEE]: 'QTCSF', // 其他措施费
        // 其他项目的子项
        [commonConstants.fixedFlag.PROVISIONAL]: 'ZLF', // 暂列金额
        [commonConstants.fixedFlag.ESTIMATE]: 'ZGJ', // 暂估价
        [commonConstants.fixedFlag.MATERIAL_PROVISIONAL]: 'ZGC', // 材料（工程设备）暂估价
        [commonConstants.fixedFlag.ENGINEERING_ESITIMATE]: 'ZGGC', // 专业工程暂估价
        [commonConstants.fixedFlag.DAYWORK]: 'LXF', // 计日工
        [commonConstants.fixedFlag.TURN_KEY_CONTRACT]: 'ZCBFWF', // 总承包服务费
        [commonConstants.fixedFlag.BUDGET_INCLUDE_WORK_FEE]: 'YSBGF', // 预算包干费
        [commonConstants.fixedFlag.PROJECT_HIGH_QUALITY_FEE]: 'GCYZF', // 工程优质费
        [commonConstants.fixedFlag.BUDGET_ESTIMATE_DIFF]: 'GSFDC', // 概算幅度差
        [commonConstants.fixedFlag.CLAIM_VISA]: 'SPYXCQZ', // 索赔与现场签证
        [commonConstants.fixedFlag.CLAIM]: 'SPFY', // 索赔费用
        [commonConstants.fixedFlag.VISA]: 'XCQZFY', // 现场签证
        [commonConstants.fixedFlag.OTHER_FEE]: 'QTFY', // 其他费用
        // 税金
        [commonConstants.fixedFlag.TAX]: 'SJ',
        // 工程造价
        [commonConstants.fixedFlag.ENGINEERINGCOST]: 'ZZJ',
    };
    // 需要用计算基数关联的费用字典
    FormulaFeeCodeMap = {
        '{分部分项工程费}': 'QDF',
        '{分部分项人工费}': 'QRG',
        '{分部分项材料费}': 'QCL',
        '{分部分项机具费}': 'QJX',
        '{分部分项主材费}': 'QZCF',
        '{分部分项设备费}': 'QSBF',
        '{分部分项人工工日}': 'FBFXRGGR', // 标准没有，自增
        '{建筑面积}': 'JZMZ', // 自增
        '{措施项目费}': 'CSF',
        '{其他项目费}': 'QTF',
        '{甲供人工费}': 'JGRGF', // 自增
        '{甲供材料费}': 'JGC',
        '{甲供机具费}': 'JGJJF', // 自增
        '{甲供主材费}': 'JGZCF', // 自增
        '{甲供设备费}': 'JGSBF', // 自增
        '{甲定人工费}': 'JDRGF', // 自增
        '{甲定材料费}': 'JDCLF', // 自增
        '{甲定机具费}': 'JDJJF', // 自增
        '{甲定主材费}': 'JDZCF', // 自增
        '{甲定设备费}': 'JDSBF', // 自增
        '{暂估材料费(从子目汇总)}': 'ZGCLFCZMHZ', // 自增
        '{税金}': 'SJ',
    };
}

if(typeof gljCol !== 'undefined'){
  gljCol.hideInfoPrice = false;
};

if(typeof module !== 'undefined'){
    module.exports = {
        getBillsCalcMode: getBillsCalcMode,
        getAreaName: getAreaName
    };
}

function getBillsCalcMode() {
    return 1
}

function getAreaName() {
    return 'guangdong_2018'
}