ttk-app-core
Version:
enterprise develop framework
248 lines (234 loc) • 9.23 kB
JavaScript
import React from 'react'
import {action as MetaAction, AppLoader} from 'edf-meta-engine'
import config from './config'
import debounce from 'lodash.debounce'
import {FormDecorator} from 'edf-component'
class action {
constructor(option) {
this.metaAction = option.metaAction
this.voucherAction = option.voucherAction
this.config = config.current
this.webapi = this.config.webapi
this.nameChange = debounce(this.nameChange, 800);
}
onInit = ({component, injections}) => {
this.voucherAction.onInit({component, injections})
this.component = component
this.injections = injections
this.clickStatus = false
if (this.component.props.setOkListener) {
this.component.props.setOkListener(this.onOk)
}
injections.reduce('init', {
isPop: this.component.props.isPop
})
this.load()
}
load = async () => {
let data = {}, response = {}, code, account
if (this.component.props.personId || this.component.props.personId === 0) {
response = await this.webapi.customer.query(this.component.props.personId)
} else {
code = await this.webapi.customer.getCode()
}
account = await this.webapi.customer.account()
if (code) data.code = code
if (response) data.response = response
if (account && account.glAccounts) data.glAccounts = account.glAccounts
this.injections.reduce('load', data)
}
onOk = async () => {
return await this.save()
}
save = async () => {
if (this.clickStatus) return
this.clickStatus = true
const form = this.metaAction.gf('data.form').toJS()
const ok = await this.voucherAction.check([{
path: 'data.form.code', value: form.code
}, {
path: 'data.form.name', value: form.name
}, {
path: 'data.form.receivableAccountId', value: form.receivableAccountId
}], this.check)
if (!ok) {
this.metaAction.toast('warning', '请按页面提示信息修改信息后才可提交')
this.clickStatus = false
return false
}
let response
form.code = form.code ? form.code.trim() : ''
form.name = form.name ? form.name.trim() : ''
form.taxNumber = form.taxNumber ? form.taxNumber.trim() : ''
form.linkman = form.linkman ? form.linkman.trim() : ''
form.contactNumber = form.contactNumber ? form.contactNumber.trim() : ''
form.openingBank = form.openingBank ? form.openingBank.trim() : ''
form.bankAccout = form.bankAccout ? form.bankAccout.trim() : ''
form.addressAndTel = form.addressAndTel ? form.addressAndTel.trim() : ''
form.receivableAccountId = form.receivableAccountId ? form.receivableAccountId : ''
form.receivableInAdvanceAccountId = form.receivableInAdvanceAccountId ? form.receivableInAdvanceAccountId : ''
form.remotherReceivableAccountIdark = form.otherReceivableAccountId ? form.otherReceivableAccountId : ''
form.remark = form.remark ? form.remark.trim() : ''
form.isReturnValue = true
if (this.component.props.personId || this.component.props.personId === 0) {
form.id = this.component.props.personId
response = await this.webapi.customer.update(form)
} else {
response = await this.webapi.customer.create(form)
}
this.clickStatus = false
if (response && response.error) {
this.metaAction.toast('error', response.error.message)
return false
} else {
this.metaAction.toast('success', '保存成功')
return response
}
}
changeCheck = (str) => {
const form = this.metaAction.gf('data.form').toJS()
switch (str){
case 'code':
this.voucherAction.check([{
path: 'data.form.code', value: form.code
}], this.check);
break;
case 'name':
this.voucherAction.check([{
path: 'data.form.name', value: form.name
}], this.check);
break;
case 'taxNumber':
this.voucherAction.check([{
path: 'data.form.taxNumber', value: form.taxNumber
}], this.check);
break;
case 'linkman':
this.voucherAction.check([{
path: 'data.form.linkman', value: form.linkman
}], this.check);
break;
case 'contactNumber':
this.voucherAction.check([{
path: 'data.form.contactNumber', value: form.contactNumber
}], this.check);
break;
case 'openingBank':
this.voucherAction.check([{
path: 'data.form.openingBank', value: form.openingBank
}], this.check);
break;
case 'bankAccout':
this.voucherAction.check([{
path: 'data.form.bankAccout', value: form.bankAccout
}], this.check);
break;
case 'addressAndTel':
this.voucherAction.check([{
path: 'data.form.addressAndTel', value: form.addressAndTel
}], this.check);
break;
case 'remark':
this.voucherAction.check([{
path: 'data.form.remark', value: form.remark
}], this.check);
break;
case 'receivableAccountId':
this.voucherAction.check([{
path: 'data.form.receivableAccountId', value: form.receivableAccountId
}], this.check);
break;
}
}
check = async (option) => {
if (!option || !option.path)
return
if (option.path == 'data.form.code') {
return {errorPath: 'data.other.error.code', message: option.value && option.value.trim() ? (option.value.trim().length > 50 ? '编码最大长度为50个字符' : "") : '请录入编码'}
} else if (option.path == 'data.form.name') {
return {errorPath: 'data.other.error.name', message: option.value && option.value.trim() ? (option.value.trim().length > 200 ? '名称最大长度为200个字符' : "") : '请录入名称'}
} else if (option.path == 'data.form.taxNumber') {
return {errorPath: 'data.other.error.taxNumber', message: option.value && (option.value.length == 20 || option.value.length == 15 || option.value.length == 18) ? '' : '税号应为15,18或20位'}
}
// else if (option.path == 'data.form.receivableAccountId') {
// return {errorPath: 'data.other.error.receivableAccountId', message: option.value ? '' : '请选择应收科目'}
// }
else if (option.path == 'data.form.linkman') {
return {errorPath: 'data.other.error.linkman', message: option.value && option.value.trim().length > 50 ? '联系人最大长度为50个字符' : ""}
} else if (option.path == 'data.form.contactNumber') {
return {errorPath: 'data.other.error.contactNumber', message: option.value && option.value.trim().length > 50 ? '联系电话最大长度为50个字符' : ""}
} else if (option.path == 'data.form.openingBank') {
return {errorPath: 'data.other.error.openingBank', message: option.value && option.value.trim().length > 50 ? '开户银行最大长度为50个字符' : ""}
} else if (option.path == 'data.form.bankAccout') {
return {errorPath: 'data.other.error.bankAccout', message: option.value && option.value.trim().length > 50 ? '账户最大长度为50个字符' : ""}
} else if (option.path == 'data.form.addressAndTel') {
return {errorPath: 'data.other.error.addressAndTel', message: option.value && option.value.trim().length > 200 ? '地址及电话最大长度为200个字符' : ""}
} else if (option.path == 'data.form.remark') {
return {errorPath: 'data.other.error.remark', message: option.value && option.value.trim().length > 200 ? '备注最大长度为200个字符' : ""}
}
}
fieldChange = (path, value) => {
this.voucherAction.fieldChange(path, value, this.check)
}
//新增科目
addSubject = async (str) => {
const ret = await this.metaAction.modal('show', {
title: '新增科目',
width: 400,
okText: '保存',
bodyStyle: { padding: 24, fontSize: 12 },
children: this.metaAction.loadApp('app-proof-of-charge-subjects-add', {
store: this.component.props.store,
columnCode: "subjects",
active: 'certificate'
})
})
if (ret) {
let account = await this.webapi.customer.account(),arg = {}
arg.glAccounts = account.glAccounts
arg.addItem = ret
arg.str = str
this.injections.reduce('glAccounts', arg)
}
}
nameChange = (name) => {
this.metaAction.sf('data.form.name',name);
this.changeCheck('name');
this.payableAccountChange(name)
}
//应收科目随名称变更
payableAccountChange = (name) => {
let data = this.metaAction.gf('data').toJS(),changeItem = { noChild:false, id:'' },saveItem
if(data.other.glAccounts){
data.other.glAccounts.forEach((data) => {
if(data.code == 1122){
changeItem.noChild = true
changeItem.id = data.id
}
if(data.code == 112201){
saveItem = data
}
})
data.other.glAccounts.forEach((data) => {
if (changeItem.noChild == false) {
if (data.name == name) {
changeItem.id = data.id
}
}
})
// if(changeItem.id == ''){
// changeItem.id = saveItem.id
// }
}
this.injections.reduce('payableAccountChange', changeItem.id)
// this.changeCheck('receivableAccountId')
}
}
export default function creator(option) {
const metaAction = new MetaAction(option),
voucherAction = FormDecorator.actionCreator({...option, metaAction}),
o = new action({...option, metaAction, voucherAction}),
ret = {...metaAction, ...voucherAction, ...o}
metaAction.config({metaHandlers: ret})
return ret
}