Files
SupplierSystem/migrations/20260743_sales_invoice_export.sql
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

92 lines
4.0 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 销项开票导出:商品税收编码/税率 + 订单开票状态
ALTER TABLE product_catalog ADD COLUMN IF NOT EXISTS tax_code VARCHAR(30);
ALTER TABLE product_catalog ADD COLUMN IF NOT EXISTS tax_rate DECIMAL(8,4);
ALTER TABLE skus ADD COLUMN IF NOT EXISTS tax_code VARCHAR(30);
ALTER TABLE skus ADD COLUMN IF NOT EXISTS tax_rate DECIMAL(8,4);
COMMENT ON COLUMN product_catalog.tax_code IS '增值税税收分类编码(税务局批量开票)';
COMMENT ON COLUMN product_catalog.tax_rate IS '税率小数:0=免税,0.09=9%,0.13=13%';
COMMENT ON COLUMN skus.tax_code IS '增值税税收分类编码';
COMMENT ON COLUMN skus.tax_rate IS '税率小数';
ALTER TABLE orders ADD COLUMN IF NOT EXISTS sales_invoice_status VARCHAR(20) DEFAULT 'pending';
ALTER TABLE orders ADD COLUMN IF NOT EXISTS sales_invoice_no VARCHAR(80);
ALTER TABLE orders ADD COLUMN IF NOT EXISTS sales_invoiced_at TIMESTAMPTZ;
COMMENT ON COLUMN orders.sales_invoice_status IS '销项开票状态:pending=未开票 invoiced=已开票';
COMMENT ON COLUMN orders.sales_invoice_no IS '销项发票号码(登记用)';
COMMENT ON COLUMN orders.sales_invoiced_at IS '销项开票登记时间';
CREATE INDEX IF NOT EXISTS idx_orders_sales_invoice
ON orders (customer_code, delivery_date, sales_invoice_status);
-- 同步 product_catalog → skus 时带上税收字段
CREATE OR REPLACE FUNCTION sync_product_catalog_to_legacy()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'DELETE' THEN
IF OLD.item_type = 'category' THEN
DELETE FROM categories
WHERE category_code = OLD.code
AND branch_code = COALESCE(OLD.branch_code, (
SELECT branch_code FROM branches WHERE status = 1 ORDER BY branch_code LIMIT 1
));
ELSE
DELETE FROM skus WHERE sku_code = OLD.code;
END IF;
RETURN OLD;
END IF;
IF NEW.item_type = 'category' THEN
INSERT INTO categories (
branch_code, category_code, parent_code, category_name, level, sort_order,
loss_threshold, is_fresh, status, created_at
) VALUES (
COALESCE(NEW.branch_code, (SELECT branch_code FROM branches WHERE status = 1 ORDER BY branch_code LIMIT 1)),
NEW.code, NEW.parent_code, NEW.name, COALESCE(NEW.level, 1), COALESCE(NEW.sort_order, 0),
COALESCE(NEW.loss_threshold, 5.00), COALESCE(NEW.is_fresh, true), COALESCE(NEW.status, 1),
COALESCE(NEW.created_at, NOW())
)
ON CONFLICT (branch_code, category_code) DO UPDATE SET
parent_code = EXCLUDED.parent_code,
category_name = EXCLUDED.category_name,
level = EXCLUDED.level,
sort_order = EXCLUDED.sort_order,
loss_threshold = EXCLUDED.loss_threshold,
is_fresh = EXCLUDED.is_fresh,
status = EXCLUDED.status;
ELSE
INSERT INTO skus (
sku_code, sku_name, category_code, unit, spec, barcode, image_url,
image_data, image_mime_type, is_prepackaged, tax_code, tax_rate,
default_loss_threshold, sale_price, cost_price, status, created_at
) VALUES (
NEW.code, NEW.name, NEW.parent_code, COALESCE(NEW.unit, '斤'), NEW.spec, NEW.barcode, NEW.image_url,
NEW.image_data, NEW.image_mime_type, COALESCE(NEW.is_prepackaged, false),
NULLIF(TRIM(NEW.tax_code), ''), NEW.tax_rate,
NEW.default_loss_threshold, COALESCE(NEW.sale_price, 0), COALESCE(NEW.cost_price, 0),
COALESCE(NEW.status, 1), COALESCE(NEW.created_at, NOW())
)
ON CONFLICT (sku_code) DO UPDATE SET
sku_name = EXCLUDED.sku_name,
category_code = EXCLUDED.category_code,
unit = EXCLUDED.unit,
spec = EXCLUDED.spec,
barcode = EXCLUDED.barcode,
image_url = EXCLUDED.image_url,
image_data = EXCLUDED.image_data,
image_mime_type = EXCLUDED.image_mime_type,
is_prepackaged = EXCLUDED.is_prepackaged,
tax_code = EXCLUDED.tax_code,
tax_rate = EXCLUDED.tax_rate,
default_loss_threshold = EXCLUDED.default_loss_threshold,
sale_price = EXCLUDED.sale_price,
cost_price = EXCLUDED.cost_price,
status = EXCLUDED.status;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;