Files
CMT/frontend/hooks/use-clients.ts
T
2025-08-16 14:41:12 +02:00

148 lines
4.4 KiB
TypeScript

"use client"
import { useState, useEffect } from "react"
import type { Client } from "@/types/business"
// Mock clients data
const mockClients: Client[] = [
{
id: "1",
name: "ABC Electronics",
email: "orders@abcelectronics.com",
phone: "(555) 123-4567",
address: "123 Tech Street, Silicon Valley, CA 94000",
creditLimit: 10000,
outstandingAmount: 4500,
paymentTerms: "Net 30",
contactPerson: "John Smith",
businessType: "Electronics Retailer",
notes: "Large volume customer, always pays on time",
createdAt: "2024-01-01",
},
{
id: "2",
name: "Tech Solutions Inc",
email: "billing@techsolutions.com",
phone: "(555) 987-6543",
address: "456 Business Ave, Downtown, NY 10001",
creditLimit: 15000,
outstandingAmount: 2800,
paymentTerms: "Net 15",
contactPerson: "Sarah Johnson",
businessType: "IT Services",
notes: "Prefers email communication",
createdAt: "2024-01-01",
},
{
id: "3",
name: "Mobile World",
email: "contact@mobileworld.com",
phone: "(555) 456-7890",
address: "789 Mobile Plaza, Austin, TX 78701",
creditLimit: 8000,
outstandingAmount: 0,
paymentTerms: "Net 30",
contactPerson: "Mike Chen",
businessType: "Mobile Phone Store",
notes: "New customer, good payment history so far",
createdAt: "2024-01-01",
},
{
id: "4",
name: "Digital Store",
email: "admin@digitalstore.com",
phone: "(555) 321-0987",
address: "321 Digital Way, Seattle, WA 98101",
creditLimit: 5000,
outstandingAmount: 5200,
paymentTerms: "Net 45",
contactPerson: "Lisa Wong",
businessType: "Online Retailer",
notes: "Currently over credit limit - monitor closely",
createdAt: "2024-01-01",
},
]
export function useClients() {
const [clients, setClients] = useState<Client[]>([])
const [loading, setLoading] = useState(true)
useEffect(() => {
// Simulate loading from localStorage or API
const savedClients = localStorage.getItem("wholesale-clients")
if (savedClients) {
setClients(JSON.parse(savedClients))
} else {
setClients(mockClients)
localStorage.setItem("wholesale-clients", JSON.stringify(mockClients))
}
setLoading(false)
}, [])
const addClient = (clientData: Omit<Client, "id" | "createdAt">) => {
const newClient: Client = {
...clientData,
id: Date.now().toString(),
createdAt: new Date().toISOString(),
}
const updatedClients = [...clients, newClient]
setClients(updatedClients)
localStorage.setItem("wholesale-clients", JSON.stringify(updatedClients))
}
const updateClient = (clientId: string, clientData: Omit<Client, "id" | "createdAt">) => {
const updatedClients = clients.map((client) => (client.id === clientId ? { ...client, ...clientData } : client))
setClients(updatedClients)
localStorage.setItem("wholesale-clients", JSON.stringify(updatedClients))
}
const deleteClient = (clientId: string) => {
const updatedClients = clients.filter((client) => client.id !== clientId)
setClients(updatedClients)
localStorage.setItem("wholesale-clients", JSON.stringify(updatedClients))
}
const addPayment = (clientId: string, amount: number, notes: string, date: string) => {
const updatedClients = clients.map((client) =>
client.id === clientId
? { ...client, outstandingAmount: Math.max(0, client.outstandingAmount - amount) }
: client,
)
setClients(updatedClients)
localStorage.setItem("wholesale-clients", JSON.stringify(updatedClients))
// In a real app, you'd also save the payment record to a payments table
console.log(`Payment recorded: $${amount} from client ${clientId} on ${date}. Notes: ${notes}`)
}
const getClientById = (clientId: string) => {
return clients.find((client) => client.id === clientId)
}
const getClientsWithOutstandingCredit = () => {
return clients.filter((client) => client.outstandingAmount > 0)
}
const getClientsOverCreditLimit = () => {
return clients.filter((client) => client.outstandingAmount > client.creditLimit)
}
const getTotalOutstandingAmount = () => {
return clients.reduce((total, client) => total + client.outstandingAmount, 0)
}
return {
clients,
loading,
addClient,
updateClient,
deleteClient,
addPayment,
getClientById,
getClientsWithOutstandingCredit,
getClientsOverCreditLimit,
getTotalOutstandingAmount,
}
}