-- 委外加工出库单表（扩展字段以支持完整数据）
CREATE TABLE [dbo].[erp_material_ww_out](
    [id] [bigint] IDENTITY(1,1) NOT NULL,
    -- 基础字段
    [record_id] [nvarchar](100) NULL,
    [date_start] [datetime] NULL,
    [date_end] [datetime] NULL,
    [ven_id] [int] NULL,
    [ven_name] [nvarchar](200) NULL,
    [inv_code] [nvarchar](100) NULL,
    [inv_name] [nvarchar](200) NULL,
    [inv_std] [nvarchar](200) NULL,
    [unit] [nvarchar](50) NULL,
    [funitid] [int] NULL,
    [fitemid] [int] NULL,
    [wh_id] [int] NULL,
    [wh_name] [nvarchar](100) NULL,
    [qty] [decimal](18, 4) NULL,
    [fqty] [decimal](18, 4) NULL,
    [qqty] [decimal](18, 4) NULL,
    [yqty] [decimal](18, 4) NULL,
    [wqty] [decimal](18, 4) NULL,
    -- 出库相关字段
    [out_qty] [decimal](18, 4) NULL DEFAULT 0,
    [remain_qty] [decimal](18, 4) NULL DEFAULT 0,
    [is_all_out] [int] NULL DEFAULT 0,
    -- 同步相关字段
    [iz_sync] [nvarchar](10) NULL,
    [iz_print] [nvarchar](10) NULL,
    [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_ww_out] PRIMARY KEY CLUSTERED ([id] ASC)
)

-- 创建索引
CREATE INDEX [IX_erp_material_ww_out_record_id] ON [dbo].[erp_material_ww_out] ([record_id])
CREATE INDEX [IX_erp_material_ww_out_inv_code] ON [dbo].[erp_material_ww_out] ([inv_code])
CREATE INDEX [IX_erp_material_ww_out_ven_id] ON [dbo].[erp_material_ww_out] ([ven_id])
CREATE INDEX [IX_erp_material_ww_out_sync] ON [dbo].[erp_material_ww_out] ([record_id], [inv_code])
