This commit is contained in:
Andreas Knuth 2026-02-16 18:42:20 -06:00
parent 911b25d96b
commit 25da1a46a8
11 changed files with 4225 additions and 695 deletions

View File

@ -24,6 +24,7 @@ RUN npm install --omit=dev
# Copy application files # Copy application files
COPY server.js ./ COPY server.js ./
COPY qbo_helper.js ./
COPY public ./public COPY public ./public
# Create uploads directory # Create uploads directory

View File

@ -31,6 +31,14 @@ services:
DB_USER: ${DB_USER} DB_USER: ${DB_USER}
DB_PASSWORD: ${DB_PASSWORD} DB_PASSWORD: ${DB_PASSWORD}
DB_NAME: ${DB_NAME} DB_NAME: ${DB_NAME}
# --- NEU: QBO Variablen durchreichen ---
QBO_CLIENT_ID: ${QBO_CLIENT_ID}
QBO_CLIENT_SECRET: ${QBO_CLIENT_SECRET}
QBO_ENVIRONMENT: ${QBO_ENVIRONMENT}
QBO_REDIRECT_URI: ${QBO_REDIRECT_URI}
QBO_REALM_ID: ${QBO_REALM_ID}
QBO_ACCESS_TOKEN: ${QBO_ACCESS_TOKEN}
QBO_REFRESH_TOKEN: ${QBO_REFRESH_TOKEN}
volumes: volumes:
- ./public/uploads:/app/public/uploads - ./public/uploads:/app/public/uploads
- ./templates:/app/templates # NEU! - ./templates:/app/templates # NEU!

157
import_customers_qbo.js Normal file
View File

@ -0,0 +1,157 @@
require('dotenv').config();
const OAuthClient = require('intuit-oauth');
const { Client } = require('pg');
// --- KONFIGURATION ---
const totalLimit = null;
// ---------------------
const config = {
clientId: process.env.QBO_CLIENT_ID,
clientSecret: process.env.QBO_CLIENT_SECRET,
environment: process.env.QBO_ENVIRONMENT || 'sandbox',
redirectUri: process.env.QBO_REDIRECT_URI,
token: {
// Wir brauchen initial nur den Refresh Token, Access holen wir uns neu
access_token: process.env.QBO_ACCESS_TOKEN,
refresh_token: process.env.QBO_REFRESH_TOKEN,
realmId: process.env.QBO_REALM_ID
}
};
// SPEZIAL-CONFIG FÜR LOKALEN ZUGRIFF AUF DOCKER DB
const dbConfig = {
user: process.env.DB_USER,
// WICHTIG: Lokal ist es immer localhost
host: 'localhost',
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
// WICHTIG: Laut deinem docker-compose mapst du 5433 auf 5432!
port: 5433,
};
async function importCustomers() {
const oauthClient = new OAuthClient(config);
const pgClient = new Client(dbConfig);
try {
// console.log("🔄 1. Versuche Token zu erneuern...");
// try {
// // Token Refresh erzwingen bevor wir starten
// const authResponse = await oauthClient.refresh();
// console.log("✅ Token erfolgreich erneuert!");
// // Optional: Das neue Token in der Session speichern, falls nötig
// } catch (tokenErr) {
// console.error("❌ Token Refresh fehlgeschlagen. Prüfe QBO_REFRESH_TOKEN in .env");
// console.error(tokenErr.originalMessage || tokenErr);
// return; // Abbruch
// }
console.log(`🔌 2. Verbinde zur DB (Port ${dbConfig.port})...`);
await pgClient.connect();
console.log(`✅ DB Verbunden.`);
// --- AB HIER DER NORMALE IMPORT ---
let startPosition = 1;
let totalProcessed = 0;
let hasMore = true;
while (hasMore) {
let limitForThisBatch = 100;
if (totalLimit) {
const remaining = totalLimit - totalProcessed;
if (remaining <= 0) break;
limitForThisBatch = Math.min(100, remaining);
}
const query = `SELECT * FROM Customer STARTPOSITION ${startPosition} MAXRESULTS ${limitForThisBatch}`;
console.log(`📡 QBO Request: Hole ${limitForThisBatch} Kunden ab Pos ${startPosition}...`);
const baseUrl = config.environment === 'production'
? 'https://quickbooks.api.intuit.com/'
: 'https://sandbox-quickbooks.api.intuit.com/';
const response = await oauthClient.makeApiCall({
url: `${baseUrl}v3/company/${config.token.realmId}/query?query=${encodeURI(query)}`,
method: 'GET',
});
const data = response.getJson ? response.getJson() : response.json;
const customers = data.QueryResponse?.Customer || [];
console.log(`📥 QBO Response: ${customers.length} Kunden erhalten.`);
if (customers.length === 0) {
hasMore = false;
break;
}
for (const c of customers) {
try {
const rawPhone = c.PrimaryPhone?.FreeFormNumber || "";
const formattedAccountNumber = rawPhone.replace(/\D/g, "");
const sql = `
INSERT INTO customers (
name, line1, line2, line3, line4, city, state, zip_code,
account_number, email, phone, phone2, taxable, qbo_id, qbo_sync_token, updated_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, NOW())
ON CONFLICT (qbo_id) DO UPDATE SET
name = EXCLUDED.name,
line1 = EXCLUDED.line1,
line2 = EXCLUDED.line2,
line3 = EXCLUDED.line3,
line4 = EXCLUDED.line4,
city = EXCLUDED.city,
state = EXCLUDED.state,
zip_code = EXCLUDED.zip_code,
email = EXCLUDED.email,
phone = EXCLUDED.phone,
phone2 = EXCLUDED.phone2,
qbo_sync_token = EXCLUDED.qbo_sync_token,
taxable = EXCLUDED.taxable,
updated_at = NOW();
`;
const values = [
c.CompanyName || c.DisplayName,
c.BillAddr?.Line1 || null,
c.BillAddr?.Line2 || null,
c.BillAddr?.Line3 || null,
c.BillAddr?.Line4 || null,
c.BillAddr?.City || null,
c.BillAddr?.CountrySubDivisionCode || null,
c.BillAddr?.PostalCode || null,
formattedAccountNumber || null,
c.PrimaryEmailAddr?.Address || null,
c.PrimaryPhone?.FreeFormNumber || null,
c.AlternatePhone?.FreeFormNumber || null,
c.Taxable || false,
c.Id,
c.SyncToken
];
await pgClient.query(sql, values);
totalProcessed++;
process.stdout.write(".");
} catch (rowError) {
console.error(`\n❌ DB Fehler bei Kunde ID ${c.Id}:`, rowError.message);
}
}
console.log("");
if (customers.length < limitForThisBatch) hasMore = false;
startPosition += customers.length;
}
console.log(`\n🎉 Fertig! ${totalProcessed} Kunden verarbeitet.`);
} catch (e) {
console.error("\n💀 FATAL ERROR:", e.message);
if(e.authResponse) console.log(JSON.stringify(e.authResponse, null, 2));
} finally {
await pgClient.end();
}
}
importCustomers();

3509
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -8,9 +8,12 @@
"dev": "nodemon server.js" "dev": "nodemon server.js"
}, },
"dependencies": { "dependencies": {
"csv-parser": "^3.2.0",
"dotenv": "^17.3.1",
"express": "^4.21.2", "express": "^4.21.2",
"pg": "^8.13.1", "intuit-oauth": "^4.2.2",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"pg": "^8.13.1",
"puppeteer": "^23.11.1" "puppeteer": "^23.11.1"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,321 +0,0 @@
--
-- PostgreSQL database dump
--
\restrict bxGU7dQ4DrNrHU2OuyEH16NHE6ZA8yFm2MADa6p2XI8qbowdWdtlaDeKSSp2NYx
-- Dumped from database version 15.15
-- Dumped by pg_dump version 15.15
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: customers; Type: TABLE; Schema: public; Owner: quoteuser
--
CREATE TABLE public.customers (
id integer NOT NULL,
name character varying(255) NOT NULL,
street character varying(255) NOT NULL,
city character varying(100) NOT NULL,
state character varying(2) NOT NULL,
zip_code character varying(10) NOT NULL,
account_number character varying(50),
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);
ALTER TABLE public.customers OWNER TO quoteuser;
--
-- Name: customers_id_seq; Type: SEQUENCE; Schema: public; Owner: quoteuser
--
CREATE SEQUENCE public.customers_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.customers_id_seq OWNER TO quoteuser;
--
-- Name: customers_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: quoteuser
--
ALTER SEQUENCE public.customers_id_seq OWNED BY public.customers.id;
--
-- Name: quote_items; Type: TABLE; Schema: public; Owner: quoteuser
--
CREATE TABLE public.quote_items (
id integer NOT NULL,
quote_id integer,
quantity character varying(20) NOT NULL,
description text NOT NULL,
rate character varying(50) NOT NULL,
amount character varying(50) NOT NULL,
is_tbd boolean DEFAULT false,
item_order integer NOT NULL,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);
ALTER TABLE public.quote_items OWNER TO quoteuser;
--
-- Name: quote_items_id_seq; Type: SEQUENCE; Schema: public; Owner: quoteuser
--
CREATE SEQUENCE public.quote_items_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.quote_items_id_seq OWNER TO quoteuser;
--
-- Name: quote_items_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: quoteuser
--
ALTER SEQUENCE public.quote_items_id_seq OWNED BY public.quote_items.id;
--
-- Name: quotes; Type: TABLE; Schema: public; Owner: quoteuser
--
CREATE TABLE public.quotes (
id integer NOT NULL,
quote_number character varying(50) NOT NULL,
customer_id integer,
quote_date date NOT NULL,
tax_exempt boolean DEFAULT false,
tax_rate numeric(5,2) DEFAULT 8.25,
subtotal numeric(10,2) DEFAULT 0,
tax_amount numeric(10,2) DEFAULT 0,
total numeric(10,2) DEFAULT 0,
has_tbd boolean DEFAULT false,
tbd_note text,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);
ALTER TABLE public.quotes OWNER TO quoteuser;
--
-- Name: quotes_id_seq; Type: SEQUENCE; Schema: public; Owner: quoteuser
--
CREATE SEQUENCE public.quotes_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.quotes_id_seq OWNER TO quoteuser;
--
-- Name: quotes_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: quoteuser
--
ALTER SEQUENCE public.quotes_id_seq OWNED BY public.quotes.id;
--
-- Name: customers id; Type: DEFAULT; Schema: public; Owner: quoteuser
--
ALTER TABLE ONLY public.customers ALTER COLUMN id SET DEFAULT nextval('public.customers_id_seq'::regclass);
--
-- Name: quote_items id; Type: DEFAULT; Schema: public; Owner: quoteuser
--
ALTER TABLE ONLY public.quote_items ALTER COLUMN id SET DEFAULT nextval('public.quote_items_id_seq'::regclass);
--
-- Name: quotes id; Type: DEFAULT; Schema: public; Owner: quoteuser
--
ALTER TABLE ONLY public.quotes ALTER COLUMN id SET DEFAULT nextval('public.quotes_id_seq'::regclass);
--
-- Data for Name: customers; Type: TABLE DATA; Schema: public; Owner: quoteuser
--
COPY public.customers (id, name, street, city, state, zip_code, account_number, created_at, updated_at) FROM stdin;
1 Braselton Development 5337 Yorktown Blvd. Suite 10-D Corpus Christi TX 78414 3617790060 2026-01-22 01:09:30.914655 2026-01-22 01:09:30.914655
2 Karen Menn 5134 Graford Place Corpus Christi TX 78413 3619933550 2026-01-22 01:19:49.357044 2026-01-22 01:49:16.051712
3 Hearing Aid Company of Texas 6468 Holly Road Corpus Christi TX 78412 3618143487 2026-01-22 03:33:56.090479 2026-01-22 03:33:56.090479
4 South Shore Christian Church 4710 S. Alameda Corpus Christi TX 78412 3619926391 2026-01-22 03:40:33.012646 2026-01-22 03:40:33.012646
5 JE Construction Services, LLC 7505 Up River Road Corpus Christi TX 78409 3612892901 2026-01-22 03:41:08.716604 2026-01-22 03:41:08.716604
6 John T. Thompson, DDS 4101 US-77 Corpus Christi TX 78410 3612423151 2026-01-30 20:50:22.987565 2026-01-30 21:06:23.354743
\.
--
-- Data for Name: quote_items; Type: TABLE DATA; Schema: public; Owner: quoteuser
--
COPY public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) FROM stdin;
26 5 1 <p>HPE ProLiant MicroServer Gen11 Ultra Micro Tower Server - 1 x Intel Xeon E-2414, 32 GB DDR5 RAM</p> 1900 1900.00 f 0 2026-01-22 18:02:30.878526
27 5 2 <p>Western Digital 24TB WD Red Pro NAS Internal Hard Drive HDD</p> 850 1700.00 f 1 2026-01-22 18:02:30.878526
28 5 1 <ul><li>Off-site installation and base configuration of the TrueNAS system</li><li>Creation of storage pools, datasets, and network shares as specified\n</li><li>Configuration of users, user groups, and access permissions\nSetup of automated snapshots with defined retention and rollback capability\n</li><li>Configuration of cloud backups to iDrive360\nSetup of system monitoring and email notifications for proactive issue detection\n</li><li>Installation and configuration of AOMEI Backup on selected desktops and laptops, storing backups on designated TrueNAS shares</li></ul> 2250 2250.00 f 2 2026-01-22 18:02:30.878526
44 1 1 <ul><li>Dell OptiPlex 7010 SFF Desktop Intel Core i5-13600,14 Cores</li><li>16GB</li><li>Windows 11 Pro</li><li>Crucial - P310 2TB Internal SSD PCIe Gen 4 x4 NVMe M.2</li></ul> 1079 1079.00 f 0 2026-01-22 18:51:00.998206
45 1 1 <p>DisplayPort to HDMI cable 10ft</p> 20 20.00 f 1 2026-01-22 18:51:00.998206
46 1 3 <p>Setup and configure Dell OptiPlex 7010 off-site\nInstall all Lenovo drivers and latest Windows updates. Deliver to site and setup on local network\nTransferred data from old computer including desktop items, documents, downloads, pictures, videos, browser bookmarks and favorites. \nSetup printing and scanning as required. Install customer requested software.Test all hardware for proper Operation</p> 125 375.00 f 2 2026-01-22 18:51:00.998206
47 2 1 <p>Lenovo Yoga 7 82YN 16" i5-1335U 1.3GHz 16GB RAM 512GB SSD</p> 500 500.00 f 0 2026-01-22 18:54:57.288474
48 2 2 <p>Setup and configure Lenovo yoga 7 82YN 16" i5-1335U 1.3GHz 16GB RAM 512GB SSD off-site. Install all Lenovo drivers and latest Windows updates. Deliver to site and setup on local network. Transferred data from old computer including desktop items, documents, downloads, pictures, videos, browser bookmarks and favorites. Setup printing and scanning as required. Install customer requested software. Test all hardware for proper operation.</p> 125 250.00 f 1 2026-01-22 18:54:57.288474
49 4 1 <ul><li>Dell OptiPlex 7020 Plus Tower Desktop PC Core i7-14700 </li><li>32GB DDR5 RAM, </li><li>2 TB SSD M.2 PCIe Gen4 TLC, </li><li>NVIDIA® GeForce RTX 5050</li></ul> 2080 2080.00 f 0 2026-01-22 20:00:28.631846
50 4 2 <p>Setup and configure Lenovo Yoga as needed. Install all Lenovo drivers and latest Windows updates. Deliver to site and setup on local network. Transfer data from old computer including desktop items, documents, downloads, pictures, videos, browser bookmarks and favorites. Setup printing and scanning as required. Install customer requested software. Test all hardware for proper operation.</p> 125 250.00 f 1 2026-01-22 20:00:28.631846
59 3 3 <ul><li><strong>Lenovo </strong>ThinkPad P16s Mobile Workstation</li><li><strong>Processor:</strong> Intel® Core Ultra 7 155H</li><li><strong>Graphics Card:</strong> NVIDIA RTX 500 Ada Generation Laptop GPU, <strong>4 GB GDDR6</strong></li><li><strong>Memory: 64 GB DDR5-5600 MT/s</strong></li><li><strong>Storage: 1 TB SSD</strong> M.2 2280 PCIe Gen4&nbsp;</li><li><strong>Display: </strong>16" &nbsp;WQUXGA (3840 × 2400) OLED</li><li><strong>Operating System:</strong> Windows 11 Pro 64-bit</li><li><strong>1 Year Warranty</strong></li><li><strong>(This device is new, not refurbished)</strong></li></ul> 1949 5847.00 f 0 2026-01-26 18:41:05.501558
60 3 <p>Setup and configure Lenovo Laptops as needed. Install all Lenovo drivers and latest Windows updates. Deliver to site and setup on local network. Setup printing and scanning as required. Install customer requested software. Test all hardware for proper operation.</p> 125 TBD t 1 2026-01-26 18:41:05.501558
74 7 1 <p>Dell Optiplex 7010 (or similar) Tower configured with Intel Core i5-13500 processor, 16GB RAM, 512GB solid state drive and Windows 11 Professional. Refurbished with a one year warranty.\t</p> 725.00 725.00 f 0 2026-01-30 21:04:38.661279
75 7 3 <p>Delivery and installation of Dell Tower PC. Setup on local network and install requested software. Install and configure local and network printers. Transfer requested data from existing PC and verify proper operation of all hardware. </p><p><br></p><p>Microsoft Office (if required) is not included in this quote.</p> 125 375.00 f 1 2026-01-30 21:04:38.661279
76 6 1 <ul><li>Dell Tower Computer configured with Intel Core Ultra 5 235 processor, 16GB RAM, 512GB SSD and Windows 11 Professional. New with One Year Warranty.</li></ul> 1325.00 1325.00 f 0 2026-01-30 21:07:26.820637
77 6 3 <p>Delivery and installation of new Dell Tower PC. Setup on local network and install requested software. Install and configure local and network printers. Transfer requested data from existing PC and verify proper operation of all hardware. </p><p><br></p><p>Microsoft Office (if required) is not included in this quote.</p> 125.00 375.00 f 1 2026-01-30 21:07:26.820637
\.
--
-- Data for Name: quotes; Type: TABLE DATA; Schema: public; Owner: quoteuser
--
COPY public.quotes (id, quote_number, customer_id, quote_date, tax_exempt, tax_rate, subtotal, tax_amount, total, has_tbd, tbd_note, created_at, updated_at) FROM stdin;
5 2026-01-0005 5 2026-01-22 f 8.25 5850.00 482.63 6332.63 f 2026-01-22 16:28:42.374654 2026-01-22 18:02:30.878526
1 2026-01-0001 2 2026-01-22 f 8.25 1474.00 121.61 1595.61 f 2026-01-22 01:34:06.558046 2026-01-22 18:51:00.998206
2 2026-01-0002 3 2026-01-22 f 8.25 750.00 61.88 811.88 f 2026-01-22 03:35:15.021729 2026-01-22 18:54:57.288474
4 2026-01-0004 4 2026-01-22 f 8.25 2330.00 192.23 2522.23 f 2026-01-22 03:45:56.686598 2026-01-22 20:00:28.631846
3 2026-01-0003 1 2026-01-26 f 8.25 5847.00 482.38 6329.38 t Total excludes labor charges which will be determined based on actual time required. 2026-01-22 03:36:47.795674 2026-01-26 18:41:05.501558
7 2026-01-0007 6 2026-01-30 f 8.25 1100.00 90.75 1190.75 f 2026-01-30 21:01:43.538202 2026-01-30 21:04:38.661279
6 2026-01-0006 6 2026-01-30 f 8.25 1700.00 140.25 1840.25 f 2026-01-30 20:58:23.014874 2026-01-30 21:07:26.820637
\.
--
-- Name: customers_id_seq; Type: SEQUENCE SET; Schema: public; Owner: quoteuser
--
SELECT pg_catalog.setval('public.customers_id_seq', 6, true);
--
-- Name: quote_items_id_seq; Type: SEQUENCE SET; Schema: public; Owner: quoteuser
--
SELECT pg_catalog.setval('public.quote_items_id_seq', 77, true);
--
-- Name: quotes_id_seq; Type: SEQUENCE SET; Schema: public; Owner: quoteuser
--
SELECT pg_catalog.setval('public.quotes_id_seq', 7, true);
--
-- Name: customers customers_pkey; Type: CONSTRAINT; Schema: public; Owner: quoteuser
--
ALTER TABLE ONLY public.customers
ADD CONSTRAINT customers_pkey PRIMARY KEY (id);
--
-- Name: quote_items quote_items_pkey; Type: CONSTRAINT; Schema: public; Owner: quoteuser
--
ALTER TABLE ONLY public.quote_items
ADD CONSTRAINT quote_items_pkey PRIMARY KEY (id);
--
-- Name: quotes quotes_pkey; Type: CONSTRAINT; Schema: public; Owner: quoteuser
--
ALTER TABLE ONLY public.quotes
ADD CONSTRAINT quotes_pkey PRIMARY KEY (id);
--
-- Name: quotes quotes_quote_number_key; Type: CONSTRAINT; Schema: public; Owner: quoteuser
--
ALTER TABLE ONLY public.quotes
ADD CONSTRAINT quotes_quote_number_key UNIQUE (quote_number);
--
-- Name: idx_quote_items_quote_id; Type: INDEX; Schema: public; Owner: quoteuser
--
CREATE INDEX idx_quote_items_quote_id ON public.quote_items USING btree (quote_id);
--
-- Name: idx_quotes_customer_id; Type: INDEX; Schema: public; Owner: quoteuser
--
CREATE INDEX idx_quotes_customer_id ON public.quotes USING btree (customer_id);
--
-- Name: idx_quotes_quote_number; Type: INDEX; Schema: public; Owner: quoteuser
--
CREATE INDEX idx_quotes_quote_number ON public.quotes USING btree (quote_number);
--
-- Name: quote_items quote_items_quote_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: quoteuser
--
ALTER TABLE ONLY public.quote_items
ADD CONSTRAINT quote_items_quote_id_fkey FOREIGN KEY (quote_id) REFERENCES public.quotes(id) ON DELETE CASCADE;
--
-- Name: quotes quotes_customer_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: quoteuser
--
ALTER TABLE ONLY public.quotes
ADD CONSTRAINT quotes_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES public.customers(id);
--
-- PostgreSQL database dump complete
--
\unrestrict bxGU7dQ4DrNrHU2OuyEH16NHE6ZA8yFm2MADa6p2XI8qbowdWdtlaDeKSSp2NYx

View File

@ -1,102 +0,0 @@
--
-- PostgreSQL database dump
--
\restrict KCbrUeHdJ7srnFlBFWbQWdZ6A6bdMlTKPXbmEoc5qE3gaNBouFxTyfvdD9oETV4
-- Dumped from database version 15.15
-- Dumped by pg_dump version 15.15
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Data for Name: customers; Type: TABLE DATA; Schema: public; Owner: quoteuser
--
INSERT INTO public.customers (id, name, street, city, state, zip_code, account_number, created_at, updated_at) VALUES (1, 'Braselton Development', '5337 Yorktown Blvd. Suite 10-D', 'Corpus Christi', 'TX', '78414', '3617790060', '2026-01-22 01:09:30.914655', '2026-01-22 01:09:30.914655');
INSERT INTO public.customers (id, name, street, city, state, zip_code, account_number, created_at, updated_at) VALUES (2, 'Karen Menn', '5134 Graford Place', 'Corpus Christi', 'TX', '78413', '3619933550', '2026-01-22 01:19:49.357044', '2026-01-22 01:49:16.051712');
INSERT INTO public.customers (id, name, street, city, state, zip_code, account_number, created_at, updated_at) VALUES (3, 'Hearing Aid Company of Texas', '6468 Holly Road', 'Corpus Christi', 'TX', '78412', '3618143487', '2026-01-22 03:33:56.090479', '2026-01-22 03:33:56.090479');
INSERT INTO public.customers (id, name, street, city, state, zip_code, account_number, created_at, updated_at) VALUES (4, 'South Shore Christian Church', '4710 S. Alameda', 'Corpus Christi', 'TX', '78412', '3619926391', '2026-01-22 03:40:33.012646', '2026-01-22 03:40:33.012646');
INSERT INTO public.customers (id, name, street, city, state, zip_code, account_number, created_at, updated_at) VALUES (5, 'JE Construction Services, LLC', '7505 Up River Road', 'Corpus Christi', 'TX', '78409', '3612892901', '2026-01-22 03:41:08.716604', '2026-01-22 03:41:08.716604');
INSERT INTO public.customers (id, name, street, city, state, zip_code, account_number, created_at, updated_at) VALUES (6, 'John T. Thompson, DDS', '4101 US-77', 'Corpus Christi', 'TX', '78410', '3612423151', '2026-01-30 20:50:22.987565', '2026-01-30 21:06:23.354743');
--
-- Data for Name: quotes; Type: TABLE DATA; Schema: public; Owner: quoteuser
--
INSERT INTO public.quotes (id, quote_number, customer_id, quote_date, tax_exempt, tax_rate, subtotal, tax_amount, total, has_tbd, tbd_note, created_at, updated_at) VALUES (5, '2026-01-0005', 5, '2026-01-22', false, 8.25, 5850.00, 482.63, 6332.63, false, '', '2026-01-22 16:28:42.374654', '2026-01-22 18:02:30.878526');
INSERT INTO public.quotes (id, quote_number, customer_id, quote_date, tax_exempt, tax_rate, subtotal, tax_amount, total, has_tbd, tbd_note, created_at, updated_at) VALUES (1, '2026-01-0001', 2, '2026-01-22', false, 8.25, 1474.00, 121.61, 1595.61, false, '', '2026-01-22 01:34:06.558046', '2026-01-22 18:51:00.998206');
INSERT INTO public.quotes (id, quote_number, customer_id, quote_date, tax_exempt, tax_rate, subtotal, tax_amount, total, has_tbd, tbd_note, created_at, updated_at) VALUES (2, '2026-01-0002', 3, '2026-01-22', false, 8.25, 750.00, 61.88, 811.88, false, '', '2026-01-22 03:35:15.021729', '2026-01-22 18:54:57.288474');
INSERT INTO public.quotes (id, quote_number, customer_id, quote_date, tax_exempt, tax_rate, subtotal, tax_amount, total, has_tbd, tbd_note, created_at, updated_at) VALUES (4, '2026-01-0004', 4, '2026-01-22', false, 8.25, 2330.00, 192.23, 2522.23, false, '', '2026-01-22 03:45:56.686598', '2026-01-22 20:00:28.631846');
INSERT INTO public.quotes (id, quote_number, customer_id, quote_date, tax_exempt, tax_rate, subtotal, tax_amount, total, has_tbd, tbd_note, created_at, updated_at) VALUES (3, '2026-01-0003', 1, '2026-01-26', false, 8.25, 5847.00, 482.38, 6329.38, true, 'Total excludes labor charges which will be determined based on actual time required.', '2026-01-22 03:36:47.795674', '2026-01-26 18:41:05.501558');
INSERT INTO public.quotes (id, quote_number, customer_id, quote_date, tax_exempt, tax_rate, subtotal, tax_amount, total, has_tbd, tbd_note, created_at, updated_at) VALUES (7, '2026-01-0007', 6, '2026-01-30', false, 8.25, 1100.00, 90.75, 1190.75, false, '', '2026-01-30 21:01:43.538202', '2026-01-30 21:04:38.661279');
INSERT INTO public.quotes (id, quote_number, customer_id, quote_date, tax_exempt, tax_rate, subtotal, tax_amount, total, has_tbd, tbd_note, created_at, updated_at) VALUES (6, '2026-01-0006', 6, '2026-01-30', false, 8.25, 1700.00, 140.25, 1840.25, false, '', '2026-01-30 20:58:23.014874', '2026-01-30 21:07:26.820637');
--
-- Data for Name: quote_items; Type: TABLE DATA; Schema: public; Owner: quoteuser
--
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (26, 5, '1', '<p>HPE ProLiant MicroServer Gen11 Ultra Micro Tower Server - 1 x Intel Xeon E-2414, 32 GB DDR5 RAM</p>', '1900', '1900.00', false, 0, '2026-01-22 18:02:30.878526');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (27, 5, '2', '<p>Western Digital 24TB WD Red Pro NAS Internal Hard Drive HDD</p>', '850', '1700.00', false, 1, '2026-01-22 18:02:30.878526');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (28, 5, '1', '<ul><li>Off-site installation and base configuration of the TrueNAS system</li><li>Creation of storage pools, datasets, and network shares as specified
</li><li>Configuration of users, user groups, and access permissions
Setup of automated snapshots with defined retention and rollback capability
</li><li>Configuration of cloud backups to iDrive360
Setup of system monitoring and email notifications for proactive issue detection
</li><li>Installation and configuration of AOMEI Backup on selected desktops and laptops, storing backups on designated TrueNAS shares</li></ul>', '2250', '2250.00', false, 2, '2026-01-22 18:02:30.878526');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (44, 1, '1', '<ul><li>Dell OptiPlex 7010 SFF Desktop Intel Core i5-13600,14 Cores</li><li>16GB</li><li>Windows 11 Pro</li><li>Crucial - P310 2TB Internal SSD PCIe Gen 4 x4 NVMe M.2</li></ul>', '1079', '1079.00', false, 0, '2026-01-22 18:51:00.998206');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (45, 1, '1', '<p>DisplayPort to HDMI cable 10ft</p>', '20', '20.00', false, 1, '2026-01-22 18:51:00.998206');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (46, 1, '3', '<p>Setup and configure Dell OptiPlex 7010 off-site
Install all Lenovo drivers and latest Windows updates. Deliver to site and setup on local network
Transferred data from old computer including desktop items, documents, downloads, pictures, videos, browser bookmarks and favorites.
Setup printing and scanning as required. Install customer requested software.Test all hardware for proper Operation</p>', '125', '375.00', false, 2, '2026-01-22 18:51:00.998206');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (47, 2, '1', '<p>Lenovo Yoga 7 82YN 16" i5-1335U 1.3GHz 16GB RAM 512GB SSD</p>', '500', '500.00', false, 0, '2026-01-22 18:54:57.288474');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (48, 2, '2', '<p>Setup and configure Lenovo yoga 7 82YN 16" i5-1335U 1.3GHz 16GB RAM 512GB SSD off-site. Install all Lenovo drivers and latest Windows updates. Deliver to site and setup on local network. Transferred data from old computer including desktop items, documents, downloads, pictures, videos, browser bookmarks and favorites. Setup printing and scanning as required. Install customer requested software. Test all hardware for proper operation.</p>', '125', '250.00', false, 1, '2026-01-22 18:54:57.288474');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (49, 4, '1', '<ul><li>Dell OptiPlex 7020 Plus Tower Desktop PC Core i7-14700 </li><li>32GB DDR5 RAM, </li><li>2 TB SSD M.2 PCIe Gen4 TLC, </li><li>NVIDIA® GeForce RTX™ 5050</li></ul>', '2080', '2080.00', false, 0, '2026-01-22 20:00:28.631846');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (50, 4, '2', '<p>Setup and configure Lenovo Yoga as needed. Install all Lenovo drivers and latest Windows updates. Deliver to site and setup on local network. Transfer data from old computer including desktop items, documents, downloads, pictures, videos, browser bookmarks and favorites. Setup printing and scanning as required. Install customer requested software. Test all hardware for proper operation.</p>', '125', '250.00', false, 1, '2026-01-22 20:00:28.631846');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (59, 3, '3', '<ul><li><strong>Lenovo </strong>ThinkPad P16s Mobile Workstation</li><li><strong>Processor:</strong> Intel® Core™ Ultra 7 155H</li><li><strong>Graphics Card:</strong> NVIDIA RTX™ 500 Ada Generation Laptop GPU, <strong>4 GB GDDR6</strong></li><li><strong>Memory: 64 GB DDR5-5600 MT/s</strong></li><li><strong>Storage: 1 TB SSD</strong> M.2 2280 PCIe Gen4&nbsp;</li><li><strong>Display: </strong>16" &nbsp;WQUXGA (3840 × 2400) OLED</li><li><strong>Operating System:</strong> Windows 11 Pro 64-bit</li><li><strong>1 Year Warranty</strong></li><li><strong>(This device is new, not refurbished)</strong></li></ul>', '1949', '5847.00', false, 0, '2026-01-26 18:41:05.501558');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (60, 3, '', '<p>Setup and configure Lenovo Laptops as needed. Install all Lenovo drivers and latest Windows updates. Deliver to site and setup on local network. Setup printing and scanning as required. Install customer requested software. Test all hardware for proper operation.</p>', '125', 'TBD', true, 1, '2026-01-26 18:41:05.501558');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (74, 7, '1', '<p>Dell Optiplex 7010 (or similar) Tower configured with Intel Core i5-13500 processor, 16GB RAM, 512GB solid state drive and Windows 11 Professional. Refurbished with a one year warranty. </p>', '725.00', '725.00', false, 0, '2026-01-30 21:04:38.661279');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (75, 7, '3', '<p>Delivery and installation of Dell Tower PC. Setup on local network and install requested software. Install and configure local and network printers. Transfer requested data from existing PC and verify proper operation of all hardware. </p><p><br></p><p>Microsoft Office (if required) is not included in this quote.</p>', '125', '375.00', false, 1, '2026-01-30 21:04:38.661279');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (76, 6, '1', '<ul><li>Dell Tower Computer configured with Intel Core Ultra 5 235 processor, 16GB RAM, 512GB SSD and Windows 11 Professional. New with One Year Warranty.</li></ul>', '1325.00', '1325.00', false, 0, '2026-01-30 21:07:26.820637');
INSERT INTO public.quote_items (id, quote_id, quantity, description, rate, amount, is_tbd, item_order, created_at) VALUES (77, 6, '3', '<p>Delivery and installation of new Dell Tower PC. Setup on local network and install requested software. Install and configure local and network printers. Transfer requested data from existing PC and verify proper operation of all hardware. </p><p><br></p><p>Microsoft Office (if required) is not included in this quote.</p>', '125.00', '375.00', false, 1, '2026-01-30 21:07:26.820637');
--
-- Name: customers_id_seq; Type: SEQUENCE SET; Schema: public; Owner: quoteuser
--
SELECT pg_catalog.setval('public.customers_id_seq', 6, true);
--
-- Name: quote_items_id_seq; Type: SEQUENCE SET; Schema: public; Owner: quoteuser
--
SELECT pg_catalog.setval('public.quote_items_id_seq', 77, true);
--
-- Name: quotes_id_seq; Type: SEQUENCE SET; Schema: public; Owner: quoteuser
--
SELECT pg_catalog.setval('public.quotes_id_seq', 7, true);
--
-- PostgreSQL database dump complete
--
\unrestrict KCbrUeHdJ7srnFlBFWbQWdZ6A6bdMlTKPXbmEoc5qE3gaNBouFxTyfvdD9oETV4

View File

@ -18,15 +18,21 @@ function customerSearch(type) {
highlighted: 0, highlighted: 0,
get filteredCustomers() { get filteredCustomers() {
// Wenn Suche leer ist: ALLE Kunden zurückgeben (kein .slice mehr!)
if (!this.search) { if (!this.search) {
return customers.slice(0, 50); // Show first 50 if no search return customers;
} }
const searchLower = this.search.toLowerCase(); const searchLower = this.search.toLowerCase();
// Filtern: Sucht im Namen, Line1, Stadt oder Account Nummer
// Auch hier: Kein .slice mehr, damit alle Ergebnisse (z.B. alle mit 'C') angezeigt werden
return customers.filter(c => return customers.filter(c =>
c.name.toLowerCase().includes(searchLower) || (c.name || '').toLowerCase().includes(searchLower) ||
c.city.toLowerCase().includes(searchLower) || (c.line1 || '').toLowerCase().includes(searchLower) ||
(c.city || '').toLowerCase().includes(searchLower) ||
(c.account_number && c.account_number.includes(searchLower)) (c.account_number && c.account_number.includes(searchLower))
).slice(0, 50); // Limit to 50 results );
}, },
selectCustomer(customer) { selectCustomer(customer) {
@ -197,17 +203,32 @@ async function loadCustomers() {
function renderCustomers() { function renderCustomers() {
const tbody = document.getElementById('customers-list'); const tbody = document.getElementById('customers-list');
tbody.innerHTML = customers.map(customer => ` tbody.innerHTML = customers.map(customer => {
// Logik: Line 1-4 zusammenbauen
// filter(Boolean) entfernt null/undefined/leere Strings
const lines = [customer.line1, customer.line2, customer.line3, customer.line4].filter(Boolean);
// City, State, Zip anhängen
const cityStateZip = [customer.city, customer.state, customer.zip_code].filter(Boolean).join(' ');
// Alles zusammenfügen
let fullAddress = lines.join(', ');
if (cityStateZip) {
fullAddress += (fullAddress ? ', ' : '') + cityStateZip;
}
return `
<tr> <tr>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">${customer.name}</td> <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">${customer.name}</td>
<td class="px-6 py-4 text-sm text-gray-500">${customer.street}, ${customer.city}, ${customer.state} ${customer.zip_code}</td> <td class="px-6 py-4 text-sm text-gray-500">${fullAddress || '-'}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${customer.account_number || '-'}</td> <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${customer.account_number || '-'}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium space-x-2"> <td class="px-6 py-4 whitespace-nowrap text-sm font-medium space-x-2">
<button onclick="editCustomer(${customer.id})" class="text-blue-600 hover:text-blue-900">Edit</button> <button onclick="editCustomer(${customer.id})" class="text-blue-600 hover:text-blue-900">Edit</button>
<button onclick="deleteCustomer(${customer.id})" class="text-red-600 hover:text-red-900">Delete</button> <button onclick="deleteCustomer(${customer.id})" class="text-red-600 hover:text-red-900">Delete</button>
</td> </td>
</tr> </tr>
`).join(''); `;
}).join('');
} }
function openCustomerModal(customerId = null) { function openCustomerModal(customerId = null) {
@ -218,17 +239,29 @@ function openCustomerModal(customerId = null) {
if (customerId) { if (customerId) {
title.textContent = 'Edit Customer'; title.textContent = 'Edit Customer';
const customer = customers.find(c => c.id === customerId); const customer = customers.find(c => c.id === customerId);
document.getElementById('customer-id').value = customer.id; document.getElementById('customer-id').value = customer.id;
document.getElementById('customer-name').value = customer.name; document.getElementById('customer-name').value = customer.name;
document.getElementById('customer-street').value = customer.street;
document.getElementById('customer-city').value = customer.city; // Neue Felder befüllen
document.getElementById('customer-state').value = customer.state; document.getElementById('customer-line1').value = customer.line1 || '';
document.getElementById('customer-zip').value = customer.zip_code; document.getElementById('customer-line2').value = customer.line2 || '';
document.getElementById('customer-line3').value = customer.line3 || '';
document.getElementById('customer-line4').value = customer.line4 || '';
document.getElementById('customer-city').value = customer.city || '';
document.getElementById('customer-state').value = customer.state || '';
document.getElementById('customer-zip').value = customer.zip_code || '';
document.getElementById('customer-account').value = customer.account_number || ''; document.getElementById('customer-account').value = customer.account_number || '';
document.getElementById('customer-email').value = customer.email || '';
document.getElementById('customer-phone').value = customer.phone || '';
document.getElementById('customer-taxable').checked = customer.taxable !== false;
} else { } else {
title.textContent = 'New Customer'; title.textContent = 'New Customer';
document.getElementById('customer-form').reset(); document.getElementById('customer-form').reset();
document.getElementById('customer-id').value = ''; document.getElementById('customer-id').value = '';
document.getElementById('customer-taxable').checked = true;
} }
modal.classList.add('active'); modal.classList.add('active');
@ -244,11 +277,21 @@ async function handleCustomerSubmit(e) {
const data = { const data = {
name: document.getElementById('customer-name').value, name: document.getElementById('customer-name').value,
street: document.getElementById('customer-street').value,
// Neue Felder auslesen
line1: document.getElementById('customer-line1').value,
line2: document.getElementById('customer-line2').value,
line3: document.getElementById('customer-line3').value,
line4: document.getElementById('customer-line4').value,
city: document.getElementById('customer-city').value, city: document.getElementById('customer-city').value,
state: document.getElementById('customer-state').value.toUpperCase(), state: document.getElementById('customer-state').value.toUpperCase(),
zip_code: document.getElementById('customer-zip').value, zip_code: document.getElementById('customer-zip').value,
account_number: document.getElementById('customer-account').value account_number: document.getElementById('customer-account').value,
email: document.getElementById('customer-email')?.value || '',
phone: document.getElementById('customer-phone')?.value || '',
phone2: '', // Erstmal leer lassen, falls kein Feld im Formular ist
taxable: document.getElementById('customer-taxable')?.checked ?? true
}; };
try { try {
@ -398,81 +441,66 @@ function addQuoteItem(item = null) {
itemDiv.className = 'border border-gray-300 rounded-lg mb-3 bg-white'; itemDiv.className = 'border border-gray-300 rounded-lg mb-3 bg-white';
itemDiv.id = `quote-item-${itemId}`; itemDiv.id = `quote-item-${itemId}`;
itemDiv.setAttribute('x-data', `{ open: ${item ? 'false' : 'true'} }`); itemDiv.setAttribute('x-data', `{ open: ${item ? 'false' : 'true'} }`);
// Get preview text // Preview Text logic
const previewQty = item ? item.quantity : ''; const previewQty = item ? item.quantity : '';
const previewAmount = item ? item.amount : '$0.00'; const previewAmount = item ? item.amount : '$0.00';
let previewDesc = 'New item'; let previewDesc = 'New item';
if (item && item.description) { if (item && item.description) {
const temp = document.createElement('div'); const temp = document.createElement('div');
temp.innerHTML = item.description; temp.innerHTML = item.description;
previewDesc = temp.textContent.substring(0, 60) + (temp.textContent.length > 60 ? '...' : ''); previewDesc = temp.textContent.substring(0, 50) + (temp.textContent.length > 50 ? '...' : '');
} }
// Preview Type Logic (NEU)
const typeLabel = (item && item.qbo_item_id == '5') ? 'Labor' : 'Parts';
itemDiv.innerHTML = ` itemDiv.innerHTML = `
<!-- Accordion Header -->
<div class="flex items-center p-4"> <div class="flex items-center p-4">
<!-- Move Buttons (Left) - Outside accordion click area -->
<div class="flex flex-col mr-3" onclick="event.stopPropagation()"> <div class="flex flex-col mr-3" onclick="event.stopPropagation()">
<button type="button" onclick="moveQuoteItemUp(${itemId})" <button type="button" onclick="moveQuoteItemUp(${itemId})" class="text-blue-600 hover:text-blue-800 text-lg leading-none mb-1"></button>
class="text-blue-600 hover:text-blue-800 text-lg leading-none mb-1" <button type="button" onclick="moveQuoteItemDown(${itemId})" class="text-blue-600 hover:text-blue-800 text-lg leading-none"></button>
title="Move Up">
</button>
<button type="button" onclick="moveQuoteItemDown(${itemId})"
class="text-blue-600 hover:text-blue-800 text-lg leading-none"
title="Move Down">
</button>
</div> </div>
<!-- Accordion Toggle & Content (Center) -->
<div @click="open = !open" class="flex items-center flex-1 cursor-pointer hover:bg-gray-50 rounded px-3 py-2"> <div @click="open = !open" class="flex items-center flex-1 cursor-pointer hover:bg-gray-50 rounded px-3 py-2">
<svg x-show="!open" class="w-5 h-5 text-gray-500 mr-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"> <svg x-show="!open" class="w-5 h-5 text-gray-500 mr-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" /></svg>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" /> <svg x-show="open" class="w-5 h-5 text-gray-500 mr-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 15l7-7 7 7" /></svg>
</svg>
<svg x-show="open" class="w-5 h-5 text-gray-500 mr-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"> <span class="text-sm font-medium mr-4">Qty: <span class="item-qty-preview">${previewQty}</span></span>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 15l7-7 7 7" /> <span class="text-xs font-bold px-2 py-1 rounded bg-gray-200 text-gray-700 mr-4 item-type-preview">${typeLabel}</span>
</svg>
<span class="text-sm font-medium">Qty: <span class="item-qty-preview">${previewQty}</span></span>
<span class="text-sm text-gray-600 flex-1 truncate mx-4 item-desc-preview">${previewDesc}</span> <span class="text-sm text-gray-600 flex-1 truncate mx-4 item-desc-preview">${previewDesc}</span>
<span class="text-sm font-semibold item-amount-preview">${previewAmount}</span> <span class="text-sm font-semibold item-amount-preview">${previewAmount}</span>
</div> </div>
<!-- Delete Button (Right) - Outside accordion click area --> <button type="button" onclick="removeQuoteItem(${itemId}); event.stopPropagation();" class="ml-3 px-3 py-2 bg-red-500 hover:bg-red-600 text-white rounded-md text-sm">×</button>
<button type="button" onclick="removeQuoteItem(${itemId}); event.stopPropagation();"
class="ml-3 px-3 py-2 bg-red-500 hover:bg-red-600 text-white rounded-md text-sm">
×
</button>
</div> </div>
<!-- Accordion Content -->
<div x-show="open" x-transition class="p-4 border-t border-gray-200"> <div x-show="open" x-transition class="p-4 border-t border-gray-200">
<div class="grid grid-cols-12 gap-3 items-start"> <div class="grid grid-cols-12 gap-3 items-start">
<div class="col-span-1"> <div class="col-span-1">
<label class="block text-xs font-medium text-gray-700 mb-1">Qty</label> <label class="block text-xs font-medium text-gray-700 mb-1">Qty</label>
<input type="text" data-item="${itemId}" data-field="quantity" <input type="text" data-item="${itemId}" data-field="quantity" value="${item ? item.quantity : ''}" class="quote-item-input w-full px-2 py-2 border border-gray-300 rounded-md text-sm">
value="${item ? item.quantity : ''}"
class="quote-item-input w-full px-2 py-2 border border-gray-300 rounded-md text-sm">
</div> </div>
<div class="col-span-6">
<div class="col-span-2">
<label class="block text-xs font-medium text-gray-700 mb-1">Type (Internal)</label>
<select data-item="${itemId}" data-field="qbo_item_id" class="w-full px-2 py-2 border border-gray-300 rounded-md text-sm bg-white" onchange="updateItemPreview(this.closest('[id^=quote-item-]'), ${itemId})">
<option value="9" ${item && item.qbo_item_id == '9' ? 'selected' : ''}>Parts</option>
<option value="5" ${item && item.qbo_item_id == '5' ? 'selected' : ''}>Labor</option>
</select>
</div>
<div class="col-span-4">
<label class="block text-xs font-medium text-gray-700 mb-1">Description</label> <label class="block text-xs font-medium text-gray-700 mb-1">Description</label>
<div data-item="${itemId}" data-field="description" <div data-item="${itemId}" data-field="description" class="quote-item-description-editor border border-gray-300 rounded-md bg-white" style="min-height: 60px;"></div>
class="quote-item-description-editor border border-gray-300 rounded-md bg-white"
style="min-height: 60px;">
</div>
</div> </div>
<div class="col-span-2"> <div class="col-span-2">
<label class="block text-xs font-medium text-gray-700 mb-1">Rate</label> <label class="block text-xs font-medium text-gray-700 mb-1">Rate</label>
<input type="text" data-item="${itemId}" data-field="rate" <input type="text" data-item="${itemId}" data-field="rate" value="${item ? item.rate : ''}" class="quote-item-input w-full px-2 py-2 border border-gray-300 rounded-md text-sm">
value="${item ? item.rate : ''}"
class="quote-item-input w-full px-2 py-2 border border-gray-300 rounded-md text-sm">
</div> </div>
<div class="col-span-3"> <div class="col-span-3">
<label class="block text-xs font-medium text-gray-700 mb-1">Amount</label> <label class="block text-xs font-medium text-gray-700 mb-1">Amount</label>
<input type="text" data-item="${itemId}" data-field="amount" <input type="text" data-item="${itemId}" data-field="amount" value="${item ? item.amount : ''}" class="quote-item-amount w-full px-2 py-2 border border-gray-300 rounded-md text-sm">
value="${item ? item.amount : ''}"
class="quote-item-amount w-full px-2 py-2 border border-gray-300 rounded-md text-sm">
</div> </div>
</div> </div>
</div> </div>
@ -480,31 +508,18 @@ function addQuoteItem(item = null) {
itemsDiv.appendChild(itemDiv); itemsDiv.appendChild(itemDiv);
// Initialize Quill editor // Quill Init
const editorDiv = itemDiv.querySelector('.quote-item-description-editor'); const editorDiv = itemDiv.querySelector('.quote-item-description-editor');
const quill = new Quill(editorDiv, { const quill = new Quill(editorDiv, {
theme: 'snow', theme: 'snow',
modules: { modules: { toolbar: [['bold', 'italic', 'underline'], [{ 'list': 'ordered'}, { 'list': 'bullet' }], ['clean']] }
toolbar: [
['bold', 'italic', 'underline'],
[{ 'list': 'ordered'}, { 'list': 'bullet' }],
['clean']
]
}
});
if (item && item.description) {
quill.root.innerHTML = item.description;
}
quill.on('text-change', () => {
updateItemPreview(itemDiv, itemId);
updateQuoteTotals();
}); });
if (item && item.description) quill.root.innerHTML = item.description;
quill.on('text-change', () => { updateItemPreview(itemDiv, itemId); updateQuoteTotals(); });
editorDiv.quillInstance = quill; editorDiv.quillInstance = quill;
// Auto-calculate amount and update preview // Auto-calculate logic
const qtyInput = itemDiv.querySelector('[data-field="quantity"]'); const qtyInput = itemDiv.querySelector('[data-field="quantity"]');
const rateInput = itemDiv.querySelector('[data-field="rate"]'); const rateInput = itemDiv.querySelector('[data-field="rate"]');
const amountInput = itemDiv.querySelector('[data-field="amount"]'); const amountInput = itemDiv.querySelector('[data-field="amount"]');
@ -521,10 +536,7 @@ function addQuoteItem(item = null) {
qtyInput.addEventListener('input', calculateAmount); qtyInput.addEventListener('input', calculateAmount);
rateInput.addEventListener('input', calculateAmount); rateInput.addEventListener('input', calculateAmount);
amountInput.addEventListener('input', () => { amountInput.addEventListener('input', () => { updateItemPreview(itemDiv, itemId); updateQuoteTotals(); });
updateItemPreview(itemDiv, itemId);
updateQuoteTotals();
});
updateItemPreview(itemDiv, itemId); updateItemPreview(itemDiv, itemId);
updateQuoteTotals(); updateQuoteTotals();
@ -533,18 +545,25 @@ function addQuoteItem(item = null) {
function updateItemPreview(itemDiv, itemId) { function updateItemPreview(itemDiv, itemId) {
const qtyInput = itemDiv.querySelector('[data-field="quantity"]'); const qtyInput = itemDiv.querySelector('[data-field="quantity"]');
const amountInput = itemDiv.querySelector('[data-field="amount"]'); const amountInput = itemDiv.querySelector('[data-field="amount"]');
const typeInput = itemDiv.querySelector('[data-field="qbo_item_id"]'); // NEU
const editorDiv = itemDiv.querySelector('.quote-item-description-editor'); const editorDiv = itemDiv.querySelector('.quote-item-description-editor');
const qtyPreview = itemDiv.querySelector('.item-qty-preview'); const qtyPreview = itemDiv.querySelector('.item-qty-preview');
const descPreview = itemDiv.querySelector('.item-desc-preview'); const descPreview = itemDiv.querySelector('.item-desc-preview');
const amountPreview = itemDiv.querySelector('.item-amount-preview'); const amountPreview = itemDiv.querySelector('.item-amount-preview');
const typePreview = itemDiv.querySelector('.item-type-preview'); // NEU
if (qtyPreview) qtyPreview.textContent = qtyInput.value || '0'; if (qtyPreview) qtyPreview.textContent = qtyInput.value || '0';
if (amountPreview) amountPreview.textContent = amountInput.value || '$0.00'; if (amountPreview) amountPreview.textContent = amountInput.value || '$0.00';
// NEU: Update Type Label
if (typePreview && typeInput) {
typePreview.textContent = typeInput.value == '5' ? 'Labor' : 'Parts';
}
if (descPreview && editorDiv.quillInstance) { if (descPreview && editorDiv.quillInstance) {
const plainText = editorDiv.quillInstance.getText().trim(); const plainText = editorDiv.quillInstance.getText().trim();
const preview = plainText.substring(0, 60) + (plainText.length > 60 ? '...' : ''); const preview = plainText.substring(0, 50) + (plainText.length > 50 ? '...' : '');
descPreview.textContent = preview || 'New item'; descPreview.textContent = preview || 'New item';
} }
} }
@ -612,13 +631,14 @@ function getQuoteItems() {
const item = { const item = {
quantity: div.querySelector('[data-field="quantity"]').value, quantity: div.querySelector('[data-field="quantity"]').value,
// NEU: ID holen
qbo_item_id: div.querySelector('[data-field="qbo_item_id"]').value,
description: descriptionHTML, description: descriptionHTML,
rate: div.querySelector('[data-field="rate"]').value, rate: div.querySelector('[data-field="rate"]').value,
amount: div.querySelector('[data-field="amount"]').value amount: div.querySelector('[data-field="amount"]').value
}; };
items.push(item); items.push(item);
}); });
return items; return items;
} }
@ -741,6 +761,9 @@ function renderInvoices() {
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 font-semibold">$${parseFloat(invoice.total).toFixed(2)}</td> <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 font-semibold">$${parseFloat(invoice.total).toFixed(2)}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium space-x-2"> <td class="px-6 py-4 whitespace-nowrap text-sm font-medium space-x-2">
<button onclick="viewInvoicePDF(${invoice.id})" class="text-green-600 hover:text-green-900">PDF</button> <button onclick="viewInvoicePDF(${invoice.id})" class="text-green-600 hover:text-green-900">PDF</button>
<button onclick="exportToQBO(${invoice.id})" class="text-orange-600 hover:text-orange-900" title="Export to QuickBooks">
QBO Export
</button>
<button onclick="editInvoice(${invoice.id})" class="text-blue-600 hover:text-blue-900">Edit</button> <button onclick="editInvoice(${invoice.id})" class="text-blue-600 hover:text-blue-900">Edit</button>
<button onclick="deleteInvoice(${invoice.id})" class="text-red-600 hover:text-red-900">Delete</button> <button onclick="deleteInvoice(${invoice.id})" class="text-red-600 hover:text-red-900">Delete</button>
</td> </td>
@ -822,81 +845,66 @@ function addInvoiceItem(item = null) {
itemDiv.className = 'border border-gray-300 rounded-lg mb-3 bg-white'; itemDiv.className = 'border border-gray-300 rounded-lg mb-3 bg-white';
itemDiv.id = `invoice-item-${itemId}`; itemDiv.id = `invoice-item-${itemId}`;
itemDiv.setAttribute('x-data', `{ open: ${item ? 'false' : 'true'} }`); itemDiv.setAttribute('x-data', `{ open: ${item ? 'false' : 'true'} }`);
// Get preview text // Preview Text logic
const previewQty = item ? item.quantity : ''; const previewQty = item ? item.quantity : '';
const previewAmount = item ? item.amount : '$0.00'; const previewAmount = item ? item.amount : '$0.00';
let previewDesc = 'New item'; let previewDesc = 'New item';
if (item && item.description) { if (item && item.description) {
const temp = document.createElement('div'); const temp = document.createElement('div');
temp.innerHTML = item.description; temp.innerHTML = item.description;
previewDesc = temp.textContent.substring(0, 60) + (temp.textContent.length > 60 ? '...' : ''); previewDesc = temp.textContent.substring(0, 50) + (temp.textContent.length > 50 ? '...' : '');
} }
// Preview Type Logic
const typeLabel = (item && item.qbo_item_id == '5') ? 'Labor' : 'Parts';
itemDiv.innerHTML = ` itemDiv.innerHTML = `
<!-- Accordion Header -->
<div class="flex items-center p-4"> <div class="flex items-center p-4">
<!-- Move Buttons (Left) - Outside accordion click area -->
<div class="flex flex-col mr-3" onclick="event.stopPropagation()"> <div class="flex flex-col mr-3" onclick="event.stopPropagation()">
<button type="button" onclick="moveInvoiceItemUp(${itemId})" <button type="button" onclick="moveInvoiceItemUp(${itemId})" class="text-blue-600 hover:text-blue-800 text-lg leading-none mb-1"></button>
class="text-blue-600 hover:text-blue-800 text-lg leading-none mb-1" <button type="button" onclick="moveInvoiceItemDown(${itemId})" class="text-blue-600 hover:text-blue-800 text-lg leading-none"></button>
title="Move Up">
</button>
<button type="button" onclick="moveInvoiceItemDown(${itemId})"
class="text-blue-600 hover:text-blue-800 text-lg leading-none"
title="Move Down">
</button>
</div> </div>
<!-- Accordion Toggle & Content (Center) -->
<div @click="open = !open" class="flex items-center flex-1 cursor-pointer hover:bg-gray-50 rounded px-3 py-2"> <div @click="open = !open" class="flex items-center flex-1 cursor-pointer hover:bg-gray-50 rounded px-3 py-2">
<svg x-show="!open" class="w-5 h-5 text-gray-500 mr-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"> <svg x-show="!open" class="w-5 h-5 text-gray-500 mr-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" /></svg>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" /> <svg x-show="open" class="w-5 h-5 text-gray-500 mr-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 15l7-7 7 7" /></svg>
</svg>
<svg x-show="open" class="w-5 h-5 text-gray-500 mr-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"> <span class="text-sm font-medium mr-4">Qty: <span class="item-qty-preview">${previewQty}</span></span>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 15l7-7 7 7" /> <span class="text-xs font-bold px-2 py-1 rounded bg-gray-200 text-gray-700 mr-4 item-type-preview">${typeLabel}</span>
</svg>
<span class="text-sm font-medium">Qty: <span class="item-qty-preview">${previewQty}</span></span>
<span class="text-sm text-gray-600 flex-1 truncate mx-4 item-desc-preview">${previewDesc}</span> <span class="text-sm text-gray-600 flex-1 truncate mx-4 item-desc-preview">${previewDesc}</span>
<span class="text-sm font-semibold item-amount-preview">${previewAmount}</span> <span class="text-sm font-semibold item-amount-preview">${previewAmount}</span>
</div> </div>
<!-- Delete Button (Right) - Outside accordion click area --> <button type="button" onclick="removeInvoiceItem(${itemId}); event.stopPropagation();" class="ml-3 px-3 py-2 bg-red-500 hover:bg-red-600 text-white rounded-md text-sm">×</button>
<button type="button" onclick="removeInvoiceItem(${itemId}); event.stopPropagation();"
class="ml-3 px-3 py-2 bg-red-500 hover:bg-red-600 text-white rounded-md text-sm">
×
</button>
</div> </div>
<!-- Accordion Content -->
<div x-show="open" x-transition class="p-4 border-t border-gray-200"> <div x-show="open" x-transition class="p-4 border-t border-gray-200">
<div class="grid grid-cols-12 gap-3 items-start"> <div class="grid grid-cols-12 gap-3 items-start">
<div class="col-span-1"> <div class="col-span-1">
<label class="block text-xs font-medium text-gray-700 mb-1">Qty</label> <label class="block text-xs font-medium text-gray-700 mb-1">Qty</label>
<input type="text" data-item="${itemId}" data-field="quantity" <input type="text" data-item="${itemId}" data-field="quantity" value="${item ? item.quantity : ''}" class="invoice-item-input w-full px-2 py-2 border border-gray-300 rounded-md text-sm">
value="${item ? item.quantity : ''}"
class="invoice-item-input w-full px-2 py-2 border border-gray-300 rounded-md text-sm">
</div> </div>
<div class="col-span-6">
<div class="col-span-2">
<label class="block text-xs font-medium text-gray-700 mb-1">Type (Internal)</label>
<select data-item="${itemId}" data-field="qbo_item_id" class="w-full px-2 py-2 border border-gray-300 rounded-md text-sm bg-white" onchange="updateInvoiceItemPreview(this.closest('[id^=invoice-item-]'), ${itemId})">
<option value="9" ${item && item.qbo_item_id == '9' ? 'selected' : ''}>Parts</option>
<option value="5" ${item && item.qbo_item_id == '5' ? 'selected' : ''}>Labor</option>
</select>
</div>
<div class="col-span-4">
<label class="block text-xs font-medium text-gray-700 mb-1">Description</label> <label class="block text-xs font-medium text-gray-700 mb-1">Description</label>
<div data-item="${itemId}" data-field="description" <div data-item="${itemId}" data-field="description" class="invoice-item-description-editor border border-gray-300 rounded-md bg-white" style="min-height: 60px;"></div>
class="invoice-item-description-editor border border-gray-300 rounded-md bg-white"
style="min-height: 60px;">
</div>
</div> </div>
<div class="col-span-2"> <div class="col-span-2">
<label class="block text-xs font-medium text-gray-700 mb-1">Rate</label> <label class="block text-xs font-medium text-gray-700 mb-1">Rate</label>
<input type="text" data-item="${itemId}" data-field="rate" <input type="text" data-item="${itemId}" data-field="rate" value="${item ? item.rate : ''}" class="invoice-item-input w-full px-2 py-2 border border-gray-300 rounded-md text-sm">
value="${item ? item.rate : ''}"
class="invoice-item-input w-full px-2 py-2 border border-gray-300 rounded-md text-sm">
</div> </div>
<div class="col-span-3"> <div class="col-span-3">
<label class="block text-xs font-medium text-gray-700 mb-1">Amount</label> <label class="block text-xs font-medium text-gray-700 mb-1">Amount</label>
<input type="text" data-item="${itemId}" data-field="amount" <input type="text" data-item="${itemId}" data-field="amount" value="${item ? item.amount : ''}" class="invoice-item-amount w-full px-2 py-2 border border-gray-300 rounded-md text-sm">
value="${item ? item.amount : ''}"
class="invoice-item-amount w-full px-2 py-2 border border-gray-300 rounded-md text-sm">
</div> </div>
</div> </div>
</div> </div>
@ -904,31 +912,18 @@ function addInvoiceItem(item = null) {
itemsDiv.appendChild(itemDiv); itemsDiv.appendChild(itemDiv);
// Initialize Quill editor // Quill Init (wie vorher)
const editorDiv = itemDiv.querySelector('.invoice-item-description-editor'); const editorDiv = itemDiv.querySelector('.invoice-item-description-editor');
const quill = new Quill(editorDiv, { const quill = new Quill(editorDiv, {
theme: 'snow', theme: 'snow',
modules: { modules: { toolbar: [['bold', 'italic', 'underline'], [{ 'list': 'ordered'}, { 'list': 'bullet' }], ['clean']] }
toolbar: [
['bold', 'italic', 'underline'],
[{ 'list': 'ordered'}, { 'list': 'bullet' }],
['clean']
]
}
});
if (item && item.description) {
quill.root.innerHTML = item.description;
}
quill.on('text-change', () => {
updateInvoiceItemPreview(itemDiv, itemId);
updateInvoiceTotals();
}); });
if (item && item.description) quill.root.innerHTML = item.description;
quill.on('text-change', () => { updateInvoiceItemPreview(itemDiv, itemId); updateInvoiceTotals(); });
editorDiv.quillInstance = quill; editorDiv.quillInstance = quill;
// Auto-calculate amount and update preview // Auto-calculate logic (wie vorher)
const qtyInput = itemDiv.querySelector('[data-field="quantity"]'); const qtyInput = itemDiv.querySelector('[data-field="quantity"]');
const rateInput = itemDiv.querySelector('[data-field="rate"]'); const rateInput = itemDiv.querySelector('[data-field="rate"]');
const amountInput = itemDiv.querySelector('[data-field="amount"]'); const amountInput = itemDiv.querySelector('[data-field="amount"]');
@ -945,10 +940,7 @@ function addInvoiceItem(item = null) {
qtyInput.addEventListener('input', calculateAmount); qtyInput.addEventListener('input', calculateAmount);
rateInput.addEventListener('input', calculateAmount); rateInput.addEventListener('input', calculateAmount);
amountInput.addEventListener('input', () => { amountInput.addEventListener('input', () => { updateInvoiceItemPreview(itemDiv, itemId); updateInvoiceTotals(); });
updateInvoiceItemPreview(itemDiv, itemId);
updateInvoiceTotals();
});
updateInvoiceItemPreview(itemDiv, itemId); updateInvoiceItemPreview(itemDiv, itemId);
updateInvoiceTotals(); updateInvoiceTotals();
@ -957,18 +949,25 @@ function addInvoiceItem(item = null) {
function updateInvoiceItemPreview(itemDiv, itemId) { function updateInvoiceItemPreview(itemDiv, itemId) {
const qtyInput = itemDiv.querySelector('[data-field="quantity"]'); const qtyInput = itemDiv.querySelector('[data-field="quantity"]');
const amountInput = itemDiv.querySelector('[data-field="amount"]'); const amountInput = itemDiv.querySelector('[data-field="amount"]');
const typeInput = itemDiv.querySelector('[data-field="qbo_item_id"]'); // NEU
const editorDiv = itemDiv.querySelector('.invoice-item-description-editor'); const editorDiv = itemDiv.querySelector('.invoice-item-description-editor');
const qtyPreview = itemDiv.querySelector('.item-qty-preview'); const qtyPreview = itemDiv.querySelector('.item-qty-preview');
const descPreview = itemDiv.querySelector('.item-desc-preview'); const descPreview = itemDiv.querySelector('.item-desc-preview');
const amountPreview = itemDiv.querySelector('.item-amount-preview'); const amountPreview = itemDiv.querySelector('.item-amount-preview');
const typePreview = itemDiv.querySelector('.item-type-preview'); // NEU
if (qtyPreview) qtyPreview.textContent = qtyInput.value || '0'; if (qtyPreview) qtyPreview.textContent = qtyInput.value || '0';
if (amountPreview) amountPreview.textContent = amountInput.value || '$0.00'; if (amountPreview) amountPreview.textContent = amountInput.value || '$0.00';
// NEU: Update Type Label
if (typePreview && typeInput) {
typePreview.textContent = typeInput.value == '5' ? 'Labor' : 'Parts';
}
if (descPreview && editorDiv.quillInstance) { if (descPreview && editorDiv.quillInstance) {
const plainText = editorDiv.quillInstance.getText().trim(); const plainText = editorDiv.quillInstance.getText().trim();
const preview = plainText.substring(0, 60) + (plainText.length > 60 ? '...' : ''); const preview = plainText.substring(0, 50) + (plainText.length > 50 ? '...' : '');
descPreview.textContent = preview || 'New item'; descPreview.textContent = preview || 'New item';
} }
} }
@ -1025,19 +1024,18 @@ function getInvoiceItems() {
itemDivs.forEach(div => { itemDivs.forEach(div => {
const descEditor = div.querySelector('.invoice-item-description-editor'); const descEditor = div.querySelector('.invoice-item-description-editor');
const descriptionHTML = descEditor && descEditor.quillInstance const descriptionHTML = descEditor && descEditor.quillInstance ? descEditor.quillInstance.root.innerHTML : '';
? descEditor.quillInstance.root.innerHTML
: '';
const item = { const item = {
quantity: div.querySelector('[data-field="quantity"]').value, quantity: div.querySelector('[data-field="quantity"]').value,
// NEU: ID holen
qbo_item_id: div.querySelector('[data-field="qbo_item_id"]').value,
description: descriptionHTML, description: descriptionHTML,
rate: div.querySelector('[data-field="rate"]').value, rate: div.querySelector('[data-field="rate"]').value,
amount: div.querySelector('[data-field="amount"]').value amount: div.querySelector('[data-field="amount"]').value
}; };
items.push(item); items.push(item);
}); });
return items; return items;
} }
@ -1113,4 +1111,33 @@ async function deleteInvoice(id) {
function viewInvoicePDF(id) { function viewInvoicePDF(id) {
window.open(`/api/invoices/${id}/pdf`, '_blank'); window.open(`/api/invoices/${id}/pdf`, '_blank');
}
async function exportToQBO(id) {
if (!confirm('Rechnung wirklich an QuickBooks Online senden?')) return;
// UI Feedback (einfach, aber wirksam)
const btn = event.target; // Der geklickte Button
const originalText = btn.textContent;
btn.textContent = "⏳...";
btn.disabled = true;
try {
const response = await fetch(`/api/invoices/${id}/export`, { method: 'POST' });
const result = await response.json();
if (response.ok) {
alert(`✅ Erfolg! QBO ID: ${result.qbo_id}`);
// Optional: Liste neu laden um Status zu aktualisieren
loadInvoices();
} else {
alert(`❌ Fehler: ${result.error}`);
}
} catch (error) {
console.error(error);
alert('Netzwerkfehler beim Export.');
} finally {
btn.textContent = originalText;
btn.disabled = false;
}
} }

View File

@ -169,41 +169,74 @@
<div> <div>
<label class="block text-sm font-medium text-gray-700 mb-1">Company Name</label> <label class="block text-sm font-medium text-gray-700 mb-1">Company Name</label>
<input type="text" id="customer-name" required <input type="text" id="customer-name" required
class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"> class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
</div> </div>
<div> <div class="space-y-3 pt-2">
<label class="block text-sm font-medium text-gray-700 mb-1">Street Address</label> <label class="block text-sm font-medium text-gray-700">Billing Address</label>
<input type="text" id="customer-street" required
class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"> <input type="text" id="customer-line1" placeholder="Line 1 (Street / PO Box / Company)"
class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
<input type="text" id="customer-line2" placeholder="Line 2"
class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
<div class="grid grid-cols-2 gap-4">
<input type="text" id="customer-line3" placeholder="Line 3"
class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
<input type="text" id="customer-line4" placeholder="Line 4"
class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
</div>
</div> </div>
<div class="grid grid-cols-3 gap-4"> <div class="grid grid-cols-3 gap-4 pt-2">
<div class="col-span-2"> <div class="col-span-2">
<label class="block text-sm font-medium text-gray-700 mb-1">City</label> <label class="block text-sm font-medium text-gray-700 mb-1">City</label>
<input type="text" id="customer-city" required <input type="text" id="customer-city"
class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"> class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
</div> </div>
<div> <div>
<label class="block text-sm font-medium text-gray-700 mb-1">State</label> <label class="block text-sm font-medium text-gray-700 mb-1">State</label>
<input type="text" id="customer-state" required maxlength="2" placeholder="TX" <input type="text" id="customer-state" maxlength="2" placeholder="TX"
class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"> class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
</div> </div>
</div> </div>
<div class="grid grid-cols-2 gap-4"> <div class="grid grid-cols-2 gap-4">
<div> <div>
<label class="block text-sm font-medium text-gray-700 mb-1">Zip Code</label> <label class="block text-sm font-medium text-gray-700 mb-1">Zip Code</label>
<input type="text" id="customer-zip" required <input type="text" id="customer-zip"
class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"> class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
</div> </div>
<div> <div>
<label class="block text-sm font-medium text-gray-700 mb-1">Account Number</label> <label class="block text-sm font-medium text-gray-700 mb-1">Account Number</label>
<input type="text" id="customer-account" <input type="text" id="customer-account"
class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"> class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
</div> </div>
</div> </div>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Email</label>
<input type="email" id="customer-email"
class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Phone</label>
<input type="tel" id="customer-phone"
class="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500">
</div>
</div>
<div class="pt-2">
<div class="flex items-center">
<input type="checkbox" id="customer-taxable"
class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded">
<label for="customer-taxable" class="ml-2 block text-sm text-gray-900">Taxable</label>
</div>
</div>
<div class="flex justify-end space-x-3 pt-4"> <div class="flex justify-end space-x-3 pt-4">
<button type="button" onclick="closeCustomerModal()" <button type="button" onclick="closeCustomerModal()"
class="px-6 py-2 bg-gray-200 text-gray-700 rounded-md hover:bg-gray-300"> class="px-6 py-2 bg-gray-200 text-gray-700 rounded-md hover:bg-gray-300">

86
qbo_helper.js Normal file
View File

@ -0,0 +1,86 @@
// qbo_helper.js
require('dotenv').config();
const OAuthClient = require('intuit-oauth');
let oauthClient = null;
const getOAuthClient = () => {
if (!oauthClient) {
oauthClient = new OAuthClient({
clientId: process.env.QBO_CLIENT_ID,
clientSecret: process.env.QBO_CLIENT_SECRET,
environment: process.env.QBO_ENVIRONMENT || 'sandbox',
redirectUri: process.env.QBO_REDIRECT_URI,
token: {
access_token: process.env.QBO_ACCESS_TOKEN || '',
refresh_token: process.env.QBO_REFRESH_TOKEN || '',
realmId: process.env.QBO_REALM_ID
}
});
}
return oauthClient;
};
async function makeQboApiCall(requestOptions) {
const client = getOAuthClient();
// Funktion zum Aktualisieren des Tokens
const doRefresh = async () => {
console.log("🔄 QBO Token Refresh wird ausgeführt...");
try {
const authResponse = await client.refresh();
console.log("✅ Token erfolgreich erneuert.");
return authResponse;
} catch (e) {
console.error("❌ Refresh fehlgeschlagen:", e);
throw e;
}
};
// 1. Pre-Check: Wenn Token leer ist, sofort refreshen
if (!client.token.access_token) {
console.log("⚠️ Kein Access Token gefunden. Versuche sofortigen Refresh...");
await doRefresh();
}
// 2. Pre-Check: Ist Token laut Zeitstempel abgelaufen?
if (!client.isAccessTokenValid()) {
console.log("⚠️ Token ist zeitlich abgelaufen. Refresh...");
await doRefresh();
}
try {
const response = await client.makeApiCall(requestOptions);
// Prüfen, ob QBO trotz HTTP 200/400 eine Fehlermeldung im Body sendet
const data = response.getJson ? response.getJson() : response.json;
if (data.fault && data.fault.error) {
const errorCode = data.fault.error[0].code;
// Fehler 3202 = Missing Access Token
if (errorCode === '3202') {
console.log("⚠️ QBO meldet fehlenden Token (3202). Versuche Refresh und Retry...");
await doRefresh();
return await client.makeApiCall(requestOptions);
}
// Anderen API-Fehler werfen, damit server.js ihn fängt
throw new Error(`QBO API Error ${errorCode}: ${data.fault.error[0].message}`);
}
return response;
} catch (e) {
// HTTP 401 Unauthorized fangen
if (e.response?.status === 401) {
console.log("⚠️ 401 Unauthorized. Versuche Refresh und Retry...");
await doRefresh();
return await client.makeApiCall(requestOptions);
}
throw e;
}
}
module.exports = {
getOAuthClient,
makeQboApiCall
};

339
server.js
View File

@ -4,6 +4,7 @@ const path = require('path');
const puppeteer = require('puppeteer'); const puppeteer = require('puppeteer');
const fs = require('fs').promises; const fs = require('fs').promises;
const multer = require('multer'); const multer = require('multer');
const { makeQboApiCall, getOAuthClient } = require('./qbo_helper');
const app = express(); const app = express();
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
@ -161,12 +162,25 @@ app.get('/api/customers', async (req, res) => {
} }
}); });
// POST /api/customers
app.post('/api/customers', async (req, res) => { app.post('/api/customers', async (req, res) => {
const { name, street, city, state, zip_code, account_number } = req.body; // line1 bis line4 statt street/pobox/suite
const {
name, line1, line2, line3, line4, city, state, zip_code,
account_number, email, phone, phone2, taxable
} = req.body;
try { try {
const result = await pool.query( const result = await pool.query(
'INSERT INTO customers (name, street, city, state, zip_code, account_number) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *', `INSERT INTO customers
[name, street, city, state, zip_code, account_number] (name, line1, line2, line3, line4, city, state,
zip_code, account_number, email, phone, phone2, taxable)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
RETURNING *`,
[name, line1 || null, line2 || null, line3 || null, line4 || null,
city || null, state || null, zip_code || null,
account_number || null, email || null, phone || null, phone2 || null,
taxable !== undefined ? taxable : true]
); );
res.json(result.rows[0]); res.json(result.rows[0]);
} catch (error) { } catch (error) {
@ -175,13 +189,26 @@ app.post('/api/customers', async (req, res) => {
} }
}); });
// PUT /api/customers/:id
app.put('/api/customers/:id', async (req, res) => { app.put('/api/customers/:id', async (req, res) => {
const { id } = req.params; const { id } = req.params;
const { name, street, city, state, zip_code, account_number } = req.body; const {
name, line1, line2, line3, line4, city, state, zip_code,
account_number, email, phone, phone2, taxable
} = req.body;
try { try {
const result = await pool.query( const result = await pool.query(
'UPDATE customers SET name = $1, street = $2, city = $3, state = $4, zip_code = $5, account_number = $6, updated_at = CURRENT_TIMESTAMP WHERE id = $7 RETURNING *', `UPDATE customers
[name, street, city, state, zip_code, account_number, id] SET name = $1, line1 = $2, line2 = $3, line3 = $4, line4 = $5,
city = $6, state = $7, zip_code = $8, account_number = $9, email = $10,
phone = $11, phone2 = $12, taxable = $13, updated_at = CURRENT_TIMESTAMP
WHERE id = $14
RETURNING *`,
[name, line1 || null, line2 || null, line3 || null, line4 || null,
city || null, state || null, zip_code || null,
account_number || null, email || null, phone || null, phone2 || null,
taxable !== undefined ? taxable : true, id]
); );
res.json(result.rows[0]); res.json(result.rows[0]);
} catch (error) { } catch (error) {
@ -220,8 +247,9 @@ app.get('/api/quotes', async (req, res) => {
app.get('/api/quotes/:id', async (req, res) => { app.get('/api/quotes/:id', async (req, res) => {
const { id } = req.params; const { id } = req.params;
try { try {
// KORRIGIERT: c.line1...c.line4 statt c.street
const quoteResult = await pool.query(` const quoteResult = await pool.query(`
SELECT q.*, c.name as customer_name, c.street, c.city, c.state, c.zip_code, c.account_number SELECT q.*, c.name as customer_name, c.line1, c.line2, c.line3, c.line4, c.city, c.state, c.zip_code, c.account_number
FROM quotes q FROM quotes q
LEFT JOIN customers c ON q.customer_id = c.id LEFT JOIN customers c ON q.customer_id = c.id
WHERE q.id = $1 WHERE q.id = $1
@ -283,8 +311,8 @@ app.post('/api/quotes', async (req, res) => {
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
await client.query( await client.query(
'INSERT INTO quote_items (quote_id, quantity, description, rate, amount, item_order) VALUES ($1, $2, $3, $4, $5, $6)', 'INSERT INTO quote_items (quote_id, quantity, description, rate, amount, item_order, qbo_item_id) VALUES ($1, $2, $3, $4, $5, $6, $7)',
[quoteId, items[i].quantity, items[i].description, items[i].rate, items[i].amount, i] [quoteId, items[i].quantity, items[i].description, items[i].rate, items[i].amount, i, items[i].qbo_item_id || '9']
); );
} }
@ -336,8 +364,8 @@ app.put('/api/quotes/:id', async (req, res) => {
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
await client.query( await client.query(
'INSERT INTO quote_items (quote_id, quantity, description, rate, amount, item_order) VALUES ($1, $2, $3, $4, $5, $6)', 'INSERT INTO quote_items (quote_id, quantity, description, rate, amount, item_order, qbo_item_id) VALUES ($1, $2, $3, $4, $5, $6, $7)',
[id, items[i].quantity, items[i].description, items[i].rate, items[i].amount, i] [id, items[i].quantity, items[i].description, items[i].rate, items[i].amount, i, items[i].qbo_item_id || '9']
); );
} }
@ -400,8 +428,9 @@ app.get('/api/invoices/next-number', async (req, res) => {
app.get('/api/invoices/:id', async (req, res) => { app.get('/api/invoices/:id', async (req, res) => {
const { id } = req.params; const { id } = req.params;
try { try {
// KORRIGIERT: c.line1, c.line2, c.line3, c.line4 statt c.street
const invoiceResult = await pool.query(` const invoiceResult = await pool.query(`
SELECT i.*, c.name as customer_name, c.street, c.city, c.state, c.zip_code, c.account_number SELECT i.*, c.name as customer_name, c.line1, c.line2, c.line3, c.line4, c.city, c.state, c.zip_code, c.account_number
FROM invoices i FROM invoices i
LEFT JOIN customers c ON i.customer_id = c.id LEFT JOIN customers c ON i.customer_id = c.id
WHERE i.id = $1 WHERE i.id = $1
@ -473,8 +502,9 @@ app.post('/api/invoices', async (req, res) => {
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
await client.query( await client.query(
'INSERT INTO invoice_items (invoice_id, quantity, description, rate, amount, item_order) VALUES ($1, $2, $3, $4, $5, $6)', // qbo_item_id hinzugefügt
[invoiceId, items[i].quantity, items[i].description, items[i].rate, items[i].amount, i] 'INSERT INTO invoice_items (invoice_id, quantity, description, rate, amount, item_order, qbo_item_id) VALUES ($1, $2, $3, $4, $5, $6, $7)',
[invoiceId, items[i].quantity, items[i].description, items[i].rate, items[i].amount, i, items[i].qbo_item_id || '9'] // Default '9' (Parts)
); );
} }
@ -496,8 +526,9 @@ app.post('/api/quotes/:id/convert-to-invoice', async (req, res) => {
try { try {
await client.query('BEGIN'); await client.query('BEGIN');
// KORRIGIERT: c.line1...c.line4 statt c.street
const quoteResult = await pool.query(` const quoteResult = await pool.query(`
SELECT q.*, c.name as customer_name, c.street, c.city, c.state, c.zip_code, c.account_number SELECT q.*, c.name as customer_name, c.line1, c.line2, c.line3, c.line4, c.city, c.state, c.zip_code, c.account_number
FROM quotes q FROM quotes q
LEFT JOIN customers c ON q.customer_id = c.id LEFT JOIN customers c ON q.customer_id = c.id
WHERE q.id = $1 WHERE q.id = $1
@ -538,8 +569,8 @@ app.post('/api/quotes/:id/convert-to-invoice', async (req, res) => {
for (let i = 0; i < itemsResult.rows.length; i++) { for (let i = 0; i < itemsResult.rows.length; i++) {
const item = itemsResult.rows[i]; const item = itemsResult.rows[i];
await client.query( await client.query(
'INSERT INTO invoice_items (invoice_id, quantity, description, rate, amount, item_order) VALUES ($1, $2, $3, $4, $5, $6)', 'INSERT INTO invoice_items (invoice_id, quantity, description, rate, amount, item_order, qbo_item_id) VALUES ($1, $2, $3, $4, $5, $6, $7)',
[invoiceId, item.quantity, item.description, item.rate, item.amount, i] [invoiceId, item.quantity, item.description, item.rate, item.amount, i, item.qbo_item_id || '9']
); );
} }
@ -603,8 +634,8 @@ app.put('/api/invoices/:id', async (req, res) => {
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
await client.query( await client.query(
'INSERT INTO invoice_items (invoice_id, quantity, description, rate, amount, item_order) VALUES ($1, $2, $3, $4, $5, $6)', 'INSERT INTO invoice_items (invoice_id, quantity, description, rate, amount, item_order, qbo_item_id) VALUES ($1, $2, $3, $4, $5, $6, $7)',
[id, items[i].quantity, items[i].description, items[i].rate, items[i].amount, i] [id, items[i].quantity, items[i].description, items[i].rate, items[i].amount, i, items[i].qbo_item_id || '9']
); );
} }
@ -646,8 +677,9 @@ app.get('/api/quotes/:id/pdf', async (req, res) => {
console.log(`[PDF] Starting quote PDF generation for ID: ${id}`); console.log(`[PDF] Starting quote PDF generation for ID: ${id}`);
try { try {
// KORRIGIERT: Abfrage von line1-4 statt street/pobox
const quoteResult = await pool.query(` const quoteResult = await pool.query(`
SELECT q.*, c.name as customer_name, c.street, c.city, c.state, c.zip_code, c.account_number SELECT q.*, c.name as customer_name, c.line1, c.line2, c.line3, c.line4, c.city, c.state, c.zip_code, c.account_number
FROM quotes q FROM quotes q
LEFT JOIN customers c ON q.customer_id = c.id LEFT JOIN customers c ON q.customer_id = c.id
WHERE q.id = $1 WHERE q.id = $1
@ -663,29 +695,23 @@ app.get('/api/quotes/:id/pdf', async (req, res) => {
[id] [id]
); );
// Load template and replace placeholders
const templatePath = path.join(__dirname, 'templates', 'quote-template.html'); const templatePath = path.join(__dirname, 'templates', 'quote-template.html');
let html = await fs.readFile(templatePath, 'utf-8'); let html = await fs.readFile(templatePath, 'utf-8');
// Get logo
let logoHTML = ''; let logoHTML = '';
try { try {
const logoPath = path.join(__dirname, 'public', 'uploads', 'company-logo.png'); const logoPath = path.join(__dirname, 'public', 'uploads', 'company-logo.png');
const logoData = await fs.readFile(logoPath); const logoData = await fs.readFile(logoPath);
const logoBase64 = logoData.toString('base64'); const logoBase64 = logoData.toString('base64');
logoHTML = `<img src="data:image/png;base64,${logoBase64}" alt="Company Logo" class="logo logo-size">`; logoHTML = `<img src="data:image/png;base64,${logoBase64}" alt="Company Logo" class="logo logo-size">`;
} catch (err) { } catch (err) {}
// No logo
}
// Generate items HTML // Items HTML generieren
let itemsHTML = itemsResult.rows.map(item => { let itemsHTML = itemsResult.rows.map(item => {
let rateFormatted = item.rate; let rateFormatted = item.rate;
if (item.rate.toUpperCase() !== 'TBD' && !item.rate.includes('/')) { if (item.rate.toUpperCase() !== 'TBD' && !item.rate.includes('/')) {
const rateNum = parseFloat(item.rate.replace(/[^0-9.]/g, '')); const rateNum = parseFloat(item.rate.replace(/[^0-9.]/g, ''));
if (!isNaN(rateNum)) { if (!isNaN(rateNum)) rateFormatted = rateNum.toFixed(2);
rateFormatted = rateNum.toFixed(2);
}
} }
return ` return `
<tr> <tr>
@ -693,17 +719,15 @@ app.get('/api/quotes/:id/pdf', async (req, res) => {
<td class="description">${item.description}</td> <td class="description">${item.description}</td>
<td class="rate">${rateFormatted}</td> <td class="rate">${rateFormatted}</td>
<td class="amount">${item.amount}</td> <td class="amount">${item.amount}</td>
</tr> </tr>`;
`;
}).join(''); }).join('');
// Add totals // Totals
itemsHTML += ` itemsHTML += `
<tr class="footer-row"> <tr class="footer-row">
<td colspan="3" class="total-label">Subtotal:</td> <td colspan="3" class="total-label">Subtotal:</td>
<td class="total-amount">$${parseFloat(quote.subtotal).toFixed(2)}</td> <td class="total-amount">$${parseFloat(quote.subtotal).toFixed(2)}</td>
</tr>`; </tr>`;
if (!quote.tax_exempt) { if (!quote.tax_exempt) {
itemsHTML += ` itemsHTML += `
<tr class="footer-row"> <tr class="footer-row">
@ -711,7 +735,6 @@ app.get('/api/quotes/:id/pdf', async (req, res) => {
<td class="total-amount">$${parseFloat(quote.tax_amount).toFixed(2)}</td> <td class="total-amount">$${parseFloat(quote.tax_amount).toFixed(2)}</td>
</tr>`; </tr>`;
} }
itemsHTML += ` itemsHTML += `
<tr class="footer-row"> <tr class="footer-row">
<td colspan="3" class="total-label">TOTAL:</td> <td colspan="3" class="total-label">TOTAL:</td>
@ -720,17 +743,26 @@ app.get('/api/quotes/:id/pdf', async (req, res) => {
<tr class="footer-row"> <tr class="footer-row">
<td colspan="4" class="thank-you">Thank you for your business!</td> <td colspan="4" class="thank-you">Thank you for your business!</td>
</tr>`; </tr>`;
let tbdNote = quote.has_tbd ? '<p style="font-size: 12px; margin-top: 20px;"><em>* Note: This quote contains items marked as "TBD". The final total may vary.</em></p>' : '';
let tbdNote = ''; // --- ADRESS-LOGIK (NEU) ---
if (quote.has_tbd) { const addressLines = [];
tbdNote = '<p style="font-size: 12px; margin-top: 20px;"><em>* Note: This quote contains items marked as "TBD" (To Be Determined). The final total may vary once all details are finalized.</em></p>'; // Wenn line1 existiert UND ungleich dem Namen ist, hinzufügen. Sonst überspringen (da Name eh drüber steht).
if (quote.line1 && quote.line1.trim().toLowerCase() !== (quote.customer_name || '').trim().toLowerCase()) {
addressLines.push(quote.line1);
} }
if (quote.line2) addressLines.push(quote.line2);
if (quote.line3) addressLines.push(quote.line3);
if (quote.line4) addressLines.push(quote.line4);
// Replace placeholders const streetBlock = addressLines.join('<br>');
// Ersetzen
html = html html = html
.replace('{{LOGO_HTML}}', logoHTML) .replace('{{LOGO_HTML}}', logoHTML)
.replace('{{CUSTOMER_NAME}}', quote.customer_name || '') .replace('{{CUSTOMER_NAME}}', quote.customer_name || '')
.replace('{{CUSTOMER_STREET}}', quote.street || '') .replace('{{CUSTOMER_STREET}}', streetBlock) // Hier kommt der Block rein
.replace('{{CUSTOMER_CITY}}', quote.city || '') .replace('{{CUSTOMER_CITY}}', quote.city || '')
.replace('{{CUSTOMER_STATE}}', quote.state || '') .replace('{{CUSTOMER_STATE}}', quote.state || '')
.replace('{{CUSTOMER_ZIP}}', quote.zip_code || '') .replace('{{CUSTOMER_ZIP}}', quote.zip_code || '')
@ -739,21 +771,15 @@ app.get('/api/quotes/:id/pdf', async (req, res) => {
.replace('{{QUOTE_DATE}}', formatDate(quote.quote_date)) .replace('{{QUOTE_DATE}}', formatDate(quote.quote_date))
.replace('{{ITEMS}}', itemsHTML) .replace('{{ITEMS}}', itemsHTML)
.replace('{{TBD_NOTE}}', tbdNote); .replace('{{TBD_NOTE}}', tbdNote);
// Use persistent browser
const browserInstance = await initBrowser(); const browserInstance = await initBrowser();
const page = await browserInstance.newPage(); const page = await browserInstance.newPage();
await page.setContent(html, { waitUntil: 'networkidle0', timeout: 60000 }); await page.setContent(html, { waitUntil: 'networkidle0', timeout: 60000 });
const pdf = await page.pdf({ const pdf = await page.pdf({
format: 'Letter', format: 'Letter', printBackground: true,
printBackground: true, margin: { top: '0.5in', right: '0.5in', bottom: '0.5in', left: '0.5in' }
margin: { top: '0.5in', right: '0.5in', bottom: '0.5in', left: '0.5in' },
timeout: 60000
}); });
await page.close();
await page.close(); // Close page, not browser
res.set({ res.set({
'Content-Type': 'application/pdf', 'Content-Type': 'application/pdf',
@ -771,12 +797,12 @@ app.get('/api/quotes/:id/pdf', async (req, res) => {
app.get('/api/invoices/:id/pdf', async (req, res) => { app.get('/api/invoices/:id/pdf', async (req, res) => {
const { id } = req.params; const { id } = req.params;
console.log(`[INVOICE-PDF] Starting invoice PDF generation for ID: ${id}`); console.log(`[INVOICE-PDF] Starting invoice PDF generation for ID: ${id}`);
try { try {
// KORRIGIERT: Abfrage von line1-4
const invoiceResult = await pool.query(` const invoiceResult = await pool.query(`
SELECT i.*, c.name as customer_name, c.street, c.city, c.state, c.zip_code, c.account_number SELECT i.*, c.name as customer_name, c.line1, c.line2, c.line3, c.line4, c.city, c.state, c.zip_code, c.account_number
FROM invoices i FROM invoices i
LEFT JOIN customers c ON i.customer_id = c.id LEFT JOIN customers c ON i.customer_id = c.id
WHERE i.id = $1 WHERE i.id = $1
@ -792,29 +818,22 @@ app.get('/api/invoices/:id/pdf', async (req, res) => {
[id] [id]
); );
// Load template
const templatePath = path.join(__dirname, 'templates', 'invoice-template.html'); const templatePath = path.join(__dirname, 'templates', 'invoice-template.html');
let html = await fs.readFile(templatePath, 'utf-8'); let html = await fs.readFile(templatePath, 'utf-8');
// Get logo
let logoHTML = ''; let logoHTML = '';
try { try {
const logoPath = path.join(__dirname, 'public', 'uploads', 'company-logo.png'); const logoPath = path.join(__dirname, 'public', 'uploads', 'company-logo.png');
const logoData = await fs.readFile(logoPath); const logoData = await fs.readFile(logoPath);
const logoBase64 = logoData.toString('base64'); const logoBase64 = logoData.toString('base64');
logoHTML = `<img src="data:image/png;base64,${logoBase64}" alt="Company Logo" class="logo logo-size">`; logoHTML = `<img src="data:image/png;base64,${logoBase64}" alt="Company Logo" class="logo logo-size">`;
} catch (err) { } catch (err) {}
// No logo
}
// Generate items HTML
let itemsHTML = itemsResult.rows.map(item => { let itemsHTML = itemsResult.rows.map(item => {
let rateFormatted = item.rate; let rateFormatted = item.rate;
if (item.rate.toUpperCase() !== 'TBD' && !item.rate.includes('/')) { if (item.rate.toUpperCase() !== 'TBD' && !item.rate.includes('/')) {
const rateNum = parseFloat(item.rate.replace(/[^0-9.]/g, '')); const rateNum = parseFloat(item.rate.replace(/[^0-9.]/g, ''));
if (!isNaN(rateNum)) { if (!isNaN(rateNum)) rateFormatted = rateNum.toFixed(2);
rateFormatted = rateNum.toFixed(2);
}
} }
return ` return `
<tr> <tr>
@ -822,17 +841,14 @@ app.get('/api/invoices/:id/pdf', async (req, res) => {
<td class="description">${item.description}</td> <td class="description">${item.description}</td>
<td class="rate">${rateFormatted}</td> <td class="rate">${rateFormatted}</td>
<td class="amount">${item.amount}</td> <td class="amount">${item.amount}</td>
</tr> </tr>`;
`;
}).join(''); }).join('');
// Add totals
itemsHTML += ` itemsHTML += `
<tr class="footer-row"> <tr class="footer-row">
<td colspan="3" class="total-label">Subtotal:</td> <td colspan="3" class="total-label">Subtotal:</td>
<td class="total-amount">$${parseFloat(invoice.subtotal).toFixed(2)}</td> <td class="total-amount">$${parseFloat(invoice.subtotal).toFixed(2)}</td>
</tr>`; </tr>`;
if (!invoice.tax_exempt) { if (!invoice.tax_exempt) {
itemsHTML += ` itemsHTML += `
<tr class="footer-row"> <tr class="footer-row">
@ -840,7 +856,6 @@ app.get('/api/invoices/:id/pdf', async (req, res) => {
<td class="total-amount">$${parseFloat(invoice.tax_amount).toFixed(2)}</td> <td class="total-amount">$${parseFloat(invoice.tax_amount).toFixed(2)}</td>
</tr>`; </tr>`;
} }
itemsHTML += ` itemsHTML += `
<tr class="footer-row"> <tr class="footer-row">
<td colspan="3" class="total-label">TOTAL:</td> <td colspan="3" class="total-label">TOTAL:</td>
@ -849,16 +864,24 @@ app.get('/api/invoices/:id/pdf', async (req, res) => {
<tr class="footer-row"> <tr class="footer-row">
<td colspan="4" class="thank-you">Thank you for your business!</td> <td colspan="4" class="thank-you">Thank you for your business!</td>
</tr>`; </tr>`;
const authHTML = invoice.auth_code ? `<p style="margin-bottom: 20px; font-size: 13px;"><strong>Authorization:</strong> ${invoice.auth_code}</p>` : '';
// Authorization field // --- ADRESS-LOGIK (NEU) ---
const authHTML = invoice.auth_code ? const addressLines = [];
`<p style="margin-bottom: 20px; font-size: 13px;"><strong>Authorization:</strong> ${invoice.auth_code}</p>` : ''; if (invoice.line1 && invoice.line1.trim().toLowerCase() !== (invoice.customer_name || '').trim().toLowerCase()) {
addressLines.push(invoice.line1);
}
if (invoice.line2) addressLines.push(invoice.line2);
if (invoice.line3) addressLines.push(invoice.line3);
if (invoice.line4) addressLines.push(invoice.line4);
// Replace placeholders const streetBlock = addressLines.join('<br>');
html = html html = html
.replace('{{LOGO_HTML}}', logoHTML) .replace('{{LOGO_HTML}}', logoHTML)
.replace('{{CUSTOMER_NAME}}', invoice.customer_name || '') .replace('{{CUSTOMER_NAME}}', invoice.customer_name || '')
.replace('{{CUSTOMER_STREET}}', invoice.street || '') .replace('{{CUSTOMER_STREET}}', streetBlock)
.replace('{{CUSTOMER_CITY}}', invoice.city || '') .replace('{{CUSTOMER_CITY}}', invoice.city || '')
.replace('{{CUSTOMER_STATE}}', invoice.state || '') .replace('{{CUSTOMER_STATE}}', invoice.state || '')
.replace('{{CUSTOMER_ZIP}}', invoice.zip_code || '') .replace('{{CUSTOMER_ZIP}}', invoice.zip_code || '')
@ -868,21 +891,15 @@ app.get('/api/invoices/:id/pdf', async (req, res) => {
.replace('{{TERMS}}', invoice.terms) .replace('{{TERMS}}', invoice.terms)
.replace('{{AUTHORIZATION}}', authHTML) .replace('{{AUTHORIZATION}}', authHTML)
.replace('{{ITEMS}}', itemsHTML); .replace('{{ITEMS}}', itemsHTML);
// Use persistent browser
const browserInstance = await initBrowser(); const browserInstance = await initBrowser();
const page = await browserInstance.newPage(); const page = await browserInstance.newPage();
await page.setContent(html, { waitUntil: 'networkidle0', timeout: 60000 }); await page.setContent(html, { waitUntil: 'networkidle0', timeout: 60000 });
const pdf = await page.pdf({ const pdf = await page.pdf({
format: 'Letter', format: 'Letter', printBackground: true,
printBackground: true, margin: { top: '0.5in', right: '0.5in', bottom: '0.5in', left: '0.5in' }
margin: { top: '0.5in', right: '0.5in', bottom: '0.5in', left: '0.5in' },
timeout: 60000
}); });
await page.close();
await page.close(); // Close page, not browser
res.set({ res.set({
'Content-Type': 'application/pdf', 'Content-Type': 'application/pdf',
@ -904,8 +921,9 @@ app.get('/api/quotes/:id/html', async (req, res) => {
const { id } = req.params; const { id } = req.params;
try { try {
// KORREKTUR: Line 1-4 abfragen
const quoteResult = await pool.query(` const quoteResult = await pool.query(`
SELECT q.*, c.name as customer_name, c.street, c.city, c.state, c.zip_code, c.account_number SELECT q.*, c.name as customer_name, c.line1, c.line2, c.line3, c.line4, c.city, c.state, c.zip_code, c.account_number
FROM quotes q FROM quotes q
LEFT JOIN customers c ON q.customer_id = c.id LEFT JOIN customers c ON q.customer_id = c.id
WHERE q.id = $1 WHERE q.id = $1
@ -936,9 +954,7 @@ app.get('/api/quotes/:id/html', async (req, res) => {
let rateFormatted = item.rate; let rateFormatted = item.rate;
if (item.rate.toUpperCase() !== 'TBD' && !item.rate.includes('/')) { if (item.rate.toUpperCase() !== 'TBD' && !item.rate.includes('/')) {
const rateNum = parseFloat(item.rate.replace(/[^0-9.]/g, '')); const rateNum = parseFloat(item.rate.replace(/[^0-9.]/g, ''));
if (!isNaN(rateNum)) { if (!isNaN(rateNum)) rateFormatted = rateNum.toFixed(2);
rateFormatted = rateNum.toFixed(2);
}
} }
return ` return `
<tr> <tr>
@ -946,8 +962,7 @@ app.get('/api/quotes/:id/html', async (req, res) => {
<td class="description">${item.description}</td> <td class="description">${item.description}</td>
<td class="rate">${rateFormatted}</td> <td class="rate">${rateFormatted}</td>
<td class="amount">${item.amount}</td> <td class="amount">${item.amount}</td>
</tr> </tr>`;
`;
}).join(''); }).join('');
itemsHTML += ` itemsHTML += `
@ -955,7 +970,6 @@ app.get('/api/quotes/:id/html', async (req, res) => {
<td colspan="3" class="total-label">Subtotal:</td> <td colspan="3" class="total-label">Subtotal:</td>
<td class="total-amount">$${parseFloat(quote.subtotal).toFixed(2)}</td> <td class="total-amount">$${parseFloat(quote.subtotal).toFixed(2)}</td>
</tr>`; </tr>`;
if (!quote.tax_exempt) { if (!quote.tax_exempt) {
itemsHTML += ` itemsHTML += `
<tr class="footer-row"> <tr class="footer-row">
@ -963,7 +977,6 @@ app.get('/api/quotes/:id/html', async (req, res) => {
<td class="total-amount">$${parseFloat(quote.tax_amount).toFixed(2)}</td> <td class="total-amount">$${parseFloat(quote.tax_amount).toFixed(2)}</td>
</tr>`; </tr>`;
} }
itemsHTML += ` itemsHTML += `
<tr class="footer-row"> <tr class="footer-row">
<td colspan="3" class="total-label">TOTAL:</td> <td colspan="3" class="total-label">TOTAL:</td>
@ -972,16 +985,24 @@ app.get('/api/quotes/:id/html', async (req, res) => {
<tr class="footer-row"> <tr class="footer-row">
<td colspan="4" class="thank-you">Thank you for your business!</td> <td colspan="4" class="thank-you">Thank you for your business!</td>
</tr>`; </tr>`;
let tbdNote = quote.has_tbd ? '<p style="font-size: 12px; margin-top: 20px;"><em>* Note: This quote contains items marked as "TBD". The final total may vary.</em></p>' : '';
let tbdNote = ''; // --- ADRESS LOGIK ---
if (quote.has_tbd) { const addressLines = [];
tbdNote = '<p style="font-size: 12px; margin-top: 20px;"><em>* Note: This quote contains items marked as "TBD" (To Be Determined). The final total may vary once all details are finalized.</em></p>'; if (quote.line1 && quote.line1.trim().toLowerCase() !== (quote.customer_name || '').trim().toLowerCase()) {
addressLines.push(quote.line1);
} }
if (quote.line2) addressLines.push(quote.line2);
if (quote.line3) addressLines.push(quote.line3);
if (quote.line4) addressLines.push(quote.line4);
const streetBlock = addressLines.join('<br>');
html = html html = html
.replace('{{LOGO_HTML}}', logoHTML) .replace('{{LOGO_HTML}}', logoHTML)
.replace('{{CUSTOMER_NAME}}', quote.customer_name || '') .replace('{{CUSTOMER_NAME}}', quote.customer_name || '')
.replace('{{CUSTOMER_STREET}}', quote.street || '') .replace('{{CUSTOMER_STREET}}', streetBlock)
.replace('{{CUSTOMER_CITY}}', quote.city || '') .replace('{{CUSTOMER_CITY}}', quote.city || '')
.replace('{{CUSTOMER_STATE}}', quote.state || '') .replace('{{CUSTOMER_STATE}}', quote.state || '')
.replace('{{CUSTOMER_ZIP}}', quote.zip_code || '') .replace('{{CUSTOMER_ZIP}}', quote.zip_code || '')
@ -990,7 +1011,7 @@ app.get('/api/quotes/:id/html', async (req, res) => {
.replace('{{QUOTE_DATE}}', formatDate(quote.quote_date)) .replace('{{QUOTE_DATE}}', formatDate(quote.quote_date))
.replace('{{ITEMS}}', itemsHTML) .replace('{{ITEMS}}', itemsHTML)
.replace('{{TBD_NOTE}}', tbdNote); .replace('{{TBD_NOTE}}', tbdNote);
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
res.send(html); res.send(html);
@ -1004,8 +1025,9 @@ app.get('/api/invoices/:id/html', async (req, res) => {
const { id } = req.params; const { id } = req.params;
try { try {
// KORREKTUR: Line 1-4 abfragen
const invoiceResult = await pool.query(` const invoiceResult = await pool.query(`
SELECT i.*, c.name as customer_name, c.street, c.city, c.state, c.zip_code, c.account_number SELECT i.*, c.name as customer_name, c.line1, c.line2, c.line3, c.line4, c.city, c.state, c.zip_code, c.account_number
FROM invoices i FROM invoices i
LEFT JOIN customers c ON i.customer_id = c.id LEFT JOIN customers c ON i.customer_id = c.id
WHERE i.id = $1 WHERE i.id = $1
@ -1036,9 +1058,7 @@ app.get('/api/invoices/:id/html', async (req, res) => {
let rateFormatted = item.rate; let rateFormatted = item.rate;
if (item.rate.toUpperCase() !== 'TBD' && !item.rate.includes('/')) { if (item.rate.toUpperCase() !== 'TBD' && !item.rate.includes('/')) {
const rateNum = parseFloat(item.rate.replace(/[^0-9.]/g, '')); const rateNum = parseFloat(item.rate.replace(/[^0-9.]/g, ''));
if (!isNaN(rateNum)) { if (!isNaN(rateNum)) rateFormatted = rateNum.toFixed(2);
rateFormatted = rateNum.toFixed(2);
}
} }
return ` return `
<tr> <tr>
@ -1046,8 +1066,7 @@ app.get('/api/invoices/:id/html', async (req, res) => {
<td class="description">${item.description}</td> <td class="description">${item.description}</td>
<td class="rate">${rateFormatted}</td> <td class="rate">${rateFormatted}</td>
<td class="amount">${item.amount}</td> <td class="amount">${item.amount}</td>
</tr> </tr>`;
`;
}).join(''); }).join('');
itemsHTML += ` itemsHTML += `
@ -1055,7 +1074,6 @@ app.get('/api/invoices/:id/html', async (req, res) => {
<td colspan="3" class="total-label">Subtotal:</td> <td colspan="3" class="total-label">Subtotal:</td>
<td class="total-amount">$${parseFloat(invoice.subtotal).toFixed(2)}</td> <td class="total-amount">$${parseFloat(invoice.subtotal).toFixed(2)}</td>
</tr>`; </tr>`;
if (!invoice.tax_exempt) { if (!invoice.tax_exempt) {
itemsHTML += ` itemsHTML += `
<tr class="footer-row"> <tr class="footer-row">
@ -1063,7 +1081,6 @@ app.get('/api/invoices/:id/html', async (req, res) => {
<td class="total-amount">$${parseFloat(invoice.tax_amount).toFixed(2)}</td> <td class="total-amount">$${parseFloat(invoice.tax_amount).toFixed(2)}</td>
</tr>`; </tr>`;
} }
itemsHTML += ` itemsHTML += `
<tr class="footer-row"> <tr class="footer-row">
<td colspan="3" class="total-label">TOTAL:</td> <td colspan="3" class="total-label">TOTAL:</td>
@ -1072,14 +1089,24 @@ app.get('/api/invoices/:id/html', async (req, res) => {
<tr class="footer-row"> <tr class="footer-row">
<td colspan="4" class="thank-you">Thank you for your business!</td> <td colspan="4" class="thank-you">Thank you for your business!</td>
</tr>`; </tr>`;
const authHTML = invoice.auth_code ? `<p style="margin-bottom: 20px; font-size: 13px;"><strong>Authorization:</strong> ${invoice.auth_code}</p>` : '';
const authHTML = invoice.auth_code ? // --- ADRESS LOGIK ---
`<p style="margin-bottom: 20px; font-size: 13px;"><strong>Authorization:</strong> ${invoice.auth_code}</p>` : ''; const addressLines = [];
if (invoice.line1 && invoice.line1.trim().toLowerCase() !== (invoice.customer_name || '').trim().toLowerCase()) {
addressLines.push(invoice.line1);
}
if (invoice.line2) addressLines.push(invoice.line2);
if (invoice.line3) addressLines.push(invoice.line3);
if (invoice.line4) addressLines.push(invoice.line4);
const streetBlock = addressLines.join('<br>');
html = html html = html
.replace('{{LOGO_HTML}}', logoHTML) .replace('{{LOGO_HTML}}', logoHTML)
.replace('{{CUSTOMER_NAME}}', invoice.customer_name || '') .replace('{{CUSTOMER_NAME}}', invoice.customer_name || '')
.replace('{{CUSTOMER_STREET}}', invoice.street || '') .replace('{{CUSTOMER_STREET}}', streetBlock)
.replace('{{CUSTOMER_CITY}}', invoice.city || '') .replace('{{CUSTOMER_CITY}}', invoice.city || '')
.replace('{{CUSTOMER_STATE}}', invoice.state || '') .replace('{{CUSTOMER_STATE}}', invoice.state || '')
.replace('{{CUSTOMER_ZIP}}', invoice.zip_code || '') .replace('{{CUSTOMER_ZIP}}', invoice.zip_code || '')
@ -1089,7 +1116,7 @@ app.get('/api/invoices/:id/html', async (req, res) => {
.replace('{{TERMS}}', invoice.terms) .replace('{{TERMS}}', invoice.terms)
.replace('{{AUTHORIZATION}}', authHTML) .replace('{{AUTHORIZATION}}', authHTML)
.replace('{{ITEMS}}', itemsHTML); .replace('{{ITEMS}}', itemsHTML);
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
res.send(html); res.send(html);
@ -1099,6 +1126,108 @@ app.get('/api/invoices/:id/html', async (req, res) => {
} }
}); });
// QBO Export Endpoint
app.post('/api/invoices/:id/export', async (req, res) => {
const { id } = req.params;
const client = await pool.connect();
// HIER SIND DEINE FESTEN IDs
const QBO_LABOR_ID = '5'; // Labor:Labor
const QBO_PARTS_ID = '9'; // Parts:Parts
try {
// 1. Lokale Rechnung laden
const invoiceRes = await client.query(`
SELECT i.*, c.qbo_id as customer_qbo_id, c.name as customer_name, c.email
FROM invoices i
LEFT JOIN customers c ON i.customer_id = c.id
WHERE i.id = $1
`, [id]);
if (invoiceRes.rows.length === 0) return res.status(404).json({ error: 'Invoice not found' });
const invoice = invoiceRes.rows[0];
if (!invoice.customer_qbo_id) {
return res.status(400).json({ error: `Kunde "${invoice.customer_name}" ist noch nicht mit QBO verknüpft.` });
}
// 2. Items laden (inkl. qbo_item_id)
const itemsRes = await client.query('SELECT * FROM invoice_items WHERE invoice_id = $1', [id]);
const items = itemsRes.rows;
// 3. QBO Client
const oauthClient = getOAuthClient();
const companyId = oauthClient.getToken().realmId;
const baseUrl = process.env.QBO_ENVIRONMENT === 'production'
? 'https://quickbooks.api.intuit.com'
: 'https://sandbox-quickbooks.api.intuit.com';
// 4. QBO JSON bauen (OHNE "select * from Item...")
const lineItems = items.map(item => {
const rate = parseFloat(item.rate.replace(/[^0-9.]/g, '')) || 0;
const amount = parseFloat(item.amount.replace(/[^0-9.]/g, '')) || 0;
// WICHTIG: Hier nutzen wir die ID aus der Datenbank oder den Fallback
const itemRefId = item.qbo_item_id || QBO_PARTS_ID;
const itemRefName = itemRefId == QBO_LABOR_ID ? "Labor:Labor" : "Parts:Parts";
return {
"DetailType": "SalesItemLineDetail",
"Amount": amount,
"Description": item.description,
"SalesItemLineDetail": {
"ItemRef": {
"value": itemRefId,
"name": itemRefName
},
"UnitPrice": rate,
"Qty": parseFloat(item.quantity) || 1
}
};
});
const qboInvoicePayload = {
"CustomerRef": { "value": invoice.customer_qbo_id },
"DocNumber": invoice.invoice_number,
"TxnDate": invoice.invoice_date.toISOString().split('T')[0],
"Line": lineItems,
"CustomerMemo": { "value": invoice.auth_code ? `Auth: ${invoice.auth_code}` : "" }
};
console.log(`📤 Sende Rechnung ${invoice.invoice_number} an QBO...`);
const createResponse = await makeQboApiCall({
url: `${baseUrl}/v3/company/${companyId}/invoice`,
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(qboInvoicePayload)
});
const qboInvoice = createResponse.getJson ? createResponse.getJson() : createResponse.json;
onsole.log("🔍 FULL QBO RESPONSE:", JSON.stringify(qboInvoice, null, 2));
if (!qboInvoice.Id) {
throw new Error("QBO hat keine ID zurückgegeben. Wahrscheinlich ein Fehler im Request (siehe Logs).");
}
console.log(`✅ QBO Rechnung erstellt! ID: ${qboInvoice.Id}`);
await client.query(
`UPDATE invoices SET qbo_id = $1, qbo_sync_token = $2, qbo_doc_number = $3 WHERE id = $4`,
[qboInvoice.Id, qboInvoice.SyncToken, qboInvoice.DocNumber, id]
);
res.json({ success: true, qbo_id: qboInvoice.Id });
} catch (error) {
console.error("QBO Export Error:", error);
let errorDetails = error.message;
if (error.response?.data?.Fault?.Error?.[0]) {
errorDetails = error.response.data.Fault.Error[0].Message + ": " + error.response.data.Fault.Error[0].Detail;
}
res.status(500).json({ error: "QBO Export failed: " + errorDetails });
} finally {
client.release();
}
});
// Start server and browser // Start server and browser
async function startServer() { async function startServer() {