Files
tilbudgivern/create_ordrestyring_tables.sql
T
alex 10473e8a32 feat: Add complete data import and incremental update scripts for Ordrestyring
- Implemented setup_ordrestyring_import.sh for initial database setup and data import from API.
- Created sync_ordrestyring_data.sh for incremental updates to the local database.
- Developed test_ordrestyring_import.js to validate API data import functionality.
- Added test_enhanced_quote_demo.js to demonstrate the enhanced quote system using hybrid data sources.
- Introduced test_ordrestyring_system.sh for comprehensive testing of the database system and API functionality.
- Enhanced logging and error handling across scripts for better traceability.
- Included analysis script creation for post-import data analysis.
2025-09-19 15:06:03 +02:00

330 lines
9.6 KiB
SQL

-- Ordrestyring API Data Import Tables
-- Create tables to store all Ordrestyring data locally
-- Cases (Primary resource)
CREATE TABLE IF NOT EXISTS ordrestyring_cases (
id INT AUTO_INCREMENT PRIMARY KEY,
case_number VARCHAR(50) UNIQUE,
yourref VARCHAR(250),
requestor VARCHAR(250),
creation_date INT,
description TEXT,
main_technician INT,
status BIGINT,
made_by INT,
offer_number INT,
additional_technicians VARCHAR(600),
remarks TEXT,
work_done TEXT,
delivery_address INT,
contact VARCHAR(100),
case_type INT,
department INT,
no_materials BOOLEAN,
sub_number INT,
upper_case_number INT,
customer_number VARCHAR(50),
updated_at INT,
created_at INT,
-- Meta information
imported_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
api_version VARCHAR(20) DEFAULT 'v2',
INDEX idx_case_number (case_number),
INDEX idx_customer_number (customer_number),
INDEX idx_creation_date (creation_date),
INDEX idx_case_type (case_type),
INDEX idx_status (status)
);
-- Case Materials
CREATE TABLE IF NOT EXISTS ordrestyring_case_materials (
id BIGINT PRIMARY KEY,
case_number VARCHAR(50),
added_type VARCHAR(50),
quantity DECIMAL(18,8),
ean VARCHAR(250),
product_number VARCHAR(250),
product_text TEXT,
supplier TEXT,
discount DECIMAL(19,8),
cost_price DECIMAL(19,8),
list_price DECIMAL(19,8),
sales_price DECIMAL(19,8),
created_by INT,
edi_invoice_number TEXT,
updated_at INT,
created_at INT,
-- Meta information
imported_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_case_number (case_number),
INDEX idx_product_number (product_number(50)),
INDEX idx_supplier (supplier(100)),
FOREIGN KEY (case_number) REFERENCES ordrestyring_cases(case_number) ON DELETE CASCADE
);
-- Hours worked on cases
CREATE TABLE IF NOT EXISTS ordrestyring_hours (
id INT PRIMARY KEY,
case_id VARCHAR(50),
emp_id INT,
hour_type INT,
start_time INT,
stop_time INT,
remark TEXT,
costprice INT,
exported BOOLEAN,
approval_status INT,
customer_internal_number VARCHAR(250),
updated_at INT,
created_at INT,
-- Meta information
imported_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_case_id (case_id),
INDEX idx_emp_id (emp_id),
INDEX idx_start_time (start_time),
INDEX idx_hour_type (hour_type)
);
-- Debtors (Customers)
CREATE TABLE IF NOT EXISTS ordrestyring_debtors (
customer_number VARCHAR(50) PRIMARY KEY,
customer_name VARCHAR(250),
customer_attention VARCHAR(250),
customer_address VARCHAR(250),
customer_postalcode VARCHAR(50),
customer_city VARCHAR(250),
customer_telephone VARCHAR(50),
customer_email VARCHAR(250),
customer_mobile VARCHAR(50),
invoice_name VARCHAR(250),
invoice_address VARCHAR(250),
invoice_postalcode VARCHAR(50),
invoice_city VARCHAR(250),
invoice_telephone VARCHAR(50),
invoice_email VARCHAR(250),
invoice_mobile VARCHAR(50),
cvr VARCHAR(50),
ean VARCHAR(50),
customer_remarks TEXT,
invoice_attention TEXT,
avance_type INT,
is_blocked BOOLEAN,
due_dates INT,
status INT,
currency_id INT,
updated_at INT,
created_at INT,
-- Meta information
imported_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_customer_name (customer_name),
INDEX idx_customer_postalcode (customer_postalcode),
INDEX idx_customer_city (customer_city),
INDEX idx_cvr (cvr)
);
-- Case Types
CREATE TABLE IF NOT EXISTS ordrestyring_case_types (
id INT PRIMARY KEY,
text TEXT,
site VARCHAR(50),
colorcode VARCHAR(50),
show_in_app BOOLEAN,
show_in_app_status_change BOOLEAN,
-- Meta information
imported_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
-- Case Statuses
CREATE TABLE IF NOT EXISTS ordrestyring_case_statuses (
id INT PRIMARY KEY,
text TEXT,
site VARCHAR(50),
colorcode VARCHAR(50),
show_in_app BOOLEAN,
show_in_app_status_change BOOLEAN,
-- Meta information
imported_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
-- Users (Technicians/Employees)
CREATE TABLE IF NOT EXISTS ordrestyring_users (
id INT PRIMARY KEY,
init VARCHAR(50),
first_name VARCHAR(50),
last_name VARCHAR(50),
fullName VARCHAR(100),
email VARCHAR(50),
tlf VARCHAR(20),
mobil_privat VARCHAR(50),
status INT,
department_id INT,
employee_type_id INT,
-- Meta information
imported_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_email (email),
INDEX idx_status (status),
INDEX idx_department_id (department_id)
);
-- Employee Types
CREATE TABLE IF NOT EXISTS ordrestyring_employee_types (
id INT PRIMARY KEY,
title VARCHAR(50),
internal_number VARCHAR(250),
is_personal BOOLEAN,
color VARCHAR(10),
salary_handle VARCHAR(255),
sort_order INT,
updated_at INT,
created_at INT,
-- Meta information
imported_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
-- Departments
CREATE TABLE IF NOT EXISTS ordrestyring_departments (
id INT PRIMARY KEY,
name VARCHAR(300),
number VARCHAR(50) UNIQUE,
-- Meta information
imported_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_number (number)
);
-- Debtor Invoices
CREATE TABLE IF NOT EXISTS ordrestyring_debtor_invoices (
invoice_number INT PRIMARY KEY,
type INT,
customer_number VARCHAR(50),
case_number VARCHAR(50),
department INT,
currency_code VARCHAR(10),
currency_multiplier DECIMAL(10,8),
invoice_name VARCHAR(250),
invoice_address VARCHAR(250),
invoice_postalcode VARCHAR(250),
invoice_city VARCHAR(250),
cust_name VARCHAR(250),
cust_address VARCHAR(250),
cust_postalcode VARCHAR(50),
cust_city VARCHAR(250),
del_name VARCHAR(250),
del_address VARCHAR(250),
del_city VARCHAR(250),
del_postalcode VARCHAR(50),
text TEXT,
date INT,
payment_date INT,
ref VARCHAR(250),
rek VARCHAR(250),
made_by INT,
amount DECIMAL(19,8),
vat DECIMAL(19,8),
amount_vat DECIMAL(19,8),
fi_number VARCHAR(50),
header TEXT,
status INT,
del_att VARCHAR(250),
cust_att VARCHAR(250),
invoice_att VARCHAR(250),
payed_date INT,
payed_amount DECIMAL(19,8),
updated_at INT,
created_at INT,
cust_cvr VARCHAR(100),
use_skattered BOOLEAN,
-- Meta information
imported_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_customer_number (customer_number),
INDEX idx_case_number (case_number),
INDEX idx_date (date),
INDEX idx_status (status),
FOREIGN KEY (customer_number) REFERENCES ordrestyring_debtors(customer_number) ON DELETE SET NULL,
FOREIGN KEY (case_number) REFERENCES ordrestyring_cases(case_number) ON DELETE SET NULL
);
-- Import Log
CREATE TABLE IF NOT EXISTS ordrestyring_import_log (
id INT AUTO_INCREMENT PRIMARY KEY,
import_type VARCHAR(50),
start_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
end_time TIMESTAMP NULL,
records_imported INT DEFAULT 0,
records_updated INT DEFAULT 0,
records_failed INT DEFAULT 0,
status ENUM('running', 'completed', 'failed') DEFAULT 'running',
error_message TEXT,
api_endpoint VARCHAR(200),
INDEX idx_import_type (import_type),
INDEX idx_start_time (start_time),
INDEX idx_status (status)
);
-- Quick Analysis Views
CREATE OR REPLACE VIEW ordrestyring_cases_with_details AS
SELECT
c.*,
d.customer_name,
d.customer_email,
d.customer_city,
ct.text as case_type_name,
cs.text as case_status_name,
cs.colorcode as status_color,
FROM_UNIXTIME(c.creation_date) as creation_datetime,
FROM_UNIXTIME(c.updated_at) as updated_datetime
FROM ordrestyring_cases c
LEFT JOIN ordrestyring_debtors d ON c.customer_number = d.customer_number
LEFT JOIN ordrestyring_case_types ct ON c.case_type = ct.id
LEFT JOIN ordrestyring_case_statuses cs ON c.status = cs.id;
CREATE OR REPLACE VIEW ordrestyring_case_financials AS
SELECT
c.case_number,
c.customer_number,
d.customer_name,
FROM_UNIXTIME(c.creation_date) as creation_date,
-- Material costs
COALESCE(SUM(m.quantity * m.sales_price), 0) as total_material_sales,
COALESCE(SUM(m.quantity * m.cost_price), 0) as total_material_costs,
COUNT(DISTINCT m.id) as material_line_count,
-- Hours worked
COALESCE(SUM((h.stop_time - h.start_time)/3600), 0) as total_hours,
COALESCE(SUM(h.costprice/100), 0) as total_labor_cost,
COUNT(DISTINCT h.id) as hour_entries_count
FROM ordrestyring_cases c
LEFT JOIN ordrestyring_debtors d ON c.customer_number = d.customer_number
LEFT JOIN ordrestyring_case_materials m ON c.case_number = m.case_number
LEFT JOIN ordrestyring_hours h ON c.case_number = h.case_id
GROUP BY c.case_number, c.customer_number, d.customer_name, c.creation_date;