/* OpenBills-web - Web based libre billing software * Copyright (C) 2022 Vidhu Kant Sharma * 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 . */ import { Client, InvoiceClient, getAllClients, Address } from '../../classes/client'; import './scss/client-picker.scss'; import { useState, useEffect } from 'react'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faPhone, faEnvelope, faGlobe } from '@fortawesome/free-solid-svg-icons' const ClientPicker = ({ client, setClient, shippingAddressId, setShippingAddressId }) => { const [clients, setClients] = useState([new Client()]); useEffect(() => refreshClients, []); const refreshClients = () => getAllClients(setClients, () => {}); const handleClientSelect = (e) => { const c = clients.filter(i => i.Id === e.target.value )[0]; setClient(c ? c : new InvoiceClient()); } const shouldShowAddressPicker = () => { if (client.Id !== null) { if (client.ShippingAddresses.length > 0) { // if the only address is same as billing address, dont show if (client.ShippingAddresses.length === 1) { return JSON.stringify(client.ShippingAddresses[0]) !== JSON.stringify(client.BillingAddress); } return true; } } return false; } const formatAddress = (addr) => `${addr.Text.length > 30 ? addr.Text.substring(0, 30) + "..." : addr.Text} | ${addr.City}`; const formatClientName = (client) => `${client.Name.length > 30 ? client.Name.substring(0, 30) + "..." : client.Name} | ${client.BillingAddress.City}`; return (

Invoice Recipient

{clients && clients.length > 0 && <> {shouldShowAddressPicker() && }

GSTIN: {client.GSTIN === "" ? "URP" : client.GSTIN}

}

Name: {client.Contact.Name}
{client.Contact.Phones.length > 0 && <> {client.Contact.Phones.map((i, id) => {` ${i}${id + 1 === client.Contact.Phones.length ? '' : ','}`})}
} {client.Contact.Emails.length > 0 && <> {client.Contact.Emails.map((i, id) => {` ${i}${id + 1 === client.Contact.Emails.length ? '' : ','}`})}
} {client.Contact.Website.length > 0 && <> {client.Contact.Website} }

{client.Id !== null && // if client is selected <>

Billing Address:
{client.BillingAddress.Text}
{client.BillingAddress.City}, {client.BillingAddress.State} - {client.BillingAddress.PostalCode} ({client.BillingAddress.Country})

{shouldShowAddressPicker() && shippingAddressId >= 0 &&

Shipping Address:
{client.ShippingAddresses[shippingAddressId].Text}
{client.ShippingAddresses[shippingAddressId].City}, {client.ShippingAddresses[shippingAddressId].State} - {client.ShippingAddresses[shippingAddressId].PostalCode} ({client.ShippingAddresses[shippingAddressId].Country})

} }

); } export default ClientPicker;