-- ============================================ -- 应付账款功能 - 创建应付账款表 -- ============================================ -- 应付账款表 CREATE TABLE public.accounts_payable ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), plan_id UUID NOT NULL REFERENCES public.production_plans(id) ON DELETE CASCADE, purchaser_id UUID NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE, textile_factory_id UUID NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE, -- 金额信息 total_amount DECIMAL NOT NULL DEFAULT 0, paid_amount DECIMAL NOT NULL DEFAULT 0, unpaid_amount DECIMAL NOT NULL DEFAULT 0, -- 数量信息 total_quantity DECIMAL NOT NULL DEFAULT 0, paid_quantity DECIMAL NOT NULL DEFAULT 0, unpaid_quantity DECIMAL NOT NULL DEFAULT 0, -- 单价(元/米) price_per_meter DECIMAL NOT NULL DEFAULT 0, -- 状态 status TEXT NOT NULL DEFAULT 'unpaid', -- 时间 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); -- 应付账款明细表(记录每次入库产生的应付记录) CREATE TABLE public.accounts_payable_items ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), accounts_payable_id UUID NOT NULL REFERENCES public.accounts_payable(id) ON DELETE CASCADE, inventory_record_id UUID REFERENCES public.inventory_records(id) ON DELETE SET NULL, -- 入库信息 quantity DECIMAL NOT NULL DEFAULT 0, rolls INTEGER DEFAULT 0, price_per_meter DECIMAL NOT NULL DEFAULT 0, amount DECIMAL NOT NULL DEFAULT 0, -- 状态 status TEXT NOT NULL DEFAULT 'unpaid', -- 关联的付款记录 payment_id UUID REFERENCES public.payments(id) ON DELETE SET NULL, -- 时间 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), paid_at TIMESTAMPTZ ); -- 添加索引 CREATE INDEX idx_accounts_payable_plan ON public.accounts_payable(plan_id); CREATE INDEX idx_accounts_payable_purchaser ON public.accounts_payable(purchaser_id); CREATE INDEX idx_accounts_payable_factory ON public.accounts_payable(textile_factory_id); CREATE INDEX idx_accounts_payable_status ON public.accounts_payable(status); CREATE INDEX idx_accounts_payable_items_ap ON public.accounts_payable_items(accounts_payable_id); CREATE INDEX idx_accounts_payable_items_status ON public.accounts_payable_items(status);