import React from 'react'; import 'bootstrap/dist/css/bootstrap.min.css'; import Row from 'react-bootstrap/Row'; import Col from 'react-bootstrap/Col'; import Button from 'react-bootstrap/Button'; import Form from 'react-bootstrap/Form'; import Card from 'react-bootstrap/Card'; import InvoiceItem from './InvoiceItem'; import InvoiceModal from './InvoiceModal'; import InputGroup from 'react-bootstrap/InputGroup'; class InvoiceForm extends React.Component { constructor(props) { super(props); this.state = { isOpen: false, currency: '$', currentDate: '', invoiceNumber: 1, dateOfIssue: '', billTo: '', billToEmail: '', billToAddress: '', billFrom: '', billFromEmail: '', billFromAddress: '', notes: '', total: '0.00', subTotal: '0.00', taxRate: '', taxAmmount: '0.00', discountRate: '', discountAmmount: '0.00' }; this.state.items = [ { id: 0, name: '', description: '', price: '1.00', quantity: 1 } ]; this.editField = this.editField.bind(this); } componentDidMount(prevProps) { this.handleCalculateTotal() } handleRowDel(items) { var index = this.state.items.indexOf(items); this.state.items.splice(index, 1); this.setState(this.state.items); }; handleAddEvent(evt) { var id = (+ new Date() + Math.floor(Math.random() * 999999)).toString(36); var items = { id: id, name: '', price: '1.00', description: '', quantity: 1 } this.state.items.push(items); this.setState(this.state.items); } handleCalculateTotal() { var items = this.state.items; var subTotal = 0; items.map(function(items) { subTotal = parseFloat(subTotal + (parseFloat(items.price).toFixed(2) * parseInt(items.quantity))).toFixed(2) }); this.setState({ subTotal: parseFloat(subTotal).toFixed(2) }, () => { this.setState({ taxAmmount: parseFloat(parseFloat(subTotal) * (this.state.taxRate / 100)).toFixed(2) }, () => { this.setState({ discountAmmount: parseFloat(parseFloat(subTotal) * (this.state.discountRate / 100)).toFixed(2) }, () => { this.setState({ total: ((subTotal - this.state.discountAmmount) + parseFloat(this.state.taxAmmount)) }); }); }); }); }; onItemizedItemEdit(evt) { var item = { id: evt.target.id, name: evt.target.name, value: evt.target.value }; var items = this.state.items.slice(); var newItems = items.map(function(items) { for (var key in items) { if (key == item.name && items.id == item.id) { items[key] = item.value; } } return items; }); this.setState({items: newItems}); this.handleCalculateTotal(); }; editField = (event) => { this.setState({ [event.target.name]: event.target.value }); this.handleCalculateTotal(); }; onCurrencyChange = (selectedOption) => { this.setState(selectedOption); }; openModal = (event) => { event.preventDefault() this.handleCalculateTotal() this.setState({isOpen: true}) }; closeModal = (event) => this.setState({isOpen: false}); render() { return (
Current Date:  {new Date().toLocaleDateString()}
Due Date: this.editField(event)} style={{ maxWidth: '150px' }} required="required"/>
Invoice Number:  this.editField(event)} min="1" style={{ maxWidth: '70px' }} required="required"/>

Bill to: this.editField(event)} autoComplete="name" required="required"/> this.editField(event)} autoComplete="email" required="required"/> this.editField(event)} required="required"/> Bill from: this.editField(event)} autoComplete="name" required="required"/> this.editField(event)} autoComplete="email" required="required"/> this.editField(event)} required="required"/>
Subtotal: {this.state.currency} {this.state.subTotal}
Discount: ({this.state.discountRate || 0}%) {this.state.currency} {this.state.discountAmmount || 0}
Tax: ({this.state.taxRate || 0}%) {this.state.currency} {this.state.taxAmmount || 0}

Total: {this.state.currency} {this.state.total || 0}

Notes: this.editField(event)} as="textarea" className="my-2" rows={1}/>
Currency: this.onCurrencyChange({currency: event.target.value})} className="btn btn-light my-1" aria-label="Change Currency"> Tax rate: this.editField(event)} className="bg-white border" placeholder="0.0" min="0.00" step="0.01" max="100.00"/> % Discount rate: this.editField(event)} className="bg-white border" placeholder="0.0" min="0.00" step="0.01" max="100.00"/> %
) } } export default InvoiceForm;