Following is code.
import React from 'react'
import ReactDOM from 'react-dom'
import { Row, Col, Card,Spinner } from 'react-bootstrap'
import { connect } from 'react-redux'
import { Link } from 'react-router-dom'
import { TextField, Popper, Typography, FormControlLabel, Checkbox } from '@material-ui/core'
import { Save, Cancel, Print, PostAdd } from '@material-ui/icons'
import { enableRipple } from '@syncfusion/ej2-base';
import { Autocomplete } from '@material-ui/lab';
import { GridComponent, ColumnsDirective, ColumnDirective, Page, Filter, Inject, Edit, Sort, ForeignKey, Toolbar } from '@syncfusion/ej2-react-grids';
import Swal from 'sweetalert2'
import { getValue } from '@syncfusion/ej2-base';
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { SelectItemsListCombo, SelectItem, SelectItemMeasuring } from "../../Services/ItemsAPI"
import { SelectGodownsList } from '../../Services/GodownsAPI'
import { DepartmentList } from '../../Services/DepartmentsAPI'
import { MachinesList } from '../../Services/MachinesAPI'
import { GetMaxInventoryNo, SaveInvoice, SelectInvoiceMain, SelectInvoiceDetail, InvoicePost } from './../../Services/InvoicesAPI'
import { GetCurrentDate } from "../../Services/Domain";
import { ItemStock } from './../../Services/InventoryAPI'
import StockDetail from './../SaleInvoices/StockDetail'
import Tick from './../../Images/Tick.jpg'
import Cross from './../../Images/Cross.jpg';
import ProductionIssuanceModal from "../ProductionIssuance/ProductionIssuanceModal"
export class StockEntry extends React.Component {
constructor(props) {
super(props)
this.state = {
MainData: [],
StockData: [],
DeletedStockData: [],
ItemList: [],
itemData: [],
SelectedItem: null,
Selected_Godown_Err: null,
Selected_Deptt_Err: null,
Selected_Machine_Err: null,
SelectedTrType: null,
TransactionNo: '',
Qty: 0,
IsCancel: false,
GodownsListData: [],
GridGodownsListData: [],
DepttListData: [],
MachineListData: [],
SelectedGodownsList: null,
ItemMeasuringData: [],
ItemStockDetail: [],
SelectedItemMeasuringData: null,
isvisible: false,
InvoiceDate: GetCurrentDate(),
IsReLocation: false,
IsAdjustment: false,
StockDetailModal: false,
IsIssuance: this.props.StockTitle == "Stock Issuance" ? true : false,
UserName: '',
isUpdate: true,
GodownErr: '',
TrTypeErr: '',
post: '',
btnSave: true,
btnPost: true,
ProductionIssuance: false,
ProductionIssuanceTitle: "",
IsHidden_ProductionIssuance: true,
btn_Disabled: false
}
this.toolbarOptions = ['Add', 'Edit', 'Delete', 'Update', 'Cancel'];
this.validationRules = { required: true };
this.ItemDescription_Ref = React.createRef()
this.customItemValidation = this.customItemValidation.bind(this)
this.customGodownValidation = this.customGodownValidation.bind(this)
this.customDepttValidation = this.customDepttValidation.bind(this)
this.customMachineValidation = this.customMachineValidation.bind(this)
this.grid = this.grid
this.Comp_Id = this.props.CompID.Branch_Id
this.Project_ID = this.props.Project && this.props.Project.Project_ID
this.ProjectName = this.props.ProjectName && this.props.ProjectName
this.ItemValidation = {
required: [this.customItemValidation, 'Item must be selected']
}
this.GodownValidation = {
required: [this.customGodownValidation, 'Godown must be selected']
}
this.DepttValidation = {
required: [this.customDepttValidation, 'Deptt must be selected']
}
this.MachineValidation = {
required: [this.customMachineValidation, 'Machine must be selected']
}
}
show_Production_Issuance_Modal = () => {
this.setState({ ProductionIssuance: true })
}
hide_Production_Issuance_Modal = () => {
this.setState({ ProductionIssuance: false })
}
customItemValidation(args) {
args.value = this.state.SelectedItem ? this.state.SelectedItem.ItemID : ''
return getValue('value', args) != ''
}
customGodownValidation(args) {
args.value = this.state.Selected_Godown_Err ? this.state.Selected_Godown_Err.God_ID : ''
return getValue('value', args) != ''
}
customDepttValidation(args) {
args.value = this.state.Selected_Deptt_Err ? this.state.Selected_Deptt_Err.DeptID : ''
return getValue('value', args) != ''
}
customMachineValidation(args) {
args.value = this.state.Selected_Machine_Err ? this.state.Selected_Machine_Err.MachineID : ''
return getValue('value', args) != ''
}
sortingOptions = {
columns: [{ field: 'sNo', direction: 'Ascending' }]
};
componentDidUpdate() {
if (this.state.isUpdate) {
this.setState({ isUpdate: false })
}
}
componentDidMount() {
SelectItemsListCombo(this.Comp_Id, (data) => {
this.setState({ ItemList: data })
})
SelectGodownsList(this.Comp_Id, (data) => {
this.setState({ GodownsListData: data, GridGodownsListData: data })
})
DepartmentList(this.Comp_Id, (data) => {
this.setState({ DepttListData: data })
})
MachinesList(this.Comp_Id, (data) => {
this.setState({ MachineListData: data })
})
})
})
}
if (this.props.StockTitle == "Stock Issuance") {
this.setState({ IsReLocation: true, IsAdjustment: true })
}
else if (this.props.StockTitle == "Stock Adjustment") {
this.setState({ IsReLocation: true, IsAdjustment: false })
}
else {
this.setState({ IsReLocation: false, IsAdjustment: true })
}
this.setState({btn_Disabled: false})
}
Select_Item = (ItemID, callback) => {
SelectItem(this.Comp_Id, ItemID, (data) => {
callback(data)
})
}
Select_Item_Measuring = (ItemID, callback) => {
SelectItemMeasuring(this.Comp_Id, ItemID, (data) => {
callback(data)
})
}
ItemDescription_Field = (rowValue) => {
return (
<Autocomplete
name="ItemDescription"
id="itemDescription"
defaultValue={this.state.ItemList.find(v => v.ItemDesc === rowValue.ItemDescription)}
ref={this.ItemDescription_Ref}
options={this.state.ItemList}
getOptionLabel={(option) => option.ItemDesc ? option.ItemDesc : ""}
onChange={(e, value) => {
if (value) {
this.Item_And_ItemMeasuring(value.ItemID)
this.setState({ SelectedItem: value })
}
else {
var munit_select = document.getElementById('Munit')
var length = munit_select.options.length;
for (let i = length - 1; i >= 0; i--) {
munit_select.options[i] = null;
}
this.setState({ SelectedItem: null })
}
}}
renderInput={(params) =>
<TextField {...params} name="ItemDescription" />}
/>
)
}
Item_And_ItemMeasuring = (ItemID) => {
this.Select_Item(ItemID, (data) => {
this.Select_Item_Measuring(ItemID, (itemMeasuringData) => {
itemMeasuringData[0].BasicUnit_Qty = data[0].Qty_WH
this.setState({ itemData: data, ItemMeasuringData: itemMeasuringData, SelectedItemMeasuringData: itemMeasuringData[0] })
var munit_select = document.getElementById('Munit')
var length = munit_select.options.length;
for (let i = length - 1; i >= 0; i--) {
munit_select.options[i] = null;
}
this.setState({ ItemMeasuringData: itemMeasuringData })
for (let i = 0; i < itemMeasuringData.length; i++) {
munit_select.options[munit_select.options.length] = new Option(itemMeasuringData[i].MUnitDesc, itemMeasuringData[i].MUnitID)
}
munit_select.selectedIndex = 0
})
})
}
Qty = (rowValue) => {
return (
<NumericTextBoxComponent
id="Qty"
name="Qty"
decimals={2}
format={"n2"}
min={0}
showSpinButton={false}
ref={(numeric) => { this.numericInstance = numeric }}
value={typeof rowValue.Qty === "string" ? (rowValue.Qty).replace(/,/g, '') : rowValue.Qty}
required={true}
/>
)
}
Godowns = (rowValue) => {
this.godown_object = <Autocomplete
name="Godown"
id="Godowns"
defaultValue={this.state.GridGodownsListData.find(x => x.GodDesc === rowValue.Godown)}
options={this.state.GridGodownsListData}
getOptionLabel={(option) => option.GodDesc ? option.GodDesc : ""}
onChange={(e, value) => {
if (value) {
this.setState({ Selected_Godown_Err: value })
}
else {
this.setState({ Selected_Godown_Err: null })
}
}}
renderInput={(params) =>
<TextField {...params} name="Godown"
/>}
/>
return (this.godown_object)
}
Deptt = (rowValue) => {
this.Deptt_object = <Autocomplete
name="DeptName"
id="DeptName"
defaultValue={this.state.DepttListData.find(x => x.DeptName === rowValue.DeptName)}
options={this.state.DepttListData}
getOptionLabel={(option) => option.DeptName ? option.DeptName : ""}
getOptionSelected={(option, value) => option.DeptID === value.DeptID}
onChange={(e, value) => {
if (value) {
this.setState({ Selected_Deptt_Err: value })
}
else {
this.setState({ Selected_Deptt_Err: null })
}
}}
renderInput={(params) =>
<TextField {...params} name="DeptName"
/>
}
/>
return (this.Deptt_object)
}
Machines = (rowValue) => {
this.Machine_object = <Autocomplete
name="MachineName"
id="MachineName"
defaultValue={this.state.MachineListData.find(x => x.MachineName === rowValue.MachineName)}
options={this.state.MachineListData}
getOptionLabel={(option) => option.MachineName ? option.MachineName : ""}
getOptionSelected={(option, value) => option.MachineID === value.MachineID}
onChange={(e, value) => {
if (value) {
this.setState({ Selected_Machine_Err: value })
}
else {
this.setState({ Selected_Machine_Err: null })
}
}}
renderInput={(params) =>
<TextField {...params} name="MachineName"
/>}
/>
return (this.Machine_object)
}
M_Unit = (rowValue) => {
return (<select id="Munit" name="MUnit"
style={{ fontSize: 17, textAlign: "right", width: "100%" }}
onChange={e => {
var Qty = 0;
this.state.ItemMeasuringData.find(value => {
if (e.target.value == value.MUnitID) {
this.setState({ SelectedItemMeasuringData: value })
}
})
}}
className="textbox" >select>)
}
beforeRowInsert = (rowValue) => {
if (rowValue.rowData) {
if (rowValue.data) {
if (rowValue.data.ItemDescription) {
if (rowValue.requestType != "cancel") {
var select = document.getElementById('Munit')
var name = select.options[select.selectedIndex].text
var value = select.options[select.selectedIndex].value
rowValue.data.MUnit = name
rowValue.data.MUnitID = value
this.state.GridGodownsListData.find(value => {
if (value.GodDesc === rowValue.data.Godown) {
rowValue.data.God_ID = value.God_ID
}
})
this.state.ItemList.find(value => {
if (value.ItemDesc === rowValue.data.ItemDescription) {
rowValue.data.ItemID = value.ItemID
}
})
this.state.DepttListData.find(value => {
if (value.DeptName === rowValue.data.DeptName) {
rowValue.data.DeptID = value.DeptID
}
})
this.state.MachineListData.find(value => {
if (value.MachineName === rowValue.data.MachineName) {
rowValue.data.MachineID = value.MachineID
}
})
rowValue.data.Amount = 0
rowValue.data.BasicUnit_Qty = 0
rowValue.data.Qty_Out = 0
rowValue.data.Rate = 0
rowValue.data.Tr_Type = 'AJV'
rowValue.data.action = rowValue.action
if (rowValue.data.InvDID === undefined) {
rowValue.data.action = 'add'
}
}
}
else {
rowValue.data.sNo = this.state.StockData.length + 1
rowValue.data.Qty = 0.00
this.setState({ SelectedItem: null, Selected_Godown_Err: null, Selected_Deptt_Err: null, Selected_Machine_Err: null })
}
}
}
}
afterRowInsert = (rowValue) => {
if (rowValue.requestType == "beginEdit") {
this.Select_Item(rowValue.rowData.ItemID, (data) => {
this.Select_Item_Measuring(rowValue.rowData.ItemID, (itemMeasuringData) => {
var munit_select = document.getElementById('Munit')
var length = munit_select.options.length;
for (let i = length - 1; i >= 0; i--) {
munit_select.options[i] = null;
}
for (let i = 0; i < itemMeasuringData.length; i++) {
if (rowValue.rowData.MUnit == itemMeasuringData[i].MUnitDesc) {
itemMeasuringData[i].BasicUnit_Qty = data[0].Qty_WH
munit_select.options[munit_select.options.length] = new Option(itemMeasuringData[i].MUnitDesc, itemMeasuringData[i].MUnitID)
munit_select.selectedIndex = i
this.setState({ ItemMeasuringData: itemMeasuringData, SelectedItemMeasuringData: itemMeasuringData[i], itemData: data })
}
else {
munit_select.options[munit_select.options.length] = new Option(itemMeasuringData[i].MUnitDesc, itemMeasuringData[i].MUnitID)
}
}
})
})
this.state.ItemList.find(value => {
if (value.ItemID === rowValue.rowData.ItemID) {
this.setState({ SelectedItem: value })
}
})
this.state.GridGodownsListData.map(value => {
if (value.God_ID === rowValue.rowData.God_ID) {
this.setState({ Selected_Godown_Err: value })
}
})
this.state.DepttListData.find(value => {
if (value.DeptID === rowValue.rowData.DeptID) {
this.setState({ Selected_Deptt_Err: value })
}
})
this.state.MachineListData.find(value => {
if (value.MachineID === rowValue.rowData.MachineID) {
this.setState({ Selected_Machine_Err: value })
}
})
}
else if (rowValue.requestType == "delete") {
if (rowValue.data[0].InvDID) {
this.setState({ DeletedStockData: this.state.DeletedStockData.concat(rowValue.data) })
}
}
else if (rowValue.requestType == "save") {
}
else if (rowValue.requestType == "add") {
document.getElementById('itemDescription').focus()
}
}
render() {
if (this.grid)
this.grid.autoFitColumns(['sNo', 'ItemDescription', 'Qty', 'Godown', 'Mnuit'])
enableRipple(true);
return ReactDOM.createPortal(
<>
<div className="block-header">
<Row>
<Col lg={7} md={6} sm={12}>
<div className="header_Title" >
{this.props.StockTitle} / {this.ProjectName}
div>
Col>
Row>
<Card>
<Row className=" row-margin-left row-margin-right row-margin-top" >
<Col >
{this.state.btnPost && <button id="btn_Post" type="button" className="btn btn-primary waves-effect height-of-button" onClick={this.Invoice_Post} > <PostAdd /> {this.state.post} </button>}
{this.state.btnSave && <button type="button" className="btn btn-primary waves-effect height-of-button" onClick={this.Save_Stock} disabled={this.state.btn_Disabled}>
{this.state.btn_Disabled ? (<><Spinner
as="span"
animation="grow"
size="sm"
role="status"
aria-hidden="true"
/> Saving... </>) : <> <Save /> Save </>}</button>}
<Link to="/Stock" className="btn btn-primary waves-effect height-of-button" id="btnCancel"> <Cancel /> Cancel</Link>
</Col>
</Row>
<Row className=" row-margin-left row-margin-right row-margin-top" >
<div style={{ display: "flex", width: "100%", height: "100%", overflow: "hidden" }}>
<div style={{ flex: "1", width: "100%", height: "100%" }}>
<GridComponent dataSource={this.state.StockData} actionBegin={this.beforeRowInsert} ref={g => this.grid = g} actionComplete={this.afterRowInsert} allowPaging={true} allowFiltering={true} allowSorting={true} editSettings={{ allowEditing: true, allowDeleting: true, allowAdding: true, newRowPosition: 'Bottom' }} sortSettings={this.sortingOptions} filterSettings={{ type: 'Menu' }} toolbar={this.toolbarOptions}>
<ColumnsDirective>
<ColumnDirective width='70' field='sNo' headerText='SrNo #' Format="{0:n}" allowEditing={false} isIdentity={true} textAlign='Left' isPrimaryKey={true}>ColumnDirective>
<ColumnDirective width='250' field='ItemDescription' headerText='Item Description' editType='dropdownedit' editTemplate={this.ItemDescription_Field} validationRules={this.ItemValidation} textAlign='Left' />
<ColumnDirective field='Stock' headerText='Stock' width='80' editTemplate={rowData => (<Link to="#" name="Stock" id="Stock" onClick={e => {
if (this.state.SelectedItem !== null) {
this.showStockDetailModal(this.state.SelectedItem.ItemID)
}
else {
Swal.fire({
icon: 'error',
text: `Please Select Item`,
})
}
}}
> Stock Link>)} textAlign="center" >ColumnDirective>
<ColumnDirective width='170' field='Qty' headerText='Qty' editTemplate={this.Qty} textAlign='right' />
<ColumnDirective width='130' field='MUnit' headerText='M Unit' editTemplate={this.M_Unit} />
<ColumnDirective width='250' field='Godown' headerText='Godown' editType='dropdownedit' editTemplate={this.Godowns} validationRules={this.GodownValidation} >ColumnDirective>
<ColumnDirective field='DeptName' headerText='Department' editType='dropdownedit' visible={this.state.IsIssuance} editTemplate={this.Deptt} validationRules={this.DepttValidation} >ColumnDirective>
<ColumnDirective field='MachineName' headerText='Machine' editType='dropdownedit' visible={this.state.IsIssuance} editTemplate={this.Machines} validationRules={this.MachineValidation} >ColumnDirective>
<ColumnDirective field='OrderNo' headerText='Order No' allowEditing={false} >ColumnDirective>
<ColumnDirective field='ItemID' headerText='ItemID' visible={false}>ColumnDirective>
<ColumnDirective field='God_ID' headerText='God_ID' width='150' hideAtMedia={true} >ColumnDirective>
<ColumnDirective field='MUnitID' width='150' hideAtMedia={true} >ColumnDirective>
<ColumnDirective field='InvDID' hideAtMedia={true} >ColumnDirective>
<ColumnDirective field='BasicUnit_Qty' hideAtMedia={true} >ColumnDirective>
<ColumnDirective field='Amount' hideAtMedia={true} >ColumnDirective>
<ColumnDirective field='OrderID' hideAtMedia={true} >ColumnDirective>
ColumnsDirective>
<Inject services={[Filter, Page, Edit, Sort, ForeignKey, Toolbar]} />
GridComponent>
div>
div>
<StockDetail
show={this.state.StockDetailModal}
hide={this.hideStockDetailModal}
itemstockdata={this.state.ItemStockDetail}
/>
Row>
<Row className="row-margin-left row-margin-top row-margin-right" >
<Col lg={2} xl={2} md={4} id="IsCancel">
<FormControlLabel
control={
<Checkbox
checked={this.state.IsCancel}
name="checkedB"
color="primary"
onChange={e => this.setState({ IsCancel: e.target.checked })}
/>
}
label="Cancel"
/>
Col>
<Col lg={2} xl={2} md={4} style={{ marginTop: "1%" }} id="Posted">
<p>Posted : <img id="img" height='20' width='20' />p>
Col>
Row>
<ProductionIssuanceModal
show={this.state.ProductionIssuance}
onHide={this.hide_Production_Issuance_Modal}
project_Id={this.props.Project && this.props.Project.Project_ID}
title={this.state.ProductionIssuanceTitle}
save={this.Save_Production}
Dispatch={false}
invType={"S"}
/>
Card>
div>
, document.getElementById('mainContent')
)
}
}
const mapStateToProps = state => ({
})
export default connect(mapStateToProps)(StockEntry)