import { sql } from "drizzle-orm"; import { pgTable, text, varchar, integer, timestamp, json, decimal, boolean } from "drizzle-orm/pg-core"; import { createInsertSchema } from "drizzle-zod"; import { z } from "zod"; export const products = pgTable("products", { id: varchar("id").primaryKey().default(sql`gen_random_uuid()`), sku: text("sku").notNull().unique(), name: text("name").notNull(), description: text("description"), currentStock: integer("current_stock").notNull().default(0), minThreshold: integer("min_threshold").notNull().default(0), unit: text("unit").notNull().default("units"), price: decimal("price", { precision: 10, scale: 2 }), createdAt: timestamp("created_at").defaultNow(), updatedAt: timestamp("updated_at").defaultNow(), }); export const stockMovements = pgTable("stock_movements", { id: varchar("id").primaryKey().default(sql`gen_random_uuid()`), productId: varchar("product_id").notNull(), type: text("type").notNull(), // 'in' | 'out' | 'adjustment' quantity: integer("quantity").notNull(), reason: text("reason"), createdAt: timestamp("created_at").defaultNow(), }); export const documents = pgTable("documents", { id: varchar("id").primaryKey().default(sql`gen_random_uuid()`), type: text("type").notNull(), // 'delivery_note' | 'packing_list' | 'shipping_label' | 'goods_receipt' | 'stock_report' | 'dispatch_note' documentNumber: text("document_number").notNull().unique(), title: text("title").notNull(), content: json("content"), status: text("status").notNull().default("draft"), // 'draft' | 'finalized' | 'printed' createdAt: timestamp("created_at").defaultNow(), updatedAt: timestamp("updated_at").defaultNow(), }); export const orders = pgTable("orders", { id: varchar("id").primaryKey().default(sql`gen_random_uuid()`), orderNumber: text("order_number").notNull().unique(), customerName: text("customer_name").notNull(), customerAddress: text("customer_address"), status: text("status").notNull().default("pending"), // 'pending' | 'processing' | 'shipped' | 'delivered' items: json("items"), // Array of {productId, quantity, price} totalAmount: decimal("total_amount", { precision: 10, scale: 2 }), createdAt: timestamp("created_at").defaultNow(), updatedAt: timestamp("updated_at").defaultNow(), }); export const notifications = pgTable("notifications", { id: varchar("id").primaryKey().default(sql`gen_random_uuid()`), type: text("type").notNull(), // 'low_stock' | 'out_of_stock' | 'stock_movement' | 'document_created' title: text("title").notNull(), message: text("message").notNull(), isRead: boolean("is_read").notNull().default(false), relatedId: varchar("related_id"), // Product or document ID createdAt: timestamp("created_at").defaultNow(), }); // Insert schemas export const insertProductSchema = createInsertSchema(products).omit({ id: true, createdAt: true, updatedAt: true, }); export const insertStockMovementSchema = createInsertSchema(stockMovements).omit({ id: true, createdAt: true, }); export const insertDocumentSchema = createInsertSchema(documents).omit({ id: true, createdAt: true, updatedAt: true, }); export const insertOrderSchema = createInsertSchema(orders).omit({ id: true, createdAt: true, updatedAt: true, }); export const insertNotificationSchema = createInsertSchema(notifications).omit({ id: true, createdAt: true, }); // Types export type Product = typeof products.$inferSelect; export type InsertProduct = z.infer; export type StockMovement = typeof stockMovements.$inferSelect; export type InsertStockMovement = z.infer; export type Document = typeof documents.$inferSelect; export type InsertDocument = z.infer; export type Order = typeof orders.$inferSelect; export type InsertOrder = z.infer; export type Notification = typeof notifications.$inferSelect; export type InsertNotification = z.infer;