File size: 11,224 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
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 (<Form onSubmit={this.openModal}>
<Row>
<Col md={8} lg={9}>
<Card className="p-4 p-xl-5 my-3 my-xl-4">
<div className="d-flex flex-row align-items-start justify-content-between mb-3">
<div class="d-flex flex-column">
<div className="d-flex flex-column">
<div class="mb-2">
<span className="fw-bold">Current Date: </span>
<span className="current-date">{new Date().toLocaleDateString()}</span>
</div>
</div>
<div className="d-flex flex-row align-items-center">
<span className="fw-bold d-block me-2">Due Date:</span>
<Form.Control type="date" value={this.state.dateOfIssue} name={"dateOfIssue"} onChange={(event) => this.editField(event)} style={{
maxWidth: '150px'
}} required="required"/>
</div>
</div>
<div className="d-flex flex-row align-items-center">
<span className="fw-bold me-2">Invoice Number: </span>
<Form.Control type="number" value={this.state.invoiceNumber} name={"invoiceNumber"} onChange={(event) => this.editField(event)} min="1" style={{
maxWidth: '70px'
}} required="required"/>
</div>
</div>
<hr className="my-4"/>
<Row className="mb-5">
<Col>
<Form.Label className="fw-bold">Bill to:</Form.Label>
<Form.Control placeholder={"Who is this invoice to?"} rows={3} value={this.state.billTo} type="text" name="billTo" className="my-2" onChange={(event) => this.editField(event)} autoComplete="name" required="required"/>
<Form.Control placeholder={"Email address"} value={this.state.billToEmail} type="email" name="billToEmail" className="my-2" onChange={(event) => this.editField(event)} autoComplete="email" required="required"/>
<Form.Control placeholder={"Billing address"} value={this.state.billToAddress} type="text" name="billToAddress" className="my-2" autoComplete="address" onChange={(event) => this.editField(event)} required="required"/>
</Col>
<Col>
<Form.Label className="fw-bold">Bill from:</Form.Label>
<Form.Control placeholder={"Who is this invoice from?"} rows={3} value={this.state.billFrom} type="text" name="billFrom" className="my-2" onChange={(event) => this.editField(event)} autoComplete="name" required="required"/>
<Form.Control placeholder={"Email address"} value={this.state.billFromEmail} type="email" name="billFromEmail" className="my-2" onChange={(event) => this.editField(event)} autoComplete="email" required="required"/>
<Form.Control placeholder={"Billing address"} value={this.state.billFromAddress} type="text" name="billFromAddress" className="my-2" autoComplete="address" onChange={(event) => this.editField(event)} required="required"/>
</Col>
</Row>
<InvoiceItem onItemizedItemEdit={this.onItemizedItemEdit.bind(this)} onRowAdd={this.handleAddEvent.bind(this)} onRowDel={this.handleRowDel.bind(this)} currency={this.state.currency} items={this.state.items}/>
<Row className="mt-4 justify-content-end">
<Col lg={6}>
<div className="d-flex flex-row align-items-start justify-content-between">
<span className="fw-bold">Subtotal:
</span>
<span>{this.state.currency}
{this.state.subTotal}</span>
</div>
<div className="d-flex flex-row align-items-start justify-content-between mt-2">
<span className="fw-bold">Discount:</span>
<span>
<span className="small ">({this.state.discountRate || 0}%)</span>
{this.state.currency}
{this.state.discountAmmount || 0}</span>
</div>
<div className="d-flex flex-row align-items-start justify-content-between mt-2">
<span className="fw-bold">Tax:
</span>
<span>
<span className="small ">({this.state.taxRate || 0}%)</span>
{this.state.currency}
{this.state.taxAmmount || 0}</span>
</div>
<hr/>
<div className="d-flex flex-row align-items-start justify-content-between" style={{
fontSize: '1.125rem'
}}>
<span className="fw-bold">Total:
</span>
<span className="fw-bold">{this.state.currency}
{this.state.total || 0}</span>
</div>
</Col>
</Row>
<hr className="my-4"/>
<Form.Label className="fw-bold">Notes:</Form.Label>
<Form.Control placeholder="Thanks for your business!" name="notes" value={this.state.notes} onChange={(event) => this.editField(event)} as="textarea" className="my-2" rows={1}/>
</Card>
</Col>
<Col md={4} lg={3}>
<div className="sticky-top pt-md-3 pt-xl-4">
<Button variant="primary" type="submit" className="d-block w-100">Review Invoice</Button>
<InvoiceModal showModal={this.state.isOpen} closeModal={this.closeModal} info={this.state} items={this.state.items} currency={this.state.currency} subTotal={this.state.subTotal} taxAmmount={this.state.taxAmmount} discountAmmount={this.state.discountAmmount} total={this.state.total}/>
<Form.Group className="mb-3">
<Form.Label className="fw-bold">Currency:</Form.Label>
<Form.Select onChange={event => this.onCurrencyChange({currency: event.target.value})} className="btn btn-light my-1" aria-label="Change Currency">
<option value="$">USD (United States Dollar)</option>
<option value="£">GBP (British Pound Sterling)</option>
<option value="¥">JPY (Japanese Yen)</option>
<option value="$">CAD (Canadian Dollar)</option>
<option value="$">AUD (Australian Dollar)</option>
<option value="$">SGD (Signapore Dollar)</option>
<option value="¥">CNY (Chinese Renminbi)</option>
<option value="₿">BTC (Bitcoin)</option>
</Form.Select>
</Form.Group>
<Form.Group className="my-3">
<Form.Label className="fw-bold">Tax rate:</Form.Label>
<InputGroup className="my-1 flex-nowrap">
<Form.Control name="taxRate" type="number" value={this.state.taxRate} onChange={(event) => this.editField(event)} className="bg-white border" placeholder="0.0" min="0.00" step="0.01" max="100.00"/>
<InputGroup.Text className="bg-light fw-bold text-secondary small">
%
</InputGroup.Text>
</InputGroup>
</Form.Group>
<Form.Group className="my-3">
<Form.Label className="fw-bold">Discount rate:</Form.Label>
<InputGroup className="my-1 flex-nowrap">
<Form.Control name="discountRate" type="number" value={this.state.discountRate} onChange={(event) => this.editField(event)} className="bg-white border" placeholder="0.0" min="0.00" step="0.01" max="100.00"/>
<InputGroup.Text className="bg-light fw-bold text-secondary small">
%
</InputGroup.Text>
</InputGroup>
</Form.Group>
</div>
</Col>
</Row>
</Form>)
}
}
export default InvoiceForm;
|