UNPKG

ttk-app-core

Version:
534 lines (477 loc) 21.5 kB
import { Map, List, fromJS } from 'immutable' import { reducer as MetaReducer } from 'edf-meta-engine' import config from './config' import moment from 'moment' import { consts } from 'edf-consts' import { getInitState } from './data' import utils from 'edf-utils' class reducer { constructor(option) { this.metaReducer = option.metaReducer } init = (state, option = {}) => { let initData = getInitState() //资产属性id initData.data.consts = { ASSETTYPE_fixedAssets: consts.ASSETPROPERTY_0001, ASSETTYPE_immaterialAssets: consts.ASSETPROPERTY_0002, ASSETTYPE_longtermPrepaidExpenses: consts.ASSETPROPERTY_0003, DEPRMETHOD_0001: consts.DEPRMETHOD_0001, DEPRMETHOD_0002: consts.DEPRMETHOD_0002, DEPRMETHOD_0003: consts.DEPRMETHOD_0003, DEPRMETHOD_0004: consts.DEPRMETHOD_0004, DEPRMETHOD_0005: consts.DEPRMETHOD_0005 } initData.data.other.assetProperty = [{ id: consts.ASSETPROPERTY_0001, name: '固定资产' }, { id: consts.ASSETPROPERTY_0002, name: '无形资产' }, { id: consts.ASSETPROPERTY_0003, name: '长期待摊费用' }] initData.data.form.assetPropertyId = consts.ASSETPROPERTY_0001 if (option.status == 'query') { initData.data.other.assetCardIsEnable = false } initData.data.other.stepEnabled = option.isGuide return this.metaReducer.init(state, initData) } load = (state, res) => { let option = res.initEnum, consts = this.metaReducer.gf(state, 'data.consts').toJS(), statusObj = this.metaReducer.gf(state, 'data.other.statusObj').toJS() if (option.beginUseTime1 && option.beginUseTime2) { let beginUseTime1 = option.beginUseTime1, beginUseTime2 = option.beginUseTime2 if (option.isYj !== undefined) { state = this.metaReducer.sf(state, 'data.other.isYj', option.isYj) } state = this.metaReducer.sf(state, 'data.other.beginUseTime1', beginUseTime1) state = this.metaReducer.sf(state, 'data.other.beginUseTime2', beginUseTime2) } if (res.status) state = this.metaReducer.sf(state, 'data.other.status', res.status) // state = this.getSelectValue(state, option) if (res.status == 'edit' && res.asset.assetPropertyId != consts.ASSETTYPE_fixedAssets) { let newdepreciationFunction = [] newdepreciationFunction.push(option.depreciationFunction[0]) option.depreciationFunction = newdepreciationFunction state = this.getSelectValue(state, option) } else { state = this.getSelectValue(state, option) } let systemDate = utils.moment.stringToMoment(option.systemDate).format('YYYY-MM-DD'), contextDate = systemDate //开始使用时间,上下文中存在会计期间的时候取上下文的,不存在的时候取系统时间 let currentOrg = this.metaReducer.context.get("currentOrg") if(currentOrg.periodDate && moment(currentOrg.periodDate)<=moment(systemDate)){ contextDate = utils.moment.momentToString(moment(currentOrg.periodDate).endOf('month'), 'YYYY-MM-DD') } state = this.metaReducer.sf(state, 'data.other.currentDate', fromJS(systemDate)) state = this.metaReducer.sf(state, 'data.other.contextDate', fromJS(contextDate)) state = this.metaReducer.sf(state, 'data.form.buyDate', fromJS(contextDate)) state = this.metaReducer.sf(state, 'data.form.changeDate', fromJS(systemDate)) if (res && res.asset) { let isDeprEnd = res.asset.isDeprEnd, deprEndTime = res.deprEndTime if (isDeprEnd !== undefined) { state = this.metaReducer.sf(state, 'data.other.isDeprEnd', isDeprEnd) if (deprEndTime) state = this.metaReducer.sf(state, 'data.other.deprEndTime', deprEndTime) } state = this.getInitDate(state, res, option) } else { if (option.depreciationFunction) { if (!option.lastDepretMethod) { state = this.metaReducer.sf(state, 'data.form.deprMethod', fromJS(option.depreciationFunction[0])) } else { if (option.lastDepretMethod != option.depreciationFunction[0].id) { state = this.metaReducer.sf(state, 'data.other.isLineDepreciation', false) } let deprMethod = option.depreciationFunction.filter((item) => item.id == option.lastDepretMethod) state = this.metaReducer.sf(state, 'data.form.deprMethod', fromJS(deprMethod[0])) } } } if (res.status == statusObj.change) state = this.metaReducer.sf(state, 'data.form.changeDate', fromJS(moment().format('YYYY-MM-DD'))) return state } //资产卡片下拉框值 getSelectValue = (state, option) => { //资产属性 if (option && option.assetProperty) { state = this.metaReducer.sf(state, 'data.other.assetProperty', fromJS(option.assetProperty)) if (option.assetProperty.length) state = this.metaReducer.sf(state, 'data.form.assetProperty', fromJS(option.assetProperty[0])) } //资产分类 if (option && option.assetClass) state = this.metaReducer.sf(state, 'data.other.assetClass', fromJS(option.assetClass)) //折旧方法 if (option && option.depreciationFunction) { state = this.metaReducer.sf(state, 'data.other.depreciationFunction', fromJS(option.depreciationFunction)) state = this.metaReducer.sf(state, 'data.other.depreciationFunctionAll', fromJS(option.depreciationFunction)) } //增加方式 if (option && option.assetAddType) state = this.metaReducer.sf(state, 'data.other.assetAddType', fromJS(option.assetAddType)) //经济用途 if (option && option.assetUses) state = this.metaReducer.sf(state, 'data.other.assetUses', fromJS(option.assetUses)) //票据类型 if (option && option.invoiceType) state = this.metaReducer.sf(state, 'data.other.invoiceType', fromJS(option.invoiceType)) //使用部门 if (option && option.departments) state = this.metaReducer.sf(state, 'data.other.departments', fromJS(option.departments)) //税率 if (option && option.taxRate) state = this.metaReducer.sf(state, 'data.other.taxRate', fromJS(option.taxRate)) //核算项目 if (option && option.setProjects) state = this.metaReducer.sf(state, 'data.other.project', fromJS(option.setProjects)) //计量单位 if (option && option.setUnits) state = this.metaReducer.sf(state, 'data.other.unit', fromJS(option.setUnits)) return state } //资产查看的回显值 getInitDate = (state, res, option) => { let asset = res.asset, consts = this.metaReducer.gf(state, 'data.consts').toJS(), deprMethod = {}, useDate, assetPropertyId = res.asset.assetPropertyId, assetClassId = res.asset.assetClassId if (assetPropertyId && assetClassId) { //无形资产使用月份默认值 if (assetPropertyId == consts.ASSETTYPE_immaterialAssets) useDate = 10 //长期待摊使用月份默认值 if (assetPropertyId == consts.ASSETTYPE_longtermPrepaidExpenses) useDate = 3 //固定资产使用月份默认值 if (assetClassId == 35 || assetClassId == 46) useDate = 20 if (assetClassId == 36 || assetClassId == 47) useDate = 10 if (assetClassId == 37 || assetClassId == 48) useDate = 5 if (assetClassId == 38 || assetClassId == 49) useDate = 4 if (assetClassId == 39 || assetClassId == 50) useDate = 3 //当前资产应带出的预计使用月值 state = this.metaReducer.sf(state, 'data.other.smallMonth', fromJS(useDate * 12)) } if (asset.origAccuDepreciation !== undefined) { asset.initAccuDepreciation = asset.origAccuDepreciation } state = this.metaReducer.sf(state, 'data.form', fromJS(asset)) //月折旧额计算 if (asset.deprMethodId == consts.DEPRMETHOD_0001) { let //accuDepreciation = asset.accuDepreciation ? asset.accuDepreciation : 0, //hasDeprMonths = asset.hasDeprMonths ? asset.hasDeprMonths : 0, //initDeprMonths = asset.initDeprMonths ? asset.initDeprMonths : 0, //initAccuDepreciation = asset.initAccuDepreciation ? asset.initAccuDepreciation : 0, salvageRate = asset.salvageRate ? asset.salvageRate : 0, origValue = asset.origValue ? asset.origValue : 0, deprMonth = asset.deprMonth ? asset.deprMonth : 0 let salvageValue = origValue * (salvageRate / 100), lineDepreciation if (asset.assetPropertyId == consts.ASSETTYPE_fixedAssets) { lineDepreciation = (origValue - salvageValue) / deprMonth } else { lineDepreciation = origValue / deprMonth //长期待摊和无形不计算salvageValue } if (!asset.lineDepreciation) { state = this.metaReducer.sf(state, 'data.form.lineDepreciation', fromJS(lineDepreciation)) } //lineDepreciation = (origValue - (accuDepreciation+initAccuDepreciation) - salvageRate)/(deprMonth-(hasDeprMonths+initDeprMonths)) state = this.metaReducer.sf(state, 'data.other.lineDepreciation', fromJS(lineDepreciation)) } //控制期初是否显示 if (asset.buyDate) { let isHasInit = false, assetPropertyId = asset.assetPropertyId, beginUseTime1 = this.getBeginUseTime(option.beginUseTime1), beginUseTime2 = this.getBeginUseTime(option.beginUseTime2), beginUseTime = beginUseTime1 if (asset.assetPropertyId == consts.ASSETTYPE_fixedAssets) beginUseTime = beginUseTime2 if (moment(asset.buyDate) < moment(beginUseTime)) isHasInit = true if (asset.isHasInit !== undefined) { isHasInit = asset.isHasInit } asset.isHasInit = isHasInit if (isHasInit) { state = this.metaReducer.sf(state, 'data.other.isDisableHasInit', !isHasInit) } state = this.metaReducer.sf(state, 'data.form.isHasInit', asset.isHasInit) } if (asset.isHasInit) { state = this.metaReducer.sf(state, 'data.other.isDisableLineDepreciation', false) } if (option.assetProperty && res.asset.assetPropertyId) { let assetProperty = option.assetProperty.filter((item) => item.id == res.asset.assetPropertyId) state = this.metaReducer.sf(state, 'data.form.assetPropertyId', fromJS(assetProperty[0].id + '')) } if (option.assetClass && res.asset.assetClassId) { let assetClass = option.assetClass.filter((item) => item.id == res.asset.assetClassId) state = this.metaReducer.sf(state, 'data.form.assetClass', fromJS(assetClass[0])) } if (option.assetUses && res.asset.usesId) { let assetUses = option.assetUses.filter((item) => item.id == res.asset.usesId) state = this.metaReducer.sf(state, 'data.form.uses', fromJS(assetUses[0])) } if (option.invoiceType && res.asset.invoiceTypeId) { let invoiceType = option.invoiceType.filter((item) => item.id == res.asset.invoiceTypeId) state = this.metaReducer.sf(state, 'data.form.invoiceType', fromJS(invoiceType[0])) } if (option.depreciationFunction && res.asset.deprMethodId) { let depreciationFunction = option.depreciationFunction.filter((item) => item.id == res.asset.deprMethodId) deprMethod = depreciationFunction[0] state = this.metaReducer.sf(state, 'data.form.deprMethod', fromJS(depreciationFunction[0])) if (deprMethod.id == consts.DEPRMETHOD_0004 || deprMethod.id == consts.DEPRMETHOD_0005) { state = this.metaReducer.sf(state, 'data.other.initDepr', fromJS(true)) state = this.metaReducer.sf(state, 'data.other.disableBeginDeprPeriod', fromJS(true)) } } if (option.assetAddType && res.asset.addTypeId) { let assetAddType = option.assetAddType.filter((item) => item.id == res.asset.addTypeId) state = this.metaReducer.sf(state, 'data.form.addType', fromJS(assetAddType[0])) } if (option.departments && res.asset.departmentId) { let departments = option.departments.filter((item) => item.id == res.asset.departmentId) state = this.metaReducer.sf(state, 'data.form.department', fromJS(departments[0])) } if (option.taxRate && res.asset.inputTaxRate) { let taxRate = option.taxRate.filter((item) => item.id == res.asset.inputTaxRate) state = this.metaReducer.sf(state, 'data.form.inputTaxRate', fromJS(taxRate[0])) } if (option.setProjects && res.asset.projectId) { let setProjects = option.setProjects.filter((item) => item.id == res.asset.projectId) state = this.metaReducer.sf(state, 'data.form.project', fromJS(setProjects[0])) } if (option.setUnits && res.asset.unitId) { let setUnits = option.setUnits.filter((item) => item.id == res.asset.unitId) state = this.metaReducer.sf(state, 'data.form.unit', fromJS(setUnits[0])) } //查看的时候月折旧额在非直线法下不显示 if (deprMethod.id != consts.DEPRMETHOD_0001) { state = this.metaReducer.sf(state, 'data.other.isLineDepreciation', fromJS(false)) } //debugger return state } saveAndNew = (state, option) => { let consts = this.metaReducer.gf(state, 'data.consts').toJS(), other = this.metaReducer.gf(state, 'data.other').toJS(), deprMethod other.depreciationFunctionAll.map(item => { if (item.id == option.deprMethodId) { deprMethod = item } }) //获取开始使用时间 let systemDate = this.metaReducer.gf(state, 'data.other.currentDate'), contextDate = systemDate //开始使用时间,上下文中存在会计期间的时候取上下文的,不存在的时候取系统时间 let currentOrg = this.metaReducer.context.get("currentOrg") if(currentOrg.periodDate && moment(currentOrg.periodDate)<=moment(systemDate)){ contextDate = utils.moment.momentToString(moment(currentOrg.periodDate).endOf('month'), 'YYYY-MM-DD') } let form = { assetPropertyId: option.assetPropertyId, buyDate: contextDate, isHasInit: false, deprMethod: option.deprMethod, quantity: 1, deprMethod: deprMethod, salvageRate: 5, } state = this.metaReducer.sf(state, `data.form`, fromJS(form)) state = this.metaReducer.sf(state, `data.other.moreInfo`, fromJS(false)) return state } assetPropertyChange = (state, value, res) => { let depreciationFunctionAll = this.metaReducer.gf(state, 'data.other.depreciationFunctionAll'), consts = this.metaReducer.gf(state, 'data.consts').toJS(), oldForm = this.metaReducer.gf(state, 'data.form').toJS(), other = this.metaReducer.gf(state, 'data.other').toJS() if (oldForm.assetPropertyId == consts.ASSETTYPE_fixedAssets) { state = this.metaReducer.sf(state, 'data.other.salvageRate', fromJS(oldForm.salvageRate)) } //资产分类 if (res && res.assetClass) { let currentDate = this.metaReducer.gf(state, 'data.other.currentDate') let form = { name: oldForm.name, code: oldForm.code != other.currentCode ? oldForm.code : '', specification: oldForm.specification, deprMonth: '', quantity: 1, assetPropertyId: value, buyDate: oldForm.buyDate, origValue: oldForm.origValue, uses: oldForm.uses, changeDate: currentDate, isHasInit: oldForm.isHasInit, beginDeprPeriod: oldForm.beginDeprPeriod, initDeprMonths: oldForm.initDeprMonths, initAccuDepreciation: 0, salvageRate: 0, } if (value == consts.ASSETTYPE_fixedAssets) form.salvageRate = other.salvageRate state = this.metaReducer.sf(state, 'data.form', fromJS(form)) state = this.metaReducer.sf(state, 'data.other.assetClass', fromJS(res.assetClass)) state = this.metaReducer.sf(state, 'data.other.moreInfo', false) //state = this.metaReducer.sf(state, 'data.other.isDisableHasInit', true) state = this.metaReducer.sf(state, 'data.other.isDisableLineDepreciation', true) } if (depreciationFunctionAll) { if (value == consts.ASSETTYPE_fixedAssets) { state = this.metaReducer.sf(state, 'data.other.depreciationFunction', fromJS(depreciationFunctionAll.toJS())) } else { state = this.metaReducer.sf(state, 'data.other.depreciationFunction', fromJS([depreciationFunctionAll.toJS()[0]])) } state = this.metaReducer.sf(state, 'data.form.deprMethod', fromJS(depreciationFunctionAll.toJS()[0])) } return state } assetClass = (state, res, assetClassItem) => { let currentCode = this.metaReducer.gf(state, 'data.other.currentCode'), consts = this.metaReducer.gf(state, 'data.consts').toJS(), code = this.metaReducer.gf(state, 'data.form.code') if (!code || currentCode == code) { state = this.metaReducer.sf(state, 'data.form.code', fromJS(res)) state = this.metaReducer.sf(state, 'data.other.currentCode', fromJS(res)) } let assetPropertyId = this.metaReducer.gf(state, 'data.form.assetPropertyId'), ASSETTYPE_fixedAssets = consts.ASSETTYPE_fixedAssets, ASSETTYPE_immaterialAssets = consts.ASSETTYPE_immaterialAssets, ASSETTYPE_longtermPrepaidExpenses = consts.ASSETTYPE_longtermPrepaidExpenses, generalTaxPayer = consts.VATTAXPAYER_generalTaxPayer, smallScaleTaxPayer = consts.VATTAXPAYER_smallScaleTaxPayer /*//选择分类带出税率 let rate, taxRate = this.metaReducer.gf(state, 'data.other.taxRate') //一般纳税人 if (this.metaReducer.context.get("currentOrg").vatTaxpayer == generalTaxPayer) { //固定资产 if(assetPropertyId == ASSETTYPE_fixedAssets){ if(assetClassItem.id == 35){ rate = '11%' }else{ rate = '17%' } //无形资产 }else if(assetPropertyId == ASSETTYPE_immaterialAssets){ if(assetClassItem.id == 19){ rate = '11%' }else{ rate = '6%' } //长期待摊 }else if(assetPropertyId == ASSETTYPE_longtermPrepaidExpenses){ rate = '6%' } //小规模纳税人 } else if (this.metaReducer.context.get("currentOrg").vatTaxpayer == smallScaleTaxPayer) { //固定资产 if(assetPropertyId == ASSETTYPE_fixedAssets){ if(assetClassItem.id == 46){ rate = '5%' }else{ rate = '3%' } //无形资产和长期待摊 }else{ rate = '3%' } } if(taxRate) { taxRate.toJS().map(item => { if(rate == item.name) { state = this.metaReducer.sf(state, 'data.form.inputTaxRate', fromJS(item)) let inputTaxRate, origValue = this.metaReducer.gf(state, 'data.form.origValue') origValue = origValue ? origValue : 0 inputTaxRate = item.taxRate state = this.metaReducer.sf(state, 'data.form.inputTax', fromJS(origValue*inputTaxRate)) } }) }*/ return state } deprMethod = (state, option) => { //let isWholeYear = false //if(option.id == 5100020002 || option.id == 5100020003) isWholeYear = true //state = this.metaReducer.sf(state, 'data.other.isWholeYear', isWholeYear) return state } useDateChange = (state, initDate) => { state = this.metaReducer.sf(state, 'data.form.beginDeprPeriod', fromJS(initDate)) return state } addUnitProject = (state, path, ret) => { if (path === 'project' && !ret.isEnable) { return state } state = this.metaReducer.sf(state, 'data.form.' + path, fromJS(ret)) if (path == "unit") path = 'unit' let list = this.metaReducer.gf(state, 'data.other.' + path).toJS() list.push(ret) state = this.metaReducer.sf(state, 'data.other.' + path, fromJS(list)) return state } usesChange = (state, path, value) => { state = this.metaReducer.sf(state, path, fromJS(value)) return state } moreClick = (state, value) => { state = this.metaReducer.sf(state, 'data.other.moreInfo', !value) return state } //获取项目、部门、计量单位 getProjects = (state, path, value) => { let list = value.list if (path === 'unit' || path === 'department' || path === 'project') { if (path === 'project') { list = list.filter(o => o.isEnable) } state = this.metaReducer.sf(state, 'data.other.' + path, fromJS(list)) return state } } //计算月折旧额 getDepreciationValue = (state, option, name) => { state = this.metaReducer.sf(state, 'data.form.lineDepreciation', fromJS(option.lineDepreciation)) if (name != 'lineDepreciation') { state = this.metaReducer.sf(state, 'data.other.lineDepreciation', fromJS(option.lineDepreciation)) } state = this.metaReducer.sf(state, 'data.form.initDeprMonths', fromJS(option.initDeprMonths)) state = this.metaReducer.sf(state, 'data.form.initAccuDepreciation', fromJS(option.initAccuDepreciation)) return state } setIsSaveAndNew = (state, name) => { if (name == 'saveAndNew') { state = this.metaReducer.sf(state, 'data.other.isSaveAndNew', true) } return state } getBeginUseTime = (beginUseTime) => { let year = beginUseTime.substring(0, 4), mounth = beginUseTime.substring(4, 6) return `${year}-${mounth}-01` } //校验提示 openTip = (state, option, isClose) => { let data if (option) { data = option } else { data = { 'data.other.codeTip': isClose, 'data.other.nameTip': isClose, 'data.other.assetClassTip': isClose, 'data.other.buyDateTip': isClose, 'data.other.origValueTip': isClose, 'data.other.deprMonthTip': isClose, 'data.other.initDeprMonthsTip': isClose, 'data.other.initAccuDepreciationTip': isClose, 'data.other.beginDeprPeriodTip': isClose, 'data.other.deprMethodTip': isClose, 'data.other.salvageRateTip': isClose, 'data.other.specificationTip': isClose, 'data.other.memoTip': isClose, 'data.other.lineDepreciationTip': isClose, 'data.other.usesTip': isClose, } } state = this.metaReducer.sfs(state, data) return state } } export default function creator(option) { const metaReducer = new MetaReducer(option), o = new reducer({ ...option, metaReducer }) return { ...metaReducer, ...o } }