165 lines
5.2 KiB
TypeScript
165 lines
5.2 KiB
TypeScript
"use client"
|
|
|
|
import { useState, useEffect } from "react"
|
|
import type { Supplier } from "@/types/business"
|
|
|
|
// Mock suppliers data
|
|
const mockSuppliers: Supplier[] = [
|
|
{
|
|
id: "1",
|
|
name: "Electronics Wholesale Co",
|
|
email: "orders@electronicswholesale.com",
|
|
phone: "(555) 111-2222",
|
|
address: "100 Industrial Blvd, Manufacturing District, CA 90210",
|
|
paymentTerms: "Net 30",
|
|
amountOwed: 8500,
|
|
contactPerson: "David Wilson",
|
|
businessType: "Electronics Manufacturer",
|
|
notes: "Primary supplier for electronic components",
|
|
taxId: "12-3456789",
|
|
createdAt: "2024-01-01",
|
|
},
|
|
{
|
|
id: "2",
|
|
name: "Global Tech Supplies",
|
|
email: "billing@globaltechsupplies.com",
|
|
phone: "(555) 333-4444",
|
|
address: "250 Tech Park Ave, Innovation City, TX 75001",
|
|
paymentTerms: "Net 15",
|
|
amountOwed: 3200,
|
|
contactPerson: "Maria Rodriguez",
|
|
businessType: "Technology Distributor",
|
|
notes: "Fast shipping, good quality products",
|
|
taxId: "98-7654321",
|
|
createdAt: "2024-01-01",
|
|
},
|
|
{
|
|
id: "3",
|
|
name: "Premium Components Ltd",
|
|
email: "accounts@premiumcomponents.com",
|
|
phone: "(555) 555-6666",
|
|
address: "500 Component Way, Quality Town, NY 10001",
|
|
paymentTerms: "Net 45",
|
|
amountOwed: 0,
|
|
contactPerson: "James Thompson",
|
|
businessType: "Component Manufacturer",
|
|
notes: "High-end components, premium pricing",
|
|
taxId: "55-1122334",
|
|
createdAt: "2024-01-01",
|
|
},
|
|
{
|
|
id: "4",
|
|
name: "Budget Electronics Supply",
|
|
email: "info@budgetelectronics.com",
|
|
phone: "(555) 777-8888",
|
|
address: "75 Discount Drive, Value City, FL 33101",
|
|
paymentTerms: "COD",
|
|
amountOwed: 1250,
|
|
contactPerson: "Susan Lee",
|
|
businessType: "Discount Supplier",
|
|
notes: "Good for budget-friendly options",
|
|
taxId: "44-9988776",
|
|
createdAt: "2024-01-01",
|
|
},
|
|
]
|
|
|
|
export function useSuppliers() {
|
|
const [suppliers, setSuppliers] = useState<Supplier[]>([])
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
useEffect(() => {
|
|
// Simulate loading from localStorage or API
|
|
const savedSuppliers = localStorage.getItem("wholesale-suppliers")
|
|
if (savedSuppliers) {
|
|
setSuppliers(JSON.parse(savedSuppliers))
|
|
} else {
|
|
setSuppliers(mockSuppliers)
|
|
localStorage.setItem("wholesale-suppliers", JSON.stringify(mockSuppliers))
|
|
}
|
|
setLoading(false)
|
|
}, [])
|
|
|
|
const addSupplier = (supplierData: Omit<Supplier, "id" | "createdAt">) => {
|
|
const newSupplier: Supplier = {
|
|
...supplierData,
|
|
id: Date.now().toString(),
|
|
createdAt: new Date().toISOString(),
|
|
}
|
|
|
|
const updatedSuppliers = [...suppliers, newSupplier]
|
|
setSuppliers(updatedSuppliers)
|
|
localStorage.setItem("wholesale-suppliers", JSON.stringify(updatedSuppliers))
|
|
}
|
|
|
|
const updateSupplier = (supplierId: string, supplierData: Omit<Supplier, "id" | "createdAt">) => {
|
|
const updatedSuppliers = suppliers.map((supplier) =>
|
|
supplier.id === supplierId ? { ...supplier, ...supplierData } : supplier,
|
|
)
|
|
setSuppliers(updatedSuppliers)
|
|
localStorage.setItem("wholesale-suppliers", JSON.stringify(updatedSuppliers))
|
|
}
|
|
|
|
const deleteSupplier = (supplierId: string) => {
|
|
const updatedSuppliers = suppliers.filter((supplier) => supplier.id !== supplierId)
|
|
setSuppliers(updatedSuppliers)
|
|
localStorage.setItem("wholesale-suppliers", JSON.stringify(updatedSuppliers))
|
|
}
|
|
|
|
const addPurchase = (
|
|
supplierId: string,
|
|
amount: number,
|
|
description: string,
|
|
date: string,
|
|
invoiceNumber: string,
|
|
) => {
|
|
const updatedSuppliers = suppliers.map((supplier) =>
|
|
supplier.id === supplierId ? { ...supplier, amountOwed: supplier.amountOwed + amount } : supplier,
|
|
)
|
|
setSuppliers(updatedSuppliers)
|
|
localStorage.setItem("wholesale-suppliers", JSON.stringify(updatedSuppliers))
|
|
|
|
// In a real app, you'd also save the purchase record to a purchases table
|
|
console.log(
|
|
`Purchase recorded: $${amount} from supplier ${supplierId} on ${date}. Invoice: ${invoiceNumber}. Description: ${description}`,
|
|
)
|
|
}
|
|
|
|
const recordPayment = (supplierId: string, amount: number, notes: string, date: string, paymentMethod: string) => {
|
|
const updatedSuppliers = suppliers.map((supplier) =>
|
|
supplier.id === supplierId ? { ...supplier, amountOwed: Math.max(0, supplier.amountOwed - amount) } : supplier,
|
|
)
|
|
setSuppliers(updatedSuppliers)
|
|
localStorage.setItem("wholesale-suppliers", JSON.stringify(updatedSuppliers))
|
|
|
|
// In a real app, you'd also save the payment record to a payments table
|
|
console.log(
|
|
`Payment recorded: $${amount} to supplier ${supplierId} on ${date}. Method: ${paymentMethod}. Notes: ${notes}`,
|
|
)
|
|
}
|
|
|
|
const getSupplierById = (supplierId: string) => {
|
|
return suppliers.find((supplier) => supplier.id === supplierId)
|
|
}
|
|
|
|
const getSuppliersWithOutstandingBalance = () => {
|
|
return suppliers.filter((supplier) => supplier.amountOwed > 0)
|
|
}
|
|
|
|
const getTotalAmountOwed = () => {
|
|
return suppliers.reduce((total, supplier) => total + supplier.amountOwed, 0)
|
|
}
|
|
|
|
return {
|
|
suppliers,
|
|
loading,
|
|
addSupplier,
|
|
updateSupplier,
|
|
deleteSupplier,
|
|
addPurchase,
|
|
recordPayment,
|
|
getSupplierById,
|
|
getSuppliersWithOutstandingBalance,
|
|
getTotalAmountOwed,
|
|
}
|
|
}
|