aboutsummaryrefslogtreecommitdiff
path: root/src/components/pickers/item-picker.js
blob: d756427054ca615dcd3da680845a57da3c45e0c7 (plain)
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
/* OpenBills-web - Web based libre billing software
 * Copyright (C) 2022  Vidhu Kant Sharma <vidhukant@vidhukant.xyz>

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import { Item, InvoiceItem, getAllItems } from '../../classes/item';
import './scss/item-picker.scss';

import { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
//import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
//import { faPhone, faEnvelope, faGlobe } from '@fortawesome/free-solid-svg-icons'

const ItemPicker = ({invoiceItems, addInvoiceItem}) => {
  const [items, setItems] = useState([new Item()]);
  const [item, setItem] = useState(new InvoiceItem());

  useEffect(() => refreshItems, []);

  const refreshItems = () =>
    getAllItems(setItems, () => {});

  const handleItemSelect = e => {
    const i = items.find(i => i.Id === e.target.value);
    setItem(prevItem => i ? ({...prevItem, ...i}) : new InvoiceItem());
  }

  const handleInput = e => {
    const { name, value, type } = e.target;
    setItem(prevItem => ({
      ...prevItem,
      [name]: type === "number" ? parseFloat(value) : value
    }));
  }

  // add item to the invoice items list
  const addItem = (e) => {
    e.preventDefault();
    addInvoiceItem(item);
    setItem(new InvoiceItem());
  }

  // input elements are sorted on the basis of
  // how likely they are going to be edited
  return (
    <div className={"picker-wrapper"}>
      <p className={"heading"}>Add an Item</p>
      <form className={"item-picker"} onSubmit={addItem}>
        {items.length > 0 ?
          <>
            <label>
              Product/Service:
              <select value={item.Id ? item.Id : ""} onChange={handleItemSelect}>
                <option key="placeholder" value={""} disabled>Select an Item</option>
                {items.map(i =>
                  <option key={i.Id} value={i.Id} disabled={invoiceItems.some(j => j.Id === i.Id)}>
                    {i.Name}{i.Brand.Id === undefined ? "" : " - " + i.Brand.Name}
                  </option>
                )}
              </select>
            </label>
            <label className={"narrow"}>
              Quantity:
                <input
                  type="number"
                  value={item.Quantity}
                  name="Quantity"
                  min={item.MinQuantity > 0 ? item.MinQuantity : 1}
                  max={item.MaxQuantity > 0 ? item.MaxQuantity : null}
                  onChange={handleInput} />
            </label>
            <label className={"narrow"}>
              Price:
                <input
                  type="number"
                  value={item.UnitPrice}
                  name="UnitPrice"
                  onChange={handleInput} />
            </label>
            <label className={"narrow"}>
              Discount %:
                <input
                  type="number"
                  value={item.DiscountPercentage}
                  name="DiscountPercentage"
                  onChange={handleInput} />
            </label>
            <label>
              Description:
                <input
                  type="text"
                  value={item.Description}
                  name="Description"
                  onChange={handleInput} />
            </label>
            <label className={"narrow"}>
              HSN:
                <input
                  type="text"
                  value={item.HSN}
                  name="HSN"
                  onChange={handleInput} />
            </label>
            <label className={"narrow"}>
              GST %:
                <input
                  type="number"
                  value={item.GSTPercentage}
                  name="GSTPercentage"
                  onChange={handleInput} />
            </label>
            <input type="submit" value="Add"/>
          </> :
          <Link to="/manage/items">
            <input type="button" value="Add Items"/>
          </Link>
        }
      </form>
      <hr/>
    </div>
  );
}

export default ItemPicker;