ttk-app-core
Version:
enterprise develop framework
210 lines (205 loc) • 10.8 kB
JavaScript
import { Map, fromJS, Set } from 'immutable'
import { reducer as MetaReducer } from 'edf-meta-engine'
import config from './config'
import { getInitState } from './data'
import moment from 'moment'
class reducer {
constructor(option) {
this.metaReducer = option.metaReducer
this.config = config.current
}
init = (state, option) => {
const initState = getInitState()
return this.metaReducer.init(state, initState)
}
load = (state, option, rptTitleList) => {
//会计总则
state = this.metaReducer.sf(state, 'data.accountingStandards', fromJS(option && option.kjzdzzDm))
//state = this.metaReducer.sf(state, 'data.urlData', fromJS(option))
//传入参数
state = this.metaReducer.sf(state, 'data.afferentParamsData', fromJS(option))
const lists = rptTitleList.find((element) => element.zlbsxlDm == option.zlbsxlDm)
//default
if (!lists) {
rptTitleList[0].titleList.forEach(item => {
state = this.metaReducer.sf(state, `data.rptTitle.${item.key}`, item.value)
})
} else {
lists.titleList.forEach(item => {
state = this.metaReducer.sf(state, `data.rptTitle.${item.key}`, item.value)
})
}
//default
if (!lists) {
rptTitleList[0].titleList.forEach(item => {
state = this.metaReducer.sf(state, `data.rptTitle.${item.key}`, item.value)
})
} else {
lists.titleList.forEach(item => {
state = this.metaReducer.sf(state, `data.rptTitle.${item.key}`, item.value)
})
}
return state
}
initForm = (state, option) => {
state = this.metaReducer.sf(state, 'data.form.ts', option && option.ts)
if (option && !option.taxTimeType) {
option.taxTimeType = 2000070002 //默认季度
}
state = this.metaReducer.sf(state, 'data.form', fromJS(option))
return state
}
//获取报表头相关的数据
setRptTitle = (state, mainTitle, value) => {
state = this.metaReducer.sf(state, 'data.rptHeader.mainTitle', '财务报表')
if (value) {
state = this.metaReducer.sf(state, 'data.rptHeader.vatTaxpayerNum', `纳税人识别号: ${value.nsrsbh}`)
state = this.metaReducer.sf(state, 'data.rptHeader.fillReportDate', `填报日期:${value.reportDate}`)
state = this.metaReducer.sf(state, 'data.rptHeader.compilingUnit', `编制单位:${value.nsrmc}`)
state = this.metaReducer.sf(state, 'data.rptHeader.reportperiod', `报表属期:${value.fromDate} 至 ${value.toDate}`)
state = this.metaReducer.sf(state, 'data.rptHeader.enterprise', `${value.reportShortName}`)
state = this.metaReducer.sf(state, 'data.rptHeader.subTitle', `${value.accountingStandardDescribe}`)
state = this.metaReducer.sf(state, 'data.rptHeader.unit', '单位:元')
}
return state
}
//初始化报表
initTranPeport = (state, tranReport) => {
state = this.metaReducer.sf(state, 'data.targetList', fromJS(tranReport))
return state
}
//初始化设置枚举信息
enumDetails = (state, res) => {
return this.metaReducer.sf(state, 'data.enumListForm', fromJS(res))
}
//设置报表数据
setSourceRpt = (state, tranReport, type, rptTitle) => {
if (tranReport) {
//初始化报表
state = this.initTranPeport(state, tranReport)
if (tranReport.header && tranReport.body) {
let header = tranReport.header, body = tranReport.body, memo = tranReport.memo
//资产负债表
if (type == 'balanceSheetRpt') {
state = this.metaReducer.sf(state, 'data.source.headerList', fromJS(header.zcfzbHeader ? header.zcfzbHeader : ''))
state = this.metaReducer.sf(state, 'data.source.dataList', fromJS(body.zcfzbBody ? body.zcfzbBody : ''))
state = this.setRptTitle(state, rptTitle, memo.zcfzbMemo ? memo.zcfzbMemo : '')
//设置报表所有表达式
state = this.getTableAllFormula(state, fromJS(body.zcfzbBody ? body.zcfzbBody : ''))
}
//利润表
if (type == 'profitStatementRpt') {
state = this.metaReducer.sf(state, 'data.source.headerList', fromJS(header.lrbHeader ? header.lrbHeader : ''))
state = this.metaReducer.sf(state, 'data.source.dataList', fromJS(body.lrbBody ? body.lrbBody : ''))
state = this.setRptTitle(state, rptTitle, memo.lrbMemo ? memo.lrbMemo : '')
//设置报表所有表达式
state = this.getTableAllFormula(state, fromJS(body.lrbBody ? body.lrbBody : ''))
}
//现金流量表
if (type == 'cashflowStatementRpt') {
state = this.metaReducer.sf(state, 'data.source.headerList', fromJS(header.xjllbHeader ? header.xjllbHeader : ''))
state = this.metaReducer.sf(state, 'data.source.dataList', fromJS(body.xjllbBody ? body.xjllbBody : ''))
state = this.setRptTitle(state, rptTitle, memo.xjllbMemo ? memo.xjllbMemo : '')
//设置报表所有表达式
state = this.getTableAllFormula(state, fromJS(body.xjllbBody ? body.xjllbBody : ''))
}
}
}
return state
}
//获取当且表格所有的合计行
getTableAllFormula = (state, list) => {
let totalFormulaList = [], isMutilCol, allColumns = []
if (list) {
//获取所有需要合计的行
for (let _rowIndex = 0; _rowIndex < list.size; _rowIndex++) {
// sumCol1: "{0}>0?{1}:{2}"
// sumListCol1: "[col1:19,col1:19+col1:4,col1:4]"
// sumCol4: "col4:col1_22+col1_23+col1_24+col1_25+col1_26+col1_27"
// sumCol5: "col5:22+23+24+25+26+27"
let rowData = list.get(_rowIndex)
if (rowData.get('isSumRow')) {
//遍历当前行对象
rowData.map((item, key) => {
if (key.indexOf('sumCol') > -1) {
let totalIndex = _rowIndex + 1
if (item.indexOf('?') > -1) { //带条件的表达式
let totalCol = `${key.replace('sum', '').toLowerCase()}_${totalIndex}`,
sumListName = key.replace('sum', 'sumList'),
arrayStrs = rowData.get(sumListName),
colFormula = item,
sumListData = JSON.parse(arrayStrs.replace('\"[\"', '\'[').replace('\"]\"', '\']'))
totalFormulaList.push({ totalCol, colFormula, sumListData, totalIndex })
allColumns.push(`${key.replace('sum', '').toLowerCase()}`)
} else {
//当前列、//跨列的计算
let tmpFormula = item.split(':'),
totalCol = `${tmpFormula[0]}_${totalIndex}`,
colFormula
if (item.indexOf('_') > -1) {//跨列
colFormula = tmpFormula[1]
isMutilCol = true
} else {
colFormula = `${tmpFormula[0]}_${tmpFormula[1].replace(/\+/g, `\+${tmpFormula[0]}_`).replace(/\-/g, `\-${tmpFormula[0]}_`)}`
}
totalFormulaList.push({ totalCol, colFormula, totalIndex })
allColumns.push(`${tmpFormula[0]}`)
}
}
})
}
}
}
//对需要合计得表达式分组
//let groupFormulaList = totalFormulaList ? fromJS(totalFormulaList).groupBy(val => val.get('totalCol')) : ''
//对需要合计得表达式排序
let orderFormulaList = totalFormulaList ? fromJS(totalFormulaList).sortBy((val, index, obj) => {
return val.get('totalCol')
}, (a, b) => {
if (a < b) { return -1 }
if (a > b) { return 1 }
if (a === b) { return 0 }
}) : ''
state = this.metaReducer.sf(state, 'data.source.allFormulaList', fromJS(orderFormulaList))
if (isMutilCol) {
//是否存在跨列计算表达式
state = this.metaReducer.sf(state, 'data.source.isMutilColFormula', true)
let orderAllColumns = allColumns ? Set(allColumns).sort() : ''
state = this.metaReducer.sf(state, 'data.source.allColumns', orderAllColumns)
}
return state
}
//设置loading加载
setLoading = (state, path, value) => {
return this.metaReducer.sf(state, path, value)
}
//初始化表格数据
tableData = (state, value) => {
state = this.metaReducer.sf(state, 'data.list', fromJS(value && value.rows))
return state
}
//上传
uploadFile = (state, value, key) => {
state = this.metaReducer.sf(state, 'data.btnState.updateState', true)
if (key == "balancesheet") {
state = this.metaReducer.sf(state, 'data.balancesheetData', fromJS({ originalName: value.originalName, id: value.id }))
} else if (key == "profitstatement") {
state = this.metaReducer.sf(state, 'data.profitstatementData', fromJS({ originalName: value.originalName, id: value.id }))
} else if (key == "cashflowstatement") {
state = this.metaReducer.sf(state, 'data.cashflowstatementData', fromJS({ originalName: value.originalName, id: value.id }))
}
return state
}
//更新状态
updateArr = (state, arr) => {
arr.forEach(item => {
state = this.metaReducer.sf(state, item.path, fromJS(item.value))
})
return state
}
}
export default function creator(option) {
const metaReducer = new MetaReducer(option),
o = new reducer({ ...option, metaReducer })
return { ...metaReducer, ...o }
}