import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Button, Select, Input, Modal, DatePicker, notification } from 'antd'
|
import moment from 'moment'
|
|
import Api from '@/api'
|
import asyncComponent from '@/utils/asyncComponent'
|
import MKEmitter from '@/utils/events.js'
|
import './index.scss'
|
|
const { confirm } = Modal
|
const VoucherTable = asyncComponent(() => import('./voucherTable'))
|
|
class VoucherModule extends Component {
|
static propTpyes = {
|
config: PropTypes.object, // 组件配置信息
|
}
|
|
state = {
|
BID: '',
|
type: 'createVoucher',
|
config: null,
|
loading: false,
|
data: [],
|
tbdata: [],
|
typeOptions: [],
|
charType: '',
|
charInt: '',
|
vouDate: null,
|
book: null,
|
username: sessionStorage.getItem('User_Name'),
|
status: '' // 新建时,empty、change、saved
|
}
|
|
UNSAFE_componentWillMount () {
|
const { config } = this.props
|
|
let BID = ''
|
let BData = ''
|
|
if (config.wrap.supModule) {
|
BData = window.GLOB.CacheData.get(config.wrap.supModule)
|
} else {
|
BData = window.GLOB.CacheData.get(config.$pageId)
|
}
|
if (BData) {
|
BID = BData.$BID || ''
|
}
|
|
window.GLOB.CacheVoucher.delete(config.uuid)
|
let type = config.wrap.type || 'createVoucher'
|
|
this.setState({
|
config: fromJS(config).toJS(),
|
type: type,
|
BID: BID || '',
|
status: 'empty',
|
book: window.GLOB.CacheData.get(config.wrap.supBook) || null
|
}, () => {
|
this.getVoucher()
|
})
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('resetSelectLine', this.resetParentParam)
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
/**
|
* @description 组件销毁,清除state更新,清除快捷键设置
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
|
MKEmitter.removeListener('resetSelectLine', this.resetParentParam)
|
}
|
|
resetParentParam = (MenuID, id, data) => {
|
const { config } = this.state
|
|
if (config.wrap.supBook === MenuID) {
|
let month = data.months
|
let vouDate = moment()
|
|
if (month && month < moment().format('YYYY-MM')) {
|
vouDate = moment(month, 'YYYY-MM').endOf('month')
|
}
|
|
this.setState({ book: data, vouDate }, () => {
|
this.loadData()
|
})
|
return
|
}
|
|
if (!config.wrap.supModule || config.wrap.supModule !== MenuID) return
|
if (id !== this.state.BID || id !== '') {
|
this.setState({ BID: id, BData: data }, () => {
|
this.getVoucher()
|
})
|
}
|
}
|
|
loadData = () => {
|
const { book, config } = this.state
|
|
if (!book) return
|
|
let param = {
|
func: 's_get_fcc_account_data',
|
account_code: book.account_code || '',
|
fcc_date: book.months ? book.months + '-01' : moment().format('YYYY-MM-DD')
|
}
|
|
Api.genericInterface(param).then(res => {
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
return
|
}
|
|
let typeOptions = res.char || []
|
|
this.setState({
|
typeOptions: typeOptions,
|
charType: typeOptions[0] ? typeOptions[0].voucher_class : '',
|
charInt: typeOptions[0] ? typeOptions[0].voucher_char_int : '',
|
})
|
|
let names = {}
|
let supplier = []
|
let customer = []
|
let department = []
|
let project = []
|
let inventory = []
|
let employee = []
|
let cash_flow = []
|
|
res.sup && res.sup.forEach(item => {
|
names[item.sup_type_code] = item.sup_type_name
|
})
|
|
res.supplier && res.supplier.forEach(item => {
|
supplier.push({value: item.suppliercode, label: item.suppliername})
|
})
|
|
res.customer && res.customer.forEach(item => {
|
customer.push({value: item.customercode, label: item.customername})
|
})
|
|
res.co_pro && res.co_pro.forEach(item => {
|
department.push({value: item.co_pro_code, label: item.co_pro_name})
|
})
|
|
res.pm && res.pm.forEach(item => {
|
project.push({value: item.projectcode, label: item.projectname})
|
})
|
|
res.materiel && res.materiel.forEach(item => {
|
inventory.push({value: item.productcode, label: item.productname})
|
})
|
|
res.workers && res.workers.forEach(item => {
|
employee.push({value: item.workercode, label: item.workername})
|
})
|
|
res.cash_flow && res.cash_flow.forEach(item => {
|
cash_flow.push({value: item.cash_flow_code, label: item.cash_flow_name})
|
})
|
|
let message = {
|
subjects: res.subjects || [],
|
names: names,
|
supplier: supplier,
|
customer: customer,
|
department: department,
|
project: project,
|
inventory: inventory,
|
employee: employee,
|
cash_flow: cash_flow,
|
}
|
|
window.GLOB.CacheVoucher.set(config.uuid, message)
|
})
|
}
|
|
getVoucher = () => {
|
const { BID, type } = this.state
|
|
if (!BID || type === 'createVoucher' || type === 'createTemp') return
|
|
let param = {
|
func: 's_get_fcc_voucher',
|
BID: BID
|
}
|
|
let data = [
|
{remark: '提现', subjectscode: '1001', subjectsname: '库存现金', debtor: 124, creditor: ''},
|
{remark: '购入固定资产', subjectscode: '1001', subjectsname: '库存现金', debtor: '', creditor: 124},
|
{remark: '转结销售成本', subjectscode: '1001', subjectsname: '库存现金', debtor: -524, creditor: ''},
|
{remark: '提现', subjectscode: '1001', subjectsname: '库存现金', debtor: 34, creditor: '', i: Math.random()},
|
]
|
|
Api.genericInterface(param).then(res => {
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
return
|
}
|
|
this.setState({
|
data: data,
|
tbdata: fromJS(data).toJS()
|
})
|
})
|
}
|
|
triggersave = (t) => {
|
const { tbdata } = this.state
|
|
let err = ''
|
let tip = ''
|
let list = []
|
|
tbdata.forEach((line, index) => {
|
if (err) return
|
if (line.type === 'total') {
|
if (line.debtor !== line.creditor) {
|
err = '借贷不平衡!'
|
}
|
return
|
}
|
|
let _index = index + 1
|
|
if (!line.remark && !line.subjectscode && !line.debtor && line.debtor !== 0 && !line.creditor && line.creditor !== 0) {
|
if (_index === 1) {
|
err = '第1行不可为空。'
|
}
|
return
|
}
|
|
if (!line.remark) {
|
err = `第${_index}行,摘要不可为空。`
|
} else if (!line.subjectscode) {
|
err = `第${_index}行,科目不可为空。`
|
} else if (!line.debtor && line.debtor !== 0 && !line.creditor && line.creditor !== 0) {
|
err = `第${_index}行,请输入金额。`
|
} else if (line.debtor === 0 || line.creditor === 0) {
|
err = `第${_index}行,金额不能为0。`
|
} else if (line.sup_accounting) {
|
line.sup_accounting.split(',').forEach(item => {
|
if (!line[item]) {
|
err = `第${_index}行,请选择辅助核算。`
|
}
|
})
|
}
|
|
if (line.count_type === 'Y' && !err) {
|
if (!line.count) {
|
tip += `第${_index}行,数量为空或为0!;`
|
} else if (line.price) {
|
if (line.debtor && line.debtor !== line.count * line.price) {
|
tip += `第${_index}行,数量和金额不匹配!;`
|
} else if (line.creditor && line.creditor !== line.count * line.price) {
|
tip += `第${_index}行,数量和金额不匹配!;`
|
}
|
}
|
}
|
|
list.push(line)
|
})
|
|
if (!err && list.length === 0) {
|
err = '第1行不可为空。'
|
}
|
|
if (err) {
|
notification.warning({
|
top: 92,
|
message: err,
|
duration: 5
|
})
|
return
|
}
|
|
if (tip) {
|
const _this = this
|
confirm({
|
content: tip + '确认要保存吗?',
|
onOk() {
|
_this.voucherSave(list, t)
|
},
|
onCancel() {}
|
})
|
} else {
|
this.voucherSave(list, t)
|
}
|
}
|
|
voucherSave = (list, t) => {
|
const { BID, config, charInt, charType, book } = this.state
|
|
if (!book) {
|
notification.warning({
|
top: 92,
|
message: '请选择账套!',
|
duration: 5
|
})
|
return
|
}
|
|
let param = {
|
func: 's_fcc_voucher_addupt',
|
BID: BID,
|
voucher_code: '',
|
voucher_text: '',
|
remark: '',
|
account_year_code: '',
|
voucher_type: '',
|
voucher_type_text: '',
|
orgcode: '',
|
orgname: '',
|
voucher_class: '',
|
years: book.years,
|
business_type: '',
|
voucher_sign: '',
|
voucher_char: charType,
|
voucher_char_int: charInt,
|
account_code: book.account_code || '',
|
fibvoucherdate: '',
|
UserName: sessionStorage.getItem('User_Name') || '',
|
FullName: sessionStorage.getItem('Full_Name') || '',
|
sup_data: '',
|
subject_data: ''
|
}
|
|
Api.genericInterface(param).then(res => {
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
return
|
}
|
|
if (t === 'add') {
|
this.setState({
|
status: 'empty',
|
tbdata: [],
|
charInt: charInt + 1
|
})
|
MKEmitter.emit('cleartable', config.uuid)
|
} else {
|
this.setState({
|
status: 'saved'
|
})
|
}
|
})
|
}
|
|
triggerprint = () => {
|
|
}
|
|
dataChange = (data) => {
|
this.setState({
|
status: 'change',
|
tbdata: data
|
})
|
}
|
|
render() {
|
const { type, status, loading, config, typeOptions, charType, charInt, data, vouDate, username } = this.state
|
|
return (
|
<div className="menu-voucher-wrap" style={config.style}>
|
{type === 'createVoucher' ? <div className="voucher-header">
|
<Button className="add-background header-btn" disabled={status === 'empty'} onClick={() => this.triggersave('add')}>保存并新增</Button>
|
<Button className="add-background header-btn" disabled={status === 'empty' || status === 'saved'} onClick={() => this.triggersave()}>保存</Button>
|
<Button className="print-background header-btn" disabled={status !== 'saved'} onClick={this.triggerprint}>打印</Button>
|
<Button className="out-background header-btn" onClick={this.triggerprint}>更多</Button>
|
</div> : null}
|
<div className="voucher-body">
|
{type === 'createVoucher' ? <div className="pre-wrap">
|
<div className="voucher-code">
|
<Select value={charType} dropdownClassName="mk-vcode-dropdown" onChange={(val, option) => this.setState({charType: val, charInt: option.props.charint})}>
|
{typeOptions.map(option =>
|
<Select.Option key={option.voucher_char_int} value={option.voucher_class} charint={option.voucher_char_int}>{option.voucher_char}</Select.Option>
|
)}
|
</Select>
|
<Input value={charInt} autoComplete="off" onChange={(e) => this.setState({charInt: e.target.value})}/> 号
|
</div>
|
<div className="voucher-date">
|
日期:<DatePicker value={vouDate} onChange={(val) => this.setState({vouDate: val})}/>
|
</div>
|
<div className="voucher-affix">
|
附单据 <Input autoComplete="off" /> 张
|
<Button type="link" className="" onClick={this.triggerprint}>附件</Button>
|
<Button type="link" className="" onClick={this.triggerprint}>备注</Button>
|
</div>
|
</div> : null}
|
<VoucherTable config={config} loading={loading} data={data} onChange={this.dataChange}/>
|
</div>
|
<div className="user">制单人:{username}</div>
|
</div>
|
)
|
}
|
}
|
|
export default VoucherModule
|