- New clients table with PF/PJ support, fiscal data (CUI, IBAN, eFactura fields) - Full CRUD API for clients with search, sync integration - Order lifecycle: edit header (DRAFT), devalidate (VALIDAT→DRAFT), delete order/invoice - Invoice types: FACTURA (B2B) vs BON_FISCAL (B2C) with different nr formats - OrderCreateView redesigned as multi-step flow (client→vehicle→details) - Autocomplete from catalog_norme/catalog_preturi in OrderLineForm - Dashboard now combines stats + full orders table with filter tabs and search - ClientPicker and VehiclePicker with inline creation capability - Frontend schema aligned with backend (missing columns causing sync errors) - Mobile responsive fixes for OrderDetailView buttons Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
296 lines
12 KiB
JavaScript
296 lines
12 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { execSQL, notifyTableChanged } from '../db/database.js'
|
|
import { syncEngine } from '../db/sync.js'
|
|
import { useAuthStore } from './auth.js'
|
|
|
|
export const useOrdersStore = defineStore('orders', () => {
|
|
const auth = useAuthStore()
|
|
|
|
async function getAll(statusFilter = null, search = '') {
|
|
let sql = `SELECT * FROM orders WHERE tenant_id = ?`
|
|
const params = [auth.tenantId]
|
|
if (statusFilter) {
|
|
sql += ` AND status = ?`
|
|
params.push(statusFilter)
|
|
}
|
|
if (search && search.length >= 2) {
|
|
sql += ` AND (nr_auto LIKE ? OR client_nume LIKE ? OR nr_comanda LIKE ?)`
|
|
const like = `%${search}%`
|
|
params.push(like, like, like)
|
|
}
|
|
sql += ` ORDER BY created_at DESC`
|
|
return execSQL(sql, params)
|
|
}
|
|
|
|
async function getById(id) {
|
|
const rows = await execSQL(`SELECT * FROM orders WHERE id = ?`, [id])
|
|
return rows[0] || null
|
|
}
|
|
|
|
async function getLines(orderId) {
|
|
return execSQL(
|
|
`SELECT * FROM order_lines WHERE order_id = ? ORDER BY ordine, created_at`,
|
|
[orderId]
|
|
)
|
|
}
|
|
|
|
async function getRecentOrders(limit = 5) {
|
|
return execSQL(
|
|
`SELECT * FROM orders WHERE tenant_id = ? ORDER BY created_at DESC LIMIT ?`,
|
|
[auth.tenantId, limit]
|
|
)
|
|
}
|
|
|
|
async function create(data) {
|
|
const id = crypto.randomUUID()
|
|
const now = new Date().toISOString()
|
|
const nr = `CMD-${Date.now().toString(36).toUpperCase()}`
|
|
|
|
// Lookup client info for denormalized fields
|
|
let clientNume = '', clientTelefon = ''
|
|
if (data.client_id) {
|
|
const [c] = await execSQL(`SELECT * FROM clients WHERE id = ?`, [data.client_id])
|
|
if (c) {
|
|
clientNume = c.tip_persoana === 'PJ' ? (c.denumire || '') : [c.nume, c.prenume].filter(Boolean).join(' ')
|
|
clientTelefon = c.telefon || ''
|
|
}
|
|
}
|
|
|
|
// Lookup vehicle info for denormalized fields
|
|
let nrAuto = '', marcaDenumire = '', modelDenumire = ''
|
|
if (data.vehicle_id) {
|
|
const [v] = await execSQL(`SELECT * FROM vehicles WHERE id = ?`, [data.vehicle_id])
|
|
if (v) {
|
|
if (!clientNume) clientNume = v.client_nume || ''
|
|
if (!clientTelefon) clientTelefon = v.client_telefon || ''
|
|
nrAuto = v.nr_inmatriculare || ''
|
|
const [marca] = await execSQL(`SELECT denumire FROM catalog_marci WHERE id = ?`, [v.marca_id])
|
|
const [model] = await execSQL(`SELECT denumire FROM catalog_modele WHERE id = ?`, [v.model_id])
|
|
marcaDenumire = marca?.denumire || ''
|
|
modelDenumire = model?.denumire || ''
|
|
}
|
|
}
|
|
|
|
await execSQL(
|
|
`INSERT INTO orders (id, tenant_id, nr_comanda, data_comanda, vehicle_id, client_id, tip_deviz_id, status, km_intrare, observatii, client_nume, client_telefon, nr_auto, marca_denumire, model_denumire, created_at, updated_at)
|
|
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
|
|
[id, auth.tenantId, nr, now, data.vehicle_id || null, data.client_id || null,
|
|
data.tip_deviz_id || null,
|
|
'DRAFT', data.km_intrare || 0, data.observatii || '',
|
|
clientNume, clientTelefon, nrAuto, marcaDenumire, modelDenumire, now, now]
|
|
)
|
|
notifyTableChanged('orders')
|
|
await syncEngine.addToQueue('orders', id, 'INSERT', {
|
|
id, tenant_id: auth.tenantId, nr_comanda: nr, data_comanda: now,
|
|
vehicle_id: data.vehicle_id, client_id: data.client_id,
|
|
tip_deviz_id: data.tip_deviz_id,
|
|
status: 'DRAFT', km_intrare: data.km_intrare || 0, observatii: data.observatii || '',
|
|
client_nume: clientNume, client_telefon: clientTelefon, nr_auto: nrAuto,
|
|
marca_denumire: marcaDenumire, model_denumire: modelDenumire
|
|
})
|
|
return id
|
|
}
|
|
|
|
async function addLine(orderId, line) {
|
|
const id = crypto.randomUUID()
|
|
const now = new Date().toISOString()
|
|
const total = line.tip === 'manopera'
|
|
? (line.ore || 0) * (line.pret_ora || 0)
|
|
: (line.cantitate || 0) * (line.pret_unitar || 0)
|
|
|
|
const maxOrdine = await execSQL(
|
|
`SELECT MAX(ordine) as mx FROM order_lines WHERE order_id = ?`, [orderId]
|
|
)
|
|
const ordine = (maxOrdine[0]?.mx || 0) + 1
|
|
|
|
await execSQL(
|
|
`INSERT INTO order_lines (id, order_id, tenant_id, tip, descriere, norma_id, ore, pret_ora, um, cantitate, pret_unitar, total, mecanic_id, ordine, created_at, updated_at)
|
|
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
|
|
[id, orderId, auth.tenantId, line.tip, line.descriere || '',
|
|
line.norma_id || null, line.ore || 0, line.pret_ora || 0,
|
|
line.um || 'buc', line.cantitate || 0, line.pret_unitar || 0,
|
|
total, line.mecanic_id || null, ordine, now, now]
|
|
)
|
|
notifyTableChanged('order_lines')
|
|
|
|
// Recalculate order totals
|
|
await recalcTotals(orderId)
|
|
|
|
await syncEngine.addToQueue('order_lines', id, 'INSERT', {
|
|
id, order_id: orderId, tenant_id: auth.tenantId,
|
|
tip: line.tip, descriere: line.descriere || '',
|
|
ore: line.ore || 0, pret_ora: line.pret_ora || 0,
|
|
um: line.um || 'buc', cantitate: line.cantitate || 0,
|
|
pret_unitar: line.pret_unitar || 0, total
|
|
})
|
|
return id
|
|
}
|
|
|
|
async function removeLine(lineId, orderId) {
|
|
await execSQL(`DELETE FROM order_lines WHERE id = ?`, [lineId])
|
|
notifyTableChanged('order_lines')
|
|
await recalcTotals(orderId)
|
|
await syncEngine.addToQueue('order_lines', lineId, 'DELETE', {})
|
|
}
|
|
|
|
async function recalcTotals(orderId) {
|
|
const [man] = await execSQL(
|
|
`SELECT COALESCE(SUM(total), 0) as s FROM order_lines WHERE order_id = ? AND tip = 'manopera'`, [orderId]
|
|
)
|
|
const [mat] = await execSQL(
|
|
`SELECT COALESCE(SUM(total), 0) as s FROM order_lines WHERE order_id = ? AND tip = 'material'`, [orderId]
|
|
)
|
|
const totalManopera = man?.s || 0
|
|
const totalMateriale = mat?.s || 0
|
|
const totalGeneral = totalManopera + totalMateriale
|
|
const now = new Date().toISOString()
|
|
|
|
await execSQL(
|
|
`UPDATE orders SET total_manopera=?, total_materiale=?, total_general=?, updated_at=? WHERE id=?`,
|
|
[totalManopera, totalMateriale, totalGeneral, now, orderId]
|
|
)
|
|
notifyTableChanged('orders')
|
|
await syncEngine.addToQueue('orders', orderId, 'UPDATE', {
|
|
total_manopera: totalManopera, total_materiale: totalMateriale, total_general: totalGeneral
|
|
})
|
|
}
|
|
|
|
async function updateHeader(orderId, data) {
|
|
const now = new Date().toISOString()
|
|
|
|
// Re-denormalize client and vehicle info if IDs changed
|
|
let clientNume = data.client_nume, clientTelefon = data.client_telefon
|
|
let nrAuto = data.nr_auto, marcaDenumire = data.marca_denumire, modelDenumire = data.model_denumire
|
|
|
|
if (data.client_id) {
|
|
const [c] = await execSQL(`SELECT * FROM clients WHERE id = ?`, [data.client_id])
|
|
if (c) {
|
|
clientNume = c.tip_persoana === 'PJ' ? (c.denumire || '') : [c.nume, c.prenume].filter(Boolean).join(' ')
|
|
clientTelefon = c.telefon || ''
|
|
}
|
|
}
|
|
if (data.vehicle_id) {
|
|
const [v] = await execSQL(`SELECT * FROM vehicles WHERE id = ?`, [data.vehicle_id])
|
|
if (v) {
|
|
nrAuto = v.nr_inmatriculare || ''
|
|
const [marca] = await execSQL(`SELECT denumire FROM catalog_marci WHERE id = ?`, [v.marca_id])
|
|
const [model] = await execSQL(`SELECT denumire FROM catalog_modele WHERE id = ?`, [v.model_id])
|
|
marcaDenumire = marca?.denumire || ''
|
|
modelDenumire = model?.denumire || ''
|
|
}
|
|
}
|
|
|
|
await execSQL(
|
|
`UPDATE orders SET client_id=?, vehicle_id=?, tip_deviz_id=?, km_intrare=?, observatii=?,
|
|
client_nume=?, client_telefon=?, nr_auto=?, marca_denumire=?, model_denumire=?, updated_at=?
|
|
WHERE id=?`,
|
|
[data.client_id || null, data.vehicle_id || null, data.tip_deviz_id || null,
|
|
data.km_intrare || 0, data.observatii || '',
|
|
clientNume || '', clientTelefon || '', nrAuto || '', marcaDenumire || '', modelDenumire || '',
|
|
now, orderId]
|
|
)
|
|
notifyTableChanged('orders')
|
|
await syncEngine.addToQueue('orders', orderId, 'UPDATE', {
|
|
client_id: data.client_id, vehicle_id: data.vehicle_id, tip_deviz_id: data.tip_deviz_id,
|
|
km_intrare: data.km_intrare, observatii: data.observatii,
|
|
client_nume: clientNume, client_telefon: clientTelefon, nr_auto: nrAuto,
|
|
marca_denumire: marcaDenumire, model_denumire: modelDenumire
|
|
})
|
|
}
|
|
|
|
async function validateOrder(orderId) {
|
|
const now = new Date().toISOString()
|
|
await execSQL(`UPDATE orders SET status='VALIDAT', updated_at=? WHERE id=?`, [now, orderId])
|
|
notifyTableChanged('orders')
|
|
await syncEngine.addToQueue('orders', orderId, 'UPDATE', { status: 'VALIDAT' })
|
|
}
|
|
|
|
async function devalidateOrder(orderId) {
|
|
const now = new Date().toISOString()
|
|
await execSQL(`UPDATE orders SET status='DRAFT', updated_at=? WHERE id=?`, [now, orderId])
|
|
notifyTableChanged('orders')
|
|
await syncEngine.addToQueue('orders', orderId, 'UPDATE', { status: 'DRAFT' })
|
|
}
|
|
|
|
async function deleteOrder(orderId) {
|
|
await execSQL(`DELETE FROM order_lines WHERE order_id = ?`, [orderId])
|
|
await execSQL(`DELETE FROM orders WHERE id = ?`, [orderId])
|
|
notifyTableChanged('order_lines')
|
|
notifyTableChanged('orders')
|
|
await syncEngine.addToQueue('orders', orderId, 'DELETE', {})
|
|
}
|
|
|
|
async function deleteInvoice(invoiceId) {
|
|
const [inv] = await execSQL(`SELECT * FROM invoices WHERE id = ?`, [invoiceId])
|
|
if (!inv) return
|
|
// Set order back to VALIDAT
|
|
if (inv.order_id) {
|
|
const now = new Date().toISOString()
|
|
await execSQL(`UPDATE orders SET status='VALIDAT', updated_at=? WHERE id=?`, [now, inv.order_id])
|
|
notifyTableChanged('orders')
|
|
await syncEngine.addToQueue('orders', inv.order_id, 'UPDATE', { status: 'VALIDAT' })
|
|
}
|
|
await execSQL(`DELETE FROM invoices WHERE id = ?`, [invoiceId])
|
|
notifyTableChanged('invoices')
|
|
await syncEngine.addToQueue('invoices', invoiceId, 'DELETE', {})
|
|
}
|
|
|
|
async function searchNorme(query) {
|
|
if (!query || query.length < 2) return []
|
|
const like = `%${query}%`
|
|
return execSQL(
|
|
`SELECT n.*, a.denumire as ansamblu_denumire
|
|
FROM catalog_norme n
|
|
LEFT JOIN catalog_ansamble a ON n.ansamblu_id = a.id
|
|
WHERE n.tenant_id = ? AND (n.denumire LIKE ? OR n.cod LIKE ?)
|
|
ORDER BY n.denumire LIMIT 10`,
|
|
[auth.tenantId, like, like]
|
|
)
|
|
}
|
|
|
|
async function searchPreturi(query) {
|
|
if (!query || query.length < 2) return []
|
|
const like = `%${query}%`
|
|
return execSQL(
|
|
`SELECT * FROM catalog_preturi WHERE tenant_id = ? AND denumire LIKE ?
|
|
ORDER BY denumire LIMIT 10`,
|
|
[auth.tenantId, like]
|
|
)
|
|
}
|
|
|
|
async function getStats() {
|
|
const [total] = await execSQL(
|
|
`SELECT COUNT(*) as cnt FROM orders WHERE tenant_id = ?`, [auth.tenantId]
|
|
)
|
|
const [draft] = await execSQL(
|
|
`SELECT COUNT(*) as cnt FROM orders WHERE tenant_id = ? AND status = 'DRAFT'`, [auth.tenantId]
|
|
)
|
|
const [validat] = await execSQL(
|
|
`SELECT COUNT(*) as cnt FROM orders WHERE tenant_id = ? AND status = 'VALIDAT'`, [auth.tenantId]
|
|
)
|
|
const [facturat] = await execSQL(
|
|
`SELECT COUNT(*) as cnt FROM orders WHERE tenant_id = ? AND status = 'FACTURAT'`, [auth.tenantId]
|
|
)
|
|
const [totalVehicles] = await execSQL(
|
|
`SELECT COUNT(*) as cnt FROM vehicles WHERE tenant_id = ?`, [auth.tenantId]
|
|
)
|
|
const [revenue] = await execSQL(
|
|
`SELECT COALESCE(SUM(total_general), 0) as s FROM orders WHERE tenant_id = ? AND status IN ('VALIDAT', 'FACTURAT')`, [auth.tenantId]
|
|
)
|
|
return {
|
|
totalOrders: total?.cnt || 0,
|
|
draftOrders: draft?.cnt || 0,
|
|
validatedOrders: validat?.cnt || 0,
|
|
facturatedOrders: facturat?.cnt || 0,
|
|
totalVehicles: totalVehicles?.cnt || 0,
|
|
totalRevenue: revenue?.s || 0,
|
|
}
|
|
}
|
|
|
|
return {
|
|
getAll, getById, getLines, getRecentOrders, create, addLine, removeLine,
|
|
updateHeader, validateOrder, devalidateOrder, deleteOrder, deleteInvoice,
|
|
searchNorme, searchPreturi, getStats
|
|
}
|
|
})
|