-- 生产领料单表（扩展字段以支持完整数据）
CREATE TABLE [dbo].[erp_material_receive](
    [id] [bigint] IDENTITY(1,1) NOT NULL,
    -- 基础字段
    [so_code] [nvarchar](100) NULL,
    [fbillno] [nvarchar](100) NULL,
    [inv_code] [nvarchar](100) NULL,
    [inv_name] [nvarchar](200) NULL,
    [inv_std] [nvarchar](200) NULL,
    [qty] [decimal](18, 2) NULL,
    [fqty] [decimal](18, 2) NULL,
    [fauxqty] [decimal](18, 2) NULL,
    [unit] [nvarchar](50) NULL,
    [funitid] [int] NULL,
    [dep_name] [nvarchar](100) NULL,
    [dep_code] [nvarchar](50) NULL,
    [dep_id] [int] NULL,
    [fworkshop] [int] NULL,
    [wh_id] [int] NULL,
    [wh_name] [nvarchar](100) NULL,
    [fplancommitdate] [datetime] NULL,
    [fplanfinishdate] [datetime] NULL,
    [finterid] [int] NULL,
    [fitemid] [int] NULL,
    -- 出库相关字段
    [out_qty] [decimal](18, 2) NULL DEFAULT 0,
    [remain_qty] [decimal](18, 2) NULL DEFAULT 0,
    [is_all_out] [int] NULL DEFAULT 0,
    -- 同步相关字段
    [sync_time] [datetime] NULL,
    [create_time] [datetime] NULL DEFAULT GETDATE(),
    [update_time] [datetime] NULL DEFAULT GETDATE(),
    -- 扩展字段（用于存储更多ERP字段，JSON格式）
    [ext_data] [nvarchar](max) NULL,
    CONSTRAINT [PK_erp_material_receive] PRIMARY KEY CLUSTERED ([id] ASC)
)

-- 创建索引
CREATE INDEX [IX_erp_material_receive_so_code] ON [dbo].[erp_material_receive] ([so_code])
CREATE INDEX [IX_erp_material_receive_inv_code] ON [dbo].[erp_material_receive] ([inv_code])
CREATE INDEX [IX_erp_material_receive_finterid] ON [dbo].[erp_material_receive] ([finterid])
CREATE INDEX [IX_erp_material_receive_sync] ON [dbo].[erp_material_receive] ([so_code], [inv_code], [finterid])
