ttk-app-core
Version:
enterprise develop framework
388 lines (352 loc) • 14.5 kB
JavaScript
import React from 'react'
import ReactDOM from 'react-dom'
import { action as MetaAction, AppLoader } from 'edf-meta-engine'
import { fromJS } from 'immutable'
import config from './config'
import moment from 'moment'
import utils from 'edf-utils'
import extend from './extend'
import { consts } from 'edf-consts'
class action {
constructor(option) {
this.metaAction = option.metaAction
this.extendAction = option.extendAction
this.config = config.current
this.webapi = this.config.webapi
this.menuList = {}
}
onInit = ({ component, injections }) => {
this.extendAction.gridAction.onInit({ component, injections })
this.component = component
this.injections = injections
let option = {
isGuide: this.component.props.isGuide,
menuKey: this.component.props.isMenuCode
}
this.menuList = option
injections.reduce('init', option)
let addEventListener = this.component.props.addEventListener
if (addEventListener) {
addEventListener('onTabFocus', :: this.onTabFocus)
}
this.load()
}
componentWillUnmount = () => {
window['app-account-subjects-content-grid'] = 0
}
load = () => {
let currentOrg = this.metaAction.context.get("currentOrg"),
accountingStandards = currentOrg.accountingStandards == consts.ACCOUNTINGSTANDARDS_2013
this.metaAction.sf('data.other.accountingStandards', accountingStandards)
this.changeSubjects()
}
//当前app的 "tab被点击" (从其他app切换到当前app)
onTabFocus = () => {
let currentOrg = this.metaAction.context.get("currentOrg"),
accountingStandards = currentOrg.accountingStandards == consts.ACCOUNTINGSTANDARDS_2013
let data = this.metaAction.gf('data').toJS()
if (accountingStandards != data.other.accountingStandards && data.filter.targetList == '5000010003') {
this.metaAction.sf('data.other.accountingStandards', accountingStandards)
this.tabChange('all')
} else {
this.metaAction.sf('data.other.accountingStandards', accountingStandards)
let targetList = this.metaAction.gf('data.filter.targetList')
this.changeSubjects(targetList)
}
}
//修改科目编码规则
setSubjectCode = async () => {
const ret = await this.metaAction.modal('show', {
title: '科目编码设置',
width: 650,
bodyStyle: { padding: 6, fontSize: 12 },
children: this.metaAction.loadApp('app-account-subjects-code', {
store: this.component.props.store
})
})
if (ret) {
console.log(ret)
}
}
//新增按钮是否可用
disabledState = (subject) => {
let state = false,
{ code, grade, cashTypeId } = subject
if ((cashTypeId == consts.CASHTYPE_003 && grade != 1)
|| (code.indexOf('1012') != -1 && grade != 1 && grade != 2)
|| (code == '22210401')
|| (code.indexOf('1001') != -1 && grade == 1)
|| (cashTypeId == consts.CASHTYPE_033 && grade != 1)
|| ((cashTypeId == consts.CASHTYPE_034 || cashTypeId == consts.CASHTYPE_035 || cashTypeId == consts.CASHTYPE_036 || cashTypeId == consts.CASHTYPE_037) && grade != 2)
|| (code == '22210109')
|| (cashTypeId == consts.CASHTYPE_005) || (grade == 5)) {
state = true
}
return state
}
addSubject = (data) => async () => {
//新增
let { id, code, grade, cashTypeId } = data
if (grade == 5) {
this.metaAction.toast('warning', '已经是第五级科目,无法再增加下级科目')
return
}
if ((cashTypeId == consts.CASHTYPE_003 && grade != 1)
|| (code.indexOf('1012') != -1 && grade != 1 && grade != 2)
|| (code == '22210401')
|| (code.indexOf('1001') != -1 && grade == 1)
|| (cashTypeId == consts.CASHTYPE_033 && grade != 1)
|| ((cashTypeId == consts.CASHTYPE_034 || cashTypeId == consts.CASHTYPE_035 || cashTypeId == consts.CASHTYPE_036 || cashTypeId == consts.CASHTYPE_037) && grade != 2)
|| (code == '22210109')
|| (cashTypeId == consts.CASHTYPE_005)) {
return
}
const oldData = this.metaAction.gf('data').toJS()
let { list, other } = oldData
const ret = await this.metaAction.modal('show', {
title: '科目',
width: 400,
okText: '保存',
style: { top: 40 },
bodyStyle: { padding: 24, fontSize: 12 },
children: this.metaAction.loadApp('app-proof-of-charge-subjects-add', {
store: this.component.props.store,
active: 'add',
newCode: generateNewSubCode(id, code, list),
data: data,
})
})
if (ret) {
this.changeSubjects()
//本地刷新
// this.injections.reduce('addSubject', ret, data.id)
}
}
batchAddSubject = (data) => async () => {
//批量新增
let { id, code, grade, cashTypeId } = data
if (grade == 5) {
this.metaAction.toast('warning', '已经是第五级科目,无法再增加下级科目')
return
}
if ((cashTypeId == consts.CASHTYPE_003 && grade != 1)
|| (code.indexOf('1012') != -1 && grade != 1 && grade != 2)
|| (code == '22210401')
|| (code.indexOf('1001') != -1 && grade == 1)
|| (cashTypeId == consts.CASHTYPE_033 && grade != 1)
|| ((cashTypeId == consts.CASHTYPE_034 || cashTypeId == consts.CASHTYPE_035 || cashTypeId == consts.CASHTYPE_036 || cashTypeId == consts.CASHTYPE_037) && grade != 2)
|| (code == '22210109')
|| (cashTypeId == consts.CASHTYPE_005)) {
return
}
const ret = await this.metaAction.modal('show', {
title: '可粘贴科目名称,多个科目请换行',
width: 400,
okText: '确定',
style: { top: 140 },
bodyStyle: { padding: 24, fontSize: 12 },
children: this.metaAction.loadApp('app-account-subjects-batch-add', {
store: this.component.props.store,
parentId: id
})
})
if (ret) {
let option = {
parentId: id,
accountNameList: ret
}
const response = await this.webapi.batchAdd(option)
if (response) {
this.metaAction.toast('success', this.message(response.message), response.message.indexOf('<br>') != -1 ? 3 : 1)
this.changeSubjects()
}
}
}
message = (message) => {
message = message.split('<br>')
return (
<div>
{
message.map((o, i) => {
return (
<div>{o}</div>
)
})
}
</div>
)
}
//编辑
editSubject = (data) => async () => {
const list = this.metaAction.gf('data.list').toJS()
const ret = await this.metaAction.modal('show', {
title: '科目',
width: 400,
style: { top: 40 },
okText: '保存',
bodyStyle: { padding: 24, fontSize: 12 },
children: this.metaAction.loadApp('app-proof-of-charge-subjects-add', {
store: this.component.props.store,
active: 'edit',
data: data,
parentSubject: getParentSubject(list, data.parentId),
})
})
if (ret) {
this.changeSubjects()
//本地刷新
// this.injections.reduce('updateSubject', ret)
}
}
deleteSubject = (data) => async () => {
if (!(!data.isSystem && data.isEndNode) || !data.isEnable)
return
const ret = await this.metaAction.modal('confirm', {
content: '确认删除?'
})
if (ret) {
// const newData = await this.webapi.find(data)
let { id, parentId, ts, code } = data, option = {}
option.id = id
option.ts = ts
const response = await this.webapi.delete(option)
let messageText, isDelCurSubject
//判断后端是否删除成功
if (response == '') {
isDelCurSubject = true
this.metaAction.toast('success', '删除成功')
this.changeSubjects()
//本地刷新
// this.injections.reduce('deleteSubject', id, parentId, isDelCurSubject)
}
}
}
changeSubjects = async (activeKey) => {
const newData = this.metaAction.gf('data').toJS()
let option = {},
key = activeKey || newData.filter.targetList
if (key !== 'all') {
option.accountTypeId = key
}
if (activeKey && !newData.other.loading) {
this.metaAction.sf('data.other.loading', true)
}
const response = await this.webapi.query(option)
newData.list = response.glAccounts
newData.other.calcDict = { ...response.calcDict }
newData.other.loading = false
const appExtendParams = this.component.props && this.component.props.appExtendParams
if (appExtendParams){
const dynParams ='subjects'
await this.webapi.handEnteringStateTwo({ dynParams})
}
this.injections.reduce('load', newData, appExtendParams)
}
onExit = (tour) => {
const intro = tour
if (intro.action == 'skip' || intro.status == 'finished') {
let stepEnabled = this.metaAction.gf('data.other.stepEnabled')
if (stepEnabled == true) {
this.metaAction.sf('data.other.stepEnabled', false)
let params = { "menuId": this.menuList.menuKey, "isVisible": false }
this.webapi.updateGuide(params)
this.component.props.closeGuide &&
this.component.props.closeGuide(this.component.props.appName)
}
}
}
/**
* 财务期初-上一步
*/
preStep = async () => {
if (this.component.props) {
const appParams = this.component.props && this.component.props.appExtendParams
this.component.props.setPortalContent('上传数据', 'ttk-gl-app-financeinit-uploaddata', appParams)
}
}
/**
* 财务期初-下一步
*/
nextStep = async () => {
if (this.component.props) {
const appParams = this.component.props && this.component.props.appExtendParams,
preStep = 'app-account-subjects'
//ttk-gl-app-finance-periodbegin app-account-beginbalance
this.component.props.setPortalContent('确认数据', 'ttk-gl-app-finance-periodbegin', { preStep, ...appParams })
}
}
tabChange = async (key) => {
this.metaAction.sf('data.filter.targetList', key)
this.metaAction.sf('data.other.loading', true)
this.changeSubjects(key)
window['app-account-subjects-content-grid'] = 0
}
numberFormat = utils.number.format
}
export default function creator(option) {
const metaAction = new MetaAction(option),
extendAction = extend.actionCreator({ ...option, metaAction }),
o = new action({ ...option, metaAction, extendAction })
const ret = { ...metaAction, ...extendAction.gridAction, ...o }
metaAction.config({ metaHandlers: ret })
return ret
}
//获取父级科目信息
function getParentSubject(subjectList, parentSubjectId) {
////
if (!parentSubjectId) {
return { id: null, code: '无父级科目', name: '', codeAndName: null, accountTypeId: null }
}
// let arr = subjectList
let parentSubject = subjectList.filter(subItem => {
return parseInt(subItem.id) == parseInt(parentSubjectId)
})
let ret
if (parentSubject && parentSubject.length > 0)
ret = {
id: parentSubject[0].id,
code: parentSubject[0].code,
name: parentSubject[0].name,
codeAndName: parentSubject[0].codeAndName,
accountTypeId: parentSubject[0].accountTypeId
}
else
ret = { id: null, code: '无父级科目', name: null, codeAndName: null, accountTypeId: null }
return ret
}
//生成新的科目编码
function generateNewSubCode(parentId, parentCode, subjectList) {
let newCode,
newSubjectList = subjectList.filter(subItem => {
return parseInt(subItem.parentId) == parseInt(parentId)
})
if (newSubjectList.length == 0) {
newCode = '01'
} else {
let endGradeList = []
for (var i = 0; i < newSubjectList.length; i++) {
let code = newSubjectList[i].code
endGradeList.push(code.substring(code.length - 2))
}
endGradeList = endGradeList.sort(sortNumber)
let maxCode = endGradeList[endGradeList.length - 1]
if (maxCode == '99' && endGradeList.length < 98) {
maxCode = endGradeList[endGradeList.length - 2]
if (!isNaN(maxCode)) {
newCode = '00' + (parseInt(maxCode) + 1).toString()
newCode = newCode.substring(newCode.length - 2)
} else {
newCode = '00'
}
} else if (maxCode != '99' && !isNaN(maxCode)) {
newCode = '00' + (parseInt(maxCode) + 1).toString()
newCode = newCode.substring(newCode.length - 2)
} else {
newCode = '00'
}
}
return newCode
}
function sortNumber(a, b) {
return a - b
}