ttk-app-core
Version:
enterprise develop framework
400 lines (381 loc) • 15.3 kB
JavaScript
import React from 'react'
import { action as MetaAction, AppLoader } from 'edf-meta-engine'
import { Select, Button, Modal, Icon, PrintOption, FormDecorator, Checkbox } from 'edf-component'
import { fromJS, toJS } from 'immutable'
import utils from 'edf-utils'
import sortSearchOption from './utils/sortSearchOption' //格式化搜索条件
import moment from 'moment'
import config from './config'
const Option = Select.Option
import { consts } from 'edf-consts'
const checkboxKey = 'invoiceTypeName';//table的主键
class action {
constructor(option) {
this.metaAction = option.metaAction
this.config = config.current
this.webapi = this.config.webapi
this.voucherAction = option.voucherAction
}
onInit = ({ component, injections }) => {
this.component = component
this.injections = injections
if (this.component.props.setOkListener) {
this.component.props.setOkListener(this.onOk)
}
if (this.component.props.setCancelLister) {
this.component.props.setCancelLister(this.onCancel)
}
injections.reduce('init', { dateVisible: this.component.props.dateVisible, vatOrEntry: this.component.props.vatOrEntry })
if (!this.component.props.dateVisible) {
//本次采集结果
if (this.component.props.resultData) {
this.load(this.component.props.resultData)
}
} else {
//合计
this.load()
}
}
refresh = () => {
this.sortParmas()
}
print = async () => {
const list = this.metaAction.gf('data.list').toJS()
if (list.length == 0) {
this.metaAction.toast('warn', '当前没有可打印的数据!')
return
}
const params = this.sortParmas('get');
const res = await this.webapi.print(params)
}
export = () => {
const list = this.metaAction.gf('data.list').toJS()
if (list.length == 0) {
this.metaAction.toast('warn', '当前没有可导出的数据!')
return
}
const params = this.sortParmas('get');
const res = this.webapi.export(params);
}
load = (res) => {
if (res) {
this.injections.reduce('load', { res, isInit: true, dateVisible: false })
setTimeout(() => {
this.getTableScroll()
}, 100)
} else {
if (this.component.props.defaultdate) {
this.injections.reduce('update', {
path: 'data.date',
value: this.component.props.defaultdate
})
}
this.sortParmas('init')
}
}
componentDidMount = () => {
this.onResize()
const win = window
if (win.addEventListener) {
win.addEventListener('resize', this.onResize, false)
} else if (win.attachEvent) {
win.attachEvent('onresize', this.onResize)
} else {
win.onresize = this.onResize
}
}
onOk = async () => {
this.component.props.closeModal();
}
dateChange = async (date) => {
this.injections.reduce('dateChange', date);
this.sortParmas()
}
getTableScroll = () => {
try {
let tableOption = this.metaAction.gf('data.tableOption').toJS()
let dom = document.getElementsByClassName('ttk-scm-app-collect-result-Body')[0]
if (!dom) {
return
}
let tableDom = dom.getElementsByClassName('ant-table-tbody')[0];
if (tableDom && dom) {
let num = dom.offsetHeight - tableDom.offsetHeight
if (num < 73) {
const width = dom.offsetWidth
const height = dom.offsetHeight
this.injections.reduce('setTableOption', { ...tableOption, y: height - 73, containerWidth: width - 20 })
} else {
delete tableOption.y
this.injections.reduce('update', { path: 'data.tableOption', value: tableOption })
}
}
} catch (err) {
console.log(err)
}
}
onResize = (type) => {
let keyRandom = Math.floor(Math.random() * 10000)
this.keyRandom = keyRandom
setTimeout(() => {
if (this.keyRandom == keyRandom) {
let dom = document.getElementsByClassName('ttk-scm-app-collect-result-Body')[0]
// let tableDom
let tableOption = this.metaAction.gf('data.tableOption').toJS()
if (!dom) {
if (type) {
return
}
setTimeout(() => {
this.onResize()
}, 20)
} else {
let tableDom = dom.getElementsByClassName('ant-table-tbody')[0];
let num = dom.offsetHeight - tableDom.offsetHeight
if (num < 73) {
// let tableOption = this.metaAction.gf('data.tableOption').toJS()
const width = dom.offsetWidth
const height = dom.offsetHeight
this.injections.reduce('setTableOption', { ...tableOption, y: height - 73, containerWidth: width - 20 })
} else {
delete tableOption.y
this.injections.reduce('update', { path: 'data.tableOption', value: tableOption })
}
}
}
}, 100)
}
//渲染列
renderColumns = () => {
const vatOrEntry = this.metaAction.gf('data.vatOrEntry');
if (vatOrEntry == 0) {
return [
{
title: '发票类型',
key: 'type',
className: 'table_td_align_left',
// width: 102,
dataIndex: 'invoiceTypeName'
},
{
title: '开票份数',
key: 'kpfs',
children: [
{
title: '总份数',
dataIndex: 'invoiceSum',
className: 'table_td_align_right',
width: 102,
key: 'invoiceSum'
},
{
title: '其中:红字',
dataIndex: 'invoiceRedSum',
className: 'table_td_align_right',
width: 102,
key: 'invoiceRedSum'
}, {
title: '其中:蓝字',
dataIndex: 'invoicebuleSum',
className: 'table_td_align_right',
key: 'invoicebuleSum',
width: 102,
}
, {
title: '其中:作废',
dataIndex: 'invoiceStateSum',
className: 'table_td_align_right',
key: 'invoiceStateSum',
width: 102,
}
]
},
{
title: '金额合计',
className: 'table_td_align_right',
dataIndex: 'amountTotal',
key: 'amountTotal',
width: 102,
render: (text, record, index) => this.normalTdRender(text, record, index, 'amountTotal')
},
{
title: '税额合计',
className: 'table_td_align_right',
dataIndex: 'taxTotal',
key: 'taxTotal',
width: 102,
render: (text, record, index) => this.normalTdRender(text, record, index, 'taxTotal')
},
{
title: '价税合计',
dataIndex: 'amountAndTaxTotal',
className: 'table_td_align_right',
key: 'amountAndTaxTotal',
width: 102,
render: (text, record, index) => this.normalTdRender(text, record, index, 'amountAndTaxTotal')
}
];
} else {
return [
{
title: '发票类型',
key: 'type',
className: 'table_td_align_left',
// width: 102,
dataIndex: 'invoiceTypeName'
},
{
title: '开票份数',
key: 'kpfs',
children: [
{
title: '总份数',
dataIndex: 'invoiceSum',
width: 102,
className: 'table_td_align_right',
key: 'invoiceSum'
},
{
title: '其中:红字',
dataIndex: 'invoiceRedSum',
width: 102,
className: 'table_td_align_right',
key: 'invoiceRedSum'
}, {
title: '其中:蓝字',
dataIndex: 'invoicebuleSum',
width: 102,
className: 'table_td_align_right',
key: 'invoicebuleSum'
}
]
},
{
title: '金额合计',
className: 'table_td_align_right',
dataIndex: 'amountTotal',
width: 102,
key: 'amountTotal',
render: (text, record, index) => this.normalTdRender(text, record, index, 'amountTotal')
},
{
title: '税额合计',
className: 'table_td_align_right',
dataIndex: 'taxTotal',
width: 102,
key: 'taxTotal',
render: (text, record, index) => this.normalTdRender(text, record, index, 'taxTotal')
},
{
title: '价税合计',
dataIndex: 'amountAndTaxTotal',
width: 102,
className: 'table_td_align_right',
key: 'amountAndTaxTotal',
render: (text, record, index) => this.normalTdRender(text, record, index, 'amountAndTaxTotal')
}
];
}
}
normalTdRender = (text, record, index, key) => {
return <span className="ttk-table-app-list-td-con" title={this.transformThoundsNumber(text, key)}>{this.transformThoundsNumber(text, key)}</span>
}
//数字的转化
transformThoundsNumber = (text, key) => {
if (text == 0) {
return '0.00'
}
const arr = ['amountTotal', 'taxTotal', 'amountAndTaxTotal', 'price']
if (arr.includes(key)) {
if (!text || parseFloat(text) == 0 || isNaN(parseInt(text))) {
return ''
}
if (key == 'price') {
return utils.number.format(text, 6)
} else {
return utils.number.format(text, 2)
}
} else {
return text
}
}
renderRowClassName = (record, index) => {
if (record[checkboxKey] == '合计') {
return 'tr_heji'
}
else {
return 'tr_normal'
}
}
sortParmas = (type) => {
const voucherMoment = moment(this.metaAction.gf('data.date'))
const vatOrEntry = this.metaAction.gf('data.vatOrEntry');
let date = voucherMoment.format('YYYY-MM');//当前月份
let begin = moment(date).startOf('month').format('YYYY-MM-DD HH:mm:ss')//起始日期
let end = moment(date).endOf('month').format('YYYY-MM-DD HH:mm:ss')//结束日期
let params = {
countFlag: 0,
vatOrEntry,
begin,
end
}
if (type == 'get') {
return { ...params }
} else if (type == 'init') {
this.initData(params)
} else {
this.requestData(params)
}
}
//初始化数据
initData = async (params) => {
let res = await this.webapi.queryInvoiceSum(params);
this.injections.reduce('load', { res: res.list })
setTimeout(() => {
this.getTableScroll()
}, 100)
}
//请求数据
requestData = async (params) => {
let loading = this.metaAction.gf('data.loading')
if (!loading) {
this.injections.reduce('tableLoading', true)
}
let res = await this.webapi.queryInvoiceSum(params)
this.injections.reduce('load', { res: res.list })
setTimeout(() => {
this.getTableScroll()
}, 100)
}
tableOnchange = async (pagination, filters, sorter) => {
// const { checkboxKey, order } = sorter
// const response = await this.webapi.report.query(sorter)
// this.injections.reduce('tableOnchange', response.value.details)
}
componentWillUnmount = () => {
// if (this.dateDom) {
// this.dateDom.removeEventListener('click', this.rangePickerClick)
// }
if (this.props && this.props.isFix === true) return
const win = window
if (win.removeEventListener) {
win.removeEventListener('resize', this.onResize, false)
} else if (win.detachEvent) {
win.detachEvent('onresize', this.onResize)
} else {
win.onresize = undefined
}
}
disabledRangePicker = (currentDate) => {
return currentDate.format('YYYYMM') < moment(this.component.props.enableddate).format('YYYYMM')
//|| currentDate.format('YYYYMM') > moment().format('YYYYMM')
}
}
export default function creator(option) {
const metaAction = new MetaAction(option),
voucherAction = FormDecorator.actionCreator({ ...option, metaAction }),//装饰器 使用metaAtjion为参数 实现扩展
o = new action({ ...option, metaAction, voucherAction }),
ret = { ...metaAction, ...voucherAction, ...o }
metaAction.config({ metaHandlers: ret })
return ret
}