ttk-app-core
Version:
enterprise develop framework
439 lines (411 loc) • 17.9 kB
JavaScript
import React from 'react'
import ReactDOM from 'react-dom'
import { action as MetaAction, AppLoader } from 'edf-meta-engine'
import config from './config'
import { Map, fromJS } from 'immutable'
import moment from 'moment'
import utils, { fetch ,history} from 'edf-utils'
import extend from './extend'
import { consts, common } from 'edf-constant'
import {FormDecorator, Select, Checkbox, Form, DatePicker, Button, Input, Icon, ColumnsSetting} from 'edf-component'
const Option = Select.Option
const FormItem = Form.Item
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.voucherAction.onInit({ component, injections })
this.component = component
this.injections = injections
let addEventListener = this.component.props.addEventListener
if (addEventListener) {
addEventListener('onTabFocus', :: this.onTabFocus)
}
injections.reduce('init')
let tabEdit = this.component.props.tabEdit
this.load(tabEdit)
this.renderNum = 1
}
load = async (tabEdit) => {
let query = await this.webapi.warehouseBegin.query()
if(query == '0'){
// 未启用,获取开账时间
const currentOrg = this.metaAction.context.get("currentOrg")
const enabledPeriod = currentOrg.enabledYear + '-' + `${currentOrg.enabledMonth}`.padStart(2, '0') //'2018-01'
const ret = await this.metaAction.modal('show', {
title: '存货核算',
wrapClassName: 'inventory-card',
width: 400,
okText: '确定',
bodyStyle: {padding: '10px 50px'},
children: this.metaAction.loadApp('ttk-scm-app-inventory-card', {
store: this.component.props.store,
enabledPeriod,
tabEdit
}),
})
if(ret) this.initLoad(null,query)
}else{
this.initLoad(null,query) // 启用存货核算
}
}
initLoad = async(value,query) => {
this.metaAction.sf('data.loading', true)
if(!value) {
value = {
id: undefined,
rate: undefined,
conds:''
}
}
let initPeriodBegin
if(value.id || value.conds) {
initPeriodBegin = await this.webapi.warehouseBegin.initPeriodBegin({inventoryPropertyId: value.id, conds:value.conds})
}else{
initPeriodBegin = await this.webapi.warehouseBegin.initPeriodBegin() //初始化
}
this.metaAction.sf('data.loading', false)
initPeriodBegin.propertyId = value.id
initPeriodBegin.rate = value.rate
if(value.details) initPeriodBegin.details = value.details
if(query) initPeriodBegin.paramValue = query.paramValue
this.injections.reduce('load', initPeriodBegin)
}
onTabFocus = async (props) => {
this.refresh()
}
refresh = () => {
let id = this.metaAction.gf('data.form.inventoryProperty'),
rate = this.metaAction.gf('data.form.typeRate'),
conds = this.metaAction.gf('data.other.searchInput')
this.initLoad({id, rate, conds})
}
save = async () => {
if (!this.checkForSave()) return
let details = this.metaAction.gf('data.form.details')
details = details.size ? details.toJS() : details
let inventoryProperty = this.metaAction.gf('data.form.inventoryProperty')
let filterDetail = []
details.map(item=>{
filterDetail.push({
inventoryId: item.id,
quantity: item.quantity ? item.quantity:0,
price: item.price ? item.price : 0,
amount: item.amount ? item.amount : 0
})
})
let filter = {
businessTypeId: inventoryProperty, //--业务类型
details: filterDetail
}
this.metaAction.sf('data.loading', true)
const ret = await this.webapi.warehouseBegin.savePeriodBegin(filter)
this.metaAction.sf('data.loading', false)
this.metaAction.sf('data.other.change', false)
this.editCloseTips(false)
if(ret) this.metaAction.toast('success', '保存成功')
}
checkForSave = () => {
let details = this.metaAction.gf('data.form.details')
details = details.size ? details.toJS() : details
let errorArr=[]
this.metaAction.sf('data.other.isSaved',false) // 记录是否点过保存
details.map((item, index)=> {
if(item.quantity && !item.amount && !item.price){ // 只有数量,单价和金额为0
this.metaAction.sfs({
[`data.form.details.${index}.amount`]: 0,
[`data.form.details.${index}.price`]: 0
})
}
if(!item.quantity && item.amount){ // 没数量,有金额
this.metaAction.sf(`data.form.style.${index}.quantityStyle`, true)
if(!item.price) this.metaAction.sf(`data.form.style.${index}.priceStyle`, true)
errorArr.push(item.code)
}else if((item.price<0 && item.quantity<0 && item.amount>0) || (item.price<0 && item.quantity>0 && item.amount<0)){
this.metaAction.sfs({
[`data.form.style.${index}.priceStyle`]: true,
[`data.form.style.${index}.quantityStyle`]: true,
[`data.form.style.${index}.amountStyle`]: true,
})
errorArr.push(item.code)
}else if(!item.quantity && !item.amount && item.price){
this.metaAction.sfs({
[`data.form.style.${index}.quantityStyle`]: true,
[`data.form.style.${index}.amountStyle`]: true,
})
errorArr.push(item.code)
}
})
if(errorArr.length){
let code
errorArr.map((item,index)=>{
if(code){
if(errorArr[index+1]) code = code +','+ errorArr[index+1]
}else{
if(errorArr.length == 1){
code = item
}else{
code = item +','+ errorArr[index+1]
}
}
})
this.metaAction.toast('error', `存货编码${code}的的存货,数量未录入或单价小于0,请检查调整`)
this.metaAction.sf('data.other.isSaved',true)
return false
}else{
return true
}
}
getBeginList = (value) => {
let details = this.metaAction.gf('data.form.details'), otherDetail, nowList
let propertyId = this.metaAction.gf('data.form.inventoryProperty')
switch(propertyId){
case 1:
otherDetail = 'otherFormB'
break;
case 2:
otherDetail = 'otherFormA'
break;
case 3:
otherDetail = 'otherFormD'
break;
case 4:
otherDetail = 'otherFormC'
break;
default:
otherDetail = 'otherFormAll'
}
let isChangepre = this.metaAction.gf(`data.other.${otherDetail}Change`)
if(isChangepre){
this.metaAction.sf(`data.${otherDetail}.details`, details) // 保存上一次修改
}
if(value && value.id){
switch(value.id){
case 1:
otherDetail = 'otherFormB'
break;
case 2:
otherDetail = 'otherFormA'
break;
case 3:
otherDetail = 'otherFormD'
break;
case 4:
otherDetail = 'otherFormC'
break;
}
let isChanged = this.metaAction.gf(`data.other.${otherDetail}Change`)
if(isChanged){
nowList = this.metaAction.gf(`data.${otherDetail}.details`) // 此时选中的属性,是否修改过未保存,拿到未保存的值
value['details'] = nowList
}
}else{
let isChanged = this.metaAction.gf('data.other.otherFormAllChange')
if(isChanged){
nowList = this.metaAction.gf(`data.otherFormAll.details`) // 全部时
value = { details: nowList }
}
}
if(value && value.id) {
this.metaAction.sf('data.form.inventoryProperty',value.id)
this.metaAction.sf('data.form.typeRate',value.rate)
let conds = this.metaAction.gf('data.other.searchInput')
value.conds = conds
this.initLoad(value)
}else{
let values = {}
this.metaAction.sf('data.form.inventoryProperty', undefined)
this.metaAction.sf('data.form.typeRate', undefined)
let conds = this.metaAction.gf('data.other.searchInput')
values.conds = conds
if(!value){
this.initLoad(values)
}else{
values.details = value.details
this.initLoad(values)
}
}
}
// 模糊查询
searchList = async(value) => {
let inventoryPropertyId = this.metaAction.gf('data.form.inventoryProperty'),
rate = this.metaAction.gf('data.form.typeRate')
this.metaAction.sf('data.other.searchInput', value)
let filter = {
id: inventoryPropertyId ? inventoryPropertyId : undefined,
rate: rate ? rate:'',
conds: value
}
this.initLoad(filter)
}
// 导出
export = async() => {
let id = this.metaAction.gf('data.form.inventoryProperty'),
list = this.metaAction.gf('data.form.details').toJS()
if(!list.length){
this.metaAction.toast('warning', '当前没有可导出数据')
return false
}else{
let res = await this.webapi.warehouseBegin.export({inventoryPropertyId: id})
}
}
//打印
print = async() => {
let id = this.metaAction.gf('data.form.inventoryProperty'),
list = this.metaAction.gf('data.form.details').toJS()
if(!list.length){
this.metaAction.toast('warning', '当前没有可打印数据')
return false
}else{
let res = await this.webapi.warehouseBegin.print({inventoryPropertyId: id})
}
}
// 导入
imports = async () => {
let propertyId = this.metaAction.gf('data.form.inventoryProperty'),
rate = this.metaAction.gf('data.form.typeRate'),
conds = this.metaAction.gf('data.other.searchInput'),
isMonthlyClosed = this.metaAction.gf('data.other.isMonthlyClosed') // 已经月结不允许导入
const ret = await this.metaAction.modal('show', {
title: '存货期初导入',
width: 560,
okText: '导入',
children: this.metaAction.loadApp('ttk-scm-app-warehouse-import', {
store: this.component.props.store,
propertyId,
rate,
isMonthlyClosed
}),
})
if(ret){
this.initLoad({id: propertyId, rate, conds}) // 应该调用属性筛选接口
}
}
// 计算 不反算数量
calc = (fieldName, rowIndex, rowData, params) => (v) => {
this.editCloseTips(true)
let propertyId = this.metaAction.gf('data.form.inventoryProperty'), otherDetail
switch(propertyId){
case 1:
otherDetail = 'otherFormBChange'
break;
case 2:
otherDetail = 'otherFormAChange'
break;
case 3:
otherDetail = 'otherFormDChange'
break;
case 4:
otherDetail = 'otherFormCChange'
break;
default:
otherDetail = 'otherFormAllChange'
}
this.metaAction.sf('data.other.change', true)
this.metaAction.sf(`data.other.${otherDetail}`, true)
if (fieldName === 'price') {
this.priceChange(rowIndex, rowData, v)
}else if (fieldName === 'amount') {
this.amountChange(rowIndex, rowData, v)
}else if (fieldName === 'quantity') {
this.quantityChange(rowIndex, rowData, v)
}
// 保存过有错误才去检查
let isSaved = this.metaAction.gf('data.other.isSaved')
// if(isSaved) this.checkIsValue(rowIndex,rowData,v,fieldName)
if(isSaved){
this.metaAction.sfs({
[`data.form.style.${rowIndex}.amountStyle`]: false,
[`data.form.style.${rowIndex}.priceStyle`]: false,
[`data.form.style.${rowIndex}.quantityStyle`]: false,
})
}
}
// 检验是否有值,去掉红框
checkIsValue = (rowIndex,rowData,v,fieldName) => {
let priceStyle = this.metaAction.gf(`data.form.style.${rowIndex}.priceStyle`),
price = this.metaAction.gf(`data.form.details.${rowIndex}.price`),
amountStyle = this.metaAction.gf(`data.form.style.${rowIndex}.amountStyle`),
amount = this.metaAction.gf(`data.form.details.${rowIndex}.amount`),
quantityStyle = this.metaAction.gf(`data.form.style.${rowIndex}.quantityStyle`),
quantity = this.metaAction.gf(`data.form.details.${rowIndex}.quantity`)
if((amount && amountStyle == true) || fieldName === 'amount'){
this.metaAction.sf(`data.form.style.${rowIndex}.amountStyle`, false)
this.metaAction.sf(`data.form.style.${rowIndex}.priceStyle`, false)
}
if((price && priceStyle == true) || fieldName === 'price'){
this.metaAction.sf(`data.form.style.${rowIndex}.amountStyle`, false)
this.metaAction.sf(`data.form.style.${rowIndex}.priceStyle`, false)
}
if((quantity && quantityStyle == true) || fieldName === 'quantity'){
this.metaAction.sf(`data.form.style.${rowIndex}.quantityStyle`, false)
this.metaAction.sf(`data.form.style.${rowIndex}.priceStyle`, false)
}
}
priceChange = (rowIndex, rowData, v) => {
let price = utils.number.round(v, 6),
quantity = utils.number.round(rowData.quantity, 6),
amount = utils.number.round(rowData.amount, 2)
if(quantity) amount = utils.number.round(quantity*price, 2)
this.metaAction.sfs({
[`data.form.details.${rowIndex}.price`]: price,
[`data.form.details.${rowIndex}.amount`]: amount
})
}
amountChange = (rowIndex, rowData, v) => {
let amount = utils.number.round(v, 2),
quantity = utils.number.round(rowData.quantity, 6),
price = utils.number.round(rowData.price, 6)
if (quantity != 0) {
price = utils.number.round(amount / quantity, 6)
}
this.metaAction.sfs({
[`data.form.details.${rowIndex}.amount`]: amount,
[`data.form.details.${rowIndex}.price`]: price
})
}
quantityChange = (rowIndex, rowData, v) => {
let quantity = utils.number.round(v, 6),
price = utils.number.round(rowData.price, 6),
amount = utils.number.round(rowData.amount, 2)
if(amount) price = utils.number.round(amount / quantity, 6)
if(price) amount = utils.number.round(price * quantity, 2)
this.metaAction.sfs({
[`data.form.details.${rowIndex}.quantity`]: quantity,
[`data.form.details.${rowIndex}.amount`]: amount,
[`data.form.details.${rowIndex}.price`]: price
})
}
quantityFormat = (quantity, decimals, isFocus) => {
if (quantity) {
return this.voucherAction.numberFormat(quantity, decimals, isFocus)
}
}
aaa = () => {
this.component.props.setPortalContent &&
this.component.props.setPortalContent('存货台账', 'ttk-scm-app-inventory')
}
// 关闭页面
editCloseTips = (istip) => {
if (this.component.props.editing) {
//设置当前单据状态,供单据页签关闭用 false:关闭不提醒 true 关闭提醒
this.component.props.editing(this.component.props.appName, istip)
}
}
}
export default function creator(option) {
const metaAction = new MetaAction(option),
extendAction = extend.actionCreator({ ...option, metaAction }),
voucherAction = FormDecorator.actionCreator({ ...option, metaAction }),
// voucherActionG = GridDecorator.actionCreator({ ...option, metaAction }),
o = new action({ ...option, metaAction, extendAction, voucherAction }),
ret = { ...metaAction, ...extendAction.gridAction, ...voucherAction, ...o }
metaAction.config({ metaHandlers: ret })
return ret
}