UNPKG

ttk-app-core

Version:
410 lines (394 loc) 15.3 kB
import React from 'react' import { action as MetaAction, AppLoader } from 'edf-meta-engine' import {Icon} from 'edf-component' import renderColumns from './utils/renderColumns' import config from './config' import {Tree, Form, Select, Input} from 'edf-component' import extend from './extend' import { fromJS } from 'immutable' import utils from 'edf-utils' class action { constructor(option) { this.metaAction = option.metaAction this.extendAction = option.extendAction this.voucherAction = option.voucherAction this.config = config.current this.webapi = this.config.webapi } onInit = ({ component, injections }) => { this.extendAction.gridAction.onInit({ component, injections }) this.component = component this.injections = injections if (this.component.props.setOkListener) { this.component.props.setOkListener(this.onOk) } let addEventListener = this.component.props.addEventListener if (addEventListener) { addEventListener('onTabFocus', :: this.onTabFocus) } injections.reduce('init') this.load() } load = async () => { const { enabledMonth, enabledYear, periodDate } = this.metaAction.context.get('currentOrg') //获取全局的启用日期 if(periodDate) { const date = utils.date.transformMomentDate(periodDate) this.injections.reduce('upDateStart', 'data.other.searchValue.beginDate', date) this.injections.reduce('upDateStart', 'data.other.searchValue.endDate', date) } let date, beginDate, endDate if(periodDate){ beginDate = periodDate endDate = periodDate }else{ date = this.metaAction.gf('data.other.searchValue').toJS() date = this.changeDate(date.beginDate, date.endDate) beginDate = date[0] endDate = date[1] } const inventoryId = this.component.props.accessType // 存货id let filter = { beginAccountingYear: beginDate && beginDate.slice(0,4), beginAccountingPeriod: beginDate && beginDate.substring(5), endAccountingYear: endDate && endDate.slice(0,4), endAccountingPeriod: endDate && endDate.substring(5), inventoryId } this.metaAction.sf('data.other.loading', true) let response = await this.webapi.inventoryDetail.initDetail(filter) setTimeout(() => { this.onResize() }, 50) this.metaAction.sf('data.other.loading', false) if(response){ this.metaAction.sf('data.tableKey', Math.random()) this.injections.reduce('load', response) } } componentWillUnmount = () => { let removeEventListener = this.component.props.removeEventListener if (removeEventListener) { removeEventListener('onTabAdd') } if (window.removeEventListener) { window.removeEventListener('resize', this.onResize, false) } else if (window.detachEvent) { window.detachEvent('onresize', this.onResize) } else { window.onresize = undefined } } componentDidMount = () => { if (window.addEventListener) { window.addEventListener('resize', this.onResize, false) } else if (window.attachEvent) { window.attachEvent('onresize', this.onResize) } else { window.onresize = this.onResize } } onResize = (e) => { let keyRandomTab = Math.floor(Math.random() * 10000) this.keyRandomTab = keyRandomTab setTimeout(() => { if (keyRandomTab == this.keyRandomTab) { this.getTableScroll('ttk-scm-app-warehouseDetail-table', 'ant-table-thead', 1, 'ant-table-body', 'data.tableOption', e) } }, 50) } getTableScroll = (contaienr, head, num, target, path, e) => { try { const tableCon = document.getElementsByClassName(contaienr)[0] if (!tableCon) { if (e) { return } setTimeout(() => { this.getTableScroll(contaienr, head, num, target, path) }, 100) return } const header = tableCon.getElementsByClassName(head)[0] const body = tableCon.getElementsByClassName(target)[0].getElementsByTagName('table')[0] const pre = this.metaAction.gf(path).toJS() const y = tableCon.offsetHeight - header.offsetHeight - num const bodyHeight = body.offsetHeight if (bodyHeight > y && y != pre.y) { this.metaAction.sf(path, fromJS({ ...pre, y })) } else if (bodyHeight < y && pre.y != null) { this.metaAction.sf(path, fromJS({ ...pre, y: null })) } else { return false } } catch (err) { console.log(err) } } onTabFocus = () => { this.getDetail() } refresh = () => { this.getDetail() } //获取时间选项 getNormalDateValue = () => { const data = this.metaAction.gf('data.other.searchValue').toJS() let arr = [] arr.push(data.beginDate) arr.push(data.endDate) return arr } // 查询搜索 getDetail = async (v) => { let searchValue = this.metaAction.gf('data.other.searchValue').toJS() const date = this.changeDate(searchValue.beginDate, searchValue.endDate) const inventoryPropertyId = searchValue.type const businessTypeId = searchValue.business const inventoryId = this.component.props.accessType // 存货id let conds if(v) conds = v let filter = { beginAccountingYear: date[0].slice(0,4), beginAccountingPeriod: date[0].substring(5), endAccountingYear: date[1].slice(0,4), endAccountingPeriod: date[1].substring(5), inventoryPropertyId, inventoryId, businessTypeId, conds } this.metaAction.sf('data.other.loading', true) let response = await this.webapi.inventoryDetail.getInventoryDetail(filter) if(response) this.injections.reduce('load', response) this.metaAction.sf('data.other.loading', false) setTimeout(() => { this.onResize() }, 50) } filterOptionSummary = (input, option) => { if (option && option.props && option.props.children) { return option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0 } return true } getSearchCard = (childrenRef) => { this.searchCard = childrenRef } changeDate = (date1,date2) => { let arr = [] const value1 = this.metaAction.momentToString(date1,'YYYY-MM') const value2 = this.metaAction.momentToString(date2,'YYYY-MM') arr.push(value1) arr.push(value2) return arr } // 高级查询 searchValueChange = (value) => { this.injections.reduce('searchUpdate', value) this.getDetail() } onPanelChange = (value) => { this.metaAction.sfs({ 'data.other.searchValue.beginDate': value[0], 'data.other.searchValue.endDate': value[1] }) this.getDetail() } delete = async() => { console.log(this.selectedOption) if(!this.selectedOption) { this.metaAction.toast('error', '请选择要删除的数据') return false } const ret = await this.metaAction.modal('confirm', { title: '删除', content: '确定删除所选明细?' }) if(ret){ let delArr = [], delObj = {}, res if(this.selectedOption.length<2){ delObj = { id: this.selectedOption[0].voucherId, ts: this.selectedOption[0].ts, } res = await this.webapi.inventoryDetail.delete(delObj) }else{ this.selectedOption.map(item=>{ delArr.push({ id: item.voucherId, ts: item.ts }) }) res = await this.webapi.inventoryDetail.delDetails(delArr) } if(res) { this.metaAction.toast('success', '删除成功') this.load() } } } cell = (text, row, index) => { let obj return obj = { children: <span title={text}>{text}</span>, } } cellIsAmount = (text, row, index) => { let obj if(!text) return text = utils.number.format(text,2) return obj = { children: <span title={text}>{text}</span>, } } cellNoAmount = (text, row, index) => { let obj if(!text) return text = utils.number.format(text,6) return obj = { children: <span title={text}>{text}</span>, } } cellA = (text, row, index) => { let obj return obj = { children: <a onClick={()=>this.toInventory(row.voucherId)} title={text}>{text}</a>, } } tableColumns = () => { let cellArr = [this.cell, this.cellA, this.cellIsAmount, this.cellNoAmount] return renderColumns(...cellArr) } checkboxChange = (arr, itemArr) => { console.log(arr, itemArr) let newArr = [] arr.forEach(item => { if( item ){ newArr.push(item) } }) let newItemArr = [] itemArr.map(item => { if( item ){ newItemArr.push(item) } }) this.injections.reduce('update', { path: 'data.tableCheckbox', value: { checkboxValue: newArr, selectedOption: newItemArr } }) this.selectedOption = newItemArr } rowSelection = (text, row, index) => { return undefined } // 存货分类 selectType = (v) => { if(v){ this.metaAction.sf('data.form.typeId', v.value) this.metaAction.sf('data.other.searchValue.type', v.value) }else{ this.metaAction.sf('data.form.typeId', undefined) this.metaAction.sf('data.other.searchValue.type', undefined) } this.getDetail() } // 业务类型 selectBusinessType = (v) => { if(v){ this.metaAction.sfs({ 'data.form.businessId': v.value, 'data.other.searchValue.business': v.value }) }else{ this.metaAction.sfs({ 'data.form.businessId': undefined, 'data.other.searchValue.business': undefined }) } this.getDetail() } // 模糊查询 searchList = (v) => { this.getDetail(v) } toInventory = async (voucherId) => { // 库存单据 if (!this.config.apps['ttk-scm-app-inventory-documents']) { throw '依赖ttk-scm-app-inventory-documents app,请使用mk clone ttk-scm-app-inventory-documents命令添加' } this.component.props.setPortalContent && this.component.props.setPortalContent('库存单据', 'ttk-scm-app-inventory-documents',{accessType: 1, id:voucherId}) } inventory = async () => { // 库存单据 if (!this.config.apps['ttk-scm-app-inventory-documents']) { throw '依赖ttk-scm-app-inventory-documents app,请使用mk clone ttk-scm-app-inventory-documents命令添加' } this.component.props.setPortalContent && this.component.props.setPortalContent('库存单据', 'ttk-scm-app-inventory-documents') } linkToEstimateList = () => { this.component.props.setPortalContent && this.component.props.setPortalContent('暂估存货', 'ttk-scm-app-estimate-list') } print = async() => { let list = this.metaAction.gf('data.other.tableList') list = list.size ? list.toJS() : list if(list.length){ let searchValue = this.metaAction.gf('data.other.searchValue').toJS() const date = this.changeDate(searchValue.beginDate, searchValue.endDate) const inventoryPropertyId = searchValue.type const businessTypeId = searchValue.business const inventoryId = this.component.props.accessType // 存货id let filter = { beginAccountingYear: date[0].slice(0,4), beginAccountingPeriod: date[0].substring(5), endAccountingYear: date[1].slice(0,4), endAccountingPeriod: date[1].substring(5), inventoryPropertyId, inventoryId, businessTypeId } let res = await this.webapi.inventoryDetail.print(filter) if(res) this.metaAction.toast('success', '打印成功') }else{ this.metaAction.toast('warning', '当前没有可打印数据') return false } } export = async() => { let list = this.metaAction.gf('data.other.tableList') list = list.size ? list.toJS() : list if(list.length){ let searchValue = this.metaAction.gf('data.other.searchValue').toJS() const date = this.changeDate(searchValue.beginDate, searchValue.endDate) const inventoryPropertyId = searchValue.type const businessTypeId = searchValue.business const inventoryId = this.component.props.accessType // 存货id let filter = { beginAccountingYear: date[0].slice(0,4), beginAccountingPeriod: date[0].substring(5), endAccountingYear: date[1].slice(0,4), endAccountingPeriod: date[1].substring(5), inventoryPropertyId, inventoryId, businessTypeId } let res = await this.webapi.inventoryDetail.export(filter) if(res) this.metaAction.toast('success', '导出成功') }else{ this.metaAction.toast('warning', '当前没有可导出数据') return false } } } export default function creator(option) { const metaAction = new MetaAction(option), extendAction = extend.actionCreator({ ...option, metaAction }), o = new action({ ...option, metaAction, extendAction }), ret = { ...metaAction, ...extendAction.gridAction, ...o } metaAction.config({ metaHandlers: ret }) return ret }